re PR tree-optimization/67328 (range test rather than single bit test for code testin...
[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
83 /* Nonzero if we are folding constants inside an initializer; zero
84 otherwise. */
85 int folding_initializer = 0;
86
87 /* The following constants represent a bit based encoding of GCC's
88 comparison operators. This encoding simplifies transformations
89 on relational comparison operators, such as AND and OR. */
90 enum comparison_code {
91 COMPCODE_FALSE = 0,
92 COMPCODE_LT = 1,
93 COMPCODE_EQ = 2,
94 COMPCODE_LE = 3,
95 COMPCODE_GT = 4,
96 COMPCODE_LTGT = 5,
97 COMPCODE_GE = 6,
98 COMPCODE_ORD = 7,
99 COMPCODE_UNORD = 8,
100 COMPCODE_UNLT = 9,
101 COMPCODE_UNEQ = 10,
102 COMPCODE_UNLE = 11,
103 COMPCODE_UNGT = 12,
104 COMPCODE_NE = 13,
105 COMPCODE_UNGE = 14,
106 COMPCODE_TRUE = 15
107 };
108
109 static bool negate_expr_p (tree);
110 static tree negate_expr (tree);
111 static tree split_tree (location_t, tree, tree, enum tree_code,
112 tree *, tree *, tree *, int);
113 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
114 static enum comparison_code comparison_to_compcode (enum tree_code);
115 static enum tree_code compcode_to_comparison (enum comparison_code);
116 static int operand_equal_for_comparison_p (tree, tree, tree);
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_div_compare (location_t, enum tree_code, tree, tree, tree);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static bool vec_cst_ctor_to_array (tree, tree *);
142 static tree fold_negate_expr (location_t, tree);
143
144
145 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
146 Otherwise, return LOC. */
147
148 static location_t
149 expr_location_or (tree t, location_t loc)
150 {
151 location_t tloc = EXPR_LOCATION (t);
152 return tloc == UNKNOWN_LOCATION ? loc : tloc;
153 }
154
155 /* Similar to protected_set_expr_location, but never modify x in place,
156 if location can and needs to be set, unshare it. */
157
158 static inline tree
159 protected_set_expr_location_unshare (tree x, location_t loc)
160 {
161 if (CAN_HAVE_LOCATION_P (x)
162 && EXPR_LOCATION (x) != loc
163 && !(TREE_CODE (x) == SAVE_EXPR
164 || TREE_CODE (x) == TARGET_EXPR
165 || TREE_CODE (x) == BIND_EXPR))
166 {
167 x = copy_node (x);
168 SET_EXPR_LOCATION (x, loc);
169 }
170 return x;
171 }
172 \f
173 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
174 division and returns the quotient. Otherwise returns
175 NULL_TREE. */
176
177 tree
178 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 {
180 widest_int quo;
181
182 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
183 SIGNED, &quo))
184 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185
186 return NULL_TREE;
187 }
188 \f
189 /* This is nonzero if we should defer warnings about undefined
190 overflow. This facility exists because these warnings are a
191 special case. The code to estimate loop iterations does not want
192 to issue any warnings, since it works with expressions which do not
193 occur in user code. Various bits of cleanup code call fold(), but
194 only use the result if it has certain characteristics (e.g., is a
195 constant); that code only wants to issue a warning if the result is
196 used. */
197
198 static int fold_deferring_overflow_warnings;
199
200 /* If a warning about undefined overflow is deferred, this is the
201 warning. Note that this may cause us to turn two warnings into
202 one, but that is fine since it is sufficient to only give one
203 warning per expression. */
204
205 static const char* fold_deferred_overflow_warning;
206
207 /* If a warning about undefined overflow is deferred, this is the
208 level at which the warning should be emitted. */
209
210 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211
212 /* Start deferring overflow warnings. We could use a stack here to
213 permit nested calls, but at present it is not necessary. */
214
215 void
216 fold_defer_overflow_warnings (void)
217 {
218 ++fold_deferring_overflow_warnings;
219 }
220
221 /* Stop deferring overflow warnings. If there is a pending warning,
222 and ISSUE is true, then issue the warning if appropriate. STMT is
223 the statement with which the warning should be associated (used for
224 location information); STMT may be NULL. CODE is the level of the
225 warning--a warn_strict_overflow_code value. This function will use
226 the smaller of CODE and the deferred code when deciding whether to
227 issue the warning. CODE may be zero to mean to always use the
228 deferred code. */
229
230 void
231 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 {
233 const char *warnmsg;
234 location_t locus;
235
236 gcc_assert (fold_deferring_overflow_warnings > 0);
237 --fold_deferring_overflow_warnings;
238 if (fold_deferring_overflow_warnings > 0)
239 {
240 if (fold_deferred_overflow_warning != NULL
241 && code != 0
242 && code < (int) fold_deferred_overflow_code)
243 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
244 return;
245 }
246
247 warnmsg = fold_deferred_overflow_warning;
248 fold_deferred_overflow_warning = NULL;
249
250 if (!issue || warnmsg == NULL)
251 return;
252
253 if (gimple_no_warning_p (stmt))
254 return;
255
256 /* Use the smallest code level when deciding to issue the
257 warning. */
258 if (code == 0 || code > (int) fold_deferred_overflow_code)
259 code = fold_deferred_overflow_code;
260
261 if (!issue_strict_overflow_warning (code))
262 return;
263
264 if (stmt == NULL)
265 locus = input_location;
266 else
267 locus = gimple_location (stmt);
268 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
269 }
270
271 /* Stop deferring overflow warnings, ignoring any deferred
272 warnings. */
273
274 void
275 fold_undefer_and_ignore_overflow_warnings (void)
276 {
277 fold_undefer_overflow_warnings (false, NULL, 0);
278 }
279
280 /* Whether we are deferring overflow warnings. */
281
282 bool
283 fold_deferring_overflow_warnings_p (void)
284 {
285 return fold_deferring_overflow_warnings > 0;
286 }
287
288 /* This is called when we fold something based on the fact that signed
289 overflow is undefined. */
290
291 void
292 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 {
294 if (fold_deferring_overflow_warnings > 0)
295 {
296 if (fold_deferred_overflow_warning == NULL
297 || wc < fold_deferred_overflow_code)
298 {
299 fold_deferred_overflow_warning = gmsgid;
300 fold_deferred_overflow_code = wc;
301 }
302 }
303 else if (issue_strict_overflow_warning (wc))
304 warning (OPT_Wstrict_overflow, gmsgid);
305 }
306 \f
307 /* Return true if the built-in mathematical function specified by CODE
308 is odd, i.e. -f(x) == f(-x). */
309
310 bool
311 negate_mathfn_p (combined_fn fn)
312 {
313 switch (fn)
314 {
315 CASE_CFN_ASIN:
316 CASE_CFN_ASINH:
317 CASE_CFN_ATAN:
318 CASE_CFN_ATANH:
319 CASE_CFN_CASIN:
320 CASE_CFN_CASINH:
321 CASE_CFN_CATAN:
322 CASE_CFN_CATANH:
323 CASE_CFN_CBRT:
324 CASE_CFN_CPROJ:
325 CASE_CFN_CSIN:
326 CASE_CFN_CSINH:
327 CASE_CFN_CTAN:
328 CASE_CFN_CTANH:
329 CASE_CFN_ERF:
330 CASE_CFN_LLROUND:
331 CASE_CFN_LROUND:
332 CASE_CFN_ROUND:
333 CASE_CFN_SIN:
334 CASE_CFN_SINH:
335 CASE_CFN_TAN:
336 CASE_CFN_TANH:
337 CASE_CFN_TRUNC:
338 return true;
339
340 CASE_CFN_LLRINT:
341 CASE_CFN_LRINT:
342 CASE_CFN_NEARBYINT:
343 CASE_CFN_RINT:
344 return !flag_rounding_math;
345
346 default:
347 break;
348 }
349 return false;
350 }
351
352 /* Check whether we may negate an integer constant T without causing
353 overflow. */
354
355 bool
356 may_negate_without_overflow_p (const_tree t)
357 {
358 tree type;
359
360 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361
362 type = TREE_TYPE (t);
363 if (TYPE_UNSIGNED (type))
364 return false;
365
366 return !wi::only_sign_bit_p (t);
367 }
368
369 /* Determine whether an expression T can be cheaply negated using
370 the function negate_expr without introducing undefined overflow. */
371
372 static bool
373 negate_expr_p (tree t)
374 {
375 tree type;
376
377 if (t == 0)
378 return false;
379
380 type = TREE_TYPE (t);
381
382 STRIP_SIGN_NOPS (t);
383 switch (TREE_CODE (t))
384 {
385 case INTEGER_CST:
386 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
387 return true;
388
389 /* Check that -CST will not overflow type. */
390 return may_negate_without_overflow_p (t);
391 case BIT_NOT_EXPR:
392 return (INTEGRAL_TYPE_P (type)
393 && TYPE_OVERFLOW_WRAPS (type));
394
395 case FIXED_CST:
396 return true;
397
398 case NEGATE_EXPR:
399 return !TYPE_OVERFLOW_SANITIZED (type);
400
401 case REAL_CST:
402 /* We want to canonicalize to positive real constants. Pretend
403 that only negative ones can be easily negated. */
404 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405
406 case COMPLEX_CST:
407 return negate_expr_p (TREE_REALPART (t))
408 && negate_expr_p (TREE_IMAGPART (t));
409
410 case VECTOR_CST:
411 {
412 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
413 return true;
414
415 int count = TYPE_VECTOR_SUBPARTS (type), i;
416
417 for (i = 0; i < count; i++)
418 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
419 return false;
420
421 return true;
422 }
423
424 case COMPLEX_EXPR:
425 return negate_expr_p (TREE_OPERAND (t, 0))
426 && negate_expr_p (TREE_OPERAND (t, 1));
427
428 case CONJ_EXPR:
429 return negate_expr_p (TREE_OPERAND (t, 0));
430
431 case PLUS_EXPR:
432 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
433 || HONOR_SIGNED_ZEROS (element_mode (type))
434 || (INTEGRAL_TYPE_P (type)
435 && ! TYPE_OVERFLOW_WRAPS (type)))
436 return false;
437 /* -(A + B) -> (-B) - A. */
438 if (negate_expr_p (TREE_OPERAND (t, 1)))
439 return true;
440 /* -(A + B) -> (-A) - B. */
441 return negate_expr_p (TREE_OPERAND (t, 0));
442
443 case MINUS_EXPR:
444 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
445 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
446 && !HONOR_SIGNED_ZEROS (element_mode (type))
447 && (! INTEGRAL_TYPE_P (type)
448 || TYPE_OVERFLOW_WRAPS (type));
449
450 case MULT_EXPR:
451 if (TYPE_UNSIGNED (type))
452 break;
453 /* INT_MIN/n * n doesn't overflow while negating one operand it does
454 if n is a (negative) power of two. */
455 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
456 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
457 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
458 && wi::popcount (wi::abs (TREE_OPERAND (t, 0))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && wi::popcount (wi::abs (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::eq_p (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 int count = TYPE_VECTOR_SUBPARTS (type), i;
570 tree *elts = XALLOCAVEC (tree, count);
571
572 for (i = 0; i < count; i++)
573 {
574 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 if (elts[i] == NULL_TREE)
576 return NULL_TREE;
577 }
578
579 return build_vector (type, elts);
580 }
581
582 case COMPLEX_EXPR:
583 if (negate_expr_p (t))
584 return fold_build2_loc (loc, COMPLEX_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
586 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
587 break;
588
589 case CONJ_EXPR:
590 if (negate_expr_p (t))
591 return fold_build1_loc (loc, CONJ_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
593 break;
594
595 case NEGATE_EXPR:
596 if (!TYPE_OVERFLOW_SANITIZED (type))
597 return TREE_OPERAND (t, 0);
598 break;
599
600 case PLUS_EXPR:
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
602 && !HONOR_SIGNED_ZEROS (element_mode (type)))
603 {
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t, 1)))
606 {
607 tem = negate_expr (TREE_OPERAND (t, 1));
608 return fold_build2_loc (loc, MINUS_EXPR, type,
609 tem, TREE_OPERAND (t, 0));
610 }
611
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t, 0)))
614 {
615 tem = negate_expr (TREE_OPERAND (t, 0));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 1));
618 }
619 }
620 break;
621
622 case MINUS_EXPR:
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
625 && !HONOR_SIGNED_ZEROS (element_mode (type)))
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
628 break;
629
630 case MULT_EXPR:
631 if (TYPE_UNSIGNED (type))
632 break;
633
634 /* Fall through. */
635
636 case RDIV_EXPR:
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
638 {
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
641 return fold_build2_loc (loc, TREE_CODE (t), type,
642 TREE_OPERAND (t, 0), negate_expr (tem));
643 tem = TREE_OPERAND (t, 0);
644 if (negate_expr_p (tem))
645 return fold_build2_loc (loc, TREE_CODE (t), type,
646 negate_expr (tem), TREE_OPERAND (t, 1));
647 }
648 break;
649
650 case TRUNC_DIV_EXPR:
651 case ROUND_DIV_EXPR:
652 case EXACT_DIV_EXPR:
653 if (TYPE_UNSIGNED (type))
654 break;
655 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (TREE_OPERAND (t, 0)),
658 TREE_OPERAND (t, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
664 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t, 1))))
666 && negate_expr_p (TREE_OPERAND (t, 1)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 TREE_OPERAND (t, 0),
669 negate_expr (TREE_OPERAND (t, 1)));
670 break;
671
672 case NOP_EXPR:
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type) == REAL_TYPE)
675 {
676 tem = strip_float_extensions (t);
677 if (tem != t && negate_expr_p (tem))
678 return fold_convert_loc (loc, type, negate_expr (tem));
679 }
680 break;
681
682 case CALL_EXPR:
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t))
685 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
686 {
687 tree fndecl, arg;
688
689 fndecl = get_callee_fndecl (t);
690 arg = negate_expr (CALL_EXPR_ARG (t, 0));
691 return build_call_expr_loc (loc, fndecl, 1, arg);
692 }
693 break;
694
695 case RSHIFT_EXPR:
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
698 {
699 tree op1 = TREE_OPERAND (t, 1);
700 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
701 {
702 tree ntype = TYPE_UNSIGNED (type)
703 ? signed_type_for (type)
704 : unsigned_type_for (type);
705 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
706 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
707 return fold_convert_loc (loc, type, temp);
708 }
709 }
710 break;
711
712 default:
713 break;
714 }
715
716 return NULL_TREE;
717 }
718
719 /* A wrapper for fold_negate_expr_1. */
720
721 static tree
722 fold_negate_expr (location_t loc, tree t)
723 {
724 tree type = TREE_TYPE (t);
725 STRIP_SIGN_NOPS (t);
726 tree tem = fold_negate_expr_1 (loc, t);
727 if (tem == NULL_TREE)
728 return NULL_TREE;
729 return fold_convert_loc (loc, type, tem);
730 }
731
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
734 return NULL_TREE. */
735
736 static tree
737 negate_expr (tree t)
738 {
739 tree type, tem;
740 location_t loc;
741
742 if (t == NULL_TREE)
743 return NULL_TREE;
744
745 loc = EXPR_LOCATION (t);
746 type = TREE_TYPE (t);
747 STRIP_SIGN_NOPS (t);
748
749 tem = fold_negate_expr (loc, t);
750 if (!tem)
751 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
752 return fold_convert_loc (loc, type, tem);
753 }
754 \f
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
762
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
766
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
772
773 If IN is itself a literal or constant, return it as appropriate.
774
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
777
778 static tree
779 split_tree (location_t loc, tree in, tree type, enum tree_code code,
780 tree *conp, tree *litp, tree *minus_litp, int negate_p)
781 {
782 tree var = 0;
783
784 *conp = 0;
785 *litp = 0;
786 *minus_litp = 0;
787
788 /* Strip any conversions that don't change the machine mode or signedness. */
789 STRIP_SIGN_NOPS (in);
790
791 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
792 || TREE_CODE (in) == FIXED_CST)
793 *litp = in;
794 else if (TREE_CODE (in) == code
795 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
796 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
797 /* We can associate addition and subtraction together (even
798 though the C standard doesn't say so) for integers because
799 the value is not affected. For reals, the value might be
800 affected, so we can't. */
801 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
802 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
803 || (code == MINUS_EXPR
804 && (TREE_CODE (in) == PLUS_EXPR
805 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
806 {
807 tree op0 = TREE_OPERAND (in, 0);
808 tree op1 = TREE_OPERAND (in, 1);
809 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
810 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
811
812 /* First see if either of the operands is a literal, then a constant. */
813 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
814 || TREE_CODE (op0) == FIXED_CST)
815 *litp = op0, op0 = 0;
816 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
817 || TREE_CODE (op1) == FIXED_CST)
818 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
819
820 if (op0 != 0 && TREE_CONSTANT (op0))
821 *conp = op0, op0 = 0;
822 else if (op1 != 0 && TREE_CONSTANT (op1))
823 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
824
825 /* If we haven't dealt with either operand, this is not a case we can
826 decompose. Otherwise, VAR is either of the ones remaining, if any. */
827 if (op0 != 0 && op1 != 0)
828 var = in;
829 else if (op0 != 0)
830 var = op0;
831 else
832 var = op1, neg_var_p = neg1_p;
833
834 /* Now do any needed negations. */
835 if (neg_litp_p)
836 *minus_litp = *litp, *litp = 0;
837 if (neg_conp_p && *conp)
838 {
839 /* Convert to TYPE before negating. */
840 *conp = fold_convert_loc (loc, type, *conp);
841 *conp = negate_expr (*conp);
842 }
843 if (neg_var_p && var)
844 {
845 /* Convert to TYPE before negating. */
846 var = fold_convert_loc (loc, type, var);
847 var = negate_expr (var);
848 }
849 }
850 else if (TREE_CONSTANT (in))
851 *conp = in;
852 else if (TREE_CODE (in) == BIT_NOT_EXPR
853 && code == PLUS_EXPR)
854 {
855 /* -X - 1 is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *minus_litp = build_one_cst (TREE_TYPE (in));
858 var = negate_expr (TREE_OPERAND (in, 0));
859 }
860 else
861 var = in;
862
863 if (negate_p)
864 {
865 if (*litp)
866 *minus_litp = *litp, *litp = 0;
867 else if (*minus_litp)
868 *litp = *minus_litp, *minus_litp = 0;
869 if (*conp)
870 {
871 /* Convert to TYPE before negating. */
872 *conp = fold_convert_loc (loc, type, *conp);
873 *conp = negate_expr (*conp);
874 }
875 if (var)
876 {
877 /* Convert to TYPE before negating. */
878 var = fold_convert_loc (loc, type, var);
879 var = negate_expr (var);
880 }
881 }
882
883 return var;
884 }
885
886 /* Re-associate trees split by the above function. T1 and T2 are
887 either expressions to associate or null. Return the new
888 expression, if any. LOC is the location of the new expression. If
889 we build an operation, do it in TYPE and with CODE. */
890
891 static tree
892 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
893 {
894 if (t1 == 0)
895 return t2;
896 else if (t2 == 0)
897 return t1;
898
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
903 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
904 {
905 if (code == PLUS_EXPR)
906 {
907 if (TREE_CODE (t1) == NEGATE_EXPR)
908 return build2_loc (loc, MINUS_EXPR, type,
909 fold_convert_loc (loc, type, t2),
910 fold_convert_loc (loc, type,
911 TREE_OPERAND (t1, 0)));
912 else if (TREE_CODE (t2) == NEGATE_EXPR)
913 return build2_loc (loc, MINUS_EXPR, type,
914 fold_convert_loc (loc, type, t1),
915 fold_convert_loc (loc, type,
916 TREE_OPERAND (t2, 0)));
917 else if (integer_zerop (t2))
918 return fold_convert_loc (loc, type, t1);
919 }
920 else if (code == MINUS_EXPR)
921 {
922 if (integer_zerop (t2))
923 return fold_convert_loc (loc, type, t1);
924 }
925
926 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
927 fold_convert_loc (loc, type, t2));
928 }
929
930 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
931 fold_convert_loc (loc, type, t2));
932 }
933 \f
934 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
935 for use in int_const_binop, size_binop and size_diffop. */
936
937 static bool
938 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
939 {
940 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
941 return false;
942 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
943 return false;
944
945 switch (code)
946 {
947 case LSHIFT_EXPR:
948 case RSHIFT_EXPR:
949 case LROTATE_EXPR:
950 case RROTATE_EXPR:
951 return true;
952
953 default:
954 break;
955 }
956
957 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
958 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
959 && TYPE_MODE (type1) == TYPE_MODE (type2);
960 }
961
962
963 /* Combine two integer constants ARG1 and ARG2 under operation CODE
964 to produce a new constant. Return NULL_TREE if we don't know how
965 to evaluate CODE at compile-time. */
966
967 static tree
968 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
969 int overflowable)
970 {
971 wide_int res;
972 tree t;
973 tree type = TREE_TYPE (arg1);
974 signop sign = TYPE_SIGN (type);
975 bool overflow = false;
976
977 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
978
979 switch (code)
980 {
981 case BIT_IOR_EXPR:
982 res = wi::bit_or (arg1, arg2);
983 break;
984
985 case BIT_XOR_EXPR:
986 res = wi::bit_xor (arg1, arg2);
987 break;
988
989 case BIT_AND_EXPR:
990 res = wi::bit_and (arg1, arg2);
991 break;
992
993 case RSHIFT_EXPR:
994 case LSHIFT_EXPR:
995 if (wi::neg_p (arg2))
996 {
997 arg2 = -arg2;
998 if (code == RSHIFT_EXPR)
999 code = LSHIFT_EXPR;
1000 else
1001 code = RSHIFT_EXPR;
1002 }
1003
1004 if (code == RSHIFT_EXPR)
1005 /* It's unclear from the C standard whether shifts can overflow.
1006 The following code ignores overflow; perhaps a C standard
1007 interpretation ruling is needed. */
1008 res = wi::rshift (arg1, arg2, sign);
1009 else
1010 res = wi::lshift (arg1, arg2);
1011 break;
1012
1013 case RROTATE_EXPR:
1014 case LROTATE_EXPR:
1015 if (wi::neg_p (arg2))
1016 {
1017 arg2 = -arg2;
1018 if (code == RROTATE_EXPR)
1019 code = LROTATE_EXPR;
1020 else
1021 code = RROTATE_EXPR;
1022 }
1023
1024 if (code == RROTATE_EXPR)
1025 res = wi::rrotate (arg1, arg2);
1026 else
1027 res = wi::lrotate (arg1, arg2);
1028 break;
1029
1030 case PLUS_EXPR:
1031 res = wi::add (arg1, arg2, sign, &overflow);
1032 break;
1033
1034 case MINUS_EXPR:
1035 res = wi::sub (arg1, arg2, sign, &overflow);
1036 break;
1037
1038 case MULT_EXPR:
1039 res = wi::mul (arg1, arg2, sign, &overflow);
1040 break;
1041
1042 case MULT_HIGHPART_EXPR:
1043 res = wi::mul_high (arg1, arg2, sign);
1044 break;
1045
1046 case TRUNC_DIV_EXPR:
1047 case EXACT_DIV_EXPR:
1048 if (arg2 == 0)
1049 return NULL_TREE;
1050 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1051 break;
1052
1053 case FLOOR_DIV_EXPR:
1054 if (arg2 == 0)
1055 return NULL_TREE;
1056 res = wi::div_floor (arg1, arg2, sign, &overflow);
1057 break;
1058
1059 case CEIL_DIV_EXPR:
1060 if (arg2 == 0)
1061 return NULL_TREE;
1062 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1063 break;
1064
1065 case ROUND_DIV_EXPR:
1066 if (arg2 == 0)
1067 return NULL_TREE;
1068 res = wi::div_round (arg1, arg2, sign, &overflow);
1069 break;
1070
1071 case TRUNC_MOD_EXPR:
1072 if (arg2 == 0)
1073 return NULL_TREE;
1074 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1075 break;
1076
1077 case FLOOR_MOD_EXPR:
1078 if (arg2 == 0)
1079 return NULL_TREE;
1080 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1081 break;
1082
1083 case CEIL_MOD_EXPR:
1084 if (arg2 == 0)
1085 return NULL_TREE;
1086 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1087 break;
1088
1089 case ROUND_MOD_EXPR:
1090 if (arg2 == 0)
1091 return NULL_TREE;
1092 res = wi::mod_round (arg1, arg2, sign, &overflow);
1093 break;
1094
1095 case MIN_EXPR:
1096 res = wi::min (arg1, arg2, sign);
1097 break;
1098
1099 case MAX_EXPR:
1100 res = wi::max (arg1, arg2, sign);
1101 break;
1102
1103 default:
1104 return NULL_TREE;
1105 }
1106
1107 t = force_fit_type (type, res, overflowable,
1108 (((sign == SIGNED || overflowable == -1)
1109 && overflow)
1110 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1111
1112 return t;
1113 }
1114
1115 tree
1116 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1117 {
1118 return int_const_binop_1 (code, arg1, arg2, 1);
1119 }
1120
1121 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1122 constant. We assume ARG1 and ARG2 have the same data type, or at least
1123 are the same kind of constant and the same machine mode. Return zero if
1124 combining the constants is not allowed in the current operating mode. */
1125
1126 static tree
1127 const_binop (enum tree_code code, tree arg1, tree arg2)
1128 {
1129 /* Sanity check for the recursive cases. */
1130 if (!arg1 || !arg2)
1131 return NULL_TREE;
1132
1133 STRIP_NOPS (arg1);
1134 STRIP_NOPS (arg2);
1135
1136 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1137 {
1138 if (code == POINTER_PLUS_EXPR)
1139 return int_const_binop (PLUS_EXPR,
1140 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1141
1142 return int_const_binop (code, arg1, arg2);
1143 }
1144
1145 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1146 {
1147 machine_mode mode;
1148 REAL_VALUE_TYPE d1;
1149 REAL_VALUE_TYPE d2;
1150 REAL_VALUE_TYPE value;
1151 REAL_VALUE_TYPE result;
1152 bool inexact;
1153 tree t, type;
1154
1155 /* The following codes are handled by real_arithmetic. */
1156 switch (code)
1157 {
1158 case PLUS_EXPR:
1159 case MINUS_EXPR:
1160 case MULT_EXPR:
1161 case RDIV_EXPR:
1162 case MIN_EXPR:
1163 case MAX_EXPR:
1164 break;
1165
1166 default:
1167 return NULL_TREE;
1168 }
1169
1170 d1 = TREE_REAL_CST (arg1);
1171 d2 = TREE_REAL_CST (arg2);
1172
1173 type = TREE_TYPE (arg1);
1174 mode = TYPE_MODE (type);
1175
1176 /* Don't perform operation if we honor signaling NaNs and
1177 either operand is a signaling NaN. */
1178 if (HONOR_SNANS (mode)
1179 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1180 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1181 return NULL_TREE;
1182
1183 /* Don't perform operation if it would raise a division
1184 by zero exception. */
1185 if (code == RDIV_EXPR
1186 && real_equal (&d2, &dconst0)
1187 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1188 return NULL_TREE;
1189
1190 /* If either operand is a NaN, just return it. Otherwise, set up
1191 for floating-point trap; we return an overflow. */
1192 if (REAL_VALUE_ISNAN (d1))
1193 {
1194 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1195 is off. */
1196 d1.signalling = 0;
1197 t = build_real (type, d1);
1198 return t;
1199 }
1200 else if (REAL_VALUE_ISNAN (d2))
1201 {
1202 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1203 is off. */
1204 d2.signalling = 0;
1205 t = build_real (type, d2);
1206 return t;
1207 }
1208
1209 inexact = real_arithmetic (&value, code, &d1, &d2);
1210 real_convert (&result, mode, &value);
1211
1212 /* Don't constant fold this floating point operation if
1213 the result has overflowed and flag_trapping_math. */
1214 if (flag_trapping_math
1215 && MODE_HAS_INFINITIES (mode)
1216 && REAL_VALUE_ISINF (result)
1217 && !REAL_VALUE_ISINF (d1)
1218 && !REAL_VALUE_ISINF (d2))
1219 return NULL_TREE;
1220
1221 /* Don't constant fold this floating point operation if the
1222 result may dependent upon the run-time rounding mode and
1223 flag_rounding_math is set, or if GCC's software emulation
1224 is unable to accurately represent the result. */
1225 if ((flag_rounding_math
1226 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1227 && (inexact || !real_identical (&result, &value)))
1228 return NULL_TREE;
1229
1230 t = build_real (type, result);
1231
1232 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1233 return t;
1234 }
1235
1236 if (TREE_CODE (arg1) == FIXED_CST)
1237 {
1238 FIXED_VALUE_TYPE f1;
1239 FIXED_VALUE_TYPE f2;
1240 FIXED_VALUE_TYPE result;
1241 tree t, type;
1242 int sat_p;
1243 bool overflow_p;
1244
1245 /* The following codes are handled by fixed_arithmetic. */
1246 switch (code)
1247 {
1248 case PLUS_EXPR:
1249 case MINUS_EXPR:
1250 case MULT_EXPR:
1251 case TRUNC_DIV_EXPR:
1252 if (TREE_CODE (arg2) != FIXED_CST)
1253 return NULL_TREE;
1254 f2 = TREE_FIXED_CST (arg2);
1255 break;
1256
1257 case LSHIFT_EXPR:
1258 case RSHIFT_EXPR:
1259 {
1260 if (TREE_CODE (arg2) != INTEGER_CST)
1261 return NULL_TREE;
1262 wide_int w2 = arg2;
1263 f2.data.high = w2.elt (1);
1264 f2.data.low = w2.ulow ();
1265 f2.mode = SImode;
1266 }
1267 break;
1268
1269 default:
1270 return NULL_TREE;
1271 }
1272
1273 f1 = TREE_FIXED_CST (arg1);
1274 type = TREE_TYPE (arg1);
1275 sat_p = TYPE_SATURATING (type);
1276 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1277 t = build_fixed (type, result);
1278 /* Propagate overflow flags. */
1279 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1280 TREE_OVERFLOW (t) = 1;
1281 return t;
1282 }
1283
1284 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1285 {
1286 tree type = TREE_TYPE (arg1);
1287 tree r1 = TREE_REALPART (arg1);
1288 tree i1 = TREE_IMAGPART (arg1);
1289 tree r2 = TREE_REALPART (arg2);
1290 tree i2 = TREE_IMAGPART (arg2);
1291 tree real, imag;
1292
1293 switch (code)
1294 {
1295 case PLUS_EXPR:
1296 case MINUS_EXPR:
1297 real = const_binop (code, r1, r2);
1298 imag = const_binop (code, i1, i2);
1299 break;
1300
1301 case MULT_EXPR:
1302 if (COMPLEX_FLOAT_TYPE_P (type))
1303 return do_mpc_arg2 (arg1, arg2, type,
1304 /* do_nonfinite= */ folding_initializer,
1305 mpc_mul);
1306
1307 real = const_binop (MINUS_EXPR,
1308 const_binop (MULT_EXPR, r1, r2),
1309 const_binop (MULT_EXPR, i1, i2));
1310 imag = const_binop (PLUS_EXPR,
1311 const_binop (MULT_EXPR, r1, i2),
1312 const_binop (MULT_EXPR, i1, r2));
1313 break;
1314
1315 case RDIV_EXPR:
1316 if (COMPLEX_FLOAT_TYPE_P (type))
1317 return do_mpc_arg2 (arg1, arg2, type,
1318 /* do_nonfinite= */ folding_initializer,
1319 mpc_div);
1320 /* Fallthru. */
1321 case TRUNC_DIV_EXPR:
1322 case CEIL_DIV_EXPR:
1323 case FLOOR_DIV_EXPR:
1324 case ROUND_DIV_EXPR:
1325 if (flag_complex_method == 0)
1326 {
1327 /* Keep this algorithm in sync with
1328 tree-complex.c:expand_complex_div_straight().
1329
1330 Expand complex division to scalars, straightforward algorithm.
1331 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1332 t = br*br + bi*bi
1333 */
1334 tree magsquared
1335 = const_binop (PLUS_EXPR,
1336 const_binop (MULT_EXPR, r2, r2),
1337 const_binop (MULT_EXPR, i2, i2));
1338 tree t1
1339 = const_binop (PLUS_EXPR,
1340 const_binop (MULT_EXPR, r1, r2),
1341 const_binop (MULT_EXPR, i1, i2));
1342 tree t2
1343 = const_binop (MINUS_EXPR,
1344 const_binop (MULT_EXPR, i1, r2),
1345 const_binop (MULT_EXPR, r1, i2));
1346
1347 real = const_binop (code, t1, magsquared);
1348 imag = const_binop (code, t2, magsquared);
1349 }
1350 else
1351 {
1352 /* Keep this algorithm in sync with
1353 tree-complex.c:expand_complex_div_wide().
1354
1355 Expand complex division to scalars, modified algorithm to minimize
1356 overflow with wide input ranges. */
1357 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1358 fold_abs_const (r2, TREE_TYPE (type)),
1359 fold_abs_const (i2, TREE_TYPE (type)));
1360
1361 if (integer_nonzerop (compare))
1362 {
1363 /* In the TRUE branch, we compute
1364 ratio = br/bi;
1365 div = (br * ratio) + bi;
1366 tr = (ar * ratio) + ai;
1367 ti = (ai * ratio) - ar;
1368 tr = tr / div;
1369 ti = ti / div; */
1370 tree ratio = const_binop (code, r2, i2);
1371 tree div = const_binop (PLUS_EXPR, i2,
1372 const_binop (MULT_EXPR, r2, ratio));
1373 real = const_binop (MULT_EXPR, r1, ratio);
1374 real = const_binop (PLUS_EXPR, real, i1);
1375 real = const_binop (code, real, div);
1376
1377 imag = const_binop (MULT_EXPR, i1, ratio);
1378 imag = const_binop (MINUS_EXPR, imag, r1);
1379 imag = const_binop (code, imag, div);
1380 }
1381 else
1382 {
1383 /* In the FALSE branch, we compute
1384 ratio = d/c;
1385 divisor = (d * ratio) + c;
1386 tr = (b * ratio) + a;
1387 ti = b - (a * ratio);
1388 tr = tr / div;
1389 ti = ti / div; */
1390 tree ratio = const_binop (code, i2, r2);
1391 tree div = const_binop (PLUS_EXPR, r2,
1392 const_binop (MULT_EXPR, i2, ratio));
1393
1394 real = const_binop (MULT_EXPR, i1, ratio);
1395 real = const_binop (PLUS_EXPR, real, r1);
1396 real = const_binop (code, real, div);
1397
1398 imag = const_binop (MULT_EXPR, r1, ratio);
1399 imag = const_binop (MINUS_EXPR, i1, imag);
1400 imag = const_binop (code, imag, div);
1401 }
1402 }
1403 break;
1404
1405 default:
1406 return NULL_TREE;
1407 }
1408
1409 if (real && imag)
1410 return build_complex (type, real, imag);
1411 }
1412
1413 if (TREE_CODE (arg1) == VECTOR_CST
1414 && TREE_CODE (arg2) == VECTOR_CST)
1415 {
1416 tree type = TREE_TYPE (arg1);
1417 int count = TYPE_VECTOR_SUBPARTS (type), i;
1418 tree *elts = XALLOCAVEC (tree, count);
1419
1420 for (i = 0; i < count; i++)
1421 {
1422 tree elem1 = VECTOR_CST_ELT (arg1, i);
1423 tree elem2 = VECTOR_CST_ELT (arg2, i);
1424
1425 elts[i] = const_binop (code, elem1, elem2);
1426
1427 /* It is possible that const_binop cannot handle the given
1428 code and return NULL_TREE */
1429 if (elts[i] == NULL_TREE)
1430 return NULL_TREE;
1431 }
1432
1433 return build_vector (type, elts);
1434 }
1435
1436 /* Shifts allow a scalar offset for a vector. */
1437 if (TREE_CODE (arg1) == VECTOR_CST
1438 && TREE_CODE (arg2) == INTEGER_CST)
1439 {
1440 tree type = TREE_TYPE (arg1);
1441 int count = TYPE_VECTOR_SUBPARTS (type), i;
1442 tree *elts = XALLOCAVEC (tree, count);
1443
1444 for (i = 0; i < count; i++)
1445 {
1446 tree elem1 = VECTOR_CST_ELT (arg1, i);
1447
1448 elts[i] = const_binop (code, elem1, arg2);
1449
1450 /* It is possible that const_binop cannot handle the given
1451 code and return NULL_TREE. */
1452 if (elts[i] == NULL_TREE)
1453 return NULL_TREE;
1454 }
1455
1456 return build_vector (type, elts);
1457 }
1458 return NULL_TREE;
1459 }
1460
1461 /* Overload that adds a TYPE parameter to be able to dispatch
1462 to fold_relational_const. */
1463
1464 tree
1465 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1466 {
1467 if (TREE_CODE_CLASS (code) == tcc_comparison)
1468 return fold_relational_const (code, type, arg1, arg2);
1469
1470 /* ??? Until we make the const_binop worker take the type of the
1471 result as argument put those cases that need it here. */
1472 switch (code)
1473 {
1474 case COMPLEX_EXPR:
1475 if ((TREE_CODE (arg1) == REAL_CST
1476 && TREE_CODE (arg2) == REAL_CST)
1477 || (TREE_CODE (arg1) == INTEGER_CST
1478 && TREE_CODE (arg2) == INTEGER_CST))
1479 return build_complex (type, arg1, arg2);
1480 return NULL_TREE;
1481
1482 case VEC_PACK_TRUNC_EXPR:
1483 case VEC_PACK_FIX_TRUNC_EXPR:
1484 {
1485 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1486 tree *elts;
1487
1488 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1489 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1490 if (TREE_CODE (arg1) != VECTOR_CST
1491 || TREE_CODE (arg2) != VECTOR_CST)
1492 return NULL_TREE;
1493
1494 elts = XALLOCAVEC (tree, nelts);
1495 if (!vec_cst_ctor_to_array (arg1, elts)
1496 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1497 return NULL_TREE;
1498
1499 for (i = 0; i < nelts; i++)
1500 {
1501 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1502 ? NOP_EXPR : FIX_TRUNC_EXPR,
1503 TREE_TYPE (type), elts[i]);
1504 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1505 return NULL_TREE;
1506 }
1507
1508 return build_vector (type, elts);
1509 }
1510
1511 case VEC_WIDEN_MULT_LO_EXPR:
1512 case VEC_WIDEN_MULT_HI_EXPR:
1513 case VEC_WIDEN_MULT_EVEN_EXPR:
1514 case VEC_WIDEN_MULT_ODD_EXPR:
1515 {
1516 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1517 unsigned int out, ofs, scale;
1518 tree *elts;
1519
1520 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1521 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1522 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1523 return NULL_TREE;
1524
1525 elts = XALLOCAVEC (tree, nelts * 4);
1526 if (!vec_cst_ctor_to_array (arg1, elts)
1527 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1528 return NULL_TREE;
1529
1530 if (code == VEC_WIDEN_MULT_LO_EXPR)
1531 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1532 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1533 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1534 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1535 scale = 1, ofs = 0;
1536 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1537 scale = 1, ofs = 1;
1538
1539 for (out = 0; out < nelts; out++)
1540 {
1541 unsigned int in1 = (out << scale) + ofs;
1542 unsigned int in2 = in1 + nelts * 2;
1543 tree t1, t2;
1544
1545 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1546 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1547
1548 if (t1 == NULL_TREE || t2 == NULL_TREE)
1549 return NULL_TREE;
1550 elts[out] = const_binop (MULT_EXPR, t1, t2);
1551 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1552 return NULL_TREE;
1553 }
1554
1555 return build_vector (type, elts);
1556 }
1557
1558 default:;
1559 }
1560
1561 if (TREE_CODE_CLASS (code) != tcc_binary)
1562 return NULL_TREE;
1563
1564 /* Make sure type and arg0 have the same saturating flag. */
1565 gcc_checking_assert (TYPE_SATURATING (type)
1566 == TYPE_SATURATING (TREE_TYPE (arg1)));
1567
1568 return const_binop (code, arg1, arg2);
1569 }
1570
1571 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1572 Return zero if computing the constants is not possible. */
1573
1574 tree
1575 const_unop (enum tree_code code, tree type, tree arg0)
1576 {
1577 /* Don't perform the operation, other than NEGATE and ABS, if
1578 flag_signaling_nans is on and the operand is a signaling NaN. */
1579 if (TREE_CODE (arg0) == REAL_CST
1580 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1581 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1582 && code != NEGATE_EXPR
1583 && code != ABS_EXPR)
1584 return NULL_TREE;
1585
1586 switch (code)
1587 {
1588 CASE_CONVERT:
1589 case FLOAT_EXPR:
1590 case FIX_TRUNC_EXPR:
1591 case FIXED_CONVERT_EXPR:
1592 return fold_convert_const (code, type, arg0);
1593
1594 case ADDR_SPACE_CONVERT_EXPR:
1595 /* If the source address is 0, and the source address space
1596 cannot have a valid object at 0, fold to dest type null. */
1597 if (integer_zerop (arg0)
1598 && !(targetm.addr_space.zero_address_valid
1599 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1600 return fold_convert_const (code, type, arg0);
1601 break;
1602
1603 case VIEW_CONVERT_EXPR:
1604 return fold_view_convert_expr (type, arg0);
1605
1606 case NEGATE_EXPR:
1607 {
1608 /* Can't call fold_negate_const directly here as that doesn't
1609 handle all cases and we might not be able to negate some
1610 constants. */
1611 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1612 if (tem && CONSTANT_CLASS_P (tem))
1613 return tem;
1614 break;
1615 }
1616
1617 case ABS_EXPR:
1618 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1619 return fold_abs_const (arg0, type);
1620 break;
1621
1622 case CONJ_EXPR:
1623 if (TREE_CODE (arg0) == COMPLEX_CST)
1624 {
1625 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1626 TREE_TYPE (type));
1627 return build_complex (type, TREE_REALPART (arg0), ipart);
1628 }
1629 break;
1630
1631 case BIT_NOT_EXPR:
1632 if (TREE_CODE (arg0) == INTEGER_CST)
1633 return fold_not_const (arg0, type);
1634 /* Perform BIT_NOT_EXPR on each element individually. */
1635 else if (TREE_CODE (arg0) == VECTOR_CST)
1636 {
1637 tree *elements;
1638 tree elem;
1639 unsigned count = VECTOR_CST_NELTS (arg0), i;
1640
1641 elements = XALLOCAVEC (tree, count);
1642 for (i = 0; i < count; i++)
1643 {
1644 elem = VECTOR_CST_ELT (arg0, i);
1645 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1646 if (elem == NULL_TREE)
1647 break;
1648 elements[i] = elem;
1649 }
1650 if (i == count)
1651 return build_vector (type, elements);
1652 }
1653 break;
1654
1655 case TRUTH_NOT_EXPR:
1656 if (TREE_CODE (arg0) == INTEGER_CST)
1657 return constant_boolean_node (integer_zerop (arg0), type);
1658 break;
1659
1660 case REALPART_EXPR:
1661 if (TREE_CODE (arg0) == COMPLEX_CST)
1662 return fold_convert (type, TREE_REALPART (arg0));
1663 break;
1664
1665 case IMAGPART_EXPR:
1666 if (TREE_CODE (arg0) == COMPLEX_CST)
1667 return fold_convert (type, TREE_IMAGPART (arg0));
1668 break;
1669
1670 case VEC_UNPACK_LO_EXPR:
1671 case VEC_UNPACK_HI_EXPR:
1672 case VEC_UNPACK_FLOAT_LO_EXPR:
1673 case VEC_UNPACK_FLOAT_HI_EXPR:
1674 {
1675 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1676 tree *elts;
1677 enum tree_code subcode;
1678
1679 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1680 if (TREE_CODE (arg0) != VECTOR_CST)
1681 return NULL_TREE;
1682
1683 elts = XALLOCAVEC (tree, nelts * 2);
1684 if (!vec_cst_ctor_to_array (arg0, elts))
1685 return NULL_TREE;
1686
1687 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1688 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1689 elts += nelts;
1690
1691 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1692 subcode = NOP_EXPR;
1693 else
1694 subcode = FLOAT_EXPR;
1695
1696 for (i = 0; i < nelts; i++)
1697 {
1698 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1699 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1700 return NULL_TREE;
1701 }
1702
1703 return build_vector (type, elts);
1704 }
1705
1706 case REDUC_MIN_EXPR:
1707 case REDUC_MAX_EXPR:
1708 case REDUC_PLUS_EXPR:
1709 {
1710 unsigned int nelts, i;
1711 tree *elts;
1712 enum tree_code subcode;
1713
1714 if (TREE_CODE (arg0) != VECTOR_CST)
1715 return NULL_TREE;
1716 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1717
1718 elts = XALLOCAVEC (tree, nelts);
1719 if (!vec_cst_ctor_to_array (arg0, elts))
1720 return NULL_TREE;
1721
1722 switch (code)
1723 {
1724 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1725 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1726 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1727 default: gcc_unreachable ();
1728 }
1729
1730 for (i = 1; i < nelts; i++)
1731 {
1732 elts[0] = const_binop (subcode, elts[0], elts[i]);
1733 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1734 return NULL_TREE;
1735 }
1736
1737 return elts[0];
1738 }
1739
1740 default:
1741 break;
1742 }
1743
1744 return NULL_TREE;
1745 }
1746
1747 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1748 indicates which particular sizetype to create. */
1749
1750 tree
1751 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1752 {
1753 return build_int_cst (sizetype_tab[(int) kind], number);
1754 }
1755 \f
1756 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1757 is a tree code. The type of the result is taken from the operands.
1758 Both must be equivalent integer types, ala int_binop_types_match_p.
1759 If the operands are constant, so is the result. */
1760
1761 tree
1762 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1763 {
1764 tree type = TREE_TYPE (arg0);
1765
1766 if (arg0 == error_mark_node || arg1 == error_mark_node)
1767 return error_mark_node;
1768
1769 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1770 TREE_TYPE (arg1)));
1771
1772 /* Handle the special case of two integer constants faster. */
1773 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1774 {
1775 /* And some specific cases even faster than that. */
1776 if (code == PLUS_EXPR)
1777 {
1778 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1779 return arg1;
1780 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1781 return arg0;
1782 }
1783 else if (code == MINUS_EXPR)
1784 {
1785 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1786 return arg0;
1787 }
1788 else if (code == MULT_EXPR)
1789 {
1790 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1791 return arg1;
1792 }
1793
1794 /* Handle general case of two integer constants. For sizetype
1795 constant calculations we always want to know about overflow,
1796 even in the unsigned case. */
1797 return int_const_binop_1 (code, arg0, arg1, -1);
1798 }
1799
1800 return fold_build2_loc (loc, code, type, arg0, arg1);
1801 }
1802
1803 /* Given two values, either both of sizetype or both of bitsizetype,
1804 compute the difference between the two values. Return the value
1805 in signed type corresponding to the type of the operands. */
1806
1807 tree
1808 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1809 {
1810 tree type = TREE_TYPE (arg0);
1811 tree ctype;
1812
1813 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1814 TREE_TYPE (arg1)));
1815
1816 /* If the type is already signed, just do the simple thing. */
1817 if (!TYPE_UNSIGNED (type))
1818 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1819
1820 if (type == sizetype)
1821 ctype = ssizetype;
1822 else if (type == bitsizetype)
1823 ctype = sbitsizetype;
1824 else
1825 ctype = signed_type_for (type);
1826
1827 /* If either operand is not a constant, do the conversions to the signed
1828 type and subtract. The hardware will do the right thing with any
1829 overflow in the subtraction. */
1830 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1831 return size_binop_loc (loc, MINUS_EXPR,
1832 fold_convert_loc (loc, ctype, arg0),
1833 fold_convert_loc (loc, ctype, arg1));
1834
1835 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1836 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1837 overflow) and negate (which can't either). Special-case a result
1838 of zero while we're here. */
1839 if (tree_int_cst_equal (arg0, arg1))
1840 return build_int_cst (ctype, 0);
1841 else if (tree_int_cst_lt (arg1, arg0))
1842 return fold_convert_loc (loc, ctype,
1843 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1844 else
1845 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1846 fold_convert_loc (loc, ctype,
1847 size_binop_loc (loc,
1848 MINUS_EXPR,
1849 arg1, arg0)));
1850 }
1851 \f
1852 /* A subroutine of fold_convert_const handling conversions of an
1853 INTEGER_CST to another integer type. */
1854
1855 static tree
1856 fold_convert_const_int_from_int (tree type, const_tree arg1)
1857 {
1858 /* Given an integer constant, make new constant with new type,
1859 appropriately sign-extended or truncated. Use widest_int
1860 so that any extension is done according ARG1's type. */
1861 return force_fit_type (type, wi::to_widest (arg1),
1862 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1863 TREE_OVERFLOW (arg1));
1864 }
1865
1866 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1867 to an integer type. */
1868
1869 static tree
1870 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1871 {
1872 bool overflow = false;
1873 tree t;
1874
1875 /* The following code implements the floating point to integer
1876 conversion rules required by the Java Language Specification,
1877 that IEEE NaNs are mapped to zero and values that overflow
1878 the target precision saturate, i.e. values greater than
1879 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1880 are mapped to INT_MIN. These semantics are allowed by the
1881 C and C++ standards that simply state that the behavior of
1882 FP-to-integer conversion is unspecified upon overflow. */
1883
1884 wide_int val;
1885 REAL_VALUE_TYPE r;
1886 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1887
1888 switch (code)
1889 {
1890 case FIX_TRUNC_EXPR:
1891 real_trunc (&r, VOIDmode, &x);
1892 break;
1893
1894 default:
1895 gcc_unreachable ();
1896 }
1897
1898 /* If R is NaN, return zero and show we have an overflow. */
1899 if (REAL_VALUE_ISNAN (r))
1900 {
1901 overflow = true;
1902 val = wi::zero (TYPE_PRECISION (type));
1903 }
1904
1905 /* See if R is less than the lower bound or greater than the
1906 upper bound. */
1907
1908 if (! overflow)
1909 {
1910 tree lt = TYPE_MIN_VALUE (type);
1911 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1912 if (real_less (&r, &l))
1913 {
1914 overflow = true;
1915 val = lt;
1916 }
1917 }
1918
1919 if (! overflow)
1920 {
1921 tree ut = TYPE_MAX_VALUE (type);
1922 if (ut)
1923 {
1924 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1925 if (real_less (&u, &r))
1926 {
1927 overflow = true;
1928 val = ut;
1929 }
1930 }
1931 }
1932
1933 if (! overflow)
1934 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1935
1936 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1937 return t;
1938 }
1939
1940 /* A subroutine of fold_convert_const handling conversions of a
1941 FIXED_CST to an integer type. */
1942
1943 static tree
1944 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1945 {
1946 tree t;
1947 double_int temp, temp_trunc;
1948 unsigned int mode;
1949
1950 /* Right shift FIXED_CST to temp by fbit. */
1951 temp = TREE_FIXED_CST (arg1).data;
1952 mode = TREE_FIXED_CST (arg1).mode;
1953 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1954 {
1955 temp = temp.rshift (GET_MODE_FBIT (mode),
1956 HOST_BITS_PER_DOUBLE_INT,
1957 SIGNED_FIXED_POINT_MODE_P (mode));
1958
1959 /* Left shift temp to temp_trunc by fbit. */
1960 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1961 HOST_BITS_PER_DOUBLE_INT,
1962 SIGNED_FIXED_POINT_MODE_P (mode));
1963 }
1964 else
1965 {
1966 temp = double_int_zero;
1967 temp_trunc = double_int_zero;
1968 }
1969
1970 /* If FIXED_CST is negative, we need to round the value toward 0.
1971 By checking if the fractional bits are not zero to add 1 to temp. */
1972 if (SIGNED_FIXED_POINT_MODE_P (mode)
1973 && temp_trunc.is_negative ()
1974 && TREE_FIXED_CST (arg1).data != temp_trunc)
1975 temp += double_int_one;
1976
1977 /* Given a fixed-point constant, make new constant with new type,
1978 appropriately sign-extended or truncated. */
1979 t = force_fit_type (type, temp, -1,
1980 (temp.is_negative ()
1981 && (TYPE_UNSIGNED (type)
1982 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1983 | TREE_OVERFLOW (arg1));
1984
1985 return t;
1986 }
1987
1988 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1989 to another floating point type. */
1990
1991 static tree
1992 fold_convert_const_real_from_real (tree type, const_tree arg1)
1993 {
1994 REAL_VALUE_TYPE value;
1995 tree t;
1996
1997 /* Don't perform the operation if flag_signaling_nans is on
1998 and the operand is a signaling NaN. */
1999 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2000 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2001 return NULL_TREE;
2002
2003 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2004 t = build_real (type, value);
2005
2006 /* If converting an infinity or NAN to a representation that doesn't
2007 have one, set the overflow bit so that we can produce some kind of
2008 error message at the appropriate point if necessary. It's not the
2009 most user-friendly message, but it's better than nothing. */
2010 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2011 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2012 TREE_OVERFLOW (t) = 1;
2013 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2014 && !MODE_HAS_NANS (TYPE_MODE (type)))
2015 TREE_OVERFLOW (t) = 1;
2016 /* Regular overflow, conversion produced an infinity in a mode that
2017 can't represent them. */
2018 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2019 && REAL_VALUE_ISINF (value)
2020 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2021 TREE_OVERFLOW (t) = 1;
2022 else
2023 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2024 return t;
2025 }
2026
2027 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2028 to a floating point type. */
2029
2030 static tree
2031 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2032 {
2033 REAL_VALUE_TYPE value;
2034 tree t;
2035
2036 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2037 t = build_real (type, value);
2038
2039 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2040 return t;
2041 }
2042
2043 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2044 to another fixed-point type. */
2045
2046 static tree
2047 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2048 {
2049 FIXED_VALUE_TYPE value;
2050 tree t;
2051 bool overflow_p;
2052
2053 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2054 TYPE_SATURATING (type));
2055 t = build_fixed (type, value);
2056
2057 /* Propagate overflow flags. */
2058 if (overflow_p | TREE_OVERFLOW (arg1))
2059 TREE_OVERFLOW (t) = 1;
2060 return t;
2061 }
2062
2063 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2064 to a fixed-point type. */
2065
2066 static tree
2067 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2068 {
2069 FIXED_VALUE_TYPE value;
2070 tree t;
2071 bool overflow_p;
2072 double_int di;
2073
2074 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2075
2076 di.low = TREE_INT_CST_ELT (arg1, 0);
2077 if (TREE_INT_CST_NUNITS (arg1) == 1)
2078 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2079 else
2080 di.high = TREE_INT_CST_ELT (arg1, 1);
2081
2082 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2083 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2084 TYPE_SATURATING (type));
2085 t = build_fixed (type, value);
2086
2087 /* Propagate overflow flags. */
2088 if (overflow_p | TREE_OVERFLOW (arg1))
2089 TREE_OVERFLOW (t) = 1;
2090 return t;
2091 }
2092
2093 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2094 to a fixed-point type. */
2095
2096 static tree
2097 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2098 {
2099 FIXED_VALUE_TYPE value;
2100 tree t;
2101 bool overflow_p;
2102
2103 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2104 &TREE_REAL_CST (arg1),
2105 TYPE_SATURATING (type));
2106 t = build_fixed (type, value);
2107
2108 /* Propagate overflow flags. */
2109 if (overflow_p | TREE_OVERFLOW (arg1))
2110 TREE_OVERFLOW (t) = 1;
2111 return t;
2112 }
2113
2114 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2115 type TYPE. If no simplification can be done return NULL_TREE. */
2116
2117 static tree
2118 fold_convert_const (enum tree_code code, tree type, tree arg1)
2119 {
2120 if (TREE_TYPE (arg1) == type)
2121 return arg1;
2122
2123 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2124 || TREE_CODE (type) == OFFSET_TYPE)
2125 {
2126 if (TREE_CODE (arg1) == INTEGER_CST)
2127 return fold_convert_const_int_from_int (type, arg1);
2128 else if (TREE_CODE (arg1) == REAL_CST)
2129 return fold_convert_const_int_from_real (code, type, arg1);
2130 else if (TREE_CODE (arg1) == FIXED_CST)
2131 return fold_convert_const_int_from_fixed (type, arg1);
2132 }
2133 else if (TREE_CODE (type) == REAL_TYPE)
2134 {
2135 if (TREE_CODE (arg1) == INTEGER_CST)
2136 return build_real_from_int_cst (type, arg1);
2137 else if (TREE_CODE (arg1) == REAL_CST)
2138 return fold_convert_const_real_from_real (type, arg1);
2139 else if (TREE_CODE (arg1) == FIXED_CST)
2140 return fold_convert_const_real_from_fixed (type, arg1);
2141 }
2142 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2143 {
2144 if (TREE_CODE (arg1) == FIXED_CST)
2145 return fold_convert_const_fixed_from_fixed (type, arg1);
2146 else if (TREE_CODE (arg1) == INTEGER_CST)
2147 return fold_convert_const_fixed_from_int (type, arg1);
2148 else if (TREE_CODE (arg1) == REAL_CST)
2149 return fold_convert_const_fixed_from_real (type, arg1);
2150 }
2151 else if (TREE_CODE (type) == VECTOR_TYPE)
2152 {
2153 if (TREE_CODE (arg1) == VECTOR_CST
2154 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2155 {
2156 int len = TYPE_VECTOR_SUBPARTS (type);
2157 tree elttype = TREE_TYPE (type);
2158 tree *v = XALLOCAVEC (tree, len);
2159 for (int i = 0; i < len; ++i)
2160 {
2161 tree elt = VECTOR_CST_ELT (arg1, i);
2162 tree cvt = fold_convert_const (code, elttype, elt);
2163 if (cvt == NULL_TREE)
2164 return NULL_TREE;
2165 v[i] = cvt;
2166 }
2167 return build_vector (type, v);
2168 }
2169 }
2170 return NULL_TREE;
2171 }
2172
2173 /* Construct a vector of zero elements of vector type TYPE. */
2174
2175 static tree
2176 build_zero_vector (tree type)
2177 {
2178 tree t;
2179
2180 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2181 return build_vector_from_val (type, t);
2182 }
2183
2184 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2185
2186 bool
2187 fold_convertible_p (const_tree type, const_tree arg)
2188 {
2189 tree orig = TREE_TYPE (arg);
2190
2191 if (type == orig)
2192 return true;
2193
2194 if (TREE_CODE (arg) == ERROR_MARK
2195 || TREE_CODE (type) == ERROR_MARK
2196 || TREE_CODE (orig) == ERROR_MARK)
2197 return false;
2198
2199 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2200 return true;
2201
2202 switch (TREE_CODE (type))
2203 {
2204 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2205 case POINTER_TYPE: case REFERENCE_TYPE:
2206 case OFFSET_TYPE:
2207 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2208 || TREE_CODE (orig) == OFFSET_TYPE);
2209
2210 case REAL_TYPE:
2211 case FIXED_POINT_TYPE:
2212 case VECTOR_TYPE:
2213 case VOID_TYPE:
2214 return TREE_CODE (type) == TREE_CODE (orig);
2215
2216 default:
2217 return false;
2218 }
2219 }
2220
2221 /* Convert expression ARG to type TYPE. Used by the middle-end for
2222 simple conversions in preference to calling the front-end's convert. */
2223
2224 tree
2225 fold_convert_loc (location_t loc, tree type, tree arg)
2226 {
2227 tree orig = TREE_TYPE (arg);
2228 tree tem;
2229
2230 if (type == orig)
2231 return arg;
2232
2233 if (TREE_CODE (arg) == ERROR_MARK
2234 || TREE_CODE (type) == ERROR_MARK
2235 || TREE_CODE (orig) == ERROR_MARK)
2236 return error_mark_node;
2237
2238 switch (TREE_CODE (type))
2239 {
2240 case POINTER_TYPE:
2241 case REFERENCE_TYPE:
2242 /* Handle conversions between pointers to different address spaces. */
2243 if (POINTER_TYPE_P (orig)
2244 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2245 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2246 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2247 /* fall through */
2248
2249 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2250 case OFFSET_TYPE:
2251 if (TREE_CODE (arg) == INTEGER_CST)
2252 {
2253 tem = fold_convert_const (NOP_EXPR, type, arg);
2254 if (tem != NULL_TREE)
2255 return tem;
2256 }
2257 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2258 || TREE_CODE (orig) == OFFSET_TYPE)
2259 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2260 if (TREE_CODE (orig) == COMPLEX_TYPE)
2261 return fold_convert_loc (loc, type,
2262 fold_build1_loc (loc, REALPART_EXPR,
2263 TREE_TYPE (orig), arg));
2264 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2265 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2266 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2267
2268 case REAL_TYPE:
2269 if (TREE_CODE (arg) == INTEGER_CST)
2270 {
2271 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2272 if (tem != NULL_TREE)
2273 return tem;
2274 }
2275 else if (TREE_CODE (arg) == REAL_CST)
2276 {
2277 tem = fold_convert_const (NOP_EXPR, type, arg);
2278 if (tem != NULL_TREE)
2279 return tem;
2280 }
2281 else if (TREE_CODE (arg) == FIXED_CST)
2282 {
2283 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2284 if (tem != NULL_TREE)
2285 return tem;
2286 }
2287
2288 switch (TREE_CODE (orig))
2289 {
2290 case INTEGER_TYPE:
2291 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2292 case POINTER_TYPE: case REFERENCE_TYPE:
2293 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2294
2295 case REAL_TYPE:
2296 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2297
2298 case FIXED_POINT_TYPE:
2299 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2300
2301 case COMPLEX_TYPE:
2302 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2303 return fold_convert_loc (loc, type, tem);
2304
2305 default:
2306 gcc_unreachable ();
2307 }
2308
2309 case FIXED_POINT_TYPE:
2310 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2311 || TREE_CODE (arg) == REAL_CST)
2312 {
2313 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2314 if (tem != NULL_TREE)
2315 goto fold_convert_exit;
2316 }
2317
2318 switch (TREE_CODE (orig))
2319 {
2320 case FIXED_POINT_TYPE:
2321 case INTEGER_TYPE:
2322 case ENUMERAL_TYPE:
2323 case BOOLEAN_TYPE:
2324 case REAL_TYPE:
2325 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2326
2327 case COMPLEX_TYPE:
2328 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2329 return fold_convert_loc (loc, type, tem);
2330
2331 default:
2332 gcc_unreachable ();
2333 }
2334
2335 case COMPLEX_TYPE:
2336 switch (TREE_CODE (orig))
2337 {
2338 case INTEGER_TYPE:
2339 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2340 case POINTER_TYPE: case REFERENCE_TYPE:
2341 case REAL_TYPE:
2342 case FIXED_POINT_TYPE:
2343 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2344 fold_convert_loc (loc, TREE_TYPE (type), arg),
2345 fold_convert_loc (loc, TREE_TYPE (type),
2346 integer_zero_node));
2347 case COMPLEX_TYPE:
2348 {
2349 tree rpart, ipart;
2350
2351 if (TREE_CODE (arg) == COMPLEX_EXPR)
2352 {
2353 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2354 TREE_OPERAND (arg, 0));
2355 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2356 TREE_OPERAND (arg, 1));
2357 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2358 }
2359
2360 arg = save_expr (arg);
2361 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2362 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2363 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2364 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2365 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2366 }
2367
2368 default:
2369 gcc_unreachable ();
2370 }
2371
2372 case VECTOR_TYPE:
2373 if (integer_zerop (arg))
2374 return build_zero_vector (type);
2375 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2376 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2377 || TREE_CODE (orig) == VECTOR_TYPE);
2378 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2379
2380 case VOID_TYPE:
2381 tem = fold_ignored_result (arg);
2382 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2383
2384 default:
2385 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2386 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2387 gcc_unreachable ();
2388 }
2389 fold_convert_exit:
2390 protected_set_expr_location_unshare (tem, loc);
2391 return tem;
2392 }
2393 \f
2394 /* Return false if expr can be assumed not to be an lvalue, true
2395 otherwise. */
2396
2397 static bool
2398 maybe_lvalue_p (const_tree x)
2399 {
2400 /* We only need to wrap lvalue tree codes. */
2401 switch (TREE_CODE (x))
2402 {
2403 case VAR_DECL:
2404 case PARM_DECL:
2405 case RESULT_DECL:
2406 case LABEL_DECL:
2407 case FUNCTION_DECL:
2408 case SSA_NAME:
2409
2410 case COMPONENT_REF:
2411 case MEM_REF:
2412 case INDIRECT_REF:
2413 case ARRAY_REF:
2414 case ARRAY_RANGE_REF:
2415 case BIT_FIELD_REF:
2416 case OBJ_TYPE_REF:
2417
2418 case REALPART_EXPR:
2419 case IMAGPART_EXPR:
2420 case PREINCREMENT_EXPR:
2421 case PREDECREMENT_EXPR:
2422 case SAVE_EXPR:
2423 case TRY_CATCH_EXPR:
2424 case WITH_CLEANUP_EXPR:
2425 case COMPOUND_EXPR:
2426 case MODIFY_EXPR:
2427 case TARGET_EXPR:
2428 case COND_EXPR:
2429 case BIND_EXPR:
2430 break;
2431
2432 default:
2433 /* Assume the worst for front-end tree codes. */
2434 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2435 break;
2436 return false;
2437 }
2438
2439 return true;
2440 }
2441
2442 /* Return an expr equal to X but certainly not valid as an lvalue. */
2443
2444 tree
2445 non_lvalue_loc (location_t loc, tree x)
2446 {
2447 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2448 us. */
2449 if (in_gimple_form)
2450 return x;
2451
2452 if (! maybe_lvalue_p (x))
2453 return x;
2454 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2455 }
2456
2457 /* When pedantic, return an expr equal to X but certainly not valid as a
2458 pedantic lvalue. Otherwise, return X. */
2459
2460 static tree
2461 pedantic_non_lvalue_loc (location_t loc, tree x)
2462 {
2463 return protected_set_expr_location_unshare (x, loc);
2464 }
2465 \f
2466 /* Given a tree comparison code, return the code that is the logical inverse.
2467 It is generally not safe to do this for floating-point comparisons, except
2468 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2469 ERROR_MARK in this case. */
2470
2471 enum tree_code
2472 invert_tree_comparison (enum tree_code code, bool honor_nans)
2473 {
2474 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2475 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2476 return ERROR_MARK;
2477
2478 switch (code)
2479 {
2480 case EQ_EXPR:
2481 return NE_EXPR;
2482 case NE_EXPR:
2483 return EQ_EXPR;
2484 case GT_EXPR:
2485 return honor_nans ? UNLE_EXPR : LE_EXPR;
2486 case GE_EXPR:
2487 return honor_nans ? UNLT_EXPR : LT_EXPR;
2488 case LT_EXPR:
2489 return honor_nans ? UNGE_EXPR : GE_EXPR;
2490 case LE_EXPR:
2491 return honor_nans ? UNGT_EXPR : GT_EXPR;
2492 case LTGT_EXPR:
2493 return UNEQ_EXPR;
2494 case UNEQ_EXPR:
2495 return LTGT_EXPR;
2496 case UNGT_EXPR:
2497 return LE_EXPR;
2498 case UNGE_EXPR:
2499 return LT_EXPR;
2500 case UNLT_EXPR:
2501 return GE_EXPR;
2502 case UNLE_EXPR:
2503 return GT_EXPR;
2504 case ORDERED_EXPR:
2505 return UNORDERED_EXPR;
2506 case UNORDERED_EXPR:
2507 return ORDERED_EXPR;
2508 default:
2509 gcc_unreachable ();
2510 }
2511 }
2512
2513 /* Similar, but return the comparison that results if the operands are
2514 swapped. This is safe for floating-point. */
2515
2516 enum tree_code
2517 swap_tree_comparison (enum tree_code code)
2518 {
2519 switch (code)
2520 {
2521 case EQ_EXPR:
2522 case NE_EXPR:
2523 case ORDERED_EXPR:
2524 case UNORDERED_EXPR:
2525 case LTGT_EXPR:
2526 case UNEQ_EXPR:
2527 return code;
2528 case GT_EXPR:
2529 return LT_EXPR;
2530 case GE_EXPR:
2531 return LE_EXPR;
2532 case LT_EXPR:
2533 return GT_EXPR;
2534 case LE_EXPR:
2535 return GE_EXPR;
2536 case UNGT_EXPR:
2537 return UNLT_EXPR;
2538 case UNGE_EXPR:
2539 return UNLE_EXPR;
2540 case UNLT_EXPR:
2541 return UNGT_EXPR;
2542 case UNLE_EXPR:
2543 return UNGE_EXPR;
2544 default:
2545 gcc_unreachable ();
2546 }
2547 }
2548
2549
2550 /* Convert a comparison tree code from an enum tree_code representation
2551 into a compcode bit-based encoding. This function is the inverse of
2552 compcode_to_comparison. */
2553
2554 static enum comparison_code
2555 comparison_to_compcode (enum tree_code code)
2556 {
2557 switch (code)
2558 {
2559 case LT_EXPR:
2560 return COMPCODE_LT;
2561 case EQ_EXPR:
2562 return COMPCODE_EQ;
2563 case LE_EXPR:
2564 return COMPCODE_LE;
2565 case GT_EXPR:
2566 return COMPCODE_GT;
2567 case NE_EXPR:
2568 return COMPCODE_NE;
2569 case GE_EXPR:
2570 return COMPCODE_GE;
2571 case ORDERED_EXPR:
2572 return COMPCODE_ORD;
2573 case UNORDERED_EXPR:
2574 return COMPCODE_UNORD;
2575 case UNLT_EXPR:
2576 return COMPCODE_UNLT;
2577 case UNEQ_EXPR:
2578 return COMPCODE_UNEQ;
2579 case UNLE_EXPR:
2580 return COMPCODE_UNLE;
2581 case UNGT_EXPR:
2582 return COMPCODE_UNGT;
2583 case LTGT_EXPR:
2584 return COMPCODE_LTGT;
2585 case UNGE_EXPR:
2586 return COMPCODE_UNGE;
2587 default:
2588 gcc_unreachable ();
2589 }
2590 }
2591
2592 /* Convert a compcode bit-based encoding of a comparison operator back
2593 to GCC's enum tree_code representation. This function is the
2594 inverse of comparison_to_compcode. */
2595
2596 static enum tree_code
2597 compcode_to_comparison (enum comparison_code code)
2598 {
2599 switch (code)
2600 {
2601 case COMPCODE_LT:
2602 return LT_EXPR;
2603 case COMPCODE_EQ:
2604 return EQ_EXPR;
2605 case COMPCODE_LE:
2606 return LE_EXPR;
2607 case COMPCODE_GT:
2608 return GT_EXPR;
2609 case COMPCODE_NE:
2610 return NE_EXPR;
2611 case COMPCODE_GE:
2612 return GE_EXPR;
2613 case COMPCODE_ORD:
2614 return ORDERED_EXPR;
2615 case COMPCODE_UNORD:
2616 return UNORDERED_EXPR;
2617 case COMPCODE_UNLT:
2618 return UNLT_EXPR;
2619 case COMPCODE_UNEQ:
2620 return UNEQ_EXPR;
2621 case COMPCODE_UNLE:
2622 return UNLE_EXPR;
2623 case COMPCODE_UNGT:
2624 return UNGT_EXPR;
2625 case COMPCODE_LTGT:
2626 return LTGT_EXPR;
2627 case COMPCODE_UNGE:
2628 return UNGE_EXPR;
2629 default:
2630 gcc_unreachable ();
2631 }
2632 }
2633
2634 /* Return a tree for the comparison which is the combination of
2635 doing the AND or OR (depending on CODE) of the two operations LCODE
2636 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2637 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2638 if this makes the transformation invalid. */
2639
2640 tree
2641 combine_comparisons (location_t loc,
2642 enum tree_code code, enum tree_code lcode,
2643 enum tree_code rcode, tree truth_type,
2644 tree ll_arg, tree lr_arg)
2645 {
2646 bool honor_nans = HONOR_NANS (ll_arg);
2647 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2648 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2649 int compcode;
2650
2651 switch (code)
2652 {
2653 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2654 compcode = lcompcode & rcompcode;
2655 break;
2656
2657 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2658 compcode = lcompcode | rcompcode;
2659 break;
2660
2661 default:
2662 return NULL_TREE;
2663 }
2664
2665 if (!honor_nans)
2666 {
2667 /* Eliminate unordered comparisons, as well as LTGT and ORD
2668 which are not used unless the mode has NaNs. */
2669 compcode &= ~COMPCODE_UNORD;
2670 if (compcode == COMPCODE_LTGT)
2671 compcode = COMPCODE_NE;
2672 else if (compcode == COMPCODE_ORD)
2673 compcode = COMPCODE_TRUE;
2674 }
2675 else if (flag_trapping_math)
2676 {
2677 /* Check that the original operation and the optimized ones will trap
2678 under the same condition. */
2679 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2680 && (lcompcode != COMPCODE_EQ)
2681 && (lcompcode != COMPCODE_ORD);
2682 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2683 && (rcompcode != COMPCODE_EQ)
2684 && (rcompcode != COMPCODE_ORD);
2685 bool trap = (compcode & COMPCODE_UNORD) == 0
2686 && (compcode != COMPCODE_EQ)
2687 && (compcode != COMPCODE_ORD);
2688
2689 /* In a short-circuited boolean expression the LHS might be
2690 such that the RHS, if evaluated, will never trap. For
2691 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2692 if neither x nor y is NaN. (This is a mixed blessing: for
2693 example, the expression above will never trap, hence
2694 optimizing it to x < y would be invalid). */
2695 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2696 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2697 rtrap = false;
2698
2699 /* If the comparison was short-circuited, and only the RHS
2700 trapped, we may now generate a spurious trap. */
2701 if (rtrap && !ltrap
2702 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2703 return NULL_TREE;
2704
2705 /* If we changed the conditions that cause a trap, we lose. */
2706 if ((ltrap || rtrap) != trap)
2707 return NULL_TREE;
2708 }
2709
2710 if (compcode == COMPCODE_TRUE)
2711 return constant_boolean_node (true, truth_type);
2712 else if (compcode == COMPCODE_FALSE)
2713 return constant_boolean_node (false, truth_type);
2714 else
2715 {
2716 enum tree_code tcode;
2717
2718 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2719 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2720 }
2721 }
2722 \f
2723 /* Return nonzero if two operands (typically of the same tree node)
2724 are necessarily equal. FLAGS modifies behavior as follows:
2725
2726 If OEP_ONLY_CONST is set, only return nonzero for constants.
2727 This function tests whether the operands are indistinguishable;
2728 it does not test whether they are equal using C's == operation.
2729 The distinction is important for IEEE floating point, because
2730 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2731 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2732
2733 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2734 even though it may hold multiple values during a function.
2735 This is because a GCC tree node guarantees that nothing else is
2736 executed between the evaluation of its "operands" (which may often
2737 be evaluated in arbitrary order). Hence if the operands themselves
2738 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2739 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2740 unset means assuming isochronic (or instantaneous) tree equivalence.
2741 Unless comparing arbitrary expression trees, such as from different
2742 statements, this flag can usually be left unset.
2743
2744 If OEP_PURE_SAME is set, then pure functions with identical arguments
2745 are considered the same. It is used when the caller has other ways
2746 to ensure that global memory is unchanged in between.
2747
2748 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2749 not values of expressions.
2750
2751 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2752 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2753
2754 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2755 any operand with side effect. This is unnecesarily conservative in the
2756 case we know that arg0 and arg1 are in disjoint code paths (such as in
2757 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2758 addresses with TREE_CONSTANT flag set so we know that &var == &var
2759 even if var is volatile. */
2760
2761 int
2762 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2763 {
2764 /* When checking, verify at the outermost operand_equal_p call that
2765 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2766 hash value. */
2767 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2768 {
2769 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2770 {
2771 if (arg0 != arg1)
2772 {
2773 inchash::hash hstate0 (0), hstate1 (0);
2774 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2775 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2776 hashval_t h0 = hstate0.end ();
2777 hashval_t h1 = hstate1.end ();
2778 gcc_assert (h0 == h1);
2779 }
2780 return 1;
2781 }
2782 else
2783 return 0;
2784 }
2785
2786 /* If either is ERROR_MARK, they aren't equal. */
2787 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2788 || TREE_TYPE (arg0) == error_mark_node
2789 || TREE_TYPE (arg1) == error_mark_node)
2790 return 0;
2791
2792 /* Similar, if either does not have a type (like a released SSA name),
2793 they aren't equal. */
2794 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2795 return 0;
2796
2797 /* We cannot consider pointers to different address space equal. */
2798 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2799 && POINTER_TYPE_P (TREE_TYPE (arg1))
2800 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2801 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2802 return 0;
2803
2804 /* Check equality of integer constants before bailing out due to
2805 precision differences. */
2806 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2807 {
2808 /* Address of INTEGER_CST is not defined; check that we did not forget
2809 to drop the OEP_ADDRESS_OF flags. */
2810 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2811 return tree_int_cst_equal (arg0, arg1);
2812 }
2813
2814 if (!(flags & OEP_ADDRESS_OF))
2815 {
2816 /* If both types don't have the same signedness, then we can't consider
2817 them equal. We must check this before the STRIP_NOPS calls
2818 because they may change the signedness of the arguments. As pointers
2819 strictly don't have a signedness, require either two pointers or
2820 two non-pointers as well. */
2821 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2822 || POINTER_TYPE_P (TREE_TYPE (arg0))
2823 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2824 return 0;
2825
2826 /* If both types don't have the same precision, then it is not safe
2827 to strip NOPs. */
2828 if (element_precision (TREE_TYPE (arg0))
2829 != element_precision (TREE_TYPE (arg1)))
2830 return 0;
2831
2832 STRIP_NOPS (arg0);
2833 STRIP_NOPS (arg1);
2834 }
2835 #if 0
2836 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2837 sanity check once the issue is solved. */
2838 else
2839 /* Addresses of conversions and SSA_NAMEs (and many other things)
2840 are not defined. Check that we did not forget to drop the
2841 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2842 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2843 && TREE_CODE (arg0) != SSA_NAME);
2844 #endif
2845
2846 /* In case both args are comparisons but with different comparison
2847 code, try to swap the comparison operands of one arg to produce
2848 a match and compare that variant. */
2849 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2850 && COMPARISON_CLASS_P (arg0)
2851 && COMPARISON_CLASS_P (arg1))
2852 {
2853 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2854
2855 if (TREE_CODE (arg0) == swap_code)
2856 return operand_equal_p (TREE_OPERAND (arg0, 0),
2857 TREE_OPERAND (arg1, 1), flags)
2858 && operand_equal_p (TREE_OPERAND (arg0, 1),
2859 TREE_OPERAND (arg1, 0), flags);
2860 }
2861
2862 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2863 {
2864 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2865 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2866 ;
2867 else if (flags & OEP_ADDRESS_OF)
2868 {
2869 /* If we are interested in comparing addresses ignore
2870 MEM_REF wrappings of the base that can appear just for
2871 TBAA reasons. */
2872 if (TREE_CODE (arg0) == MEM_REF
2873 && DECL_P (arg1)
2874 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2875 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2876 && integer_zerop (TREE_OPERAND (arg0, 1)))
2877 return 1;
2878 else if (TREE_CODE (arg1) == MEM_REF
2879 && DECL_P (arg0)
2880 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2881 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2882 && integer_zerop (TREE_OPERAND (arg1, 1)))
2883 return 1;
2884 return 0;
2885 }
2886 else
2887 return 0;
2888 }
2889
2890 /* When not checking adddresses, this is needed for conversions and for
2891 COMPONENT_REF. Might as well play it safe and always test this. */
2892 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2893 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2894 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2895 && !(flags & OEP_ADDRESS_OF)))
2896 return 0;
2897
2898 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2899 We don't care about side effects in that case because the SAVE_EXPR
2900 takes care of that for us. In all other cases, two expressions are
2901 equal if they have no side effects. If we have two identical
2902 expressions with side effects that should be treated the same due
2903 to the only side effects being identical SAVE_EXPR's, that will
2904 be detected in the recursive calls below.
2905 If we are taking an invariant address of two identical objects
2906 they are necessarily equal as well. */
2907 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2908 && (TREE_CODE (arg0) == SAVE_EXPR
2909 || (flags & OEP_MATCH_SIDE_EFFECTS)
2910 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2911 return 1;
2912
2913 /* Next handle constant cases, those for which we can return 1 even
2914 if ONLY_CONST is set. */
2915 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2916 switch (TREE_CODE (arg0))
2917 {
2918 case INTEGER_CST:
2919 return tree_int_cst_equal (arg0, arg1);
2920
2921 case FIXED_CST:
2922 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2923 TREE_FIXED_CST (arg1));
2924
2925 case REAL_CST:
2926 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2927 return 1;
2928
2929
2930 if (!HONOR_SIGNED_ZEROS (arg0))
2931 {
2932 /* If we do not distinguish between signed and unsigned zero,
2933 consider them equal. */
2934 if (real_zerop (arg0) && real_zerop (arg1))
2935 return 1;
2936 }
2937 return 0;
2938
2939 case VECTOR_CST:
2940 {
2941 unsigned i;
2942
2943 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2944 return 0;
2945
2946 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2947 {
2948 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2949 VECTOR_CST_ELT (arg1, i), flags))
2950 return 0;
2951 }
2952 return 1;
2953 }
2954
2955 case COMPLEX_CST:
2956 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2957 flags)
2958 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2959 flags));
2960
2961 case STRING_CST:
2962 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2963 && ! memcmp (TREE_STRING_POINTER (arg0),
2964 TREE_STRING_POINTER (arg1),
2965 TREE_STRING_LENGTH (arg0)));
2966
2967 case ADDR_EXPR:
2968 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2969 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2970 flags | OEP_ADDRESS_OF
2971 | OEP_MATCH_SIDE_EFFECTS);
2972 case CONSTRUCTOR:
2973 /* In GIMPLE empty constructors are allowed in initializers of
2974 aggregates. */
2975 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
2976 default:
2977 break;
2978 }
2979
2980 if (flags & OEP_ONLY_CONST)
2981 return 0;
2982
2983 /* Define macros to test an operand from arg0 and arg1 for equality and a
2984 variant that allows null and views null as being different from any
2985 non-null value. In the latter case, if either is null, the both
2986 must be; otherwise, do the normal comparison. */
2987 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2988 TREE_OPERAND (arg1, N), flags)
2989
2990 #define OP_SAME_WITH_NULL(N) \
2991 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2992 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2993
2994 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2995 {
2996 case tcc_unary:
2997 /* Two conversions are equal only if signedness and modes match. */
2998 switch (TREE_CODE (arg0))
2999 {
3000 CASE_CONVERT:
3001 case FIX_TRUNC_EXPR:
3002 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3003 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3004 return 0;
3005 break;
3006 default:
3007 break;
3008 }
3009
3010 return OP_SAME (0);
3011
3012
3013 case tcc_comparison:
3014 case tcc_binary:
3015 if (OP_SAME (0) && OP_SAME (1))
3016 return 1;
3017
3018 /* For commutative ops, allow the other order. */
3019 return (commutative_tree_code (TREE_CODE (arg0))
3020 && operand_equal_p (TREE_OPERAND (arg0, 0),
3021 TREE_OPERAND (arg1, 1), flags)
3022 && operand_equal_p (TREE_OPERAND (arg0, 1),
3023 TREE_OPERAND (arg1, 0), flags));
3024
3025 case tcc_reference:
3026 /* If either of the pointer (or reference) expressions we are
3027 dereferencing contain a side effect, these cannot be equal,
3028 but their addresses can be. */
3029 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3030 && (TREE_SIDE_EFFECTS (arg0)
3031 || TREE_SIDE_EFFECTS (arg1)))
3032 return 0;
3033
3034 switch (TREE_CODE (arg0))
3035 {
3036 case INDIRECT_REF:
3037 if (!(flags & OEP_ADDRESS_OF)
3038 && (TYPE_ALIGN (TREE_TYPE (arg0))
3039 != TYPE_ALIGN (TREE_TYPE (arg1))))
3040 return 0;
3041 flags &= ~OEP_ADDRESS_OF;
3042 return OP_SAME (0);
3043
3044 case IMAGPART_EXPR:
3045 /* Require the same offset. */
3046 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3047 TYPE_SIZE (TREE_TYPE (arg1)),
3048 flags & ~OEP_ADDRESS_OF))
3049 return 0;
3050
3051 /* Fallthru. */
3052 case REALPART_EXPR:
3053 case VIEW_CONVERT_EXPR:
3054 return OP_SAME (0);
3055
3056 case TARGET_MEM_REF:
3057 case MEM_REF:
3058 if (!(flags & OEP_ADDRESS_OF))
3059 {
3060 /* Require equal access sizes */
3061 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3062 && (!TYPE_SIZE (TREE_TYPE (arg0))
3063 || !TYPE_SIZE (TREE_TYPE (arg1))
3064 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3065 TYPE_SIZE (TREE_TYPE (arg1)),
3066 flags)))
3067 return 0;
3068 /* Verify that access happens in similar types. */
3069 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3070 return 0;
3071 /* Verify that accesses are TBAA compatible. */
3072 if (!alias_ptr_types_compatible_p
3073 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3074 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3075 || (MR_DEPENDENCE_CLIQUE (arg0)
3076 != MR_DEPENDENCE_CLIQUE (arg1))
3077 || (MR_DEPENDENCE_BASE (arg0)
3078 != MR_DEPENDENCE_BASE (arg1)))
3079 return 0;
3080 /* Verify that alignment is compatible. */
3081 if (TYPE_ALIGN (TREE_TYPE (arg0))
3082 != TYPE_ALIGN (TREE_TYPE (arg1)))
3083 return 0;
3084 }
3085 flags &= ~OEP_ADDRESS_OF;
3086 return (OP_SAME (0) && OP_SAME (1)
3087 /* TARGET_MEM_REF require equal extra operands. */
3088 && (TREE_CODE (arg0) != TARGET_MEM_REF
3089 || (OP_SAME_WITH_NULL (2)
3090 && OP_SAME_WITH_NULL (3)
3091 && OP_SAME_WITH_NULL (4))));
3092
3093 case ARRAY_REF:
3094 case ARRAY_RANGE_REF:
3095 if (!OP_SAME (0))
3096 return 0;
3097 flags &= ~OEP_ADDRESS_OF;
3098 /* Compare the array index by value if it is constant first as we
3099 may have different types but same value here. */
3100 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3101 TREE_OPERAND (arg1, 1))
3102 || OP_SAME (1))
3103 && OP_SAME_WITH_NULL (2)
3104 && OP_SAME_WITH_NULL (3)
3105 /* Compare low bound and element size as with OEP_ADDRESS_OF
3106 we have to account for the offset of the ref. */
3107 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3108 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3109 || (operand_equal_p (array_ref_low_bound
3110 (CONST_CAST_TREE (arg0)),
3111 array_ref_low_bound
3112 (CONST_CAST_TREE (arg1)), flags)
3113 && operand_equal_p (array_ref_element_size
3114 (CONST_CAST_TREE (arg0)),
3115 array_ref_element_size
3116 (CONST_CAST_TREE (arg1)),
3117 flags))));
3118
3119 case COMPONENT_REF:
3120 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3121 may be NULL when we're called to compare MEM_EXPRs. */
3122 if (!OP_SAME_WITH_NULL (0)
3123 || !OP_SAME (1))
3124 return 0;
3125 flags &= ~OEP_ADDRESS_OF;
3126 return OP_SAME_WITH_NULL (2);
3127
3128 case BIT_FIELD_REF:
3129 if (!OP_SAME (0))
3130 return 0;
3131 flags &= ~OEP_ADDRESS_OF;
3132 return OP_SAME (1) && OP_SAME (2);
3133
3134 default:
3135 return 0;
3136 }
3137
3138 case tcc_expression:
3139 switch (TREE_CODE (arg0))
3140 {
3141 case ADDR_EXPR:
3142 /* Be sure we pass right ADDRESS_OF flag. */
3143 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3144 return operand_equal_p (TREE_OPERAND (arg0, 0),
3145 TREE_OPERAND (arg1, 0),
3146 flags | OEP_ADDRESS_OF);
3147
3148 case TRUTH_NOT_EXPR:
3149 return OP_SAME (0);
3150
3151 case TRUTH_ANDIF_EXPR:
3152 case TRUTH_ORIF_EXPR:
3153 return OP_SAME (0) && OP_SAME (1);
3154
3155 case FMA_EXPR:
3156 case WIDEN_MULT_PLUS_EXPR:
3157 case WIDEN_MULT_MINUS_EXPR:
3158 if (!OP_SAME (2))
3159 return 0;
3160 /* The multiplcation operands are commutative. */
3161 /* FALLTHRU */
3162
3163 case TRUTH_AND_EXPR:
3164 case TRUTH_OR_EXPR:
3165 case TRUTH_XOR_EXPR:
3166 if (OP_SAME (0) && OP_SAME (1))
3167 return 1;
3168
3169 /* Otherwise take into account this is a commutative operation. */
3170 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3171 TREE_OPERAND (arg1, 1), flags)
3172 && operand_equal_p (TREE_OPERAND (arg0, 1),
3173 TREE_OPERAND (arg1, 0), flags));
3174
3175 case COND_EXPR:
3176 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3177 return 0;
3178 flags &= ~OEP_ADDRESS_OF;
3179 return OP_SAME (0);
3180
3181 case VEC_COND_EXPR:
3182 case DOT_PROD_EXPR:
3183 case BIT_INSERT_EXPR:
3184 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3185
3186 case MODIFY_EXPR:
3187 case INIT_EXPR:
3188 case COMPOUND_EXPR:
3189 case PREDECREMENT_EXPR:
3190 case PREINCREMENT_EXPR:
3191 case POSTDECREMENT_EXPR:
3192 case POSTINCREMENT_EXPR:
3193 if (flags & OEP_LEXICOGRAPHIC)
3194 return OP_SAME (0) && OP_SAME (1);
3195 return 0;
3196
3197 case CLEANUP_POINT_EXPR:
3198 case EXPR_STMT:
3199 if (flags & OEP_LEXICOGRAPHIC)
3200 return OP_SAME (0);
3201 return 0;
3202
3203 default:
3204 return 0;
3205 }
3206
3207 case tcc_vl_exp:
3208 switch (TREE_CODE (arg0))
3209 {
3210 case CALL_EXPR:
3211 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3212 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3213 /* If not both CALL_EXPRs are either internal or normal function
3214 functions, then they are not equal. */
3215 return 0;
3216 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3217 {
3218 /* If the CALL_EXPRs call different internal functions, then they
3219 are not equal. */
3220 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3221 return 0;
3222 }
3223 else
3224 {
3225 /* If the CALL_EXPRs call different functions, then they are not
3226 equal. */
3227 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3228 flags))
3229 return 0;
3230 }
3231
3232 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3233 {
3234 unsigned int cef = call_expr_flags (arg0);
3235 if (flags & OEP_PURE_SAME)
3236 cef &= ECF_CONST | ECF_PURE;
3237 else
3238 cef &= ECF_CONST;
3239 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3240 return 0;
3241 }
3242
3243 /* Now see if all the arguments are the same. */
3244 {
3245 const_call_expr_arg_iterator iter0, iter1;
3246 const_tree a0, a1;
3247 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3248 a1 = first_const_call_expr_arg (arg1, &iter1);
3249 a0 && a1;
3250 a0 = next_const_call_expr_arg (&iter0),
3251 a1 = next_const_call_expr_arg (&iter1))
3252 if (! operand_equal_p (a0, a1, flags))
3253 return 0;
3254
3255 /* If we get here and both argument lists are exhausted
3256 then the CALL_EXPRs are equal. */
3257 return ! (a0 || a1);
3258 }
3259 default:
3260 return 0;
3261 }
3262
3263 case tcc_declaration:
3264 /* Consider __builtin_sqrt equal to sqrt. */
3265 return (TREE_CODE (arg0) == FUNCTION_DECL
3266 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3267 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3268 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3269
3270 case tcc_exceptional:
3271 if (TREE_CODE (arg0) == CONSTRUCTOR)
3272 {
3273 /* In GIMPLE constructors are used only to build vectors from
3274 elements. Individual elements in the constructor must be
3275 indexed in increasing order and form an initial sequence.
3276
3277 We make no effort to compare constructors in generic.
3278 (see sem_variable::equals in ipa-icf which can do so for
3279 constants). */
3280 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3281 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3282 return 0;
3283
3284 /* Be sure that vectors constructed have the same representation.
3285 We only tested element precision and modes to match.
3286 Vectors may be BLKmode and thus also check that the number of
3287 parts match. */
3288 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3289 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3290 return 0;
3291
3292 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3293 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3294 unsigned int len = vec_safe_length (v0);
3295
3296 if (len != vec_safe_length (v1))
3297 return 0;
3298
3299 for (unsigned int i = 0; i < len; i++)
3300 {
3301 constructor_elt *c0 = &(*v0)[i];
3302 constructor_elt *c1 = &(*v1)[i];
3303
3304 if (!operand_equal_p (c0->value, c1->value, flags)
3305 /* In GIMPLE the indexes can be either NULL or matching i.
3306 Double check this so we won't get false
3307 positives for GENERIC. */
3308 || (c0->index
3309 && (TREE_CODE (c0->index) != INTEGER_CST
3310 || !compare_tree_int (c0->index, i)))
3311 || (c1->index
3312 && (TREE_CODE (c1->index) != INTEGER_CST
3313 || !compare_tree_int (c1->index, i))))
3314 return 0;
3315 }
3316 return 1;
3317 }
3318 else if (TREE_CODE (arg0) == STATEMENT_LIST
3319 && (flags & OEP_LEXICOGRAPHIC))
3320 {
3321 /* Compare the STATEMENT_LISTs. */
3322 tree_stmt_iterator tsi1, tsi2;
3323 tree body1 = CONST_CAST_TREE (arg0);
3324 tree body2 = CONST_CAST_TREE (arg1);
3325 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3326 tsi_next (&tsi1), tsi_next (&tsi2))
3327 {
3328 /* The lists don't have the same number of statements. */
3329 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3330 return 0;
3331 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3332 return 1;
3333 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3334 OEP_LEXICOGRAPHIC))
3335 return 0;
3336 }
3337 }
3338 return 0;
3339
3340 case tcc_statement:
3341 switch (TREE_CODE (arg0))
3342 {
3343 case RETURN_EXPR:
3344 if (flags & OEP_LEXICOGRAPHIC)
3345 return OP_SAME_WITH_NULL (0);
3346 return 0;
3347 default:
3348 return 0;
3349 }
3350
3351 default:
3352 return 0;
3353 }
3354
3355 #undef OP_SAME
3356 #undef OP_SAME_WITH_NULL
3357 }
3358 \f
3359 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3360 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3361
3362 When in doubt, return 0. */
3363
3364 static int
3365 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3366 {
3367 int unsignedp1, unsignedpo;
3368 tree primarg0, primarg1, primother;
3369 unsigned int correct_width;
3370
3371 if (operand_equal_p (arg0, arg1, 0))
3372 return 1;
3373
3374 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3375 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3376 return 0;
3377
3378 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3379 and see if the inner values are the same. This removes any
3380 signedness comparison, which doesn't matter here. */
3381 primarg0 = arg0, primarg1 = arg1;
3382 STRIP_NOPS (primarg0);
3383 STRIP_NOPS (primarg1);
3384 if (operand_equal_p (primarg0, primarg1, 0))
3385 return 1;
3386
3387 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3388 actual comparison operand, ARG0.
3389
3390 First throw away any conversions to wider types
3391 already present in the operands. */
3392
3393 primarg1 = get_narrower (arg1, &unsignedp1);
3394 primother = get_narrower (other, &unsignedpo);
3395
3396 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3397 if (unsignedp1 == unsignedpo
3398 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3399 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3400 {
3401 tree type = TREE_TYPE (arg0);
3402
3403 /* Make sure shorter operand is extended the right way
3404 to match the longer operand. */
3405 primarg1 = fold_convert (signed_or_unsigned_type_for
3406 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3407
3408 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3409 return 1;
3410 }
3411
3412 return 0;
3413 }
3414 \f
3415 /* See if ARG is an expression that is either a comparison or is performing
3416 arithmetic on comparisons. The comparisons must only be comparing
3417 two different values, which will be stored in *CVAL1 and *CVAL2; if
3418 they are nonzero it means that some operands have already been found.
3419 No variables may be used anywhere else in the expression except in the
3420 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3421 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3422
3423 If this is true, return 1. Otherwise, return zero. */
3424
3425 static int
3426 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3427 {
3428 enum tree_code code = TREE_CODE (arg);
3429 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3430
3431 /* We can handle some of the tcc_expression cases here. */
3432 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3433 tclass = tcc_unary;
3434 else if (tclass == tcc_expression
3435 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3436 || code == COMPOUND_EXPR))
3437 tclass = tcc_binary;
3438
3439 else if (tclass == tcc_expression && code == SAVE_EXPR
3440 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3441 {
3442 /* If we've already found a CVAL1 or CVAL2, this expression is
3443 two complex to handle. */
3444 if (*cval1 || *cval2)
3445 return 0;
3446
3447 tclass = tcc_unary;
3448 *save_p = 1;
3449 }
3450
3451 switch (tclass)
3452 {
3453 case tcc_unary:
3454 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3455
3456 case tcc_binary:
3457 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3458 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3459 cval1, cval2, save_p));
3460
3461 case tcc_constant:
3462 return 1;
3463
3464 case tcc_expression:
3465 if (code == COND_EXPR)
3466 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3467 cval1, cval2, save_p)
3468 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3469 cval1, cval2, save_p)
3470 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3471 cval1, cval2, save_p));
3472 return 0;
3473
3474 case tcc_comparison:
3475 /* First see if we can handle the first operand, then the second. For
3476 the second operand, we know *CVAL1 can't be zero. It must be that
3477 one side of the comparison is each of the values; test for the
3478 case where this isn't true by failing if the two operands
3479 are the same. */
3480
3481 if (operand_equal_p (TREE_OPERAND (arg, 0),
3482 TREE_OPERAND (arg, 1), 0))
3483 return 0;
3484
3485 if (*cval1 == 0)
3486 *cval1 = TREE_OPERAND (arg, 0);
3487 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3488 ;
3489 else if (*cval2 == 0)
3490 *cval2 = TREE_OPERAND (arg, 0);
3491 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3492 ;
3493 else
3494 return 0;
3495
3496 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3497 ;
3498 else if (*cval2 == 0)
3499 *cval2 = TREE_OPERAND (arg, 1);
3500 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3501 ;
3502 else
3503 return 0;
3504
3505 return 1;
3506
3507 default:
3508 return 0;
3509 }
3510 }
3511 \f
3512 /* ARG is a tree that is known to contain just arithmetic operations and
3513 comparisons. Evaluate the operations in the tree substituting NEW0 for
3514 any occurrence of OLD0 as an operand of a comparison and likewise for
3515 NEW1 and OLD1. */
3516
3517 static tree
3518 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3519 tree old1, tree new1)
3520 {
3521 tree type = TREE_TYPE (arg);
3522 enum tree_code code = TREE_CODE (arg);
3523 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3524
3525 /* We can handle some of the tcc_expression cases here. */
3526 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3527 tclass = tcc_unary;
3528 else if (tclass == tcc_expression
3529 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3530 tclass = tcc_binary;
3531
3532 switch (tclass)
3533 {
3534 case tcc_unary:
3535 return fold_build1_loc (loc, code, type,
3536 eval_subst (loc, TREE_OPERAND (arg, 0),
3537 old0, new0, old1, new1));
3538
3539 case tcc_binary:
3540 return fold_build2_loc (loc, code, type,
3541 eval_subst (loc, TREE_OPERAND (arg, 0),
3542 old0, new0, old1, new1),
3543 eval_subst (loc, TREE_OPERAND (arg, 1),
3544 old0, new0, old1, new1));
3545
3546 case tcc_expression:
3547 switch (code)
3548 {
3549 case SAVE_EXPR:
3550 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3551 old1, new1);
3552
3553 case COMPOUND_EXPR:
3554 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3555 old1, new1);
3556
3557 case COND_EXPR:
3558 return fold_build3_loc (loc, code, type,
3559 eval_subst (loc, TREE_OPERAND (arg, 0),
3560 old0, new0, old1, new1),
3561 eval_subst (loc, TREE_OPERAND (arg, 1),
3562 old0, new0, old1, new1),
3563 eval_subst (loc, TREE_OPERAND (arg, 2),
3564 old0, new0, old1, new1));
3565 default:
3566 break;
3567 }
3568 /* Fall through - ??? */
3569
3570 case tcc_comparison:
3571 {
3572 tree arg0 = TREE_OPERAND (arg, 0);
3573 tree arg1 = TREE_OPERAND (arg, 1);
3574
3575 /* We need to check both for exact equality and tree equality. The
3576 former will be true if the operand has a side-effect. In that
3577 case, we know the operand occurred exactly once. */
3578
3579 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3580 arg0 = new0;
3581 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3582 arg0 = new1;
3583
3584 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3585 arg1 = new0;
3586 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3587 arg1 = new1;
3588
3589 return fold_build2_loc (loc, code, type, arg0, arg1);
3590 }
3591
3592 default:
3593 return arg;
3594 }
3595 }
3596 \f
3597 /* Return a tree for the case when the result of an expression is RESULT
3598 converted to TYPE and OMITTED was previously an operand of the expression
3599 but is now not needed (e.g., we folded OMITTED * 0).
3600
3601 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3602 the conversion of RESULT to TYPE. */
3603
3604 tree
3605 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3606 {
3607 tree t = fold_convert_loc (loc, type, result);
3608
3609 /* If the resulting operand is an empty statement, just return the omitted
3610 statement casted to void. */
3611 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3612 return build1_loc (loc, NOP_EXPR, void_type_node,
3613 fold_ignored_result (omitted));
3614
3615 if (TREE_SIDE_EFFECTS (omitted))
3616 return build2_loc (loc, COMPOUND_EXPR, type,
3617 fold_ignored_result (omitted), t);
3618
3619 return non_lvalue_loc (loc, t);
3620 }
3621
3622 /* Return a tree for the case when the result of an expression is RESULT
3623 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3624 of the expression but are now not needed.
3625
3626 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3627 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3628 evaluated before OMITTED2. Otherwise, if neither has side effects,
3629 just do the conversion of RESULT to TYPE. */
3630
3631 tree
3632 omit_two_operands_loc (location_t loc, tree type, tree result,
3633 tree omitted1, tree omitted2)
3634 {
3635 tree t = fold_convert_loc (loc, type, result);
3636
3637 if (TREE_SIDE_EFFECTS (omitted2))
3638 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3639 if (TREE_SIDE_EFFECTS (omitted1))
3640 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3641
3642 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3643 }
3644
3645 \f
3646 /* Return a simplified tree node for the truth-negation of ARG. This
3647 never alters ARG itself. We assume that ARG is an operation that
3648 returns a truth value (0 or 1).
3649
3650 FIXME: one would think we would fold the result, but it causes
3651 problems with the dominator optimizer. */
3652
3653 static tree
3654 fold_truth_not_expr (location_t loc, tree arg)
3655 {
3656 tree type = TREE_TYPE (arg);
3657 enum tree_code code = TREE_CODE (arg);
3658 location_t loc1, loc2;
3659
3660 /* If this is a comparison, we can simply invert it, except for
3661 floating-point non-equality comparisons, in which case we just
3662 enclose a TRUTH_NOT_EXPR around what we have. */
3663
3664 if (TREE_CODE_CLASS (code) == tcc_comparison)
3665 {
3666 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3667 if (FLOAT_TYPE_P (op_type)
3668 && flag_trapping_math
3669 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3670 && code != NE_EXPR && code != EQ_EXPR)
3671 return NULL_TREE;
3672
3673 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3674 if (code == ERROR_MARK)
3675 return NULL_TREE;
3676
3677 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3678 TREE_OPERAND (arg, 1));
3679 if (TREE_NO_WARNING (arg))
3680 TREE_NO_WARNING (ret) = 1;
3681 return ret;
3682 }
3683
3684 switch (code)
3685 {
3686 case INTEGER_CST:
3687 return constant_boolean_node (integer_zerop (arg), type);
3688
3689 case TRUTH_AND_EXPR:
3690 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3691 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3692 return build2_loc (loc, TRUTH_OR_EXPR, type,
3693 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3694 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3695
3696 case TRUTH_OR_EXPR:
3697 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3698 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3699 return build2_loc (loc, TRUTH_AND_EXPR, type,
3700 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3701 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3702
3703 case TRUTH_XOR_EXPR:
3704 /* Here we can invert either operand. We invert the first operand
3705 unless the second operand is a TRUTH_NOT_EXPR in which case our
3706 result is the XOR of the first operand with the inside of the
3707 negation of the second operand. */
3708
3709 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3710 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3711 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3712 else
3713 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3714 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3715 TREE_OPERAND (arg, 1));
3716
3717 case TRUTH_ANDIF_EXPR:
3718 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3719 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3720 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3721 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3722 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3723
3724 case TRUTH_ORIF_EXPR:
3725 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3726 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3727 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3728 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3729 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3730
3731 case TRUTH_NOT_EXPR:
3732 return TREE_OPERAND (arg, 0);
3733
3734 case COND_EXPR:
3735 {
3736 tree arg1 = TREE_OPERAND (arg, 1);
3737 tree arg2 = TREE_OPERAND (arg, 2);
3738
3739 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3740 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3741
3742 /* A COND_EXPR may have a throw as one operand, which
3743 then has void type. Just leave void operands
3744 as they are. */
3745 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3746 VOID_TYPE_P (TREE_TYPE (arg1))
3747 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3748 VOID_TYPE_P (TREE_TYPE (arg2))
3749 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3750 }
3751
3752 case COMPOUND_EXPR:
3753 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3754 return build2_loc (loc, COMPOUND_EXPR, type,
3755 TREE_OPERAND (arg, 0),
3756 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3757
3758 case NON_LVALUE_EXPR:
3759 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3760 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3761
3762 CASE_CONVERT:
3763 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3764 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3765
3766 /* fall through */
3767
3768 case FLOAT_EXPR:
3769 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3770 return build1_loc (loc, TREE_CODE (arg), type,
3771 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3772
3773 case BIT_AND_EXPR:
3774 if (!integer_onep (TREE_OPERAND (arg, 1)))
3775 return NULL_TREE;
3776 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3777
3778 case SAVE_EXPR:
3779 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3780
3781 case CLEANUP_POINT_EXPR:
3782 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3783 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3784 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3785
3786 default:
3787 return NULL_TREE;
3788 }
3789 }
3790
3791 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3792 assume that ARG is an operation that returns a truth value (0 or 1
3793 for scalars, 0 or -1 for vectors). Return the folded expression if
3794 folding is successful. Otherwise, return NULL_TREE. */
3795
3796 static tree
3797 fold_invert_truthvalue (location_t loc, tree arg)
3798 {
3799 tree type = TREE_TYPE (arg);
3800 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3801 ? BIT_NOT_EXPR
3802 : TRUTH_NOT_EXPR,
3803 type, arg);
3804 }
3805
3806 /* Return a simplified tree node for the truth-negation of ARG. This
3807 never alters ARG itself. We assume that ARG is an operation that
3808 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3809
3810 tree
3811 invert_truthvalue_loc (location_t loc, tree arg)
3812 {
3813 if (TREE_CODE (arg) == ERROR_MARK)
3814 return arg;
3815
3816 tree type = TREE_TYPE (arg);
3817 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3818 ? BIT_NOT_EXPR
3819 : TRUTH_NOT_EXPR,
3820 type, arg);
3821 }
3822
3823 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3824 with code CODE. This optimization is unsafe. */
3825 static tree
3826 distribute_real_division (location_t loc, enum tree_code code, tree type,
3827 tree arg0, tree arg1)
3828 {
3829 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3830 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3831
3832 /* (A / C) +- (B / C) -> (A +- B) / C. */
3833 if (mul0 == mul1
3834 && operand_equal_p (TREE_OPERAND (arg0, 1),
3835 TREE_OPERAND (arg1, 1), 0))
3836 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3837 fold_build2_loc (loc, code, type,
3838 TREE_OPERAND (arg0, 0),
3839 TREE_OPERAND (arg1, 0)),
3840 TREE_OPERAND (arg0, 1));
3841
3842 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3843 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3844 TREE_OPERAND (arg1, 0), 0)
3845 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3846 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3847 {
3848 REAL_VALUE_TYPE r0, r1;
3849 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3850 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3851 if (!mul0)
3852 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3853 if (!mul1)
3854 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3855 real_arithmetic (&r0, code, &r0, &r1);
3856 return fold_build2_loc (loc, MULT_EXPR, type,
3857 TREE_OPERAND (arg0, 0),
3858 build_real (type, r0));
3859 }
3860
3861 return NULL_TREE;
3862 }
3863 \f
3864 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3865 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3866 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3867 is the original memory reference used to preserve the alias set of
3868 the access. */
3869
3870 static tree
3871 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3872 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3873 int unsignedp, int reversep)
3874 {
3875 tree result, bftype;
3876
3877 /* Attempt not to lose the access path if possible. */
3878 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3879 {
3880 tree ninner = TREE_OPERAND (orig_inner, 0);
3881 machine_mode nmode;
3882 HOST_WIDE_INT nbitsize, nbitpos;
3883 tree noffset;
3884 int nunsignedp, nreversep, nvolatilep = 0;
3885 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3886 &noffset, &nmode, &nunsignedp,
3887 &nreversep, &nvolatilep);
3888 if (base == inner
3889 && noffset == NULL_TREE
3890 && nbitsize >= bitsize
3891 && nbitpos <= bitpos
3892 && bitpos + bitsize <= nbitpos + nbitsize
3893 && !reversep
3894 && !nreversep
3895 && !nvolatilep)
3896 {
3897 inner = ninner;
3898 bitpos -= nbitpos;
3899 }
3900 }
3901
3902 alias_set_type iset = get_alias_set (orig_inner);
3903 if (iset == 0 && get_alias_set (inner) != iset)
3904 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3905 build_fold_addr_expr (inner),
3906 build_int_cst (ptr_type_node, 0));
3907
3908 if (bitpos == 0 && !reversep)
3909 {
3910 tree size = TYPE_SIZE (TREE_TYPE (inner));
3911 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3912 || POINTER_TYPE_P (TREE_TYPE (inner)))
3913 && tree_fits_shwi_p (size)
3914 && tree_to_shwi (size) == bitsize)
3915 return fold_convert_loc (loc, type, inner);
3916 }
3917
3918 bftype = type;
3919 if (TYPE_PRECISION (bftype) != bitsize
3920 || TYPE_UNSIGNED (bftype) == !unsignedp)
3921 bftype = build_nonstandard_integer_type (bitsize, 0);
3922
3923 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3924 size_int (bitsize), bitsize_int (bitpos));
3925 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3926
3927 if (bftype != type)
3928 result = fold_convert_loc (loc, type, result);
3929
3930 return result;
3931 }
3932
3933 /* Optimize a bit-field compare.
3934
3935 There are two cases: First is a compare against a constant and the
3936 second is a comparison of two items where the fields are at the same
3937 bit position relative to the start of a chunk (byte, halfword, word)
3938 large enough to contain it. In these cases we can avoid the shift
3939 implicit in bitfield extractions.
3940
3941 For constants, we emit a compare of the shifted constant with the
3942 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3943 compared. For two fields at the same position, we do the ANDs with the
3944 similar mask and compare the result of the ANDs.
3945
3946 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3947 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3948 are the left and right operands of the comparison, respectively.
3949
3950 If the optimization described above can be done, we return the resulting
3951 tree. Otherwise we return zero. */
3952
3953 static tree
3954 optimize_bit_field_compare (location_t loc, enum tree_code code,
3955 tree compare_type, tree lhs, tree rhs)
3956 {
3957 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3958 tree type = TREE_TYPE (lhs);
3959 tree unsigned_type;
3960 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3961 machine_mode lmode, rmode, nmode;
3962 int lunsignedp, runsignedp;
3963 int lreversep, rreversep;
3964 int lvolatilep = 0, rvolatilep = 0;
3965 tree linner, rinner = NULL_TREE;
3966 tree mask;
3967 tree offset;
3968
3969 /* Get all the information about the extractions being done. If the bit size
3970 if the same as the size of the underlying object, we aren't doing an
3971 extraction at all and so can do nothing. We also don't want to
3972 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3973 then will no longer be able to replace it. */
3974 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3975 &lunsignedp, &lreversep, &lvolatilep);
3976 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3977 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3978 return 0;
3979
3980 if (const_p)
3981 rreversep = lreversep;
3982 else
3983 {
3984 /* If this is not a constant, we can only do something if bit positions,
3985 sizes, signedness and storage order are the same. */
3986 rinner
3987 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3988 &runsignedp, &rreversep, &rvolatilep);
3989
3990 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3991 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3992 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3993 return 0;
3994 }
3995
3996 /* Honor the C++ memory model and mimic what RTL expansion does. */
3997 unsigned HOST_WIDE_INT bitstart = 0;
3998 unsigned HOST_WIDE_INT bitend = 0;
3999 if (TREE_CODE (lhs) == COMPONENT_REF)
4000 {
4001 get_bit_range (&bitstart, &bitend, lhs, &lbitpos, &offset);
4002 if (offset != NULL_TREE)
4003 return 0;
4004 }
4005
4006 /* See if we can find a mode to refer to this field. We should be able to,
4007 but fail if we can't. */
4008 nmode = get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4009 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4010 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4011 TYPE_ALIGN (TREE_TYPE (rinner))),
4012 word_mode, false);
4013 if (nmode == VOIDmode)
4014 return 0;
4015
4016 /* Set signed and unsigned types of the precision of this mode for the
4017 shifts below. */
4018 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4019
4020 /* Compute the bit position and size for the new reference and our offset
4021 within it. If the new reference is the same size as the original, we
4022 won't optimize anything, so return zero. */
4023 nbitsize = GET_MODE_BITSIZE (nmode);
4024 nbitpos = lbitpos & ~ (nbitsize - 1);
4025 lbitpos -= nbitpos;
4026 if (nbitsize == lbitsize)
4027 return 0;
4028
4029 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4030 lbitpos = nbitsize - lbitsize - lbitpos;
4031
4032 /* Make the mask to be used against the extracted field. */
4033 mask = build_int_cst_type (unsigned_type, -1);
4034 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4035 mask = const_binop (RSHIFT_EXPR, mask,
4036 size_int (nbitsize - lbitsize - lbitpos));
4037
4038 if (! const_p)
4039 /* If not comparing with constant, just rework the comparison
4040 and return. */
4041 return fold_build2_loc (loc, code, compare_type,
4042 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4043 make_bit_field_ref (loc, linner, lhs,
4044 unsigned_type,
4045 nbitsize, nbitpos,
4046 1, lreversep),
4047 mask),
4048 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4049 make_bit_field_ref (loc, rinner, rhs,
4050 unsigned_type,
4051 nbitsize, nbitpos,
4052 1, rreversep),
4053 mask));
4054
4055 /* Otherwise, we are handling the constant case. See if the constant is too
4056 big for the field. Warn and return a tree for 0 (false) if so. We do
4057 this not only for its own sake, but to avoid having to test for this
4058 error case below. If we didn't, we might generate wrong code.
4059
4060 For unsigned fields, the constant shifted right by the field length should
4061 be all zero. For signed fields, the high-order bits should agree with
4062 the sign bit. */
4063
4064 if (lunsignedp)
4065 {
4066 if (wi::lrshift (rhs, lbitsize) != 0)
4067 {
4068 warning (0, "comparison is always %d due to width of bit-field",
4069 code == NE_EXPR);
4070 return constant_boolean_node (code == NE_EXPR, compare_type);
4071 }
4072 }
4073 else
4074 {
4075 wide_int tem = wi::arshift (rhs, lbitsize - 1);
4076 if (tem != 0 && tem != -1)
4077 {
4078 warning (0, "comparison is always %d due to width of bit-field",
4079 code == NE_EXPR);
4080 return constant_boolean_node (code == NE_EXPR, compare_type);
4081 }
4082 }
4083
4084 /* Single-bit compares should always be against zero. */
4085 if (lbitsize == 1 && ! integer_zerop (rhs))
4086 {
4087 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4088 rhs = build_int_cst (type, 0);
4089 }
4090
4091 /* Make a new bitfield reference, shift the constant over the
4092 appropriate number of bits and mask it with the computed mask
4093 (in case this was a signed field). If we changed it, make a new one. */
4094 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4095 nbitsize, nbitpos, 1, lreversep);
4096
4097 rhs = const_binop (BIT_AND_EXPR,
4098 const_binop (LSHIFT_EXPR,
4099 fold_convert_loc (loc, unsigned_type, rhs),
4100 size_int (lbitpos)),
4101 mask);
4102
4103 lhs = build2_loc (loc, code, compare_type,
4104 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4105 return lhs;
4106 }
4107 \f
4108 /* Subroutine for fold_truth_andor_1: decode a field reference.
4109
4110 If EXP is a comparison reference, we return the innermost reference.
4111
4112 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4113 set to the starting bit number.
4114
4115 If the innermost field can be completely contained in a mode-sized
4116 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4117
4118 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4119 otherwise it is not changed.
4120
4121 *PUNSIGNEDP is set to the signedness of the field.
4122
4123 *PREVERSEP is set to the storage order of the field.
4124
4125 *PMASK is set to the mask used. This is either contained in a
4126 BIT_AND_EXPR or derived from the width of the field.
4127
4128 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4129
4130 Return 0 if this is not a component reference or is one that we can't
4131 do anything with. */
4132
4133 static tree
4134 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4135 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4136 int *punsignedp, int *preversep, int *pvolatilep,
4137 tree *pmask, tree *pand_mask)
4138 {
4139 tree exp = *exp_;
4140 tree outer_type = 0;
4141 tree and_mask = 0;
4142 tree mask, inner, offset;
4143 tree unsigned_type;
4144 unsigned int precision;
4145
4146 /* All the optimizations using this function assume integer fields.
4147 There are problems with FP fields since the type_for_size call
4148 below can fail for, e.g., XFmode. */
4149 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4150 return 0;
4151
4152 /* We are interested in the bare arrangement of bits, so strip everything
4153 that doesn't affect the machine mode. However, record the type of the
4154 outermost expression if it may matter below. */
4155 if (CONVERT_EXPR_P (exp)
4156 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4157 outer_type = TREE_TYPE (exp);
4158 STRIP_NOPS (exp);
4159
4160 if (TREE_CODE (exp) == BIT_AND_EXPR)
4161 {
4162 and_mask = TREE_OPERAND (exp, 1);
4163 exp = TREE_OPERAND (exp, 0);
4164 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4165 if (TREE_CODE (and_mask) != INTEGER_CST)
4166 return 0;
4167 }
4168
4169 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4170 punsignedp, preversep, pvolatilep);
4171 if ((inner == exp && and_mask == 0)
4172 || *pbitsize < 0 || offset != 0
4173 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4174 /* Reject out-of-bound accesses (PR79731). */
4175 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4176 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4177 *pbitpos + *pbitsize) < 0))
4178 return 0;
4179
4180 *exp_ = exp;
4181
4182 /* If the number of bits in the reference is the same as the bitsize of
4183 the outer type, then the outer type gives the signedness. Otherwise
4184 (in case of a small bitfield) the signedness is unchanged. */
4185 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4186 *punsignedp = TYPE_UNSIGNED (outer_type);
4187
4188 /* Compute the mask to access the bitfield. */
4189 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4190 precision = TYPE_PRECISION (unsigned_type);
4191
4192 mask = build_int_cst_type (unsigned_type, -1);
4193
4194 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4195 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4196
4197 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4198 if (and_mask != 0)
4199 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4200 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4201
4202 *pmask = mask;
4203 *pand_mask = and_mask;
4204 return inner;
4205 }
4206
4207 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4208 bit positions and MASK is SIGNED. */
4209
4210 static int
4211 all_ones_mask_p (const_tree mask, unsigned int size)
4212 {
4213 tree type = TREE_TYPE (mask);
4214 unsigned int precision = TYPE_PRECISION (type);
4215
4216 /* If this function returns true when the type of the mask is
4217 UNSIGNED, then there will be errors. In particular see
4218 gcc.c-torture/execute/990326-1.c. There does not appear to be
4219 any documentation paper trail as to why this is so. But the pre
4220 wide-int worked with that restriction and it has been preserved
4221 here. */
4222 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4223 return false;
4224
4225 return wi::mask (size, false, precision) == mask;
4226 }
4227
4228 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4229 represents the sign bit of EXP's type. If EXP represents a sign
4230 or zero extension, also test VAL against the unextended type.
4231 The return value is the (sub)expression whose sign bit is VAL,
4232 or NULL_TREE otherwise. */
4233
4234 tree
4235 sign_bit_p (tree exp, const_tree val)
4236 {
4237 int width;
4238 tree t;
4239
4240 /* Tree EXP must have an integral type. */
4241 t = TREE_TYPE (exp);
4242 if (! INTEGRAL_TYPE_P (t))
4243 return NULL_TREE;
4244
4245 /* Tree VAL must be an integer constant. */
4246 if (TREE_CODE (val) != INTEGER_CST
4247 || TREE_OVERFLOW (val))
4248 return NULL_TREE;
4249
4250 width = TYPE_PRECISION (t);
4251 if (wi::only_sign_bit_p (val, width))
4252 return exp;
4253
4254 /* Handle extension from a narrower type. */
4255 if (TREE_CODE (exp) == NOP_EXPR
4256 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4257 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4258
4259 return NULL_TREE;
4260 }
4261
4262 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4263 to be evaluated unconditionally. */
4264
4265 static int
4266 simple_operand_p (const_tree exp)
4267 {
4268 /* Strip any conversions that don't change the machine mode. */
4269 STRIP_NOPS (exp);
4270
4271 return (CONSTANT_CLASS_P (exp)
4272 || TREE_CODE (exp) == SSA_NAME
4273 || (DECL_P (exp)
4274 && ! TREE_ADDRESSABLE (exp)
4275 && ! TREE_THIS_VOLATILE (exp)
4276 && ! DECL_NONLOCAL (exp)
4277 /* Don't regard global variables as simple. They may be
4278 allocated in ways unknown to the compiler (shared memory,
4279 #pragma weak, etc). */
4280 && ! TREE_PUBLIC (exp)
4281 && ! DECL_EXTERNAL (exp)
4282 /* Weakrefs are not safe to be read, since they can be NULL.
4283 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4284 have DECL_WEAK flag set. */
4285 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4286 /* Loading a static variable is unduly expensive, but global
4287 registers aren't expensive. */
4288 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4289 }
4290
4291 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4292 to be evaluated unconditionally.
4293 I addition to simple_operand_p, we assume that comparisons, conversions,
4294 and logic-not operations are simple, if their operands are simple, too. */
4295
4296 static bool
4297 simple_operand_p_2 (tree exp)
4298 {
4299 enum tree_code code;
4300
4301 if (TREE_SIDE_EFFECTS (exp)
4302 || tree_could_trap_p (exp))
4303 return false;
4304
4305 while (CONVERT_EXPR_P (exp))
4306 exp = TREE_OPERAND (exp, 0);
4307
4308 code = TREE_CODE (exp);
4309
4310 if (TREE_CODE_CLASS (code) == tcc_comparison)
4311 return (simple_operand_p (TREE_OPERAND (exp, 0))
4312 && simple_operand_p (TREE_OPERAND (exp, 1)));
4313
4314 if (code == TRUTH_NOT_EXPR)
4315 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4316
4317 return simple_operand_p (exp);
4318 }
4319
4320 \f
4321 /* The following functions are subroutines to fold_range_test and allow it to
4322 try to change a logical combination of comparisons into a range test.
4323
4324 For example, both
4325 X == 2 || X == 3 || X == 4 || X == 5
4326 and
4327 X >= 2 && X <= 5
4328 are converted to
4329 (unsigned) (X - 2) <= 3
4330
4331 We describe each set of comparisons as being either inside or outside
4332 a range, using a variable named like IN_P, and then describe the
4333 range with a lower and upper bound. If one of the bounds is omitted,
4334 it represents either the highest or lowest value of the type.
4335
4336 In the comments below, we represent a range by two numbers in brackets
4337 preceded by a "+" to designate being inside that range, or a "-" to
4338 designate being outside that range, so the condition can be inverted by
4339 flipping the prefix. An omitted bound is represented by a "-". For
4340 example, "- [-, 10]" means being outside the range starting at the lowest
4341 possible value and ending at 10, in other words, being greater than 10.
4342 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4343 always false.
4344
4345 We set up things so that the missing bounds are handled in a consistent
4346 manner so neither a missing bound nor "true" and "false" need to be
4347 handled using a special case. */
4348
4349 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4350 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4351 and UPPER1_P are nonzero if the respective argument is an upper bound
4352 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4353 must be specified for a comparison. ARG1 will be converted to ARG0's
4354 type if both are specified. */
4355
4356 static tree
4357 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4358 tree arg1, int upper1_p)
4359 {
4360 tree tem;
4361 int result;
4362 int sgn0, sgn1;
4363
4364 /* If neither arg represents infinity, do the normal operation.
4365 Else, if not a comparison, return infinity. Else handle the special
4366 comparison rules. Note that most of the cases below won't occur, but
4367 are handled for consistency. */
4368
4369 if (arg0 != 0 && arg1 != 0)
4370 {
4371 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4372 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4373 STRIP_NOPS (tem);
4374 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4375 }
4376
4377 if (TREE_CODE_CLASS (code) != tcc_comparison)
4378 return 0;
4379
4380 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4381 for neither. In real maths, we cannot assume open ended ranges are
4382 the same. But, this is computer arithmetic, where numbers are finite.
4383 We can therefore make the transformation of any unbounded range with
4384 the value Z, Z being greater than any representable number. This permits
4385 us to treat unbounded ranges as equal. */
4386 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4387 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4388 switch (code)
4389 {
4390 case EQ_EXPR:
4391 result = sgn0 == sgn1;
4392 break;
4393 case NE_EXPR:
4394 result = sgn0 != sgn1;
4395 break;
4396 case LT_EXPR:
4397 result = sgn0 < sgn1;
4398 break;
4399 case LE_EXPR:
4400 result = sgn0 <= sgn1;
4401 break;
4402 case GT_EXPR:
4403 result = sgn0 > sgn1;
4404 break;
4405 case GE_EXPR:
4406 result = sgn0 >= sgn1;
4407 break;
4408 default:
4409 gcc_unreachable ();
4410 }
4411
4412 return constant_boolean_node (result, type);
4413 }
4414 \f
4415 /* Helper routine for make_range. Perform one step for it, return
4416 new expression if the loop should continue or NULL_TREE if it should
4417 stop. */
4418
4419 tree
4420 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4421 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4422 bool *strict_overflow_p)
4423 {
4424 tree arg0_type = TREE_TYPE (arg0);
4425 tree n_low, n_high, low = *p_low, high = *p_high;
4426 int in_p = *p_in_p, n_in_p;
4427
4428 switch (code)
4429 {
4430 case TRUTH_NOT_EXPR:
4431 /* We can only do something if the range is testing for zero. */
4432 if (low == NULL_TREE || high == NULL_TREE
4433 || ! integer_zerop (low) || ! integer_zerop (high))
4434 return NULL_TREE;
4435 *p_in_p = ! in_p;
4436 return arg0;
4437
4438 case EQ_EXPR: case NE_EXPR:
4439 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4440 /* We can only do something if the range is testing for zero
4441 and if the second operand is an integer constant. Note that
4442 saying something is "in" the range we make is done by
4443 complementing IN_P since it will set in the initial case of
4444 being not equal to zero; "out" is leaving it alone. */
4445 if (low == NULL_TREE || high == NULL_TREE
4446 || ! integer_zerop (low) || ! integer_zerop (high)
4447 || TREE_CODE (arg1) != INTEGER_CST)
4448 return NULL_TREE;
4449
4450 switch (code)
4451 {
4452 case NE_EXPR: /* - [c, c] */
4453 low = high = arg1;
4454 break;
4455 case EQ_EXPR: /* + [c, c] */
4456 in_p = ! in_p, low = high = arg1;
4457 break;
4458 case GT_EXPR: /* - [-, c] */
4459 low = 0, high = arg1;
4460 break;
4461 case GE_EXPR: /* + [c, -] */
4462 in_p = ! in_p, low = arg1, high = 0;
4463 break;
4464 case LT_EXPR: /* - [c, -] */
4465 low = arg1, high = 0;
4466 break;
4467 case LE_EXPR: /* + [-, c] */
4468 in_p = ! in_p, low = 0, high = arg1;
4469 break;
4470 default:
4471 gcc_unreachable ();
4472 }
4473
4474 /* If this is an unsigned comparison, we also know that EXP is
4475 greater than or equal to zero. We base the range tests we make
4476 on that fact, so we record it here so we can parse existing
4477 range tests. We test arg0_type since often the return type
4478 of, e.g. EQ_EXPR, is boolean. */
4479 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4480 {
4481 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4482 in_p, low, high, 1,
4483 build_int_cst (arg0_type, 0),
4484 NULL_TREE))
4485 return NULL_TREE;
4486
4487 in_p = n_in_p, low = n_low, high = n_high;
4488
4489 /* If the high bound is missing, but we have a nonzero low
4490 bound, reverse the range so it goes from zero to the low bound
4491 minus 1. */
4492 if (high == 0 && low && ! integer_zerop (low))
4493 {
4494 in_p = ! in_p;
4495 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4496 build_int_cst (TREE_TYPE (low), 1), 0);
4497 low = build_int_cst (arg0_type, 0);
4498 }
4499 }
4500
4501 *p_low = low;
4502 *p_high = high;
4503 *p_in_p = in_p;
4504 return arg0;
4505
4506 case NEGATE_EXPR:
4507 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4508 low and high are non-NULL, then normalize will DTRT. */
4509 if (!TYPE_UNSIGNED (arg0_type)
4510 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4511 {
4512 if (low == NULL_TREE)
4513 low = TYPE_MIN_VALUE (arg0_type);
4514 if (high == NULL_TREE)
4515 high = TYPE_MAX_VALUE (arg0_type);
4516 }
4517
4518 /* (-x) IN [a,b] -> x in [-b, -a] */
4519 n_low = range_binop (MINUS_EXPR, exp_type,
4520 build_int_cst (exp_type, 0),
4521 0, high, 1);
4522 n_high = range_binop (MINUS_EXPR, exp_type,
4523 build_int_cst (exp_type, 0),
4524 0, low, 0);
4525 if (n_high != 0 && TREE_OVERFLOW (n_high))
4526 return NULL_TREE;
4527 goto normalize;
4528
4529 case BIT_NOT_EXPR:
4530 /* ~ X -> -X - 1 */
4531 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4532 build_int_cst (exp_type, 1));
4533
4534 case PLUS_EXPR:
4535 case MINUS_EXPR:
4536 if (TREE_CODE (arg1) != INTEGER_CST)
4537 return NULL_TREE;
4538
4539 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4540 move a constant to the other side. */
4541 if (!TYPE_UNSIGNED (arg0_type)
4542 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4543 return NULL_TREE;
4544
4545 /* If EXP is signed, any overflow in the computation is undefined,
4546 so we don't worry about it so long as our computations on
4547 the bounds don't overflow. For unsigned, overflow is defined
4548 and this is exactly the right thing. */
4549 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4550 arg0_type, low, 0, arg1, 0);
4551 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4552 arg0_type, high, 1, arg1, 0);
4553 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4554 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4555 return NULL_TREE;
4556
4557 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4558 *strict_overflow_p = true;
4559
4560 normalize:
4561 /* Check for an unsigned range which has wrapped around the maximum
4562 value thus making n_high < n_low, and normalize it. */
4563 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4564 {
4565 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4566 build_int_cst (TREE_TYPE (n_high), 1), 0);
4567 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4568 build_int_cst (TREE_TYPE (n_low), 1), 0);
4569
4570 /* If the range is of the form +/- [ x+1, x ], we won't
4571 be able to normalize it. But then, it represents the
4572 whole range or the empty set, so make it
4573 +/- [ -, - ]. */
4574 if (tree_int_cst_equal (n_low, low)
4575 && tree_int_cst_equal (n_high, high))
4576 low = high = 0;
4577 else
4578 in_p = ! in_p;
4579 }
4580 else
4581 low = n_low, high = n_high;
4582
4583 *p_low = low;
4584 *p_high = high;
4585 *p_in_p = in_p;
4586 return arg0;
4587
4588 CASE_CONVERT:
4589 case NON_LVALUE_EXPR:
4590 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4591 return NULL_TREE;
4592
4593 if (! INTEGRAL_TYPE_P (arg0_type)
4594 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4595 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4596 return NULL_TREE;
4597
4598 n_low = low, n_high = high;
4599
4600 if (n_low != 0)
4601 n_low = fold_convert_loc (loc, arg0_type, n_low);
4602
4603 if (n_high != 0)
4604 n_high = fold_convert_loc (loc, arg0_type, n_high);
4605
4606 /* If we're converting arg0 from an unsigned type, to exp,
4607 a signed type, we will be doing the comparison as unsigned.
4608 The tests above have already verified that LOW and HIGH
4609 are both positive.
4610
4611 So we have to ensure that we will handle large unsigned
4612 values the same way that the current signed bounds treat
4613 negative values. */
4614
4615 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4616 {
4617 tree high_positive;
4618 tree equiv_type;
4619 /* For fixed-point modes, we need to pass the saturating flag
4620 as the 2nd parameter. */
4621 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4622 equiv_type
4623 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4624 TYPE_SATURATING (arg0_type));
4625 else
4626 equiv_type
4627 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4628
4629 /* A range without an upper bound is, naturally, unbounded.
4630 Since convert would have cropped a very large value, use
4631 the max value for the destination type. */
4632 high_positive
4633 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4634 : TYPE_MAX_VALUE (arg0_type);
4635
4636 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4637 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4638 fold_convert_loc (loc, arg0_type,
4639 high_positive),
4640 build_int_cst (arg0_type, 1));
4641
4642 /* If the low bound is specified, "and" the range with the
4643 range for which the original unsigned value will be
4644 positive. */
4645 if (low != 0)
4646 {
4647 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4648 1, fold_convert_loc (loc, arg0_type,
4649 integer_zero_node),
4650 high_positive))
4651 return NULL_TREE;
4652
4653 in_p = (n_in_p == in_p);
4654 }
4655 else
4656 {
4657 /* Otherwise, "or" the range with the range of the input
4658 that will be interpreted as negative. */
4659 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4660 1, fold_convert_loc (loc, arg0_type,
4661 integer_zero_node),
4662 high_positive))
4663 return NULL_TREE;
4664
4665 in_p = (in_p != n_in_p);
4666 }
4667 }
4668
4669 *p_low = n_low;
4670 *p_high = n_high;
4671 *p_in_p = in_p;
4672 return arg0;
4673
4674 default:
4675 return NULL_TREE;
4676 }
4677 }
4678
4679 /* Given EXP, a logical expression, set the range it is testing into
4680 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4681 actually being tested. *PLOW and *PHIGH will be made of the same
4682 type as the returned expression. If EXP is not a comparison, we
4683 will most likely not be returning a useful value and range. Set
4684 *STRICT_OVERFLOW_P to true if the return value is only valid
4685 because signed overflow is undefined; otherwise, do not change
4686 *STRICT_OVERFLOW_P. */
4687
4688 tree
4689 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4690 bool *strict_overflow_p)
4691 {
4692 enum tree_code code;
4693 tree arg0, arg1 = NULL_TREE;
4694 tree exp_type, nexp;
4695 int in_p;
4696 tree low, high;
4697 location_t loc = EXPR_LOCATION (exp);
4698
4699 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4700 and see if we can refine the range. Some of the cases below may not
4701 happen, but it doesn't seem worth worrying about this. We "continue"
4702 the outer loop when we've changed something; otherwise we "break"
4703 the switch, which will "break" the while. */
4704
4705 in_p = 0;
4706 low = high = build_int_cst (TREE_TYPE (exp), 0);
4707
4708 while (1)
4709 {
4710 code = TREE_CODE (exp);
4711 exp_type = TREE_TYPE (exp);
4712 arg0 = NULL_TREE;
4713
4714 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4715 {
4716 if (TREE_OPERAND_LENGTH (exp) > 0)
4717 arg0 = TREE_OPERAND (exp, 0);
4718 if (TREE_CODE_CLASS (code) == tcc_binary
4719 || TREE_CODE_CLASS (code) == tcc_comparison
4720 || (TREE_CODE_CLASS (code) == tcc_expression
4721 && TREE_OPERAND_LENGTH (exp) > 1))
4722 arg1 = TREE_OPERAND (exp, 1);
4723 }
4724 if (arg0 == NULL_TREE)
4725 break;
4726
4727 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4728 &high, &in_p, strict_overflow_p);
4729 if (nexp == NULL_TREE)
4730 break;
4731 exp = nexp;
4732 }
4733
4734 /* If EXP is a constant, we can evaluate whether this is true or false. */
4735 if (TREE_CODE (exp) == INTEGER_CST)
4736 {
4737 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4738 exp, 0, low, 0))
4739 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4740 exp, 1, high, 1)));
4741 low = high = 0;
4742 exp = 0;
4743 }
4744
4745 *pin_p = in_p, *plow = low, *phigh = high;
4746 return exp;
4747 }
4748
4749 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4750 a bitwise check i.e. when
4751 LOW == 0xXX...X00...0
4752 HIGH == 0xXX...X11...1
4753 Return corresponding mask in MASK and stem in VALUE. */
4754
4755 static bool
4756 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4757 tree *value)
4758 {
4759 if (TREE_CODE (low) != INTEGER_CST
4760 || TREE_CODE (high) != INTEGER_CST)
4761 return false;
4762
4763 unsigned prec = TYPE_PRECISION (type);
4764 wide_int lo = wi::to_wide (low, prec);
4765 wide_int hi = wi::to_wide (high, prec);
4766
4767 wide_int end_mask = lo ^ hi;
4768 if ((end_mask & (end_mask + 1)) != 0
4769 || (lo & end_mask) != 0)
4770 return false;
4771
4772 wide_int stem_mask = ~end_mask;
4773 wide_int stem = lo & stem_mask;
4774 if (stem != (hi & stem_mask))
4775 return false;
4776
4777 *mask = wide_int_to_tree (type, stem_mask);
4778 *value = wide_int_to_tree (type, stem);
4779
4780 return true;
4781 }
4782 \f
4783 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4784 type, TYPE, return an expression to test if EXP is in (or out of, depending
4785 on IN_P) the range. Return 0 if the test couldn't be created. */
4786
4787 tree
4788 build_range_check (location_t loc, tree type, tree exp, int in_p,
4789 tree low, tree high)
4790 {
4791 tree etype = TREE_TYPE (exp), mask, value;
4792
4793 /* Disable this optimization for function pointer expressions
4794 on targets that require function pointer canonicalization. */
4795 if (targetm.have_canonicalize_funcptr_for_compare ()
4796 && TREE_CODE (etype) == POINTER_TYPE
4797 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4798 return NULL_TREE;
4799
4800 if (! in_p)
4801 {
4802 value = build_range_check (loc, type, exp, 1, low, high);
4803 if (value != 0)
4804 return invert_truthvalue_loc (loc, value);
4805
4806 return 0;
4807 }
4808
4809 if (low == 0 && high == 0)
4810 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4811
4812 if (low == 0)
4813 return fold_build2_loc (loc, LE_EXPR, type, exp,
4814 fold_convert_loc (loc, etype, high));
4815
4816 if (high == 0)
4817 return fold_build2_loc (loc, GE_EXPR, type, exp,
4818 fold_convert_loc (loc, etype, low));
4819
4820 if (operand_equal_p (low, high, 0))
4821 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4822 fold_convert_loc (loc, etype, low));
4823
4824 if (TREE_CODE (exp) == BIT_AND_EXPR
4825 && maskable_range_p (low, high, etype, &mask, &value))
4826 return fold_build2_loc (loc, EQ_EXPR, type,
4827 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4828 exp, mask),
4829 value);
4830
4831 if (integer_zerop (low))
4832 {
4833 if (! TYPE_UNSIGNED (etype))
4834 {
4835 etype = unsigned_type_for (etype);
4836 high = fold_convert_loc (loc, etype, high);
4837 exp = fold_convert_loc (loc, etype, exp);
4838 }
4839 return build_range_check (loc, type, exp, 1, 0, high);
4840 }
4841
4842 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4843 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4844 {
4845 int prec = TYPE_PRECISION (etype);
4846
4847 if (wi::mask (prec - 1, false, prec) == high)
4848 {
4849 if (TYPE_UNSIGNED (etype))
4850 {
4851 tree signed_etype = signed_type_for (etype);
4852 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4853 etype
4854 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4855 else
4856 etype = signed_etype;
4857 exp = fold_convert_loc (loc, etype, exp);
4858 }
4859 return fold_build2_loc (loc, GT_EXPR, type, exp,
4860 build_int_cst (etype, 0));
4861 }
4862 }
4863
4864 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4865 This requires wrap-around arithmetics for the type of the expression.
4866 First make sure that arithmetics in this type is valid, then make sure
4867 that it wraps around. */
4868 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4869 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4870 TYPE_UNSIGNED (etype));
4871
4872 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4873 {
4874 tree utype, minv, maxv;
4875
4876 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4877 for the type in question, as we rely on this here. */
4878 utype = unsigned_type_for (etype);
4879 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4880 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4881 build_int_cst (TREE_TYPE (maxv), 1), 1);
4882 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4883
4884 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4885 minv, 1, maxv, 1)))
4886 etype = utype;
4887 else
4888 return 0;
4889 }
4890
4891 high = fold_convert_loc (loc, etype, high);
4892 low = fold_convert_loc (loc, etype, low);
4893 exp = fold_convert_loc (loc, etype, exp);
4894
4895 value = const_binop (MINUS_EXPR, high, low);
4896
4897
4898 if (POINTER_TYPE_P (etype))
4899 {
4900 if (value != 0 && !TREE_OVERFLOW (value))
4901 {
4902 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4903 return build_range_check (loc, type,
4904 fold_build_pointer_plus_loc (loc, exp, low),
4905 1, build_int_cst (etype, 0), value);
4906 }
4907 return 0;
4908 }
4909
4910 if (value != 0 && !TREE_OVERFLOW (value))
4911 return build_range_check (loc, type,
4912 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4913 1, build_int_cst (etype, 0), value);
4914
4915 return 0;
4916 }
4917 \f
4918 /* Return the predecessor of VAL in its type, handling the infinite case. */
4919
4920 static tree
4921 range_predecessor (tree val)
4922 {
4923 tree type = TREE_TYPE (val);
4924
4925 if (INTEGRAL_TYPE_P (type)
4926 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4927 return 0;
4928 else
4929 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4930 build_int_cst (TREE_TYPE (val), 1), 0);
4931 }
4932
4933 /* Return the successor of VAL in its type, handling the infinite case. */
4934
4935 static tree
4936 range_successor (tree val)
4937 {
4938 tree type = TREE_TYPE (val);
4939
4940 if (INTEGRAL_TYPE_P (type)
4941 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4942 return 0;
4943 else
4944 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4945 build_int_cst (TREE_TYPE (val), 1), 0);
4946 }
4947
4948 /* Given two ranges, see if we can merge them into one. Return 1 if we
4949 can, 0 if we can't. Set the output range into the specified parameters. */
4950
4951 bool
4952 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4953 tree high0, int in1_p, tree low1, tree high1)
4954 {
4955 int no_overlap;
4956 int subset;
4957 int temp;
4958 tree tem;
4959 int in_p;
4960 tree low, high;
4961 int lowequal = ((low0 == 0 && low1 == 0)
4962 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4963 low0, 0, low1, 0)));
4964 int highequal = ((high0 == 0 && high1 == 0)
4965 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4966 high0, 1, high1, 1)));
4967
4968 /* Make range 0 be the range that starts first, or ends last if they
4969 start at the same value. Swap them if it isn't. */
4970 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4971 low0, 0, low1, 0))
4972 || (lowequal
4973 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4974 high1, 1, high0, 1))))
4975 {
4976 temp = in0_p, in0_p = in1_p, in1_p = temp;
4977 tem = low0, low0 = low1, low1 = tem;
4978 tem = high0, high0 = high1, high1 = tem;
4979 }
4980
4981 /* Now flag two cases, whether the ranges are disjoint or whether the
4982 second range is totally subsumed in the first. Note that the tests
4983 below are simplified by the ones above. */
4984 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4985 high0, 1, low1, 0));
4986 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4987 high1, 1, high0, 1));
4988
4989 /* We now have four cases, depending on whether we are including or
4990 excluding the two ranges. */
4991 if (in0_p && in1_p)
4992 {
4993 /* If they don't overlap, the result is false. If the second range
4994 is a subset it is the result. Otherwise, the range is from the start
4995 of the second to the end of the first. */
4996 if (no_overlap)
4997 in_p = 0, low = high = 0;
4998 else if (subset)
4999 in_p = 1, low = low1, high = high1;
5000 else
5001 in_p = 1, low = low1, high = high0;
5002 }
5003
5004 else if (in0_p && ! in1_p)
5005 {
5006 /* If they don't overlap, the result is the first range. If they are
5007 equal, the result is false. If the second range is a subset of the
5008 first, and the ranges begin at the same place, we go from just after
5009 the end of the second range to the end of the first. If the second
5010 range is not a subset of the first, or if it is a subset and both
5011 ranges end at the same place, the range starts at the start of the
5012 first range and ends just before the second range.
5013 Otherwise, we can't describe this as a single range. */
5014 if (no_overlap)
5015 in_p = 1, low = low0, high = high0;
5016 else if (lowequal && highequal)
5017 in_p = 0, low = high = 0;
5018 else if (subset && lowequal)
5019 {
5020 low = range_successor (high1);
5021 high = high0;
5022 in_p = 1;
5023 if (low == 0)
5024 {
5025 /* We are in the weird situation where high0 > high1 but
5026 high1 has no successor. Punt. */
5027 return 0;
5028 }
5029 }
5030 else if (! subset || highequal)
5031 {
5032 low = low0;
5033 high = range_predecessor (low1);
5034 in_p = 1;
5035 if (high == 0)
5036 {
5037 /* low0 < low1 but low1 has no predecessor. Punt. */
5038 return 0;
5039 }
5040 }
5041 else
5042 return 0;
5043 }
5044
5045 else if (! in0_p && in1_p)
5046 {
5047 /* If they don't overlap, the result is the second range. If the second
5048 is a subset of the first, the result is false. Otherwise,
5049 the range starts just after the first range and ends at the
5050 end of the second. */
5051 if (no_overlap)
5052 in_p = 1, low = low1, high = high1;
5053 else if (subset || highequal)
5054 in_p = 0, low = high = 0;
5055 else
5056 {
5057 low = range_successor (high0);
5058 high = high1;
5059 in_p = 1;
5060 if (low == 0)
5061 {
5062 /* high1 > high0 but high0 has no successor. Punt. */
5063 return 0;
5064 }
5065 }
5066 }
5067
5068 else
5069 {
5070 /* The case where we are excluding both ranges. Here the complex case
5071 is if they don't overlap. In that case, the only time we have a
5072 range is if they are adjacent. If the second is a subset of the
5073 first, the result is the first. Otherwise, the range to exclude
5074 starts at the beginning of the first range and ends at the end of the
5075 second. */
5076 if (no_overlap)
5077 {
5078 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5079 range_successor (high0),
5080 1, low1, 0)))
5081 in_p = 0, low = low0, high = high1;
5082 else
5083 {
5084 /* Canonicalize - [min, x] into - [-, x]. */
5085 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5086 switch (TREE_CODE (TREE_TYPE (low0)))
5087 {
5088 case ENUMERAL_TYPE:
5089 if (TYPE_PRECISION (TREE_TYPE (low0))
5090 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5091 break;
5092 /* FALLTHROUGH */
5093 case INTEGER_TYPE:
5094 if (tree_int_cst_equal (low0,
5095 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5096 low0 = 0;
5097 break;
5098 case POINTER_TYPE:
5099 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5100 && integer_zerop (low0))
5101 low0 = 0;
5102 break;
5103 default:
5104 break;
5105 }
5106
5107 /* Canonicalize - [x, max] into - [x, -]. */
5108 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5109 switch (TREE_CODE (TREE_TYPE (high1)))
5110 {
5111 case ENUMERAL_TYPE:
5112 if (TYPE_PRECISION (TREE_TYPE (high1))
5113 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5114 break;
5115 /* FALLTHROUGH */
5116 case INTEGER_TYPE:
5117 if (tree_int_cst_equal (high1,
5118 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5119 high1 = 0;
5120 break;
5121 case POINTER_TYPE:
5122 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5123 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5124 high1, 1,
5125 build_int_cst (TREE_TYPE (high1), 1),
5126 1)))
5127 high1 = 0;
5128 break;
5129 default:
5130 break;
5131 }
5132
5133 /* The ranges might be also adjacent between the maximum and
5134 minimum values of the given type. For
5135 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5136 return + [x + 1, y - 1]. */
5137 if (low0 == 0 && high1 == 0)
5138 {
5139 low = range_successor (high0);
5140 high = range_predecessor (low1);
5141 if (low == 0 || high == 0)
5142 return 0;
5143
5144 in_p = 1;
5145 }
5146 else
5147 return 0;
5148 }
5149 }
5150 else if (subset)
5151 in_p = 0, low = low0, high = high0;
5152 else
5153 in_p = 0, low = low0, high = high1;
5154 }
5155
5156 *pin_p = in_p, *plow = low, *phigh = high;
5157 return 1;
5158 }
5159 \f
5160
5161 /* Subroutine of fold, looking inside expressions of the form
5162 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5163 of the COND_EXPR. This function is being used also to optimize
5164 A op B ? C : A, by reversing the comparison first.
5165
5166 Return a folded expression whose code is not a COND_EXPR
5167 anymore, or NULL_TREE if no folding opportunity is found. */
5168
5169 static tree
5170 fold_cond_expr_with_comparison (location_t loc, tree type,
5171 tree arg0, tree arg1, tree arg2)
5172 {
5173 enum tree_code comp_code = TREE_CODE (arg0);
5174 tree arg00 = TREE_OPERAND (arg0, 0);
5175 tree arg01 = TREE_OPERAND (arg0, 1);
5176 tree arg1_type = TREE_TYPE (arg1);
5177 tree tem;
5178
5179 STRIP_NOPS (arg1);
5180 STRIP_NOPS (arg2);
5181
5182 /* If we have A op 0 ? A : -A, consider applying the following
5183 transformations:
5184
5185 A == 0? A : -A same as -A
5186 A != 0? A : -A same as A
5187 A >= 0? A : -A same as abs (A)
5188 A > 0? A : -A same as abs (A)
5189 A <= 0? A : -A same as -abs (A)
5190 A < 0? A : -A same as -abs (A)
5191
5192 None of these transformations work for modes with signed
5193 zeros. If A is +/-0, the first two transformations will
5194 change the sign of the result (from +0 to -0, or vice
5195 versa). The last four will fix the sign of the result,
5196 even though the original expressions could be positive or
5197 negative, depending on the sign of A.
5198
5199 Note that all these transformations are correct if A is
5200 NaN, since the two alternatives (A and -A) are also NaNs. */
5201 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5202 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5203 ? real_zerop (arg01)
5204 : integer_zerop (arg01))
5205 && ((TREE_CODE (arg2) == NEGATE_EXPR
5206 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5207 /* In the case that A is of the form X-Y, '-A' (arg2) may
5208 have already been folded to Y-X, check for that. */
5209 || (TREE_CODE (arg1) == MINUS_EXPR
5210 && TREE_CODE (arg2) == MINUS_EXPR
5211 && operand_equal_p (TREE_OPERAND (arg1, 0),
5212 TREE_OPERAND (arg2, 1), 0)
5213 && operand_equal_p (TREE_OPERAND (arg1, 1),
5214 TREE_OPERAND (arg2, 0), 0))))
5215 switch (comp_code)
5216 {
5217 case EQ_EXPR:
5218 case UNEQ_EXPR:
5219 tem = fold_convert_loc (loc, arg1_type, arg1);
5220 return fold_convert_loc (loc, type, negate_expr (tem));
5221 case NE_EXPR:
5222 case LTGT_EXPR:
5223 return fold_convert_loc (loc, type, arg1);
5224 case UNGE_EXPR:
5225 case UNGT_EXPR:
5226 if (flag_trapping_math)
5227 break;
5228 /* Fall through. */
5229 case GE_EXPR:
5230 case GT_EXPR:
5231 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5232 break;
5233 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5234 return fold_convert_loc (loc, type, tem);
5235 case UNLE_EXPR:
5236 case UNLT_EXPR:
5237 if (flag_trapping_math)
5238 break;
5239 /* FALLTHRU */
5240 case LE_EXPR:
5241 case LT_EXPR:
5242 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5243 break;
5244 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5245 return negate_expr (fold_convert_loc (loc, type, tem));
5246 default:
5247 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5248 break;
5249 }
5250
5251 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5252 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5253 both transformations are correct when A is NaN: A != 0
5254 is then true, and A == 0 is false. */
5255
5256 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5257 && integer_zerop (arg01) && integer_zerop (arg2))
5258 {
5259 if (comp_code == NE_EXPR)
5260 return fold_convert_loc (loc, type, arg1);
5261 else if (comp_code == EQ_EXPR)
5262 return build_zero_cst (type);
5263 }
5264
5265 /* Try some transformations of A op B ? A : B.
5266
5267 A == B? A : B same as B
5268 A != B? A : B same as A
5269 A >= B? A : B same as max (A, B)
5270 A > B? A : B same as max (B, A)
5271 A <= B? A : B same as min (A, B)
5272 A < B? A : B same as min (B, A)
5273
5274 As above, these transformations don't work in the presence
5275 of signed zeros. For example, if A and B are zeros of
5276 opposite sign, the first two transformations will change
5277 the sign of the result. In the last four, the original
5278 expressions give different results for (A=+0, B=-0) and
5279 (A=-0, B=+0), but the transformed expressions do not.
5280
5281 The first two transformations are correct if either A or B
5282 is a NaN. In the first transformation, the condition will
5283 be false, and B will indeed be chosen. In the case of the
5284 second transformation, the condition A != B will be true,
5285 and A will be chosen.
5286
5287 The conversions to max() and min() are not correct if B is
5288 a number and A is not. The conditions in the original
5289 expressions will be false, so all four give B. The min()
5290 and max() versions would give a NaN instead. */
5291 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5292 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5293 /* Avoid these transformations if the COND_EXPR may be used
5294 as an lvalue in the C++ front-end. PR c++/19199. */
5295 && (in_gimple_form
5296 || VECTOR_TYPE_P (type)
5297 || (! lang_GNU_CXX ()
5298 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5299 || ! maybe_lvalue_p (arg1)
5300 || ! maybe_lvalue_p (arg2)))
5301 {
5302 tree comp_op0 = arg00;
5303 tree comp_op1 = arg01;
5304 tree comp_type = TREE_TYPE (comp_op0);
5305
5306 switch (comp_code)
5307 {
5308 case EQ_EXPR:
5309 return fold_convert_loc (loc, type, arg2);
5310 case NE_EXPR:
5311 return fold_convert_loc (loc, type, arg1);
5312 case LE_EXPR:
5313 case LT_EXPR:
5314 case UNLE_EXPR:
5315 case UNLT_EXPR:
5316 /* In C++ a ?: expression can be an lvalue, so put the
5317 operand which will be used if they are equal first
5318 so that we can convert this back to the
5319 corresponding COND_EXPR. */
5320 if (!HONOR_NANS (arg1))
5321 {
5322 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5323 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5324 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5325 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5326 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5327 comp_op1, comp_op0);
5328 return fold_convert_loc (loc, type, tem);
5329 }
5330 break;
5331 case GE_EXPR:
5332 case GT_EXPR:
5333 case UNGE_EXPR:
5334 case UNGT_EXPR:
5335 if (!HONOR_NANS (arg1))
5336 {
5337 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5338 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5339 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5340 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5341 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5342 comp_op1, comp_op0);
5343 return fold_convert_loc (loc, type, tem);
5344 }
5345 break;
5346 case UNEQ_EXPR:
5347 if (!HONOR_NANS (arg1))
5348 return fold_convert_loc (loc, type, arg2);
5349 break;
5350 case LTGT_EXPR:
5351 if (!HONOR_NANS (arg1))
5352 return fold_convert_loc (loc, type, arg1);
5353 break;
5354 default:
5355 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5356 break;
5357 }
5358 }
5359
5360 return NULL_TREE;
5361 }
5362
5363
5364 \f
5365 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5366 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5367 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5368 false) >= 2)
5369 #endif
5370
5371 /* EXP is some logical combination of boolean tests. See if we can
5372 merge it into some range test. Return the new tree if so. */
5373
5374 static tree
5375 fold_range_test (location_t loc, enum tree_code code, tree type,
5376 tree op0, tree op1)
5377 {
5378 int or_op = (code == TRUTH_ORIF_EXPR
5379 || code == TRUTH_OR_EXPR);
5380 int in0_p, in1_p, in_p;
5381 tree low0, low1, low, high0, high1, high;
5382 bool strict_overflow_p = false;
5383 tree tem, lhs, rhs;
5384 const char * const warnmsg = G_("assuming signed overflow does not occur "
5385 "when simplifying range test");
5386
5387 if (!INTEGRAL_TYPE_P (type))
5388 return 0;
5389
5390 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5391 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5392
5393 /* If this is an OR operation, invert both sides; we will invert
5394 again at the end. */
5395 if (or_op)
5396 in0_p = ! in0_p, in1_p = ! in1_p;
5397
5398 /* If both expressions are the same, if we can merge the ranges, and we
5399 can build the range test, return it or it inverted. If one of the
5400 ranges is always true or always false, consider it to be the same
5401 expression as the other. */
5402 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5403 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5404 in1_p, low1, high1)
5405 && 0 != (tem = (build_range_check (loc, type,
5406 lhs != 0 ? lhs
5407 : rhs != 0 ? rhs : integer_zero_node,
5408 in_p, low, high))))
5409 {
5410 if (strict_overflow_p)
5411 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5412 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5413 }
5414
5415 /* On machines where the branch cost is expensive, if this is a
5416 short-circuited branch and the underlying object on both sides
5417 is the same, make a non-short-circuit operation. */
5418 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5419 && lhs != 0 && rhs != 0
5420 && (code == TRUTH_ANDIF_EXPR
5421 || code == TRUTH_ORIF_EXPR)
5422 && operand_equal_p (lhs, rhs, 0))
5423 {
5424 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5425 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5426 which cases we can't do this. */
5427 if (simple_operand_p (lhs))
5428 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5429 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5430 type, op0, op1);
5431
5432 else if (!lang_hooks.decls.global_bindings_p ()
5433 && !CONTAINS_PLACEHOLDER_P (lhs))
5434 {
5435 tree common = save_expr (lhs);
5436
5437 if (0 != (lhs = build_range_check (loc, type, common,
5438 or_op ? ! in0_p : in0_p,
5439 low0, high0))
5440 && (0 != (rhs = build_range_check (loc, type, common,
5441 or_op ? ! in1_p : in1_p,
5442 low1, high1))))
5443 {
5444 if (strict_overflow_p)
5445 fold_overflow_warning (warnmsg,
5446 WARN_STRICT_OVERFLOW_COMPARISON);
5447 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5448 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5449 type, lhs, rhs);
5450 }
5451 }
5452 }
5453
5454 return 0;
5455 }
5456 \f
5457 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5458 bit value. Arrange things so the extra bits will be set to zero if and
5459 only if C is signed-extended to its full width. If MASK is nonzero,
5460 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5461
5462 static tree
5463 unextend (tree c, int p, int unsignedp, tree mask)
5464 {
5465 tree type = TREE_TYPE (c);
5466 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5467 tree temp;
5468
5469 if (p == modesize || unsignedp)
5470 return c;
5471
5472 /* We work by getting just the sign bit into the low-order bit, then
5473 into the high-order bit, then sign-extend. We then XOR that value
5474 with C. */
5475 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5476
5477 /* We must use a signed type in order to get an arithmetic right shift.
5478 However, we must also avoid introducing accidental overflows, so that
5479 a subsequent call to integer_zerop will work. Hence we must
5480 do the type conversion here. At this point, the constant is either
5481 zero or one, and the conversion to a signed type can never overflow.
5482 We could get an overflow if this conversion is done anywhere else. */
5483 if (TYPE_UNSIGNED (type))
5484 temp = fold_convert (signed_type_for (type), temp);
5485
5486 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5487 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5488 if (mask != 0)
5489 temp = const_binop (BIT_AND_EXPR, temp,
5490 fold_convert (TREE_TYPE (c), mask));
5491 /* If necessary, convert the type back to match the type of C. */
5492 if (TYPE_UNSIGNED (type))
5493 temp = fold_convert (type, temp);
5494
5495 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5496 }
5497 \f
5498 /* For an expression that has the form
5499 (A && B) || ~B
5500 or
5501 (A || B) && ~B,
5502 we can drop one of the inner expressions and simplify to
5503 A || ~B
5504 or
5505 A && ~B
5506 LOC is the location of the resulting expression. OP is the inner
5507 logical operation; the left-hand side in the examples above, while CMPOP
5508 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5509 removing a condition that guards another, as in
5510 (A != NULL && A->...) || A == NULL
5511 which we must not transform. If RHS_ONLY is true, only eliminate the
5512 right-most operand of the inner logical operation. */
5513
5514 static tree
5515 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5516 bool rhs_only)
5517 {
5518 tree type = TREE_TYPE (cmpop);
5519 enum tree_code code = TREE_CODE (cmpop);
5520 enum tree_code truthop_code = TREE_CODE (op);
5521 tree lhs = TREE_OPERAND (op, 0);
5522 tree rhs = TREE_OPERAND (op, 1);
5523 tree orig_lhs = lhs, orig_rhs = rhs;
5524 enum tree_code rhs_code = TREE_CODE (rhs);
5525 enum tree_code lhs_code = TREE_CODE (lhs);
5526 enum tree_code inv_code;
5527
5528 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5529 return NULL_TREE;
5530
5531 if (TREE_CODE_CLASS (code) != tcc_comparison)
5532 return NULL_TREE;
5533
5534 if (rhs_code == truthop_code)
5535 {
5536 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5537 if (newrhs != NULL_TREE)
5538 {
5539 rhs = newrhs;
5540 rhs_code = TREE_CODE (rhs);
5541 }
5542 }
5543 if (lhs_code == truthop_code && !rhs_only)
5544 {
5545 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5546 if (newlhs != NULL_TREE)
5547 {
5548 lhs = newlhs;
5549 lhs_code = TREE_CODE (lhs);
5550 }
5551 }
5552
5553 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5554 if (inv_code == rhs_code
5555 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5556 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5557 return lhs;
5558 if (!rhs_only && inv_code == lhs_code
5559 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5560 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5561 return rhs;
5562 if (rhs != orig_rhs || lhs != orig_lhs)
5563 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5564 lhs, rhs);
5565 return NULL_TREE;
5566 }
5567
5568 /* Find ways of folding logical expressions of LHS and RHS:
5569 Try to merge two comparisons to the same innermost item.
5570 Look for range tests like "ch >= '0' && ch <= '9'".
5571 Look for combinations of simple terms on machines with expensive branches
5572 and evaluate the RHS unconditionally.
5573
5574 For example, if we have p->a == 2 && p->b == 4 and we can make an
5575 object large enough to span both A and B, we can do this with a comparison
5576 against the object ANDed with the a mask.
5577
5578 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5579 operations to do this with one comparison.
5580
5581 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5582 function and the one above.
5583
5584 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5585 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5586
5587 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5588 two operands.
5589
5590 We return the simplified tree or 0 if no optimization is possible. */
5591
5592 static tree
5593 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5594 tree lhs, tree rhs)
5595 {
5596 /* If this is the "or" of two comparisons, we can do something if
5597 the comparisons are NE_EXPR. If this is the "and", we can do something
5598 if the comparisons are EQ_EXPR. I.e.,
5599 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5600
5601 WANTED_CODE is this operation code. For single bit fields, we can
5602 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5603 comparison for one-bit fields. */
5604
5605 enum tree_code wanted_code;
5606 enum tree_code lcode, rcode;
5607 tree ll_arg, lr_arg, rl_arg, rr_arg;
5608 tree ll_inner, lr_inner, rl_inner, rr_inner;
5609 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5610 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5611 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5612 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5613 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5614 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5615 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5616 machine_mode lnmode, rnmode;
5617 tree ll_mask, lr_mask, rl_mask, rr_mask;
5618 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5619 tree l_const, r_const;
5620 tree lntype, rntype, result;
5621 HOST_WIDE_INT first_bit, end_bit;
5622 int volatilep;
5623
5624 /* Start by getting the comparison codes. Fail if anything is volatile.
5625 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5626 it were surrounded with a NE_EXPR. */
5627
5628 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5629 return 0;
5630
5631 lcode = TREE_CODE (lhs);
5632 rcode = TREE_CODE (rhs);
5633
5634 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5635 {
5636 lhs = build2 (NE_EXPR, truth_type, lhs,
5637 build_int_cst (TREE_TYPE (lhs), 0));
5638 lcode = NE_EXPR;
5639 }
5640
5641 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5642 {
5643 rhs = build2 (NE_EXPR, truth_type, rhs,
5644 build_int_cst (TREE_TYPE (rhs), 0));
5645 rcode = NE_EXPR;
5646 }
5647
5648 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5649 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5650 return 0;
5651
5652 ll_arg = TREE_OPERAND (lhs, 0);
5653 lr_arg = TREE_OPERAND (lhs, 1);
5654 rl_arg = TREE_OPERAND (rhs, 0);
5655 rr_arg = TREE_OPERAND (rhs, 1);
5656
5657 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5658 if (simple_operand_p (ll_arg)
5659 && simple_operand_p (lr_arg))
5660 {
5661 if (operand_equal_p (ll_arg, rl_arg, 0)
5662 && operand_equal_p (lr_arg, rr_arg, 0))
5663 {
5664 result = combine_comparisons (loc, code, lcode, rcode,
5665 truth_type, ll_arg, lr_arg);
5666 if (result)
5667 return result;
5668 }
5669 else if (operand_equal_p (ll_arg, rr_arg, 0)
5670 && operand_equal_p (lr_arg, rl_arg, 0))
5671 {
5672 result = combine_comparisons (loc, code, lcode,
5673 swap_tree_comparison (rcode),
5674 truth_type, ll_arg, lr_arg);
5675 if (result)
5676 return result;
5677 }
5678 }
5679
5680 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5681 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5682
5683 /* If the RHS can be evaluated unconditionally and its operands are
5684 simple, it wins to evaluate the RHS unconditionally on machines
5685 with expensive branches. In this case, this isn't a comparison
5686 that can be merged. */
5687
5688 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5689 false) >= 2
5690 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5691 && simple_operand_p (rl_arg)
5692 && simple_operand_p (rr_arg))
5693 {
5694 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5695 if (code == TRUTH_OR_EXPR
5696 && lcode == NE_EXPR && integer_zerop (lr_arg)
5697 && rcode == NE_EXPR && integer_zerop (rr_arg)
5698 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5699 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5700 return build2_loc (loc, NE_EXPR, truth_type,
5701 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5702 ll_arg, rl_arg),
5703 build_int_cst (TREE_TYPE (ll_arg), 0));
5704
5705 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5706 if (code == TRUTH_AND_EXPR
5707 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5708 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5709 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5710 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5711 return build2_loc (loc, EQ_EXPR, truth_type,
5712 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5713 ll_arg, rl_arg),
5714 build_int_cst (TREE_TYPE (ll_arg), 0));
5715 }
5716
5717 /* See if the comparisons can be merged. Then get all the parameters for
5718 each side. */
5719
5720 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5721 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5722 return 0;
5723
5724 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5725 volatilep = 0;
5726 ll_inner = decode_field_reference (loc, &ll_arg,
5727 &ll_bitsize, &ll_bitpos, &ll_mode,
5728 &ll_unsignedp, &ll_reversep, &volatilep,
5729 &ll_mask, &ll_and_mask);
5730 lr_inner = decode_field_reference (loc, &lr_arg,
5731 &lr_bitsize, &lr_bitpos, &lr_mode,
5732 &lr_unsignedp, &lr_reversep, &volatilep,
5733 &lr_mask, &lr_and_mask);
5734 rl_inner = decode_field_reference (loc, &rl_arg,
5735 &rl_bitsize, &rl_bitpos, &rl_mode,
5736 &rl_unsignedp, &rl_reversep, &volatilep,
5737 &rl_mask, &rl_and_mask);
5738 rr_inner = decode_field_reference (loc, &rr_arg,
5739 &rr_bitsize, &rr_bitpos, &rr_mode,
5740 &rr_unsignedp, &rr_reversep, &volatilep,
5741 &rr_mask, &rr_and_mask);
5742
5743 /* It must be true that the inner operation on the lhs of each
5744 comparison must be the same if we are to be able to do anything.
5745 Then see if we have constants. If not, the same must be true for
5746 the rhs's. */
5747 if (volatilep
5748 || ll_reversep != rl_reversep
5749 || ll_inner == 0 || rl_inner == 0
5750 || ! operand_equal_p (ll_inner, rl_inner, 0))
5751 return 0;
5752
5753 if (TREE_CODE (lr_arg) == INTEGER_CST
5754 && TREE_CODE (rr_arg) == INTEGER_CST)
5755 {
5756 l_const = lr_arg, r_const = rr_arg;
5757 lr_reversep = ll_reversep;
5758 }
5759 else if (lr_reversep != rr_reversep
5760 || lr_inner == 0 || rr_inner == 0
5761 || ! operand_equal_p (lr_inner, rr_inner, 0))
5762 return 0;
5763 else
5764 l_const = r_const = 0;
5765
5766 /* If either comparison code is not correct for our logical operation,
5767 fail. However, we can convert a one-bit comparison against zero into
5768 the opposite comparison against that bit being set in the field. */
5769
5770 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5771 if (lcode != wanted_code)
5772 {
5773 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5774 {
5775 /* Make the left operand unsigned, since we are only interested
5776 in the value of one bit. Otherwise we are doing the wrong
5777 thing below. */
5778 ll_unsignedp = 1;
5779 l_const = ll_mask;
5780 }
5781 else
5782 return 0;
5783 }
5784
5785 /* This is analogous to the code for l_const above. */
5786 if (rcode != wanted_code)
5787 {
5788 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5789 {
5790 rl_unsignedp = 1;
5791 r_const = rl_mask;
5792 }
5793 else
5794 return 0;
5795 }
5796
5797 /* See if we can find a mode that contains both fields being compared on
5798 the left. If we can't, fail. Otherwise, update all constants and masks
5799 to be relative to a field of that size. */
5800 first_bit = MIN (ll_bitpos, rl_bitpos);
5801 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5802 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5803 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5804 volatilep);
5805 if (lnmode == VOIDmode)
5806 return 0;
5807
5808 lnbitsize = GET_MODE_BITSIZE (lnmode);
5809 lnbitpos = first_bit & ~ (lnbitsize - 1);
5810 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5811 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5812
5813 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5814 {
5815 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5816 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5817 }
5818
5819 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5820 size_int (xll_bitpos));
5821 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5822 size_int (xrl_bitpos));
5823
5824 if (l_const)
5825 {
5826 l_const = fold_convert_loc (loc, lntype, l_const);
5827 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5828 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5829 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5830 fold_build1_loc (loc, BIT_NOT_EXPR,
5831 lntype, ll_mask))))
5832 {
5833 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5834
5835 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5836 }
5837 }
5838 if (r_const)
5839 {
5840 r_const = fold_convert_loc (loc, lntype, r_const);
5841 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5842 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5843 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5844 fold_build1_loc (loc, BIT_NOT_EXPR,
5845 lntype, rl_mask))))
5846 {
5847 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5848
5849 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5850 }
5851 }
5852
5853 /* If the right sides are not constant, do the same for it. Also,
5854 disallow this optimization if a size or signedness mismatch occurs
5855 between the left and right sides. */
5856 if (l_const == 0)
5857 {
5858 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5859 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5860 /* Make sure the two fields on the right
5861 correspond to the left without being swapped. */
5862 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5863 return 0;
5864
5865 first_bit = MIN (lr_bitpos, rr_bitpos);
5866 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5867 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5868 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5869 volatilep);
5870 if (rnmode == VOIDmode)
5871 return 0;
5872
5873 rnbitsize = GET_MODE_BITSIZE (rnmode);
5874 rnbitpos = first_bit & ~ (rnbitsize - 1);
5875 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5876 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5877
5878 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5879 {
5880 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5881 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5882 }
5883
5884 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5885 rntype, lr_mask),
5886 size_int (xlr_bitpos));
5887 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5888 rntype, rr_mask),
5889 size_int (xrr_bitpos));
5890
5891 /* Make a mask that corresponds to both fields being compared.
5892 Do this for both items being compared. If the operands are the
5893 same size and the bits being compared are in the same position
5894 then we can do this by masking both and comparing the masked
5895 results. */
5896 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5897 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5898 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5899 {
5900 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
5901 lntype, lnbitsize, lnbitpos,
5902 ll_unsignedp || rl_unsignedp, ll_reversep);
5903 if (! all_ones_mask_p (ll_mask, lnbitsize))
5904 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5905
5906 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
5907 rntype, rnbitsize, rnbitpos,
5908 lr_unsignedp || rr_unsignedp, lr_reversep);
5909 if (! all_ones_mask_p (lr_mask, rnbitsize))
5910 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5911
5912 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5913 }
5914
5915 /* There is still another way we can do something: If both pairs of
5916 fields being compared are adjacent, we may be able to make a wider
5917 field containing them both.
5918
5919 Note that we still must mask the lhs/rhs expressions. Furthermore,
5920 the mask must be shifted to account for the shift done by
5921 make_bit_field_ref. */
5922 if ((ll_bitsize + ll_bitpos == rl_bitpos
5923 && lr_bitsize + lr_bitpos == rr_bitpos)
5924 || (ll_bitpos == rl_bitpos + rl_bitsize
5925 && lr_bitpos == rr_bitpos + rr_bitsize))
5926 {
5927 tree type;
5928
5929 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
5930 ll_bitsize + rl_bitsize,
5931 MIN (ll_bitpos, rl_bitpos),
5932 ll_unsignedp, ll_reversep);
5933 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
5934 lr_bitsize + rr_bitsize,
5935 MIN (lr_bitpos, rr_bitpos),
5936 lr_unsignedp, lr_reversep);
5937
5938 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5939 size_int (MIN (xll_bitpos, xrl_bitpos)));
5940 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5941 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5942
5943 /* Convert to the smaller type before masking out unwanted bits. */
5944 type = lntype;
5945 if (lntype != rntype)
5946 {
5947 if (lnbitsize > rnbitsize)
5948 {
5949 lhs = fold_convert_loc (loc, rntype, lhs);
5950 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5951 type = rntype;
5952 }
5953 else if (lnbitsize < rnbitsize)
5954 {
5955 rhs = fold_convert_loc (loc, lntype, rhs);
5956 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5957 type = lntype;
5958 }
5959 }
5960
5961 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5962 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5963
5964 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5965 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5966
5967 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5968 }
5969
5970 return 0;
5971 }
5972
5973 /* Handle the case of comparisons with constants. If there is something in
5974 common between the masks, those bits of the constants must be the same.
5975 If not, the condition is always false. Test for this to avoid generating
5976 incorrect code below. */
5977 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5978 if (! integer_zerop (result)
5979 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5980 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5981 {
5982 if (wanted_code == NE_EXPR)
5983 {
5984 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5985 return constant_boolean_node (true, truth_type);
5986 }
5987 else
5988 {
5989 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5990 return constant_boolean_node (false, truth_type);
5991 }
5992 }
5993
5994 /* Construct the expression we will return. First get the component
5995 reference we will make. Unless the mask is all ones the width of
5996 that field, perform the mask operation. Then compare with the
5997 merged constant. */
5998 result = make_bit_field_ref (loc, ll_inner, ll_arg,
5999 lntype, lnbitsize, lnbitpos,
6000 ll_unsignedp || rl_unsignedp, ll_reversep);
6001
6002 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6003 if (! all_ones_mask_p (ll_mask, lnbitsize))
6004 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6005
6006 return build2_loc (loc, wanted_code, truth_type, result,
6007 const_binop (BIT_IOR_EXPR, l_const, r_const));
6008 }
6009 \f
6010 /* T is an integer expression that is being multiplied, divided, or taken a
6011 modulus (CODE says which and what kind of divide or modulus) by a
6012 constant C. See if we can eliminate that operation by folding it with
6013 other operations already in T. WIDE_TYPE, if non-null, is a type that
6014 should be used for the computation if wider than our type.
6015
6016 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6017 (X * 2) + (Y * 4). We must, however, be assured that either the original
6018 expression would not overflow or that overflow is undefined for the type
6019 in the language in question.
6020
6021 If we return a non-null expression, it is an equivalent form of the
6022 original computation, but need not be in the original type.
6023
6024 We set *STRICT_OVERFLOW_P to true if the return values depends on
6025 signed overflow being undefined. Otherwise we do not change
6026 *STRICT_OVERFLOW_P. */
6027
6028 static tree
6029 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6030 bool *strict_overflow_p)
6031 {
6032 /* To avoid exponential search depth, refuse to allow recursion past
6033 three levels. Beyond that (1) it's highly unlikely that we'll find
6034 something interesting and (2) we've probably processed it before
6035 when we built the inner expression. */
6036
6037 static int depth;
6038 tree ret;
6039
6040 if (depth > 3)
6041 return NULL;
6042
6043 depth++;
6044 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6045 depth--;
6046
6047 return ret;
6048 }
6049
6050 static tree
6051 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6052 bool *strict_overflow_p)
6053 {
6054 tree type = TREE_TYPE (t);
6055 enum tree_code tcode = TREE_CODE (t);
6056 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6057 > GET_MODE_SIZE (TYPE_MODE (type)))
6058 ? wide_type : type);
6059 tree t1, t2;
6060 int same_p = tcode == code;
6061 tree op0 = NULL_TREE, op1 = NULL_TREE;
6062 bool sub_strict_overflow_p;
6063
6064 /* Don't deal with constants of zero here; they confuse the code below. */
6065 if (integer_zerop (c))
6066 return NULL_TREE;
6067
6068 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6069 op0 = TREE_OPERAND (t, 0);
6070
6071 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6072 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6073
6074 /* Note that we need not handle conditional operations here since fold
6075 already handles those cases. So just do arithmetic here. */
6076 switch (tcode)
6077 {
6078 case INTEGER_CST:
6079 /* For a constant, we can always simplify if we are a multiply
6080 or (for divide and modulus) if it is a multiple of our constant. */
6081 if (code == MULT_EXPR
6082 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6083 {
6084 tree tem = const_binop (code, fold_convert (ctype, t),
6085 fold_convert (ctype, c));
6086 /* If the multiplication overflowed, we lost information on it.
6087 See PR68142 and PR69845. */
6088 if (TREE_OVERFLOW (tem))
6089 return NULL_TREE;
6090 return tem;
6091 }
6092 break;
6093
6094 CASE_CONVERT: case NON_LVALUE_EXPR:
6095 /* If op0 is an expression ... */
6096 if ((COMPARISON_CLASS_P (op0)
6097 || UNARY_CLASS_P (op0)
6098 || BINARY_CLASS_P (op0)
6099 || VL_EXP_CLASS_P (op0)
6100 || EXPRESSION_CLASS_P (op0))
6101 /* ... and has wrapping overflow, and its type is smaller
6102 than ctype, then we cannot pass through as widening. */
6103 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6104 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6105 && (TYPE_PRECISION (ctype)
6106 > TYPE_PRECISION (TREE_TYPE (op0))))
6107 /* ... or this is a truncation (t is narrower than op0),
6108 then we cannot pass through this narrowing. */
6109 || (TYPE_PRECISION (type)
6110 < TYPE_PRECISION (TREE_TYPE (op0)))
6111 /* ... or signedness changes for division or modulus,
6112 then we cannot pass through this conversion. */
6113 || (code != MULT_EXPR
6114 && (TYPE_UNSIGNED (ctype)
6115 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6116 /* ... or has undefined overflow while the converted to
6117 type has not, we cannot do the operation in the inner type
6118 as that would introduce undefined overflow. */
6119 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6120 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6121 && !TYPE_OVERFLOW_UNDEFINED (type))))
6122 break;
6123
6124 /* Pass the constant down and see if we can make a simplification. If
6125 we can, replace this expression with the inner simplification for
6126 possible later conversion to our or some other type. */
6127 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6128 && TREE_CODE (t2) == INTEGER_CST
6129 && !TREE_OVERFLOW (t2)
6130 && (0 != (t1 = extract_muldiv (op0, t2, code,
6131 code == MULT_EXPR
6132 ? ctype : NULL_TREE,
6133 strict_overflow_p))))
6134 return t1;
6135 break;
6136
6137 case ABS_EXPR:
6138 /* If widening the type changes it from signed to unsigned, then we
6139 must avoid building ABS_EXPR itself as unsigned. */
6140 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6141 {
6142 tree cstype = (*signed_type_for) (ctype);
6143 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6144 != 0)
6145 {
6146 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6147 return fold_convert (ctype, t1);
6148 }
6149 break;
6150 }
6151 /* If the constant is negative, we cannot simplify this. */
6152 if (tree_int_cst_sgn (c) == -1)
6153 break;
6154 /* FALLTHROUGH */
6155 case NEGATE_EXPR:
6156 /* For division and modulus, type can't be unsigned, as e.g.
6157 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6158 For signed types, even with wrapping overflow, this is fine. */
6159 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6160 break;
6161 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6162 != 0)
6163 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6164 break;
6165
6166 case MIN_EXPR: case MAX_EXPR:
6167 /* If widening the type changes the signedness, then we can't perform
6168 this optimization as that changes the result. */
6169 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6170 break;
6171
6172 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6173 sub_strict_overflow_p = false;
6174 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6175 &sub_strict_overflow_p)) != 0
6176 && (t2 = extract_muldiv (op1, c, code, wide_type,
6177 &sub_strict_overflow_p)) != 0)
6178 {
6179 if (tree_int_cst_sgn (c) < 0)
6180 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6181 if (sub_strict_overflow_p)
6182 *strict_overflow_p = true;
6183 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6184 fold_convert (ctype, t2));
6185 }
6186 break;
6187
6188 case LSHIFT_EXPR: case RSHIFT_EXPR:
6189 /* If the second operand is constant, this is a multiplication
6190 or floor division, by a power of two, so we can treat it that
6191 way unless the multiplier or divisor overflows. Signed
6192 left-shift overflow is implementation-defined rather than
6193 undefined in C90, so do not convert signed left shift into
6194 multiplication. */
6195 if (TREE_CODE (op1) == INTEGER_CST
6196 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6197 /* const_binop may not detect overflow correctly,
6198 so check for it explicitly here. */
6199 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6200 && 0 != (t1 = fold_convert (ctype,
6201 const_binop (LSHIFT_EXPR,
6202 size_one_node,
6203 op1)))
6204 && !TREE_OVERFLOW (t1))
6205 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6206 ? MULT_EXPR : FLOOR_DIV_EXPR,
6207 ctype,
6208 fold_convert (ctype, op0),
6209 t1),
6210 c, code, wide_type, strict_overflow_p);
6211 break;
6212
6213 case PLUS_EXPR: case MINUS_EXPR:
6214 /* See if we can eliminate the operation on both sides. If we can, we
6215 can return a new PLUS or MINUS. If we can't, the only remaining
6216 cases where we can do anything are if the second operand is a
6217 constant. */
6218 sub_strict_overflow_p = false;
6219 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6220 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6221 if (t1 != 0 && t2 != 0
6222 && TYPE_OVERFLOW_WRAPS (ctype)
6223 && (code == MULT_EXPR
6224 /* If not multiplication, we can only do this if both operands
6225 are divisible by c. */
6226 || (multiple_of_p (ctype, op0, c)
6227 && multiple_of_p (ctype, op1, c))))
6228 {
6229 if (sub_strict_overflow_p)
6230 *strict_overflow_p = true;
6231 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6232 fold_convert (ctype, t2));
6233 }
6234
6235 /* If this was a subtraction, negate OP1 and set it to be an addition.
6236 This simplifies the logic below. */
6237 if (tcode == MINUS_EXPR)
6238 {
6239 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6240 /* If OP1 was not easily negatable, the constant may be OP0. */
6241 if (TREE_CODE (op0) == INTEGER_CST)
6242 {
6243 std::swap (op0, op1);
6244 std::swap (t1, t2);
6245 }
6246 }
6247
6248 if (TREE_CODE (op1) != INTEGER_CST)
6249 break;
6250
6251 /* If either OP1 or C are negative, this optimization is not safe for
6252 some of the division and remainder types while for others we need
6253 to change the code. */
6254 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6255 {
6256 if (code == CEIL_DIV_EXPR)
6257 code = FLOOR_DIV_EXPR;
6258 else if (code == FLOOR_DIV_EXPR)
6259 code = CEIL_DIV_EXPR;
6260 else if (code != MULT_EXPR
6261 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6262 break;
6263 }
6264
6265 /* If it's a multiply or a division/modulus operation of a multiple
6266 of our constant, do the operation and verify it doesn't overflow. */
6267 if (code == MULT_EXPR
6268 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6269 {
6270 op1 = const_binop (code, fold_convert (ctype, op1),
6271 fold_convert (ctype, c));
6272 /* We allow the constant to overflow with wrapping semantics. */
6273 if (op1 == 0
6274 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6275 break;
6276 }
6277 else
6278 break;
6279
6280 /* If we have an unsigned type, we cannot widen the operation since it
6281 will change the result if the original computation overflowed. */
6282 if (TYPE_UNSIGNED (ctype) && ctype != type)
6283 break;
6284
6285 /* The last case is if we are a multiply. In that case, we can
6286 apply the distributive law to commute the multiply and addition
6287 if the multiplication of the constants doesn't overflow
6288 and overflow is defined. With undefined overflow
6289 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6290 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6291 return fold_build2 (tcode, ctype,
6292 fold_build2 (code, ctype,
6293 fold_convert (ctype, op0),
6294 fold_convert (ctype, c)),
6295 op1);
6296
6297 break;
6298
6299 case MULT_EXPR:
6300 /* We have a special case here if we are doing something like
6301 (C * 8) % 4 since we know that's zero. */
6302 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6303 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6304 /* If the multiplication can overflow we cannot optimize this. */
6305 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6306 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6307 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6308 {
6309 *strict_overflow_p = true;
6310 return omit_one_operand (type, integer_zero_node, op0);
6311 }
6312
6313 /* ... fall through ... */
6314
6315 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6316 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6317 /* If we can extract our operation from the LHS, do so and return a
6318 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6319 do something only if the second operand is a constant. */
6320 if (same_p
6321 && TYPE_OVERFLOW_WRAPS (ctype)
6322 && (t1 = extract_muldiv (op0, c, code, wide_type,
6323 strict_overflow_p)) != 0)
6324 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6325 fold_convert (ctype, op1));
6326 else if (tcode == MULT_EXPR && code == MULT_EXPR
6327 && TYPE_OVERFLOW_WRAPS (ctype)
6328 && (t1 = extract_muldiv (op1, c, code, wide_type,
6329 strict_overflow_p)) != 0)
6330 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6331 fold_convert (ctype, t1));
6332 else if (TREE_CODE (op1) != INTEGER_CST)
6333 return 0;
6334
6335 /* If these are the same operation types, we can associate them
6336 assuming no overflow. */
6337 if (tcode == code)
6338 {
6339 bool overflow_p = false;
6340 bool overflow_mul_p;
6341 signop sign = TYPE_SIGN (ctype);
6342 unsigned prec = TYPE_PRECISION (ctype);
6343 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6344 wi::to_wide (c, prec),
6345 sign, &overflow_mul_p);
6346 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6347 if (overflow_mul_p
6348 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6349 overflow_p = true;
6350 if (!overflow_p)
6351 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6352 wide_int_to_tree (ctype, mul));
6353 }
6354
6355 /* If these operations "cancel" each other, we have the main
6356 optimizations of this pass, which occur when either constant is a
6357 multiple of the other, in which case we replace this with either an
6358 operation or CODE or TCODE.
6359
6360 If we have an unsigned type, we cannot do this since it will change
6361 the result if the original computation overflowed. */
6362 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6363 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6364 || (tcode == MULT_EXPR
6365 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6366 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6367 && code != MULT_EXPR)))
6368 {
6369 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6370 {
6371 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6372 *strict_overflow_p = true;
6373 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6374 fold_convert (ctype,
6375 const_binop (TRUNC_DIV_EXPR,
6376 op1, c)));
6377 }
6378 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6379 {
6380 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6381 *strict_overflow_p = true;
6382 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6383 fold_convert (ctype,
6384 const_binop (TRUNC_DIV_EXPR,
6385 c, op1)));
6386 }
6387 }
6388 break;
6389
6390 default:
6391 break;
6392 }
6393
6394 return 0;
6395 }
6396 \f
6397 /* Return a node which has the indicated constant VALUE (either 0 or
6398 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6399 and is of the indicated TYPE. */
6400
6401 tree
6402 constant_boolean_node (bool value, tree type)
6403 {
6404 if (type == integer_type_node)
6405 return value ? integer_one_node : integer_zero_node;
6406 else if (type == boolean_type_node)
6407 return value ? boolean_true_node : boolean_false_node;
6408 else if (TREE_CODE (type) == VECTOR_TYPE)
6409 return build_vector_from_val (type,
6410 build_int_cst (TREE_TYPE (type),
6411 value ? -1 : 0));
6412 else
6413 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6414 }
6415
6416
6417 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6418 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6419 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6420 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6421 COND is the first argument to CODE; otherwise (as in the example
6422 given here), it is the second argument. TYPE is the type of the
6423 original expression. Return NULL_TREE if no simplification is
6424 possible. */
6425
6426 static tree
6427 fold_binary_op_with_conditional_arg (location_t loc,
6428 enum tree_code code,
6429 tree type, tree op0, tree op1,
6430 tree cond, tree arg, int cond_first_p)
6431 {
6432 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6433 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6434 tree test, true_value, false_value;
6435 tree lhs = NULL_TREE;
6436 tree rhs = NULL_TREE;
6437 enum tree_code cond_code = COND_EXPR;
6438
6439 if (TREE_CODE (cond) == COND_EXPR
6440 || TREE_CODE (cond) == VEC_COND_EXPR)
6441 {
6442 test = TREE_OPERAND (cond, 0);
6443 true_value = TREE_OPERAND (cond, 1);
6444 false_value = TREE_OPERAND (cond, 2);
6445 /* If this operand throws an expression, then it does not make
6446 sense to try to perform a logical or arithmetic operation
6447 involving it. */
6448 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6449 lhs = true_value;
6450 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6451 rhs = false_value;
6452 }
6453 else if (!(TREE_CODE (type) != VECTOR_TYPE
6454 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6455 {
6456 tree testtype = TREE_TYPE (cond);
6457 test = cond;
6458 true_value = constant_boolean_node (true, testtype);
6459 false_value = constant_boolean_node (false, testtype);
6460 }
6461 else
6462 /* Detect the case of mixing vector and scalar types - bail out. */
6463 return NULL_TREE;
6464
6465 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6466 cond_code = VEC_COND_EXPR;
6467
6468 /* This transformation is only worthwhile if we don't have to wrap ARG
6469 in a SAVE_EXPR and the operation can be simplified without recursing
6470 on at least one of the branches once its pushed inside the COND_EXPR. */
6471 if (!TREE_CONSTANT (arg)
6472 && (TREE_SIDE_EFFECTS (arg)
6473 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6474 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6475 return NULL_TREE;
6476
6477 arg = fold_convert_loc (loc, arg_type, arg);
6478 if (lhs == 0)
6479 {
6480 true_value = fold_convert_loc (loc, cond_type, true_value);
6481 if (cond_first_p)
6482 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6483 else
6484 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6485 }
6486 if (rhs == 0)
6487 {
6488 false_value = fold_convert_loc (loc, cond_type, false_value);
6489 if (cond_first_p)
6490 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6491 else
6492 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6493 }
6494
6495 /* Check that we have simplified at least one of the branches. */
6496 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6497 return NULL_TREE;
6498
6499 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6500 }
6501
6502 \f
6503 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6504
6505 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6506 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6507 ADDEND is the same as X.
6508
6509 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6510 and finite. The problematic cases are when X is zero, and its mode
6511 has signed zeros. In the case of rounding towards -infinity,
6512 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6513 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6514
6515 bool
6516 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6517 {
6518 if (!real_zerop (addend))
6519 return false;
6520
6521 /* Don't allow the fold with -fsignaling-nans. */
6522 if (HONOR_SNANS (element_mode (type)))
6523 return false;
6524
6525 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6526 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6527 return true;
6528
6529 /* In a vector or complex, we would need to check the sign of all zeros. */
6530 if (TREE_CODE (addend) != REAL_CST)
6531 return false;
6532
6533 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6534 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6535 negate = !negate;
6536
6537 /* The mode has signed zeros, and we have to honor their sign.
6538 In this situation, there is only one case we can return true for.
6539 X - 0 is the same as X unless rounding towards -infinity is
6540 supported. */
6541 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6542 }
6543
6544 /* Subroutine of fold() that optimizes comparisons of a division by
6545 a nonzero integer constant against an integer constant, i.e.
6546 X/C1 op C2.
6547
6548 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6549 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6550 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6551
6552 The function returns the constant folded tree if a simplification
6553 can be made, and NULL_TREE otherwise. */
6554
6555 static tree
6556 fold_div_compare (location_t loc,
6557 enum tree_code code, tree type, tree arg0, tree arg1)
6558 {
6559 tree prod, tmp, hi, lo;
6560 tree arg00 = TREE_OPERAND (arg0, 0);
6561 tree arg01 = TREE_OPERAND (arg0, 1);
6562 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6563 bool neg_overflow = false;
6564 bool overflow;
6565
6566 /* We have to do this the hard way to detect unsigned overflow.
6567 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6568 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6569 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6570 neg_overflow = false;
6571
6572 if (sign == UNSIGNED)
6573 {
6574 tmp = int_const_binop (MINUS_EXPR, arg01,
6575 build_int_cst (TREE_TYPE (arg01), 1));
6576 lo = prod;
6577
6578 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6579 val = wi::add (prod, tmp, sign, &overflow);
6580 hi = force_fit_type (TREE_TYPE (arg00), val,
6581 -1, overflow | TREE_OVERFLOW (prod));
6582 }
6583 else if (tree_int_cst_sgn (arg01) >= 0)
6584 {
6585 tmp = int_const_binop (MINUS_EXPR, arg01,
6586 build_int_cst (TREE_TYPE (arg01), 1));
6587 switch (tree_int_cst_sgn (arg1))
6588 {
6589 case -1:
6590 neg_overflow = true;
6591 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6592 hi = prod;
6593 break;
6594
6595 case 0:
6596 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6597 hi = tmp;
6598 break;
6599
6600 case 1:
6601 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6602 lo = prod;
6603 break;
6604
6605 default:
6606 gcc_unreachable ();
6607 }
6608 }
6609 else
6610 {
6611 /* A negative divisor reverses the relational operators. */
6612 code = swap_tree_comparison (code);
6613
6614 tmp = int_const_binop (PLUS_EXPR, arg01,
6615 build_int_cst (TREE_TYPE (arg01), 1));
6616 switch (tree_int_cst_sgn (arg1))
6617 {
6618 case -1:
6619 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6620 lo = prod;
6621 break;
6622
6623 case 0:
6624 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6625 lo = tmp;
6626 break;
6627
6628 case 1:
6629 neg_overflow = true;
6630 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6631 hi = prod;
6632 break;
6633
6634 default:
6635 gcc_unreachable ();
6636 }
6637 }
6638
6639 switch (code)
6640 {
6641 case EQ_EXPR:
6642 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6643 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6644 if (TREE_OVERFLOW (hi))
6645 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6646 if (TREE_OVERFLOW (lo))
6647 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6648 return build_range_check (loc, type, arg00, 1, lo, hi);
6649
6650 case NE_EXPR:
6651 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6652 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6653 if (TREE_OVERFLOW (hi))
6654 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6655 if (TREE_OVERFLOW (lo))
6656 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6657 return build_range_check (loc, type, arg00, 0, lo, hi);
6658
6659 case LT_EXPR:
6660 if (TREE_OVERFLOW (lo))
6661 {
6662 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6663 return omit_one_operand_loc (loc, type, tmp, arg00);
6664 }
6665 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6666
6667 case LE_EXPR:
6668 if (TREE_OVERFLOW (hi))
6669 {
6670 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6671 return omit_one_operand_loc (loc, type, tmp, arg00);
6672 }
6673 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6674
6675 case GT_EXPR:
6676 if (TREE_OVERFLOW (hi))
6677 {
6678 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6679 return omit_one_operand_loc (loc, type, tmp, arg00);
6680 }
6681 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6682
6683 case GE_EXPR:
6684 if (TREE_OVERFLOW (lo))
6685 {
6686 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6687 return omit_one_operand_loc (loc, type, tmp, arg00);
6688 }
6689 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6690
6691 default:
6692 break;
6693 }
6694
6695 return NULL_TREE;
6696 }
6697
6698
6699 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6700 equality/inequality test, then return a simplified form of the test
6701 using a sign testing. Otherwise return NULL. TYPE is the desired
6702 result type. */
6703
6704 static tree
6705 fold_single_bit_test_into_sign_test (location_t loc,
6706 enum tree_code code, tree arg0, tree arg1,
6707 tree result_type)
6708 {
6709 /* If this is testing a single bit, we can optimize the test. */
6710 if ((code == NE_EXPR || code == EQ_EXPR)
6711 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6712 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6713 {
6714 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6715 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6716 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6717
6718 if (arg00 != NULL_TREE
6719 /* This is only a win if casting to a signed type is cheap,
6720 i.e. when arg00's type is not a partial mode. */
6721 && TYPE_PRECISION (TREE_TYPE (arg00))
6722 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6723 {
6724 tree stype = signed_type_for (TREE_TYPE (arg00));
6725 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6726 result_type,
6727 fold_convert_loc (loc, stype, arg00),
6728 build_int_cst (stype, 0));
6729 }
6730 }
6731
6732 return NULL_TREE;
6733 }
6734
6735 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6736 equality/inequality test, then return a simplified form of
6737 the test using shifts and logical operations. Otherwise return
6738 NULL. TYPE is the desired result type. */
6739
6740 tree
6741 fold_single_bit_test (location_t loc, enum tree_code code,
6742 tree arg0, tree arg1, tree result_type)
6743 {
6744 /* If this is testing a single bit, we can optimize the test. */
6745 if ((code == NE_EXPR || code == EQ_EXPR)
6746 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6747 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6748 {
6749 tree inner = TREE_OPERAND (arg0, 0);
6750 tree type = TREE_TYPE (arg0);
6751 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6752 machine_mode operand_mode = TYPE_MODE (type);
6753 int ops_unsigned;
6754 tree signed_type, unsigned_type, intermediate_type;
6755 tree tem, one;
6756
6757 /* First, see if we can fold the single bit test into a sign-bit
6758 test. */
6759 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6760 result_type);
6761 if (tem)
6762 return tem;
6763
6764 /* Otherwise we have (A & C) != 0 where C is a single bit,
6765 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6766 Similarly for (A & C) == 0. */
6767
6768 /* If INNER is a right shift of a constant and it plus BITNUM does
6769 not overflow, adjust BITNUM and INNER. */
6770 if (TREE_CODE (inner) == RSHIFT_EXPR
6771 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6772 && bitnum < TYPE_PRECISION (type)
6773 && wi::ltu_p (TREE_OPERAND (inner, 1),
6774 TYPE_PRECISION (type) - bitnum))
6775 {
6776 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6777 inner = TREE_OPERAND (inner, 0);
6778 }
6779
6780 /* If we are going to be able to omit the AND below, we must do our
6781 operations as unsigned. If we must use the AND, we have a choice.
6782 Normally unsigned is faster, but for some machines signed is. */
6783 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6784 && !flag_syntax_only) ? 0 : 1;
6785
6786 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6787 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6788 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6789 inner = fold_convert_loc (loc, intermediate_type, inner);
6790
6791 if (bitnum != 0)
6792 inner = build2 (RSHIFT_EXPR, intermediate_type,
6793 inner, size_int (bitnum));
6794
6795 one = build_int_cst (intermediate_type, 1);
6796
6797 if (code == EQ_EXPR)
6798 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6799
6800 /* Put the AND last so it can combine with more things. */
6801 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6802
6803 /* Make sure to return the proper type. */
6804 inner = fold_convert_loc (loc, result_type, inner);
6805
6806 return inner;
6807 }
6808 return NULL_TREE;
6809 }
6810
6811 /* Test whether it is preferable two swap two operands, ARG0 and
6812 ARG1, for example because ARG0 is an integer constant and ARG1
6813 isn't. */
6814
6815 bool
6816 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6817 {
6818 if (CONSTANT_CLASS_P (arg1))
6819 return 0;
6820 if (CONSTANT_CLASS_P (arg0))
6821 return 1;
6822
6823 STRIP_NOPS (arg0);
6824 STRIP_NOPS (arg1);
6825
6826 if (TREE_CONSTANT (arg1))
6827 return 0;
6828 if (TREE_CONSTANT (arg0))
6829 return 1;
6830
6831 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6832 for commutative and comparison operators. Ensuring a canonical
6833 form allows the optimizers to find additional redundancies without
6834 having to explicitly check for both orderings. */
6835 if (TREE_CODE (arg0) == SSA_NAME
6836 && TREE_CODE (arg1) == SSA_NAME
6837 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6838 return 1;
6839
6840 /* Put SSA_NAMEs last. */
6841 if (TREE_CODE (arg1) == SSA_NAME)
6842 return 0;
6843 if (TREE_CODE (arg0) == SSA_NAME)
6844 return 1;
6845
6846 /* Put variables last. */
6847 if (DECL_P (arg1))
6848 return 0;
6849 if (DECL_P (arg0))
6850 return 1;
6851
6852 return 0;
6853 }
6854
6855
6856 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6857 means A >= Y && A != MAX, but in this case we know that
6858 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6859
6860 static tree
6861 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6862 {
6863 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6864
6865 if (TREE_CODE (bound) == LT_EXPR)
6866 a = TREE_OPERAND (bound, 0);
6867 else if (TREE_CODE (bound) == GT_EXPR)
6868 a = TREE_OPERAND (bound, 1);
6869 else
6870 return NULL_TREE;
6871
6872 typea = TREE_TYPE (a);
6873 if (!INTEGRAL_TYPE_P (typea)
6874 && !POINTER_TYPE_P (typea))
6875 return NULL_TREE;
6876
6877 if (TREE_CODE (ineq) == LT_EXPR)
6878 {
6879 a1 = TREE_OPERAND (ineq, 1);
6880 y = TREE_OPERAND (ineq, 0);
6881 }
6882 else if (TREE_CODE (ineq) == GT_EXPR)
6883 {
6884 a1 = TREE_OPERAND (ineq, 0);
6885 y = TREE_OPERAND (ineq, 1);
6886 }
6887 else
6888 return NULL_TREE;
6889
6890 if (TREE_TYPE (a1) != typea)
6891 return NULL_TREE;
6892
6893 if (POINTER_TYPE_P (typea))
6894 {
6895 /* Convert the pointer types into integer before taking the difference. */
6896 tree ta = fold_convert_loc (loc, ssizetype, a);
6897 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6898 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6899 }
6900 else
6901 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6902
6903 if (!diff || !integer_onep (diff))
6904 return NULL_TREE;
6905
6906 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6907 }
6908
6909 /* Fold a sum or difference of at least one multiplication.
6910 Returns the folded tree or NULL if no simplification could be made. */
6911
6912 static tree
6913 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6914 tree arg0, tree arg1)
6915 {
6916 tree arg00, arg01, arg10, arg11;
6917 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6918
6919 /* (A * C) +- (B * C) -> (A+-B) * C.
6920 (A * C) +- A -> A * (C+-1).
6921 We are most concerned about the case where C is a constant,
6922 but other combinations show up during loop reduction. Since
6923 it is not difficult, try all four possibilities. */
6924
6925 if (TREE_CODE (arg0) == MULT_EXPR)
6926 {
6927 arg00 = TREE_OPERAND (arg0, 0);
6928 arg01 = TREE_OPERAND (arg0, 1);
6929 }
6930 else if (TREE_CODE (arg0) == INTEGER_CST)
6931 {
6932 arg00 = build_one_cst (type);
6933 arg01 = arg0;
6934 }
6935 else
6936 {
6937 /* We cannot generate constant 1 for fract. */
6938 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6939 return NULL_TREE;
6940 arg00 = arg0;
6941 arg01 = build_one_cst (type);
6942 }
6943 if (TREE_CODE (arg1) == MULT_EXPR)
6944 {
6945 arg10 = TREE_OPERAND (arg1, 0);
6946 arg11 = TREE_OPERAND (arg1, 1);
6947 }
6948 else if (TREE_CODE (arg1) == INTEGER_CST)
6949 {
6950 arg10 = build_one_cst (type);
6951 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6952 the purpose of this canonicalization. */
6953 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6954 && negate_expr_p (arg1)
6955 && code == PLUS_EXPR)
6956 {
6957 arg11 = negate_expr (arg1);
6958 code = MINUS_EXPR;
6959 }
6960 else
6961 arg11 = arg1;
6962 }
6963 else
6964 {
6965 /* We cannot generate constant 1 for fract. */
6966 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6967 return NULL_TREE;
6968 arg10 = arg1;
6969 arg11 = build_one_cst (type);
6970 }
6971 same = NULL_TREE;
6972
6973 /* Prefer factoring a common non-constant. */
6974 if (operand_equal_p (arg00, arg10, 0))
6975 same = arg00, alt0 = arg01, alt1 = arg11;
6976 else if (operand_equal_p (arg01, arg11, 0))
6977 same = arg01, alt0 = arg00, alt1 = arg10;
6978 else if (operand_equal_p (arg00, arg11, 0))
6979 same = arg00, alt0 = arg01, alt1 = arg10;
6980 else if (operand_equal_p (arg01, arg10, 0))
6981 same = arg01, alt0 = arg00, alt1 = arg11;
6982
6983 /* No identical multiplicands; see if we can find a common
6984 power-of-two factor in non-power-of-two multiplies. This
6985 can help in multi-dimensional array access. */
6986 else if (tree_fits_shwi_p (arg01)
6987 && tree_fits_shwi_p (arg11))
6988 {
6989 HOST_WIDE_INT int01, int11, tmp;
6990 bool swap = false;
6991 tree maybe_same;
6992 int01 = tree_to_shwi (arg01);
6993 int11 = tree_to_shwi (arg11);
6994
6995 /* Move min of absolute values to int11. */
6996 if (absu_hwi (int01) < absu_hwi (int11))
6997 {
6998 tmp = int01, int01 = int11, int11 = tmp;
6999 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7000 maybe_same = arg01;
7001 swap = true;
7002 }
7003 else
7004 maybe_same = arg11;
7005
7006 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7007 /* The remainder should not be a constant, otherwise we
7008 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7009 increased the number of multiplications necessary. */
7010 && TREE_CODE (arg10) != INTEGER_CST)
7011 {
7012 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7013 build_int_cst (TREE_TYPE (arg00),
7014 int01 / int11));
7015 alt1 = arg10;
7016 same = maybe_same;
7017 if (swap)
7018 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7019 }
7020 }
7021
7022 if (!same)
7023 return NULL_TREE;
7024
7025 if (! INTEGRAL_TYPE_P (type)
7026 || TYPE_OVERFLOW_WRAPS (type)
7027 /* We are neither factoring zero nor minus one. */
7028 || TREE_CODE (same) == INTEGER_CST)
7029 return fold_build2_loc (loc, MULT_EXPR, type,
7030 fold_build2_loc (loc, code, type,
7031 fold_convert_loc (loc, type, alt0),
7032 fold_convert_loc (loc, type, alt1)),
7033 fold_convert_loc (loc, type, same));
7034
7035 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7036 same may be minus one and thus the multiplication may overflow. Perform
7037 the operations in an unsigned type. */
7038 tree utype = unsigned_type_for (type);
7039 tree tem = fold_build2_loc (loc, code, utype,
7040 fold_convert_loc (loc, utype, alt0),
7041 fold_convert_loc (loc, utype, alt1));
7042 /* If the sum evaluated to a constant that is not -INF the multiplication
7043 cannot overflow. */
7044 if (TREE_CODE (tem) == INTEGER_CST
7045 && ! wi::eq_p (tem, wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7046 return fold_build2_loc (loc, MULT_EXPR, type,
7047 fold_convert (type, tem), same);
7048
7049 return fold_convert_loc (loc, type,
7050 fold_build2_loc (loc, MULT_EXPR, utype, tem,
7051 fold_convert_loc (loc, utype, same)));
7052 }
7053
7054 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7055 specified by EXPR into the buffer PTR of length LEN bytes.
7056 Return the number of bytes placed in the buffer, or zero
7057 upon failure. */
7058
7059 static int
7060 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7061 {
7062 tree type = TREE_TYPE (expr);
7063 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7064 int byte, offset, word, words;
7065 unsigned char value;
7066
7067 if ((off == -1 && total_bytes > len)
7068 || off >= total_bytes)
7069 return 0;
7070 if (off == -1)
7071 off = 0;
7072 words = total_bytes / UNITS_PER_WORD;
7073
7074 for (byte = 0; byte < total_bytes; byte++)
7075 {
7076 int bitpos = byte * BITS_PER_UNIT;
7077 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7078 number of bytes. */
7079 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7080
7081 if (total_bytes > UNITS_PER_WORD)
7082 {
7083 word = byte / UNITS_PER_WORD;
7084 if (WORDS_BIG_ENDIAN)
7085 word = (words - 1) - word;
7086 offset = word * UNITS_PER_WORD;
7087 if (BYTES_BIG_ENDIAN)
7088 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7089 else
7090 offset += byte % UNITS_PER_WORD;
7091 }
7092 else
7093 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7094 if (offset >= off
7095 && offset - off < len)
7096 ptr[offset - off] = value;
7097 }
7098 return MIN (len, total_bytes - off);
7099 }
7100
7101
7102 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7103 specified by EXPR into the buffer PTR of length LEN bytes.
7104 Return the number of bytes placed in the buffer, or zero
7105 upon failure. */
7106
7107 static int
7108 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7109 {
7110 tree type = TREE_TYPE (expr);
7111 machine_mode mode = TYPE_MODE (type);
7112 int total_bytes = GET_MODE_SIZE (mode);
7113 FIXED_VALUE_TYPE value;
7114 tree i_value, i_type;
7115
7116 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7117 return 0;
7118
7119 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7120
7121 if (NULL_TREE == i_type
7122 || TYPE_PRECISION (i_type) != total_bytes)
7123 return 0;
7124
7125 value = TREE_FIXED_CST (expr);
7126 i_value = double_int_to_tree (i_type, value.data);
7127
7128 return native_encode_int (i_value, ptr, len, off);
7129 }
7130
7131
7132 /* Subroutine of native_encode_expr. Encode the REAL_CST
7133 specified by EXPR into the buffer PTR of length LEN bytes.
7134 Return the number of bytes placed in the buffer, or zero
7135 upon failure. */
7136
7137 static int
7138 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7139 {
7140 tree type = TREE_TYPE (expr);
7141 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7142 int byte, offset, word, words, bitpos;
7143 unsigned char value;
7144
7145 /* There are always 32 bits in each long, no matter the size of
7146 the hosts long. We handle floating point representations with
7147 up to 192 bits. */
7148 long tmp[6];
7149
7150 if ((off == -1 && total_bytes > len)
7151 || off >= total_bytes)
7152 return 0;
7153 if (off == -1)
7154 off = 0;
7155 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7156
7157 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7158
7159 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7160 bitpos += BITS_PER_UNIT)
7161 {
7162 byte = (bitpos / BITS_PER_UNIT) & 3;
7163 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7164
7165 if (UNITS_PER_WORD < 4)
7166 {
7167 word = byte / UNITS_PER_WORD;
7168 if (WORDS_BIG_ENDIAN)
7169 word = (words - 1) - word;
7170 offset = word * UNITS_PER_WORD;
7171 if (BYTES_BIG_ENDIAN)
7172 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7173 else
7174 offset += byte % UNITS_PER_WORD;
7175 }
7176 else
7177 {
7178 offset = byte;
7179 if (BYTES_BIG_ENDIAN)
7180 {
7181 /* Reverse bytes within each long, or within the entire float
7182 if it's smaller than a long (for HFmode). */
7183 offset = MIN (3, total_bytes - 1) - offset;
7184 gcc_assert (offset >= 0);
7185 }
7186 }
7187 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7188 if (offset >= off
7189 && offset - off < len)
7190 ptr[offset - off] = value;
7191 }
7192 return MIN (len, total_bytes - off);
7193 }
7194
7195 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7196 specified by EXPR into the buffer PTR of length LEN bytes.
7197 Return the number of bytes placed in the buffer, or zero
7198 upon failure. */
7199
7200 static int
7201 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7202 {
7203 int rsize, isize;
7204 tree part;
7205
7206 part = TREE_REALPART (expr);
7207 rsize = native_encode_expr (part, ptr, len, off);
7208 if (off == -1
7209 && rsize == 0)
7210 return 0;
7211 part = TREE_IMAGPART (expr);
7212 if (off != -1)
7213 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7214 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7215 if (off == -1
7216 && isize != rsize)
7217 return 0;
7218 return rsize + isize;
7219 }
7220
7221
7222 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7223 specified by EXPR into the buffer PTR of length LEN bytes.
7224 Return the number of bytes placed in the buffer, or zero
7225 upon failure. */
7226
7227 static int
7228 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7229 {
7230 unsigned i, count;
7231 int size, offset;
7232 tree itype, elem;
7233
7234 offset = 0;
7235 count = VECTOR_CST_NELTS (expr);
7236 itype = TREE_TYPE (TREE_TYPE (expr));
7237 size = GET_MODE_SIZE (TYPE_MODE (itype));
7238 for (i = 0; i < count; i++)
7239 {
7240 if (off >= size)
7241 {
7242 off -= size;
7243 continue;
7244 }
7245 elem = VECTOR_CST_ELT (expr, i);
7246 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7247 if ((off == -1 && res != size)
7248 || res == 0)
7249 return 0;
7250 offset += res;
7251 if (offset >= len)
7252 return offset;
7253 if (off != -1)
7254 off = 0;
7255 }
7256 return offset;
7257 }
7258
7259
7260 /* Subroutine of native_encode_expr. Encode the STRING_CST
7261 specified by EXPR into the buffer PTR of length LEN bytes.
7262 Return the number of bytes placed in the buffer, or zero
7263 upon failure. */
7264
7265 static int
7266 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7267 {
7268 tree type = TREE_TYPE (expr);
7269 HOST_WIDE_INT total_bytes;
7270
7271 if (TREE_CODE (type) != ARRAY_TYPE
7272 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7273 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7274 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7275 return 0;
7276 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7277 if ((off == -1 && total_bytes > len)
7278 || off >= total_bytes)
7279 return 0;
7280 if (off == -1)
7281 off = 0;
7282 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7283 {
7284 int written = 0;
7285 if (off < TREE_STRING_LENGTH (expr))
7286 {
7287 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7288 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7289 }
7290 memset (ptr + written, 0,
7291 MIN (total_bytes - written, len - written));
7292 }
7293 else
7294 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7295 return MIN (total_bytes - off, len);
7296 }
7297
7298
7299 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7300 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7301 buffer PTR of length LEN bytes. If OFF is not -1 then start
7302 the encoding at byte offset OFF and encode at most LEN bytes.
7303 Return the number of bytes placed in the buffer, or zero upon failure. */
7304
7305 int
7306 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7307 {
7308 /* We don't support starting at negative offset and -1 is special. */
7309 if (off < -1)
7310 return 0;
7311
7312 switch (TREE_CODE (expr))
7313 {
7314 case INTEGER_CST:
7315 return native_encode_int (expr, ptr, len, off);
7316
7317 case REAL_CST:
7318 return native_encode_real (expr, ptr, len, off);
7319
7320 case FIXED_CST:
7321 return native_encode_fixed (expr, ptr, len, off);
7322
7323 case COMPLEX_CST:
7324 return native_encode_complex (expr, ptr, len, off);
7325
7326 case VECTOR_CST:
7327 return native_encode_vector (expr, ptr, len, off);
7328
7329 case STRING_CST:
7330 return native_encode_string (expr, ptr, len, off);
7331
7332 default:
7333 return 0;
7334 }
7335 }
7336
7337
7338 /* Subroutine of native_interpret_expr. Interpret the contents of
7339 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7340 If the buffer cannot be interpreted, return NULL_TREE. */
7341
7342 static tree
7343 native_interpret_int (tree type, const unsigned char *ptr, int len)
7344 {
7345 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7346
7347 if (total_bytes > len
7348 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7349 return NULL_TREE;
7350
7351 wide_int result = wi::from_buffer (ptr, total_bytes);
7352
7353 return wide_int_to_tree (type, result);
7354 }
7355
7356
7357 /* Subroutine of native_interpret_expr. Interpret the contents of
7358 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7359 If the buffer cannot be interpreted, return NULL_TREE. */
7360
7361 static tree
7362 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7363 {
7364 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7365 double_int result;
7366 FIXED_VALUE_TYPE fixed_value;
7367
7368 if (total_bytes > len
7369 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7370 return NULL_TREE;
7371
7372 result = double_int::from_buffer (ptr, total_bytes);
7373 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7374
7375 return build_fixed (type, fixed_value);
7376 }
7377
7378
7379 /* Subroutine of native_interpret_expr. Interpret the contents of
7380 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7381 If the buffer cannot be interpreted, return NULL_TREE. */
7382
7383 static tree
7384 native_interpret_real (tree type, const unsigned char *ptr, int len)
7385 {
7386 machine_mode mode = TYPE_MODE (type);
7387 int total_bytes = GET_MODE_SIZE (mode);
7388 unsigned char value;
7389 /* There are always 32 bits in each long, no matter the size of
7390 the hosts long. We handle floating point representations with
7391 up to 192 bits. */
7392 REAL_VALUE_TYPE r;
7393 long tmp[6];
7394
7395 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7396 if (total_bytes > len || total_bytes > 24)
7397 return NULL_TREE;
7398 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7399
7400 memset (tmp, 0, sizeof (tmp));
7401 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7402 bitpos += BITS_PER_UNIT)
7403 {
7404 /* Both OFFSET and BYTE index within a long;
7405 bitpos indexes the whole float. */
7406 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7407 if (UNITS_PER_WORD < 4)
7408 {
7409 int word = byte / UNITS_PER_WORD;
7410 if (WORDS_BIG_ENDIAN)
7411 word = (words - 1) - word;
7412 offset = word * UNITS_PER_WORD;
7413 if (BYTES_BIG_ENDIAN)
7414 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7415 else
7416 offset += byte % UNITS_PER_WORD;
7417 }
7418 else
7419 {
7420 offset = byte;
7421 if (BYTES_BIG_ENDIAN)
7422 {
7423 /* Reverse bytes within each long, or within the entire float
7424 if it's smaller than a long (for HFmode). */
7425 offset = MIN (3, total_bytes - 1) - offset;
7426 gcc_assert (offset >= 0);
7427 }
7428 }
7429 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7430
7431 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7432 }
7433
7434 real_from_target (&r, tmp, mode);
7435 return build_real (type, r);
7436 }
7437
7438
7439 /* Subroutine of native_interpret_expr. Interpret the contents of
7440 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7441 If the buffer cannot be interpreted, return NULL_TREE. */
7442
7443 static tree
7444 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7445 {
7446 tree etype, rpart, ipart;
7447 int size;
7448
7449 etype = TREE_TYPE (type);
7450 size = GET_MODE_SIZE (TYPE_MODE (etype));
7451 if (size * 2 > len)
7452 return NULL_TREE;
7453 rpart = native_interpret_expr (etype, ptr, size);
7454 if (!rpart)
7455 return NULL_TREE;
7456 ipart = native_interpret_expr (etype, ptr+size, size);
7457 if (!ipart)
7458 return NULL_TREE;
7459 return build_complex (type, rpart, ipart);
7460 }
7461
7462
7463 /* Subroutine of native_interpret_expr. Interpret the contents of
7464 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7465 If the buffer cannot be interpreted, return NULL_TREE. */
7466
7467 static tree
7468 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7469 {
7470 tree etype, elem;
7471 int i, size, count;
7472 tree *elements;
7473
7474 etype = TREE_TYPE (type);
7475 size = GET_MODE_SIZE (TYPE_MODE (etype));
7476 count = TYPE_VECTOR_SUBPARTS (type);
7477 if (size * count > len)
7478 return NULL_TREE;
7479
7480 elements = XALLOCAVEC (tree, count);
7481 for (i = count - 1; i >= 0; i--)
7482 {
7483 elem = native_interpret_expr (etype, ptr+(i*size), size);
7484 if (!elem)
7485 return NULL_TREE;
7486 elements[i] = elem;
7487 }
7488 return build_vector (type, elements);
7489 }
7490
7491
7492 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7493 the buffer PTR of length LEN as a constant of type TYPE. For
7494 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7495 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7496 return NULL_TREE. */
7497
7498 tree
7499 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7500 {
7501 switch (TREE_CODE (type))
7502 {
7503 case INTEGER_TYPE:
7504 case ENUMERAL_TYPE:
7505 case BOOLEAN_TYPE:
7506 case POINTER_TYPE:
7507 case REFERENCE_TYPE:
7508 return native_interpret_int (type, ptr, len);
7509
7510 case REAL_TYPE:
7511 return native_interpret_real (type, ptr, len);
7512
7513 case FIXED_POINT_TYPE:
7514 return native_interpret_fixed (type, ptr, len);
7515
7516 case COMPLEX_TYPE:
7517 return native_interpret_complex (type, ptr, len);
7518
7519 case VECTOR_TYPE:
7520 return native_interpret_vector (type, ptr, len);
7521
7522 default:
7523 return NULL_TREE;
7524 }
7525 }
7526
7527 /* Returns true if we can interpret the contents of a native encoding
7528 as TYPE. */
7529
7530 static bool
7531 can_native_interpret_type_p (tree type)
7532 {
7533 switch (TREE_CODE (type))
7534 {
7535 case INTEGER_TYPE:
7536 case ENUMERAL_TYPE:
7537 case BOOLEAN_TYPE:
7538 case POINTER_TYPE:
7539 case REFERENCE_TYPE:
7540 case FIXED_POINT_TYPE:
7541 case REAL_TYPE:
7542 case COMPLEX_TYPE:
7543 case VECTOR_TYPE:
7544 return true;
7545 default:
7546 return false;
7547 }
7548 }
7549
7550 /* Return true iff a constant of type TYPE is accepted by
7551 native_encode_expr. */
7552
7553 bool
7554 can_native_encode_type_p (tree type)
7555 {
7556 switch (TREE_CODE (type))
7557 {
7558 case INTEGER_TYPE:
7559 case REAL_TYPE:
7560 case FIXED_POINT_TYPE:
7561 case COMPLEX_TYPE:
7562 case VECTOR_TYPE:
7563 case POINTER_TYPE:
7564 return true;
7565 default:
7566 return false;
7567 }
7568 }
7569
7570 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7571 TYPE at compile-time. If we're unable to perform the conversion
7572 return NULL_TREE. */
7573
7574 static tree
7575 fold_view_convert_expr (tree type, tree expr)
7576 {
7577 /* We support up to 512-bit values (for V8DFmode). */
7578 unsigned char buffer[64];
7579 int len;
7580
7581 /* Check that the host and target are sane. */
7582 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7583 return NULL_TREE;
7584
7585 len = native_encode_expr (expr, buffer, sizeof (buffer));
7586 if (len == 0)
7587 return NULL_TREE;
7588
7589 return native_interpret_expr (type, buffer, len);
7590 }
7591
7592 /* Build an expression for the address of T. Folds away INDIRECT_REF
7593 to avoid confusing the gimplify process. */
7594
7595 tree
7596 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7597 {
7598 /* The size of the object is not relevant when talking about its address. */
7599 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7600 t = TREE_OPERAND (t, 0);
7601
7602 if (TREE_CODE (t) == INDIRECT_REF)
7603 {
7604 t = TREE_OPERAND (t, 0);
7605
7606 if (TREE_TYPE (t) != ptrtype)
7607 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7608 }
7609 else if (TREE_CODE (t) == MEM_REF
7610 && integer_zerop (TREE_OPERAND (t, 1)))
7611 return TREE_OPERAND (t, 0);
7612 else if (TREE_CODE (t) == MEM_REF
7613 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7614 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7615 TREE_OPERAND (t, 0),
7616 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7617 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7618 {
7619 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7620
7621 if (TREE_TYPE (t) != ptrtype)
7622 t = fold_convert_loc (loc, ptrtype, t);
7623 }
7624 else
7625 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7626
7627 return t;
7628 }
7629
7630 /* Build an expression for the address of T. */
7631
7632 tree
7633 build_fold_addr_expr_loc (location_t loc, tree t)
7634 {
7635 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7636
7637 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7638 }
7639
7640 /* Fold a unary expression of code CODE and type TYPE with operand
7641 OP0. Return the folded expression if folding is successful.
7642 Otherwise, return NULL_TREE. */
7643
7644 tree
7645 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7646 {
7647 tree tem;
7648 tree arg0;
7649 enum tree_code_class kind = TREE_CODE_CLASS (code);
7650
7651 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7652 && TREE_CODE_LENGTH (code) == 1);
7653
7654 arg0 = op0;
7655 if (arg0)
7656 {
7657 if (CONVERT_EXPR_CODE_P (code)
7658 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7659 {
7660 /* Don't use STRIP_NOPS, because signedness of argument type
7661 matters. */
7662 STRIP_SIGN_NOPS (arg0);
7663 }
7664 else
7665 {
7666 /* Strip any conversions that don't change the mode. This
7667 is safe for every expression, except for a comparison
7668 expression because its signedness is derived from its
7669 operands.
7670
7671 Note that this is done as an internal manipulation within
7672 the constant folder, in order to find the simplest
7673 representation of the arguments so that their form can be
7674 studied. In any cases, the appropriate type conversions
7675 should be put back in the tree that will get out of the
7676 constant folder. */
7677 STRIP_NOPS (arg0);
7678 }
7679
7680 if (CONSTANT_CLASS_P (arg0))
7681 {
7682 tree tem = const_unop (code, type, arg0);
7683 if (tem)
7684 {
7685 if (TREE_TYPE (tem) != type)
7686 tem = fold_convert_loc (loc, type, tem);
7687 return tem;
7688 }
7689 }
7690 }
7691
7692 tem = generic_simplify (loc, code, type, op0);
7693 if (tem)
7694 return tem;
7695
7696 if (TREE_CODE_CLASS (code) == tcc_unary)
7697 {
7698 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7699 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7700 fold_build1_loc (loc, code, type,
7701 fold_convert_loc (loc, TREE_TYPE (op0),
7702 TREE_OPERAND (arg0, 1))));
7703 else if (TREE_CODE (arg0) == COND_EXPR)
7704 {
7705 tree arg01 = TREE_OPERAND (arg0, 1);
7706 tree arg02 = TREE_OPERAND (arg0, 2);
7707 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7708 arg01 = fold_build1_loc (loc, code, type,
7709 fold_convert_loc (loc,
7710 TREE_TYPE (op0), arg01));
7711 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7712 arg02 = fold_build1_loc (loc, code, type,
7713 fold_convert_loc (loc,
7714 TREE_TYPE (op0), arg02));
7715 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7716 arg01, arg02);
7717
7718 /* If this was a conversion, and all we did was to move into
7719 inside the COND_EXPR, bring it back out. But leave it if
7720 it is a conversion from integer to integer and the
7721 result precision is no wider than a word since such a
7722 conversion is cheap and may be optimized away by combine,
7723 while it couldn't if it were outside the COND_EXPR. Then return
7724 so we don't get into an infinite recursion loop taking the
7725 conversion out and then back in. */
7726
7727 if ((CONVERT_EXPR_CODE_P (code)
7728 || code == NON_LVALUE_EXPR)
7729 && TREE_CODE (tem) == COND_EXPR
7730 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7731 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7732 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7733 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7734 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7735 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7736 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7737 && (INTEGRAL_TYPE_P
7738 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7739 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7740 || flag_syntax_only))
7741 tem = build1_loc (loc, code, type,
7742 build3 (COND_EXPR,
7743 TREE_TYPE (TREE_OPERAND
7744 (TREE_OPERAND (tem, 1), 0)),
7745 TREE_OPERAND (tem, 0),
7746 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7747 TREE_OPERAND (TREE_OPERAND (tem, 2),
7748 0)));
7749 return tem;
7750 }
7751 }
7752
7753 switch (code)
7754 {
7755 case NON_LVALUE_EXPR:
7756 if (!maybe_lvalue_p (op0))
7757 return fold_convert_loc (loc, type, op0);
7758 return NULL_TREE;
7759
7760 CASE_CONVERT:
7761 case FLOAT_EXPR:
7762 case FIX_TRUNC_EXPR:
7763 if (COMPARISON_CLASS_P (op0))
7764 {
7765 /* If we have (type) (a CMP b) and type is an integral type, return
7766 new expression involving the new type. Canonicalize
7767 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7768 non-integral type.
7769 Do not fold the result as that would not simplify further, also
7770 folding again results in recursions. */
7771 if (TREE_CODE (type) == BOOLEAN_TYPE)
7772 return build2_loc (loc, TREE_CODE (op0), type,
7773 TREE_OPERAND (op0, 0),
7774 TREE_OPERAND (op0, 1));
7775 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7776 && TREE_CODE (type) != VECTOR_TYPE)
7777 return build3_loc (loc, COND_EXPR, type, op0,
7778 constant_boolean_node (true, type),
7779 constant_boolean_node (false, type));
7780 }
7781
7782 /* Handle (T *)&A.B.C for A being of type T and B and C
7783 living at offset zero. This occurs frequently in
7784 C++ upcasting and then accessing the base. */
7785 if (TREE_CODE (op0) == ADDR_EXPR
7786 && POINTER_TYPE_P (type)
7787 && handled_component_p (TREE_OPERAND (op0, 0)))
7788 {
7789 HOST_WIDE_INT bitsize, bitpos;
7790 tree offset;
7791 machine_mode mode;
7792 int unsignedp, reversep, volatilep;
7793 tree base
7794 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7795 &offset, &mode, &unsignedp, &reversep,
7796 &volatilep);
7797 /* If the reference was to a (constant) zero offset, we can use
7798 the address of the base if it has the same base type
7799 as the result type and the pointer type is unqualified. */
7800 if (! offset && bitpos == 0
7801 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7802 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7803 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7804 return fold_convert_loc (loc, type,
7805 build_fold_addr_expr_loc (loc, base));
7806 }
7807
7808 if (TREE_CODE (op0) == MODIFY_EXPR
7809 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7810 /* Detect assigning a bitfield. */
7811 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7812 && DECL_BIT_FIELD
7813 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7814 {
7815 /* Don't leave an assignment inside a conversion
7816 unless assigning a bitfield. */
7817 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7818 /* First do the assignment, then return converted constant. */
7819 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7820 TREE_NO_WARNING (tem) = 1;
7821 TREE_USED (tem) = 1;
7822 return tem;
7823 }
7824
7825 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7826 constants (if x has signed type, the sign bit cannot be set
7827 in c). This folds extension into the BIT_AND_EXPR.
7828 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7829 very likely don't have maximal range for their precision and this
7830 transformation effectively doesn't preserve non-maximal ranges. */
7831 if (TREE_CODE (type) == INTEGER_TYPE
7832 && TREE_CODE (op0) == BIT_AND_EXPR
7833 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7834 {
7835 tree and_expr = op0;
7836 tree and0 = TREE_OPERAND (and_expr, 0);
7837 tree and1 = TREE_OPERAND (and_expr, 1);
7838 int change = 0;
7839
7840 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7841 || (TYPE_PRECISION (type)
7842 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7843 change = 1;
7844 else if (TYPE_PRECISION (TREE_TYPE (and1))
7845 <= HOST_BITS_PER_WIDE_INT
7846 && tree_fits_uhwi_p (and1))
7847 {
7848 unsigned HOST_WIDE_INT cst;
7849
7850 cst = tree_to_uhwi (and1);
7851 cst &= HOST_WIDE_INT_M1U
7852 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7853 change = (cst == 0);
7854 if (change
7855 && !flag_syntax_only
7856 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7857 == ZERO_EXTEND))
7858 {
7859 tree uns = unsigned_type_for (TREE_TYPE (and0));
7860 and0 = fold_convert_loc (loc, uns, and0);
7861 and1 = fold_convert_loc (loc, uns, and1);
7862 }
7863 }
7864 if (change)
7865 {
7866 tem = force_fit_type (type, wi::to_widest (and1), 0,
7867 TREE_OVERFLOW (and1));
7868 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7869 fold_convert_loc (loc, type, and0), tem);
7870 }
7871 }
7872
7873 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7874 cast (T1)X will fold away. We assume that this happens when X itself
7875 is a cast. */
7876 if (POINTER_TYPE_P (type)
7877 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7878 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7879 {
7880 tree arg00 = TREE_OPERAND (arg0, 0);
7881 tree arg01 = TREE_OPERAND (arg0, 1);
7882
7883 return fold_build_pointer_plus_loc
7884 (loc, fold_convert_loc (loc, type, arg00), arg01);
7885 }
7886
7887 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7888 of the same precision, and X is an integer type not narrower than
7889 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7890 if (INTEGRAL_TYPE_P (type)
7891 && TREE_CODE (op0) == BIT_NOT_EXPR
7892 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7893 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7894 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7895 {
7896 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7897 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7898 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7899 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7900 fold_convert_loc (loc, type, tem));
7901 }
7902
7903 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7904 type of X and Y (integer types only). */
7905 if (INTEGRAL_TYPE_P (type)
7906 && TREE_CODE (op0) == MULT_EXPR
7907 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7908 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7909 {
7910 /* Be careful not to introduce new overflows. */
7911 tree mult_type;
7912 if (TYPE_OVERFLOW_WRAPS (type))
7913 mult_type = type;
7914 else
7915 mult_type = unsigned_type_for (type);
7916
7917 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7918 {
7919 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7920 fold_convert_loc (loc, mult_type,
7921 TREE_OPERAND (op0, 0)),
7922 fold_convert_loc (loc, mult_type,
7923 TREE_OPERAND (op0, 1)));
7924 return fold_convert_loc (loc, type, tem);
7925 }
7926 }
7927
7928 return NULL_TREE;
7929
7930 case VIEW_CONVERT_EXPR:
7931 if (TREE_CODE (op0) == MEM_REF)
7932 {
7933 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7934 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7935 tem = fold_build2_loc (loc, MEM_REF, type,
7936 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7937 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7938 return tem;
7939 }
7940
7941 return NULL_TREE;
7942
7943 case NEGATE_EXPR:
7944 tem = fold_negate_expr (loc, arg0);
7945 if (tem)
7946 return fold_convert_loc (loc, type, tem);
7947 return NULL_TREE;
7948
7949 case ABS_EXPR:
7950 /* Convert fabs((double)float) into (double)fabsf(float). */
7951 if (TREE_CODE (arg0) == NOP_EXPR
7952 && TREE_CODE (type) == REAL_TYPE)
7953 {
7954 tree targ0 = strip_float_extensions (arg0);
7955 if (targ0 != arg0)
7956 return fold_convert_loc (loc, type,
7957 fold_build1_loc (loc, ABS_EXPR,
7958 TREE_TYPE (targ0),
7959 targ0));
7960 }
7961 return NULL_TREE;
7962
7963 case BIT_NOT_EXPR:
7964 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7965 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7966 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7967 fold_convert_loc (loc, type,
7968 TREE_OPERAND (arg0, 0)))))
7969 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7970 fold_convert_loc (loc, type,
7971 TREE_OPERAND (arg0, 1)));
7972 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7973 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7974 fold_convert_loc (loc, type,
7975 TREE_OPERAND (arg0, 1)))))
7976 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7977 fold_convert_loc (loc, type,
7978 TREE_OPERAND (arg0, 0)), tem);
7979
7980 return NULL_TREE;
7981
7982 case TRUTH_NOT_EXPR:
7983 /* Note that the operand of this must be an int
7984 and its values must be 0 or 1.
7985 ("true" is a fixed value perhaps depending on the language,
7986 but we don't handle values other than 1 correctly yet.) */
7987 tem = fold_truth_not_expr (loc, arg0);
7988 if (!tem)
7989 return NULL_TREE;
7990 return fold_convert_loc (loc, type, tem);
7991
7992 case INDIRECT_REF:
7993 /* Fold *&X to X if X is an lvalue. */
7994 if (TREE_CODE (op0) == ADDR_EXPR)
7995 {
7996 tree op00 = TREE_OPERAND (op0, 0);
7997 if ((VAR_P (op00)
7998 || TREE_CODE (op00) == PARM_DECL
7999 || TREE_CODE (op00) == RESULT_DECL)
8000 && !TREE_READONLY (op00))
8001 return op00;
8002 }
8003 return NULL_TREE;
8004
8005 default:
8006 return NULL_TREE;
8007 } /* switch (code) */
8008 }
8009
8010
8011 /* If the operation was a conversion do _not_ mark a resulting constant
8012 with TREE_OVERFLOW if the original constant was not. These conversions
8013 have implementation defined behavior and retaining the TREE_OVERFLOW
8014 flag here would confuse later passes such as VRP. */
8015 tree
8016 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8017 tree type, tree op0)
8018 {
8019 tree res = fold_unary_loc (loc, code, type, op0);
8020 if (res
8021 && TREE_CODE (res) == INTEGER_CST
8022 && TREE_CODE (op0) == INTEGER_CST
8023 && CONVERT_EXPR_CODE_P (code))
8024 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8025
8026 return res;
8027 }
8028
8029 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8030 operands OP0 and OP1. LOC is the location of the resulting expression.
8031 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8032 Return the folded expression if folding is successful. Otherwise,
8033 return NULL_TREE. */
8034 static tree
8035 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8036 tree arg0, tree arg1, tree op0, tree op1)
8037 {
8038 tree tem;
8039
8040 /* We only do these simplifications if we are optimizing. */
8041 if (!optimize)
8042 return NULL_TREE;
8043
8044 /* Check for things like (A || B) && (A || C). We can convert this
8045 to A || (B && C). Note that either operator can be any of the four
8046 truth and/or operations and the transformation will still be
8047 valid. Also note that we only care about order for the
8048 ANDIF and ORIF operators. If B contains side effects, this
8049 might change the truth-value of A. */
8050 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8051 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8052 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8053 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8054 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8055 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8056 {
8057 tree a00 = TREE_OPERAND (arg0, 0);
8058 tree a01 = TREE_OPERAND (arg0, 1);
8059 tree a10 = TREE_OPERAND (arg1, 0);
8060 tree a11 = TREE_OPERAND (arg1, 1);
8061 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8062 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8063 && (code == TRUTH_AND_EXPR
8064 || code == TRUTH_OR_EXPR));
8065
8066 if (operand_equal_p (a00, a10, 0))
8067 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8068 fold_build2_loc (loc, code, type, a01, a11));
8069 else if (commutative && operand_equal_p (a00, a11, 0))
8070 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8071 fold_build2_loc (loc, code, type, a01, a10));
8072 else if (commutative && operand_equal_p (a01, a10, 0))
8073 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8074 fold_build2_loc (loc, code, type, a00, a11));
8075
8076 /* This case if tricky because we must either have commutative
8077 operators or else A10 must not have side-effects. */
8078
8079 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8080 && operand_equal_p (a01, a11, 0))
8081 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8082 fold_build2_loc (loc, code, type, a00, a10),
8083 a01);
8084 }
8085
8086 /* See if we can build a range comparison. */
8087 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8088 return tem;
8089
8090 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8091 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8092 {
8093 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8094 if (tem)
8095 return fold_build2_loc (loc, code, type, tem, arg1);
8096 }
8097
8098 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8099 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8100 {
8101 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8102 if (tem)
8103 return fold_build2_loc (loc, code, type, arg0, tem);
8104 }
8105
8106 /* Check for the possibility of merging component references. If our
8107 lhs is another similar operation, try to merge its rhs with our
8108 rhs. Then try to merge our lhs and rhs. */
8109 if (TREE_CODE (arg0) == code
8110 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8111 TREE_OPERAND (arg0, 1), arg1)))
8112 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8113
8114 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8115 return tem;
8116
8117 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8118 && (code == TRUTH_AND_EXPR
8119 || code == TRUTH_ANDIF_EXPR
8120 || code == TRUTH_OR_EXPR
8121 || code == TRUTH_ORIF_EXPR))
8122 {
8123 enum tree_code ncode, icode;
8124
8125 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8126 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8127 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8128
8129 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8130 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8131 We don't want to pack more than two leafs to a non-IF AND/OR
8132 expression.
8133 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8134 equal to IF-CODE, then we don't want to add right-hand operand.
8135 If the inner right-hand side of left-hand operand has
8136 side-effects, or isn't simple, then we can't add to it,
8137 as otherwise we might destroy if-sequence. */
8138 if (TREE_CODE (arg0) == icode
8139 && simple_operand_p_2 (arg1)
8140 /* Needed for sequence points to handle trappings, and
8141 side-effects. */
8142 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8143 {
8144 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8145 arg1);
8146 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8147 tem);
8148 }
8149 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8150 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8151 else if (TREE_CODE (arg1) == icode
8152 && simple_operand_p_2 (arg0)
8153 /* Needed for sequence points to handle trappings, and
8154 side-effects. */
8155 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8156 {
8157 tem = fold_build2_loc (loc, ncode, type,
8158 arg0, TREE_OPERAND (arg1, 0));
8159 return fold_build2_loc (loc, icode, type, tem,
8160 TREE_OPERAND (arg1, 1));
8161 }
8162 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8163 into (A OR B).
8164 For sequence point consistancy, we need to check for trapping,
8165 and side-effects. */
8166 else if (code == icode && simple_operand_p_2 (arg0)
8167 && simple_operand_p_2 (arg1))
8168 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8169 }
8170
8171 return NULL_TREE;
8172 }
8173
8174 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8175 by changing CODE to reduce the magnitude of constants involved in
8176 ARG0 of the comparison.
8177 Returns a canonicalized comparison tree if a simplification was
8178 possible, otherwise returns NULL_TREE.
8179 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8180 valid if signed overflow is undefined. */
8181
8182 static tree
8183 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8184 tree arg0, tree arg1,
8185 bool *strict_overflow_p)
8186 {
8187 enum tree_code code0 = TREE_CODE (arg0);
8188 tree t, cst0 = NULL_TREE;
8189 int sgn0;
8190
8191 /* Match A +- CST code arg1. We can change this only if overflow
8192 is undefined. */
8193 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8194 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8195 /* In principle pointers also have undefined overflow behavior,
8196 but that causes problems elsewhere. */
8197 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8198 && (code0 == MINUS_EXPR
8199 || code0 == PLUS_EXPR)
8200 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8201 return NULL_TREE;
8202
8203 /* Identify the constant in arg0 and its sign. */
8204 cst0 = TREE_OPERAND (arg0, 1);
8205 sgn0 = tree_int_cst_sgn (cst0);
8206
8207 /* Overflowed constants and zero will cause problems. */
8208 if (integer_zerop (cst0)
8209 || TREE_OVERFLOW (cst0))
8210 return NULL_TREE;
8211
8212 /* See if we can reduce the magnitude of the constant in
8213 arg0 by changing the comparison code. */
8214 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8215 if (code == LT_EXPR
8216 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8217 code = LE_EXPR;
8218 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8219 else if (code == GT_EXPR
8220 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8221 code = GE_EXPR;
8222 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8223 else if (code == LE_EXPR
8224 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8225 code = LT_EXPR;
8226 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8227 else if (code == GE_EXPR
8228 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8229 code = GT_EXPR;
8230 else
8231 return NULL_TREE;
8232 *strict_overflow_p = true;
8233
8234 /* Now build the constant reduced in magnitude. But not if that
8235 would produce one outside of its types range. */
8236 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8237 && ((sgn0 == 1
8238 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8239 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8240 || (sgn0 == -1
8241 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8242 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8243 return NULL_TREE;
8244
8245 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8246 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8247 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8248 t = fold_convert (TREE_TYPE (arg1), t);
8249
8250 return fold_build2_loc (loc, code, type, t, arg1);
8251 }
8252
8253 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8254 overflow further. Try to decrease the magnitude of constants involved
8255 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8256 and put sole constants at the second argument position.
8257 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8258
8259 static tree
8260 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8261 tree arg0, tree arg1)
8262 {
8263 tree t;
8264 bool strict_overflow_p;
8265 const char * const warnmsg = G_("assuming signed overflow does not occur "
8266 "when reducing constant in comparison");
8267
8268 /* Try canonicalization by simplifying arg0. */
8269 strict_overflow_p = false;
8270 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8271 &strict_overflow_p);
8272 if (t)
8273 {
8274 if (strict_overflow_p)
8275 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8276 return t;
8277 }
8278
8279 /* Try canonicalization by simplifying arg1 using the swapped
8280 comparison. */
8281 code = swap_tree_comparison (code);
8282 strict_overflow_p = false;
8283 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8284 &strict_overflow_p);
8285 if (t && strict_overflow_p)
8286 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8287 return t;
8288 }
8289
8290 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8291 space. This is used to avoid issuing overflow warnings for
8292 expressions like &p->x which can not wrap. */
8293
8294 static bool
8295 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8296 {
8297 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8298 return true;
8299
8300 if (bitpos < 0)
8301 return true;
8302
8303 wide_int wi_offset;
8304 int precision = TYPE_PRECISION (TREE_TYPE (base));
8305 if (offset == NULL_TREE)
8306 wi_offset = wi::zero (precision);
8307 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8308 return true;
8309 else
8310 wi_offset = offset;
8311
8312 bool overflow;
8313 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8314 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8315 if (overflow)
8316 return true;
8317
8318 if (!wi::fits_uhwi_p (total))
8319 return true;
8320
8321 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8322 if (size <= 0)
8323 return true;
8324
8325 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8326 array. */
8327 if (TREE_CODE (base) == ADDR_EXPR)
8328 {
8329 HOST_WIDE_INT base_size;
8330
8331 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8332 if (base_size > 0 && size < base_size)
8333 size = base_size;
8334 }
8335
8336 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8337 }
8338
8339 /* Return a positive integer when the symbol DECL is known to have
8340 a nonzero address, zero when it's known not to (e.g., it's a weak
8341 symbol), and a negative integer when the symbol is not yet in the
8342 symbol table and so whether or not its address is zero is unknown.
8343 For function local objects always return positive integer. */
8344 static int
8345 maybe_nonzero_address (tree decl)
8346 {
8347 if (DECL_P (decl) && decl_in_symtab_p (decl))
8348 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8349 return symbol->nonzero_address ();
8350
8351 /* Function local objects are never NULL. */
8352 if (DECL_P (decl)
8353 && (DECL_CONTEXT (decl)
8354 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8355 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8356 return 1;
8357
8358 return -1;
8359 }
8360
8361 /* Subroutine of fold_binary. This routine performs all of the
8362 transformations that are common to the equality/inequality
8363 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8364 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8365 fold_binary should call fold_binary. Fold a comparison with
8366 tree code CODE and type TYPE with operands OP0 and OP1. Return
8367 the folded comparison or NULL_TREE. */
8368
8369 static tree
8370 fold_comparison (location_t loc, enum tree_code code, tree type,
8371 tree op0, tree op1)
8372 {
8373 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8374 tree arg0, arg1, tem;
8375
8376 arg0 = op0;
8377 arg1 = op1;
8378
8379 STRIP_SIGN_NOPS (arg0);
8380 STRIP_SIGN_NOPS (arg1);
8381
8382 /* For comparisons of pointers we can decompose it to a compile time
8383 comparison of the base objects and the offsets into the object.
8384 This requires at least one operand being an ADDR_EXPR or a
8385 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8386 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8387 && (TREE_CODE (arg0) == ADDR_EXPR
8388 || TREE_CODE (arg1) == ADDR_EXPR
8389 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8390 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8391 {
8392 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8393 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8394 machine_mode mode;
8395 int volatilep, reversep, unsignedp;
8396 bool indirect_base0 = false, indirect_base1 = false;
8397
8398 /* Get base and offset for the access. Strip ADDR_EXPR for
8399 get_inner_reference, but put it back by stripping INDIRECT_REF
8400 off the base object if possible. indirect_baseN will be true
8401 if baseN is not an address but refers to the object itself. */
8402 base0 = arg0;
8403 if (TREE_CODE (arg0) == ADDR_EXPR)
8404 {
8405 base0
8406 = get_inner_reference (TREE_OPERAND (arg0, 0),
8407 &bitsize, &bitpos0, &offset0, &mode,
8408 &unsignedp, &reversep, &volatilep);
8409 if (TREE_CODE (base0) == INDIRECT_REF)
8410 base0 = TREE_OPERAND (base0, 0);
8411 else
8412 indirect_base0 = true;
8413 }
8414 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8415 {
8416 base0 = TREE_OPERAND (arg0, 0);
8417 STRIP_SIGN_NOPS (base0);
8418 if (TREE_CODE (base0) == ADDR_EXPR)
8419 {
8420 base0
8421 = get_inner_reference (TREE_OPERAND (base0, 0),
8422 &bitsize, &bitpos0, &offset0, &mode,
8423 &unsignedp, &reversep, &volatilep);
8424 if (TREE_CODE (base0) == INDIRECT_REF)
8425 base0 = TREE_OPERAND (base0, 0);
8426 else
8427 indirect_base0 = true;
8428 }
8429 if (offset0 == NULL_TREE || integer_zerop (offset0))
8430 offset0 = TREE_OPERAND (arg0, 1);
8431 else
8432 offset0 = size_binop (PLUS_EXPR, offset0,
8433 TREE_OPERAND (arg0, 1));
8434 if (TREE_CODE (offset0) == INTEGER_CST)
8435 {
8436 offset_int tem = wi::sext (wi::to_offset (offset0),
8437 TYPE_PRECISION (sizetype));
8438 tem <<= LOG2_BITS_PER_UNIT;
8439 tem += bitpos0;
8440 if (wi::fits_shwi_p (tem))
8441 {
8442 bitpos0 = tem.to_shwi ();
8443 offset0 = NULL_TREE;
8444 }
8445 }
8446 }
8447
8448 base1 = arg1;
8449 if (TREE_CODE (arg1) == ADDR_EXPR)
8450 {
8451 base1
8452 = get_inner_reference (TREE_OPERAND (arg1, 0),
8453 &bitsize, &bitpos1, &offset1, &mode,
8454 &unsignedp, &reversep, &volatilep);
8455 if (TREE_CODE (base1) == INDIRECT_REF)
8456 base1 = TREE_OPERAND (base1, 0);
8457 else
8458 indirect_base1 = true;
8459 }
8460 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8461 {
8462 base1 = TREE_OPERAND (arg1, 0);
8463 STRIP_SIGN_NOPS (base1);
8464 if (TREE_CODE (base1) == ADDR_EXPR)
8465 {
8466 base1
8467 = get_inner_reference (TREE_OPERAND (base1, 0),
8468 &bitsize, &bitpos1, &offset1, &mode,
8469 &unsignedp, &reversep, &volatilep);
8470 if (TREE_CODE (base1) == INDIRECT_REF)
8471 base1 = TREE_OPERAND (base1, 0);
8472 else
8473 indirect_base1 = true;
8474 }
8475 if (offset1 == NULL_TREE || integer_zerop (offset1))
8476 offset1 = TREE_OPERAND (arg1, 1);
8477 else
8478 offset1 = size_binop (PLUS_EXPR, offset1,
8479 TREE_OPERAND (arg1, 1));
8480 if (TREE_CODE (offset1) == INTEGER_CST)
8481 {
8482 offset_int tem = wi::sext (wi::to_offset (offset1),
8483 TYPE_PRECISION (sizetype));
8484 tem <<= LOG2_BITS_PER_UNIT;
8485 tem += bitpos1;
8486 if (wi::fits_shwi_p (tem))
8487 {
8488 bitpos1 = tem.to_shwi ();
8489 offset1 = NULL_TREE;
8490 }
8491 }
8492 }
8493
8494 /* If we have equivalent bases we might be able to simplify. */
8495 if (indirect_base0 == indirect_base1
8496 && operand_equal_p (base0, base1,
8497 indirect_base0 ? OEP_ADDRESS_OF : 0))
8498 {
8499 /* We can fold this expression to a constant if the non-constant
8500 offset parts are equal. */
8501 if ((offset0 == offset1
8502 || (offset0 && offset1
8503 && operand_equal_p (offset0, offset1, 0)))
8504 && (equality_code
8505 || (indirect_base0
8506 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8507 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8508
8509 {
8510 if (!equality_code
8511 && bitpos0 != bitpos1
8512 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8513 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8514 fold_overflow_warning (("assuming pointer wraparound does not "
8515 "occur when comparing P +- C1 with "
8516 "P +- C2"),
8517 WARN_STRICT_OVERFLOW_CONDITIONAL);
8518
8519 switch (code)
8520 {
8521 case EQ_EXPR:
8522 return constant_boolean_node (bitpos0 == bitpos1, type);
8523 case NE_EXPR:
8524 return constant_boolean_node (bitpos0 != bitpos1, type);
8525 case LT_EXPR:
8526 return constant_boolean_node (bitpos0 < bitpos1, type);
8527 case LE_EXPR:
8528 return constant_boolean_node (bitpos0 <= bitpos1, type);
8529 case GE_EXPR:
8530 return constant_boolean_node (bitpos0 >= bitpos1, type);
8531 case GT_EXPR:
8532 return constant_boolean_node (bitpos0 > bitpos1, type);
8533 default:;
8534 }
8535 }
8536 /* We can simplify the comparison to a comparison of the variable
8537 offset parts if the constant offset parts are equal.
8538 Be careful to use signed sizetype here because otherwise we
8539 mess with array offsets in the wrong way. This is possible
8540 because pointer arithmetic is restricted to retain within an
8541 object and overflow on pointer differences is undefined as of
8542 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8543 else if (bitpos0 == bitpos1
8544 && (equality_code
8545 || (indirect_base0
8546 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8547 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8548 {
8549 /* By converting to signed sizetype we cover middle-end pointer
8550 arithmetic which operates on unsigned pointer types of size
8551 type size and ARRAY_REF offsets which are properly sign or
8552 zero extended from their type in case it is narrower than
8553 sizetype. */
8554 if (offset0 == NULL_TREE)
8555 offset0 = build_int_cst (ssizetype, 0);
8556 else
8557 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8558 if (offset1 == NULL_TREE)
8559 offset1 = build_int_cst (ssizetype, 0);
8560 else
8561 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8562
8563 if (!equality_code
8564 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8565 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8566 fold_overflow_warning (("assuming pointer wraparound does not "
8567 "occur when comparing P +- C1 with "
8568 "P +- C2"),
8569 WARN_STRICT_OVERFLOW_COMPARISON);
8570
8571 return fold_build2_loc (loc, code, type, offset0, offset1);
8572 }
8573 }
8574 /* For equal offsets we can simplify to a comparison of the
8575 base addresses. */
8576 else if (bitpos0 == bitpos1
8577 && (indirect_base0
8578 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8579 && (indirect_base1
8580 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8581 && ((offset0 == offset1)
8582 || (offset0 && offset1
8583 && operand_equal_p (offset0, offset1, 0))))
8584 {
8585 if (indirect_base0)
8586 base0 = build_fold_addr_expr_loc (loc, base0);
8587 if (indirect_base1)
8588 base1 = build_fold_addr_expr_loc (loc, base1);
8589 return fold_build2_loc (loc, code, type, base0, base1);
8590 }
8591 /* Comparison between an ordinary (non-weak) symbol and a null
8592 pointer can be eliminated since such symbols must have a non
8593 null address. In C, relational expressions between pointers
8594 to objects and null pointers are undefined. The results
8595 below follow the C++ rules with the additional property that
8596 every object pointer compares greater than a null pointer.
8597 */
8598 else if (((DECL_P (base0)
8599 && maybe_nonzero_address (base0) > 0
8600 /* Avoid folding references to struct members at offset 0 to
8601 prevent tests like '&ptr->firstmember == 0' from getting
8602 eliminated. When ptr is null, although the -> expression
8603 is strictly speaking invalid, GCC retains it as a matter
8604 of QoI. See PR c/44555. */
8605 && (offset0 == NULL_TREE && bitpos0 != 0))
8606 || CONSTANT_CLASS_P (base0))
8607 && indirect_base0
8608 /* The caller guarantees that when one of the arguments is
8609 constant (i.e., null in this case) it is second. */
8610 && integer_zerop (arg1))
8611 {
8612 switch (code)
8613 {
8614 case EQ_EXPR:
8615 case LE_EXPR:
8616 case LT_EXPR:
8617 return constant_boolean_node (false, type);
8618 case GE_EXPR:
8619 case GT_EXPR:
8620 case NE_EXPR:
8621 return constant_boolean_node (true, type);
8622 default:
8623 gcc_unreachable ();
8624 }
8625 }
8626 }
8627
8628 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8629 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8630 the resulting offset is smaller in absolute value than the
8631 original one and has the same sign. */
8632 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8634 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8635 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8636 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8637 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8638 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8639 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8640 {
8641 tree const1 = TREE_OPERAND (arg0, 1);
8642 tree const2 = TREE_OPERAND (arg1, 1);
8643 tree variable1 = TREE_OPERAND (arg0, 0);
8644 tree variable2 = TREE_OPERAND (arg1, 0);
8645 tree cst;
8646 const char * const warnmsg = G_("assuming signed overflow does not "
8647 "occur when combining constants around "
8648 "a comparison");
8649
8650 /* Put the constant on the side where it doesn't overflow and is
8651 of lower absolute value and of same sign than before. */
8652 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8653 ? MINUS_EXPR : PLUS_EXPR,
8654 const2, const1);
8655 if (!TREE_OVERFLOW (cst)
8656 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8657 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8658 {
8659 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8660 return fold_build2_loc (loc, code, type,
8661 variable1,
8662 fold_build2_loc (loc, TREE_CODE (arg1),
8663 TREE_TYPE (arg1),
8664 variable2, cst));
8665 }
8666
8667 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8668 ? MINUS_EXPR : PLUS_EXPR,
8669 const1, const2);
8670 if (!TREE_OVERFLOW (cst)
8671 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8672 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8673 {
8674 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8675 return fold_build2_loc (loc, code, type,
8676 fold_build2_loc (loc, TREE_CODE (arg0),
8677 TREE_TYPE (arg0),
8678 variable1, cst),
8679 variable2);
8680 }
8681 }
8682
8683 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8684 if (tem)
8685 return tem;
8686
8687 /* If we are comparing an expression that just has comparisons
8688 of two integer values, arithmetic expressions of those comparisons,
8689 and constants, we can simplify it. There are only three cases
8690 to check: the two values can either be equal, the first can be
8691 greater, or the second can be greater. Fold the expression for
8692 those three values. Since each value must be 0 or 1, we have
8693 eight possibilities, each of which corresponds to the constant 0
8694 or 1 or one of the six possible comparisons.
8695
8696 This handles common cases like (a > b) == 0 but also handles
8697 expressions like ((x > y) - (y > x)) > 0, which supposedly
8698 occur in macroized code. */
8699
8700 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8701 {
8702 tree cval1 = 0, cval2 = 0;
8703 int save_p = 0;
8704
8705 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8706 /* Don't handle degenerate cases here; they should already
8707 have been handled anyway. */
8708 && cval1 != 0 && cval2 != 0
8709 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8710 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8711 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8712 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8713 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8714 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8715 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8716 {
8717 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8718 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8719
8720 /* We can't just pass T to eval_subst in case cval1 or cval2
8721 was the same as ARG1. */
8722
8723 tree high_result
8724 = fold_build2_loc (loc, code, type,
8725 eval_subst (loc, arg0, cval1, maxval,
8726 cval2, minval),
8727 arg1);
8728 tree equal_result
8729 = fold_build2_loc (loc, code, type,
8730 eval_subst (loc, arg0, cval1, maxval,
8731 cval2, maxval),
8732 arg1);
8733 tree low_result
8734 = fold_build2_loc (loc, code, type,
8735 eval_subst (loc, arg0, cval1, minval,
8736 cval2, maxval),
8737 arg1);
8738
8739 /* All three of these results should be 0 or 1. Confirm they are.
8740 Then use those values to select the proper code to use. */
8741
8742 if (TREE_CODE (high_result) == INTEGER_CST
8743 && TREE_CODE (equal_result) == INTEGER_CST
8744 && TREE_CODE (low_result) == INTEGER_CST)
8745 {
8746 /* Make a 3-bit mask with the high-order bit being the
8747 value for `>', the next for '=', and the low for '<'. */
8748 switch ((integer_onep (high_result) * 4)
8749 + (integer_onep (equal_result) * 2)
8750 + integer_onep (low_result))
8751 {
8752 case 0:
8753 /* Always false. */
8754 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8755 case 1:
8756 code = LT_EXPR;
8757 break;
8758 case 2:
8759 code = EQ_EXPR;
8760 break;
8761 case 3:
8762 code = LE_EXPR;
8763 break;
8764 case 4:
8765 code = GT_EXPR;
8766 break;
8767 case 5:
8768 code = NE_EXPR;
8769 break;
8770 case 6:
8771 code = GE_EXPR;
8772 break;
8773 case 7:
8774 /* Always true. */
8775 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8776 }
8777
8778 if (save_p)
8779 {
8780 tem = save_expr (build2 (code, type, cval1, cval2));
8781 protected_set_expr_location (tem, loc);
8782 return tem;
8783 }
8784 return fold_build2_loc (loc, code, type, cval1, cval2);
8785 }
8786 }
8787 }
8788
8789 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8790 into a single range test. */
8791 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8792 && TREE_CODE (arg1) == INTEGER_CST
8793 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8794 && !integer_zerop (TREE_OPERAND (arg0, 1))
8795 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8796 && !TREE_OVERFLOW (arg1))
8797 {
8798 tem = fold_div_compare (loc, code, type, arg0, arg1);
8799 if (tem != NULL_TREE)
8800 return tem;
8801 }
8802
8803 return NULL_TREE;
8804 }
8805
8806
8807 /* Subroutine of fold_binary. Optimize complex multiplications of the
8808 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8809 argument EXPR represents the expression "z" of type TYPE. */
8810
8811 static tree
8812 fold_mult_zconjz (location_t loc, tree type, tree expr)
8813 {
8814 tree itype = TREE_TYPE (type);
8815 tree rpart, ipart, tem;
8816
8817 if (TREE_CODE (expr) == COMPLEX_EXPR)
8818 {
8819 rpart = TREE_OPERAND (expr, 0);
8820 ipart = TREE_OPERAND (expr, 1);
8821 }
8822 else if (TREE_CODE (expr) == COMPLEX_CST)
8823 {
8824 rpart = TREE_REALPART (expr);
8825 ipart = TREE_IMAGPART (expr);
8826 }
8827 else
8828 {
8829 expr = save_expr (expr);
8830 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8831 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8832 }
8833
8834 rpart = save_expr (rpart);
8835 ipart = save_expr (ipart);
8836 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8837 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8838 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8839 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8840 build_zero_cst (itype));
8841 }
8842
8843
8844 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8845 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8846
8847 static bool
8848 vec_cst_ctor_to_array (tree arg, tree *elts)
8849 {
8850 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8851
8852 if (TREE_CODE (arg) == VECTOR_CST)
8853 {
8854 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8855 elts[i] = VECTOR_CST_ELT (arg, i);
8856 }
8857 else if (TREE_CODE (arg) == CONSTRUCTOR)
8858 {
8859 constructor_elt *elt;
8860
8861 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8862 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8863 return false;
8864 else
8865 elts[i] = elt->value;
8866 }
8867 else
8868 return false;
8869 for (; i < nelts; i++)
8870 elts[i]
8871 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8872 return true;
8873 }
8874
8875 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8876 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8877 NULL_TREE otherwise. */
8878
8879 static tree
8880 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8881 {
8882 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8883 tree *elts;
8884 bool need_ctor = false;
8885
8886 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8887 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8888 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8889 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8890 return NULL_TREE;
8891
8892 elts = XALLOCAVEC (tree, nelts * 3);
8893 if (!vec_cst_ctor_to_array (arg0, elts)
8894 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8895 return NULL_TREE;
8896
8897 for (i = 0; i < nelts; i++)
8898 {
8899 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8900 need_ctor = true;
8901 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8902 }
8903
8904 if (need_ctor)
8905 {
8906 vec<constructor_elt, va_gc> *v;
8907 vec_alloc (v, nelts);
8908 for (i = 0; i < nelts; i++)
8909 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8910 return build_constructor (type, v);
8911 }
8912 else
8913 return build_vector (type, &elts[2 * nelts]);
8914 }
8915
8916 /* Try to fold a pointer difference of type TYPE two address expressions of
8917 array references AREF0 and AREF1 using location LOC. Return a
8918 simplified expression for the difference or NULL_TREE. */
8919
8920 static tree
8921 fold_addr_of_array_ref_difference (location_t loc, tree type,
8922 tree aref0, tree aref1)
8923 {
8924 tree base0 = TREE_OPERAND (aref0, 0);
8925 tree base1 = TREE_OPERAND (aref1, 0);
8926 tree base_offset = build_int_cst (type, 0);
8927
8928 /* If the bases are array references as well, recurse. If the bases
8929 are pointer indirections compute the difference of the pointers.
8930 If the bases are equal, we are set. */
8931 if ((TREE_CODE (base0) == ARRAY_REF
8932 && TREE_CODE (base1) == ARRAY_REF
8933 && (base_offset
8934 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8935 || (INDIRECT_REF_P (base0)
8936 && INDIRECT_REF_P (base1)
8937 && (base_offset
8938 = fold_binary_loc (loc, MINUS_EXPR, type,
8939 fold_convert (type, TREE_OPERAND (base0, 0)),
8940 fold_convert (type,
8941 TREE_OPERAND (base1, 0)))))
8942 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8943 {
8944 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8945 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8946 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8947 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
8948 return fold_build2_loc (loc, PLUS_EXPR, type,
8949 base_offset,
8950 fold_build2_loc (loc, MULT_EXPR, type,
8951 diff, esz));
8952 }
8953 return NULL_TREE;
8954 }
8955
8956 /* If the real or vector real constant CST of type TYPE has an exact
8957 inverse, return it, else return NULL. */
8958
8959 tree
8960 exact_inverse (tree type, tree cst)
8961 {
8962 REAL_VALUE_TYPE r;
8963 tree unit_type, *elts;
8964 machine_mode mode;
8965 unsigned vec_nelts, i;
8966
8967 switch (TREE_CODE (cst))
8968 {
8969 case REAL_CST:
8970 r = TREE_REAL_CST (cst);
8971
8972 if (exact_real_inverse (TYPE_MODE (type), &r))
8973 return build_real (type, r);
8974
8975 return NULL_TREE;
8976
8977 case VECTOR_CST:
8978 vec_nelts = VECTOR_CST_NELTS (cst);
8979 elts = XALLOCAVEC (tree, vec_nelts);
8980 unit_type = TREE_TYPE (type);
8981 mode = TYPE_MODE (unit_type);
8982
8983 for (i = 0; i < vec_nelts; i++)
8984 {
8985 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8986 if (!exact_real_inverse (mode, &r))
8987 return NULL_TREE;
8988 elts[i] = build_real (unit_type, r);
8989 }
8990
8991 return build_vector (type, elts);
8992
8993 default:
8994 return NULL_TREE;
8995 }
8996 }
8997
8998 /* Mask out the tz least significant bits of X of type TYPE where
8999 tz is the number of trailing zeroes in Y. */
9000 static wide_int
9001 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9002 {
9003 int tz = wi::ctz (y);
9004 if (tz > 0)
9005 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9006 return x;
9007 }
9008
9009 /* Return true when T is an address and is known to be nonzero.
9010 For floating point we further ensure that T is not denormal.
9011 Similar logic is present in nonzero_address in rtlanal.h.
9012
9013 If the return value is based on the assumption that signed overflow
9014 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9015 change *STRICT_OVERFLOW_P. */
9016
9017 static bool
9018 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9019 {
9020 tree type = TREE_TYPE (t);
9021 enum tree_code code;
9022
9023 /* Doing something useful for floating point would need more work. */
9024 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9025 return false;
9026
9027 code = TREE_CODE (t);
9028 switch (TREE_CODE_CLASS (code))
9029 {
9030 case tcc_unary:
9031 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9032 strict_overflow_p);
9033 case tcc_binary:
9034 case tcc_comparison:
9035 return tree_binary_nonzero_warnv_p (code, type,
9036 TREE_OPERAND (t, 0),
9037 TREE_OPERAND (t, 1),
9038 strict_overflow_p);
9039 case tcc_constant:
9040 case tcc_declaration:
9041 case tcc_reference:
9042 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9043
9044 default:
9045 break;
9046 }
9047
9048 switch (code)
9049 {
9050 case TRUTH_NOT_EXPR:
9051 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9052 strict_overflow_p);
9053
9054 case TRUTH_AND_EXPR:
9055 case TRUTH_OR_EXPR:
9056 case TRUTH_XOR_EXPR:
9057 return tree_binary_nonzero_warnv_p (code, type,
9058 TREE_OPERAND (t, 0),
9059 TREE_OPERAND (t, 1),
9060 strict_overflow_p);
9061
9062 case COND_EXPR:
9063 case CONSTRUCTOR:
9064 case OBJ_TYPE_REF:
9065 case ASSERT_EXPR:
9066 case ADDR_EXPR:
9067 case WITH_SIZE_EXPR:
9068 case SSA_NAME:
9069 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9070
9071 case COMPOUND_EXPR:
9072 case MODIFY_EXPR:
9073 case BIND_EXPR:
9074 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9075 strict_overflow_p);
9076
9077 case SAVE_EXPR:
9078 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9079 strict_overflow_p);
9080
9081 case CALL_EXPR:
9082 {
9083 tree fndecl = get_callee_fndecl (t);
9084 if (!fndecl) return false;
9085 if (flag_delete_null_pointer_checks && !flag_check_new
9086 && DECL_IS_OPERATOR_NEW (fndecl)
9087 && !TREE_NOTHROW (fndecl))
9088 return true;
9089 if (flag_delete_null_pointer_checks
9090 && lookup_attribute ("returns_nonnull",
9091 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9092 return true;
9093 return alloca_call_p (t);
9094 }
9095
9096 default:
9097 break;
9098 }
9099 return false;
9100 }
9101
9102 /* Return true when T is an address and is known to be nonzero.
9103 Handle warnings about undefined signed overflow. */
9104
9105 bool
9106 tree_expr_nonzero_p (tree t)
9107 {
9108 bool ret, strict_overflow_p;
9109
9110 strict_overflow_p = false;
9111 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9112 if (strict_overflow_p)
9113 fold_overflow_warning (("assuming signed overflow does not occur when "
9114 "determining that expression is always "
9115 "non-zero"),
9116 WARN_STRICT_OVERFLOW_MISC);
9117 return ret;
9118 }
9119
9120 /* Return true if T is known not to be equal to an integer W. */
9121
9122 bool
9123 expr_not_equal_to (tree t, const wide_int &w)
9124 {
9125 wide_int min, max, nz;
9126 value_range_type rtype;
9127 switch (TREE_CODE (t))
9128 {
9129 case INTEGER_CST:
9130 return wi::ne_p (t, w);
9131
9132 case SSA_NAME:
9133 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9134 return false;
9135 rtype = get_range_info (t, &min, &max);
9136 if (rtype == VR_RANGE)
9137 {
9138 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9139 return true;
9140 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9141 return true;
9142 }
9143 else if (rtype == VR_ANTI_RANGE
9144 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9145 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9146 return true;
9147 /* If T has some known zero bits and W has any of those bits set,
9148 then T is known not to be equal to W. */
9149 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9150 TYPE_PRECISION (TREE_TYPE (t))), 0))
9151 return true;
9152 return false;
9153
9154 default:
9155 return false;
9156 }
9157 }
9158
9159 /* Fold a binary expression of code CODE and type TYPE with operands
9160 OP0 and OP1. LOC is the location of the resulting expression.
9161 Return the folded expression if folding is successful. Otherwise,
9162 return NULL_TREE. */
9163
9164 tree
9165 fold_binary_loc (location_t loc,
9166 enum tree_code code, tree type, tree op0, tree op1)
9167 {
9168 enum tree_code_class kind = TREE_CODE_CLASS (code);
9169 tree arg0, arg1, tem;
9170 tree t1 = NULL_TREE;
9171 bool strict_overflow_p;
9172 unsigned int prec;
9173
9174 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9175 && TREE_CODE_LENGTH (code) == 2
9176 && op0 != NULL_TREE
9177 && op1 != NULL_TREE);
9178
9179 arg0 = op0;
9180 arg1 = op1;
9181
9182 /* Strip any conversions that don't change the mode. This is
9183 safe for every expression, except for a comparison expression
9184 because its signedness is derived from its operands. So, in
9185 the latter case, only strip conversions that don't change the
9186 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9187 preserved.
9188
9189 Note that this is done as an internal manipulation within the
9190 constant folder, in order to find the simplest representation
9191 of the arguments so that their form can be studied. In any
9192 cases, the appropriate type conversions should be put back in
9193 the tree that will get out of the constant folder. */
9194
9195 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9196 {
9197 STRIP_SIGN_NOPS (arg0);
9198 STRIP_SIGN_NOPS (arg1);
9199 }
9200 else
9201 {
9202 STRIP_NOPS (arg0);
9203 STRIP_NOPS (arg1);
9204 }
9205
9206 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9207 constant but we can't do arithmetic on them. */
9208 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9209 {
9210 tem = const_binop (code, type, arg0, arg1);
9211 if (tem != NULL_TREE)
9212 {
9213 if (TREE_TYPE (tem) != type)
9214 tem = fold_convert_loc (loc, type, tem);
9215 return tem;
9216 }
9217 }
9218
9219 /* If this is a commutative operation, and ARG0 is a constant, move it
9220 to ARG1 to reduce the number of tests below. */
9221 if (commutative_tree_code (code)
9222 && tree_swap_operands_p (arg0, arg1))
9223 return fold_build2_loc (loc, code, type, op1, op0);
9224
9225 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9226 to ARG1 to reduce the number of tests below. */
9227 if (kind == tcc_comparison
9228 && tree_swap_operands_p (arg0, arg1))
9229 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9230
9231 tem = generic_simplify (loc, code, type, op0, op1);
9232 if (tem)
9233 return tem;
9234
9235 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9236
9237 First check for cases where an arithmetic operation is applied to a
9238 compound, conditional, or comparison operation. Push the arithmetic
9239 operation inside the compound or conditional to see if any folding
9240 can then be done. Convert comparison to conditional for this purpose.
9241 The also optimizes non-constant cases that used to be done in
9242 expand_expr.
9243
9244 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9245 one of the operands is a comparison and the other is a comparison, a
9246 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9247 code below would make the expression more complex. Change it to a
9248 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9249 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9250
9251 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9252 || code == EQ_EXPR || code == NE_EXPR)
9253 && TREE_CODE (type) != VECTOR_TYPE
9254 && ((truth_value_p (TREE_CODE (arg0))
9255 && (truth_value_p (TREE_CODE (arg1))
9256 || (TREE_CODE (arg1) == BIT_AND_EXPR
9257 && integer_onep (TREE_OPERAND (arg1, 1)))))
9258 || (truth_value_p (TREE_CODE (arg1))
9259 && (truth_value_p (TREE_CODE (arg0))
9260 || (TREE_CODE (arg0) == BIT_AND_EXPR
9261 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9262 {
9263 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9264 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9265 : TRUTH_XOR_EXPR,
9266 boolean_type_node,
9267 fold_convert_loc (loc, boolean_type_node, arg0),
9268 fold_convert_loc (loc, boolean_type_node, arg1));
9269
9270 if (code == EQ_EXPR)
9271 tem = invert_truthvalue_loc (loc, tem);
9272
9273 return fold_convert_loc (loc, type, tem);
9274 }
9275
9276 if (TREE_CODE_CLASS (code) == tcc_binary
9277 || TREE_CODE_CLASS (code) == tcc_comparison)
9278 {
9279 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9280 {
9281 tem = fold_build2_loc (loc, code, type,
9282 fold_convert_loc (loc, TREE_TYPE (op0),
9283 TREE_OPERAND (arg0, 1)), op1);
9284 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9285 tem);
9286 }
9287 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9288 {
9289 tem = fold_build2_loc (loc, code, type, op0,
9290 fold_convert_loc (loc, TREE_TYPE (op1),
9291 TREE_OPERAND (arg1, 1)));
9292 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9293 tem);
9294 }
9295
9296 if (TREE_CODE (arg0) == COND_EXPR
9297 || TREE_CODE (arg0) == VEC_COND_EXPR
9298 || COMPARISON_CLASS_P (arg0))
9299 {
9300 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9301 arg0, arg1,
9302 /*cond_first_p=*/1);
9303 if (tem != NULL_TREE)
9304 return tem;
9305 }
9306
9307 if (TREE_CODE (arg1) == COND_EXPR
9308 || TREE_CODE (arg1) == VEC_COND_EXPR
9309 || COMPARISON_CLASS_P (arg1))
9310 {
9311 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9312 arg1, arg0,
9313 /*cond_first_p=*/0);
9314 if (tem != NULL_TREE)
9315 return tem;
9316 }
9317 }
9318
9319 switch (code)
9320 {
9321 case MEM_REF:
9322 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9323 if (TREE_CODE (arg0) == ADDR_EXPR
9324 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9325 {
9326 tree iref = TREE_OPERAND (arg0, 0);
9327 return fold_build2 (MEM_REF, type,
9328 TREE_OPERAND (iref, 0),
9329 int_const_binop (PLUS_EXPR, arg1,
9330 TREE_OPERAND (iref, 1)));
9331 }
9332
9333 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9334 if (TREE_CODE (arg0) == ADDR_EXPR
9335 && handled_component_p (TREE_OPERAND (arg0, 0)))
9336 {
9337 tree base;
9338 HOST_WIDE_INT coffset;
9339 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9340 &coffset);
9341 if (!base)
9342 return NULL_TREE;
9343 return fold_build2 (MEM_REF, type,
9344 build_fold_addr_expr (base),
9345 int_const_binop (PLUS_EXPR, arg1,
9346 size_int (coffset)));
9347 }
9348
9349 return NULL_TREE;
9350
9351 case POINTER_PLUS_EXPR:
9352 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9353 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9354 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9355 return fold_convert_loc (loc, type,
9356 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9357 fold_convert_loc (loc, sizetype,
9358 arg1),
9359 fold_convert_loc (loc, sizetype,
9360 arg0)));
9361
9362 return NULL_TREE;
9363
9364 case PLUS_EXPR:
9365 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9366 {
9367 /* X + (X / CST) * -CST is X % CST. */
9368 if (TREE_CODE (arg1) == MULT_EXPR
9369 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9370 && operand_equal_p (arg0,
9371 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9372 {
9373 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9374 tree cst1 = TREE_OPERAND (arg1, 1);
9375 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9376 cst1, cst0);
9377 if (sum && integer_zerop (sum))
9378 return fold_convert_loc (loc, type,
9379 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9380 TREE_TYPE (arg0), arg0,
9381 cst0));
9382 }
9383 }
9384
9385 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9386 one. Make sure the type is not saturating and has the signedness of
9387 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9388 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9389 if ((TREE_CODE (arg0) == MULT_EXPR
9390 || TREE_CODE (arg1) == MULT_EXPR)
9391 && !TYPE_SATURATING (type)
9392 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9393 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9394 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9395 {
9396 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9397 if (tem)
9398 return tem;
9399 }
9400
9401 if (! FLOAT_TYPE_P (type))
9402 {
9403 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9404 (plus (plus (mult) (mult)) (foo)) so that we can
9405 take advantage of the factoring cases below. */
9406 if (ANY_INTEGRAL_TYPE_P (type)
9407 && TYPE_OVERFLOW_WRAPS (type)
9408 && (((TREE_CODE (arg0) == PLUS_EXPR
9409 || TREE_CODE (arg0) == MINUS_EXPR)
9410 && TREE_CODE (arg1) == MULT_EXPR)
9411 || ((TREE_CODE (arg1) == PLUS_EXPR
9412 || TREE_CODE (arg1) == MINUS_EXPR)
9413 && TREE_CODE (arg0) == MULT_EXPR)))
9414 {
9415 tree parg0, parg1, parg, marg;
9416 enum tree_code pcode;
9417
9418 if (TREE_CODE (arg1) == MULT_EXPR)
9419 parg = arg0, marg = arg1;
9420 else
9421 parg = arg1, marg = arg0;
9422 pcode = TREE_CODE (parg);
9423 parg0 = TREE_OPERAND (parg, 0);
9424 parg1 = TREE_OPERAND (parg, 1);
9425 STRIP_NOPS (parg0);
9426 STRIP_NOPS (parg1);
9427
9428 if (TREE_CODE (parg0) == MULT_EXPR
9429 && TREE_CODE (parg1) != MULT_EXPR)
9430 return fold_build2_loc (loc, pcode, type,
9431 fold_build2_loc (loc, PLUS_EXPR, type,
9432 fold_convert_loc (loc, type,
9433 parg0),
9434 fold_convert_loc (loc, type,
9435 marg)),
9436 fold_convert_loc (loc, type, parg1));
9437 if (TREE_CODE (parg0) != MULT_EXPR
9438 && TREE_CODE (parg1) == MULT_EXPR)
9439 return
9440 fold_build2_loc (loc, PLUS_EXPR, type,
9441 fold_convert_loc (loc, type, parg0),
9442 fold_build2_loc (loc, pcode, type,
9443 fold_convert_loc (loc, type, marg),
9444 fold_convert_loc (loc, type,
9445 parg1)));
9446 }
9447 }
9448 else
9449 {
9450 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9451 to __complex__ ( x, y ). This is not the same for SNaNs or
9452 if signed zeros are involved. */
9453 if (!HONOR_SNANS (element_mode (arg0))
9454 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9455 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9456 {
9457 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9458 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9459 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9460 bool arg0rz = false, arg0iz = false;
9461 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9462 || (arg0i && (arg0iz = real_zerop (arg0i))))
9463 {
9464 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9465 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9466 if (arg0rz && arg1i && real_zerop (arg1i))
9467 {
9468 tree rp = arg1r ? arg1r
9469 : build1 (REALPART_EXPR, rtype, arg1);
9470 tree ip = arg0i ? arg0i
9471 : build1 (IMAGPART_EXPR, rtype, arg0);
9472 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9473 }
9474 else if (arg0iz && arg1r && real_zerop (arg1r))
9475 {
9476 tree rp = arg0r ? arg0r
9477 : build1 (REALPART_EXPR, rtype, arg0);
9478 tree ip = arg1i ? arg1i
9479 : build1 (IMAGPART_EXPR, rtype, arg1);
9480 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9481 }
9482 }
9483 }
9484
9485 if (flag_unsafe_math_optimizations
9486 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9487 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9488 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9489 return tem;
9490
9491 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9492 We associate floats only if the user has specified
9493 -fassociative-math. */
9494 if (flag_associative_math
9495 && TREE_CODE (arg1) == PLUS_EXPR
9496 && TREE_CODE (arg0) != MULT_EXPR)
9497 {
9498 tree tree10 = TREE_OPERAND (arg1, 0);
9499 tree tree11 = TREE_OPERAND (arg1, 1);
9500 if (TREE_CODE (tree11) == MULT_EXPR
9501 && TREE_CODE (tree10) == MULT_EXPR)
9502 {
9503 tree tree0;
9504 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9505 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9506 }
9507 }
9508 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9509 We associate floats only if the user has specified
9510 -fassociative-math. */
9511 if (flag_associative_math
9512 && TREE_CODE (arg0) == PLUS_EXPR
9513 && TREE_CODE (arg1) != MULT_EXPR)
9514 {
9515 tree tree00 = TREE_OPERAND (arg0, 0);
9516 tree tree01 = TREE_OPERAND (arg0, 1);
9517 if (TREE_CODE (tree01) == MULT_EXPR
9518 && TREE_CODE (tree00) == MULT_EXPR)
9519 {
9520 tree tree0;
9521 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9522 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9523 }
9524 }
9525 }
9526
9527 bit_rotate:
9528 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9529 is a rotate of A by C1 bits. */
9530 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9531 is a rotate of A by B bits. */
9532 {
9533 enum tree_code code0, code1;
9534 tree rtype;
9535 code0 = TREE_CODE (arg0);
9536 code1 = TREE_CODE (arg1);
9537 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9538 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9539 && operand_equal_p (TREE_OPERAND (arg0, 0),
9540 TREE_OPERAND (arg1, 0), 0)
9541 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9542 TYPE_UNSIGNED (rtype))
9543 /* Only create rotates in complete modes. Other cases are not
9544 expanded properly. */
9545 && (element_precision (rtype)
9546 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9547 {
9548 tree tree01, tree11;
9549 enum tree_code code01, code11;
9550
9551 tree01 = TREE_OPERAND (arg0, 1);
9552 tree11 = TREE_OPERAND (arg1, 1);
9553 STRIP_NOPS (tree01);
9554 STRIP_NOPS (tree11);
9555 code01 = TREE_CODE (tree01);
9556 code11 = TREE_CODE (tree11);
9557 if (code01 == INTEGER_CST
9558 && code11 == INTEGER_CST
9559 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9560 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9561 {
9562 tem = build2_loc (loc, LROTATE_EXPR,
9563 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9564 TREE_OPERAND (arg0, 0),
9565 code0 == LSHIFT_EXPR
9566 ? TREE_OPERAND (arg0, 1)
9567 : TREE_OPERAND (arg1, 1));
9568 return fold_convert_loc (loc, type, tem);
9569 }
9570 else if (code11 == MINUS_EXPR)
9571 {
9572 tree tree110, tree111;
9573 tree110 = TREE_OPERAND (tree11, 0);
9574 tree111 = TREE_OPERAND (tree11, 1);
9575 STRIP_NOPS (tree110);
9576 STRIP_NOPS (tree111);
9577 if (TREE_CODE (tree110) == INTEGER_CST
9578 && 0 == compare_tree_int (tree110,
9579 element_precision
9580 (TREE_TYPE (TREE_OPERAND
9581 (arg0, 0))))
9582 && operand_equal_p (tree01, tree111, 0))
9583 return
9584 fold_convert_loc (loc, type,
9585 build2 ((code0 == LSHIFT_EXPR
9586 ? LROTATE_EXPR
9587 : RROTATE_EXPR),
9588 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9589 TREE_OPERAND (arg0, 0),
9590 TREE_OPERAND (arg0, 1)));
9591 }
9592 else if (code01 == MINUS_EXPR)
9593 {
9594 tree tree010, tree011;
9595 tree010 = TREE_OPERAND (tree01, 0);
9596 tree011 = TREE_OPERAND (tree01, 1);
9597 STRIP_NOPS (tree010);
9598 STRIP_NOPS (tree011);
9599 if (TREE_CODE (tree010) == INTEGER_CST
9600 && 0 == compare_tree_int (tree010,
9601 element_precision
9602 (TREE_TYPE (TREE_OPERAND
9603 (arg0, 0))))
9604 && operand_equal_p (tree11, tree011, 0))
9605 return fold_convert_loc
9606 (loc, type,
9607 build2 ((code0 != LSHIFT_EXPR
9608 ? LROTATE_EXPR
9609 : RROTATE_EXPR),
9610 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9611 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9612 }
9613 }
9614 }
9615
9616 associate:
9617 /* In most languages, can't associate operations on floats through
9618 parentheses. Rather than remember where the parentheses were, we
9619 don't associate floats at all, unless the user has specified
9620 -fassociative-math.
9621 And, we need to make sure type is not saturating. */
9622
9623 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9624 && !TYPE_SATURATING (type))
9625 {
9626 tree var0, con0, lit0, minus_lit0;
9627 tree var1, con1, lit1, minus_lit1;
9628 tree atype = type;
9629 bool ok = true;
9630
9631 /* Split both trees into variables, constants, and literals. Then
9632 associate each group together, the constants with literals,
9633 then the result with variables. This increases the chances of
9634 literals being recombined later and of generating relocatable
9635 expressions for the sum of a constant and literal. */
9636 var0 = split_tree (loc, arg0, type, code,
9637 &con0, &lit0, &minus_lit0, 0);
9638 var1 = split_tree (loc, arg1, type, code,
9639 &con1, &lit1, &minus_lit1, code == MINUS_EXPR);
9640
9641 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9642 if (code == MINUS_EXPR)
9643 code = PLUS_EXPR;
9644
9645 /* With undefined overflow prefer doing association in a type
9646 which wraps on overflow, if that is one of the operand types. */
9647 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9648 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9649 {
9650 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9651 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9652 atype = TREE_TYPE (arg0);
9653 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9654 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9655 atype = TREE_TYPE (arg1);
9656 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9657 }
9658
9659 /* With undefined overflow we can only associate constants with one
9660 variable, and constants whose association doesn't overflow. */
9661 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9662 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9663 {
9664 if (var0 && var1)
9665 {
9666 tree tmp0 = var0;
9667 tree tmp1 = var1;
9668 bool one_neg = false;
9669
9670 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9671 {
9672 tmp0 = TREE_OPERAND (tmp0, 0);
9673 one_neg = !one_neg;
9674 }
9675 if (CONVERT_EXPR_P (tmp0)
9676 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9677 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9678 <= TYPE_PRECISION (atype)))
9679 tmp0 = TREE_OPERAND (tmp0, 0);
9680 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9681 {
9682 tmp1 = TREE_OPERAND (tmp1, 0);
9683 one_neg = !one_neg;
9684 }
9685 if (CONVERT_EXPR_P (tmp1)
9686 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9687 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9688 <= TYPE_PRECISION (atype)))
9689 tmp1 = TREE_OPERAND (tmp1, 0);
9690 /* The only case we can still associate with two variables
9691 is if they cancel out. */
9692 if (!one_neg
9693 || !operand_equal_p (tmp0, tmp1, 0))
9694 ok = false;
9695 }
9696 }
9697
9698 /* Only do something if we found more than two objects. Otherwise,
9699 nothing has changed and we risk infinite recursion. */
9700 if (ok
9701 && (2 < ((var0 != 0) + (var1 != 0)
9702 + (con0 != 0) + (con1 != 0)
9703 + (lit0 != 0) + (lit1 != 0)
9704 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9705 {
9706 bool any_overflows = false;
9707 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9708 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9709 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9710 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9711 var0 = associate_trees (loc, var0, var1, code, atype);
9712 con0 = associate_trees (loc, con0, con1, code, atype);
9713 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9714 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9715 code, atype);
9716
9717 /* Preserve the MINUS_EXPR if the negative part of the literal is
9718 greater than the positive part. Otherwise, the multiplicative
9719 folding code (i.e extract_muldiv) may be fooled in case
9720 unsigned constants are subtracted, like in the following
9721 example: ((X*2 + 4) - 8U)/2. */
9722 if (minus_lit0 && lit0)
9723 {
9724 if (TREE_CODE (lit0) == INTEGER_CST
9725 && TREE_CODE (minus_lit0) == INTEGER_CST
9726 && tree_int_cst_lt (lit0, minus_lit0))
9727 {
9728 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9729 MINUS_EXPR, atype);
9730 lit0 = 0;
9731 }
9732 else
9733 {
9734 lit0 = associate_trees (loc, lit0, minus_lit0,
9735 MINUS_EXPR, atype);
9736 minus_lit0 = 0;
9737 }
9738 }
9739
9740 /* Don't introduce overflows through reassociation. */
9741 if (!any_overflows
9742 && ((lit0 && TREE_OVERFLOW_P (lit0))
9743 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9744 return NULL_TREE;
9745
9746 if (minus_lit0)
9747 {
9748 if (con0 == 0)
9749 return
9750 fold_convert_loc (loc, type,
9751 associate_trees (loc, var0, minus_lit0,
9752 MINUS_EXPR, atype));
9753 else
9754 {
9755 con0 = associate_trees (loc, con0, minus_lit0,
9756 MINUS_EXPR, atype);
9757 return
9758 fold_convert_loc (loc, type,
9759 associate_trees (loc, var0, con0,
9760 PLUS_EXPR, atype));
9761 }
9762 }
9763
9764 con0 = associate_trees (loc, con0, lit0, code, atype);
9765 return
9766 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9767 code, atype));
9768 }
9769 }
9770
9771 return NULL_TREE;
9772
9773 case MINUS_EXPR:
9774 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9775 if (TREE_CODE (arg0) == NEGATE_EXPR
9776 && negate_expr_p (op1))
9777 return fold_build2_loc (loc, MINUS_EXPR, type,
9778 negate_expr (op1),
9779 fold_convert_loc (loc, type,
9780 TREE_OPERAND (arg0, 0)));
9781
9782 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9783 __complex__ ( x, -y ). This is not the same for SNaNs or if
9784 signed zeros are involved. */
9785 if (!HONOR_SNANS (element_mode (arg0))
9786 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9787 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9788 {
9789 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9790 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9791 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9792 bool arg0rz = false, arg0iz = false;
9793 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9794 || (arg0i && (arg0iz = real_zerop (arg0i))))
9795 {
9796 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9797 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9798 if (arg0rz && arg1i && real_zerop (arg1i))
9799 {
9800 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9801 arg1r ? arg1r
9802 : build1 (REALPART_EXPR, rtype, arg1));
9803 tree ip = arg0i ? arg0i
9804 : build1 (IMAGPART_EXPR, rtype, arg0);
9805 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9806 }
9807 else if (arg0iz && arg1r && real_zerop (arg1r))
9808 {
9809 tree rp = arg0r ? arg0r
9810 : build1 (REALPART_EXPR, rtype, arg0);
9811 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9812 arg1i ? arg1i
9813 : build1 (IMAGPART_EXPR, rtype, arg1));
9814 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9815 }
9816 }
9817 }
9818
9819 /* A - B -> A + (-B) if B is easily negatable. */
9820 if (negate_expr_p (op1)
9821 && ! TYPE_OVERFLOW_SANITIZED (type)
9822 && ((FLOAT_TYPE_P (type)
9823 /* Avoid this transformation if B is a positive REAL_CST. */
9824 && (TREE_CODE (op1) != REAL_CST
9825 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9826 || INTEGRAL_TYPE_P (type)))
9827 return fold_build2_loc (loc, PLUS_EXPR, type,
9828 fold_convert_loc (loc, type, arg0),
9829 negate_expr (op1));
9830
9831 /* Fold &a[i] - &a[j] to i-j. */
9832 if (TREE_CODE (arg0) == ADDR_EXPR
9833 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9834 && TREE_CODE (arg1) == ADDR_EXPR
9835 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9836 {
9837 tree tem = fold_addr_of_array_ref_difference (loc, type,
9838 TREE_OPERAND (arg0, 0),
9839 TREE_OPERAND (arg1, 0));
9840 if (tem)
9841 return tem;
9842 }
9843
9844 if (FLOAT_TYPE_P (type)
9845 && flag_unsafe_math_optimizations
9846 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9847 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9848 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9849 return tem;
9850
9851 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9852 one. Make sure the type is not saturating and has the signedness of
9853 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9854 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9855 if ((TREE_CODE (arg0) == MULT_EXPR
9856 || TREE_CODE (arg1) == MULT_EXPR)
9857 && !TYPE_SATURATING (type)
9858 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9859 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9860 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9861 {
9862 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9863 if (tem)
9864 return tem;
9865 }
9866
9867 goto associate;
9868
9869 case MULT_EXPR:
9870 if (! FLOAT_TYPE_P (type))
9871 {
9872 /* Transform x * -C into -x * C if x is easily negatable. */
9873 if (TREE_CODE (op1) == INTEGER_CST
9874 && tree_int_cst_sgn (op1) == -1
9875 && negate_expr_p (op0)
9876 && negate_expr_p (op1)
9877 && (tem = negate_expr (op1)) != op1
9878 && ! TREE_OVERFLOW (tem))
9879 return fold_build2_loc (loc, MULT_EXPR, type,
9880 fold_convert_loc (loc, type,
9881 negate_expr (op0)), tem);
9882
9883 strict_overflow_p = false;
9884 if (TREE_CODE (arg1) == INTEGER_CST
9885 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9886 &strict_overflow_p)))
9887 {
9888 if (strict_overflow_p)
9889 fold_overflow_warning (("assuming signed overflow does not "
9890 "occur when simplifying "
9891 "multiplication"),
9892 WARN_STRICT_OVERFLOW_MISC);
9893 return fold_convert_loc (loc, type, tem);
9894 }
9895
9896 /* Optimize z * conj(z) for integer complex numbers. */
9897 if (TREE_CODE (arg0) == CONJ_EXPR
9898 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9899 return fold_mult_zconjz (loc, type, arg1);
9900 if (TREE_CODE (arg1) == CONJ_EXPR
9901 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9902 return fold_mult_zconjz (loc, type, arg0);
9903 }
9904 else
9905 {
9906 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9907 This is not the same for NaNs or if signed zeros are
9908 involved. */
9909 if (!HONOR_NANS (arg0)
9910 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9911 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9912 && TREE_CODE (arg1) == COMPLEX_CST
9913 && real_zerop (TREE_REALPART (arg1)))
9914 {
9915 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9916 if (real_onep (TREE_IMAGPART (arg1)))
9917 return
9918 fold_build2_loc (loc, COMPLEX_EXPR, type,
9919 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9920 rtype, arg0)),
9921 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9922 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9923 return
9924 fold_build2_loc (loc, COMPLEX_EXPR, type,
9925 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9926 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9927 rtype, arg0)));
9928 }
9929
9930 /* Optimize z * conj(z) for floating point complex numbers.
9931 Guarded by flag_unsafe_math_optimizations as non-finite
9932 imaginary components don't produce scalar results. */
9933 if (flag_unsafe_math_optimizations
9934 && TREE_CODE (arg0) == CONJ_EXPR
9935 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9936 return fold_mult_zconjz (loc, type, arg1);
9937 if (flag_unsafe_math_optimizations
9938 && TREE_CODE (arg1) == CONJ_EXPR
9939 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9940 return fold_mult_zconjz (loc, type, arg0);
9941 }
9942 goto associate;
9943
9944 case BIT_IOR_EXPR:
9945 /* Canonicalize (X & C1) | C2. */
9946 if (TREE_CODE (arg0) == BIT_AND_EXPR
9947 && TREE_CODE (arg1) == INTEGER_CST
9948 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9949 {
9950 int width = TYPE_PRECISION (type), w;
9951 wide_int c1 = TREE_OPERAND (arg0, 1);
9952 wide_int c2 = arg1;
9953
9954 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9955 if ((c1 & c2) == c1)
9956 return omit_one_operand_loc (loc, type, arg1,
9957 TREE_OPERAND (arg0, 0));
9958
9959 wide_int msk = wi::mask (width, false,
9960 TYPE_PRECISION (TREE_TYPE (arg1)));
9961
9962 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9963 if (msk.and_not (c1 | c2) == 0)
9964 {
9965 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9966 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9967 }
9968
9969 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9970 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9971 mode which allows further optimizations. */
9972 c1 &= msk;
9973 c2 &= msk;
9974 wide_int c3 = c1.and_not (c2);
9975 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9976 {
9977 wide_int mask = wi::mask (w, false,
9978 TYPE_PRECISION (type));
9979 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9980 {
9981 c3 = mask;
9982 break;
9983 }
9984 }
9985
9986 if (c3 != c1)
9987 {
9988 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9989 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
9990 wide_int_to_tree (type, c3));
9991 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
9992 }
9993 }
9994
9995 /* See if this can be simplified into a rotate first. If that
9996 is unsuccessful continue in the association code. */
9997 goto bit_rotate;
9998
9999 case BIT_XOR_EXPR:
10000 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10001 if (TREE_CODE (arg0) == BIT_AND_EXPR
10002 && INTEGRAL_TYPE_P (type)
10003 && integer_onep (TREE_OPERAND (arg0, 1))
10004 && integer_onep (arg1))
10005 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10006 build_zero_cst (TREE_TYPE (arg0)));
10007
10008 /* See if this can be simplified into a rotate first. If that
10009 is unsuccessful continue in the association code. */
10010 goto bit_rotate;
10011
10012 case BIT_AND_EXPR:
10013 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10014 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10015 && INTEGRAL_TYPE_P (type)
10016 && integer_onep (TREE_OPERAND (arg0, 1))
10017 && integer_onep (arg1))
10018 {
10019 tree tem2;
10020 tem = TREE_OPERAND (arg0, 0);
10021 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10022 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10023 tem, tem2);
10024 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10025 build_zero_cst (TREE_TYPE (tem)));
10026 }
10027 /* Fold ~X & 1 as (X & 1) == 0. */
10028 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10029 && INTEGRAL_TYPE_P (type)
10030 && integer_onep (arg1))
10031 {
10032 tree tem2;
10033 tem = TREE_OPERAND (arg0, 0);
10034 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10035 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10036 tem, tem2);
10037 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10038 build_zero_cst (TREE_TYPE (tem)));
10039 }
10040 /* Fold !X & 1 as X == 0. */
10041 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10042 && integer_onep (arg1))
10043 {
10044 tem = TREE_OPERAND (arg0, 0);
10045 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10046 build_zero_cst (TREE_TYPE (tem)));
10047 }
10048
10049 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10050 multiple of 1 << CST. */
10051 if (TREE_CODE (arg1) == INTEGER_CST)
10052 {
10053 wide_int cst1 = arg1;
10054 wide_int ncst1 = -cst1;
10055 if ((cst1 & ncst1) == ncst1
10056 && multiple_of_p (type, arg0,
10057 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10058 return fold_convert_loc (loc, type, arg0);
10059 }
10060
10061 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10062 bits from CST2. */
10063 if (TREE_CODE (arg1) == INTEGER_CST
10064 && TREE_CODE (arg0) == MULT_EXPR
10065 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10066 {
10067 wide_int warg1 = arg1;
10068 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10069
10070 if (masked == 0)
10071 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10072 arg0, arg1);
10073 else if (masked != warg1)
10074 {
10075 /* Avoid the transform if arg1 is a mask of some
10076 mode which allows further optimizations. */
10077 int pop = wi::popcount (warg1);
10078 if (!(pop >= BITS_PER_UNIT
10079 && pow2p_hwi (pop)
10080 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10081 return fold_build2_loc (loc, code, type, op0,
10082 wide_int_to_tree (type, masked));
10083 }
10084 }
10085
10086 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10087 ((A & N) + B) & M -> (A + B) & M
10088 Similarly if (N & M) == 0,
10089 ((A | N) + B) & M -> (A + B) & M
10090 and for - instead of + (or unary - instead of +)
10091 and/or ^ instead of |.
10092 If B is constant and (B & M) == 0, fold into A & M. */
10093 if (TREE_CODE (arg1) == INTEGER_CST)
10094 {
10095 wide_int cst1 = arg1;
10096 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10097 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10098 && (TREE_CODE (arg0) == PLUS_EXPR
10099 || TREE_CODE (arg0) == MINUS_EXPR
10100 || TREE_CODE (arg0) == NEGATE_EXPR)
10101 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10102 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10103 {
10104 tree pmop[2];
10105 int which = 0;
10106 wide_int cst0;
10107
10108 /* Now we know that arg0 is (C + D) or (C - D) or
10109 -C and arg1 (M) is == (1LL << cst) - 1.
10110 Store C into PMOP[0] and D into PMOP[1]. */
10111 pmop[0] = TREE_OPERAND (arg0, 0);
10112 pmop[1] = NULL;
10113 if (TREE_CODE (arg0) != NEGATE_EXPR)
10114 {
10115 pmop[1] = TREE_OPERAND (arg0, 1);
10116 which = 1;
10117 }
10118
10119 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10120 which = -1;
10121
10122 for (; which >= 0; which--)
10123 switch (TREE_CODE (pmop[which]))
10124 {
10125 case BIT_AND_EXPR:
10126 case BIT_IOR_EXPR:
10127 case BIT_XOR_EXPR:
10128 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10129 != INTEGER_CST)
10130 break;
10131 cst0 = TREE_OPERAND (pmop[which], 1);
10132 cst0 &= cst1;
10133 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10134 {
10135 if (cst0 != cst1)
10136 break;
10137 }
10138 else if (cst0 != 0)
10139 break;
10140 /* If C or D is of the form (A & N) where
10141 (N & M) == M, or of the form (A | N) or
10142 (A ^ N) where (N & M) == 0, replace it with A. */
10143 pmop[which] = TREE_OPERAND (pmop[which], 0);
10144 break;
10145 case INTEGER_CST:
10146 /* If C or D is a N where (N & M) == 0, it can be
10147 omitted (assumed 0). */
10148 if ((TREE_CODE (arg0) == PLUS_EXPR
10149 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10150 && (cst1 & pmop[which]) == 0)
10151 pmop[which] = NULL;
10152 break;
10153 default:
10154 break;
10155 }
10156
10157 /* Only build anything new if we optimized one or both arguments
10158 above. */
10159 if (pmop[0] != TREE_OPERAND (arg0, 0)
10160 || (TREE_CODE (arg0) != NEGATE_EXPR
10161 && pmop[1] != TREE_OPERAND (arg0, 1)))
10162 {
10163 tree utype = TREE_TYPE (arg0);
10164 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10165 {
10166 /* Perform the operations in a type that has defined
10167 overflow behavior. */
10168 utype = unsigned_type_for (TREE_TYPE (arg0));
10169 if (pmop[0] != NULL)
10170 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10171 if (pmop[1] != NULL)
10172 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10173 }
10174
10175 if (TREE_CODE (arg0) == NEGATE_EXPR)
10176 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10177 else if (TREE_CODE (arg0) == PLUS_EXPR)
10178 {
10179 if (pmop[0] != NULL && pmop[1] != NULL)
10180 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10181 pmop[0], pmop[1]);
10182 else if (pmop[0] != NULL)
10183 tem = pmop[0];
10184 else if (pmop[1] != NULL)
10185 tem = pmop[1];
10186 else
10187 return build_int_cst (type, 0);
10188 }
10189 else if (pmop[0] == NULL)
10190 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10191 else
10192 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10193 pmop[0], pmop[1]);
10194 /* TEM is now the new binary +, - or unary - replacement. */
10195 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10196 fold_convert_loc (loc, utype, arg1));
10197 return fold_convert_loc (loc, type, tem);
10198 }
10199 }
10200 }
10201
10202 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10203 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10204 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10205 {
10206 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10207
10208 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10209 if (mask == -1)
10210 return
10211 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10212 }
10213
10214 goto associate;
10215
10216 case RDIV_EXPR:
10217 /* Don't touch a floating-point divide by zero unless the mode
10218 of the constant can represent infinity. */
10219 if (TREE_CODE (arg1) == REAL_CST
10220 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10221 && real_zerop (arg1))
10222 return NULL_TREE;
10223
10224 /* (-A) / (-B) -> A / B */
10225 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10226 return fold_build2_loc (loc, RDIV_EXPR, type,
10227 TREE_OPERAND (arg0, 0),
10228 negate_expr (arg1));
10229 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10230 return fold_build2_loc (loc, RDIV_EXPR, type,
10231 negate_expr (arg0),
10232 TREE_OPERAND (arg1, 0));
10233 return NULL_TREE;
10234
10235 case TRUNC_DIV_EXPR:
10236 /* Fall through */
10237
10238 case FLOOR_DIV_EXPR:
10239 /* Simplify A / (B << N) where A and B are positive and B is
10240 a power of 2, to A >> (N + log2(B)). */
10241 strict_overflow_p = false;
10242 if (TREE_CODE (arg1) == LSHIFT_EXPR
10243 && (TYPE_UNSIGNED (type)
10244 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10245 {
10246 tree sval = TREE_OPERAND (arg1, 0);
10247 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10248 {
10249 tree sh_cnt = TREE_OPERAND (arg1, 1);
10250 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10251 wi::exact_log2 (sval));
10252
10253 if (strict_overflow_p)
10254 fold_overflow_warning (("assuming signed overflow does not "
10255 "occur when simplifying A / (B << N)"),
10256 WARN_STRICT_OVERFLOW_MISC);
10257
10258 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10259 sh_cnt, pow2);
10260 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10261 fold_convert_loc (loc, type, arg0), sh_cnt);
10262 }
10263 }
10264
10265 /* Fall through */
10266
10267 case ROUND_DIV_EXPR:
10268 case CEIL_DIV_EXPR:
10269 case EXACT_DIV_EXPR:
10270 if (integer_zerop (arg1))
10271 return NULL_TREE;
10272
10273 /* Convert -A / -B to A / B when the type is signed and overflow is
10274 undefined. */
10275 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10276 && TREE_CODE (op0) == NEGATE_EXPR
10277 && negate_expr_p (op1))
10278 {
10279 if (INTEGRAL_TYPE_P (type))
10280 fold_overflow_warning (("assuming signed overflow does not occur "
10281 "when distributing negation across "
10282 "division"),
10283 WARN_STRICT_OVERFLOW_MISC);
10284 return fold_build2_loc (loc, code, type,
10285 fold_convert_loc (loc, type,
10286 TREE_OPERAND (arg0, 0)),
10287 negate_expr (op1));
10288 }
10289 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10290 && TREE_CODE (arg1) == NEGATE_EXPR
10291 && negate_expr_p (op0))
10292 {
10293 if (INTEGRAL_TYPE_P (type))
10294 fold_overflow_warning (("assuming signed overflow does not occur "
10295 "when distributing negation across "
10296 "division"),
10297 WARN_STRICT_OVERFLOW_MISC);
10298 return fold_build2_loc (loc, code, type,
10299 negate_expr (op0),
10300 fold_convert_loc (loc, type,
10301 TREE_OPERAND (arg1, 0)));
10302 }
10303
10304 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10305 operation, EXACT_DIV_EXPR.
10306
10307 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10308 At one time others generated faster code, it's not clear if they do
10309 after the last round to changes to the DIV code in expmed.c. */
10310 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10311 && multiple_of_p (type, arg0, arg1))
10312 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10313 fold_convert (type, arg0),
10314 fold_convert (type, arg1));
10315
10316 strict_overflow_p = false;
10317 if (TREE_CODE (arg1) == INTEGER_CST
10318 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10319 &strict_overflow_p)))
10320 {
10321 if (strict_overflow_p)
10322 fold_overflow_warning (("assuming signed overflow does not occur "
10323 "when simplifying division"),
10324 WARN_STRICT_OVERFLOW_MISC);
10325 return fold_convert_loc (loc, type, tem);
10326 }
10327
10328 return NULL_TREE;
10329
10330 case CEIL_MOD_EXPR:
10331 case FLOOR_MOD_EXPR:
10332 case ROUND_MOD_EXPR:
10333 case TRUNC_MOD_EXPR:
10334 strict_overflow_p = false;
10335 if (TREE_CODE (arg1) == INTEGER_CST
10336 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10337 &strict_overflow_p)))
10338 {
10339 if (strict_overflow_p)
10340 fold_overflow_warning (("assuming signed overflow does not occur "
10341 "when simplifying modulus"),
10342 WARN_STRICT_OVERFLOW_MISC);
10343 return fold_convert_loc (loc, type, tem);
10344 }
10345
10346 return NULL_TREE;
10347
10348 case LROTATE_EXPR:
10349 case RROTATE_EXPR:
10350 case RSHIFT_EXPR:
10351 case LSHIFT_EXPR:
10352 /* Since negative shift count is not well-defined,
10353 don't try to compute it in the compiler. */
10354 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10355 return NULL_TREE;
10356
10357 prec = element_precision (type);
10358
10359 /* If we have a rotate of a bit operation with the rotate count and
10360 the second operand of the bit operation both constant,
10361 permute the two operations. */
10362 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10363 && (TREE_CODE (arg0) == BIT_AND_EXPR
10364 || TREE_CODE (arg0) == BIT_IOR_EXPR
10365 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10366 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10367 {
10368 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10369 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10370 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10371 fold_build2_loc (loc, code, type,
10372 arg00, arg1),
10373 fold_build2_loc (loc, code, type,
10374 arg01, arg1));
10375 }
10376
10377 /* Two consecutive rotates adding up to the some integer
10378 multiple of the precision of the type can be ignored. */
10379 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10380 && TREE_CODE (arg0) == RROTATE_EXPR
10381 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10382 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10383 prec) == 0)
10384 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10385
10386 return NULL_TREE;
10387
10388 case MIN_EXPR:
10389 case MAX_EXPR:
10390 goto associate;
10391
10392 case TRUTH_ANDIF_EXPR:
10393 /* Note that the operands of this must be ints
10394 and their values must be 0 or 1.
10395 ("true" is a fixed value perhaps depending on the language.) */
10396 /* If first arg is constant zero, return it. */
10397 if (integer_zerop (arg0))
10398 return fold_convert_loc (loc, type, arg0);
10399 /* FALLTHRU */
10400 case TRUTH_AND_EXPR:
10401 /* If either arg is constant true, drop it. */
10402 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10403 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10404 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10405 /* Preserve sequence points. */
10406 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10407 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10408 /* If second arg is constant zero, result is zero, but first arg
10409 must be evaluated. */
10410 if (integer_zerop (arg1))
10411 return omit_one_operand_loc (loc, type, arg1, arg0);
10412 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10413 case will be handled here. */
10414 if (integer_zerop (arg0))
10415 return omit_one_operand_loc (loc, type, arg0, arg1);
10416
10417 /* !X && X is always false. */
10418 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10419 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10420 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10421 /* X && !X is always false. */
10422 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10423 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10424 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10425
10426 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10427 means A >= Y && A != MAX, but in this case we know that
10428 A < X <= MAX. */
10429
10430 if (!TREE_SIDE_EFFECTS (arg0)
10431 && !TREE_SIDE_EFFECTS (arg1))
10432 {
10433 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10434 if (tem && !operand_equal_p (tem, arg0, 0))
10435 return fold_build2_loc (loc, code, type, tem, arg1);
10436
10437 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10438 if (tem && !operand_equal_p (tem, arg1, 0))
10439 return fold_build2_loc (loc, code, type, arg0, tem);
10440 }
10441
10442 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10443 != NULL_TREE)
10444 return tem;
10445
10446 return NULL_TREE;
10447
10448 case TRUTH_ORIF_EXPR:
10449 /* Note that the operands of this must be ints
10450 and their values must be 0 or true.
10451 ("true" is a fixed value perhaps depending on the language.) */
10452 /* If first arg is constant true, return it. */
10453 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10454 return fold_convert_loc (loc, type, arg0);
10455 /* FALLTHRU */
10456 case TRUTH_OR_EXPR:
10457 /* If either arg is constant zero, drop it. */
10458 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10459 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10460 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10461 /* Preserve sequence points. */
10462 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10463 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10464 /* If second arg is constant true, result is true, but we must
10465 evaluate first arg. */
10466 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10467 return omit_one_operand_loc (loc, type, arg1, arg0);
10468 /* Likewise for first arg, but note this only occurs here for
10469 TRUTH_OR_EXPR. */
10470 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10471 return omit_one_operand_loc (loc, type, arg0, arg1);
10472
10473 /* !X || X is always true. */
10474 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10475 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10476 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10477 /* X || !X is always true. */
10478 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10479 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10480 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10481
10482 /* (X && !Y) || (!X && Y) is X ^ Y */
10483 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10484 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10485 {
10486 tree a0, a1, l0, l1, n0, n1;
10487
10488 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10489 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10490
10491 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10492 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10493
10494 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10495 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10496
10497 if ((operand_equal_p (n0, a0, 0)
10498 && operand_equal_p (n1, a1, 0))
10499 || (operand_equal_p (n0, a1, 0)
10500 && operand_equal_p (n1, a0, 0)))
10501 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10502 }
10503
10504 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10505 != NULL_TREE)
10506 return tem;
10507
10508 return NULL_TREE;
10509
10510 case TRUTH_XOR_EXPR:
10511 /* If the second arg is constant zero, drop it. */
10512 if (integer_zerop (arg1))
10513 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10514 /* If the second arg is constant true, this is a logical inversion. */
10515 if (integer_onep (arg1))
10516 {
10517 tem = invert_truthvalue_loc (loc, arg0);
10518 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10519 }
10520 /* Identical arguments cancel to zero. */
10521 if (operand_equal_p (arg0, arg1, 0))
10522 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10523
10524 /* !X ^ X is always true. */
10525 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10526 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10527 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10528
10529 /* X ^ !X is always true. */
10530 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10531 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10532 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10533
10534 return NULL_TREE;
10535
10536 case EQ_EXPR:
10537 case NE_EXPR:
10538 STRIP_NOPS (arg0);
10539 STRIP_NOPS (arg1);
10540
10541 tem = fold_comparison (loc, code, type, op0, op1);
10542 if (tem != NULL_TREE)
10543 return tem;
10544
10545 /* bool_var != 1 becomes !bool_var. */
10546 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10547 && code == NE_EXPR)
10548 return fold_convert_loc (loc, type,
10549 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10550 TREE_TYPE (arg0), arg0));
10551
10552 /* bool_var == 0 becomes !bool_var. */
10553 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10554 && code == EQ_EXPR)
10555 return fold_convert_loc (loc, type,
10556 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10557 TREE_TYPE (arg0), arg0));
10558
10559 /* !exp != 0 becomes !exp */
10560 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10561 && code == NE_EXPR)
10562 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10563
10564 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10565 if ((TREE_CODE (arg0) == PLUS_EXPR
10566 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10567 || TREE_CODE (arg0) == MINUS_EXPR)
10568 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10569 0)),
10570 arg1, 0)
10571 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10572 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10573 {
10574 tree val = TREE_OPERAND (arg0, 1);
10575 val = fold_build2_loc (loc, code, type, val,
10576 build_int_cst (TREE_TYPE (val), 0));
10577 return omit_two_operands_loc (loc, type, val,
10578 TREE_OPERAND (arg0, 0), arg1);
10579 }
10580
10581 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10582 if ((TREE_CODE (arg1) == PLUS_EXPR
10583 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10584 || TREE_CODE (arg1) == MINUS_EXPR)
10585 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10586 0)),
10587 arg0, 0)
10588 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10589 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10590 {
10591 tree val = TREE_OPERAND (arg1, 1);
10592 val = fold_build2_loc (loc, code, type, val,
10593 build_int_cst (TREE_TYPE (val), 0));
10594 return omit_two_operands_loc (loc, type, val,
10595 TREE_OPERAND (arg1, 0), arg0);
10596 }
10597
10598 /* If this is an EQ or NE comparison with zero and ARG0 is
10599 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10600 two operations, but the latter can be done in one less insn
10601 on machines that have only two-operand insns or on which a
10602 constant cannot be the first operand. */
10603 if (TREE_CODE (arg0) == BIT_AND_EXPR
10604 && integer_zerop (arg1))
10605 {
10606 tree arg00 = TREE_OPERAND (arg0, 0);
10607 tree arg01 = TREE_OPERAND (arg0, 1);
10608 if (TREE_CODE (arg00) == LSHIFT_EXPR
10609 && integer_onep (TREE_OPERAND (arg00, 0)))
10610 {
10611 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10612 arg01, TREE_OPERAND (arg00, 1));
10613 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10614 build_int_cst (TREE_TYPE (arg0), 1));
10615 return fold_build2_loc (loc, code, type,
10616 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10617 arg1);
10618 }
10619 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10620 && integer_onep (TREE_OPERAND (arg01, 0)))
10621 {
10622 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10623 arg00, TREE_OPERAND (arg01, 1));
10624 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10625 build_int_cst (TREE_TYPE (arg0), 1));
10626 return fold_build2_loc (loc, code, type,
10627 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10628 arg1);
10629 }
10630 }
10631
10632 /* If this is an NE or EQ comparison of zero against the result of a
10633 signed MOD operation whose second operand is a power of 2, make
10634 the MOD operation unsigned since it is simpler and equivalent. */
10635 if (integer_zerop (arg1)
10636 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10637 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10638 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10639 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10640 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10641 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10642 {
10643 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10644 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10645 fold_convert_loc (loc, newtype,
10646 TREE_OPERAND (arg0, 0)),
10647 fold_convert_loc (loc, newtype,
10648 TREE_OPERAND (arg0, 1)));
10649
10650 return fold_build2_loc (loc, code, type, newmod,
10651 fold_convert_loc (loc, newtype, arg1));
10652 }
10653
10654 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10655 C1 is a valid shift constant, and C2 is a power of two, i.e.
10656 a single bit. */
10657 if (TREE_CODE (arg0) == BIT_AND_EXPR
10658 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10659 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10660 == INTEGER_CST
10661 && integer_pow2p (TREE_OPERAND (arg0, 1))
10662 && integer_zerop (arg1))
10663 {
10664 tree itype = TREE_TYPE (arg0);
10665 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10666 prec = TYPE_PRECISION (itype);
10667
10668 /* Check for a valid shift count. */
10669 if (wi::ltu_p (arg001, prec))
10670 {
10671 tree arg01 = TREE_OPERAND (arg0, 1);
10672 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10673 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10674 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10675 can be rewritten as (X & (C2 << C1)) != 0. */
10676 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10677 {
10678 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10679 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10680 return fold_build2_loc (loc, code, type, tem,
10681 fold_convert_loc (loc, itype, arg1));
10682 }
10683 /* Otherwise, for signed (arithmetic) shifts,
10684 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10685 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10686 else if (!TYPE_UNSIGNED (itype))
10687 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10688 arg000, build_int_cst (itype, 0));
10689 /* Otherwise, of unsigned (logical) shifts,
10690 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10691 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10692 else
10693 return omit_one_operand_loc (loc, type,
10694 code == EQ_EXPR ? integer_one_node
10695 : integer_zero_node,
10696 arg000);
10697 }
10698 }
10699
10700 /* If this is a comparison of a field, we may be able to simplify it. */
10701 if ((TREE_CODE (arg0) == COMPONENT_REF
10702 || TREE_CODE (arg0) == BIT_FIELD_REF)
10703 /* Handle the constant case even without -O
10704 to make sure the warnings are given. */
10705 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10706 {
10707 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10708 if (t1)
10709 return t1;
10710 }
10711
10712 /* Optimize comparisons of strlen vs zero to a compare of the
10713 first character of the string vs zero. To wit,
10714 strlen(ptr) == 0 => *ptr == 0
10715 strlen(ptr) != 0 => *ptr != 0
10716 Other cases should reduce to one of these two (or a constant)
10717 due to the return value of strlen being unsigned. */
10718 if (TREE_CODE (arg0) == CALL_EXPR
10719 && integer_zerop (arg1))
10720 {
10721 tree fndecl = get_callee_fndecl (arg0);
10722
10723 if (fndecl
10724 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10725 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10726 && call_expr_nargs (arg0) == 1
10727 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10728 {
10729 tree iref = build_fold_indirect_ref_loc (loc,
10730 CALL_EXPR_ARG (arg0, 0));
10731 return fold_build2_loc (loc, code, type, iref,
10732 build_int_cst (TREE_TYPE (iref), 0));
10733 }
10734 }
10735
10736 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10737 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10738 if (TREE_CODE (arg0) == RSHIFT_EXPR
10739 && integer_zerop (arg1)
10740 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10741 {
10742 tree arg00 = TREE_OPERAND (arg0, 0);
10743 tree arg01 = TREE_OPERAND (arg0, 1);
10744 tree itype = TREE_TYPE (arg00);
10745 if (wi::eq_p (arg01, element_precision (itype) - 1))
10746 {
10747 if (TYPE_UNSIGNED (itype))
10748 {
10749 itype = signed_type_for (itype);
10750 arg00 = fold_convert_loc (loc, itype, arg00);
10751 }
10752 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10753 type, arg00, build_zero_cst (itype));
10754 }
10755 }
10756
10757 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10758 (X & C) == 0 when C is a single bit. */
10759 if (TREE_CODE (arg0) == BIT_AND_EXPR
10760 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10761 && integer_zerop (arg1)
10762 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10763 {
10764 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10765 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10766 TREE_OPERAND (arg0, 1));
10767 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10768 type, tem,
10769 fold_convert_loc (loc, TREE_TYPE (arg0),
10770 arg1));
10771 }
10772
10773 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10774 constant C is a power of two, i.e. a single bit. */
10775 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10776 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10777 && integer_zerop (arg1)
10778 && integer_pow2p (TREE_OPERAND (arg0, 1))
10779 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10780 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10781 {
10782 tree arg00 = TREE_OPERAND (arg0, 0);
10783 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10784 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10785 }
10786
10787 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10788 when is C is a power of two, i.e. a single bit. */
10789 if (TREE_CODE (arg0) == BIT_AND_EXPR
10790 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10791 && integer_zerop (arg1)
10792 && integer_pow2p (TREE_OPERAND (arg0, 1))
10793 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10794 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10795 {
10796 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10797 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10798 arg000, TREE_OPERAND (arg0, 1));
10799 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10800 tem, build_int_cst (TREE_TYPE (tem), 0));
10801 }
10802
10803 if (integer_zerop (arg1)
10804 && tree_expr_nonzero_p (arg0))
10805 {
10806 tree res = constant_boolean_node (code==NE_EXPR, type);
10807 return omit_one_operand_loc (loc, type, res, arg0);
10808 }
10809
10810 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10811 if (TREE_CODE (arg0) == BIT_AND_EXPR
10812 && TREE_CODE (arg1) == BIT_AND_EXPR)
10813 {
10814 tree arg00 = TREE_OPERAND (arg0, 0);
10815 tree arg01 = TREE_OPERAND (arg0, 1);
10816 tree arg10 = TREE_OPERAND (arg1, 0);
10817 tree arg11 = TREE_OPERAND (arg1, 1);
10818 tree itype = TREE_TYPE (arg0);
10819
10820 if (operand_equal_p (arg01, arg11, 0))
10821 {
10822 tem = fold_convert_loc (loc, itype, arg10);
10823 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10824 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10825 return fold_build2_loc (loc, code, type, tem,
10826 build_zero_cst (itype));
10827 }
10828 if (operand_equal_p (arg01, arg10, 0))
10829 {
10830 tem = fold_convert_loc (loc, itype, arg11);
10831 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10832 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10833 return fold_build2_loc (loc, code, type, tem,
10834 build_zero_cst (itype));
10835 }
10836 if (operand_equal_p (arg00, arg11, 0))
10837 {
10838 tem = fold_convert_loc (loc, itype, arg10);
10839 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10840 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10841 return fold_build2_loc (loc, code, type, tem,
10842 build_zero_cst (itype));
10843 }
10844 if (operand_equal_p (arg00, arg10, 0))
10845 {
10846 tem = fold_convert_loc (loc, itype, arg11);
10847 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10848 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10849 return fold_build2_loc (loc, code, type, tem,
10850 build_zero_cst (itype));
10851 }
10852 }
10853
10854 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10855 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10856 {
10857 tree arg00 = TREE_OPERAND (arg0, 0);
10858 tree arg01 = TREE_OPERAND (arg0, 1);
10859 tree arg10 = TREE_OPERAND (arg1, 0);
10860 tree arg11 = TREE_OPERAND (arg1, 1);
10861 tree itype = TREE_TYPE (arg0);
10862
10863 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10864 operand_equal_p guarantees no side-effects so we don't need
10865 to use omit_one_operand on Z. */
10866 if (operand_equal_p (arg01, arg11, 0))
10867 return fold_build2_loc (loc, code, type, arg00,
10868 fold_convert_loc (loc, TREE_TYPE (arg00),
10869 arg10));
10870 if (operand_equal_p (arg01, arg10, 0))
10871 return fold_build2_loc (loc, code, type, arg00,
10872 fold_convert_loc (loc, TREE_TYPE (arg00),
10873 arg11));
10874 if (operand_equal_p (arg00, arg11, 0))
10875 return fold_build2_loc (loc, code, type, arg01,
10876 fold_convert_loc (loc, TREE_TYPE (arg01),
10877 arg10));
10878 if (operand_equal_p (arg00, arg10, 0))
10879 return fold_build2_loc (loc, code, type, arg01,
10880 fold_convert_loc (loc, TREE_TYPE (arg01),
10881 arg11));
10882
10883 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10884 if (TREE_CODE (arg01) == INTEGER_CST
10885 && TREE_CODE (arg11) == INTEGER_CST)
10886 {
10887 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10888 fold_convert_loc (loc, itype, arg11));
10889 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10890 return fold_build2_loc (loc, code, type, tem,
10891 fold_convert_loc (loc, itype, arg10));
10892 }
10893 }
10894
10895 /* Attempt to simplify equality/inequality comparisons of complex
10896 values. Only lower the comparison if the result is known or
10897 can be simplified to a single scalar comparison. */
10898 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10899 || TREE_CODE (arg0) == COMPLEX_CST)
10900 && (TREE_CODE (arg1) == COMPLEX_EXPR
10901 || TREE_CODE (arg1) == COMPLEX_CST))
10902 {
10903 tree real0, imag0, real1, imag1;
10904 tree rcond, icond;
10905
10906 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10907 {
10908 real0 = TREE_OPERAND (arg0, 0);
10909 imag0 = TREE_OPERAND (arg0, 1);
10910 }
10911 else
10912 {
10913 real0 = TREE_REALPART (arg0);
10914 imag0 = TREE_IMAGPART (arg0);
10915 }
10916
10917 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10918 {
10919 real1 = TREE_OPERAND (arg1, 0);
10920 imag1 = TREE_OPERAND (arg1, 1);
10921 }
10922 else
10923 {
10924 real1 = TREE_REALPART (arg1);
10925 imag1 = TREE_IMAGPART (arg1);
10926 }
10927
10928 rcond = fold_binary_loc (loc, code, type, real0, real1);
10929 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10930 {
10931 if (integer_zerop (rcond))
10932 {
10933 if (code == EQ_EXPR)
10934 return omit_two_operands_loc (loc, type, boolean_false_node,
10935 imag0, imag1);
10936 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10937 }
10938 else
10939 {
10940 if (code == NE_EXPR)
10941 return omit_two_operands_loc (loc, type, boolean_true_node,
10942 imag0, imag1);
10943 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10944 }
10945 }
10946
10947 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10948 if (icond && TREE_CODE (icond) == INTEGER_CST)
10949 {
10950 if (integer_zerop (icond))
10951 {
10952 if (code == EQ_EXPR)
10953 return omit_two_operands_loc (loc, type, boolean_false_node,
10954 real0, real1);
10955 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10956 }
10957 else
10958 {
10959 if (code == NE_EXPR)
10960 return omit_two_operands_loc (loc, type, boolean_true_node,
10961 real0, real1);
10962 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10963 }
10964 }
10965 }
10966
10967 return NULL_TREE;
10968
10969 case LT_EXPR:
10970 case GT_EXPR:
10971 case LE_EXPR:
10972 case GE_EXPR:
10973 tem = fold_comparison (loc, code, type, op0, op1);
10974 if (tem != NULL_TREE)
10975 return tem;
10976
10977 /* Transform comparisons of the form X +- C CMP X. */
10978 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10979 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10980 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10981 && !HONOR_SNANS (arg0))
10982 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10983 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
10984 {
10985 tree arg01 = TREE_OPERAND (arg0, 1);
10986 enum tree_code code0 = TREE_CODE (arg0);
10987 int is_positive;
10988
10989 if (TREE_CODE (arg01) == REAL_CST)
10990 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10991 else
10992 is_positive = tree_int_cst_sgn (arg01);
10993
10994 /* (X - c) > X becomes false. */
10995 if (code == GT_EXPR
10996 && ((code0 == MINUS_EXPR && is_positive >= 0)
10997 || (code0 == PLUS_EXPR && is_positive <= 0)))
10998 {
10999 if (TREE_CODE (arg01) == INTEGER_CST
11000 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11001 fold_overflow_warning (("assuming signed overflow does not "
11002 "occur when assuming that (X - c) > X "
11003 "is always false"),
11004 WARN_STRICT_OVERFLOW_ALL);
11005 return constant_boolean_node (0, type);
11006 }
11007
11008 /* Likewise (X + c) < X becomes false. */
11009 if (code == LT_EXPR
11010 && ((code0 == PLUS_EXPR && is_positive >= 0)
11011 || (code0 == MINUS_EXPR && is_positive <= 0)))
11012 {
11013 if (TREE_CODE (arg01) == INTEGER_CST
11014 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11015 fold_overflow_warning (("assuming signed overflow does not "
11016 "occur when assuming that "
11017 "(X + c) < X is always false"),
11018 WARN_STRICT_OVERFLOW_ALL);
11019 return constant_boolean_node (0, type);
11020 }
11021
11022 /* Convert (X - c) <= X to true. */
11023 if (!HONOR_NANS (arg1)
11024 && code == LE_EXPR
11025 && ((code0 == MINUS_EXPR && is_positive >= 0)
11026 || (code0 == PLUS_EXPR && is_positive <= 0)))
11027 {
11028 if (TREE_CODE (arg01) == INTEGER_CST
11029 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11030 fold_overflow_warning (("assuming signed overflow does not "
11031 "occur when assuming that "
11032 "(X - c) <= X is always true"),
11033 WARN_STRICT_OVERFLOW_ALL);
11034 return constant_boolean_node (1, type);
11035 }
11036
11037 /* Convert (X + c) >= X to true. */
11038 if (!HONOR_NANS (arg1)
11039 && code == GE_EXPR
11040 && ((code0 == PLUS_EXPR && is_positive >= 0)
11041 || (code0 == MINUS_EXPR && is_positive <= 0)))
11042 {
11043 if (TREE_CODE (arg01) == INTEGER_CST
11044 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11045 fold_overflow_warning (("assuming signed overflow does not "
11046 "occur when assuming that "
11047 "(X + c) >= X is always true"),
11048 WARN_STRICT_OVERFLOW_ALL);
11049 return constant_boolean_node (1, type);
11050 }
11051
11052 if (TREE_CODE (arg01) == INTEGER_CST)
11053 {
11054 /* Convert X + c > X and X - c < X to true for integers. */
11055 if (code == GT_EXPR
11056 && ((code0 == PLUS_EXPR && is_positive > 0)
11057 || (code0 == MINUS_EXPR && is_positive < 0)))
11058 {
11059 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11060 fold_overflow_warning (("assuming signed overflow does "
11061 "not occur when assuming that "
11062 "(X + c) > X is always true"),
11063 WARN_STRICT_OVERFLOW_ALL);
11064 return constant_boolean_node (1, type);
11065 }
11066
11067 if (code == LT_EXPR
11068 && ((code0 == MINUS_EXPR && is_positive > 0)
11069 || (code0 == PLUS_EXPR && is_positive < 0)))
11070 {
11071 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11072 fold_overflow_warning (("assuming signed overflow does "
11073 "not occur when assuming that "
11074 "(X - c) < X is always true"),
11075 WARN_STRICT_OVERFLOW_ALL);
11076 return constant_boolean_node (1, type);
11077 }
11078
11079 /* Convert X + c <= X and X - c >= X to false for integers. */
11080 if (code == LE_EXPR
11081 && ((code0 == PLUS_EXPR && is_positive > 0)
11082 || (code0 == MINUS_EXPR && is_positive < 0)))
11083 {
11084 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11085 fold_overflow_warning (("assuming signed overflow does "
11086 "not occur when assuming that "
11087 "(X + c) <= X is always false"),
11088 WARN_STRICT_OVERFLOW_ALL);
11089 return constant_boolean_node (0, type);
11090 }
11091
11092 if (code == GE_EXPR
11093 && ((code0 == MINUS_EXPR && is_positive > 0)
11094 || (code0 == PLUS_EXPR && is_positive < 0)))
11095 {
11096 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11097 fold_overflow_warning (("assuming signed overflow does "
11098 "not occur when assuming that "
11099 "(X - c) >= X is always false"),
11100 WARN_STRICT_OVERFLOW_ALL);
11101 return constant_boolean_node (0, type);
11102 }
11103 }
11104 }
11105
11106 /* If we are comparing an ABS_EXPR with a constant, we can
11107 convert all the cases into explicit comparisons, but they may
11108 well not be faster than doing the ABS and one comparison.
11109 But ABS (X) <= C is a range comparison, which becomes a subtraction
11110 and a comparison, and is probably faster. */
11111 if (code == LE_EXPR
11112 && TREE_CODE (arg1) == INTEGER_CST
11113 && TREE_CODE (arg0) == ABS_EXPR
11114 && ! TREE_SIDE_EFFECTS (arg0)
11115 && (0 != (tem = negate_expr (arg1)))
11116 && TREE_CODE (tem) == INTEGER_CST
11117 && !TREE_OVERFLOW (tem))
11118 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11119 build2 (GE_EXPR, type,
11120 TREE_OPERAND (arg0, 0), tem),
11121 build2 (LE_EXPR, type,
11122 TREE_OPERAND (arg0, 0), arg1));
11123
11124 /* Convert ABS_EXPR<x> >= 0 to true. */
11125 strict_overflow_p = false;
11126 if (code == GE_EXPR
11127 && (integer_zerop (arg1)
11128 || (! HONOR_NANS (arg0)
11129 && real_zerop (arg1)))
11130 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11131 {
11132 if (strict_overflow_p)
11133 fold_overflow_warning (("assuming signed overflow does not occur "
11134 "when simplifying comparison of "
11135 "absolute value and zero"),
11136 WARN_STRICT_OVERFLOW_CONDITIONAL);
11137 return omit_one_operand_loc (loc, type,
11138 constant_boolean_node (true, type),
11139 arg0);
11140 }
11141
11142 /* Convert ABS_EXPR<x> < 0 to false. */
11143 strict_overflow_p = false;
11144 if (code == LT_EXPR
11145 && (integer_zerop (arg1) || real_zerop (arg1))
11146 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11147 {
11148 if (strict_overflow_p)
11149 fold_overflow_warning (("assuming signed overflow does not occur "
11150 "when simplifying comparison of "
11151 "absolute value and zero"),
11152 WARN_STRICT_OVERFLOW_CONDITIONAL);
11153 return omit_one_operand_loc (loc, type,
11154 constant_boolean_node (false, type),
11155 arg0);
11156 }
11157
11158 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11159 and similarly for >= into !=. */
11160 if ((code == LT_EXPR || code == GE_EXPR)
11161 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11162 && TREE_CODE (arg1) == LSHIFT_EXPR
11163 && integer_onep (TREE_OPERAND (arg1, 0)))
11164 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11165 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11166 TREE_OPERAND (arg1, 1)),
11167 build_zero_cst (TREE_TYPE (arg0)));
11168
11169 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11170 otherwise Y might be >= # of bits in X's type and thus e.g.
11171 (unsigned char) (1 << Y) for Y 15 might be 0.
11172 If the cast is widening, then 1 << Y should have unsigned type,
11173 otherwise if Y is number of bits in the signed shift type minus 1,
11174 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11175 31 might be 0xffffffff80000000. */
11176 if ((code == LT_EXPR || code == GE_EXPR)
11177 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11178 && CONVERT_EXPR_P (arg1)
11179 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11180 && (element_precision (TREE_TYPE (arg1))
11181 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11182 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11183 || (element_precision (TREE_TYPE (arg1))
11184 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11185 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11186 {
11187 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11188 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11189 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11190 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11191 build_zero_cst (TREE_TYPE (arg0)));
11192 }
11193
11194 return NULL_TREE;
11195
11196 case UNORDERED_EXPR:
11197 case ORDERED_EXPR:
11198 case UNLT_EXPR:
11199 case UNLE_EXPR:
11200 case UNGT_EXPR:
11201 case UNGE_EXPR:
11202 case UNEQ_EXPR:
11203 case LTGT_EXPR:
11204 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11205 {
11206 tree targ0 = strip_float_extensions (arg0);
11207 tree targ1 = strip_float_extensions (arg1);
11208 tree newtype = TREE_TYPE (targ0);
11209
11210 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11211 newtype = TREE_TYPE (targ1);
11212
11213 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11214 return fold_build2_loc (loc, code, type,
11215 fold_convert_loc (loc, newtype, targ0),
11216 fold_convert_loc (loc, newtype, targ1));
11217 }
11218
11219 return NULL_TREE;
11220
11221 case COMPOUND_EXPR:
11222 /* When pedantic, a compound expression can be neither an lvalue
11223 nor an integer constant expression. */
11224 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11225 return NULL_TREE;
11226 /* Don't let (0, 0) be null pointer constant. */
11227 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11228 : fold_convert_loc (loc, type, arg1);
11229 return pedantic_non_lvalue_loc (loc, tem);
11230
11231 case ASSERT_EXPR:
11232 /* An ASSERT_EXPR should never be passed to fold_binary. */
11233 gcc_unreachable ();
11234
11235 default:
11236 return NULL_TREE;
11237 } /* switch (code) */
11238 }
11239
11240 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11241 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11242 of GOTO_EXPR. */
11243
11244 static tree
11245 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11246 {
11247 switch (TREE_CODE (*tp))
11248 {
11249 case LABEL_EXPR:
11250 return *tp;
11251
11252 case GOTO_EXPR:
11253 *walk_subtrees = 0;
11254
11255 /* fall through */
11256
11257 default:
11258 return NULL_TREE;
11259 }
11260 }
11261
11262 /* Return whether the sub-tree ST contains a label which is accessible from
11263 outside the sub-tree. */
11264
11265 static bool
11266 contains_label_p (tree st)
11267 {
11268 return
11269 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11270 }
11271
11272 /* Fold a ternary expression of code CODE and type TYPE with operands
11273 OP0, OP1, and OP2. Return the folded expression if folding is
11274 successful. Otherwise, return NULL_TREE. */
11275
11276 tree
11277 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11278 tree op0, tree op1, tree op2)
11279 {
11280 tree tem;
11281 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11282 enum tree_code_class kind = TREE_CODE_CLASS (code);
11283
11284 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11285 && TREE_CODE_LENGTH (code) == 3);
11286
11287 /* If this is a commutative operation, and OP0 is a constant, move it
11288 to OP1 to reduce the number of tests below. */
11289 if (commutative_ternary_tree_code (code)
11290 && tree_swap_operands_p (op0, op1))
11291 return fold_build3_loc (loc, code, type, op1, op0, op2);
11292
11293 tem = generic_simplify (loc, code, type, op0, op1, op2);
11294 if (tem)
11295 return tem;
11296
11297 /* Strip any conversions that don't change the mode. This is safe
11298 for every expression, except for a comparison expression because
11299 its signedness is derived from its operands. So, in the latter
11300 case, only strip conversions that don't change the signedness.
11301
11302 Note that this is done as an internal manipulation within the
11303 constant folder, in order to find the simplest representation of
11304 the arguments so that their form can be studied. In any cases,
11305 the appropriate type conversions should be put back in the tree
11306 that will get out of the constant folder. */
11307 if (op0)
11308 {
11309 arg0 = op0;
11310 STRIP_NOPS (arg0);
11311 }
11312
11313 if (op1)
11314 {
11315 arg1 = op1;
11316 STRIP_NOPS (arg1);
11317 }
11318
11319 if (op2)
11320 {
11321 arg2 = op2;
11322 STRIP_NOPS (arg2);
11323 }
11324
11325 switch (code)
11326 {
11327 case COMPONENT_REF:
11328 if (TREE_CODE (arg0) == CONSTRUCTOR
11329 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11330 {
11331 unsigned HOST_WIDE_INT idx;
11332 tree field, value;
11333 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11334 if (field == arg1)
11335 return value;
11336 }
11337 return NULL_TREE;
11338
11339 case COND_EXPR:
11340 case VEC_COND_EXPR:
11341 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11342 so all simple results must be passed through pedantic_non_lvalue. */
11343 if (TREE_CODE (arg0) == INTEGER_CST)
11344 {
11345 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11346 tem = integer_zerop (arg0) ? op2 : op1;
11347 /* Only optimize constant conditions when the selected branch
11348 has the same type as the COND_EXPR. This avoids optimizing
11349 away "c ? x : throw", where the throw has a void type.
11350 Avoid throwing away that operand which contains label. */
11351 if ((!TREE_SIDE_EFFECTS (unused_op)
11352 || !contains_label_p (unused_op))
11353 && (! VOID_TYPE_P (TREE_TYPE (tem))
11354 || VOID_TYPE_P (type)))
11355 return pedantic_non_lvalue_loc (loc, tem);
11356 return NULL_TREE;
11357 }
11358 else if (TREE_CODE (arg0) == VECTOR_CST)
11359 {
11360 if ((TREE_CODE (arg1) == VECTOR_CST
11361 || TREE_CODE (arg1) == CONSTRUCTOR)
11362 && (TREE_CODE (arg2) == VECTOR_CST
11363 || TREE_CODE (arg2) == CONSTRUCTOR))
11364 {
11365 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11366 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11367 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11368 for (i = 0; i < nelts; i++)
11369 {
11370 tree val = VECTOR_CST_ELT (arg0, i);
11371 if (integer_all_onesp (val))
11372 sel[i] = i;
11373 else if (integer_zerop (val))
11374 sel[i] = nelts + i;
11375 else /* Currently unreachable. */
11376 return NULL_TREE;
11377 }
11378 tree t = fold_vec_perm (type, arg1, arg2, sel);
11379 if (t != NULL_TREE)
11380 return t;
11381 }
11382 }
11383
11384 /* If we have A op B ? A : C, we may be able to convert this to a
11385 simpler expression, depending on the operation and the values
11386 of B and C. Signed zeros prevent all of these transformations,
11387 for reasons given above each one.
11388
11389 Also try swapping the arguments and inverting the conditional. */
11390 if (COMPARISON_CLASS_P (arg0)
11391 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11392 arg1, TREE_OPERAND (arg0, 1))
11393 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11394 {
11395 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11396 if (tem)
11397 return tem;
11398 }
11399
11400 if (COMPARISON_CLASS_P (arg0)
11401 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11402 op2,
11403 TREE_OPERAND (arg0, 1))
11404 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11405 {
11406 location_t loc0 = expr_location_or (arg0, loc);
11407 tem = fold_invert_truthvalue (loc0, arg0);
11408 if (tem && COMPARISON_CLASS_P (tem))
11409 {
11410 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11411 if (tem)
11412 return tem;
11413 }
11414 }
11415
11416 /* If the second operand is simpler than the third, swap them
11417 since that produces better jump optimization results. */
11418 if (truth_value_p (TREE_CODE (arg0))
11419 && tree_swap_operands_p (op1, op2))
11420 {
11421 location_t loc0 = expr_location_or (arg0, loc);
11422 /* See if this can be inverted. If it can't, possibly because
11423 it was a floating-point inequality comparison, don't do
11424 anything. */
11425 tem = fold_invert_truthvalue (loc0, arg0);
11426 if (tem)
11427 return fold_build3_loc (loc, code, type, tem, op2, op1);
11428 }
11429
11430 /* Convert A ? 1 : 0 to simply A. */
11431 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11432 : (integer_onep (op1)
11433 && !VECTOR_TYPE_P (type)))
11434 && integer_zerop (op2)
11435 /* If we try to convert OP0 to our type, the
11436 call to fold will try to move the conversion inside
11437 a COND, which will recurse. In that case, the COND_EXPR
11438 is probably the best choice, so leave it alone. */
11439 && type == TREE_TYPE (arg0))
11440 return pedantic_non_lvalue_loc (loc, arg0);
11441
11442 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11443 over COND_EXPR in cases such as floating point comparisons. */
11444 if (integer_zerop (op1)
11445 && code == COND_EXPR
11446 && integer_onep (op2)
11447 && !VECTOR_TYPE_P (type)
11448 && truth_value_p (TREE_CODE (arg0)))
11449 return pedantic_non_lvalue_loc (loc,
11450 fold_convert_loc (loc, type,
11451 invert_truthvalue_loc (loc,
11452 arg0)));
11453
11454 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11455 if (TREE_CODE (arg0) == LT_EXPR
11456 && integer_zerop (TREE_OPERAND (arg0, 1))
11457 && integer_zerop (op2)
11458 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11459 {
11460 /* sign_bit_p looks through both zero and sign extensions,
11461 but for this optimization only sign extensions are
11462 usable. */
11463 tree tem2 = TREE_OPERAND (arg0, 0);
11464 while (tem != tem2)
11465 {
11466 if (TREE_CODE (tem2) != NOP_EXPR
11467 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11468 {
11469 tem = NULL_TREE;
11470 break;
11471 }
11472 tem2 = TREE_OPERAND (tem2, 0);
11473 }
11474 /* sign_bit_p only checks ARG1 bits within A's precision.
11475 If <sign bit of A> has wider type than A, bits outside
11476 of A's precision in <sign bit of A> need to be checked.
11477 If they are all 0, this optimization needs to be done
11478 in unsigned A's type, if they are all 1 in signed A's type,
11479 otherwise this can't be done. */
11480 if (tem
11481 && TYPE_PRECISION (TREE_TYPE (tem))
11482 < TYPE_PRECISION (TREE_TYPE (arg1))
11483 && TYPE_PRECISION (TREE_TYPE (tem))
11484 < TYPE_PRECISION (type))
11485 {
11486 int inner_width, outer_width;
11487 tree tem_type;
11488
11489 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11490 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11491 if (outer_width > TYPE_PRECISION (type))
11492 outer_width = TYPE_PRECISION (type);
11493
11494 wide_int mask = wi::shifted_mask
11495 (inner_width, outer_width - inner_width, false,
11496 TYPE_PRECISION (TREE_TYPE (arg1)));
11497
11498 wide_int common = mask & arg1;
11499 if (common == mask)
11500 {
11501 tem_type = signed_type_for (TREE_TYPE (tem));
11502 tem = fold_convert_loc (loc, tem_type, tem);
11503 }
11504 else if (common == 0)
11505 {
11506 tem_type = unsigned_type_for (TREE_TYPE (tem));
11507 tem = fold_convert_loc (loc, tem_type, tem);
11508 }
11509 else
11510 tem = NULL;
11511 }
11512
11513 if (tem)
11514 return
11515 fold_convert_loc (loc, type,
11516 fold_build2_loc (loc, BIT_AND_EXPR,
11517 TREE_TYPE (tem), tem,
11518 fold_convert_loc (loc,
11519 TREE_TYPE (tem),
11520 arg1)));
11521 }
11522
11523 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11524 already handled above. */
11525 if (TREE_CODE (arg0) == BIT_AND_EXPR
11526 && integer_onep (TREE_OPERAND (arg0, 1))
11527 && integer_zerop (op2)
11528 && integer_pow2p (arg1))
11529 {
11530 tree tem = TREE_OPERAND (arg0, 0);
11531 STRIP_NOPS (tem);
11532 if (TREE_CODE (tem) == RSHIFT_EXPR
11533 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11534 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11535 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11536 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11537 fold_convert_loc (loc, type,
11538 TREE_OPERAND (tem, 0)),
11539 op1);
11540 }
11541
11542 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11543 is probably obsolete because the first operand should be a
11544 truth value (that's why we have the two cases above), but let's
11545 leave it in until we can confirm this for all front-ends. */
11546 if (integer_zerop (op2)
11547 && TREE_CODE (arg0) == NE_EXPR
11548 && integer_zerop (TREE_OPERAND (arg0, 1))
11549 && integer_pow2p (arg1)
11550 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11551 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11552 arg1, OEP_ONLY_CONST))
11553 return pedantic_non_lvalue_loc (loc,
11554 fold_convert_loc (loc, type,
11555 TREE_OPERAND (arg0, 0)));
11556
11557 /* Disable the transformations below for vectors, since
11558 fold_binary_op_with_conditional_arg may undo them immediately,
11559 yielding an infinite loop. */
11560 if (code == VEC_COND_EXPR)
11561 return NULL_TREE;
11562
11563 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11564 if (integer_zerop (op2)
11565 && truth_value_p (TREE_CODE (arg0))
11566 && truth_value_p (TREE_CODE (arg1))
11567 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11568 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11569 : TRUTH_ANDIF_EXPR,
11570 type, fold_convert_loc (loc, type, arg0), op1);
11571
11572 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11573 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11574 && truth_value_p (TREE_CODE (arg0))
11575 && truth_value_p (TREE_CODE (arg1))
11576 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11577 {
11578 location_t loc0 = expr_location_or (arg0, loc);
11579 /* Only perform transformation if ARG0 is easily inverted. */
11580 tem = fold_invert_truthvalue (loc0, arg0);
11581 if (tem)
11582 return fold_build2_loc (loc, code == VEC_COND_EXPR
11583 ? BIT_IOR_EXPR
11584 : TRUTH_ORIF_EXPR,
11585 type, fold_convert_loc (loc, type, tem),
11586 op1);
11587 }
11588
11589 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11590 if (integer_zerop (arg1)
11591 && truth_value_p (TREE_CODE (arg0))
11592 && truth_value_p (TREE_CODE (op2))
11593 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11594 {
11595 location_t loc0 = expr_location_or (arg0, loc);
11596 /* Only perform transformation if ARG0 is easily inverted. */
11597 tem = fold_invert_truthvalue (loc0, arg0);
11598 if (tem)
11599 return fold_build2_loc (loc, code == VEC_COND_EXPR
11600 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11601 type, fold_convert_loc (loc, type, tem),
11602 op2);
11603 }
11604
11605 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11606 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11607 && truth_value_p (TREE_CODE (arg0))
11608 && truth_value_p (TREE_CODE (op2))
11609 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11610 return fold_build2_loc (loc, code == VEC_COND_EXPR
11611 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11612 type, fold_convert_loc (loc, type, arg0), op2);
11613
11614 return NULL_TREE;
11615
11616 case CALL_EXPR:
11617 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11618 of fold_ternary on them. */
11619 gcc_unreachable ();
11620
11621 case BIT_FIELD_REF:
11622 if (TREE_CODE (arg0) == VECTOR_CST
11623 && (type == TREE_TYPE (TREE_TYPE (arg0))
11624 || (TREE_CODE (type) == VECTOR_TYPE
11625 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11626 {
11627 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11628 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11629 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11630 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11631
11632 if (n != 0
11633 && (idx % width) == 0
11634 && (n % width) == 0
11635 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11636 {
11637 idx = idx / width;
11638 n = n / width;
11639
11640 if (TREE_CODE (arg0) == VECTOR_CST)
11641 {
11642 if (n == 1)
11643 return VECTOR_CST_ELT (arg0, idx);
11644
11645 tree *vals = XALLOCAVEC (tree, n);
11646 for (unsigned i = 0; i < n; ++i)
11647 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11648 return build_vector (type, vals);
11649 }
11650 }
11651 }
11652
11653 /* On constants we can use native encode/interpret to constant
11654 fold (nearly) all BIT_FIELD_REFs. */
11655 if (CONSTANT_CLASS_P (arg0)
11656 && can_native_interpret_type_p (type)
11657 && BITS_PER_UNIT == 8)
11658 {
11659 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11660 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11661 /* Limit us to a reasonable amount of work. To relax the
11662 other limitations we need bit-shifting of the buffer
11663 and rounding up the size. */
11664 if (bitpos % BITS_PER_UNIT == 0
11665 && bitsize % BITS_PER_UNIT == 0
11666 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11667 {
11668 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11669 unsigned HOST_WIDE_INT len
11670 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11671 bitpos / BITS_PER_UNIT);
11672 if (len > 0
11673 && len * BITS_PER_UNIT >= bitsize)
11674 {
11675 tree v = native_interpret_expr (type, b,
11676 bitsize / BITS_PER_UNIT);
11677 if (v)
11678 return v;
11679 }
11680 }
11681 }
11682
11683 return NULL_TREE;
11684
11685 case FMA_EXPR:
11686 /* For integers we can decompose the FMA if possible. */
11687 if (TREE_CODE (arg0) == INTEGER_CST
11688 && TREE_CODE (arg1) == INTEGER_CST)
11689 return fold_build2_loc (loc, PLUS_EXPR, type,
11690 const_binop (MULT_EXPR, arg0, arg1), arg2);
11691 if (integer_zerop (arg2))
11692 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11693
11694 return fold_fma (loc, type, arg0, arg1, arg2);
11695
11696 case VEC_PERM_EXPR:
11697 if (TREE_CODE (arg2) == VECTOR_CST)
11698 {
11699 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11700 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11701 unsigned char *sel2 = sel + nelts;
11702 bool need_mask_canon = false;
11703 bool need_mask_canon2 = false;
11704 bool all_in_vec0 = true;
11705 bool all_in_vec1 = true;
11706 bool maybe_identity = true;
11707 bool single_arg = (op0 == op1);
11708 bool changed = false;
11709
11710 mask2 = 2 * nelts - 1;
11711 mask = single_arg ? (nelts - 1) : mask2;
11712 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11713 for (i = 0; i < nelts; i++)
11714 {
11715 tree val = VECTOR_CST_ELT (arg2, i);
11716 if (TREE_CODE (val) != INTEGER_CST)
11717 return NULL_TREE;
11718
11719 /* Make sure that the perm value is in an acceptable
11720 range. */
11721 wide_int t = val;
11722 need_mask_canon |= wi::gtu_p (t, mask);
11723 need_mask_canon2 |= wi::gtu_p (t, mask2);
11724 sel[i] = t.to_uhwi () & mask;
11725 sel2[i] = t.to_uhwi () & mask2;
11726
11727 if (sel[i] < nelts)
11728 all_in_vec1 = false;
11729 else
11730 all_in_vec0 = false;
11731
11732 if ((sel[i] & (nelts-1)) != i)
11733 maybe_identity = false;
11734 }
11735
11736 if (maybe_identity)
11737 {
11738 if (all_in_vec0)
11739 return op0;
11740 if (all_in_vec1)
11741 return op1;
11742 }
11743
11744 if (all_in_vec0)
11745 op1 = op0;
11746 else if (all_in_vec1)
11747 {
11748 op0 = op1;
11749 for (i = 0; i < nelts; i++)
11750 sel[i] -= nelts;
11751 need_mask_canon = true;
11752 }
11753
11754 if ((TREE_CODE (op0) == VECTOR_CST
11755 || TREE_CODE (op0) == CONSTRUCTOR)
11756 && (TREE_CODE (op1) == VECTOR_CST
11757 || TREE_CODE (op1) == CONSTRUCTOR))
11758 {
11759 tree t = fold_vec_perm (type, op0, op1, sel);
11760 if (t != NULL_TREE)
11761 return t;
11762 }
11763
11764 if (op0 == op1 && !single_arg)
11765 changed = true;
11766
11767 /* Some targets are deficient and fail to expand a single
11768 argument permutation while still allowing an equivalent
11769 2-argument version. */
11770 if (need_mask_canon && arg2 == op2
11771 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11772 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11773 {
11774 need_mask_canon = need_mask_canon2;
11775 sel = sel2;
11776 }
11777
11778 if (need_mask_canon && arg2 == op2)
11779 {
11780 tree *tsel = XALLOCAVEC (tree, nelts);
11781 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11782 for (i = 0; i < nelts; i++)
11783 tsel[i] = build_int_cst (eltype, sel[i]);
11784 op2 = build_vector (TREE_TYPE (arg2), tsel);
11785 changed = true;
11786 }
11787
11788 if (changed)
11789 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11790 }
11791 return NULL_TREE;
11792
11793 case BIT_INSERT_EXPR:
11794 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11795 if (TREE_CODE (arg0) == INTEGER_CST
11796 && TREE_CODE (arg1) == INTEGER_CST)
11797 {
11798 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11799 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11800 wide_int tem = wi::bit_and (arg0,
11801 wi::shifted_mask (bitpos, bitsize, true,
11802 TYPE_PRECISION (type)));
11803 wide_int tem2
11804 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11805 bitsize), bitpos);
11806 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11807 }
11808 else if (TREE_CODE (arg0) == VECTOR_CST
11809 && CONSTANT_CLASS_P (arg1)
11810 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11811 TREE_TYPE (arg1)))
11812 {
11813 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11814 unsigned HOST_WIDE_INT elsize
11815 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11816 if (bitpos % elsize == 0)
11817 {
11818 unsigned k = bitpos / elsize;
11819 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11820 return arg0;
11821 else
11822 {
11823 tree *elts = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
11824 memcpy (elts, VECTOR_CST_ELTS (arg0),
11825 sizeof (tree) * TYPE_VECTOR_SUBPARTS (type));
11826 elts[k] = arg1;
11827 return build_vector (type, elts);
11828 }
11829 }
11830 }
11831 return NULL_TREE;
11832
11833 default:
11834 return NULL_TREE;
11835 } /* switch (code) */
11836 }
11837
11838 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11839 of an array (or vector). */
11840
11841 tree
11842 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11843 {
11844 tree index_type = NULL_TREE;
11845 offset_int low_bound = 0;
11846
11847 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11848 {
11849 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11850 if (domain_type && TYPE_MIN_VALUE (domain_type))
11851 {
11852 /* Static constructors for variably sized objects makes no sense. */
11853 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11854 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11855 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11856 }
11857 }
11858
11859 if (index_type)
11860 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11861 TYPE_SIGN (index_type));
11862
11863 offset_int index = low_bound - 1;
11864 if (index_type)
11865 index = wi::ext (index, TYPE_PRECISION (index_type),
11866 TYPE_SIGN (index_type));
11867
11868 offset_int max_index;
11869 unsigned HOST_WIDE_INT cnt;
11870 tree cfield, cval;
11871
11872 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11873 {
11874 /* Array constructor might explicitly set index, or specify a range,
11875 or leave index NULL meaning that it is next index after previous
11876 one. */
11877 if (cfield)
11878 {
11879 if (TREE_CODE (cfield) == INTEGER_CST)
11880 max_index = index = wi::to_offset (cfield);
11881 else
11882 {
11883 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11884 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11885 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11886 }
11887 }
11888 else
11889 {
11890 index += 1;
11891 if (index_type)
11892 index = wi::ext (index, TYPE_PRECISION (index_type),
11893 TYPE_SIGN (index_type));
11894 max_index = index;
11895 }
11896
11897 /* Do we have match? */
11898 if (wi::cmpu (access_index, index) >= 0
11899 && wi::cmpu (access_index, max_index) <= 0)
11900 return cval;
11901 }
11902 return NULL_TREE;
11903 }
11904
11905 /* Perform constant folding and related simplification of EXPR.
11906 The related simplifications include x*1 => x, x*0 => 0, etc.,
11907 and application of the associative law.
11908 NOP_EXPR conversions may be removed freely (as long as we
11909 are careful not to change the type of the overall expression).
11910 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11911 but we can constant-fold them if they have constant operands. */
11912
11913 #ifdef ENABLE_FOLD_CHECKING
11914 # define fold(x) fold_1 (x)
11915 static tree fold_1 (tree);
11916 static
11917 #endif
11918 tree
11919 fold (tree expr)
11920 {
11921 const tree t = expr;
11922 enum tree_code code = TREE_CODE (t);
11923 enum tree_code_class kind = TREE_CODE_CLASS (code);
11924 tree tem;
11925 location_t loc = EXPR_LOCATION (expr);
11926
11927 /* Return right away if a constant. */
11928 if (kind == tcc_constant)
11929 return t;
11930
11931 /* CALL_EXPR-like objects with variable numbers of operands are
11932 treated specially. */
11933 if (kind == tcc_vl_exp)
11934 {
11935 if (code == CALL_EXPR)
11936 {
11937 tem = fold_call_expr (loc, expr, false);
11938 return tem ? tem : expr;
11939 }
11940 return expr;
11941 }
11942
11943 if (IS_EXPR_CODE_CLASS (kind))
11944 {
11945 tree type = TREE_TYPE (t);
11946 tree op0, op1, op2;
11947
11948 switch (TREE_CODE_LENGTH (code))
11949 {
11950 case 1:
11951 op0 = TREE_OPERAND (t, 0);
11952 tem = fold_unary_loc (loc, code, type, op0);
11953 return tem ? tem : expr;
11954 case 2:
11955 op0 = TREE_OPERAND (t, 0);
11956 op1 = TREE_OPERAND (t, 1);
11957 tem = fold_binary_loc (loc, code, type, op0, op1);
11958 return tem ? tem : expr;
11959 case 3:
11960 op0 = TREE_OPERAND (t, 0);
11961 op1 = TREE_OPERAND (t, 1);
11962 op2 = TREE_OPERAND (t, 2);
11963 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11964 return tem ? tem : expr;
11965 default:
11966 break;
11967 }
11968 }
11969
11970 switch (code)
11971 {
11972 case ARRAY_REF:
11973 {
11974 tree op0 = TREE_OPERAND (t, 0);
11975 tree op1 = TREE_OPERAND (t, 1);
11976
11977 if (TREE_CODE (op1) == INTEGER_CST
11978 && TREE_CODE (op0) == CONSTRUCTOR
11979 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11980 {
11981 tree val = get_array_ctor_element_at_index (op0,
11982 wi::to_offset (op1));
11983 if (val)
11984 return val;
11985 }
11986
11987 return t;
11988 }
11989
11990 /* Return a VECTOR_CST if possible. */
11991 case CONSTRUCTOR:
11992 {
11993 tree type = TREE_TYPE (t);
11994 if (TREE_CODE (type) != VECTOR_TYPE)
11995 return t;
11996
11997 unsigned i;
11998 tree val;
11999 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12000 if (! CONSTANT_CLASS_P (val))
12001 return t;
12002
12003 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12004 }
12005
12006 case CONST_DECL:
12007 return fold (DECL_INITIAL (t));
12008
12009 default:
12010 return t;
12011 } /* switch (code) */
12012 }
12013
12014 #ifdef ENABLE_FOLD_CHECKING
12015 #undef fold
12016
12017 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12018 hash_table<nofree_ptr_hash<const tree_node> > *);
12019 static void fold_check_failed (const_tree, const_tree);
12020 void print_fold_checksum (const_tree);
12021
12022 /* When --enable-checking=fold, compute a digest of expr before
12023 and after actual fold call to see if fold did not accidentally
12024 change original expr. */
12025
12026 tree
12027 fold (tree expr)
12028 {
12029 tree ret;
12030 struct md5_ctx ctx;
12031 unsigned char checksum_before[16], checksum_after[16];
12032 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12033
12034 md5_init_ctx (&ctx);
12035 fold_checksum_tree (expr, &ctx, &ht);
12036 md5_finish_ctx (&ctx, checksum_before);
12037 ht.empty ();
12038
12039 ret = fold_1 (expr);
12040
12041 md5_init_ctx (&ctx);
12042 fold_checksum_tree (expr, &ctx, &ht);
12043 md5_finish_ctx (&ctx, checksum_after);
12044
12045 if (memcmp (checksum_before, checksum_after, 16))
12046 fold_check_failed (expr, ret);
12047
12048 return ret;
12049 }
12050
12051 void
12052 print_fold_checksum (const_tree expr)
12053 {
12054 struct md5_ctx ctx;
12055 unsigned char checksum[16], cnt;
12056 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12057
12058 md5_init_ctx (&ctx);
12059 fold_checksum_tree (expr, &ctx, &ht);
12060 md5_finish_ctx (&ctx, checksum);
12061 for (cnt = 0; cnt < 16; ++cnt)
12062 fprintf (stderr, "%02x", checksum[cnt]);
12063 putc ('\n', stderr);
12064 }
12065
12066 static void
12067 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12068 {
12069 internal_error ("fold check: original tree changed by fold");
12070 }
12071
12072 static void
12073 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12074 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12075 {
12076 const tree_node **slot;
12077 enum tree_code code;
12078 union tree_node buf;
12079 int i, len;
12080
12081 recursive_label:
12082 if (expr == NULL)
12083 return;
12084 slot = ht->find_slot (expr, INSERT);
12085 if (*slot != NULL)
12086 return;
12087 *slot = expr;
12088 code = TREE_CODE (expr);
12089 if (TREE_CODE_CLASS (code) == tcc_declaration
12090 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12091 {
12092 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12093 memcpy ((char *) &buf, expr, tree_size (expr));
12094 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12095 buf.decl_with_vis.symtab_node = NULL;
12096 expr = (tree) &buf;
12097 }
12098 else if (TREE_CODE_CLASS (code) == tcc_type
12099 && (TYPE_POINTER_TO (expr)
12100 || TYPE_REFERENCE_TO (expr)
12101 || TYPE_CACHED_VALUES_P (expr)
12102 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12103 || TYPE_NEXT_VARIANT (expr)
12104 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12105 {
12106 /* Allow these fields to be modified. */
12107 tree tmp;
12108 memcpy ((char *) &buf, expr, tree_size (expr));
12109 expr = tmp = (tree) &buf;
12110 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12111 TYPE_POINTER_TO (tmp) = NULL;
12112 TYPE_REFERENCE_TO (tmp) = NULL;
12113 TYPE_NEXT_VARIANT (tmp) = NULL;
12114 TYPE_ALIAS_SET (tmp) = -1;
12115 if (TYPE_CACHED_VALUES_P (tmp))
12116 {
12117 TYPE_CACHED_VALUES_P (tmp) = 0;
12118 TYPE_CACHED_VALUES (tmp) = NULL;
12119 }
12120 }
12121 md5_process_bytes (expr, tree_size (expr), ctx);
12122 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12123 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12124 if (TREE_CODE_CLASS (code) != tcc_type
12125 && TREE_CODE_CLASS (code) != tcc_declaration
12126 && code != TREE_LIST
12127 && code != SSA_NAME
12128 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12129 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12130 switch (TREE_CODE_CLASS (code))
12131 {
12132 case tcc_constant:
12133 switch (code)
12134 {
12135 case STRING_CST:
12136 md5_process_bytes (TREE_STRING_POINTER (expr),
12137 TREE_STRING_LENGTH (expr), ctx);
12138 break;
12139 case COMPLEX_CST:
12140 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12141 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12142 break;
12143 case VECTOR_CST:
12144 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12145 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12146 break;
12147 default:
12148 break;
12149 }
12150 break;
12151 case tcc_exceptional:
12152 switch (code)
12153 {
12154 case TREE_LIST:
12155 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12156 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12157 expr = TREE_CHAIN (expr);
12158 goto recursive_label;
12159 break;
12160 case TREE_VEC:
12161 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12162 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12163 break;
12164 default:
12165 break;
12166 }
12167 break;
12168 case tcc_expression:
12169 case tcc_reference:
12170 case tcc_comparison:
12171 case tcc_unary:
12172 case tcc_binary:
12173 case tcc_statement:
12174 case tcc_vl_exp:
12175 len = TREE_OPERAND_LENGTH (expr);
12176 for (i = 0; i < len; ++i)
12177 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12178 break;
12179 case tcc_declaration:
12180 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12181 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12182 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12183 {
12184 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12185 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12186 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12187 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12188 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12189 }
12190
12191 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12192 {
12193 if (TREE_CODE (expr) == FUNCTION_DECL)
12194 {
12195 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12196 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12197 }
12198 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12199 }
12200 break;
12201 case tcc_type:
12202 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12203 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12204 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12205 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12206 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12207 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12208 if (INTEGRAL_TYPE_P (expr)
12209 || SCALAR_FLOAT_TYPE_P (expr))
12210 {
12211 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12212 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12213 }
12214 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12215 if (TREE_CODE (expr) == RECORD_TYPE
12216 || TREE_CODE (expr) == UNION_TYPE
12217 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12218 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12219 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12220 break;
12221 default:
12222 break;
12223 }
12224 }
12225
12226 /* Helper function for outputting the checksum of a tree T. When
12227 debugging with gdb, you can "define mynext" to be "next" followed
12228 by "call debug_fold_checksum (op0)", then just trace down till the
12229 outputs differ. */
12230
12231 DEBUG_FUNCTION void
12232 debug_fold_checksum (const_tree t)
12233 {
12234 int i;
12235 unsigned char checksum[16];
12236 struct md5_ctx ctx;
12237 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12238
12239 md5_init_ctx (&ctx);
12240 fold_checksum_tree (t, &ctx, &ht);
12241 md5_finish_ctx (&ctx, checksum);
12242 ht.empty ();
12243
12244 for (i = 0; i < 16; i++)
12245 fprintf (stderr, "%d ", checksum[i]);
12246
12247 fprintf (stderr, "\n");
12248 }
12249
12250 #endif
12251
12252 /* Fold a unary tree expression with code CODE of type TYPE with an
12253 operand OP0. LOC is the location of the resulting expression.
12254 Return a folded expression if successful. Otherwise, return a tree
12255 expression with code CODE of type TYPE with an operand OP0. */
12256
12257 tree
12258 fold_build1_stat_loc (location_t loc,
12259 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12260 {
12261 tree tem;
12262 #ifdef ENABLE_FOLD_CHECKING
12263 unsigned char checksum_before[16], checksum_after[16];
12264 struct md5_ctx ctx;
12265 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12266
12267 md5_init_ctx (&ctx);
12268 fold_checksum_tree (op0, &ctx, &ht);
12269 md5_finish_ctx (&ctx, checksum_before);
12270 ht.empty ();
12271 #endif
12272
12273 tem = fold_unary_loc (loc, code, type, op0);
12274 if (!tem)
12275 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12276
12277 #ifdef ENABLE_FOLD_CHECKING
12278 md5_init_ctx (&ctx);
12279 fold_checksum_tree (op0, &ctx, &ht);
12280 md5_finish_ctx (&ctx, checksum_after);
12281
12282 if (memcmp (checksum_before, checksum_after, 16))
12283 fold_check_failed (op0, tem);
12284 #endif
12285 return tem;
12286 }
12287
12288 /* Fold a binary tree expression with code CODE of type TYPE with
12289 operands OP0 and OP1. LOC is the location of the resulting
12290 expression. Return a folded expression if successful. Otherwise,
12291 return a tree expression with code CODE of type TYPE with operands
12292 OP0 and OP1. */
12293
12294 tree
12295 fold_build2_stat_loc (location_t loc,
12296 enum tree_code code, tree type, tree op0, tree op1
12297 MEM_STAT_DECL)
12298 {
12299 tree tem;
12300 #ifdef ENABLE_FOLD_CHECKING
12301 unsigned char checksum_before_op0[16],
12302 checksum_before_op1[16],
12303 checksum_after_op0[16],
12304 checksum_after_op1[16];
12305 struct md5_ctx ctx;
12306 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12307
12308 md5_init_ctx (&ctx);
12309 fold_checksum_tree (op0, &ctx, &ht);
12310 md5_finish_ctx (&ctx, checksum_before_op0);
12311 ht.empty ();
12312
12313 md5_init_ctx (&ctx);
12314 fold_checksum_tree (op1, &ctx, &ht);
12315 md5_finish_ctx (&ctx, checksum_before_op1);
12316 ht.empty ();
12317 #endif
12318
12319 tem = fold_binary_loc (loc, code, type, op0, op1);
12320 if (!tem)
12321 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12322
12323 #ifdef ENABLE_FOLD_CHECKING
12324 md5_init_ctx (&ctx);
12325 fold_checksum_tree (op0, &ctx, &ht);
12326 md5_finish_ctx (&ctx, checksum_after_op0);
12327 ht.empty ();
12328
12329 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12330 fold_check_failed (op0, tem);
12331
12332 md5_init_ctx (&ctx);
12333 fold_checksum_tree (op1, &ctx, &ht);
12334 md5_finish_ctx (&ctx, checksum_after_op1);
12335
12336 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12337 fold_check_failed (op1, tem);
12338 #endif
12339 return tem;
12340 }
12341
12342 /* Fold a ternary tree expression with code CODE of type TYPE with
12343 operands OP0, OP1, and OP2. Return a folded expression if
12344 successful. Otherwise, return a tree expression with code CODE of
12345 type TYPE with operands OP0, OP1, and OP2. */
12346
12347 tree
12348 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12349 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12350 {
12351 tree tem;
12352 #ifdef ENABLE_FOLD_CHECKING
12353 unsigned char checksum_before_op0[16],
12354 checksum_before_op1[16],
12355 checksum_before_op2[16],
12356 checksum_after_op0[16],
12357 checksum_after_op1[16],
12358 checksum_after_op2[16];
12359 struct md5_ctx ctx;
12360 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12361
12362 md5_init_ctx (&ctx);
12363 fold_checksum_tree (op0, &ctx, &ht);
12364 md5_finish_ctx (&ctx, checksum_before_op0);
12365 ht.empty ();
12366
12367 md5_init_ctx (&ctx);
12368 fold_checksum_tree (op1, &ctx, &ht);
12369 md5_finish_ctx (&ctx, checksum_before_op1);
12370 ht.empty ();
12371
12372 md5_init_ctx (&ctx);
12373 fold_checksum_tree (op2, &ctx, &ht);
12374 md5_finish_ctx (&ctx, checksum_before_op2);
12375 ht.empty ();
12376 #endif
12377
12378 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12379 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12380 if (!tem)
12381 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12382
12383 #ifdef ENABLE_FOLD_CHECKING
12384 md5_init_ctx (&ctx);
12385 fold_checksum_tree (op0, &ctx, &ht);
12386 md5_finish_ctx (&ctx, checksum_after_op0);
12387 ht.empty ();
12388
12389 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12390 fold_check_failed (op0, tem);
12391
12392 md5_init_ctx (&ctx);
12393 fold_checksum_tree (op1, &ctx, &ht);
12394 md5_finish_ctx (&ctx, checksum_after_op1);
12395 ht.empty ();
12396
12397 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12398 fold_check_failed (op1, tem);
12399
12400 md5_init_ctx (&ctx);
12401 fold_checksum_tree (op2, &ctx, &ht);
12402 md5_finish_ctx (&ctx, checksum_after_op2);
12403
12404 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12405 fold_check_failed (op2, tem);
12406 #endif
12407 return tem;
12408 }
12409
12410 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12411 arguments in ARGARRAY, and a null static chain.
12412 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12413 of type TYPE from the given operands as constructed by build_call_array. */
12414
12415 tree
12416 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12417 int nargs, tree *argarray)
12418 {
12419 tree tem;
12420 #ifdef ENABLE_FOLD_CHECKING
12421 unsigned char checksum_before_fn[16],
12422 checksum_before_arglist[16],
12423 checksum_after_fn[16],
12424 checksum_after_arglist[16];
12425 struct md5_ctx ctx;
12426 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12427 int i;
12428
12429 md5_init_ctx (&ctx);
12430 fold_checksum_tree (fn, &ctx, &ht);
12431 md5_finish_ctx (&ctx, checksum_before_fn);
12432 ht.empty ();
12433
12434 md5_init_ctx (&ctx);
12435 for (i = 0; i < nargs; i++)
12436 fold_checksum_tree (argarray[i], &ctx, &ht);
12437 md5_finish_ctx (&ctx, checksum_before_arglist);
12438 ht.empty ();
12439 #endif
12440
12441 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12442 if (!tem)
12443 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12444
12445 #ifdef ENABLE_FOLD_CHECKING
12446 md5_init_ctx (&ctx);
12447 fold_checksum_tree (fn, &ctx, &ht);
12448 md5_finish_ctx (&ctx, checksum_after_fn);
12449 ht.empty ();
12450
12451 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12452 fold_check_failed (fn, tem);
12453
12454 md5_init_ctx (&ctx);
12455 for (i = 0; i < nargs; i++)
12456 fold_checksum_tree (argarray[i], &ctx, &ht);
12457 md5_finish_ctx (&ctx, checksum_after_arglist);
12458
12459 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12460 fold_check_failed (NULL_TREE, tem);
12461 #endif
12462 return tem;
12463 }
12464
12465 /* Perform constant folding and related simplification of initializer
12466 expression EXPR. These behave identically to "fold_buildN" but ignore
12467 potential run-time traps and exceptions that fold must preserve. */
12468
12469 #define START_FOLD_INIT \
12470 int saved_signaling_nans = flag_signaling_nans;\
12471 int saved_trapping_math = flag_trapping_math;\
12472 int saved_rounding_math = flag_rounding_math;\
12473 int saved_trapv = flag_trapv;\
12474 int saved_folding_initializer = folding_initializer;\
12475 flag_signaling_nans = 0;\
12476 flag_trapping_math = 0;\
12477 flag_rounding_math = 0;\
12478 flag_trapv = 0;\
12479 folding_initializer = 1;
12480
12481 #define END_FOLD_INIT \
12482 flag_signaling_nans = saved_signaling_nans;\
12483 flag_trapping_math = saved_trapping_math;\
12484 flag_rounding_math = saved_rounding_math;\
12485 flag_trapv = saved_trapv;\
12486 folding_initializer = saved_folding_initializer;
12487
12488 tree
12489 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12490 tree type, tree op)
12491 {
12492 tree result;
12493 START_FOLD_INIT;
12494
12495 result = fold_build1_loc (loc, code, type, op);
12496
12497 END_FOLD_INIT;
12498 return result;
12499 }
12500
12501 tree
12502 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12503 tree type, tree op0, tree op1)
12504 {
12505 tree result;
12506 START_FOLD_INIT;
12507
12508 result = fold_build2_loc (loc, code, type, op0, op1);
12509
12510 END_FOLD_INIT;
12511 return result;
12512 }
12513
12514 tree
12515 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12516 int nargs, tree *argarray)
12517 {
12518 tree result;
12519 START_FOLD_INIT;
12520
12521 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12522
12523 END_FOLD_INIT;
12524 return result;
12525 }
12526
12527 #undef START_FOLD_INIT
12528 #undef END_FOLD_INIT
12529
12530 /* Determine if first argument is a multiple of second argument. Return 0 if
12531 it is not, or we cannot easily determined it to be.
12532
12533 An example of the sort of thing we care about (at this point; this routine
12534 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12535 fold cases do now) is discovering that
12536
12537 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12538
12539 is a multiple of
12540
12541 SAVE_EXPR (J * 8)
12542
12543 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12544
12545 This code also handles discovering that
12546
12547 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12548
12549 is a multiple of 8 so we don't have to worry about dealing with a
12550 possible remainder.
12551
12552 Note that we *look* inside a SAVE_EXPR only to determine how it was
12553 calculated; it is not safe for fold to do much of anything else with the
12554 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12555 at run time. For example, the latter example above *cannot* be implemented
12556 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12557 evaluation time of the original SAVE_EXPR is not necessarily the same at
12558 the time the new expression is evaluated. The only optimization of this
12559 sort that would be valid is changing
12560
12561 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12562
12563 divided by 8 to
12564
12565 SAVE_EXPR (I) * SAVE_EXPR (J)
12566
12567 (where the same SAVE_EXPR (J) is used in the original and the
12568 transformed version). */
12569
12570 int
12571 multiple_of_p (tree type, const_tree top, const_tree bottom)
12572 {
12573 gimple *stmt;
12574 tree t1, op1, op2;
12575
12576 if (operand_equal_p (top, bottom, 0))
12577 return 1;
12578
12579 if (TREE_CODE (type) != INTEGER_TYPE)
12580 return 0;
12581
12582 switch (TREE_CODE (top))
12583 {
12584 case BIT_AND_EXPR:
12585 /* Bitwise and provides a power of two multiple. If the mask is
12586 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12587 if (!integer_pow2p (bottom))
12588 return 0;
12589 /* FALLTHRU */
12590
12591 case MULT_EXPR:
12592 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12593 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12594
12595 case MINUS_EXPR:
12596 /* It is impossible to prove if op0 - op1 is multiple of bottom
12597 precisely, so be conservative here checking if both op0 and op1
12598 are multiple of bottom. Note we check the second operand first
12599 since it's usually simpler. */
12600 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12601 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12602
12603 case PLUS_EXPR:
12604 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12605 as op0 - 3 if the expression has unsigned type. For example,
12606 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12607 op1 = TREE_OPERAND (top, 1);
12608 if (TYPE_UNSIGNED (type)
12609 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12610 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12611 return (multiple_of_p (type, op1, bottom)
12612 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12613
12614 case LSHIFT_EXPR:
12615 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12616 {
12617 op1 = TREE_OPERAND (top, 1);
12618 /* const_binop may not detect overflow correctly,
12619 so check for it explicitly here. */
12620 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12621 && 0 != (t1 = fold_convert (type,
12622 const_binop (LSHIFT_EXPR,
12623 size_one_node,
12624 op1)))
12625 && !TREE_OVERFLOW (t1))
12626 return multiple_of_p (type, t1, bottom);
12627 }
12628 return 0;
12629
12630 case NOP_EXPR:
12631 /* Can't handle conversions from non-integral or wider integral type. */
12632 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12633 || (TYPE_PRECISION (type)
12634 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12635 return 0;
12636
12637 /* fall through */
12638
12639 case SAVE_EXPR:
12640 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12641
12642 case COND_EXPR:
12643 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12644 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12645
12646 case INTEGER_CST:
12647 if (TREE_CODE (bottom) != INTEGER_CST
12648 || integer_zerop (bottom)
12649 || (TYPE_UNSIGNED (type)
12650 && (tree_int_cst_sgn (top) < 0
12651 || tree_int_cst_sgn (bottom) < 0)))
12652 return 0;
12653 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12654 SIGNED);
12655
12656 case SSA_NAME:
12657 if (TREE_CODE (bottom) == INTEGER_CST
12658 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12659 && gimple_code (stmt) == GIMPLE_ASSIGN)
12660 {
12661 enum tree_code code = gimple_assign_rhs_code (stmt);
12662
12663 /* Check for special cases to see if top is defined as multiple
12664 of bottom:
12665
12666 top = (X & ~(bottom - 1) ; bottom is power of 2
12667
12668 or
12669
12670 Y = X % bottom
12671 top = X - Y. */
12672 if (code == BIT_AND_EXPR
12673 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12674 && TREE_CODE (op2) == INTEGER_CST
12675 && integer_pow2p (bottom)
12676 && wi::multiple_of_p (wi::to_widest (op2),
12677 wi::to_widest (bottom), UNSIGNED))
12678 return 1;
12679
12680 op1 = gimple_assign_rhs1 (stmt);
12681 if (code == MINUS_EXPR
12682 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12683 && TREE_CODE (op2) == SSA_NAME
12684 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12685 && gimple_code (stmt) == GIMPLE_ASSIGN
12686 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12687 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12688 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12689 return 1;
12690 }
12691
12692 /* fall through */
12693
12694 default:
12695 return 0;
12696 }
12697 }
12698
12699 #define tree_expr_nonnegative_warnv_p(X, Y) \
12700 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12701
12702 #define RECURSE(X) \
12703 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12704
12705 /* Return true if CODE or TYPE is known to be non-negative. */
12706
12707 static bool
12708 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12709 {
12710 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12711 && truth_value_p (code))
12712 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12713 have a signed:1 type (where the value is -1 and 0). */
12714 return true;
12715 return false;
12716 }
12717
12718 /* Return true if (CODE OP0) is known to be non-negative. If the return
12719 value is based on the assumption that signed overflow is undefined,
12720 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12721 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12722
12723 bool
12724 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12725 bool *strict_overflow_p, int depth)
12726 {
12727 if (TYPE_UNSIGNED (type))
12728 return true;
12729
12730 switch (code)
12731 {
12732 case ABS_EXPR:
12733 /* We can't return 1 if flag_wrapv is set because
12734 ABS_EXPR<INT_MIN> = INT_MIN. */
12735 if (!ANY_INTEGRAL_TYPE_P (type))
12736 return true;
12737 if (TYPE_OVERFLOW_UNDEFINED (type))
12738 {
12739 *strict_overflow_p = true;
12740 return true;
12741 }
12742 break;
12743
12744 case NON_LVALUE_EXPR:
12745 case FLOAT_EXPR:
12746 case FIX_TRUNC_EXPR:
12747 return RECURSE (op0);
12748
12749 CASE_CONVERT:
12750 {
12751 tree inner_type = TREE_TYPE (op0);
12752 tree outer_type = type;
12753
12754 if (TREE_CODE (outer_type) == REAL_TYPE)
12755 {
12756 if (TREE_CODE (inner_type) == REAL_TYPE)
12757 return RECURSE (op0);
12758 if (INTEGRAL_TYPE_P (inner_type))
12759 {
12760 if (TYPE_UNSIGNED (inner_type))
12761 return true;
12762 return RECURSE (op0);
12763 }
12764 }
12765 else if (INTEGRAL_TYPE_P (outer_type))
12766 {
12767 if (TREE_CODE (inner_type) == REAL_TYPE)
12768 return RECURSE (op0);
12769 if (INTEGRAL_TYPE_P (inner_type))
12770 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12771 && TYPE_UNSIGNED (inner_type);
12772 }
12773 }
12774 break;
12775
12776 default:
12777 return tree_simple_nonnegative_warnv_p (code, type);
12778 }
12779
12780 /* We don't know sign of `t', so be conservative and return false. */
12781 return false;
12782 }
12783
12784 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12785 value is based on the assumption that signed overflow is undefined,
12786 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12787 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12788
12789 bool
12790 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12791 tree op1, bool *strict_overflow_p,
12792 int depth)
12793 {
12794 if (TYPE_UNSIGNED (type))
12795 return true;
12796
12797 switch (code)
12798 {
12799 case POINTER_PLUS_EXPR:
12800 case PLUS_EXPR:
12801 if (FLOAT_TYPE_P (type))
12802 return RECURSE (op0) && RECURSE (op1);
12803
12804 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12805 both unsigned and at least 2 bits shorter than the result. */
12806 if (TREE_CODE (type) == INTEGER_TYPE
12807 && TREE_CODE (op0) == NOP_EXPR
12808 && TREE_CODE (op1) == NOP_EXPR)
12809 {
12810 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12811 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12812 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12813 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12814 {
12815 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12816 TYPE_PRECISION (inner2)) + 1;
12817 return prec < TYPE_PRECISION (type);
12818 }
12819 }
12820 break;
12821
12822 case MULT_EXPR:
12823 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12824 {
12825 /* x * x is always non-negative for floating point x
12826 or without overflow. */
12827 if (operand_equal_p (op0, op1, 0)
12828 || (RECURSE (op0) && RECURSE (op1)))
12829 {
12830 if (ANY_INTEGRAL_TYPE_P (type)
12831 && TYPE_OVERFLOW_UNDEFINED (type))
12832 *strict_overflow_p = true;
12833 return true;
12834 }
12835 }
12836
12837 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12838 both unsigned and their total bits is shorter than the result. */
12839 if (TREE_CODE (type) == INTEGER_TYPE
12840 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12841 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12842 {
12843 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12844 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12845 : TREE_TYPE (op0);
12846 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12847 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12848 : TREE_TYPE (op1);
12849
12850 bool unsigned0 = TYPE_UNSIGNED (inner0);
12851 bool unsigned1 = TYPE_UNSIGNED (inner1);
12852
12853 if (TREE_CODE (op0) == INTEGER_CST)
12854 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12855
12856 if (TREE_CODE (op1) == INTEGER_CST)
12857 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12858
12859 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12860 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12861 {
12862 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12863 ? tree_int_cst_min_precision (op0, UNSIGNED)
12864 : TYPE_PRECISION (inner0);
12865
12866 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12867 ? tree_int_cst_min_precision (op1, UNSIGNED)
12868 : TYPE_PRECISION (inner1);
12869
12870 return precision0 + precision1 < TYPE_PRECISION (type);
12871 }
12872 }
12873 return false;
12874
12875 case BIT_AND_EXPR:
12876 case MAX_EXPR:
12877 return RECURSE (op0) || RECURSE (op1);
12878
12879 case BIT_IOR_EXPR:
12880 case BIT_XOR_EXPR:
12881 case MIN_EXPR:
12882 case RDIV_EXPR:
12883 case TRUNC_DIV_EXPR:
12884 case CEIL_DIV_EXPR:
12885 case FLOOR_DIV_EXPR:
12886 case ROUND_DIV_EXPR:
12887 return RECURSE (op0) && RECURSE (op1);
12888
12889 case TRUNC_MOD_EXPR:
12890 return RECURSE (op0);
12891
12892 case FLOOR_MOD_EXPR:
12893 return RECURSE (op1);
12894
12895 case CEIL_MOD_EXPR:
12896 case ROUND_MOD_EXPR:
12897 default:
12898 return tree_simple_nonnegative_warnv_p (code, type);
12899 }
12900
12901 /* We don't know sign of `t', so be conservative and return false. */
12902 return false;
12903 }
12904
12905 /* Return true if T is known to be non-negative. If the return
12906 value is based on the assumption that signed overflow is undefined,
12907 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12908 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12909
12910 bool
12911 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12912 {
12913 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12914 return true;
12915
12916 switch (TREE_CODE (t))
12917 {
12918 case INTEGER_CST:
12919 return tree_int_cst_sgn (t) >= 0;
12920
12921 case REAL_CST:
12922 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12923
12924 case FIXED_CST:
12925 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12926
12927 case COND_EXPR:
12928 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12929
12930 case SSA_NAME:
12931 /* Limit the depth of recursion to avoid quadratic behavior.
12932 This is expected to catch almost all occurrences in practice.
12933 If this code misses important cases that unbounded recursion
12934 would not, passes that need this information could be revised
12935 to provide it through dataflow propagation. */
12936 return (!name_registered_for_update_p (t)
12937 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12938 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12939 strict_overflow_p, depth));
12940
12941 default:
12942 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12943 }
12944 }
12945
12946 /* Return true if T is known to be non-negative. If the return
12947 value is based on the assumption that signed overflow is undefined,
12948 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12949 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12950
12951 bool
12952 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12953 bool *strict_overflow_p, int depth)
12954 {
12955 switch (fn)
12956 {
12957 CASE_CFN_ACOS:
12958 CASE_CFN_ACOSH:
12959 CASE_CFN_CABS:
12960 CASE_CFN_COSH:
12961 CASE_CFN_ERFC:
12962 CASE_CFN_EXP:
12963 CASE_CFN_EXP10:
12964 CASE_CFN_EXP2:
12965 CASE_CFN_FABS:
12966 CASE_CFN_FDIM:
12967 CASE_CFN_HYPOT:
12968 CASE_CFN_POW10:
12969 CASE_CFN_FFS:
12970 CASE_CFN_PARITY:
12971 CASE_CFN_POPCOUNT:
12972 CASE_CFN_CLZ:
12973 CASE_CFN_CLRSB:
12974 case CFN_BUILT_IN_BSWAP32:
12975 case CFN_BUILT_IN_BSWAP64:
12976 /* Always true. */
12977 return true;
12978
12979 CASE_CFN_SQRT:
12980 /* sqrt(-0.0) is -0.0. */
12981 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12982 return true;
12983 return RECURSE (arg0);
12984
12985 CASE_CFN_ASINH:
12986 CASE_CFN_ATAN:
12987 CASE_CFN_ATANH:
12988 CASE_CFN_CBRT:
12989 CASE_CFN_CEIL:
12990 CASE_CFN_ERF:
12991 CASE_CFN_EXPM1:
12992 CASE_CFN_FLOOR:
12993 CASE_CFN_FMOD:
12994 CASE_CFN_FREXP:
12995 CASE_CFN_ICEIL:
12996 CASE_CFN_IFLOOR:
12997 CASE_CFN_IRINT:
12998 CASE_CFN_IROUND:
12999 CASE_CFN_LCEIL:
13000 CASE_CFN_LDEXP:
13001 CASE_CFN_LFLOOR:
13002 CASE_CFN_LLCEIL:
13003 CASE_CFN_LLFLOOR:
13004 CASE_CFN_LLRINT:
13005 CASE_CFN_LLROUND:
13006 CASE_CFN_LRINT:
13007 CASE_CFN_LROUND:
13008 CASE_CFN_MODF:
13009 CASE_CFN_NEARBYINT:
13010 CASE_CFN_RINT:
13011 CASE_CFN_ROUND:
13012 CASE_CFN_SCALB:
13013 CASE_CFN_SCALBLN:
13014 CASE_CFN_SCALBN:
13015 CASE_CFN_SIGNBIT:
13016 CASE_CFN_SIGNIFICAND:
13017 CASE_CFN_SINH:
13018 CASE_CFN_TANH:
13019 CASE_CFN_TRUNC:
13020 /* True if the 1st argument is nonnegative. */
13021 return RECURSE (arg0);
13022
13023 CASE_CFN_FMAX:
13024 /* True if the 1st OR 2nd arguments are nonnegative. */
13025 return RECURSE (arg0) || RECURSE (arg1);
13026
13027 CASE_CFN_FMIN:
13028 /* True if the 1st AND 2nd arguments are nonnegative. */
13029 return RECURSE (arg0) && RECURSE (arg1);
13030
13031 CASE_CFN_COPYSIGN:
13032 /* True if the 2nd argument is nonnegative. */
13033 return RECURSE (arg1);
13034
13035 CASE_CFN_POWI:
13036 /* True if the 1st argument is nonnegative or the second
13037 argument is an even integer. */
13038 if (TREE_CODE (arg1) == INTEGER_CST
13039 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13040 return true;
13041 return RECURSE (arg0);
13042
13043 CASE_CFN_POW:
13044 /* True if the 1st argument is nonnegative or the second
13045 argument is an even integer valued real. */
13046 if (TREE_CODE (arg1) == REAL_CST)
13047 {
13048 REAL_VALUE_TYPE c;
13049 HOST_WIDE_INT n;
13050
13051 c = TREE_REAL_CST (arg1);
13052 n = real_to_integer (&c);
13053 if ((n & 1) == 0)
13054 {
13055 REAL_VALUE_TYPE cint;
13056 real_from_integer (&cint, VOIDmode, n, SIGNED);
13057 if (real_identical (&c, &cint))
13058 return true;
13059 }
13060 }
13061 return RECURSE (arg0);
13062
13063 default:
13064 break;
13065 }
13066 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13067 }
13068
13069 /* Return true if T is known to be non-negative. If the return
13070 value is based on the assumption that signed overflow is undefined,
13071 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13072 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13073
13074 static bool
13075 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13076 {
13077 enum tree_code code = TREE_CODE (t);
13078 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13079 return true;
13080
13081 switch (code)
13082 {
13083 case TARGET_EXPR:
13084 {
13085 tree temp = TARGET_EXPR_SLOT (t);
13086 t = TARGET_EXPR_INITIAL (t);
13087
13088 /* If the initializer is non-void, then it's a normal expression
13089 that will be assigned to the slot. */
13090 if (!VOID_TYPE_P (t))
13091 return RECURSE (t);
13092
13093 /* Otherwise, the initializer sets the slot in some way. One common
13094 way is an assignment statement at the end of the initializer. */
13095 while (1)
13096 {
13097 if (TREE_CODE (t) == BIND_EXPR)
13098 t = expr_last (BIND_EXPR_BODY (t));
13099 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13100 || TREE_CODE (t) == TRY_CATCH_EXPR)
13101 t = expr_last (TREE_OPERAND (t, 0));
13102 else if (TREE_CODE (t) == STATEMENT_LIST)
13103 t = expr_last (t);
13104 else
13105 break;
13106 }
13107 if (TREE_CODE (t) == MODIFY_EXPR
13108 && TREE_OPERAND (t, 0) == temp)
13109 return RECURSE (TREE_OPERAND (t, 1));
13110
13111 return false;
13112 }
13113
13114 case CALL_EXPR:
13115 {
13116 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13117 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13118
13119 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13120 get_call_combined_fn (t),
13121 arg0,
13122 arg1,
13123 strict_overflow_p, depth);
13124 }
13125 case COMPOUND_EXPR:
13126 case MODIFY_EXPR:
13127 return RECURSE (TREE_OPERAND (t, 1));
13128
13129 case BIND_EXPR:
13130 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13131
13132 case SAVE_EXPR:
13133 return RECURSE (TREE_OPERAND (t, 0));
13134
13135 default:
13136 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13137 }
13138 }
13139
13140 #undef RECURSE
13141 #undef tree_expr_nonnegative_warnv_p
13142
13143 /* Return true if T is known to be non-negative. If the return
13144 value is based on the assumption that signed overflow is undefined,
13145 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13146 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13147
13148 bool
13149 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13150 {
13151 enum tree_code code;
13152 if (t == error_mark_node)
13153 return false;
13154
13155 code = TREE_CODE (t);
13156 switch (TREE_CODE_CLASS (code))
13157 {
13158 case tcc_binary:
13159 case tcc_comparison:
13160 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13161 TREE_TYPE (t),
13162 TREE_OPERAND (t, 0),
13163 TREE_OPERAND (t, 1),
13164 strict_overflow_p, depth);
13165
13166 case tcc_unary:
13167 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13168 TREE_TYPE (t),
13169 TREE_OPERAND (t, 0),
13170 strict_overflow_p, depth);
13171
13172 case tcc_constant:
13173 case tcc_declaration:
13174 case tcc_reference:
13175 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13176
13177 default:
13178 break;
13179 }
13180
13181 switch (code)
13182 {
13183 case TRUTH_AND_EXPR:
13184 case TRUTH_OR_EXPR:
13185 case TRUTH_XOR_EXPR:
13186 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13187 TREE_TYPE (t),
13188 TREE_OPERAND (t, 0),
13189 TREE_OPERAND (t, 1),
13190 strict_overflow_p, depth);
13191 case TRUTH_NOT_EXPR:
13192 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13193 TREE_TYPE (t),
13194 TREE_OPERAND (t, 0),
13195 strict_overflow_p, depth);
13196
13197 case COND_EXPR:
13198 case CONSTRUCTOR:
13199 case OBJ_TYPE_REF:
13200 case ASSERT_EXPR:
13201 case ADDR_EXPR:
13202 case WITH_SIZE_EXPR:
13203 case SSA_NAME:
13204 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13205
13206 default:
13207 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13208 }
13209 }
13210
13211 /* Return true if `t' is known to be non-negative. Handle warnings
13212 about undefined signed overflow. */
13213
13214 bool
13215 tree_expr_nonnegative_p (tree t)
13216 {
13217 bool ret, strict_overflow_p;
13218
13219 strict_overflow_p = false;
13220 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13221 if (strict_overflow_p)
13222 fold_overflow_warning (("assuming signed overflow does not occur when "
13223 "determining that expression is always "
13224 "non-negative"),
13225 WARN_STRICT_OVERFLOW_MISC);
13226 return ret;
13227 }
13228
13229
13230 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13231 For floating point we further ensure that T is not denormal.
13232 Similar logic is present in nonzero_address in rtlanal.h.
13233
13234 If the return value is based on the assumption that signed overflow
13235 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13236 change *STRICT_OVERFLOW_P. */
13237
13238 bool
13239 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13240 bool *strict_overflow_p)
13241 {
13242 switch (code)
13243 {
13244 case ABS_EXPR:
13245 return tree_expr_nonzero_warnv_p (op0,
13246 strict_overflow_p);
13247
13248 case NOP_EXPR:
13249 {
13250 tree inner_type = TREE_TYPE (op0);
13251 tree outer_type = type;
13252
13253 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13254 && tree_expr_nonzero_warnv_p (op0,
13255 strict_overflow_p));
13256 }
13257 break;
13258
13259 case NON_LVALUE_EXPR:
13260 return tree_expr_nonzero_warnv_p (op0,
13261 strict_overflow_p);
13262
13263 default:
13264 break;
13265 }
13266
13267 return false;
13268 }
13269
13270 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13271 For floating point we further ensure that T is not denormal.
13272 Similar logic is present in nonzero_address in rtlanal.h.
13273
13274 If the return value is based on the assumption that signed overflow
13275 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13276 change *STRICT_OVERFLOW_P. */
13277
13278 bool
13279 tree_binary_nonzero_warnv_p (enum tree_code code,
13280 tree type,
13281 tree op0,
13282 tree op1, bool *strict_overflow_p)
13283 {
13284 bool sub_strict_overflow_p;
13285 switch (code)
13286 {
13287 case POINTER_PLUS_EXPR:
13288 case PLUS_EXPR:
13289 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13290 {
13291 /* With the presence of negative values it is hard
13292 to say something. */
13293 sub_strict_overflow_p = false;
13294 if (!tree_expr_nonnegative_warnv_p (op0,
13295 &sub_strict_overflow_p)
13296 || !tree_expr_nonnegative_warnv_p (op1,
13297 &sub_strict_overflow_p))
13298 return false;
13299 /* One of operands must be positive and the other non-negative. */
13300 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13301 overflows, on a twos-complement machine the sum of two
13302 nonnegative numbers can never be zero. */
13303 return (tree_expr_nonzero_warnv_p (op0,
13304 strict_overflow_p)
13305 || tree_expr_nonzero_warnv_p (op1,
13306 strict_overflow_p));
13307 }
13308 break;
13309
13310 case MULT_EXPR:
13311 if (TYPE_OVERFLOW_UNDEFINED (type))
13312 {
13313 if (tree_expr_nonzero_warnv_p (op0,
13314 strict_overflow_p)
13315 && tree_expr_nonzero_warnv_p (op1,
13316 strict_overflow_p))
13317 {
13318 *strict_overflow_p = true;
13319 return true;
13320 }
13321 }
13322 break;
13323
13324 case MIN_EXPR:
13325 sub_strict_overflow_p = false;
13326 if (tree_expr_nonzero_warnv_p (op0,
13327 &sub_strict_overflow_p)
13328 && tree_expr_nonzero_warnv_p (op1,
13329 &sub_strict_overflow_p))
13330 {
13331 if (sub_strict_overflow_p)
13332 *strict_overflow_p = true;
13333 }
13334 break;
13335
13336 case MAX_EXPR:
13337 sub_strict_overflow_p = false;
13338 if (tree_expr_nonzero_warnv_p (op0,
13339 &sub_strict_overflow_p))
13340 {
13341 if (sub_strict_overflow_p)
13342 *strict_overflow_p = true;
13343
13344 /* When both operands are nonzero, then MAX must be too. */
13345 if (tree_expr_nonzero_warnv_p (op1,
13346 strict_overflow_p))
13347 return true;
13348
13349 /* MAX where operand 0 is positive is positive. */
13350 return tree_expr_nonnegative_warnv_p (op0,
13351 strict_overflow_p);
13352 }
13353 /* MAX where operand 1 is positive is positive. */
13354 else if (tree_expr_nonzero_warnv_p (op1,
13355 &sub_strict_overflow_p)
13356 && tree_expr_nonnegative_warnv_p (op1,
13357 &sub_strict_overflow_p))
13358 {
13359 if (sub_strict_overflow_p)
13360 *strict_overflow_p = true;
13361 return true;
13362 }
13363 break;
13364
13365 case BIT_IOR_EXPR:
13366 return (tree_expr_nonzero_warnv_p (op1,
13367 strict_overflow_p)
13368 || tree_expr_nonzero_warnv_p (op0,
13369 strict_overflow_p));
13370
13371 default:
13372 break;
13373 }
13374
13375 return false;
13376 }
13377
13378 /* Return true when T is an address and is known to be nonzero.
13379 For floating point we further ensure that T is not denormal.
13380 Similar logic is present in nonzero_address in rtlanal.h.
13381
13382 If the return value is based on the assumption that signed overflow
13383 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13384 change *STRICT_OVERFLOW_P. */
13385
13386 bool
13387 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13388 {
13389 bool sub_strict_overflow_p;
13390 switch (TREE_CODE (t))
13391 {
13392 case INTEGER_CST:
13393 return !integer_zerop (t);
13394
13395 case ADDR_EXPR:
13396 {
13397 tree base = TREE_OPERAND (t, 0);
13398
13399 if (!DECL_P (base))
13400 base = get_base_address (base);
13401
13402 if (base && TREE_CODE (base) == TARGET_EXPR)
13403 base = TARGET_EXPR_SLOT (base);
13404
13405 if (!base)
13406 return false;
13407
13408 /* For objects in symbol table check if we know they are non-zero.
13409 Don't do anything for variables and functions before symtab is built;
13410 it is quite possible that they will be declared weak later. */
13411 int nonzero_addr = maybe_nonzero_address (base);
13412 if (nonzero_addr >= 0)
13413 return nonzero_addr;
13414
13415 /* Constants are never weak. */
13416 if (CONSTANT_CLASS_P (base))
13417 return true;
13418
13419 return false;
13420 }
13421
13422 case COND_EXPR:
13423 sub_strict_overflow_p = false;
13424 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13425 &sub_strict_overflow_p)
13426 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13427 &sub_strict_overflow_p))
13428 {
13429 if (sub_strict_overflow_p)
13430 *strict_overflow_p = true;
13431 return true;
13432 }
13433 break;
13434
13435 case SSA_NAME:
13436 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13437 break;
13438 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13439
13440 default:
13441 break;
13442 }
13443 return false;
13444 }
13445
13446 #define integer_valued_real_p(X) \
13447 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13448
13449 #define RECURSE(X) \
13450 ((integer_valued_real_p) (X, depth + 1))
13451
13452 /* Return true if the floating point result of (CODE OP0) has an
13453 integer value. We also allow +Inf, -Inf and NaN to be considered
13454 integer values. Return false for signaling NaN.
13455
13456 DEPTH is the current nesting depth of the query. */
13457
13458 bool
13459 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13460 {
13461 switch (code)
13462 {
13463 case FLOAT_EXPR:
13464 return true;
13465
13466 case ABS_EXPR:
13467 return RECURSE (op0);
13468
13469 CASE_CONVERT:
13470 {
13471 tree type = TREE_TYPE (op0);
13472 if (TREE_CODE (type) == INTEGER_TYPE)
13473 return true;
13474 if (TREE_CODE (type) == REAL_TYPE)
13475 return RECURSE (op0);
13476 break;
13477 }
13478
13479 default:
13480 break;
13481 }
13482 return false;
13483 }
13484
13485 /* Return true if the floating point result of (CODE OP0 OP1) has an
13486 integer value. We also allow +Inf, -Inf and NaN to be considered
13487 integer values. Return false for signaling NaN.
13488
13489 DEPTH is the current nesting depth of the query. */
13490
13491 bool
13492 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13493 {
13494 switch (code)
13495 {
13496 case PLUS_EXPR:
13497 case MINUS_EXPR:
13498 case MULT_EXPR:
13499 case MIN_EXPR:
13500 case MAX_EXPR:
13501 return RECURSE (op0) && RECURSE (op1);
13502
13503 default:
13504 break;
13505 }
13506 return false;
13507 }
13508
13509 /* Return true if the floating point result of calling FNDECL with arguments
13510 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13511 considered integer values. Return false for signaling NaN. If FNDECL
13512 takes fewer than 2 arguments, the remaining ARGn are null.
13513
13514 DEPTH is the current nesting depth of the query. */
13515
13516 bool
13517 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13518 {
13519 switch (fn)
13520 {
13521 CASE_CFN_CEIL:
13522 CASE_CFN_FLOOR:
13523 CASE_CFN_NEARBYINT:
13524 CASE_CFN_RINT:
13525 CASE_CFN_ROUND:
13526 CASE_CFN_TRUNC:
13527 return true;
13528
13529 CASE_CFN_FMIN:
13530 CASE_CFN_FMAX:
13531 return RECURSE (arg0) && RECURSE (arg1);
13532
13533 default:
13534 break;
13535 }
13536 return false;
13537 }
13538
13539 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13540 has an integer value. We also allow +Inf, -Inf and NaN to be
13541 considered integer values. Return false for signaling NaN.
13542
13543 DEPTH is the current nesting depth of the query. */
13544
13545 bool
13546 integer_valued_real_single_p (tree t, int depth)
13547 {
13548 switch (TREE_CODE (t))
13549 {
13550 case REAL_CST:
13551 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13552
13553 case COND_EXPR:
13554 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13555
13556 case SSA_NAME:
13557 /* Limit the depth of recursion to avoid quadratic behavior.
13558 This is expected to catch almost all occurrences in practice.
13559 If this code misses important cases that unbounded recursion
13560 would not, passes that need this information could be revised
13561 to provide it through dataflow propagation. */
13562 return (!name_registered_for_update_p (t)
13563 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13564 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13565 depth));
13566
13567 default:
13568 break;
13569 }
13570 return false;
13571 }
13572
13573 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13574 has an integer value. We also allow +Inf, -Inf and NaN to be
13575 considered integer values. Return false for signaling NaN.
13576
13577 DEPTH is the current nesting depth of the query. */
13578
13579 static bool
13580 integer_valued_real_invalid_p (tree t, int depth)
13581 {
13582 switch (TREE_CODE (t))
13583 {
13584 case COMPOUND_EXPR:
13585 case MODIFY_EXPR:
13586 case BIND_EXPR:
13587 return RECURSE (TREE_OPERAND (t, 1));
13588
13589 case SAVE_EXPR:
13590 return RECURSE (TREE_OPERAND (t, 0));
13591
13592 default:
13593 break;
13594 }
13595 return false;
13596 }
13597
13598 #undef RECURSE
13599 #undef integer_valued_real_p
13600
13601 /* Return true if the floating point expression T has an integer value.
13602 We also allow +Inf, -Inf and NaN to be considered integer values.
13603 Return false for signaling NaN.
13604
13605 DEPTH is the current nesting depth of the query. */
13606
13607 bool
13608 integer_valued_real_p (tree t, int depth)
13609 {
13610 if (t == error_mark_node)
13611 return false;
13612
13613 tree_code code = TREE_CODE (t);
13614 switch (TREE_CODE_CLASS (code))
13615 {
13616 case tcc_binary:
13617 case tcc_comparison:
13618 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13619 TREE_OPERAND (t, 1), depth);
13620
13621 case tcc_unary:
13622 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13623
13624 case tcc_constant:
13625 case tcc_declaration:
13626 case tcc_reference:
13627 return integer_valued_real_single_p (t, depth);
13628
13629 default:
13630 break;
13631 }
13632
13633 switch (code)
13634 {
13635 case COND_EXPR:
13636 case SSA_NAME:
13637 return integer_valued_real_single_p (t, depth);
13638
13639 case CALL_EXPR:
13640 {
13641 tree arg0 = (call_expr_nargs (t) > 0
13642 ? CALL_EXPR_ARG (t, 0)
13643 : NULL_TREE);
13644 tree arg1 = (call_expr_nargs (t) > 1
13645 ? CALL_EXPR_ARG (t, 1)
13646 : NULL_TREE);
13647 return integer_valued_real_call_p (get_call_combined_fn (t),
13648 arg0, arg1, depth);
13649 }
13650
13651 default:
13652 return integer_valued_real_invalid_p (t, depth);
13653 }
13654 }
13655
13656 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13657 attempt to fold the expression to a constant without modifying TYPE,
13658 OP0 or OP1.
13659
13660 If the expression could be simplified to a constant, then return
13661 the constant. If the expression would not be simplified to a
13662 constant, then return NULL_TREE. */
13663
13664 tree
13665 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13666 {
13667 tree tem = fold_binary (code, type, op0, op1);
13668 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13669 }
13670
13671 /* Given the components of a unary expression CODE, TYPE and OP0,
13672 attempt to fold the expression to a constant without modifying
13673 TYPE or OP0.
13674
13675 If the expression could be simplified to a constant, then return
13676 the constant. If the expression would not be simplified to a
13677 constant, then return NULL_TREE. */
13678
13679 tree
13680 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13681 {
13682 tree tem = fold_unary (code, type, op0);
13683 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13684 }
13685
13686 /* If EXP represents referencing an element in a constant string
13687 (either via pointer arithmetic or array indexing), return the
13688 tree representing the value accessed, otherwise return NULL. */
13689
13690 tree
13691 fold_read_from_constant_string (tree exp)
13692 {
13693 if ((TREE_CODE (exp) == INDIRECT_REF
13694 || TREE_CODE (exp) == ARRAY_REF)
13695 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13696 {
13697 tree exp1 = TREE_OPERAND (exp, 0);
13698 tree index;
13699 tree string;
13700 location_t loc = EXPR_LOCATION (exp);
13701
13702 if (TREE_CODE (exp) == INDIRECT_REF)
13703 string = string_constant (exp1, &index);
13704 else
13705 {
13706 tree low_bound = array_ref_low_bound (exp);
13707 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13708
13709 /* Optimize the special-case of a zero lower bound.
13710
13711 We convert the low_bound to sizetype to avoid some problems
13712 with constant folding. (E.g. suppose the lower bound is 1,
13713 and its mode is QI. Without the conversion,l (ARRAY
13714 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13715 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13716 if (! integer_zerop (low_bound))
13717 index = size_diffop_loc (loc, index,
13718 fold_convert_loc (loc, sizetype, low_bound));
13719
13720 string = exp1;
13721 }
13722
13723 if (string
13724 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13725 && TREE_CODE (string) == STRING_CST
13726 && TREE_CODE (index) == INTEGER_CST
13727 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13728 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13729 == MODE_INT)
13730 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13731 return build_int_cst_type (TREE_TYPE (exp),
13732 (TREE_STRING_POINTER (string)
13733 [TREE_INT_CST_LOW (index)]));
13734 }
13735 return NULL;
13736 }
13737
13738 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13739 an integer constant, real, or fixed-point constant.
13740
13741 TYPE is the type of the result. */
13742
13743 static tree
13744 fold_negate_const (tree arg0, tree type)
13745 {
13746 tree t = NULL_TREE;
13747
13748 switch (TREE_CODE (arg0))
13749 {
13750 case INTEGER_CST:
13751 {
13752 bool overflow;
13753 wide_int val = wi::neg (arg0, &overflow);
13754 t = force_fit_type (type, val, 1,
13755 (overflow | TREE_OVERFLOW (arg0))
13756 && !TYPE_UNSIGNED (type));
13757 break;
13758 }
13759
13760 case REAL_CST:
13761 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13762 break;
13763
13764 case FIXED_CST:
13765 {
13766 FIXED_VALUE_TYPE f;
13767 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13768 &(TREE_FIXED_CST (arg0)), NULL,
13769 TYPE_SATURATING (type));
13770 t = build_fixed (type, f);
13771 /* Propagate overflow flags. */
13772 if (overflow_p | TREE_OVERFLOW (arg0))
13773 TREE_OVERFLOW (t) = 1;
13774 break;
13775 }
13776
13777 default:
13778 gcc_unreachable ();
13779 }
13780
13781 return t;
13782 }
13783
13784 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13785 an integer constant or real constant.
13786
13787 TYPE is the type of the result. */
13788
13789 tree
13790 fold_abs_const (tree arg0, tree type)
13791 {
13792 tree t = NULL_TREE;
13793
13794 switch (TREE_CODE (arg0))
13795 {
13796 case INTEGER_CST:
13797 {
13798 /* If the value is unsigned or non-negative, then the absolute value
13799 is the same as the ordinary value. */
13800 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13801 t = arg0;
13802
13803 /* If the value is negative, then the absolute value is
13804 its negation. */
13805 else
13806 {
13807 bool overflow;
13808 wide_int val = wi::neg (arg0, &overflow);
13809 t = force_fit_type (type, val, -1,
13810 overflow | TREE_OVERFLOW (arg0));
13811 }
13812 }
13813 break;
13814
13815 case REAL_CST:
13816 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13817 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13818 else
13819 t = arg0;
13820 break;
13821
13822 default:
13823 gcc_unreachable ();
13824 }
13825
13826 return t;
13827 }
13828
13829 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13830 constant. TYPE is the type of the result. */
13831
13832 static tree
13833 fold_not_const (const_tree arg0, tree type)
13834 {
13835 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13836
13837 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13838 }
13839
13840 /* Given CODE, a relational operator, the target type, TYPE and two
13841 constant operands OP0 and OP1, return the result of the
13842 relational operation. If the result is not a compile time
13843 constant, then return NULL_TREE. */
13844
13845 static tree
13846 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13847 {
13848 int result, invert;
13849
13850 /* From here on, the only cases we handle are when the result is
13851 known to be a constant. */
13852
13853 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13854 {
13855 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13856 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13857
13858 /* Handle the cases where either operand is a NaN. */
13859 if (real_isnan (c0) || real_isnan (c1))
13860 {
13861 switch (code)
13862 {
13863 case EQ_EXPR:
13864 case ORDERED_EXPR:
13865 result = 0;
13866 break;
13867
13868 case NE_EXPR:
13869 case UNORDERED_EXPR:
13870 case UNLT_EXPR:
13871 case UNLE_EXPR:
13872 case UNGT_EXPR:
13873 case UNGE_EXPR:
13874 case UNEQ_EXPR:
13875 result = 1;
13876 break;
13877
13878 case LT_EXPR:
13879 case LE_EXPR:
13880 case GT_EXPR:
13881 case GE_EXPR:
13882 case LTGT_EXPR:
13883 if (flag_trapping_math)
13884 return NULL_TREE;
13885 result = 0;
13886 break;
13887
13888 default:
13889 gcc_unreachable ();
13890 }
13891
13892 return constant_boolean_node (result, type);
13893 }
13894
13895 return constant_boolean_node (real_compare (code, c0, c1), type);
13896 }
13897
13898 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13899 {
13900 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13901 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13902 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13903 }
13904
13905 /* Handle equality/inequality of complex constants. */
13906 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13907 {
13908 tree rcond = fold_relational_const (code, type,
13909 TREE_REALPART (op0),
13910 TREE_REALPART (op1));
13911 tree icond = fold_relational_const (code, type,
13912 TREE_IMAGPART (op0),
13913 TREE_IMAGPART (op1));
13914 if (code == EQ_EXPR)
13915 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13916 else if (code == NE_EXPR)
13917 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13918 else
13919 return NULL_TREE;
13920 }
13921
13922 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13923 {
13924 if (!VECTOR_TYPE_P (type))
13925 {
13926 /* Have vector comparison with scalar boolean result. */
13927 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13928 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13929 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13930 {
13931 tree elem0 = VECTOR_CST_ELT (op0, i);
13932 tree elem1 = VECTOR_CST_ELT (op1, i);
13933 tree tmp = fold_relational_const (code, type, elem0, elem1);
13934 if (tmp == NULL_TREE)
13935 return NULL_TREE;
13936 if (integer_zerop (tmp))
13937 return constant_boolean_node (false, type);
13938 }
13939 return constant_boolean_node (true, type);
13940 }
13941 unsigned count = VECTOR_CST_NELTS (op0);
13942 tree *elts = XALLOCAVEC (tree, count);
13943 gcc_assert (VECTOR_CST_NELTS (op1) == count
13944 && TYPE_VECTOR_SUBPARTS (type) == count);
13945
13946 for (unsigned i = 0; i < count; i++)
13947 {
13948 tree elem_type = TREE_TYPE (type);
13949 tree elem0 = VECTOR_CST_ELT (op0, i);
13950 tree elem1 = VECTOR_CST_ELT (op1, i);
13951
13952 tree tem = fold_relational_const (code, elem_type,
13953 elem0, elem1);
13954
13955 if (tem == NULL_TREE)
13956 return NULL_TREE;
13957
13958 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13959 }
13960
13961 return build_vector (type, elts);
13962 }
13963
13964 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13965
13966 To compute GT, swap the arguments and do LT.
13967 To compute GE, do LT and invert the result.
13968 To compute LE, swap the arguments, do LT and invert the result.
13969 To compute NE, do EQ and invert the result.
13970
13971 Therefore, the code below must handle only EQ and LT. */
13972
13973 if (code == LE_EXPR || code == GT_EXPR)
13974 {
13975 std::swap (op0, op1);
13976 code = swap_tree_comparison (code);
13977 }
13978
13979 /* Note that it is safe to invert for real values here because we
13980 have already handled the one case that it matters. */
13981
13982 invert = 0;
13983 if (code == NE_EXPR || code == GE_EXPR)
13984 {
13985 invert = 1;
13986 code = invert_tree_comparison (code, false);
13987 }
13988
13989 /* Compute a result for LT or EQ if args permit;
13990 Otherwise return T. */
13991 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13992 {
13993 if (code == EQ_EXPR)
13994 result = tree_int_cst_equal (op0, op1);
13995 else
13996 result = tree_int_cst_lt (op0, op1);
13997 }
13998 else
13999 return NULL_TREE;
14000
14001 if (invert)
14002 result ^= 1;
14003 return constant_boolean_node (result, type);
14004 }
14005
14006 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14007 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14008 itself. */
14009
14010 tree
14011 fold_build_cleanup_point_expr (tree type, tree expr)
14012 {
14013 /* If the expression does not have side effects then we don't have to wrap
14014 it with a cleanup point expression. */
14015 if (!TREE_SIDE_EFFECTS (expr))
14016 return expr;
14017
14018 /* If the expression is a return, check to see if the expression inside the
14019 return has no side effects or the right hand side of the modify expression
14020 inside the return. If either don't have side effects set we don't need to
14021 wrap the expression in a cleanup point expression. Note we don't check the
14022 left hand side of the modify because it should always be a return decl. */
14023 if (TREE_CODE (expr) == RETURN_EXPR)
14024 {
14025 tree op = TREE_OPERAND (expr, 0);
14026 if (!op || !TREE_SIDE_EFFECTS (op))
14027 return expr;
14028 op = TREE_OPERAND (op, 1);
14029 if (!TREE_SIDE_EFFECTS (op))
14030 return expr;
14031 }
14032
14033 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14034 }
14035
14036 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14037 of an indirection through OP0, or NULL_TREE if no simplification is
14038 possible. */
14039
14040 tree
14041 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14042 {
14043 tree sub = op0;
14044 tree subtype;
14045
14046 STRIP_NOPS (sub);
14047 subtype = TREE_TYPE (sub);
14048 if (!POINTER_TYPE_P (subtype)
14049 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14050 return NULL_TREE;
14051
14052 if (TREE_CODE (sub) == ADDR_EXPR)
14053 {
14054 tree op = TREE_OPERAND (sub, 0);
14055 tree optype = TREE_TYPE (op);
14056 /* *&CONST_DECL -> to the value of the const decl. */
14057 if (TREE_CODE (op) == CONST_DECL)
14058 return DECL_INITIAL (op);
14059 /* *&p => p; make sure to handle *&"str"[cst] here. */
14060 if (type == optype)
14061 {
14062 tree fop = fold_read_from_constant_string (op);
14063 if (fop)
14064 return fop;
14065 else
14066 return op;
14067 }
14068 /* *(foo *)&fooarray => fooarray[0] */
14069 else if (TREE_CODE (optype) == ARRAY_TYPE
14070 && type == TREE_TYPE (optype)
14071 && (!in_gimple_form
14072 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14073 {
14074 tree type_domain = TYPE_DOMAIN (optype);
14075 tree min_val = size_zero_node;
14076 if (type_domain && TYPE_MIN_VALUE (type_domain))
14077 min_val = TYPE_MIN_VALUE (type_domain);
14078 if (in_gimple_form
14079 && TREE_CODE (min_val) != INTEGER_CST)
14080 return NULL_TREE;
14081 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14082 NULL_TREE, NULL_TREE);
14083 }
14084 /* *(foo *)&complexfoo => __real__ complexfoo */
14085 else if (TREE_CODE (optype) == COMPLEX_TYPE
14086 && type == TREE_TYPE (optype))
14087 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14088 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14089 else if (TREE_CODE (optype) == VECTOR_TYPE
14090 && type == TREE_TYPE (optype))
14091 {
14092 tree part_width = TYPE_SIZE (type);
14093 tree index = bitsize_int (0);
14094 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14095 }
14096 }
14097
14098 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14099 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14100 {
14101 tree op00 = TREE_OPERAND (sub, 0);
14102 tree op01 = TREE_OPERAND (sub, 1);
14103
14104 STRIP_NOPS (op00);
14105 if (TREE_CODE (op00) == ADDR_EXPR)
14106 {
14107 tree op00type;
14108 op00 = TREE_OPERAND (op00, 0);
14109 op00type = TREE_TYPE (op00);
14110
14111 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14112 if (TREE_CODE (op00type) == VECTOR_TYPE
14113 && type == TREE_TYPE (op00type))
14114 {
14115 tree part_width = TYPE_SIZE (type);
14116 unsigned HOST_WIDE_INT max_offset
14117 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14118 * TYPE_VECTOR_SUBPARTS (op00type));
14119 if (tree_int_cst_sign_bit (op01) == 0
14120 && compare_tree_int (op01, max_offset) == -1)
14121 {
14122 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14123 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14124 tree index = bitsize_int (indexi);
14125 return fold_build3_loc (loc,
14126 BIT_FIELD_REF, type, op00,
14127 part_width, index);
14128 }
14129 }
14130 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14131 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14132 && type == TREE_TYPE (op00type))
14133 {
14134 tree size = TYPE_SIZE_UNIT (type);
14135 if (tree_int_cst_equal (size, op01))
14136 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14137 }
14138 /* ((foo *)&fooarray)[1] => fooarray[1] */
14139 else if (TREE_CODE (op00type) == ARRAY_TYPE
14140 && type == TREE_TYPE (op00type))
14141 {
14142 tree type_domain = TYPE_DOMAIN (op00type);
14143 tree min_val = size_zero_node;
14144 if (type_domain && TYPE_MIN_VALUE (type_domain))
14145 min_val = TYPE_MIN_VALUE (type_domain);
14146 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14147 TYPE_SIZE_UNIT (type));
14148 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14149 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14150 NULL_TREE, NULL_TREE);
14151 }
14152 }
14153 }
14154
14155 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14156 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14157 && type == TREE_TYPE (TREE_TYPE (subtype))
14158 && (!in_gimple_form
14159 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14160 {
14161 tree type_domain;
14162 tree min_val = size_zero_node;
14163 sub = build_fold_indirect_ref_loc (loc, sub);
14164 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14165 if (type_domain && TYPE_MIN_VALUE (type_domain))
14166 min_val = TYPE_MIN_VALUE (type_domain);
14167 if (in_gimple_form
14168 && TREE_CODE (min_val) != INTEGER_CST)
14169 return NULL_TREE;
14170 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14171 NULL_TREE);
14172 }
14173
14174 return NULL_TREE;
14175 }
14176
14177 /* Builds an expression for an indirection through T, simplifying some
14178 cases. */
14179
14180 tree
14181 build_fold_indirect_ref_loc (location_t loc, tree t)
14182 {
14183 tree type = TREE_TYPE (TREE_TYPE (t));
14184 tree sub = fold_indirect_ref_1 (loc, type, t);
14185
14186 if (sub)
14187 return sub;
14188
14189 return build1_loc (loc, INDIRECT_REF, type, t);
14190 }
14191
14192 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14193
14194 tree
14195 fold_indirect_ref_loc (location_t loc, tree t)
14196 {
14197 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14198
14199 if (sub)
14200 return sub;
14201 else
14202 return t;
14203 }
14204
14205 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14206 whose result is ignored. The type of the returned tree need not be
14207 the same as the original expression. */
14208
14209 tree
14210 fold_ignored_result (tree t)
14211 {
14212 if (!TREE_SIDE_EFFECTS (t))
14213 return integer_zero_node;
14214
14215 for (;;)
14216 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14217 {
14218 case tcc_unary:
14219 t = TREE_OPERAND (t, 0);
14220 break;
14221
14222 case tcc_binary:
14223 case tcc_comparison:
14224 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14225 t = TREE_OPERAND (t, 0);
14226 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14227 t = TREE_OPERAND (t, 1);
14228 else
14229 return t;
14230 break;
14231
14232 case tcc_expression:
14233 switch (TREE_CODE (t))
14234 {
14235 case COMPOUND_EXPR:
14236 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14237 return t;
14238 t = TREE_OPERAND (t, 0);
14239 break;
14240
14241 case COND_EXPR:
14242 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14243 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14244 return t;
14245 t = TREE_OPERAND (t, 0);
14246 break;
14247
14248 default:
14249 return t;
14250 }
14251 break;
14252
14253 default:
14254 return t;
14255 }
14256 }
14257
14258 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14259
14260 tree
14261 round_up_loc (location_t loc, tree value, unsigned int divisor)
14262 {
14263 tree div = NULL_TREE;
14264
14265 if (divisor == 1)
14266 return value;
14267
14268 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14269 have to do anything. Only do this when we are not given a const,
14270 because in that case, this check is more expensive than just
14271 doing it. */
14272 if (TREE_CODE (value) != INTEGER_CST)
14273 {
14274 div = build_int_cst (TREE_TYPE (value), divisor);
14275
14276 if (multiple_of_p (TREE_TYPE (value), value, div))
14277 return value;
14278 }
14279
14280 /* If divisor is a power of two, simplify this to bit manipulation. */
14281 if (pow2_or_zerop (divisor))
14282 {
14283 if (TREE_CODE (value) == INTEGER_CST)
14284 {
14285 wide_int val = value;
14286 bool overflow_p;
14287
14288 if ((val & (divisor - 1)) == 0)
14289 return value;
14290
14291 overflow_p = TREE_OVERFLOW (value);
14292 val += divisor - 1;
14293 val &= (int) -divisor;
14294 if (val == 0)
14295 overflow_p = true;
14296
14297 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14298 }
14299 else
14300 {
14301 tree t;
14302
14303 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14304 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14305 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14306 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14307 }
14308 }
14309 else
14310 {
14311 if (!div)
14312 div = build_int_cst (TREE_TYPE (value), divisor);
14313 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14314 value = size_binop_loc (loc, MULT_EXPR, value, div);
14315 }
14316
14317 return value;
14318 }
14319
14320 /* Likewise, but round down. */
14321
14322 tree
14323 round_down_loc (location_t loc, tree value, int divisor)
14324 {
14325 tree div = NULL_TREE;
14326
14327 gcc_assert (divisor > 0);
14328 if (divisor == 1)
14329 return value;
14330
14331 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14332 have to do anything. Only do this when we are not given a const,
14333 because in that case, this check is more expensive than just
14334 doing it. */
14335 if (TREE_CODE (value) != INTEGER_CST)
14336 {
14337 div = build_int_cst (TREE_TYPE (value), divisor);
14338
14339 if (multiple_of_p (TREE_TYPE (value), value, div))
14340 return value;
14341 }
14342
14343 /* If divisor is a power of two, simplify this to bit manipulation. */
14344 if (pow2_or_zerop (divisor))
14345 {
14346 tree t;
14347
14348 t = build_int_cst (TREE_TYPE (value), -divisor);
14349 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14350 }
14351 else
14352 {
14353 if (!div)
14354 div = build_int_cst (TREE_TYPE (value), divisor);
14355 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14356 value = size_binop_loc (loc, MULT_EXPR, value, div);
14357 }
14358
14359 return value;
14360 }
14361
14362 /* Returns the pointer to the base of the object addressed by EXP and
14363 extracts the information about the offset of the access, storing it
14364 to PBITPOS and POFFSET. */
14365
14366 static tree
14367 split_address_to_core_and_offset (tree exp,
14368 HOST_WIDE_INT *pbitpos, tree *poffset)
14369 {
14370 tree core;
14371 machine_mode mode;
14372 int unsignedp, reversep, volatilep;
14373 HOST_WIDE_INT bitsize;
14374 location_t loc = EXPR_LOCATION (exp);
14375
14376 if (TREE_CODE (exp) == ADDR_EXPR)
14377 {
14378 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14379 poffset, &mode, &unsignedp, &reversep,
14380 &volatilep);
14381 core = build_fold_addr_expr_loc (loc, core);
14382 }
14383 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14384 {
14385 core = TREE_OPERAND (exp, 0);
14386 STRIP_NOPS (core);
14387 *pbitpos = 0;
14388 *poffset = TREE_OPERAND (exp, 1);
14389 if (TREE_CODE (*poffset) == INTEGER_CST)
14390 {
14391 offset_int tem = wi::sext (wi::to_offset (*poffset),
14392 TYPE_PRECISION (TREE_TYPE (*poffset)));
14393 tem <<= LOG2_BITS_PER_UNIT;
14394 if (wi::fits_shwi_p (tem))
14395 {
14396 *pbitpos = tem.to_shwi ();
14397 *poffset = NULL_TREE;
14398 }
14399 }
14400 }
14401 else
14402 {
14403 core = exp;
14404 *pbitpos = 0;
14405 *poffset = NULL_TREE;
14406 }
14407
14408 return core;
14409 }
14410
14411 /* Returns true if addresses of E1 and E2 differ by a constant, false
14412 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14413
14414 bool
14415 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14416 {
14417 tree core1, core2;
14418 HOST_WIDE_INT bitpos1, bitpos2;
14419 tree toffset1, toffset2, tdiff, type;
14420
14421 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14422 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14423
14424 if (bitpos1 % BITS_PER_UNIT != 0
14425 || bitpos2 % BITS_PER_UNIT != 0
14426 || !operand_equal_p (core1, core2, 0))
14427 return false;
14428
14429 if (toffset1 && toffset2)
14430 {
14431 type = TREE_TYPE (toffset1);
14432 if (type != TREE_TYPE (toffset2))
14433 toffset2 = fold_convert (type, toffset2);
14434
14435 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14436 if (!cst_and_fits_in_hwi (tdiff))
14437 return false;
14438
14439 *diff = int_cst_value (tdiff);
14440 }
14441 else if (toffset1 || toffset2)
14442 {
14443 /* If only one of the offsets is non-constant, the difference cannot
14444 be a constant. */
14445 return false;
14446 }
14447 else
14448 *diff = 0;
14449
14450 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14451 return true;
14452 }
14453
14454 /* Return OFF converted to a pointer offset type suitable as offset for
14455 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14456 tree
14457 convert_to_ptrofftype_loc (location_t loc, tree off)
14458 {
14459 return fold_convert_loc (loc, sizetype, off);
14460 }
14461
14462 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14463 tree
14464 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14465 {
14466 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14467 ptr, convert_to_ptrofftype_loc (loc, off));
14468 }
14469
14470 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14471 tree
14472 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14473 {
14474 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14475 ptr, size_int (off));
14476 }
14477
14478 /* Return a char pointer for a C string if it is a string constant
14479 or sum of string constant and integer constant. We only support
14480 string constants properly terminated with '\0' character.
14481 If STRLEN is a valid pointer, length (including terminating character)
14482 of returned string is stored to the argument. */
14483
14484 const char *
14485 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14486 {
14487 tree offset_node;
14488
14489 if (strlen)
14490 *strlen = 0;
14491
14492 src = string_constant (src, &offset_node);
14493 if (src == 0)
14494 return NULL;
14495
14496 unsigned HOST_WIDE_INT offset = 0;
14497 if (offset_node != NULL_TREE)
14498 {
14499 if (!tree_fits_uhwi_p (offset_node))
14500 return NULL;
14501 else
14502 offset = tree_to_uhwi (offset_node);
14503 }
14504
14505 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14506 const char *string = TREE_STRING_POINTER (src);
14507
14508 /* Support only properly null-terminated strings. */
14509 if (string_length == 0
14510 || string[string_length - 1] != '\0'
14511 || offset >= string_length)
14512 return NULL;
14513
14514 if (strlen)
14515 *strlen = string_length - offset;
14516 return string + offset;
14517 }
14518
14519 #if CHECKING_P
14520
14521 namespace selftest {
14522
14523 /* Helper functions for writing tests of folding trees. */
14524
14525 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14526
14527 static void
14528 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14529 tree constant)
14530 {
14531 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14532 }
14533
14534 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14535 wrapping WRAPPED_EXPR. */
14536
14537 static void
14538 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14539 tree wrapped_expr)
14540 {
14541 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14542 ASSERT_NE (wrapped_expr, result);
14543 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14544 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14545 }
14546
14547 /* Verify that various arithmetic binary operations are folded
14548 correctly. */
14549
14550 static void
14551 test_arithmetic_folding ()
14552 {
14553 tree type = integer_type_node;
14554 tree x = create_tmp_var_raw (type, "x");
14555 tree zero = build_zero_cst (type);
14556 tree one = build_int_cst (type, 1);
14557
14558 /* Addition. */
14559 /* 1 <-- (0 + 1) */
14560 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14561 one);
14562 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14563 one);
14564
14565 /* (nonlvalue)x <-- (x + 0) */
14566 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14567 x);
14568
14569 /* Subtraction. */
14570 /* 0 <-- (x - x) */
14571 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14572 zero);
14573 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14574 x);
14575
14576 /* Multiplication. */
14577 /* 0 <-- (x * 0) */
14578 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14579 zero);
14580
14581 /* (nonlvalue)x <-- (x * 1) */
14582 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14583 x);
14584 }
14585
14586 /* Verify that various binary operations on vectors are folded
14587 correctly. */
14588
14589 static void
14590 test_vector_folding ()
14591 {
14592 tree inner_type = integer_type_node;
14593 tree type = build_vector_type (inner_type, 4);
14594 tree zero = build_zero_cst (type);
14595 tree one = build_one_cst (type);
14596
14597 /* Verify equality tests that return a scalar boolean result. */
14598 tree res_type = boolean_type_node;
14599 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14600 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14601 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14602 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14603 }
14604
14605 /* Run all of the selftests within this file. */
14606
14607 void
14608 fold_const_c_tests ()
14609 {
14610 test_arithmetic_folding ();
14611 test_vector_folding ();
14612 }
14613
14614 } // namespace selftest
14615
14616 #endif /* CHECKING_P */