Make more use of VECTOR_CST_ENCODED_ELT
[gcc.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 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
86 /* Nonzero if we are folding constants inside an initializer; zero
87 otherwise. */
88 int folding_initializer = 0;
89
90 /* The following constants represent a bit based encoding of GCC's
91 comparison operators. This encoding simplifies transformations
92 on relational comparison operators, such as AND and OR. */
93 enum comparison_code {
94 COMPCODE_FALSE = 0,
95 COMPCODE_LT = 1,
96 COMPCODE_EQ = 2,
97 COMPCODE_LE = 3,
98 COMPCODE_GT = 4,
99 COMPCODE_LTGT = 5,
100 COMPCODE_GE = 6,
101 COMPCODE_ORD = 7,
102 COMPCODE_UNORD = 8,
103 COMPCODE_UNLT = 9,
104 COMPCODE_UNEQ = 10,
105 COMPCODE_UNLE = 11,
106 COMPCODE_UNGT = 12,
107 COMPCODE_NE = 13,
108 COMPCODE_UNGE = 14,
109 COMPCODE_TRUE = 15
110 };
111
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
115 static enum comparison_code comparison_to_compcode (enum tree_code);
116 static enum tree_code compcode_to_comparison (enum comparison_code);
117 static int twoval_comparison_p (tree, tree *, tree *, int *);
118 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
119 static tree optimize_bit_field_compare (location_t, enum tree_code,
120 tree, tree, tree);
121 static int simple_operand_p (const_tree);
122 static bool simple_operand_p_2 (tree);
123 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
124 static tree range_predecessor (tree);
125 static tree range_successor (tree);
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
130 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
131 static tree fold_binary_op_with_conditional_arg (location_t,
132 enum tree_code, tree,
133 tree, tree,
134 tree, tree, int);
135 static tree fold_negate_const (tree, tree);
136 static tree fold_not_const (const_tree, tree);
137 static tree fold_relational_const (enum tree_code, tree, tree, tree);
138 static tree fold_convert_const (enum tree_code, tree, tree);
139 static tree fold_view_convert_expr (tree, tree);
140 static tree fold_negate_expr (location_t, tree);
141
142
143 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
144 Otherwise, return LOC. */
145
146 static location_t
147 expr_location_or (tree t, location_t loc)
148 {
149 location_t tloc = EXPR_LOCATION (t);
150 return tloc == UNKNOWN_LOCATION ? loc : tloc;
151 }
152
153 /* Similar to protected_set_expr_location, but never modify x in place,
154 if location can and needs to be set, unshare it. */
155
156 static inline tree
157 protected_set_expr_location_unshare (tree x, location_t loc)
158 {
159 if (CAN_HAVE_LOCATION_P (x)
160 && EXPR_LOCATION (x) != loc
161 && !(TREE_CODE (x) == SAVE_EXPR
162 || TREE_CODE (x) == TARGET_EXPR
163 || TREE_CODE (x) == BIND_EXPR))
164 {
165 x = copy_node (x);
166 SET_EXPR_LOCATION (x, loc);
167 }
168 return x;
169 }
170 \f
171 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
172 division and returns the quotient. Otherwise returns
173 NULL_TREE. */
174
175 tree
176 div_if_zero_remainder (const_tree arg1, const_tree arg2)
177 {
178 widest_int quo;
179
180 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
181 SIGNED, &quo))
182 return wide_int_to_tree (TREE_TYPE (arg1), quo);
183
184 return NULL_TREE;
185 }
186 \f
187 /* This is nonzero if we should defer warnings about undefined
188 overflow. This facility exists because these warnings are a
189 special case. The code to estimate loop iterations does not want
190 to issue any warnings, since it works with expressions which do not
191 occur in user code. Various bits of cleanup code call fold(), but
192 only use the result if it has certain characteristics (e.g., is a
193 constant); that code only wants to issue a warning if the result is
194 used. */
195
196 static int fold_deferring_overflow_warnings;
197
198 /* If a warning about undefined overflow is deferred, this is the
199 warning. Note that this may cause us to turn two warnings into
200 one, but that is fine since it is sufficient to only give one
201 warning per expression. */
202
203 static const char* fold_deferred_overflow_warning;
204
205 /* If a warning about undefined overflow is deferred, this is the
206 level at which the warning should be emitted. */
207
208 static enum warn_strict_overflow_code fold_deferred_overflow_code;
209
210 /* Start deferring overflow warnings. We could use a stack here to
211 permit nested calls, but at present it is not necessary. */
212
213 void
214 fold_defer_overflow_warnings (void)
215 {
216 ++fold_deferring_overflow_warnings;
217 }
218
219 /* Stop deferring overflow warnings. If there is a pending warning,
220 and ISSUE is true, then issue the warning if appropriate. STMT is
221 the statement with which the warning should be associated (used for
222 location information); STMT may be NULL. CODE is the level of the
223 warning--a warn_strict_overflow_code value. This function will use
224 the smaller of CODE and the deferred code when deciding whether to
225 issue the warning. CODE may be zero to mean to always use the
226 deferred code. */
227
228 void
229 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
230 {
231 const char *warnmsg;
232 location_t locus;
233
234 gcc_assert (fold_deferring_overflow_warnings > 0);
235 --fold_deferring_overflow_warnings;
236 if (fold_deferring_overflow_warnings > 0)
237 {
238 if (fold_deferred_overflow_warning != NULL
239 && code != 0
240 && code < (int) fold_deferred_overflow_code)
241 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
242 return;
243 }
244
245 warnmsg = fold_deferred_overflow_warning;
246 fold_deferred_overflow_warning = NULL;
247
248 if (!issue || warnmsg == NULL)
249 return;
250
251 if (gimple_no_warning_p (stmt))
252 return;
253
254 /* Use the smallest code level when deciding to issue the
255 warning. */
256 if (code == 0 || code > (int) fold_deferred_overflow_code)
257 code = fold_deferred_overflow_code;
258
259 if (!issue_strict_overflow_warning (code))
260 return;
261
262 if (stmt == NULL)
263 locus = input_location;
264 else
265 locus = gimple_location (stmt);
266 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
267 }
268
269 /* Stop deferring overflow warnings, ignoring any deferred
270 warnings. */
271
272 void
273 fold_undefer_and_ignore_overflow_warnings (void)
274 {
275 fold_undefer_overflow_warnings (false, NULL, 0);
276 }
277
278 /* Whether we are deferring overflow warnings. */
279
280 bool
281 fold_deferring_overflow_warnings_p (void)
282 {
283 return fold_deferring_overflow_warnings > 0;
284 }
285
286 /* This is called when we fold something based on the fact that signed
287 overflow is undefined. */
288
289 void
290 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
291 {
292 if (fold_deferring_overflow_warnings > 0)
293 {
294 if (fold_deferred_overflow_warning == NULL
295 || wc < fold_deferred_overflow_code)
296 {
297 fold_deferred_overflow_warning = gmsgid;
298 fold_deferred_overflow_code = wc;
299 }
300 }
301 else if (issue_strict_overflow_warning (wc))
302 warning (OPT_Wstrict_overflow, gmsgid);
303 }
304 \f
305 /* Return true if the built-in mathematical function specified by CODE
306 is odd, i.e. -f(x) == f(-x). */
307
308 bool
309 negate_mathfn_p (combined_fn fn)
310 {
311 switch (fn)
312 {
313 CASE_CFN_ASIN:
314 CASE_CFN_ASINH:
315 CASE_CFN_ATAN:
316 CASE_CFN_ATANH:
317 CASE_CFN_CASIN:
318 CASE_CFN_CASINH:
319 CASE_CFN_CATAN:
320 CASE_CFN_CATANH:
321 CASE_CFN_CBRT:
322 CASE_CFN_CPROJ:
323 CASE_CFN_CSIN:
324 CASE_CFN_CSINH:
325 CASE_CFN_CTAN:
326 CASE_CFN_CTANH:
327 CASE_CFN_ERF:
328 CASE_CFN_LLROUND:
329 CASE_CFN_LROUND:
330 CASE_CFN_ROUND:
331 CASE_CFN_SIN:
332 CASE_CFN_SINH:
333 CASE_CFN_TAN:
334 CASE_CFN_TANH:
335 CASE_CFN_TRUNC:
336 return true;
337
338 CASE_CFN_LLRINT:
339 CASE_CFN_LRINT:
340 CASE_CFN_NEARBYINT:
341 CASE_CFN_RINT:
342 return !flag_rounding_math;
343
344 default:
345 break;
346 }
347 return false;
348 }
349
350 /* Check whether we may negate an integer constant T without causing
351 overflow. */
352
353 bool
354 may_negate_without_overflow_p (const_tree t)
355 {
356 tree type;
357
358 gcc_assert (TREE_CODE (t) == INTEGER_CST);
359
360 type = TREE_TYPE (t);
361 if (TYPE_UNSIGNED (type))
362 return false;
363
364 return !wi::only_sign_bit_p (wi::to_wide (t));
365 }
366
367 /* Determine whether an expression T can be cheaply negated using
368 the function negate_expr without introducing undefined overflow. */
369
370 static bool
371 negate_expr_p (tree t)
372 {
373 tree type;
374
375 if (t == 0)
376 return false;
377
378 type = TREE_TYPE (t);
379
380 STRIP_SIGN_NOPS (t);
381 switch (TREE_CODE (t))
382 {
383 case INTEGER_CST:
384 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
385 return true;
386
387 /* Check that -CST will not overflow type. */
388 return may_negate_without_overflow_p (t);
389 case BIT_NOT_EXPR:
390 return (INTEGRAL_TYPE_P (type)
391 && TYPE_OVERFLOW_WRAPS (type));
392
393 case FIXED_CST:
394 return true;
395
396 case NEGATE_EXPR:
397 return !TYPE_OVERFLOW_SANITIZED (type);
398
399 case REAL_CST:
400 /* We want to canonicalize to positive real constants. Pretend
401 that only negative ones can be easily negated. */
402 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
403
404 case COMPLEX_CST:
405 return negate_expr_p (TREE_REALPART (t))
406 && negate_expr_p (TREE_IMAGPART (t));
407
408 case VECTOR_CST:
409 {
410 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
411 return true;
412
413 /* Steps don't prevent negation. */
414 unsigned int count = vector_cst_encoded_nelts (t);
415 for (unsigned int i = 0; i < count; ++i)
416 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
417 return false;
418
419 return true;
420 }
421
422 case COMPLEX_EXPR:
423 return negate_expr_p (TREE_OPERAND (t, 0))
424 && negate_expr_p (TREE_OPERAND (t, 1));
425
426 case CONJ_EXPR:
427 return negate_expr_p (TREE_OPERAND (t, 0));
428
429 case PLUS_EXPR:
430 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
431 || HONOR_SIGNED_ZEROS (element_mode (type))
432 || (ANY_INTEGRAL_TYPE_P (type)
433 && ! TYPE_OVERFLOW_WRAPS (type)))
434 return false;
435 /* -(A + B) -> (-B) - A. */
436 if (negate_expr_p (TREE_OPERAND (t, 1)))
437 return true;
438 /* -(A + B) -> (-A) - B. */
439 return negate_expr_p (TREE_OPERAND (t, 0));
440
441 case MINUS_EXPR:
442 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
443 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
444 && !HONOR_SIGNED_ZEROS (element_mode (type))
445 && (! ANY_INTEGRAL_TYPE_P (type)
446 || TYPE_OVERFLOW_WRAPS (type));
447
448 case MULT_EXPR:
449 if (TYPE_UNSIGNED (type))
450 break;
451 /* INT_MIN/n * n doesn't overflow while negating one operand it does
452 if n is a (negative) power of two. */
453 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
454 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
455 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
456 && (wi::popcount
457 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
458 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
459 && (wi::popcount
460 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
461 break;
462
463 /* Fall through. */
464
465 case RDIV_EXPR:
466 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
467 return negate_expr_p (TREE_OPERAND (t, 1))
468 || negate_expr_p (TREE_OPERAND (t, 0));
469 break;
470
471 case TRUNC_DIV_EXPR:
472 case ROUND_DIV_EXPR:
473 case EXACT_DIV_EXPR:
474 if (TYPE_UNSIGNED (type))
475 break;
476 if (negate_expr_p (TREE_OPERAND (t, 0)))
477 return true;
478 /* In general we can't negate B in A / B, because if A is INT_MIN and
479 B is 1, we may turn this into INT_MIN / -1 which is undefined
480 and actually traps on some architectures. */
481 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
482 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
483 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
484 && ! integer_onep (TREE_OPERAND (t, 1))))
485 return negate_expr_p (TREE_OPERAND (t, 1));
486 break;
487
488 case NOP_EXPR:
489 /* Negate -((double)float) as (double)(-float). */
490 if (TREE_CODE (type) == REAL_TYPE)
491 {
492 tree tem = strip_float_extensions (t);
493 if (tem != t)
494 return negate_expr_p (tem);
495 }
496 break;
497
498 case CALL_EXPR:
499 /* Negate -f(x) as f(-x). */
500 if (negate_mathfn_p (get_call_combined_fn (t)))
501 return negate_expr_p (CALL_EXPR_ARG (t, 0));
502 break;
503
504 case RSHIFT_EXPR:
505 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
506 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
507 {
508 tree op1 = TREE_OPERAND (t, 1);
509 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
510 return true;
511 }
512 break;
513
514 default:
515 break;
516 }
517 return false;
518 }
519
520 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
521 simplification is possible.
522 If negate_expr_p would return true for T, NULL_TREE will never be
523 returned. */
524
525 static tree
526 fold_negate_expr_1 (location_t loc, tree t)
527 {
528 tree type = TREE_TYPE (t);
529 tree tem;
530
531 switch (TREE_CODE (t))
532 {
533 /* Convert - (~A) to A + 1. */
534 case BIT_NOT_EXPR:
535 if (INTEGRAL_TYPE_P (type))
536 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
537 build_one_cst (type));
538 break;
539
540 case INTEGER_CST:
541 tem = fold_negate_const (t, type);
542 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
543 || (ANY_INTEGRAL_TYPE_P (type)
544 && !TYPE_OVERFLOW_TRAPS (type)
545 && TYPE_OVERFLOW_WRAPS (type))
546 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
547 return tem;
548 break;
549
550 case REAL_CST:
551 tem = fold_negate_const (t, type);
552 return tem;
553
554 case FIXED_CST:
555 tem = fold_negate_const (t, type);
556 return tem;
557
558 case COMPLEX_CST:
559 {
560 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
561 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
562 if (rpart && ipart)
563 return build_complex (type, rpart, ipart);
564 }
565 break;
566
567 case VECTOR_CST:
568 {
569 tree_vector_builder elts;
570 elts.new_unary_operation (type, t, true);
571 unsigned int count = elts.encoded_nelts ();
572 for (unsigned int i = 0; i < count; ++i)
573 {
574 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 if (elt == NULL_TREE)
576 return NULL_TREE;
577 elts.quick_push (elt);
578 }
579
580 return elts.build ();
581 }
582
583 case COMPLEX_EXPR:
584 if (negate_expr_p (t))
585 return fold_build2_loc (loc, COMPLEX_EXPR, type,
586 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
587 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
588 break;
589
590 case CONJ_EXPR:
591 if (negate_expr_p (t))
592 return fold_build1_loc (loc, CONJ_EXPR, type,
593 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
594 break;
595
596 case NEGATE_EXPR:
597 if (!TYPE_OVERFLOW_SANITIZED (type))
598 return TREE_OPERAND (t, 0);
599 break;
600
601 case PLUS_EXPR:
602 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
603 && !HONOR_SIGNED_ZEROS (element_mode (type)))
604 {
605 /* -(A + B) -> (-B) - A. */
606 if (negate_expr_p (TREE_OPERAND (t, 1)))
607 {
608 tem = negate_expr (TREE_OPERAND (t, 1));
609 return fold_build2_loc (loc, MINUS_EXPR, type,
610 tem, TREE_OPERAND (t, 0));
611 }
612
613 /* -(A + B) -> (-A) - B. */
614 if (negate_expr_p (TREE_OPERAND (t, 0)))
615 {
616 tem = negate_expr (TREE_OPERAND (t, 0));
617 return fold_build2_loc (loc, MINUS_EXPR, type,
618 tem, TREE_OPERAND (t, 1));
619 }
620 }
621 break;
622
623 case MINUS_EXPR:
624 /* - (A - B) -> B - A */
625 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
626 && !HONOR_SIGNED_ZEROS (element_mode (type)))
627 return fold_build2_loc (loc, MINUS_EXPR, type,
628 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
629 break;
630
631 case MULT_EXPR:
632 if (TYPE_UNSIGNED (type))
633 break;
634
635 /* Fall through. */
636
637 case RDIV_EXPR:
638 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
639 {
640 tem = TREE_OPERAND (t, 1);
641 if (negate_expr_p (tem))
642 return fold_build2_loc (loc, TREE_CODE (t), type,
643 TREE_OPERAND (t, 0), negate_expr (tem));
644 tem = TREE_OPERAND (t, 0);
645 if (negate_expr_p (tem))
646 return fold_build2_loc (loc, TREE_CODE (t), type,
647 negate_expr (tem), TREE_OPERAND (t, 1));
648 }
649 break;
650
651 case TRUNC_DIV_EXPR:
652 case ROUND_DIV_EXPR:
653 case EXACT_DIV_EXPR:
654 if (TYPE_UNSIGNED (type))
655 break;
656 if (negate_expr_p (TREE_OPERAND (t, 0)))
657 return fold_build2_loc (loc, TREE_CODE (t), type,
658 negate_expr (TREE_OPERAND (t, 0)),
659 TREE_OPERAND (t, 1));
660 /* In general we can't negate B in A / B, because if A is INT_MIN and
661 B is 1, we may turn this into INT_MIN / -1 which is undefined
662 and actually traps on some architectures. */
663 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
664 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
665 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
666 && ! integer_onep (TREE_OPERAND (t, 1))))
667 && negate_expr_p (TREE_OPERAND (t, 1)))
668 return fold_build2_loc (loc, TREE_CODE (t), type,
669 TREE_OPERAND (t, 0),
670 negate_expr (TREE_OPERAND (t, 1)));
671 break;
672
673 case NOP_EXPR:
674 /* Convert -((double)float) into (double)(-float). */
675 if (TREE_CODE (type) == REAL_TYPE)
676 {
677 tem = strip_float_extensions (t);
678 if (tem != t && negate_expr_p (tem))
679 return fold_convert_loc (loc, type, negate_expr (tem));
680 }
681 break;
682
683 case CALL_EXPR:
684 /* Negate -f(x) as f(-x). */
685 if (negate_mathfn_p (get_call_combined_fn (t))
686 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
687 {
688 tree fndecl, arg;
689
690 fndecl = get_callee_fndecl (t);
691 arg = negate_expr (CALL_EXPR_ARG (t, 0));
692 return build_call_expr_loc (loc, fndecl, 1, arg);
693 }
694 break;
695
696 case RSHIFT_EXPR:
697 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
698 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
699 {
700 tree op1 = TREE_OPERAND (t, 1);
701 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
702 {
703 tree ntype = TYPE_UNSIGNED (type)
704 ? signed_type_for (type)
705 : unsigned_type_for (type);
706 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
707 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
708 return fold_convert_loc (loc, type, temp);
709 }
710 }
711 break;
712
713 default:
714 break;
715 }
716
717 return NULL_TREE;
718 }
719
720 /* A wrapper for fold_negate_expr_1. */
721
722 static tree
723 fold_negate_expr (location_t loc, tree t)
724 {
725 tree type = TREE_TYPE (t);
726 STRIP_SIGN_NOPS (t);
727 tree tem = fold_negate_expr_1 (loc, t);
728 if (tem == NULL_TREE)
729 return NULL_TREE;
730 return fold_convert_loc (loc, type, tem);
731 }
732
733 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
734 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
735 return NULL_TREE. */
736
737 static tree
738 negate_expr (tree t)
739 {
740 tree type, tem;
741 location_t loc;
742
743 if (t == NULL_TREE)
744 return NULL_TREE;
745
746 loc = EXPR_LOCATION (t);
747 type = TREE_TYPE (t);
748 STRIP_SIGN_NOPS (t);
749
750 tem = fold_negate_expr (loc, t);
751 if (!tem)
752 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
753 return fold_convert_loc (loc, type, tem);
754 }
755 \f
756 /* Split a tree IN into a constant, literal and variable parts that could be
757 combined with CODE to make IN. "constant" means an expression with
758 TREE_CONSTANT but that isn't an actual constant. CODE must be a
759 commutative arithmetic operation. Store the constant part into *CONP,
760 the literal in *LITP and return the variable part. If a part isn't
761 present, set it to null. If the tree does not decompose in this way,
762 return the entire tree as the variable part and the other parts as null.
763
764 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
765 case, we negate an operand that was subtracted. Except if it is a
766 literal for which we use *MINUS_LITP instead.
767
768 If NEGATE_P is true, we are negating all of IN, again except a literal
769 for which we use *MINUS_LITP instead. If a variable part is of pointer
770 type, it is negated after converting to TYPE. This prevents us from
771 generating illegal MINUS pointer expression. LOC is the location of
772 the converted variable part.
773
774 If IN is itself a literal or constant, return it as appropriate.
775
776 Note that we do not guarantee that any of the three values will be the
777 same type as IN, but they will have the same signedness and mode. */
778
779 static tree
780 split_tree (tree in, tree type, enum tree_code code,
781 tree *minus_varp, tree *conp, tree *minus_conp,
782 tree *litp, tree *minus_litp, int negate_p)
783 {
784 tree var = 0;
785 *minus_varp = 0;
786 *conp = 0;
787 *minus_conp = 0;
788 *litp = 0;
789 *minus_litp = 0;
790
791 /* Strip any conversions that don't change the machine mode or signedness. */
792 STRIP_SIGN_NOPS (in);
793
794 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
795 || TREE_CODE (in) == FIXED_CST)
796 *litp = in;
797 else if (TREE_CODE (in) == code
798 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
799 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
800 /* We can associate addition and subtraction together (even
801 though the C standard doesn't say so) for integers because
802 the value is not affected. For reals, the value might be
803 affected, so we can't. */
804 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
805 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
806 || (code == MINUS_EXPR
807 && (TREE_CODE (in) == PLUS_EXPR
808 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
809 {
810 tree op0 = TREE_OPERAND (in, 0);
811 tree op1 = TREE_OPERAND (in, 1);
812 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
813 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
814
815 /* First see if either of the operands is a literal, then a constant. */
816 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
817 || TREE_CODE (op0) == FIXED_CST)
818 *litp = op0, op0 = 0;
819 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
820 || TREE_CODE (op1) == FIXED_CST)
821 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
822
823 if (op0 != 0 && TREE_CONSTANT (op0))
824 *conp = op0, op0 = 0;
825 else if (op1 != 0 && TREE_CONSTANT (op1))
826 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
827
828 /* If we haven't dealt with either operand, this is not a case we can
829 decompose. Otherwise, VAR is either of the ones remaining, if any. */
830 if (op0 != 0 && op1 != 0)
831 var = in;
832 else if (op0 != 0)
833 var = op0;
834 else
835 var = op1, neg_var_p = neg1_p;
836
837 /* Now do any needed negations. */
838 if (neg_litp_p)
839 *minus_litp = *litp, *litp = 0;
840 if (neg_conp_p && *conp)
841 *minus_conp = *conp, *conp = 0;
842 if (neg_var_p && var)
843 *minus_varp = var, var = 0;
844 }
845 else if (TREE_CONSTANT (in))
846 *conp = in;
847 else if (TREE_CODE (in) == BIT_NOT_EXPR
848 && code == PLUS_EXPR)
849 {
850 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
851 when IN is constant. */
852 *litp = build_minus_one_cst (type);
853 *minus_varp = TREE_OPERAND (in, 0);
854 }
855 else
856 var = in;
857
858 if (negate_p)
859 {
860 if (*litp)
861 *minus_litp = *litp, *litp = 0;
862 else if (*minus_litp)
863 *litp = *minus_litp, *minus_litp = 0;
864 if (*conp)
865 *minus_conp = *conp, *conp = 0;
866 else if (*minus_conp)
867 *conp = *minus_conp, *minus_conp = 0;
868 if (var)
869 *minus_varp = var, var = 0;
870 else if (*minus_varp)
871 var = *minus_varp, *minus_varp = 0;
872 }
873
874 if (*litp
875 && TREE_OVERFLOW_P (*litp))
876 *litp = drop_tree_overflow (*litp);
877 if (*minus_litp
878 && TREE_OVERFLOW_P (*minus_litp))
879 *minus_litp = drop_tree_overflow (*minus_litp);
880
881 return var;
882 }
883
884 /* Re-associate trees split by the above function. T1 and T2 are
885 either expressions to associate or null. Return the new
886 expression, if any. LOC is the location of the new expression. If
887 we build an operation, do it in TYPE and with CODE. */
888
889 static tree
890 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
891 {
892 if (t1 == 0)
893 {
894 gcc_assert (t2 == 0 || code != MINUS_EXPR);
895 return t2;
896 }
897 else if (t2 == 0)
898 return t1;
899
900 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
901 try to fold this since we will have infinite recursion. But do
902 deal with any NEGATE_EXPRs. */
903 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
904 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
905 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
906 {
907 if (code == PLUS_EXPR)
908 {
909 if (TREE_CODE (t1) == NEGATE_EXPR)
910 return build2_loc (loc, MINUS_EXPR, type,
911 fold_convert_loc (loc, type, t2),
912 fold_convert_loc (loc, type,
913 TREE_OPERAND (t1, 0)));
914 else if (TREE_CODE (t2) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t1),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t2, 0)));
919 else if (integer_zerop (t2))
920 return fold_convert_loc (loc, type, t1);
921 }
922 else if (code == MINUS_EXPR)
923 {
924 if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
926 }
927
928 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
929 fold_convert_loc (loc, type, t2));
930 }
931
932 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
933 fold_convert_loc (loc, type, t2));
934 }
935 \f
936 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
937 for use in int_const_binop, size_binop and size_diffop. */
938
939 static bool
940 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
941 {
942 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
943 return false;
944 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
945 return false;
946
947 switch (code)
948 {
949 case LSHIFT_EXPR:
950 case RSHIFT_EXPR:
951 case LROTATE_EXPR:
952 case RROTATE_EXPR:
953 return true;
954
955 default:
956 break;
957 }
958
959 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
960 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
961 && TYPE_MODE (type1) == TYPE_MODE (type2);
962 }
963
964
965 /* Combine two integer constants PARG1 and PARG2 under operation CODE
966 to produce a new constant. Return NULL_TREE if we don't know how
967 to evaluate CODE at compile-time. */
968
969 static tree
970 int_const_binop_1 (enum tree_code code, const_tree parg1, const_tree parg2,
971 int overflowable)
972 {
973 wide_int res;
974 tree t;
975 tree type = TREE_TYPE (parg1);
976 signop sign = TYPE_SIGN (type);
977 bool overflow = false;
978
979 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
980 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
981
982 switch (code)
983 {
984 case BIT_IOR_EXPR:
985 res = wi::bit_or (arg1, arg2);
986 break;
987
988 case BIT_XOR_EXPR:
989 res = wi::bit_xor (arg1, arg2);
990 break;
991
992 case BIT_AND_EXPR:
993 res = wi::bit_and (arg1, arg2);
994 break;
995
996 case RSHIFT_EXPR:
997 case LSHIFT_EXPR:
998 if (wi::neg_p (arg2))
999 {
1000 arg2 = -arg2;
1001 if (code == RSHIFT_EXPR)
1002 code = LSHIFT_EXPR;
1003 else
1004 code = RSHIFT_EXPR;
1005 }
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, arg2, sign);
1012 else
1013 res = wi::lshift (arg1, arg2);
1014 break;
1015
1016 case RROTATE_EXPR:
1017 case LROTATE_EXPR:
1018 if (wi::neg_p (arg2))
1019 {
1020 arg2 = -arg2;
1021 if (code == RROTATE_EXPR)
1022 code = LROTATE_EXPR;
1023 else
1024 code = RROTATE_EXPR;
1025 }
1026
1027 if (code == RROTATE_EXPR)
1028 res = wi::rrotate (arg1, arg2);
1029 else
1030 res = wi::lrotate (arg1, arg2);
1031 break;
1032
1033 case PLUS_EXPR:
1034 res = wi::add (arg1, arg2, sign, &overflow);
1035 break;
1036
1037 case MINUS_EXPR:
1038 res = wi::sub (arg1, arg2, sign, &overflow);
1039 break;
1040
1041 case MULT_EXPR:
1042 res = wi::mul (arg1, arg2, sign, &overflow);
1043 break;
1044
1045 case MULT_HIGHPART_EXPR:
1046 res = wi::mul_high (arg1, arg2, sign);
1047 break;
1048
1049 case TRUNC_DIV_EXPR:
1050 case EXACT_DIV_EXPR:
1051 if (arg2 == 0)
1052 return NULL_TREE;
1053 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1054 break;
1055
1056 case FLOOR_DIV_EXPR:
1057 if (arg2 == 0)
1058 return NULL_TREE;
1059 res = wi::div_floor (arg1, arg2, sign, &overflow);
1060 break;
1061
1062 case CEIL_DIV_EXPR:
1063 if (arg2 == 0)
1064 return NULL_TREE;
1065 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1066 break;
1067
1068 case ROUND_DIV_EXPR:
1069 if (arg2 == 0)
1070 return NULL_TREE;
1071 res = wi::div_round (arg1, arg2, sign, &overflow);
1072 break;
1073
1074 case TRUNC_MOD_EXPR:
1075 if (arg2 == 0)
1076 return NULL_TREE;
1077 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1078 break;
1079
1080 case FLOOR_MOD_EXPR:
1081 if (arg2 == 0)
1082 return NULL_TREE;
1083 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1084 break;
1085
1086 case CEIL_MOD_EXPR:
1087 if (arg2 == 0)
1088 return NULL_TREE;
1089 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1090 break;
1091
1092 case ROUND_MOD_EXPR:
1093 if (arg2 == 0)
1094 return NULL_TREE;
1095 res = wi::mod_round (arg1, arg2, sign, &overflow);
1096 break;
1097
1098 case MIN_EXPR:
1099 res = wi::min (arg1, arg2, sign);
1100 break;
1101
1102 case MAX_EXPR:
1103 res = wi::max (arg1, arg2, sign);
1104 break;
1105
1106 default:
1107 return NULL_TREE;
1108 }
1109
1110 t = force_fit_type (type, res, overflowable,
1111 (((sign == SIGNED || overflowable == -1)
1112 && overflow)
1113 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1114
1115 return t;
1116 }
1117
1118 tree
1119 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1120 {
1121 return int_const_binop_1 (code, arg1, arg2, 1);
1122 }
1123
1124 /* Return true if binary operation OP distributes over addition in operand
1125 OPNO, with the other operand being held constant. OPNO counts from 1. */
1126
1127 static bool
1128 distributes_over_addition_p (tree_code op, int opno)
1129 {
1130 switch (op)
1131 {
1132 case PLUS_EXPR:
1133 case MINUS_EXPR:
1134 case MULT_EXPR:
1135 return true;
1136
1137 case LSHIFT_EXPR:
1138 return opno == 1;
1139
1140 default:
1141 return false;
1142 }
1143 }
1144
1145 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1146 constant. We assume ARG1 and ARG2 have the same data type, or at least
1147 are the same kind of constant and the same machine mode. Return zero if
1148 combining the constants is not allowed in the current operating mode. */
1149
1150 static tree
1151 const_binop (enum tree_code code, tree arg1, tree arg2)
1152 {
1153 /* Sanity check for the recursive cases. */
1154 if (!arg1 || !arg2)
1155 return NULL_TREE;
1156
1157 STRIP_NOPS (arg1);
1158 STRIP_NOPS (arg2);
1159
1160 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1161 {
1162 if (code == POINTER_PLUS_EXPR)
1163 return int_const_binop (PLUS_EXPR,
1164 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1165
1166 return int_const_binop (code, arg1, arg2);
1167 }
1168
1169 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1170 {
1171 machine_mode mode;
1172 REAL_VALUE_TYPE d1;
1173 REAL_VALUE_TYPE d2;
1174 REAL_VALUE_TYPE value;
1175 REAL_VALUE_TYPE result;
1176 bool inexact;
1177 tree t, type;
1178
1179 /* The following codes are handled by real_arithmetic. */
1180 switch (code)
1181 {
1182 case PLUS_EXPR:
1183 case MINUS_EXPR:
1184 case MULT_EXPR:
1185 case RDIV_EXPR:
1186 case MIN_EXPR:
1187 case MAX_EXPR:
1188 break;
1189
1190 default:
1191 return NULL_TREE;
1192 }
1193
1194 d1 = TREE_REAL_CST (arg1);
1195 d2 = TREE_REAL_CST (arg2);
1196
1197 type = TREE_TYPE (arg1);
1198 mode = TYPE_MODE (type);
1199
1200 /* Don't perform operation if we honor signaling NaNs and
1201 either operand is a signaling NaN. */
1202 if (HONOR_SNANS (mode)
1203 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1204 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1205 return NULL_TREE;
1206
1207 /* Don't perform operation if it would raise a division
1208 by zero exception. */
1209 if (code == RDIV_EXPR
1210 && real_equal (&d2, &dconst0)
1211 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1212 return NULL_TREE;
1213
1214 /* If either operand is a NaN, just return it. Otherwise, set up
1215 for floating-point trap; we return an overflow. */
1216 if (REAL_VALUE_ISNAN (d1))
1217 {
1218 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1219 is off. */
1220 d1.signalling = 0;
1221 t = build_real (type, d1);
1222 return t;
1223 }
1224 else if (REAL_VALUE_ISNAN (d2))
1225 {
1226 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1227 is off. */
1228 d2.signalling = 0;
1229 t = build_real (type, d2);
1230 return t;
1231 }
1232
1233 inexact = real_arithmetic (&value, code, &d1, &d2);
1234 real_convert (&result, mode, &value);
1235
1236 /* Don't constant fold this floating point operation if
1237 the result has overflowed and flag_trapping_math. */
1238 if (flag_trapping_math
1239 && MODE_HAS_INFINITIES (mode)
1240 && REAL_VALUE_ISINF (result)
1241 && !REAL_VALUE_ISINF (d1)
1242 && !REAL_VALUE_ISINF (d2))
1243 return NULL_TREE;
1244
1245 /* Don't constant fold this floating point operation if the
1246 result may dependent upon the run-time rounding mode and
1247 flag_rounding_math is set, or if GCC's software emulation
1248 is unable to accurately represent the result. */
1249 if ((flag_rounding_math
1250 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1251 && (inexact || !real_identical (&result, &value)))
1252 return NULL_TREE;
1253
1254 t = build_real (type, result);
1255
1256 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1257 return t;
1258 }
1259
1260 if (TREE_CODE (arg1) == FIXED_CST)
1261 {
1262 FIXED_VALUE_TYPE f1;
1263 FIXED_VALUE_TYPE f2;
1264 FIXED_VALUE_TYPE result;
1265 tree t, type;
1266 int sat_p;
1267 bool overflow_p;
1268
1269 /* The following codes are handled by fixed_arithmetic. */
1270 switch (code)
1271 {
1272 case PLUS_EXPR:
1273 case MINUS_EXPR:
1274 case MULT_EXPR:
1275 case TRUNC_DIV_EXPR:
1276 if (TREE_CODE (arg2) != FIXED_CST)
1277 return NULL_TREE;
1278 f2 = TREE_FIXED_CST (arg2);
1279 break;
1280
1281 case LSHIFT_EXPR:
1282 case RSHIFT_EXPR:
1283 {
1284 if (TREE_CODE (arg2) != INTEGER_CST)
1285 return NULL_TREE;
1286 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1287 f2.data.high = w2.elt (1);
1288 f2.data.low = w2.ulow ();
1289 f2.mode = SImode;
1290 }
1291 break;
1292
1293 default:
1294 return NULL_TREE;
1295 }
1296
1297 f1 = TREE_FIXED_CST (arg1);
1298 type = TREE_TYPE (arg1);
1299 sat_p = TYPE_SATURATING (type);
1300 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1301 t = build_fixed (type, result);
1302 /* Propagate overflow flags. */
1303 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1304 TREE_OVERFLOW (t) = 1;
1305 return t;
1306 }
1307
1308 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1309 {
1310 tree type = TREE_TYPE (arg1);
1311 tree r1 = TREE_REALPART (arg1);
1312 tree i1 = TREE_IMAGPART (arg1);
1313 tree r2 = TREE_REALPART (arg2);
1314 tree i2 = TREE_IMAGPART (arg2);
1315 tree real, imag;
1316
1317 switch (code)
1318 {
1319 case PLUS_EXPR:
1320 case MINUS_EXPR:
1321 real = const_binop (code, r1, r2);
1322 imag = const_binop (code, i1, i2);
1323 break;
1324
1325 case MULT_EXPR:
1326 if (COMPLEX_FLOAT_TYPE_P (type))
1327 return do_mpc_arg2 (arg1, arg2, type,
1328 /* do_nonfinite= */ folding_initializer,
1329 mpc_mul);
1330
1331 real = const_binop (MINUS_EXPR,
1332 const_binop (MULT_EXPR, r1, r2),
1333 const_binop (MULT_EXPR, i1, i2));
1334 imag = const_binop (PLUS_EXPR,
1335 const_binop (MULT_EXPR, r1, i2),
1336 const_binop (MULT_EXPR, i1, r2));
1337 break;
1338
1339 case RDIV_EXPR:
1340 if (COMPLEX_FLOAT_TYPE_P (type))
1341 return do_mpc_arg2 (arg1, arg2, type,
1342 /* do_nonfinite= */ folding_initializer,
1343 mpc_div);
1344 /* Fallthru. */
1345 case TRUNC_DIV_EXPR:
1346 case CEIL_DIV_EXPR:
1347 case FLOOR_DIV_EXPR:
1348 case ROUND_DIV_EXPR:
1349 if (flag_complex_method == 0)
1350 {
1351 /* Keep this algorithm in sync with
1352 tree-complex.c:expand_complex_div_straight().
1353
1354 Expand complex division to scalars, straightforward algorithm.
1355 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1356 t = br*br + bi*bi
1357 */
1358 tree magsquared
1359 = const_binop (PLUS_EXPR,
1360 const_binop (MULT_EXPR, r2, r2),
1361 const_binop (MULT_EXPR, i2, i2));
1362 tree t1
1363 = const_binop (PLUS_EXPR,
1364 const_binop (MULT_EXPR, r1, r2),
1365 const_binop (MULT_EXPR, i1, i2));
1366 tree t2
1367 = const_binop (MINUS_EXPR,
1368 const_binop (MULT_EXPR, i1, r2),
1369 const_binop (MULT_EXPR, r1, i2));
1370
1371 real = const_binop (code, t1, magsquared);
1372 imag = const_binop (code, t2, magsquared);
1373 }
1374 else
1375 {
1376 /* Keep this algorithm in sync with
1377 tree-complex.c:expand_complex_div_wide().
1378
1379 Expand complex division to scalars, modified algorithm to minimize
1380 overflow with wide input ranges. */
1381 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1382 fold_abs_const (r2, TREE_TYPE (type)),
1383 fold_abs_const (i2, TREE_TYPE (type)));
1384
1385 if (integer_nonzerop (compare))
1386 {
1387 /* In the TRUE branch, we compute
1388 ratio = br/bi;
1389 div = (br * ratio) + bi;
1390 tr = (ar * ratio) + ai;
1391 ti = (ai * ratio) - ar;
1392 tr = tr / div;
1393 ti = ti / div; */
1394 tree ratio = const_binop (code, r2, i2);
1395 tree div = const_binop (PLUS_EXPR, i2,
1396 const_binop (MULT_EXPR, r2, ratio));
1397 real = const_binop (MULT_EXPR, r1, ratio);
1398 real = const_binop (PLUS_EXPR, real, i1);
1399 real = const_binop (code, real, div);
1400
1401 imag = const_binop (MULT_EXPR, i1, ratio);
1402 imag = const_binop (MINUS_EXPR, imag, r1);
1403 imag = const_binop (code, imag, div);
1404 }
1405 else
1406 {
1407 /* In the FALSE branch, we compute
1408 ratio = d/c;
1409 divisor = (d * ratio) + c;
1410 tr = (b * ratio) + a;
1411 ti = b - (a * ratio);
1412 tr = tr / div;
1413 ti = ti / div; */
1414 tree ratio = const_binop (code, i2, r2);
1415 tree div = const_binop (PLUS_EXPR, r2,
1416 const_binop (MULT_EXPR, i2, ratio));
1417
1418 real = const_binop (MULT_EXPR, i1, ratio);
1419 real = const_binop (PLUS_EXPR, real, r1);
1420 real = const_binop (code, real, div);
1421
1422 imag = const_binop (MULT_EXPR, r1, ratio);
1423 imag = const_binop (MINUS_EXPR, i1, imag);
1424 imag = const_binop (code, imag, div);
1425 }
1426 }
1427 break;
1428
1429 default:
1430 return NULL_TREE;
1431 }
1432
1433 if (real && imag)
1434 return build_complex (type, real, imag);
1435 }
1436
1437 if (TREE_CODE (arg1) == VECTOR_CST
1438 && TREE_CODE (arg2) == VECTOR_CST
1439 && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))
1440 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1441 {
1442 tree type = TREE_TYPE (arg1);
1443 bool step_ok_p;
1444 if (VECTOR_CST_STEPPED_P (arg1)
1445 && VECTOR_CST_STEPPED_P (arg2))
1446 /* We can operate directly on the encoding if:
1447
1448 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1449 implies
1450 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1451
1452 Addition and subtraction are the supported operators
1453 for which this is true. */
1454 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1455 else if (VECTOR_CST_STEPPED_P (arg1))
1456 /* We can operate directly on stepped encodings if:
1457
1458 a3 - a2 == a2 - a1
1459 implies:
1460 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1461
1462 which is true if (x -> x op c) distributes over addition. */
1463 step_ok_p = distributes_over_addition_p (code, 1);
1464 else
1465 /* Similarly in reverse. */
1466 step_ok_p = distributes_over_addition_p (code, 2);
1467 tree_vector_builder elts;
1468 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1469 return NULL_TREE;
1470 unsigned int count = elts.encoded_nelts ();
1471 for (unsigned int i = 0; i < count; ++i)
1472 {
1473 tree elem1 = VECTOR_CST_ELT (arg1, i);
1474 tree elem2 = VECTOR_CST_ELT (arg2, i);
1475
1476 tree elt = const_binop (code, elem1, elem2);
1477
1478 /* It is possible that const_binop cannot handle the given
1479 code and return NULL_TREE */
1480 if (elt == NULL_TREE)
1481 return NULL_TREE;
1482 elts.quick_push (elt);
1483 }
1484
1485 return elts.build ();
1486 }
1487
1488 /* Shifts allow a scalar offset for a vector. */
1489 if (TREE_CODE (arg1) == VECTOR_CST
1490 && TREE_CODE (arg2) == INTEGER_CST)
1491 {
1492 tree type = TREE_TYPE (arg1);
1493 bool step_ok_p = distributes_over_addition_p (code, 1);
1494 tree_vector_builder elts;
1495 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1496 return NULL_TREE;
1497 unsigned int count = elts.encoded_nelts ();
1498 for (unsigned int i = 0; i < count; ++i)
1499 {
1500 tree elem1 = VECTOR_CST_ELT (arg1, i);
1501
1502 tree elt = const_binop (code, elem1, arg2);
1503
1504 /* It is possible that const_binop cannot handle the given
1505 code and return NULL_TREE. */
1506 if (elt == NULL_TREE)
1507 return NULL_TREE;
1508 elts.quick_push (elt);
1509 }
1510
1511 return elts.build ();
1512 }
1513 return NULL_TREE;
1514 }
1515
1516 /* Overload that adds a TYPE parameter to be able to dispatch
1517 to fold_relational_const. */
1518
1519 tree
1520 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1521 {
1522 if (TREE_CODE_CLASS (code) == tcc_comparison)
1523 return fold_relational_const (code, type, arg1, arg2);
1524
1525 /* ??? Until we make the const_binop worker take the type of the
1526 result as argument put those cases that need it here. */
1527 switch (code)
1528 {
1529 case COMPLEX_EXPR:
1530 if ((TREE_CODE (arg1) == REAL_CST
1531 && TREE_CODE (arg2) == REAL_CST)
1532 || (TREE_CODE (arg1) == INTEGER_CST
1533 && TREE_CODE (arg2) == INTEGER_CST))
1534 return build_complex (type, arg1, arg2);
1535 return NULL_TREE;
1536
1537 case POINTER_DIFF_EXPR:
1538 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1539 {
1540 offset_int res = wi::sub (wi::to_offset (arg1),
1541 wi::to_offset (arg2));
1542 return force_fit_type (type, res, 1,
1543 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1544 }
1545 return NULL_TREE;
1546
1547 case VEC_PACK_TRUNC_EXPR:
1548 case VEC_PACK_FIX_TRUNC_EXPR:
1549 {
1550 unsigned int out_nelts, in_nelts, i;
1551
1552 if (TREE_CODE (arg1) != VECTOR_CST
1553 || TREE_CODE (arg2) != VECTOR_CST)
1554 return NULL_TREE;
1555
1556 in_nelts = VECTOR_CST_NELTS (arg1);
1557 out_nelts = in_nelts * 2;
1558 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1559 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1560
1561 tree_vector_builder elts (type, out_nelts, 1);
1562 for (i = 0; i < out_nelts; i++)
1563 {
1564 tree elt = (i < in_nelts
1565 ? VECTOR_CST_ELT (arg1, i)
1566 : VECTOR_CST_ELT (arg2, i - in_nelts));
1567 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1568 ? NOP_EXPR : FIX_TRUNC_EXPR,
1569 TREE_TYPE (type), elt);
1570 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1571 return NULL_TREE;
1572 elts.quick_push (elt);
1573 }
1574
1575 return elts.build ();
1576 }
1577
1578 case VEC_WIDEN_MULT_LO_EXPR:
1579 case VEC_WIDEN_MULT_HI_EXPR:
1580 case VEC_WIDEN_MULT_EVEN_EXPR:
1581 case VEC_WIDEN_MULT_ODD_EXPR:
1582 {
1583 unsigned int out_nelts, in_nelts, out, ofs, scale;
1584
1585 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1586 return NULL_TREE;
1587
1588 in_nelts = VECTOR_CST_NELTS (arg1);
1589 out_nelts = in_nelts / 2;
1590 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1591 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1592
1593 if (code == VEC_WIDEN_MULT_LO_EXPR)
1594 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1595 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1596 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1597 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1598 scale = 1, ofs = 0;
1599 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1600 scale = 1, ofs = 1;
1601
1602 tree_vector_builder elts (type, out_nelts, 1);
1603 for (out = 0; out < out_nelts; out++)
1604 {
1605 unsigned int in = (out << scale) + ofs;
1606 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1607 VECTOR_CST_ELT (arg1, in));
1608 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1609 VECTOR_CST_ELT (arg2, in));
1610
1611 if (t1 == NULL_TREE || t2 == NULL_TREE)
1612 return NULL_TREE;
1613 tree elt = const_binop (MULT_EXPR, t1, t2);
1614 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1615 return NULL_TREE;
1616 elts.quick_push (elt);
1617 }
1618
1619 return elts.build ();
1620 }
1621
1622 default:;
1623 }
1624
1625 if (TREE_CODE_CLASS (code) != tcc_binary)
1626 return NULL_TREE;
1627
1628 /* Make sure type and arg0 have the same saturating flag. */
1629 gcc_checking_assert (TYPE_SATURATING (type)
1630 == TYPE_SATURATING (TREE_TYPE (arg1)));
1631
1632 return const_binop (code, arg1, arg2);
1633 }
1634
1635 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1636 Return zero if computing the constants is not possible. */
1637
1638 tree
1639 const_unop (enum tree_code code, tree type, tree arg0)
1640 {
1641 /* Don't perform the operation, other than NEGATE and ABS, if
1642 flag_signaling_nans is on and the operand is a signaling NaN. */
1643 if (TREE_CODE (arg0) == REAL_CST
1644 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1645 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1646 && code != NEGATE_EXPR
1647 && code != ABS_EXPR)
1648 return NULL_TREE;
1649
1650 switch (code)
1651 {
1652 CASE_CONVERT:
1653 case FLOAT_EXPR:
1654 case FIX_TRUNC_EXPR:
1655 case FIXED_CONVERT_EXPR:
1656 return fold_convert_const (code, type, arg0);
1657
1658 case ADDR_SPACE_CONVERT_EXPR:
1659 /* If the source address is 0, and the source address space
1660 cannot have a valid object at 0, fold to dest type null. */
1661 if (integer_zerop (arg0)
1662 && !(targetm.addr_space.zero_address_valid
1663 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1664 return fold_convert_const (code, type, arg0);
1665 break;
1666
1667 case VIEW_CONVERT_EXPR:
1668 return fold_view_convert_expr (type, arg0);
1669
1670 case NEGATE_EXPR:
1671 {
1672 /* Can't call fold_negate_const directly here as that doesn't
1673 handle all cases and we might not be able to negate some
1674 constants. */
1675 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1676 if (tem && CONSTANT_CLASS_P (tem))
1677 return tem;
1678 break;
1679 }
1680
1681 case ABS_EXPR:
1682 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1683 return fold_abs_const (arg0, type);
1684 break;
1685
1686 case CONJ_EXPR:
1687 if (TREE_CODE (arg0) == COMPLEX_CST)
1688 {
1689 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1690 TREE_TYPE (type));
1691 return build_complex (type, TREE_REALPART (arg0), ipart);
1692 }
1693 break;
1694
1695 case BIT_NOT_EXPR:
1696 if (TREE_CODE (arg0) == INTEGER_CST)
1697 return fold_not_const (arg0, type);
1698 /* Perform BIT_NOT_EXPR on each element individually. */
1699 else if (TREE_CODE (arg0) == VECTOR_CST)
1700 {
1701 tree elem;
1702
1703 /* This can cope with stepped encodings because ~x == -1 - x. */
1704 tree_vector_builder elements;
1705 elements.new_unary_operation (type, arg0, true);
1706 unsigned int i, count = elements.encoded_nelts ();
1707 for (i = 0; i < count; ++i)
1708 {
1709 elem = VECTOR_CST_ELT (arg0, i);
1710 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1711 if (elem == NULL_TREE)
1712 break;
1713 elements.quick_push (elem);
1714 }
1715 if (i == count)
1716 return elements.build ();
1717 }
1718 break;
1719
1720 case TRUTH_NOT_EXPR:
1721 if (TREE_CODE (arg0) == INTEGER_CST)
1722 return constant_boolean_node (integer_zerop (arg0), type);
1723 break;
1724
1725 case REALPART_EXPR:
1726 if (TREE_CODE (arg0) == COMPLEX_CST)
1727 return fold_convert (type, TREE_REALPART (arg0));
1728 break;
1729
1730 case IMAGPART_EXPR:
1731 if (TREE_CODE (arg0) == COMPLEX_CST)
1732 return fold_convert (type, TREE_IMAGPART (arg0));
1733 break;
1734
1735 case VEC_UNPACK_LO_EXPR:
1736 case VEC_UNPACK_HI_EXPR:
1737 case VEC_UNPACK_FLOAT_LO_EXPR:
1738 case VEC_UNPACK_FLOAT_HI_EXPR:
1739 {
1740 unsigned int out_nelts, in_nelts, i;
1741 enum tree_code subcode;
1742
1743 if (TREE_CODE (arg0) != VECTOR_CST)
1744 return NULL_TREE;
1745
1746 in_nelts = VECTOR_CST_NELTS (arg0);
1747 out_nelts = in_nelts / 2;
1748 gcc_assert (out_nelts == TYPE_VECTOR_SUBPARTS (type));
1749
1750 unsigned int offset = 0;
1751 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1752 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1753 offset = out_nelts;
1754
1755 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1756 subcode = NOP_EXPR;
1757 else
1758 subcode = FLOAT_EXPR;
1759
1760 tree_vector_builder elts (type, out_nelts, 1);
1761 for (i = 0; i < out_nelts; i++)
1762 {
1763 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1764 VECTOR_CST_ELT (arg0, i + offset));
1765 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1766 return NULL_TREE;
1767 elts.quick_push (elt);
1768 }
1769
1770 return elts.build ();
1771 }
1772
1773 default:
1774 break;
1775 }
1776
1777 return NULL_TREE;
1778 }
1779
1780 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1781 indicates which particular sizetype to create. */
1782
1783 tree
1784 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1785 {
1786 return build_int_cst (sizetype_tab[(int) kind], number);
1787 }
1788 \f
1789 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1790 is a tree code. The type of the result is taken from the operands.
1791 Both must be equivalent integer types, ala int_binop_types_match_p.
1792 If the operands are constant, so is the result. */
1793
1794 tree
1795 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1796 {
1797 tree type = TREE_TYPE (arg0);
1798
1799 if (arg0 == error_mark_node || arg1 == error_mark_node)
1800 return error_mark_node;
1801
1802 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1803 TREE_TYPE (arg1)));
1804
1805 /* Handle the special case of two integer constants faster. */
1806 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1807 {
1808 /* And some specific cases even faster than that. */
1809 if (code == PLUS_EXPR)
1810 {
1811 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1812 return arg1;
1813 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1814 return arg0;
1815 }
1816 else if (code == MINUS_EXPR)
1817 {
1818 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1819 return arg0;
1820 }
1821 else if (code == MULT_EXPR)
1822 {
1823 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1824 return arg1;
1825 }
1826
1827 /* Handle general case of two integer constants. For sizetype
1828 constant calculations we always want to know about overflow,
1829 even in the unsigned case. */
1830 return int_const_binop_1 (code, arg0, arg1, -1);
1831 }
1832
1833 return fold_build2_loc (loc, code, type, arg0, arg1);
1834 }
1835
1836 /* Given two values, either both of sizetype or both of bitsizetype,
1837 compute the difference between the two values. Return the value
1838 in signed type corresponding to the type of the operands. */
1839
1840 tree
1841 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1842 {
1843 tree type = TREE_TYPE (arg0);
1844 tree ctype;
1845
1846 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1847 TREE_TYPE (arg1)));
1848
1849 /* If the type is already signed, just do the simple thing. */
1850 if (!TYPE_UNSIGNED (type))
1851 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1852
1853 if (type == sizetype)
1854 ctype = ssizetype;
1855 else if (type == bitsizetype)
1856 ctype = sbitsizetype;
1857 else
1858 ctype = signed_type_for (type);
1859
1860 /* If either operand is not a constant, do the conversions to the signed
1861 type and subtract. The hardware will do the right thing with any
1862 overflow in the subtraction. */
1863 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1864 return size_binop_loc (loc, MINUS_EXPR,
1865 fold_convert_loc (loc, ctype, arg0),
1866 fold_convert_loc (loc, ctype, arg1));
1867
1868 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1869 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1870 overflow) and negate (which can't either). Special-case a result
1871 of zero while we're here. */
1872 if (tree_int_cst_equal (arg0, arg1))
1873 return build_int_cst (ctype, 0);
1874 else if (tree_int_cst_lt (arg1, arg0))
1875 return fold_convert_loc (loc, ctype,
1876 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1877 else
1878 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1879 fold_convert_loc (loc, ctype,
1880 size_binop_loc (loc,
1881 MINUS_EXPR,
1882 arg1, arg0)));
1883 }
1884 \f
1885 /* A subroutine of fold_convert_const handling conversions of an
1886 INTEGER_CST to another integer type. */
1887
1888 static tree
1889 fold_convert_const_int_from_int (tree type, const_tree arg1)
1890 {
1891 /* Given an integer constant, make new constant with new type,
1892 appropriately sign-extended or truncated. Use widest_int
1893 so that any extension is done according ARG1's type. */
1894 return force_fit_type (type, wi::to_widest (arg1),
1895 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1896 TREE_OVERFLOW (arg1));
1897 }
1898
1899 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1900 to an integer type. */
1901
1902 static tree
1903 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1904 {
1905 bool overflow = false;
1906 tree t;
1907
1908 /* The following code implements the floating point to integer
1909 conversion rules required by the Java Language Specification,
1910 that IEEE NaNs are mapped to zero and values that overflow
1911 the target precision saturate, i.e. values greater than
1912 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1913 are mapped to INT_MIN. These semantics are allowed by the
1914 C and C++ standards that simply state that the behavior of
1915 FP-to-integer conversion is unspecified upon overflow. */
1916
1917 wide_int val;
1918 REAL_VALUE_TYPE r;
1919 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1920
1921 switch (code)
1922 {
1923 case FIX_TRUNC_EXPR:
1924 real_trunc (&r, VOIDmode, &x);
1925 break;
1926
1927 default:
1928 gcc_unreachable ();
1929 }
1930
1931 /* If R is NaN, return zero and show we have an overflow. */
1932 if (REAL_VALUE_ISNAN (r))
1933 {
1934 overflow = true;
1935 val = wi::zero (TYPE_PRECISION (type));
1936 }
1937
1938 /* See if R is less than the lower bound or greater than the
1939 upper bound. */
1940
1941 if (! overflow)
1942 {
1943 tree lt = TYPE_MIN_VALUE (type);
1944 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1945 if (real_less (&r, &l))
1946 {
1947 overflow = true;
1948 val = wi::to_wide (lt);
1949 }
1950 }
1951
1952 if (! overflow)
1953 {
1954 tree ut = TYPE_MAX_VALUE (type);
1955 if (ut)
1956 {
1957 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1958 if (real_less (&u, &r))
1959 {
1960 overflow = true;
1961 val = wi::to_wide (ut);
1962 }
1963 }
1964 }
1965
1966 if (! overflow)
1967 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1968
1969 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1970 return t;
1971 }
1972
1973 /* A subroutine of fold_convert_const handling conversions of a
1974 FIXED_CST to an integer type. */
1975
1976 static tree
1977 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1978 {
1979 tree t;
1980 double_int temp, temp_trunc;
1981 scalar_mode mode;
1982
1983 /* Right shift FIXED_CST to temp by fbit. */
1984 temp = TREE_FIXED_CST (arg1).data;
1985 mode = TREE_FIXED_CST (arg1).mode;
1986 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1987 {
1988 temp = temp.rshift (GET_MODE_FBIT (mode),
1989 HOST_BITS_PER_DOUBLE_INT,
1990 SIGNED_FIXED_POINT_MODE_P (mode));
1991
1992 /* Left shift temp to temp_trunc by fbit. */
1993 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1994 HOST_BITS_PER_DOUBLE_INT,
1995 SIGNED_FIXED_POINT_MODE_P (mode));
1996 }
1997 else
1998 {
1999 temp = double_int_zero;
2000 temp_trunc = double_int_zero;
2001 }
2002
2003 /* If FIXED_CST is negative, we need to round the value toward 0.
2004 By checking if the fractional bits are not zero to add 1 to temp. */
2005 if (SIGNED_FIXED_POINT_MODE_P (mode)
2006 && temp_trunc.is_negative ()
2007 && TREE_FIXED_CST (arg1).data != temp_trunc)
2008 temp += double_int_one;
2009
2010 /* Given a fixed-point constant, make new constant with new type,
2011 appropriately sign-extended or truncated. */
2012 t = force_fit_type (type, temp, -1,
2013 (temp.is_negative ()
2014 && (TYPE_UNSIGNED (type)
2015 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2016 | TREE_OVERFLOW (arg1));
2017
2018 return t;
2019 }
2020
2021 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2022 to another floating point type. */
2023
2024 static tree
2025 fold_convert_const_real_from_real (tree type, const_tree arg1)
2026 {
2027 REAL_VALUE_TYPE value;
2028 tree t;
2029
2030 /* Don't perform the operation if flag_signaling_nans is on
2031 and the operand is a signaling NaN. */
2032 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2033 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2034 return NULL_TREE;
2035
2036 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2037 t = build_real (type, value);
2038
2039 /* If converting an infinity or NAN to a representation that doesn't
2040 have one, set the overflow bit so that we can produce some kind of
2041 error message at the appropriate point if necessary. It's not the
2042 most user-friendly message, but it's better than nothing. */
2043 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2044 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2045 TREE_OVERFLOW (t) = 1;
2046 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2047 && !MODE_HAS_NANS (TYPE_MODE (type)))
2048 TREE_OVERFLOW (t) = 1;
2049 /* Regular overflow, conversion produced an infinity in a mode that
2050 can't represent them. */
2051 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2052 && REAL_VALUE_ISINF (value)
2053 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2054 TREE_OVERFLOW (t) = 1;
2055 else
2056 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2057 return t;
2058 }
2059
2060 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2061 to a floating point type. */
2062
2063 static tree
2064 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2065 {
2066 REAL_VALUE_TYPE value;
2067 tree t;
2068
2069 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2070 &TREE_FIXED_CST (arg1));
2071 t = build_real (type, value);
2072
2073 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2074 return t;
2075 }
2076
2077 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2078 to another fixed-point type. */
2079
2080 static tree
2081 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2082 {
2083 FIXED_VALUE_TYPE value;
2084 tree t;
2085 bool overflow_p;
2086
2087 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2088 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2089 t = build_fixed (type, value);
2090
2091 /* Propagate overflow flags. */
2092 if (overflow_p | TREE_OVERFLOW (arg1))
2093 TREE_OVERFLOW (t) = 1;
2094 return t;
2095 }
2096
2097 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2098 to a fixed-point type. */
2099
2100 static tree
2101 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2102 {
2103 FIXED_VALUE_TYPE value;
2104 tree t;
2105 bool overflow_p;
2106 double_int di;
2107
2108 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2109
2110 di.low = TREE_INT_CST_ELT (arg1, 0);
2111 if (TREE_INT_CST_NUNITS (arg1) == 1)
2112 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2113 else
2114 di.high = TREE_INT_CST_ELT (arg1, 1);
2115
2116 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2117 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2118 TYPE_SATURATING (type));
2119 t = build_fixed (type, value);
2120
2121 /* Propagate overflow flags. */
2122 if (overflow_p | TREE_OVERFLOW (arg1))
2123 TREE_OVERFLOW (t) = 1;
2124 return t;
2125 }
2126
2127 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2128 to a fixed-point type. */
2129
2130 static tree
2131 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2132 {
2133 FIXED_VALUE_TYPE value;
2134 tree t;
2135 bool overflow_p;
2136
2137 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2138 &TREE_REAL_CST (arg1),
2139 TYPE_SATURATING (type));
2140 t = build_fixed (type, value);
2141
2142 /* Propagate overflow flags. */
2143 if (overflow_p | TREE_OVERFLOW (arg1))
2144 TREE_OVERFLOW (t) = 1;
2145 return t;
2146 }
2147
2148 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2149 type TYPE. If no simplification can be done return NULL_TREE. */
2150
2151 static tree
2152 fold_convert_const (enum tree_code code, tree type, tree arg1)
2153 {
2154 if (TREE_TYPE (arg1) == type)
2155 return arg1;
2156
2157 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2158 || TREE_CODE (type) == OFFSET_TYPE)
2159 {
2160 if (TREE_CODE (arg1) == INTEGER_CST)
2161 return fold_convert_const_int_from_int (type, arg1);
2162 else if (TREE_CODE (arg1) == REAL_CST)
2163 return fold_convert_const_int_from_real (code, type, arg1);
2164 else if (TREE_CODE (arg1) == FIXED_CST)
2165 return fold_convert_const_int_from_fixed (type, arg1);
2166 }
2167 else if (TREE_CODE (type) == REAL_TYPE)
2168 {
2169 if (TREE_CODE (arg1) == INTEGER_CST)
2170 return build_real_from_int_cst (type, arg1);
2171 else if (TREE_CODE (arg1) == REAL_CST)
2172 return fold_convert_const_real_from_real (type, arg1);
2173 else if (TREE_CODE (arg1) == FIXED_CST)
2174 return fold_convert_const_real_from_fixed (type, arg1);
2175 }
2176 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2177 {
2178 if (TREE_CODE (arg1) == FIXED_CST)
2179 return fold_convert_const_fixed_from_fixed (type, arg1);
2180 else if (TREE_CODE (arg1) == INTEGER_CST)
2181 return fold_convert_const_fixed_from_int (type, arg1);
2182 else if (TREE_CODE (arg1) == REAL_CST)
2183 return fold_convert_const_fixed_from_real (type, arg1);
2184 }
2185 else if (TREE_CODE (type) == VECTOR_TYPE)
2186 {
2187 if (TREE_CODE (arg1) == VECTOR_CST
2188 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2189 {
2190 tree elttype = TREE_TYPE (type);
2191 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2192 /* We can't handle steps directly when extending, since the
2193 values need to wrap at the original precision first. */
2194 bool step_ok_p
2195 = (INTEGRAL_TYPE_P (elttype)
2196 && INTEGRAL_TYPE_P (arg1_elttype)
2197 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2198 tree_vector_builder v;
2199 if (!v.new_unary_operation (type, arg1, step_ok_p))
2200 return NULL_TREE;
2201 unsigned int len = v.encoded_nelts ();
2202 for (unsigned int i = 0; i < len; ++i)
2203 {
2204 tree elt = VECTOR_CST_ELT (arg1, i);
2205 tree cvt = fold_convert_const (code, elttype, elt);
2206 if (cvt == NULL_TREE)
2207 return NULL_TREE;
2208 v.quick_push (cvt);
2209 }
2210 return v.build ();
2211 }
2212 }
2213 return NULL_TREE;
2214 }
2215
2216 /* Construct a vector of zero elements of vector type TYPE. */
2217
2218 static tree
2219 build_zero_vector (tree type)
2220 {
2221 tree t;
2222
2223 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2224 return build_vector_from_val (type, t);
2225 }
2226
2227 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2228
2229 bool
2230 fold_convertible_p (const_tree type, const_tree arg)
2231 {
2232 tree orig = TREE_TYPE (arg);
2233
2234 if (type == orig)
2235 return true;
2236
2237 if (TREE_CODE (arg) == ERROR_MARK
2238 || TREE_CODE (type) == ERROR_MARK
2239 || TREE_CODE (orig) == ERROR_MARK)
2240 return false;
2241
2242 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2243 return true;
2244
2245 switch (TREE_CODE (type))
2246 {
2247 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2248 case POINTER_TYPE: case REFERENCE_TYPE:
2249 case OFFSET_TYPE:
2250 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2251 || TREE_CODE (orig) == OFFSET_TYPE);
2252
2253 case REAL_TYPE:
2254 case FIXED_POINT_TYPE:
2255 case VECTOR_TYPE:
2256 case VOID_TYPE:
2257 return TREE_CODE (type) == TREE_CODE (orig);
2258
2259 default:
2260 return false;
2261 }
2262 }
2263
2264 /* Convert expression ARG to type TYPE. Used by the middle-end for
2265 simple conversions in preference to calling the front-end's convert. */
2266
2267 tree
2268 fold_convert_loc (location_t loc, tree type, tree arg)
2269 {
2270 tree orig = TREE_TYPE (arg);
2271 tree tem;
2272
2273 if (type == orig)
2274 return arg;
2275
2276 if (TREE_CODE (arg) == ERROR_MARK
2277 || TREE_CODE (type) == ERROR_MARK
2278 || TREE_CODE (orig) == ERROR_MARK)
2279 return error_mark_node;
2280
2281 switch (TREE_CODE (type))
2282 {
2283 case POINTER_TYPE:
2284 case REFERENCE_TYPE:
2285 /* Handle conversions between pointers to different address spaces. */
2286 if (POINTER_TYPE_P (orig)
2287 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2288 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2289 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2290 /* fall through */
2291
2292 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2293 case OFFSET_TYPE:
2294 if (TREE_CODE (arg) == INTEGER_CST)
2295 {
2296 tem = fold_convert_const (NOP_EXPR, type, arg);
2297 if (tem != NULL_TREE)
2298 return tem;
2299 }
2300 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2301 || TREE_CODE (orig) == OFFSET_TYPE)
2302 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2303 if (TREE_CODE (orig) == COMPLEX_TYPE)
2304 return fold_convert_loc (loc, type,
2305 fold_build1_loc (loc, REALPART_EXPR,
2306 TREE_TYPE (orig), arg));
2307 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2308 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2309 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2310
2311 case REAL_TYPE:
2312 if (TREE_CODE (arg) == INTEGER_CST)
2313 {
2314 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2315 if (tem != NULL_TREE)
2316 return tem;
2317 }
2318 else if (TREE_CODE (arg) == REAL_CST)
2319 {
2320 tem = fold_convert_const (NOP_EXPR, type, arg);
2321 if (tem != NULL_TREE)
2322 return tem;
2323 }
2324 else if (TREE_CODE (arg) == FIXED_CST)
2325 {
2326 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2327 if (tem != NULL_TREE)
2328 return tem;
2329 }
2330
2331 switch (TREE_CODE (orig))
2332 {
2333 case INTEGER_TYPE:
2334 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2335 case POINTER_TYPE: case REFERENCE_TYPE:
2336 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2337
2338 case REAL_TYPE:
2339 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2340
2341 case FIXED_POINT_TYPE:
2342 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2343
2344 case COMPLEX_TYPE:
2345 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2346 return fold_convert_loc (loc, type, tem);
2347
2348 default:
2349 gcc_unreachable ();
2350 }
2351
2352 case FIXED_POINT_TYPE:
2353 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2354 || TREE_CODE (arg) == REAL_CST)
2355 {
2356 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2357 if (tem != NULL_TREE)
2358 goto fold_convert_exit;
2359 }
2360
2361 switch (TREE_CODE (orig))
2362 {
2363 case FIXED_POINT_TYPE:
2364 case INTEGER_TYPE:
2365 case ENUMERAL_TYPE:
2366 case BOOLEAN_TYPE:
2367 case REAL_TYPE:
2368 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2369
2370 case COMPLEX_TYPE:
2371 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2372 return fold_convert_loc (loc, type, tem);
2373
2374 default:
2375 gcc_unreachable ();
2376 }
2377
2378 case COMPLEX_TYPE:
2379 switch (TREE_CODE (orig))
2380 {
2381 case INTEGER_TYPE:
2382 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2383 case POINTER_TYPE: case REFERENCE_TYPE:
2384 case REAL_TYPE:
2385 case FIXED_POINT_TYPE:
2386 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2387 fold_convert_loc (loc, TREE_TYPE (type), arg),
2388 fold_convert_loc (loc, TREE_TYPE (type),
2389 integer_zero_node));
2390 case COMPLEX_TYPE:
2391 {
2392 tree rpart, ipart;
2393
2394 if (TREE_CODE (arg) == COMPLEX_EXPR)
2395 {
2396 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2397 TREE_OPERAND (arg, 0));
2398 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2399 TREE_OPERAND (arg, 1));
2400 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2401 }
2402
2403 arg = save_expr (arg);
2404 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2405 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2406 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2407 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2408 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2409 }
2410
2411 default:
2412 gcc_unreachable ();
2413 }
2414
2415 case VECTOR_TYPE:
2416 if (integer_zerop (arg))
2417 return build_zero_vector (type);
2418 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2419 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2420 || TREE_CODE (orig) == VECTOR_TYPE);
2421 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2422
2423 case VOID_TYPE:
2424 tem = fold_ignored_result (arg);
2425 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2426
2427 default:
2428 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2429 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2430 gcc_unreachable ();
2431 }
2432 fold_convert_exit:
2433 protected_set_expr_location_unshare (tem, loc);
2434 return tem;
2435 }
2436 \f
2437 /* Return false if expr can be assumed not to be an lvalue, true
2438 otherwise. */
2439
2440 static bool
2441 maybe_lvalue_p (const_tree x)
2442 {
2443 /* We only need to wrap lvalue tree codes. */
2444 switch (TREE_CODE (x))
2445 {
2446 case VAR_DECL:
2447 case PARM_DECL:
2448 case RESULT_DECL:
2449 case LABEL_DECL:
2450 case FUNCTION_DECL:
2451 case SSA_NAME:
2452
2453 case COMPONENT_REF:
2454 case MEM_REF:
2455 case INDIRECT_REF:
2456 case ARRAY_REF:
2457 case ARRAY_RANGE_REF:
2458 case BIT_FIELD_REF:
2459 case OBJ_TYPE_REF:
2460
2461 case REALPART_EXPR:
2462 case IMAGPART_EXPR:
2463 case PREINCREMENT_EXPR:
2464 case PREDECREMENT_EXPR:
2465 case SAVE_EXPR:
2466 case TRY_CATCH_EXPR:
2467 case WITH_CLEANUP_EXPR:
2468 case COMPOUND_EXPR:
2469 case MODIFY_EXPR:
2470 case TARGET_EXPR:
2471 case COND_EXPR:
2472 case BIND_EXPR:
2473 break;
2474
2475 default:
2476 /* Assume the worst for front-end tree codes. */
2477 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2478 break;
2479 return false;
2480 }
2481
2482 return true;
2483 }
2484
2485 /* Return an expr equal to X but certainly not valid as an lvalue. */
2486
2487 tree
2488 non_lvalue_loc (location_t loc, tree x)
2489 {
2490 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2491 us. */
2492 if (in_gimple_form)
2493 return x;
2494
2495 if (! maybe_lvalue_p (x))
2496 return x;
2497 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2498 }
2499
2500 /* When pedantic, return an expr equal to X but certainly not valid as a
2501 pedantic lvalue. Otherwise, return X. */
2502
2503 static tree
2504 pedantic_non_lvalue_loc (location_t loc, tree x)
2505 {
2506 return protected_set_expr_location_unshare (x, loc);
2507 }
2508 \f
2509 /* Given a tree comparison code, return the code that is the logical inverse.
2510 It is generally not safe to do this for floating-point comparisons, except
2511 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2512 ERROR_MARK in this case. */
2513
2514 enum tree_code
2515 invert_tree_comparison (enum tree_code code, bool honor_nans)
2516 {
2517 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2518 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2519 return ERROR_MARK;
2520
2521 switch (code)
2522 {
2523 case EQ_EXPR:
2524 return NE_EXPR;
2525 case NE_EXPR:
2526 return EQ_EXPR;
2527 case GT_EXPR:
2528 return honor_nans ? UNLE_EXPR : LE_EXPR;
2529 case GE_EXPR:
2530 return honor_nans ? UNLT_EXPR : LT_EXPR;
2531 case LT_EXPR:
2532 return honor_nans ? UNGE_EXPR : GE_EXPR;
2533 case LE_EXPR:
2534 return honor_nans ? UNGT_EXPR : GT_EXPR;
2535 case LTGT_EXPR:
2536 return UNEQ_EXPR;
2537 case UNEQ_EXPR:
2538 return LTGT_EXPR;
2539 case UNGT_EXPR:
2540 return LE_EXPR;
2541 case UNGE_EXPR:
2542 return LT_EXPR;
2543 case UNLT_EXPR:
2544 return GE_EXPR;
2545 case UNLE_EXPR:
2546 return GT_EXPR;
2547 case ORDERED_EXPR:
2548 return UNORDERED_EXPR;
2549 case UNORDERED_EXPR:
2550 return ORDERED_EXPR;
2551 default:
2552 gcc_unreachable ();
2553 }
2554 }
2555
2556 /* Similar, but return the comparison that results if the operands are
2557 swapped. This is safe for floating-point. */
2558
2559 enum tree_code
2560 swap_tree_comparison (enum tree_code code)
2561 {
2562 switch (code)
2563 {
2564 case EQ_EXPR:
2565 case NE_EXPR:
2566 case ORDERED_EXPR:
2567 case UNORDERED_EXPR:
2568 case LTGT_EXPR:
2569 case UNEQ_EXPR:
2570 return code;
2571 case GT_EXPR:
2572 return LT_EXPR;
2573 case GE_EXPR:
2574 return LE_EXPR;
2575 case LT_EXPR:
2576 return GT_EXPR;
2577 case LE_EXPR:
2578 return GE_EXPR;
2579 case UNGT_EXPR:
2580 return UNLT_EXPR;
2581 case UNGE_EXPR:
2582 return UNLE_EXPR;
2583 case UNLT_EXPR:
2584 return UNGT_EXPR;
2585 case UNLE_EXPR:
2586 return UNGE_EXPR;
2587 default:
2588 gcc_unreachable ();
2589 }
2590 }
2591
2592
2593 /* Convert a comparison tree code from an enum tree_code representation
2594 into a compcode bit-based encoding. This function is the inverse of
2595 compcode_to_comparison. */
2596
2597 static enum comparison_code
2598 comparison_to_compcode (enum tree_code code)
2599 {
2600 switch (code)
2601 {
2602 case LT_EXPR:
2603 return COMPCODE_LT;
2604 case EQ_EXPR:
2605 return COMPCODE_EQ;
2606 case LE_EXPR:
2607 return COMPCODE_LE;
2608 case GT_EXPR:
2609 return COMPCODE_GT;
2610 case NE_EXPR:
2611 return COMPCODE_NE;
2612 case GE_EXPR:
2613 return COMPCODE_GE;
2614 case ORDERED_EXPR:
2615 return COMPCODE_ORD;
2616 case UNORDERED_EXPR:
2617 return COMPCODE_UNORD;
2618 case UNLT_EXPR:
2619 return COMPCODE_UNLT;
2620 case UNEQ_EXPR:
2621 return COMPCODE_UNEQ;
2622 case UNLE_EXPR:
2623 return COMPCODE_UNLE;
2624 case UNGT_EXPR:
2625 return COMPCODE_UNGT;
2626 case LTGT_EXPR:
2627 return COMPCODE_LTGT;
2628 case UNGE_EXPR:
2629 return COMPCODE_UNGE;
2630 default:
2631 gcc_unreachable ();
2632 }
2633 }
2634
2635 /* Convert a compcode bit-based encoding of a comparison operator back
2636 to GCC's enum tree_code representation. This function is the
2637 inverse of comparison_to_compcode. */
2638
2639 static enum tree_code
2640 compcode_to_comparison (enum comparison_code code)
2641 {
2642 switch (code)
2643 {
2644 case COMPCODE_LT:
2645 return LT_EXPR;
2646 case COMPCODE_EQ:
2647 return EQ_EXPR;
2648 case COMPCODE_LE:
2649 return LE_EXPR;
2650 case COMPCODE_GT:
2651 return GT_EXPR;
2652 case COMPCODE_NE:
2653 return NE_EXPR;
2654 case COMPCODE_GE:
2655 return GE_EXPR;
2656 case COMPCODE_ORD:
2657 return ORDERED_EXPR;
2658 case COMPCODE_UNORD:
2659 return UNORDERED_EXPR;
2660 case COMPCODE_UNLT:
2661 return UNLT_EXPR;
2662 case COMPCODE_UNEQ:
2663 return UNEQ_EXPR;
2664 case COMPCODE_UNLE:
2665 return UNLE_EXPR;
2666 case COMPCODE_UNGT:
2667 return UNGT_EXPR;
2668 case COMPCODE_LTGT:
2669 return LTGT_EXPR;
2670 case COMPCODE_UNGE:
2671 return UNGE_EXPR;
2672 default:
2673 gcc_unreachable ();
2674 }
2675 }
2676
2677 /* Return a tree for the comparison which is the combination of
2678 doing the AND or OR (depending on CODE) of the two operations LCODE
2679 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2680 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2681 if this makes the transformation invalid. */
2682
2683 tree
2684 combine_comparisons (location_t loc,
2685 enum tree_code code, enum tree_code lcode,
2686 enum tree_code rcode, tree truth_type,
2687 tree ll_arg, tree lr_arg)
2688 {
2689 bool honor_nans = HONOR_NANS (ll_arg);
2690 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2691 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2692 int compcode;
2693
2694 switch (code)
2695 {
2696 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2697 compcode = lcompcode & rcompcode;
2698 break;
2699
2700 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2701 compcode = lcompcode | rcompcode;
2702 break;
2703
2704 default:
2705 return NULL_TREE;
2706 }
2707
2708 if (!honor_nans)
2709 {
2710 /* Eliminate unordered comparisons, as well as LTGT and ORD
2711 which are not used unless the mode has NaNs. */
2712 compcode &= ~COMPCODE_UNORD;
2713 if (compcode == COMPCODE_LTGT)
2714 compcode = COMPCODE_NE;
2715 else if (compcode == COMPCODE_ORD)
2716 compcode = COMPCODE_TRUE;
2717 }
2718 else if (flag_trapping_math)
2719 {
2720 /* Check that the original operation and the optimized ones will trap
2721 under the same condition. */
2722 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2723 && (lcompcode != COMPCODE_EQ)
2724 && (lcompcode != COMPCODE_ORD);
2725 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2726 && (rcompcode != COMPCODE_EQ)
2727 && (rcompcode != COMPCODE_ORD);
2728 bool trap = (compcode & COMPCODE_UNORD) == 0
2729 && (compcode != COMPCODE_EQ)
2730 && (compcode != COMPCODE_ORD);
2731
2732 /* In a short-circuited boolean expression the LHS might be
2733 such that the RHS, if evaluated, will never trap. For
2734 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2735 if neither x nor y is NaN. (This is a mixed blessing: for
2736 example, the expression above will never trap, hence
2737 optimizing it to x < y would be invalid). */
2738 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2739 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2740 rtrap = false;
2741
2742 /* If the comparison was short-circuited, and only the RHS
2743 trapped, we may now generate a spurious trap. */
2744 if (rtrap && !ltrap
2745 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2746 return NULL_TREE;
2747
2748 /* If we changed the conditions that cause a trap, we lose. */
2749 if ((ltrap || rtrap) != trap)
2750 return NULL_TREE;
2751 }
2752
2753 if (compcode == COMPCODE_TRUE)
2754 return constant_boolean_node (true, truth_type);
2755 else if (compcode == COMPCODE_FALSE)
2756 return constant_boolean_node (false, truth_type);
2757 else
2758 {
2759 enum tree_code tcode;
2760
2761 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2762 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2763 }
2764 }
2765 \f
2766 /* Return nonzero if two operands (typically of the same tree node)
2767 are necessarily equal. FLAGS modifies behavior as follows:
2768
2769 If OEP_ONLY_CONST is set, only return nonzero for constants.
2770 This function tests whether the operands are indistinguishable;
2771 it does not test whether they are equal using C's == operation.
2772 The distinction is important for IEEE floating point, because
2773 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2774 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2775
2776 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2777 even though it may hold multiple values during a function.
2778 This is because a GCC tree node guarantees that nothing else is
2779 executed between the evaluation of its "operands" (which may often
2780 be evaluated in arbitrary order). Hence if the operands themselves
2781 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2782 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2783 unset means assuming isochronic (or instantaneous) tree equivalence.
2784 Unless comparing arbitrary expression trees, such as from different
2785 statements, this flag can usually be left unset.
2786
2787 If OEP_PURE_SAME is set, then pure functions with identical arguments
2788 are considered the same. It is used when the caller has other ways
2789 to ensure that global memory is unchanged in between.
2790
2791 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2792 not values of expressions.
2793
2794 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2795 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2796
2797 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2798 any operand with side effect. This is unnecesarily conservative in the
2799 case we know that arg0 and arg1 are in disjoint code paths (such as in
2800 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2801 addresses with TREE_CONSTANT flag set so we know that &var == &var
2802 even if var is volatile. */
2803
2804 int
2805 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2806 {
2807 /* When checking, verify at the outermost operand_equal_p call that
2808 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2809 hash value. */
2810 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2811 {
2812 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2813 {
2814 if (arg0 != arg1)
2815 {
2816 inchash::hash hstate0 (0), hstate1 (0);
2817 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2818 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2819 hashval_t h0 = hstate0.end ();
2820 hashval_t h1 = hstate1.end ();
2821 gcc_assert (h0 == h1);
2822 }
2823 return 1;
2824 }
2825 else
2826 return 0;
2827 }
2828
2829 /* If either is ERROR_MARK, they aren't equal. */
2830 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2831 || TREE_TYPE (arg0) == error_mark_node
2832 || TREE_TYPE (arg1) == error_mark_node)
2833 return 0;
2834
2835 /* Similar, if either does not have a type (like a released SSA name),
2836 they aren't equal. */
2837 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2838 return 0;
2839
2840 /* We cannot consider pointers to different address space equal. */
2841 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2842 && POINTER_TYPE_P (TREE_TYPE (arg1))
2843 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2844 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2845 return 0;
2846
2847 /* Check equality of integer constants before bailing out due to
2848 precision differences. */
2849 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2850 {
2851 /* Address of INTEGER_CST is not defined; check that we did not forget
2852 to drop the OEP_ADDRESS_OF flags. */
2853 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2854 return tree_int_cst_equal (arg0, arg1);
2855 }
2856
2857 if (!(flags & OEP_ADDRESS_OF))
2858 {
2859 /* If both types don't have the same signedness, then we can't consider
2860 them equal. We must check this before the STRIP_NOPS calls
2861 because they may change the signedness of the arguments. As pointers
2862 strictly don't have a signedness, require either two pointers or
2863 two non-pointers as well. */
2864 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2865 || POINTER_TYPE_P (TREE_TYPE (arg0))
2866 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2867 return 0;
2868
2869 /* If both types don't have the same precision, then it is not safe
2870 to strip NOPs. */
2871 if (element_precision (TREE_TYPE (arg0))
2872 != element_precision (TREE_TYPE (arg1)))
2873 return 0;
2874
2875 STRIP_NOPS (arg0);
2876 STRIP_NOPS (arg1);
2877 }
2878 #if 0
2879 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2880 sanity check once the issue is solved. */
2881 else
2882 /* Addresses of conversions and SSA_NAMEs (and many other things)
2883 are not defined. Check that we did not forget to drop the
2884 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2885 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2886 && TREE_CODE (arg0) != SSA_NAME);
2887 #endif
2888
2889 /* In case both args are comparisons but with different comparison
2890 code, try to swap the comparison operands of one arg to produce
2891 a match and compare that variant. */
2892 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2893 && COMPARISON_CLASS_P (arg0)
2894 && COMPARISON_CLASS_P (arg1))
2895 {
2896 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2897
2898 if (TREE_CODE (arg0) == swap_code)
2899 return operand_equal_p (TREE_OPERAND (arg0, 0),
2900 TREE_OPERAND (arg1, 1), flags)
2901 && operand_equal_p (TREE_OPERAND (arg0, 1),
2902 TREE_OPERAND (arg1, 0), flags);
2903 }
2904
2905 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2906 {
2907 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2908 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2909 ;
2910 else if (flags & OEP_ADDRESS_OF)
2911 {
2912 /* If we are interested in comparing addresses ignore
2913 MEM_REF wrappings of the base that can appear just for
2914 TBAA reasons. */
2915 if (TREE_CODE (arg0) == MEM_REF
2916 && DECL_P (arg1)
2917 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2918 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2919 && integer_zerop (TREE_OPERAND (arg0, 1)))
2920 return 1;
2921 else if (TREE_CODE (arg1) == MEM_REF
2922 && DECL_P (arg0)
2923 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2924 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2925 && integer_zerop (TREE_OPERAND (arg1, 1)))
2926 return 1;
2927 return 0;
2928 }
2929 else
2930 return 0;
2931 }
2932
2933 /* When not checking adddresses, this is needed for conversions and for
2934 COMPONENT_REF. Might as well play it safe and always test this. */
2935 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2936 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2937 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2938 && !(flags & OEP_ADDRESS_OF)))
2939 return 0;
2940
2941 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2942 We don't care about side effects in that case because the SAVE_EXPR
2943 takes care of that for us. In all other cases, two expressions are
2944 equal if they have no side effects. If we have two identical
2945 expressions with side effects that should be treated the same due
2946 to the only side effects being identical SAVE_EXPR's, that will
2947 be detected in the recursive calls below.
2948 If we are taking an invariant address of two identical objects
2949 they are necessarily equal as well. */
2950 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2951 && (TREE_CODE (arg0) == SAVE_EXPR
2952 || (flags & OEP_MATCH_SIDE_EFFECTS)
2953 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2954 return 1;
2955
2956 /* Next handle constant cases, those for which we can return 1 even
2957 if ONLY_CONST is set. */
2958 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2959 switch (TREE_CODE (arg0))
2960 {
2961 case INTEGER_CST:
2962 return tree_int_cst_equal (arg0, arg1);
2963
2964 case FIXED_CST:
2965 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2966 TREE_FIXED_CST (arg1));
2967
2968 case REAL_CST:
2969 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2970 return 1;
2971
2972
2973 if (!HONOR_SIGNED_ZEROS (arg0))
2974 {
2975 /* If we do not distinguish between signed and unsigned zero,
2976 consider them equal. */
2977 if (real_zerop (arg0) && real_zerop (arg1))
2978 return 1;
2979 }
2980 return 0;
2981
2982 case VECTOR_CST:
2983 {
2984 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
2985 != VECTOR_CST_LOG2_NPATTERNS (arg1))
2986 return 0;
2987
2988 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
2989 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
2990 return 0;
2991
2992 unsigned int count = vector_cst_encoded_nelts (arg0);
2993 for (unsigned int i = 0; i < count; ++i)
2994 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
2995 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
2996 return 0;
2997 return 1;
2998 }
2999
3000 case COMPLEX_CST:
3001 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3002 flags)
3003 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3004 flags));
3005
3006 case STRING_CST:
3007 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3008 && ! memcmp (TREE_STRING_POINTER (arg0),
3009 TREE_STRING_POINTER (arg1),
3010 TREE_STRING_LENGTH (arg0)));
3011
3012 case ADDR_EXPR:
3013 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3014 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3015 flags | OEP_ADDRESS_OF
3016 | OEP_MATCH_SIDE_EFFECTS);
3017 case CONSTRUCTOR:
3018 /* In GIMPLE empty constructors are allowed in initializers of
3019 aggregates. */
3020 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3021 default:
3022 break;
3023 }
3024
3025 if (flags & OEP_ONLY_CONST)
3026 return 0;
3027
3028 /* Define macros to test an operand from arg0 and arg1 for equality and a
3029 variant that allows null and views null as being different from any
3030 non-null value. In the latter case, if either is null, the both
3031 must be; otherwise, do the normal comparison. */
3032 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3033 TREE_OPERAND (arg1, N), flags)
3034
3035 #define OP_SAME_WITH_NULL(N) \
3036 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3037 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3038
3039 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3040 {
3041 case tcc_unary:
3042 /* Two conversions are equal only if signedness and modes match. */
3043 switch (TREE_CODE (arg0))
3044 {
3045 CASE_CONVERT:
3046 case FIX_TRUNC_EXPR:
3047 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3048 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3049 return 0;
3050 break;
3051 default:
3052 break;
3053 }
3054
3055 return OP_SAME (0);
3056
3057
3058 case tcc_comparison:
3059 case tcc_binary:
3060 if (OP_SAME (0) && OP_SAME (1))
3061 return 1;
3062
3063 /* For commutative ops, allow the other order. */
3064 return (commutative_tree_code (TREE_CODE (arg0))
3065 && operand_equal_p (TREE_OPERAND (arg0, 0),
3066 TREE_OPERAND (arg1, 1), flags)
3067 && operand_equal_p (TREE_OPERAND (arg0, 1),
3068 TREE_OPERAND (arg1, 0), flags));
3069
3070 case tcc_reference:
3071 /* If either of the pointer (or reference) expressions we are
3072 dereferencing contain a side effect, these cannot be equal,
3073 but their addresses can be. */
3074 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3075 && (TREE_SIDE_EFFECTS (arg0)
3076 || TREE_SIDE_EFFECTS (arg1)))
3077 return 0;
3078
3079 switch (TREE_CODE (arg0))
3080 {
3081 case INDIRECT_REF:
3082 if (!(flags & OEP_ADDRESS_OF)
3083 && (TYPE_ALIGN (TREE_TYPE (arg0))
3084 != TYPE_ALIGN (TREE_TYPE (arg1))))
3085 return 0;
3086 flags &= ~OEP_ADDRESS_OF;
3087 return OP_SAME (0);
3088
3089 case IMAGPART_EXPR:
3090 /* Require the same offset. */
3091 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3092 TYPE_SIZE (TREE_TYPE (arg1)),
3093 flags & ~OEP_ADDRESS_OF))
3094 return 0;
3095
3096 /* Fallthru. */
3097 case REALPART_EXPR:
3098 case VIEW_CONVERT_EXPR:
3099 return OP_SAME (0);
3100
3101 case TARGET_MEM_REF:
3102 case MEM_REF:
3103 if (!(flags & OEP_ADDRESS_OF))
3104 {
3105 /* Require equal access sizes */
3106 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3107 && (!TYPE_SIZE (TREE_TYPE (arg0))
3108 || !TYPE_SIZE (TREE_TYPE (arg1))
3109 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3110 TYPE_SIZE (TREE_TYPE (arg1)),
3111 flags)))
3112 return 0;
3113 /* Verify that access happens in similar types. */
3114 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3115 return 0;
3116 /* Verify that accesses are TBAA compatible. */
3117 if (!alias_ptr_types_compatible_p
3118 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3119 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3120 || (MR_DEPENDENCE_CLIQUE (arg0)
3121 != MR_DEPENDENCE_CLIQUE (arg1))
3122 || (MR_DEPENDENCE_BASE (arg0)
3123 != MR_DEPENDENCE_BASE (arg1)))
3124 return 0;
3125 /* Verify that alignment is compatible. */
3126 if (TYPE_ALIGN (TREE_TYPE (arg0))
3127 != TYPE_ALIGN (TREE_TYPE (arg1)))
3128 return 0;
3129 }
3130 flags &= ~OEP_ADDRESS_OF;
3131 return (OP_SAME (0) && OP_SAME (1)
3132 /* TARGET_MEM_REF require equal extra operands. */
3133 && (TREE_CODE (arg0) != TARGET_MEM_REF
3134 || (OP_SAME_WITH_NULL (2)
3135 && OP_SAME_WITH_NULL (3)
3136 && OP_SAME_WITH_NULL (4))));
3137
3138 case ARRAY_REF:
3139 case ARRAY_RANGE_REF:
3140 if (!OP_SAME (0))
3141 return 0;
3142 flags &= ~OEP_ADDRESS_OF;
3143 /* Compare the array index by value if it is constant first as we
3144 may have different types but same value here. */
3145 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3146 TREE_OPERAND (arg1, 1))
3147 || OP_SAME (1))
3148 && OP_SAME_WITH_NULL (2)
3149 && OP_SAME_WITH_NULL (3)
3150 /* Compare low bound and element size as with OEP_ADDRESS_OF
3151 we have to account for the offset of the ref. */
3152 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3153 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3154 || (operand_equal_p (array_ref_low_bound
3155 (CONST_CAST_TREE (arg0)),
3156 array_ref_low_bound
3157 (CONST_CAST_TREE (arg1)), flags)
3158 && operand_equal_p (array_ref_element_size
3159 (CONST_CAST_TREE (arg0)),
3160 array_ref_element_size
3161 (CONST_CAST_TREE (arg1)),
3162 flags))));
3163
3164 case COMPONENT_REF:
3165 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3166 may be NULL when we're called to compare MEM_EXPRs. */
3167 if (!OP_SAME_WITH_NULL (0)
3168 || !OP_SAME (1))
3169 return 0;
3170 flags &= ~OEP_ADDRESS_OF;
3171 return OP_SAME_WITH_NULL (2);
3172
3173 case BIT_FIELD_REF:
3174 if (!OP_SAME (0))
3175 return 0;
3176 flags &= ~OEP_ADDRESS_OF;
3177 return OP_SAME (1) && OP_SAME (2);
3178
3179 default:
3180 return 0;
3181 }
3182
3183 case tcc_expression:
3184 switch (TREE_CODE (arg0))
3185 {
3186 case ADDR_EXPR:
3187 /* Be sure we pass right ADDRESS_OF flag. */
3188 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3189 return operand_equal_p (TREE_OPERAND (arg0, 0),
3190 TREE_OPERAND (arg1, 0),
3191 flags | OEP_ADDRESS_OF);
3192
3193 case TRUTH_NOT_EXPR:
3194 return OP_SAME (0);
3195
3196 case TRUTH_ANDIF_EXPR:
3197 case TRUTH_ORIF_EXPR:
3198 return OP_SAME (0) && OP_SAME (1);
3199
3200 case FMA_EXPR:
3201 case WIDEN_MULT_PLUS_EXPR:
3202 case WIDEN_MULT_MINUS_EXPR:
3203 if (!OP_SAME (2))
3204 return 0;
3205 /* The multiplcation operands are commutative. */
3206 /* FALLTHRU */
3207
3208 case TRUTH_AND_EXPR:
3209 case TRUTH_OR_EXPR:
3210 case TRUTH_XOR_EXPR:
3211 if (OP_SAME (0) && OP_SAME (1))
3212 return 1;
3213
3214 /* Otherwise take into account this is a commutative operation. */
3215 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3216 TREE_OPERAND (arg1, 1), flags)
3217 && operand_equal_p (TREE_OPERAND (arg0, 1),
3218 TREE_OPERAND (arg1, 0), flags));
3219
3220 case COND_EXPR:
3221 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3222 return 0;
3223 flags &= ~OEP_ADDRESS_OF;
3224 return OP_SAME (0);
3225
3226 case BIT_INSERT_EXPR:
3227 /* BIT_INSERT_EXPR has an implict operand as the type precision
3228 of op1. Need to check to make sure they are the same. */
3229 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3230 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3231 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3232 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3233 return false;
3234 /* FALLTHRU */
3235
3236 case VEC_COND_EXPR:
3237 case DOT_PROD_EXPR:
3238 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3239
3240 case MODIFY_EXPR:
3241 case INIT_EXPR:
3242 case COMPOUND_EXPR:
3243 case PREDECREMENT_EXPR:
3244 case PREINCREMENT_EXPR:
3245 case POSTDECREMENT_EXPR:
3246 case POSTINCREMENT_EXPR:
3247 if (flags & OEP_LEXICOGRAPHIC)
3248 return OP_SAME (0) && OP_SAME (1);
3249 return 0;
3250
3251 case CLEANUP_POINT_EXPR:
3252 case EXPR_STMT:
3253 if (flags & OEP_LEXICOGRAPHIC)
3254 return OP_SAME (0);
3255 return 0;
3256
3257 default:
3258 return 0;
3259 }
3260
3261 case tcc_vl_exp:
3262 switch (TREE_CODE (arg0))
3263 {
3264 case CALL_EXPR:
3265 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3266 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3267 /* If not both CALL_EXPRs are either internal or normal function
3268 functions, then they are not equal. */
3269 return 0;
3270 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3271 {
3272 /* If the CALL_EXPRs call different internal functions, then they
3273 are not equal. */
3274 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3275 return 0;
3276 }
3277 else
3278 {
3279 /* If the CALL_EXPRs call different functions, then they are not
3280 equal. */
3281 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3282 flags))
3283 return 0;
3284 }
3285
3286 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3287 {
3288 unsigned int cef = call_expr_flags (arg0);
3289 if (flags & OEP_PURE_SAME)
3290 cef &= ECF_CONST | ECF_PURE;
3291 else
3292 cef &= ECF_CONST;
3293 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3294 return 0;
3295 }
3296
3297 /* Now see if all the arguments are the same. */
3298 {
3299 const_call_expr_arg_iterator iter0, iter1;
3300 const_tree a0, a1;
3301 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3302 a1 = first_const_call_expr_arg (arg1, &iter1);
3303 a0 && a1;
3304 a0 = next_const_call_expr_arg (&iter0),
3305 a1 = next_const_call_expr_arg (&iter1))
3306 if (! operand_equal_p (a0, a1, flags))
3307 return 0;
3308
3309 /* If we get here and both argument lists are exhausted
3310 then the CALL_EXPRs are equal. */
3311 return ! (a0 || a1);
3312 }
3313 default:
3314 return 0;
3315 }
3316
3317 case tcc_declaration:
3318 /* Consider __builtin_sqrt equal to sqrt. */
3319 return (TREE_CODE (arg0) == FUNCTION_DECL
3320 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3321 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3322 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3323
3324 case tcc_exceptional:
3325 if (TREE_CODE (arg0) == CONSTRUCTOR)
3326 {
3327 /* In GIMPLE constructors are used only to build vectors from
3328 elements. Individual elements in the constructor must be
3329 indexed in increasing order and form an initial sequence.
3330
3331 We make no effort to compare constructors in generic.
3332 (see sem_variable::equals in ipa-icf which can do so for
3333 constants). */
3334 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3335 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3336 return 0;
3337
3338 /* Be sure that vectors constructed have the same representation.
3339 We only tested element precision and modes to match.
3340 Vectors may be BLKmode and thus also check that the number of
3341 parts match. */
3342 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3343 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3344 return 0;
3345
3346 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3347 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3348 unsigned int len = vec_safe_length (v0);
3349
3350 if (len != vec_safe_length (v1))
3351 return 0;
3352
3353 for (unsigned int i = 0; i < len; i++)
3354 {
3355 constructor_elt *c0 = &(*v0)[i];
3356 constructor_elt *c1 = &(*v1)[i];
3357
3358 if (!operand_equal_p (c0->value, c1->value, flags)
3359 /* In GIMPLE the indexes can be either NULL or matching i.
3360 Double check this so we won't get false
3361 positives for GENERIC. */
3362 || (c0->index
3363 && (TREE_CODE (c0->index) != INTEGER_CST
3364 || !compare_tree_int (c0->index, i)))
3365 || (c1->index
3366 && (TREE_CODE (c1->index) != INTEGER_CST
3367 || !compare_tree_int (c1->index, i))))
3368 return 0;
3369 }
3370 return 1;
3371 }
3372 else if (TREE_CODE (arg0) == STATEMENT_LIST
3373 && (flags & OEP_LEXICOGRAPHIC))
3374 {
3375 /* Compare the STATEMENT_LISTs. */
3376 tree_stmt_iterator tsi1, tsi2;
3377 tree body1 = CONST_CAST_TREE (arg0);
3378 tree body2 = CONST_CAST_TREE (arg1);
3379 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3380 tsi_next (&tsi1), tsi_next (&tsi2))
3381 {
3382 /* The lists don't have the same number of statements. */
3383 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3384 return 0;
3385 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3386 return 1;
3387 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3388 OEP_LEXICOGRAPHIC))
3389 return 0;
3390 }
3391 }
3392 return 0;
3393
3394 case tcc_statement:
3395 switch (TREE_CODE (arg0))
3396 {
3397 case RETURN_EXPR:
3398 if (flags & OEP_LEXICOGRAPHIC)
3399 return OP_SAME_WITH_NULL (0);
3400 return 0;
3401 default:
3402 return 0;
3403 }
3404
3405 default:
3406 return 0;
3407 }
3408
3409 #undef OP_SAME
3410 #undef OP_SAME_WITH_NULL
3411 }
3412 \f
3413 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3414 with a different signedness or a narrower precision. */
3415
3416 static bool
3417 operand_equal_for_comparison_p (tree arg0, tree arg1)
3418 {
3419 if (operand_equal_p (arg0, arg1, 0))
3420 return true;
3421
3422 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3423 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3424 return false;
3425
3426 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3427 and see if the inner values are the same. This removes any
3428 signedness comparison, which doesn't matter here. */
3429 tree op0 = arg0;
3430 tree op1 = arg1;
3431 STRIP_NOPS (op0);
3432 STRIP_NOPS (op1);
3433 if (operand_equal_p (op0, op1, 0))
3434 return true;
3435
3436 /* Discard a single widening conversion from ARG1 and see if the inner
3437 value is the same as ARG0. */
3438 if (CONVERT_EXPR_P (arg1)
3439 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3440 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3441 < TYPE_PRECISION (TREE_TYPE (arg1))
3442 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3443 return true;
3444
3445 return false;
3446 }
3447 \f
3448 /* See if ARG is an expression that is either a comparison or is performing
3449 arithmetic on comparisons. The comparisons must only be comparing
3450 two different values, which will be stored in *CVAL1 and *CVAL2; if
3451 they are nonzero it means that some operands have already been found.
3452 No variables may be used anywhere else in the expression except in the
3453 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3454 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3455
3456 If this is true, return 1. Otherwise, return zero. */
3457
3458 static int
3459 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3460 {
3461 enum tree_code code = TREE_CODE (arg);
3462 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3463
3464 /* We can handle some of the tcc_expression cases here. */
3465 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3466 tclass = tcc_unary;
3467 else if (tclass == tcc_expression
3468 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3469 || code == COMPOUND_EXPR))
3470 tclass = tcc_binary;
3471
3472 else if (tclass == tcc_expression && code == SAVE_EXPR
3473 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3474 {
3475 /* If we've already found a CVAL1 or CVAL2, this expression is
3476 two complex to handle. */
3477 if (*cval1 || *cval2)
3478 return 0;
3479
3480 tclass = tcc_unary;
3481 *save_p = 1;
3482 }
3483
3484 switch (tclass)
3485 {
3486 case tcc_unary:
3487 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3488
3489 case tcc_binary:
3490 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3491 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3492 cval1, cval2, save_p));
3493
3494 case tcc_constant:
3495 return 1;
3496
3497 case tcc_expression:
3498 if (code == COND_EXPR)
3499 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3500 cval1, cval2, save_p)
3501 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3502 cval1, cval2, save_p)
3503 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3504 cval1, cval2, save_p));
3505 return 0;
3506
3507 case tcc_comparison:
3508 /* First see if we can handle the first operand, then the second. For
3509 the second operand, we know *CVAL1 can't be zero. It must be that
3510 one side of the comparison is each of the values; test for the
3511 case where this isn't true by failing if the two operands
3512 are the same. */
3513
3514 if (operand_equal_p (TREE_OPERAND (arg, 0),
3515 TREE_OPERAND (arg, 1), 0))
3516 return 0;
3517
3518 if (*cval1 == 0)
3519 *cval1 = TREE_OPERAND (arg, 0);
3520 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3521 ;
3522 else if (*cval2 == 0)
3523 *cval2 = TREE_OPERAND (arg, 0);
3524 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3525 ;
3526 else
3527 return 0;
3528
3529 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3530 ;
3531 else if (*cval2 == 0)
3532 *cval2 = TREE_OPERAND (arg, 1);
3533 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3534 ;
3535 else
3536 return 0;
3537
3538 return 1;
3539
3540 default:
3541 return 0;
3542 }
3543 }
3544 \f
3545 /* ARG is a tree that is known to contain just arithmetic operations and
3546 comparisons. Evaluate the operations in the tree substituting NEW0 for
3547 any occurrence of OLD0 as an operand of a comparison and likewise for
3548 NEW1 and OLD1. */
3549
3550 static tree
3551 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3552 tree old1, tree new1)
3553 {
3554 tree type = TREE_TYPE (arg);
3555 enum tree_code code = TREE_CODE (arg);
3556 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3557
3558 /* We can handle some of the tcc_expression cases here. */
3559 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3560 tclass = tcc_unary;
3561 else if (tclass == tcc_expression
3562 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3563 tclass = tcc_binary;
3564
3565 switch (tclass)
3566 {
3567 case tcc_unary:
3568 return fold_build1_loc (loc, code, type,
3569 eval_subst (loc, TREE_OPERAND (arg, 0),
3570 old0, new0, old1, new1));
3571
3572 case tcc_binary:
3573 return fold_build2_loc (loc, code, type,
3574 eval_subst (loc, TREE_OPERAND (arg, 0),
3575 old0, new0, old1, new1),
3576 eval_subst (loc, TREE_OPERAND (arg, 1),
3577 old0, new0, old1, new1));
3578
3579 case tcc_expression:
3580 switch (code)
3581 {
3582 case SAVE_EXPR:
3583 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3584 old1, new1);
3585
3586 case COMPOUND_EXPR:
3587 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3588 old1, new1);
3589
3590 case COND_EXPR:
3591 return fold_build3_loc (loc, code, type,
3592 eval_subst (loc, TREE_OPERAND (arg, 0),
3593 old0, new0, old1, new1),
3594 eval_subst (loc, TREE_OPERAND (arg, 1),
3595 old0, new0, old1, new1),
3596 eval_subst (loc, TREE_OPERAND (arg, 2),
3597 old0, new0, old1, new1));
3598 default:
3599 break;
3600 }
3601 /* Fall through - ??? */
3602
3603 case tcc_comparison:
3604 {
3605 tree arg0 = TREE_OPERAND (arg, 0);
3606 tree arg1 = TREE_OPERAND (arg, 1);
3607
3608 /* We need to check both for exact equality and tree equality. The
3609 former will be true if the operand has a side-effect. In that
3610 case, we know the operand occurred exactly once. */
3611
3612 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3613 arg0 = new0;
3614 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3615 arg0 = new1;
3616
3617 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3618 arg1 = new0;
3619 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3620 arg1 = new1;
3621
3622 return fold_build2_loc (loc, code, type, arg0, arg1);
3623 }
3624
3625 default:
3626 return arg;
3627 }
3628 }
3629 \f
3630 /* Return a tree for the case when the result of an expression is RESULT
3631 converted to TYPE and OMITTED was previously an operand of the expression
3632 but is now not needed (e.g., we folded OMITTED * 0).
3633
3634 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3635 the conversion of RESULT to TYPE. */
3636
3637 tree
3638 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3639 {
3640 tree t = fold_convert_loc (loc, type, result);
3641
3642 /* If the resulting operand is an empty statement, just return the omitted
3643 statement casted to void. */
3644 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3645 return build1_loc (loc, NOP_EXPR, void_type_node,
3646 fold_ignored_result (omitted));
3647
3648 if (TREE_SIDE_EFFECTS (omitted))
3649 return build2_loc (loc, COMPOUND_EXPR, type,
3650 fold_ignored_result (omitted), t);
3651
3652 return non_lvalue_loc (loc, t);
3653 }
3654
3655 /* Return a tree for the case when the result of an expression is RESULT
3656 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3657 of the expression but are now not needed.
3658
3659 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3660 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3661 evaluated before OMITTED2. Otherwise, if neither has side effects,
3662 just do the conversion of RESULT to TYPE. */
3663
3664 tree
3665 omit_two_operands_loc (location_t loc, tree type, tree result,
3666 tree omitted1, tree omitted2)
3667 {
3668 tree t = fold_convert_loc (loc, type, result);
3669
3670 if (TREE_SIDE_EFFECTS (omitted2))
3671 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3672 if (TREE_SIDE_EFFECTS (omitted1))
3673 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3674
3675 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3676 }
3677
3678 \f
3679 /* Return a simplified tree node for the truth-negation of ARG. This
3680 never alters ARG itself. We assume that ARG is an operation that
3681 returns a truth value (0 or 1).
3682
3683 FIXME: one would think we would fold the result, but it causes
3684 problems with the dominator optimizer. */
3685
3686 static tree
3687 fold_truth_not_expr (location_t loc, tree arg)
3688 {
3689 tree type = TREE_TYPE (arg);
3690 enum tree_code code = TREE_CODE (arg);
3691 location_t loc1, loc2;
3692
3693 /* If this is a comparison, we can simply invert it, except for
3694 floating-point non-equality comparisons, in which case we just
3695 enclose a TRUTH_NOT_EXPR around what we have. */
3696
3697 if (TREE_CODE_CLASS (code) == tcc_comparison)
3698 {
3699 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3700 if (FLOAT_TYPE_P (op_type)
3701 && flag_trapping_math
3702 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3703 && code != NE_EXPR && code != EQ_EXPR)
3704 return NULL_TREE;
3705
3706 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3707 if (code == ERROR_MARK)
3708 return NULL_TREE;
3709
3710 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3711 TREE_OPERAND (arg, 1));
3712 if (TREE_NO_WARNING (arg))
3713 TREE_NO_WARNING (ret) = 1;
3714 return ret;
3715 }
3716
3717 switch (code)
3718 {
3719 case INTEGER_CST:
3720 return constant_boolean_node (integer_zerop (arg), type);
3721
3722 case TRUTH_AND_EXPR:
3723 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3724 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3725 return build2_loc (loc, TRUTH_OR_EXPR, type,
3726 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3727 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3728
3729 case TRUTH_OR_EXPR:
3730 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3731 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3732 return build2_loc (loc, TRUTH_AND_EXPR, type,
3733 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3734 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3735
3736 case TRUTH_XOR_EXPR:
3737 /* Here we can invert either operand. We invert the first operand
3738 unless the second operand is a TRUTH_NOT_EXPR in which case our
3739 result is the XOR of the first operand with the inside of the
3740 negation of the second operand. */
3741
3742 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3743 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3744 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3745 else
3746 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3747 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3748 TREE_OPERAND (arg, 1));
3749
3750 case TRUTH_ANDIF_EXPR:
3751 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3752 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3753 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3754 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3755 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3756
3757 case TRUTH_ORIF_EXPR:
3758 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3759 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3760 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3761 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3762 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3763
3764 case TRUTH_NOT_EXPR:
3765 return TREE_OPERAND (arg, 0);
3766
3767 case COND_EXPR:
3768 {
3769 tree arg1 = TREE_OPERAND (arg, 1);
3770 tree arg2 = TREE_OPERAND (arg, 2);
3771
3772 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3773 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3774
3775 /* A COND_EXPR may have a throw as one operand, which
3776 then has void type. Just leave void operands
3777 as they are. */
3778 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3779 VOID_TYPE_P (TREE_TYPE (arg1))
3780 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3781 VOID_TYPE_P (TREE_TYPE (arg2))
3782 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3783 }
3784
3785 case COMPOUND_EXPR:
3786 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3787 return build2_loc (loc, COMPOUND_EXPR, type,
3788 TREE_OPERAND (arg, 0),
3789 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3790
3791 case NON_LVALUE_EXPR:
3792 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3793 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3794
3795 CASE_CONVERT:
3796 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3797 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3798
3799 /* fall through */
3800
3801 case FLOAT_EXPR:
3802 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3803 return build1_loc (loc, TREE_CODE (arg), type,
3804 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3805
3806 case BIT_AND_EXPR:
3807 if (!integer_onep (TREE_OPERAND (arg, 1)))
3808 return NULL_TREE;
3809 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3810
3811 case SAVE_EXPR:
3812 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3813
3814 case CLEANUP_POINT_EXPR:
3815 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3816 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3817 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3818
3819 default:
3820 return NULL_TREE;
3821 }
3822 }
3823
3824 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3825 assume that ARG is an operation that returns a truth value (0 or 1
3826 for scalars, 0 or -1 for vectors). Return the folded expression if
3827 folding is successful. Otherwise, return NULL_TREE. */
3828
3829 static tree
3830 fold_invert_truthvalue (location_t loc, tree arg)
3831 {
3832 tree type = TREE_TYPE (arg);
3833 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3834 ? BIT_NOT_EXPR
3835 : TRUTH_NOT_EXPR,
3836 type, arg);
3837 }
3838
3839 /* Return a simplified tree node for the truth-negation of ARG. This
3840 never alters ARG itself. We assume that ARG is an operation that
3841 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3842
3843 tree
3844 invert_truthvalue_loc (location_t loc, tree arg)
3845 {
3846 if (TREE_CODE (arg) == ERROR_MARK)
3847 return arg;
3848
3849 tree type = TREE_TYPE (arg);
3850 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3851 ? BIT_NOT_EXPR
3852 : TRUTH_NOT_EXPR,
3853 type, arg);
3854 }
3855 \f
3856 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3857 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3858 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3859 is the original memory reference used to preserve the alias set of
3860 the access. */
3861
3862 static tree
3863 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3864 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3865 int unsignedp, int reversep)
3866 {
3867 tree result, bftype;
3868
3869 /* Attempt not to lose the access path if possible. */
3870 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3871 {
3872 tree ninner = TREE_OPERAND (orig_inner, 0);
3873 machine_mode nmode;
3874 HOST_WIDE_INT nbitsize, nbitpos;
3875 tree noffset;
3876 int nunsignedp, nreversep, nvolatilep = 0;
3877 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3878 &noffset, &nmode, &nunsignedp,
3879 &nreversep, &nvolatilep);
3880 if (base == inner
3881 && noffset == NULL_TREE
3882 && nbitsize >= bitsize
3883 && nbitpos <= bitpos
3884 && bitpos + bitsize <= nbitpos + nbitsize
3885 && !reversep
3886 && !nreversep
3887 && !nvolatilep)
3888 {
3889 inner = ninner;
3890 bitpos -= nbitpos;
3891 }
3892 }
3893
3894 alias_set_type iset = get_alias_set (orig_inner);
3895 if (iset == 0 && get_alias_set (inner) != iset)
3896 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3897 build_fold_addr_expr (inner),
3898 build_int_cst (ptr_type_node, 0));
3899
3900 if (bitpos == 0 && !reversep)
3901 {
3902 tree size = TYPE_SIZE (TREE_TYPE (inner));
3903 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3904 || POINTER_TYPE_P (TREE_TYPE (inner)))
3905 && tree_fits_shwi_p (size)
3906 && tree_to_shwi (size) == bitsize)
3907 return fold_convert_loc (loc, type, inner);
3908 }
3909
3910 bftype = type;
3911 if (TYPE_PRECISION (bftype) != bitsize
3912 || TYPE_UNSIGNED (bftype) == !unsignedp)
3913 bftype = build_nonstandard_integer_type (bitsize, 0);
3914
3915 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3916 bitsize_int (bitsize), bitsize_int (bitpos));
3917 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3918
3919 if (bftype != type)
3920 result = fold_convert_loc (loc, type, result);
3921
3922 return result;
3923 }
3924
3925 /* Optimize a bit-field compare.
3926
3927 There are two cases: First is a compare against a constant and the
3928 second is a comparison of two items where the fields are at the same
3929 bit position relative to the start of a chunk (byte, halfword, word)
3930 large enough to contain it. In these cases we can avoid the shift
3931 implicit in bitfield extractions.
3932
3933 For constants, we emit a compare of the shifted constant with the
3934 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3935 compared. For two fields at the same position, we do the ANDs with the
3936 similar mask and compare the result of the ANDs.
3937
3938 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3939 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3940 are the left and right operands of the comparison, respectively.
3941
3942 If the optimization described above can be done, we return the resulting
3943 tree. Otherwise we return zero. */
3944
3945 static tree
3946 optimize_bit_field_compare (location_t loc, enum tree_code code,
3947 tree compare_type, tree lhs, tree rhs)
3948 {
3949 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3950 tree type = TREE_TYPE (lhs);
3951 tree unsigned_type;
3952 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3953 machine_mode lmode, rmode;
3954 scalar_int_mode nmode;
3955 int lunsignedp, runsignedp;
3956 int lreversep, rreversep;
3957 int lvolatilep = 0, rvolatilep = 0;
3958 tree linner, rinner = NULL_TREE;
3959 tree mask;
3960 tree offset;
3961
3962 /* Get all the information about the extractions being done. If the bit size
3963 if the same as the size of the underlying object, we aren't doing an
3964 extraction at all and so can do nothing. We also don't want to
3965 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3966 then will no longer be able to replace it. */
3967 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3968 &lunsignedp, &lreversep, &lvolatilep);
3969 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3970 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3971 return 0;
3972
3973 if (const_p)
3974 rreversep = lreversep;
3975 else
3976 {
3977 /* If this is not a constant, we can only do something if bit positions,
3978 sizes, signedness and storage order are the same. */
3979 rinner
3980 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3981 &runsignedp, &rreversep, &rvolatilep);
3982
3983 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3984 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3985 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3986 return 0;
3987 }
3988
3989 /* Honor the C++ memory model and mimic what RTL expansion does. */
3990 unsigned HOST_WIDE_INT bitstart = 0;
3991 unsigned HOST_WIDE_INT bitend = 0;
3992 if (TREE_CODE (lhs) == COMPONENT_REF)
3993 {
3994 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
3995 if (offset != NULL_TREE)
3996 return 0;
3997 }
3998
3999 /* See if we can find a mode to refer to this field. We should be able to,
4000 but fail if we can't. */
4001 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4002 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4003 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4004 TYPE_ALIGN (TREE_TYPE (rinner))),
4005 BITS_PER_WORD, false, &nmode))
4006 return 0;
4007
4008 /* Set signed and unsigned types of the precision of this mode for the
4009 shifts below. */
4010 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4011
4012 /* Compute the bit position and size for the new reference and our offset
4013 within it. If the new reference is the same size as the original, we
4014 won't optimize anything, so return zero. */
4015 nbitsize = GET_MODE_BITSIZE (nmode);
4016 nbitpos = lbitpos & ~ (nbitsize - 1);
4017 lbitpos -= nbitpos;
4018 if (nbitsize == lbitsize)
4019 return 0;
4020
4021 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4022 lbitpos = nbitsize - lbitsize - lbitpos;
4023
4024 /* Make the mask to be used against the extracted field. */
4025 mask = build_int_cst_type (unsigned_type, -1);
4026 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4027 mask = const_binop (RSHIFT_EXPR, mask,
4028 size_int (nbitsize - lbitsize - lbitpos));
4029
4030 if (! const_p)
4031 {
4032 if (nbitpos < 0)
4033 return 0;
4034
4035 /* If not comparing with constant, just rework the comparison
4036 and return. */
4037 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4038 nbitsize, nbitpos, 1, lreversep);
4039 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4040 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4041 nbitsize, nbitpos, 1, rreversep);
4042 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4043 return fold_build2_loc (loc, code, compare_type, t1, t2);
4044 }
4045
4046 /* Otherwise, we are handling the constant case. See if the constant is too
4047 big for the field. Warn and return a tree for 0 (false) if so. We do
4048 this not only for its own sake, but to avoid having to test for this
4049 error case below. If we didn't, we might generate wrong code.
4050
4051 For unsigned fields, the constant shifted right by the field length should
4052 be all zero. For signed fields, the high-order bits should agree with
4053 the sign bit. */
4054
4055 if (lunsignedp)
4056 {
4057 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4058 {
4059 warning (0, "comparison is always %d due to width of bit-field",
4060 code == NE_EXPR);
4061 return constant_boolean_node (code == NE_EXPR, compare_type);
4062 }
4063 }
4064 else
4065 {
4066 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4067 if (tem != 0 && tem != -1)
4068 {
4069 warning (0, "comparison is always %d due to width of bit-field",
4070 code == NE_EXPR);
4071 return constant_boolean_node (code == NE_EXPR, compare_type);
4072 }
4073 }
4074
4075 if (nbitpos < 0)
4076 return 0;
4077
4078 /* Single-bit compares should always be against zero. */
4079 if (lbitsize == 1 && ! integer_zerop (rhs))
4080 {
4081 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4082 rhs = build_int_cst (type, 0);
4083 }
4084
4085 /* Make a new bitfield reference, shift the constant over the
4086 appropriate number of bits and mask it with the computed mask
4087 (in case this was a signed field). If we changed it, make a new one. */
4088 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4089 nbitsize, nbitpos, 1, lreversep);
4090
4091 rhs = const_binop (BIT_AND_EXPR,
4092 const_binop (LSHIFT_EXPR,
4093 fold_convert_loc (loc, unsigned_type, rhs),
4094 size_int (lbitpos)),
4095 mask);
4096
4097 lhs = build2_loc (loc, code, compare_type,
4098 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4099 return lhs;
4100 }
4101 \f
4102 /* Subroutine for fold_truth_andor_1: decode a field reference.
4103
4104 If EXP is a comparison reference, we return the innermost reference.
4105
4106 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4107 set to the starting bit number.
4108
4109 If the innermost field can be completely contained in a mode-sized
4110 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4111
4112 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4113 otherwise it is not changed.
4114
4115 *PUNSIGNEDP is set to the signedness of the field.
4116
4117 *PREVERSEP is set to the storage order of the field.
4118
4119 *PMASK is set to the mask used. This is either contained in a
4120 BIT_AND_EXPR or derived from the width of the field.
4121
4122 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4123
4124 Return 0 if this is not a component reference or is one that we can't
4125 do anything with. */
4126
4127 static tree
4128 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4129 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4130 int *punsignedp, int *preversep, int *pvolatilep,
4131 tree *pmask, tree *pand_mask)
4132 {
4133 tree exp = *exp_;
4134 tree outer_type = 0;
4135 tree and_mask = 0;
4136 tree mask, inner, offset;
4137 tree unsigned_type;
4138 unsigned int precision;
4139
4140 /* All the optimizations using this function assume integer fields.
4141 There are problems with FP fields since the type_for_size call
4142 below can fail for, e.g., XFmode. */
4143 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4144 return 0;
4145
4146 /* We are interested in the bare arrangement of bits, so strip everything
4147 that doesn't affect the machine mode. However, record the type of the
4148 outermost expression if it may matter below. */
4149 if (CONVERT_EXPR_P (exp)
4150 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4151 outer_type = TREE_TYPE (exp);
4152 STRIP_NOPS (exp);
4153
4154 if (TREE_CODE (exp) == BIT_AND_EXPR)
4155 {
4156 and_mask = TREE_OPERAND (exp, 1);
4157 exp = TREE_OPERAND (exp, 0);
4158 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4159 if (TREE_CODE (and_mask) != INTEGER_CST)
4160 return 0;
4161 }
4162
4163 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4164 punsignedp, preversep, pvolatilep);
4165 if ((inner == exp && and_mask == 0)
4166 || *pbitsize < 0 || offset != 0
4167 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4168 /* Reject out-of-bound accesses (PR79731). */
4169 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4170 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4171 *pbitpos + *pbitsize) < 0))
4172 return 0;
4173
4174 *exp_ = exp;
4175
4176 /* If the number of bits in the reference is the same as the bitsize of
4177 the outer type, then the outer type gives the signedness. Otherwise
4178 (in case of a small bitfield) the signedness is unchanged. */
4179 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4180 *punsignedp = TYPE_UNSIGNED (outer_type);
4181
4182 /* Compute the mask to access the bitfield. */
4183 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4184 precision = TYPE_PRECISION (unsigned_type);
4185
4186 mask = build_int_cst_type (unsigned_type, -1);
4187
4188 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4189 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4190
4191 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4192 if (and_mask != 0)
4193 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4194 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4195
4196 *pmask = mask;
4197 *pand_mask = and_mask;
4198 return inner;
4199 }
4200
4201 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4202 bit positions and MASK is SIGNED. */
4203
4204 static int
4205 all_ones_mask_p (const_tree mask, unsigned int size)
4206 {
4207 tree type = TREE_TYPE (mask);
4208 unsigned int precision = TYPE_PRECISION (type);
4209
4210 /* If this function returns true when the type of the mask is
4211 UNSIGNED, then there will be errors. In particular see
4212 gcc.c-torture/execute/990326-1.c. There does not appear to be
4213 any documentation paper trail as to why this is so. But the pre
4214 wide-int worked with that restriction and it has been preserved
4215 here. */
4216 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4217 return false;
4218
4219 return wi::mask (size, false, precision) == wi::to_wide (mask);
4220 }
4221
4222 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4223 represents the sign bit of EXP's type. If EXP represents a sign
4224 or zero extension, also test VAL against the unextended type.
4225 The return value is the (sub)expression whose sign bit is VAL,
4226 or NULL_TREE otherwise. */
4227
4228 tree
4229 sign_bit_p (tree exp, const_tree val)
4230 {
4231 int width;
4232 tree t;
4233
4234 /* Tree EXP must have an integral type. */
4235 t = TREE_TYPE (exp);
4236 if (! INTEGRAL_TYPE_P (t))
4237 return NULL_TREE;
4238
4239 /* Tree VAL must be an integer constant. */
4240 if (TREE_CODE (val) != INTEGER_CST
4241 || TREE_OVERFLOW (val))
4242 return NULL_TREE;
4243
4244 width = TYPE_PRECISION (t);
4245 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4246 return exp;
4247
4248 /* Handle extension from a narrower type. */
4249 if (TREE_CODE (exp) == NOP_EXPR
4250 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4251 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4252
4253 return NULL_TREE;
4254 }
4255
4256 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4257 to be evaluated unconditionally. */
4258
4259 static int
4260 simple_operand_p (const_tree exp)
4261 {
4262 /* Strip any conversions that don't change the machine mode. */
4263 STRIP_NOPS (exp);
4264
4265 return (CONSTANT_CLASS_P (exp)
4266 || TREE_CODE (exp) == SSA_NAME
4267 || (DECL_P (exp)
4268 && ! TREE_ADDRESSABLE (exp)
4269 && ! TREE_THIS_VOLATILE (exp)
4270 && ! DECL_NONLOCAL (exp)
4271 /* Don't regard global variables as simple. They may be
4272 allocated in ways unknown to the compiler (shared memory,
4273 #pragma weak, etc). */
4274 && ! TREE_PUBLIC (exp)
4275 && ! DECL_EXTERNAL (exp)
4276 /* Weakrefs are not safe to be read, since they can be NULL.
4277 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4278 have DECL_WEAK flag set. */
4279 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4280 /* Loading a static variable is unduly expensive, but global
4281 registers aren't expensive. */
4282 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4283 }
4284
4285 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4286 to be evaluated unconditionally.
4287 I addition to simple_operand_p, we assume that comparisons, conversions,
4288 and logic-not operations are simple, if their operands are simple, too. */
4289
4290 static bool
4291 simple_operand_p_2 (tree exp)
4292 {
4293 enum tree_code code;
4294
4295 if (TREE_SIDE_EFFECTS (exp)
4296 || tree_could_trap_p (exp))
4297 return false;
4298
4299 while (CONVERT_EXPR_P (exp))
4300 exp = TREE_OPERAND (exp, 0);
4301
4302 code = TREE_CODE (exp);
4303
4304 if (TREE_CODE_CLASS (code) == tcc_comparison)
4305 return (simple_operand_p (TREE_OPERAND (exp, 0))
4306 && simple_operand_p (TREE_OPERAND (exp, 1)));
4307
4308 if (code == TRUTH_NOT_EXPR)
4309 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4310
4311 return simple_operand_p (exp);
4312 }
4313
4314 \f
4315 /* The following functions are subroutines to fold_range_test and allow it to
4316 try to change a logical combination of comparisons into a range test.
4317
4318 For example, both
4319 X == 2 || X == 3 || X == 4 || X == 5
4320 and
4321 X >= 2 && X <= 5
4322 are converted to
4323 (unsigned) (X - 2) <= 3
4324
4325 We describe each set of comparisons as being either inside or outside
4326 a range, using a variable named like IN_P, and then describe the
4327 range with a lower and upper bound. If one of the bounds is omitted,
4328 it represents either the highest or lowest value of the type.
4329
4330 In the comments below, we represent a range by two numbers in brackets
4331 preceded by a "+" to designate being inside that range, or a "-" to
4332 designate being outside that range, so the condition can be inverted by
4333 flipping the prefix. An omitted bound is represented by a "-". For
4334 example, "- [-, 10]" means being outside the range starting at the lowest
4335 possible value and ending at 10, in other words, being greater than 10.
4336 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4337 always false.
4338
4339 We set up things so that the missing bounds are handled in a consistent
4340 manner so neither a missing bound nor "true" and "false" need to be
4341 handled using a special case. */
4342
4343 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4344 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4345 and UPPER1_P are nonzero if the respective argument is an upper bound
4346 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4347 must be specified for a comparison. ARG1 will be converted to ARG0's
4348 type if both are specified. */
4349
4350 static tree
4351 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4352 tree arg1, int upper1_p)
4353 {
4354 tree tem;
4355 int result;
4356 int sgn0, sgn1;
4357
4358 /* If neither arg represents infinity, do the normal operation.
4359 Else, if not a comparison, return infinity. Else handle the special
4360 comparison rules. Note that most of the cases below won't occur, but
4361 are handled for consistency. */
4362
4363 if (arg0 != 0 && arg1 != 0)
4364 {
4365 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4366 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4367 STRIP_NOPS (tem);
4368 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4369 }
4370
4371 if (TREE_CODE_CLASS (code) != tcc_comparison)
4372 return 0;
4373
4374 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4375 for neither. In real maths, we cannot assume open ended ranges are
4376 the same. But, this is computer arithmetic, where numbers are finite.
4377 We can therefore make the transformation of any unbounded range with
4378 the value Z, Z being greater than any representable number. This permits
4379 us to treat unbounded ranges as equal. */
4380 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4381 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4382 switch (code)
4383 {
4384 case EQ_EXPR:
4385 result = sgn0 == sgn1;
4386 break;
4387 case NE_EXPR:
4388 result = sgn0 != sgn1;
4389 break;
4390 case LT_EXPR:
4391 result = sgn0 < sgn1;
4392 break;
4393 case LE_EXPR:
4394 result = sgn0 <= sgn1;
4395 break;
4396 case GT_EXPR:
4397 result = sgn0 > sgn1;
4398 break;
4399 case GE_EXPR:
4400 result = sgn0 >= sgn1;
4401 break;
4402 default:
4403 gcc_unreachable ();
4404 }
4405
4406 return constant_boolean_node (result, type);
4407 }
4408 \f
4409 /* Helper routine for make_range. Perform one step for it, return
4410 new expression if the loop should continue or NULL_TREE if it should
4411 stop. */
4412
4413 tree
4414 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4415 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4416 bool *strict_overflow_p)
4417 {
4418 tree arg0_type = TREE_TYPE (arg0);
4419 tree n_low, n_high, low = *p_low, high = *p_high;
4420 int in_p = *p_in_p, n_in_p;
4421
4422 switch (code)
4423 {
4424 case TRUTH_NOT_EXPR:
4425 /* We can only do something if the range is testing for zero. */
4426 if (low == NULL_TREE || high == NULL_TREE
4427 || ! integer_zerop (low) || ! integer_zerop (high))
4428 return NULL_TREE;
4429 *p_in_p = ! in_p;
4430 return arg0;
4431
4432 case EQ_EXPR: case NE_EXPR:
4433 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4434 /* We can only do something if the range is testing for zero
4435 and if the second operand is an integer constant. Note that
4436 saying something is "in" the range we make is done by
4437 complementing IN_P since it will set in the initial case of
4438 being not equal to zero; "out" is leaving it alone. */
4439 if (low == NULL_TREE || high == NULL_TREE
4440 || ! integer_zerop (low) || ! integer_zerop (high)
4441 || TREE_CODE (arg1) != INTEGER_CST)
4442 return NULL_TREE;
4443
4444 switch (code)
4445 {
4446 case NE_EXPR: /* - [c, c] */
4447 low = high = arg1;
4448 break;
4449 case EQ_EXPR: /* + [c, c] */
4450 in_p = ! in_p, low = high = arg1;
4451 break;
4452 case GT_EXPR: /* - [-, c] */
4453 low = 0, high = arg1;
4454 break;
4455 case GE_EXPR: /* + [c, -] */
4456 in_p = ! in_p, low = arg1, high = 0;
4457 break;
4458 case LT_EXPR: /* - [c, -] */
4459 low = arg1, high = 0;
4460 break;
4461 case LE_EXPR: /* + [-, c] */
4462 in_p = ! in_p, low = 0, high = arg1;
4463 break;
4464 default:
4465 gcc_unreachable ();
4466 }
4467
4468 /* If this is an unsigned comparison, we also know that EXP is
4469 greater than or equal to zero. We base the range tests we make
4470 on that fact, so we record it here so we can parse existing
4471 range tests. We test arg0_type since often the return type
4472 of, e.g. EQ_EXPR, is boolean. */
4473 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4474 {
4475 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4476 in_p, low, high, 1,
4477 build_int_cst (arg0_type, 0),
4478 NULL_TREE))
4479 return NULL_TREE;
4480
4481 in_p = n_in_p, low = n_low, high = n_high;
4482
4483 /* If the high bound is missing, but we have a nonzero low
4484 bound, reverse the range so it goes from zero to the low bound
4485 minus 1. */
4486 if (high == 0 && low && ! integer_zerop (low))
4487 {
4488 in_p = ! in_p;
4489 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4490 build_int_cst (TREE_TYPE (low), 1), 0);
4491 low = build_int_cst (arg0_type, 0);
4492 }
4493 }
4494
4495 *p_low = low;
4496 *p_high = high;
4497 *p_in_p = in_p;
4498 return arg0;
4499
4500 case NEGATE_EXPR:
4501 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4502 low and high are non-NULL, then normalize will DTRT. */
4503 if (!TYPE_UNSIGNED (arg0_type)
4504 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4505 {
4506 if (low == NULL_TREE)
4507 low = TYPE_MIN_VALUE (arg0_type);
4508 if (high == NULL_TREE)
4509 high = TYPE_MAX_VALUE (arg0_type);
4510 }
4511
4512 /* (-x) IN [a,b] -> x in [-b, -a] */
4513 n_low = range_binop (MINUS_EXPR, exp_type,
4514 build_int_cst (exp_type, 0),
4515 0, high, 1);
4516 n_high = range_binop (MINUS_EXPR, exp_type,
4517 build_int_cst (exp_type, 0),
4518 0, low, 0);
4519 if (n_high != 0 && TREE_OVERFLOW (n_high))
4520 return NULL_TREE;
4521 goto normalize;
4522
4523 case BIT_NOT_EXPR:
4524 /* ~ X -> -X - 1 */
4525 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4526 build_int_cst (exp_type, 1));
4527
4528 case PLUS_EXPR:
4529 case MINUS_EXPR:
4530 if (TREE_CODE (arg1) != INTEGER_CST)
4531 return NULL_TREE;
4532
4533 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4534 move a constant to the other side. */
4535 if (!TYPE_UNSIGNED (arg0_type)
4536 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4537 return NULL_TREE;
4538
4539 /* If EXP is signed, any overflow in the computation is undefined,
4540 so we don't worry about it so long as our computations on
4541 the bounds don't overflow. For unsigned, overflow is defined
4542 and this is exactly the right thing. */
4543 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4544 arg0_type, low, 0, arg1, 0);
4545 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4546 arg0_type, high, 1, arg1, 0);
4547 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4548 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4549 return NULL_TREE;
4550
4551 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4552 *strict_overflow_p = true;
4553
4554 normalize:
4555 /* Check for an unsigned range which has wrapped around the maximum
4556 value thus making n_high < n_low, and normalize it. */
4557 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4558 {
4559 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4560 build_int_cst (TREE_TYPE (n_high), 1), 0);
4561 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4562 build_int_cst (TREE_TYPE (n_low), 1), 0);
4563
4564 /* If the range is of the form +/- [ x+1, x ], we won't
4565 be able to normalize it. But then, it represents the
4566 whole range or the empty set, so make it
4567 +/- [ -, - ]. */
4568 if (tree_int_cst_equal (n_low, low)
4569 && tree_int_cst_equal (n_high, high))
4570 low = high = 0;
4571 else
4572 in_p = ! in_p;
4573 }
4574 else
4575 low = n_low, high = n_high;
4576
4577 *p_low = low;
4578 *p_high = high;
4579 *p_in_p = in_p;
4580 return arg0;
4581
4582 CASE_CONVERT:
4583 case NON_LVALUE_EXPR:
4584 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4585 return NULL_TREE;
4586
4587 if (! INTEGRAL_TYPE_P (arg0_type)
4588 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4589 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4590 return NULL_TREE;
4591
4592 n_low = low, n_high = high;
4593
4594 if (n_low != 0)
4595 n_low = fold_convert_loc (loc, arg0_type, n_low);
4596
4597 if (n_high != 0)
4598 n_high = fold_convert_loc (loc, arg0_type, n_high);
4599
4600 /* If we're converting arg0 from an unsigned type, to exp,
4601 a signed type, we will be doing the comparison as unsigned.
4602 The tests above have already verified that LOW and HIGH
4603 are both positive.
4604
4605 So we have to ensure that we will handle large unsigned
4606 values the same way that the current signed bounds treat
4607 negative values. */
4608
4609 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4610 {
4611 tree high_positive;
4612 tree equiv_type;
4613 /* For fixed-point modes, we need to pass the saturating flag
4614 as the 2nd parameter. */
4615 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4616 equiv_type
4617 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4618 TYPE_SATURATING (arg0_type));
4619 else
4620 equiv_type
4621 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4622
4623 /* A range without an upper bound is, naturally, unbounded.
4624 Since convert would have cropped a very large value, use
4625 the max value for the destination type. */
4626 high_positive
4627 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4628 : TYPE_MAX_VALUE (arg0_type);
4629
4630 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4631 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4632 fold_convert_loc (loc, arg0_type,
4633 high_positive),
4634 build_int_cst (arg0_type, 1));
4635
4636 /* If the low bound is specified, "and" the range with the
4637 range for which the original unsigned value will be
4638 positive. */
4639 if (low != 0)
4640 {
4641 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4642 1, fold_convert_loc (loc, arg0_type,
4643 integer_zero_node),
4644 high_positive))
4645 return NULL_TREE;
4646
4647 in_p = (n_in_p == in_p);
4648 }
4649 else
4650 {
4651 /* Otherwise, "or" the range with the range of the input
4652 that will be interpreted as negative. */
4653 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4654 1, fold_convert_loc (loc, arg0_type,
4655 integer_zero_node),
4656 high_positive))
4657 return NULL_TREE;
4658
4659 in_p = (in_p != n_in_p);
4660 }
4661 }
4662
4663 *p_low = n_low;
4664 *p_high = n_high;
4665 *p_in_p = in_p;
4666 return arg0;
4667
4668 default:
4669 return NULL_TREE;
4670 }
4671 }
4672
4673 /* Given EXP, a logical expression, set the range it is testing into
4674 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4675 actually being tested. *PLOW and *PHIGH will be made of the same
4676 type as the returned expression. If EXP is not a comparison, we
4677 will most likely not be returning a useful value and range. Set
4678 *STRICT_OVERFLOW_P to true if the return value is only valid
4679 because signed overflow is undefined; otherwise, do not change
4680 *STRICT_OVERFLOW_P. */
4681
4682 tree
4683 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4684 bool *strict_overflow_p)
4685 {
4686 enum tree_code code;
4687 tree arg0, arg1 = NULL_TREE;
4688 tree exp_type, nexp;
4689 int in_p;
4690 tree low, high;
4691 location_t loc = EXPR_LOCATION (exp);
4692
4693 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4694 and see if we can refine the range. Some of the cases below may not
4695 happen, but it doesn't seem worth worrying about this. We "continue"
4696 the outer loop when we've changed something; otherwise we "break"
4697 the switch, which will "break" the while. */
4698
4699 in_p = 0;
4700 low = high = build_int_cst (TREE_TYPE (exp), 0);
4701
4702 while (1)
4703 {
4704 code = TREE_CODE (exp);
4705 exp_type = TREE_TYPE (exp);
4706 arg0 = NULL_TREE;
4707
4708 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4709 {
4710 if (TREE_OPERAND_LENGTH (exp) > 0)
4711 arg0 = TREE_OPERAND (exp, 0);
4712 if (TREE_CODE_CLASS (code) == tcc_binary
4713 || TREE_CODE_CLASS (code) == tcc_comparison
4714 || (TREE_CODE_CLASS (code) == tcc_expression
4715 && TREE_OPERAND_LENGTH (exp) > 1))
4716 arg1 = TREE_OPERAND (exp, 1);
4717 }
4718 if (arg0 == NULL_TREE)
4719 break;
4720
4721 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4722 &high, &in_p, strict_overflow_p);
4723 if (nexp == NULL_TREE)
4724 break;
4725 exp = nexp;
4726 }
4727
4728 /* If EXP is a constant, we can evaluate whether this is true or false. */
4729 if (TREE_CODE (exp) == INTEGER_CST)
4730 {
4731 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4732 exp, 0, low, 0))
4733 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4734 exp, 1, high, 1)));
4735 low = high = 0;
4736 exp = 0;
4737 }
4738
4739 *pin_p = in_p, *plow = low, *phigh = high;
4740 return exp;
4741 }
4742
4743 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4744 a bitwise check i.e. when
4745 LOW == 0xXX...X00...0
4746 HIGH == 0xXX...X11...1
4747 Return corresponding mask in MASK and stem in VALUE. */
4748
4749 static bool
4750 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4751 tree *value)
4752 {
4753 if (TREE_CODE (low) != INTEGER_CST
4754 || TREE_CODE (high) != INTEGER_CST)
4755 return false;
4756
4757 unsigned prec = TYPE_PRECISION (type);
4758 wide_int lo = wi::to_wide (low, prec);
4759 wide_int hi = wi::to_wide (high, prec);
4760
4761 wide_int end_mask = lo ^ hi;
4762 if ((end_mask & (end_mask + 1)) != 0
4763 || (lo & end_mask) != 0)
4764 return false;
4765
4766 wide_int stem_mask = ~end_mask;
4767 wide_int stem = lo & stem_mask;
4768 if (stem != (hi & stem_mask))
4769 return false;
4770
4771 *mask = wide_int_to_tree (type, stem_mask);
4772 *value = wide_int_to_tree (type, stem);
4773
4774 return true;
4775 }
4776 \f
4777 /* Helper routine for build_range_check and match.pd. Return the type to
4778 perform the check or NULL if it shouldn't be optimized. */
4779
4780 tree
4781 range_check_type (tree etype)
4782 {
4783 /* First make sure that arithmetics in this type is valid, then make sure
4784 that it wraps around. */
4785 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4786 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4787 TYPE_UNSIGNED (etype));
4788
4789 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4790 {
4791 tree utype, minv, maxv;
4792
4793 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4794 for the type in question, as we rely on this here. */
4795 utype = unsigned_type_for (etype);
4796 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4797 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4798 build_int_cst (TREE_TYPE (maxv), 1), 1);
4799 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4800
4801 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4802 minv, 1, maxv, 1)))
4803 etype = utype;
4804 else
4805 return NULL_TREE;
4806 }
4807 return etype;
4808 }
4809
4810 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4811 type, TYPE, return an expression to test if EXP is in (or out of, depending
4812 on IN_P) the range. Return 0 if the test couldn't be created. */
4813
4814 tree
4815 build_range_check (location_t loc, tree type, tree exp, int in_p,
4816 tree low, tree high)
4817 {
4818 tree etype = TREE_TYPE (exp), mask, value;
4819
4820 /* Disable this optimization for function pointer expressions
4821 on targets that require function pointer canonicalization. */
4822 if (targetm.have_canonicalize_funcptr_for_compare ()
4823 && TREE_CODE (etype) == POINTER_TYPE
4824 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4825 return NULL_TREE;
4826
4827 if (! in_p)
4828 {
4829 value = build_range_check (loc, type, exp, 1, low, high);
4830 if (value != 0)
4831 return invert_truthvalue_loc (loc, value);
4832
4833 return 0;
4834 }
4835
4836 if (low == 0 && high == 0)
4837 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4838
4839 if (low == 0)
4840 return fold_build2_loc (loc, LE_EXPR, type, exp,
4841 fold_convert_loc (loc, etype, high));
4842
4843 if (high == 0)
4844 return fold_build2_loc (loc, GE_EXPR, type, exp,
4845 fold_convert_loc (loc, etype, low));
4846
4847 if (operand_equal_p (low, high, 0))
4848 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4849 fold_convert_loc (loc, etype, low));
4850
4851 if (TREE_CODE (exp) == BIT_AND_EXPR
4852 && maskable_range_p (low, high, etype, &mask, &value))
4853 return fold_build2_loc (loc, EQ_EXPR, type,
4854 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4855 exp, mask),
4856 value);
4857
4858 if (integer_zerop (low))
4859 {
4860 if (! TYPE_UNSIGNED (etype))
4861 {
4862 etype = unsigned_type_for (etype);
4863 high = fold_convert_loc (loc, etype, high);
4864 exp = fold_convert_loc (loc, etype, exp);
4865 }
4866 return build_range_check (loc, type, exp, 1, 0, high);
4867 }
4868
4869 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4870 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4871 {
4872 int prec = TYPE_PRECISION (etype);
4873
4874 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4875 {
4876 if (TYPE_UNSIGNED (etype))
4877 {
4878 tree signed_etype = signed_type_for (etype);
4879 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4880 etype
4881 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4882 else
4883 etype = signed_etype;
4884 exp = fold_convert_loc (loc, etype, exp);
4885 }
4886 return fold_build2_loc (loc, GT_EXPR, type, exp,
4887 build_int_cst (etype, 0));
4888 }
4889 }
4890
4891 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4892 This requires wrap-around arithmetics for the type of the expression. */
4893 etype = range_check_type (etype);
4894 if (etype == NULL_TREE)
4895 return NULL_TREE;
4896
4897 if (POINTER_TYPE_P (etype))
4898 etype = unsigned_type_for (etype);
4899
4900 high = fold_convert_loc (loc, etype, high);
4901 low = fold_convert_loc (loc, etype, low);
4902 exp = fold_convert_loc (loc, etype, exp);
4903
4904 value = const_binop (MINUS_EXPR, high, low);
4905
4906 if (value != 0 && !TREE_OVERFLOW (value))
4907 return build_range_check (loc, type,
4908 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4909 1, build_int_cst (etype, 0), value);
4910
4911 return 0;
4912 }
4913 \f
4914 /* Return the predecessor of VAL in its type, handling the infinite case. */
4915
4916 static tree
4917 range_predecessor (tree val)
4918 {
4919 tree type = TREE_TYPE (val);
4920
4921 if (INTEGRAL_TYPE_P (type)
4922 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4923 return 0;
4924 else
4925 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4926 build_int_cst (TREE_TYPE (val), 1), 0);
4927 }
4928
4929 /* Return the successor of VAL in its type, handling the infinite case. */
4930
4931 static tree
4932 range_successor (tree val)
4933 {
4934 tree type = TREE_TYPE (val);
4935
4936 if (INTEGRAL_TYPE_P (type)
4937 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4938 return 0;
4939 else
4940 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4941 build_int_cst (TREE_TYPE (val), 1), 0);
4942 }
4943
4944 /* Given two ranges, see if we can merge them into one. Return 1 if we
4945 can, 0 if we can't. Set the output range into the specified parameters. */
4946
4947 bool
4948 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4949 tree high0, int in1_p, tree low1, tree high1)
4950 {
4951 int no_overlap;
4952 int subset;
4953 int temp;
4954 tree tem;
4955 int in_p;
4956 tree low, high;
4957 int lowequal = ((low0 == 0 && low1 == 0)
4958 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4959 low0, 0, low1, 0)));
4960 int highequal = ((high0 == 0 && high1 == 0)
4961 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4962 high0, 1, high1, 1)));
4963
4964 /* Make range 0 be the range that starts first, or ends last if they
4965 start at the same value. Swap them if it isn't. */
4966 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4967 low0, 0, low1, 0))
4968 || (lowequal
4969 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4970 high1, 1, high0, 1))))
4971 {
4972 temp = in0_p, in0_p = in1_p, in1_p = temp;
4973 tem = low0, low0 = low1, low1 = tem;
4974 tem = high0, high0 = high1, high1 = tem;
4975 }
4976
4977 /* Now flag two cases, whether the ranges are disjoint or whether the
4978 second range is totally subsumed in the first. Note that the tests
4979 below are simplified by the ones above. */
4980 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4981 high0, 1, low1, 0));
4982 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4983 high1, 1, high0, 1));
4984
4985 /* We now have four cases, depending on whether we are including or
4986 excluding the two ranges. */
4987 if (in0_p && in1_p)
4988 {
4989 /* If they don't overlap, the result is false. If the second range
4990 is a subset it is the result. Otherwise, the range is from the start
4991 of the second to the end of the first. */
4992 if (no_overlap)
4993 in_p = 0, low = high = 0;
4994 else if (subset)
4995 in_p = 1, low = low1, high = high1;
4996 else
4997 in_p = 1, low = low1, high = high0;
4998 }
4999
5000 else if (in0_p && ! in1_p)
5001 {
5002 /* If they don't overlap, the result is the first range. If they are
5003 equal, the result is false. If the second range is a subset of the
5004 first, and the ranges begin at the same place, we go from just after
5005 the end of the second range to the end of the first. If the second
5006 range is not a subset of the first, or if it is a subset and both
5007 ranges end at the same place, the range starts at the start of the
5008 first range and ends just before the second range.
5009 Otherwise, we can't describe this as a single range. */
5010 if (no_overlap)
5011 in_p = 1, low = low0, high = high0;
5012 else if (lowequal && highequal)
5013 in_p = 0, low = high = 0;
5014 else if (subset && lowequal)
5015 {
5016 low = range_successor (high1);
5017 high = high0;
5018 in_p = 1;
5019 if (low == 0)
5020 {
5021 /* We are in the weird situation where high0 > high1 but
5022 high1 has no successor. Punt. */
5023 return 0;
5024 }
5025 }
5026 else if (! subset || highequal)
5027 {
5028 low = low0;
5029 high = range_predecessor (low1);
5030 in_p = 1;
5031 if (high == 0)
5032 {
5033 /* low0 < low1 but low1 has no predecessor. Punt. */
5034 return 0;
5035 }
5036 }
5037 else
5038 return 0;
5039 }
5040
5041 else if (! in0_p && in1_p)
5042 {
5043 /* If they don't overlap, the result is the second range. If the second
5044 is a subset of the first, the result is false. Otherwise,
5045 the range starts just after the first range and ends at the
5046 end of the second. */
5047 if (no_overlap)
5048 in_p = 1, low = low1, high = high1;
5049 else if (subset || highequal)
5050 in_p = 0, low = high = 0;
5051 else
5052 {
5053 low = range_successor (high0);
5054 high = high1;
5055 in_p = 1;
5056 if (low == 0)
5057 {
5058 /* high1 > high0 but high0 has no successor. Punt. */
5059 return 0;
5060 }
5061 }
5062 }
5063
5064 else
5065 {
5066 /* The case where we are excluding both ranges. Here the complex case
5067 is if they don't overlap. In that case, the only time we have a
5068 range is if they are adjacent. If the second is a subset of the
5069 first, the result is the first. Otherwise, the range to exclude
5070 starts at the beginning of the first range and ends at the end of the
5071 second. */
5072 if (no_overlap)
5073 {
5074 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5075 range_successor (high0),
5076 1, low1, 0)))
5077 in_p = 0, low = low0, high = high1;
5078 else
5079 {
5080 /* Canonicalize - [min, x] into - [-, x]. */
5081 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5082 switch (TREE_CODE (TREE_TYPE (low0)))
5083 {
5084 case ENUMERAL_TYPE:
5085 if (TYPE_PRECISION (TREE_TYPE (low0))
5086 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5087 break;
5088 /* FALLTHROUGH */
5089 case INTEGER_TYPE:
5090 if (tree_int_cst_equal (low0,
5091 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5092 low0 = 0;
5093 break;
5094 case POINTER_TYPE:
5095 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5096 && integer_zerop (low0))
5097 low0 = 0;
5098 break;
5099 default:
5100 break;
5101 }
5102
5103 /* Canonicalize - [x, max] into - [x, -]. */
5104 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5105 switch (TREE_CODE (TREE_TYPE (high1)))
5106 {
5107 case ENUMERAL_TYPE:
5108 if (TYPE_PRECISION (TREE_TYPE (high1))
5109 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5110 break;
5111 /* FALLTHROUGH */
5112 case INTEGER_TYPE:
5113 if (tree_int_cst_equal (high1,
5114 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5115 high1 = 0;
5116 break;
5117 case POINTER_TYPE:
5118 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5119 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5120 high1, 1,
5121 build_int_cst (TREE_TYPE (high1), 1),
5122 1)))
5123 high1 = 0;
5124 break;
5125 default:
5126 break;
5127 }
5128
5129 /* The ranges might be also adjacent between the maximum and
5130 minimum values of the given type. For
5131 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5132 return + [x + 1, y - 1]. */
5133 if (low0 == 0 && high1 == 0)
5134 {
5135 low = range_successor (high0);
5136 high = range_predecessor (low1);
5137 if (low == 0 || high == 0)
5138 return 0;
5139
5140 in_p = 1;
5141 }
5142 else
5143 return 0;
5144 }
5145 }
5146 else if (subset)
5147 in_p = 0, low = low0, high = high0;
5148 else
5149 in_p = 0, low = low0, high = high1;
5150 }
5151
5152 *pin_p = in_p, *plow = low, *phigh = high;
5153 return 1;
5154 }
5155 \f
5156
5157 /* Subroutine of fold, looking inside expressions of the form
5158 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5159 of the COND_EXPR. This function is being used also to optimize
5160 A op B ? C : A, by reversing the comparison first.
5161
5162 Return a folded expression whose code is not a COND_EXPR
5163 anymore, or NULL_TREE if no folding opportunity is found. */
5164
5165 static tree
5166 fold_cond_expr_with_comparison (location_t loc, tree type,
5167 tree arg0, tree arg1, tree arg2)
5168 {
5169 enum tree_code comp_code = TREE_CODE (arg0);
5170 tree arg00 = TREE_OPERAND (arg0, 0);
5171 tree arg01 = TREE_OPERAND (arg0, 1);
5172 tree arg1_type = TREE_TYPE (arg1);
5173 tree tem;
5174
5175 STRIP_NOPS (arg1);
5176 STRIP_NOPS (arg2);
5177
5178 /* If we have A op 0 ? A : -A, consider applying the following
5179 transformations:
5180
5181 A == 0? A : -A same as -A
5182 A != 0? A : -A same as A
5183 A >= 0? A : -A same as abs (A)
5184 A > 0? A : -A same as abs (A)
5185 A <= 0? A : -A same as -abs (A)
5186 A < 0? A : -A same as -abs (A)
5187
5188 None of these transformations work for modes with signed
5189 zeros. If A is +/-0, the first two transformations will
5190 change the sign of the result (from +0 to -0, or vice
5191 versa). The last four will fix the sign of the result,
5192 even though the original expressions could be positive or
5193 negative, depending on the sign of A.
5194
5195 Note that all these transformations are correct if A is
5196 NaN, since the two alternatives (A and -A) are also NaNs. */
5197 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5198 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5199 ? real_zerop (arg01)
5200 : integer_zerop (arg01))
5201 && ((TREE_CODE (arg2) == NEGATE_EXPR
5202 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5203 /* In the case that A is of the form X-Y, '-A' (arg2) may
5204 have already been folded to Y-X, check for that. */
5205 || (TREE_CODE (arg1) == MINUS_EXPR
5206 && TREE_CODE (arg2) == MINUS_EXPR
5207 && operand_equal_p (TREE_OPERAND (arg1, 0),
5208 TREE_OPERAND (arg2, 1), 0)
5209 && operand_equal_p (TREE_OPERAND (arg1, 1),
5210 TREE_OPERAND (arg2, 0), 0))))
5211 switch (comp_code)
5212 {
5213 case EQ_EXPR:
5214 case UNEQ_EXPR:
5215 tem = fold_convert_loc (loc, arg1_type, arg1);
5216 return fold_convert_loc (loc, type, negate_expr (tem));
5217 case NE_EXPR:
5218 case LTGT_EXPR:
5219 return fold_convert_loc (loc, type, arg1);
5220 case UNGE_EXPR:
5221 case UNGT_EXPR:
5222 if (flag_trapping_math)
5223 break;
5224 /* Fall through. */
5225 case GE_EXPR:
5226 case GT_EXPR:
5227 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5228 break;
5229 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5230 return fold_convert_loc (loc, type, tem);
5231 case UNLE_EXPR:
5232 case UNLT_EXPR:
5233 if (flag_trapping_math)
5234 break;
5235 /* FALLTHRU */
5236 case LE_EXPR:
5237 case LT_EXPR:
5238 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5239 break;
5240 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5241 return negate_expr (fold_convert_loc (loc, type, tem));
5242 default:
5243 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5244 break;
5245 }
5246
5247 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5248 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5249 both transformations are correct when A is NaN: A != 0
5250 is then true, and A == 0 is false. */
5251
5252 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5253 && integer_zerop (arg01) && integer_zerop (arg2))
5254 {
5255 if (comp_code == NE_EXPR)
5256 return fold_convert_loc (loc, type, arg1);
5257 else if (comp_code == EQ_EXPR)
5258 return build_zero_cst (type);
5259 }
5260
5261 /* Try some transformations of A op B ? A : B.
5262
5263 A == B? A : B same as B
5264 A != B? A : B same as A
5265 A >= B? A : B same as max (A, B)
5266 A > B? A : B same as max (B, A)
5267 A <= B? A : B same as min (A, B)
5268 A < B? A : B same as min (B, A)
5269
5270 As above, these transformations don't work in the presence
5271 of signed zeros. For example, if A and B are zeros of
5272 opposite sign, the first two transformations will change
5273 the sign of the result. In the last four, the original
5274 expressions give different results for (A=+0, B=-0) and
5275 (A=-0, B=+0), but the transformed expressions do not.
5276
5277 The first two transformations are correct if either A or B
5278 is a NaN. In the first transformation, the condition will
5279 be false, and B will indeed be chosen. In the case of the
5280 second transformation, the condition A != B will be true,
5281 and A will be chosen.
5282
5283 The conversions to max() and min() are not correct if B is
5284 a number and A is not. The conditions in the original
5285 expressions will be false, so all four give B. The min()
5286 and max() versions would give a NaN instead. */
5287 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5288 && operand_equal_for_comparison_p (arg01, arg2)
5289 /* Avoid these transformations if the COND_EXPR may be used
5290 as an lvalue in the C++ front-end. PR c++/19199. */
5291 && (in_gimple_form
5292 || VECTOR_TYPE_P (type)
5293 || (! lang_GNU_CXX ()
5294 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5295 || ! maybe_lvalue_p (arg1)
5296 || ! maybe_lvalue_p (arg2)))
5297 {
5298 tree comp_op0 = arg00;
5299 tree comp_op1 = arg01;
5300 tree comp_type = TREE_TYPE (comp_op0);
5301
5302 switch (comp_code)
5303 {
5304 case EQ_EXPR:
5305 return fold_convert_loc (loc, type, arg2);
5306 case NE_EXPR:
5307 return fold_convert_loc (loc, type, arg1);
5308 case LE_EXPR:
5309 case LT_EXPR:
5310 case UNLE_EXPR:
5311 case UNLT_EXPR:
5312 /* In C++ a ?: expression can be an lvalue, so put the
5313 operand which will be used if they are equal first
5314 so that we can convert this back to the
5315 corresponding COND_EXPR. */
5316 if (!HONOR_NANS (arg1))
5317 {
5318 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5319 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5320 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5321 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5322 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5323 comp_op1, comp_op0);
5324 return fold_convert_loc (loc, type, tem);
5325 }
5326 break;
5327 case GE_EXPR:
5328 case GT_EXPR:
5329 case UNGE_EXPR:
5330 case UNGT_EXPR:
5331 if (!HONOR_NANS (arg1))
5332 {
5333 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5334 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5335 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5336 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5337 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5338 comp_op1, comp_op0);
5339 return fold_convert_loc (loc, type, tem);
5340 }
5341 break;
5342 case UNEQ_EXPR:
5343 if (!HONOR_NANS (arg1))
5344 return fold_convert_loc (loc, type, arg2);
5345 break;
5346 case LTGT_EXPR:
5347 if (!HONOR_NANS (arg1))
5348 return fold_convert_loc (loc, type, arg1);
5349 break;
5350 default:
5351 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5352 break;
5353 }
5354 }
5355
5356 return NULL_TREE;
5357 }
5358
5359
5360 \f
5361 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5362 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5363 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5364 false) >= 2)
5365 #endif
5366
5367 /* EXP is some logical combination of boolean tests. See if we can
5368 merge it into some range test. Return the new tree if so. */
5369
5370 static tree
5371 fold_range_test (location_t loc, enum tree_code code, tree type,
5372 tree op0, tree op1)
5373 {
5374 int or_op = (code == TRUTH_ORIF_EXPR
5375 || code == TRUTH_OR_EXPR);
5376 int in0_p, in1_p, in_p;
5377 tree low0, low1, low, high0, high1, high;
5378 bool strict_overflow_p = false;
5379 tree tem, lhs, rhs;
5380 const char * const warnmsg = G_("assuming signed overflow does not occur "
5381 "when simplifying range test");
5382
5383 if (!INTEGRAL_TYPE_P (type))
5384 return 0;
5385
5386 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5387 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5388
5389 /* If this is an OR operation, invert both sides; we will invert
5390 again at the end. */
5391 if (or_op)
5392 in0_p = ! in0_p, in1_p = ! in1_p;
5393
5394 /* If both expressions are the same, if we can merge the ranges, and we
5395 can build the range test, return it or it inverted. If one of the
5396 ranges is always true or always false, consider it to be the same
5397 expression as the other. */
5398 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5399 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5400 in1_p, low1, high1)
5401 && 0 != (tem = (build_range_check (loc, type,
5402 lhs != 0 ? lhs
5403 : rhs != 0 ? rhs : integer_zero_node,
5404 in_p, low, high))))
5405 {
5406 if (strict_overflow_p)
5407 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5408 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5409 }
5410
5411 /* On machines where the branch cost is expensive, if this is a
5412 short-circuited branch and the underlying object on both sides
5413 is the same, make a non-short-circuit operation. */
5414 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5415 && !flag_sanitize_coverage
5416 && lhs != 0 && rhs != 0
5417 && (code == TRUTH_ANDIF_EXPR
5418 || code == TRUTH_ORIF_EXPR)
5419 && operand_equal_p (lhs, rhs, 0))
5420 {
5421 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5422 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5423 which cases we can't do this. */
5424 if (simple_operand_p (lhs))
5425 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5426 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5427 type, op0, op1);
5428
5429 else if (!lang_hooks.decls.global_bindings_p ()
5430 && !CONTAINS_PLACEHOLDER_P (lhs))
5431 {
5432 tree common = save_expr (lhs);
5433
5434 if (0 != (lhs = build_range_check (loc, type, common,
5435 or_op ? ! in0_p : in0_p,
5436 low0, high0))
5437 && (0 != (rhs = build_range_check (loc, type, common,
5438 or_op ? ! in1_p : in1_p,
5439 low1, high1))))
5440 {
5441 if (strict_overflow_p)
5442 fold_overflow_warning (warnmsg,
5443 WARN_STRICT_OVERFLOW_COMPARISON);
5444 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5445 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5446 type, lhs, rhs);
5447 }
5448 }
5449 }
5450
5451 return 0;
5452 }
5453 \f
5454 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5455 bit value. Arrange things so the extra bits will be set to zero if and
5456 only if C is signed-extended to its full width. If MASK is nonzero,
5457 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5458
5459 static tree
5460 unextend (tree c, int p, int unsignedp, tree mask)
5461 {
5462 tree type = TREE_TYPE (c);
5463 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5464 tree temp;
5465
5466 if (p == modesize || unsignedp)
5467 return c;
5468
5469 /* We work by getting just the sign bit into the low-order bit, then
5470 into the high-order bit, then sign-extend. We then XOR that value
5471 with C. */
5472 temp = build_int_cst (TREE_TYPE (c),
5473 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5474
5475 /* We must use a signed type in order to get an arithmetic right shift.
5476 However, we must also avoid introducing accidental overflows, so that
5477 a subsequent call to integer_zerop will work. Hence we must
5478 do the type conversion here. At this point, the constant is either
5479 zero or one, and the conversion to a signed type can never overflow.
5480 We could get an overflow if this conversion is done anywhere else. */
5481 if (TYPE_UNSIGNED (type))
5482 temp = fold_convert (signed_type_for (type), temp);
5483
5484 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5485 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5486 if (mask != 0)
5487 temp = const_binop (BIT_AND_EXPR, temp,
5488 fold_convert (TREE_TYPE (c), mask));
5489 /* If necessary, convert the type back to match the type of C. */
5490 if (TYPE_UNSIGNED (type))
5491 temp = fold_convert (type, temp);
5492
5493 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5494 }
5495 \f
5496 /* For an expression that has the form
5497 (A && B) || ~B
5498 or
5499 (A || B) && ~B,
5500 we can drop one of the inner expressions and simplify to
5501 A || ~B
5502 or
5503 A && ~B
5504 LOC is the location of the resulting expression. OP is the inner
5505 logical operation; the left-hand side in the examples above, while CMPOP
5506 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5507 removing a condition that guards another, as in
5508 (A != NULL && A->...) || A == NULL
5509 which we must not transform. If RHS_ONLY is true, only eliminate the
5510 right-most operand of the inner logical operation. */
5511
5512 static tree
5513 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5514 bool rhs_only)
5515 {
5516 tree type = TREE_TYPE (cmpop);
5517 enum tree_code code = TREE_CODE (cmpop);
5518 enum tree_code truthop_code = TREE_CODE (op);
5519 tree lhs = TREE_OPERAND (op, 0);
5520 tree rhs = TREE_OPERAND (op, 1);
5521 tree orig_lhs = lhs, orig_rhs = rhs;
5522 enum tree_code rhs_code = TREE_CODE (rhs);
5523 enum tree_code lhs_code = TREE_CODE (lhs);
5524 enum tree_code inv_code;
5525
5526 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5527 return NULL_TREE;
5528
5529 if (TREE_CODE_CLASS (code) != tcc_comparison)
5530 return NULL_TREE;
5531
5532 if (rhs_code == truthop_code)
5533 {
5534 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5535 if (newrhs != NULL_TREE)
5536 {
5537 rhs = newrhs;
5538 rhs_code = TREE_CODE (rhs);
5539 }
5540 }
5541 if (lhs_code == truthop_code && !rhs_only)
5542 {
5543 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5544 if (newlhs != NULL_TREE)
5545 {
5546 lhs = newlhs;
5547 lhs_code = TREE_CODE (lhs);
5548 }
5549 }
5550
5551 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5552 if (inv_code == rhs_code
5553 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5554 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5555 return lhs;
5556 if (!rhs_only && inv_code == lhs_code
5557 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5558 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5559 return rhs;
5560 if (rhs != orig_rhs || lhs != orig_lhs)
5561 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5562 lhs, rhs);
5563 return NULL_TREE;
5564 }
5565
5566 /* Find ways of folding logical expressions of LHS and RHS:
5567 Try to merge two comparisons to the same innermost item.
5568 Look for range tests like "ch >= '0' && ch <= '9'".
5569 Look for combinations of simple terms on machines with expensive branches
5570 and evaluate the RHS unconditionally.
5571
5572 For example, if we have p->a == 2 && p->b == 4 and we can make an
5573 object large enough to span both A and B, we can do this with a comparison
5574 against the object ANDed with the a mask.
5575
5576 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5577 operations to do this with one comparison.
5578
5579 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5580 function and the one above.
5581
5582 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5583 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5584
5585 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5586 two operands.
5587
5588 We return the simplified tree or 0 if no optimization is possible. */
5589
5590 static tree
5591 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5592 tree lhs, tree rhs)
5593 {
5594 /* If this is the "or" of two comparisons, we can do something if
5595 the comparisons are NE_EXPR. If this is the "and", we can do something
5596 if the comparisons are EQ_EXPR. I.e.,
5597 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5598
5599 WANTED_CODE is this operation code. For single bit fields, we can
5600 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5601 comparison for one-bit fields. */
5602
5603 enum tree_code wanted_code;
5604 enum tree_code lcode, rcode;
5605 tree ll_arg, lr_arg, rl_arg, rr_arg;
5606 tree ll_inner, lr_inner, rl_inner, rr_inner;
5607 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5608 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5609 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5610 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5611 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5612 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5613 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5614 scalar_int_mode lnmode, rnmode;
5615 tree ll_mask, lr_mask, rl_mask, rr_mask;
5616 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5617 tree l_const, r_const;
5618 tree lntype, rntype, result;
5619 HOST_WIDE_INT first_bit, end_bit;
5620 int volatilep;
5621
5622 /* Start by getting the comparison codes. Fail if anything is volatile.
5623 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5624 it were surrounded with a NE_EXPR. */
5625
5626 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5627 return 0;
5628
5629 lcode = TREE_CODE (lhs);
5630 rcode = TREE_CODE (rhs);
5631
5632 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5633 {
5634 lhs = build2 (NE_EXPR, truth_type, lhs,
5635 build_int_cst (TREE_TYPE (lhs), 0));
5636 lcode = NE_EXPR;
5637 }
5638
5639 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5640 {
5641 rhs = build2 (NE_EXPR, truth_type, rhs,
5642 build_int_cst (TREE_TYPE (rhs), 0));
5643 rcode = NE_EXPR;
5644 }
5645
5646 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5647 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5648 return 0;
5649
5650 ll_arg = TREE_OPERAND (lhs, 0);
5651 lr_arg = TREE_OPERAND (lhs, 1);
5652 rl_arg = TREE_OPERAND (rhs, 0);
5653 rr_arg = TREE_OPERAND (rhs, 1);
5654
5655 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5656 if (simple_operand_p (ll_arg)
5657 && simple_operand_p (lr_arg))
5658 {
5659 if (operand_equal_p (ll_arg, rl_arg, 0)
5660 && operand_equal_p (lr_arg, rr_arg, 0))
5661 {
5662 result = combine_comparisons (loc, code, lcode, rcode,
5663 truth_type, ll_arg, lr_arg);
5664 if (result)
5665 return result;
5666 }
5667 else if (operand_equal_p (ll_arg, rr_arg, 0)
5668 && operand_equal_p (lr_arg, rl_arg, 0))
5669 {
5670 result = combine_comparisons (loc, code, lcode,
5671 swap_tree_comparison (rcode),
5672 truth_type, ll_arg, lr_arg);
5673 if (result)
5674 return result;
5675 }
5676 }
5677
5678 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5679 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5680
5681 /* If the RHS can be evaluated unconditionally and its operands are
5682 simple, it wins to evaluate the RHS unconditionally on machines
5683 with expensive branches. In this case, this isn't a comparison
5684 that can be merged. */
5685
5686 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5687 false) >= 2
5688 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5689 && simple_operand_p (rl_arg)
5690 && simple_operand_p (rr_arg))
5691 {
5692 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5693 if (code == TRUTH_OR_EXPR
5694 && lcode == NE_EXPR && integer_zerop (lr_arg)
5695 && rcode == NE_EXPR && integer_zerop (rr_arg)
5696 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5697 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5698 return build2_loc (loc, NE_EXPR, truth_type,
5699 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5700 ll_arg, rl_arg),
5701 build_int_cst (TREE_TYPE (ll_arg), 0));
5702
5703 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5704 if (code == TRUTH_AND_EXPR
5705 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5706 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5707 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5708 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5709 return build2_loc (loc, EQ_EXPR, truth_type,
5710 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5711 ll_arg, rl_arg),
5712 build_int_cst (TREE_TYPE (ll_arg), 0));
5713 }
5714
5715 /* See if the comparisons can be merged. Then get all the parameters for
5716 each side. */
5717
5718 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5719 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5720 return 0;
5721
5722 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5723 volatilep = 0;
5724 ll_inner = decode_field_reference (loc, &ll_arg,
5725 &ll_bitsize, &ll_bitpos, &ll_mode,
5726 &ll_unsignedp, &ll_reversep, &volatilep,
5727 &ll_mask, &ll_and_mask);
5728 lr_inner = decode_field_reference (loc, &lr_arg,
5729 &lr_bitsize, &lr_bitpos, &lr_mode,
5730 &lr_unsignedp, &lr_reversep, &volatilep,
5731 &lr_mask, &lr_and_mask);
5732 rl_inner = decode_field_reference (loc, &rl_arg,
5733 &rl_bitsize, &rl_bitpos, &rl_mode,
5734 &rl_unsignedp, &rl_reversep, &volatilep,
5735 &rl_mask, &rl_and_mask);
5736 rr_inner = decode_field_reference (loc, &rr_arg,
5737 &rr_bitsize, &rr_bitpos, &rr_mode,
5738 &rr_unsignedp, &rr_reversep, &volatilep,
5739 &rr_mask, &rr_and_mask);
5740
5741 /* It must be true that the inner operation on the lhs of each
5742 comparison must be the same if we are to be able to do anything.
5743 Then see if we have constants. If not, the same must be true for
5744 the rhs's. */
5745 if (volatilep
5746 || ll_reversep != rl_reversep
5747 || ll_inner == 0 || rl_inner == 0
5748 || ! operand_equal_p (ll_inner, rl_inner, 0))
5749 return 0;
5750
5751 if (TREE_CODE (lr_arg) == INTEGER_CST
5752 && TREE_CODE (rr_arg) == INTEGER_CST)
5753 {
5754 l_const = lr_arg, r_const = rr_arg;
5755 lr_reversep = ll_reversep;
5756 }
5757 else if (lr_reversep != rr_reversep
5758 || lr_inner == 0 || rr_inner == 0
5759 || ! operand_equal_p (lr_inner, rr_inner, 0))
5760 return 0;
5761 else
5762 l_const = r_const = 0;
5763
5764 /* If either comparison code is not correct for our logical operation,
5765 fail. However, we can convert a one-bit comparison against zero into
5766 the opposite comparison against that bit being set in the field. */
5767
5768 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5769 if (lcode != wanted_code)
5770 {
5771 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5772 {
5773 /* Make the left operand unsigned, since we are only interested
5774 in the value of one bit. Otherwise we are doing the wrong
5775 thing below. */
5776 ll_unsignedp = 1;
5777 l_const = ll_mask;
5778 }
5779 else
5780 return 0;
5781 }
5782
5783 /* This is analogous to the code for l_const above. */
5784 if (rcode != wanted_code)
5785 {
5786 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5787 {
5788 rl_unsignedp = 1;
5789 r_const = rl_mask;
5790 }
5791 else
5792 return 0;
5793 }
5794
5795 /* See if we can find a mode that contains both fields being compared on
5796 the left. If we can't, fail. Otherwise, update all constants and masks
5797 to be relative to a field of that size. */
5798 first_bit = MIN (ll_bitpos, rl_bitpos);
5799 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5800 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5801 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5802 volatilep, &lnmode))
5803 return 0;
5804
5805 lnbitsize = GET_MODE_BITSIZE (lnmode);
5806 lnbitpos = first_bit & ~ (lnbitsize - 1);
5807 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5808 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5809
5810 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5811 {
5812 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5813 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5814 }
5815
5816 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5817 size_int (xll_bitpos));
5818 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5819 size_int (xrl_bitpos));
5820
5821 if (l_const)
5822 {
5823 l_const = fold_convert_loc (loc, lntype, l_const);
5824 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5825 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5826 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5827 fold_build1_loc (loc, BIT_NOT_EXPR,
5828 lntype, ll_mask))))
5829 {
5830 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5831
5832 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5833 }
5834 }
5835 if (r_const)
5836 {
5837 r_const = fold_convert_loc (loc, lntype, r_const);
5838 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5839 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5840 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5841 fold_build1_loc (loc, BIT_NOT_EXPR,
5842 lntype, rl_mask))))
5843 {
5844 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5845
5846 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5847 }
5848 }
5849
5850 /* If the right sides are not constant, do the same for it. Also,
5851 disallow this optimization if a size or signedness mismatch occurs
5852 between the left and right sides. */
5853 if (l_const == 0)
5854 {
5855 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5856 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5857 /* Make sure the two fields on the right
5858 correspond to the left without being swapped. */
5859 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5860 return 0;
5861
5862 first_bit = MIN (lr_bitpos, rr_bitpos);
5863 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5864 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5865 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5866 volatilep, &rnmode))
5867 return 0;
5868
5869 rnbitsize = GET_MODE_BITSIZE (rnmode);
5870 rnbitpos = first_bit & ~ (rnbitsize - 1);
5871 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5872 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5873
5874 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5875 {
5876 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5877 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5878 }
5879
5880 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5881 rntype, lr_mask),
5882 size_int (xlr_bitpos));
5883 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5884 rntype, rr_mask),
5885 size_int (xrr_bitpos));
5886
5887 /* Make a mask that corresponds to both fields being compared.
5888 Do this for both items being compared. If the operands are the
5889 same size and the bits being compared are in the same position
5890 then we can do this by masking both and comparing the masked
5891 results. */
5892 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5893 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5894 if (lnbitsize == rnbitsize
5895 && xll_bitpos == xlr_bitpos
5896 && lnbitpos >= 0
5897 && rnbitpos >= 0)
5898 {
5899 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5900 lntype, lnbitsize, lnbitpos,
5901 ll_unsignedp || rl_unsignedp, ll_reversep);
5902 if (! all_ones_mask_p (ll_mask, lnbitsize))
5903 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5904
5905 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5906 rntype, rnbitsize, rnbitpos,
5907 lr_unsignedp || rr_unsignedp, lr_reversep);
5908 if (! all_ones_mask_p (lr_mask, rnbitsize))
5909 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5910
5911 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5912 }
5913
5914 /* There is still another way we can do something: If both pairs of
5915 fields being compared are adjacent, we may be able to make a wider
5916 field containing them both.
5917
5918 Note that we still must mask the lhs/rhs expressions. Furthermore,
5919 the mask must be shifted to account for the shift done by
5920 make_bit_field_ref. */
5921 if (((ll_bitsize + ll_bitpos == rl_bitpos
5922 && lr_bitsize + lr_bitpos == rr_bitpos)
5923 || (ll_bitpos == rl_bitpos + rl_bitsize
5924 && lr_bitpos == rr_bitpos + rr_bitsize))
5925 && ll_bitpos >= 0
5926 && rl_bitpos >= 0
5927 && lr_bitpos >= 0
5928 && rr_bitpos >= 0)
5929 {
5930 tree type;
5931
5932 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5933 ll_bitsize + rl_bitsize,
5934 MIN (ll_bitpos, rl_bitpos),
5935 ll_unsignedp, ll_reversep);
5936 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5937 lr_bitsize + rr_bitsize,
5938 MIN (lr_bitpos, rr_bitpos),
5939 lr_unsignedp, lr_reversep);
5940
5941 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5942 size_int (MIN (xll_bitpos, xrl_bitpos)));
5943 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5944 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5945
5946 /* Convert to the smaller type before masking out unwanted bits. */
5947 type = lntype;
5948 if (lntype != rntype)
5949 {
5950 if (lnbitsize > rnbitsize)
5951 {
5952 lhs = fold_convert_loc (loc, rntype, lhs);
5953 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5954 type = rntype;
5955 }
5956 else if (lnbitsize < rnbitsize)
5957 {
5958 rhs = fold_convert_loc (loc, lntype, rhs);
5959 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5960 type = lntype;
5961 }
5962 }
5963
5964 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5965 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5966
5967 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5968 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5969
5970 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5971 }
5972
5973 return 0;
5974 }
5975
5976 /* Handle the case of comparisons with constants. If there is something in
5977 common between the masks, those bits of the constants must be the same.
5978 If not, the condition is always false. Test for this to avoid generating
5979 incorrect code below. */
5980 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5981 if (! integer_zerop (result)
5982 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5983 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5984 {
5985 if (wanted_code == NE_EXPR)
5986 {
5987 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5988 return constant_boolean_node (true, truth_type);
5989 }
5990 else
5991 {
5992 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5993 return constant_boolean_node (false, truth_type);
5994 }
5995 }
5996
5997 if (lnbitpos < 0)
5998 return 0;
5999
6000 /* Construct the expression we will return. First get the component
6001 reference we will make. Unless the mask is all ones the width of
6002 that field, perform the mask operation. Then compare with the
6003 merged constant. */
6004 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6005 lntype, lnbitsize, lnbitpos,
6006 ll_unsignedp || rl_unsignedp, ll_reversep);
6007
6008 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6009 if (! all_ones_mask_p (ll_mask, lnbitsize))
6010 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6011
6012 return build2_loc (loc, wanted_code, truth_type, result,
6013 const_binop (BIT_IOR_EXPR, l_const, r_const));
6014 }
6015 \f
6016 /* T is an integer expression that is being multiplied, divided, or taken a
6017 modulus (CODE says which and what kind of divide or modulus) by a
6018 constant C. See if we can eliminate that operation by folding it with
6019 other operations already in T. WIDE_TYPE, if non-null, is a type that
6020 should be used for the computation if wider than our type.
6021
6022 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6023 (X * 2) + (Y * 4). We must, however, be assured that either the original
6024 expression would not overflow or that overflow is undefined for the type
6025 in the language in question.
6026
6027 If we return a non-null expression, it is an equivalent form of the
6028 original computation, but need not be in the original type.
6029
6030 We set *STRICT_OVERFLOW_P to true if the return values depends on
6031 signed overflow being undefined. Otherwise we do not change
6032 *STRICT_OVERFLOW_P. */
6033
6034 static tree
6035 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6036 bool *strict_overflow_p)
6037 {
6038 /* To avoid exponential search depth, refuse to allow recursion past
6039 three levels. Beyond that (1) it's highly unlikely that we'll find
6040 something interesting and (2) we've probably processed it before
6041 when we built the inner expression. */
6042
6043 static int depth;
6044 tree ret;
6045
6046 if (depth > 3)
6047 return NULL;
6048
6049 depth++;
6050 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6051 depth--;
6052
6053 return ret;
6054 }
6055
6056 static tree
6057 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6058 bool *strict_overflow_p)
6059 {
6060 tree type = TREE_TYPE (t);
6061 enum tree_code tcode = TREE_CODE (t);
6062 tree ctype = (wide_type != 0
6063 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6064 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6065 ? wide_type : type);
6066 tree t1, t2;
6067 int same_p = tcode == code;
6068 tree op0 = NULL_TREE, op1 = NULL_TREE;
6069 bool sub_strict_overflow_p;
6070
6071 /* Don't deal with constants of zero here; they confuse the code below. */
6072 if (integer_zerop (c))
6073 return NULL_TREE;
6074
6075 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6076 op0 = TREE_OPERAND (t, 0);
6077
6078 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6079 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6080
6081 /* Note that we need not handle conditional operations here since fold
6082 already handles those cases. So just do arithmetic here. */
6083 switch (tcode)
6084 {
6085 case INTEGER_CST:
6086 /* For a constant, we can always simplify if we are a multiply
6087 or (for divide and modulus) if it is a multiple of our constant. */
6088 if (code == MULT_EXPR
6089 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6090 TYPE_SIGN (type)))
6091 {
6092 tree tem = const_binop (code, fold_convert (ctype, t),
6093 fold_convert (ctype, c));
6094 /* If the multiplication overflowed, we lost information on it.
6095 See PR68142 and PR69845. */
6096 if (TREE_OVERFLOW (tem))
6097 return NULL_TREE;
6098 return tem;
6099 }
6100 break;
6101
6102 CASE_CONVERT: case NON_LVALUE_EXPR:
6103 /* If op0 is an expression ... */
6104 if ((COMPARISON_CLASS_P (op0)
6105 || UNARY_CLASS_P (op0)
6106 || BINARY_CLASS_P (op0)
6107 || VL_EXP_CLASS_P (op0)
6108 || EXPRESSION_CLASS_P (op0))
6109 /* ... and has wrapping overflow, and its type is smaller
6110 than ctype, then we cannot pass through as widening. */
6111 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6112 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6113 && (TYPE_PRECISION (ctype)
6114 > TYPE_PRECISION (TREE_TYPE (op0))))
6115 /* ... or this is a truncation (t is narrower than op0),
6116 then we cannot pass through this narrowing. */
6117 || (TYPE_PRECISION (type)
6118 < TYPE_PRECISION (TREE_TYPE (op0)))
6119 /* ... or signedness changes for division or modulus,
6120 then we cannot pass through this conversion. */
6121 || (code != MULT_EXPR
6122 && (TYPE_UNSIGNED (ctype)
6123 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6124 /* ... or has undefined overflow while the converted to
6125 type has not, we cannot do the operation in the inner type
6126 as that would introduce undefined overflow. */
6127 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6129 && !TYPE_OVERFLOW_UNDEFINED (type))))
6130 break;
6131
6132 /* Pass the constant down and see if we can make a simplification. If
6133 we can, replace this expression with the inner simplification for
6134 possible later conversion to our or some other type. */
6135 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6136 && TREE_CODE (t2) == INTEGER_CST
6137 && !TREE_OVERFLOW (t2)
6138 && (0 != (t1 = extract_muldiv (op0, t2, code,
6139 code == MULT_EXPR
6140 ? ctype : NULL_TREE,
6141 strict_overflow_p))))
6142 return t1;
6143 break;
6144
6145 case ABS_EXPR:
6146 /* If widening the type changes it from signed to unsigned, then we
6147 must avoid building ABS_EXPR itself as unsigned. */
6148 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6149 {
6150 tree cstype = (*signed_type_for) (ctype);
6151 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6152 != 0)
6153 {
6154 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6155 return fold_convert (ctype, t1);
6156 }
6157 break;
6158 }
6159 /* If the constant is negative, we cannot simplify this. */
6160 if (tree_int_cst_sgn (c) == -1)
6161 break;
6162 /* FALLTHROUGH */
6163 case NEGATE_EXPR:
6164 /* For division and modulus, type can't be unsigned, as e.g.
6165 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6166 For signed types, even with wrapping overflow, this is fine. */
6167 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6168 break;
6169 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6170 != 0)
6171 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6172 break;
6173
6174 case MIN_EXPR: case MAX_EXPR:
6175 /* If widening the type changes the signedness, then we can't perform
6176 this optimization as that changes the result. */
6177 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6178 break;
6179
6180 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6181 sub_strict_overflow_p = false;
6182 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6183 &sub_strict_overflow_p)) != 0
6184 && (t2 = extract_muldiv (op1, c, code, wide_type,
6185 &sub_strict_overflow_p)) != 0)
6186 {
6187 if (tree_int_cst_sgn (c) < 0)
6188 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6189 if (sub_strict_overflow_p)
6190 *strict_overflow_p = true;
6191 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6192 fold_convert (ctype, t2));
6193 }
6194 break;
6195
6196 case LSHIFT_EXPR: case RSHIFT_EXPR:
6197 /* If the second operand is constant, this is a multiplication
6198 or floor division, by a power of two, so we can treat it that
6199 way unless the multiplier or divisor overflows. Signed
6200 left-shift overflow is implementation-defined rather than
6201 undefined in C90, so do not convert signed left shift into
6202 multiplication. */
6203 if (TREE_CODE (op1) == INTEGER_CST
6204 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6205 /* const_binop may not detect overflow correctly,
6206 so check for it explicitly here. */
6207 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6208 wi::to_wide (op1))
6209 && 0 != (t1 = fold_convert (ctype,
6210 const_binop (LSHIFT_EXPR,
6211 size_one_node,
6212 op1)))
6213 && !TREE_OVERFLOW (t1))
6214 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6215 ? MULT_EXPR : FLOOR_DIV_EXPR,
6216 ctype,
6217 fold_convert (ctype, op0),
6218 t1),
6219 c, code, wide_type, strict_overflow_p);
6220 break;
6221
6222 case PLUS_EXPR: case MINUS_EXPR:
6223 /* See if we can eliminate the operation on both sides. If we can, we
6224 can return a new PLUS or MINUS. If we can't, the only remaining
6225 cases where we can do anything are if the second operand is a
6226 constant. */
6227 sub_strict_overflow_p = false;
6228 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6229 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6230 if (t1 != 0 && t2 != 0
6231 && TYPE_OVERFLOW_WRAPS (ctype)
6232 && (code == MULT_EXPR
6233 /* If not multiplication, we can only do this if both operands
6234 are divisible by c. */
6235 || (multiple_of_p (ctype, op0, c)
6236 && multiple_of_p (ctype, op1, c))))
6237 {
6238 if (sub_strict_overflow_p)
6239 *strict_overflow_p = true;
6240 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6241 fold_convert (ctype, t2));
6242 }
6243
6244 /* If this was a subtraction, negate OP1 and set it to be an addition.
6245 This simplifies the logic below. */
6246 if (tcode == MINUS_EXPR)
6247 {
6248 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6249 /* If OP1 was not easily negatable, the constant may be OP0. */
6250 if (TREE_CODE (op0) == INTEGER_CST)
6251 {
6252 std::swap (op0, op1);
6253 std::swap (t1, t2);
6254 }
6255 }
6256
6257 if (TREE_CODE (op1) != INTEGER_CST)
6258 break;
6259
6260 /* If either OP1 or C are negative, this optimization is not safe for
6261 some of the division and remainder types while for others we need
6262 to change the code. */
6263 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6264 {
6265 if (code == CEIL_DIV_EXPR)
6266 code = FLOOR_DIV_EXPR;
6267 else if (code == FLOOR_DIV_EXPR)
6268 code = CEIL_DIV_EXPR;
6269 else if (code != MULT_EXPR
6270 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6271 break;
6272 }
6273
6274 /* If it's a multiply or a division/modulus operation of a multiple
6275 of our constant, do the operation and verify it doesn't overflow. */
6276 if (code == MULT_EXPR
6277 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6278 TYPE_SIGN (type)))
6279 {
6280 op1 = const_binop (code, fold_convert (ctype, op1),
6281 fold_convert (ctype, c));
6282 /* We allow the constant to overflow with wrapping semantics. */
6283 if (op1 == 0
6284 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6285 break;
6286 }
6287 else
6288 break;
6289
6290 /* If we have an unsigned type, we cannot widen the operation since it
6291 will change the result if the original computation overflowed. */
6292 if (TYPE_UNSIGNED (ctype) && ctype != type)
6293 break;
6294
6295 /* The last case is if we are a multiply. In that case, we can
6296 apply the distributive law to commute the multiply and addition
6297 if the multiplication of the constants doesn't overflow
6298 and overflow is defined. With undefined overflow
6299 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6300 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6301 return fold_build2 (tcode, ctype,
6302 fold_build2 (code, ctype,
6303 fold_convert (ctype, op0),
6304 fold_convert (ctype, c)),
6305 op1);
6306
6307 break;
6308
6309 case MULT_EXPR:
6310 /* We have a special case here if we are doing something like
6311 (C * 8) % 4 since we know that's zero. */
6312 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6313 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6314 /* If the multiplication can overflow we cannot optimize this. */
6315 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6316 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6317 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6318 TYPE_SIGN (type)))
6319 {
6320 *strict_overflow_p = true;
6321 return omit_one_operand (type, integer_zero_node, op0);
6322 }
6323
6324 /* ... fall through ... */
6325
6326 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6327 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6328 /* If we can extract our operation from the LHS, do so and return a
6329 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6330 do something only if the second operand is a constant. */
6331 if (same_p
6332 && TYPE_OVERFLOW_WRAPS (ctype)
6333 && (t1 = extract_muldiv (op0, c, code, wide_type,
6334 strict_overflow_p)) != 0)
6335 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6336 fold_convert (ctype, op1));
6337 else if (tcode == MULT_EXPR && code == MULT_EXPR
6338 && TYPE_OVERFLOW_WRAPS (ctype)
6339 && (t1 = extract_muldiv (op1, c, code, wide_type,
6340 strict_overflow_p)) != 0)
6341 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6342 fold_convert (ctype, t1));
6343 else if (TREE_CODE (op1) != INTEGER_CST)
6344 return 0;
6345
6346 /* If these are the same operation types, we can associate them
6347 assuming no overflow. */
6348 if (tcode == code)
6349 {
6350 bool overflow_p = false;
6351 bool overflow_mul_p;
6352 signop sign = TYPE_SIGN (ctype);
6353 unsigned prec = TYPE_PRECISION (ctype);
6354 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6355 wi::to_wide (c, prec),
6356 sign, &overflow_mul_p);
6357 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6358 if (overflow_mul_p
6359 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6360 overflow_p = true;
6361 if (!overflow_p)
6362 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6363 wide_int_to_tree (ctype, mul));
6364 }
6365
6366 /* If these operations "cancel" each other, we have the main
6367 optimizations of this pass, which occur when either constant is a
6368 multiple of the other, in which case we replace this with either an
6369 operation or CODE or TCODE.
6370
6371 If we have an unsigned type, we cannot do this since it will change
6372 the result if the original computation overflowed. */
6373 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6374 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6375 || (tcode == MULT_EXPR
6376 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6377 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6378 && code != MULT_EXPR)))
6379 {
6380 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6381 TYPE_SIGN (type)))
6382 {
6383 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6384 *strict_overflow_p = true;
6385 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6386 fold_convert (ctype,
6387 const_binop (TRUNC_DIV_EXPR,
6388 op1, c)));
6389 }
6390 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6391 TYPE_SIGN (type)))
6392 {
6393 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6394 *strict_overflow_p = true;
6395 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6396 fold_convert (ctype,
6397 const_binop (TRUNC_DIV_EXPR,
6398 c, op1)));
6399 }
6400 }
6401 break;
6402
6403 default:
6404 break;
6405 }
6406
6407 return 0;
6408 }
6409 \f
6410 /* Return a node which has the indicated constant VALUE (either 0 or
6411 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6412 and is of the indicated TYPE. */
6413
6414 tree
6415 constant_boolean_node (bool value, tree type)
6416 {
6417 if (type == integer_type_node)
6418 return value ? integer_one_node : integer_zero_node;
6419 else if (type == boolean_type_node)
6420 return value ? boolean_true_node : boolean_false_node;
6421 else if (TREE_CODE (type) == VECTOR_TYPE)
6422 return build_vector_from_val (type,
6423 build_int_cst (TREE_TYPE (type),
6424 value ? -1 : 0));
6425 else
6426 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6427 }
6428
6429
6430 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6431 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6432 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6433 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6434 COND is the first argument to CODE; otherwise (as in the example
6435 given here), it is the second argument. TYPE is the type of the
6436 original expression. Return NULL_TREE if no simplification is
6437 possible. */
6438
6439 static tree
6440 fold_binary_op_with_conditional_arg (location_t loc,
6441 enum tree_code code,
6442 tree type, tree op0, tree op1,
6443 tree cond, tree arg, int cond_first_p)
6444 {
6445 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6446 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6447 tree test, true_value, false_value;
6448 tree lhs = NULL_TREE;
6449 tree rhs = NULL_TREE;
6450 enum tree_code cond_code = COND_EXPR;
6451
6452 if (TREE_CODE (cond) == COND_EXPR
6453 || TREE_CODE (cond) == VEC_COND_EXPR)
6454 {
6455 test = TREE_OPERAND (cond, 0);
6456 true_value = TREE_OPERAND (cond, 1);
6457 false_value = TREE_OPERAND (cond, 2);
6458 /* If this operand throws an expression, then it does not make
6459 sense to try to perform a logical or arithmetic operation
6460 involving it. */
6461 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6462 lhs = true_value;
6463 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6464 rhs = false_value;
6465 }
6466 else if (!(TREE_CODE (type) != VECTOR_TYPE
6467 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6468 {
6469 tree testtype = TREE_TYPE (cond);
6470 test = cond;
6471 true_value = constant_boolean_node (true, testtype);
6472 false_value = constant_boolean_node (false, testtype);
6473 }
6474 else
6475 /* Detect the case of mixing vector and scalar types - bail out. */
6476 return NULL_TREE;
6477
6478 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6479 cond_code = VEC_COND_EXPR;
6480
6481 /* This transformation is only worthwhile if we don't have to wrap ARG
6482 in a SAVE_EXPR and the operation can be simplified without recursing
6483 on at least one of the branches once its pushed inside the COND_EXPR. */
6484 if (!TREE_CONSTANT (arg)
6485 && (TREE_SIDE_EFFECTS (arg)
6486 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6487 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6488 return NULL_TREE;
6489
6490 arg = fold_convert_loc (loc, arg_type, arg);
6491 if (lhs == 0)
6492 {
6493 true_value = fold_convert_loc (loc, cond_type, true_value);
6494 if (cond_first_p)
6495 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6496 else
6497 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6498 }
6499 if (rhs == 0)
6500 {
6501 false_value = fold_convert_loc (loc, cond_type, false_value);
6502 if (cond_first_p)
6503 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6504 else
6505 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6506 }
6507
6508 /* Check that we have simplified at least one of the branches. */
6509 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6510 return NULL_TREE;
6511
6512 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6513 }
6514
6515 \f
6516 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6517
6518 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6519 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6520 ADDEND is the same as X.
6521
6522 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6523 and finite. The problematic cases are when X is zero, and its mode
6524 has signed zeros. In the case of rounding towards -infinity,
6525 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6526 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6527
6528 bool
6529 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6530 {
6531 if (!real_zerop (addend))
6532 return false;
6533
6534 /* Don't allow the fold with -fsignaling-nans. */
6535 if (HONOR_SNANS (element_mode (type)))
6536 return false;
6537
6538 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6539 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6540 return true;
6541
6542 /* In a vector or complex, we would need to check the sign of all zeros. */
6543 if (TREE_CODE (addend) != REAL_CST)
6544 return false;
6545
6546 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6547 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6548 negate = !negate;
6549
6550 /* The mode has signed zeros, and we have to honor their sign.
6551 In this situation, there is only one case we can return true for.
6552 X - 0 is the same as X unless rounding towards -infinity is
6553 supported. */
6554 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6555 }
6556
6557 /* Subroutine of match.pd that optimizes comparisons of a division by
6558 a nonzero integer constant against an integer constant, i.e.
6559 X/C1 op C2.
6560
6561 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6562 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6563
6564 enum tree_code
6565 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6566 tree *hi, bool *neg_overflow)
6567 {
6568 tree prod, tmp, type = TREE_TYPE (c1);
6569 signop sign = TYPE_SIGN (type);
6570 bool overflow;
6571
6572 /* We have to do this the hard way to detect unsigned overflow.
6573 prod = int_const_binop (MULT_EXPR, c1, c2); */
6574 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6575 prod = force_fit_type (type, val, -1, overflow);
6576 *neg_overflow = false;
6577
6578 if (sign == UNSIGNED)
6579 {
6580 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6581 *lo = prod;
6582
6583 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6584 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6585 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6586 }
6587 else if (tree_int_cst_sgn (c1) >= 0)
6588 {
6589 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6590 switch (tree_int_cst_sgn (c2))
6591 {
6592 case -1:
6593 *neg_overflow = true;
6594 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6595 *hi = prod;
6596 break;
6597
6598 case 0:
6599 *lo = fold_negate_const (tmp, type);
6600 *hi = tmp;
6601 break;
6602
6603 case 1:
6604 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6605 *lo = prod;
6606 break;
6607
6608 default:
6609 gcc_unreachable ();
6610 }
6611 }
6612 else
6613 {
6614 /* A negative divisor reverses the relational operators. */
6615 code = swap_tree_comparison (code);
6616
6617 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6618 switch (tree_int_cst_sgn (c2))
6619 {
6620 case -1:
6621 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6622 *lo = prod;
6623 break;
6624
6625 case 0:
6626 *hi = fold_negate_const (tmp, type);
6627 *lo = tmp;
6628 break;
6629
6630 case 1:
6631 *neg_overflow = true;
6632 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6633 *hi = prod;
6634 break;
6635
6636 default:
6637 gcc_unreachable ();
6638 }
6639 }
6640
6641 if (code != EQ_EXPR && code != NE_EXPR)
6642 return code;
6643
6644 if (TREE_OVERFLOW (*lo)
6645 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6646 *lo = NULL_TREE;
6647 if (TREE_OVERFLOW (*hi)
6648 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6649 *hi = NULL_TREE;
6650
6651 return code;
6652 }
6653
6654
6655 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6656 equality/inequality test, then return a simplified form of the test
6657 using a sign testing. Otherwise return NULL. TYPE is the desired
6658 result type. */
6659
6660 static tree
6661 fold_single_bit_test_into_sign_test (location_t loc,
6662 enum tree_code code, tree arg0, tree arg1,
6663 tree result_type)
6664 {
6665 /* If this is testing a single bit, we can optimize the test. */
6666 if ((code == NE_EXPR || code == EQ_EXPR)
6667 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6668 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6669 {
6670 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6671 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6672 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6673
6674 if (arg00 != NULL_TREE
6675 /* This is only a win if casting to a signed type is cheap,
6676 i.e. when arg00's type is not a partial mode. */
6677 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6678 {
6679 tree stype = signed_type_for (TREE_TYPE (arg00));
6680 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6681 result_type,
6682 fold_convert_loc (loc, stype, arg00),
6683 build_int_cst (stype, 0));
6684 }
6685 }
6686
6687 return NULL_TREE;
6688 }
6689
6690 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6691 equality/inequality test, then return a simplified form of
6692 the test using shifts and logical operations. Otherwise return
6693 NULL. TYPE is the desired result type. */
6694
6695 tree
6696 fold_single_bit_test (location_t loc, enum tree_code code,
6697 tree arg0, tree arg1, tree result_type)
6698 {
6699 /* If this is testing a single bit, we can optimize the test. */
6700 if ((code == NE_EXPR || code == EQ_EXPR)
6701 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6702 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6703 {
6704 tree inner = TREE_OPERAND (arg0, 0);
6705 tree type = TREE_TYPE (arg0);
6706 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6707 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6708 int ops_unsigned;
6709 tree signed_type, unsigned_type, intermediate_type;
6710 tree tem, one;
6711
6712 /* First, see if we can fold the single bit test into a sign-bit
6713 test. */
6714 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6715 result_type);
6716 if (tem)
6717 return tem;
6718
6719 /* Otherwise we have (A & C) != 0 where C is a single bit,
6720 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6721 Similarly for (A & C) == 0. */
6722
6723 /* If INNER is a right shift of a constant and it plus BITNUM does
6724 not overflow, adjust BITNUM and INNER. */
6725 if (TREE_CODE (inner) == RSHIFT_EXPR
6726 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6727 && bitnum < TYPE_PRECISION (type)
6728 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6729 TYPE_PRECISION (type) - bitnum))
6730 {
6731 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6732 inner = TREE_OPERAND (inner, 0);
6733 }
6734
6735 /* If we are going to be able to omit the AND below, we must do our
6736 operations as unsigned. If we must use the AND, we have a choice.
6737 Normally unsigned is faster, but for some machines signed is. */
6738 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6739 && !flag_syntax_only) ? 0 : 1;
6740
6741 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6742 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6743 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6744 inner = fold_convert_loc (loc, intermediate_type, inner);
6745
6746 if (bitnum != 0)
6747 inner = build2 (RSHIFT_EXPR, intermediate_type,
6748 inner, size_int (bitnum));
6749
6750 one = build_int_cst (intermediate_type, 1);
6751
6752 if (code == EQ_EXPR)
6753 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6754
6755 /* Put the AND last so it can combine with more things. */
6756 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6757
6758 /* Make sure to return the proper type. */
6759 inner = fold_convert_loc (loc, result_type, inner);
6760
6761 return inner;
6762 }
6763 return NULL_TREE;
6764 }
6765
6766 /* Test whether it is preferable two swap two operands, ARG0 and
6767 ARG1, for example because ARG0 is an integer constant and ARG1
6768 isn't. */
6769
6770 bool
6771 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6772 {
6773 if (CONSTANT_CLASS_P (arg1))
6774 return 0;
6775 if (CONSTANT_CLASS_P (arg0))
6776 return 1;
6777
6778 STRIP_NOPS (arg0);
6779 STRIP_NOPS (arg1);
6780
6781 if (TREE_CONSTANT (arg1))
6782 return 0;
6783 if (TREE_CONSTANT (arg0))
6784 return 1;
6785
6786 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6787 for commutative and comparison operators. Ensuring a canonical
6788 form allows the optimizers to find additional redundancies without
6789 having to explicitly check for both orderings. */
6790 if (TREE_CODE (arg0) == SSA_NAME
6791 && TREE_CODE (arg1) == SSA_NAME
6792 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6793 return 1;
6794
6795 /* Put SSA_NAMEs last. */
6796 if (TREE_CODE (arg1) == SSA_NAME)
6797 return 0;
6798 if (TREE_CODE (arg0) == SSA_NAME)
6799 return 1;
6800
6801 /* Put variables last. */
6802 if (DECL_P (arg1))
6803 return 0;
6804 if (DECL_P (arg0))
6805 return 1;
6806
6807 return 0;
6808 }
6809
6810
6811 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6812 means A >= Y && A != MAX, but in this case we know that
6813 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6814
6815 static tree
6816 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6817 {
6818 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6819
6820 if (TREE_CODE (bound) == LT_EXPR)
6821 a = TREE_OPERAND (bound, 0);
6822 else if (TREE_CODE (bound) == GT_EXPR)
6823 a = TREE_OPERAND (bound, 1);
6824 else
6825 return NULL_TREE;
6826
6827 typea = TREE_TYPE (a);
6828 if (!INTEGRAL_TYPE_P (typea)
6829 && !POINTER_TYPE_P (typea))
6830 return NULL_TREE;
6831
6832 if (TREE_CODE (ineq) == LT_EXPR)
6833 {
6834 a1 = TREE_OPERAND (ineq, 1);
6835 y = TREE_OPERAND (ineq, 0);
6836 }
6837 else if (TREE_CODE (ineq) == GT_EXPR)
6838 {
6839 a1 = TREE_OPERAND (ineq, 0);
6840 y = TREE_OPERAND (ineq, 1);
6841 }
6842 else
6843 return NULL_TREE;
6844
6845 if (TREE_TYPE (a1) != typea)
6846 return NULL_TREE;
6847
6848 if (POINTER_TYPE_P (typea))
6849 {
6850 /* Convert the pointer types into integer before taking the difference. */
6851 tree ta = fold_convert_loc (loc, ssizetype, a);
6852 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6853 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6854 }
6855 else
6856 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6857
6858 if (!diff || !integer_onep (diff))
6859 return NULL_TREE;
6860
6861 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6862 }
6863
6864 /* Fold a sum or difference of at least one multiplication.
6865 Returns the folded tree or NULL if no simplification could be made. */
6866
6867 static tree
6868 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6869 tree arg0, tree arg1)
6870 {
6871 tree arg00, arg01, arg10, arg11;
6872 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6873
6874 /* (A * C) +- (B * C) -> (A+-B) * C.
6875 (A * C) +- A -> A * (C+-1).
6876 We are most concerned about the case where C is a constant,
6877 but other combinations show up during loop reduction. Since
6878 it is not difficult, try all four possibilities. */
6879
6880 if (TREE_CODE (arg0) == MULT_EXPR)
6881 {
6882 arg00 = TREE_OPERAND (arg0, 0);
6883 arg01 = TREE_OPERAND (arg0, 1);
6884 }
6885 else if (TREE_CODE (arg0) == INTEGER_CST)
6886 {
6887 arg00 = build_one_cst (type);
6888 arg01 = arg0;
6889 }
6890 else
6891 {
6892 /* We cannot generate constant 1 for fract. */
6893 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6894 return NULL_TREE;
6895 arg00 = arg0;
6896 arg01 = build_one_cst (type);
6897 }
6898 if (TREE_CODE (arg1) == MULT_EXPR)
6899 {
6900 arg10 = TREE_OPERAND (arg1, 0);
6901 arg11 = TREE_OPERAND (arg1, 1);
6902 }
6903 else if (TREE_CODE (arg1) == INTEGER_CST)
6904 {
6905 arg10 = build_one_cst (type);
6906 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6907 the purpose of this canonicalization. */
6908 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
6909 && negate_expr_p (arg1)
6910 && code == PLUS_EXPR)
6911 {
6912 arg11 = negate_expr (arg1);
6913 code = MINUS_EXPR;
6914 }
6915 else
6916 arg11 = arg1;
6917 }
6918 else
6919 {
6920 /* We cannot generate constant 1 for fract. */
6921 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6922 return NULL_TREE;
6923 arg10 = arg1;
6924 arg11 = build_one_cst (type);
6925 }
6926 same = NULL_TREE;
6927
6928 /* Prefer factoring a common non-constant. */
6929 if (operand_equal_p (arg00, arg10, 0))
6930 same = arg00, alt0 = arg01, alt1 = arg11;
6931 else if (operand_equal_p (arg01, arg11, 0))
6932 same = arg01, alt0 = arg00, alt1 = arg10;
6933 else if (operand_equal_p (arg00, arg11, 0))
6934 same = arg00, alt0 = arg01, alt1 = arg10;
6935 else if (operand_equal_p (arg01, arg10, 0))
6936 same = arg01, alt0 = arg00, alt1 = arg11;
6937
6938 /* No identical multiplicands; see if we can find a common
6939 power-of-two factor in non-power-of-two multiplies. This
6940 can help in multi-dimensional array access. */
6941 else if (tree_fits_shwi_p (arg01)
6942 && tree_fits_shwi_p (arg11))
6943 {
6944 HOST_WIDE_INT int01, int11, tmp;
6945 bool swap = false;
6946 tree maybe_same;
6947 int01 = tree_to_shwi (arg01);
6948 int11 = tree_to_shwi (arg11);
6949
6950 /* Move min of absolute values to int11. */
6951 if (absu_hwi (int01) < absu_hwi (int11))
6952 {
6953 tmp = int01, int01 = int11, int11 = tmp;
6954 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6955 maybe_same = arg01;
6956 swap = true;
6957 }
6958 else
6959 maybe_same = arg11;
6960
6961 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6962 /* The remainder should not be a constant, otherwise we
6963 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6964 increased the number of multiplications necessary. */
6965 && TREE_CODE (arg10) != INTEGER_CST)
6966 {
6967 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6968 build_int_cst (TREE_TYPE (arg00),
6969 int01 / int11));
6970 alt1 = arg10;
6971 same = maybe_same;
6972 if (swap)
6973 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6974 }
6975 }
6976
6977 if (!same)
6978 return NULL_TREE;
6979
6980 if (! INTEGRAL_TYPE_P (type)
6981 || TYPE_OVERFLOW_WRAPS (type)
6982 /* We are neither factoring zero nor minus one. */
6983 || TREE_CODE (same) == INTEGER_CST)
6984 return fold_build2_loc (loc, MULT_EXPR, type,
6985 fold_build2_loc (loc, code, type,
6986 fold_convert_loc (loc, type, alt0),
6987 fold_convert_loc (loc, type, alt1)),
6988 fold_convert_loc (loc, type, same));
6989
6990 /* Same may be zero and thus the operation 'code' may overflow. Likewise
6991 same may be minus one and thus the multiplication may overflow. Perform
6992 the operations in an unsigned type. */
6993 tree utype = unsigned_type_for (type);
6994 tree tem = fold_build2_loc (loc, code, utype,
6995 fold_convert_loc (loc, utype, alt0),
6996 fold_convert_loc (loc, utype, alt1));
6997 /* If the sum evaluated to a constant that is not -INF the multiplication
6998 cannot overflow. */
6999 if (TREE_CODE (tem) == INTEGER_CST
7000 && (wi::to_wide (tem)
7001 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7002 return fold_build2_loc (loc, MULT_EXPR, type,
7003 fold_convert (type, tem), same);
7004
7005 return fold_convert_loc (loc, type,
7006 fold_build2_loc (loc, MULT_EXPR, utype, tem,
7007 fold_convert_loc (loc, utype, same)));
7008 }
7009
7010 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7011 specified by EXPR into the buffer PTR of length LEN bytes.
7012 Return the number of bytes placed in the buffer, or zero
7013 upon failure. */
7014
7015 static int
7016 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7017 {
7018 tree type = TREE_TYPE (expr);
7019 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7020 int byte, offset, word, words;
7021 unsigned char value;
7022
7023 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7024 return 0;
7025 if (off == -1)
7026 off = 0;
7027
7028 if (ptr == NULL)
7029 /* Dry run. */
7030 return MIN (len, total_bytes - off);
7031
7032 words = total_bytes / UNITS_PER_WORD;
7033
7034 for (byte = 0; byte < total_bytes; byte++)
7035 {
7036 int bitpos = byte * BITS_PER_UNIT;
7037 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7038 number of bytes. */
7039 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7040
7041 if (total_bytes > UNITS_PER_WORD)
7042 {
7043 word = byte / UNITS_PER_WORD;
7044 if (WORDS_BIG_ENDIAN)
7045 word = (words - 1) - word;
7046 offset = word * UNITS_PER_WORD;
7047 if (BYTES_BIG_ENDIAN)
7048 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7049 else
7050 offset += byte % UNITS_PER_WORD;
7051 }
7052 else
7053 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7054 if (offset >= off && offset - off < len)
7055 ptr[offset - off] = value;
7056 }
7057 return MIN (len, total_bytes - off);
7058 }
7059
7060
7061 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7062 specified by EXPR into the buffer PTR of length LEN bytes.
7063 Return the number of bytes placed in the buffer, or zero
7064 upon failure. */
7065
7066 static int
7067 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7068 {
7069 tree type = TREE_TYPE (expr);
7070 scalar_mode mode = SCALAR_TYPE_MODE (type);
7071 int total_bytes = GET_MODE_SIZE (mode);
7072 FIXED_VALUE_TYPE value;
7073 tree i_value, i_type;
7074
7075 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7076 return 0;
7077
7078 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7079
7080 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7081 return 0;
7082
7083 value = TREE_FIXED_CST (expr);
7084 i_value = double_int_to_tree (i_type, value.data);
7085
7086 return native_encode_int (i_value, ptr, len, off);
7087 }
7088
7089
7090 /* Subroutine of native_encode_expr. Encode the REAL_CST
7091 specified by EXPR into the buffer PTR of length LEN bytes.
7092 Return the number of bytes placed in the buffer, or zero
7093 upon failure. */
7094
7095 static int
7096 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7097 {
7098 tree type = TREE_TYPE (expr);
7099 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7100 int byte, offset, word, words, bitpos;
7101 unsigned char value;
7102
7103 /* There are always 32 bits in each long, no matter the size of
7104 the hosts long. We handle floating point representations with
7105 up to 192 bits. */
7106 long tmp[6];
7107
7108 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7109 return 0;
7110 if (off == -1)
7111 off = 0;
7112
7113 if (ptr == NULL)
7114 /* Dry run. */
7115 return MIN (len, total_bytes - off);
7116
7117 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7118
7119 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7120
7121 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7122 bitpos += BITS_PER_UNIT)
7123 {
7124 byte = (bitpos / BITS_PER_UNIT) & 3;
7125 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7126
7127 if (UNITS_PER_WORD < 4)
7128 {
7129 word = byte / UNITS_PER_WORD;
7130 if (WORDS_BIG_ENDIAN)
7131 word = (words - 1) - word;
7132 offset = word * UNITS_PER_WORD;
7133 if (BYTES_BIG_ENDIAN)
7134 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7135 else
7136 offset += byte % UNITS_PER_WORD;
7137 }
7138 else
7139 {
7140 offset = byte;
7141 if (BYTES_BIG_ENDIAN)
7142 {
7143 /* Reverse bytes within each long, or within the entire float
7144 if it's smaller than a long (for HFmode). */
7145 offset = MIN (3, total_bytes - 1) - offset;
7146 gcc_assert (offset >= 0);
7147 }
7148 }
7149 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7150 if (offset >= off
7151 && offset - off < len)
7152 ptr[offset - off] = value;
7153 }
7154 return MIN (len, total_bytes - off);
7155 }
7156
7157 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7158 specified by EXPR into the buffer PTR of length LEN bytes.
7159 Return the number of bytes placed in the buffer, or zero
7160 upon failure. */
7161
7162 static int
7163 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7164 {
7165 int rsize, isize;
7166 tree part;
7167
7168 part = TREE_REALPART (expr);
7169 rsize = native_encode_expr (part, ptr, len, off);
7170 if (off == -1 && rsize == 0)
7171 return 0;
7172 part = TREE_IMAGPART (expr);
7173 if (off != -1)
7174 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7175 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7176 len - rsize, off);
7177 if (off == -1 && isize != rsize)
7178 return 0;
7179 return rsize + isize;
7180 }
7181
7182
7183 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7184 specified by EXPR into the buffer PTR of length LEN bytes.
7185 Return the number of bytes placed in the buffer, or zero
7186 upon failure. */
7187
7188 static int
7189 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7190 {
7191 unsigned i, count;
7192 int size, offset;
7193 tree itype, elem;
7194
7195 offset = 0;
7196 count = VECTOR_CST_NELTS (expr);
7197 itype = TREE_TYPE (TREE_TYPE (expr));
7198 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7199 for (i = 0; i < count; i++)
7200 {
7201 if (off >= size)
7202 {
7203 off -= size;
7204 continue;
7205 }
7206 elem = VECTOR_CST_ELT (expr, i);
7207 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7208 len - offset, off);
7209 if ((off == -1 && res != size) || res == 0)
7210 return 0;
7211 offset += res;
7212 if (offset >= len)
7213 return offset;
7214 if (off != -1)
7215 off = 0;
7216 }
7217 return offset;
7218 }
7219
7220
7221 /* Subroutine of native_encode_expr. Encode the STRING_CST
7222 specified by EXPR into the buffer PTR of length LEN bytes.
7223 Return the number of bytes placed in the buffer, or zero
7224 upon failure. */
7225
7226 static int
7227 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7228 {
7229 tree type = TREE_TYPE (expr);
7230
7231 /* Wide-char strings are encoded in target byte-order so native
7232 encoding them is trivial. */
7233 if (BITS_PER_UNIT != CHAR_BIT
7234 || TREE_CODE (type) != ARRAY_TYPE
7235 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7236 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7237 return 0;
7238
7239 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7240 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7241 return 0;
7242 if (off == -1)
7243 off = 0;
7244 if (ptr == NULL)
7245 /* Dry run. */;
7246 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7247 {
7248 int written = 0;
7249 if (off < TREE_STRING_LENGTH (expr))
7250 {
7251 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7252 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7253 }
7254 memset (ptr + written, 0,
7255 MIN (total_bytes - written, len - written));
7256 }
7257 else
7258 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7259 return MIN (total_bytes - off, len);
7260 }
7261
7262
7263 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7264 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7265 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7266 anything, just do a dry run. If OFF is not -1 then start
7267 the encoding at byte offset OFF and encode at most LEN bytes.
7268 Return the number of bytes placed in the buffer, or zero upon failure. */
7269
7270 int
7271 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7272 {
7273 /* We don't support starting at negative offset and -1 is special. */
7274 if (off < -1)
7275 return 0;
7276
7277 switch (TREE_CODE (expr))
7278 {
7279 case INTEGER_CST:
7280 return native_encode_int (expr, ptr, len, off);
7281
7282 case REAL_CST:
7283 return native_encode_real (expr, ptr, len, off);
7284
7285 case FIXED_CST:
7286 return native_encode_fixed (expr, ptr, len, off);
7287
7288 case COMPLEX_CST:
7289 return native_encode_complex (expr, ptr, len, off);
7290
7291 case VECTOR_CST:
7292 return native_encode_vector (expr, ptr, len, off);
7293
7294 case STRING_CST:
7295 return native_encode_string (expr, ptr, len, off);
7296
7297 default:
7298 return 0;
7299 }
7300 }
7301
7302
7303 /* Subroutine of native_interpret_expr. Interpret the contents of
7304 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7305 If the buffer cannot be interpreted, return NULL_TREE. */
7306
7307 static tree
7308 native_interpret_int (tree type, const unsigned char *ptr, int len)
7309 {
7310 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7311
7312 if (total_bytes > len
7313 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7314 return NULL_TREE;
7315
7316 wide_int result = wi::from_buffer (ptr, total_bytes);
7317
7318 return wide_int_to_tree (type, result);
7319 }
7320
7321
7322 /* Subroutine of native_interpret_expr. Interpret the contents of
7323 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7324 If the buffer cannot be interpreted, return NULL_TREE. */
7325
7326 static tree
7327 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7328 {
7329 scalar_mode mode = SCALAR_TYPE_MODE (type);
7330 int total_bytes = GET_MODE_SIZE (mode);
7331 double_int result;
7332 FIXED_VALUE_TYPE fixed_value;
7333
7334 if (total_bytes > len
7335 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7336 return NULL_TREE;
7337
7338 result = double_int::from_buffer (ptr, total_bytes);
7339 fixed_value = fixed_from_double_int (result, mode);
7340
7341 return build_fixed (type, fixed_value);
7342 }
7343
7344
7345 /* Subroutine of native_interpret_expr. Interpret the contents of
7346 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7347 If the buffer cannot be interpreted, return NULL_TREE. */
7348
7349 static tree
7350 native_interpret_real (tree type, const unsigned char *ptr, int len)
7351 {
7352 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7353 int total_bytes = GET_MODE_SIZE (mode);
7354 unsigned char value;
7355 /* There are always 32 bits in each long, no matter the size of
7356 the hosts long. We handle floating point representations with
7357 up to 192 bits. */
7358 REAL_VALUE_TYPE r;
7359 long tmp[6];
7360
7361 if (total_bytes > len || total_bytes > 24)
7362 return NULL_TREE;
7363 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7364
7365 memset (tmp, 0, sizeof (tmp));
7366 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7367 bitpos += BITS_PER_UNIT)
7368 {
7369 /* Both OFFSET and BYTE index within a long;
7370 bitpos indexes the whole float. */
7371 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7372 if (UNITS_PER_WORD < 4)
7373 {
7374 int word = byte / UNITS_PER_WORD;
7375 if (WORDS_BIG_ENDIAN)
7376 word = (words - 1) - word;
7377 offset = word * UNITS_PER_WORD;
7378 if (BYTES_BIG_ENDIAN)
7379 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7380 else
7381 offset += byte % UNITS_PER_WORD;
7382 }
7383 else
7384 {
7385 offset = byte;
7386 if (BYTES_BIG_ENDIAN)
7387 {
7388 /* Reverse bytes within each long, or within the entire float
7389 if it's smaller than a long (for HFmode). */
7390 offset = MIN (3, total_bytes - 1) - offset;
7391 gcc_assert (offset >= 0);
7392 }
7393 }
7394 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7395
7396 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7397 }
7398
7399 real_from_target (&r, tmp, mode);
7400 return build_real (type, r);
7401 }
7402
7403
7404 /* Subroutine of native_interpret_expr. Interpret the contents of
7405 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7406 If the buffer cannot be interpreted, return NULL_TREE. */
7407
7408 static tree
7409 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7410 {
7411 tree etype, rpart, ipart;
7412 int size;
7413
7414 etype = TREE_TYPE (type);
7415 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7416 if (size * 2 > len)
7417 return NULL_TREE;
7418 rpart = native_interpret_expr (etype, ptr, size);
7419 if (!rpart)
7420 return NULL_TREE;
7421 ipart = native_interpret_expr (etype, ptr+size, size);
7422 if (!ipart)
7423 return NULL_TREE;
7424 return build_complex (type, rpart, ipart);
7425 }
7426
7427
7428 /* Subroutine of native_interpret_expr. Interpret the contents of
7429 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7430 If the buffer cannot be interpreted, return NULL_TREE. */
7431
7432 static tree
7433 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7434 {
7435 tree etype, elem;
7436 int i, size, count;
7437
7438 etype = TREE_TYPE (type);
7439 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7440 count = TYPE_VECTOR_SUBPARTS (type);
7441 if (size * count > len)
7442 return NULL_TREE;
7443
7444 tree_vector_builder elements (type, count, 1);
7445 for (i = 0; i < count; ++i)
7446 {
7447 elem = native_interpret_expr (etype, ptr+(i*size), size);
7448 if (!elem)
7449 return NULL_TREE;
7450 elements.quick_push (elem);
7451 }
7452 return elements.build ();
7453 }
7454
7455
7456 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7457 the buffer PTR of length LEN as a constant of type TYPE. For
7458 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7459 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7460 return NULL_TREE. */
7461
7462 tree
7463 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7464 {
7465 switch (TREE_CODE (type))
7466 {
7467 case INTEGER_TYPE:
7468 case ENUMERAL_TYPE:
7469 case BOOLEAN_TYPE:
7470 case POINTER_TYPE:
7471 case REFERENCE_TYPE:
7472 return native_interpret_int (type, ptr, len);
7473
7474 case REAL_TYPE:
7475 return native_interpret_real (type, ptr, len);
7476
7477 case FIXED_POINT_TYPE:
7478 return native_interpret_fixed (type, ptr, len);
7479
7480 case COMPLEX_TYPE:
7481 return native_interpret_complex (type, ptr, len);
7482
7483 case VECTOR_TYPE:
7484 return native_interpret_vector (type, ptr, len);
7485
7486 default:
7487 return NULL_TREE;
7488 }
7489 }
7490
7491 /* Returns true if we can interpret the contents of a native encoding
7492 as TYPE. */
7493
7494 static bool
7495 can_native_interpret_type_p (tree type)
7496 {
7497 switch (TREE_CODE (type))
7498 {
7499 case INTEGER_TYPE:
7500 case ENUMERAL_TYPE:
7501 case BOOLEAN_TYPE:
7502 case POINTER_TYPE:
7503 case REFERENCE_TYPE:
7504 case FIXED_POINT_TYPE:
7505 case REAL_TYPE:
7506 case COMPLEX_TYPE:
7507 case VECTOR_TYPE:
7508 return true;
7509 default:
7510 return false;
7511 }
7512 }
7513
7514
7515 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7516 TYPE at compile-time. If we're unable to perform the conversion
7517 return NULL_TREE. */
7518
7519 static tree
7520 fold_view_convert_expr (tree type, tree expr)
7521 {
7522 /* We support up to 512-bit values (for V8DFmode). */
7523 unsigned char buffer[64];
7524 int len;
7525
7526 /* Check that the host and target are sane. */
7527 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7528 return NULL_TREE;
7529
7530 len = native_encode_expr (expr, buffer, sizeof (buffer));
7531 if (len == 0)
7532 return NULL_TREE;
7533
7534 return native_interpret_expr (type, buffer, len);
7535 }
7536
7537 /* Build an expression for the address of T. Folds away INDIRECT_REF
7538 to avoid confusing the gimplify process. */
7539
7540 tree
7541 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7542 {
7543 /* The size of the object is not relevant when talking about its address. */
7544 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7545 t = TREE_OPERAND (t, 0);
7546
7547 if (TREE_CODE (t) == INDIRECT_REF)
7548 {
7549 t = TREE_OPERAND (t, 0);
7550
7551 if (TREE_TYPE (t) != ptrtype)
7552 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7553 }
7554 else if (TREE_CODE (t) == MEM_REF
7555 && integer_zerop (TREE_OPERAND (t, 1)))
7556 return TREE_OPERAND (t, 0);
7557 else if (TREE_CODE (t) == MEM_REF
7558 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7559 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7560 TREE_OPERAND (t, 0),
7561 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7562 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7563 {
7564 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7565
7566 if (TREE_TYPE (t) != ptrtype)
7567 t = fold_convert_loc (loc, ptrtype, t);
7568 }
7569 else
7570 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7571
7572 return t;
7573 }
7574
7575 /* Build an expression for the address of T. */
7576
7577 tree
7578 build_fold_addr_expr_loc (location_t loc, tree t)
7579 {
7580 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7581
7582 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7583 }
7584
7585 /* Fold a unary expression of code CODE and type TYPE with operand
7586 OP0. Return the folded expression if folding is successful.
7587 Otherwise, return NULL_TREE. */
7588
7589 tree
7590 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7591 {
7592 tree tem;
7593 tree arg0;
7594 enum tree_code_class kind = TREE_CODE_CLASS (code);
7595
7596 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7597 && TREE_CODE_LENGTH (code) == 1);
7598
7599 arg0 = op0;
7600 if (arg0)
7601 {
7602 if (CONVERT_EXPR_CODE_P (code)
7603 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7604 {
7605 /* Don't use STRIP_NOPS, because signedness of argument type
7606 matters. */
7607 STRIP_SIGN_NOPS (arg0);
7608 }
7609 else
7610 {
7611 /* Strip any conversions that don't change the mode. This
7612 is safe for every expression, except for a comparison
7613 expression because its signedness is derived from its
7614 operands.
7615
7616 Note that this is done as an internal manipulation within
7617 the constant folder, in order to find the simplest
7618 representation of the arguments so that their form can be
7619 studied. In any cases, the appropriate type conversions
7620 should be put back in the tree that will get out of the
7621 constant folder. */
7622 STRIP_NOPS (arg0);
7623 }
7624
7625 if (CONSTANT_CLASS_P (arg0))
7626 {
7627 tree tem = const_unop (code, type, arg0);
7628 if (tem)
7629 {
7630 if (TREE_TYPE (tem) != type)
7631 tem = fold_convert_loc (loc, type, tem);
7632 return tem;
7633 }
7634 }
7635 }
7636
7637 tem = generic_simplify (loc, code, type, op0);
7638 if (tem)
7639 return tem;
7640
7641 if (TREE_CODE_CLASS (code) == tcc_unary)
7642 {
7643 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7644 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7645 fold_build1_loc (loc, code, type,
7646 fold_convert_loc (loc, TREE_TYPE (op0),
7647 TREE_OPERAND (arg0, 1))));
7648 else if (TREE_CODE (arg0) == COND_EXPR)
7649 {
7650 tree arg01 = TREE_OPERAND (arg0, 1);
7651 tree arg02 = TREE_OPERAND (arg0, 2);
7652 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7653 arg01 = fold_build1_loc (loc, code, type,
7654 fold_convert_loc (loc,
7655 TREE_TYPE (op0), arg01));
7656 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7657 arg02 = fold_build1_loc (loc, code, type,
7658 fold_convert_loc (loc,
7659 TREE_TYPE (op0), arg02));
7660 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7661 arg01, arg02);
7662
7663 /* If this was a conversion, and all we did was to move into
7664 inside the COND_EXPR, bring it back out. But leave it if
7665 it is a conversion from integer to integer and the
7666 result precision is no wider than a word since such a
7667 conversion is cheap and may be optimized away by combine,
7668 while it couldn't if it were outside the COND_EXPR. Then return
7669 so we don't get into an infinite recursion loop taking the
7670 conversion out and then back in. */
7671
7672 if ((CONVERT_EXPR_CODE_P (code)
7673 || code == NON_LVALUE_EXPR)
7674 && TREE_CODE (tem) == COND_EXPR
7675 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7676 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7677 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7678 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7679 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7680 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7681 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7682 && (INTEGRAL_TYPE_P
7683 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7684 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7685 || flag_syntax_only))
7686 tem = build1_loc (loc, code, type,
7687 build3 (COND_EXPR,
7688 TREE_TYPE (TREE_OPERAND
7689 (TREE_OPERAND (tem, 1), 0)),
7690 TREE_OPERAND (tem, 0),
7691 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7692 TREE_OPERAND (TREE_OPERAND (tem, 2),
7693 0)));
7694 return tem;
7695 }
7696 }
7697
7698 switch (code)
7699 {
7700 case NON_LVALUE_EXPR:
7701 if (!maybe_lvalue_p (op0))
7702 return fold_convert_loc (loc, type, op0);
7703 return NULL_TREE;
7704
7705 CASE_CONVERT:
7706 case FLOAT_EXPR:
7707 case FIX_TRUNC_EXPR:
7708 if (COMPARISON_CLASS_P (op0))
7709 {
7710 /* If we have (type) (a CMP b) and type is an integral type, return
7711 new expression involving the new type. Canonicalize
7712 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7713 non-integral type.
7714 Do not fold the result as that would not simplify further, also
7715 folding again results in recursions. */
7716 if (TREE_CODE (type) == BOOLEAN_TYPE)
7717 return build2_loc (loc, TREE_CODE (op0), type,
7718 TREE_OPERAND (op0, 0),
7719 TREE_OPERAND (op0, 1));
7720 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7721 && TREE_CODE (type) != VECTOR_TYPE)
7722 return build3_loc (loc, COND_EXPR, type, op0,
7723 constant_boolean_node (true, type),
7724 constant_boolean_node (false, type));
7725 }
7726
7727 /* Handle (T *)&A.B.C for A being of type T and B and C
7728 living at offset zero. This occurs frequently in
7729 C++ upcasting and then accessing the base. */
7730 if (TREE_CODE (op0) == ADDR_EXPR
7731 && POINTER_TYPE_P (type)
7732 && handled_component_p (TREE_OPERAND (op0, 0)))
7733 {
7734 HOST_WIDE_INT bitsize, bitpos;
7735 tree offset;
7736 machine_mode mode;
7737 int unsignedp, reversep, volatilep;
7738 tree base
7739 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7740 &offset, &mode, &unsignedp, &reversep,
7741 &volatilep);
7742 /* If the reference was to a (constant) zero offset, we can use
7743 the address of the base if it has the same base type
7744 as the result type and the pointer type is unqualified. */
7745 if (! offset && bitpos == 0
7746 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7747 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7748 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7749 return fold_convert_loc (loc, type,
7750 build_fold_addr_expr_loc (loc, base));
7751 }
7752
7753 if (TREE_CODE (op0) == MODIFY_EXPR
7754 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7755 /* Detect assigning a bitfield. */
7756 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7757 && DECL_BIT_FIELD
7758 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7759 {
7760 /* Don't leave an assignment inside a conversion
7761 unless assigning a bitfield. */
7762 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7763 /* First do the assignment, then return converted constant. */
7764 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7765 TREE_NO_WARNING (tem) = 1;
7766 TREE_USED (tem) = 1;
7767 return tem;
7768 }
7769
7770 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7771 constants (if x has signed type, the sign bit cannot be set
7772 in c). This folds extension into the BIT_AND_EXPR.
7773 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7774 very likely don't have maximal range for their precision and this
7775 transformation effectively doesn't preserve non-maximal ranges. */
7776 if (TREE_CODE (type) == INTEGER_TYPE
7777 && TREE_CODE (op0) == BIT_AND_EXPR
7778 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7779 {
7780 tree and_expr = op0;
7781 tree and0 = TREE_OPERAND (and_expr, 0);
7782 tree and1 = TREE_OPERAND (and_expr, 1);
7783 int change = 0;
7784
7785 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7786 || (TYPE_PRECISION (type)
7787 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7788 change = 1;
7789 else if (TYPE_PRECISION (TREE_TYPE (and1))
7790 <= HOST_BITS_PER_WIDE_INT
7791 && tree_fits_uhwi_p (and1))
7792 {
7793 unsigned HOST_WIDE_INT cst;
7794
7795 cst = tree_to_uhwi (and1);
7796 cst &= HOST_WIDE_INT_M1U
7797 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7798 change = (cst == 0);
7799 if (change
7800 && !flag_syntax_only
7801 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7802 == ZERO_EXTEND))
7803 {
7804 tree uns = unsigned_type_for (TREE_TYPE (and0));
7805 and0 = fold_convert_loc (loc, uns, and0);
7806 and1 = fold_convert_loc (loc, uns, and1);
7807 }
7808 }
7809 if (change)
7810 {
7811 tem = force_fit_type (type, wi::to_widest (and1), 0,
7812 TREE_OVERFLOW (and1));
7813 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7814 fold_convert_loc (loc, type, and0), tem);
7815 }
7816 }
7817
7818 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7819 cast (T1)X will fold away. We assume that this happens when X itself
7820 is a cast. */
7821 if (POINTER_TYPE_P (type)
7822 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7823 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7824 {
7825 tree arg00 = TREE_OPERAND (arg0, 0);
7826 tree arg01 = TREE_OPERAND (arg0, 1);
7827
7828 return fold_build_pointer_plus_loc
7829 (loc, fold_convert_loc (loc, type, arg00), arg01);
7830 }
7831
7832 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7833 of the same precision, and X is an integer type not narrower than
7834 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7835 if (INTEGRAL_TYPE_P (type)
7836 && TREE_CODE (op0) == BIT_NOT_EXPR
7837 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7838 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7839 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7840 {
7841 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7842 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7843 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7844 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7845 fold_convert_loc (loc, type, tem));
7846 }
7847
7848 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7849 type of X and Y (integer types only). */
7850 if (INTEGRAL_TYPE_P (type)
7851 && TREE_CODE (op0) == MULT_EXPR
7852 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7853 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7854 {
7855 /* Be careful not to introduce new overflows. */
7856 tree mult_type;
7857 if (TYPE_OVERFLOW_WRAPS (type))
7858 mult_type = type;
7859 else
7860 mult_type = unsigned_type_for (type);
7861
7862 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7863 {
7864 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7865 fold_convert_loc (loc, mult_type,
7866 TREE_OPERAND (op0, 0)),
7867 fold_convert_loc (loc, mult_type,
7868 TREE_OPERAND (op0, 1)));
7869 return fold_convert_loc (loc, type, tem);
7870 }
7871 }
7872
7873 return NULL_TREE;
7874
7875 case VIEW_CONVERT_EXPR:
7876 if (TREE_CODE (op0) == MEM_REF)
7877 {
7878 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7879 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7880 tem = fold_build2_loc (loc, MEM_REF, type,
7881 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7882 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7883 return tem;
7884 }
7885
7886 return NULL_TREE;
7887
7888 case NEGATE_EXPR:
7889 tem = fold_negate_expr (loc, arg0);
7890 if (tem)
7891 return fold_convert_loc (loc, type, tem);
7892 return NULL_TREE;
7893
7894 case ABS_EXPR:
7895 /* Convert fabs((double)float) into (double)fabsf(float). */
7896 if (TREE_CODE (arg0) == NOP_EXPR
7897 && TREE_CODE (type) == REAL_TYPE)
7898 {
7899 tree targ0 = strip_float_extensions (arg0);
7900 if (targ0 != arg0)
7901 return fold_convert_loc (loc, type,
7902 fold_build1_loc (loc, ABS_EXPR,
7903 TREE_TYPE (targ0),
7904 targ0));
7905 }
7906 return NULL_TREE;
7907
7908 case BIT_NOT_EXPR:
7909 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7910 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7911 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7912 fold_convert_loc (loc, type,
7913 TREE_OPERAND (arg0, 0)))))
7914 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7915 fold_convert_loc (loc, type,
7916 TREE_OPERAND (arg0, 1)));
7917 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7918 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7919 fold_convert_loc (loc, type,
7920 TREE_OPERAND (arg0, 1)))))
7921 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7922 fold_convert_loc (loc, type,
7923 TREE_OPERAND (arg0, 0)), tem);
7924
7925 return NULL_TREE;
7926
7927 case TRUTH_NOT_EXPR:
7928 /* Note that the operand of this must be an int
7929 and its values must be 0 or 1.
7930 ("true" is a fixed value perhaps depending on the language,
7931 but we don't handle values other than 1 correctly yet.) */
7932 tem = fold_truth_not_expr (loc, arg0);
7933 if (!tem)
7934 return NULL_TREE;
7935 return fold_convert_loc (loc, type, tem);
7936
7937 case INDIRECT_REF:
7938 /* Fold *&X to X if X is an lvalue. */
7939 if (TREE_CODE (op0) == ADDR_EXPR)
7940 {
7941 tree op00 = TREE_OPERAND (op0, 0);
7942 if ((VAR_P (op00)
7943 || TREE_CODE (op00) == PARM_DECL
7944 || TREE_CODE (op00) == RESULT_DECL)
7945 && !TREE_READONLY (op00))
7946 return op00;
7947 }
7948 return NULL_TREE;
7949
7950 default:
7951 return NULL_TREE;
7952 } /* switch (code) */
7953 }
7954
7955
7956 /* If the operation was a conversion do _not_ mark a resulting constant
7957 with TREE_OVERFLOW if the original constant was not. These conversions
7958 have implementation defined behavior and retaining the TREE_OVERFLOW
7959 flag here would confuse later passes such as VRP. */
7960 tree
7961 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7962 tree type, tree op0)
7963 {
7964 tree res = fold_unary_loc (loc, code, type, op0);
7965 if (res
7966 && TREE_CODE (res) == INTEGER_CST
7967 && TREE_CODE (op0) == INTEGER_CST
7968 && CONVERT_EXPR_CODE_P (code))
7969 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7970
7971 return res;
7972 }
7973
7974 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7975 operands OP0 and OP1. LOC is the location of the resulting expression.
7976 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7977 Return the folded expression if folding is successful. Otherwise,
7978 return NULL_TREE. */
7979 static tree
7980 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7981 tree arg0, tree arg1, tree op0, tree op1)
7982 {
7983 tree tem;
7984
7985 /* We only do these simplifications if we are optimizing. */
7986 if (!optimize)
7987 return NULL_TREE;
7988
7989 /* Check for things like (A || B) && (A || C). We can convert this
7990 to A || (B && C). Note that either operator can be any of the four
7991 truth and/or operations and the transformation will still be
7992 valid. Also note that we only care about order for the
7993 ANDIF and ORIF operators. If B contains side effects, this
7994 might change the truth-value of A. */
7995 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7996 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7997 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7998 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7999 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8000 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8001 {
8002 tree a00 = TREE_OPERAND (arg0, 0);
8003 tree a01 = TREE_OPERAND (arg0, 1);
8004 tree a10 = TREE_OPERAND (arg1, 0);
8005 tree a11 = TREE_OPERAND (arg1, 1);
8006 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8007 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8008 && (code == TRUTH_AND_EXPR
8009 || code == TRUTH_OR_EXPR));
8010
8011 if (operand_equal_p (a00, a10, 0))
8012 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8013 fold_build2_loc (loc, code, type, a01, a11));
8014 else if (commutative && operand_equal_p (a00, a11, 0))
8015 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8016 fold_build2_loc (loc, code, type, a01, a10));
8017 else if (commutative && operand_equal_p (a01, a10, 0))
8018 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8019 fold_build2_loc (loc, code, type, a00, a11));
8020
8021 /* This case if tricky because we must either have commutative
8022 operators or else A10 must not have side-effects. */
8023
8024 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8025 && operand_equal_p (a01, a11, 0))
8026 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8027 fold_build2_loc (loc, code, type, a00, a10),
8028 a01);
8029 }
8030
8031 /* See if we can build a range comparison. */
8032 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8033 return tem;
8034
8035 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8036 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8037 {
8038 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8039 if (tem)
8040 return fold_build2_loc (loc, code, type, tem, arg1);
8041 }
8042
8043 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8044 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8045 {
8046 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8047 if (tem)
8048 return fold_build2_loc (loc, code, type, arg0, tem);
8049 }
8050
8051 /* Check for the possibility of merging component references. If our
8052 lhs is another similar operation, try to merge its rhs with our
8053 rhs. Then try to merge our lhs and rhs. */
8054 if (TREE_CODE (arg0) == code
8055 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8056 TREE_OPERAND (arg0, 1), arg1)))
8057 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8058
8059 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8060 return tem;
8061
8062 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8063 && !flag_sanitize_coverage
8064 && (code == TRUTH_AND_EXPR
8065 || code == TRUTH_ANDIF_EXPR
8066 || code == TRUTH_OR_EXPR
8067 || code == TRUTH_ORIF_EXPR))
8068 {
8069 enum tree_code ncode, icode;
8070
8071 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8072 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8073 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8074
8075 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8076 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8077 We don't want to pack more than two leafs to a non-IF AND/OR
8078 expression.
8079 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8080 equal to IF-CODE, then we don't want to add right-hand operand.
8081 If the inner right-hand side of left-hand operand has
8082 side-effects, or isn't simple, then we can't add to it,
8083 as otherwise we might destroy if-sequence. */
8084 if (TREE_CODE (arg0) == icode
8085 && simple_operand_p_2 (arg1)
8086 /* Needed for sequence points to handle trappings, and
8087 side-effects. */
8088 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8089 {
8090 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8091 arg1);
8092 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8093 tem);
8094 }
8095 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8096 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8097 else if (TREE_CODE (arg1) == icode
8098 && simple_operand_p_2 (arg0)
8099 /* Needed for sequence points to handle trappings, and
8100 side-effects. */
8101 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8102 {
8103 tem = fold_build2_loc (loc, ncode, type,
8104 arg0, TREE_OPERAND (arg1, 0));
8105 return fold_build2_loc (loc, icode, type, tem,
8106 TREE_OPERAND (arg1, 1));
8107 }
8108 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8109 into (A OR B).
8110 For sequence point consistancy, we need to check for trapping,
8111 and side-effects. */
8112 else if (code == icode && simple_operand_p_2 (arg0)
8113 && simple_operand_p_2 (arg1))
8114 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8115 }
8116
8117 return NULL_TREE;
8118 }
8119
8120 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8121 by changing CODE to reduce the magnitude of constants involved in
8122 ARG0 of the comparison.
8123 Returns a canonicalized comparison tree if a simplification was
8124 possible, otherwise returns NULL_TREE.
8125 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8126 valid if signed overflow is undefined. */
8127
8128 static tree
8129 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8130 tree arg0, tree arg1,
8131 bool *strict_overflow_p)
8132 {
8133 enum tree_code code0 = TREE_CODE (arg0);
8134 tree t, cst0 = NULL_TREE;
8135 int sgn0;
8136
8137 /* Match A +- CST code arg1. We can change this only if overflow
8138 is undefined. */
8139 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8140 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8141 /* In principle pointers also have undefined overflow behavior,
8142 but that causes problems elsewhere. */
8143 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8144 && (code0 == MINUS_EXPR
8145 || code0 == PLUS_EXPR)
8146 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8147 return NULL_TREE;
8148
8149 /* Identify the constant in arg0 and its sign. */
8150 cst0 = TREE_OPERAND (arg0, 1);
8151 sgn0 = tree_int_cst_sgn (cst0);
8152
8153 /* Overflowed constants and zero will cause problems. */
8154 if (integer_zerop (cst0)
8155 || TREE_OVERFLOW (cst0))
8156 return NULL_TREE;
8157
8158 /* See if we can reduce the magnitude of the constant in
8159 arg0 by changing the comparison code. */
8160 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8161 if (code == LT_EXPR
8162 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8163 code = LE_EXPR;
8164 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8165 else if (code == GT_EXPR
8166 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8167 code = GE_EXPR;
8168 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8169 else if (code == LE_EXPR
8170 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8171 code = LT_EXPR;
8172 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8173 else if (code == GE_EXPR
8174 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8175 code = GT_EXPR;
8176 else
8177 return NULL_TREE;
8178 *strict_overflow_p = true;
8179
8180 /* Now build the constant reduced in magnitude. But not if that
8181 would produce one outside of its types range. */
8182 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8183 && ((sgn0 == 1
8184 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8185 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8186 || (sgn0 == -1
8187 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8188 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8189 return NULL_TREE;
8190
8191 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8192 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8193 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8194 t = fold_convert (TREE_TYPE (arg1), t);
8195
8196 return fold_build2_loc (loc, code, type, t, arg1);
8197 }
8198
8199 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8200 overflow further. Try to decrease the magnitude of constants involved
8201 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8202 and put sole constants at the second argument position.
8203 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8204
8205 static tree
8206 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8207 tree arg0, tree arg1)
8208 {
8209 tree t;
8210 bool strict_overflow_p;
8211 const char * const warnmsg = G_("assuming signed overflow does not occur "
8212 "when reducing constant in comparison");
8213
8214 /* Try canonicalization by simplifying arg0. */
8215 strict_overflow_p = false;
8216 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8217 &strict_overflow_p);
8218 if (t)
8219 {
8220 if (strict_overflow_p)
8221 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8222 return t;
8223 }
8224
8225 /* Try canonicalization by simplifying arg1 using the swapped
8226 comparison. */
8227 code = swap_tree_comparison (code);
8228 strict_overflow_p = false;
8229 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8230 &strict_overflow_p);
8231 if (t && strict_overflow_p)
8232 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8233 return t;
8234 }
8235
8236 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8237 space. This is used to avoid issuing overflow warnings for
8238 expressions like &p->x which can not wrap. */
8239
8240 static bool
8241 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8242 {
8243 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8244 return true;
8245
8246 if (bitpos < 0)
8247 return true;
8248
8249 wide_int wi_offset;
8250 int precision = TYPE_PRECISION (TREE_TYPE (base));
8251 if (offset == NULL_TREE)
8252 wi_offset = wi::zero (precision);
8253 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8254 return true;
8255 else
8256 wi_offset = wi::to_wide (offset);
8257
8258 bool overflow;
8259 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8260 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8261 if (overflow)
8262 return true;
8263
8264 if (!wi::fits_uhwi_p (total))
8265 return true;
8266
8267 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8268 if (size <= 0)
8269 return true;
8270
8271 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8272 array. */
8273 if (TREE_CODE (base) == ADDR_EXPR)
8274 {
8275 HOST_WIDE_INT base_size;
8276
8277 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8278 if (base_size > 0 && size < base_size)
8279 size = base_size;
8280 }
8281
8282 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8283 }
8284
8285 /* Return a positive integer when the symbol DECL is known to have
8286 a nonzero address, zero when it's known not to (e.g., it's a weak
8287 symbol), and a negative integer when the symbol is not yet in the
8288 symbol table and so whether or not its address is zero is unknown.
8289 For function local objects always return positive integer. */
8290 static int
8291 maybe_nonzero_address (tree decl)
8292 {
8293 if (DECL_P (decl) && decl_in_symtab_p (decl))
8294 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8295 return symbol->nonzero_address ();
8296
8297 /* Function local objects are never NULL. */
8298 if (DECL_P (decl)
8299 && (DECL_CONTEXT (decl)
8300 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8301 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8302 return 1;
8303
8304 return -1;
8305 }
8306
8307 /* Subroutine of fold_binary. This routine performs all of the
8308 transformations that are common to the equality/inequality
8309 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8310 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8311 fold_binary should call fold_binary. Fold a comparison with
8312 tree code CODE and type TYPE with operands OP0 and OP1. Return
8313 the folded comparison or NULL_TREE. */
8314
8315 static tree
8316 fold_comparison (location_t loc, enum tree_code code, tree type,
8317 tree op0, tree op1)
8318 {
8319 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8320 tree arg0, arg1, tem;
8321
8322 arg0 = op0;
8323 arg1 = op1;
8324
8325 STRIP_SIGN_NOPS (arg0);
8326 STRIP_SIGN_NOPS (arg1);
8327
8328 /* For comparisons of pointers we can decompose it to a compile time
8329 comparison of the base objects and the offsets into the object.
8330 This requires at least one operand being an ADDR_EXPR or a
8331 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8332 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8333 && (TREE_CODE (arg0) == ADDR_EXPR
8334 || TREE_CODE (arg1) == ADDR_EXPR
8335 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8336 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8337 {
8338 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8339 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8340 machine_mode mode;
8341 int volatilep, reversep, unsignedp;
8342 bool indirect_base0 = false, indirect_base1 = false;
8343
8344 /* Get base and offset for the access. Strip ADDR_EXPR for
8345 get_inner_reference, but put it back by stripping INDIRECT_REF
8346 off the base object if possible. indirect_baseN will be true
8347 if baseN is not an address but refers to the object itself. */
8348 base0 = arg0;
8349 if (TREE_CODE (arg0) == ADDR_EXPR)
8350 {
8351 base0
8352 = get_inner_reference (TREE_OPERAND (arg0, 0),
8353 &bitsize, &bitpos0, &offset0, &mode,
8354 &unsignedp, &reversep, &volatilep);
8355 if (TREE_CODE (base0) == INDIRECT_REF)
8356 base0 = TREE_OPERAND (base0, 0);
8357 else
8358 indirect_base0 = true;
8359 }
8360 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8361 {
8362 base0 = TREE_OPERAND (arg0, 0);
8363 STRIP_SIGN_NOPS (base0);
8364 if (TREE_CODE (base0) == ADDR_EXPR)
8365 {
8366 base0
8367 = get_inner_reference (TREE_OPERAND (base0, 0),
8368 &bitsize, &bitpos0, &offset0, &mode,
8369 &unsignedp, &reversep, &volatilep);
8370 if (TREE_CODE (base0) == INDIRECT_REF)
8371 base0 = TREE_OPERAND (base0, 0);
8372 else
8373 indirect_base0 = true;
8374 }
8375 if (offset0 == NULL_TREE || integer_zerop (offset0))
8376 offset0 = TREE_OPERAND (arg0, 1);
8377 else
8378 offset0 = size_binop (PLUS_EXPR, offset0,
8379 TREE_OPERAND (arg0, 1));
8380 if (TREE_CODE (offset0) == INTEGER_CST)
8381 {
8382 offset_int tem = wi::sext (wi::to_offset (offset0),
8383 TYPE_PRECISION (sizetype));
8384 tem <<= LOG2_BITS_PER_UNIT;
8385 tem += bitpos0;
8386 if (wi::fits_shwi_p (tem))
8387 {
8388 bitpos0 = tem.to_shwi ();
8389 offset0 = NULL_TREE;
8390 }
8391 }
8392 }
8393
8394 base1 = arg1;
8395 if (TREE_CODE (arg1) == ADDR_EXPR)
8396 {
8397 base1
8398 = get_inner_reference (TREE_OPERAND (arg1, 0),
8399 &bitsize, &bitpos1, &offset1, &mode,
8400 &unsignedp, &reversep, &volatilep);
8401 if (TREE_CODE (base1) == INDIRECT_REF)
8402 base1 = TREE_OPERAND (base1, 0);
8403 else
8404 indirect_base1 = true;
8405 }
8406 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8407 {
8408 base1 = TREE_OPERAND (arg1, 0);
8409 STRIP_SIGN_NOPS (base1);
8410 if (TREE_CODE (base1) == ADDR_EXPR)
8411 {
8412 base1
8413 = get_inner_reference (TREE_OPERAND (base1, 0),
8414 &bitsize, &bitpos1, &offset1, &mode,
8415 &unsignedp, &reversep, &volatilep);
8416 if (TREE_CODE (base1) == INDIRECT_REF)
8417 base1 = TREE_OPERAND (base1, 0);
8418 else
8419 indirect_base1 = true;
8420 }
8421 if (offset1 == NULL_TREE || integer_zerop (offset1))
8422 offset1 = TREE_OPERAND (arg1, 1);
8423 else
8424 offset1 = size_binop (PLUS_EXPR, offset1,
8425 TREE_OPERAND (arg1, 1));
8426 if (TREE_CODE (offset1) == INTEGER_CST)
8427 {
8428 offset_int tem = wi::sext (wi::to_offset (offset1),
8429 TYPE_PRECISION (sizetype));
8430 tem <<= LOG2_BITS_PER_UNIT;
8431 tem += bitpos1;
8432 if (wi::fits_shwi_p (tem))
8433 {
8434 bitpos1 = tem.to_shwi ();
8435 offset1 = NULL_TREE;
8436 }
8437 }
8438 }
8439
8440 /* If we have equivalent bases we might be able to simplify. */
8441 if (indirect_base0 == indirect_base1
8442 && operand_equal_p (base0, base1,
8443 indirect_base0 ? OEP_ADDRESS_OF : 0))
8444 {
8445 /* We can fold this expression to a constant if the non-constant
8446 offset parts are equal. */
8447 if (offset0 == offset1
8448 || (offset0 && offset1
8449 && operand_equal_p (offset0, offset1, 0)))
8450 {
8451 if (!equality_code
8452 && bitpos0 != bitpos1
8453 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8454 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8455 fold_overflow_warning (("assuming pointer wraparound does not "
8456 "occur when comparing P +- C1 with "
8457 "P +- C2"),
8458 WARN_STRICT_OVERFLOW_CONDITIONAL);
8459
8460 switch (code)
8461 {
8462 case EQ_EXPR:
8463 return constant_boolean_node (bitpos0 == bitpos1, type);
8464 case NE_EXPR:
8465 return constant_boolean_node (bitpos0 != bitpos1, type);
8466 case LT_EXPR:
8467 return constant_boolean_node (bitpos0 < bitpos1, type);
8468 case LE_EXPR:
8469 return constant_boolean_node (bitpos0 <= bitpos1, type);
8470 case GE_EXPR:
8471 return constant_boolean_node (bitpos0 >= bitpos1, type);
8472 case GT_EXPR:
8473 return constant_boolean_node (bitpos0 > bitpos1, type);
8474 default:;
8475 }
8476 }
8477 /* We can simplify the comparison to a comparison of the variable
8478 offset parts if the constant offset parts are equal.
8479 Be careful to use signed sizetype here because otherwise we
8480 mess with array offsets in the wrong way. This is possible
8481 because pointer arithmetic is restricted to retain within an
8482 object and overflow on pointer differences is undefined as of
8483 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8484 else if (bitpos0 == bitpos1)
8485 {
8486 /* By converting to signed sizetype we cover middle-end pointer
8487 arithmetic which operates on unsigned pointer types of size
8488 type size and ARRAY_REF offsets which are properly sign or
8489 zero extended from their type in case it is narrower than
8490 sizetype. */
8491 if (offset0 == NULL_TREE)
8492 offset0 = build_int_cst (ssizetype, 0);
8493 else
8494 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8495 if (offset1 == NULL_TREE)
8496 offset1 = build_int_cst (ssizetype, 0);
8497 else
8498 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8499
8500 if (!equality_code
8501 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8502 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8503 fold_overflow_warning (("assuming pointer wraparound does not "
8504 "occur when comparing P +- C1 with "
8505 "P +- C2"),
8506 WARN_STRICT_OVERFLOW_COMPARISON);
8507
8508 return fold_build2_loc (loc, code, type, offset0, offset1);
8509 }
8510 }
8511 /* For equal offsets we can simplify to a comparison of the
8512 base addresses. */
8513 else if (bitpos0 == bitpos1
8514 && (indirect_base0
8515 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8516 && (indirect_base1
8517 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8518 && ((offset0 == offset1)
8519 || (offset0 && offset1
8520 && operand_equal_p (offset0, offset1, 0))))
8521 {
8522 if (indirect_base0)
8523 base0 = build_fold_addr_expr_loc (loc, base0);
8524 if (indirect_base1)
8525 base1 = build_fold_addr_expr_loc (loc, base1);
8526 return fold_build2_loc (loc, code, type, base0, base1);
8527 }
8528 /* Comparison between an ordinary (non-weak) symbol and a null
8529 pointer can be eliminated since such symbols must have a non
8530 null address. In C, relational expressions between pointers
8531 to objects and null pointers are undefined. The results
8532 below follow the C++ rules with the additional property that
8533 every object pointer compares greater than a null pointer.
8534 */
8535 else if (((DECL_P (base0)
8536 && maybe_nonzero_address (base0) > 0
8537 /* Avoid folding references to struct members at offset 0 to
8538 prevent tests like '&ptr->firstmember == 0' from getting
8539 eliminated. When ptr is null, although the -> expression
8540 is strictly speaking invalid, GCC retains it as a matter
8541 of QoI. See PR c/44555. */
8542 && (offset0 == NULL_TREE && bitpos0 != 0))
8543 || CONSTANT_CLASS_P (base0))
8544 && indirect_base0
8545 /* The caller guarantees that when one of the arguments is
8546 constant (i.e., null in this case) it is second. */
8547 && integer_zerop (arg1))
8548 {
8549 switch (code)
8550 {
8551 case EQ_EXPR:
8552 case LE_EXPR:
8553 case LT_EXPR:
8554 return constant_boolean_node (false, type);
8555 case GE_EXPR:
8556 case GT_EXPR:
8557 case NE_EXPR:
8558 return constant_boolean_node (true, type);
8559 default:
8560 gcc_unreachable ();
8561 }
8562 }
8563 }
8564
8565 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8566 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8567 the resulting offset is smaller in absolute value than the
8568 original one and has the same sign. */
8569 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8570 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8571 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8572 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8573 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8574 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8575 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8576 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8577 {
8578 tree const1 = TREE_OPERAND (arg0, 1);
8579 tree const2 = TREE_OPERAND (arg1, 1);
8580 tree variable1 = TREE_OPERAND (arg0, 0);
8581 tree variable2 = TREE_OPERAND (arg1, 0);
8582 tree cst;
8583 const char * const warnmsg = G_("assuming signed overflow does not "
8584 "occur when combining constants around "
8585 "a comparison");
8586
8587 /* Put the constant on the side where it doesn't overflow and is
8588 of lower absolute value and of same sign than before. */
8589 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8590 ? MINUS_EXPR : PLUS_EXPR,
8591 const2, const1);
8592 if (!TREE_OVERFLOW (cst)
8593 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8594 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8595 {
8596 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8597 return fold_build2_loc (loc, code, type,
8598 variable1,
8599 fold_build2_loc (loc, TREE_CODE (arg1),
8600 TREE_TYPE (arg1),
8601 variable2, cst));
8602 }
8603
8604 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8605 ? MINUS_EXPR : PLUS_EXPR,
8606 const1, const2);
8607 if (!TREE_OVERFLOW (cst)
8608 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8609 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8610 {
8611 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8612 return fold_build2_loc (loc, code, type,
8613 fold_build2_loc (loc, TREE_CODE (arg0),
8614 TREE_TYPE (arg0),
8615 variable1, cst),
8616 variable2);
8617 }
8618 }
8619
8620 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8621 if (tem)
8622 return tem;
8623
8624 /* If we are comparing an expression that just has comparisons
8625 of two integer values, arithmetic expressions of those comparisons,
8626 and constants, we can simplify it. There are only three cases
8627 to check: the two values can either be equal, the first can be
8628 greater, or the second can be greater. Fold the expression for
8629 those three values. Since each value must be 0 or 1, we have
8630 eight possibilities, each of which corresponds to the constant 0
8631 or 1 or one of the six possible comparisons.
8632
8633 This handles common cases like (a > b) == 0 but also handles
8634 expressions like ((x > y) - (y > x)) > 0, which supposedly
8635 occur in macroized code. */
8636
8637 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8638 {
8639 tree cval1 = 0, cval2 = 0;
8640 int save_p = 0;
8641
8642 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8643 /* Don't handle degenerate cases here; they should already
8644 have been handled anyway. */
8645 && cval1 != 0 && cval2 != 0
8646 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8647 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8648 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8649 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8650 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8651 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8652 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8653 {
8654 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8655 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8656
8657 /* We can't just pass T to eval_subst in case cval1 or cval2
8658 was the same as ARG1. */
8659
8660 tree high_result
8661 = fold_build2_loc (loc, code, type,
8662 eval_subst (loc, arg0, cval1, maxval,
8663 cval2, minval),
8664 arg1);
8665 tree equal_result
8666 = fold_build2_loc (loc, code, type,
8667 eval_subst (loc, arg0, cval1, maxval,
8668 cval2, maxval),
8669 arg1);
8670 tree low_result
8671 = fold_build2_loc (loc, code, type,
8672 eval_subst (loc, arg0, cval1, minval,
8673 cval2, maxval),
8674 arg1);
8675
8676 /* All three of these results should be 0 or 1. Confirm they are.
8677 Then use those values to select the proper code to use. */
8678
8679 if (TREE_CODE (high_result) == INTEGER_CST
8680 && TREE_CODE (equal_result) == INTEGER_CST
8681 && TREE_CODE (low_result) == INTEGER_CST)
8682 {
8683 /* Make a 3-bit mask with the high-order bit being the
8684 value for `>', the next for '=', and the low for '<'. */
8685 switch ((integer_onep (high_result) * 4)
8686 + (integer_onep (equal_result) * 2)
8687 + integer_onep (low_result))
8688 {
8689 case 0:
8690 /* Always false. */
8691 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8692 case 1:
8693 code = LT_EXPR;
8694 break;
8695 case 2:
8696 code = EQ_EXPR;
8697 break;
8698 case 3:
8699 code = LE_EXPR;
8700 break;
8701 case 4:
8702 code = GT_EXPR;
8703 break;
8704 case 5:
8705 code = NE_EXPR;
8706 break;
8707 case 6:
8708 code = GE_EXPR;
8709 break;
8710 case 7:
8711 /* Always true. */
8712 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8713 }
8714
8715 if (save_p)
8716 {
8717 tem = save_expr (build2 (code, type, cval1, cval2));
8718 protected_set_expr_location (tem, loc);
8719 return tem;
8720 }
8721 return fold_build2_loc (loc, code, type, cval1, cval2);
8722 }
8723 }
8724 }
8725
8726 return NULL_TREE;
8727 }
8728
8729
8730 /* Subroutine of fold_binary. Optimize complex multiplications of the
8731 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8732 argument EXPR represents the expression "z" of type TYPE. */
8733
8734 static tree
8735 fold_mult_zconjz (location_t loc, tree type, tree expr)
8736 {
8737 tree itype = TREE_TYPE (type);
8738 tree rpart, ipart, tem;
8739
8740 if (TREE_CODE (expr) == COMPLEX_EXPR)
8741 {
8742 rpart = TREE_OPERAND (expr, 0);
8743 ipart = TREE_OPERAND (expr, 1);
8744 }
8745 else if (TREE_CODE (expr) == COMPLEX_CST)
8746 {
8747 rpart = TREE_REALPART (expr);
8748 ipart = TREE_IMAGPART (expr);
8749 }
8750 else
8751 {
8752 expr = save_expr (expr);
8753 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8754 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8755 }
8756
8757 rpart = save_expr (rpart);
8758 ipart = save_expr (ipart);
8759 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8760 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8761 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8762 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8763 build_zero_cst (itype));
8764 }
8765
8766
8767 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8768 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8769 true if successful. */
8770
8771 static bool
8772 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8773 {
8774 unsigned int i;
8775
8776 if (TREE_CODE (arg) == VECTOR_CST)
8777 {
8778 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8779 elts[i] = VECTOR_CST_ELT (arg, i);
8780 }
8781 else if (TREE_CODE (arg) == CONSTRUCTOR)
8782 {
8783 constructor_elt *elt;
8784
8785 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8786 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8787 return false;
8788 else
8789 elts[i] = elt->value;
8790 }
8791 else
8792 return false;
8793 for (; i < nelts; i++)
8794 elts[i]
8795 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8796 return true;
8797 }
8798
8799 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8800 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8801 NULL_TREE otherwise. */
8802
8803 static tree
8804 fold_vec_perm (tree type, tree arg0, tree arg1, vec_perm_indices sel)
8805 {
8806 unsigned int i;
8807 bool need_ctor = false;
8808
8809 unsigned int nelts = sel.length ();
8810 gcc_assert (TYPE_VECTOR_SUBPARTS (type) == nelts
8811 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8812 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8813 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8814 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8815 return NULL_TREE;
8816
8817 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8818 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8819 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8820 return NULL_TREE;
8821
8822 tree_vector_builder out_elts (type, nelts, 1);
8823 for (i = 0; i < nelts; i++)
8824 {
8825 if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
8826 need_ctor = true;
8827 out_elts.quick_push (unshare_expr (in_elts[sel[i]]));
8828 }
8829
8830 if (need_ctor)
8831 {
8832 vec<constructor_elt, va_gc> *v;
8833 vec_alloc (v, nelts);
8834 for (i = 0; i < nelts; i++)
8835 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8836 return build_constructor (type, v);
8837 }
8838 else
8839 return out_elts.build ();
8840 }
8841
8842 /* Try to fold a pointer difference of type TYPE two address expressions of
8843 array references AREF0 and AREF1 using location LOC. Return a
8844 simplified expression for the difference or NULL_TREE. */
8845
8846 static tree
8847 fold_addr_of_array_ref_difference (location_t loc, tree type,
8848 tree aref0, tree aref1,
8849 bool use_pointer_diff)
8850 {
8851 tree base0 = TREE_OPERAND (aref0, 0);
8852 tree base1 = TREE_OPERAND (aref1, 0);
8853 tree base_offset = build_int_cst (type, 0);
8854
8855 /* If the bases are array references as well, recurse. If the bases
8856 are pointer indirections compute the difference of the pointers.
8857 If the bases are equal, we are set. */
8858 if ((TREE_CODE (base0) == ARRAY_REF
8859 && TREE_CODE (base1) == ARRAY_REF
8860 && (base_offset
8861 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
8862 use_pointer_diff)))
8863 || (INDIRECT_REF_P (base0)
8864 && INDIRECT_REF_P (base1)
8865 && (base_offset
8866 = use_pointer_diff
8867 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
8868 TREE_OPERAND (base0, 0),
8869 TREE_OPERAND (base1, 0))
8870 : fold_binary_loc (loc, MINUS_EXPR, type,
8871 fold_convert (type,
8872 TREE_OPERAND (base0, 0)),
8873 fold_convert (type,
8874 TREE_OPERAND (base1, 0)))))
8875 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8876 {
8877 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8878 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8879 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8880 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
8881 return fold_build2_loc (loc, PLUS_EXPR, type,
8882 base_offset,
8883 fold_build2_loc (loc, MULT_EXPR, type,
8884 diff, esz));
8885 }
8886 return NULL_TREE;
8887 }
8888
8889 /* If the real or vector real constant CST of type TYPE has an exact
8890 inverse, return it, else return NULL. */
8891
8892 tree
8893 exact_inverse (tree type, tree cst)
8894 {
8895 REAL_VALUE_TYPE r;
8896 tree unit_type;
8897 machine_mode mode;
8898
8899 switch (TREE_CODE (cst))
8900 {
8901 case REAL_CST:
8902 r = TREE_REAL_CST (cst);
8903
8904 if (exact_real_inverse (TYPE_MODE (type), &r))
8905 return build_real (type, r);
8906
8907 return NULL_TREE;
8908
8909 case VECTOR_CST:
8910 {
8911 unit_type = TREE_TYPE (type);
8912 mode = TYPE_MODE (unit_type);
8913
8914 tree_vector_builder elts;
8915 if (!elts.new_unary_operation (type, cst, false))
8916 return NULL_TREE;
8917 unsigned int count = elts.encoded_nelts ();
8918 for (unsigned int i = 0; i < count; ++i)
8919 {
8920 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8921 if (!exact_real_inverse (mode, &r))
8922 return NULL_TREE;
8923 elts.quick_push (build_real (unit_type, r));
8924 }
8925
8926 return elts.build ();
8927 }
8928
8929 default:
8930 return NULL_TREE;
8931 }
8932 }
8933
8934 /* Mask out the tz least significant bits of X of type TYPE where
8935 tz is the number of trailing zeroes in Y. */
8936 static wide_int
8937 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8938 {
8939 int tz = wi::ctz (y);
8940 if (tz > 0)
8941 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8942 return x;
8943 }
8944
8945 /* Return true when T is an address and is known to be nonzero.
8946 For floating point we further ensure that T is not denormal.
8947 Similar logic is present in nonzero_address in rtlanal.h.
8948
8949 If the return value is based on the assumption that signed overflow
8950 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8951 change *STRICT_OVERFLOW_P. */
8952
8953 static bool
8954 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8955 {
8956 tree type = TREE_TYPE (t);
8957 enum tree_code code;
8958
8959 /* Doing something useful for floating point would need more work. */
8960 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8961 return false;
8962
8963 code = TREE_CODE (t);
8964 switch (TREE_CODE_CLASS (code))
8965 {
8966 case tcc_unary:
8967 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8968 strict_overflow_p);
8969 case tcc_binary:
8970 case tcc_comparison:
8971 return tree_binary_nonzero_warnv_p (code, type,
8972 TREE_OPERAND (t, 0),
8973 TREE_OPERAND (t, 1),
8974 strict_overflow_p);
8975 case tcc_constant:
8976 case tcc_declaration:
8977 case tcc_reference:
8978 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8979
8980 default:
8981 break;
8982 }
8983
8984 switch (code)
8985 {
8986 case TRUTH_NOT_EXPR:
8987 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8988 strict_overflow_p);
8989
8990 case TRUTH_AND_EXPR:
8991 case TRUTH_OR_EXPR:
8992 case TRUTH_XOR_EXPR:
8993 return tree_binary_nonzero_warnv_p (code, type,
8994 TREE_OPERAND (t, 0),
8995 TREE_OPERAND (t, 1),
8996 strict_overflow_p);
8997
8998 case COND_EXPR:
8999 case CONSTRUCTOR:
9000 case OBJ_TYPE_REF:
9001 case ASSERT_EXPR:
9002 case ADDR_EXPR:
9003 case WITH_SIZE_EXPR:
9004 case SSA_NAME:
9005 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9006
9007 case COMPOUND_EXPR:
9008 case MODIFY_EXPR:
9009 case BIND_EXPR:
9010 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9011 strict_overflow_p);
9012
9013 case SAVE_EXPR:
9014 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9015 strict_overflow_p);
9016
9017 case CALL_EXPR:
9018 {
9019 tree fndecl = get_callee_fndecl (t);
9020 if (!fndecl) return false;
9021 if (flag_delete_null_pointer_checks && !flag_check_new
9022 && DECL_IS_OPERATOR_NEW (fndecl)
9023 && !TREE_NOTHROW (fndecl))
9024 return true;
9025 if (flag_delete_null_pointer_checks
9026 && lookup_attribute ("returns_nonnull",
9027 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9028 return true;
9029 return alloca_call_p (t);
9030 }
9031
9032 default:
9033 break;
9034 }
9035 return false;
9036 }
9037
9038 /* Return true when T is an address and is known to be nonzero.
9039 Handle warnings about undefined signed overflow. */
9040
9041 bool
9042 tree_expr_nonzero_p (tree t)
9043 {
9044 bool ret, strict_overflow_p;
9045
9046 strict_overflow_p = false;
9047 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9048 if (strict_overflow_p)
9049 fold_overflow_warning (("assuming signed overflow does not occur when "
9050 "determining that expression is always "
9051 "non-zero"),
9052 WARN_STRICT_OVERFLOW_MISC);
9053 return ret;
9054 }
9055
9056 /* Return true if T is known not to be equal to an integer W. */
9057
9058 bool
9059 expr_not_equal_to (tree t, const wide_int &w)
9060 {
9061 wide_int min, max, nz;
9062 value_range_type rtype;
9063 switch (TREE_CODE (t))
9064 {
9065 case INTEGER_CST:
9066 return wi::to_wide (t) != w;
9067
9068 case SSA_NAME:
9069 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9070 return false;
9071 rtype = get_range_info (t, &min, &max);
9072 if (rtype == VR_RANGE)
9073 {
9074 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9075 return true;
9076 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9077 return true;
9078 }
9079 else if (rtype == VR_ANTI_RANGE
9080 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9081 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9082 return true;
9083 /* If T has some known zero bits and W has any of those bits set,
9084 then T is known not to be equal to W. */
9085 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9086 TYPE_PRECISION (TREE_TYPE (t))), 0))
9087 return true;
9088 return false;
9089
9090 default:
9091 return false;
9092 }
9093 }
9094
9095 /* Fold a binary expression of code CODE and type TYPE with operands
9096 OP0 and OP1. LOC is the location of the resulting expression.
9097 Return the folded expression if folding is successful. Otherwise,
9098 return NULL_TREE. */
9099
9100 tree
9101 fold_binary_loc (location_t loc,
9102 enum tree_code code, tree type, tree op0, tree op1)
9103 {
9104 enum tree_code_class kind = TREE_CODE_CLASS (code);
9105 tree arg0, arg1, tem;
9106 tree t1 = NULL_TREE;
9107 bool strict_overflow_p;
9108 unsigned int prec;
9109
9110 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9111 && TREE_CODE_LENGTH (code) == 2
9112 && op0 != NULL_TREE
9113 && op1 != NULL_TREE);
9114
9115 arg0 = op0;
9116 arg1 = op1;
9117
9118 /* Strip any conversions that don't change the mode. This is
9119 safe for every expression, except for a comparison expression
9120 because its signedness is derived from its operands. So, in
9121 the latter case, only strip conversions that don't change the
9122 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9123 preserved.
9124
9125 Note that this is done as an internal manipulation within the
9126 constant folder, in order to find the simplest representation
9127 of the arguments so that their form can be studied. In any
9128 cases, the appropriate type conversions should be put back in
9129 the tree that will get out of the constant folder. */
9130
9131 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9132 {
9133 STRIP_SIGN_NOPS (arg0);
9134 STRIP_SIGN_NOPS (arg1);
9135 }
9136 else
9137 {
9138 STRIP_NOPS (arg0);
9139 STRIP_NOPS (arg1);
9140 }
9141
9142 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9143 constant but we can't do arithmetic on them. */
9144 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9145 {
9146 tem = const_binop (code, type, arg0, arg1);
9147 if (tem != NULL_TREE)
9148 {
9149 if (TREE_TYPE (tem) != type)
9150 tem = fold_convert_loc (loc, type, tem);
9151 return tem;
9152 }
9153 }
9154
9155 /* If this is a commutative operation, and ARG0 is a constant, move it
9156 to ARG1 to reduce the number of tests below. */
9157 if (commutative_tree_code (code)
9158 && tree_swap_operands_p (arg0, arg1))
9159 return fold_build2_loc (loc, code, type, op1, op0);
9160
9161 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9162 to ARG1 to reduce the number of tests below. */
9163 if (kind == tcc_comparison
9164 && tree_swap_operands_p (arg0, arg1))
9165 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9166
9167 tem = generic_simplify (loc, code, type, op0, op1);
9168 if (tem)
9169 return tem;
9170
9171 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9172
9173 First check for cases where an arithmetic operation is applied to a
9174 compound, conditional, or comparison operation. Push the arithmetic
9175 operation inside the compound or conditional to see if any folding
9176 can then be done. Convert comparison to conditional for this purpose.
9177 The also optimizes non-constant cases that used to be done in
9178 expand_expr.
9179
9180 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9181 one of the operands is a comparison and the other is a comparison, a
9182 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9183 code below would make the expression more complex. Change it to a
9184 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9185 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9186
9187 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9188 || code == EQ_EXPR || code == NE_EXPR)
9189 && TREE_CODE (type) != VECTOR_TYPE
9190 && ((truth_value_p (TREE_CODE (arg0))
9191 && (truth_value_p (TREE_CODE (arg1))
9192 || (TREE_CODE (arg1) == BIT_AND_EXPR
9193 && integer_onep (TREE_OPERAND (arg1, 1)))))
9194 || (truth_value_p (TREE_CODE (arg1))
9195 && (truth_value_p (TREE_CODE (arg0))
9196 || (TREE_CODE (arg0) == BIT_AND_EXPR
9197 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9198 {
9199 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9200 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9201 : TRUTH_XOR_EXPR,
9202 boolean_type_node,
9203 fold_convert_loc (loc, boolean_type_node, arg0),
9204 fold_convert_loc (loc, boolean_type_node, arg1));
9205
9206 if (code == EQ_EXPR)
9207 tem = invert_truthvalue_loc (loc, tem);
9208
9209 return fold_convert_loc (loc, type, tem);
9210 }
9211
9212 if (TREE_CODE_CLASS (code) == tcc_binary
9213 || TREE_CODE_CLASS (code) == tcc_comparison)
9214 {
9215 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9216 {
9217 tem = fold_build2_loc (loc, code, type,
9218 fold_convert_loc (loc, TREE_TYPE (op0),
9219 TREE_OPERAND (arg0, 1)), op1);
9220 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9221 tem);
9222 }
9223 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9224 {
9225 tem = fold_build2_loc (loc, code, type, op0,
9226 fold_convert_loc (loc, TREE_TYPE (op1),
9227 TREE_OPERAND (arg1, 1)));
9228 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9229 tem);
9230 }
9231
9232 if (TREE_CODE (arg0) == COND_EXPR
9233 || TREE_CODE (arg0) == VEC_COND_EXPR
9234 || COMPARISON_CLASS_P (arg0))
9235 {
9236 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9237 arg0, arg1,
9238 /*cond_first_p=*/1);
9239 if (tem != NULL_TREE)
9240 return tem;
9241 }
9242
9243 if (TREE_CODE (arg1) == COND_EXPR
9244 || TREE_CODE (arg1) == VEC_COND_EXPR
9245 || COMPARISON_CLASS_P (arg1))
9246 {
9247 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9248 arg1, arg0,
9249 /*cond_first_p=*/0);
9250 if (tem != NULL_TREE)
9251 return tem;
9252 }
9253 }
9254
9255 switch (code)
9256 {
9257 case MEM_REF:
9258 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9259 if (TREE_CODE (arg0) == ADDR_EXPR
9260 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9261 {
9262 tree iref = TREE_OPERAND (arg0, 0);
9263 return fold_build2 (MEM_REF, type,
9264 TREE_OPERAND (iref, 0),
9265 int_const_binop (PLUS_EXPR, arg1,
9266 TREE_OPERAND (iref, 1)));
9267 }
9268
9269 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9270 if (TREE_CODE (arg0) == ADDR_EXPR
9271 && handled_component_p (TREE_OPERAND (arg0, 0)))
9272 {
9273 tree base;
9274 HOST_WIDE_INT coffset;
9275 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9276 &coffset);
9277 if (!base)
9278 return NULL_TREE;
9279 return fold_build2 (MEM_REF, type,
9280 build_fold_addr_expr (base),
9281 int_const_binop (PLUS_EXPR, arg1,
9282 size_int (coffset)));
9283 }
9284
9285 return NULL_TREE;
9286
9287 case POINTER_PLUS_EXPR:
9288 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9289 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9290 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9291 return fold_convert_loc (loc, type,
9292 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9293 fold_convert_loc (loc, sizetype,
9294 arg1),
9295 fold_convert_loc (loc, sizetype,
9296 arg0)));
9297
9298 return NULL_TREE;
9299
9300 case PLUS_EXPR:
9301 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9302 {
9303 /* X + (X / CST) * -CST is X % CST. */
9304 if (TREE_CODE (arg1) == MULT_EXPR
9305 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9306 && operand_equal_p (arg0,
9307 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9308 {
9309 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9310 tree cst1 = TREE_OPERAND (arg1, 1);
9311 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9312 cst1, cst0);
9313 if (sum && integer_zerop (sum))
9314 return fold_convert_loc (loc, type,
9315 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9316 TREE_TYPE (arg0), arg0,
9317 cst0));
9318 }
9319 }
9320
9321 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9322 one. Make sure the type is not saturating and has the signedness of
9323 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9324 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9325 if ((TREE_CODE (arg0) == MULT_EXPR
9326 || TREE_CODE (arg1) == MULT_EXPR)
9327 && !TYPE_SATURATING (type)
9328 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9329 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9330 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9331 {
9332 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9333 if (tem)
9334 return tem;
9335 }
9336
9337 if (! FLOAT_TYPE_P (type))
9338 {
9339 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9340 (plus (plus (mult) (mult)) (foo)) so that we can
9341 take advantage of the factoring cases below. */
9342 if (ANY_INTEGRAL_TYPE_P (type)
9343 && TYPE_OVERFLOW_WRAPS (type)
9344 && (((TREE_CODE (arg0) == PLUS_EXPR
9345 || TREE_CODE (arg0) == MINUS_EXPR)
9346 && TREE_CODE (arg1) == MULT_EXPR)
9347 || ((TREE_CODE (arg1) == PLUS_EXPR
9348 || TREE_CODE (arg1) == MINUS_EXPR)
9349 && TREE_CODE (arg0) == MULT_EXPR)))
9350 {
9351 tree parg0, parg1, parg, marg;
9352 enum tree_code pcode;
9353
9354 if (TREE_CODE (arg1) == MULT_EXPR)
9355 parg = arg0, marg = arg1;
9356 else
9357 parg = arg1, marg = arg0;
9358 pcode = TREE_CODE (parg);
9359 parg0 = TREE_OPERAND (parg, 0);
9360 parg1 = TREE_OPERAND (parg, 1);
9361 STRIP_NOPS (parg0);
9362 STRIP_NOPS (parg1);
9363
9364 if (TREE_CODE (parg0) == MULT_EXPR
9365 && TREE_CODE (parg1) != MULT_EXPR)
9366 return fold_build2_loc (loc, pcode, type,
9367 fold_build2_loc (loc, PLUS_EXPR, type,
9368 fold_convert_loc (loc, type,
9369 parg0),
9370 fold_convert_loc (loc, type,
9371 marg)),
9372 fold_convert_loc (loc, type, parg1));
9373 if (TREE_CODE (parg0) != MULT_EXPR
9374 && TREE_CODE (parg1) == MULT_EXPR)
9375 return
9376 fold_build2_loc (loc, PLUS_EXPR, type,
9377 fold_convert_loc (loc, type, parg0),
9378 fold_build2_loc (loc, pcode, type,
9379 fold_convert_loc (loc, type, marg),
9380 fold_convert_loc (loc, type,
9381 parg1)));
9382 }
9383 }
9384 else
9385 {
9386 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9387 to __complex__ ( x, y ). This is not the same for SNaNs or
9388 if signed zeros are involved. */
9389 if (!HONOR_SNANS (element_mode (arg0))
9390 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9391 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9392 {
9393 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9394 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9395 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9396 bool arg0rz = false, arg0iz = false;
9397 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9398 || (arg0i && (arg0iz = real_zerop (arg0i))))
9399 {
9400 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9401 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9402 if (arg0rz && arg1i && real_zerop (arg1i))
9403 {
9404 tree rp = arg1r ? arg1r
9405 : build1 (REALPART_EXPR, rtype, arg1);
9406 tree ip = arg0i ? arg0i
9407 : build1 (IMAGPART_EXPR, rtype, arg0);
9408 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9409 }
9410 else if (arg0iz && arg1r && real_zerop (arg1r))
9411 {
9412 tree rp = arg0r ? arg0r
9413 : build1 (REALPART_EXPR, rtype, arg0);
9414 tree ip = arg1i ? arg1i
9415 : build1 (IMAGPART_EXPR, rtype, arg1);
9416 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9417 }
9418 }
9419 }
9420
9421 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9422 We associate floats only if the user has specified
9423 -fassociative-math. */
9424 if (flag_associative_math
9425 && TREE_CODE (arg1) == PLUS_EXPR
9426 && TREE_CODE (arg0) != MULT_EXPR)
9427 {
9428 tree tree10 = TREE_OPERAND (arg1, 0);
9429 tree tree11 = TREE_OPERAND (arg1, 1);
9430 if (TREE_CODE (tree11) == MULT_EXPR
9431 && TREE_CODE (tree10) == MULT_EXPR)
9432 {
9433 tree tree0;
9434 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9435 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9436 }
9437 }
9438 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9439 We associate floats only if the user has specified
9440 -fassociative-math. */
9441 if (flag_associative_math
9442 && TREE_CODE (arg0) == PLUS_EXPR
9443 && TREE_CODE (arg1) != MULT_EXPR)
9444 {
9445 tree tree00 = TREE_OPERAND (arg0, 0);
9446 tree tree01 = TREE_OPERAND (arg0, 1);
9447 if (TREE_CODE (tree01) == MULT_EXPR
9448 && TREE_CODE (tree00) == MULT_EXPR)
9449 {
9450 tree tree0;
9451 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9452 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9453 }
9454 }
9455 }
9456
9457 bit_rotate:
9458 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9459 is a rotate of A by C1 bits. */
9460 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9461 is a rotate of A by B bits.
9462 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9463 though in this case CODE must be | and not + or ^, otherwise
9464 it doesn't return A when B is 0. */
9465 {
9466 enum tree_code code0, code1;
9467 tree rtype;
9468 code0 = TREE_CODE (arg0);
9469 code1 = TREE_CODE (arg1);
9470 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9471 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9472 && operand_equal_p (TREE_OPERAND (arg0, 0),
9473 TREE_OPERAND (arg1, 0), 0)
9474 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9475 TYPE_UNSIGNED (rtype))
9476 /* Only create rotates in complete modes. Other cases are not
9477 expanded properly. */
9478 && (element_precision (rtype)
9479 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9480 {
9481 tree tree01, tree11;
9482 tree orig_tree01, orig_tree11;
9483 enum tree_code code01, code11;
9484
9485 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9486 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9487 STRIP_NOPS (tree01);
9488 STRIP_NOPS (tree11);
9489 code01 = TREE_CODE (tree01);
9490 code11 = TREE_CODE (tree11);
9491 if (code11 != MINUS_EXPR
9492 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9493 {
9494 std::swap (code0, code1);
9495 std::swap (code01, code11);
9496 std::swap (tree01, tree11);
9497 std::swap (orig_tree01, orig_tree11);
9498 }
9499 if (code01 == INTEGER_CST
9500 && code11 == INTEGER_CST
9501 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9502 == element_precision (rtype)))
9503 {
9504 tem = build2_loc (loc, LROTATE_EXPR,
9505 rtype, TREE_OPERAND (arg0, 0),
9506 code0 == LSHIFT_EXPR
9507 ? orig_tree01 : orig_tree11);
9508 return fold_convert_loc (loc, type, tem);
9509 }
9510 else if (code11 == MINUS_EXPR)
9511 {
9512 tree tree110, tree111;
9513 tree110 = TREE_OPERAND (tree11, 0);
9514 tree111 = TREE_OPERAND (tree11, 1);
9515 STRIP_NOPS (tree110);
9516 STRIP_NOPS (tree111);
9517 if (TREE_CODE (tree110) == INTEGER_CST
9518 && 0 == compare_tree_int (tree110,
9519 element_precision (rtype))
9520 && operand_equal_p (tree01, tree111, 0))
9521 {
9522 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9523 ? LROTATE_EXPR : RROTATE_EXPR),
9524 rtype, TREE_OPERAND (arg0, 0),
9525 orig_tree01);
9526 return fold_convert_loc (loc, type, tem);
9527 }
9528 }
9529 else if (code == BIT_IOR_EXPR
9530 && code11 == BIT_AND_EXPR
9531 && pow2p_hwi (element_precision (rtype)))
9532 {
9533 tree tree110, tree111;
9534 tree110 = TREE_OPERAND (tree11, 0);
9535 tree111 = TREE_OPERAND (tree11, 1);
9536 STRIP_NOPS (tree110);
9537 STRIP_NOPS (tree111);
9538 if (TREE_CODE (tree110) == NEGATE_EXPR
9539 && TREE_CODE (tree111) == INTEGER_CST
9540 && 0 == compare_tree_int (tree111,
9541 element_precision (rtype) - 1)
9542 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9543 {
9544 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9545 ? LROTATE_EXPR : RROTATE_EXPR),
9546 rtype, TREE_OPERAND (arg0, 0),
9547 orig_tree01);
9548 return fold_convert_loc (loc, type, tem);
9549 }
9550 }
9551 }
9552 }
9553
9554 associate:
9555 /* In most languages, can't associate operations on floats through
9556 parentheses. Rather than remember where the parentheses were, we
9557 don't associate floats at all, unless the user has specified
9558 -fassociative-math.
9559 And, we need to make sure type is not saturating. */
9560
9561 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9562 && !TYPE_SATURATING (type))
9563 {
9564 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9565 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9566 tree atype = type;
9567 bool ok = true;
9568
9569 /* Split both trees into variables, constants, and literals. Then
9570 associate each group together, the constants with literals,
9571 then the result with variables. This increases the chances of
9572 literals being recombined later and of generating relocatable
9573 expressions for the sum of a constant and literal. */
9574 var0 = split_tree (arg0, type, code,
9575 &minus_var0, &con0, &minus_con0,
9576 &lit0, &minus_lit0, 0);
9577 var1 = split_tree (arg1, type, code,
9578 &minus_var1, &con1, &minus_con1,
9579 &lit1, &minus_lit1, code == MINUS_EXPR);
9580
9581 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9582 if (code == MINUS_EXPR)
9583 code = PLUS_EXPR;
9584
9585 /* With undefined overflow prefer doing association in a type
9586 which wraps on overflow, if that is one of the operand types. */
9587 if (POINTER_TYPE_P (type)
9588 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9589 {
9590 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9591 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9592 atype = TREE_TYPE (arg0);
9593 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9594 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9595 atype = TREE_TYPE (arg1);
9596 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9597 }
9598
9599 /* With undefined overflow we can only associate constants with one
9600 variable, and constants whose association doesn't overflow. */
9601 if (POINTER_TYPE_P (atype)
9602 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9603 {
9604 if ((var0 && var1) || (minus_var0 && minus_var1))
9605 {
9606 /* ??? If split_tree would handle NEGATE_EXPR we could
9607 simply reject these cases and the allowed cases would
9608 be the var0/minus_var1 ones. */
9609 tree tmp0 = var0 ? var0 : minus_var0;
9610 tree tmp1 = var1 ? var1 : minus_var1;
9611 bool one_neg = false;
9612
9613 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9614 {
9615 tmp0 = TREE_OPERAND (tmp0, 0);
9616 one_neg = !one_neg;
9617 }
9618 if (CONVERT_EXPR_P (tmp0)
9619 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9620 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9621 <= TYPE_PRECISION (atype)))
9622 tmp0 = TREE_OPERAND (tmp0, 0);
9623 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9624 {
9625 tmp1 = TREE_OPERAND (tmp1, 0);
9626 one_neg = !one_neg;
9627 }
9628 if (CONVERT_EXPR_P (tmp1)
9629 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9630 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9631 <= TYPE_PRECISION (atype)))
9632 tmp1 = TREE_OPERAND (tmp1, 0);
9633 /* The only case we can still associate with two variables
9634 is if they cancel out. */
9635 if (!one_neg
9636 || !operand_equal_p (tmp0, tmp1, 0))
9637 ok = false;
9638 }
9639 else if ((var0 && minus_var1
9640 && ! operand_equal_p (var0, minus_var1, 0))
9641 || (minus_var0 && var1
9642 && ! operand_equal_p (minus_var0, var1, 0)))
9643 ok = false;
9644 }
9645
9646 /* Only do something if we found more than two objects. Otherwise,
9647 nothing has changed and we risk infinite recursion. */
9648 if (ok
9649 && (2 < ((var0 != 0) + (var1 != 0)
9650 + (minus_var0 != 0) + (minus_var1 != 0)
9651 + (con0 != 0) + (con1 != 0)
9652 + (minus_con0 != 0) + (minus_con1 != 0)
9653 + (lit0 != 0) + (lit1 != 0)
9654 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9655 {
9656 var0 = associate_trees (loc, var0, var1, code, atype);
9657 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9658 code, atype);
9659 con0 = associate_trees (loc, con0, con1, code, atype);
9660 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9661 code, atype);
9662 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9663 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9664 code, atype);
9665
9666 if (minus_var0 && var0)
9667 {
9668 var0 = associate_trees (loc, var0, minus_var0,
9669 MINUS_EXPR, atype);
9670 minus_var0 = 0;
9671 }
9672 if (minus_con0 && con0)
9673 {
9674 con0 = associate_trees (loc, con0, minus_con0,
9675 MINUS_EXPR, atype);
9676 minus_con0 = 0;
9677 }
9678
9679 /* Preserve the MINUS_EXPR if the negative part of the literal is
9680 greater than the positive part. Otherwise, the multiplicative
9681 folding code (i.e extract_muldiv) may be fooled in case
9682 unsigned constants are subtracted, like in the following
9683 example: ((X*2 + 4) - 8U)/2. */
9684 if (minus_lit0 && lit0)
9685 {
9686 if (TREE_CODE (lit0) == INTEGER_CST
9687 && TREE_CODE (minus_lit0) == INTEGER_CST
9688 && tree_int_cst_lt (lit0, minus_lit0)
9689 /* But avoid ending up with only negated parts. */
9690 && (var0 || con0))
9691 {
9692 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9693 MINUS_EXPR, atype);
9694 lit0 = 0;
9695 }
9696 else
9697 {
9698 lit0 = associate_trees (loc, lit0, minus_lit0,
9699 MINUS_EXPR, atype);
9700 minus_lit0 = 0;
9701 }
9702 }
9703
9704 /* Don't introduce overflows through reassociation. */
9705 if ((lit0 && TREE_OVERFLOW_P (lit0))
9706 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9707 return NULL_TREE;
9708
9709 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9710 con0 = associate_trees (loc, con0, lit0, code, atype);
9711 lit0 = 0;
9712 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9713 code, atype);
9714 minus_lit0 = 0;
9715
9716 /* Eliminate minus_con0. */
9717 if (minus_con0)
9718 {
9719 if (con0)
9720 con0 = associate_trees (loc, con0, minus_con0,
9721 MINUS_EXPR, atype);
9722 else if (var0)
9723 var0 = associate_trees (loc, var0, minus_con0,
9724 MINUS_EXPR, atype);
9725 else
9726 gcc_unreachable ();
9727 minus_con0 = 0;
9728 }
9729
9730 /* Eliminate minus_var0. */
9731 if (minus_var0)
9732 {
9733 if (con0)
9734 con0 = associate_trees (loc, con0, minus_var0,
9735 MINUS_EXPR, atype);
9736 else
9737 gcc_unreachable ();
9738 minus_var0 = 0;
9739 }
9740
9741 return
9742 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9743 code, atype));
9744 }
9745 }
9746
9747 return NULL_TREE;
9748
9749 case POINTER_DIFF_EXPR:
9750 case MINUS_EXPR:
9751 /* Fold &a[i] - &a[j] to i-j. */
9752 if (TREE_CODE (arg0) == ADDR_EXPR
9753 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9754 && TREE_CODE (arg1) == ADDR_EXPR
9755 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9756 {
9757 tree tem = fold_addr_of_array_ref_difference (loc, type,
9758 TREE_OPERAND (arg0, 0),
9759 TREE_OPERAND (arg1, 0),
9760 code
9761 == POINTER_DIFF_EXPR);
9762 if (tem)
9763 return tem;
9764 }
9765
9766 /* Further transformations are not for pointers. */
9767 if (code == POINTER_DIFF_EXPR)
9768 return NULL_TREE;
9769
9770 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9771 if (TREE_CODE (arg0) == NEGATE_EXPR
9772 && negate_expr_p (op1))
9773 return fold_build2_loc (loc, MINUS_EXPR, type,
9774 negate_expr (op1),
9775 fold_convert_loc (loc, type,
9776 TREE_OPERAND (arg0, 0)));
9777
9778 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9779 __complex__ ( x, -y ). This is not the same for SNaNs or if
9780 signed zeros are involved. */
9781 if (!HONOR_SNANS (element_mode (arg0))
9782 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9783 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9784 {
9785 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9786 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9787 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9788 bool arg0rz = false, arg0iz = false;
9789 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9790 || (arg0i && (arg0iz = real_zerop (arg0i))))
9791 {
9792 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9793 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9794 if (arg0rz && arg1i && real_zerop (arg1i))
9795 {
9796 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9797 arg1r ? arg1r
9798 : build1 (REALPART_EXPR, rtype, arg1));
9799 tree ip = arg0i ? arg0i
9800 : build1 (IMAGPART_EXPR, rtype, arg0);
9801 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9802 }
9803 else if (arg0iz && arg1r && real_zerop (arg1r))
9804 {
9805 tree rp = arg0r ? arg0r
9806 : build1 (REALPART_EXPR, rtype, arg0);
9807 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9808 arg1i ? arg1i
9809 : build1 (IMAGPART_EXPR, rtype, arg1));
9810 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9811 }
9812 }
9813 }
9814
9815 /* A - B -> A + (-B) if B is easily negatable. */
9816 if (negate_expr_p (op1)
9817 && ! TYPE_OVERFLOW_SANITIZED (type)
9818 && ((FLOAT_TYPE_P (type)
9819 /* Avoid this transformation if B is a positive REAL_CST. */
9820 && (TREE_CODE (op1) != REAL_CST
9821 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9822 || INTEGRAL_TYPE_P (type)))
9823 return fold_build2_loc (loc, PLUS_EXPR, type,
9824 fold_convert_loc (loc, type, arg0),
9825 negate_expr (op1));
9826
9827 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9828 one. Make sure the type is not saturating and has the signedness of
9829 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9830 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9831 if ((TREE_CODE (arg0) == MULT_EXPR
9832 || TREE_CODE (arg1) == MULT_EXPR)
9833 && !TYPE_SATURATING (type)
9834 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9835 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9836 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9837 {
9838 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9839 if (tem)
9840 return tem;
9841 }
9842
9843 goto associate;
9844
9845 case MULT_EXPR:
9846 if (! FLOAT_TYPE_P (type))
9847 {
9848 /* Transform x * -C into -x * C if x is easily negatable. */
9849 if (TREE_CODE (op1) == INTEGER_CST
9850 && tree_int_cst_sgn (op1) == -1
9851 && negate_expr_p (op0)
9852 && negate_expr_p (op1)
9853 && (tem = negate_expr (op1)) != op1
9854 && ! TREE_OVERFLOW (tem))
9855 return fold_build2_loc (loc, MULT_EXPR, type,
9856 fold_convert_loc (loc, type,
9857 negate_expr (op0)), tem);
9858
9859 strict_overflow_p = false;
9860 if (TREE_CODE (arg1) == INTEGER_CST
9861 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9862 &strict_overflow_p)))
9863 {
9864 if (strict_overflow_p)
9865 fold_overflow_warning (("assuming signed overflow does not "
9866 "occur when simplifying "
9867 "multiplication"),
9868 WARN_STRICT_OVERFLOW_MISC);
9869 return fold_convert_loc (loc, type, tem);
9870 }
9871
9872 /* Optimize z * conj(z) for integer complex numbers. */
9873 if (TREE_CODE (arg0) == CONJ_EXPR
9874 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9875 return fold_mult_zconjz (loc, type, arg1);
9876 if (TREE_CODE (arg1) == CONJ_EXPR
9877 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9878 return fold_mult_zconjz (loc, type, arg0);
9879 }
9880 else
9881 {
9882 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9883 This is not the same for NaNs or if signed zeros are
9884 involved. */
9885 if (!HONOR_NANS (arg0)
9886 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9887 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9888 && TREE_CODE (arg1) == COMPLEX_CST
9889 && real_zerop (TREE_REALPART (arg1)))
9890 {
9891 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9892 if (real_onep (TREE_IMAGPART (arg1)))
9893 return
9894 fold_build2_loc (loc, COMPLEX_EXPR, type,
9895 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9896 rtype, arg0)),
9897 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9898 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9899 return
9900 fold_build2_loc (loc, COMPLEX_EXPR, type,
9901 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9902 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9903 rtype, arg0)));
9904 }
9905
9906 /* Optimize z * conj(z) for floating point complex numbers.
9907 Guarded by flag_unsafe_math_optimizations as non-finite
9908 imaginary components don't produce scalar results. */
9909 if (flag_unsafe_math_optimizations
9910 && TREE_CODE (arg0) == CONJ_EXPR
9911 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9912 return fold_mult_zconjz (loc, type, arg1);
9913 if (flag_unsafe_math_optimizations
9914 && TREE_CODE (arg1) == CONJ_EXPR
9915 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9916 return fold_mult_zconjz (loc, type, arg0);
9917 }
9918 goto associate;
9919
9920 case BIT_IOR_EXPR:
9921 /* Canonicalize (X & C1) | C2. */
9922 if (TREE_CODE (arg0) == BIT_AND_EXPR
9923 && TREE_CODE (arg1) == INTEGER_CST
9924 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9925 {
9926 int width = TYPE_PRECISION (type), w;
9927 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
9928 wide_int c2 = wi::to_wide (arg1);
9929
9930 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9931 if ((c1 & c2) == c1)
9932 return omit_one_operand_loc (loc, type, arg1,
9933 TREE_OPERAND (arg0, 0));
9934
9935 wide_int msk = wi::mask (width, false,
9936 TYPE_PRECISION (TREE_TYPE (arg1)));
9937
9938 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9939 if (wi::bit_and_not (msk, c1 | c2) == 0)
9940 {
9941 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9942 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9943 }
9944
9945 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9946 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9947 mode which allows further optimizations. */
9948 c1 &= msk;
9949 c2 &= msk;
9950 wide_int c3 = wi::bit_and_not (c1, c2);
9951 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9952 {
9953 wide_int mask = wi::mask (w, false,
9954 TYPE_PRECISION (type));
9955 if (((c1 | c2) & mask) == mask
9956 && wi::bit_and_not (c1, mask) == 0)
9957 {
9958 c3 = mask;
9959 break;
9960 }
9961 }
9962
9963 if (c3 != c1)
9964 {
9965 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9966 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
9967 wide_int_to_tree (type, c3));
9968 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9969 }
9970 }
9971
9972 /* See if this can be simplified into a rotate first. If that
9973 is unsuccessful continue in the association code. */
9974 goto bit_rotate;
9975
9976 case BIT_XOR_EXPR:
9977 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9978 if (TREE_CODE (arg0) == BIT_AND_EXPR
9979 && INTEGRAL_TYPE_P (type)
9980 && integer_onep (TREE_OPERAND (arg0, 1))
9981 && integer_onep (arg1))
9982 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
9983 build_zero_cst (TREE_TYPE (arg0)));
9984
9985 /* See if this can be simplified into a rotate first. If that
9986 is unsuccessful continue in the association code. */
9987 goto bit_rotate;
9988
9989 case BIT_AND_EXPR:
9990 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9991 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9992 && INTEGRAL_TYPE_P (type)
9993 && integer_onep (TREE_OPERAND (arg0, 1))
9994 && integer_onep (arg1))
9995 {
9996 tree tem2;
9997 tem = TREE_OPERAND (arg0, 0);
9998 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
9999 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10000 tem, tem2);
10001 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10002 build_zero_cst (TREE_TYPE (tem)));
10003 }
10004 /* Fold ~X & 1 as (X & 1) == 0. */
10005 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10006 && INTEGRAL_TYPE_P (type)
10007 && integer_onep (arg1))
10008 {
10009 tree tem2;
10010 tem = TREE_OPERAND (arg0, 0);
10011 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10012 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10013 tem, tem2);
10014 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10015 build_zero_cst (TREE_TYPE (tem)));
10016 }
10017 /* Fold !X & 1 as X == 0. */
10018 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10019 && integer_onep (arg1))
10020 {
10021 tem = TREE_OPERAND (arg0, 0);
10022 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10023 build_zero_cst (TREE_TYPE (tem)));
10024 }
10025
10026 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10027 multiple of 1 << CST. */
10028 if (TREE_CODE (arg1) == INTEGER_CST)
10029 {
10030 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10031 wide_int ncst1 = -cst1;
10032 if ((cst1 & ncst1) == ncst1
10033 && multiple_of_p (type, arg0,
10034 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10035 return fold_convert_loc (loc, type, arg0);
10036 }
10037
10038 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10039 bits from CST2. */
10040 if (TREE_CODE (arg1) == INTEGER_CST
10041 && TREE_CODE (arg0) == MULT_EXPR
10042 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10043 {
10044 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10045 wide_int masked
10046 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10047
10048 if (masked == 0)
10049 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10050 arg0, arg1);
10051 else if (masked != warg1)
10052 {
10053 /* Avoid the transform if arg1 is a mask of some
10054 mode which allows further optimizations. */
10055 int pop = wi::popcount (warg1);
10056 if (!(pop >= BITS_PER_UNIT
10057 && pow2p_hwi (pop)
10058 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10059 return fold_build2_loc (loc, code, type, op0,
10060 wide_int_to_tree (type, masked));
10061 }
10062 }
10063
10064 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10065 ((A & N) + B) & M -> (A + B) & M
10066 Similarly if (N & M) == 0,
10067 ((A | N) + B) & M -> (A + B) & M
10068 and for - instead of + (or unary - instead of +)
10069 and/or ^ instead of |.
10070 If B is constant and (B & M) == 0, fold into A & M. */
10071 if (TREE_CODE (arg1) == INTEGER_CST)
10072 {
10073 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10074 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10075 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10076 && (TREE_CODE (arg0) == PLUS_EXPR
10077 || TREE_CODE (arg0) == MINUS_EXPR
10078 || TREE_CODE (arg0) == NEGATE_EXPR)
10079 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10080 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10081 {
10082 tree pmop[2];
10083 int which = 0;
10084 wide_int cst0;
10085
10086 /* Now we know that arg0 is (C + D) or (C - D) or
10087 -C and arg1 (M) is == (1LL << cst) - 1.
10088 Store C into PMOP[0] and D into PMOP[1]. */
10089 pmop[0] = TREE_OPERAND (arg0, 0);
10090 pmop[1] = NULL;
10091 if (TREE_CODE (arg0) != NEGATE_EXPR)
10092 {
10093 pmop[1] = TREE_OPERAND (arg0, 1);
10094 which = 1;
10095 }
10096
10097 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10098 which = -1;
10099
10100 for (; which >= 0; which--)
10101 switch (TREE_CODE (pmop[which]))
10102 {
10103 case BIT_AND_EXPR:
10104 case BIT_IOR_EXPR:
10105 case BIT_XOR_EXPR:
10106 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10107 != INTEGER_CST)
10108 break;
10109 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10110 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10111 {
10112 if (cst0 != cst1)
10113 break;
10114 }
10115 else if (cst0 != 0)
10116 break;
10117 /* If C or D is of the form (A & N) where
10118 (N & M) == M, or of the form (A | N) or
10119 (A ^ N) where (N & M) == 0, replace it with A. */
10120 pmop[which] = TREE_OPERAND (pmop[which], 0);
10121 break;
10122 case INTEGER_CST:
10123 /* If C or D is a N where (N & M) == 0, it can be
10124 omitted (assumed 0). */
10125 if ((TREE_CODE (arg0) == PLUS_EXPR
10126 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10127 && (cst1 & wi::to_wide (pmop[which])) == 0)
10128 pmop[which] = NULL;
10129 break;
10130 default:
10131 break;
10132 }
10133
10134 /* Only build anything new if we optimized one or both arguments
10135 above. */
10136 if (pmop[0] != TREE_OPERAND (arg0, 0)
10137 || (TREE_CODE (arg0) != NEGATE_EXPR
10138 && pmop[1] != TREE_OPERAND (arg0, 1)))
10139 {
10140 tree utype = TREE_TYPE (arg0);
10141 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10142 {
10143 /* Perform the operations in a type that has defined
10144 overflow behavior. */
10145 utype = unsigned_type_for (TREE_TYPE (arg0));
10146 if (pmop[0] != NULL)
10147 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10148 if (pmop[1] != NULL)
10149 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10150 }
10151
10152 if (TREE_CODE (arg0) == NEGATE_EXPR)
10153 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10154 else if (TREE_CODE (arg0) == PLUS_EXPR)
10155 {
10156 if (pmop[0] != NULL && pmop[1] != NULL)
10157 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10158 pmop[0], pmop[1]);
10159 else if (pmop[0] != NULL)
10160 tem = pmop[0];
10161 else if (pmop[1] != NULL)
10162 tem = pmop[1];
10163 else
10164 return build_int_cst (type, 0);
10165 }
10166 else if (pmop[0] == NULL)
10167 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10168 else
10169 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10170 pmop[0], pmop[1]);
10171 /* TEM is now the new binary +, - or unary - replacement. */
10172 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10173 fold_convert_loc (loc, utype, arg1));
10174 return fold_convert_loc (loc, type, tem);
10175 }
10176 }
10177 }
10178
10179 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10180 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10181 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10182 {
10183 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10184
10185 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10186 if (mask == -1)
10187 return
10188 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10189 }
10190
10191 goto associate;
10192
10193 case RDIV_EXPR:
10194 /* Don't touch a floating-point divide by zero unless the mode
10195 of the constant can represent infinity. */
10196 if (TREE_CODE (arg1) == REAL_CST
10197 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10198 && real_zerop (arg1))
10199 return NULL_TREE;
10200
10201 /* (-A) / (-B) -> A / B */
10202 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10203 return fold_build2_loc (loc, RDIV_EXPR, type,
10204 TREE_OPERAND (arg0, 0),
10205 negate_expr (arg1));
10206 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10207 return fold_build2_loc (loc, RDIV_EXPR, type,
10208 negate_expr (arg0),
10209 TREE_OPERAND (arg1, 0));
10210 return NULL_TREE;
10211
10212 case TRUNC_DIV_EXPR:
10213 /* Fall through */
10214
10215 case FLOOR_DIV_EXPR:
10216 /* Simplify A / (B << N) where A and B are positive and B is
10217 a power of 2, to A >> (N + log2(B)). */
10218 strict_overflow_p = false;
10219 if (TREE_CODE (arg1) == LSHIFT_EXPR
10220 && (TYPE_UNSIGNED (type)
10221 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10222 {
10223 tree sval = TREE_OPERAND (arg1, 0);
10224 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10225 {
10226 tree sh_cnt = TREE_OPERAND (arg1, 1);
10227 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10228 wi::exact_log2 (wi::to_wide (sval)));
10229
10230 if (strict_overflow_p)
10231 fold_overflow_warning (("assuming signed overflow does not "
10232 "occur when simplifying A / (B << N)"),
10233 WARN_STRICT_OVERFLOW_MISC);
10234
10235 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10236 sh_cnt, pow2);
10237 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10238 fold_convert_loc (loc, type, arg0), sh_cnt);
10239 }
10240 }
10241
10242 /* Fall through */
10243
10244 case ROUND_DIV_EXPR:
10245 case CEIL_DIV_EXPR:
10246 case EXACT_DIV_EXPR:
10247 if (integer_zerop (arg1))
10248 return NULL_TREE;
10249
10250 /* Convert -A / -B to A / B when the type is signed and overflow is
10251 undefined. */
10252 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10253 && TREE_CODE (op0) == NEGATE_EXPR
10254 && negate_expr_p (op1))
10255 {
10256 if (INTEGRAL_TYPE_P (type))
10257 fold_overflow_warning (("assuming signed overflow does not occur "
10258 "when distributing negation across "
10259 "division"),
10260 WARN_STRICT_OVERFLOW_MISC);
10261 return fold_build2_loc (loc, code, type,
10262 fold_convert_loc (loc, type,
10263 TREE_OPERAND (arg0, 0)),
10264 negate_expr (op1));
10265 }
10266 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10267 && TREE_CODE (arg1) == NEGATE_EXPR
10268 && negate_expr_p (op0))
10269 {
10270 if (INTEGRAL_TYPE_P (type))
10271 fold_overflow_warning (("assuming signed overflow does not occur "
10272 "when distributing negation across "
10273 "division"),
10274 WARN_STRICT_OVERFLOW_MISC);
10275 return fold_build2_loc (loc, code, type,
10276 negate_expr (op0),
10277 fold_convert_loc (loc, type,
10278 TREE_OPERAND (arg1, 0)));
10279 }
10280
10281 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10282 operation, EXACT_DIV_EXPR.
10283
10284 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10285 At one time others generated faster code, it's not clear if they do
10286 after the last round to changes to the DIV code in expmed.c. */
10287 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10288 && multiple_of_p (type, arg0, arg1))
10289 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10290 fold_convert (type, arg0),
10291 fold_convert (type, arg1));
10292
10293 strict_overflow_p = false;
10294 if (TREE_CODE (arg1) == INTEGER_CST
10295 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10296 &strict_overflow_p)))
10297 {
10298 if (strict_overflow_p)
10299 fold_overflow_warning (("assuming signed overflow does not occur "
10300 "when simplifying division"),
10301 WARN_STRICT_OVERFLOW_MISC);
10302 return fold_convert_loc (loc, type, tem);
10303 }
10304
10305 return NULL_TREE;
10306
10307 case CEIL_MOD_EXPR:
10308 case FLOOR_MOD_EXPR:
10309 case ROUND_MOD_EXPR:
10310 case TRUNC_MOD_EXPR:
10311 strict_overflow_p = false;
10312 if (TREE_CODE (arg1) == INTEGER_CST
10313 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10314 &strict_overflow_p)))
10315 {
10316 if (strict_overflow_p)
10317 fold_overflow_warning (("assuming signed overflow does not occur "
10318 "when simplifying modulus"),
10319 WARN_STRICT_OVERFLOW_MISC);
10320 return fold_convert_loc (loc, type, tem);
10321 }
10322
10323 return NULL_TREE;
10324
10325 case LROTATE_EXPR:
10326 case RROTATE_EXPR:
10327 case RSHIFT_EXPR:
10328 case LSHIFT_EXPR:
10329 /* Since negative shift count is not well-defined,
10330 don't try to compute it in the compiler. */
10331 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10332 return NULL_TREE;
10333
10334 prec = element_precision (type);
10335
10336 /* If we have a rotate of a bit operation with the rotate count and
10337 the second operand of the bit operation both constant,
10338 permute the two operations. */
10339 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10340 && (TREE_CODE (arg0) == BIT_AND_EXPR
10341 || TREE_CODE (arg0) == BIT_IOR_EXPR
10342 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10343 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10344 {
10345 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10346 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10347 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10348 fold_build2_loc (loc, code, type,
10349 arg00, arg1),
10350 fold_build2_loc (loc, code, type,
10351 arg01, arg1));
10352 }
10353
10354 /* Two consecutive rotates adding up to the some integer
10355 multiple of the precision of the type can be ignored. */
10356 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10357 && TREE_CODE (arg0) == RROTATE_EXPR
10358 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10359 && wi::umod_trunc (wi::to_wide (arg1)
10360 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10361 prec) == 0)
10362 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10363
10364 return NULL_TREE;
10365
10366 case MIN_EXPR:
10367 case MAX_EXPR:
10368 goto associate;
10369
10370 case TRUTH_ANDIF_EXPR:
10371 /* Note that the operands of this must be ints
10372 and their values must be 0 or 1.
10373 ("true" is a fixed value perhaps depending on the language.) */
10374 /* If first arg is constant zero, return it. */
10375 if (integer_zerop (arg0))
10376 return fold_convert_loc (loc, type, arg0);
10377 /* FALLTHRU */
10378 case TRUTH_AND_EXPR:
10379 /* If either arg is constant true, drop it. */
10380 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10381 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10382 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10383 /* Preserve sequence points. */
10384 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10385 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10386 /* If second arg is constant zero, result is zero, but first arg
10387 must be evaluated. */
10388 if (integer_zerop (arg1))
10389 return omit_one_operand_loc (loc, type, arg1, arg0);
10390 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10391 case will be handled here. */
10392 if (integer_zerop (arg0))
10393 return omit_one_operand_loc (loc, type, arg0, arg1);
10394
10395 /* !X && X is always false. */
10396 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10397 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10398 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10399 /* X && !X is always false. */
10400 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10401 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10402 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10403
10404 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10405 means A >= Y && A != MAX, but in this case we know that
10406 A < X <= MAX. */
10407
10408 if (!TREE_SIDE_EFFECTS (arg0)
10409 && !TREE_SIDE_EFFECTS (arg1))
10410 {
10411 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10412 if (tem && !operand_equal_p (tem, arg0, 0))
10413 return fold_build2_loc (loc, code, type, tem, arg1);
10414
10415 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10416 if (tem && !operand_equal_p (tem, arg1, 0))
10417 return fold_build2_loc (loc, code, type, arg0, tem);
10418 }
10419
10420 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10421 != NULL_TREE)
10422 return tem;
10423
10424 return NULL_TREE;
10425
10426 case TRUTH_ORIF_EXPR:
10427 /* Note that the operands of this must be ints
10428 and their values must be 0 or true.
10429 ("true" is a fixed value perhaps depending on the language.) */
10430 /* If first arg is constant true, return it. */
10431 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10432 return fold_convert_loc (loc, type, arg0);
10433 /* FALLTHRU */
10434 case TRUTH_OR_EXPR:
10435 /* If either arg is constant zero, drop it. */
10436 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10437 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10438 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10439 /* Preserve sequence points. */
10440 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10441 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10442 /* If second arg is constant true, result is true, but we must
10443 evaluate first arg. */
10444 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10445 return omit_one_operand_loc (loc, type, arg1, arg0);
10446 /* Likewise for first arg, but note this only occurs here for
10447 TRUTH_OR_EXPR. */
10448 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10449 return omit_one_operand_loc (loc, type, arg0, arg1);
10450
10451 /* !X || X is always true. */
10452 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10453 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10454 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10455 /* X || !X is always true. */
10456 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10457 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10458 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10459
10460 /* (X && !Y) || (!X && Y) is X ^ Y */
10461 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10462 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10463 {
10464 tree a0, a1, l0, l1, n0, n1;
10465
10466 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10467 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10468
10469 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10470 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10471
10472 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10473 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10474
10475 if ((operand_equal_p (n0, a0, 0)
10476 && operand_equal_p (n1, a1, 0))
10477 || (operand_equal_p (n0, a1, 0)
10478 && operand_equal_p (n1, a0, 0)))
10479 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10480 }
10481
10482 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10483 != NULL_TREE)
10484 return tem;
10485
10486 return NULL_TREE;
10487
10488 case TRUTH_XOR_EXPR:
10489 /* If the second arg is constant zero, drop it. */
10490 if (integer_zerop (arg1))
10491 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10492 /* If the second arg is constant true, this is a logical inversion. */
10493 if (integer_onep (arg1))
10494 {
10495 tem = invert_truthvalue_loc (loc, arg0);
10496 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10497 }
10498 /* Identical arguments cancel to zero. */
10499 if (operand_equal_p (arg0, arg1, 0))
10500 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10501
10502 /* !X ^ X is always true. */
10503 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10504 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10505 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10506
10507 /* X ^ !X is always true. */
10508 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10509 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10510 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10511
10512 return NULL_TREE;
10513
10514 case EQ_EXPR:
10515 case NE_EXPR:
10516 STRIP_NOPS (arg0);
10517 STRIP_NOPS (arg1);
10518
10519 tem = fold_comparison (loc, code, type, op0, op1);
10520 if (tem != NULL_TREE)
10521 return tem;
10522
10523 /* bool_var != 1 becomes !bool_var. */
10524 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10525 && code == NE_EXPR)
10526 return fold_convert_loc (loc, type,
10527 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10528 TREE_TYPE (arg0), arg0));
10529
10530 /* bool_var == 0 becomes !bool_var. */
10531 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10532 && code == EQ_EXPR)
10533 return fold_convert_loc (loc, type,
10534 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10535 TREE_TYPE (arg0), arg0));
10536
10537 /* !exp != 0 becomes !exp */
10538 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10539 && code == NE_EXPR)
10540 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10541
10542 /* If this is an EQ or NE comparison with zero and ARG0 is
10543 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10544 two operations, but the latter can be done in one less insn
10545 on machines that have only two-operand insns or on which a
10546 constant cannot be the first operand. */
10547 if (TREE_CODE (arg0) == BIT_AND_EXPR
10548 && integer_zerop (arg1))
10549 {
10550 tree arg00 = TREE_OPERAND (arg0, 0);
10551 tree arg01 = TREE_OPERAND (arg0, 1);
10552 if (TREE_CODE (arg00) == LSHIFT_EXPR
10553 && integer_onep (TREE_OPERAND (arg00, 0)))
10554 {
10555 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10556 arg01, TREE_OPERAND (arg00, 1));
10557 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10558 build_int_cst (TREE_TYPE (arg0), 1));
10559 return fold_build2_loc (loc, code, type,
10560 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10561 arg1);
10562 }
10563 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10564 && integer_onep (TREE_OPERAND (arg01, 0)))
10565 {
10566 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10567 arg00, TREE_OPERAND (arg01, 1));
10568 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10569 build_int_cst (TREE_TYPE (arg0), 1));
10570 return fold_build2_loc (loc, code, type,
10571 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10572 arg1);
10573 }
10574 }
10575
10576 /* If this is an NE or EQ comparison of zero against the result of a
10577 signed MOD operation whose second operand is a power of 2, make
10578 the MOD operation unsigned since it is simpler and equivalent. */
10579 if (integer_zerop (arg1)
10580 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10581 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10582 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10583 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10584 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10585 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10586 {
10587 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10588 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10589 fold_convert_loc (loc, newtype,
10590 TREE_OPERAND (arg0, 0)),
10591 fold_convert_loc (loc, newtype,
10592 TREE_OPERAND (arg0, 1)));
10593
10594 return fold_build2_loc (loc, code, type, newmod,
10595 fold_convert_loc (loc, newtype, arg1));
10596 }
10597
10598 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10599 C1 is a valid shift constant, and C2 is a power of two, i.e.
10600 a single bit. */
10601 if (TREE_CODE (arg0) == BIT_AND_EXPR
10602 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10603 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10604 == INTEGER_CST
10605 && integer_pow2p (TREE_OPERAND (arg0, 1))
10606 && integer_zerop (arg1))
10607 {
10608 tree itype = TREE_TYPE (arg0);
10609 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10610 prec = TYPE_PRECISION (itype);
10611
10612 /* Check for a valid shift count. */
10613 if (wi::ltu_p (wi::to_wide (arg001), prec))
10614 {
10615 tree arg01 = TREE_OPERAND (arg0, 1);
10616 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10617 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10618 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10619 can be rewritten as (X & (C2 << C1)) != 0. */
10620 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10621 {
10622 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10623 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10624 return fold_build2_loc (loc, code, type, tem,
10625 fold_convert_loc (loc, itype, arg1));
10626 }
10627 /* Otherwise, for signed (arithmetic) shifts,
10628 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10629 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10630 else if (!TYPE_UNSIGNED (itype))
10631 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10632 arg000, build_int_cst (itype, 0));
10633 /* Otherwise, of unsigned (logical) shifts,
10634 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10635 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10636 else
10637 return omit_one_operand_loc (loc, type,
10638 code == EQ_EXPR ? integer_one_node
10639 : integer_zero_node,
10640 arg000);
10641 }
10642 }
10643
10644 /* If this is a comparison of a field, we may be able to simplify it. */
10645 if ((TREE_CODE (arg0) == COMPONENT_REF
10646 || TREE_CODE (arg0) == BIT_FIELD_REF)
10647 /* Handle the constant case even without -O
10648 to make sure the warnings are given. */
10649 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10650 {
10651 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10652 if (t1)
10653 return t1;
10654 }
10655
10656 /* Optimize comparisons of strlen vs zero to a compare of the
10657 first character of the string vs zero. To wit,
10658 strlen(ptr) == 0 => *ptr == 0
10659 strlen(ptr) != 0 => *ptr != 0
10660 Other cases should reduce to one of these two (or a constant)
10661 due to the return value of strlen being unsigned. */
10662 if (TREE_CODE (arg0) == CALL_EXPR
10663 && integer_zerop (arg1))
10664 {
10665 tree fndecl = get_callee_fndecl (arg0);
10666
10667 if (fndecl
10668 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10669 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10670 && call_expr_nargs (arg0) == 1
10671 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10672 {
10673 tree iref = build_fold_indirect_ref_loc (loc,
10674 CALL_EXPR_ARG (arg0, 0));
10675 return fold_build2_loc (loc, code, type, iref,
10676 build_int_cst (TREE_TYPE (iref), 0));
10677 }
10678 }
10679
10680 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10681 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10682 if (TREE_CODE (arg0) == RSHIFT_EXPR
10683 && integer_zerop (arg1)
10684 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10685 {
10686 tree arg00 = TREE_OPERAND (arg0, 0);
10687 tree arg01 = TREE_OPERAND (arg0, 1);
10688 tree itype = TREE_TYPE (arg00);
10689 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10690 {
10691 if (TYPE_UNSIGNED (itype))
10692 {
10693 itype = signed_type_for (itype);
10694 arg00 = fold_convert_loc (loc, itype, arg00);
10695 }
10696 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10697 type, arg00, build_zero_cst (itype));
10698 }
10699 }
10700
10701 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10702 (X & C) == 0 when C is a single bit. */
10703 if (TREE_CODE (arg0) == BIT_AND_EXPR
10704 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10705 && integer_zerop (arg1)
10706 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10707 {
10708 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10709 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10710 TREE_OPERAND (arg0, 1));
10711 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10712 type, tem,
10713 fold_convert_loc (loc, TREE_TYPE (arg0),
10714 arg1));
10715 }
10716
10717 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10718 constant C is a power of two, i.e. a single bit. */
10719 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10720 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10721 && integer_zerop (arg1)
10722 && integer_pow2p (TREE_OPERAND (arg0, 1))
10723 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10724 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10725 {
10726 tree arg00 = TREE_OPERAND (arg0, 0);
10727 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10728 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10729 }
10730
10731 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10732 when is C is a power of two, i.e. a single bit. */
10733 if (TREE_CODE (arg0) == BIT_AND_EXPR
10734 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10735 && integer_zerop (arg1)
10736 && integer_pow2p (TREE_OPERAND (arg0, 1))
10737 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10738 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10739 {
10740 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10741 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10742 arg000, TREE_OPERAND (arg0, 1));
10743 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10744 tem, build_int_cst (TREE_TYPE (tem), 0));
10745 }
10746
10747 if (integer_zerop (arg1)
10748 && tree_expr_nonzero_p (arg0))
10749 {
10750 tree res = constant_boolean_node (code==NE_EXPR, type);
10751 return omit_one_operand_loc (loc, type, res, arg0);
10752 }
10753
10754 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10755 if (TREE_CODE (arg0) == BIT_AND_EXPR
10756 && TREE_CODE (arg1) == BIT_AND_EXPR)
10757 {
10758 tree arg00 = TREE_OPERAND (arg0, 0);
10759 tree arg01 = TREE_OPERAND (arg0, 1);
10760 tree arg10 = TREE_OPERAND (arg1, 0);
10761 tree arg11 = TREE_OPERAND (arg1, 1);
10762 tree itype = TREE_TYPE (arg0);
10763
10764 if (operand_equal_p (arg01, arg11, 0))
10765 {
10766 tem = fold_convert_loc (loc, itype, arg10);
10767 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10768 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10769 return fold_build2_loc (loc, code, type, tem,
10770 build_zero_cst (itype));
10771 }
10772 if (operand_equal_p (arg01, arg10, 0))
10773 {
10774 tem = fold_convert_loc (loc, itype, arg11);
10775 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10776 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10777 return fold_build2_loc (loc, code, type, tem,
10778 build_zero_cst (itype));
10779 }
10780 if (operand_equal_p (arg00, arg11, 0))
10781 {
10782 tem = fold_convert_loc (loc, itype, arg10);
10783 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10784 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10785 return fold_build2_loc (loc, code, type, tem,
10786 build_zero_cst (itype));
10787 }
10788 if (operand_equal_p (arg00, arg10, 0))
10789 {
10790 tem = fold_convert_loc (loc, itype, arg11);
10791 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10792 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10793 return fold_build2_loc (loc, code, type, tem,
10794 build_zero_cst (itype));
10795 }
10796 }
10797
10798 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10799 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10800 {
10801 tree arg00 = TREE_OPERAND (arg0, 0);
10802 tree arg01 = TREE_OPERAND (arg0, 1);
10803 tree arg10 = TREE_OPERAND (arg1, 0);
10804 tree arg11 = TREE_OPERAND (arg1, 1);
10805 tree itype = TREE_TYPE (arg0);
10806
10807 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10808 operand_equal_p guarantees no side-effects so we don't need
10809 to use omit_one_operand on Z. */
10810 if (operand_equal_p (arg01, arg11, 0))
10811 return fold_build2_loc (loc, code, type, arg00,
10812 fold_convert_loc (loc, TREE_TYPE (arg00),
10813 arg10));
10814 if (operand_equal_p (arg01, arg10, 0))
10815 return fold_build2_loc (loc, code, type, arg00,
10816 fold_convert_loc (loc, TREE_TYPE (arg00),
10817 arg11));
10818 if (operand_equal_p (arg00, arg11, 0))
10819 return fold_build2_loc (loc, code, type, arg01,
10820 fold_convert_loc (loc, TREE_TYPE (arg01),
10821 arg10));
10822 if (operand_equal_p (arg00, arg10, 0))
10823 return fold_build2_loc (loc, code, type, arg01,
10824 fold_convert_loc (loc, TREE_TYPE (arg01),
10825 arg11));
10826
10827 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10828 if (TREE_CODE (arg01) == INTEGER_CST
10829 && TREE_CODE (arg11) == INTEGER_CST)
10830 {
10831 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10832 fold_convert_loc (loc, itype, arg11));
10833 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10834 return fold_build2_loc (loc, code, type, tem,
10835 fold_convert_loc (loc, itype, arg10));
10836 }
10837 }
10838
10839 /* Attempt to simplify equality/inequality comparisons of complex
10840 values. Only lower the comparison if the result is known or
10841 can be simplified to a single scalar comparison. */
10842 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10843 || TREE_CODE (arg0) == COMPLEX_CST)
10844 && (TREE_CODE (arg1) == COMPLEX_EXPR
10845 || TREE_CODE (arg1) == COMPLEX_CST))
10846 {
10847 tree real0, imag0, real1, imag1;
10848 tree rcond, icond;
10849
10850 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10851 {
10852 real0 = TREE_OPERAND (arg0, 0);
10853 imag0 = TREE_OPERAND (arg0, 1);
10854 }
10855 else
10856 {
10857 real0 = TREE_REALPART (arg0);
10858 imag0 = TREE_IMAGPART (arg0);
10859 }
10860
10861 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10862 {
10863 real1 = TREE_OPERAND (arg1, 0);
10864 imag1 = TREE_OPERAND (arg1, 1);
10865 }
10866 else
10867 {
10868 real1 = TREE_REALPART (arg1);
10869 imag1 = TREE_IMAGPART (arg1);
10870 }
10871
10872 rcond = fold_binary_loc (loc, code, type, real0, real1);
10873 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10874 {
10875 if (integer_zerop (rcond))
10876 {
10877 if (code == EQ_EXPR)
10878 return omit_two_operands_loc (loc, type, boolean_false_node,
10879 imag0, imag1);
10880 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10881 }
10882 else
10883 {
10884 if (code == NE_EXPR)
10885 return omit_two_operands_loc (loc, type, boolean_true_node,
10886 imag0, imag1);
10887 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10888 }
10889 }
10890
10891 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10892 if (icond && TREE_CODE (icond) == INTEGER_CST)
10893 {
10894 if (integer_zerop (icond))
10895 {
10896 if (code == EQ_EXPR)
10897 return omit_two_operands_loc (loc, type, boolean_false_node,
10898 real0, real1);
10899 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10900 }
10901 else
10902 {
10903 if (code == NE_EXPR)
10904 return omit_two_operands_loc (loc, type, boolean_true_node,
10905 real0, real1);
10906 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10907 }
10908 }
10909 }
10910
10911 return NULL_TREE;
10912
10913 case LT_EXPR:
10914 case GT_EXPR:
10915 case LE_EXPR:
10916 case GE_EXPR:
10917 tem = fold_comparison (loc, code, type, op0, op1);
10918 if (tem != NULL_TREE)
10919 return tem;
10920
10921 /* Transform comparisons of the form X +- C CMP X. */
10922 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10923 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10924 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10925 && !HONOR_SNANS (arg0))
10926 {
10927 tree arg01 = TREE_OPERAND (arg0, 1);
10928 enum tree_code code0 = TREE_CODE (arg0);
10929 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10930
10931 /* (X - c) > X becomes false. */
10932 if (code == GT_EXPR
10933 && ((code0 == MINUS_EXPR && is_positive >= 0)
10934 || (code0 == PLUS_EXPR && is_positive <= 0)))
10935 return constant_boolean_node (0, type);
10936
10937 /* Likewise (X + c) < X becomes false. */
10938 if (code == LT_EXPR
10939 && ((code0 == PLUS_EXPR && is_positive >= 0)
10940 || (code0 == MINUS_EXPR && is_positive <= 0)))
10941 return constant_boolean_node (0, type);
10942
10943 /* Convert (X - c) <= X to true. */
10944 if (!HONOR_NANS (arg1)
10945 && code == LE_EXPR
10946 && ((code0 == MINUS_EXPR && is_positive >= 0)
10947 || (code0 == PLUS_EXPR && is_positive <= 0)))
10948 return constant_boolean_node (1, type);
10949
10950 /* Convert (X + c) >= X to true. */
10951 if (!HONOR_NANS (arg1)
10952 && code == GE_EXPR
10953 && ((code0 == PLUS_EXPR && is_positive >= 0)
10954 || (code0 == MINUS_EXPR && is_positive <= 0)))
10955 return constant_boolean_node (1, type);
10956 }
10957
10958 /* If we are comparing an ABS_EXPR with a constant, we can
10959 convert all the cases into explicit comparisons, but they may
10960 well not be faster than doing the ABS and one comparison.
10961 But ABS (X) <= C is a range comparison, which becomes a subtraction
10962 and a comparison, and is probably faster. */
10963 if (code == LE_EXPR
10964 && TREE_CODE (arg1) == INTEGER_CST
10965 && TREE_CODE (arg0) == ABS_EXPR
10966 && ! TREE_SIDE_EFFECTS (arg0)
10967 && (0 != (tem = negate_expr (arg1)))
10968 && TREE_CODE (tem) == INTEGER_CST
10969 && !TREE_OVERFLOW (tem))
10970 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
10971 build2 (GE_EXPR, type,
10972 TREE_OPERAND (arg0, 0), tem),
10973 build2 (LE_EXPR, type,
10974 TREE_OPERAND (arg0, 0), arg1));
10975
10976 /* Convert ABS_EXPR<x> >= 0 to true. */
10977 strict_overflow_p = false;
10978 if (code == GE_EXPR
10979 && (integer_zerop (arg1)
10980 || (! HONOR_NANS (arg0)
10981 && real_zerop (arg1)))
10982 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10983 {
10984 if (strict_overflow_p)
10985 fold_overflow_warning (("assuming signed overflow does not occur "
10986 "when simplifying comparison of "
10987 "absolute value and zero"),
10988 WARN_STRICT_OVERFLOW_CONDITIONAL);
10989 return omit_one_operand_loc (loc, type,
10990 constant_boolean_node (true, type),
10991 arg0);
10992 }
10993
10994 /* Convert ABS_EXPR<x> < 0 to false. */
10995 strict_overflow_p = false;
10996 if (code == LT_EXPR
10997 && (integer_zerop (arg1) || real_zerop (arg1))
10998 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
10999 {
11000 if (strict_overflow_p)
11001 fold_overflow_warning (("assuming signed overflow does not occur "
11002 "when simplifying comparison of "
11003 "absolute value and zero"),
11004 WARN_STRICT_OVERFLOW_CONDITIONAL);
11005 return omit_one_operand_loc (loc, type,
11006 constant_boolean_node (false, type),
11007 arg0);
11008 }
11009
11010 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11011 and similarly for >= into !=. */
11012 if ((code == LT_EXPR || code == GE_EXPR)
11013 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11014 && TREE_CODE (arg1) == LSHIFT_EXPR
11015 && integer_onep (TREE_OPERAND (arg1, 0)))
11016 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11017 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11018 TREE_OPERAND (arg1, 1)),
11019 build_zero_cst (TREE_TYPE (arg0)));
11020
11021 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11022 otherwise Y might be >= # of bits in X's type and thus e.g.
11023 (unsigned char) (1 << Y) for Y 15 might be 0.
11024 If the cast is widening, then 1 << Y should have unsigned type,
11025 otherwise if Y is number of bits in the signed shift type minus 1,
11026 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11027 31 might be 0xffffffff80000000. */
11028 if ((code == LT_EXPR || code == GE_EXPR)
11029 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11030 && CONVERT_EXPR_P (arg1)
11031 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11032 && (element_precision (TREE_TYPE (arg1))
11033 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11034 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11035 || (element_precision (TREE_TYPE (arg1))
11036 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11037 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11038 {
11039 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11040 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11041 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11042 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11043 build_zero_cst (TREE_TYPE (arg0)));
11044 }
11045
11046 return NULL_TREE;
11047
11048 case UNORDERED_EXPR:
11049 case ORDERED_EXPR:
11050 case UNLT_EXPR:
11051 case UNLE_EXPR:
11052 case UNGT_EXPR:
11053 case UNGE_EXPR:
11054 case UNEQ_EXPR:
11055 case LTGT_EXPR:
11056 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11057 {
11058 tree targ0 = strip_float_extensions (arg0);
11059 tree targ1 = strip_float_extensions (arg1);
11060 tree newtype = TREE_TYPE (targ0);
11061
11062 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11063 newtype = TREE_TYPE (targ1);
11064
11065 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11066 return fold_build2_loc (loc, code, type,
11067 fold_convert_loc (loc, newtype, targ0),
11068 fold_convert_loc (loc, newtype, targ1));
11069 }
11070
11071 return NULL_TREE;
11072
11073 case COMPOUND_EXPR:
11074 /* When pedantic, a compound expression can be neither an lvalue
11075 nor an integer constant expression. */
11076 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11077 return NULL_TREE;
11078 /* Don't let (0, 0) be null pointer constant. */
11079 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11080 : fold_convert_loc (loc, type, arg1);
11081 return pedantic_non_lvalue_loc (loc, tem);
11082
11083 case ASSERT_EXPR:
11084 /* An ASSERT_EXPR should never be passed to fold_binary. */
11085 gcc_unreachable ();
11086
11087 default:
11088 return NULL_TREE;
11089 } /* switch (code) */
11090 }
11091
11092 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11093 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11094 of GOTO_EXPR. */
11095
11096 static tree
11097 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11098 {
11099 switch (TREE_CODE (*tp))
11100 {
11101 case LABEL_EXPR:
11102 return *tp;
11103
11104 case GOTO_EXPR:
11105 *walk_subtrees = 0;
11106
11107 /* fall through */
11108
11109 default:
11110 return NULL_TREE;
11111 }
11112 }
11113
11114 /* Return whether the sub-tree ST contains a label which is accessible from
11115 outside the sub-tree. */
11116
11117 static bool
11118 contains_label_p (tree st)
11119 {
11120 return
11121 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11122 }
11123
11124 /* Fold a ternary expression of code CODE and type TYPE with operands
11125 OP0, OP1, and OP2. Return the folded expression if folding is
11126 successful. Otherwise, return NULL_TREE. */
11127
11128 tree
11129 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11130 tree op0, tree op1, tree op2)
11131 {
11132 tree tem;
11133 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11134 enum tree_code_class kind = TREE_CODE_CLASS (code);
11135
11136 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11137 && TREE_CODE_LENGTH (code) == 3);
11138
11139 /* If this is a commutative operation, and OP0 is a constant, move it
11140 to OP1 to reduce the number of tests below. */
11141 if (commutative_ternary_tree_code (code)
11142 && tree_swap_operands_p (op0, op1))
11143 return fold_build3_loc (loc, code, type, op1, op0, op2);
11144
11145 tem = generic_simplify (loc, code, type, op0, op1, op2);
11146 if (tem)
11147 return tem;
11148
11149 /* Strip any conversions that don't change the mode. This is safe
11150 for every expression, except for a comparison expression because
11151 its signedness is derived from its operands. So, in the latter
11152 case, only strip conversions that don't change the signedness.
11153
11154 Note that this is done as an internal manipulation within the
11155 constant folder, in order to find the simplest representation of
11156 the arguments so that their form can be studied. In any cases,
11157 the appropriate type conversions should be put back in the tree
11158 that will get out of the constant folder. */
11159 if (op0)
11160 {
11161 arg0 = op0;
11162 STRIP_NOPS (arg0);
11163 }
11164
11165 if (op1)
11166 {
11167 arg1 = op1;
11168 STRIP_NOPS (arg1);
11169 }
11170
11171 if (op2)
11172 {
11173 arg2 = op2;
11174 STRIP_NOPS (arg2);
11175 }
11176
11177 switch (code)
11178 {
11179 case COMPONENT_REF:
11180 if (TREE_CODE (arg0) == CONSTRUCTOR
11181 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11182 {
11183 unsigned HOST_WIDE_INT idx;
11184 tree field, value;
11185 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11186 if (field == arg1)
11187 return value;
11188 }
11189 return NULL_TREE;
11190
11191 case COND_EXPR:
11192 case VEC_COND_EXPR:
11193 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11194 so all simple results must be passed through pedantic_non_lvalue. */
11195 if (TREE_CODE (arg0) == INTEGER_CST)
11196 {
11197 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11198 tem = integer_zerop (arg0) ? op2 : op1;
11199 /* Only optimize constant conditions when the selected branch
11200 has the same type as the COND_EXPR. This avoids optimizing
11201 away "c ? x : throw", where the throw has a void type.
11202 Avoid throwing away that operand which contains label. */
11203 if ((!TREE_SIDE_EFFECTS (unused_op)
11204 || !contains_label_p (unused_op))
11205 && (! VOID_TYPE_P (TREE_TYPE (tem))
11206 || VOID_TYPE_P (type)))
11207 return pedantic_non_lvalue_loc (loc, tem);
11208 return NULL_TREE;
11209 }
11210 else if (TREE_CODE (arg0) == VECTOR_CST)
11211 {
11212 if ((TREE_CODE (arg1) == VECTOR_CST
11213 || TREE_CODE (arg1) == CONSTRUCTOR)
11214 && (TREE_CODE (arg2) == VECTOR_CST
11215 || TREE_CODE (arg2) == CONSTRUCTOR))
11216 {
11217 unsigned int nelts = VECTOR_CST_NELTS (arg0), i;
11218 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11219 auto_vec_perm_indices sel (nelts);
11220 for (i = 0; i < nelts; i++)
11221 {
11222 tree val = VECTOR_CST_ELT (arg0, i);
11223 if (integer_all_onesp (val))
11224 sel.quick_push (i);
11225 else if (integer_zerop (val))
11226 sel.quick_push (nelts + i);
11227 else /* Currently unreachable. */
11228 return NULL_TREE;
11229 }
11230 tree t = fold_vec_perm (type, arg1, arg2, sel);
11231 if (t != NULL_TREE)
11232 return t;
11233 }
11234 }
11235
11236 /* If we have A op B ? A : C, we may be able to convert this to a
11237 simpler expression, depending on the operation and the values
11238 of B and C. Signed zeros prevent all of these transformations,
11239 for reasons given above each one.
11240
11241 Also try swapping the arguments and inverting the conditional. */
11242 if (COMPARISON_CLASS_P (arg0)
11243 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11244 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11245 {
11246 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11247 if (tem)
11248 return tem;
11249 }
11250
11251 if (COMPARISON_CLASS_P (arg0)
11252 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11253 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11254 {
11255 location_t loc0 = expr_location_or (arg0, loc);
11256 tem = fold_invert_truthvalue (loc0, arg0);
11257 if (tem && COMPARISON_CLASS_P (tem))
11258 {
11259 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11260 if (tem)
11261 return tem;
11262 }
11263 }
11264
11265 /* If the second operand is simpler than the third, swap them
11266 since that produces better jump optimization results. */
11267 if (truth_value_p (TREE_CODE (arg0))
11268 && tree_swap_operands_p (op1, op2))
11269 {
11270 location_t loc0 = expr_location_or (arg0, loc);
11271 /* See if this can be inverted. If it can't, possibly because
11272 it was a floating-point inequality comparison, don't do
11273 anything. */
11274 tem = fold_invert_truthvalue (loc0, arg0);
11275 if (tem)
11276 return fold_build3_loc (loc, code, type, tem, op2, op1);
11277 }
11278
11279 /* Convert A ? 1 : 0 to simply A. */
11280 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11281 : (integer_onep (op1)
11282 && !VECTOR_TYPE_P (type)))
11283 && integer_zerop (op2)
11284 /* If we try to convert OP0 to our type, the
11285 call to fold will try to move the conversion inside
11286 a COND, which will recurse. In that case, the COND_EXPR
11287 is probably the best choice, so leave it alone. */
11288 && type == TREE_TYPE (arg0))
11289 return pedantic_non_lvalue_loc (loc, arg0);
11290
11291 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11292 over COND_EXPR in cases such as floating point comparisons. */
11293 if (integer_zerop (op1)
11294 && code == COND_EXPR
11295 && integer_onep (op2)
11296 && !VECTOR_TYPE_P (type)
11297 && truth_value_p (TREE_CODE (arg0)))
11298 return pedantic_non_lvalue_loc (loc,
11299 fold_convert_loc (loc, type,
11300 invert_truthvalue_loc (loc,
11301 arg0)));
11302
11303 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11304 if (TREE_CODE (arg0) == LT_EXPR
11305 && integer_zerop (TREE_OPERAND (arg0, 1))
11306 && integer_zerop (op2)
11307 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11308 {
11309 /* sign_bit_p looks through both zero and sign extensions,
11310 but for this optimization only sign extensions are
11311 usable. */
11312 tree tem2 = TREE_OPERAND (arg0, 0);
11313 while (tem != tem2)
11314 {
11315 if (TREE_CODE (tem2) != NOP_EXPR
11316 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11317 {
11318 tem = NULL_TREE;
11319 break;
11320 }
11321 tem2 = TREE_OPERAND (tem2, 0);
11322 }
11323 /* sign_bit_p only checks ARG1 bits within A's precision.
11324 If <sign bit of A> has wider type than A, bits outside
11325 of A's precision in <sign bit of A> need to be checked.
11326 If they are all 0, this optimization needs to be done
11327 in unsigned A's type, if they are all 1 in signed A's type,
11328 otherwise this can't be done. */
11329 if (tem
11330 && TYPE_PRECISION (TREE_TYPE (tem))
11331 < TYPE_PRECISION (TREE_TYPE (arg1))
11332 && TYPE_PRECISION (TREE_TYPE (tem))
11333 < TYPE_PRECISION (type))
11334 {
11335 int inner_width, outer_width;
11336 tree tem_type;
11337
11338 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11339 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11340 if (outer_width > TYPE_PRECISION (type))
11341 outer_width = TYPE_PRECISION (type);
11342
11343 wide_int mask = wi::shifted_mask
11344 (inner_width, outer_width - inner_width, false,
11345 TYPE_PRECISION (TREE_TYPE (arg1)));
11346
11347 wide_int common = mask & wi::to_wide (arg1);
11348 if (common == mask)
11349 {
11350 tem_type = signed_type_for (TREE_TYPE (tem));
11351 tem = fold_convert_loc (loc, tem_type, tem);
11352 }
11353 else if (common == 0)
11354 {
11355 tem_type = unsigned_type_for (TREE_TYPE (tem));
11356 tem = fold_convert_loc (loc, tem_type, tem);
11357 }
11358 else
11359 tem = NULL;
11360 }
11361
11362 if (tem)
11363 return
11364 fold_convert_loc (loc, type,
11365 fold_build2_loc (loc, BIT_AND_EXPR,
11366 TREE_TYPE (tem), tem,
11367 fold_convert_loc (loc,
11368 TREE_TYPE (tem),
11369 arg1)));
11370 }
11371
11372 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11373 already handled above. */
11374 if (TREE_CODE (arg0) == BIT_AND_EXPR
11375 && integer_onep (TREE_OPERAND (arg0, 1))
11376 && integer_zerop (op2)
11377 && integer_pow2p (arg1))
11378 {
11379 tree tem = TREE_OPERAND (arg0, 0);
11380 STRIP_NOPS (tem);
11381 if (TREE_CODE (tem) == RSHIFT_EXPR
11382 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11383 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11384 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11385 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11386 fold_convert_loc (loc, type,
11387 TREE_OPERAND (tem, 0)),
11388 op1);
11389 }
11390
11391 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11392 is probably obsolete because the first operand should be a
11393 truth value (that's why we have the two cases above), but let's
11394 leave it in until we can confirm this for all front-ends. */
11395 if (integer_zerop (op2)
11396 && TREE_CODE (arg0) == NE_EXPR
11397 && integer_zerop (TREE_OPERAND (arg0, 1))
11398 && integer_pow2p (arg1)
11399 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11400 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11401 arg1, OEP_ONLY_CONST))
11402 return pedantic_non_lvalue_loc (loc,
11403 fold_convert_loc (loc, type,
11404 TREE_OPERAND (arg0, 0)));
11405
11406 /* Disable the transformations below for vectors, since
11407 fold_binary_op_with_conditional_arg may undo them immediately,
11408 yielding an infinite loop. */
11409 if (code == VEC_COND_EXPR)
11410 return NULL_TREE;
11411
11412 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11413 if (integer_zerop (op2)
11414 && truth_value_p (TREE_CODE (arg0))
11415 && truth_value_p (TREE_CODE (arg1))
11416 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11417 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11418 : TRUTH_ANDIF_EXPR,
11419 type, fold_convert_loc (loc, type, arg0), op1);
11420
11421 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11422 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11423 && truth_value_p (TREE_CODE (arg0))
11424 && truth_value_p (TREE_CODE (arg1))
11425 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11426 {
11427 location_t loc0 = expr_location_or (arg0, loc);
11428 /* Only perform transformation if ARG0 is easily inverted. */
11429 tem = fold_invert_truthvalue (loc0, arg0);
11430 if (tem)
11431 return fold_build2_loc (loc, code == VEC_COND_EXPR
11432 ? BIT_IOR_EXPR
11433 : TRUTH_ORIF_EXPR,
11434 type, fold_convert_loc (loc, type, tem),
11435 op1);
11436 }
11437
11438 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11439 if (integer_zerop (arg1)
11440 && truth_value_p (TREE_CODE (arg0))
11441 && truth_value_p (TREE_CODE (op2))
11442 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11443 {
11444 location_t loc0 = expr_location_or (arg0, loc);
11445 /* Only perform transformation if ARG0 is easily inverted. */
11446 tem = fold_invert_truthvalue (loc0, arg0);
11447 if (tem)
11448 return fold_build2_loc (loc, code == VEC_COND_EXPR
11449 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11450 type, fold_convert_loc (loc, type, tem),
11451 op2);
11452 }
11453
11454 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11455 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11456 && truth_value_p (TREE_CODE (arg0))
11457 && truth_value_p (TREE_CODE (op2))
11458 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11459 return fold_build2_loc (loc, code == VEC_COND_EXPR
11460 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11461 type, fold_convert_loc (loc, type, arg0), op2);
11462
11463 return NULL_TREE;
11464
11465 case CALL_EXPR:
11466 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11467 of fold_ternary on them. */
11468 gcc_unreachable ();
11469
11470 case BIT_FIELD_REF:
11471 if (TREE_CODE (arg0) == VECTOR_CST
11472 && (type == TREE_TYPE (TREE_TYPE (arg0))
11473 || (TREE_CODE (type) == VECTOR_TYPE
11474 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11475 {
11476 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11477 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11478 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11479 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11480
11481 if (n != 0
11482 && (idx % width) == 0
11483 && (n % width) == 0
11484 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11485 {
11486 idx = idx / width;
11487 n = n / width;
11488
11489 if (TREE_CODE (arg0) == VECTOR_CST)
11490 {
11491 if (n == 1)
11492 return VECTOR_CST_ELT (arg0, idx);
11493
11494 tree_vector_builder vals (type, n, 1);
11495 for (unsigned i = 0; i < n; ++i)
11496 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11497 return vals.build ();
11498 }
11499 }
11500 }
11501
11502 /* On constants we can use native encode/interpret to constant
11503 fold (nearly) all BIT_FIELD_REFs. */
11504 if (CONSTANT_CLASS_P (arg0)
11505 && can_native_interpret_type_p (type)
11506 && BITS_PER_UNIT == 8)
11507 {
11508 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11509 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11510 /* Limit us to a reasonable amount of work. To relax the
11511 other limitations we need bit-shifting of the buffer
11512 and rounding up the size. */
11513 if (bitpos % BITS_PER_UNIT == 0
11514 && bitsize % BITS_PER_UNIT == 0
11515 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11516 {
11517 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11518 unsigned HOST_WIDE_INT len
11519 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11520 bitpos / BITS_PER_UNIT);
11521 if (len > 0
11522 && len * BITS_PER_UNIT >= bitsize)
11523 {
11524 tree v = native_interpret_expr (type, b,
11525 bitsize / BITS_PER_UNIT);
11526 if (v)
11527 return v;
11528 }
11529 }
11530 }
11531
11532 return NULL_TREE;
11533
11534 case FMA_EXPR:
11535 /* For integers we can decompose the FMA if possible. */
11536 if (TREE_CODE (arg0) == INTEGER_CST
11537 && TREE_CODE (arg1) == INTEGER_CST)
11538 return fold_build2_loc (loc, PLUS_EXPR, type,
11539 const_binop (MULT_EXPR, arg0, arg1), arg2);
11540 if (integer_zerop (arg2))
11541 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11542
11543 return fold_fma (loc, type, arg0, arg1, arg2);
11544
11545 case VEC_PERM_EXPR:
11546 if (TREE_CODE (arg2) == VECTOR_CST)
11547 {
11548 unsigned int nelts = VECTOR_CST_NELTS (arg2), i, mask, mask2;
11549 bool need_mask_canon = false;
11550 bool need_mask_canon2 = false;
11551 bool all_in_vec0 = true;
11552 bool all_in_vec1 = true;
11553 bool maybe_identity = true;
11554 bool single_arg = (op0 == op1);
11555 bool changed = false;
11556
11557 mask2 = 2 * nelts - 1;
11558 mask = single_arg ? (nelts - 1) : mask2;
11559 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11560 auto_vec_perm_indices sel (nelts);
11561 auto_vec_perm_indices sel2 (nelts);
11562 for (i = 0; i < nelts; i++)
11563 {
11564 tree val = VECTOR_CST_ELT (arg2, i);
11565 if (TREE_CODE (val) != INTEGER_CST)
11566 return NULL_TREE;
11567
11568 /* Make sure that the perm value is in an acceptable
11569 range. */
11570 wi::tree_to_wide_ref t = wi::to_wide (val);
11571 need_mask_canon |= wi::gtu_p (t, mask);
11572 need_mask_canon2 |= wi::gtu_p (t, mask2);
11573 unsigned int elt = t.to_uhwi () & mask;
11574 unsigned int elt2 = t.to_uhwi () & mask2;
11575
11576 if (elt < nelts)
11577 all_in_vec1 = false;
11578 else
11579 all_in_vec0 = false;
11580
11581 if ((elt & (nelts - 1)) != i)
11582 maybe_identity = false;
11583
11584 sel.quick_push (elt);
11585 sel2.quick_push (elt2);
11586 }
11587
11588 if (maybe_identity)
11589 {
11590 if (all_in_vec0)
11591 return op0;
11592 if (all_in_vec1)
11593 return op1;
11594 }
11595
11596 if (all_in_vec0)
11597 op1 = op0;
11598 else if (all_in_vec1)
11599 {
11600 op0 = op1;
11601 for (i = 0; i < nelts; i++)
11602 sel[i] -= nelts;
11603 need_mask_canon = true;
11604 }
11605
11606 if ((TREE_CODE (op0) == VECTOR_CST
11607 || TREE_CODE (op0) == CONSTRUCTOR)
11608 && (TREE_CODE (op1) == VECTOR_CST
11609 || TREE_CODE (op1) == CONSTRUCTOR))
11610 {
11611 tree t = fold_vec_perm (type, op0, op1, sel);
11612 if (t != NULL_TREE)
11613 return t;
11614 }
11615
11616 if (op0 == op1 && !single_arg)
11617 changed = true;
11618
11619 /* Some targets are deficient and fail to expand a single
11620 argument permutation while still allowing an equivalent
11621 2-argument version. */
11622 if (need_mask_canon && arg2 == op2
11623 && !can_vec_perm_p (TYPE_MODE (type), false, &sel)
11624 && can_vec_perm_p (TYPE_MODE (type), false, &sel2))
11625 {
11626 need_mask_canon = need_mask_canon2;
11627 sel = sel2;
11628 }
11629
11630 if (need_mask_canon && arg2 == op2)
11631 {
11632 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11633 tree_vector_builder tsel (TREE_TYPE (arg2), nelts, 1);
11634 for (i = 0; i < nelts; i++)
11635 tsel.quick_push (build_int_cst (eltype, sel[i]));
11636 op2 = tsel.build ();
11637 changed = true;
11638 }
11639
11640 if (changed)
11641 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11642 }
11643 return NULL_TREE;
11644
11645 case BIT_INSERT_EXPR:
11646 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11647 if (TREE_CODE (arg0) == INTEGER_CST
11648 && TREE_CODE (arg1) == INTEGER_CST)
11649 {
11650 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11651 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11652 wide_int tem = (wi::to_wide (arg0)
11653 & wi::shifted_mask (bitpos, bitsize, true,
11654 TYPE_PRECISION (type)));
11655 wide_int tem2
11656 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11657 bitsize), bitpos);
11658 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11659 }
11660 else if (TREE_CODE (arg0) == VECTOR_CST
11661 && CONSTANT_CLASS_P (arg1)
11662 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11663 TREE_TYPE (arg1)))
11664 {
11665 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11666 unsigned HOST_WIDE_INT elsize
11667 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11668 if (bitpos % elsize == 0)
11669 {
11670 unsigned k = bitpos / elsize;
11671 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11672 return arg0;
11673 else
11674 {
11675 unsigned int nelts = VECTOR_CST_NELTS (arg0);
11676 tree_vector_builder elts (type, nelts, 1);
11677 elts.quick_grow (nelts);
11678 for (unsigned int i = 0; i < nelts; ++i)
11679 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11680 return elts.build ();
11681 }
11682 }
11683 }
11684 return NULL_TREE;
11685
11686 default:
11687 return NULL_TREE;
11688 } /* switch (code) */
11689 }
11690
11691 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11692 of an array (or vector). */
11693
11694 tree
11695 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11696 {
11697 tree index_type = NULL_TREE;
11698 offset_int low_bound = 0;
11699
11700 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11701 {
11702 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11703 if (domain_type && TYPE_MIN_VALUE (domain_type))
11704 {
11705 /* Static constructors for variably sized objects makes no sense. */
11706 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11707 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11708 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11709 }
11710 }
11711
11712 if (index_type)
11713 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11714 TYPE_SIGN (index_type));
11715
11716 offset_int index = low_bound - 1;
11717 if (index_type)
11718 index = wi::ext (index, TYPE_PRECISION (index_type),
11719 TYPE_SIGN (index_type));
11720
11721 offset_int max_index;
11722 unsigned HOST_WIDE_INT cnt;
11723 tree cfield, cval;
11724
11725 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11726 {
11727 /* Array constructor might explicitly set index, or specify a range,
11728 or leave index NULL meaning that it is next index after previous
11729 one. */
11730 if (cfield)
11731 {
11732 if (TREE_CODE (cfield) == INTEGER_CST)
11733 max_index = index = wi::to_offset (cfield);
11734 else
11735 {
11736 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11737 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11738 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11739 }
11740 }
11741 else
11742 {
11743 index += 1;
11744 if (index_type)
11745 index = wi::ext (index, TYPE_PRECISION (index_type),
11746 TYPE_SIGN (index_type));
11747 max_index = index;
11748 }
11749
11750 /* Do we have match? */
11751 if (wi::cmpu (access_index, index) >= 0
11752 && wi::cmpu (access_index, max_index) <= 0)
11753 return cval;
11754 }
11755 return NULL_TREE;
11756 }
11757
11758 /* Perform constant folding and related simplification of EXPR.
11759 The related simplifications include x*1 => x, x*0 => 0, etc.,
11760 and application of the associative law.
11761 NOP_EXPR conversions may be removed freely (as long as we
11762 are careful not to change the type of the overall expression).
11763 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11764 but we can constant-fold them if they have constant operands. */
11765
11766 #ifdef ENABLE_FOLD_CHECKING
11767 # define fold(x) fold_1 (x)
11768 static tree fold_1 (tree);
11769 static
11770 #endif
11771 tree
11772 fold (tree expr)
11773 {
11774 const tree t = expr;
11775 enum tree_code code = TREE_CODE (t);
11776 enum tree_code_class kind = TREE_CODE_CLASS (code);
11777 tree tem;
11778 location_t loc = EXPR_LOCATION (expr);
11779
11780 /* Return right away if a constant. */
11781 if (kind == tcc_constant)
11782 return t;
11783
11784 /* CALL_EXPR-like objects with variable numbers of operands are
11785 treated specially. */
11786 if (kind == tcc_vl_exp)
11787 {
11788 if (code == CALL_EXPR)
11789 {
11790 tem = fold_call_expr (loc, expr, false);
11791 return tem ? tem : expr;
11792 }
11793 return expr;
11794 }
11795
11796 if (IS_EXPR_CODE_CLASS (kind))
11797 {
11798 tree type = TREE_TYPE (t);
11799 tree op0, op1, op2;
11800
11801 switch (TREE_CODE_LENGTH (code))
11802 {
11803 case 1:
11804 op0 = TREE_OPERAND (t, 0);
11805 tem = fold_unary_loc (loc, code, type, op0);
11806 return tem ? tem : expr;
11807 case 2:
11808 op0 = TREE_OPERAND (t, 0);
11809 op1 = TREE_OPERAND (t, 1);
11810 tem = fold_binary_loc (loc, code, type, op0, op1);
11811 return tem ? tem : expr;
11812 case 3:
11813 op0 = TREE_OPERAND (t, 0);
11814 op1 = TREE_OPERAND (t, 1);
11815 op2 = TREE_OPERAND (t, 2);
11816 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11817 return tem ? tem : expr;
11818 default:
11819 break;
11820 }
11821 }
11822
11823 switch (code)
11824 {
11825 case ARRAY_REF:
11826 {
11827 tree op0 = TREE_OPERAND (t, 0);
11828 tree op1 = TREE_OPERAND (t, 1);
11829
11830 if (TREE_CODE (op1) == INTEGER_CST
11831 && TREE_CODE (op0) == CONSTRUCTOR
11832 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11833 {
11834 tree val = get_array_ctor_element_at_index (op0,
11835 wi::to_offset (op1));
11836 if (val)
11837 return val;
11838 }
11839
11840 return t;
11841 }
11842
11843 /* Return a VECTOR_CST if possible. */
11844 case CONSTRUCTOR:
11845 {
11846 tree type = TREE_TYPE (t);
11847 if (TREE_CODE (type) != VECTOR_TYPE)
11848 return t;
11849
11850 unsigned i;
11851 tree val;
11852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11853 if (! CONSTANT_CLASS_P (val))
11854 return t;
11855
11856 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11857 }
11858
11859 case CONST_DECL:
11860 return fold (DECL_INITIAL (t));
11861
11862 default:
11863 return t;
11864 } /* switch (code) */
11865 }
11866
11867 #ifdef ENABLE_FOLD_CHECKING
11868 #undef fold
11869
11870 static void fold_checksum_tree (const_tree, struct md5_ctx *,
11871 hash_table<nofree_ptr_hash<const tree_node> > *);
11872 static void fold_check_failed (const_tree, const_tree);
11873 void print_fold_checksum (const_tree);
11874
11875 /* When --enable-checking=fold, compute a digest of expr before
11876 and after actual fold call to see if fold did not accidentally
11877 change original expr. */
11878
11879 tree
11880 fold (tree expr)
11881 {
11882 tree ret;
11883 struct md5_ctx ctx;
11884 unsigned char checksum_before[16], checksum_after[16];
11885 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11886
11887 md5_init_ctx (&ctx);
11888 fold_checksum_tree (expr, &ctx, &ht);
11889 md5_finish_ctx (&ctx, checksum_before);
11890 ht.empty ();
11891
11892 ret = fold_1 (expr);
11893
11894 md5_init_ctx (&ctx);
11895 fold_checksum_tree (expr, &ctx, &ht);
11896 md5_finish_ctx (&ctx, checksum_after);
11897
11898 if (memcmp (checksum_before, checksum_after, 16))
11899 fold_check_failed (expr, ret);
11900
11901 return ret;
11902 }
11903
11904 void
11905 print_fold_checksum (const_tree expr)
11906 {
11907 struct md5_ctx ctx;
11908 unsigned char checksum[16], cnt;
11909 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
11910
11911 md5_init_ctx (&ctx);
11912 fold_checksum_tree (expr, &ctx, &ht);
11913 md5_finish_ctx (&ctx, checksum);
11914 for (cnt = 0; cnt < 16; ++cnt)
11915 fprintf (stderr, "%02x", checksum[cnt]);
11916 putc ('\n', stderr);
11917 }
11918
11919 static void
11920 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
11921 {
11922 internal_error ("fold check: original tree changed by fold");
11923 }
11924
11925 static void
11926 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
11927 hash_table<nofree_ptr_hash <const tree_node> > *ht)
11928 {
11929 const tree_node **slot;
11930 enum tree_code code;
11931 union tree_node buf;
11932 int i, len;
11933
11934 recursive_label:
11935 if (expr == NULL)
11936 return;
11937 slot = ht->find_slot (expr, INSERT);
11938 if (*slot != NULL)
11939 return;
11940 *slot = expr;
11941 code = TREE_CODE (expr);
11942 if (TREE_CODE_CLASS (code) == tcc_declaration
11943 && HAS_DECL_ASSEMBLER_NAME_P (expr))
11944 {
11945 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
11946 memcpy ((char *) &buf, expr, tree_size (expr));
11947 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
11948 buf.decl_with_vis.symtab_node = NULL;
11949 expr = (tree) &buf;
11950 }
11951 else if (TREE_CODE_CLASS (code) == tcc_type
11952 && (TYPE_POINTER_TO (expr)
11953 || TYPE_REFERENCE_TO (expr)
11954 || TYPE_CACHED_VALUES_P (expr)
11955 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
11956 || TYPE_NEXT_VARIANT (expr)
11957 || TYPE_ALIAS_SET_KNOWN_P (expr)))
11958 {
11959 /* Allow these fields to be modified. */
11960 tree tmp;
11961 memcpy ((char *) &buf, expr, tree_size (expr));
11962 expr = tmp = (tree) &buf;
11963 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
11964 TYPE_POINTER_TO (tmp) = NULL;
11965 TYPE_REFERENCE_TO (tmp) = NULL;
11966 TYPE_NEXT_VARIANT (tmp) = NULL;
11967 TYPE_ALIAS_SET (tmp) = -1;
11968 if (TYPE_CACHED_VALUES_P (tmp))
11969 {
11970 TYPE_CACHED_VALUES_P (tmp) = 0;
11971 TYPE_CACHED_VALUES (tmp) = NULL;
11972 }
11973 }
11974 md5_process_bytes (expr, tree_size (expr), ctx);
11975 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
11976 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
11977 if (TREE_CODE_CLASS (code) != tcc_type
11978 && TREE_CODE_CLASS (code) != tcc_declaration
11979 && code != TREE_LIST
11980 && code != SSA_NAME
11981 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
11982 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
11983 switch (TREE_CODE_CLASS (code))
11984 {
11985 case tcc_constant:
11986 switch (code)
11987 {
11988 case STRING_CST:
11989 md5_process_bytes (TREE_STRING_POINTER (expr),
11990 TREE_STRING_LENGTH (expr), ctx);
11991 break;
11992 case COMPLEX_CST:
11993 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
11994 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
11995 break;
11996 case VECTOR_CST:
11997 len = vector_cst_encoded_nelts (expr);
11998 for (i = 0; i < len; ++i)
11999 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12000 break;
12001 default:
12002 break;
12003 }
12004 break;
12005 case tcc_exceptional:
12006 switch (code)
12007 {
12008 case TREE_LIST:
12009 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12010 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12011 expr = TREE_CHAIN (expr);
12012 goto recursive_label;
12013 break;
12014 case TREE_VEC:
12015 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12016 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12017 break;
12018 default:
12019 break;
12020 }
12021 break;
12022 case tcc_expression:
12023 case tcc_reference:
12024 case tcc_comparison:
12025 case tcc_unary:
12026 case tcc_binary:
12027 case tcc_statement:
12028 case tcc_vl_exp:
12029 len = TREE_OPERAND_LENGTH (expr);
12030 for (i = 0; i < len; ++i)
12031 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12032 break;
12033 case tcc_declaration:
12034 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12035 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12036 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12037 {
12038 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12039 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12040 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12041 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12042 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12043 }
12044
12045 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12046 {
12047 if (TREE_CODE (expr) == FUNCTION_DECL)
12048 {
12049 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12050 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12051 }
12052 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12053 }
12054 break;
12055 case tcc_type:
12056 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12057 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12058 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12059 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12060 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12061 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12062 if (INTEGRAL_TYPE_P (expr)
12063 || SCALAR_FLOAT_TYPE_P (expr))
12064 {
12065 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12066 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12067 }
12068 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12069 if (TREE_CODE (expr) == RECORD_TYPE
12070 || TREE_CODE (expr) == UNION_TYPE
12071 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12072 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12073 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12074 break;
12075 default:
12076 break;
12077 }
12078 }
12079
12080 /* Helper function for outputting the checksum of a tree T. When
12081 debugging with gdb, you can "define mynext" to be "next" followed
12082 by "call debug_fold_checksum (op0)", then just trace down till the
12083 outputs differ. */
12084
12085 DEBUG_FUNCTION void
12086 debug_fold_checksum (const_tree t)
12087 {
12088 int i;
12089 unsigned char checksum[16];
12090 struct md5_ctx ctx;
12091 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12092
12093 md5_init_ctx (&ctx);
12094 fold_checksum_tree (t, &ctx, &ht);
12095 md5_finish_ctx (&ctx, checksum);
12096 ht.empty ();
12097
12098 for (i = 0; i < 16; i++)
12099 fprintf (stderr, "%d ", checksum[i]);
12100
12101 fprintf (stderr, "\n");
12102 }
12103
12104 #endif
12105
12106 /* Fold a unary tree expression with code CODE of type TYPE with an
12107 operand OP0. LOC is the location of the resulting expression.
12108 Return a folded expression if successful. Otherwise, return a tree
12109 expression with code CODE of type TYPE with an operand OP0. */
12110
12111 tree
12112 fold_build1_loc (location_t loc,
12113 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12114 {
12115 tree tem;
12116 #ifdef ENABLE_FOLD_CHECKING
12117 unsigned char checksum_before[16], checksum_after[16];
12118 struct md5_ctx ctx;
12119 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12120
12121 md5_init_ctx (&ctx);
12122 fold_checksum_tree (op0, &ctx, &ht);
12123 md5_finish_ctx (&ctx, checksum_before);
12124 ht.empty ();
12125 #endif
12126
12127 tem = fold_unary_loc (loc, code, type, op0);
12128 if (!tem)
12129 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12130
12131 #ifdef ENABLE_FOLD_CHECKING
12132 md5_init_ctx (&ctx);
12133 fold_checksum_tree (op0, &ctx, &ht);
12134 md5_finish_ctx (&ctx, checksum_after);
12135
12136 if (memcmp (checksum_before, checksum_after, 16))
12137 fold_check_failed (op0, tem);
12138 #endif
12139 return tem;
12140 }
12141
12142 /* Fold a binary tree expression with code CODE of type TYPE with
12143 operands OP0 and OP1. LOC is the location of the resulting
12144 expression. Return a folded expression if successful. Otherwise,
12145 return a tree expression with code CODE of type TYPE with operands
12146 OP0 and OP1. */
12147
12148 tree
12149 fold_build2_loc (location_t loc,
12150 enum tree_code code, tree type, tree op0, tree op1
12151 MEM_STAT_DECL)
12152 {
12153 tree tem;
12154 #ifdef ENABLE_FOLD_CHECKING
12155 unsigned char checksum_before_op0[16],
12156 checksum_before_op1[16],
12157 checksum_after_op0[16],
12158 checksum_after_op1[16];
12159 struct md5_ctx ctx;
12160 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12161
12162 md5_init_ctx (&ctx);
12163 fold_checksum_tree (op0, &ctx, &ht);
12164 md5_finish_ctx (&ctx, checksum_before_op0);
12165 ht.empty ();
12166
12167 md5_init_ctx (&ctx);
12168 fold_checksum_tree (op1, &ctx, &ht);
12169 md5_finish_ctx (&ctx, checksum_before_op1);
12170 ht.empty ();
12171 #endif
12172
12173 tem = fold_binary_loc (loc, code, type, op0, op1);
12174 if (!tem)
12175 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12176
12177 #ifdef ENABLE_FOLD_CHECKING
12178 md5_init_ctx (&ctx);
12179 fold_checksum_tree (op0, &ctx, &ht);
12180 md5_finish_ctx (&ctx, checksum_after_op0);
12181 ht.empty ();
12182
12183 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12184 fold_check_failed (op0, tem);
12185
12186 md5_init_ctx (&ctx);
12187 fold_checksum_tree (op1, &ctx, &ht);
12188 md5_finish_ctx (&ctx, checksum_after_op1);
12189
12190 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12191 fold_check_failed (op1, tem);
12192 #endif
12193 return tem;
12194 }
12195
12196 /* Fold a ternary tree expression with code CODE of type TYPE with
12197 operands OP0, OP1, and OP2. Return a folded expression if
12198 successful. Otherwise, return a tree expression with code CODE of
12199 type TYPE with operands OP0, OP1, and OP2. */
12200
12201 tree
12202 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12203 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12204 {
12205 tree tem;
12206 #ifdef ENABLE_FOLD_CHECKING
12207 unsigned char checksum_before_op0[16],
12208 checksum_before_op1[16],
12209 checksum_before_op2[16],
12210 checksum_after_op0[16],
12211 checksum_after_op1[16],
12212 checksum_after_op2[16];
12213 struct md5_ctx ctx;
12214 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12215
12216 md5_init_ctx (&ctx);
12217 fold_checksum_tree (op0, &ctx, &ht);
12218 md5_finish_ctx (&ctx, checksum_before_op0);
12219 ht.empty ();
12220
12221 md5_init_ctx (&ctx);
12222 fold_checksum_tree (op1, &ctx, &ht);
12223 md5_finish_ctx (&ctx, checksum_before_op1);
12224 ht.empty ();
12225
12226 md5_init_ctx (&ctx);
12227 fold_checksum_tree (op2, &ctx, &ht);
12228 md5_finish_ctx (&ctx, checksum_before_op2);
12229 ht.empty ();
12230 #endif
12231
12232 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12233 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12234 if (!tem)
12235 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12236
12237 #ifdef ENABLE_FOLD_CHECKING
12238 md5_init_ctx (&ctx);
12239 fold_checksum_tree (op0, &ctx, &ht);
12240 md5_finish_ctx (&ctx, checksum_after_op0);
12241 ht.empty ();
12242
12243 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12244 fold_check_failed (op0, tem);
12245
12246 md5_init_ctx (&ctx);
12247 fold_checksum_tree (op1, &ctx, &ht);
12248 md5_finish_ctx (&ctx, checksum_after_op1);
12249 ht.empty ();
12250
12251 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12252 fold_check_failed (op1, tem);
12253
12254 md5_init_ctx (&ctx);
12255 fold_checksum_tree (op2, &ctx, &ht);
12256 md5_finish_ctx (&ctx, checksum_after_op2);
12257
12258 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12259 fold_check_failed (op2, tem);
12260 #endif
12261 return tem;
12262 }
12263
12264 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12265 arguments in ARGARRAY, and a null static chain.
12266 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12267 of type TYPE from the given operands as constructed by build_call_array. */
12268
12269 tree
12270 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12271 int nargs, tree *argarray)
12272 {
12273 tree tem;
12274 #ifdef ENABLE_FOLD_CHECKING
12275 unsigned char checksum_before_fn[16],
12276 checksum_before_arglist[16],
12277 checksum_after_fn[16],
12278 checksum_after_arglist[16];
12279 struct md5_ctx ctx;
12280 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12281 int i;
12282
12283 md5_init_ctx (&ctx);
12284 fold_checksum_tree (fn, &ctx, &ht);
12285 md5_finish_ctx (&ctx, checksum_before_fn);
12286 ht.empty ();
12287
12288 md5_init_ctx (&ctx);
12289 for (i = 0; i < nargs; i++)
12290 fold_checksum_tree (argarray[i], &ctx, &ht);
12291 md5_finish_ctx (&ctx, checksum_before_arglist);
12292 ht.empty ();
12293 #endif
12294
12295 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12296 if (!tem)
12297 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12298
12299 #ifdef ENABLE_FOLD_CHECKING
12300 md5_init_ctx (&ctx);
12301 fold_checksum_tree (fn, &ctx, &ht);
12302 md5_finish_ctx (&ctx, checksum_after_fn);
12303 ht.empty ();
12304
12305 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12306 fold_check_failed (fn, tem);
12307
12308 md5_init_ctx (&ctx);
12309 for (i = 0; i < nargs; i++)
12310 fold_checksum_tree (argarray[i], &ctx, &ht);
12311 md5_finish_ctx (&ctx, checksum_after_arglist);
12312
12313 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12314 fold_check_failed (NULL_TREE, tem);
12315 #endif
12316 return tem;
12317 }
12318
12319 /* Perform constant folding and related simplification of initializer
12320 expression EXPR. These behave identically to "fold_buildN" but ignore
12321 potential run-time traps and exceptions that fold must preserve. */
12322
12323 #define START_FOLD_INIT \
12324 int saved_signaling_nans = flag_signaling_nans;\
12325 int saved_trapping_math = flag_trapping_math;\
12326 int saved_rounding_math = flag_rounding_math;\
12327 int saved_trapv = flag_trapv;\
12328 int saved_folding_initializer = folding_initializer;\
12329 flag_signaling_nans = 0;\
12330 flag_trapping_math = 0;\
12331 flag_rounding_math = 0;\
12332 flag_trapv = 0;\
12333 folding_initializer = 1;
12334
12335 #define END_FOLD_INIT \
12336 flag_signaling_nans = saved_signaling_nans;\
12337 flag_trapping_math = saved_trapping_math;\
12338 flag_rounding_math = saved_rounding_math;\
12339 flag_trapv = saved_trapv;\
12340 folding_initializer = saved_folding_initializer;
12341
12342 tree
12343 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12344 tree type, tree op)
12345 {
12346 tree result;
12347 START_FOLD_INIT;
12348
12349 result = fold_build1_loc (loc, code, type, op);
12350
12351 END_FOLD_INIT;
12352 return result;
12353 }
12354
12355 tree
12356 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12357 tree type, tree op0, tree op1)
12358 {
12359 tree result;
12360 START_FOLD_INIT;
12361
12362 result = fold_build2_loc (loc, code, type, op0, op1);
12363
12364 END_FOLD_INIT;
12365 return result;
12366 }
12367
12368 tree
12369 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12370 int nargs, tree *argarray)
12371 {
12372 tree result;
12373 START_FOLD_INIT;
12374
12375 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12376
12377 END_FOLD_INIT;
12378 return result;
12379 }
12380
12381 #undef START_FOLD_INIT
12382 #undef END_FOLD_INIT
12383
12384 /* Determine if first argument is a multiple of second argument. Return 0 if
12385 it is not, or we cannot easily determined it to be.
12386
12387 An example of the sort of thing we care about (at this point; this routine
12388 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12389 fold cases do now) is discovering that
12390
12391 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12392
12393 is a multiple of
12394
12395 SAVE_EXPR (J * 8)
12396
12397 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12398
12399 This code also handles discovering that
12400
12401 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12402
12403 is a multiple of 8 so we don't have to worry about dealing with a
12404 possible remainder.
12405
12406 Note that we *look* inside a SAVE_EXPR only to determine how it was
12407 calculated; it is not safe for fold to do much of anything else with the
12408 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12409 at run time. For example, the latter example above *cannot* be implemented
12410 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12411 evaluation time of the original SAVE_EXPR is not necessarily the same at
12412 the time the new expression is evaluated. The only optimization of this
12413 sort that would be valid is changing
12414
12415 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12416
12417 divided by 8 to
12418
12419 SAVE_EXPR (I) * SAVE_EXPR (J)
12420
12421 (where the same SAVE_EXPR (J) is used in the original and the
12422 transformed version). */
12423
12424 int
12425 multiple_of_p (tree type, const_tree top, const_tree bottom)
12426 {
12427 gimple *stmt;
12428 tree t1, op1, op2;
12429
12430 if (operand_equal_p (top, bottom, 0))
12431 return 1;
12432
12433 if (TREE_CODE (type) != INTEGER_TYPE)
12434 return 0;
12435
12436 switch (TREE_CODE (top))
12437 {
12438 case BIT_AND_EXPR:
12439 /* Bitwise and provides a power of two multiple. If the mask is
12440 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12441 if (!integer_pow2p (bottom))
12442 return 0;
12443 /* FALLTHRU */
12444
12445 case MULT_EXPR:
12446 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12447 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12448
12449 case MINUS_EXPR:
12450 /* It is impossible to prove if op0 - op1 is multiple of bottom
12451 precisely, so be conservative here checking if both op0 and op1
12452 are multiple of bottom. Note we check the second operand first
12453 since it's usually simpler. */
12454 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12455 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12456
12457 case PLUS_EXPR:
12458 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12459 as op0 - 3 if the expression has unsigned type. For example,
12460 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12461 op1 = TREE_OPERAND (top, 1);
12462 if (TYPE_UNSIGNED (type)
12463 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12464 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12465 return (multiple_of_p (type, op1, bottom)
12466 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12467
12468 case LSHIFT_EXPR:
12469 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12470 {
12471 op1 = TREE_OPERAND (top, 1);
12472 /* const_binop may not detect overflow correctly,
12473 so check for it explicitly here. */
12474 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12475 wi::to_wide (op1))
12476 && 0 != (t1 = fold_convert (type,
12477 const_binop (LSHIFT_EXPR,
12478 size_one_node,
12479 op1)))
12480 && !TREE_OVERFLOW (t1))
12481 return multiple_of_p (type, t1, bottom);
12482 }
12483 return 0;
12484
12485 case NOP_EXPR:
12486 /* Can't handle conversions from non-integral or wider integral type. */
12487 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12488 || (TYPE_PRECISION (type)
12489 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12490 return 0;
12491
12492 /* fall through */
12493
12494 case SAVE_EXPR:
12495 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12496
12497 case COND_EXPR:
12498 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12499 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12500
12501 case INTEGER_CST:
12502 if (TREE_CODE (bottom) != INTEGER_CST
12503 || integer_zerop (bottom)
12504 || (TYPE_UNSIGNED (type)
12505 && (tree_int_cst_sgn (top) < 0
12506 || tree_int_cst_sgn (bottom) < 0)))
12507 return 0;
12508 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12509 SIGNED);
12510
12511 case SSA_NAME:
12512 if (TREE_CODE (bottom) == INTEGER_CST
12513 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12514 && gimple_code (stmt) == GIMPLE_ASSIGN)
12515 {
12516 enum tree_code code = gimple_assign_rhs_code (stmt);
12517
12518 /* Check for special cases to see if top is defined as multiple
12519 of bottom:
12520
12521 top = (X & ~(bottom - 1) ; bottom is power of 2
12522
12523 or
12524
12525 Y = X % bottom
12526 top = X - Y. */
12527 if (code == BIT_AND_EXPR
12528 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12529 && TREE_CODE (op2) == INTEGER_CST
12530 && integer_pow2p (bottom)
12531 && wi::multiple_of_p (wi::to_widest (op2),
12532 wi::to_widest (bottom), UNSIGNED))
12533 return 1;
12534
12535 op1 = gimple_assign_rhs1 (stmt);
12536 if (code == MINUS_EXPR
12537 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12538 && TREE_CODE (op2) == SSA_NAME
12539 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12540 && gimple_code (stmt) == GIMPLE_ASSIGN
12541 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12542 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12543 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12544 return 1;
12545 }
12546
12547 /* fall through */
12548
12549 default:
12550 return 0;
12551 }
12552 }
12553
12554 #define tree_expr_nonnegative_warnv_p(X, Y) \
12555 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12556
12557 #define RECURSE(X) \
12558 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12559
12560 /* Return true if CODE or TYPE is known to be non-negative. */
12561
12562 static bool
12563 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12564 {
12565 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12566 && truth_value_p (code))
12567 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12568 have a signed:1 type (where the value is -1 and 0). */
12569 return true;
12570 return false;
12571 }
12572
12573 /* Return true if (CODE OP0) is known to be non-negative. If the return
12574 value is based on the assumption that signed overflow is undefined,
12575 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12576 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12577
12578 bool
12579 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12580 bool *strict_overflow_p, int depth)
12581 {
12582 if (TYPE_UNSIGNED (type))
12583 return true;
12584
12585 switch (code)
12586 {
12587 case ABS_EXPR:
12588 /* We can't return 1 if flag_wrapv is set because
12589 ABS_EXPR<INT_MIN> = INT_MIN. */
12590 if (!ANY_INTEGRAL_TYPE_P (type))
12591 return true;
12592 if (TYPE_OVERFLOW_UNDEFINED (type))
12593 {
12594 *strict_overflow_p = true;
12595 return true;
12596 }
12597 break;
12598
12599 case NON_LVALUE_EXPR:
12600 case FLOAT_EXPR:
12601 case FIX_TRUNC_EXPR:
12602 return RECURSE (op0);
12603
12604 CASE_CONVERT:
12605 {
12606 tree inner_type = TREE_TYPE (op0);
12607 tree outer_type = type;
12608
12609 if (TREE_CODE (outer_type) == REAL_TYPE)
12610 {
12611 if (TREE_CODE (inner_type) == REAL_TYPE)
12612 return RECURSE (op0);
12613 if (INTEGRAL_TYPE_P (inner_type))
12614 {
12615 if (TYPE_UNSIGNED (inner_type))
12616 return true;
12617 return RECURSE (op0);
12618 }
12619 }
12620 else if (INTEGRAL_TYPE_P (outer_type))
12621 {
12622 if (TREE_CODE (inner_type) == REAL_TYPE)
12623 return RECURSE (op0);
12624 if (INTEGRAL_TYPE_P (inner_type))
12625 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12626 && TYPE_UNSIGNED (inner_type);
12627 }
12628 }
12629 break;
12630
12631 default:
12632 return tree_simple_nonnegative_warnv_p (code, type);
12633 }
12634
12635 /* We don't know sign of `t', so be conservative and return false. */
12636 return false;
12637 }
12638
12639 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12640 value is based on the assumption that signed overflow is undefined,
12641 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12642 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12643
12644 bool
12645 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12646 tree op1, bool *strict_overflow_p,
12647 int depth)
12648 {
12649 if (TYPE_UNSIGNED (type))
12650 return true;
12651
12652 switch (code)
12653 {
12654 case POINTER_PLUS_EXPR:
12655 case PLUS_EXPR:
12656 if (FLOAT_TYPE_P (type))
12657 return RECURSE (op0) && RECURSE (op1);
12658
12659 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12660 both unsigned and at least 2 bits shorter than the result. */
12661 if (TREE_CODE (type) == INTEGER_TYPE
12662 && TREE_CODE (op0) == NOP_EXPR
12663 && TREE_CODE (op1) == NOP_EXPR)
12664 {
12665 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12666 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12667 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12668 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12669 {
12670 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12671 TYPE_PRECISION (inner2)) + 1;
12672 return prec < TYPE_PRECISION (type);
12673 }
12674 }
12675 break;
12676
12677 case MULT_EXPR:
12678 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12679 {
12680 /* x * x is always non-negative for floating point x
12681 or without overflow. */
12682 if (operand_equal_p (op0, op1, 0)
12683 || (RECURSE (op0) && RECURSE (op1)))
12684 {
12685 if (ANY_INTEGRAL_TYPE_P (type)
12686 && TYPE_OVERFLOW_UNDEFINED (type))
12687 *strict_overflow_p = true;
12688 return true;
12689 }
12690 }
12691
12692 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12693 both unsigned and their total bits is shorter than the result. */
12694 if (TREE_CODE (type) == INTEGER_TYPE
12695 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12696 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12697 {
12698 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12699 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12700 : TREE_TYPE (op0);
12701 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12702 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12703 : TREE_TYPE (op1);
12704
12705 bool unsigned0 = TYPE_UNSIGNED (inner0);
12706 bool unsigned1 = TYPE_UNSIGNED (inner1);
12707
12708 if (TREE_CODE (op0) == INTEGER_CST)
12709 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12710
12711 if (TREE_CODE (op1) == INTEGER_CST)
12712 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12713
12714 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12715 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12716 {
12717 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12718 ? tree_int_cst_min_precision (op0, UNSIGNED)
12719 : TYPE_PRECISION (inner0);
12720
12721 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12722 ? tree_int_cst_min_precision (op1, UNSIGNED)
12723 : TYPE_PRECISION (inner1);
12724
12725 return precision0 + precision1 < TYPE_PRECISION (type);
12726 }
12727 }
12728 return false;
12729
12730 case BIT_AND_EXPR:
12731 case MAX_EXPR:
12732 return RECURSE (op0) || RECURSE (op1);
12733
12734 case BIT_IOR_EXPR:
12735 case BIT_XOR_EXPR:
12736 case MIN_EXPR:
12737 case RDIV_EXPR:
12738 case TRUNC_DIV_EXPR:
12739 case CEIL_DIV_EXPR:
12740 case FLOOR_DIV_EXPR:
12741 case ROUND_DIV_EXPR:
12742 return RECURSE (op0) && RECURSE (op1);
12743
12744 case TRUNC_MOD_EXPR:
12745 return RECURSE (op0);
12746
12747 case FLOOR_MOD_EXPR:
12748 return RECURSE (op1);
12749
12750 case CEIL_MOD_EXPR:
12751 case ROUND_MOD_EXPR:
12752 default:
12753 return tree_simple_nonnegative_warnv_p (code, type);
12754 }
12755
12756 /* We don't know sign of `t', so be conservative and return false. */
12757 return false;
12758 }
12759
12760 /* Return true if T is known to be non-negative. If the return
12761 value is based on the assumption that signed overflow is undefined,
12762 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12763 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12764
12765 bool
12766 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12767 {
12768 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12769 return true;
12770
12771 switch (TREE_CODE (t))
12772 {
12773 case INTEGER_CST:
12774 return tree_int_cst_sgn (t) >= 0;
12775
12776 case REAL_CST:
12777 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12778
12779 case FIXED_CST:
12780 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12781
12782 case COND_EXPR:
12783 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12784
12785 case SSA_NAME:
12786 /* Limit the depth of recursion to avoid quadratic behavior.
12787 This is expected to catch almost all occurrences in practice.
12788 If this code misses important cases that unbounded recursion
12789 would not, passes that need this information could be revised
12790 to provide it through dataflow propagation. */
12791 return (!name_registered_for_update_p (t)
12792 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12793 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12794 strict_overflow_p, depth));
12795
12796 default:
12797 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12798 }
12799 }
12800
12801 /* Return true if T is known to be non-negative. If the return
12802 value is based on the assumption that signed overflow is undefined,
12803 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12804 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12805
12806 bool
12807 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12808 bool *strict_overflow_p, int depth)
12809 {
12810 switch (fn)
12811 {
12812 CASE_CFN_ACOS:
12813 CASE_CFN_ACOSH:
12814 CASE_CFN_CABS:
12815 CASE_CFN_COSH:
12816 CASE_CFN_ERFC:
12817 CASE_CFN_EXP:
12818 CASE_CFN_EXP10:
12819 CASE_CFN_EXP2:
12820 CASE_CFN_FABS:
12821 CASE_CFN_FDIM:
12822 CASE_CFN_HYPOT:
12823 CASE_CFN_POW10:
12824 CASE_CFN_FFS:
12825 CASE_CFN_PARITY:
12826 CASE_CFN_POPCOUNT:
12827 CASE_CFN_CLZ:
12828 CASE_CFN_CLRSB:
12829 case CFN_BUILT_IN_BSWAP32:
12830 case CFN_BUILT_IN_BSWAP64:
12831 /* Always true. */
12832 return true;
12833
12834 CASE_CFN_SQRT:
12835 CASE_CFN_SQRT_FN:
12836 /* sqrt(-0.0) is -0.0. */
12837 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12838 return true;
12839 return RECURSE (arg0);
12840
12841 CASE_CFN_ASINH:
12842 CASE_CFN_ATAN:
12843 CASE_CFN_ATANH:
12844 CASE_CFN_CBRT:
12845 CASE_CFN_CEIL:
12846 CASE_CFN_ERF:
12847 CASE_CFN_EXPM1:
12848 CASE_CFN_FLOOR:
12849 CASE_CFN_FMOD:
12850 CASE_CFN_FREXP:
12851 CASE_CFN_ICEIL:
12852 CASE_CFN_IFLOOR:
12853 CASE_CFN_IRINT:
12854 CASE_CFN_IROUND:
12855 CASE_CFN_LCEIL:
12856 CASE_CFN_LDEXP:
12857 CASE_CFN_LFLOOR:
12858 CASE_CFN_LLCEIL:
12859 CASE_CFN_LLFLOOR:
12860 CASE_CFN_LLRINT:
12861 CASE_CFN_LLROUND:
12862 CASE_CFN_LRINT:
12863 CASE_CFN_LROUND:
12864 CASE_CFN_MODF:
12865 CASE_CFN_NEARBYINT:
12866 CASE_CFN_RINT:
12867 CASE_CFN_ROUND:
12868 CASE_CFN_SCALB:
12869 CASE_CFN_SCALBLN:
12870 CASE_CFN_SCALBN:
12871 CASE_CFN_SIGNBIT:
12872 CASE_CFN_SIGNIFICAND:
12873 CASE_CFN_SINH:
12874 CASE_CFN_TANH:
12875 CASE_CFN_TRUNC:
12876 /* True if the 1st argument is nonnegative. */
12877 return RECURSE (arg0);
12878
12879 CASE_CFN_FMAX:
12880 CASE_CFN_FMAX_FN:
12881 /* True if the 1st OR 2nd arguments are nonnegative. */
12882 return RECURSE (arg0) || RECURSE (arg1);
12883
12884 CASE_CFN_FMIN:
12885 CASE_CFN_FMIN_FN:
12886 /* True if the 1st AND 2nd arguments are nonnegative. */
12887 return RECURSE (arg0) && RECURSE (arg1);
12888
12889 CASE_CFN_COPYSIGN:
12890 CASE_CFN_COPYSIGN_FN:
12891 /* True if the 2nd argument is nonnegative. */
12892 return RECURSE (arg1);
12893
12894 CASE_CFN_POWI:
12895 /* True if the 1st argument is nonnegative or the second
12896 argument is an even integer. */
12897 if (TREE_CODE (arg1) == INTEGER_CST
12898 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
12899 return true;
12900 return RECURSE (arg0);
12901
12902 CASE_CFN_POW:
12903 /* True if the 1st argument is nonnegative or the second
12904 argument is an even integer valued real. */
12905 if (TREE_CODE (arg1) == REAL_CST)
12906 {
12907 REAL_VALUE_TYPE c;
12908 HOST_WIDE_INT n;
12909
12910 c = TREE_REAL_CST (arg1);
12911 n = real_to_integer (&c);
12912 if ((n & 1) == 0)
12913 {
12914 REAL_VALUE_TYPE cint;
12915 real_from_integer (&cint, VOIDmode, n, SIGNED);
12916 if (real_identical (&c, &cint))
12917 return true;
12918 }
12919 }
12920 return RECURSE (arg0);
12921
12922 default:
12923 break;
12924 }
12925 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
12926 }
12927
12928 /* Return true if T is known to be non-negative. If the return
12929 value is based on the assumption that signed overflow is undefined,
12930 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12931 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12932
12933 static bool
12934 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12935 {
12936 enum tree_code code = TREE_CODE (t);
12937 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12938 return true;
12939
12940 switch (code)
12941 {
12942 case TARGET_EXPR:
12943 {
12944 tree temp = TARGET_EXPR_SLOT (t);
12945 t = TARGET_EXPR_INITIAL (t);
12946
12947 /* If the initializer is non-void, then it's a normal expression
12948 that will be assigned to the slot. */
12949 if (!VOID_TYPE_P (t))
12950 return RECURSE (t);
12951
12952 /* Otherwise, the initializer sets the slot in some way. One common
12953 way is an assignment statement at the end of the initializer. */
12954 while (1)
12955 {
12956 if (TREE_CODE (t) == BIND_EXPR)
12957 t = expr_last (BIND_EXPR_BODY (t));
12958 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
12959 || TREE_CODE (t) == TRY_CATCH_EXPR)
12960 t = expr_last (TREE_OPERAND (t, 0));
12961 else if (TREE_CODE (t) == STATEMENT_LIST)
12962 t = expr_last (t);
12963 else
12964 break;
12965 }
12966 if (TREE_CODE (t) == MODIFY_EXPR
12967 && TREE_OPERAND (t, 0) == temp)
12968 return RECURSE (TREE_OPERAND (t, 1));
12969
12970 return false;
12971 }
12972
12973 case CALL_EXPR:
12974 {
12975 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
12976 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
12977
12978 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
12979 get_call_combined_fn (t),
12980 arg0,
12981 arg1,
12982 strict_overflow_p, depth);
12983 }
12984 case COMPOUND_EXPR:
12985 case MODIFY_EXPR:
12986 return RECURSE (TREE_OPERAND (t, 1));
12987
12988 case BIND_EXPR:
12989 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
12990
12991 case SAVE_EXPR:
12992 return RECURSE (TREE_OPERAND (t, 0));
12993
12994 default:
12995 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12996 }
12997 }
12998
12999 #undef RECURSE
13000 #undef tree_expr_nonnegative_warnv_p
13001
13002 /* Return true if T is known to be non-negative. If the return
13003 value is based on the assumption that signed overflow is undefined,
13004 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13005 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13006
13007 bool
13008 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13009 {
13010 enum tree_code code;
13011 if (t == error_mark_node)
13012 return false;
13013
13014 code = TREE_CODE (t);
13015 switch (TREE_CODE_CLASS (code))
13016 {
13017 case tcc_binary:
13018 case tcc_comparison:
13019 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13020 TREE_TYPE (t),
13021 TREE_OPERAND (t, 0),
13022 TREE_OPERAND (t, 1),
13023 strict_overflow_p, depth);
13024
13025 case tcc_unary:
13026 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13027 TREE_TYPE (t),
13028 TREE_OPERAND (t, 0),
13029 strict_overflow_p, depth);
13030
13031 case tcc_constant:
13032 case tcc_declaration:
13033 case tcc_reference:
13034 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13035
13036 default:
13037 break;
13038 }
13039
13040 switch (code)
13041 {
13042 case TRUTH_AND_EXPR:
13043 case TRUTH_OR_EXPR:
13044 case TRUTH_XOR_EXPR:
13045 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13046 TREE_TYPE (t),
13047 TREE_OPERAND (t, 0),
13048 TREE_OPERAND (t, 1),
13049 strict_overflow_p, depth);
13050 case TRUTH_NOT_EXPR:
13051 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13052 TREE_TYPE (t),
13053 TREE_OPERAND (t, 0),
13054 strict_overflow_p, depth);
13055
13056 case COND_EXPR:
13057 case CONSTRUCTOR:
13058 case OBJ_TYPE_REF:
13059 case ASSERT_EXPR:
13060 case ADDR_EXPR:
13061 case WITH_SIZE_EXPR:
13062 case SSA_NAME:
13063 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13064
13065 default:
13066 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13067 }
13068 }
13069
13070 /* Return true if `t' is known to be non-negative. Handle warnings
13071 about undefined signed overflow. */
13072
13073 bool
13074 tree_expr_nonnegative_p (tree t)
13075 {
13076 bool ret, strict_overflow_p;
13077
13078 strict_overflow_p = false;
13079 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13080 if (strict_overflow_p)
13081 fold_overflow_warning (("assuming signed overflow does not occur when "
13082 "determining that expression is always "
13083 "non-negative"),
13084 WARN_STRICT_OVERFLOW_MISC);
13085 return ret;
13086 }
13087
13088
13089 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13090 For floating point we further ensure that T is not denormal.
13091 Similar logic is present in nonzero_address in rtlanal.h.
13092
13093 If the return value is based on the assumption that signed overflow
13094 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13095 change *STRICT_OVERFLOW_P. */
13096
13097 bool
13098 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13099 bool *strict_overflow_p)
13100 {
13101 switch (code)
13102 {
13103 case ABS_EXPR:
13104 return tree_expr_nonzero_warnv_p (op0,
13105 strict_overflow_p);
13106
13107 case NOP_EXPR:
13108 {
13109 tree inner_type = TREE_TYPE (op0);
13110 tree outer_type = type;
13111
13112 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13113 && tree_expr_nonzero_warnv_p (op0,
13114 strict_overflow_p));
13115 }
13116 break;
13117
13118 case NON_LVALUE_EXPR:
13119 return tree_expr_nonzero_warnv_p (op0,
13120 strict_overflow_p);
13121
13122 default:
13123 break;
13124 }
13125
13126 return false;
13127 }
13128
13129 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13130 For floating point we further ensure that T is not denormal.
13131 Similar logic is present in nonzero_address in rtlanal.h.
13132
13133 If the return value is based on the assumption that signed overflow
13134 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13135 change *STRICT_OVERFLOW_P. */
13136
13137 bool
13138 tree_binary_nonzero_warnv_p (enum tree_code code,
13139 tree type,
13140 tree op0,
13141 tree op1, bool *strict_overflow_p)
13142 {
13143 bool sub_strict_overflow_p;
13144 switch (code)
13145 {
13146 case POINTER_PLUS_EXPR:
13147 case PLUS_EXPR:
13148 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13149 {
13150 /* With the presence of negative values it is hard
13151 to say something. */
13152 sub_strict_overflow_p = false;
13153 if (!tree_expr_nonnegative_warnv_p (op0,
13154 &sub_strict_overflow_p)
13155 || !tree_expr_nonnegative_warnv_p (op1,
13156 &sub_strict_overflow_p))
13157 return false;
13158 /* One of operands must be positive and the other non-negative. */
13159 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13160 overflows, on a twos-complement machine the sum of two
13161 nonnegative numbers can never be zero. */
13162 return (tree_expr_nonzero_warnv_p (op0,
13163 strict_overflow_p)
13164 || tree_expr_nonzero_warnv_p (op1,
13165 strict_overflow_p));
13166 }
13167 break;
13168
13169 case MULT_EXPR:
13170 if (TYPE_OVERFLOW_UNDEFINED (type))
13171 {
13172 if (tree_expr_nonzero_warnv_p (op0,
13173 strict_overflow_p)
13174 && tree_expr_nonzero_warnv_p (op1,
13175 strict_overflow_p))
13176 {
13177 *strict_overflow_p = true;
13178 return true;
13179 }
13180 }
13181 break;
13182
13183 case MIN_EXPR:
13184 sub_strict_overflow_p = false;
13185 if (tree_expr_nonzero_warnv_p (op0,
13186 &sub_strict_overflow_p)
13187 && tree_expr_nonzero_warnv_p (op1,
13188 &sub_strict_overflow_p))
13189 {
13190 if (sub_strict_overflow_p)
13191 *strict_overflow_p = true;
13192 }
13193 break;
13194
13195 case MAX_EXPR:
13196 sub_strict_overflow_p = false;
13197 if (tree_expr_nonzero_warnv_p (op0,
13198 &sub_strict_overflow_p))
13199 {
13200 if (sub_strict_overflow_p)
13201 *strict_overflow_p = true;
13202
13203 /* When both operands are nonzero, then MAX must be too. */
13204 if (tree_expr_nonzero_warnv_p (op1,
13205 strict_overflow_p))
13206 return true;
13207
13208 /* MAX where operand 0 is positive is positive. */
13209 return tree_expr_nonnegative_warnv_p (op0,
13210 strict_overflow_p);
13211 }
13212 /* MAX where operand 1 is positive is positive. */
13213 else if (tree_expr_nonzero_warnv_p (op1,
13214 &sub_strict_overflow_p)
13215 && tree_expr_nonnegative_warnv_p (op1,
13216 &sub_strict_overflow_p))
13217 {
13218 if (sub_strict_overflow_p)
13219 *strict_overflow_p = true;
13220 return true;
13221 }
13222 break;
13223
13224 case BIT_IOR_EXPR:
13225 return (tree_expr_nonzero_warnv_p (op1,
13226 strict_overflow_p)
13227 || tree_expr_nonzero_warnv_p (op0,
13228 strict_overflow_p));
13229
13230 default:
13231 break;
13232 }
13233
13234 return false;
13235 }
13236
13237 /* Return true when T is an address and is known to be nonzero.
13238 For floating point we further ensure that T is not denormal.
13239 Similar logic is present in nonzero_address in rtlanal.h.
13240
13241 If the return value is based on the assumption that signed overflow
13242 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13243 change *STRICT_OVERFLOW_P. */
13244
13245 bool
13246 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13247 {
13248 bool sub_strict_overflow_p;
13249 switch (TREE_CODE (t))
13250 {
13251 case INTEGER_CST:
13252 return !integer_zerop (t);
13253
13254 case ADDR_EXPR:
13255 {
13256 tree base = TREE_OPERAND (t, 0);
13257
13258 if (!DECL_P (base))
13259 base = get_base_address (base);
13260
13261 if (base && TREE_CODE (base) == TARGET_EXPR)
13262 base = TARGET_EXPR_SLOT (base);
13263
13264 if (!base)
13265 return false;
13266
13267 /* For objects in symbol table check if we know they are non-zero.
13268 Don't do anything for variables and functions before symtab is built;
13269 it is quite possible that they will be declared weak later. */
13270 int nonzero_addr = maybe_nonzero_address (base);
13271 if (nonzero_addr >= 0)
13272 return nonzero_addr;
13273
13274 /* Constants are never weak. */
13275 if (CONSTANT_CLASS_P (base))
13276 return true;
13277
13278 return false;
13279 }
13280
13281 case COND_EXPR:
13282 sub_strict_overflow_p = false;
13283 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13284 &sub_strict_overflow_p)
13285 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13286 &sub_strict_overflow_p))
13287 {
13288 if (sub_strict_overflow_p)
13289 *strict_overflow_p = true;
13290 return true;
13291 }
13292 break;
13293
13294 case SSA_NAME:
13295 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13296 break;
13297 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13298
13299 default:
13300 break;
13301 }
13302 return false;
13303 }
13304
13305 #define integer_valued_real_p(X) \
13306 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13307
13308 #define RECURSE(X) \
13309 ((integer_valued_real_p) (X, depth + 1))
13310
13311 /* Return true if the floating point result of (CODE OP0) has an
13312 integer value. We also allow +Inf, -Inf and NaN to be considered
13313 integer values. Return false for signaling NaN.
13314
13315 DEPTH is the current nesting depth of the query. */
13316
13317 bool
13318 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13319 {
13320 switch (code)
13321 {
13322 case FLOAT_EXPR:
13323 return true;
13324
13325 case ABS_EXPR:
13326 return RECURSE (op0);
13327
13328 CASE_CONVERT:
13329 {
13330 tree type = TREE_TYPE (op0);
13331 if (TREE_CODE (type) == INTEGER_TYPE)
13332 return true;
13333 if (TREE_CODE (type) == REAL_TYPE)
13334 return RECURSE (op0);
13335 break;
13336 }
13337
13338 default:
13339 break;
13340 }
13341 return false;
13342 }
13343
13344 /* Return true if the floating point result of (CODE OP0 OP1) has an
13345 integer value. We also allow +Inf, -Inf and NaN to be considered
13346 integer values. Return false for signaling NaN.
13347
13348 DEPTH is the current nesting depth of the query. */
13349
13350 bool
13351 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13352 {
13353 switch (code)
13354 {
13355 case PLUS_EXPR:
13356 case MINUS_EXPR:
13357 case MULT_EXPR:
13358 case MIN_EXPR:
13359 case MAX_EXPR:
13360 return RECURSE (op0) && RECURSE (op1);
13361
13362 default:
13363 break;
13364 }
13365 return false;
13366 }
13367
13368 /* Return true if the floating point result of calling FNDECL with arguments
13369 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13370 considered integer values. Return false for signaling NaN. If FNDECL
13371 takes fewer than 2 arguments, the remaining ARGn are null.
13372
13373 DEPTH is the current nesting depth of the query. */
13374
13375 bool
13376 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13377 {
13378 switch (fn)
13379 {
13380 CASE_CFN_CEIL:
13381 CASE_CFN_FLOOR:
13382 CASE_CFN_NEARBYINT:
13383 CASE_CFN_RINT:
13384 CASE_CFN_ROUND:
13385 CASE_CFN_TRUNC:
13386 return true;
13387
13388 CASE_CFN_FMIN:
13389 CASE_CFN_FMIN_FN:
13390 CASE_CFN_FMAX:
13391 CASE_CFN_FMAX_FN:
13392 return RECURSE (arg0) && RECURSE (arg1);
13393
13394 default:
13395 break;
13396 }
13397 return false;
13398 }
13399
13400 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13401 has an integer value. We also allow +Inf, -Inf and NaN to be
13402 considered integer values. Return false for signaling NaN.
13403
13404 DEPTH is the current nesting depth of the query. */
13405
13406 bool
13407 integer_valued_real_single_p (tree t, int depth)
13408 {
13409 switch (TREE_CODE (t))
13410 {
13411 case REAL_CST:
13412 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13413
13414 case COND_EXPR:
13415 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13416
13417 case SSA_NAME:
13418 /* Limit the depth of recursion to avoid quadratic behavior.
13419 This is expected to catch almost all occurrences in practice.
13420 If this code misses important cases that unbounded recursion
13421 would not, passes that need this information could be revised
13422 to provide it through dataflow propagation. */
13423 return (!name_registered_for_update_p (t)
13424 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13425 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13426 depth));
13427
13428 default:
13429 break;
13430 }
13431 return false;
13432 }
13433
13434 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13435 has an integer value. We also allow +Inf, -Inf and NaN to be
13436 considered integer values. Return false for signaling NaN.
13437
13438 DEPTH is the current nesting depth of the query. */
13439
13440 static bool
13441 integer_valued_real_invalid_p (tree t, int depth)
13442 {
13443 switch (TREE_CODE (t))
13444 {
13445 case COMPOUND_EXPR:
13446 case MODIFY_EXPR:
13447 case BIND_EXPR:
13448 return RECURSE (TREE_OPERAND (t, 1));
13449
13450 case SAVE_EXPR:
13451 return RECURSE (TREE_OPERAND (t, 0));
13452
13453 default:
13454 break;
13455 }
13456 return false;
13457 }
13458
13459 #undef RECURSE
13460 #undef integer_valued_real_p
13461
13462 /* Return true if the floating point expression T has an integer value.
13463 We also allow +Inf, -Inf and NaN to be considered integer values.
13464 Return false for signaling NaN.
13465
13466 DEPTH is the current nesting depth of the query. */
13467
13468 bool
13469 integer_valued_real_p (tree t, int depth)
13470 {
13471 if (t == error_mark_node)
13472 return false;
13473
13474 tree_code code = TREE_CODE (t);
13475 switch (TREE_CODE_CLASS (code))
13476 {
13477 case tcc_binary:
13478 case tcc_comparison:
13479 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13480 TREE_OPERAND (t, 1), depth);
13481
13482 case tcc_unary:
13483 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13484
13485 case tcc_constant:
13486 case tcc_declaration:
13487 case tcc_reference:
13488 return integer_valued_real_single_p (t, depth);
13489
13490 default:
13491 break;
13492 }
13493
13494 switch (code)
13495 {
13496 case COND_EXPR:
13497 case SSA_NAME:
13498 return integer_valued_real_single_p (t, depth);
13499
13500 case CALL_EXPR:
13501 {
13502 tree arg0 = (call_expr_nargs (t) > 0
13503 ? CALL_EXPR_ARG (t, 0)
13504 : NULL_TREE);
13505 tree arg1 = (call_expr_nargs (t) > 1
13506 ? CALL_EXPR_ARG (t, 1)
13507 : NULL_TREE);
13508 return integer_valued_real_call_p (get_call_combined_fn (t),
13509 arg0, arg1, depth);
13510 }
13511
13512 default:
13513 return integer_valued_real_invalid_p (t, depth);
13514 }
13515 }
13516
13517 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13518 attempt to fold the expression to a constant without modifying TYPE,
13519 OP0 or OP1.
13520
13521 If the expression could be simplified to a constant, then return
13522 the constant. If the expression would not be simplified to a
13523 constant, then return NULL_TREE. */
13524
13525 tree
13526 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13527 {
13528 tree tem = fold_binary (code, type, op0, op1);
13529 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13530 }
13531
13532 /* Given the components of a unary expression CODE, TYPE and OP0,
13533 attempt to fold the expression to a constant without modifying
13534 TYPE or OP0.
13535
13536 If the expression could be simplified to a constant, then return
13537 the constant. If the expression would not be simplified to a
13538 constant, then return NULL_TREE. */
13539
13540 tree
13541 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13542 {
13543 tree tem = fold_unary (code, type, op0);
13544 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13545 }
13546
13547 /* If EXP represents referencing an element in a constant string
13548 (either via pointer arithmetic or array indexing), return the
13549 tree representing the value accessed, otherwise return NULL. */
13550
13551 tree
13552 fold_read_from_constant_string (tree exp)
13553 {
13554 if ((TREE_CODE (exp) == INDIRECT_REF
13555 || TREE_CODE (exp) == ARRAY_REF)
13556 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13557 {
13558 tree exp1 = TREE_OPERAND (exp, 0);
13559 tree index;
13560 tree string;
13561 location_t loc = EXPR_LOCATION (exp);
13562
13563 if (TREE_CODE (exp) == INDIRECT_REF)
13564 string = string_constant (exp1, &index);
13565 else
13566 {
13567 tree low_bound = array_ref_low_bound (exp);
13568 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13569
13570 /* Optimize the special-case of a zero lower bound.
13571
13572 We convert the low_bound to sizetype to avoid some problems
13573 with constant folding. (E.g. suppose the lower bound is 1,
13574 and its mode is QI. Without the conversion,l (ARRAY
13575 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13576 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13577 if (! integer_zerop (low_bound))
13578 index = size_diffop_loc (loc, index,
13579 fold_convert_loc (loc, sizetype, low_bound));
13580
13581 string = exp1;
13582 }
13583
13584 scalar_int_mode char_mode;
13585 if (string
13586 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13587 && TREE_CODE (string) == STRING_CST
13588 && TREE_CODE (index) == INTEGER_CST
13589 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13590 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13591 &char_mode)
13592 && GET_MODE_SIZE (char_mode) == 1)
13593 return build_int_cst_type (TREE_TYPE (exp),
13594 (TREE_STRING_POINTER (string)
13595 [TREE_INT_CST_LOW (index)]));
13596 }
13597 return NULL;
13598 }
13599
13600 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13601 an integer constant, real, or fixed-point constant.
13602
13603 TYPE is the type of the result. */
13604
13605 static tree
13606 fold_negate_const (tree arg0, tree type)
13607 {
13608 tree t = NULL_TREE;
13609
13610 switch (TREE_CODE (arg0))
13611 {
13612 case INTEGER_CST:
13613 {
13614 bool overflow;
13615 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13616 t = force_fit_type (type, val, 1,
13617 (overflow && ! TYPE_UNSIGNED (type))
13618 || TREE_OVERFLOW (arg0));
13619 break;
13620 }
13621
13622 case REAL_CST:
13623 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13624 break;
13625
13626 case FIXED_CST:
13627 {
13628 FIXED_VALUE_TYPE f;
13629 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13630 &(TREE_FIXED_CST (arg0)), NULL,
13631 TYPE_SATURATING (type));
13632 t = build_fixed (type, f);
13633 /* Propagate overflow flags. */
13634 if (overflow_p | TREE_OVERFLOW (arg0))
13635 TREE_OVERFLOW (t) = 1;
13636 break;
13637 }
13638
13639 default:
13640 gcc_unreachable ();
13641 }
13642
13643 return t;
13644 }
13645
13646 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13647 an integer constant or real constant.
13648
13649 TYPE is the type of the result. */
13650
13651 tree
13652 fold_abs_const (tree arg0, tree type)
13653 {
13654 tree t = NULL_TREE;
13655
13656 switch (TREE_CODE (arg0))
13657 {
13658 case INTEGER_CST:
13659 {
13660 /* If the value is unsigned or non-negative, then the absolute value
13661 is the same as the ordinary value. */
13662 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13663 t = arg0;
13664
13665 /* If the value is negative, then the absolute value is
13666 its negation. */
13667 else
13668 {
13669 bool overflow;
13670 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13671 t = force_fit_type (type, val, -1,
13672 overflow | TREE_OVERFLOW (arg0));
13673 }
13674 }
13675 break;
13676
13677 case REAL_CST:
13678 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13679 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13680 else
13681 t = arg0;
13682 break;
13683
13684 default:
13685 gcc_unreachable ();
13686 }
13687
13688 return t;
13689 }
13690
13691 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13692 constant. TYPE is the type of the result. */
13693
13694 static tree
13695 fold_not_const (const_tree arg0, tree type)
13696 {
13697 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13698
13699 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13700 }
13701
13702 /* Given CODE, a relational operator, the target type, TYPE and two
13703 constant operands OP0 and OP1, return the result of the
13704 relational operation. If the result is not a compile time
13705 constant, then return NULL_TREE. */
13706
13707 static tree
13708 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13709 {
13710 int result, invert;
13711
13712 /* From here on, the only cases we handle are when the result is
13713 known to be a constant. */
13714
13715 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13716 {
13717 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13718 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13719
13720 /* Handle the cases where either operand is a NaN. */
13721 if (real_isnan (c0) || real_isnan (c1))
13722 {
13723 switch (code)
13724 {
13725 case EQ_EXPR:
13726 case ORDERED_EXPR:
13727 result = 0;
13728 break;
13729
13730 case NE_EXPR:
13731 case UNORDERED_EXPR:
13732 case UNLT_EXPR:
13733 case UNLE_EXPR:
13734 case UNGT_EXPR:
13735 case UNGE_EXPR:
13736 case UNEQ_EXPR:
13737 result = 1;
13738 break;
13739
13740 case LT_EXPR:
13741 case LE_EXPR:
13742 case GT_EXPR:
13743 case GE_EXPR:
13744 case LTGT_EXPR:
13745 if (flag_trapping_math)
13746 return NULL_TREE;
13747 result = 0;
13748 break;
13749
13750 default:
13751 gcc_unreachable ();
13752 }
13753
13754 return constant_boolean_node (result, type);
13755 }
13756
13757 return constant_boolean_node (real_compare (code, c0, c1), type);
13758 }
13759
13760 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13761 {
13762 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13763 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13764 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13765 }
13766
13767 /* Handle equality/inequality of complex constants. */
13768 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13769 {
13770 tree rcond = fold_relational_const (code, type,
13771 TREE_REALPART (op0),
13772 TREE_REALPART (op1));
13773 tree icond = fold_relational_const (code, type,
13774 TREE_IMAGPART (op0),
13775 TREE_IMAGPART (op1));
13776 if (code == EQ_EXPR)
13777 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13778 else if (code == NE_EXPR)
13779 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13780 else
13781 return NULL_TREE;
13782 }
13783
13784 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13785 {
13786 if (!VECTOR_TYPE_P (type))
13787 {
13788 /* Have vector comparison with scalar boolean result. */
13789 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13790 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13791 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13792 {
13793 tree elem0 = VECTOR_CST_ELT (op0, i);
13794 tree elem1 = VECTOR_CST_ELT (op1, i);
13795 tree tmp = fold_relational_const (code, type, elem0, elem1);
13796 if (tmp == NULL_TREE)
13797 return NULL_TREE;
13798 if (integer_zerop (tmp))
13799 return constant_boolean_node (false, type);
13800 }
13801 return constant_boolean_node (true, type);
13802 }
13803 tree_vector_builder elts;
13804 if (!elts.new_binary_operation (type, op0, op1, false))
13805 return NULL_TREE;
13806 unsigned int count = elts.encoded_nelts ();
13807 for (unsigned i = 0; i < count; i++)
13808 {
13809 tree elem_type = TREE_TYPE (type);
13810 tree elem0 = VECTOR_CST_ELT (op0, i);
13811 tree elem1 = VECTOR_CST_ELT (op1, i);
13812
13813 tree tem = fold_relational_const (code, elem_type,
13814 elem0, elem1);
13815
13816 if (tem == NULL_TREE)
13817 return NULL_TREE;
13818
13819 elts.quick_push (build_int_cst (elem_type,
13820 integer_zerop (tem) ? 0 : -1));
13821 }
13822
13823 return elts.build ();
13824 }
13825
13826 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13827
13828 To compute GT, swap the arguments and do LT.
13829 To compute GE, do LT and invert the result.
13830 To compute LE, swap the arguments, do LT and invert the result.
13831 To compute NE, do EQ and invert the result.
13832
13833 Therefore, the code below must handle only EQ and LT. */
13834
13835 if (code == LE_EXPR || code == GT_EXPR)
13836 {
13837 std::swap (op0, op1);
13838 code = swap_tree_comparison (code);
13839 }
13840
13841 /* Note that it is safe to invert for real values here because we
13842 have already handled the one case that it matters. */
13843
13844 invert = 0;
13845 if (code == NE_EXPR || code == GE_EXPR)
13846 {
13847 invert = 1;
13848 code = invert_tree_comparison (code, false);
13849 }
13850
13851 /* Compute a result for LT or EQ if args permit;
13852 Otherwise return T. */
13853 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13854 {
13855 if (code == EQ_EXPR)
13856 result = tree_int_cst_equal (op0, op1);
13857 else
13858 result = tree_int_cst_lt (op0, op1);
13859 }
13860 else
13861 return NULL_TREE;
13862
13863 if (invert)
13864 result ^= 1;
13865 return constant_boolean_node (result, type);
13866 }
13867
13868 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13869 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13870 itself. */
13871
13872 tree
13873 fold_build_cleanup_point_expr (tree type, tree expr)
13874 {
13875 /* If the expression does not have side effects then we don't have to wrap
13876 it with a cleanup point expression. */
13877 if (!TREE_SIDE_EFFECTS (expr))
13878 return expr;
13879
13880 /* If the expression is a return, check to see if the expression inside the
13881 return has no side effects or the right hand side of the modify expression
13882 inside the return. If either don't have side effects set we don't need to
13883 wrap the expression in a cleanup point expression. Note we don't check the
13884 left hand side of the modify because it should always be a return decl. */
13885 if (TREE_CODE (expr) == RETURN_EXPR)
13886 {
13887 tree op = TREE_OPERAND (expr, 0);
13888 if (!op || !TREE_SIDE_EFFECTS (op))
13889 return expr;
13890 op = TREE_OPERAND (op, 1);
13891 if (!TREE_SIDE_EFFECTS (op))
13892 return expr;
13893 }
13894
13895 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
13896 }
13897
13898 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13899 of an indirection through OP0, or NULL_TREE if no simplification is
13900 possible. */
13901
13902 tree
13903 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13904 {
13905 tree sub = op0;
13906 tree subtype;
13907
13908 STRIP_NOPS (sub);
13909 subtype = TREE_TYPE (sub);
13910 if (!POINTER_TYPE_P (subtype)
13911 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
13912 return NULL_TREE;
13913
13914 if (TREE_CODE (sub) == ADDR_EXPR)
13915 {
13916 tree op = TREE_OPERAND (sub, 0);
13917 tree optype = TREE_TYPE (op);
13918 /* *&CONST_DECL -> to the value of the const decl. */
13919 if (TREE_CODE (op) == CONST_DECL)
13920 return DECL_INITIAL (op);
13921 /* *&p => p; make sure to handle *&"str"[cst] here. */
13922 if (type == optype)
13923 {
13924 tree fop = fold_read_from_constant_string (op);
13925 if (fop)
13926 return fop;
13927 else
13928 return op;
13929 }
13930 /* *(foo *)&fooarray => fooarray[0] */
13931 else if (TREE_CODE (optype) == ARRAY_TYPE
13932 && type == TREE_TYPE (optype)
13933 && (!in_gimple_form
13934 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13935 {
13936 tree type_domain = TYPE_DOMAIN (optype);
13937 tree min_val = size_zero_node;
13938 if (type_domain && TYPE_MIN_VALUE (type_domain))
13939 min_val = TYPE_MIN_VALUE (type_domain);
13940 if (in_gimple_form
13941 && TREE_CODE (min_val) != INTEGER_CST)
13942 return NULL_TREE;
13943 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13944 NULL_TREE, NULL_TREE);
13945 }
13946 /* *(foo *)&complexfoo => __real__ complexfoo */
13947 else if (TREE_CODE (optype) == COMPLEX_TYPE
13948 && type == TREE_TYPE (optype))
13949 return fold_build1_loc (loc, REALPART_EXPR, type, op);
13950 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13951 else if (TREE_CODE (optype) == VECTOR_TYPE
13952 && type == TREE_TYPE (optype))
13953 {
13954 tree part_width = TYPE_SIZE (type);
13955 tree index = bitsize_int (0);
13956 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
13957 }
13958 }
13959
13960 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
13961 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13962 {
13963 tree op00 = TREE_OPERAND (sub, 0);
13964 tree op01 = TREE_OPERAND (sub, 1);
13965
13966 STRIP_NOPS (op00);
13967 if (TREE_CODE (op00) == ADDR_EXPR)
13968 {
13969 tree op00type;
13970 op00 = TREE_OPERAND (op00, 0);
13971 op00type = TREE_TYPE (op00);
13972
13973 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
13974 if (TREE_CODE (op00type) == VECTOR_TYPE
13975 && type == TREE_TYPE (op00type))
13976 {
13977 tree part_width = TYPE_SIZE (type);
13978 unsigned HOST_WIDE_INT max_offset
13979 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
13980 * TYPE_VECTOR_SUBPARTS (op00type));
13981 if (tree_int_cst_sign_bit (op01) == 0
13982 && compare_tree_int (op01, max_offset) == -1)
13983 {
13984 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
13985 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
13986 tree index = bitsize_int (indexi);
13987 return fold_build3_loc (loc,
13988 BIT_FIELD_REF, type, op00,
13989 part_width, index);
13990 }
13991 }
13992 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13993 else if (TREE_CODE (op00type) == COMPLEX_TYPE
13994 && type == TREE_TYPE (op00type))
13995 {
13996 tree size = TYPE_SIZE_UNIT (type);
13997 if (tree_int_cst_equal (size, op01))
13998 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
13999 }
14000 /* ((foo *)&fooarray)[1] => fooarray[1] */
14001 else if (TREE_CODE (op00type) == ARRAY_TYPE
14002 && type == TREE_TYPE (op00type))
14003 {
14004 tree type_domain = TYPE_DOMAIN (op00type);
14005 tree min = size_zero_node;
14006 if (type_domain && TYPE_MIN_VALUE (type_domain))
14007 min = TYPE_MIN_VALUE (type_domain);
14008 offset_int off = wi::to_offset (op01);
14009 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14010 offset_int remainder;
14011 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14012 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
14013 {
14014 off = off + wi::to_offset (min);
14015 op01 = wide_int_to_tree (sizetype, off);
14016 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14017 NULL_TREE, NULL_TREE);
14018 }
14019 }
14020 }
14021 }
14022
14023 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14024 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14025 && type == TREE_TYPE (TREE_TYPE (subtype))
14026 && (!in_gimple_form
14027 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14028 {
14029 tree type_domain;
14030 tree min_val = size_zero_node;
14031 sub = build_fold_indirect_ref_loc (loc, sub);
14032 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14033 if (type_domain && TYPE_MIN_VALUE (type_domain))
14034 min_val = TYPE_MIN_VALUE (type_domain);
14035 if (in_gimple_form
14036 && TREE_CODE (min_val) != INTEGER_CST)
14037 return NULL_TREE;
14038 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14039 NULL_TREE);
14040 }
14041
14042 return NULL_TREE;
14043 }
14044
14045 /* Builds an expression for an indirection through T, simplifying some
14046 cases. */
14047
14048 tree
14049 build_fold_indirect_ref_loc (location_t loc, tree t)
14050 {
14051 tree type = TREE_TYPE (TREE_TYPE (t));
14052 tree sub = fold_indirect_ref_1 (loc, type, t);
14053
14054 if (sub)
14055 return sub;
14056
14057 return build1_loc (loc, INDIRECT_REF, type, t);
14058 }
14059
14060 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14061
14062 tree
14063 fold_indirect_ref_loc (location_t loc, tree t)
14064 {
14065 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14066
14067 if (sub)
14068 return sub;
14069 else
14070 return t;
14071 }
14072
14073 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14074 whose result is ignored. The type of the returned tree need not be
14075 the same as the original expression. */
14076
14077 tree
14078 fold_ignored_result (tree t)
14079 {
14080 if (!TREE_SIDE_EFFECTS (t))
14081 return integer_zero_node;
14082
14083 for (;;)
14084 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14085 {
14086 case tcc_unary:
14087 t = TREE_OPERAND (t, 0);
14088 break;
14089
14090 case tcc_binary:
14091 case tcc_comparison:
14092 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14093 t = TREE_OPERAND (t, 0);
14094 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14095 t = TREE_OPERAND (t, 1);
14096 else
14097 return t;
14098 break;
14099
14100 case tcc_expression:
14101 switch (TREE_CODE (t))
14102 {
14103 case COMPOUND_EXPR:
14104 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14105 return t;
14106 t = TREE_OPERAND (t, 0);
14107 break;
14108
14109 case COND_EXPR:
14110 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14111 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14112 return t;
14113 t = TREE_OPERAND (t, 0);
14114 break;
14115
14116 default:
14117 return t;
14118 }
14119 break;
14120
14121 default:
14122 return t;
14123 }
14124 }
14125
14126 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14127
14128 tree
14129 round_up_loc (location_t loc, tree value, unsigned int divisor)
14130 {
14131 tree div = NULL_TREE;
14132
14133 if (divisor == 1)
14134 return value;
14135
14136 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14137 have to do anything. Only do this when we are not given a const,
14138 because in that case, this check is more expensive than just
14139 doing it. */
14140 if (TREE_CODE (value) != INTEGER_CST)
14141 {
14142 div = build_int_cst (TREE_TYPE (value), divisor);
14143
14144 if (multiple_of_p (TREE_TYPE (value), value, div))
14145 return value;
14146 }
14147
14148 /* If divisor is a power of two, simplify this to bit manipulation. */
14149 if (pow2_or_zerop (divisor))
14150 {
14151 if (TREE_CODE (value) == INTEGER_CST)
14152 {
14153 wide_int val = wi::to_wide (value);
14154 bool overflow_p;
14155
14156 if ((val & (divisor - 1)) == 0)
14157 return value;
14158
14159 overflow_p = TREE_OVERFLOW (value);
14160 val += divisor - 1;
14161 val &= (int) -divisor;
14162 if (val == 0)
14163 overflow_p = true;
14164
14165 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14166 }
14167 else
14168 {
14169 tree t;
14170
14171 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14172 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14173 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14174 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14175 }
14176 }
14177 else
14178 {
14179 if (!div)
14180 div = build_int_cst (TREE_TYPE (value), divisor);
14181 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14182 value = size_binop_loc (loc, MULT_EXPR, value, div);
14183 }
14184
14185 return value;
14186 }
14187
14188 /* Likewise, but round down. */
14189
14190 tree
14191 round_down_loc (location_t loc, tree value, int divisor)
14192 {
14193 tree div = NULL_TREE;
14194
14195 gcc_assert (divisor > 0);
14196 if (divisor == 1)
14197 return value;
14198
14199 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14200 have to do anything. Only do this when we are not given a const,
14201 because in that case, this check is more expensive than just
14202 doing it. */
14203 if (TREE_CODE (value) != INTEGER_CST)
14204 {
14205 div = build_int_cst (TREE_TYPE (value), divisor);
14206
14207 if (multiple_of_p (TREE_TYPE (value), value, div))
14208 return value;
14209 }
14210
14211 /* If divisor is a power of two, simplify this to bit manipulation. */
14212 if (pow2_or_zerop (divisor))
14213 {
14214 tree t;
14215
14216 t = build_int_cst (TREE_TYPE (value), -divisor);
14217 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14218 }
14219 else
14220 {
14221 if (!div)
14222 div = build_int_cst (TREE_TYPE (value), divisor);
14223 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14224 value = size_binop_loc (loc, MULT_EXPR, value, div);
14225 }
14226
14227 return value;
14228 }
14229
14230 /* Returns the pointer to the base of the object addressed by EXP and
14231 extracts the information about the offset of the access, storing it
14232 to PBITPOS and POFFSET. */
14233
14234 static tree
14235 split_address_to_core_and_offset (tree exp,
14236 HOST_WIDE_INT *pbitpos, tree *poffset)
14237 {
14238 tree core;
14239 machine_mode mode;
14240 int unsignedp, reversep, volatilep;
14241 HOST_WIDE_INT bitsize;
14242 location_t loc = EXPR_LOCATION (exp);
14243
14244 if (TREE_CODE (exp) == ADDR_EXPR)
14245 {
14246 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14247 poffset, &mode, &unsignedp, &reversep,
14248 &volatilep);
14249 core = build_fold_addr_expr_loc (loc, core);
14250 }
14251 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14252 {
14253 core = TREE_OPERAND (exp, 0);
14254 STRIP_NOPS (core);
14255 *pbitpos = 0;
14256 *poffset = TREE_OPERAND (exp, 1);
14257 if (TREE_CODE (*poffset) == INTEGER_CST)
14258 {
14259 offset_int tem = wi::sext (wi::to_offset (*poffset),
14260 TYPE_PRECISION (TREE_TYPE (*poffset)));
14261 tem <<= LOG2_BITS_PER_UNIT;
14262 if (wi::fits_shwi_p (tem))
14263 {
14264 *pbitpos = tem.to_shwi ();
14265 *poffset = NULL_TREE;
14266 }
14267 }
14268 }
14269 else
14270 {
14271 core = exp;
14272 *pbitpos = 0;
14273 *poffset = NULL_TREE;
14274 }
14275
14276 return core;
14277 }
14278
14279 /* Returns true if addresses of E1 and E2 differ by a constant, false
14280 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14281
14282 bool
14283 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14284 {
14285 tree core1, core2;
14286 HOST_WIDE_INT bitpos1, bitpos2;
14287 tree toffset1, toffset2, tdiff, type;
14288
14289 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14290 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14291
14292 if (bitpos1 % BITS_PER_UNIT != 0
14293 || bitpos2 % BITS_PER_UNIT != 0
14294 || !operand_equal_p (core1, core2, 0))
14295 return false;
14296
14297 if (toffset1 && toffset2)
14298 {
14299 type = TREE_TYPE (toffset1);
14300 if (type != TREE_TYPE (toffset2))
14301 toffset2 = fold_convert (type, toffset2);
14302
14303 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14304 if (!cst_and_fits_in_hwi (tdiff))
14305 return false;
14306
14307 *diff = int_cst_value (tdiff);
14308 }
14309 else if (toffset1 || toffset2)
14310 {
14311 /* If only one of the offsets is non-constant, the difference cannot
14312 be a constant. */
14313 return false;
14314 }
14315 else
14316 *diff = 0;
14317
14318 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14319 return true;
14320 }
14321
14322 /* Return OFF converted to a pointer offset type suitable as offset for
14323 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14324 tree
14325 convert_to_ptrofftype_loc (location_t loc, tree off)
14326 {
14327 return fold_convert_loc (loc, sizetype, off);
14328 }
14329
14330 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14331 tree
14332 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14333 {
14334 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14335 ptr, convert_to_ptrofftype_loc (loc, off));
14336 }
14337
14338 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14339 tree
14340 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14341 {
14342 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14343 ptr, size_int (off));
14344 }
14345
14346 /* Return a char pointer for a C string if it is a string constant
14347 or sum of string constant and integer constant. We only support
14348 string constants properly terminated with '\0' character.
14349 If STRLEN is a valid pointer, length (including terminating character)
14350 of returned string is stored to the argument. */
14351
14352 const char *
14353 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14354 {
14355 tree offset_node;
14356
14357 if (strlen)
14358 *strlen = 0;
14359
14360 src = string_constant (src, &offset_node);
14361 if (src == 0)
14362 return NULL;
14363
14364 unsigned HOST_WIDE_INT offset = 0;
14365 if (offset_node != NULL_TREE)
14366 {
14367 if (!tree_fits_uhwi_p (offset_node))
14368 return NULL;
14369 else
14370 offset = tree_to_uhwi (offset_node);
14371 }
14372
14373 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14374 const char *string = TREE_STRING_POINTER (src);
14375
14376 /* Support only properly null-terminated strings. */
14377 if (string_length == 0
14378 || string[string_length - 1] != '\0'
14379 || offset >= string_length)
14380 return NULL;
14381
14382 if (strlen)
14383 *strlen = string_length - offset;
14384 return string + offset;
14385 }
14386
14387 #if CHECKING_P
14388
14389 namespace selftest {
14390
14391 /* Helper functions for writing tests of folding trees. */
14392
14393 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14394
14395 static void
14396 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14397 tree constant)
14398 {
14399 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14400 }
14401
14402 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14403 wrapping WRAPPED_EXPR. */
14404
14405 static void
14406 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14407 tree wrapped_expr)
14408 {
14409 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14410 ASSERT_NE (wrapped_expr, result);
14411 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14412 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14413 }
14414
14415 /* Verify that various arithmetic binary operations are folded
14416 correctly. */
14417
14418 static void
14419 test_arithmetic_folding ()
14420 {
14421 tree type = integer_type_node;
14422 tree x = create_tmp_var_raw (type, "x");
14423 tree zero = build_zero_cst (type);
14424 tree one = build_int_cst (type, 1);
14425
14426 /* Addition. */
14427 /* 1 <-- (0 + 1) */
14428 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14429 one);
14430 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14431 one);
14432
14433 /* (nonlvalue)x <-- (x + 0) */
14434 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14435 x);
14436
14437 /* Subtraction. */
14438 /* 0 <-- (x - x) */
14439 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14440 zero);
14441 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14442 x);
14443
14444 /* Multiplication. */
14445 /* 0 <-- (x * 0) */
14446 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14447 zero);
14448
14449 /* (nonlvalue)x <-- (x * 1) */
14450 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14451 x);
14452 }
14453
14454 /* Verify that various binary operations on vectors are folded
14455 correctly. */
14456
14457 static void
14458 test_vector_folding ()
14459 {
14460 tree inner_type = integer_type_node;
14461 tree type = build_vector_type (inner_type, 4);
14462 tree zero = build_zero_cst (type);
14463 tree one = build_one_cst (type);
14464
14465 /* Verify equality tests that return a scalar boolean result. */
14466 tree res_type = boolean_type_node;
14467 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14468 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14469 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14470 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14471 }
14472
14473 /* Run all of the selftests within this file. */
14474
14475 void
14476 fold_const_c_tests ()
14477 {
14478 test_arithmetic_folding ();
14479 test_vector_folding ();
14480 }
14481
14482 } // namespace selftest
14483
14484 #endif /* CHECKING_P */