genmatch.c (add_operator): Treat ADDR_EXPR as atom.
[gcc.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2015 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 "tm.h"
47 #include "flags.h"
48 #include "alias.h"
49 #include "symtab.h"
50 #include "tree.h"
51 #include "fold-const.h"
52 #include "stor-layout.h"
53 #include "calls.h"
54 #include "tree-iterator.h"
55 #include "realmpfr.h"
56 #include "rtl.h"
57 #include "hard-reg-set.h"
58 #include "function.h"
59 #include "insn-config.h"
60 #include "expmed.h"
61 #include "dojump.h"
62 #include "explow.h"
63 #include "emit-rtl.h"
64 #include "varasm.h"
65 #include "stmt.h"
66 #include "expr.h"
67 #include "tm_p.h"
68 #include "target.h"
69 #include "diagnostic-core.h"
70 #include "intl.h"
71 #include "langhooks.h"
72 #include "md5.h"
73 #include "predict.h"
74 #include "basic-block.h"
75 #include "tree-ssa-alias.h"
76 #include "internal-fn.h"
77 #include "tree-eh.h"
78 #include "gimple-expr.h"
79 #include "gimple.h"
80 #include "gimplify.h"
81 #include "tree-dfa.h"
82 #include "builtins.h"
83 #include "cgraph.h"
84 #include "generic-match.h"
85 #include "optabs.h"
86
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
90
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
111 };
112
113 static bool negate_mathfn_p (enum built_in_function);
114 static bool negate_expr_p (tree);
115 static tree negate_expr (tree);
116 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
117 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
118 static enum comparison_code comparison_to_compcode (enum tree_code);
119 static enum tree_code compcode_to_comparison (enum comparison_code);
120 static int operand_equal_for_comparison_p (tree, tree, tree);
121 static int twoval_comparison_p (tree, tree *, tree *, int *);
122 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
123 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
124 static tree make_bit_field_ref (location_t, tree, tree,
125 HOST_WIDE_INT, HOST_WIDE_INT, int);
126 static tree optimize_bit_field_compare (location_t, enum tree_code,
127 tree, tree, tree);
128 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
129 HOST_WIDE_INT *,
130 machine_mode *, int *, int *,
131 tree *, tree *);
132 static int simple_operand_p (const_tree);
133 static bool simple_operand_p_2 (tree);
134 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
135 static tree range_predecessor (tree);
136 static tree range_successor (tree);
137 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
138 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
139 static tree unextend (tree, int, int, tree);
140 static tree optimize_minmax_comparison (location_t, enum tree_code,
141 tree, tree, tree);
142 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
143 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
144 static tree fold_binary_op_with_conditional_arg (location_t,
145 enum tree_code, tree,
146 tree, tree,
147 tree, tree, int);
148 static tree fold_mathfn_compare (location_t,
149 enum built_in_function, enum tree_code,
150 tree, tree, tree);
151 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
152 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
153 static bool reorder_operands_p (const_tree, const_tree);
154 static tree fold_negate_const (tree, tree);
155 static tree fold_not_const (const_tree, tree);
156 static tree fold_relational_const (enum tree_code, tree, tree, tree);
157 static tree fold_convert_const (enum tree_code, tree, tree);
158 static tree fold_view_convert_expr (tree, tree);
159 static bool vec_cst_ctor_to_array (tree, tree *);
160
161
162 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
163 Otherwise, return LOC. */
164
165 static location_t
166 expr_location_or (tree t, location_t loc)
167 {
168 location_t tloc = EXPR_LOCATION (t);
169 return tloc == UNKNOWN_LOCATION ? loc : tloc;
170 }
171
172 /* Similar to protected_set_expr_location, but never modify x in place,
173 if location can and needs to be set, unshare it. */
174
175 static inline tree
176 protected_set_expr_location_unshare (tree x, location_t loc)
177 {
178 if (CAN_HAVE_LOCATION_P (x)
179 && EXPR_LOCATION (x) != loc
180 && !(TREE_CODE (x) == SAVE_EXPR
181 || TREE_CODE (x) == TARGET_EXPR
182 || TREE_CODE (x) == BIND_EXPR))
183 {
184 x = copy_node (x);
185 SET_EXPR_LOCATION (x, loc);
186 }
187 return x;
188 }
189 \f
190 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
191 division and returns the quotient. Otherwise returns
192 NULL_TREE. */
193
194 tree
195 div_if_zero_remainder (const_tree arg1, const_tree arg2)
196 {
197 widest_int quo;
198
199 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
200 SIGNED, &quo))
201 return wide_int_to_tree (TREE_TYPE (arg1), quo);
202
203 return NULL_TREE;
204 }
205 \f
206 /* This is nonzero if we should defer warnings about undefined
207 overflow. This facility exists because these warnings are a
208 special case. The code to estimate loop iterations does not want
209 to issue any warnings, since it works with expressions which do not
210 occur in user code. Various bits of cleanup code call fold(), but
211 only use the result if it has certain characteristics (e.g., is a
212 constant); that code only wants to issue a warning if the result is
213 used. */
214
215 static int fold_deferring_overflow_warnings;
216
217 /* If a warning about undefined overflow is deferred, this is the
218 warning. Note that this may cause us to turn two warnings into
219 one, but that is fine since it is sufficient to only give one
220 warning per expression. */
221
222 static const char* fold_deferred_overflow_warning;
223
224 /* If a warning about undefined overflow is deferred, this is the
225 level at which the warning should be emitted. */
226
227 static enum warn_strict_overflow_code fold_deferred_overflow_code;
228
229 /* Start deferring overflow warnings. We could use a stack here to
230 permit nested calls, but at present it is not necessary. */
231
232 void
233 fold_defer_overflow_warnings (void)
234 {
235 ++fold_deferring_overflow_warnings;
236 }
237
238 /* Stop deferring overflow warnings. If there is a pending warning,
239 and ISSUE is true, then issue the warning if appropriate. STMT is
240 the statement with which the warning should be associated (used for
241 location information); STMT may be NULL. CODE is the level of the
242 warning--a warn_strict_overflow_code value. This function will use
243 the smaller of CODE and the deferred code when deciding whether to
244 issue the warning. CODE may be zero to mean to always use the
245 deferred code. */
246
247 void
248 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
249 {
250 const char *warnmsg;
251 location_t locus;
252
253 gcc_assert (fold_deferring_overflow_warnings > 0);
254 --fold_deferring_overflow_warnings;
255 if (fold_deferring_overflow_warnings > 0)
256 {
257 if (fold_deferred_overflow_warning != NULL
258 && code != 0
259 && code < (int) fold_deferred_overflow_code)
260 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
261 return;
262 }
263
264 warnmsg = fold_deferred_overflow_warning;
265 fold_deferred_overflow_warning = NULL;
266
267 if (!issue || warnmsg == NULL)
268 return;
269
270 if (gimple_no_warning_p (stmt))
271 return;
272
273 /* Use the smallest code level when deciding to issue the
274 warning. */
275 if (code == 0 || code > (int) fold_deferred_overflow_code)
276 code = fold_deferred_overflow_code;
277
278 if (!issue_strict_overflow_warning (code))
279 return;
280
281 if (stmt == NULL)
282 locus = input_location;
283 else
284 locus = gimple_location (stmt);
285 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
286 }
287
288 /* Stop deferring overflow warnings, ignoring any deferred
289 warnings. */
290
291 void
292 fold_undefer_and_ignore_overflow_warnings (void)
293 {
294 fold_undefer_overflow_warnings (false, NULL, 0);
295 }
296
297 /* Whether we are deferring overflow warnings. */
298
299 bool
300 fold_deferring_overflow_warnings_p (void)
301 {
302 return fold_deferring_overflow_warnings > 0;
303 }
304
305 /* This is called when we fold something based on the fact that signed
306 overflow is undefined. */
307
308 static void
309 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
310 {
311 if (fold_deferring_overflow_warnings > 0)
312 {
313 if (fold_deferred_overflow_warning == NULL
314 || wc < fold_deferred_overflow_code)
315 {
316 fold_deferred_overflow_warning = gmsgid;
317 fold_deferred_overflow_code = wc;
318 }
319 }
320 else if (issue_strict_overflow_warning (wc))
321 warning (OPT_Wstrict_overflow, gmsgid);
322 }
323 \f
324 /* Return true if the built-in mathematical function specified by CODE
325 is odd, i.e. -f(x) == f(-x). */
326
327 static bool
328 negate_mathfn_p (enum built_in_function code)
329 {
330 switch (code)
331 {
332 CASE_FLT_FN (BUILT_IN_ASIN):
333 CASE_FLT_FN (BUILT_IN_ASINH):
334 CASE_FLT_FN (BUILT_IN_ATAN):
335 CASE_FLT_FN (BUILT_IN_ATANH):
336 CASE_FLT_FN (BUILT_IN_CASIN):
337 CASE_FLT_FN (BUILT_IN_CASINH):
338 CASE_FLT_FN (BUILT_IN_CATAN):
339 CASE_FLT_FN (BUILT_IN_CATANH):
340 CASE_FLT_FN (BUILT_IN_CBRT):
341 CASE_FLT_FN (BUILT_IN_CPROJ):
342 CASE_FLT_FN (BUILT_IN_CSIN):
343 CASE_FLT_FN (BUILT_IN_CSINH):
344 CASE_FLT_FN (BUILT_IN_CTAN):
345 CASE_FLT_FN (BUILT_IN_CTANH):
346 CASE_FLT_FN (BUILT_IN_ERF):
347 CASE_FLT_FN (BUILT_IN_LLROUND):
348 CASE_FLT_FN (BUILT_IN_LROUND):
349 CASE_FLT_FN (BUILT_IN_ROUND):
350 CASE_FLT_FN (BUILT_IN_SIN):
351 CASE_FLT_FN (BUILT_IN_SINH):
352 CASE_FLT_FN (BUILT_IN_TAN):
353 CASE_FLT_FN (BUILT_IN_TANH):
354 CASE_FLT_FN (BUILT_IN_TRUNC):
355 return true;
356
357 CASE_FLT_FN (BUILT_IN_LLRINT):
358 CASE_FLT_FN (BUILT_IN_LRINT):
359 CASE_FLT_FN (BUILT_IN_NEARBYINT):
360 CASE_FLT_FN (BUILT_IN_RINT):
361 return !flag_rounding_math;
362
363 default:
364 break;
365 }
366 return false;
367 }
368
369 /* Check whether we may negate an integer constant T without causing
370 overflow. */
371
372 bool
373 may_negate_without_overflow_p (const_tree t)
374 {
375 tree type;
376
377 gcc_assert (TREE_CODE (t) == INTEGER_CST);
378
379 type = TREE_TYPE (t);
380 if (TYPE_UNSIGNED (type))
381 return false;
382
383 return !wi::only_sign_bit_p (t);
384 }
385
386 /* Determine whether an expression T can be cheaply negated using
387 the function negate_expr without introducing undefined overflow. */
388
389 static bool
390 negate_expr_p (tree t)
391 {
392 tree type;
393
394 if (t == 0)
395 return false;
396
397 type = TREE_TYPE (t);
398
399 STRIP_SIGN_NOPS (t);
400 switch (TREE_CODE (t))
401 {
402 case INTEGER_CST:
403 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
404 return true;
405
406 /* Check that -CST will not overflow type. */
407 return may_negate_without_overflow_p (t);
408 case BIT_NOT_EXPR:
409 return (INTEGRAL_TYPE_P (type)
410 && TYPE_OVERFLOW_WRAPS (type));
411
412 case FIXED_CST:
413 return true;
414
415 case NEGATE_EXPR:
416 return !TYPE_OVERFLOW_SANITIZED (type);
417
418 case REAL_CST:
419 /* We want to canonicalize to positive real constants. Pretend
420 that only negative ones can be easily negated. */
421 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
422
423 case COMPLEX_CST:
424 return negate_expr_p (TREE_REALPART (t))
425 && negate_expr_p (TREE_IMAGPART (t));
426
427 case VECTOR_CST:
428 {
429 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
430 return true;
431
432 int count = TYPE_VECTOR_SUBPARTS (type), i;
433
434 for (i = 0; i < count; i++)
435 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
436 return false;
437
438 return true;
439 }
440
441 case COMPLEX_EXPR:
442 return negate_expr_p (TREE_OPERAND (t, 0))
443 && negate_expr_p (TREE_OPERAND (t, 1));
444
445 case CONJ_EXPR:
446 return negate_expr_p (TREE_OPERAND (t, 0));
447
448 case PLUS_EXPR:
449 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
450 || HONOR_SIGNED_ZEROS (element_mode (type)))
451 return false;
452 /* -(A + B) -> (-B) - A. */
453 if (negate_expr_p (TREE_OPERAND (t, 1))
454 && reorder_operands_p (TREE_OPERAND (t, 0),
455 TREE_OPERAND (t, 1)))
456 return true;
457 /* -(A + B) -> (-A) - B. */
458 return negate_expr_p (TREE_OPERAND (t, 0));
459
460 case MINUS_EXPR:
461 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
462 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
463 && !HONOR_SIGNED_ZEROS (element_mode (type))
464 && reorder_operands_p (TREE_OPERAND (t, 0),
465 TREE_OPERAND (t, 1));
466
467 case MULT_EXPR:
468 if (TYPE_UNSIGNED (TREE_TYPE (t)))
469 break;
470
471 /* Fall through. */
472
473 case RDIV_EXPR:
474 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
475 return negate_expr_p (TREE_OPERAND (t, 1))
476 || negate_expr_p (TREE_OPERAND (t, 0));
477 break;
478
479 case TRUNC_DIV_EXPR:
480 case ROUND_DIV_EXPR:
481 case EXACT_DIV_EXPR:
482 /* In general we can't negate A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. But if overflow is
485 undefined, we can negate, because - (INT_MIN / 1) is an
486 overflow. */
487 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
488 {
489 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
490 break;
491 /* If overflow is undefined then we have to be careful because
492 we ask whether it's ok to associate the negate with the
493 division which is not ok for example for
494 -((a - b) / c) where (-(a - b)) / c may invoke undefined
495 overflow because of negating INT_MIN. So do not use
496 negate_expr_p here but open-code the two important cases. */
497 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
498 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
499 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
500 return true;
501 }
502 else if (negate_expr_p (TREE_OPERAND (t, 0)))
503 return true;
504 return negate_expr_p (TREE_OPERAND (t, 1));
505
506 case NOP_EXPR:
507 /* Negate -((double)float) as (double)(-float). */
508 if (TREE_CODE (type) == REAL_TYPE)
509 {
510 tree tem = strip_float_extensions (t);
511 if (tem != t)
512 return negate_expr_p (tem);
513 }
514 break;
515
516 case CALL_EXPR:
517 /* Negate -f(x) as f(-x). */
518 if (negate_mathfn_p (builtin_mathfn_code (t)))
519 return negate_expr_p (CALL_EXPR_ARG (t, 0));
520 break;
521
522 case RSHIFT_EXPR:
523 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
524 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
525 {
526 tree op1 = TREE_OPERAND (t, 1);
527 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
528 return true;
529 }
530 break;
531
532 default:
533 break;
534 }
535 return false;
536 }
537
538 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
539 simplification is possible.
540 If negate_expr_p would return true for T, NULL_TREE will never be
541 returned. */
542
543 static tree
544 fold_negate_expr (location_t loc, tree t)
545 {
546 tree type = TREE_TYPE (t);
547 tree tem;
548
549 switch (TREE_CODE (t))
550 {
551 /* Convert - (~A) to A + 1. */
552 case BIT_NOT_EXPR:
553 if (INTEGRAL_TYPE_P (type))
554 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
555 build_one_cst (type));
556 break;
557
558 case INTEGER_CST:
559 tem = fold_negate_const (t, type);
560 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
561 || (ANY_INTEGRAL_TYPE_P (type)
562 && !TYPE_OVERFLOW_TRAPS (type)
563 && TYPE_OVERFLOW_WRAPS (type))
564 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
565 return tem;
566 break;
567
568 case REAL_CST:
569 tem = fold_negate_const (t, type);
570 return tem;
571
572 case FIXED_CST:
573 tem = fold_negate_const (t, type);
574 return tem;
575
576 case COMPLEX_CST:
577 {
578 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
579 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
580 if (rpart && ipart)
581 return build_complex (type, rpart, ipart);
582 }
583 break;
584
585 case VECTOR_CST:
586 {
587 int count = TYPE_VECTOR_SUBPARTS (type), i;
588 tree *elts = XALLOCAVEC (tree, count);
589
590 for (i = 0; i < count; i++)
591 {
592 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
593 if (elts[i] == NULL_TREE)
594 return NULL_TREE;
595 }
596
597 return build_vector (type, elts);
598 }
599
600 case COMPLEX_EXPR:
601 if (negate_expr_p (t))
602 return fold_build2_loc (loc, COMPLEX_EXPR, type,
603 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
604 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
605 break;
606
607 case CONJ_EXPR:
608 if (negate_expr_p (t))
609 return fold_build1_loc (loc, CONJ_EXPR, type,
610 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
611 break;
612
613 case NEGATE_EXPR:
614 if (!TYPE_OVERFLOW_SANITIZED (type))
615 return TREE_OPERAND (t, 0);
616 break;
617
618 case PLUS_EXPR:
619 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
620 && !HONOR_SIGNED_ZEROS (element_mode (type)))
621 {
622 /* -(A + B) -> (-B) - A. */
623 if (negate_expr_p (TREE_OPERAND (t, 1))
624 && reorder_operands_p (TREE_OPERAND (t, 0),
625 TREE_OPERAND (t, 1)))
626 {
627 tem = negate_expr (TREE_OPERAND (t, 1));
628 return fold_build2_loc (loc, MINUS_EXPR, type,
629 tem, TREE_OPERAND (t, 0));
630 }
631
632 /* -(A + B) -> (-A) - B. */
633 if (negate_expr_p (TREE_OPERAND (t, 0)))
634 {
635 tem = negate_expr (TREE_OPERAND (t, 0));
636 return fold_build2_loc (loc, MINUS_EXPR, type,
637 tem, TREE_OPERAND (t, 1));
638 }
639 }
640 break;
641
642 case MINUS_EXPR:
643 /* - (A - B) -> B - A */
644 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
645 && !HONOR_SIGNED_ZEROS (element_mode (type))
646 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
647 return fold_build2_loc (loc, MINUS_EXPR, type,
648 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
649 break;
650
651 case MULT_EXPR:
652 if (TYPE_UNSIGNED (type))
653 break;
654
655 /* Fall through. */
656
657 case RDIV_EXPR:
658 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
659 {
660 tem = TREE_OPERAND (t, 1);
661 if (negate_expr_p (tem))
662 return fold_build2_loc (loc, TREE_CODE (t), type,
663 TREE_OPERAND (t, 0), negate_expr (tem));
664 tem = TREE_OPERAND (t, 0);
665 if (negate_expr_p (tem))
666 return fold_build2_loc (loc, TREE_CODE (t), type,
667 negate_expr (tem), TREE_OPERAND (t, 1));
668 }
669 break;
670
671 case TRUNC_DIV_EXPR:
672 case ROUND_DIV_EXPR:
673 case EXACT_DIV_EXPR:
674 /* In general we can't negate A / B, because if A is INT_MIN and
675 B is 1, we may turn this into INT_MIN / -1 which is undefined
676 and actually traps on some architectures. But if overflow is
677 undefined, we can negate, because - (INT_MIN / 1) is an
678 overflow. */
679 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
680 {
681 const char * const warnmsg = G_("assuming signed overflow does not "
682 "occur when negating a division");
683 tem = TREE_OPERAND (t, 1);
684 if (negate_expr_p (tem))
685 {
686 if (INTEGRAL_TYPE_P (type)
687 && (TREE_CODE (tem) != INTEGER_CST
688 || integer_onep (tem)))
689 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
690 return fold_build2_loc (loc, TREE_CODE (t), type,
691 TREE_OPERAND (t, 0), negate_expr (tem));
692 }
693 /* If overflow is undefined then we have to be careful because
694 we ask whether it's ok to associate the negate with the
695 division which is not ok for example for
696 -((a - b) / c) where (-(a - b)) / c may invoke undefined
697 overflow because of negating INT_MIN. So do not use
698 negate_expr_p here but open-code the two important cases. */
699 tem = TREE_OPERAND (t, 0);
700 if ((INTEGRAL_TYPE_P (type)
701 && (TREE_CODE (tem) == NEGATE_EXPR
702 || (TREE_CODE (tem) == INTEGER_CST
703 && may_negate_without_overflow_p (tem))))
704 || !INTEGRAL_TYPE_P (type))
705 return fold_build2_loc (loc, TREE_CODE (t), type,
706 negate_expr (tem), TREE_OPERAND (t, 1));
707 }
708 break;
709
710 case NOP_EXPR:
711 /* Convert -((double)float) into (double)(-float). */
712 if (TREE_CODE (type) == REAL_TYPE)
713 {
714 tem = strip_float_extensions (t);
715 if (tem != t && negate_expr_p (tem))
716 return fold_convert_loc (loc, type, negate_expr (tem));
717 }
718 break;
719
720 case CALL_EXPR:
721 /* Negate -f(x) as f(-x). */
722 if (negate_mathfn_p (builtin_mathfn_code (t))
723 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
724 {
725 tree fndecl, arg;
726
727 fndecl = get_callee_fndecl (t);
728 arg = negate_expr (CALL_EXPR_ARG (t, 0));
729 return build_call_expr_loc (loc, fndecl, 1, arg);
730 }
731 break;
732
733 case RSHIFT_EXPR:
734 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
735 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
736 {
737 tree op1 = TREE_OPERAND (t, 1);
738 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
739 {
740 tree ntype = TYPE_UNSIGNED (type)
741 ? signed_type_for (type)
742 : unsigned_type_for (type);
743 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
744 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
745 return fold_convert_loc (loc, type, temp);
746 }
747 }
748 break;
749
750 default:
751 break;
752 }
753
754 return NULL_TREE;
755 }
756
757 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
758 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
759 return NULL_TREE. */
760
761 static tree
762 negate_expr (tree t)
763 {
764 tree type, tem;
765 location_t loc;
766
767 if (t == NULL_TREE)
768 return NULL_TREE;
769
770 loc = EXPR_LOCATION (t);
771 type = TREE_TYPE (t);
772 STRIP_SIGN_NOPS (t);
773
774 tem = fold_negate_expr (loc, t);
775 if (!tem)
776 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
777 return fold_convert_loc (loc, type, tem);
778 }
779 \f
780 /* Split a tree IN into a constant, literal and variable parts that could be
781 combined with CODE to make IN. "constant" means an expression with
782 TREE_CONSTANT but that isn't an actual constant. CODE must be a
783 commutative arithmetic operation. Store the constant part into *CONP,
784 the literal in *LITP and return the variable part. If a part isn't
785 present, set it to null. If the tree does not decompose in this way,
786 return the entire tree as the variable part and the other parts as null.
787
788 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
789 case, we negate an operand that was subtracted. Except if it is a
790 literal for which we use *MINUS_LITP instead.
791
792 If NEGATE_P is true, we are negating all of IN, again except a literal
793 for which we use *MINUS_LITP instead.
794
795 If IN is itself a literal or constant, return it as appropriate.
796
797 Note that we do not guarantee that any of the three values will be the
798 same type as IN, but they will have the same signedness and mode. */
799
800 static tree
801 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
802 tree *minus_litp, int negate_p)
803 {
804 tree var = 0;
805
806 *conp = 0;
807 *litp = 0;
808 *minus_litp = 0;
809
810 /* Strip any conversions that don't change the machine mode or signedness. */
811 STRIP_SIGN_NOPS (in);
812
813 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
814 || TREE_CODE (in) == FIXED_CST)
815 *litp = in;
816 else if (TREE_CODE (in) == code
817 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
818 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
819 /* We can associate addition and subtraction together (even
820 though the C standard doesn't say so) for integers because
821 the value is not affected. For reals, the value might be
822 affected, so we can't. */
823 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
824 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
825 {
826 tree op0 = TREE_OPERAND (in, 0);
827 tree op1 = TREE_OPERAND (in, 1);
828 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
829 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
830
831 /* First see if either of the operands is a literal, then a constant. */
832 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
833 || TREE_CODE (op0) == FIXED_CST)
834 *litp = op0, op0 = 0;
835 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
836 || TREE_CODE (op1) == FIXED_CST)
837 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
838
839 if (op0 != 0 && TREE_CONSTANT (op0))
840 *conp = op0, op0 = 0;
841 else if (op1 != 0 && TREE_CONSTANT (op1))
842 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
843
844 /* If we haven't dealt with either operand, this is not a case we can
845 decompose. Otherwise, VAR is either of the ones remaining, if any. */
846 if (op0 != 0 && op1 != 0)
847 var = in;
848 else if (op0 != 0)
849 var = op0;
850 else
851 var = op1, neg_var_p = neg1_p;
852
853 /* Now do any needed negations. */
854 if (neg_litp_p)
855 *minus_litp = *litp, *litp = 0;
856 if (neg_conp_p)
857 *conp = negate_expr (*conp);
858 if (neg_var_p)
859 var = negate_expr (var);
860 }
861 else if (TREE_CODE (in) == BIT_NOT_EXPR
862 && code == PLUS_EXPR)
863 {
864 /* -X - 1 is folded to ~X, undo that here. */
865 *minus_litp = build_one_cst (TREE_TYPE (in));
866 var = negate_expr (TREE_OPERAND (in, 0));
867 }
868 else if (TREE_CONSTANT (in))
869 *conp = in;
870 else
871 var = in;
872
873 if (negate_p)
874 {
875 if (*litp)
876 *minus_litp = *litp, *litp = 0;
877 else if (*minus_litp)
878 *litp = *minus_litp, *minus_litp = 0;
879 *conp = negate_expr (*conp);
880 var = negate_expr (var);
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 = wide_int::from (parg2, TYPE_PRECISION (type),
978 TYPE_SIGN (TREE_TYPE (parg2)));
979
980 switch (code)
981 {
982 case BIT_IOR_EXPR:
983 res = wi::bit_or (arg1, arg2);
984 break;
985
986 case BIT_XOR_EXPR:
987 res = wi::bit_xor (arg1, arg2);
988 break;
989
990 case BIT_AND_EXPR:
991 res = wi::bit_and (arg1, arg2);
992 break;
993
994 case RSHIFT_EXPR:
995 case LSHIFT_EXPR:
996 if (wi::neg_p (arg2))
997 {
998 arg2 = -arg2;
999 if (code == RSHIFT_EXPR)
1000 code = LSHIFT_EXPR;
1001 else
1002 code = RSHIFT_EXPR;
1003 }
1004
1005 if (code == RSHIFT_EXPR)
1006 /* It's unclear from the C standard whether shifts can overflow.
1007 The following code ignores overflow; perhaps a C standard
1008 interpretation ruling is needed. */
1009 res = wi::rshift (arg1, arg2, sign);
1010 else
1011 res = wi::lshift (arg1, arg2);
1012 break;
1013
1014 case RROTATE_EXPR:
1015 case LROTATE_EXPR:
1016 if (wi::neg_p (arg2))
1017 {
1018 arg2 = -arg2;
1019 if (code == RROTATE_EXPR)
1020 code = LROTATE_EXPR;
1021 else
1022 code = RROTATE_EXPR;
1023 }
1024
1025 if (code == RROTATE_EXPR)
1026 res = wi::rrotate (arg1, arg2);
1027 else
1028 res = wi::lrotate (arg1, arg2);
1029 break;
1030
1031 case PLUS_EXPR:
1032 res = wi::add (arg1, arg2, sign, &overflow);
1033 break;
1034
1035 case MINUS_EXPR:
1036 res = wi::sub (arg1, arg2, sign, &overflow);
1037 break;
1038
1039 case MULT_EXPR:
1040 res = wi::mul (arg1, arg2, sign, &overflow);
1041 break;
1042
1043 case MULT_HIGHPART_EXPR:
1044 res = wi::mul_high (arg1, arg2, sign);
1045 break;
1046
1047 case TRUNC_DIV_EXPR:
1048 case EXACT_DIV_EXPR:
1049 if (arg2 == 0)
1050 return NULL_TREE;
1051 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1052 break;
1053
1054 case FLOOR_DIV_EXPR:
1055 if (arg2 == 0)
1056 return NULL_TREE;
1057 res = wi::div_floor (arg1, arg2, sign, &overflow);
1058 break;
1059
1060 case CEIL_DIV_EXPR:
1061 if (arg2 == 0)
1062 return NULL_TREE;
1063 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1064 break;
1065
1066 case ROUND_DIV_EXPR:
1067 if (arg2 == 0)
1068 return NULL_TREE;
1069 res = wi::div_round (arg1, arg2, sign, &overflow);
1070 break;
1071
1072 case TRUNC_MOD_EXPR:
1073 if (arg2 == 0)
1074 return NULL_TREE;
1075 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1076 break;
1077
1078 case FLOOR_MOD_EXPR:
1079 if (arg2 == 0)
1080 return NULL_TREE;
1081 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1082 break;
1083
1084 case CEIL_MOD_EXPR:
1085 if (arg2 == 0)
1086 return NULL_TREE;
1087 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1088 break;
1089
1090 case ROUND_MOD_EXPR:
1091 if (arg2 == 0)
1092 return NULL_TREE;
1093 res = wi::mod_round (arg1, arg2, sign, &overflow);
1094 break;
1095
1096 case MIN_EXPR:
1097 res = wi::min (arg1, arg2, sign);
1098 break;
1099
1100 case MAX_EXPR:
1101 res = wi::max (arg1, arg2, sign);
1102 break;
1103
1104 default:
1105 return NULL_TREE;
1106 }
1107
1108 t = force_fit_type (type, res, overflowable,
1109 (((sign == SIGNED || overflowable == -1)
1110 && overflow)
1111 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1112
1113 return t;
1114 }
1115
1116 tree
1117 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1118 {
1119 return int_const_binop_1 (code, arg1, arg2, 1);
1120 }
1121
1122 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1123 constant. We assume ARG1 and ARG2 have the same data type, or at least
1124 are the same kind of constant and the same machine mode. Return zero if
1125 combining the constants is not allowed in the current operating mode. */
1126
1127 static tree
1128 const_binop (enum tree_code code, tree arg1, tree arg2)
1129 {
1130 /* Sanity check for the recursive cases. */
1131 if (!arg1 || !arg2)
1132 return NULL_TREE;
1133
1134 STRIP_NOPS (arg1);
1135 STRIP_NOPS (arg2);
1136
1137 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1138 {
1139 if (code == POINTER_PLUS_EXPR)
1140 return int_const_binop (PLUS_EXPR,
1141 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1142
1143 return int_const_binop (code, arg1, arg2);
1144 }
1145
1146 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1147 {
1148 machine_mode mode;
1149 REAL_VALUE_TYPE d1;
1150 REAL_VALUE_TYPE d2;
1151 REAL_VALUE_TYPE value;
1152 REAL_VALUE_TYPE result;
1153 bool inexact;
1154 tree t, type;
1155
1156 /* The following codes are handled by real_arithmetic. */
1157 switch (code)
1158 {
1159 case PLUS_EXPR:
1160 case MINUS_EXPR:
1161 case MULT_EXPR:
1162 case RDIV_EXPR:
1163 case MIN_EXPR:
1164 case MAX_EXPR:
1165 break;
1166
1167 default:
1168 return NULL_TREE;
1169 }
1170
1171 d1 = TREE_REAL_CST (arg1);
1172 d2 = TREE_REAL_CST (arg2);
1173
1174 type = TREE_TYPE (arg1);
1175 mode = TYPE_MODE (type);
1176
1177 /* Don't perform operation if we honor signaling NaNs and
1178 either operand is a NaN. */
1179 if (HONOR_SNANS (mode)
1180 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (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_VALUES_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 return arg1;
1194 else if (REAL_VALUE_ISNAN (d2))
1195 return arg2;
1196
1197 inexact = real_arithmetic (&value, code, &d1, &d2);
1198 real_convert (&result, mode, &value);
1199
1200 /* Don't constant fold this floating point operation if
1201 the result has overflowed and flag_trapping_math. */
1202 if (flag_trapping_math
1203 && MODE_HAS_INFINITIES (mode)
1204 && REAL_VALUE_ISINF (result)
1205 && !REAL_VALUE_ISINF (d1)
1206 && !REAL_VALUE_ISINF (d2))
1207 return NULL_TREE;
1208
1209 /* Don't constant fold this floating point operation if the
1210 result may dependent upon the run-time rounding mode and
1211 flag_rounding_math is set, or if GCC's software emulation
1212 is unable to accurately represent the result. */
1213 if ((flag_rounding_math
1214 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1215 && (inexact || !real_identical (&result, &value)))
1216 return NULL_TREE;
1217
1218 t = build_real (type, result);
1219
1220 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1221 return t;
1222 }
1223
1224 if (TREE_CODE (arg1) == FIXED_CST)
1225 {
1226 FIXED_VALUE_TYPE f1;
1227 FIXED_VALUE_TYPE f2;
1228 FIXED_VALUE_TYPE result;
1229 tree t, type;
1230 int sat_p;
1231 bool overflow_p;
1232
1233 /* The following codes are handled by fixed_arithmetic. */
1234 switch (code)
1235 {
1236 case PLUS_EXPR:
1237 case MINUS_EXPR:
1238 case MULT_EXPR:
1239 case TRUNC_DIV_EXPR:
1240 if (TREE_CODE (arg2) != FIXED_CST)
1241 return NULL_TREE;
1242 f2 = TREE_FIXED_CST (arg2);
1243 break;
1244
1245 case LSHIFT_EXPR:
1246 case RSHIFT_EXPR:
1247 {
1248 if (TREE_CODE (arg2) != INTEGER_CST)
1249 return NULL_TREE;
1250 wide_int w2 = arg2;
1251 f2.data.high = w2.elt (1);
1252 f2.data.low = w2.elt (0);
1253 f2.mode = SImode;
1254 }
1255 break;
1256
1257 default:
1258 return NULL_TREE;
1259 }
1260
1261 f1 = TREE_FIXED_CST (arg1);
1262 type = TREE_TYPE (arg1);
1263 sat_p = TYPE_SATURATING (type);
1264 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1265 t = build_fixed (type, result);
1266 /* Propagate overflow flags. */
1267 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1268 TREE_OVERFLOW (t) = 1;
1269 return t;
1270 }
1271
1272 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1273 {
1274 tree type = TREE_TYPE (arg1);
1275 tree r1 = TREE_REALPART (arg1);
1276 tree i1 = TREE_IMAGPART (arg1);
1277 tree r2 = TREE_REALPART (arg2);
1278 tree i2 = TREE_IMAGPART (arg2);
1279 tree real, imag;
1280
1281 switch (code)
1282 {
1283 case PLUS_EXPR:
1284 case MINUS_EXPR:
1285 real = const_binop (code, r1, r2);
1286 imag = const_binop (code, i1, i2);
1287 break;
1288
1289 case MULT_EXPR:
1290 if (COMPLEX_FLOAT_TYPE_P (type))
1291 return do_mpc_arg2 (arg1, arg2, type,
1292 /* do_nonfinite= */ folding_initializer,
1293 mpc_mul);
1294
1295 real = const_binop (MINUS_EXPR,
1296 const_binop (MULT_EXPR, r1, r2),
1297 const_binop (MULT_EXPR, i1, i2));
1298 imag = const_binop (PLUS_EXPR,
1299 const_binop (MULT_EXPR, r1, i2),
1300 const_binop (MULT_EXPR, i1, r2));
1301 break;
1302
1303 case RDIV_EXPR:
1304 if (COMPLEX_FLOAT_TYPE_P (type))
1305 return do_mpc_arg2 (arg1, arg2, type,
1306 /* do_nonfinite= */ folding_initializer,
1307 mpc_div);
1308 /* Fallthru ... */
1309 case TRUNC_DIV_EXPR:
1310 case CEIL_DIV_EXPR:
1311 case FLOOR_DIV_EXPR:
1312 case ROUND_DIV_EXPR:
1313 if (flag_complex_method == 0)
1314 {
1315 /* Keep this algorithm in sync with
1316 tree-complex.c:expand_complex_div_straight().
1317
1318 Expand complex division to scalars, straightforward algorithm.
1319 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1320 t = br*br + bi*bi
1321 */
1322 tree magsquared
1323 = const_binop (PLUS_EXPR,
1324 const_binop (MULT_EXPR, r2, r2),
1325 const_binop (MULT_EXPR, i2, i2));
1326 tree t1
1327 = const_binop (PLUS_EXPR,
1328 const_binop (MULT_EXPR, r1, r2),
1329 const_binop (MULT_EXPR, i1, i2));
1330 tree t2
1331 = const_binop (MINUS_EXPR,
1332 const_binop (MULT_EXPR, i1, r2),
1333 const_binop (MULT_EXPR, r1, i2));
1334
1335 real = const_binop (code, t1, magsquared);
1336 imag = const_binop (code, t2, magsquared);
1337 }
1338 else
1339 {
1340 /* Keep this algorithm in sync with
1341 tree-complex.c:expand_complex_div_wide().
1342
1343 Expand complex division to scalars, modified algorithm to minimize
1344 overflow with wide input ranges. */
1345 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1346 fold_abs_const (r2, TREE_TYPE (type)),
1347 fold_abs_const (i2, TREE_TYPE (type)));
1348
1349 if (integer_nonzerop (compare))
1350 {
1351 /* In the TRUE branch, we compute
1352 ratio = br/bi;
1353 div = (br * ratio) + bi;
1354 tr = (ar * ratio) + ai;
1355 ti = (ai * ratio) - ar;
1356 tr = tr / div;
1357 ti = ti / div; */
1358 tree ratio = const_binop (code, r2, i2);
1359 tree div = const_binop (PLUS_EXPR, i2,
1360 const_binop (MULT_EXPR, r2, ratio));
1361 real = const_binop (MULT_EXPR, r1, ratio);
1362 real = const_binop (PLUS_EXPR, real, i1);
1363 real = const_binop (code, real, div);
1364
1365 imag = const_binop (MULT_EXPR, i1, ratio);
1366 imag = const_binop (MINUS_EXPR, imag, r1);
1367 imag = const_binop (code, imag, div);
1368 }
1369 else
1370 {
1371 /* In the FALSE branch, we compute
1372 ratio = d/c;
1373 divisor = (d * ratio) + c;
1374 tr = (b * ratio) + a;
1375 ti = b - (a * ratio);
1376 tr = tr / div;
1377 ti = ti / div; */
1378 tree ratio = const_binop (code, i2, r2);
1379 tree div = const_binop (PLUS_EXPR, r2,
1380 const_binop (MULT_EXPR, i2, ratio));
1381
1382 real = const_binop (MULT_EXPR, i1, ratio);
1383 real = const_binop (PLUS_EXPR, real, r1);
1384 real = const_binop (code, real, div);
1385
1386 imag = const_binop (MULT_EXPR, r1, ratio);
1387 imag = const_binop (MINUS_EXPR, i1, imag);
1388 imag = const_binop (code, imag, div);
1389 }
1390 }
1391 break;
1392
1393 default:
1394 return NULL_TREE;
1395 }
1396
1397 if (real && imag)
1398 return build_complex (type, real, imag);
1399 }
1400
1401 if (TREE_CODE (arg1) == VECTOR_CST
1402 && TREE_CODE (arg2) == VECTOR_CST)
1403 {
1404 tree type = TREE_TYPE (arg1);
1405 int count = TYPE_VECTOR_SUBPARTS (type), i;
1406 tree *elts = XALLOCAVEC (tree, count);
1407
1408 for (i = 0; i < count; i++)
1409 {
1410 tree elem1 = VECTOR_CST_ELT (arg1, i);
1411 tree elem2 = VECTOR_CST_ELT (arg2, i);
1412
1413 elts[i] = const_binop (code, elem1, elem2);
1414
1415 /* It is possible that const_binop cannot handle the given
1416 code and return NULL_TREE */
1417 if (elts[i] == NULL_TREE)
1418 return NULL_TREE;
1419 }
1420
1421 return build_vector (type, elts);
1422 }
1423
1424 /* Shifts allow a scalar offset for a vector. */
1425 if (TREE_CODE (arg1) == VECTOR_CST
1426 && TREE_CODE (arg2) == INTEGER_CST)
1427 {
1428 tree type = TREE_TYPE (arg1);
1429 int count = TYPE_VECTOR_SUBPARTS (type), i;
1430 tree *elts = XALLOCAVEC (tree, count);
1431
1432 for (i = 0; i < count; i++)
1433 {
1434 tree elem1 = VECTOR_CST_ELT (arg1, i);
1435
1436 elts[i] = const_binop (code, elem1, arg2);
1437
1438 /* It is possible that const_binop cannot handle the given
1439 code and return NULL_TREE. */
1440 if (elts[i] == NULL_TREE)
1441 return NULL_TREE;
1442 }
1443
1444 return build_vector (type, elts);
1445 }
1446 return NULL_TREE;
1447 }
1448
1449 /* Overload that adds a TYPE parameter to be able to dispatch
1450 to fold_relational_const. */
1451
1452 tree
1453 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1454 {
1455 if (TREE_CODE_CLASS (code) == tcc_comparison)
1456 return fold_relational_const (code, type, arg1, arg2);
1457
1458 /* ??? Until we make the const_binop worker take the type of the
1459 result as argument put those cases that need it here. */
1460 switch (code)
1461 {
1462 case COMPLEX_EXPR:
1463 if ((TREE_CODE (arg1) == REAL_CST
1464 && TREE_CODE (arg2) == REAL_CST)
1465 || (TREE_CODE (arg1) == INTEGER_CST
1466 && TREE_CODE (arg2) == INTEGER_CST))
1467 return build_complex (type, arg1, arg2);
1468 return NULL_TREE;
1469
1470 case VEC_PACK_TRUNC_EXPR:
1471 case VEC_PACK_FIX_TRUNC_EXPR:
1472 {
1473 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1474 tree *elts;
1475
1476 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1477 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1478 if (TREE_CODE (arg1) != VECTOR_CST
1479 || TREE_CODE (arg2) != VECTOR_CST)
1480 return NULL_TREE;
1481
1482 elts = XALLOCAVEC (tree, nelts);
1483 if (!vec_cst_ctor_to_array (arg1, elts)
1484 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1485 return NULL_TREE;
1486
1487 for (i = 0; i < nelts; i++)
1488 {
1489 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1490 ? NOP_EXPR : FIX_TRUNC_EXPR,
1491 TREE_TYPE (type), elts[i]);
1492 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1493 return NULL_TREE;
1494 }
1495
1496 return build_vector (type, elts);
1497 }
1498
1499 case VEC_WIDEN_MULT_LO_EXPR:
1500 case VEC_WIDEN_MULT_HI_EXPR:
1501 case VEC_WIDEN_MULT_EVEN_EXPR:
1502 case VEC_WIDEN_MULT_ODD_EXPR:
1503 {
1504 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1505 unsigned int out, ofs, scale;
1506 tree *elts;
1507
1508 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1509 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1510 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1511 return NULL_TREE;
1512
1513 elts = XALLOCAVEC (tree, nelts * 4);
1514 if (!vec_cst_ctor_to_array (arg1, elts)
1515 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1516 return NULL_TREE;
1517
1518 if (code == VEC_WIDEN_MULT_LO_EXPR)
1519 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1520 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1521 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1522 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1523 scale = 1, ofs = 0;
1524 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1525 scale = 1, ofs = 1;
1526
1527 for (out = 0; out < nelts; out++)
1528 {
1529 unsigned int in1 = (out << scale) + ofs;
1530 unsigned int in2 = in1 + nelts * 2;
1531 tree t1, t2;
1532
1533 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1534 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1535
1536 if (t1 == NULL_TREE || t2 == NULL_TREE)
1537 return NULL_TREE;
1538 elts[out] = const_binop (MULT_EXPR, t1, t2);
1539 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1540 return NULL_TREE;
1541 }
1542
1543 return build_vector (type, elts);
1544 }
1545
1546 default:;
1547 }
1548
1549 if (TREE_CODE_CLASS (code) != tcc_binary)
1550 return NULL_TREE;
1551
1552 /* Make sure type and arg0 have the same saturating flag. */
1553 gcc_checking_assert (TYPE_SATURATING (type)
1554 == TYPE_SATURATING (TREE_TYPE (arg1)));
1555
1556 return const_binop (code, arg1, arg2);
1557 }
1558
1559 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1560 Return zero if computing the constants is not possible. */
1561
1562 tree
1563 const_unop (enum tree_code code, tree type, tree arg0)
1564 {
1565 switch (code)
1566 {
1567 CASE_CONVERT:
1568 case FLOAT_EXPR:
1569 case FIX_TRUNC_EXPR:
1570 case FIXED_CONVERT_EXPR:
1571 return fold_convert_const (code, type, arg0);
1572
1573 case ADDR_SPACE_CONVERT_EXPR:
1574 if (integer_zerop (arg0))
1575 return fold_convert_const (code, type, arg0);
1576 break;
1577
1578 case VIEW_CONVERT_EXPR:
1579 return fold_view_convert_expr (type, arg0);
1580
1581 case NEGATE_EXPR:
1582 {
1583 /* Can't call fold_negate_const directly here as that doesn't
1584 handle all cases and we might not be able to negate some
1585 constants. */
1586 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1587 if (tem && CONSTANT_CLASS_P (tem))
1588 return tem;
1589 break;
1590 }
1591
1592 case ABS_EXPR:
1593 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1594 return fold_abs_const (arg0, type);
1595 break;
1596
1597 case CONJ_EXPR:
1598 if (TREE_CODE (arg0) == COMPLEX_CST)
1599 {
1600 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1601 TREE_TYPE (type));
1602 return build_complex (type, TREE_REALPART (arg0), ipart);
1603 }
1604 break;
1605
1606 case BIT_NOT_EXPR:
1607 if (TREE_CODE (arg0) == INTEGER_CST)
1608 return fold_not_const (arg0, type);
1609 /* Perform BIT_NOT_EXPR on each element individually. */
1610 else if (TREE_CODE (arg0) == VECTOR_CST)
1611 {
1612 tree *elements;
1613 tree elem;
1614 unsigned count = VECTOR_CST_NELTS (arg0), i;
1615
1616 elements = XALLOCAVEC (tree, count);
1617 for (i = 0; i < count; i++)
1618 {
1619 elem = VECTOR_CST_ELT (arg0, i);
1620 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1621 if (elem == NULL_TREE)
1622 break;
1623 elements[i] = elem;
1624 }
1625 if (i == count)
1626 return build_vector (type, elements);
1627 }
1628 break;
1629
1630 case TRUTH_NOT_EXPR:
1631 if (TREE_CODE (arg0) == INTEGER_CST)
1632 return constant_boolean_node (integer_zerop (arg0), type);
1633 break;
1634
1635 case REALPART_EXPR:
1636 if (TREE_CODE (arg0) == COMPLEX_CST)
1637 return fold_convert (type, TREE_REALPART (arg0));
1638 break;
1639
1640 case IMAGPART_EXPR:
1641 if (TREE_CODE (arg0) == COMPLEX_CST)
1642 return fold_convert (type, TREE_IMAGPART (arg0));
1643 break;
1644
1645 case VEC_UNPACK_LO_EXPR:
1646 case VEC_UNPACK_HI_EXPR:
1647 case VEC_UNPACK_FLOAT_LO_EXPR:
1648 case VEC_UNPACK_FLOAT_HI_EXPR:
1649 {
1650 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1651 tree *elts;
1652 enum tree_code subcode;
1653
1654 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1655 if (TREE_CODE (arg0) != VECTOR_CST)
1656 return NULL_TREE;
1657
1658 elts = XALLOCAVEC (tree, nelts * 2);
1659 if (!vec_cst_ctor_to_array (arg0, elts))
1660 return NULL_TREE;
1661
1662 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1663 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1664 elts += nelts;
1665
1666 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1667 subcode = NOP_EXPR;
1668 else
1669 subcode = FLOAT_EXPR;
1670
1671 for (i = 0; i < nelts; i++)
1672 {
1673 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1674 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1675 return NULL_TREE;
1676 }
1677
1678 return build_vector (type, elts);
1679 }
1680
1681 case REDUC_MIN_EXPR:
1682 case REDUC_MAX_EXPR:
1683 case REDUC_PLUS_EXPR:
1684 {
1685 unsigned int nelts, i;
1686 tree *elts;
1687 enum tree_code subcode;
1688
1689 if (TREE_CODE (arg0) != VECTOR_CST)
1690 return NULL_TREE;
1691 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1692
1693 elts = XALLOCAVEC (tree, nelts);
1694 if (!vec_cst_ctor_to_array (arg0, elts))
1695 return NULL_TREE;
1696
1697 switch (code)
1698 {
1699 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1700 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1701 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1702 default: gcc_unreachable ();
1703 }
1704
1705 for (i = 1; i < nelts; i++)
1706 {
1707 elts[0] = const_binop (subcode, elts[0], elts[i]);
1708 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1709 return NULL_TREE;
1710 }
1711
1712 return elts[0];
1713 }
1714
1715 default:
1716 break;
1717 }
1718
1719 return NULL_TREE;
1720 }
1721
1722 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1723 indicates which particular sizetype to create. */
1724
1725 tree
1726 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1727 {
1728 return build_int_cst (sizetype_tab[(int) kind], number);
1729 }
1730 \f
1731 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1732 is a tree code. The type of the result is taken from the operands.
1733 Both must be equivalent integer types, ala int_binop_types_match_p.
1734 If the operands are constant, so is the result. */
1735
1736 tree
1737 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1738 {
1739 tree type = TREE_TYPE (arg0);
1740
1741 if (arg0 == error_mark_node || arg1 == error_mark_node)
1742 return error_mark_node;
1743
1744 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1745 TREE_TYPE (arg1)));
1746
1747 /* Handle the special case of two integer constants faster. */
1748 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1749 {
1750 /* And some specific cases even faster than that. */
1751 if (code == PLUS_EXPR)
1752 {
1753 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1754 return arg1;
1755 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1756 return arg0;
1757 }
1758 else if (code == MINUS_EXPR)
1759 {
1760 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1761 return arg0;
1762 }
1763 else if (code == MULT_EXPR)
1764 {
1765 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1766 return arg1;
1767 }
1768
1769 /* Handle general case of two integer constants. For sizetype
1770 constant calculations we always want to know about overflow,
1771 even in the unsigned case. */
1772 return int_const_binop_1 (code, arg0, arg1, -1);
1773 }
1774
1775 return fold_build2_loc (loc, code, type, arg0, arg1);
1776 }
1777
1778 /* Given two values, either both of sizetype or both of bitsizetype,
1779 compute the difference between the two values. Return the value
1780 in signed type corresponding to the type of the operands. */
1781
1782 tree
1783 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1784 {
1785 tree type = TREE_TYPE (arg0);
1786 tree ctype;
1787
1788 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1789 TREE_TYPE (arg1)));
1790
1791 /* If the type is already signed, just do the simple thing. */
1792 if (!TYPE_UNSIGNED (type))
1793 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1794
1795 if (type == sizetype)
1796 ctype = ssizetype;
1797 else if (type == bitsizetype)
1798 ctype = sbitsizetype;
1799 else
1800 ctype = signed_type_for (type);
1801
1802 /* If either operand is not a constant, do the conversions to the signed
1803 type and subtract. The hardware will do the right thing with any
1804 overflow in the subtraction. */
1805 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1806 return size_binop_loc (loc, MINUS_EXPR,
1807 fold_convert_loc (loc, ctype, arg0),
1808 fold_convert_loc (loc, ctype, arg1));
1809
1810 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1811 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1812 overflow) and negate (which can't either). Special-case a result
1813 of zero while we're here. */
1814 if (tree_int_cst_equal (arg0, arg1))
1815 return build_int_cst (ctype, 0);
1816 else if (tree_int_cst_lt (arg1, arg0))
1817 return fold_convert_loc (loc, ctype,
1818 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1819 else
1820 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1821 fold_convert_loc (loc, ctype,
1822 size_binop_loc (loc,
1823 MINUS_EXPR,
1824 arg1, arg0)));
1825 }
1826 \f
1827 /* A subroutine of fold_convert_const handling conversions of an
1828 INTEGER_CST to another integer type. */
1829
1830 static tree
1831 fold_convert_const_int_from_int (tree type, const_tree arg1)
1832 {
1833 /* Given an integer constant, make new constant with new type,
1834 appropriately sign-extended or truncated. Use widest_int
1835 so that any extension is done according ARG1's type. */
1836 return force_fit_type (type, wi::to_widest (arg1),
1837 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1838 TREE_OVERFLOW (arg1));
1839 }
1840
1841 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1842 to an integer type. */
1843
1844 static tree
1845 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1846 {
1847 bool overflow = false;
1848 tree t;
1849
1850 /* The following code implements the floating point to integer
1851 conversion rules required by the Java Language Specification,
1852 that IEEE NaNs are mapped to zero and values that overflow
1853 the target precision saturate, i.e. values greater than
1854 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1855 are mapped to INT_MIN. These semantics are allowed by the
1856 C and C++ standards that simply state that the behavior of
1857 FP-to-integer conversion is unspecified upon overflow. */
1858
1859 wide_int val;
1860 REAL_VALUE_TYPE r;
1861 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1862
1863 switch (code)
1864 {
1865 case FIX_TRUNC_EXPR:
1866 real_trunc (&r, VOIDmode, &x);
1867 break;
1868
1869 default:
1870 gcc_unreachable ();
1871 }
1872
1873 /* If R is NaN, return zero and show we have an overflow. */
1874 if (REAL_VALUE_ISNAN (r))
1875 {
1876 overflow = true;
1877 val = wi::zero (TYPE_PRECISION (type));
1878 }
1879
1880 /* See if R is less than the lower bound or greater than the
1881 upper bound. */
1882
1883 if (! overflow)
1884 {
1885 tree lt = TYPE_MIN_VALUE (type);
1886 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1887 if (REAL_VALUES_LESS (r, l))
1888 {
1889 overflow = true;
1890 val = lt;
1891 }
1892 }
1893
1894 if (! overflow)
1895 {
1896 tree ut = TYPE_MAX_VALUE (type);
1897 if (ut)
1898 {
1899 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1900 if (REAL_VALUES_LESS (u, r))
1901 {
1902 overflow = true;
1903 val = ut;
1904 }
1905 }
1906 }
1907
1908 if (! overflow)
1909 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1910
1911 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1912 return t;
1913 }
1914
1915 /* A subroutine of fold_convert_const handling conversions of a
1916 FIXED_CST to an integer type. */
1917
1918 static tree
1919 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1920 {
1921 tree t;
1922 double_int temp, temp_trunc;
1923 unsigned int mode;
1924
1925 /* Right shift FIXED_CST to temp by fbit. */
1926 temp = TREE_FIXED_CST (arg1).data;
1927 mode = TREE_FIXED_CST (arg1).mode;
1928 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1929 {
1930 temp = temp.rshift (GET_MODE_FBIT (mode),
1931 HOST_BITS_PER_DOUBLE_INT,
1932 SIGNED_FIXED_POINT_MODE_P (mode));
1933
1934 /* Left shift temp to temp_trunc by fbit. */
1935 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1936 HOST_BITS_PER_DOUBLE_INT,
1937 SIGNED_FIXED_POINT_MODE_P (mode));
1938 }
1939 else
1940 {
1941 temp = double_int_zero;
1942 temp_trunc = double_int_zero;
1943 }
1944
1945 /* If FIXED_CST is negative, we need to round the value toward 0.
1946 By checking if the fractional bits are not zero to add 1 to temp. */
1947 if (SIGNED_FIXED_POINT_MODE_P (mode)
1948 && temp_trunc.is_negative ()
1949 && TREE_FIXED_CST (arg1).data != temp_trunc)
1950 temp += double_int_one;
1951
1952 /* Given a fixed-point constant, make new constant with new type,
1953 appropriately sign-extended or truncated. */
1954 t = force_fit_type (type, temp, -1,
1955 (temp.is_negative ()
1956 && (TYPE_UNSIGNED (type)
1957 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1958 | TREE_OVERFLOW (arg1));
1959
1960 return t;
1961 }
1962
1963 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1964 to another floating point type. */
1965
1966 static tree
1967 fold_convert_const_real_from_real (tree type, const_tree arg1)
1968 {
1969 REAL_VALUE_TYPE value;
1970 tree t;
1971
1972 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1973 t = build_real (type, value);
1974
1975 /* If converting an infinity or NAN to a representation that doesn't
1976 have one, set the overflow bit so that we can produce some kind of
1977 error message at the appropriate point if necessary. It's not the
1978 most user-friendly message, but it's better than nothing. */
1979 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1980 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1981 TREE_OVERFLOW (t) = 1;
1982 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1983 && !MODE_HAS_NANS (TYPE_MODE (type)))
1984 TREE_OVERFLOW (t) = 1;
1985 /* Regular overflow, conversion produced an infinity in a mode that
1986 can't represent them. */
1987 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1988 && REAL_VALUE_ISINF (value)
1989 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1990 TREE_OVERFLOW (t) = 1;
1991 else
1992 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1993 return t;
1994 }
1995
1996 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1997 to a floating point type. */
1998
1999 static tree
2000 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2001 {
2002 REAL_VALUE_TYPE value;
2003 tree t;
2004
2005 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2006 t = build_real (type, value);
2007
2008 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2009 return t;
2010 }
2011
2012 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2013 to another fixed-point type. */
2014
2015 static tree
2016 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2017 {
2018 FIXED_VALUE_TYPE value;
2019 tree t;
2020 bool overflow_p;
2021
2022 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2023 TYPE_SATURATING (type));
2024 t = build_fixed (type, value);
2025
2026 /* Propagate overflow flags. */
2027 if (overflow_p | TREE_OVERFLOW (arg1))
2028 TREE_OVERFLOW (t) = 1;
2029 return t;
2030 }
2031
2032 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2033 to a fixed-point type. */
2034
2035 static tree
2036 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2037 {
2038 FIXED_VALUE_TYPE value;
2039 tree t;
2040 bool overflow_p;
2041 double_int di;
2042
2043 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2044
2045 di.low = TREE_INT_CST_ELT (arg1, 0);
2046 if (TREE_INT_CST_NUNITS (arg1) == 1)
2047 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2048 else
2049 di.high = TREE_INT_CST_ELT (arg1, 1);
2050
2051 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2052 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2053 TYPE_SATURATING (type));
2054 t = build_fixed (type, value);
2055
2056 /* Propagate overflow flags. */
2057 if (overflow_p | TREE_OVERFLOW (arg1))
2058 TREE_OVERFLOW (t) = 1;
2059 return t;
2060 }
2061
2062 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2063 to a fixed-point type. */
2064
2065 static tree
2066 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2067 {
2068 FIXED_VALUE_TYPE value;
2069 tree t;
2070 bool overflow_p;
2071
2072 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2073 &TREE_REAL_CST (arg1),
2074 TYPE_SATURATING (type));
2075 t = build_fixed (type, value);
2076
2077 /* Propagate overflow flags. */
2078 if (overflow_p | TREE_OVERFLOW (arg1))
2079 TREE_OVERFLOW (t) = 1;
2080 return t;
2081 }
2082
2083 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2084 type TYPE. If no simplification can be done return NULL_TREE. */
2085
2086 static tree
2087 fold_convert_const (enum tree_code code, tree type, tree arg1)
2088 {
2089 if (TREE_TYPE (arg1) == type)
2090 return arg1;
2091
2092 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2093 || TREE_CODE (type) == OFFSET_TYPE)
2094 {
2095 if (TREE_CODE (arg1) == INTEGER_CST)
2096 return fold_convert_const_int_from_int (type, arg1);
2097 else if (TREE_CODE (arg1) == REAL_CST)
2098 return fold_convert_const_int_from_real (code, type, arg1);
2099 else if (TREE_CODE (arg1) == FIXED_CST)
2100 return fold_convert_const_int_from_fixed (type, arg1);
2101 }
2102 else if (TREE_CODE (type) == REAL_TYPE)
2103 {
2104 if (TREE_CODE (arg1) == INTEGER_CST)
2105 return build_real_from_int_cst (type, arg1);
2106 else if (TREE_CODE (arg1) == REAL_CST)
2107 return fold_convert_const_real_from_real (type, arg1);
2108 else if (TREE_CODE (arg1) == FIXED_CST)
2109 return fold_convert_const_real_from_fixed (type, arg1);
2110 }
2111 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2112 {
2113 if (TREE_CODE (arg1) == FIXED_CST)
2114 return fold_convert_const_fixed_from_fixed (type, arg1);
2115 else if (TREE_CODE (arg1) == INTEGER_CST)
2116 return fold_convert_const_fixed_from_int (type, arg1);
2117 else if (TREE_CODE (arg1) == REAL_CST)
2118 return fold_convert_const_fixed_from_real (type, arg1);
2119 }
2120 return NULL_TREE;
2121 }
2122
2123 /* Construct a vector of zero elements of vector type TYPE. */
2124
2125 static tree
2126 build_zero_vector (tree type)
2127 {
2128 tree t;
2129
2130 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2131 return build_vector_from_val (type, t);
2132 }
2133
2134 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2135
2136 bool
2137 fold_convertible_p (const_tree type, const_tree arg)
2138 {
2139 tree orig = TREE_TYPE (arg);
2140
2141 if (type == orig)
2142 return true;
2143
2144 if (TREE_CODE (arg) == ERROR_MARK
2145 || TREE_CODE (type) == ERROR_MARK
2146 || TREE_CODE (orig) == ERROR_MARK)
2147 return false;
2148
2149 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2150 return true;
2151
2152 switch (TREE_CODE (type))
2153 {
2154 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2155 case POINTER_TYPE: case REFERENCE_TYPE:
2156 case OFFSET_TYPE:
2157 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2158 || TREE_CODE (orig) == OFFSET_TYPE)
2159 return true;
2160 return (TREE_CODE (orig) == VECTOR_TYPE
2161 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2162
2163 case REAL_TYPE:
2164 case FIXED_POINT_TYPE:
2165 case COMPLEX_TYPE:
2166 case VECTOR_TYPE:
2167 case VOID_TYPE:
2168 return TREE_CODE (type) == TREE_CODE (orig);
2169
2170 default:
2171 return false;
2172 }
2173 }
2174
2175 /* Convert expression ARG to type TYPE. Used by the middle-end for
2176 simple conversions in preference to calling the front-end's convert. */
2177
2178 tree
2179 fold_convert_loc (location_t loc, tree type, tree arg)
2180 {
2181 tree orig = TREE_TYPE (arg);
2182 tree tem;
2183
2184 if (type == orig)
2185 return arg;
2186
2187 if (TREE_CODE (arg) == ERROR_MARK
2188 || TREE_CODE (type) == ERROR_MARK
2189 || TREE_CODE (orig) == ERROR_MARK)
2190 return error_mark_node;
2191
2192 switch (TREE_CODE (type))
2193 {
2194 case POINTER_TYPE:
2195 case REFERENCE_TYPE:
2196 /* Handle conversions between pointers to different address spaces. */
2197 if (POINTER_TYPE_P (orig)
2198 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2199 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2200 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2201 /* fall through */
2202
2203 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2204 case OFFSET_TYPE:
2205 if (TREE_CODE (arg) == INTEGER_CST)
2206 {
2207 tem = fold_convert_const (NOP_EXPR, type, arg);
2208 if (tem != NULL_TREE)
2209 return tem;
2210 }
2211 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2212 || TREE_CODE (orig) == OFFSET_TYPE)
2213 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2214 if (TREE_CODE (orig) == COMPLEX_TYPE)
2215 return fold_convert_loc (loc, type,
2216 fold_build1_loc (loc, REALPART_EXPR,
2217 TREE_TYPE (orig), arg));
2218 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2219 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2220 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2221
2222 case REAL_TYPE:
2223 if (TREE_CODE (arg) == INTEGER_CST)
2224 {
2225 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2226 if (tem != NULL_TREE)
2227 return tem;
2228 }
2229 else if (TREE_CODE (arg) == REAL_CST)
2230 {
2231 tem = fold_convert_const (NOP_EXPR, type, arg);
2232 if (tem != NULL_TREE)
2233 return tem;
2234 }
2235 else if (TREE_CODE (arg) == FIXED_CST)
2236 {
2237 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2238 if (tem != NULL_TREE)
2239 return tem;
2240 }
2241
2242 switch (TREE_CODE (orig))
2243 {
2244 case INTEGER_TYPE:
2245 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2246 case POINTER_TYPE: case REFERENCE_TYPE:
2247 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2248
2249 case REAL_TYPE:
2250 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2251
2252 case FIXED_POINT_TYPE:
2253 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2254
2255 case COMPLEX_TYPE:
2256 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2257 return fold_convert_loc (loc, type, tem);
2258
2259 default:
2260 gcc_unreachable ();
2261 }
2262
2263 case FIXED_POINT_TYPE:
2264 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2265 || TREE_CODE (arg) == REAL_CST)
2266 {
2267 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2268 if (tem != NULL_TREE)
2269 goto fold_convert_exit;
2270 }
2271
2272 switch (TREE_CODE (orig))
2273 {
2274 case FIXED_POINT_TYPE:
2275 case INTEGER_TYPE:
2276 case ENUMERAL_TYPE:
2277 case BOOLEAN_TYPE:
2278 case REAL_TYPE:
2279 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2280
2281 case COMPLEX_TYPE:
2282 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2283 return fold_convert_loc (loc, type, tem);
2284
2285 default:
2286 gcc_unreachable ();
2287 }
2288
2289 case COMPLEX_TYPE:
2290 switch (TREE_CODE (orig))
2291 {
2292 case INTEGER_TYPE:
2293 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2294 case POINTER_TYPE: case REFERENCE_TYPE:
2295 case REAL_TYPE:
2296 case FIXED_POINT_TYPE:
2297 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2298 fold_convert_loc (loc, TREE_TYPE (type), arg),
2299 fold_convert_loc (loc, TREE_TYPE (type),
2300 integer_zero_node));
2301 case COMPLEX_TYPE:
2302 {
2303 tree rpart, ipart;
2304
2305 if (TREE_CODE (arg) == COMPLEX_EXPR)
2306 {
2307 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2308 TREE_OPERAND (arg, 0));
2309 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2310 TREE_OPERAND (arg, 1));
2311 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2312 }
2313
2314 arg = save_expr (arg);
2315 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2316 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2317 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2318 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2319 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2320 }
2321
2322 default:
2323 gcc_unreachable ();
2324 }
2325
2326 case VECTOR_TYPE:
2327 if (integer_zerop (arg))
2328 return build_zero_vector (type);
2329 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2330 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2331 || TREE_CODE (orig) == VECTOR_TYPE);
2332 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2333
2334 case VOID_TYPE:
2335 tem = fold_ignored_result (arg);
2336 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2337
2338 default:
2339 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2340 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2341 gcc_unreachable ();
2342 }
2343 fold_convert_exit:
2344 protected_set_expr_location_unshare (tem, loc);
2345 return tem;
2346 }
2347 \f
2348 /* Return false if expr can be assumed not to be an lvalue, true
2349 otherwise. */
2350
2351 static bool
2352 maybe_lvalue_p (const_tree x)
2353 {
2354 /* We only need to wrap lvalue tree codes. */
2355 switch (TREE_CODE (x))
2356 {
2357 case VAR_DECL:
2358 case PARM_DECL:
2359 case RESULT_DECL:
2360 case LABEL_DECL:
2361 case FUNCTION_DECL:
2362 case SSA_NAME:
2363
2364 case COMPONENT_REF:
2365 case MEM_REF:
2366 case INDIRECT_REF:
2367 case ARRAY_REF:
2368 case ARRAY_RANGE_REF:
2369 case BIT_FIELD_REF:
2370 case OBJ_TYPE_REF:
2371
2372 case REALPART_EXPR:
2373 case IMAGPART_EXPR:
2374 case PREINCREMENT_EXPR:
2375 case PREDECREMENT_EXPR:
2376 case SAVE_EXPR:
2377 case TRY_CATCH_EXPR:
2378 case WITH_CLEANUP_EXPR:
2379 case COMPOUND_EXPR:
2380 case MODIFY_EXPR:
2381 case TARGET_EXPR:
2382 case COND_EXPR:
2383 case BIND_EXPR:
2384 break;
2385
2386 default:
2387 /* Assume the worst for front-end tree codes. */
2388 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2389 break;
2390 return false;
2391 }
2392
2393 return true;
2394 }
2395
2396 /* Return an expr equal to X but certainly not valid as an lvalue. */
2397
2398 tree
2399 non_lvalue_loc (location_t loc, tree x)
2400 {
2401 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2402 us. */
2403 if (in_gimple_form)
2404 return x;
2405
2406 if (! maybe_lvalue_p (x))
2407 return x;
2408 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2409 }
2410
2411 /* When pedantic, return an expr equal to X but certainly not valid as a
2412 pedantic lvalue. Otherwise, return X. */
2413
2414 static tree
2415 pedantic_non_lvalue_loc (location_t loc, tree x)
2416 {
2417 return protected_set_expr_location_unshare (x, loc);
2418 }
2419 \f
2420 /* Given a tree comparison code, return the code that is the logical inverse.
2421 It is generally not safe to do this for floating-point comparisons, except
2422 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2423 ERROR_MARK in this case. */
2424
2425 enum tree_code
2426 invert_tree_comparison (enum tree_code code, bool honor_nans)
2427 {
2428 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2429 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2430 return ERROR_MARK;
2431
2432 switch (code)
2433 {
2434 case EQ_EXPR:
2435 return NE_EXPR;
2436 case NE_EXPR:
2437 return EQ_EXPR;
2438 case GT_EXPR:
2439 return honor_nans ? UNLE_EXPR : LE_EXPR;
2440 case GE_EXPR:
2441 return honor_nans ? UNLT_EXPR : LT_EXPR;
2442 case LT_EXPR:
2443 return honor_nans ? UNGE_EXPR : GE_EXPR;
2444 case LE_EXPR:
2445 return honor_nans ? UNGT_EXPR : GT_EXPR;
2446 case LTGT_EXPR:
2447 return UNEQ_EXPR;
2448 case UNEQ_EXPR:
2449 return LTGT_EXPR;
2450 case UNGT_EXPR:
2451 return LE_EXPR;
2452 case UNGE_EXPR:
2453 return LT_EXPR;
2454 case UNLT_EXPR:
2455 return GE_EXPR;
2456 case UNLE_EXPR:
2457 return GT_EXPR;
2458 case ORDERED_EXPR:
2459 return UNORDERED_EXPR;
2460 case UNORDERED_EXPR:
2461 return ORDERED_EXPR;
2462 default:
2463 gcc_unreachable ();
2464 }
2465 }
2466
2467 /* Similar, but return the comparison that results if the operands are
2468 swapped. This is safe for floating-point. */
2469
2470 enum tree_code
2471 swap_tree_comparison (enum tree_code code)
2472 {
2473 switch (code)
2474 {
2475 case EQ_EXPR:
2476 case NE_EXPR:
2477 case ORDERED_EXPR:
2478 case UNORDERED_EXPR:
2479 case LTGT_EXPR:
2480 case UNEQ_EXPR:
2481 return code;
2482 case GT_EXPR:
2483 return LT_EXPR;
2484 case GE_EXPR:
2485 return LE_EXPR;
2486 case LT_EXPR:
2487 return GT_EXPR;
2488 case LE_EXPR:
2489 return GE_EXPR;
2490 case UNGT_EXPR:
2491 return UNLT_EXPR;
2492 case UNGE_EXPR:
2493 return UNLE_EXPR;
2494 case UNLT_EXPR:
2495 return UNGT_EXPR;
2496 case UNLE_EXPR:
2497 return UNGE_EXPR;
2498 default:
2499 gcc_unreachable ();
2500 }
2501 }
2502
2503
2504 /* Convert a comparison tree code from an enum tree_code representation
2505 into a compcode bit-based encoding. This function is the inverse of
2506 compcode_to_comparison. */
2507
2508 static enum comparison_code
2509 comparison_to_compcode (enum tree_code code)
2510 {
2511 switch (code)
2512 {
2513 case LT_EXPR:
2514 return COMPCODE_LT;
2515 case EQ_EXPR:
2516 return COMPCODE_EQ;
2517 case LE_EXPR:
2518 return COMPCODE_LE;
2519 case GT_EXPR:
2520 return COMPCODE_GT;
2521 case NE_EXPR:
2522 return COMPCODE_NE;
2523 case GE_EXPR:
2524 return COMPCODE_GE;
2525 case ORDERED_EXPR:
2526 return COMPCODE_ORD;
2527 case UNORDERED_EXPR:
2528 return COMPCODE_UNORD;
2529 case UNLT_EXPR:
2530 return COMPCODE_UNLT;
2531 case UNEQ_EXPR:
2532 return COMPCODE_UNEQ;
2533 case UNLE_EXPR:
2534 return COMPCODE_UNLE;
2535 case UNGT_EXPR:
2536 return COMPCODE_UNGT;
2537 case LTGT_EXPR:
2538 return COMPCODE_LTGT;
2539 case UNGE_EXPR:
2540 return COMPCODE_UNGE;
2541 default:
2542 gcc_unreachable ();
2543 }
2544 }
2545
2546 /* Convert a compcode bit-based encoding of a comparison operator back
2547 to GCC's enum tree_code representation. This function is the
2548 inverse of comparison_to_compcode. */
2549
2550 static enum tree_code
2551 compcode_to_comparison (enum comparison_code code)
2552 {
2553 switch (code)
2554 {
2555 case COMPCODE_LT:
2556 return LT_EXPR;
2557 case COMPCODE_EQ:
2558 return EQ_EXPR;
2559 case COMPCODE_LE:
2560 return LE_EXPR;
2561 case COMPCODE_GT:
2562 return GT_EXPR;
2563 case COMPCODE_NE:
2564 return NE_EXPR;
2565 case COMPCODE_GE:
2566 return GE_EXPR;
2567 case COMPCODE_ORD:
2568 return ORDERED_EXPR;
2569 case COMPCODE_UNORD:
2570 return UNORDERED_EXPR;
2571 case COMPCODE_UNLT:
2572 return UNLT_EXPR;
2573 case COMPCODE_UNEQ:
2574 return UNEQ_EXPR;
2575 case COMPCODE_UNLE:
2576 return UNLE_EXPR;
2577 case COMPCODE_UNGT:
2578 return UNGT_EXPR;
2579 case COMPCODE_LTGT:
2580 return LTGT_EXPR;
2581 case COMPCODE_UNGE:
2582 return UNGE_EXPR;
2583 default:
2584 gcc_unreachable ();
2585 }
2586 }
2587
2588 /* Return a tree for the comparison which is the combination of
2589 doing the AND or OR (depending on CODE) of the two operations LCODE
2590 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2591 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2592 if this makes the transformation invalid. */
2593
2594 tree
2595 combine_comparisons (location_t loc,
2596 enum tree_code code, enum tree_code lcode,
2597 enum tree_code rcode, tree truth_type,
2598 tree ll_arg, tree lr_arg)
2599 {
2600 bool honor_nans = HONOR_NANS (ll_arg);
2601 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2602 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2603 int compcode;
2604
2605 switch (code)
2606 {
2607 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2608 compcode = lcompcode & rcompcode;
2609 break;
2610
2611 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2612 compcode = lcompcode | rcompcode;
2613 break;
2614
2615 default:
2616 return NULL_TREE;
2617 }
2618
2619 if (!honor_nans)
2620 {
2621 /* Eliminate unordered comparisons, as well as LTGT and ORD
2622 which are not used unless the mode has NaNs. */
2623 compcode &= ~COMPCODE_UNORD;
2624 if (compcode == COMPCODE_LTGT)
2625 compcode = COMPCODE_NE;
2626 else if (compcode == COMPCODE_ORD)
2627 compcode = COMPCODE_TRUE;
2628 }
2629 else if (flag_trapping_math)
2630 {
2631 /* Check that the original operation and the optimized ones will trap
2632 under the same condition. */
2633 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2634 && (lcompcode != COMPCODE_EQ)
2635 && (lcompcode != COMPCODE_ORD);
2636 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2637 && (rcompcode != COMPCODE_EQ)
2638 && (rcompcode != COMPCODE_ORD);
2639 bool trap = (compcode & COMPCODE_UNORD) == 0
2640 && (compcode != COMPCODE_EQ)
2641 && (compcode != COMPCODE_ORD);
2642
2643 /* In a short-circuited boolean expression the LHS might be
2644 such that the RHS, if evaluated, will never trap. For
2645 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2646 if neither x nor y is NaN. (This is a mixed blessing: for
2647 example, the expression above will never trap, hence
2648 optimizing it to x < y would be invalid). */
2649 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2650 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2651 rtrap = false;
2652
2653 /* If the comparison was short-circuited, and only the RHS
2654 trapped, we may now generate a spurious trap. */
2655 if (rtrap && !ltrap
2656 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2657 return NULL_TREE;
2658
2659 /* If we changed the conditions that cause a trap, we lose. */
2660 if ((ltrap || rtrap) != trap)
2661 return NULL_TREE;
2662 }
2663
2664 if (compcode == COMPCODE_TRUE)
2665 return constant_boolean_node (true, truth_type);
2666 else if (compcode == COMPCODE_FALSE)
2667 return constant_boolean_node (false, truth_type);
2668 else
2669 {
2670 enum tree_code tcode;
2671
2672 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2673 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2674 }
2675 }
2676 \f
2677 /* Return nonzero if two operands (typically of the same tree node)
2678 are necessarily equal. If either argument has side-effects this
2679 function returns zero. FLAGS modifies behavior as follows:
2680
2681 If OEP_ONLY_CONST is set, only return nonzero for constants.
2682 This function tests whether the operands are indistinguishable;
2683 it does not test whether they are equal using C's == operation.
2684 The distinction is important for IEEE floating point, because
2685 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2686 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2687
2688 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2689 even though it may hold multiple values during a function.
2690 This is because a GCC tree node guarantees that nothing else is
2691 executed between the evaluation of its "operands" (which may often
2692 be evaluated in arbitrary order). Hence if the operands themselves
2693 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2694 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2695 unset means assuming isochronic (or instantaneous) tree equivalence.
2696 Unless comparing arbitrary expression trees, such as from different
2697 statements, this flag can usually be left unset.
2698
2699 If OEP_PURE_SAME is set, then pure functions with identical arguments
2700 are considered the same. It is used when the caller has other ways
2701 to ensure that global memory is unchanged in between. */
2702
2703 int
2704 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2705 {
2706 /* If either is ERROR_MARK, they aren't equal. */
2707 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2708 || TREE_TYPE (arg0) == error_mark_node
2709 || TREE_TYPE (arg1) == error_mark_node)
2710 return 0;
2711
2712 /* Similar, if either does not have a type (like a released SSA name),
2713 they aren't equal. */
2714 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2715 return 0;
2716
2717 /* Check equality of integer constants before bailing out due to
2718 precision differences. */
2719 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2720 return tree_int_cst_equal (arg0, arg1);
2721
2722 /* If both types don't have the same signedness, then we can't consider
2723 them equal. We must check this before the STRIP_NOPS calls
2724 because they may change the signedness of the arguments. As pointers
2725 strictly don't have a signedness, require either two pointers or
2726 two non-pointers as well. */
2727 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2728 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2729 return 0;
2730
2731 /* We cannot consider pointers to different address space equal. */
2732 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2733 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2734 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2735 return 0;
2736
2737 /* If both types don't have the same precision, then it is not safe
2738 to strip NOPs. */
2739 if (element_precision (TREE_TYPE (arg0))
2740 != element_precision (TREE_TYPE (arg1)))
2741 return 0;
2742
2743 STRIP_NOPS (arg0);
2744 STRIP_NOPS (arg1);
2745
2746 /* In case both args are comparisons but with different comparison
2747 code, try to swap the comparison operands of one arg to produce
2748 a match and compare that variant. */
2749 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2750 && COMPARISON_CLASS_P (arg0)
2751 && COMPARISON_CLASS_P (arg1))
2752 {
2753 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2754
2755 if (TREE_CODE (arg0) == swap_code)
2756 return operand_equal_p (TREE_OPERAND (arg0, 0),
2757 TREE_OPERAND (arg1, 1), flags)
2758 && operand_equal_p (TREE_OPERAND (arg0, 1),
2759 TREE_OPERAND (arg1, 0), flags);
2760 }
2761
2762 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2763 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2764 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2765 return 0;
2766
2767 /* This is needed for conversions and for COMPONENT_REF.
2768 Might as well play it safe and always test this. */
2769 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2770 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2771 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2772 return 0;
2773
2774 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2775 We don't care about side effects in that case because the SAVE_EXPR
2776 takes care of that for us. In all other cases, two expressions are
2777 equal if they have no side effects. If we have two identical
2778 expressions with side effects that should be treated the same due
2779 to the only side effects being identical SAVE_EXPR's, that will
2780 be detected in the recursive calls below.
2781 If we are taking an invariant address of two identical objects
2782 they are necessarily equal as well. */
2783 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2784 && (TREE_CODE (arg0) == SAVE_EXPR
2785 || (flags & OEP_CONSTANT_ADDRESS_OF)
2786 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2787 return 1;
2788
2789 /* Next handle constant cases, those for which we can return 1 even
2790 if ONLY_CONST is set. */
2791 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2792 switch (TREE_CODE (arg0))
2793 {
2794 case INTEGER_CST:
2795 return tree_int_cst_equal (arg0, arg1);
2796
2797 case FIXED_CST:
2798 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2799 TREE_FIXED_CST (arg1));
2800
2801 case REAL_CST:
2802 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2803 TREE_REAL_CST (arg1)))
2804 return 1;
2805
2806
2807 if (!HONOR_SIGNED_ZEROS (arg0))
2808 {
2809 /* If we do not distinguish between signed and unsigned zero,
2810 consider them equal. */
2811 if (real_zerop (arg0) && real_zerop (arg1))
2812 return 1;
2813 }
2814 return 0;
2815
2816 case VECTOR_CST:
2817 {
2818 unsigned i;
2819
2820 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2821 return 0;
2822
2823 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2824 {
2825 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2826 VECTOR_CST_ELT (arg1, i), flags))
2827 return 0;
2828 }
2829 return 1;
2830 }
2831
2832 case COMPLEX_CST:
2833 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2834 flags)
2835 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2836 flags));
2837
2838 case STRING_CST:
2839 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2840 && ! memcmp (TREE_STRING_POINTER (arg0),
2841 TREE_STRING_POINTER (arg1),
2842 TREE_STRING_LENGTH (arg0)));
2843
2844 case ADDR_EXPR:
2845 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2846 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2847 ? OEP_CONSTANT_ADDRESS_OF | OEP_ADDRESS_OF : 0);
2848 default:
2849 break;
2850 }
2851
2852 if (flags & OEP_ONLY_CONST)
2853 return 0;
2854
2855 /* Define macros to test an operand from arg0 and arg1 for equality and a
2856 variant that allows null and views null as being different from any
2857 non-null value. In the latter case, if either is null, the both
2858 must be; otherwise, do the normal comparison. */
2859 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2860 TREE_OPERAND (arg1, N), flags)
2861
2862 #define OP_SAME_WITH_NULL(N) \
2863 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2864 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2865
2866 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2867 {
2868 case tcc_unary:
2869 /* Two conversions are equal only if signedness and modes match. */
2870 switch (TREE_CODE (arg0))
2871 {
2872 CASE_CONVERT:
2873 case FIX_TRUNC_EXPR:
2874 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2875 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2876 return 0;
2877 break;
2878 default:
2879 break;
2880 }
2881
2882 return OP_SAME (0);
2883
2884
2885 case tcc_comparison:
2886 case tcc_binary:
2887 if (OP_SAME (0) && OP_SAME (1))
2888 return 1;
2889
2890 /* For commutative ops, allow the other order. */
2891 return (commutative_tree_code (TREE_CODE (arg0))
2892 && operand_equal_p (TREE_OPERAND (arg0, 0),
2893 TREE_OPERAND (arg1, 1), flags)
2894 && operand_equal_p (TREE_OPERAND (arg0, 1),
2895 TREE_OPERAND (arg1, 0), flags));
2896
2897 case tcc_reference:
2898 /* If either of the pointer (or reference) expressions we are
2899 dereferencing contain a side effect, these cannot be equal,
2900 but their addresses can be. */
2901 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2902 && (TREE_SIDE_EFFECTS (arg0)
2903 || TREE_SIDE_EFFECTS (arg1)))
2904 return 0;
2905
2906 switch (TREE_CODE (arg0))
2907 {
2908 case INDIRECT_REF:
2909 if (!(flags & OEP_ADDRESS_OF)
2910 && (TYPE_ALIGN (TREE_TYPE (arg0))
2911 != TYPE_ALIGN (TREE_TYPE (arg1))))
2912 return 0;
2913 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2914 return OP_SAME (0);
2915
2916 case REALPART_EXPR:
2917 case IMAGPART_EXPR:
2918 return OP_SAME (0);
2919
2920 case TARGET_MEM_REF:
2921 case MEM_REF:
2922 /* Require equal access sizes, and similar pointer types.
2923 We can have incomplete types for array references of
2924 variable-sized arrays from the Fortran frontend
2925 though. Also verify the types are compatible. */
2926 if (!((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2927 || (TYPE_SIZE (TREE_TYPE (arg0))
2928 && TYPE_SIZE (TREE_TYPE (arg1))
2929 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2930 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2931 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2932 && ((flags & OEP_ADDRESS_OF)
2933 || (alias_ptr_types_compatible_p
2934 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2935 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2936 && (MR_DEPENDENCE_CLIQUE (arg0)
2937 == MR_DEPENDENCE_CLIQUE (arg1))
2938 && (MR_DEPENDENCE_BASE (arg0)
2939 == MR_DEPENDENCE_BASE (arg1))
2940 && (TYPE_ALIGN (TREE_TYPE (arg0))
2941 == TYPE_ALIGN (TREE_TYPE (arg1)))))))
2942 return 0;
2943 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2944 return (OP_SAME (0) && OP_SAME (1)
2945 /* TARGET_MEM_REF require equal extra operands. */
2946 && (TREE_CODE (arg0) != TARGET_MEM_REF
2947 || (OP_SAME_WITH_NULL (2)
2948 && OP_SAME_WITH_NULL (3)
2949 && OP_SAME_WITH_NULL (4))));
2950
2951 case ARRAY_REF:
2952 case ARRAY_RANGE_REF:
2953 /* Operands 2 and 3 may be null.
2954 Compare the array index by value if it is constant first as we
2955 may have different types but same value here. */
2956 if (!OP_SAME (0))
2957 return 0;
2958 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2959 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2960 TREE_OPERAND (arg1, 1))
2961 || OP_SAME (1))
2962 && OP_SAME_WITH_NULL (2)
2963 && OP_SAME_WITH_NULL (3));
2964
2965 case COMPONENT_REF:
2966 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2967 may be NULL when we're called to compare MEM_EXPRs. */
2968 if (!OP_SAME_WITH_NULL (0)
2969 || !OP_SAME (1))
2970 return 0;
2971 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2972 return OP_SAME_WITH_NULL (2);
2973
2974 case BIT_FIELD_REF:
2975 if (!OP_SAME (0))
2976 return 0;
2977 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2978 return OP_SAME (1) && OP_SAME (2);
2979
2980 default:
2981 return 0;
2982 }
2983
2984 case tcc_expression:
2985 switch (TREE_CODE (arg0))
2986 {
2987 case ADDR_EXPR:
2988 return operand_equal_p (TREE_OPERAND (arg0, 0),
2989 TREE_OPERAND (arg1, 0),
2990 flags | OEP_ADDRESS_OF);
2991
2992 case TRUTH_NOT_EXPR:
2993 return OP_SAME (0);
2994
2995 case TRUTH_ANDIF_EXPR:
2996 case TRUTH_ORIF_EXPR:
2997 return OP_SAME (0) && OP_SAME (1);
2998
2999 case FMA_EXPR:
3000 case WIDEN_MULT_PLUS_EXPR:
3001 case WIDEN_MULT_MINUS_EXPR:
3002 if (!OP_SAME (2))
3003 return 0;
3004 /* The multiplcation operands are commutative. */
3005 /* FALLTHRU */
3006
3007 case TRUTH_AND_EXPR:
3008 case TRUTH_OR_EXPR:
3009 case TRUTH_XOR_EXPR:
3010 if (OP_SAME (0) && OP_SAME (1))
3011 return 1;
3012
3013 /* Otherwise take into account this is a commutative operation. */
3014 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3015 TREE_OPERAND (arg1, 1), flags)
3016 && operand_equal_p (TREE_OPERAND (arg0, 1),
3017 TREE_OPERAND (arg1, 0), flags));
3018
3019 case COND_EXPR:
3020 case VEC_COND_EXPR:
3021 case DOT_PROD_EXPR:
3022 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3023
3024 default:
3025 return 0;
3026 }
3027
3028 case tcc_vl_exp:
3029 switch (TREE_CODE (arg0))
3030 {
3031 case CALL_EXPR:
3032 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3033 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3034 /* If not both CALL_EXPRs are either internal or normal function
3035 functions, then they are not equal. */
3036 return 0;
3037 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3038 {
3039 /* If the CALL_EXPRs call different internal functions, then they
3040 are not equal. */
3041 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3042 return 0;
3043 }
3044 else
3045 {
3046 /* If the CALL_EXPRs call different functions, then they are not
3047 equal. */
3048 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3049 flags))
3050 return 0;
3051 }
3052
3053 {
3054 unsigned int cef = call_expr_flags (arg0);
3055 if (flags & OEP_PURE_SAME)
3056 cef &= ECF_CONST | ECF_PURE;
3057 else
3058 cef &= ECF_CONST;
3059 if (!cef)
3060 return 0;
3061 }
3062
3063 /* Now see if all the arguments are the same. */
3064 {
3065 const_call_expr_arg_iterator iter0, iter1;
3066 const_tree a0, a1;
3067 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3068 a1 = first_const_call_expr_arg (arg1, &iter1);
3069 a0 && a1;
3070 a0 = next_const_call_expr_arg (&iter0),
3071 a1 = next_const_call_expr_arg (&iter1))
3072 if (! operand_equal_p (a0, a1, flags))
3073 return 0;
3074
3075 /* If we get here and both argument lists are exhausted
3076 then the CALL_EXPRs are equal. */
3077 return ! (a0 || a1);
3078 }
3079 default:
3080 return 0;
3081 }
3082
3083 case tcc_declaration:
3084 /* Consider __builtin_sqrt equal to sqrt. */
3085 return (TREE_CODE (arg0) == FUNCTION_DECL
3086 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3087 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3088 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3089
3090 default:
3091 return 0;
3092 }
3093
3094 #undef OP_SAME
3095 #undef OP_SAME_WITH_NULL
3096 }
3097 \f
3098 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3099 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3100
3101 When in doubt, return 0. */
3102
3103 static int
3104 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3105 {
3106 int unsignedp1, unsignedpo;
3107 tree primarg0, primarg1, primother;
3108 unsigned int correct_width;
3109
3110 if (operand_equal_p (arg0, arg1, 0))
3111 return 1;
3112
3113 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3114 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3115 return 0;
3116
3117 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3118 and see if the inner values are the same. This removes any
3119 signedness comparison, which doesn't matter here. */
3120 primarg0 = arg0, primarg1 = arg1;
3121 STRIP_NOPS (primarg0);
3122 STRIP_NOPS (primarg1);
3123 if (operand_equal_p (primarg0, primarg1, 0))
3124 return 1;
3125
3126 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3127 actual comparison operand, ARG0.
3128
3129 First throw away any conversions to wider types
3130 already present in the operands. */
3131
3132 primarg1 = get_narrower (arg1, &unsignedp1);
3133 primother = get_narrower (other, &unsignedpo);
3134
3135 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3136 if (unsignedp1 == unsignedpo
3137 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3138 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3139 {
3140 tree type = TREE_TYPE (arg0);
3141
3142 /* Make sure shorter operand is extended the right way
3143 to match the longer operand. */
3144 primarg1 = fold_convert (signed_or_unsigned_type_for
3145 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3146
3147 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3148 return 1;
3149 }
3150
3151 return 0;
3152 }
3153 \f
3154 /* See if ARG is an expression that is either a comparison or is performing
3155 arithmetic on comparisons. The comparisons must only be comparing
3156 two different values, which will be stored in *CVAL1 and *CVAL2; if
3157 they are nonzero it means that some operands have already been found.
3158 No variables may be used anywhere else in the expression except in the
3159 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3160 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3161
3162 If this is true, return 1. Otherwise, return zero. */
3163
3164 static int
3165 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3166 {
3167 enum tree_code code = TREE_CODE (arg);
3168 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3169
3170 /* We can handle some of the tcc_expression cases here. */
3171 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3172 tclass = tcc_unary;
3173 else if (tclass == tcc_expression
3174 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3175 || code == COMPOUND_EXPR))
3176 tclass = tcc_binary;
3177
3178 else if (tclass == tcc_expression && code == SAVE_EXPR
3179 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3180 {
3181 /* If we've already found a CVAL1 or CVAL2, this expression is
3182 two complex to handle. */
3183 if (*cval1 || *cval2)
3184 return 0;
3185
3186 tclass = tcc_unary;
3187 *save_p = 1;
3188 }
3189
3190 switch (tclass)
3191 {
3192 case tcc_unary:
3193 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3194
3195 case tcc_binary:
3196 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3197 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3198 cval1, cval2, save_p));
3199
3200 case tcc_constant:
3201 return 1;
3202
3203 case tcc_expression:
3204 if (code == COND_EXPR)
3205 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3206 cval1, cval2, save_p)
3207 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3208 cval1, cval2, save_p)
3209 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3210 cval1, cval2, save_p));
3211 return 0;
3212
3213 case tcc_comparison:
3214 /* First see if we can handle the first operand, then the second. For
3215 the second operand, we know *CVAL1 can't be zero. It must be that
3216 one side of the comparison is each of the values; test for the
3217 case where this isn't true by failing if the two operands
3218 are the same. */
3219
3220 if (operand_equal_p (TREE_OPERAND (arg, 0),
3221 TREE_OPERAND (arg, 1), 0))
3222 return 0;
3223
3224 if (*cval1 == 0)
3225 *cval1 = TREE_OPERAND (arg, 0);
3226 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3227 ;
3228 else if (*cval2 == 0)
3229 *cval2 = TREE_OPERAND (arg, 0);
3230 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3231 ;
3232 else
3233 return 0;
3234
3235 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3236 ;
3237 else if (*cval2 == 0)
3238 *cval2 = TREE_OPERAND (arg, 1);
3239 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3240 ;
3241 else
3242 return 0;
3243
3244 return 1;
3245
3246 default:
3247 return 0;
3248 }
3249 }
3250 \f
3251 /* ARG is a tree that is known to contain just arithmetic operations and
3252 comparisons. Evaluate the operations in the tree substituting NEW0 for
3253 any occurrence of OLD0 as an operand of a comparison and likewise for
3254 NEW1 and OLD1. */
3255
3256 static tree
3257 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3258 tree old1, tree new1)
3259 {
3260 tree type = TREE_TYPE (arg);
3261 enum tree_code code = TREE_CODE (arg);
3262 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3263
3264 /* We can handle some of the tcc_expression cases here. */
3265 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3266 tclass = tcc_unary;
3267 else if (tclass == tcc_expression
3268 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3269 tclass = tcc_binary;
3270
3271 switch (tclass)
3272 {
3273 case tcc_unary:
3274 return fold_build1_loc (loc, code, type,
3275 eval_subst (loc, TREE_OPERAND (arg, 0),
3276 old0, new0, old1, new1));
3277
3278 case tcc_binary:
3279 return fold_build2_loc (loc, code, type,
3280 eval_subst (loc, TREE_OPERAND (arg, 0),
3281 old0, new0, old1, new1),
3282 eval_subst (loc, TREE_OPERAND (arg, 1),
3283 old0, new0, old1, new1));
3284
3285 case tcc_expression:
3286 switch (code)
3287 {
3288 case SAVE_EXPR:
3289 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3290 old1, new1);
3291
3292 case COMPOUND_EXPR:
3293 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3294 old1, new1);
3295
3296 case COND_EXPR:
3297 return fold_build3_loc (loc, code, type,
3298 eval_subst (loc, TREE_OPERAND (arg, 0),
3299 old0, new0, old1, new1),
3300 eval_subst (loc, TREE_OPERAND (arg, 1),
3301 old0, new0, old1, new1),
3302 eval_subst (loc, TREE_OPERAND (arg, 2),
3303 old0, new0, old1, new1));
3304 default:
3305 break;
3306 }
3307 /* Fall through - ??? */
3308
3309 case tcc_comparison:
3310 {
3311 tree arg0 = TREE_OPERAND (arg, 0);
3312 tree arg1 = TREE_OPERAND (arg, 1);
3313
3314 /* We need to check both for exact equality and tree equality. The
3315 former will be true if the operand has a side-effect. In that
3316 case, we know the operand occurred exactly once. */
3317
3318 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3319 arg0 = new0;
3320 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3321 arg0 = new1;
3322
3323 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3324 arg1 = new0;
3325 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3326 arg1 = new1;
3327
3328 return fold_build2_loc (loc, code, type, arg0, arg1);
3329 }
3330
3331 default:
3332 return arg;
3333 }
3334 }
3335 \f
3336 /* Return a tree for the case when the result of an expression is RESULT
3337 converted to TYPE and OMITTED was previously an operand of the expression
3338 but is now not needed (e.g., we folded OMITTED * 0).
3339
3340 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3341 the conversion of RESULT to TYPE. */
3342
3343 tree
3344 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3345 {
3346 tree t = fold_convert_loc (loc, type, result);
3347
3348 /* If the resulting operand is an empty statement, just return the omitted
3349 statement casted to void. */
3350 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3351 return build1_loc (loc, NOP_EXPR, void_type_node,
3352 fold_ignored_result (omitted));
3353
3354 if (TREE_SIDE_EFFECTS (omitted))
3355 return build2_loc (loc, COMPOUND_EXPR, type,
3356 fold_ignored_result (omitted), t);
3357
3358 return non_lvalue_loc (loc, t);
3359 }
3360
3361 /* Return a tree for the case when the result of an expression is RESULT
3362 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3363 of the expression but are now not needed.
3364
3365 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3366 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3367 evaluated before OMITTED2. Otherwise, if neither has side effects,
3368 just do the conversion of RESULT to TYPE. */
3369
3370 tree
3371 omit_two_operands_loc (location_t loc, tree type, tree result,
3372 tree omitted1, tree omitted2)
3373 {
3374 tree t = fold_convert_loc (loc, type, result);
3375
3376 if (TREE_SIDE_EFFECTS (omitted2))
3377 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3378 if (TREE_SIDE_EFFECTS (omitted1))
3379 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3380
3381 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3382 }
3383
3384 \f
3385 /* Return a simplified tree node for the truth-negation of ARG. This
3386 never alters ARG itself. We assume that ARG is an operation that
3387 returns a truth value (0 or 1).
3388
3389 FIXME: one would think we would fold the result, but it causes
3390 problems with the dominator optimizer. */
3391
3392 static tree
3393 fold_truth_not_expr (location_t loc, tree arg)
3394 {
3395 tree type = TREE_TYPE (arg);
3396 enum tree_code code = TREE_CODE (arg);
3397 location_t loc1, loc2;
3398
3399 /* If this is a comparison, we can simply invert it, except for
3400 floating-point non-equality comparisons, in which case we just
3401 enclose a TRUTH_NOT_EXPR around what we have. */
3402
3403 if (TREE_CODE_CLASS (code) == tcc_comparison)
3404 {
3405 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3406 if (FLOAT_TYPE_P (op_type)
3407 && flag_trapping_math
3408 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3409 && code != NE_EXPR && code != EQ_EXPR)
3410 return NULL_TREE;
3411
3412 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3413 if (code == ERROR_MARK)
3414 return NULL_TREE;
3415
3416 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3417 TREE_OPERAND (arg, 1));
3418 }
3419
3420 switch (code)
3421 {
3422 case INTEGER_CST:
3423 return constant_boolean_node (integer_zerop (arg), type);
3424
3425 case TRUTH_AND_EXPR:
3426 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3427 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3428 return build2_loc (loc, TRUTH_OR_EXPR, type,
3429 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3430 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3431
3432 case TRUTH_OR_EXPR:
3433 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3434 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3435 return build2_loc (loc, TRUTH_AND_EXPR, type,
3436 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3437 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3438
3439 case TRUTH_XOR_EXPR:
3440 /* Here we can invert either operand. We invert the first operand
3441 unless the second operand is a TRUTH_NOT_EXPR in which case our
3442 result is the XOR of the first operand with the inside of the
3443 negation of the second operand. */
3444
3445 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3446 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3447 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3448 else
3449 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3450 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3451 TREE_OPERAND (arg, 1));
3452
3453 case TRUTH_ANDIF_EXPR:
3454 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3455 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3456 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3457 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3458 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3459
3460 case TRUTH_ORIF_EXPR:
3461 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3462 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3463 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3464 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3465 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3466
3467 case TRUTH_NOT_EXPR:
3468 return TREE_OPERAND (arg, 0);
3469
3470 case COND_EXPR:
3471 {
3472 tree arg1 = TREE_OPERAND (arg, 1);
3473 tree arg2 = TREE_OPERAND (arg, 2);
3474
3475 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3476 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3477
3478 /* A COND_EXPR may have a throw as one operand, which
3479 then has void type. Just leave void operands
3480 as they are. */
3481 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3482 VOID_TYPE_P (TREE_TYPE (arg1))
3483 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3484 VOID_TYPE_P (TREE_TYPE (arg2))
3485 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3486 }
3487
3488 case COMPOUND_EXPR:
3489 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3490 return build2_loc (loc, COMPOUND_EXPR, type,
3491 TREE_OPERAND (arg, 0),
3492 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3493
3494 case NON_LVALUE_EXPR:
3495 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3496 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3497
3498 CASE_CONVERT:
3499 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3500 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3501
3502 /* ... fall through ... */
3503
3504 case FLOAT_EXPR:
3505 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3506 return build1_loc (loc, TREE_CODE (arg), type,
3507 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3508
3509 case BIT_AND_EXPR:
3510 if (!integer_onep (TREE_OPERAND (arg, 1)))
3511 return NULL_TREE;
3512 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3513
3514 case SAVE_EXPR:
3515 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3516
3517 case CLEANUP_POINT_EXPR:
3518 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3519 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3520 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3521
3522 default:
3523 return NULL_TREE;
3524 }
3525 }
3526
3527 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3528 assume that ARG is an operation that returns a truth value (0 or 1
3529 for scalars, 0 or -1 for vectors). Return the folded expression if
3530 folding is successful. Otherwise, return NULL_TREE. */
3531
3532 static tree
3533 fold_invert_truthvalue (location_t loc, tree arg)
3534 {
3535 tree type = TREE_TYPE (arg);
3536 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3537 ? BIT_NOT_EXPR
3538 : TRUTH_NOT_EXPR,
3539 type, arg);
3540 }
3541
3542 /* Return a simplified tree node for the truth-negation of ARG. This
3543 never alters ARG itself. We assume that ARG is an operation that
3544 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3545
3546 tree
3547 invert_truthvalue_loc (location_t loc, tree arg)
3548 {
3549 if (TREE_CODE (arg) == ERROR_MARK)
3550 return arg;
3551
3552 tree type = TREE_TYPE (arg);
3553 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3554 ? BIT_NOT_EXPR
3555 : TRUTH_NOT_EXPR,
3556 type, arg);
3557 }
3558
3559 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3560 operands are another bit-wise operation with a common input. If so,
3561 distribute the bit operations to save an operation and possibly two if
3562 constants are involved. For example, convert
3563 (A | B) & (A | C) into A | (B & C)
3564 Further simplification will occur if B and C are constants.
3565
3566 If this optimization cannot be done, 0 will be returned. */
3567
3568 static tree
3569 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3570 tree arg0, tree arg1)
3571 {
3572 tree common;
3573 tree left, right;
3574
3575 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3576 || TREE_CODE (arg0) == code
3577 || (TREE_CODE (arg0) != BIT_AND_EXPR
3578 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3579 return 0;
3580
3581 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3582 {
3583 common = TREE_OPERAND (arg0, 0);
3584 left = TREE_OPERAND (arg0, 1);
3585 right = TREE_OPERAND (arg1, 1);
3586 }
3587 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3588 {
3589 common = TREE_OPERAND (arg0, 0);
3590 left = TREE_OPERAND (arg0, 1);
3591 right = TREE_OPERAND (arg1, 0);
3592 }
3593 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3594 {
3595 common = TREE_OPERAND (arg0, 1);
3596 left = TREE_OPERAND (arg0, 0);
3597 right = TREE_OPERAND (arg1, 1);
3598 }
3599 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3600 {
3601 common = TREE_OPERAND (arg0, 1);
3602 left = TREE_OPERAND (arg0, 0);
3603 right = TREE_OPERAND (arg1, 0);
3604 }
3605 else
3606 return 0;
3607
3608 common = fold_convert_loc (loc, type, common);
3609 left = fold_convert_loc (loc, type, left);
3610 right = fold_convert_loc (loc, type, right);
3611 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3612 fold_build2_loc (loc, code, type, left, right));
3613 }
3614
3615 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3616 with code CODE. This optimization is unsafe. */
3617 static tree
3618 distribute_real_division (location_t loc, enum tree_code code, tree type,
3619 tree arg0, tree arg1)
3620 {
3621 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3622 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3623
3624 /* (A / C) +- (B / C) -> (A +- B) / C. */
3625 if (mul0 == mul1
3626 && operand_equal_p (TREE_OPERAND (arg0, 1),
3627 TREE_OPERAND (arg1, 1), 0))
3628 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3629 fold_build2_loc (loc, code, type,
3630 TREE_OPERAND (arg0, 0),
3631 TREE_OPERAND (arg1, 0)),
3632 TREE_OPERAND (arg0, 1));
3633
3634 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3635 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3636 TREE_OPERAND (arg1, 0), 0)
3637 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3638 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3639 {
3640 REAL_VALUE_TYPE r0, r1;
3641 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3642 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3643 if (!mul0)
3644 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3645 if (!mul1)
3646 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3647 real_arithmetic (&r0, code, &r0, &r1);
3648 return fold_build2_loc (loc, MULT_EXPR, type,
3649 TREE_OPERAND (arg0, 0),
3650 build_real (type, r0));
3651 }
3652
3653 return NULL_TREE;
3654 }
3655 \f
3656 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3657 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3658
3659 static tree
3660 make_bit_field_ref (location_t loc, tree inner, tree type,
3661 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3662 {
3663 tree result, bftype;
3664
3665 if (bitpos == 0)
3666 {
3667 tree size = TYPE_SIZE (TREE_TYPE (inner));
3668 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3669 || POINTER_TYPE_P (TREE_TYPE (inner)))
3670 && tree_fits_shwi_p (size)
3671 && tree_to_shwi (size) == bitsize)
3672 return fold_convert_loc (loc, type, inner);
3673 }
3674
3675 bftype = type;
3676 if (TYPE_PRECISION (bftype) != bitsize
3677 || TYPE_UNSIGNED (bftype) == !unsignedp)
3678 bftype = build_nonstandard_integer_type (bitsize, 0);
3679
3680 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3681 size_int (bitsize), bitsize_int (bitpos));
3682
3683 if (bftype != type)
3684 result = fold_convert_loc (loc, type, result);
3685
3686 return result;
3687 }
3688
3689 /* Optimize a bit-field compare.
3690
3691 There are two cases: First is a compare against a constant and the
3692 second is a comparison of two items where the fields are at the same
3693 bit position relative to the start of a chunk (byte, halfword, word)
3694 large enough to contain it. In these cases we can avoid the shift
3695 implicit in bitfield extractions.
3696
3697 For constants, we emit a compare of the shifted constant with the
3698 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3699 compared. For two fields at the same position, we do the ANDs with the
3700 similar mask and compare the result of the ANDs.
3701
3702 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3703 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3704 are the left and right operands of the comparison, respectively.
3705
3706 If the optimization described above can be done, we return the resulting
3707 tree. Otherwise we return zero. */
3708
3709 static tree
3710 optimize_bit_field_compare (location_t loc, enum tree_code code,
3711 tree compare_type, tree lhs, tree rhs)
3712 {
3713 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3714 tree type = TREE_TYPE (lhs);
3715 tree unsigned_type;
3716 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3717 machine_mode lmode, rmode, nmode;
3718 int lunsignedp, runsignedp;
3719 int lvolatilep = 0, rvolatilep = 0;
3720 tree linner, rinner = NULL_TREE;
3721 tree mask;
3722 tree offset;
3723
3724 /* Get all the information about the extractions being done. If the bit size
3725 if the same as the size of the underlying object, we aren't doing an
3726 extraction at all and so can do nothing. We also don't want to
3727 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3728 then will no longer be able to replace it. */
3729 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3730 &lunsignedp, &lvolatilep, false);
3731 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3732 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3733 return 0;
3734
3735 if (!const_p)
3736 {
3737 /* If this is not a constant, we can only do something if bit positions,
3738 sizes, and signedness are the same. */
3739 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3740 &runsignedp, &rvolatilep, false);
3741
3742 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3743 || lunsignedp != runsignedp || offset != 0
3744 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3745 return 0;
3746 }
3747
3748 /* See if we can find a mode to refer to this field. We should be able to,
3749 but fail if we can't. */
3750 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3751 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3752 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3753 TYPE_ALIGN (TREE_TYPE (rinner))),
3754 word_mode, false);
3755 if (nmode == VOIDmode)
3756 return 0;
3757
3758 /* Set signed and unsigned types of the precision of this mode for the
3759 shifts below. */
3760 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3761
3762 /* Compute the bit position and size for the new reference and our offset
3763 within it. If the new reference is the same size as the original, we
3764 won't optimize anything, so return zero. */
3765 nbitsize = GET_MODE_BITSIZE (nmode);
3766 nbitpos = lbitpos & ~ (nbitsize - 1);
3767 lbitpos -= nbitpos;
3768 if (nbitsize == lbitsize)
3769 return 0;
3770
3771 if (BYTES_BIG_ENDIAN)
3772 lbitpos = nbitsize - lbitsize - lbitpos;
3773
3774 /* Make the mask to be used against the extracted field. */
3775 mask = build_int_cst_type (unsigned_type, -1);
3776 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3777 mask = const_binop (RSHIFT_EXPR, mask,
3778 size_int (nbitsize - lbitsize - lbitpos));
3779
3780 if (! const_p)
3781 /* If not comparing with constant, just rework the comparison
3782 and return. */
3783 return fold_build2_loc (loc, code, compare_type,
3784 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3785 make_bit_field_ref (loc, linner,
3786 unsigned_type,
3787 nbitsize, nbitpos,
3788 1),
3789 mask),
3790 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3791 make_bit_field_ref (loc, rinner,
3792 unsigned_type,
3793 nbitsize, nbitpos,
3794 1),
3795 mask));
3796
3797 /* Otherwise, we are handling the constant case. See if the constant is too
3798 big for the field. Warn and return a tree of for 0 (false) if so. We do
3799 this not only for its own sake, but to avoid having to test for this
3800 error case below. If we didn't, we might generate wrong code.
3801
3802 For unsigned fields, the constant shifted right by the field length should
3803 be all zero. For signed fields, the high-order bits should agree with
3804 the sign bit. */
3805
3806 if (lunsignedp)
3807 {
3808 if (wi::lrshift (rhs, lbitsize) != 0)
3809 {
3810 warning (0, "comparison is always %d due to width of bit-field",
3811 code == NE_EXPR);
3812 return constant_boolean_node (code == NE_EXPR, compare_type);
3813 }
3814 }
3815 else
3816 {
3817 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3818 if (tem != 0 && tem != -1)
3819 {
3820 warning (0, "comparison is always %d due to width of bit-field",
3821 code == NE_EXPR);
3822 return constant_boolean_node (code == NE_EXPR, compare_type);
3823 }
3824 }
3825
3826 /* Single-bit compares should always be against zero. */
3827 if (lbitsize == 1 && ! integer_zerop (rhs))
3828 {
3829 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3830 rhs = build_int_cst (type, 0);
3831 }
3832
3833 /* Make a new bitfield reference, shift the constant over the
3834 appropriate number of bits and mask it with the computed mask
3835 (in case this was a signed field). If we changed it, make a new one. */
3836 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3837
3838 rhs = const_binop (BIT_AND_EXPR,
3839 const_binop (LSHIFT_EXPR,
3840 fold_convert_loc (loc, unsigned_type, rhs),
3841 size_int (lbitpos)),
3842 mask);
3843
3844 lhs = build2_loc (loc, code, compare_type,
3845 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3846 return lhs;
3847 }
3848 \f
3849 /* Subroutine for fold_truth_andor_1: decode a field reference.
3850
3851 If EXP is a comparison reference, we return the innermost reference.
3852
3853 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3854 set to the starting bit number.
3855
3856 If the innermost field can be completely contained in a mode-sized
3857 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3858
3859 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3860 otherwise it is not changed.
3861
3862 *PUNSIGNEDP is set to the signedness of the field.
3863
3864 *PMASK is set to the mask used. This is either contained in a
3865 BIT_AND_EXPR or derived from the width of the field.
3866
3867 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3868
3869 Return 0 if this is not a component reference or is one that we can't
3870 do anything with. */
3871
3872 static tree
3873 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3874 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3875 int *punsignedp, int *pvolatilep,
3876 tree *pmask, tree *pand_mask)
3877 {
3878 tree outer_type = 0;
3879 tree and_mask = 0;
3880 tree mask, inner, offset;
3881 tree unsigned_type;
3882 unsigned int precision;
3883
3884 /* All the optimizations using this function assume integer fields.
3885 There are problems with FP fields since the type_for_size call
3886 below can fail for, e.g., XFmode. */
3887 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3888 return 0;
3889
3890 /* We are interested in the bare arrangement of bits, so strip everything
3891 that doesn't affect the machine mode. However, record the type of the
3892 outermost expression if it may matter below. */
3893 if (CONVERT_EXPR_P (exp)
3894 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3895 outer_type = TREE_TYPE (exp);
3896 STRIP_NOPS (exp);
3897
3898 if (TREE_CODE (exp) == BIT_AND_EXPR)
3899 {
3900 and_mask = TREE_OPERAND (exp, 1);
3901 exp = TREE_OPERAND (exp, 0);
3902 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3903 if (TREE_CODE (and_mask) != INTEGER_CST)
3904 return 0;
3905 }
3906
3907 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3908 punsignedp, pvolatilep, false);
3909 if ((inner == exp && and_mask == 0)
3910 || *pbitsize < 0 || offset != 0
3911 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3912 return 0;
3913
3914 /* If the number of bits in the reference is the same as the bitsize of
3915 the outer type, then the outer type gives the signedness. Otherwise
3916 (in case of a small bitfield) the signedness is unchanged. */
3917 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3918 *punsignedp = TYPE_UNSIGNED (outer_type);
3919
3920 /* Compute the mask to access the bitfield. */
3921 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3922 precision = TYPE_PRECISION (unsigned_type);
3923
3924 mask = build_int_cst_type (unsigned_type, -1);
3925
3926 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3927 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3928
3929 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3930 if (and_mask != 0)
3931 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3932 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3933
3934 *pmask = mask;
3935 *pand_mask = and_mask;
3936 return inner;
3937 }
3938
3939 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3940 bit positions and MASK is SIGNED. */
3941
3942 static int
3943 all_ones_mask_p (const_tree mask, unsigned int size)
3944 {
3945 tree type = TREE_TYPE (mask);
3946 unsigned int precision = TYPE_PRECISION (type);
3947
3948 /* If this function returns true when the type of the mask is
3949 UNSIGNED, then there will be errors. In particular see
3950 gcc.c-torture/execute/990326-1.c. There does not appear to be
3951 any documentation paper trail as to why this is so. But the pre
3952 wide-int worked with that restriction and it has been preserved
3953 here. */
3954 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3955 return false;
3956
3957 return wi::mask (size, false, precision) == mask;
3958 }
3959
3960 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3961 represents the sign bit of EXP's type. If EXP represents a sign
3962 or zero extension, also test VAL against the unextended type.
3963 The return value is the (sub)expression whose sign bit is VAL,
3964 or NULL_TREE otherwise. */
3965
3966 tree
3967 sign_bit_p (tree exp, const_tree val)
3968 {
3969 int width;
3970 tree t;
3971
3972 /* Tree EXP must have an integral type. */
3973 t = TREE_TYPE (exp);
3974 if (! INTEGRAL_TYPE_P (t))
3975 return NULL_TREE;
3976
3977 /* Tree VAL must be an integer constant. */
3978 if (TREE_CODE (val) != INTEGER_CST
3979 || TREE_OVERFLOW (val))
3980 return NULL_TREE;
3981
3982 width = TYPE_PRECISION (t);
3983 if (wi::only_sign_bit_p (val, width))
3984 return exp;
3985
3986 /* Handle extension from a narrower type. */
3987 if (TREE_CODE (exp) == NOP_EXPR
3988 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3989 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3990
3991 return NULL_TREE;
3992 }
3993
3994 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3995 to be evaluated unconditionally. */
3996
3997 static int
3998 simple_operand_p (const_tree exp)
3999 {
4000 /* Strip any conversions that don't change the machine mode. */
4001 STRIP_NOPS (exp);
4002
4003 return (CONSTANT_CLASS_P (exp)
4004 || TREE_CODE (exp) == SSA_NAME
4005 || (DECL_P (exp)
4006 && ! TREE_ADDRESSABLE (exp)
4007 && ! TREE_THIS_VOLATILE (exp)
4008 && ! DECL_NONLOCAL (exp)
4009 /* Don't regard global variables as simple. They may be
4010 allocated in ways unknown to the compiler (shared memory,
4011 #pragma weak, etc). */
4012 && ! TREE_PUBLIC (exp)
4013 && ! DECL_EXTERNAL (exp)
4014 /* Weakrefs are not safe to be read, since they can be NULL.
4015 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4016 have DECL_WEAK flag set. */
4017 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4018 /* Loading a static variable is unduly expensive, but global
4019 registers aren't expensive. */
4020 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4021 }
4022
4023 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4024 to be evaluated unconditionally.
4025 I addition to simple_operand_p, we assume that comparisons, conversions,
4026 and logic-not operations are simple, if their operands are simple, too. */
4027
4028 static bool
4029 simple_operand_p_2 (tree exp)
4030 {
4031 enum tree_code code;
4032
4033 if (TREE_SIDE_EFFECTS (exp)
4034 || tree_could_trap_p (exp))
4035 return false;
4036
4037 while (CONVERT_EXPR_P (exp))
4038 exp = TREE_OPERAND (exp, 0);
4039
4040 code = TREE_CODE (exp);
4041
4042 if (TREE_CODE_CLASS (code) == tcc_comparison)
4043 return (simple_operand_p (TREE_OPERAND (exp, 0))
4044 && simple_operand_p (TREE_OPERAND (exp, 1)));
4045
4046 if (code == TRUTH_NOT_EXPR)
4047 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4048
4049 return simple_operand_p (exp);
4050 }
4051
4052 \f
4053 /* The following functions are subroutines to fold_range_test and allow it to
4054 try to change a logical combination of comparisons into a range test.
4055
4056 For example, both
4057 X == 2 || X == 3 || X == 4 || X == 5
4058 and
4059 X >= 2 && X <= 5
4060 are converted to
4061 (unsigned) (X - 2) <= 3
4062
4063 We describe each set of comparisons as being either inside or outside
4064 a range, using a variable named like IN_P, and then describe the
4065 range with a lower and upper bound. If one of the bounds is omitted,
4066 it represents either the highest or lowest value of the type.
4067
4068 In the comments below, we represent a range by two numbers in brackets
4069 preceded by a "+" to designate being inside that range, or a "-" to
4070 designate being outside that range, so the condition can be inverted by
4071 flipping the prefix. An omitted bound is represented by a "-". For
4072 example, "- [-, 10]" means being outside the range starting at the lowest
4073 possible value and ending at 10, in other words, being greater than 10.
4074 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4075 always false.
4076
4077 We set up things so that the missing bounds are handled in a consistent
4078 manner so neither a missing bound nor "true" and "false" need to be
4079 handled using a special case. */
4080
4081 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4082 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4083 and UPPER1_P are nonzero if the respective argument is an upper bound
4084 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4085 must be specified for a comparison. ARG1 will be converted to ARG0's
4086 type if both are specified. */
4087
4088 static tree
4089 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4090 tree arg1, int upper1_p)
4091 {
4092 tree tem;
4093 int result;
4094 int sgn0, sgn1;
4095
4096 /* If neither arg represents infinity, do the normal operation.
4097 Else, if not a comparison, return infinity. Else handle the special
4098 comparison rules. Note that most of the cases below won't occur, but
4099 are handled for consistency. */
4100
4101 if (arg0 != 0 && arg1 != 0)
4102 {
4103 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4104 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4105 STRIP_NOPS (tem);
4106 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4107 }
4108
4109 if (TREE_CODE_CLASS (code) != tcc_comparison)
4110 return 0;
4111
4112 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4113 for neither. In real maths, we cannot assume open ended ranges are
4114 the same. But, this is computer arithmetic, where numbers are finite.
4115 We can therefore make the transformation of any unbounded range with
4116 the value Z, Z being greater than any representable number. This permits
4117 us to treat unbounded ranges as equal. */
4118 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4119 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4120 switch (code)
4121 {
4122 case EQ_EXPR:
4123 result = sgn0 == sgn1;
4124 break;
4125 case NE_EXPR:
4126 result = sgn0 != sgn1;
4127 break;
4128 case LT_EXPR:
4129 result = sgn0 < sgn1;
4130 break;
4131 case LE_EXPR:
4132 result = sgn0 <= sgn1;
4133 break;
4134 case GT_EXPR:
4135 result = sgn0 > sgn1;
4136 break;
4137 case GE_EXPR:
4138 result = sgn0 >= sgn1;
4139 break;
4140 default:
4141 gcc_unreachable ();
4142 }
4143
4144 return constant_boolean_node (result, type);
4145 }
4146 \f
4147 /* Helper routine for make_range. Perform one step for it, return
4148 new expression if the loop should continue or NULL_TREE if it should
4149 stop. */
4150
4151 tree
4152 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4153 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4154 bool *strict_overflow_p)
4155 {
4156 tree arg0_type = TREE_TYPE (arg0);
4157 tree n_low, n_high, low = *p_low, high = *p_high;
4158 int in_p = *p_in_p, n_in_p;
4159
4160 switch (code)
4161 {
4162 case TRUTH_NOT_EXPR:
4163 /* We can only do something if the range is testing for zero. */
4164 if (low == NULL_TREE || high == NULL_TREE
4165 || ! integer_zerop (low) || ! integer_zerop (high))
4166 return NULL_TREE;
4167 *p_in_p = ! in_p;
4168 return arg0;
4169
4170 case EQ_EXPR: case NE_EXPR:
4171 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4172 /* We can only do something if the range is testing for zero
4173 and if the second operand is an integer constant. Note that
4174 saying something is "in" the range we make is done by
4175 complementing IN_P since it will set in the initial case of
4176 being not equal to zero; "out" is leaving it alone. */
4177 if (low == NULL_TREE || high == NULL_TREE
4178 || ! integer_zerop (low) || ! integer_zerop (high)
4179 || TREE_CODE (arg1) != INTEGER_CST)
4180 return NULL_TREE;
4181
4182 switch (code)
4183 {
4184 case NE_EXPR: /* - [c, c] */
4185 low = high = arg1;
4186 break;
4187 case EQ_EXPR: /* + [c, c] */
4188 in_p = ! in_p, low = high = arg1;
4189 break;
4190 case GT_EXPR: /* - [-, c] */
4191 low = 0, high = arg1;
4192 break;
4193 case GE_EXPR: /* + [c, -] */
4194 in_p = ! in_p, low = arg1, high = 0;
4195 break;
4196 case LT_EXPR: /* - [c, -] */
4197 low = arg1, high = 0;
4198 break;
4199 case LE_EXPR: /* + [-, c] */
4200 in_p = ! in_p, low = 0, high = arg1;
4201 break;
4202 default:
4203 gcc_unreachable ();
4204 }
4205
4206 /* If this is an unsigned comparison, we also know that EXP is
4207 greater than or equal to zero. We base the range tests we make
4208 on that fact, so we record it here so we can parse existing
4209 range tests. We test arg0_type since often the return type
4210 of, e.g. EQ_EXPR, is boolean. */
4211 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4212 {
4213 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4214 in_p, low, high, 1,
4215 build_int_cst (arg0_type, 0),
4216 NULL_TREE))
4217 return NULL_TREE;
4218
4219 in_p = n_in_p, low = n_low, high = n_high;
4220
4221 /* If the high bound is missing, but we have a nonzero low
4222 bound, reverse the range so it goes from zero to the low bound
4223 minus 1. */
4224 if (high == 0 && low && ! integer_zerop (low))
4225 {
4226 in_p = ! in_p;
4227 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4228 build_int_cst (TREE_TYPE (low), 1), 0);
4229 low = build_int_cst (arg0_type, 0);
4230 }
4231 }
4232
4233 *p_low = low;
4234 *p_high = high;
4235 *p_in_p = in_p;
4236 return arg0;
4237
4238 case NEGATE_EXPR:
4239 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4240 low and high are non-NULL, then normalize will DTRT. */
4241 if (!TYPE_UNSIGNED (arg0_type)
4242 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4243 {
4244 if (low == NULL_TREE)
4245 low = TYPE_MIN_VALUE (arg0_type);
4246 if (high == NULL_TREE)
4247 high = TYPE_MAX_VALUE (arg0_type);
4248 }
4249
4250 /* (-x) IN [a,b] -> x in [-b, -a] */
4251 n_low = range_binop (MINUS_EXPR, exp_type,
4252 build_int_cst (exp_type, 0),
4253 0, high, 1);
4254 n_high = range_binop (MINUS_EXPR, exp_type,
4255 build_int_cst (exp_type, 0),
4256 0, low, 0);
4257 if (n_high != 0 && TREE_OVERFLOW (n_high))
4258 return NULL_TREE;
4259 goto normalize;
4260
4261 case BIT_NOT_EXPR:
4262 /* ~ X -> -X - 1 */
4263 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4264 build_int_cst (exp_type, 1));
4265
4266 case PLUS_EXPR:
4267 case MINUS_EXPR:
4268 if (TREE_CODE (arg1) != INTEGER_CST)
4269 return NULL_TREE;
4270
4271 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4272 move a constant to the other side. */
4273 if (!TYPE_UNSIGNED (arg0_type)
4274 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4275 return NULL_TREE;
4276
4277 /* If EXP is signed, any overflow in the computation is undefined,
4278 so we don't worry about it so long as our computations on
4279 the bounds don't overflow. For unsigned, overflow is defined
4280 and this is exactly the right thing. */
4281 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4282 arg0_type, low, 0, arg1, 0);
4283 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4284 arg0_type, high, 1, arg1, 0);
4285 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4286 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4287 return NULL_TREE;
4288
4289 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4290 *strict_overflow_p = true;
4291
4292 normalize:
4293 /* Check for an unsigned range which has wrapped around the maximum
4294 value thus making n_high < n_low, and normalize it. */
4295 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4296 {
4297 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4298 build_int_cst (TREE_TYPE (n_high), 1), 0);
4299 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4300 build_int_cst (TREE_TYPE (n_low), 1), 0);
4301
4302 /* If the range is of the form +/- [ x+1, x ], we won't
4303 be able to normalize it. But then, it represents the
4304 whole range or the empty set, so make it
4305 +/- [ -, - ]. */
4306 if (tree_int_cst_equal (n_low, low)
4307 && tree_int_cst_equal (n_high, high))
4308 low = high = 0;
4309 else
4310 in_p = ! in_p;
4311 }
4312 else
4313 low = n_low, high = n_high;
4314
4315 *p_low = low;
4316 *p_high = high;
4317 *p_in_p = in_p;
4318 return arg0;
4319
4320 CASE_CONVERT:
4321 case NON_LVALUE_EXPR:
4322 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4323 return NULL_TREE;
4324
4325 if (! INTEGRAL_TYPE_P (arg0_type)
4326 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4327 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4328 return NULL_TREE;
4329
4330 n_low = low, n_high = high;
4331
4332 if (n_low != 0)
4333 n_low = fold_convert_loc (loc, arg0_type, n_low);
4334
4335 if (n_high != 0)
4336 n_high = fold_convert_loc (loc, arg0_type, n_high);
4337
4338 /* If we're converting arg0 from an unsigned type, to exp,
4339 a signed type, we will be doing the comparison as unsigned.
4340 The tests above have already verified that LOW and HIGH
4341 are both positive.
4342
4343 So we have to ensure that we will handle large unsigned
4344 values the same way that the current signed bounds treat
4345 negative values. */
4346
4347 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4348 {
4349 tree high_positive;
4350 tree equiv_type;
4351 /* For fixed-point modes, we need to pass the saturating flag
4352 as the 2nd parameter. */
4353 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4354 equiv_type
4355 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4356 TYPE_SATURATING (arg0_type));
4357 else
4358 equiv_type
4359 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4360
4361 /* A range without an upper bound is, naturally, unbounded.
4362 Since convert would have cropped a very large value, use
4363 the max value for the destination type. */
4364 high_positive
4365 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4366 : TYPE_MAX_VALUE (arg0_type);
4367
4368 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4369 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4370 fold_convert_loc (loc, arg0_type,
4371 high_positive),
4372 build_int_cst (arg0_type, 1));
4373
4374 /* If the low bound is specified, "and" the range with the
4375 range for which the original unsigned value will be
4376 positive. */
4377 if (low != 0)
4378 {
4379 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4380 1, fold_convert_loc (loc, arg0_type,
4381 integer_zero_node),
4382 high_positive))
4383 return NULL_TREE;
4384
4385 in_p = (n_in_p == in_p);
4386 }
4387 else
4388 {
4389 /* Otherwise, "or" the range with the range of the input
4390 that will be interpreted as negative. */
4391 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4392 1, fold_convert_loc (loc, arg0_type,
4393 integer_zero_node),
4394 high_positive))
4395 return NULL_TREE;
4396
4397 in_p = (in_p != n_in_p);
4398 }
4399 }
4400
4401 *p_low = n_low;
4402 *p_high = n_high;
4403 *p_in_p = in_p;
4404 return arg0;
4405
4406 default:
4407 return NULL_TREE;
4408 }
4409 }
4410
4411 /* Given EXP, a logical expression, set the range it is testing into
4412 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4413 actually being tested. *PLOW and *PHIGH will be made of the same
4414 type as the returned expression. If EXP is not a comparison, we
4415 will most likely not be returning a useful value and range. Set
4416 *STRICT_OVERFLOW_P to true if the return value is only valid
4417 because signed overflow is undefined; otherwise, do not change
4418 *STRICT_OVERFLOW_P. */
4419
4420 tree
4421 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4422 bool *strict_overflow_p)
4423 {
4424 enum tree_code code;
4425 tree arg0, arg1 = NULL_TREE;
4426 tree exp_type, nexp;
4427 int in_p;
4428 tree low, high;
4429 location_t loc = EXPR_LOCATION (exp);
4430
4431 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4432 and see if we can refine the range. Some of the cases below may not
4433 happen, but it doesn't seem worth worrying about this. We "continue"
4434 the outer loop when we've changed something; otherwise we "break"
4435 the switch, which will "break" the while. */
4436
4437 in_p = 0;
4438 low = high = build_int_cst (TREE_TYPE (exp), 0);
4439
4440 while (1)
4441 {
4442 code = TREE_CODE (exp);
4443 exp_type = TREE_TYPE (exp);
4444 arg0 = NULL_TREE;
4445
4446 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4447 {
4448 if (TREE_OPERAND_LENGTH (exp) > 0)
4449 arg0 = TREE_OPERAND (exp, 0);
4450 if (TREE_CODE_CLASS (code) == tcc_binary
4451 || TREE_CODE_CLASS (code) == tcc_comparison
4452 || (TREE_CODE_CLASS (code) == tcc_expression
4453 && TREE_OPERAND_LENGTH (exp) > 1))
4454 arg1 = TREE_OPERAND (exp, 1);
4455 }
4456 if (arg0 == NULL_TREE)
4457 break;
4458
4459 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4460 &high, &in_p, strict_overflow_p);
4461 if (nexp == NULL_TREE)
4462 break;
4463 exp = nexp;
4464 }
4465
4466 /* If EXP is a constant, we can evaluate whether this is true or false. */
4467 if (TREE_CODE (exp) == INTEGER_CST)
4468 {
4469 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4470 exp, 0, low, 0))
4471 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4472 exp, 1, high, 1)));
4473 low = high = 0;
4474 exp = 0;
4475 }
4476
4477 *pin_p = in_p, *plow = low, *phigh = high;
4478 return exp;
4479 }
4480 \f
4481 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4482 type, TYPE, return an expression to test if EXP is in (or out of, depending
4483 on IN_P) the range. Return 0 if the test couldn't be created. */
4484
4485 tree
4486 build_range_check (location_t loc, tree type, tree exp, int in_p,
4487 tree low, tree high)
4488 {
4489 tree etype = TREE_TYPE (exp), value;
4490
4491 #ifdef HAVE_canonicalize_funcptr_for_compare
4492 /* Disable this optimization for function pointer expressions
4493 on targets that require function pointer canonicalization. */
4494 if (HAVE_canonicalize_funcptr_for_compare
4495 && TREE_CODE (etype) == POINTER_TYPE
4496 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4497 return NULL_TREE;
4498 #endif
4499
4500 if (! in_p)
4501 {
4502 value = build_range_check (loc, type, exp, 1, low, high);
4503 if (value != 0)
4504 return invert_truthvalue_loc (loc, value);
4505
4506 return 0;
4507 }
4508
4509 if (low == 0 && high == 0)
4510 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4511
4512 if (low == 0)
4513 return fold_build2_loc (loc, LE_EXPR, type, exp,
4514 fold_convert_loc (loc, etype, high));
4515
4516 if (high == 0)
4517 return fold_build2_loc (loc, GE_EXPR, type, exp,
4518 fold_convert_loc (loc, etype, low));
4519
4520 if (operand_equal_p (low, high, 0))
4521 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4522 fold_convert_loc (loc, etype, low));
4523
4524 if (integer_zerop (low))
4525 {
4526 if (! TYPE_UNSIGNED (etype))
4527 {
4528 etype = unsigned_type_for (etype);
4529 high = fold_convert_loc (loc, etype, high);
4530 exp = fold_convert_loc (loc, etype, exp);
4531 }
4532 return build_range_check (loc, type, exp, 1, 0, high);
4533 }
4534
4535 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4536 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4537 {
4538 int prec = TYPE_PRECISION (etype);
4539
4540 if (wi::mask (prec - 1, false, prec) == high)
4541 {
4542 if (TYPE_UNSIGNED (etype))
4543 {
4544 tree signed_etype = signed_type_for (etype);
4545 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4546 etype
4547 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4548 else
4549 etype = signed_etype;
4550 exp = fold_convert_loc (loc, etype, exp);
4551 }
4552 return fold_build2_loc (loc, GT_EXPR, type, exp,
4553 build_int_cst (etype, 0));
4554 }
4555 }
4556
4557 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4558 This requires wrap-around arithmetics for the type of the expression.
4559 First make sure that arithmetics in this type is valid, then make sure
4560 that it wraps around. */
4561 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4562 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4563 TYPE_UNSIGNED (etype));
4564
4565 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4566 {
4567 tree utype, minv, maxv;
4568
4569 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4570 for the type in question, as we rely on this here. */
4571 utype = unsigned_type_for (etype);
4572 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4573 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4574 build_int_cst (TREE_TYPE (maxv), 1), 1);
4575 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4576
4577 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4578 minv, 1, maxv, 1)))
4579 etype = utype;
4580 else
4581 return 0;
4582 }
4583
4584 high = fold_convert_loc (loc, etype, high);
4585 low = fold_convert_loc (loc, etype, low);
4586 exp = fold_convert_loc (loc, etype, exp);
4587
4588 value = const_binop (MINUS_EXPR, high, low);
4589
4590
4591 if (POINTER_TYPE_P (etype))
4592 {
4593 if (value != 0 && !TREE_OVERFLOW (value))
4594 {
4595 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4596 return build_range_check (loc, type,
4597 fold_build_pointer_plus_loc (loc, exp, low),
4598 1, build_int_cst (etype, 0), value);
4599 }
4600 return 0;
4601 }
4602
4603 if (value != 0 && !TREE_OVERFLOW (value))
4604 return build_range_check (loc, type,
4605 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4606 1, build_int_cst (etype, 0), value);
4607
4608 return 0;
4609 }
4610 \f
4611 /* Return the predecessor of VAL in its type, handling the infinite case. */
4612
4613 static tree
4614 range_predecessor (tree val)
4615 {
4616 tree type = TREE_TYPE (val);
4617
4618 if (INTEGRAL_TYPE_P (type)
4619 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4620 return 0;
4621 else
4622 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4623 build_int_cst (TREE_TYPE (val), 1), 0);
4624 }
4625
4626 /* Return the successor of VAL in its type, handling the infinite case. */
4627
4628 static tree
4629 range_successor (tree val)
4630 {
4631 tree type = TREE_TYPE (val);
4632
4633 if (INTEGRAL_TYPE_P (type)
4634 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4635 return 0;
4636 else
4637 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4638 build_int_cst (TREE_TYPE (val), 1), 0);
4639 }
4640
4641 /* Given two ranges, see if we can merge them into one. Return 1 if we
4642 can, 0 if we can't. Set the output range into the specified parameters. */
4643
4644 bool
4645 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4646 tree high0, int in1_p, tree low1, tree high1)
4647 {
4648 int no_overlap;
4649 int subset;
4650 int temp;
4651 tree tem;
4652 int in_p;
4653 tree low, high;
4654 int lowequal = ((low0 == 0 && low1 == 0)
4655 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4656 low0, 0, low1, 0)));
4657 int highequal = ((high0 == 0 && high1 == 0)
4658 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4659 high0, 1, high1, 1)));
4660
4661 /* Make range 0 be the range that starts first, or ends last if they
4662 start at the same value. Swap them if it isn't. */
4663 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4664 low0, 0, low1, 0))
4665 || (lowequal
4666 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4667 high1, 1, high0, 1))))
4668 {
4669 temp = in0_p, in0_p = in1_p, in1_p = temp;
4670 tem = low0, low0 = low1, low1 = tem;
4671 tem = high0, high0 = high1, high1 = tem;
4672 }
4673
4674 /* Now flag two cases, whether the ranges are disjoint or whether the
4675 second range is totally subsumed in the first. Note that the tests
4676 below are simplified by the ones above. */
4677 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4678 high0, 1, low1, 0));
4679 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4680 high1, 1, high0, 1));
4681
4682 /* We now have four cases, depending on whether we are including or
4683 excluding the two ranges. */
4684 if (in0_p && in1_p)
4685 {
4686 /* If they don't overlap, the result is false. If the second range
4687 is a subset it is the result. Otherwise, the range is from the start
4688 of the second to the end of the first. */
4689 if (no_overlap)
4690 in_p = 0, low = high = 0;
4691 else if (subset)
4692 in_p = 1, low = low1, high = high1;
4693 else
4694 in_p = 1, low = low1, high = high0;
4695 }
4696
4697 else if (in0_p && ! in1_p)
4698 {
4699 /* If they don't overlap, the result is the first range. If they are
4700 equal, the result is false. If the second range is a subset of the
4701 first, and the ranges begin at the same place, we go from just after
4702 the end of the second range to the end of the first. If the second
4703 range is not a subset of the first, or if it is a subset and both
4704 ranges end at the same place, the range starts at the start of the
4705 first range and ends just before the second range.
4706 Otherwise, we can't describe this as a single range. */
4707 if (no_overlap)
4708 in_p = 1, low = low0, high = high0;
4709 else if (lowequal && highequal)
4710 in_p = 0, low = high = 0;
4711 else if (subset && lowequal)
4712 {
4713 low = range_successor (high1);
4714 high = high0;
4715 in_p = 1;
4716 if (low == 0)
4717 {
4718 /* We are in the weird situation where high0 > high1 but
4719 high1 has no successor. Punt. */
4720 return 0;
4721 }
4722 }
4723 else if (! subset || highequal)
4724 {
4725 low = low0;
4726 high = range_predecessor (low1);
4727 in_p = 1;
4728 if (high == 0)
4729 {
4730 /* low0 < low1 but low1 has no predecessor. Punt. */
4731 return 0;
4732 }
4733 }
4734 else
4735 return 0;
4736 }
4737
4738 else if (! in0_p && in1_p)
4739 {
4740 /* If they don't overlap, the result is the second range. If the second
4741 is a subset of the first, the result is false. Otherwise,
4742 the range starts just after the first range and ends at the
4743 end of the second. */
4744 if (no_overlap)
4745 in_p = 1, low = low1, high = high1;
4746 else if (subset || highequal)
4747 in_p = 0, low = high = 0;
4748 else
4749 {
4750 low = range_successor (high0);
4751 high = high1;
4752 in_p = 1;
4753 if (low == 0)
4754 {
4755 /* high1 > high0 but high0 has no successor. Punt. */
4756 return 0;
4757 }
4758 }
4759 }
4760
4761 else
4762 {
4763 /* The case where we are excluding both ranges. Here the complex case
4764 is if they don't overlap. In that case, the only time we have a
4765 range is if they are adjacent. If the second is a subset of the
4766 first, the result is the first. Otherwise, the range to exclude
4767 starts at the beginning of the first range and ends at the end of the
4768 second. */
4769 if (no_overlap)
4770 {
4771 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4772 range_successor (high0),
4773 1, low1, 0)))
4774 in_p = 0, low = low0, high = high1;
4775 else
4776 {
4777 /* Canonicalize - [min, x] into - [-, x]. */
4778 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4779 switch (TREE_CODE (TREE_TYPE (low0)))
4780 {
4781 case ENUMERAL_TYPE:
4782 if (TYPE_PRECISION (TREE_TYPE (low0))
4783 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4784 break;
4785 /* FALLTHROUGH */
4786 case INTEGER_TYPE:
4787 if (tree_int_cst_equal (low0,
4788 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4789 low0 = 0;
4790 break;
4791 case POINTER_TYPE:
4792 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4793 && integer_zerop (low0))
4794 low0 = 0;
4795 break;
4796 default:
4797 break;
4798 }
4799
4800 /* Canonicalize - [x, max] into - [x, -]. */
4801 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4802 switch (TREE_CODE (TREE_TYPE (high1)))
4803 {
4804 case ENUMERAL_TYPE:
4805 if (TYPE_PRECISION (TREE_TYPE (high1))
4806 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4807 break;
4808 /* FALLTHROUGH */
4809 case INTEGER_TYPE:
4810 if (tree_int_cst_equal (high1,
4811 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4812 high1 = 0;
4813 break;
4814 case POINTER_TYPE:
4815 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4816 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4817 high1, 1,
4818 build_int_cst (TREE_TYPE (high1), 1),
4819 1)))
4820 high1 = 0;
4821 break;
4822 default:
4823 break;
4824 }
4825
4826 /* The ranges might be also adjacent between the maximum and
4827 minimum values of the given type. For
4828 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4829 return + [x + 1, y - 1]. */
4830 if (low0 == 0 && high1 == 0)
4831 {
4832 low = range_successor (high0);
4833 high = range_predecessor (low1);
4834 if (low == 0 || high == 0)
4835 return 0;
4836
4837 in_p = 1;
4838 }
4839 else
4840 return 0;
4841 }
4842 }
4843 else if (subset)
4844 in_p = 0, low = low0, high = high0;
4845 else
4846 in_p = 0, low = low0, high = high1;
4847 }
4848
4849 *pin_p = in_p, *plow = low, *phigh = high;
4850 return 1;
4851 }
4852 \f
4853
4854 /* Subroutine of fold, looking inside expressions of the form
4855 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4856 of the COND_EXPR. This function is being used also to optimize
4857 A op B ? C : A, by reversing the comparison first.
4858
4859 Return a folded expression whose code is not a COND_EXPR
4860 anymore, or NULL_TREE if no folding opportunity is found. */
4861
4862 static tree
4863 fold_cond_expr_with_comparison (location_t loc, tree type,
4864 tree arg0, tree arg1, tree arg2)
4865 {
4866 enum tree_code comp_code = TREE_CODE (arg0);
4867 tree arg00 = TREE_OPERAND (arg0, 0);
4868 tree arg01 = TREE_OPERAND (arg0, 1);
4869 tree arg1_type = TREE_TYPE (arg1);
4870 tree tem;
4871
4872 STRIP_NOPS (arg1);
4873 STRIP_NOPS (arg2);
4874
4875 /* If we have A op 0 ? A : -A, consider applying the following
4876 transformations:
4877
4878 A == 0? A : -A same as -A
4879 A != 0? A : -A same as A
4880 A >= 0? A : -A same as abs (A)
4881 A > 0? A : -A same as abs (A)
4882 A <= 0? A : -A same as -abs (A)
4883 A < 0? A : -A same as -abs (A)
4884
4885 None of these transformations work for modes with signed
4886 zeros. If A is +/-0, the first two transformations will
4887 change the sign of the result (from +0 to -0, or vice
4888 versa). The last four will fix the sign of the result,
4889 even though the original expressions could be positive or
4890 negative, depending on the sign of A.
4891
4892 Note that all these transformations are correct if A is
4893 NaN, since the two alternatives (A and -A) are also NaNs. */
4894 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4895 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4896 ? real_zerop (arg01)
4897 : integer_zerop (arg01))
4898 && ((TREE_CODE (arg2) == NEGATE_EXPR
4899 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4900 /* In the case that A is of the form X-Y, '-A' (arg2) may
4901 have already been folded to Y-X, check for that. */
4902 || (TREE_CODE (arg1) == MINUS_EXPR
4903 && TREE_CODE (arg2) == MINUS_EXPR
4904 && operand_equal_p (TREE_OPERAND (arg1, 0),
4905 TREE_OPERAND (arg2, 1), 0)
4906 && operand_equal_p (TREE_OPERAND (arg1, 1),
4907 TREE_OPERAND (arg2, 0), 0))))
4908 switch (comp_code)
4909 {
4910 case EQ_EXPR:
4911 case UNEQ_EXPR:
4912 tem = fold_convert_loc (loc, arg1_type, arg1);
4913 return pedantic_non_lvalue_loc (loc,
4914 fold_convert_loc (loc, type,
4915 negate_expr (tem)));
4916 case NE_EXPR:
4917 case LTGT_EXPR:
4918 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4919 case UNGE_EXPR:
4920 case UNGT_EXPR:
4921 if (flag_trapping_math)
4922 break;
4923 /* Fall through. */
4924 case GE_EXPR:
4925 case GT_EXPR:
4926 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4927 arg1 = fold_convert_loc (loc, signed_type_for
4928 (TREE_TYPE (arg1)), arg1);
4929 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4930 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4931 case UNLE_EXPR:
4932 case UNLT_EXPR:
4933 if (flag_trapping_math)
4934 break;
4935 case LE_EXPR:
4936 case LT_EXPR:
4937 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4938 arg1 = fold_convert_loc (loc, signed_type_for
4939 (TREE_TYPE (arg1)), arg1);
4940 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4941 return negate_expr (fold_convert_loc (loc, type, tem));
4942 default:
4943 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4944 break;
4945 }
4946
4947 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4948 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4949 both transformations are correct when A is NaN: A != 0
4950 is then true, and A == 0 is false. */
4951
4952 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4953 && integer_zerop (arg01) && integer_zerop (arg2))
4954 {
4955 if (comp_code == NE_EXPR)
4956 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4957 else if (comp_code == EQ_EXPR)
4958 return build_zero_cst (type);
4959 }
4960
4961 /* Try some transformations of A op B ? A : B.
4962
4963 A == B? A : B same as B
4964 A != B? A : B same as A
4965 A >= B? A : B same as max (A, B)
4966 A > B? A : B same as max (B, A)
4967 A <= B? A : B same as min (A, B)
4968 A < B? A : B same as min (B, A)
4969
4970 As above, these transformations don't work in the presence
4971 of signed zeros. For example, if A and B are zeros of
4972 opposite sign, the first two transformations will change
4973 the sign of the result. In the last four, the original
4974 expressions give different results for (A=+0, B=-0) and
4975 (A=-0, B=+0), but the transformed expressions do not.
4976
4977 The first two transformations are correct if either A or B
4978 is a NaN. In the first transformation, the condition will
4979 be false, and B will indeed be chosen. In the case of the
4980 second transformation, the condition A != B will be true,
4981 and A will be chosen.
4982
4983 The conversions to max() and min() are not correct if B is
4984 a number and A is not. The conditions in the original
4985 expressions will be false, so all four give B. The min()
4986 and max() versions would give a NaN instead. */
4987 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4988 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4989 /* Avoid these transformations if the COND_EXPR may be used
4990 as an lvalue in the C++ front-end. PR c++/19199. */
4991 && (in_gimple_form
4992 || VECTOR_TYPE_P (type)
4993 || (! lang_GNU_CXX ()
4994 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4995 || ! maybe_lvalue_p (arg1)
4996 || ! maybe_lvalue_p (arg2)))
4997 {
4998 tree comp_op0 = arg00;
4999 tree comp_op1 = arg01;
5000 tree comp_type = TREE_TYPE (comp_op0);
5001
5002 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5003 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5004 {
5005 comp_type = type;
5006 comp_op0 = arg1;
5007 comp_op1 = arg2;
5008 }
5009
5010 switch (comp_code)
5011 {
5012 case EQ_EXPR:
5013 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5014 case NE_EXPR:
5015 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5016 case LE_EXPR:
5017 case LT_EXPR:
5018 case UNLE_EXPR:
5019 case UNLT_EXPR:
5020 /* In C++ a ?: expression can be an lvalue, so put the
5021 operand which will be used if they are equal first
5022 so that we can convert this back to the
5023 corresponding COND_EXPR. */
5024 if (!HONOR_NANS (arg1))
5025 {
5026 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5027 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5028 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5029 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5030 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5031 comp_op1, comp_op0);
5032 return pedantic_non_lvalue_loc (loc,
5033 fold_convert_loc (loc, type, tem));
5034 }
5035 break;
5036 case GE_EXPR:
5037 case GT_EXPR:
5038 case UNGE_EXPR:
5039 case UNGT_EXPR:
5040 if (!HONOR_NANS (arg1))
5041 {
5042 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5043 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5044 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5045 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5046 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5047 comp_op1, comp_op0);
5048 return pedantic_non_lvalue_loc (loc,
5049 fold_convert_loc (loc, type, tem));
5050 }
5051 break;
5052 case UNEQ_EXPR:
5053 if (!HONOR_NANS (arg1))
5054 return pedantic_non_lvalue_loc (loc,
5055 fold_convert_loc (loc, type, arg2));
5056 break;
5057 case LTGT_EXPR:
5058 if (!HONOR_NANS (arg1))
5059 return pedantic_non_lvalue_loc (loc,
5060 fold_convert_loc (loc, type, arg1));
5061 break;
5062 default:
5063 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5064 break;
5065 }
5066 }
5067
5068 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5069 we might still be able to simplify this. For example,
5070 if C1 is one less or one more than C2, this might have started
5071 out as a MIN or MAX and been transformed by this function.
5072 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5073
5074 if (INTEGRAL_TYPE_P (type)
5075 && TREE_CODE (arg01) == INTEGER_CST
5076 && TREE_CODE (arg2) == INTEGER_CST)
5077 switch (comp_code)
5078 {
5079 case EQ_EXPR:
5080 if (TREE_CODE (arg1) == INTEGER_CST)
5081 break;
5082 /* We can replace A with C1 in this case. */
5083 arg1 = fold_convert_loc (loc, type, arg01);
5084 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5085
5086 case LT_EXPR:
5087 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5088 MIN_EXPR, to preserve the signedness of the comparison. */
5089 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5090 OEP_ONLY_CONST)
5091 && operand_equal_p (arg01,
5092 const_binop (PLUS_EXPR, arg2,
5093 build_int_cst (type, 1)),
5094 OEP_ONLY_CONST))
5095 {
5096 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5097 fold_convert_loc (loc, TREE_TYPE (arg00),
5098 arg2));
5099 return pedantic_non_lvalue_loc (loc,
5100 fold_convert_loc (loc, type, tem));
5101 }
5102 break;
5103
5104 case LE_EXPR:
5105 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5106 as above. */
5107 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5108 OEP_ONLY_CONST)
5109 && operand_equal_p (arg01,
5110 const_binop (MINUS_EXPR, arg2,
5111 build_int_cst (type, 1)),
5112 OEP_ONLY_CONST))
5113 {
5114 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5115 fold_convert_loc (loc, TREE_TYPE (arg00),
5116 arg2));
5117 return pedantic_non_lvalue_loc (loc,
5118 fold_convert_loc (loc, type, tem));
5119 }
5120 break;
5121
5122 case GT_EXPR:
5123 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5124 MAX_EXPR, to preserve the signedness of the comparison. */
5125 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5126 OEP_ONLY_CONST)
5127 && operand_equal_p (arg01,
5128 const_binop (MINUS_EXPR, arg2,
5129 build_int_cst (type, 1)),
5130 OEP_ONLY_CONST))
5131 {
5132 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5133 fold_convert_loc (loc, TREE_TYPE (arg00),
5134 arg2));
5135 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5136 }
5137 break;
5138
5139 case GE_EXPR:
5140 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5141 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5142 OEP_ONLY_CONST)
5143 && operand_equal_p (arg01,
5144 const_binop (PLUS_EXPR, arg2,
5145 build_int_cst (type, 1)),
5146 OEP_ONLY_CONST))
5147 {
5148 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5149 fold_convert_loc (loc, TREE_TYPE (arg00),
5150 arg2));
5151 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5152 }
5153 break;
5154 case NE_EXPR:
5155 break;
5156 default:
5157 gcc_unreachable ();
5158 }
5159
5160 return NULL_TREE;
5161 }
5162
5163
5164 \f
5165 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5166 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5167 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5168 false) >= 2)
5169 #endif
5170
5171 /* EXP is some logical combination of boolean tests. See if we can
5172 merge it into some range test. Return the new tree if so. */
5173
5174 static tree
5175 fold_range_test (location_t loc, enum tree_code code, tree type,
5176 tree op0, tree op1)
5177 {
5178 int or_op = (code == TRUTH_ORIF_EXPR
5179 || code == TRUTH_OR_EXPR);
5180 int in0_p, in1_p, in_p;
5181 tree low0, low1, low, high0, high1, high;
5182 bool strict_overflow_p = false;
5183 tree tem, lhs, rhs;
5184 const char * const warnmsg = G_("assuming signed overflow does not occur "
5185 "when simplifying range test");
5186
5187 if (!INTEGRAL_TYPE_P (type))
5188 return 0;
5189
5190 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5191 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5192
5193 /* If this is an OR operation, invert both sides; we will invert
5194 again at the end. */
5195 if (or_op)
5196 in0_p = ! in0_p, in1_p = ! in1_p;
5197
5198 /* If both expressions are the same, if we can merge the ranges, and we
5199 can build the range test, return it or it inverted. If one of the
5200 ranges is always true or always false, consider it to be the same
5201 expression as the other. */
5202 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5203 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5204 in1_p, low1, high1)
5205 && 0 != (tem = (build_range_check (loc, type,
5206 lhs != 0 ? lhs
5207 : rhs != 0 ? rhs : integer_zero_node,
5208 in_p, low, high))))
5209 {
5210 if (strict_overflow_p)
5211 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5212 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5213 }
5214
5215 /* On machines where the branch cost is expensive, if this is a
5216 short-circuited branch and the underlying object on both sides
5217 is the same, make a non-short-circuit operation. */
5218 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5219 && lhs != 0 && rhs != 0
5220 && (code == TRUTH_ANDIF_EXPR
5221 || code == TRUTH_ORIF_EXPR)
5222 && operand_equal_p (lhs, rhs, 0))
5223 {
5224 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5225 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5226 which cases we can't do this. */
5227 if (simple_operand_p (lhs))
5228 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5229 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5230 type, op0, op1);
5231
5232 else if (!lang_hooks.decls.global_bindings_p ()
5233 && !CONTAINS_PLACEHOLDER_P (lhs))
5234 {
5235 tree common = save_expr (lhs);
5236
5237 if (0 != (lhs = build_range_check (loc, type, common,
5238 or_op ? ! in0_p : in0_p,
5239 low0, high0))
5240 && (0 != (rhs = build_range_check (loc, type, common,
5241 or_op ? ! in1_p : in1_p,
5242 low1, high1))))
5243 {
5244 if (strict_overflow_p)
5245 fold_overflow_warning (warnmsg,
5246 WARN_STRICT_OVERFLOW_COMPARISON);
5247 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5248 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5249 type, lhs, rhs);
5250 }
5251 }
5252 }
5253
5254 return 0;
5255 }
5256 \f
5257 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5258 bit value. Arrange things so the extra bits will be set to zero if and
5259 only if C is signed-extended to its full width. If MASK is nonzero,
5260 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5261
5262 static tree
5263 unextend (tree c, int p, int unsignedp, tree mask)
5264 {
5265 tree type = TREE_TYPE (c);
5266 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5267 tree temp;
5268
5269 if (p == modesize || unsignedp)
5270 return c;
5271
5272 /* We work by getting just the sign bit into the low-order bit, then
5273 into the high-order bit, then sign-extend. We then XOR that value
5274 with C. */
5275 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5276
5277 /* We must use a signed type in order to get an arithmetic right shift.
5278 However, we must also avoid introducing accidental overflows, so that
5279 a subsequent call to integer_zerop will work. Hence we must
5280 do the type conversion here. At this point, the constant is either
5281 zero or one, and the conversion to a signed type can never overflow.
5282 We could get an overflow if this conversion is done anywhere else. */
5283 if (TYPE_UNSIGNED (type))
5284 temp = fold_convert (signed_type_for (type), temp);
5285
5286 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5287 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5288 if (mask != 0)
5289 temp = const_binop (BIT_AND_EXPR, temp,
5290 fold_convert (TREE_TYPE (c), mask));
5291 /* If necessary, convert the type back to match the type of C. */
5292 if (TYPE_UNSIGNED (type))
5293 temp = fold_convert (type, temp);
5294
5295 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5296 }
5297 \f
5298 /* For an expression that has the form
5299 (A && B) || ~B
5300 or
5301 (A || B) && ~B,
5302 we can drop one of the inner expressions and simplify to
5303 A || ~B
5304 or
5305 A && ~B
5306 LOC is the location of the resulting expression. OP is the inner
5307 logical operation; the left-hand side in the examples above, while CMPOP
5308 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5309 removing a condition that guards another, as in
5310 (A != NULL && A->...) || A == NULL
5311 which we must not transform. If RHS_ONLY is true, only eliminate the
5312 right-most operand of the inner logical operation. */
5313
5314 static tree
5315 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5316 bool rhs_only)
5317 {
5318 tree type = TREE_TYPE (cmpop);
5319 enum tree_code code = TREE_CODE (cmpop);
5320 enum tree_code truthop_code = TREE_CODE (op);
5321 tree lhs = TREE_OPERAND (op, 0);
5322 tree rhs = TREE_OPERAND (op, 1);
5323 tree orig_lhs = lhs, orig_rhs = rhs;
5324 enum tree_code rhs_code = TREE_CODE (rhs);
5325 enum tree_code lhs_code = TREE_CODE (lhs);
5326 enum tree_code inv_code;
5327
5328 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5329 return NULL_TREE;
5330
5331 if (TREE_CODE_CLASS (code) != tcc_comparison)
5332 return NULL_TREE;
5333
5334 if (rhs_code == truthop_code)
5335 {
5336 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5337 if (newrhs != NULL_TREE)
5338 {
5339 rhs = newrhs;
5340 rhs_code = TREE_CODE (rhs);
5341 }
5342 }
5343 if (lhs_code == truthop_code && !rhs_only)
5344 {
5345 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5346 if (newlhs != NULL_TREE)
5347 {
5348 lhs = newlhs;
5349 lhs_code = TREE_CODE (lhs);
5350 }
5351 }
5352
5353 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5354 if (inv_code == rhs_code
5355 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5356 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5357 return lhs;
5358 if (!rhs_only && inv_code == lhs_code
5359 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5360 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5361 return rhs;
5362 if (rhs != orig_rhs || lhs != orig_lhs)
5363 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5364 lhs, rhs);
5365 return NULL_TREE;
5366 }
5367
5368 /* Find ways of folding logical expressions of LHS and RHS:
5369 Try to merge two comparisons to the same innermost item.
5370 Look for range tests like "ch >= '0' && ch <= '9'".
5371 Look for combinations of simple terms on machines with expensive branches
5372 and evaluate the RHS unconditionally.
5373
5374 For example, if we have p->a == 2 && p->b == 4 and we can make an
5375 object large enough to span both A and B, we can do this with a comparison
5376 against the object ANDed with the a mask.
5377
5378 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5379 operations to do this with one comparison.
5380
5381 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5382 function and the one above.
5383
5384 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5385 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5386
5387 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5388 two operands.
5389
5390 We return the simplified tree or 0 if no optimization is possible. */
5391
5392 static tree
5393 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5394 tree lhs, tree rhs)
5395 {
5396 /* If this is the "or" of two comparisons, we can do something if
5397 the comparisons are NE_EXPR. If this is the "and", we can do something
5398 if the comparisons are EQ_EXPR. I.e.,
5399 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5400
5401 WANTED_CODE is this operation code. For single bit fields, we can
5402 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5403 comparison for one-bit fields. */
5404
5405 enum tree_code wanted_code;
5406 enum tree_code lcode, rcode;
5407 tree ll_arg, lr_arg, rl_arg, rr_arg;
5408 tree ll_inner, lr_inner, rl_inner, rr_inner;
5409 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5410 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5411 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5412 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5413 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5414 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5415 machine_mode lnmode, rnmode;
5416 tree ll_mask, lr_mask, rl_mask, rr_mask;
5417 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5418 tree l_const, r_const;
5419 tree lntype, rntype, result;
5420 HOST_WIDE_INT first_bit, end_bit;
5421 int volatilep;
5422
5423 /* Start by getting the comparison codes. Fail if anything is volatile.
5424 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5425 it were surrounded with a NE_EXPR. */
5426
5427 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5428 return 0;
5429
5430 lcode = TREE_CODE (lhs);
5431 rcode = TREE_CODE (rhs);
5432
5433 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5434 {
5435 lhs = build2 (NE_EXPR, truth_type, lhs,
5436 build_int_cst (TREE_TYPE (lhs), 0));
5437 lcode = NE_EXPR;
5438 }
5439
5440 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5441 {
5442 rhs = build2 (NE_EXPR, truth_type, rhs,
5443 build_int_cst (TREE_TYPE (rhs), 0));
5444 rcode = NE_EXPR;
5445 }
5446
5447 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5448 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5449 return 0;
5450
5451 ll_arg = TREE_OPERAND (lhs, 0);
5452 lr_arg = TREE_OPERAND (lhs, 1);
5453 rl_arg = TREE_OPERAND (rhs, 0);
5454 rr_arg = TREE_OPERAND (rhs, 1);
5455
5456 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5457 if (simple_operand_p (ll_arg)
5458 && simple_operand_p (lr_arg))
5459 {
5460 if (operand_equal_p (ll_arg, rl_arg, 0)
5461 && operand_equal_p (lr_arg, rr_arg, 0))
5462 {
5463 result = combine_comparisons (loc, code, lcode, rcode,
5464 truth_type, ll_arg, lr_arg);
5465 if (result)
5466 return result;
5467 }
5468 else if (operand_equal_p (ll_arg, rr_arg, 0)
5469 && operand_equal_p (lr_arg, rl_arg, 0))
5470 {
5471 result = combine_comparisons (loc, code, lcode,
5472 swap_tree_comparison (rcode),
5473 truth_type, ll_arg, lr_arg);
5474 if (result)
5475 return result;
5476 }
5477 }
5478
5479 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5480 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5481
5482 /* If the RHS can be evaluated unconditionally and its operands are
5483 simple, it wins to evaluate the RHS unconditionally on machines
5484 with expensive branches. In this case, this isn't a comparison
5485 that can be merged. */
5486
5487 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5488 false) >= 2
5489 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5490 && simple_operand_p (rl_arg)
5491 && simple_operand_p (rr_arg))
5492 {
5493 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5494 if (code == TRUTH_OR_EXPR
5495 && lcode == NE_EXPR && integer_zerop (lr_arg)
5496 && rcode == NE_EXPR && integer_zerop (rr_arg)
5497 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5498 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5499 return build2_loc (loc, NE_EXPR, truth_type,
5500 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5501 ll_arg, rl_arg),
5502 build_int_cst (TREE_TYPE (ll_arg), 0));
5503
5504 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5505 if (code == TRUTH_AND_EXPR
5506 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5507 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5508 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5509 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5510 return build2_loc (loc, EQ_EXPR, truth_type,
5511 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5512 ll_arg, rl_arg),
5513 build_int_cst (TREE_TYPE (ll_arg), 0));
5514 }
5515
5516 /* See if the comparisons can be merged. Then get all the parameters for
5517 each side. */
5518
5519 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5520 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5521 return 0;
5522
5523 volatilep = 0;
5524 ll_inner = decode_field_reference (loc, ll_arg,
5525 &ll_bitsize, &ll_bitpos, &ll_mode,
5526 &ll_unsignedp, &volatilep, &ll_mask,
5527 &ll_and_mask);
5528 lr_inner = decode_field_reference (loc, lr_arg,
5529 &lr_bitsize, &lr_bitpos, &lr_mode,
5530 &lr_unsignedp, &volatilep, &lr_mask,
5531 &lr_and_mask);
5532 rl_inner = decode_field_reference (loc, rl_arg,
5533 &rl_bitsize, &rl_bitpos, &rl_mode,
5534 &rl_unsignedp, &volatilep, &rl_mask,
5535 &rl_and_mask);
5536 rr_inner = decode_field_reference (loc, rr_arg,
5537 &rr_bitsize, &rr_bitpos, &rr_mode,
5538 &rr_unsignedp, &volatilep, &rr_mask,
5539 &rr_and_mask);
5540
5541 /* It must be true that the inner operation on the lhs of each
5542 comparison must be the same if we are to be able to do anything.
5543 Then see if we have constants. If not, the same must be true for
5544 the rhs's. */
5545 if (volatilep || ll_inner == 0 || rl_inner == 0
5546 || ! operand_equal_p (ll_inner, rl_inner, 0))
5547 return 0;
5548
5549 if (TREE_CODE (lr_arg) == INTEGER_CST
5550 && TREE_CODE (rr_arg) == INTEGER_CST)
5551 l_const = lr_arg, r_const = rr_arg;
5552 else if (lr_inner == 0 || rr_inner == 0
5553 || ! operand_equal_p (lr_inner, rr_inner, 0))
5554 return 0;
5555 else
5556 l_const = r_const = 0;
5557
5558 /* If either comparison code is not correct for our logical operation,
5559 fail. However, we can convert a one-bit comparison against zero into
5560 the opposite comparison against that bit being set in the field. */
5561
5562 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5563 if (lcode != wanted_code)
5564 {
5565 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5566 {
5567 /* Make the left operand unsigned, since we are only interested
5568 in the value of one bit. Otherwise we are doing the wrong
5569 thing below. */
5570 ll_unsignedp = 1;
5571 l_const = ll_mask;
5572 }
5573 else
5574 return 0;
5575 }
5576
5577 /* This is analogous to the code for l_const above. */
5578 if (rcode != wanted_code)
5579 {
5580 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5581 {
5582 rl_unsignedp = 1;
5583 r_const = rl_mask;
5584 }
5585 else
5586 return 0;
5587 }
5588
5589 /* See if we can find a mode that contains both fields being compared on
5590 the left. If we can't, fail. Otherwise, update all constants and masks
5591 to be relative to a field of that size. */
5592 first_bit = MIN (ll_bitpos, rl_bitpos);
5593 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5594 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5595 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5596 volatilep);
5597 if (lnmode == VOIDmode)
5598 return 0;
5599
5600 lnbitsize = GET_MODE_BITSIZE (lnmode);
5601 lnbitpos = first_bit & ~ (lnbitsize - 1);
5602 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5603 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5604
5605 if (BYTES_BIG_ENDIAN)
5606 {
5607 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5608 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5609 }
5610
5611 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5612 size_int (xll_bitpos));
5613 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5614 size_int (xrl_bitpos));
5615
5616 if (l_const)
5617 {
5618 l_const = fold_convert_loc (loc, lntype, l_const);
5619 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5620 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5621 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5622 fold_build1_loc (loc, BIT_NOT_EXPR,
5623 lntype, ll_mask))))
5624 {
5625 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5626
5627 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5628 }
5629 }
5630 if (r_const)
5631 {
5632 r_const = fold_convert_loc (loc, lntype, r_const);
5633 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5634 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5635 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5636 fold_build1_loc (loc, BIT_NOT_EXPR,
5637 lntype, rl_mask))))
5638 {
5639 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5640
5641 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5642 }
5643 }
5644
5645 /* If the right sides are not constant, do the same for it. Also,
5646 disallow this optimization if a size or signedness mismatch occurs
5647 between the left and right sides. */
5648 if (l_const == 0)
5649 {
5650 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5651 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5652 /* Make sure the two fields on the right
5653 correspond to the left without being swapped. */
5654 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5655 return 0;
5656
5657 first_bit = MIN (lr_bitpos, rr_bitpos);
5658 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5659 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5660 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5661 volatilep);
5662 if (rnmode == VOIDmode)
5663 return 0;
5664
5665 rnbitsize = GET_MODE_BITSIZE (rnmode);
5666 rnbitpos = first_bit & ~ (rnbitsize - 1);
5667 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5668 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5669
5670 if (BYTES_BIG_ENDIAN)
5671 {
5672 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5673 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5674 }
5675
5676 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5677 rntype, lr_mask),
5678 size_int (xlr_bitpos));
5679 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5680 rntype, rr_mask),
5681 size_int (xrr_bitpos));
5682
5683 /* Make a mask that corresponds to both fields being compared.
5684 Do this for both items being compared. If the operands are the
5685 same size and the bits being compared are in the same position
5686 then we can do this by masking both and comparing the masked
5687 results. */
5688 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5689 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5690 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5691 {
5692 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5693 ll_unsignedp || rl_unsignedp);
5694 if (! all_ones_mask_p (ll_mask, lnbitsize))
5695 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5696
5697 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5698 lr_unsignedp || rr_unsignedp);
5699 if (! all_ones_mask_p (lr_mask, rnbitsize))
5700 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5701
5702 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5703 }
5704
5705 /* There is still another way we can do something: If both pairs of
5706 fields being compared are adjacent, we may be able to make a wider
5707 field containing them both.
5708
5709 Note that we still must mask the lhs/rhs expressions. Furthermore,
5710 the mask must be shifted to account for the shift done by
5711 make_bit_field_ref. */
5712 if ((ll_bitsize + ll_bitpos == rl_bitpos
5713 && lr_bitsize + lr_bitpos == rr_bitpos)
5714 || (ll_bitpos == rl_bitpos + rl_bitsize
5715 && lr_bitpos == rr_bitpos + rr_bitsize))
5716 {
5717 tree type;
5718
5719 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5720 ll_bitsize + rl_bitsize,
5721 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5722 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5723 lr_bitsize + rr_bitsize,
5724 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5725
5726 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5727 size_int (MIN (xll_bitpos, xrl_bitpos)));
5728 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5729 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5730
5731 /* Convert to the smaller type before masking out unwanted bits. */
5732 type = lntype;
5733 if (lntype != rntype)
5734 {
5735 if (lnbitsize > rnbitsize)
5736 {
5737 lhs = fold_convert_loc (loc, rntype, lhs);
5738 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5739 type = rntype;
5740 }
5741 else if (lnbitsize < rnbitsize)
5742 {
5743 rhs = fold_convert_loc (loc, lntype, rhs);
5744 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5745 type = lntype;
5746 }
5747 }
5748
5749 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5750 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5751
5752 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5753 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5754
5755 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5756 }
5757
5758 return 0;
5759 }
5760
5761 /* Handle the case of comparisons with constants. If there is something in
5762 common between the masks, those bits of the constants must be the same.
5763 If not, the condition is always false. Test for this to avoid generating
5764 incorrect code below. */
5765 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5766 if (! integer_zerop (result)
5767 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5768 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5769 {
5770 if (wanted_code == NE_EXPR)
5771 {
5772 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5773 return constant_boolean_node (true, truth_type);
5774 }
5775 else
5776 {
5777 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5778 return constant_boolean_node (false, truth_type);
5779 }
5780 }
5781
5782 /* Construct the expression we will return. First get the component
5783 reference we will make. Unless the mask is all ones the width of
5784 that field, perform the mask operation. Then compare with the
5785 merged constant. */
5786 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5787 ll_unsignedp || rl_unsignedp);
5788
5789 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5790 if (! all_ones_mask_p (ll_mask, lnbitsize))
5791 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5792
5793 return build2_loc (loc, wanted_code, truth_type, result,
5794 const_binop (BIT_IOR_EXPR, l_const, r_const));
5795 }
5796 \f
5797 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5798 constant. */
5799
5800 static tree
5801 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5802 tree op0, tree op1)
5803 {
5804 tree arg0 = op0;
5805 enum tree_code op_code;
5806 tree comp_const;
5807 tree minmax_const;
5808 int consts_equal, consts_lt;
5809 tree inner;
5810
5811 STRIP_SIGN_NOPS (arg0);
5812
5813 op_code = TREE_CODE (arg0);
5814 minmax_const = TREE_OPERAND (arg0, 1);
5815 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5816 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5817 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5818 inner = TREE_OPERAND (arg0, 0);
5819
5820 /* If something does not permit us to optimize, return the original tree. */
5821 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5822 || TREE_CODE (comp_const) != INTEGER_CST
5823 || TREE_OVERFLOW (comp_const)
5824 || TREE_CODE (minmax_const) != INTEGER_CST
5825 || TREE_OVERFLOW (minmax_const))
5826 return NULL_TREE;
5827
5828 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5829 and GT_EXPR, doing the rest with recursive calls using logical
5830 simplifications. */
5831 switch (code)
5832 {
5833 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5834 {
5835 tree tem
5836 = optimize_minmax_comparison (loc,
5837 invert_tree_comparison (code, false),
5838 type, op0, op1);
5839 if (tem)
5840 return invert_truthvalue_loc (loc, tem);
5841 return NULL_TREE;
5842 }
5843
5844 case GE_EXPR:
5845 return
5846 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5847 optimize_minmax_comparison
5848 (loc, EQ_EXPR, type, arg0, comp_const),
5849 optimize_minmax_comparison
5850 (loc, GT_EXPR, type, arg0, comp_const));
5851
5852 case EQ_EXPR:
5853 if (op_code == MAX_EXPR && consts_equal)
5854 /* MAX (X, 0) == 0 -> X <= 0 */
5855 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5856
5857 else if (op_code == MAX_EXPR && consts_lt)
5858 /* MAX (X, 0) == 5 -> X == 5 */
5859 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5860
5861 else if (op_code == MAX_EXPR)
5862 /* MAX (X, 0) == -1 -> false */
5863 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5864
5865 else if (consts_equal)
5866 /* MIN (X, 0) == 0 -> X >= 0 */
5867 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5868
5869 else if (consts_lt)
5870 /* MIN (X, 0) == 5 -> false */
5871 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5872
5873 else
5874 /* MIN (X, 0) == -1 -> X == -1 */
5875 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5876
5877 case GT_EXPR:
5878 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5879 /* MAX (X, 0) > 0 -> X > 0
5880 MAX (X, 0) > 5 -> X > 5 */
5881 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5882
5883 else if (op_code == MAX_EXPR)
5884 /* MAX (X, 0) > -1 -> true */
5885 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5886
5887 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5888 /* MIN (X, 0) > 0 -> false
5889 MIN (X, 0) > 5 -> false */
5890 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5891
5892 else
5893 /* MIN (X, 0) > -1 -> X > -1 */
5894 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5895
5896 default:
5897 return NULL_TREE;
5898 }
5899 }
5900 \f
5901 /* T is an integer expression that is being multiplied, divided, or taken a
5902 modulus (CODE says which and what kind of divide or modulus) by a
5903 constant C. See if we can eliminate that operation by folding it with
5904 other operations already in T. WIDE_TYPE, if non-null, is a type that
5905 should be used for the computation if wider than our type.
5906
5907 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5908 (X * 2) + (Y * 4). We must, however, be assured that either the original
5909 expression would not overflow or that overflow is undefined for the type
5910 in the language in question.
5911
5912 If we return a non-null expression, it is an equivalent form of the
5913 original computation, but need not be in the original type.
5914
5915 We set *STRICT_OVERFLOW_P to true if the return values depends on
5916 signed overflow being undefined. Otherwise we do not change
5917 *STRICT_OVERFLOW_P. */
5918
5919 static tree
5920 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5921 bool *strict_overflow_p)
5922 {
5923 /* To avoid exponential search depth, refuse to allow recursion past
5924 three levels. Beyond that (1) it's highly unlikely that we'll find
5925 something interesting and (2) we've probably processed it before
5926 when we built the inner expression. */
5927
5928 static int depth;
5929 tree ret;
5930
5931 if (depth > 3)
5932 return NULL;
5933
5934 depth++;
5935 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5936 depth--;
5937
5938 return ret;
5939 }
5940
5941 static tree
5942 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5943 bool *strict_overflow_p)
5944 {
5945 tree type = TREE_TYPE (t);
5946 enum tree_code tcode = TREE_CODE (t);
5947 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5948 > GET_MODE_SIZE (TYPE_MODE (type)))
5949 ? wide_type : type);
5950 tree t1, t2;
5951 int same_p = tcode == code;
5952 tree op0 = NULL_TREE, op1 = NULL_TREE;
5953 bool sub_strict_overflow_p;
5954
5955 /* Don't deal with constants of zero here; they confuse the code below. */
5956 if (integer_zerop (c))
5957 return NULL_TREE;
5958
5959 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5960 op0 = TREE_OPERAND (t, 0);
5961
5962 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5963 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5964
5965 /* Note that we need not handle conditional operations here since fold
5966 already handles those cases. So just do arithmetic here. */
5967 switch (tcode)
5968 {
5969 case INTEGER_CST:
5970 /* For a constant, we can always simplify if we are a multiply
5971 or (for divide and modulus) if it is a multiple of our constant. */
5972 if (code == MULT_EXPR
5973 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5974 return const_binop (code, fold_convert (ctype, t),
5975 fold_convert (ctype, c));
5976 break;
5977
5978 CASE_CONVERT: case NON_LVALUE_EXPR:
5979 /* If op0 is an expression ... */
5980 if ((COMPARISON_CLASS_P (op0)
5981 || UNARY_CLASS_P (op0)
5982 || BINARY_CLASS_P (op0)
5983 || VL_EXP_CLASS_P (op0)
5984 || EXPRESSION_CLASS_P (op0))
5985 /* ... and has wrapping overflow, and its type is smaller
5986 than ctype, then we cannot pass through as widening. */
5987 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5988 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5989 && (TYPE_PRECISION (ctype)
5990 > TYPE_PRECISION (TREE_TYPE (op0))))
5991 /* ... or this is a truncation (t is narrower than op0),
5992 then we cannot pass through this narrowing. */
5993 || (TYPE_PRECISION (type)
5994 < TYPE_PRECISION (TREE_TYPE (op0)))
5995 /* ... or signedness changes for division or modulus,
5996 then we cannot pass through this conversion. */
5997 || (code != MULT_EXPR
5998 && (TYPE_UNSIGNED (ctype)
5999 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6000 /* ... or has undefined overflow while the converted to
6001 type has not, we cannot do the operation in the inner type
6002 as that would introduce undefined overflow. */
6003 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6004 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6005 && !TYPE_OVERFLOW_UNDEFINED (type))))
6006 break;
6007
6008 /* Pass the constant down and see if we can make a simplification. If
6009 we can, replace this expression with the inner simplification for
6010 possible later conversion to our or some other type. */
6011 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6012 && TREE_CODE (t2) == INTEGER_CST
6013 && !TREE_OVERFLOW (t2)
6014 && (0 != (t1 = extract_muldiv (op0, t2, code,
6015 code == MULT_EXPR
6016 ? ctype : NULL_TREE,
6017 strict_overflow_p))))
6018 return t1;
6019 break;
6020
6021 case ABS_EXPR:
6022 /* If widening the type changes it from signed to unsigned, then we
6023 must avoid building ABS_EXPR itself as unsigned. */
6024 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6025 {
6026 tree cstype = (*signed_type_for) (ctype);
6027 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6028 != 0)
6029 {
6030 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6031 return fold_convert (ctype, t1);
6032 }
6033 break;
6034 }
6035 /* If the constant is negative, we cannot simplify this. */
6036 if (tree_int_cst_sgn (c) == -1)
6037 break;
6038 /* FALLTHROUGH */
6039 case NEGATE_EXPR:
6040 /* For division and modulus, type can't be unsigned, as e.g.
6041 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6042 For signed types, even with wrapping overflow, this is fine. */
6043 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6044 break;
6045 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6046 != 0)
6047 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6048 break;
6049
6050 case MIN_EXPR: case MAX_EXPR:
6051 /* If widening the type changes the signedness, then we can't perform
6052 this optimization as that changes the result. */
6053 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6054 break;
6055
6056 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6057 sub_strict_overflow_p = false;
6058 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6059 &sub_strict_overflow_p)) != 0
6060 && (t2 = extract_muldiv (op1, c, code, wide_type,
6061 &sub_strict_overflow_p)) != 0)
6062 {
6063 if (tree_int_cst_sgn (c) < 0)
6064 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6065 if (sub_strict_overflow_p)
6066 *strict_overflow_p = true;
6067 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6068 fold_convert (ctype, t2));
6069 }
6070 break;
6071
6072 case LSHIFT_EXPR: case RSHIFT_EXPR:
6073 /* If the second operand is constant, this is a multiplication
6074 or floor division, by a power of two, so we can treat it that
6075 way unless the multiplier or divisor overflows. Signed
6076 left-shift overflow is implementation-defined rather than
6077 undefined in C90, so do not convert signed left shift into
6078 multiplication. */
6079 if (TREE_CODE (op1) == INTEGER_CST
6080 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6081 /* const_binop may not detect overflow correctly,
6082 so check for it explicitly here. */
6083 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6084 && 0 != (t1 = fold_convert (ctype,
6085 const_binop (LSHIFT_EXPR,
6086 size_one_node,
6087 op1)))
6088 && !TREE_OVERFLOW (t1))
6089 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6090 ? MULT_EXPR : FLOOR_DIV_EXPR,
6091 ctype,
6092 fold_convert (ctype, op0),
6093 t1),
6094 c, code, wide_type, strict_overflow_p);
6095 break;
6096
6097 case PLUS_EXPR: case MINUS_EXPR:
6098 /* See if we can eliminate the operation on both sides. If we can, we
6099 can return a new PLUS or MINUS. If we can't, the only remaining
6100 cases where we can do anything are if the second operand is a
6101 constant. */
6102 sub_strict_overflow_p = false;
6103 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6104 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6105 if (t1 != 0 && t2 != 0
6106 && (code == MULT_EXPR
6107 /* If not multiplication, we can only do this if both operands
6108 are divisible by c. */
6109 || (multiple_of_p (ctype, op0, c)
6110 && multiple_of_p (ctype, op1, c))))
6111 {
6112 if (sub_strict_overflow_p)
6113 *strict_overflow_p = true;
6114 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6115 fold_convert (ctype, t2));
6116 }
6117
6118 /* If this was a subtraction, negate OP1 and set it to be an addition.
6119 This simplifies the logic below. */
6120 if (tcode == MINUS_EXPR)
6121 {
6122 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6123 /* If OP1 was not easily negatable, the constant may be OP0. */
6124 if (TREE_CODE (op0) == INTEGER_CST)
6125 {
6126 std::swap (op0, op1);
6127 std::swap (t1, t2);
6128 }
6129 }
6130
6131 if (TREE_CODE (op1) != INTEGER_CST)
6132 break;
6133
6134 /* If either OP1 or C are negative, this optimization is not safe for
6135 some of the division and remainder types while for others we need
6136 to change the code. */
6137 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6138 {
6139 if (code == CEIL_DIV_EXPR)
6140 code = FLOOR_DIV_EXPR;
6141 else if (code == FLOOR_DIV_EXPR)
6142 code = CEIL_DIV_EXPR;
6143 else if (code != MULT_EXPR
6144 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6145 break;
6146 }
6147
6148 /* If it's a multiply or a division/modulus operation of a multiple
6149 of our constant, do the operation and verify it doesn't overflow. */
6150 if (code == MULT_EXPR
6151 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6152 {
6153 op1 = const_binop (code, fold_convert (ctype, op1),
6154 fold_convert (ctype, c));
6155 /* We allow the constant to overflow with wrapping semantics. */
6156 if (op1 == 0
6157 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6158 break;
6159 }
6160 else
6161 break;
6162
6163 /* If we have an unsigned type, we cannot widen the operation since it
6164 will change the result if the original computation overflowed. */
6165 if (TYPE_UNSIGNED (ctype) && ctype != type)
6166 break;
6167
6168 /* If we were able to eliminate our operation from the first side,
6169 apply our operation to the second side and reform the PLUS. */
6170 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6171 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6172
6173 /* The last case is if we are a multiply. In that case, we can
6174 apply the distributive law to commute the multiply and addition
6175 if the multiplication of the constants doesn't overflow
6176 and overflow is defined. With undefined overflow
6177 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6178 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6179 return fold_build2 (tcode, ctype,
6180 fold_build2 (code, ctype,
6181 fold_convert (ctype, op0),
6182 fold_convert (ctype, c)),
6183 op1);
6184
6185 break;
6186
6187 case MULT_EXPR:
6188 /* We have a special case here if we are doing something like
6189 (C * 8) % 4 since we know that's zero. */
6190 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6191 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6192 /* If the multiplication can overflow we cannot optimize this. */
6193 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6194 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6195 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6196 {
6197 *strict_overflow_p = true;
6198 return omit_one_operand (type, integer_zero_node, op0);
6199 }
6200
6201 /* ... fall through ... */
6202
6203 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6204 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6205 /* If we can extract our operation from the LHS, do so and return a
6206 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6207 do something only if the second operand is a constant. */
6208 if (same_p
6209 && (t1 = extract_muldiv (op0, c, code, wide_type,
6210 strict_overflow_p)) != 0)
6211 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6212 fold_convert (ctype, op1));
6213 else if (tcode == MULT_EXPR && code == MULT_EXPR
6214 && (t1 = extract_muldiv (op1, c, code, wide_type,
6215 strict_overflow_p)) != 0)
6216 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6217 fold_convert (ctype, t1));
6218 else if (TREE_CODE (op1) != INTEGER_CST)
6219 return 0;
6220
6221 /* If these are the same operation types, we can associate them
6222 assuming no overflow. */
6223 if (tcode == code)
6224 {
6225 bool overflow_p = false;
6226 bool overflow_mul_p;
6227 signop sign = TYPE_SIGN (ctype);
6228 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6229 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6230 if (overflow_mul_p
6231 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6232 overflow_p = true;
6233 if (!overflow_p)
6234 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6235 wide_int_to_tree (ctype, mul));
6236 }
6237
6238 /* If these operations "cancel" each other, we have the main
6239 optimizations of this pass, which occur when either constant is a
6240 multiple of the other, in which case we replace this with either an
6241 operation or CODE or TCODE.
6242
6243 If we have an unsigned type, we cannot do this since it will change
6244 the result if the original computation overflowed. */
6245 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6246 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6247 || (tcode == MULT_EXPR
6248 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6249 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6250 && code != MULT_EXPR)))
6251 {
6252 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6253 {
6254 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6255 *strict_overflow_p = true;
6256 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6257 fold_convert (ctype,
6258 const_binop (TRUNC_DIV_EXPR,
6259 op1, c)));
6260 }
6261 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6262 {
6263 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6264 *strict_overflow_p = true;
6265 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6266 fold_convert (ctype,
6267 const_binop (TRUNC_DIV_EXPR,
6268 c, op1)));
6269 }
6270 }
6271 break;
6272
6273 default:
6274 break;
6275 }
6276
6277 return 0;
6278 }
6279 \f
6280 /* Return a node which has the indicated constant VALUE (either 0 or
6281 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6282 and is of the indicated TYPE. */
6283
6284 tree
6285 constant_boolean_node (bool value, tree type)
6286 {
6287 if (type == integer_type_node)
6288 return value ? integer_one_node : integer_zero_node;
6289 else if (type == boolean_type_node)
6290 return value ? boolean_true_node : boolean_false_node;
6291 else if (TREE_CODE (type) == VECTOR_TYPE)
6292 return build_vector_from_val (type,
6293 build_int_cst (TREE_TYPE (type),
6294 value ? -1 : 0));
6295 else
6296 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6297 }
6298
6299
6300 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6301 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6302 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6303 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6304 COND is the first argument to CODE; otherwise (as in the example
6305 given here), it is the second argument. TYPE is the type of the
6306 original expression. Return NULL_TREE if no simplification is
6307 possible. */
6308
6309 static tree
6310 fold_binary_op_with_conditional_arg (location_t loc,
6311 enum tree_code code,
6312 tree type, tree op0, tree op1,
6313 tree cond, tree arg, int cond_first_p)
6314 {
6315 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6316 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6317 tree test, true_value, false_value;
6318 tree lhs = NULL_TREE;
6319 tree rhs = NULL_TREE;
6320 enum tree_code cond_code = COND_EXPR;
6321
6322 if (TREE_CODE (cond) == COND_EXPR
6323 || TREE_CODE (cond) == VEC_COND_EXPR)
6324 {
6325 test = TREE_OPERAND (cond, 0);
6326 true_value = TREE_OPERAND (cond, 1);
6327 false_value = TREE_OPERAND (cond, 2);
6328 /* If this operand throws an expression, then it does not make
6329 sense to try to perform a logical or arithmetic operation
6330 involving it. */
6331 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6332 lhs = true_value;
6333 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6334 rhs = false_value;
6335 }
6336 else
6337 {
6338 tree testtype = TREE_TYPE (cond);
6339 test = cond;
6340 true_value = constant_boolean_node (true, testtype);
6341 false_value = constant_boolean_node (false, testtype);
6342 }
6343
6344 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6345 cond_code = VEC_COND_EXPR;
6346
6347 /* This transformation is only worthwhile if we don't have to wrap ARG
6348 in a SAVE_EXPR and the operation can be simplified without recursing
6349 on at least one of the branches once its pushed inside the COND_EXPR. */
6350 if (!TREE_CONSTANT (arg)
6351 && (TREE_SIDE_EFFECTS (arg)
6352 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6353 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6354 return NULL_TREE;
6355
6356 arg = fold_convert_loc (loc, arg_type, arg);
6357 if (lhs == 0)
6358 {
6359 true_value = fold_convert_loc (loc, cond_type, true_value);
6360 if (cond_first_p)
6361 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6362 else
6363 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6364 }
6365 if (rhs == 0)
6366 {
6367 false_value = fold_convert_loc (loc, cond_type, false_value);
6368 if (cond_first_p)
6369 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6370 else
6371 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6372 }
6373
6374 /* Check that we have simplified at least one of the branches. */
6375 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6376 return NULL_TREE;
6377
6378 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6379 }
6380
6381 \f
6382 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6383
6384 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6385 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6386 ADDEND is the same as X.
6387
6388 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6389 and finite. The problematic cases are when X is zero, and its mode
6390 has signed zeros. In the case of rounding towards -infinity,
6391 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6392 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6393
6394 bool
6395 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6396 {
6397 if (!real_zerop (addend))
6398 return false;
6399
6400 /* Don't allow the fold with -fsignaling-nans. */
6401 if (HONOR_SNANS (element_mode (type)))
6402 return false;
6403
6404 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6405 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6406 return true;
6407
6408 /* In a vector or complex, we would need to check the sign of all zeros. */
6409 if (TREE_CODE (addend) != REAL_CST)
6410 return false;
6411
6412 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6413 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6414 negate = !negate;
6415
6416 /* The mode has signed zeros, and we have to honor their sign.
6417 In this situation, there is only one case we can return true for.
6418 X - 0 is the same as X unless rounding towards -infinity is
6419 supported. */
6420 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6421 }
6422
6423 /* Subroutine of fold() that checks comparisons of built-in math
6424 functions against real constants.
6425
6426 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6427 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6428 is the type of the result and ARG0 and ARG1 are the operands of the
6429 comparison. ARG1 must be a TREE_REAL_CST.
6430
6431 The function returns the constant folded tree if a simplification
6432 can be made, and NULL_TREE otherwise. */
6433
6434 static tree
6435 fold_mathfn_compare (location_t loc,
6436 enum built_in_function fcode, enum tree_code code,
6437 tree type, tree arg0, tree arg1)
6438 {
6439 REAL_VALUE_TYPE c;
6440
6441 if (BUILTIN_SQRT_P (fcode))
6442 {
6443 tree arg = CALL_EXPR_ARG (arg0, 0);
6444 machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6445
6446 c = TREE_REAL_CST (arg1);
6447 if (REAL_VALUE_NEGATIVE (c))
6448 {
6449 /* sqrt(x) < y is always false, if y is negative. */
6450 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6451 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6452
6453 /* sqrt(x) > y is always true, if y is negative and we
6454 don't care about NaNs, i.e. negative values of x. */
6455 if (code == NE_EXPR || !HONOR_NANS (mode))
6456 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6457
6458 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6459 return fold_build2_loc (loc, GE_EXPR, type, arg,
6460 build_real (TREE_TYPE (arg), dconst0));
6461 }
6462 else if (code == GT_EXPR || code == GE_EXPR)
6463 {
6464 REAL_VALUE_TYPE c2;
6465
6466 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6467 real_convert (&c2, mode, &c2);
6468
6469 if (REAL_VALUE_ISINF (c2))
6470 {
6471 /* sqrt(x) > y is x == +Inf, when y is very large. */
6472 if (HONOR_INFINITIES (mode))
6473 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6474 build_real (TREE_TYPE (arg), c2));
6475
6476 /* sqrt(x) > y is always false, when y is very large
6477 and we don't care about infinities. */
6478 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6479 }
6480
6481 /* sqrt(x) > c is the same as x > c*c. */
6482 return fold_build2_loc (loc, code, type, arg,
6483 build_real (TREE_TYPE (arg), c2));
6484 }
6485 else if (code == LT_EXPR || code == LE_EXPR)
6486 {
6487 REAL_VALUE_TYPE c2;
6488
6489 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6490 real_convert (&c2, mode, &c2);
6491
6492 if (REAL_VALUE_ISINF (c2))
6493 {
6494 /* sqrt(x) < y is always true, when y is a very large
6495 value and we don't care about NaNs or Infinities. */
6496 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6497 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6498
6499 /* sqrt(x) < y is x != +Inf when y is very large and we
6500 don't care about NaNs. */
6501 if (! HONOR_NANS (mode))
6502 return fold_build2_loc (loc, NE_EXPR, type, arg,
6503 build_real (TREE_TYPE (arg), c2));
6504
6505 /* sqrt(x) < y is x >= 0 when y is very large and we
6506 don't care about Infinities. */
6507 if (! HONOR_INFINITIES (mode))
6508 return fold_build2_loc (loc, GE_EXPR, type, arg,
6509 build_real (TREE_TYPE (arg), dconst0));
6510
6511 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6512 arg = save_expr (arg);
6513 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6514 fold_build2_loc (loc, GE_EXPR, type, arg,
6515 build_real (TREE_TYPE (arg),
6516 dconst0)),
6517 fold_build2_loc (loc, NE_EXPR, type, arg,
6518 build_real (TREE_TYPE (arg),
6519 c2)));
6520 }
6521
6522 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6523 if (! HONOR_NANS (mode))
6524 return fold_build2_loc (loc, code, type, arg,
6525 build_real (TREE_TYPE (arg), c2));
6526
6527 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6528 arg = save_expr (arg);
6529 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6530 fold_build2_loc (loc, GE_EXPR, type, arg,
6531 build_real (TREE_TYPE (arg),
6532 dconst0)),
6533 fold_build2_loc (loc, code, type, arg,
6534 build_real (TREE_TYPE (arg),
6535 c2)));
6536 }
6537 }
6538
6539 return NULL_TREE;
6540 }
6541
6542 /* Subroutine of fold() that optimizes comparisons against Infinities,
6543 either +Inf or -Inf.
6544
6545 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6546 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6547 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6548
6549 The function returns the constant folded tree if a simplification
6550 can be made, and NULL_TREE otherwise. */
6551
6552 static tree
6553 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6554 tree arg0, tree arg1)
6555 {
6556 machine_mode mode;
6557 REAL_VALUE_TYPE max;
6558 tree temp;
6559 bool neg;
6560
6561 mode = TYPE_MODE (TREE_TYPE (arg0));
6562
6563 /* For negative infinity swap the sense of the comparison. */
6564 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6565 if (neg)
6566 code = swap_tree_comparison (code);
6567
6568 switch (code)
6569 {
6570 case GT_EXPR:
6571 /* x > +Inf is always false, if with ignore sNANs. */
6572 if (HONOR_SNANS (mode))
6573 return NULL_TREE;
6574 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6575
6576 case LE_EXPR:
6577 /* x <= +Inf is always true, if we don't case about NaNs. */
6578 if (! HONOR_NANS (mode))
6579 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6580
6581 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6582 arg0 = save_expr (arg0);
6583 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6584
6585 case EQ_EXPR:
6586 case GE_EXPR:
6587 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6588 real_maxval (&max, neg, mode);
6589 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6590 arg0, build_real (TREE_TYPE (arg0), max));
6591
6592 case LT_EXPR:
6593 /* x < +Inf is always equal to x <= DBL_MAX. */
6594 real_maxval (&max, neg, mode);
6595 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6596 arg0, build_real (TREE_TYPE (arg0), max));
6597
6598 case NE_EXPR:
6599 /* x != +Inf is always equal to !(x > DBL_MAX). */
6600 real_maxval (&max, neg, mode);
6601 if (! HONOR_NANS (mode))
6602 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6603 arg0, build_real (TREE_TYPE (arg0), max));
6604
6605 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6606 arg0, build_real (TREE_TYPE (arg0), max));
6607 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6608
6609 default:
6610 break;
6611 }
6612
6613 return NULL_TREE;
6614 }
6615
6616 /* Subroutine of fold() that optimizes comparisons of a division by
6617 a nonzero integer constant against an integer constant, i.e.
6618 X/C1 op C2.
6619
6620 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6621 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6622 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6623
6624 The function returns the constant folded tree if a simplification
6625 can be made, and NULL_TREE otherwise. */
6626
6627 static tree
6628 fold_div_compare (location_t loc,
6629 enum tree_code code, tree type, tree arg0, tree arg1)
6630 {
6631 tree prod, tmp, hi, lo;
6632 tree arg00 = TREE_OPERAND (arg0, 0);
6633 tree arg01 = TREE_OPERAND (arg0, 1);
6634 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6635 bool neg_overflow = false;
6636 bool overflow;
6637
6638 /* We have to do this the hard way to detect unsigned overflow.
6639 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6640 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6641 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6642 neg_overflow = false;
6643
6644 if (sign == UNSIGNED)
6645 {
6646 tmp = int_const_binop (MINUS_EXPR, arg01,
6647 build_int_cst (TREE_TYPE (arg01), 1));
6648 lo = prod;
6649
6650 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6651 val = wi::add (prod, tmp, sign, &overflow);
6652 hi = force_fit_type (TREE_TYPE (arg00), val,
6653 -1, overflow | TREE_OVERFLOW (prod));
6654 }
6655 else if (tree_int_cst_sgn (arg01) >= 0)
6656 {
6657 tmp = int_const_binop (MINUS_EXPR, arg01,
6658 build_int_cst (TREE_TYPE (arg01), 1));
6659 switch (tree_int_cst_sgn (arg1))
6660 {
6661 case -1:
6662 neg_overflow = true;
6663 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6664 hi = prod;
6665 break;
6666
6667 case 0:
6668 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6669 hi = tmp;
6670 break;
6671
6672 case 1:
6673 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6674 lo = prod;
6675 break;
6676
6677 default:
6678 gcc_unreachable ();
6679 }
6680 }
6681 else
6682 {
6683 /* A negative divisor reverses the relational operators. */
6684 code = swap_tree_comparison (code);
6685
6686 tmp = int_const_binop (PLUS_EXPR, arg01,
6687 build_int_cst (TREE_TYPE (arg01), 1));
6688 switch (tree_int_cst_sgn (arg1))
6689 {
6690 case -1:
6691 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6692 lo = prod;
6693 break;
6694
6695 case 0:
6696 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6697 lo = tmp;
6698 break;
6699
6700 case 1:
6701 neg_overflow = true;
6702 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6703 hi = prod;
6704 break;
6705
6706 default:
6707 gcc_unreachable ();
6708 }
6709 }
6710
6711 switch (code)
6712 {
6713 case EQ_EXPR:
6714 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6715 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6716 if (TREE_OVERFLOW (hi))
6717 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6718 if (TREE_OVERFLOW (lo))
6719 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6720 return build_range_check (loc, type, arg00, 1, lo, hi);
6721
6722 case NE_EXPR:
6723 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6724 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6725 if (TREE_OVERFLOW (hi))
6726 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6727 if (TREE_OVERFLOW (lo))
6728 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6729 return build_range_check (loc, type, arg00, 0, lo, hi);
6730
6731 case LT_EXPR:
6732 if (TREE_OVERFLOW (lo))
6733 {
6734 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6735 return omit_one_operand_loc (loc, type, tmp, arg00);
6736 }
6737 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6738
6739 case LE_EXPR:
6740 if (TREE_OVERFLOW (hi))
6741 {
6742 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6743 return omit_one_operand_loc (loc, type, tmp, arg00);
6744 }
6745 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6746
6747 case GT_EXPR:
6748 if (TREE_OVERFLOW (hi))
6749 {
6750 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6751 return omit_one_operand_loc (loc, type, tmp, arg00);
6752 }
6753 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6754
6755 case GE_EXPR:
6756 if (TREE_OVERFLOW (lo))
6757 {
6758 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6759 return omit_one_operand_loc (loc, type, tmp, arg00);
6760 }
6761 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6762
6763 default:
6764 break;
6765 }
6766
6767 return NULL_TREE;
6768 }
6769
6770
6771 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6772 equality/inequality test, then return a simplified form of the test
6773 using a sign testing. Otherwise return NULL. TYPE is the desired
6774 result type. */
6775
6776 static tree
6777 fold_single_bit_test_into_sign_test (location_t loc,
6778 enum tree_code code, tree arg0, tree arg1,
6779 tree result_type)
6780 {
6781 /* If this is testing a single bit, we can optimize the test. */
6782 if ((code == NE_EXPR || code == EQ_EXPR)
6783 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6784 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6785 {
6786 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6787 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6788 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6789
6790 if (arg00 != NULL_TREE
6791 /* This is only a win if casting to a signed type is cheap,
6792 i.e. when arg00's type is not a partial mode. */
6793 && TYPE_PRECISION (TREE_TYPE (arg00))
6794 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6795 {
6796 tree stype = signed_type_for (TREE_TYPE (arg00));
6797 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6798 result_type,
6799 fold_convert_loc (loc, stype, arg00),
6800 build_int_cst (stype, 0));
6801 }
6802 }
6803
6804 return NULL_TREE;
6805 }
6806
6807 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6808 equality/inequality test, then return a simplified form of
6809 the test using shifts and logical operations. Otherwise return
6810 NULL. TYPE is the desired result type. */
6811
6812 tree
6813 fold_single_bit_test (location_t loc, enum tree_code code,
6814 tree arg0, tree arg1, tree result_type)
6815 {
6816 /* If this is testing a single bit, we can optimize the test. */
6817 if ((code == NE_EXPR || code == EQ_EXPR)
6818 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6819 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6820 {
6821 tree inner = TREE_OPERAND (arg0, 0);
6822 tree type = TREE_TYPE (arg0);
6823 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6824 machine_mode operand_mode = TYPE_MODE (type);
6825 int ops_unsigned;
6826 tree signed_type, unsigned_type, intermediate_type;
6827 tree tem, one;
6828
6829 /* First, see if we can fold the single bit test into a sign-bit
6830 test. */
6831 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6832 result_type);
6833 if (tem)
6834 return tem;
6835
6836 /* Otherwise we have (A & C) != 0 where C is a single bit,
6837 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6838 Similarly for (A & C) == 0. */
6839
6840 /* If INNER is a right shift of a constant and it plus BITNUM does
6841 not overflow, adjust BITNUM and INNER. */
6842 if (TREE_CODE (inner) == RSHIFT_EXPR
6843 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6844 && bitnum < TYPE_PRECISION (type)
6845 && wi::ltu_p (TREE_OPERAND (inner, 1),
6846 TYPE_PRECISION (type) - bitnum))
6847 {
6848 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6849 inner = TREE_OPERAND (inner, 0);
6850 }
6851
6852 /* If we are going to be able to omit the AND below, we must do our
6853 operations as unsigned. If we must use the AND, we have a choice.
6854 Normally unsigned is faster, but for some machines signed is. */
6855 #ifdef LOAD_EXTEND_OP
6856 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6857 && !flag_syntax_only) ? 0 : 1;
6858 #else
6859 ops_unsigned = 1;
6860 #endif
6861
6862 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6863 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6864 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6865 inner = fold_convert_loc (loc, intermediate_type, inner);
6866
6867 if (bitnum != 0)
6868 inner = build2 (RSHIFT_EXPR, intermediate_type,
6869 inner, size_int (bitnum));
6870
6871 one = build_int_cst (intermediate_type, 1);
6872
6873 if (code == EQ_EXPR)
6874 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6875
6876 /* Put the AND last so it can combine with more things. */
6877 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6878
6879 /* Make sure to return the proper type. */
6880 inner = fold_convert_loc (loc, result_type, inner);
6881
6882 return inner;
6883 }
6884 return NULL_TREE;
6885 }
6886
6887 /* Check whether we are allowed to reorder operands arg0 and arg1,
6888 such that the evaluation of arg1 occurs before arg0. */
6889
6890 static bool
6891 reorder_operands_p (const_tree arg0, const_tree arg1)
6892 {
6893 if (! flag_evaluation_order)
6894 return true;
6895 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6896 return true;
6897 return ! TREE_SIDE_EFFECTS (arg0)
6898 && ! TREE_SIDE_EFFECTS (arg1);
6899 }
6900
6901 /* Test whether it is preferable two swap two operands, ARG0 and
6902 ARG1, for example because ARG0 is an integer constant and ARG1
6903 isn't. If REORDER is true, only recommend swapping if we can
6904 evaluate the operands in reverse order. */
6905
6906 bool
6907 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6908 {
6909 if (CONSTANT_CLASS_P (arg1))
6910 return 0;
6911 if (CONSTANT_CLASS_P (arg0))
6912 return 1;
6913
6914 STRIP_NOPS (arg0);
6915 STRIP_NOPS (arg1);
6916
6917 if (TREE_CONSTANT (arg1))
6918 return 0;
6919 if (TREE_CONSTANT (arg0))
6920 return 1;
6921
6922 if (reorder && flag_evaluation_order
6923 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6924 return 0;
6925
6926 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6927 for commutative and comparison operators. Ensuring a canonical
6928 form allows the optimizers to find additional redundancies without
6929 having to explicitly check for both orderings. */
6930 if (TREE_CODE (arg0) == SSA_NAME
6931 && TREE_CODE (arg1) == SSA_NAME
6932 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6933 return 1;
6934
6935 /* Put SSA_NAMEs last. */
6936 if (TREE_CODE (arg1) == SSA_NAME)
6937 return 0;
6938 if (TREE_CODE (arg0) == SSA_NAME)
6939 return 1;
6940
6941 /* Put variables last. */
6942 if (DECL_P (arg1))
6943 return 0;
6944 if (DECL_P (arg0))
6945 return 1;
6946
6947 return 0;
6948 }
6949
6950 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6951 ARG0 is extended to a wider type. */
6952
6953 static tree
6954 fold_widened_comparison (location_t loc, enum tree_code code,
6955 tree type, tree arg0, tree arg1)
6956 {
6957 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6958 tree arg1_unw;
6959 tree shorter_type, outer_type;
6960 tree min, max;
6961 bool above, below;
6962
6963 if (arg0_unw == arg0)
6964 return NULL_TREE;
6965 shorter_type = TREE_TYPE (arg0_unw);
6966
6967 #ifdef HAVE_canonicalize_funcptr_for_compare
6968 /* Disable this optimization if we're casting a function pointer
6969 type on targets that require function pointer canonicalization. */
6970 if (HAVE_canonicalize_funcptr_for_compare
6971 && TREE_CODE (shorter_type) == POINTER_TYPE
6972 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6973 return NULL_TREE;
6974 #endif
6975
6976 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6977 return NULL_TREE;
6978
6979 arg1_unw = get_unwidened (arg1, NULL_TREE);
6980
6981 /* If possible, express the comparison in the shorter mode. */
6982 if ((code == EQ_EXPR || code == NE_EXPR
6983 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6984 && (TREE_TYPE (arg1_unw) == shorter_type
6985 || ((TYPE_PRECISION (shorter_type)
6986 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6987 && (TYPE_UNSIGNED (shorter_type)
6988 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6989 || (TREE_CODE (arg1_unw) == INTEGER_CST
6990 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6991 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6992 && int_fits_type_p (arg1_unw, shorter_type))))
6993 return fold_build2_loc (loc, code, type, arg0_unw,
6994 fold_convert_loc (loc, shorter_type, arg1_unw));
6995
6996 if (TREE_CODE (arg1_unw) != INTEGER_CST
6997 || TREE_CODE (shorter_type) != INTEGER_TYPE
6998 || !int_fits_type_p (arg1_unw, shorter_type))
6999 return NULL_TREE;
7000
7001 /* If we are comparing with the integer that does not fit into the range
7002 of the shorter type, the result is known. */
7003 outer_type = TREE_TYPE (arg1_unw);
7004 min = lower_bound_in_type (outer_type, shorter_type);
7005 max = upper_bound_in_type (outer_type, shorter_type);
7006
7007 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7008 max, arg1_unw));
7009 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7010 arg1_unw, min));
7011
7012 switch (code)
7013 {
7014 case EQ_EXPR:
7015 if (above || below)
7016 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7017 break;
7018
7019 case NE_EXPR:
7020 if (above || below)
7021 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7022 break;
7023
7024 case LT_EXPR:
7025 case LE_EXPR:
7026 if (above)
7027 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7028 else if (below)
7029 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7030
7031 case GT_EXPR:
7032 case GE_EXPR:
7033 if (above)
7034 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7035 else if (below)
7036 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7037
7038 default:
7039 break;
7040 }
7041
7042 return NULL_TREE;
7043 }
7044
7045 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7046 ARG0 just the signedness is changed. */
7047
7048 static tree
7049 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7050 tree arg0, tree arg1)
7051 {
7052 tree arg0_inner;
7053 tree inner_type, outer_type;
7054
7055 if (!CONVERT_EXPR_P (arg0))
7056 return NULL_TREE;
7057
7058 outer_type = TREE_TYPE (arg0);
7059 arg0_inner = TREE_OPERAND (arg0, 0);
7060 inner_type = TREE_TYPE (arg0_inner);
7061
7062 #ifdef HAVE_canonicalize_funcptr_for_compare
7063 /* Disable this optimization if we're casting a function pointer
7064 type on targets that require function pointer canonicalization. */
7065 if (HAVE_canonicalize_funcptr_for_compare
7066 && TREE_CODE (inner_type) == POINTER_TYPE
7067 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7068 return NULL_TREE;
7069 #endif
7070
7071 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7072 return NULL_TREE;
7073
7074 if (TREE_CODE (arg1) != INTEGER_CST
7075 && !(CONVERT_EXPR_P (arg1)
7076 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7077 return NULL_TREE;
7078
7079 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7080 && code != NE_EXPR
7081 && code != EQ_EXPR)
7082 return NULL_TREE;
7083
7084 if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7085 return NULL_TREE;
7086
7087 if (TREE_CODE (arg1) == INTEGER_CST)
7088 arg1 = force_fit_type (inner_type, wi::to_widest (arg1), 0,
7089 TREE_OVERFLOW (arg1));
7090 else
7091 arg1 = fold_convert_loc (loc, inner_type, arg1);
7092
7093 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7094 }
7095
7096
7097 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7098 means A >= Y && A != MAX, but in this case we know that
7099 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7100
7101 static tree
7102 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7103 {
7104 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7105
7106 if (TREE_CODE (bound) == LT_EXPR)
7107 a = TREE_OPERAND (bound, 0);
7108 else if (TREE_CODE (bound) == GT_EXPR)
7109 a = TREE_OPERAND (bound, 1);
7110 else
7111 return NULL_TREE;
7112
7113 typea = TREE_TYPE (a);
7114 if (!INTEGRAL_TYPE_P (typea)
7115 && !POINTER_TYPE_P (typea))
7116 return NULL_TREE;
7117
7118 if (TREE_CODE (ineq) == LT_EXPR)
7119 {
7120 a1 = TREE_OPERAND (ineq, 1);
7121 y = TREE_OPERAND (ineq, 0);
7122 }
7123 else if (TREE_CODE (ineq) == GT_EXPR)
7124 {
7125 a1 = TREE_OPERAND (ineq, 0);
7126 y = TREE_OPERAND (ineq, 1);
7127 }
7128 else
7129 return NULL_TREE;
7130
7131 if (TREE_TYPE (a1) != typea)
7132 return NULL_TREE;
7133
7134 if (POINTER_TYPE_P (typea))
7135 {
7136 /* Convert the pointer types into integer before taking the difference. */
7137 tree ta = fold_convert_loc (loc, ssizetype, a);
7138 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7139 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7140 }
7141 else
7142 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7143
7144 if (!diff || !integer_onep (diff))
7145 return NULL_TREE;
7146
7147 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7148 }
7149
7150 /* Fold a sum or difference of at least one multiplication.
7151 Returns the folded tree or NULL if no simplification could be made. */
7152
7153 static tree
7154 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7155 tree arg0, tree arg1)
7156 {
7157 tree arg00, arg01, arg10, arg11;
7158 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7159
7160 /* (A * C) +- (B * C) -> (A+-B) * C.
7161 (A * C) +- A -> A * (C+-1).
7162 We are most concerned about the case where C is a constant,
7163 but other combinations show up during loop reduction. Since
7164 it is not difficult, try all four possibilities. */
7165
7166 if (TREE_CODE (arg0) == MULT_EXPR)
7167 {
7168 arg00 = TREE_OPERAND (arg0, 0);
7169 arg01 = TREE_OPERAND (arg0, 1);
7170 }
7171 else if (TREE_CODE (arg0) == INTEGER_CST)
7172 {
7173 arg00 = build_one_cst (type);
7174 arg01 = arg0;
7175 }
7176 else
7177 {
7178 /* We cannot generate constant 1 for fract. */
7179 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7180 return NULL_TREE;
7181 arg00 = arg0;
7182 arg01 = build_one_cst (type);
7183 }
7184 if (TREE_CODE (arg1) == MULT_EXPR)
7185 {
7186 arg10 = TREE_OPERAND (arg1, 0);
7187 arg11 = TREE_OPERAND (arg1, 1);
7188 }
7189 else if (TREE_CODE (arg1) == INTEGER_CST)
7190 {
7191 arg10 = build_one_cst (type);
7192 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7193 the purpose of this canonicalization. */
7194 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
7195 && negate_expr_p (arg1)
7196 && code == PLUS_EXPR)
7197 {
7198 arg11 = negate_expr (arg1);
7199 code = MINUS_EXPR;
7200 }
7201 else
7202 arg11 = arg1;
7203 }
7204 else
7205 {
7206 /* We cannot generate constant 1 for fract. */
7207 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7208 return NULL_TREE;
7209 arg10 = arg1;
7210 arg11 = build_one_cst (type);
7211 }
7212 same = NULL_TREE;
7213
7214 if (operand_equal_p (arg01, arg11, 0))
7215 same = arg01, alt0 = arg00, alt1 = arg10;
7216 else if (operand_equal_p (arg00, arg10, 0))
7217 same = arg00, alt0 = arg01, alt1 = arg11;
7218 else if (operand_equal_p (arg00, arg11, 0))
7219 same = arg00, alt0 = arg01, alt1 = arg10;
7220 else if (operand_equal_p (arg01, arg10, 0))
7221 same = arg01, alt0 = arg00, alt1 = arg11;
7222
7223 /* No identical multiplicands; see if we can find a common
7224 power-of-two factor in non-power-of-two multiplies. This
7225 can help in multi-dimensional array access. */
7226 else if (tree_fits_shwi_p (arg01)
7227 && tree_fits_shwi_p (arg11))
7228 {
7229 HOST_WIDE_INT int01, int11, tmp;
7230 bool swap = false;
7231 tree maybe_same;
7232 int01 = tree_to_shwi (arg01);
7233 int11 = tree_to_shwi (arg11);
7234
7235 /* Move min of absolute values to int11. */
7236 if (absu_hwi (int01) < absu_hwi (int11))
7237 {
7238 tmp = int01, int01 = int11, int11 = tmp;
7239 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7240 maybe_same = arg01;
7241 swap = true;
7242 }
7243 else
7244 maybe_same = arg11;
7245
7246 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7247 /* The remainder should not be a constant, otherwise we
7248 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7249 increased the number of multiplications necessary. */
7250 && TREE_CODE (arg10) != INTEGER_CST)
7251 {
7252 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7253 build_int_cst (TREE_TYPE (arg00),
7254 int01 / int11));
7255 alt1 = arg10;
7256 same = maybe_same;
7257 if (swap)
7258 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7259 }
7260 }
7261
7262 if (same)
7263 return fold_build2_loc (loc, MULT_EXPR, type,
7264 fold_build2_loc (loc, code, type,
7265 fold_convert_loc (loc, type, alt0),
7266 fold_convert_loc (loc, type, alt1)),
7267 fold_convert_loc (loc, type, same));
7268
7269 return NULL_TREE;
7270 }
7271
7272 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7273 specified by EXPR into the buffer PTR of length LEN bytes.
7274 Return the number of bytes placed in the buffer, or zero
7275 upon failure. */
7276
7277 static int
7278 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7279 {
7280 tree type = TREE_TYPE (expr);
7281 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7282 int byte, offset, word, words;
7283 unsigned char value;
7284
7285 if ((off == -1 && total_bytes > len)
7286 || off >= total_bytes)
7287 return 0;
7288 if (off == -1)
7289 off = 0;
7290 words = total_bytes / UNITS_PER_WORD;
7291
7292 for (byte = 0; byte < total_bytes; byte++)
7293 {
7294 int bitpos = byte * BITS_PER_UNIT;
7295 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7296 number of bytes. */
7297 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7298
7299 if (total_bytes > UNITS_PER_WORD)
7300 {
7301 word = byte / UNITS_PER_WORD;
7302 if (WORDS_BIG_ENDIAN)
7303 word = (words - 1) - word;
7304 offset = word * UNITS_PER_WORD;
7305 if (BYTES_BIG_ENDIAN)
7306 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7307 else
7308 offset += byte % UNITS_PER_WORD;
7309 }
7310 else
7311 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7312 if (offset >= off
7313 && offset - off < len)
7314 ptr[offset - off] = value;
7315 }
7316 return MIN (len, total_bytes - off);
7317 }
7318
7319
7320 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7321 specified by EXPR into the buffer PTR of length LEN bytes.
7322 Return the number of bytes placed in the buffer, or zero
7323 upon failure. */
7324
7325 static int
7326 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7327 {
7328 tree type = TREE_TYPE (expr);
7329 machine_mode mode = TYPE_MODE (type);
7330 int total_bytes = GET_MODE_SIZE (mode);
7331 FIXED_VALUE_TYPE value;
7332 tree i_value, i_type;
7333
7334 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7335 return 0;
7336
7337 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7338
7339 if (NULL_TREE == i_type
7340 || TYPE_PRECISION (i_type) != total_bytes)
7341 return 0;
7342
7343 value = TREE_FIXED_CST (expr);
7344 i_value = double_int_to_tree (i_type, value.data);
7345
7346 return native_encode_int (i_value, ptr, len, off);
7347 }
7348
7349
7350 /* Subroutine of native_encode_expr. Encode the REAL_CST
7351 specified by EXPR into the buffer PTR of length LEN bytes.
7352 Return the number of bytes placed in the buffer, or zero
7353 upon failure. */
7354
7355 static int
7356 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7357 {
7358 tree type = TREE_TYPE (expr);
7359 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7360 int byte, offset, word, words, bitpos;
7361 unsigned char value;
7362
7363 /* There are always 32 bits in each long, no matter the size of
7364 the hosts long. We handle floating point representations with
7365 up to 192 bits. */
7366 long tmp[6];
7367
7368 if ((off == -1 && total_bytes > len)
7369 || off >= total_bytes)
7370 return 0;
7371 if (off == -1)
7372 off = 0;
7373 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7374
7375 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7376
7377 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7378 bitpos += BITS_PER_UNIT)
7379 {
7380 byte = (bitpos / BITS_PER_UNIT) & 3;
7381 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7382
7383 if (UNITS_PER_WORD < 4)
7384 {
7385 word = byte / UNITS_PER_WORD;
7386 if (WORDS_BIG_ENDIAN)
7387 word = (words - 1) - word;
7388 offset = word * UNITS_PER_WORD;
7389 if (BYTES_BIG_ENDIAN)
7390 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7391 else
7392 offset += byte % UNITS_PER_WORD;
7393 }
7394 else
7395 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7396 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7397 if (offset >= off
7398 && offset - off < len)
7399 ptr[offset - off] = value;
7400 }
7401 return MIN (len, total_bytes - off);
7402 }
7403
7404 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7405 specified by EXPR into the buffer PTR of length LEN bytes.
7406 Return the number of bytes placed in the buffer, or zero
7407 upon failure. */
7408
7409 static int
7410 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7411 {
7412 int rsize, isize;
7413 tree part;
7414
7415 part = TREE_REALPART (expr);
7416 rsize = native_encode_expr (part, ptr, len, off);
7417 if (off == -1
7418 && rsize == 0)
7419 return 0;
7420 part = TREE_IMAGPART (expr);
7421 if (off != -1)
7422 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7423 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7424 if (off == -1
7425 && isize != rsize)
7426 return 0;
7427 return rsize + isize;
7428 }
7429
7430
7431 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7432 specified by EXPR into the buffer PTR of length LEN bytes.
7433 Return the number of bytes placed in the buffer, or zero
7434 upon failure. */
7435
7436 static int
7437 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7438 {
7439 unsigned i, count;
7440 int size, offset;
7441 tree itype, elem;
7442
7443 offset = 0;
7444 count = VECTOR_CST_NELTS (expr);
7445 itype = TREE_TYPE (TREE_TYPE (expr));
7446 size = GET_MODE_SIZE (TYPE_MODE (itype));
7447 for (i = 0; i < count; i++)
7448 {
7449 if (off >= size)
7450 {
7451 off -= size;
7452 continue;
7453 }
7454 elem = VECTOR_CST_ELT (expr, i);
7455 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7456 if ((off == -1 && res != size)
7457 || res == 0)
7458 return 0;
7459 offset += res;
7460 if (offset >= len)
7461 return offset;
7462 if (off != -1)
7463 off = 0;
7464 }
7465 return offset;
7466 }
7467
7468
7469 /* Subroutine of native_encode_expr. Encode the STRING_CST
7470 specified by EXPR into the buffer PTR of length LEN bytes.
7471 Return the number of bytes placed in the buffer, or zero
7472 upon failure. */
7473
7474 static int
7475 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7476 {
7477 tree type = TREE_TYPE (expr);
7478 HOST_WIDE_INT total_bytes;
7479
7480 if (TREE_CODE (type) != ARRAY_TYPE
7481 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7482 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7483 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7484 return 0;
7485 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7486 if ((off == -1 && total_bytes > len)
7487 || off >= total_bytes)
7488 return 0;
7489 if (off == -1)
7490 off = 0;
7491 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7492 {
7493 int written = 0;
7494 if (off < TREE_STRING_LENGTH (expr))
7495 {
7496 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7497 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7498 }
7499 memset (ptr + written, 0,
7500 MIN (total_bytes - written, len - written));
7501 }
7502 else
7503 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7504 return MIN (total_bytes - off, len);
7505 }
7506
7507
7508 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7509 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7510 buffer PTR of length LEN bytes. If OFF is not -1 then start
7511 the encoding at byte offset OFF and encode at most LEN bytes.
7512 Return the number of bytes placed in the buffer, or zero upon failure. */
7513
7514 int
7515 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7516 {
7517 switch (TREE_CODE (expr))
7518 {
7519 case INTEGER_CST:
7520 return native_encode_int (expr, ptr, len, off);
7521
7522 case REAL_CST:
7523 return native_encode_real (expr, ptr, len, off);
7524
7525 case FIXED_CST:
7526 return native_encode_fixed (expr, ptr, len, off);
7527
7528 case COMPLEX_CST:
7529 return native_encode_complex (expr, ptr, len, off);
7530
7531 case VECTOR_CST:
7532 return native_encode_vector (expr, ptr, len, off);
7533
7534 case STRING_CST:
7535 return native_encode_string (expr, ptr, len, off);
7536
7537 default:
7538 return 0;
7539 }
7540 }
7541
7542
7543 /* Subroutine of native_interpret_expr. Interpret the contents of
7544 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7545 If the buffer cannot be interpreted, return NULL_TREE. */
7546
7547 static tree
7548 native_interpret_int (tree type, const unsigned char *ptr, int len)
7549 {
7550 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7551
7552 if (total_bytes > len
7553 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7554 return NULL_TREE;
7555
7556 wide_int result = wi::from_buffer (ptr, total_bytes);
7557
7558 return wide_int_to_tree (type, result);
7559 }
7560
7561
7562 /* Subroutine of native_interpret_expr. Interpret the contents of
7563 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7564 If the buffer cannot be interpreted, return NULL_TREE. */
7565
7566 static tree
7567 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7568 {
7569 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7570 double_int result;
7571 FIXED_VALUE_TYPE fixed_value;
7572
7573 if (total_bytes > len
7574 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7575 return NULL_TREE;
7576
7577 result = double_int::from_buffer (ptr, total_bytes);
7578 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7579
7580 return build_fixed (type, fixed_value);
7581 }
7582
7583
7584 /* Subroutine of native_interpret_expr. Interpret the contents of
7585 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7586 If the buffer cannot be interpreted, return NULL_TREE. */
7587
7588 static tree
7589 native_interpret_real (tree type, const unsigned char *ptr, int len)
7590 {
7591 machine_mode mode = TYPE_MODE (type);
7592 int total_bytes = GET_MODE_SIZE (mode);
7593 int byte, offset, word, words, bitpos;
7594 unsigned char value;
7595 /* There are always 32 bits in each long, no matter the size of
7596 the hosts long. We handle floating point representations with
7597 up to 192 bits. */
7598 REAL_VALUE_TYPE r;
7599 long tmp[6];
7600
7601 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7602 if (total_bytes > len || total_bytes > 24)
7603 return NULL_TREE;
7604 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7605
7606 memset (tmp, 0, sizeof (tmp));
7607 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7608 bitpos += BITS_PER_UNIT)
7609 {
7610 byte = (bitpos / BITS_PER_UNIT) & 3;
7611 if (UNITS_PER_WORD < 4)
7612 {
7613 word = byte / UNITS_PER_WORD;
7614 if (WORDS_BIG_ENDIAN)
7615 word = (words - 1) - word;
7616 offset = word * UNITS_PER_WORD;
7617 if (BYTES_BIG_ENDIAN)
7618 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7619 else
7620 offset += byte % UNITS_PER_WORD;
7621 }
7622 else
7623 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7624 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7625
7626 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7627 }
7628
7629 real_from_target (&r, tmp, mode);
7630 return build_real (type, r);
7631 }
7632
7633
7634 /* Subroutine of native_interpret_expr. Interpret the contents of
7635 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7636 If the buffer cannot be interpreted, return NULL_TREE. */
7637
7638 static tree
7639 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7640 {
7641 tree etype, rpart, ipart;
7642 int size;
7643
7644 etype = TREE_TYPE (type);
7645 size = GET_MODE_SIZE (TYPE_MODE (etype));
7646 if (size * 2 > len)
7647 return NULL_TREE;
7648 rpart = native_interpret_expr (etype, ptr, size);
7649 if (!rpart)
7650 return NULL_TREE;
7651 ipart = native_interpret_expr (etype, ptr+size, size);
7652 if (!ipart)
7653 return NULL_TREE;
7654 return build_complex (type, rpart, ipart);
7655 }
7656
7657
7658 /* Subroutine of native_interpret_expr. Interpret the contents of
7659 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7660 If the buffer cannot be interpreted, return NULL_TREE. */
7661
7662 static tree
7663 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7664 {
7665 tree etype, elem;
7666 int i, size, count;
7667 tree *elements;
7668
7669 etype = TREE_TYPE (type);
7670 size = GET_MODE_SIZE (TYPE_MODE (etype));
7671 count = TYPE_VECTOR_SUBPARTS (type);
7672 if (size * count > len)
7673 return NULL_TREE;
7674
7675 elements = XALLOCAVEC (tree, count);
7676 for (i = count - 1; i >= 0; i--)
7677 {
7678 elem = native_interpret_expr (etype, ptr+(i*size), size);
7679 if (!elem)
7680 return NULL_TREE;
7681 elements[i] = elem;
7682 }
7683 return build_vector (type, elements);
7684 }
7685
7686
7687 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7688 the buffer PTR of length LEN as a constant of type TYPE. For
7689 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7690 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7691 return NULL_TREE. */
7692
7693 tree
7694 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7695 {
7696 switch (TREE_CODE (type))
7697 {
7698 case INTEGER_TYPE:
7699 case ENUMERAL_TYPE:
7700 case BOOLEAN_TYPE:
7701 case POINTER_TYPE:
7702 case REFERENCE_TYPE:
7703 return native_interpret_int (type, ptr, len);
7704
7705 case REAL_TYPE:
7706 return native_interpret_real (type, ptr, len);
7707
7708 case FIXED_POINT_TYPE:
7709 return native_interpret_fixed (type, ptr, len);
7710
7711 case COMPLEX_TYPE:
7712 return native_interpret_complex (type, ptr, len);
7713
7714 case VECTOR_TYPE:
7715 return native_interpret_vector (type, ptr, len);
7716
7717 default:
7718 return NULL_TREE;
7719 }
7720 }
7721
7722 /* Returns true if we can interpret the contents of a native encoding
7723 as TYPE. */
7724
7725 static bool
7726 can_native_interpret_type_p (tree type)
7727 {
7728 switch (TREE_CODE (type))
7729 {
7730 case INTEGER_TYPE:
7731 case ENUMERAL_TYPE:
7732 case BOOLEAN_TYPE:
7733 case POINTER_TYPE:
7734 case REFERENCE_TYPE:
7735 case FIXED_POINT_TYPE:
7736 case REAL_TYPE:
7737 case COMPLEX_TYPE:
7738 case VECTOR_TYPE:
7739 return true;
7740 default:
7741 return false;
7742 }
7743 }
7744
7745 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7746 TYPE at compile-time. If we're unable to perform the conversion
7747 return NULL_TREE. */
7748
7749 static tree
7750 fold_view_convert_expr (tree type, tree expr)
7751 {
7752 /* We support up to 512-bit values (for V8DFmode). */
7753 unsigned char buffer[64];
7754 int len;
7755
7756 /* Check that the host and target are sane. */
7757 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7758 return NULL_TREE;
7759
7760 len = native_encode_expr (expr, buffer, sizeof (buffer));
7761 if (len == 0)
7762 return NULL_TREE;
7763
7764 return native_interpret_expr (type, buffer, len);
7765 }
7766
7767 /* Build an expression for the address of T. Folds away INDIRECT_REF
7768 to avoid confusing the gimplify process. */
7769
7770 tree
7771 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7772 {
7773 /* The size of the object is not relevant when talking about its address. */
7774 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7775 t = TREE_OPERAND (t, 0);
7776
7777 if (TREE_CODE (t) == INDIRECT_REF)
7778 {
7779 t = TREE_OPERAND (t, 0);
7780
7781 if (TREE_TYPE (t) != ptrtype)
7782 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7783 }
7784 else if (TREE_CODE (t) == MEM_REF
7785 && integer_zerop (TREE_OPERAND (t, 1)))
7786 return TREE_OPERAND (t, 0);
7787 else if (TREE_CODE (t) == MEM_REF
7788 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7789 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7790 TREE_OPERAND (t, 0),
7791 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7792 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7793 {
7794 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7795
7796 if (TREE_TYPE (t) != ptrtype)
7797 t = fold_convert_loc (loc, ptrtype, t);
7798 }
7799 else
7800 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7801
7802 return t;
7803 }
7804
7805 /* Build an expression for the address of T. */
7806
7807 tree
7808 build_fold_addr_expr_loc (location_t loc, tree t)
7809 {
7810 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7811
7812 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7813 }
7814
7815 /* Fold a unary expression of code CODE and type TYPE with operand
7816 OP0. Return the folded expression if folding is successful.
7817 Otherwise, return NULL_TREE. */
7818
7819 tree
7820 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7821 {
7822 tree tem;
7823 tree arg0;
7824 enum tree_code_class kind = TREE_CODE_CLASS (code);
7825
7826 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7827 && TREE_CODE_LENGTH (code) == 1);
7828
7829 arg0 = op0;
7830 if (arg0)
7831 {
7832 if (CONVERT_EXPR_CODE_P (code)
7833 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7834 {
7835 /* Don't use STRIP_NOPS, because signedness of argument type
7836 matters. */
7837 STRIP_SIGN_NOPS (arg0);
7838 }
7839 else
7840 {
7841 /* Strip any conversions that don't change the mode. This
7842 is safe for every expression, except for a comparison
7843 expression because its signedness is derived from its
7844 operands.
7845
7846 Note that this is done as an internal manipulation within
7847 the constant folder, in order to find the simplest
7848 representation of the arguments so that their form can be
7849 studied. In any cases, the appropriate type conversions
7850 should be put back in the tree that will get out of the
7851 constant folder. */
7852 STRIP_NOPS (arg0);
7853 }
7854
7855 if (CONSTANT_CLASS_P (arg0))
7856 {
7857 tree tem = const_unop (code, type, arg0);
7858 if (tem)
7859 {
7860 if (TREE_TYPE (tem) != type)
7861 tem = fold_convert_loc (loc, type, tem);
7862 return tem;
7863 }
7864 }
7865 }
7866
7867 tem = generic_simplify (loc, code, type, op0);
7868 if (tem)
7869 return tem;
7870
7871 if (TREE_CODE_CLASS (code) == tcc_unary)
7872 {
7873 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7874 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7875 fold_build1_loc (loc, code, type,
7876 fold_convert_loc (loc, TREE_TYPE (op0),
7877 TREE_OPERAND (arg0, 1))));
7878 else if (TREE_CODE (arg0) == COND_EXPR)
7879 {
7880 tree arg01 = TREE_OPERAND (arg0, 1);
7881 tree arg02 = TREE_OPERAND (arg0, 2);
7882 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7883 arg01 = fold_build1_loc (loc, code, type,
7884 fold_convert_loc (loc,
7885 TREE_TYPE (op0), arg01));
7886 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7887 arg02 = fold_build1_loc (loc, code, type,
7888 fold_convert_loc (loc,
7889 TREE_TYPE (op0), arg02));
7890 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7891 arg01, arg02);
7892
7893 /* If this was a conversion, and all we did was to move into
7894 inside the COND_EXPR, bring it back out. But leave it if
7895 it is a conversion from integer to integer and the
7896 result precision is no wider than a word since such a
7897 conversion is cheap and may be optimized away by combine,
7898 while it couldn't if it were outside the COND_EXPR. Then return
7899 so we don't get into an infinite recursion loop taking the
7900 conversion out and then back in. */
7901
7902 if ((CONVERT_EXPR_CODE_P (code)
7903 || code == NON_LVALUE_EXPR)
7904 && TREE_CODE (tem) == COND_EXPR
7905 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7906 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7907 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7908 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7909 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7910 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7911 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7912 && (INTEGRAL_TYPE_P
7913 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7914 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7915 || flag_syntax_only))
7916 tem = build1_loc (loc, code, type,
7917 build3 (COND_EXPR,
7918 TREE_TYPE (TREE_OPERAND
7919 (TREE_OPERAND (tem, 1), 0)),
7920 TREE_OPERAND (tem, 0),
7921 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7922 TREE_OPERAND (TREE_OPERAND (tem, 2),
7923 0)));
7924 return tem;
7925 }
7926 }
7927
7928 switch (code)
7929 {
7930 case NON_LVALUE_EXPR:
7931 if (!maybe_lvalue_p (op0))
7932 return fold_convert_loc (loc, type, op0);
7933 return NULL_TREE;
7934
7935 CASE_CONVERT:
7936 case FLOAT_EXPR:
7937 case FIX_TRUNC_EXPR:
7938 if (COMPARISON_CLASS_P (op0))
7939 {
7940 /* If we have (type) (a CMP b) and type is an integral type, return
7941 new expression involving the new type. Canonicalize
7942 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7943 non-integral type.
7944 Do not fold the result as that would not simplify further, also
7945 folding again results in recursions. */
7946 if (TREE_CODE (type) == BOOLEAN_TYPE)
7947 return build2_loc (loc, TREE_CODE (op0), type,
7948 TREE_OPERAND (op0, 0),
7949 TREE_OPERAND (op0, 1));
7950 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7951 && TREE_CODE (type) != VECTOR_TYPE)
7952 return build3_loc (loc, COND_EXPR, type, op0,
7953 constant_boolean_node (true, type),
7954 constant_boolean_node (false, type));
7955 }
7956
7957 /* Handle (T *)&A.B.C for A being of type T and B and C
7958 living at offset zero. This occurs frequently in
7959 C++ upcasting and then accessing the base. */
7960 if (TREE_CODE (op0) == ADDR_EXPR
7961 && POINTER_TYPE_P (type)
7962 && handled_component_p (TREE_OPERAND (op0, 0)))
7963 {
7964 HOST_WIDE_INT bitsize, bitpos;
7965 tree offset;
7966 machine_mode mode;
7967 int unsignedp, volatilep;
7968 tree base = TREE_OPERAND (op0, 0);
7969 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7970 &mode, &unsignedp, &volatilep, false);
7971 /* If the reference was to a (constant) zero offset, we can use
7972 the address of the base if it has the same base type
7973 as the result type and the pointer type is unqualified. */
7974 if (! offset && bitpos == 0
7975 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7976 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7977 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7978 return fold_convert_loc (loc, type,
7979 build_fold_addr_expr_loc (loc, base));
7980 }
7981
7982 if (TREE_CODE (op0) == MODIFY_EXPR
7983 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7984 /* Detect assigning a bitfield. */
7985 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7986 && DECL_BIT_FIELD
7987 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7988 {
7989 /* Don't leave an assignment inside a conversion
7990 unless assigning a bitfield. */
7991 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7992 /* First do the assignment, then return converted constant. */
7993 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7994 TREE_NO_WARNING (tem) = 1;
7995 TREE_USED (tem) = 1;
7996 return tem;
7997 }
7998
7999 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8000 constants (if x has signed type, the sign bit cannot be set
8001 in c). This folds extension into the BIT_AND_EXPR.
8002 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8003 very likely don't have maximal range for their precision and this
8004 transformation effectively doesn't preserve non-maximal ranges. */
8005 if (TREE_CODE (type) == INTEGER_TYPE
8006 && TREE_CODE (op0) == BIT_AND_EXPR
8007 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8008 {
8009 tree and_expr = op0;
8010 tree and0 = TREE_OPERAND (and_expr, 0);
8011 tree and1 = TREE_OPERAND (and_expr, 1);
8012 int change = 0;
8013
8014 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8015 || (TYPE_PRECISION (type)
8016 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8017 change = 1;
8018 else if (TYPE_PRECISION (TREE_TYPE (and1))
8019 <= HOST_BITS_PER_WIDE_INT
8020 && tree_fits_uhwi_p (and1))
8021 {
8022 unsigned HOST_WIDE_INT cst;
8023
8024 cst = tree_to_uhwi (and1);
8025 cst &= HOST_WIDE_INT_M1U
8026 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8027 change = (cst == 0);
8028 #ifdef LOAD_EXTEND_OP
8029 if (change
8030 && !flag_syntax_only
8031 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8032 == ZERO_EXTEND))
8033 {
8034 tree uns = unsigned_type_for (TREE_TYPE (and0));
8035 and0 = fold_convert_loc (loc, uns, and0);
8036 and1 = fold_convert_loc (loc, uns, and1);
8037 }
8038 #endif
8039 }
8040 if (change)
8041 {
8042 tem = force_fit_type (type, wi::to_widest (and1), 0,
8043 TREE_OVERFLOW (and1));
8044 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8045 fold_convert_loc (loc, type, and0), tem);
8046 }
8047 }
8048
8049 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8050 when one of the new casts will fold away. Conservatively we assume
8051 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8052 if (POINTER_TYPE_P (type)
8053 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8054 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
8055 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8056 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8057 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8058 {
8059 tree arg00 = TREE_OPERAND (arg0, 0);
8060 tree arg01 = TREE_OPERAND (arg0, 1);
8061
8062 return fold_build_pointer_plus_loc
8063 (loc, fold_convert_loc (loc, type, arg00), arg01);
8064 }
8065
8066 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8067 of the same precision, and X is an integer type not narrower than
8068 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8069 if (INTEGRAL_TYPE_P (type)
8070 && TREE_CODE (op0) == BIT_NOT_EXPR
8071 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8072 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8073 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8074 {
8075 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8076 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8077 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8078 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8079 fold_convert_loc (loc, type, tem));
8080 }
8081
8082 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8083 type of X and Y (integer types only). */
8084 if (INTEGRAL_TYPE_P (type)
8085 && TREE_CODE (op0) == MULT_EXPR
8086 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8087 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8088 {
8089 /* Be careful not to introduce new overflows. */
8090 tree mult_type;
8091 if (TYPE_OVERFLOW_WRAPS (type))
8092 mult_type = type;
8093 else
8094 mult_type = unsigned_type_for (type);
8095
8096 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8097 {
8098 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8099 fold_convert_loc (loc, mult_type,
8100 TREE_OPERAND (op0, 0)),
8101 fold_convert_loc (loc, mult_type,
8102 TREE_OPERAND (op0, 1)));
8103 return fold_convert_loc (loc, type, tem);
8104 }
8105 }
8106
8107 return NULL_TREE;
8108
8109 case VIEW_CONVERT_EXPR:
8110 if (TREE_CODE (op0) == MEM_REF)
8111 return fold_build2_loc (loc, MEM_REF, type,
8112 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8113
8114 return NULL_TREE;
8115
8116 case NEGATE_EXPR:
8117 tem = fold_negate_expr (loc, arg0);
8118 if (tem)
8119 return fold_convert_loc (loc, type, tem);
8120 return NULL_TREE;
8121
8122 case ABS_EXPR:
8123 /* Convert fabs((double)float) into (double)fabsf(float). */
8124 if (TREE_CODE (arg0) == NOP_EXPR
8125 && TREE_CODE (type) == REAL_TYPE)
8126 {
8127 tree targ0 = strip_float_extensions (arg0);
8128 if (targ0 != arg0)
8129 return fold_convert_loc (loc, type,
8130 fold_build1_loc (loc, ABS_EXPR,
8131 TREE_TYPE (targ0),
8132 targ0));
8133 }
8134 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8135 else if (TREE_CODE (arg0) == ABS_EXPR)
8136 return arg0;
8137
8138 /* Strip sign ops from argument. */
8139 if (TREE_CODE (type) == REAL_TYPE)
8140 {
8141 tem = fold_strip_sign_ops (arg0);
8142 if (tem)
8143 return fold_build1_loc (loc, ABS_EXPR, type,
8144 fold_convert_loc (loc, type, tem));
8145 }
8146 return NULL_TREE;
8147
8148 case CONJ_EXPR:
8149 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8150 return fold_convert_loc (loc, type, arg0);
8151 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8152 {
8153 tree itype = TREE_TYPE (type);
8154 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8155 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8156 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8157 negate_expr (ipart));
8158 }
8159 if (TREE_CODE (arg0) == CONJ_EXPR)
8160 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8161 return NULL_TREE;
8162
8163 case BIT_NOT_EXPR:
8164 /* Convert ~ (-A) to A - 1. */
8165 if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8166 return fold_build2_loc (loc, MINUS_EXPR, type,
8167 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8168 build_int_cst (type, 1));
8169 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8170 else if (INTEGRAL_TYPE_P (type)
8171 && ((TREE_CODE (arg0) == MINUS_EXPR
8172 && integer_onep (TREE_OPERAND (arg0, 1)))
8173 || (TREE_CODE (arg0) == PLUS_EXPR
8174 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8175 {
8176 /* Perform the negation in ARG0's type and only then convert
8177 to TYPE as to avoid introducing undefined behavior. */
8178 tree t = fold_build1_loc (loc, NEGATE_EXPR,
8179 TREE_TYPE (TREE_OPERAND (arg0, 0)),
8180 TREE_OPERAND (arg0, 0));
8181 return fold_convert_loc (loc, type, t);
8182 }
8183 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8184 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8185 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8186 fold_convert_loc (loc, type,
8187 TREE_OPERAND (arg0, 0)))))
8188 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8189 fold_convert_loc (loc, type,
8190 TREE_OPERAND (arg0, 1)));
8191 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8192 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8193 fold_convert_loc (loc, type,
8194 TREE_OPERAND (arg0, 1)))))
8195 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8196 fold_convert_loc (loc, type,
8197 TREE_OPERAND (arg0, 0)), tem);
8198
8199 return NULL_TREE;
8200
8201 case TRUTH_NOT_EXPR:
8202 /* Note that the operand of this must be an int
8203 and its values must be 0 or 1.
8204 ("true" is a fixed value perhaps depending on the language,
8205 but we don't handle values other than 1 correctly yet.) */
8206 tem = fold_truth_not_expr (loc, arg0);
8207 if (!tem)
8208 return NULL_TREE;
8209 return fold_convert_loc (loc, type, tem);
8210
8211 case REALPART_EXPR:
8212 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8213 return fold_convert_loc (loc, type, arg0);
8214 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8215 {
8216 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8217 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8218 fold_build1_loc (loc, REALPART_EXPR, itype,
8219 TREE_OPERAND (arg0, 0)),
8220 fold_build1_loc (loc, REALPART_EXPR, itype,
8221 TREE_OPERAND (arg0, 1)));
8222 return fold_convert_loc (loc, type, tem);
8223 }
8224 if (TREE_CODE (arg0) == CONJ_EXPR)
8225 {
8226 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8227 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8228 TREE_OPERAND (arg0, 0));
8229 return fold_convert_loc (loc, type, tem);
8230 }
8231 if (TREE_CODE (arg0) == CALL_EXPR)
8232 {
8233 tree fn = get_callee_fndecl (arg0);
8234 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8235 switch (DECL_FUNCTION_CODE (fn))
8236 {
8237 CASE_FLT_FN (BUILT_IN_CEXPI):
8238 fn = mathfn_built_in (type, BUILT_IN_COS);
8239 if (fn)
8240 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8241 break;
8242
8243 default:
8244 break;
8245 }
8246 }
8247 return NULL_TREE;
8248
8249 case IMAGPART_EXPR:
8250 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8251 return build_zero_cst (type);
8252 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8253 {
8254 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8255 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8256 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8257 TREE_OPERAND (arg0, 0)),
8258 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8259 TREE_OPERAND (arg0, 1)));
8260 return fold_convert_loc (loc, type, tem);
8261 }
8262 if (TREE_CODE (arg0) == CONJ_EXPR)
8263 {
8264 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8265 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8266 return fold_convert_loc (loc, type, negate_expr (tem));
8267 }
8268 if (TREE_CODE (arg0) == CALL_EXPR)
8269 {
8270 tree fn = get_callee_fndecl (arg0);
8271 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8272 switch (DECL_FUNCTION_CODE (fn))
8273 {
8274 CASE_FLT_FN (BUILT_IN_CEXPI):
8275 fn = mathfn_built_in (type, BUILT_IN_SIN);
8276 if (fn)
8277 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8278 break;
8279
8280 default:
8281 break;
8282 }
8283 }
8284 return NULL_TREE;
8285
8286 case INDIRECT_REF:
8287 /* Fold *&X to X if X is an lvalue. */
8288 if (TREE_CODE (op0) == ADDR_EXPR)
8289 {
8290 tree op00 = TREE_OPERAND (op0, 0);
8291 if ((TREE_CODE (op00) == VAR_DECL
8292 || TREE_CODE (op00) == PARM_DECL
8293 || TREE_CODE (op00) == RESULT_DECL)
8294 && !TREE_READONLY (op00))
8295 return op00;
8296 }
8297 return NULL_TREE;
8298
8299 default:
8300 return NULL_TREE;
8301 } /* switch (code) */
8302 }
8303
8304
8305 /* If the operation was a conversion do _not_ mark a resulting constant
8306 with TREE_OVERFLOW if the original constant was not. These conversions
8307 have implementation defined behavior and retaining the TREE_OVERFLOW
8308 flag here would confuse later passes such as VRP. */
8309 tree
8310 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8311 tree type, tree op0)
8312 {
8313 tree res = fold_unary_loc (loc, code, type, op0);
8314 if (res
8315 && TREE_CODE (res) == INTEGER_CST
8316 && TREE_CODE (op0) == INTEGER_CST
8317 && CONVERT_EXPR_CODE_P (code))
8318 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8319
8320 return res;
8321 }
8322
8323 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8324 operands OP0 and OP1. LOC is the location of the resulting expression.
8325 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8326 Return the folded expression if folding is successful. Otherwise,
8327 return NULL_TREE. */
8328 static tree
8329 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8330 tree arg0, tree arg1, tree op0, tree op1)
8331 {
8332 tree tem;
8333
8334 /* We only do these simplifications if we are optimizing. */
8335 if (!optimize)
8336 return NULL_TREE;
8337
8338 /* Check for things like (A || B) && (A || C). We can convert this
8339 to A || (B && C). Note that either operator can be any of the four
8340 truth and/or operations and the transformation will still be
8341 valid. Also note that we only care about order for the
8342 ANDIF and ORIF operators. If B contains side effects, this
8343 might change the truth-value of A. */
8344 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8345 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8346 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8347 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8348 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8349 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8350 {
8351 tree a00 = TREE_OPERAND (arg0, 0);
8352 tree a01 = TREE_OPERAND (arg0, 1);
8353 tree a10 = TREE_OPERAND (arg1, 0);
8354 tree a11 = TREE_OPERAND (arg1, 1);
8355 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8356 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8357 && (code == TRUTH_AND_EXPR
8358 || code == TRUTH_OR_EXPR));
8359
8360 if (operand_equal_p (a00, a10, 0))
8361 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8362 fold_build2_loc (loc, code, type, a01, a11));
8363 else if (commutative && operand_equal_p (a00, a11, 0))
8364 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8365 fold_build2_loc (loc, code, type, a01, a10));
8366 else if (commutative && operand_equal_p (a01, a10, 0))
8367 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8368 fold_build2_loc (loc, code, type, a00, a11));
8369
8370 /* This case if tricky because we must either have commutative
8371 operators or else A10 must not have side-effects. */
8372
8373 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8374 && operand_equal_p (a01, a11, 0))
8375 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8376 fold_build2_loc (loc, code, type, a00, a10),
8377 a01);
8378 }
8379
8380 /* See if we can build a range comparison. */
8381 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8382 return tem;
8383
8384 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8385 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8386 {
8387 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8388 if (tem)
8389 return fold_build2_loc (loc, code, type, tem, arg1);
8390 }
8391
8392 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8393 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8394 {
8395 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8396 if (tem)
8397 return fold_build2_loc (loc, code, type, arg0, tem);
8398 }
8399
8400 /* Check for the possibility of merging component references. If our
8401 lhs is another similar operation, try to merge its rhs with our
8402 rhs. Then try to merge our lhs and rhs. */
8403 if (TREE_CODE (arg0) == code
8404 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8405 TREE_OPERAND (arg0, 1), arg1)))
8406 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8407
8408 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8409 return tem;
8410
8411 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8412 && (code == TRUTH_AND_EXPR
8413 || code == TRUTH_ANDIF_EXPR
8414 || code == TRUTH_OR_EXPR
8415 || code == TRUTH_ORIF_EXPR))
8416 {
8417 enum tree_code ncode, icode;
8418
8419 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8420 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8421 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8422
8423 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8424 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8425 We don't want to pack more than two leafs to a non-IF AND/OR
8426 expression.
8427 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8428 equal to IF-CODE, then we don't want to add right-hand operand.
8429 If the inner right-hand side of left-hand operand has
8430 side-effects, or isn't simple, then we can't add to it,
8431 as otherwise we might destroy if-sequence. */
8432 if (TREE_CODE (arg0) == icode
8433 && simple_operand_p_2 (arg1)
8434 /* Needed for sequence points to handle trappings, and
8435 side-effects. */
8436 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8437 {
8438 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8439 arg1);
8440 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8441 tem);
8442 }
8443 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8444 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8445 else if (TREE_CODE (arg1) == icode
8446 && simple_operand_p_2 (arg0)
8447 /* Needed for sequence points to handle trappings, and
8448 side-effects. */
8449 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8450 {
8451 tem = fold_build2_loc (loc, ncode, type,
8452 arg0, TREE_OPERAND (arg1, 0));
8453 return fold_build2_loc (loc, icode, type, tem,
8454 TREE_OPERAND (arg1, 1));
8455 }
8456 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8457 into (A OR B).
8458 For sequence point consistancy, we need to check for trapping,
8459 and side-effects. */
8460 else if (code == icode && simple_operand_p_2 (arg0)
8461 && simple_operand_p_2 (arg1))
8462 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8463 }
8464
8465 return NULL_TREE;
8466 }
8467
8468 /* Fold a binary expression of code CODE and type TYPE with operands
8469 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8470 Return the folded expression if folding is successful. Otherwise,
8471 return NULL_TREE. */
8472
8473 static tree
8474 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8475 {
8476 enum tree_code compl_code;
8477
8478 if (code == MIN_EXPR)
8479 compl_code = MAX_EXPR;
8480 else if (code == MAX_EXPR)
8481 compl_code = MIN_EXPR;
8482 else
8483 gcc_unreachable ();
8484
8485 /* MIN (MAX (a, b), b) == b. */
8486 if (TREE_CODE (op0) == compl_code
8487 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8488 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8489
8490 /* MIN (MAX (b, a), b) == b. */
8491 if (TREE_CODE (op0) == compl_code
8492 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8493 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8494 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8495
8496 /* MIN (a, MAX (a, b)) == a. */
8497 if (TREE_CODE (op1) == compl_code
8498 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8499 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8500 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8501
8502 /* MIN (a, MAX (b, a)) == a. */
8503 if (TREE_CODE (op1) == compl_code
8504 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8505 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8506 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8507
8508 return NULL_TREE;
8509 }
8510
8511 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8512 by changing CODE to reduce the magnitude of constants involved in
8513 ARG0 of the comparison.
8514 Returns a canonicalized comparison tree if a simplification was
8515 possible, otherwise returns NULL_TREE.
8516 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8517 valid if signed overflow is undefined. */
8518
8519 static tree
8520 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8521 tree arg0, tree arg1,
8522 bool *strict_overflow_p)
8523 {
8524 enum tree_code code0 = TREE_CODE (arg0);
8525 tree t, cst0 = NULL_TREE;
8526 int sgn0;
8527 bool swap = false;
8528
8529 /* Match A +- CST code arg1 and CST code arg1. We can change the
8530 first form only if overflow is undefined. */
8531 if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8532 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8533 /* In principle pointers also have undefined overflow behavior,
8534 but that causes problems elsewhere. */
8535 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8536 && (code0 == MINUS_EXPR
8537 || code0 == PLUS_EXPR)
8538 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8539 || code0 == INTEGER_CST))
8540 return NULL_TREE;
8541
8542 /* Identify the constant in arg0 and its sign. */
8543 if (code0 == INTEGER_CST)
8544 cst0 = arg0;
8545 else
8546 cst0 = TREE_OPERAND (arg0, 1);
8547 sgn0 = tree_int_cst_sgn (cst0);
8548
8549 /* Overflowed constants and zero will cause problems. */
8550 if (integer_zerop (cst0)
8551 || TREE_OVERFLOW (cst0))
8552 return NULL_TREE;
8553
8554 /* See if we can reduce the magnitude of the constant in
8555 arg0 by changing the comparison code. */
8556 if (code0 == INTEGER_CST)
8557 {
8558 /* CST <= arg1 -> CST-1 < arg1. */
8559 if (code == LE_EXPR && sgn0 == 1)
8560 code = LT_EXPR;
8561 /* -CST < arg1 -> -CST-1 <= arg1. */
8562 else if (code == LT_EXPR && sgn0 == -1)
8563 code = LE_EXPR;
8564 /* CST > arg1 -> CST-1 >= arg1. */
8565 else if (code == GT_EXPR && sgn0 == 1)
8566 code = GE_EXPR;
8567 /* -CST >= arg1 -> -CST-1 > arg1. */
8568 else if (code == GE_EXPR && sgn0 == -1)
8569 code = GT_EXPR;
8570 else
8571 return NULL_TREE;
8572 /* arg1 code' CST' might be more canonical. */
8573 swap = true;
8574 }
8575 else
8576 {
8577 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8578 if (code == LT_EXPR
8579 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8580 code = LE_EXPR;
8581 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8582 else if (code == GT_EXPR
8583 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8584 code = GE_EXPR;
8585 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8586 else if (code == LE_EXPR
8587 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8588 code = LT_EXPR;
8589 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8590 else if (code == GE_EXPR
8591 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8592 code = GT_EXPR;
8593 else
8594 return NULL_TREE;
8595 *strict_overflow_p = true;
8596 }
8597
8598 /* Now build the constant reduced in magnitude. But not if that
8599 would produce one outside of its types range. */
8600 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8601 && ((sgn0 == 1
8602 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8603 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8604 || (sgn0 == -1
8605 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8606 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8607 /* We cannot swap the comparison here as that would cause us to
8608 endlessly recurse. */
8609 return NULL_TREE;
8610
8611 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8612 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8613 if (code0 != INTEGER_CST)
8614 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8615 t = fold_convert (TREE_TYPE (arg1), t);
8616
8617 /* If swapping might yield to a more canonical form, do so. */
8618 if (swap)
8619 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8620 else
8621 return fold_build2_loc (loc, code, type, t, arg1);
8622 }
8623
8624 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8625 overflow further. Try to decrease the magnitude of constants involved
8626 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8627 and put sole constants at the second argument position.
8628 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8629
8630 static tree
8631 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8632 tree arg0, tree arg1)
8633 {
8634 tree t;
8635 bool strict_overflow_p;
8636 const char * const warnmsg = G_("assuming signed overflow does not occur "
8637 "when reducing constant in comparison");
8638
8639 /* Try canonicalization by simplifying arg0. */
8640 strict_overflow_p = false;
8641 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8642 &strict_overflow_p);
8643 if (t)
8644 {
8645 if (strict_overflow_p)
8646 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8647 return t;
8648 }
8649
8650 /* Try canonicalization by simplifying arg1 using the swapped
8651 comparison. */
8652 code = swap_tree_comparison (code);
8653 strict_overflow_p = false;
8654 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8655 &strict_overflow_p);
8656 if (t && strict_overflow_p)
8657 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8658 return t;
8659 }
8660
8661 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8662 space. This is used to avoid issuing overflow warnings for
8663 expressions like &p->x which can not wrap. */
8664
8665 static bool
8666 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8667 {
8668 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8669 return true;
8670
8671 if (bitpos < 0)
8672 return true;
8673
8674 wide_int wi_offset;
8675 int precision = TYPE_PRECISION (TREE_TYPE (base));
8676 if (offset == NULL_TREE)
8677 wi_offset = wi::zero (precision);
8678 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8679 return true;
8680 else
8681 wi_offset = offset;
8682
8683 bool overflow;
8684 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8685 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8686 if (overflow)
8687 return true;
8688
8689 if (!wi::fits_uhwi_p (total))
8690 return true;
8691
8692 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8693 if (size <= 0)
8694 return true;
8695
8696 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8697 array. */
8698 if (TREE_CODE (base) == ADDR_EXPR)
8699 {
8700 HOST_WIDE_INT base_size;
8701
8702 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8703 if (base_size > 0 && size < base_size)
8704 size = base_size;
8705 }
8706
8707 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8708 }
8709
8710 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8711 kind INTEGER_CST. This makes sure to properly sign-extend the
8712 constant. */
8713
8714 static HOST_WIDE_INT
8715 size_low_cst (const_tree t)
8716 {
8717 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8718 int prec = TYPE_PRECISION (TREE_TYPE (t));
8719 if (prec < HOST_BITS_PER_WIDE_INT)
8720 return sext_hwi (w, prec);
8721 return w;
8722 }
8723
8724 /* Subroutine of fold_binary. This routine performs all of the
8725 transformations that are common to the equality/inequality
8726 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8727 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8728 fold_binary should call fold_binary. Fold a comparison with
8729 tree code CODE and type TYPE with operands OP0 and OP1. Return
8730 the folded comparison or NULL_TREE. */
8731
8732 static tree
8733 fold_comparison (location_t loc, enum tree_code code, tree type,
8734 tree op0, tree op1)
8735 {
8736 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8737 tree arg0, arg1, tem;
8738
8739 arg0 = op0;
8740 arg1 = op1;
8741
8742 STRIP_SIGN_NOPS (arg0);
8743 STRIP_SIGN_NOPS (arg1);
8744
8745 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8746 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8747 && (equality_code
8748 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8749 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8750 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8751 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8752 && TREE_CODE (arg1) == INTEGER_CST
8753 && !TREE_OVERFLOW (arg1))
8754 {
8755 const enum tree_code
8756 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8757 tree const1 = TREE_OPERAND (arg0, 1);
8758 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8759 tree variable = TREE_OPERAND (arg0, 0);
8760 tree new_const = int_const_binop (reverse_op, const2, const1);
8761
8762 /* If the constant operation overflowed this can be
8763 simplified as a comparison against INT_MAX/INT_MIN. */
8764 if (TREE_OVERFLOW (new_const)
8765 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8766 {
8767 int const1_sgn = tree_int_cst_sgn (const1);
8768 enum tree_code code2 = code;
8769
8770 /* Get the sign of the constant on the lhs if the
8771 operation were VARIABLE + CONST1. */
8772 if (TREE_CODE (arg0) == MINUS_EXPR)
8773 const1_sgn = -const1_sgn;
8774
8775 /* The sign of the constant determines if we overflowed
8776 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8777 Canonicalize to the INT_MIN overflow by swapping the comparison
8778 if necessary. */
8779 if (const1_sgn == -1)
8780 code2 = swap_tree_comparison (code);
8781
8782 /* We now can look at the canonicalized case
8783 VARIABLE + 1 CODE2 INT_MIN
8784 and decide on the result. */
8785 switch (code2)
8786 {
8787 case EQ_EXPR:
8788 case LT_EXPR:
8789 case LE_EXPR:
8790 return
8791 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8792
8793 case NE_EXPR:
8794 case GE_EXPR:
8795 case GT_EXPR:
8796 return
8797 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8798
8799 default:
8800 gcc_unreachable ();
8801 }
8802 }
8803 else
8804 {
8805 if (!equality_code)
8806 fold_overflow_warning ("assuming signed overflow does not occur "
8807 "when changing X +- C1 cmp C2 to "
8808 "X cmp C2 -+ C1",
8809 WARN_STRICT_OVERFLOW_COMPARISON);
8810 return fold_build2_loc (loc, code, type, variable, new_const);
8811 }
8812 }
8813
8814 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. */
8815 if (TREE_CODE (arg0) == MINUS_EXPR
8816 && equality_code
8817 && integer_zerop (arg1))
8818 {
8819 /* ??? The transformation is valid for the other operators if overflow
8820 is undefined for the type, but performing it here badly interacts
8821 with the transformation in fold_cond_expr_with_comparison which
8822 attempts to synthetize ABS_EXPR. */
8823 if (!equality_code)
8824 fold_overflow_warning ("assuming signed overflow does not occur "
8825 "when changing X - Y cmp 0 to X cmp Y",
8826 WARN_STRICT_OVERFLOW_COMPARISON);
8827 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
8828 TREE_OPERAND (arg0, 1));
8829 }
8830
8831 /* For comparisons of pointers we can decompose it to a compile time
8832 comparison of the base objects and the offsets into the object.
8833 This requires at least one operand being an ADDR_EXPR or a
8834 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8835 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8836 && (TREE_CODE (arg0) == ADDR_EXPR
8837 || TREE_CODE (arg1) == ADDR_EXPR
8838 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8839 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8840 {
8841 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8842 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8843 machine_mode mode;
8844 int volatilep, unsignedp;
8845 bool indirect_base0 = false, indirect_base1 = false;
8846
8847 /* Get base and offset for the access. Strip ADDR_EXPR for
8848 get_inner_reference, but put it back by stripping INDIRECT_REF
8849 off the base object if possible. indirect_baseN will be true
8850 if baseN is not an address but refers to the object itself. */
8851 base0 = arg0;
8852 if (TREE_CODE (arg0) == ADDR_EXPR)
8853 {
8854 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8855 &bitsize, &bitpos0, &offset0, &mode,
8856 &unsignedp, &volatilep, false);
8857 if (TREE_CODE (base0) == INDIRECT_REF)
8858 base0 = TREE_OPERAND (base0, 0);
8859 else
8860 indirect_base0 = true;
8861 }
8862 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8863 {
8864 base0 = TREE_OPERAND (arg0, 0);
8865 STRIP_SIGN_NOPS (base0);
8866 if (TREE_CODE (base0) == ADDR_EXPR)
8867 {
8868 base0 = TREE_OPERAND (base0, 0);
8869 indirect_base0 = true;
8870 }
8871 offset0 = TREE_OPERAND (arg0, 1);
8872 if (tree_fits_shwi_p (offset0))
8873 {
8874 HOST_WIDE_INT off = size_low_cst (offset0);
8875 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8876 * BITS_PER_UNIT)
8877 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8878 {
8879 bitpos0 = off * BITS_PER_UNIT;
8880 offset0 = NULL_TREE;
8881 }
8882 }
8883 }
8884
8885 base1 = arg1;
8886 if (TREE_CODE (arg1) == ADDR_EXPR)
8887 {
8888 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8889 &bitsize, &bitpos1, &offset1, &mode,
8890 &unsignedp, &volatilep, false);
8891 if (TREE_CODE (base1) == INDIRECT_REF)
8892 base1 = TREE_OPERAND (base1, 0);
8893 else
8894 indirect_base1 = true;
8895 }
8896 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8897 {
8898 base1 = TREE_OPERAND (arg1, 0);
8899 STRIP_SIGN_NOPS (base1);
8900 if (TREE_CODE (base1) == ADDR_EXPR)
8901 {
8902 base1 = TREE_OPERAND (base1, 0);
8903 indirect_base1 = true;
8904 }
8905 offset1 = TREE_OPERAND (arg1, 1);
8906 if (tree_fits_shwi_p (offset1))
8907 {
8908 HOST_WIDE_INT off = size_low_cst (offset1);
8909 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8910 * BITS_PER_UNIT)
8911 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8912 {
8913 bitpos1 = off * BITS_PER_UNIT;
8914 offset1 = NULL_TREE;
8915 }
8916 }
8917 }
8918
8919 /* A local variable can never be pointed to by
8920 the default SSA name of an incoming parameter. */
8921 if ((TREE_CODE (arg0) == ADDR_EXPR
8922 && indirect_base0
8923 && TREE_CODE (base0) == VAR_DECL
8924 && auto_var_in_fn_p (base0, current_function_decl)
8925 && !indirect_base1
8926 && TREE_CODE (base1) == SSA_NAME
8927 && SSA_NAME_IS_DEFAULT_DEF (base1)
8928 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
8929 || (TREE_CODE (arg1) == ADDR_EXPR
8930 && indirect_base1
8931 && TREE_CODE (base1) == VAR_DECL
8932 && auto_var_in_fn_p (base1, current_function_decl)
8933 && !indirect_base0
8934 && TREE_CODE (base0) == SSA_NAME
8935 && SSA_NAME_IS_DEFAULT_DEF (base0)
8936 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
8937 {
8938 if (code == NE_EXPR)
8939 return constant_boolean_node (1, type);
8940 else if (code == EQ_EXPR)
8941 return constant_boolean_node (0, type);
8942 }
8943 /* If we have equivalent bases we might be able to simplify. */
8944 else if (indirect_base0 == indirect_base1
8945 && operand_equal_p (base0, base1, 0))
8946 {
8947 /* We can fold this expression to a constant if the non-constant
8948 offset parts are equal. */
8949 if ((offset0 == offset1
8950 || (offset0 && offset1
8951 && operand_equal_p (offset0, offset1, 0)))
8952 && (code == EQ_EXPR
8953 || code == NE_EXPR
8954 || (indirect_base0 && DECL_P (base0))
8955 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8956
8957 {
8958 if (!equality_code
8959 && bitpos0 != bitpos1
8960 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8961 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8962 fold_overflow_warning (("assuming pointer wraparound does not "
8963 "occur when comparing P +- C1 with "
8964 "P +- C2"),
8965 WARN_STRICT_OVERFLOW_CONDITIONAL);
8966
8967 switch (code)
8968 {
8969 case EQ_EXPR:
8970 return constant_boolean_node (bitpos0 == bitpos1, type);
8971 case NE_EXPR:
8972 return constant_boolean_node (bitpos0 != bitpos1, type);
8973 case LT_EXPR:
8974 return constant_boolean_node (bitpos0 < bitpos1, type);
8975 case LE_EXPR:
8976 return constant_boolean_node (bitpos0 <= bitpos1, type);
8977 case GE_EXPR:
8978 return constant_boolean_node (bitpos0 >= bitpos1, type);
8979 case GT_EXPR:
8980 return constant_boolean_node (bitpos0 > bitpos1, type);
8981 default:;
8982 }
8983 }
8984 /* We can simplify the comparison to a comparison of the variable
8985 offset parts if the constant offset parts are equal.
8986 Be careful to use signed sizetype here because otherwise we
8987 mess with array offsets in the wrong way. This is possible
8988 because pointer arithmetic is restricted to retain within an
8989 object and overflow on pointer differences is undefined as of
8990 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8991 else if (bitpos0 == bitpos1
8992 && (equality_code
8993 || (indirect_base0 && DECL_P (base0))
8994 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8995 {
8996 /* By converting to signed sizetype we cover middle-end pointer
8997 arithmetic which operates on unsigned pointer types of size
8998 type size and ARRAY_REF offsets which are properly sign or
8999 zero extended from their type in case it is narrower than
9000 sizetype. */
9001 if (offset0 == NULL_TREE)
9002 offset0 = build_int_cst (ssizetype, 0);
9003 else
9004 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9005 if (offset1 == NULL_TREE)
9006 offset1 = build_int_cst (ssizetype, 0);
9007 else
9008 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9009
9010 if (!equality_code
9011 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9012 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9013 fold_overflow_warning (("assuming pointer wraparound does not "
9014 "occur when comparing P +- C1 with "
9015 "P +- C2"),
9016 WARN_STRICT_OVERFLOW_COMPARISON);
9017
9018 return fold_build2_loc (loc, code, type, offset0, offset1);
9019 }
9020 }
9021 /* For non-equal bases we can simplify if they are addresses
9022 declarations with different addresses. */
9023 else if (indirect_base0 && indirect_base1
9024 /* We know that !operand_equal_p (base0, base1, 0)
9025 because the if condition was false. But make
9026 sure two decls are not the same. */
9027 && base0 != base1
9028 && TREE_CODE (arg0) == ADDR_EXPR
9029 && TREE_CODE (arg1) == ADDR_EXPR
9030 && DECL_P (base0)
9031 && DECL_P (base1)
9032 /* Watch for aliases. */
9033 && (!decl_in_symtab_p (base0)
9034 || !decl_in_symtab_p (base1)
9035 || !symtab_node::get_create (base0)->equal_address_to
9036 (symtab_node::get_create (base1))))
9037 {
9038 if (code == EQ_EXPR)
9039 return omit_two_operands_loc (loc, type, boolean_false_node,
9040 arg0, arg1);
9041 else if (code == NE_EXPR)
9042 return omit_two_operands_loc (loc, type, boolean_true_node,
9043 arg0, arg1);
9044 }
9045 /* For equal offsets we can simplify to a comparison of the
9046 base addresses. */
9047 else if (bitpos0 == bitpos1
9048 && (indirect_base0
9049 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9050 && (indirect_base1
9051 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9052 && ((offset0 == offset1)
9053 || (offset0 && offset1
9054 && operand_equal_p (offset0, offset1, 0))))
9055 {
9056 if (indirect_base0)
9057 base0 = build_fold_addr_expr_loc (loc, base0);
9058 if (indirect_base1)
9059 base1 = build_fold_addr_expr_loc (loc, base1);
9060 return fold_build2_loc (loc, code, type, base0, base1);
9061 }
9062 }
9063
9064 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9065 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9066 the resulting offset is smaller in absolute value than the
9067 original one and has the same sign. */
9068 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9069 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9070 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9071 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9072 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9073 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9074 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9075 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9076 {
9077 tree const1 = TREE_OPERAND (arg0, 1);
9078 tree const2 = TREE_OPERAND (arg1, 1);
9079 tree variable1 = TREE_OPERAND (arg0, 0);
9080 tree variable2 = TREE_OPERAND (arg1, 0);
9081 tree cst;
9082 const char * const warnmsg = G_("assuming signed overflow does not "
9083 "occur when combining constants around "
9084 "a comparison");
9085
9086 /* Put the constant on the side where it doesn't overflow and is
9087 of lower absolute value and of same sign than before. */
9088 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9089 ? MINUS_EXPR : PLUS_EXPR,
9090 const2, const1);
9091 if (!TREE_OVERFLOW (cst)
9092 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
9093 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
9094 {
9095 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9096 return fold_build2_loc (loc, code, type,
9097 variable1,
9098 fold_build2_loc (loc, TREE_CODE (arg1),
9099 TREE_TYPE (arg1),
9100 variable2, cst));
9101 }
9102
9103 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9104 ? MINUS_EXPR : PLUS_EXPR,
9105 const1, const2);
9106 if (!TREE_OVERFLOW (cst)
9107 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
9108 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
9109 {
9110 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9111 return fold_build2_loc (loc, code, type,
9112 fold_build2_loc (loc, TREE_CODE (arg0),
9113 TREE_TYPE (arg0),
9114 variable1, cst),
9115 variable2);
9116 }
9117 }
9118
9119 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9120 signed arithmetic case. That form is created by the compiler
9121 often enough for folding it to be of value. One example is in
9122 computing loop trip counts after Operator Strength Reduction. */
9123 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9124 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9125 && TREE_CODE (arg0) == MULT_EXPR
9126 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9127 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9128 && integer_zerop (arg1))
9129 {
9130 tree const1 = TREE_OPERAND (arg0, 1);
9131 tree const2 = arg1; /* zero */
9132 tree variable1 = TREE_OPERAND (arg0, 0);
9133 enum tree_code cmp_code = code;
9134
9135 /* Handle unfolded multiplication by zero. */
9136 if (integer_zerop (const1))
9137 return fold_build2_loc (loc, cmp_code, type, const1, const2);
9138
9139 fold_overflow_warning (("assuming signed overflow does not occur when "
9140 "eliminating multiplication in comparison "
9141 "with zero"),
9142 WARN_STRICT_OVERFLOW_COMPARISON);
9143
9144 /* If const1 is negative we swap the sense of the comparison. */
9145 if (tree_int_cst_sgn (const1) < 0)
9146 cmp_code = swap_tree_comparison (cmp_code);
9147
9148 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9149 }
9150
9151 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9152 if (tem)
9153 return tem;
9154
9155 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9156 {
9157 tree targ0 = strip_float_extensions (arg0);
9158 tree targ1 = strip_float_extensions (arg1);
9159 tree newtype = TREE_TYPE (targ0);
9160
9161 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9162 newtype = TREE_TYPE (targ1);
9163
9164 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9165 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9166 return fold_build2_loc (loc, code, type,
9167 fold_convert_loc (loc, newtype, targ0),
9168 fold_convert_loc (loc, newtype, targ1));
9169
9170 if (TREE_CODE (arg1) == REAL_CST)
9171 {
9172 REAL_VALUE_TYPE cst;
9173 cst = TREE_REAL_CST (arg1);
9174
9175 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9176 /* a CMP (-0) -> a CMP 0 */
9177 if (REAL_VALUE_MINUS_ZERO (cst))
9178 return fold_build2_loc (loc, code, type, arg0,
9179 build_real (TREE_TYPE (arg1), dconst0));
9180
9181 /* x != NaN is always true, other ops are always false. */
9182 if (REAL_VALUE_ISNAN (cst)
9183 && ! HONOR_SNANS (arg1))
9184 {
9185 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9186 return omit_one_operand_loc (loc, type, tem, arg0);
9187 }
9188
9189 /* Fold comparisons against infinity. */
9190 if (REAL_VALUE_ISINF (cst)
9191 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9192 {
9193 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9194 if (tem != NULL_TREE)
9195 return tem;
9196 }
9197 }
9198
9199 /* If this is a comparison of a real constant with a PLUS_EXPR
9200 or a MINUS_EXPR of a real constant, we can convert it into a
9201 comparison with a revised real constant as long as no overflow
9202 occurs when unsafe_math_optimizations are enabled. */
9203 if (flag_unsafe_math_optimizations
9204 && TREE_CODE (arg1) == REAL_CST
9205 && (TREE_CODE (arg0) == PLUS_EXPR
9206 || TREE_CODE (arg0) == MINUS_EXPR)
9207 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9208 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9209 ? MINUS_EXPR : PLUS_EXPR,
9210 arg1, TREE_OPERAND (arg0, 1)))
9211 && !TREE_OVERFLOW (tem))
9212 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9213
9214 /* Likewise, we can simplify a comparison of a real constant with
9215 a MINUS_EXPR whose first operand is also a real constant, i.e.
9216 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9217 floating-point types only if -fassociative-math is set. */
9218 if (flag_associative_math
9219 && TREE_CODE (arg1) == REAL_CST
9220 && TREE_CODE (arg0) == MINUS_EXPR
9221 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9222 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9223 arg1))
9224 && !TREE_OVERFLOW (tem))
9225 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9226 TREE_OPERAND (arg0, 1), tem);
9227
9228 /* Fold comparisons against built-in math functions. */
9229 if (TREE_CODE (arg1) == REAL_CST
9230 && flag_unsafe_math_optimizations
9231 && ! flag_errno_math)
9232 {
9233 enum built_in_function fcode = builtin_mathfn_code (arg0);
9234
9235 if (fcode != END_BUILTINS)
9236 {
9237 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9238 if (tem != NULL_TREE)
9239 return tem;
9240 }
9241 }
9242 }
9243
9244 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9245 && CONVERT_EXPR_P (arg0))
9246 {
9247 /* If we are widening one operand of an integer comparison,
9248 see if the other operand is similarly being widened. Perhaps we
9249 can do the comparison in the narrower type. */
9250 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9251 if (tem)
9252 return tem;
9253
9254 /* Or if we are changing signedness. */
9255 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9256 if (tem)
9257 return tem;
9258 }
9259
9260 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9261 constant, we can simplify it. */
9262 if (TREE_CODE (arg1) == INTEGER_CST
9263 && (TREE_CODE (arg0) == MIN_EXPR
9264 || TREE_CODE (arg0) == MAX_EXPR)
9265 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9266 {
9267 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9268 if (tem)
9269 return tem;
9270 }
9271
9272 /* Simplify comparison of something with itself. (For IEEE
9273 floating-point, we can only do some of these simplifications.) */
9274 if (operand_equal_p (arg0, arg1, 0))
9275 {
9276 switch (code)
9277 {
9278 case EQ_EXPR:
9279 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9280 || ! HONOR_NANS (arg0))
9281 return constant_boolean_node (1, type);
9282 break;
9283
9284 case GE_EXPR:
9285 case LE_EXPR:
9286 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9287 || ! HONOR_NANS (arg0))
9288 return constant_boolean_node (1, type);
9289 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9290
9291 case NE_EXPR:
9292 /* For NE, we can only do this simplification if integer
9293 or we don't honor IEEE floating point NaNs. */
9294 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9295 && HONOR_NANS (arg0))
9296 break;
9297 /* ... fall through ... */
9298 case GT_EXPR:
9299 case LT_EXPR:
9300 return constant_boolean_node (0, type);
9301 default:
9302 gcc_unreachable ();
9303 }
9304 }
9305
9306 /* If we are comparing an expression that just has comparisons
9307 of two integer values, arithmetic expressions of those comparisons,
9308 and constants, we can simplify it. There are only three cases
9309 to check: the two values can either be equal, the first can be
9310 greater, or the second can be greater. Fold the expression for
9311 those three values. Since each value must be 0 or 1, we have
9312 eight possibilities, each of which corresponds to the constant 0
9313 or 1 or one of the six possible comparisons.
9314
9315 This handles common cases like (a > b) == 0 but also handles
9316 expressions like ((x > y) - (y > x)) > 0, which supposedly
9317 occur in macroized code. */
9318
9319 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9320 {
9321 tree cval1 = 0, cval2 = 0;
9322 int save_p = 0;
9323
9324 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9325 /* Don't handle degenerate cases here; they should already
9326 have been handled anyway. */
9327 && cval1 != 0 && cval2 != 0
9328 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9329 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9330 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9331 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9332 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9333 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9334 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9335 {
9336 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9337 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9338
9339 /* We can't just pass T to eval_subst in case cval1 or cval2
9340 was the same as ARG1. */
9341
9342 tree high_result
9343 = fold_build2_loc (loc, code, type,
9344 eval_subst (loc, arg0, cval1, maxval,
9345 cval2, minval),
9346 arg1);
9347 tree equal_result
9348 = fold_build2_loc (loc, code, type,
9349 eval_subst (loc, arg0, cval1, maxval,
9350 cval2, maxval),
9351 arg1);
9352 tree low_result
9353 = fold_build2_loc (loc, code, type,
9354 eval_subst (loc, arg0, cval1, minval,
9355 cval2, maxval),
9356 arg1);
9357
9358 /* All three of these results should be 0 or 1. Confirm they are.
9359 Then use those values to select the proper code to use. */
9360
9361 if (TREE_CODE (high_result) == INTEGER_CST
9362 && TREE_CODE (equal_result) == INTEGER_CST
9363 && TREE_CODE (low_result) == INTEGER_CST)
9364 {
9365 /* Make a 3-bit mask with the high-order bit being the
9366 value for `>', the next for '=', and the low for '<'. */
9367 switch ((integer_onep (high_result) * 4)
9368 + (integer_onep (equal_result) * 2)
9369 + integer_onep (low_result))
9370 {
9371 case 0:
9372 /* Always false. */
9373 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9374 case 1:
9375 code = LT_EXPR;
9376 break;
9377 case 2:
9378 code = EQ_EXPR;
9379 break;
9380 case 3:
9381 code = LE_EXPR;
9382 break;
9383 case 4:
9384 code = GT_EXPR;
9385 break;
9386 case 5:
9387 code = NE_EXPR;
9388 break;
9389 case 6:
9390 code = GE_EXPR;
9391 break;
9392 case 7:
9393 /* Always true. */
9394 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9395 }
9396
9397 if (save_p)
9398 {
9399 tem = save_expr (build2 (code, type, cval1, cval2));
9400 SET_EXPR_LOCATION (tem, loc);
9401 return tem;
9402 }
9403 return fold_build2_loc (loc, code, type, cval1, cval2);
9404 }
9405 }
9406 }
9407
9408 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9409 into a single range test. */
9410 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9411 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9412 && TREE_CODE (arg1) == INTEGER_CST
9413 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9414 && !integer_zerop (TREE_OPERAND (arg0, 1))
9415 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9416 && !TREE_OVERFLOW (arg1))
9417 {
9418 tem = fold_div_compare (loc, code, type, arg0, arg1);
9419 if (tem != NULL_TREE)
9420 return tem;
9421 }
9422
9423 /* Fold ~X op ~Y as Y op X. */
9424 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9425 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9426 {
9427 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9428 return fold_build2_loc (loc, code, type,
9429 fold_convert_loc (loc, cmp_type,
9430 TREE_OPERAND (arg1, 0)),
9431 TREE_OPERAND (arg0, 0));
9432 }
9433
9434 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9435 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9436 && (TREE_CODE (arg1) == INTEGER_CST || TREE_CODE (arg1) == VECTOR_CST))
9437 {
9438 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9439 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9440 TREE_OPERAND (arg0, 0),
9441 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9442 fold_convert_loc (loc, cmp_type, arg1)));
9443 }
9444
9445 return NULL_TREE;
9446 }
9447
9448
9449 /* Subroutine of fold_binary. Optimize complex multiplications of the
9450 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9451 argument EXPR represents the expression "z" of type TYPE. */
9452
9453 static tree
9454 fold_mult_zconjz (location_t loc, tree type, tree expr)
9455 {
9456 tree itype = TREE_TYPE (type);
9457 tree rpart, ipart, tem;
9458
9459 if (TREE_CODE (expr) == COMPLEX_EXPR)
9460 {
9461 rpart = TREE_OPERAND (expr, 0);
9462 ipart = TREE_OPERAND (expr, 1);
9463 }
9464 else if (TREE_CODE (expr) == COMPLEX_CST)
9465 {
9466 rpart = TREE_REALPART (expr);
9467 ipart = TREE_IMAGPART (expr);
9468 }
9469 else
9470 {
9471 expr = save_expr (expr);
9472 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9473 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9474 }
9475
9476 rpart = save_expr (rpart);
9477 ipart = save_expr (ipart);
9478 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9479 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9480 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9481 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9482 build_zero_cst (itype));
9483 }
9484
9485
9486 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9487 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9488 guarantees that P and N have the same least significant log2(M) bits.
9489 N is not otherwise constrained. In particular, N is not normalized to
9490 0 <= N < M as is common. In general, the precise value of P is unknown.
9491 M is chosen as large as possible such that constant N can be determined.
9492
9493 Returns M and sets *RESIDUE to N.
9494
9495 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9496 account. This is not always possible due to PR 35705.
9497 */
9498
9499 static unsigned HOST_WIDE_INT
9500 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9501 bool allow_func_align)
9502 {
9503 enum tree_code code;
9504
9505 *residue = 0;
9506
9507 code = TREE_CODE (expr);
9508 if (code == ADDR_EXPR)
9509 {
9510 unsigned int bitalign;
9511 get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue);
9512 *residue /= BITS_PER_UNIT;
9513 return bitalign / BITS_PER_UNIT;
9514 }
9515 else if (code == POINTER_PLUS_EXPR)
9516 {
9517 tree op0, op1;
9518 unsigned HOST_WIDE_INT modulus;
9519 enum tree_code inner_code;
9520
9521 op0 = TREE_OPERAND (expr, 0);
9522 STRIP_NOPS (op0);
9523 modulus = get_pointer_modulus_and_residue (op0, residue,
9524 allow_func_align);
9525
9526 op1 = TREE_OPERAND (expr, 1);
9527 STRIP_NOPS (op1);
9528 inner_code = TREE_CODE (op1);
9529 if (inner_code == INTEGER_CST)
9530 {
9531 *residue += TREE_INT_CST_LOW (op1);
9532 return modulus;
9533 }
9534 else if (inner_code == MULT_EXPR)
9535 {
9536 op1 = TREE_OPERAND (op1, 1);
9537 if (TREE_CODE (op1) == INTEGER_CST)
9538 {
9539 unsigned HOST_WIDE_INT align;
9540
9541 /* Compute the greatest power-of-2 divisor of op1. */
9542 align = TREE_INT_CST_LOW (op1);
9543 align &= -align;
9544
9545 /* If align is non-zero and less than *modulus, replace
9546 *modulus with align., If align is 0, then either op1 is 0
9547 or the greatest power-of-2 divisor of op1 doesn't fit in an
9548 unsigned HOST_WIDE_INT. In either case, no additional
9549 constraint is imposed. */
9550 if (align)
9551 modulus = MIN (modulus, align);
9552
9553 return modulus;
9554 }
9555 }
9556 }
9557
9558 /* If we get here, we were unable to determine anything useful about the
9559 expression. */
9560 return 1;
9561 }
9562
9563 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9564 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9565
9566 static bool
9567 vec_cst_ctor_to_array (tree arg, tree *elts)
9568 {
9569 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9570
9571 if (TREE_CODE (arg) == VECTOR_CST)
9572 {
9573 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
9574 elts[i] = VECTOR_CST_ELT (arg, i);
9575 }
9576 else if (TREE_CODE (arg) == CONSTRUCTOR)
9577 {
9578 constructor_elt *elt;
9579
9580 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9581 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9582 return false;
9583 else
9584 elts[i] = elt->value;
9585 }
9586 else
9587 return false;
9588 for (; i < nelts; i++)
9589 elts[i]
9590 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9591 return true;
9592 }
9593
9594 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9595 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9596 NULL_TREE otherwise. */
9597
9598 static tree
9599 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9600 {
9601 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9602 tree *elts;
9603 bool need_ctor = false;
9604
9605 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9606 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9607 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9608 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9609 return NULL_TREE;
9610
9611 elts = XALLOCAVEC (tree, nelts * 3);
9612 if (!vec_cst_ctor_to_array (arg0, elts)
9613 || !vec_cst_ctor_to_array (arg1, elts + nelts))
9614 return NULL_TREE;
9615
9616 for (i = 0; i < nelts; i++)
9617 {
9618 if (!CONSTANT_CLASS_P (elts[sel[i]]))
9619 need_ctor = true;
9620 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9621 }
9622
9623 if (need_ctor)
9624 {
9625 vec<constructor_elt, va_gc> *v;
9626 vec_alloc (v, nelts);
9627 for (i = 0; i < nelts; i++)
9628 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9629 return build_constructor (type, v);
9630 }
9631 else
9632 return build_vector (type, &elts[2 * nelts]);
9633 }
9634
9635 /* Try to fold a pointer difference of type TYPE two address expressions of
9636 array references AREF0 and AREF1 using location LOC. Return a
9637 simplified expression for the difference or NULL_TREE. */
9638
9639 static tree
9640 fold_addr_of_array_ref_difference (location_t loc, tree type,
9641 tree aref0, tree aref1)
9642 {
9643 tree base0 = TREE_OPERAND (aref0, 0);
9644 tree base1 = TREE_OPERAND (aref1, 0);
9645 tree base_offset = build_int_cst (type, 0);
9646
9647 /* If the bases are array references as well, recurse. If the bases
9648 are pointer indirections compute the difference of the pointers.
9649 If the bases are equal, we are set. */
9650 if ((TREE_CODE (base0) == ARRAY_REF
9651 && TREE_CODE (base1) == ARRAY_REF
9652 && (base_offset
9653 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9654 || (INDIRECT_REF_P (base0)
9655 && INDIRECT_REF_P (base1)
9656 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9657 TREE_OPERAND (base0, 0),
9658 TREE_OPERAND (base1, 0))))
9659 || operand_equal_p (base0, base1, 0))
9660 {
9661 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9662 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9663 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9664 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9665 return fold_build2_loc (loc, PLUS_EXPR, type,
9666 base_offset,
9667 fold_build2_loc (loc, MULT_EXPR, type,
9668 diff, esz));
9669 }
9670 return NULL_TREE;
9671 }
9672
9673 /* If the real or vector real constant CST of type TYPE has an exact
9674 inverse, return it, else return NULL. */
9675
9676 tree
9677 exact_inverse (tree type, tree cst)
9678 {
9679 REAL_VALUE_TYPE r;
9680 tree unit_type, *elts;
9681 machine_mode mode;
9682 unsigned vec_nelts, i;
9683
9684 switch (TREE_CODE (cst))
9685 {
9686 case REAL_CST:
9687 r = TREE_REAL_CST (cst);
9688
9689 if (exact_real_inverse (TYPE_MODE (type), &r))
9690 return build_real (type, r);
9691
9692 return NULL_TREE;
9693
9694 case VECTOR_CST:
9695 vec_nelts = VECTOR_CST_NELTS (cst);
9696 elts = XALLOCAVEC (tree, vec_nelts);
9697 unit_type = TREE_TYPE (type);
9698 mode = TYPE_MODE (unit_type);
9699
9700 for (i = 0; i < vec_nelts; i++)
9701 {
9702 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9703 if (!exact_real_inverse (mode, &r))
9704 return NULL_TREE;
9705 elts[i] = build_real (unit_type, r);
9706 }
9707
9708 return build_vector (type, elts);
9709
9710 default:
9711 return NULL_TREE;
9712 }
9713 }
9714
9715 /* Mask out the tz least significant bits of X of type TYPE where
9716 tz is the number of trailing zeroes in Y. */
9717 static wide_int
9718 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9719 {
9720 int tz = wi::ctz (y);
9721 if (tz > 0)
9722 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9723 return x;
9724 }
9725
9726 /* Return true when T is an address and is known to be nonzero.
9727 For floating point we further ensure that T is not denormal.
9728 Similar logic is present in nonzero_address in rtlanal.h.
9729
9730 If the return value is based on the assumption that signed overflow
9731 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9732 change *STRICT_OVERFLOW_P. */
9733
9734 static bool
9735 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9736 {
9737 tree type = TREE_TYPE (t);
9738 enum tree_code code;
9739
9740 /* Doing something useful for floating point would need more work. */
9741 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9742 return false;
9743
9744 code = TREE_CODE (t);
9745 switch (TREE_CODE_CLASS (code))
9746 {
9747 case tcc_unary:
9748 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9749 strict_overflow_p);
9750 case tcc_binary:
9751 case tcc_comparison:
9752 return tree_binary_nonzero_warnv_p (code, type,
9753 TREE_OPERAND (t, 0),
9754 TREE_OPERAND (t, 1),
9755 strict_overflow_p);
9756 case tcc_constant:
9757 case tcc_declaration:
9758 case tcc_reference:
9759 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9760
9761 default:
9762 break;
9763 }
9764
9765 switch (code)
9766 {
9767 case TRUTH_NOT_EXPR:
9768 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9769 strict_overflow_p);
9770
9771 case TRUTH_AND_EXPR:
9772 case TRUTH_OR_EXPR:
9773 case TRUTH_XOR_EXPR:
9774 return tree_binary_nonzero_warnv_p (code, type,
9775 TREE_OPERAND (t, 0),
9776 TREE_OPERAND (t, 1),
9777 strict_overflow_p);
9778
9779 case COND_EXPR:
9780 case CONSTRUCTOR:
9781 case OBJ_TYPE_REF:
9782 case ASSERT_EXPR:
9783 case ADDR_EXPR:
9784 case WITH_SIZE_EXPR:
9785 case SSA_NAME:
9786 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9787
9788 case COMPOUND_EXPR:
9789 case MODIFY_EXPR:
9790 case BIND_EXPR:
9791 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9792 strict_overflow_p);
9793
9794 case SAVE_EXPR:
9795 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9796 strict_overflow_p);
9797
9798 case CALL_EXPR:
9799 {
9800 tree fndecl = get_callee_fndecl (t);
9801 if (!fndecl) return false;
9802 if (flag_delete_null_pointer_checks && !flag_check_new
9803 && DECL_IS_OPERATOR_NEW (fndecl)
9804 && !TREE_NOTHROW (fndecl))
9805 return true;
9806 if (flag_delete_null_pointer_checks
9807 && lookup_attribute ("returns_nonnull",
9808 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9809 return true;
9810 return alloca_call_p (t);
9811 }
9812
9813 default:
9814 break;
9815 }
9816 return false;
9817 }
9818
9819 /* Return true when T is an address and is known to be nonzero.
9820 Handle warnings about undefined signed overflow. */
9821
9822 static bool
9823 tree_expr_nonzero_p (tree t)
9824 {
9825 bool ret, strict_overflow_p;
9826
9827 strict_overflow_p = false;
9828 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9829 if (strict_overflow_p)
9830 fold_overflow_warning (("assuming signed overflow does not occur when "
9831 "determining that expression is always "
9832 "non-zero"),
9833 WARN_STRICT_OVERFLOW_MISC);
9834 return ret;
9835 }
9836
9837 /* Fold a binary expression of code CODE and type TYPE with operands
9838 OP0 and OP1. LOC is the location of the resulting expression.
9839 Return the folded expression if folding is successful. Otherwise,
9840 return NULL_TREE. */
9841
9842 tree
9843 fold_binary_loc (location_t loc,
9844 enum tree_code code, tree type, tree op0, tree op1)
9845 {
9846 enum tree_code_class kind = TREE_CODE_CLASS (code);
9847 tree arg0, arg1, tem;
9848 tree t1 = NULL_TREE;
9849 bool strict_overflow_p;
9850 unsigned int prec;
9851
9852 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9853 && TREE_CODE_LENGTH (code) == 2
9854 && op0 != NULL_TREE
9855 && op1 != NULL_TREE);
9856
9857 arg0 = op0;
9858 arg1 = op1;
9859
9860 /* Strip any conversions that don't change the mode. This is
9861 safe for every expression, except for a comparison expression
9862 because its signedness is derived from its operands. So, in
9863 the latter case, only strip conversions that don't change the
9864 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9865 preserved.
9866
9867 Note that this is done as an internal manipulation within the
9868 constant folder, in order to find the simplest representation
9869 of the arguments so that their form can be studied. In any
9870 cases, the appropriate type conversions should be put back in
9871 the tree that will get out of the constant folder. */
9872
9873 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9874 {
9875 STRIP_SIGN_NOPS (arg0);
9876 STRIP_SIGN_NOPS (arg1);
9877 }
9878 else
9879 {
9880 STRIP_NOPS (arg0);
9881 STRIP_NOPS (arg1);
9882 }
9883
9884 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9885 constant but we can't do arithmetic on them. */
9886 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9887 {
9888 tem = const_binop (code, type, arg0, arg1);
9889 if (tem != NULL_TREE)
9890 {
9891 if (TREE_TYPE (tem) != type)
9892 tem = fold_convert_loc (loc, type, tem);
9893 return tem;
9894 }
9895 }
9896
9897 /* If this is a commutative operation, and ARG0 is a constant, move it
9898 to ARG1 to reduce the number of tests below. */
9899 if (commutative_tree_code (code)
9900 && tree_swap_operands_p (arg0, arg1, true))
9901 return fold_build2_loc (loc, code, type, op1, op0);
9902
9903 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9904 to ARG1 to reduce the number of tests below. */
9905 if (kind == tcc_comparison
9906 && tree_swap_operands_p (arg0, arg1, true))
9907 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9908
9909 tem = generic_simplify (loc, code, type, op0, op1);
9910 if (tem)
9911 return tem;
9912
9913 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9914
9915 First check for cases where an arithmetic operation is applied to a
9916 compound, conditional, or comparison operation. Push the arithmetic
9917 operation inside the compound or conditional to see if any folding
9918 can then be done. Convert comparison to conditional for this purpose.
9919 The also optimizes non-constant cases that used to be done in
9920 expand_expr.
9921
9922 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9923 one of the operands is a comparison and the other is a comparison, a
9924 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9925 code below would make the expression more complex. Change it to a
9926 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9927 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9928
9929 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9930 || code == EQ_EXPR || code == NE_EXPR)
9931 && TREE_CODE (type) != VECTOR_TYPE
9932 && ((truth_value_p (TREE_CODE (arg0))
9933 && (truth_value_p (TREE_CODE (arg1))
9934 || (TREE_CODE (arg1) == BIT_AND_EXPR
9935 && integer_onep (TREE_OPERAND (arg1, 1)))))
9936 || (truth_value_p (TREE_CODE (arg1))
9937 && (truth_value_p (TREE_CODE (arg0))
9938 || (TREE_CODE (arg0) == BIT_AND_EXPR
9939 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9940 {
9941 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9942 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9943 : TRUTH_XOR_EXPR,
9944 boolean_type_node,
9945 fold_convert_loc (loc, boolean_type_node, arg0),
9946 fold_convert_loc (loc, boolean_type_node, arg1));
9947
9948 if (code == EQ_EXPR)
9949 tem = invert_truthvalue_loc (loc, tem);
9950
9951 return fold_convert_loc (loc, type, tem);
9952 }
9953
9954 if (TREE_CODE_CLASS (code) == tcc_binary
9955 || TREE_CODE_CLASS (code) == tcc_comparison)
9956 {
9957 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9958 {
9959 tem = fold_build2_loc (loc, code, type,
9960 fold_convert_loc (loc, TREE_TYPE (op0),
9961 TREE_OPERAND (arg0, 1)), op1);
9962 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9963 tem);
9964 }
9965 if (TREE_CODE (arg1) == COMPOUND_EXPR
9966 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9967 {
9968 tem = fold_build2_loc (loc, code, type, op0,
9969 fold_convert_loc (loc, TREE_TYPE (op1),
9970 TREE_OPERAND (arg1, 1)));
9971 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9972 tem);
9973 }
9974
9975 if (TREE_CODE (arg0) == COND_EXPR
9976 || TREE_CODE (arg0) == VEC_COND_EXPR
9977 || COMPARISON_CLASS_P (arg0))
9978 {
9979 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9980 arg0, arg1,
9981 /*cond_first_p=*/1);
9982 if (tem != NULL_TREE)
9983 return tem;
9984 }
9985
9986 if (TREE_CODE (arg1) == COND_EXPR
9987 || TREE_CODE (arg1) == VEC_COND_EXPR
9988 || COMPARISON_CLASS_P (arg1))
9989 {
9990 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9991 arg1, arg0,
9992 /*cond_first_p=*/0);
9993 if (tem != NULL_TREE)
9994 return tem;
9995 }
9996 }
9997
9998 switch (code)
9999 {
10000 case MEM_REF:
10001 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10002 if (TREE_CODE (arg0) == ADDR_EXPR
10003 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10004 {
10005 tree iref = TREE_OPERAND (arg0, 0);
10006 return fold_build2 (MEM_REF, type,
10007 TREE_OPERAND (iref, 0),
10008 int_const_binop (PLUS_EXPR, arg1,
10009 TREE_OPERAND (iref, 1)));
10010 }
10011
10012 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10013 if (TREE_CODE (arg0) == ADDR_EXPR
10014 && handled_component_p (TREE_OPERAND (arg0, 0)))
10015 {
10016 tree base;
10017 HOST_WIDE_INT coffset;
10018 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10019 &coffset);
10020 if (!base)
10021 return NULL_TREE;
10022 return fold_build2 (MEM_REF, type,
10023 build_fold_addr_expr (base),
10024 int_const_binop (PLUS_EXPR, arg1,
10025 size_int (coffset)));
10026 }
10027
10028 return NULL_TREE;
10029
10030 case POINTER_PLUS_EXPR:
10031 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10032 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10033 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10034 return fold_convert_loc (loc, type,
10035 fold_build2_loc (loc, PLUS_EXPR, sizetype,
10036 fold_convert_loc (loc, sizetype,
10037 arg1),
10038 fold_convert_loc (loc, sizetype,
10039 arg0)));
10040
10041 return NULL_TREE;
10042
10043 case PLUS_EXPR:
10044 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10045 {
10046 /* X + (X / CST) * -CST is X % CST. */
10047 if (TREE_CODE (arg1) == MULT_EXPR
10048 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10049 && operand_equal_p (arg0,
10050 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10051 {
10052 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10053 tree cst1 = TREE_OPERAND (arg1, 1);
10054 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10055 cst1, cst0);
10056 if (sum && integer_zerop (sum))
10057 return fold_convert_loc (loc, type,
10058 fold_build2_loc (loc, TRUNC_MOD_EXPR,
10059 TREE_TYPE (arg0), arg0,
10060 cst0));
10061 }
10062 }
10063
10064 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10065 one. Make sure the type is not saturating and has the signedness of
10066 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10067 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10068 if ((TREE_CODE (arg0) == MULT_EXPR
10069 || TREE_CODE (arg1) == MULT_EXPR)
10070 && !TYPE_SATURATING (type)
10071 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10072 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10073 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10074 {
10075 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10076 if (tem)
10077 return tem;
10078 }
10079
10080 if (! FLOAT_TYPE_P (type))
10081 {
10082 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10083 with a constant, and the two constants have no bits in common,
10084 we should treat this as a BIT_IOR_EXPR since this may produce more
10085 simplifications. */
10086 if (TREE_CODE (arg0) == BIT_AND_EXPR
10087 && TREE_CODE (arg1) == BIT_AND_EXPR
10088 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10089 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10090 && wi::bit_and (TREE_OPERAND (arg0, 1),
10091 TREE_OPERAND (arg1, 1)) == 0)
10092 {
10093 code = BIT_IOR_EXPR;
10094 goto bit_ior;
10095 }
10096
10097 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10098 (plus (plus (mult) (mult)) (foo)) so that we can
10099 take advantage of the factoring cases below. */
10100 if (ANY_INTEGRAL_TYPE_P (type)
10101 && TYPE_OVERFLOW_WRAPS (type)
10102 && (((TREE_CODE (arg0) == PLUS_EXPR
10103 || TREE_CODE (arg0) == MINUS_EXPR)
10104 && TREE_CODE (arg1) == MULT_EXPR)
10105 || ((TREE_CODE (arg1) == PLUS_EXPR
10106 || TREE_CODE (arg1) == MINUS_EXPR)
10107 && TREE_CODE (arg0) == MULT_EXPR)))
10108 {
10109 tree parg0, parg1, parg, marg;
10110 enum tree_code pcode;
10111
10112 if (TREE_CODE (arg1) == MULT_EXPR)
10113 parg = arg0, marg = arg1;
10114 else
10115 parg = arg1, marg = arg0;
10116 pcode = TREE_CODE (parg);
10117 parg0 = TREE_OPERAND (parg, 0);
10118 parg1 = TREE_OPERAND (parg, 1);
10119 STRIP_NOPS (parg0);
10120 STRIP_NOPS (parg1);
10121
10122 if (TREE_CODE (parg0) == MULT_EXPR
10123 && TREE_CODE (parg1) != MULT_EXPR)
10124 return fold_build2_loc (loc, pcode, type,
10125 fold_build2_loc (loc, PLUS_EXPR, type,
10126 fold_convert_loc (loc, type,
10127 parg0),
10128 fold_convert_loc (loc, type,
10129 marg)),
10130 fold_convert_loc (loc, type, parg1));
10131 if (TREE_CODE (parg0) != MULT_EXPR
10132 && TREE_CODE (parg1) == MULT_EXPR)
10133 return
10134 fold_build2_loc (loc, PLUS_EXPR, type,
10135 fold_convert_loc (loc, type, parg0),
10136 fold_build2_loc (loc, pcode, type,
10137 fold_convert_loc (loc, type, marg),
10138 fold_convert_loc (loc, type,
10139 parg1)));
10140 }
10141 }
10142 else
10143 {
10144 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10145 to __complex__ ( x, y ). This is not the same for SNaNs or
10146 if signed zeros are involved. */
10147 if (!HONOR_SNANS (element_mode (arg0))
10148 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10149 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10150 {
10151 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10152 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10153 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10154 bool arg0rz = false, arg0iz = false;
10155 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10156 || (arg0i && (arg0iz = real_zerop (arg0i))))
10157 {
10158 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10159 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10160 if (arg0rz && arg1i && real_zerop (arg1i))
10161 {
10162 tree rp = arg1r ? arg1r
10163 : build1 (REALPART_EXPR, rtype, arg1);
10164 tree ip = arg0i ? arg0i
10165 : build1 (IMAGPART_EXPR, rtype, arg0);
10166 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10167 }
10168 else if (arg0iz && arg1r && real_zerop (arg1r))
10169 {
10170 tree rp = arg0r ? arg0r
10171 : build1 (REALPART_EXPR, rtype, arg0);
10172 tree ip = arg1i ? arg1i
10173 : build1 (IMAGPART_EXPR, rtype, arg1);
10174 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10175 }
10176 }
10177 }
10178
10179 if (flag_unsafe_math_optimizations
10180 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10181 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10182 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10183 return tem;
10184
10185 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10186 We associate floats only if the user has specified
10187 -fassociative-math. */
10188 if (flag_associative_math
10189 && TREE_CODE (arg1) == PLUS_EXPR
10190 && TREE_CODE (arg0) != MULT_EXPR)
10191 {
10192 tree tree10 = TREE_OPERAND (arg1, 0);
10193 tree tree11 = TREE_OPERAND (arg1, 1);
10194 if (TREE_CODE (tree11) == MULT_EXPR
10195 && TREE_CODE (tree10) == MULT_EXPR)
10196 {
10197 tree tree0;
10198 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10199 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10200 }
10201 }
10202 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10203 We associate floats only if the user has specified
10204 -fassociative-math. */
10205 if (flag_associative_math
10206 && TREE_CODE (arg0) == PLUS_EXPR
10207 && TREE_CODE (arg1) != MULT_EXPR)
10208 {
10209 tree tree00 = TREE_OPERAND (arg0, 0);
10210 tree tree01 = TREE_OPERAND (arg0, 1);
10211 if (TREE_CODE (tree01) == MULT_EXPR
10212 && TREE_CODE (tree00) == MULT_EXPR)
10213 {
10214 tree tree0;
10215 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10216 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10217 }
10218 }
10219 }
10220
10221 bit_rotate:
10222 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10223 is a rotate of A by C1 bits. */
10224 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10225 is a rotate of A by B bits. */
10226 {
10227 enum tree_code code0, code1;
10228 tree rtype;
10229 code0 = TREE_CODE (arg0);
10230 code1 = TREE_CODE (arg1);
10231 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10232 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10233 && operand_equal_p (TREE_OPERAND (arg0, 0),
10234 TREE_OPERAND (arg1, 0), 0)
10235 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10236 TYPE_UNSIGNED (rtype))
10237 /* Only create rotates in complete modes. Other cases are not
10238 expanded properly. */
10239 && (element_precision (rtype)
10240 == element_precision (TYPE_MODE (rtype))))
10241 {
10242 tree tree01, tree11;
10243 enum tree_code code01, code11;
10244
10245 tree01 = TREE_OPERAND (arg0, 1);
10246 tree11 = TREE_OPERAND (arg1, 1);
10247 STRIP_NOPS (tree01);
10248 STRIP_NOPS (tree11);
10249 code01 = TREE_CODE (tree01);
10250 code11 = TREE_CODE (tree11);
10251 if (code01 == INTEGER_CST
10252 && code11 == INTEGER_CST
10253 && (wi::to_widest (tree01) + wi::to_widest (tree11)
10254 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10255 {
10256 tem = build2_loc (loc, LROTATE_EXPR,
10257 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10258 TREE_OPERAND (arg0, 0),
10259 code0 == LSHIFT_EXPR
10260 ? TREE_OPERAND (arg0, 1)
10261 : TREE_OPERAND (arg1, 1));
10262 return fold_convert_loc (loc, type, tem);
10263 }
10264 else if (code11 == MINUS_EXPR)
10265 {
10266 tree tree110, tree111;
10267 tree110 = TREE_OPERAND (tree11, 0);
10268 tree111 = TREE_OPERAND (tree11, 1);
10269 STRIP_NOPS (tree110);
10270 STRIP_NOPS (tree111);
10271 if (TREE_CODE (tree110) == INTEGER_CST
10272 && 0 == compare_tree_int (tree110,
10273 element_precision
10274 (TREE_TYPE (TREE_OPERAND
10275 (arg0, 0))))
10276 && operand_equal_p (tree01, tree111, 0))
10277 return
10278 fold_convert_loc (loc, type,
10279 build2 ((code0 == LSHIFT_EXPR
10280 ? LROTATE_EXPR
10281 : RROTATE_EXPR),
10282 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10283 TREE_OPERAND (arg0, 0),
10284 TREE_OPERAND (arg0, 1)));
10285 }
10286 else if (code01 == MINUS_EXPR)
10287 {
10288 tree tree010, tree011;
10289 tree010 = TREE_OPERAND (tree01, 0);
10290 tree011 = TREE_OPERAND (tree01, 1);
10291 STRIP_NOPS (tree010);
10292 STRIP_NOPS (tree011);
10293 if (TREE_CODE (tree010) == INTEGER_CST
10294 && 0 == compare_tree_int (tree010,
10295 element_precision
10296 (TREE_TYPE (TREE_OPERAND
10297 (arg0, 0))))
10298 && operand_equal_p (tree11, tree011, 0))
10299 return fold_convert_loc
10300 (loc, type,
10301 build2 ((code0 != LSHIFT_EXPR
10302 ? LROTATE_EXPR
10303 : RROTATE_EXPR),
10304 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10305 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
10306 }
10307 }
10308 }
10309
10310 associate:
10311 /* In most languages, can't associate operations on floats through
10312 parentheses. Rather than remember where the parentheses were, we
10313 don't associate floats at all, unless the user has specified
10314 -fassociative-math.
10315 And, we need to make sure type is not saturating. */
10316
10317 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10318 && !TYPE_SATURATING (type))
10319 {
10320 tree var0, con0, lit0, minus_lit0;
10321 tree var1, con1, lit1, minus_lit1;
10322 tree atype = type;
10323 bool ok = true;
10324
10325 /* Split both trees into variables, constants, and literals. Then
10326 associate each group together, the constants with literals,
10327 then the result with variables. This increases the chances of
10328 literals being recombined later and of generating relocatable
10329 expressions for the sum of a constant and literal. */
10330 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10331 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10332 code == MINUS_EXPR);
10333
10334 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10335 if (code == MINUS_EXPR)
10336 code = PLUS_EXPR;
10337
10338 /* With undefined overflow prefer doing association in a type
10339 which wraps on overflow, if that is one of the operand types. */
10340 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10341 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10342 {
10343 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10344 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10345 atype = TREE_TYPE (arg0);
10346 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10347 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
10348 atype = TREE_TYPE (arg1);
10349 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
10350 }
10351
10352 /* With undefined overflow we can only associate constants with one
10353 variable, and constants whose association doesn't overflow. */
10354 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10355 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
10356 {
10357 if (var0 && var1)
10358 {
10359 tree tmp0 = var0;
10360 tree tmp1 = var1;
10361
10362 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10363 tmp0 = TREE_OPERAND (tmp0, 0);
10364 if (CONVERT_EXPR_P (tmp0)
10365 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10366 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10367 <= TYPE_PRECISION (atype)))
10368 tmp0 = TREE_OPERAND (tmp0, 0);
10369 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10370 tmp1 = TREE_OPERAND (tmp1, 0);
10371 if (CONVERT_EXPR_P (tmp1)
10372 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10373 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10374 <= TYPE_PRECISION (atype)))
10375 tmp1 = TREE_OPERAND (tmp1, 0);
10376 /* The only case we can still associate with two variables
10377 is if they are the same, modulo negation and bit-pattern
10378 preserving conversions. */
10379 if (!operand_equal_p (tmp0, tmp1, 0))
10380 ok = false;
10381 }
10382 }
10383
10384 /* Only do something if we found more than two objects. Otherwise,
10385 nothing has changed and we risk infinite recursion. */
10386 if (ok
10387 && (2 < ((var0 != 0) + (var1 != 0)
10388 + (con0 != 0) + (con1 != 0)
10389 + (lit0 != 0) + (lit1 != 0)
10390 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10391 {
10392 bool any_overflows = false;
10393 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
10394 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
10395 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
10396 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
10397 var0 = associate_trees (loc, var0, var1, code, atype);
10398 con0 = associate_trees (loc, con0, con1, code, atype);
10399 lit0 = associate_trees (loc, lit0, lit1, code, atype);
10400 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
10401 code, atype);
10402
10403 /* Preserve the MINUS_EXPR if the negative part of the literal is
10404 greater than the positive part. Otherwise, the multiplicative
10405 folding code (i.e extract_muldiv) may be fooled in case
10406 unsigned constants are subtracted, like in the following
10407 example: ((X*2 + 4) - 8U)/2. */
10408 if (minus_lit0 && lit0)
10409 {
10410 if (TREE_CODE (lit0) == INTEGER_CST
10411 && TREE_CODE (minus_lit0) == INTEGER_CST
10412 && tree_int_cst_lt (lit0, minus_lit0))
10413 {
10414 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10415 MINUS_EXPR, atype);
10416 lit0 = 0;
10417 }
10418 else
10419 {
10420 lit0 = associate_trees (loc, lit0, minus_lit0,
10421 MINUS_EXPR, atype);
10422 minus_lit0 = 0;
10423 }
10424 }
10425
10426 /* Don't introduce overflows through reassociation. */
10427 if (!any_overflows
10428 && ((lit0 && TREE_OVERFLOW_P (lit0))
10429 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
10430 return NULL_TREE;
10431
10432 if (minus_lit0)
10433 {
10434 if (con0 == 0)
10435 return
10436 fold_convert_loc (loc, type,
10437 associate_trees (loc, var0, minus_lit0,
10438 MINUS_EXPR, atype));
10439 else
10440 {
10441 con0 = associate_trees (loc, con0, minus_lit0,
10442 MINUS_EXPR, atype);
10443 return
10444 fold_convert_loc (loc, type,
10445 associate_trees (loc, var0, con0,
10446 PLUS_EXPR, atype));
10447 }
10448 }
10449
10450 con0 = associate_trees (loc, con0, lit0, code, atype);
10451 return
10452 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10453 code, atype));
10454 }
10455 }
10456
10457 return NULL_TREE;
10458
10459 case MINUS_EXPR:
10460 /* Pointer simplifications for subtraction, simple reassociations. */
10461 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10462 {
10463 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10464 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10465 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10466 {
10467 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10468 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10469 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10470 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10471 return fold_build2_loc (loc, PLUS_EXPR, type,
10472 fold_build2_loc (loc, MINUS_EXPR, type,
10473 arg00, arg10),
10474 fold_build2_loc (loc, MINUS_EXPR, type,
10475 arg01, arg11));
10476 }
10477 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10478 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10479 {
10480 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10481 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10482 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10483 fold_convert_loc (loc, type, arg1));
10484 if (tmp)
10485 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10486 }
10487 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
10488 simplifies. */
10489 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10490 {
10491 tree arg10 = fold_convert_loc (loc, type,
10492 TREE_OPERAND (arg1, 0));
10493 tree arg11 = fold_convert_loc (loc, type,
10494 TREE_OPERAND (arg1, 1));
10495 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
10496 fold_convert_loc (loc, type, arg0),
10497 arg10);
10498 if (tmp)
10499 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
10500 }
10501 }
10502 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10503 if (TREE_CODE (arg0) == NEGATE_EXPR
10504 && negate_expr_p (arg1)
10505 && reorder_operands_p (arg0, arg1))
10506 return fold_build2_loc (loc, MINUS_EXPR, type,
10507 fold_convert_loc (loc, type,
10508 negate_expr (arg1)),
10509 fold_convert_loc (loc, type,
10510 TREE_OPERAND (arg0, 0)));
10511
10512 /* X - (X / Y) * Y is X % Y. */
10513 if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10514 && TREE_CODE (arg1) == MULT_EXPR
10515 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10516 && operand_equal_p (arg0,
10517 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10518 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10519 TREE_OPERAND (arg1, 1), 0))
10520 return
10521 fold_convert_loc (loc, type,
10522 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10523 arg0, TREE_OPERAND (arg1, 1)));
10524
10525 if (! FLOAT_TYPE_P (type))
10526 {
10527 /* Fold A - (A & B) into ~B & A. */
10528 if (!TREE_SIDE_EFFECTS (arg0)
10529 && TREE_CODE (arg1) == BIT_AND_EXPR)
10530 {
10531 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10532 {
10533 tree arg10 = fold_convert_loc (loc, type,
10534 TREE_OPERAND (arg1, 0));
10535 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10536 fold_build1_loc (loc, BIT_NOT_EXPR,
10537 type, arg10),
10538 fold_convert_loc (loc, type, arg0));
10539 }
10540 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10541 {
10542 tree arg11 = fold_convert_loc (loc,
10543 type, TREE_OPERAND (arg1, 1));
10544 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10545 fold_build1_loc (loc, BIT_NOT_EXPR,
10546 type, arg11),
10547 fold_convert_loc (loc, type, arg0));
10548 }
10549 }
10550
10551 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10552 any power of 2 minus 1. */
10553 if (TREE_CODE (arg0) == BIT_AND_EXPR
10554 && TREE_CODE (arg1) == BIT_AND_EXPR
10555 && operand_equal_p (TREE_OPERAND (arg0, 0),
10556 TREE_OPERAND (arg1, 0), 0))
10557 {
10558 tree mask0 = TREE_OPERAND (arg0, 1);
10559 tree mask1 = TREE_OPERAND (arg1, 1);
10560 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10561
10562 if (operand_equal_p (tem, mask1, 0))
10563 {
10564 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10565 TREE_OPERAND (arg0, 0), mask1);
10566 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10567 }
10568 }
10569 }
10570
10571 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10572 __complex__ ( x, -y ). This is not the same for SNaNs or if
10573 signed zeros are involved. */
10574 if (!HONOR_SNANS (element_mode (arg0))
10575 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10576 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10577 {
10578 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10579 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10580 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10581 bool arg0rz = false, arg0iz = false;
10582 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10583 || (arg0i && (arg0iz = real_zerop (arg0i))))
10584 {
10585 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10586 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10587 if (arg0rz && arg1i && real_zerop (arg1i))
10588 {
10589 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10590 arg1r ? arg1r
10591 : build1 (REALPART_EXPR, rtype, arg1));
10592 tree ip = arg0i ? arg0i
10593 : build1 (IMAGPART_EXPR, rtype, arg0);
10594 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10595 }
10596 else if (arg0iz && arg1r && real_zerop (arg1r))
10597 {
10598 tree rp = arg0r ? arg0r
10599 : build1 (REALPART_EXPR, rtype, arg0);
10600 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10601 arg1i ? arg1i
10602 : build1 (IMAGPART_EXPR, rtype, arg1));
10603 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10604 }
10605 }
10606 }
10607
10608 /* A - B -> A + (-B) if B is easily negatable. */
10609 if (negate_expr_p (arg1)
10610 && !TYPE_OVERFLOW_SANITIZED (type)
10611 && ((FLOAT_TYPE_P (type)
10612 /* Avoid this transformation if B is a positive REAL_CST. */
10613 && (TREE_CODE (arg1) != REAL_CST
10614 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10615 || INTEGRAL_TYPE_P (type)))
10616 return fold_build2_loc (loc, PLUS_EXPR, type,
10617 fold_convert_loc (loc, type, arg0),
10618 fold_convert_loc (loc, type,
10619 negate_expr (arg1)));
10620
10621 /* Fold &a[i] - &a[j] to i-j. */
10622 if (TREE_CODE (arg0) == ADDR_EXPR
10623 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10624 && TREE_CODE (arg1) == ADDR_EXPR
10625 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10626 {
10627 tree tem = fold_addr_of_array_ref_difference (loc, type,
10628 TREE_OPERAND (arg0, 0),
10629 TREE_OPERAND (arg1, 0));
10630 if (tem)
10631 return tem;
10632 }
10633
10634 if (FLOAT_TYPE_P (type)
10635 && flag_unsafe_math_optimizations
10636 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10637 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10638 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10639 return tem;
10640
10641 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10642 one. Make sure the type is not saturating and has the signedness of
10643 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10644 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10645 if ((TREE_CODE (arg0) == MULT_EXPR
10646 || TREE_CODE (arg1) == MULT_EXPR)
10647 && !TYPE_SATURATING (type)
10648 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10649 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10650 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10651 {
10652 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10653 if (tem)
10654 return tem;
10655 }
10656
10657 goto associate;
10658
10659 case MULT_EXPR:
10660 /* (-A) * (-B) -> A * B */
10661 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10662 return fold_build2_loc (loc, MULT_EXPR, type,
10663 fold_convert_loc (loc, type,
10664 TREE_OPERAND (arg0, 0)),
10665 fold_convert_loc (loc, type,
10666 negate_expr (arg1)));
10667 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10668 return fold_build2_loc (loc, MULT_EXPR, type,
10669 fold_convert_loc (loc, type,
10670 negate_expr (arg0)),
10671 fold_convert_loc (loc, type,
10672 TREE_OPERAND (arg1, 0)));
10673
10674 if (! FLOAT_TYPE_P (type))
10675 {
10676 /* Transform x * -C into -x * C if x is easily negatable. */
10677 if (TREE_CODE (arg1) == INTEGER_CST
10678 && tree_int_cst_sgn (arg1) == -1
10679 && negate_expr_p (arg0)
10680 && (tem = negate_expr (arg1)) != arg1
10681 && !TREE_OVERFLOW (tem))
10682 return fold_build2_loc (loc, MULT_EXPR, type,
10683 fold_convert_loc (loc, type,
10684 negate_expr (arg0)),
10685 tem);
10686
10687 /* (a * (1 << b)) is (a << b) */
10688 if (TREE_CODE (arg1) == LSHIFT_EXPR
10689 && integer_onep (TREE_OPERAND (arg1, 0)))
10690 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10691 TREE_OPERAND (arg1, 1));
10692 if (TREE_CODE (arg0) == LSHIFT_EXPR
10693 && integer_onep (TREE_OPERAND (arg0, 0)))
10694 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10695 TREE_OPERAND (arg0, 1));
10696
10697 /* (A + A) * C -> A * 2 * C */
10698 if (TREE_CODE (arg0) == PLUS_EXPR
10699 && TREE_CODE (arg1) == INTEGER_CST
10700 && operand_equal_p (TREE_OPERAND (arg0, 0),
10701 TREE_OPERAND (arg0, 1), 0))
10702 return fold_build2_loc (loc, MULT_EXPR, type,
10703 omit_one_operand_loc (loc, type,
10704 TREE_OPERAND (arg0, 0),
10705 TREE_OPERAND (arg0, 1)),
10706 fold_build2_loc (loc, MULT_EXPR, type,
10707 build_int_cst (type, 2) , arg1));
10708
10709 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10710 sign-changing only. */
10711 if (TREE_CODE (arg1) == INTEGER_CST
10712 && TREE_CODE (arg0) == EXACT_DIV_EXPR
10713 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
10714 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10715
10716 strict_overflow_p = false;
10717 if (TREE_CODE (arg1) == INTEGER_CST
10718 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10719 &strict_overflow_p)))
10720 {
10721 if (strict_overflow_p)
10722 fold_overflow_warning (("assuming signed overflow does not "
10723 "occur when simplifying "
10724 "multiplication"),
10725 WARN_STRICT_OVERFLOW_MISC);
10726 return fold_convert_loc (loc, type, tem);
10727 }
10728
10729 /* Optimize z * conj(z) for integer complex numbers. */
10730 if (TREE_CODE (arg0) == CONJ_EXPR
10731 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10732 return fold_mult_zconjz (loc, type, arg1);
10733 if (TREE_CODE (arg1) == CONJ_EXPR
10734 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10735 return fold_mult_zconjz (loc, type, arg0);
10736 }
10737 else
10738 {
10739 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10740 the result for floating point types due to rounding so it is applied
10741 only if -fassociative-math was specify. */
10742 if (flag_associative_math
10743 && TREE_CODE (arg0) == RDIV_EXPR
10744 && TREE_CODE (arg1) == REAL_CST
10745 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10746 {
10747 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10748 arg1);
10749 if (tem)
10750 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10751 TREE_OPERAND (arg0, 1));
10752 }
10753
10754 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10755 if (operand_equal_p (arg0, arg1, 0))
10756 {
10757 tree tem = fold_strip_sign_ops (arg0);
10758 if (tem != NULL_TREE)
10759 {
10760 tem = fold_convert_loc (loc, type, tem);
10761 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10762 }
10763 }
10764
10765 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10766 This is not the same for NaNs or if signed zeros are
10767 involved. */
10768 if (!HONOR_NANS (arg0)
10769 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10770 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10771 && TREE_CODE (arg1) == COMPLEX_CST
10772 && real_zerop (TREE_REALPART (arg1)))
10773 {
10774 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10775 if (real_onep (TREE_IMAGPART (arg1)))
10776 return
10777 fold_build2_loc (loc, COMPLEX_EXPR, type,
10778 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10779 rtype, arg0)),
10780 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10781 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10782 return
10783 fold_build2_loc (loc, COMPLEX_EXPR, type,
10784 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10785 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10786 rtype, arg0)));
10787 }
10788
10789 /* Optimize z * conj(z) for floating point complex numbers.
10790 Guarded by flag_unsafe_math_optimizations as non-finite
10791 imaginary components don't produce scalar results. */
10792 if (flag_unsafe_math_optimizations
10793 && TREE_CODE (arg0) == CONJ_EXPR
10794 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10795 return fold_mult_zconjz (loc, type, arg1);
10796 if (flag_unsafe_math_optimizations
10797 && TREE_CODE (arg1) == CONJ_EXPR
10798 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10799 return fold_mult_zconjz (loc, type, arg0);
10800
10801 if (flag_unsafe_math_optimizations)
10802 {
10803 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10804 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10805
10806 /* Optimizations of root(...)*root(...). */
10807 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10808 {
10809 tree rootfn, arg;
10810 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10811 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10812
10813 /* Optimize sqrt(x)*sqrt(x) as x. */
10814 if (BUILTIN_SQRT_P (fcode0)
10815 && operand_equal_p (arg00, arg10, 0)
10816 && ! HONOR_SNANS (element_mode (type)))
10817 return arg00;
10818
10819 /* Optimize root(x)*root(y) as root(x*y). */
10820 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10821 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10822 return build_call_expr_loc (loc, rootfn, 1, arg);
10823 }
10824
10825 /* Optimize expN(x)*expN(y) as expN(x+y). */
10826 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10827 {
10828 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10829 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10830 CALL_EXPR_ARG (arg0, 0),
10831 CALL_EXPR_ARG (arg1, 0));
10832 return build_call_expr_loc (loc, expfn, 1, arg);
10833 }
10834
10835 /* Optimizations of pow(...)*pow(...). */
10836 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10837 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10838 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10839 {
10840 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10841 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10842 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10843 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10844
10845 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10846 if (operand_equal_p (arg01, arg11, 0))
10847 {
10848 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10849 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10850 arg00, arg10);
10851 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10852 }
10853
10854 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10855 if (operand_equal_p (arg00, arg10, 0))
10856 {
10857 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10858 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10859 arg01, arg11);
10860 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10861 }
10862 }
10863
10864 /* Optimize tan(x)*cos(x) as sin(x). */
10865 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10866 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10867 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10868 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10869 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10870 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10871 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10872 CALL_EXPR_ARG (arg1, 0), 0))
10873 {
10874 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10875
10876 if (sinfn != NULL_TREE)
10877 return build_call_expr_loc (loc, sinfn, 1,
10878 CALL_EXPR_ARG (arg0, 0));
10879 }
10880
10881 /* Optimize x*pow(x,c) as pow(x,c+1). */
10882 if (fcode1 == BUILT_IN_POW
10883 || fcode1 == BUILT_IN_POWF
10884 || fcode1 == BUILT_IN_POWL)
10885 {
10886 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10887 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10888 if (TREE_CODE (arg11) == REAL_CST
10889 && !TREE_OVERFLOW (arg11)
10890 && operand_equal_p (arg0, arg10, 0))
10891 {
10892 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10893 REAL_VALUE_TYPE c;
10894 tree arg;
10895
10896 c = TREE_REAL_CST (arg11);
10897 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10898 arg = build_real (type, c);
10899 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10900 }
10901 }
10902
10903 /* Optimize pow(x,c)*x as pow(x,c+1). */
10904 if (fcode0 == BUILT_IN_POW
10905 || fcode0 == BUILT_IN_POWF
10906 || fcode0 == BUILT_IN_POWL)
10907 {
10908 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10909 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10910 if (TREE_CODE (arg01) == REAL_CST
10911 && !TREE_OVERFLOW (arg01)
10912 && operand_equal_p (arg1, arg00, 0))
10913 {
10914 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10915 REAL_VALUE_TYPE c;
10916 tree arg;
10917
10918 c = TREE_REAL_CST (arg01);
10919 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10920 arg = build_real (type, c);
10921 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10922 }
10923 }
10924
10925 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10926 if (!in_gimple_form
10927 && optimize
10928 && operand_equal_p (arg0, arg1, 0))
10929 {
10930 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10931
10932 if (powfn)
10933 {
10934 tree arg = build_real (type, dconst2);
10935 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10936 }
10937 }
10938 }
10939 }
10940 goto associate;
10941
10942 case BIT_IOR_EXPR:
10943 bit_ior:
10944 /* ~X | X is -1. */
10945 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10946 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10947 {
10948 t1 = build_zero_cst (type);
10949 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10950 return omit_one_operand_loc (loc, type, t1, arg1);
10951 }
10952
10953 /* X | ~X is -1. */
10954 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10955 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10956 {
10957 t1 = build_zero_cst (type);
10958 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10959 return omit_one_operand_loc (loc, type, t1, arg0);
10960 }
10961
10962 /* Canonicalize (X & C1) | C2. */
10963 if (TREE_CODE (arg0) == BIT_AND_EXPR
10964 && TREE_CODE (arg1) == INTEGER_CST
10965 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10966 {
10967 int width = TYPE_PRECISION (type), w;
10968 wide_int c1 = TREE_OPERAND (arg0, 1);
10969 wide_int c2 = arg1;
10970
10971 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10972 if ((c1 & c2) == c1)
10973 return omit_one_operand_loc (loc, type, arg1,
10974 TREE_OPERAND (arg0, 0));
10975
10976 wide_int msk = wi::mask (width, false,
10977 TYPE_PRECISION (TREE_TYPE (arg1)));
10978
10979 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10980 if (msk.and_not (c1 | c2) == 0)
10981 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10982 TREE_OPERAND (arg0, 0), arg1);
10983
10984 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10985 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10986 mode which allows further optimizations. */
10987 c1 &= msk;
10988 c2 &= msk;
10989 wide_int c3 = c1.and_not (c2);
10990 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10991 {
10992 wide_int mask = wi::mask (w, false,
10993 TYPE_PRECISION (type));
10994 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10995 {
10996 c3 = mask;
10997 break;
10998 }
10999 }
11000
11001 if (c3 != c1)
11002 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11003 fold_build2_loc (loc, BIT_AND_EXPR, type,
11004 TREE_OPERAND (arg0, 0),
11005 wide_int_to_tree (type,
11006 c3)),
11007 arg1);
11008 }
11009
11010 /* (X & ~Y) | (~X & Y) is X ^ Y */
11011 if (TREE_CODE (arg0) == BIT_AND_EXPR
11012 && TREE_CODE (arg1) == BIT_AND_EXPR)
11013 {
11014 tree a0, a1, l0, l1, n0, n1;
11015
11016 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11017 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11018
11019 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11020 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11021
11022 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11023 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11024
11025 if ((operand_equal_p (n0, a0, 0)
11026 && operand_equal_p (n1, a1, 0))
11027 || (operand_equal_p (n0, a1, 0)
11028 && operand_equal_p (n1, a0, 0)))
11029 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11030 }
11031
11032 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11033 if (t1 != NULL_TREE)
11034 return t1;
11035
11036 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11037
11038 This results in more efficient code for machines without a NAND
11039 instruction. Combine will canonicalize to the first form
11040 which will allow use of NAND instructions provided by the
11041 backend if they exist. */
11042 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11043 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11044 {
11045 return
11046 fold_build1_loc (loc, BIT_NOT_EXPR, type,
11047 build2 (BIT_AND_EXPR, type,
11048 fold_convert_loc (loc, type,
11049 TREE_OPERAND (arg0, 0)),
11050 fold_convert_loc (loc, type,
11051 TREE_OPERAND (arg1, 0))));
11052 }
11053
11054 /* See if this can be simplified into a rotate first. If that
11055 is unsuccessful continue in the association code. */
11056 goto bit_rotate;
11057
11058 case BIT_XOR_EXPR:
11059 /* ~X ^ X is -1. */
11060 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11061 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11062 {
11063 t1 = build_zero_cst (type);
11064 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11065 return omit_one_operand_loc (loc, type, t1, arg1);
11066 }
11067
11068 /* X ^ ~X is -1. */
11069 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11070 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11071 {
11072 t1 = build_zero_cst (type);
11073 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11074 return omit_one_operand_loc (loc, type, t1, arg0);
11075 }
11076
11077 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11078 with a constant, and the two constants have no bits in common,
11079 we should treat this as a BIT_IOR_EXPR since this may produce more
11080 simplifications. */
11081 if (TREE_CODE (arg0) == BIT_AND_EXPR
11082 && TREE_CODE (arg1) == BIT_AND_EXPR
11083 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11084 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11085 && wi::bit_and (TREE_OPERAND (arg0, 1),
11086 TREE_OPERAND (arg1, 1)) == 0)
11087 {
11088 code = BIT_IOR_EXPR;
11089 goto bit_ior;
11090 }
11091
11092 /* (X | Y) ^ X -> Y & ~ X*/
11093 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11094 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11095 {
11096 tree t2 = TREE_OPERAND (arg0, 1);
11097 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11098 arg1);
11099 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11100 fold_convert_loc (loc, type, t2),
11101 fold_convert_loc (loc, type, t1));
11102 return t1;
11103 }
11104
11105 /* (Y | X) ^ X -> Y & ~ X*/
11106 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11107 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11108 {
11109 tree t2 = TREE_OPERAND (arg0, 0);
11110 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11111 arg1);
11112 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11113 fold_convert_loc (loc, type, t2),
11114 fold_convert_loc (loc, type, t1));
11115 return t1;
11116 }
11117
11118 /* X ^ (X | Y) -> Y & ~ X*/
11119 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11120 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11121 {
11122 tree t2 = TREE_OPERAND (arg1, 1);
11123 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11124 arg0);
11125 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11126 fold_convert_loc (loc, type, t2),
11127 fold_convert_loc (loc, type, t1));
11128 return t1;
11129 }
11130
11131 /* X ^ (Y | X) -> Y & ~ X*/
11132 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11133 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11134 {
11135 tree t2 = TREE_OPERAND (arg1, 0);
11136 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11137 arg0);
11138 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11139 fold_convert_loc (loc, type, t2),
11140 fold_convert_loc (loc, type, t1));
11141 return t1;
11142 }
11143
11144 /* Convert ~X ^ ~Y to X ^ Y. */
11145 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11146 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11147 return fold_build2_loc (loc, code, type,
11148 fold_convert_loc (loc, type,
11149 TREE_OPERAND (arg0, 0)),
11150 fold_convert_loc (loc, type,
11151 TREE_OPERAND (arg1, 0)));
11152
11153 /* Convert ~X ^ C to X ^ ~C. */
11154 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11155 && TREE_CODE (arg1) == INTEGER_CST)
11156 return fold_build2_loc (loc, code, type,
11157 fold_convert_loc (loc, type,
11158 TREE_OPERAND (arg0, 0)),
11159 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11160
11161 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11162 if (TREE_CODE (arg0) == BIT_AND_EXPR
11163 && INTEGRAL_TYPE_P (type)
11164 && integer_onep (TREE_OPERAND (arg0, 1))
11165 && integer_onep (arg1))
11166 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11167 build_zero_cst (TREE_TYPE (arg0)));
11168
11169 /* Fold (X & Y) ^ Y as ~X & Y. */
11170 if (TREE_CODE (arg0) == BIT_AND_EXPR
11171 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11172 {
11173 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11174 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11175 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11176 fold_convert_loc (loc, type, arg1));
11177 }
11178 /* Fold (X & Y) ^ X as ~Y & X. */
11179 if (TREE_CODE (arg0) == BIT_AND_EXPR
11180 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11181 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11182 {
11183 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11184 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11185 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11186 fold_convert_loc (loc, type, arg1));
11187 }
11188 /* Fold X ^ (X & Y) as X & ~Y. */
11189 if (TREE_CODE (arg1) == BIT_AND_EXPR
11190 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11191 {
11192 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11193 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11194 fold_convert_loc (loc, type, arg0),
11195 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11196 }
11197 /* Fold X ^ (Y & X) as ~Y & X. */
11198 if (TREE_CODE (arg1) == BIT_AND_EXPR
11199 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11200 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11201 {
11202 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11203 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11204 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11205 fold_convert_loc (loc, type, arg0));
11206 }
11207
11208 /* See if this can be simplified into a rotate first. If that
11209 is unsuccessful continue in the association code. */
11210 goto bit_rotate;
11211
11212 case BIT_AND_EXPR:
11213 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11214 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11215 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11216 || (TREE_CODE (arg0) == EQ_EXPR
11217 && integer_zerop (TREE_OPERAND (arg0, 1))))
11218 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11219 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11220
11221 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11222 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11223 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11224 || (TREE_CODE (arg1) == EQ_EXPR
11225 && integer_zerop (TREE_OPERAND (arg1, 1))))
11226 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11227 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11228
11229 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11230 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11231 && INTEGRAL_TYPE_P (type)
11232 && integer_onep (TREE_OPERAND (arg0, 1))
11233 && integer_onep (arg1))
11234 {
11235 tree tem2;
11236 tem = TREE_OPERAND (arg0, 0);
11237 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11238 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11239 tem, tem2);
11240 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11241 build_zero_cst (TREE_TYPE (tem)));
11242 }
11243 /* Fold ~X & 1 as (X & 1) == 0. */
11244 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11245 && INTEGRAL_TYPE_P (type)
11246 && integer_onep (arg1))
11247 {
11248 tree tem2;
11249 tem = TREE_OPERAND (arg0, 0);
11250 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11251 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11252 tem, tem2);
11253 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11254 build_zero_cst (TREE_TYPE (tem)));
11255 }
11256 /* Fold !X & 1 as X == 0. */
11257 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11258 && integer_onep (arg1))
11259 {
11260 tem = TREE_OPERAND (arg0, 0);
11261 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11262 build_zero_cst (TREE_TYPE (tem)));
11263 }
11264
11265 /* Fold (X ^ Y) & Y as ~X & Y. */
11266 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11267 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11268 {
11269 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11270 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11271 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11272 fold_convert_loc (loc, type, arg1));
11273 }
11274 /* Fold (X ^ Y) & X as ~Y & X. */
11275 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11276 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11277 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11278 {
11279 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11280 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11281 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11282 fold_convert_loc (loc, type, arg1));
11283 }
11284 /* Fold X & (X ^ Y) as X & ~Y. */
11285 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11286 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11287 {
11288 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11289 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11290 fold_convert_loc (loc, type, arg0),
11291 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11292 }
11293 /* Fold X & (Y ^ X) as ~Y & X. */
11294 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11295 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11296 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11297 {
11298 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11299 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11300 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11301 fold_convert_loc (loc, type, arg0));
11302 }
11303
11304 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11305 multiple of 1 << CST. */
11306 if (TREE_CODE (arg1) == INTEGER_CST)
11307 {
11308 wide_int cst1 = arg1;
11309 wide_int ncst1 = -cst1;
11310 if ((cst1 & ncst1) == ncst1
11311 && multiple_of_p (type, arg0,
11312 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11313 return fold_convert_loc (loc, type, arg0);
11314 }
11315
11316 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11317 bits from CST2. */
11318 if (TREE_CODE (arg1) == INTEGER_CST
11319 && TREE_CODE (arg0) == MULT_EXPR
11320 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11321 {
11322 wide_int warg1 = arg1;
11323 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
11324
11325 if (masked == 0)
11326 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11327 arg0, arg1);
11328 else if (masked != warg1)
11329 {
11330 /* Avoid the transform if arg1 is a mask of some
11331 mode which allows further optimizations. */
11332 int pop = wi::popcount (warg1);
11333 if (!(pop >= BITS_PER_UNIT
11334 && exact_log2 (pop) != -1
11335 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11336 return fold_build2_loc (loc, code, type, op0,
11337 wide_int_to_tree (type, masked));
11338 }
11339 }
11340
11341 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11342 ((A & N) + B) & M -> (A + B) & M
11343 Similarly if (N & M) == 0,
11344 ((A | N) + B) & M -> (A + B) & M
11345 and for - instead of + (or unary - instead of +)
11346 and/or ^ instead of |.
11347 If B is constant and (B & M) == 0, fold into A & M. */
11348 if (TREE_CODE (arg1) == INTEGER_CST)
11349 {
11350 wide_int cst1 = arg1;
11351 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
11352 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11353 && (TREE_CODE (arg0) == PLUS_EXPR
11354 || TREE_CODE (arg0) == MINUS_EXPR
11355 || TREE_CODE (arg0) == NEGATE_EXPR)
11356 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11357 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11358 {
11359 tree pmop[2];
11360 int which = 0;
11361 wide_int cst0;
11362
11363 /* Now we know that arg0 is (C + D) or (C - D) or
11364 -C and arg1 (M) is == (1LL << cst) - 1.
11365 Store C into PMOP[0] and D into PMOP[1]. */
11366 pmop[0] = TREE_OPERAND (arg0, 0);
11367 pmop[1] = NULL;
11368 if (TREE_CODE (arg0) != NEGATE_EXPR)
11369 {
11370 pmop[1] = TREE_OPERAND (arg0, 1);
11371 which = 1;
11372 }
11373
11374 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
11375 which = -1;
11376
11377 for (; which >= 0; which--)
11378 switch (TREE_CODE (pmop[which]))
11379 {
11380 case BIT_AND_EXPR:
11381 case BIT_IOR_EXPR:
11382 case BIT_XOR_EXPR:
11383 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11384 != INTEGER_CST)
11385 break;
11386 cst0 = TREE_OPERAND (pmop[which], 1);
11387 cst0 &= cst1;
11388 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11389 {
11390 if (cst0 != cst1)
11391 break;
11392 }
11393 else if (cst0 != 0)
11394 break;
11395 /* If C or D is of the form (A & N) where
11396 (N & M) == M, or of the form (A | N) or
11397 (A ^ N) where (N & M) == 0, replace it with A. */
11398 pmop[which] = TREE_OPERAND (pmop[which], 0);
11399 break;
11400 case INTEGER_CST:
11401 /* If C or D is a N where (N & M) == 0, it can be
11402 omitted (assumed 0). */
11403 if ((TREE_CODE (arg0) == PLUS_EXPR
11404 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11405 && (cst1 & pmop[which]) == 0)
11406 pmop[which] = NULL;
11407 break;
11408 default:
11409 break;
11410 }
11411
11412 /* Only build anything new if we optimized one or both arguments
11413 above. */
11414 if (pmop[0] != TREE_OPERAND (arg0, 0)
11415 || (TREE_CODE (arg0) != NEGATE_EXPR
11416 && pmop[1] != TREE_OPERAND (arg0, 1)))
11417 {
11418 tree utype = TREE_TYPE (arg0);
11419 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11420 {
11421 /* Perform the operations in a type that has defined
11422 overflow behavior. */
11423 utype = unsigned_type_for (TREE_TYPE (arg0));
11424 if (pmop[0] != NULL)
11425 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11426 if (pmop[1] != NULL)
11427 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11428 }
11429
11430 if (TREE_CODE (arg0) == NEGATE_EXPR)
11431 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11432 else if (TREE_CODE (arg0) == PLUS_EXPR)
11433 {
11434 if (pmop[0] != NULL && pmop[1] != NULL)
11435 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11436 pmop[0], pmop[1]);
11437 else if (pmop[0] != NULL)
11438 tem = pmop[0];
11439 else if (pmop[1] != NULL)
11440 tem = pmop[1];
11441 else
11442 return build_int_cst (type, 0);
11443 }
11444 else if (pmop[0] == NULL)
11445 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11446 else
11447 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11448 pmop[0], pmop[1]);
11449 /* TEM is now the new binary +, - or unary - replacement. */
11450 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11451 fold_convert_loc (loc, utype, arg1));
11452 return fold_convert_loc (loc, type, tem);
11453 }
11454 }
11455 }
11456
11457 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11458 if (t1 != NULL_TREE)
11459 return t1;
11460 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11461 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11462 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11463 {
11464 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11465
11466 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
11467 if (mask == -1)
11468 return
11469 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11470 }
11471
11472 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11473
11474 This results in more efficient code for machines without a NOR
11475 instruction. Combine will canonicalize to the first form
11476 which will allow use of NOR instructions provided by the
11477 backend if they exist. */
11478 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11479 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11480 {
11481 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11482 build2 (BIT_IOR_EXPR, type,
11483 fold_convert_loc (loc, type,
11484 TREE_OPERAND (arg0, 0)),
11485 fold_convert_loc (loc, type,
11486 TREE_OPERAND (arg1, 0))));
11487 }
11488
11489 /* If arg0 is derived from the address of an object or function, we may
11490 be able to fold this expression using the object or function's
11491 alignment. */
11492 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && tree_fits_uhwi_p (arg1))
11493 {
11494 unsigned HOST_WIDE_INT modulus, residue;
11495 unsigned HOST_WIDE_INT low = tree_to_uhwi (arg1);
11496
11497 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11498 integer_onep (arg1));
11499
11500 /* This works because modulus is a power of 2. If this weren't the
11501 case, we'd have to replace it by its greatest power-of-2
11502 divisor: modulus & -modulus. */
11503 if (low < modulus)
11504 return build_int_cst (type, residue & low);
11505 }
11506
11507 goto associate;
11508
11509 case RDIV_EXPR:
11510 /* Don't touch a floating-point divide by zero unless the mode
11511 of the constant can represent infinity. */
11512 if (TREE_CODE (arg1) == REAL_CST
11513 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11514 && real_zerop (arg1))
11515 return NULL_TREE;
11516
11517 /* (-A) / (-B) -> A / B */
11518 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11519 return fold_build2_loc (loc, RDIV_EXPR, type,
11520 TREE_OPERAND (arg0, 0),
11521 negate_expr (arg1));
11522 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11523 return fold_build2_loc (loc, RDIV_EXPR, type,
11524 negate_expr (arg0),
11525 TREE_OPERAND (arg1, 0));
11526
11527 /* Convert A/B/C to A/(B*C). */
11528 if (flag_reciprocal_math
11529 && TREE_CODE (arg0) == RDIV_EXPR)
11530 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11531 fold_build2_loc (loc, MULT_EXPR, type,
11532 TREE_OPERAND (arg0, 1), arg1));
11533
11534 /* Convert A/(B/C) to (A/B)*C. */
11535 if (flag_reciprocal_math
11536 && TREE_CODE (arg1) == RDIV_EXPR)
11537 return fold_build2_loc (loc, MULT_EXPR, type,
11538 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11539 TREE_OPERAND (arg1, 0)),
11540 TREE_OPERAND (arg1, 1));
11541
11542 /* Convert C1/(X*C2) into (C1/C2)/X. */
11543 if (flag_reciprocal_math
11544 && TREE_CODE (arg1) == MULT_EXPR
11545 && TREE_CODE (arg0) == REAL_CST
11546 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11547 {
11548 tree tem = const_binop (RDIV_EXPR, arg0,
11549 TREE_OPERAND (arg1, 1));
11550 if (tem)
11551 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11552 TREE_OPERAND (arg1, 0));
11553 }
11554
11555 if (flag_unsafe_math_optimizations)
11556 {
11557 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11558 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11559
11560 /* Optimize sin(x)/cos(x) as tan(x). */
11561 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11562 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11563 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11564 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11565 CALL_EXPR_ARG (arg1, 0), 0))
11566 {
11567 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11568
11569 if (tanfn != NULL_TREE)
11570 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11571 }
11572
11573 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11574 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11575 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11576 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11577 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11578 CALL_EXPR_ARG (arg1, 0), 0))
11579 {
11580 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11581
11582 if (tanfn != NULL_TREE)
11583 {
11584 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11585 CALL_EXPR_ARG (arg0, 0));
11586 return fold_build2_loc (loc, RDIV_EXPR, type,
11587 build_real (type, dconst1), tmp);
11588 }
11589 }
11590
11591 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11592 NaNs or Infinities. */
11593 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11594 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11595 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11596 {
11597 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11598 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11599
11600 if (! HONOR_NANS (arg00)
11601 && ! HONOR_INFINITIES (element_mode (arg00))
11602 && operand_equal_p (arg00, arg01, 0))
11603 {
11604 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11605
11606 if (cosfn != NULL_TREE)
11607 return build_call_expr_loc (loc, cosfn, 1, arg00);
11608 }
11609 }
11610
11611 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11612 NaNs or Infinities. */
11613 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11614 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11615 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11616 {
11617 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11618 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11619
11620 if (! HONOR_NANS (arg00)
11621 && ! HONOR_INFINITIES (element_mode (arg00))
11622 && operand_equal_p (arg00, arg01, 0))
11623 {
11624 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11625
11626 if (cosfn != NULL_TREE)
11627 {
11628 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11629 return fold_build2_loc (loc, RDIV_EXPR, type,
11630 build_real (type, dconst1),
11631 tmp);
11632 }
11633 }
11634 }
11635
11636 /* Optimize pow(x,c)/x as pow(x,c-1). */
11637 if (fcode0 == BUILT_IN_POW
11638 || fcode0 == BUILT_IN_POWF
11639 || fcode0 == BUILT_IN_POWL)
11640 {
11641 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11642 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11643 if (TREE_CODE (arg01) == REAL_CST
11644 && !TREE_OVERFLOW (arg01)
11645 && operand_equal_p (arg1, arg00, 0))
11646 {
11647 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11648 REAL_VALUE_TYPE c;
11649 tree arg;
11650
11651 c = TREE_REAL_CST (arg01);
11652 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11653 arg = build_real (type, c);
11654 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11655 }
11656 }
11657
11658 /* Optimize a/root(b/c) into a*root(c/b). */
11659 if (BUILTIN_ROOT_P (fcode1))
11660 {
11661 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11662
11663 if (TREE_CODE (rootarg) == RDIV_EXPR)
11664 {
11665 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11666 tree b = TREE_OPERAND (rootarg, 0);
11667 tree c = TREE_OPERAND (rootarg, 1);
11668
11669 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11670
11671 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11672 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11673 }
11674 }
11675
11676 /* Optimize x/expN(y) into x*expN(-y). */
11677 if (BUILTIN_EXPONENT_P (fcode1))
11678 {
11679 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11680 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11681 arg1 = build_call_expr_loc (loc,
11682 expfn, 1,
11683 fold_convert_loc (loc, type, arg));
11684 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11685 }
11686
11687 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11688 if (fcode1 == BUILT_IN_POW
11689 || fcode1 == BUILT_IN_POWF
11690 || fcode1 == BUILT_IN_POWL)
11691 {
11692 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11693 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11694 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11695 tree neg11 = fold_convert_loc (loc, type,
11696 negate_expr (arg11));
11697 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11698 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11699 }
11700 }
11701 return NULL_TREE;
11702
11703 case TRUNC_DIV_EXPR:
11704 /* Optimize (X & (-A)) / A where A is a power of 2,
11705 to X >> log2(A) */
11706 if (TREE_CODE (arg0) == BIT_AND_EXPR
11707 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11708 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11709 {
11710 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11711 arg1, TREE_OPERAND (arg0, 1));
11712 if (sum && integer_zerop (sum)) {
11713 tree pow2 = build_int_cst (integer_type_node,
11714 wi::exact_log2 (arg1));
11715 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11716 TREE_OPERAND (arg0, 0), pow2);
11717 }
11718 }
11719
11720 /* Fall through */
11721
11722 case FLOOR_DIV_EXPR:
11723 /* Simplify A / (B << N) where A and B are positive and B is
11724 a power of 2, to A >> (N + log2(B)). */
11725 strict_overflow_p = false;
11726 if (TREE_CODE (arg1) == LSHIFT_EXPR
11727 && (TYPE_UNSIGNED (type)
11728 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11729 {
11730 tree sval = TREE_OPERAND (arg1, 0);
11731 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11732 {
11733 tree sh_cnt = TREE_OPERAND (arg1, 1);
11734 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11735 wi::exact_log2 (sval));
11736
11737 if (strict_overflow_p)
11738 fold_overflow_warning (("assuming signed overflow does not "
11739 "occur when simplifying A / (B << N)"),
11740 WARN_STRICT_OVERFLOW_MISC);
11741
11742 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11743 sh_cnt, pow2);
11744 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11745 fold_convert_loc (loc, type, arg0), sh_cnt);
11746 }
11747 }
11748
11749 /* Fall through */
11750
11751 case ROUND_DIV_EXPR:
11752 case CEIL_DIV_EXPR:
11753 case EXACT_DIV_EXPR:
11754 if (integer_zerop (arg1))
11755 return NULL_TREE;
11756
11757 /* Convert -A / -B to A / B when the type is signed and overflow is
11758 undefined. */
11759 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11760 && TREE_CODE (arg0) == NEGATE_EXPR
11761 && negate_expr_p (arg1))
11762 {
11763 if (INTEGRAL_TYPE_P (type))
11764 fold_overflow_warning (("assuming signed overflow does not occur "
11765 "when distributing negation across "
11766 "division"),
11767 WARN_STRICT_OVERFLOW_MISC);
11768 return fold_build2_loc (loc, code, type,
11769 fold_convert_loc (loc, type,
11770 TREE_OPERAND (arg0, 0)),
11771 fold_convert_loc (loc, type,
11772 negate_expr (arg1)));
11773 }
11774 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11775 && TREE_CODE (arg1) == NEGATE_EXPR
11776 && negate_expr_p (arg0))
11777 {
11778 if (INTEGRAL_TYPE_P (type))
11779 fold_overflow_warning (("assuming signed overflow does not occur "
11780 "when distributing negation across "
11781 "division"),
11782 WARN_STRICT_OVERFLOW_MISC);
11783 return fold_build2_loc (loc, code, type,
11784 fold_convert_loc (loc, type,
11785 negate_expr (arg0)),
11786 fold_convert_loc (loc, type,
11787 TREE_OPERAND (arg1, 0)));
11788 }
11789
11790 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11791 operation, EXACT_DIV_EXPR.
11792
11793 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11794 At one time others generated faster code, it's not clear if they do
11795 after the last round to changes to the DIV code in expmed.c. */
11796 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11797 && multiple_of_p (type, arg0, arg1))
11798 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11799
11800 strict_overflow_p = false;
11801 if (TREE_CODE (arg1) == INTEGER_CST
11802 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11803 &strict_overflow_p)))
11804 {
11805 if (strict_overflow_p)
11806 fold_overflow_warning (("assuming signed overflow does not occur "
11807 "when simplifying division"),
11808 WARN_STRICT_OVERFLOW_MISC);
11809 return fold_convert_loc (loc, type, tem);
11810 }
11811
11812 return NULL_TREE;
11813
11814 case CEIL_MOD_EXPR:
11815 case FLOOR_MOD_EXPR:
11816 case ROUND_MOD_EXPR:
11817 case TRUNC_MOD_EXPR:
11818 strict_overflow_p = false;
11819 if (TREE_CODE (arg1) == INTEGER_CST
11820 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11821 &strict_overflow_p)))
11822 {
11823 if (strict_overflow_p)
11824 fold_overflow_warning (("assuming signed overflow does not occur "
11825 "when simplifying modulus"),
11826 WARN_STRICT_OVERFLOW_MISC);
11827 return fold_convert_loc (loc, type, tem);
11828 }
11829
11830 return NULL_TREE;
11831
11832 case LROTATE_EXPR:
11833 case RROTATE_EXPR:
11834 case RSHIFT_EXPR:
11835 case LSHIFT_EXPR:
11836 /* Since negative shift count is not well-defined,
11837 don't try to compute it in the compiler. */
11838 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11839 return NULL_TREE;
11840
11841 prec = element_precision (type);
11842
11843 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11844 if (TREE_CODE (op0) == code && tree_fits_uhwi_p (arg1)
11845 && tree_to_uhwi (arg1) < prec
11846 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11847 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11848 {
11849 unsigned int low = (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11850 + tree_to_uhwi (arg1));
11851
11852 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11853 being well defined. */
11854 if (low >= prec)
11855 {
11856 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11857 low = low % prec;
11858 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11859 return omit_one_operand_loc (loc, type, build_zero_cst (type),
11860 TREE_OPERAND (arg0, 0));
11861 else
11862 low = prec - 1;
11863 }
11864
11865 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11866 build_int_cst (TREE_TYPE (arg1), low));
11867 }
11868
11869 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11870 into x & ((unsigned)-1 >> c) for unsigned types. */
11871 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11872 || (TYPE_UNSIGNED (type)
11873 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11874 && tree_fits_uhwi_p (arg1)
11875 && tree_to_uhwi (arg1) < prec
11876 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11877 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11878 {
11879 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11880 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
11881 tree lshift;
11882 tree arg00;
11883
11884 if (low0 == low1)
11885 {
11886 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11887
11888 lshift = build_minus_one_cst (type);
11889 lshift = const_binop (code, lshift, arg1);
11890
11891 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11892 }
11893 }
11894
11895 /* If we have a rotate of a bit operation with the rotate count and
11896 the second operand of the bit operation both constant,
11897 permute the two operations. */
11898 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11899 && (TREE_CODE (arg0) == BIT_AND_EXPR
11900 || TREE_CODE (arg0) == BIT_IOR_EXPR
11901 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11902 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11903 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11904 fold_build2_loc (loc, code, type,
11905 TREE_OPERAND (arg0, 0), arg1),
11906 fold_build2_loc (loc, code, type,
11907 TREE_OPERAND (arg0, 1), arg1));
11908
11909 /* Two consecutive rotates adding up to the some integer
11910 multiple of the precision of the type can be ignored. */
11911 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11912 && TREE_CODE (arg0) == RROTATE_EXPR
11913 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11914 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
11915 prec) == 0)
11916 return TREE_OPERAND (arg0, 0);
11917
11918 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11919 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11920 if the latter can be further optimized. */
11921 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11922 && TREE_CODE (arg0) == BIT_AND_EXPR
11923 && TREE_CODE (arg1) == INTEGER_CST
11924 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11925 {
11926 tree mask = fold_build2_loc (loc, code, type,
11927 fold_convert_loc (loc, type,
11928 TREE_OPERAND (arg0, 1)),
11929 arg1);
11930 tree shift = fold_build2_loc (loc, code, type,
11931 fold_convert_loc (loc, type,
11932 TREE_OPERAND (arg0, 0)),
11933 arg1);
11934 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11935 if (tem)
11936 return tem;
11937 }
11938
11939 return NULL_TREE;
11940
11941 case MIN_EXPR:
11942 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11943 if (tem)
11944 return tem;
11945 goto associate;
11946
11947 case MAX_EXPR:
11948 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11949 if (tem)
11950 return tem;
11951 goto associate;
11952
11953 case TRUTH_ANDIF_EXPR:
11954 /* Note that the operands of this must be ints
11955 and their values must be 0 or 1.
11956 ("true" is a fixed value perhaps depending on the language.) */
11957 /* If first arg is constant zero, return it. */
11958 if (integer_zerop (arg0))
11959 return fold_convert_loc (loc, type, arg0);
11960 case TRUTH_AND_EXPR:
11961 /* If either arg is constant true, drop it. */
11962 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11963 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11964 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11965 /* Preserve sequence points. */
11966 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11967 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11968 /* If second arg is constant zero, result is zero, but first arg
11969 must be evaluated. */
11970 if (integer_zerop (arg1))
11971 return omit_one_operand_loc (loc, type, arg1, arg0);
11972 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11973 case will be handled here. */
11974 if (integer_zerop (arg0))
11975 return omit_one_operand_loc (loc, type, arg0, arg1);
11976
11977 /* !X && X is always false. */
11978 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11979 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11980 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11981 /* X && !X is always false. */
11982 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11983 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11984 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11985
11986 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11987 means A >= Y && A != MAX, but in this case we know that
11988 A < X <= MAX. */
11989
11990 if (!TREE_SIDE_EFFECTS (arg0)
11991 && !TREE_SIDE_EFFECTS (arg1))
11992 {
11993 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11994 if (tem && !operand_equal_p (tem, arg0, 0))
11995 return fold_build2_loc (loc, code, type, tem, arg1);
11996
11997 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11998 if (tem && !operand_equal_p (tem, arg1, 0))
11999 return fold_build2_loc (loc, code, type, arg0, tem);
12000 }
12001
12002 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12003 != NULL_TREE)
12004 return tem;
12005
12006 return NULL_TREE;
12007
12008 case TRUTH_ORIF_EXPR:
12009 /* Note that the operands of this must be ints
12010 and their values must be 0 or true.
12011 ("true" is a fixed value perhaps depending on the language.) */
12012 /* If first arg is constant true, return it. */
12013 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12014 return fold_convert_loc (loc, type, arg0);
12015 case TRUTH_OR_EXPR:
12016 /* If either arg is constant zero, drop it. */
12017 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12018 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12019 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12020 /* Preserve sequence points. */
12021 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12022 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12023 /* If second arg is constant true, result is true, but we must
12024 evaluate first arg. */
12025 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12026 return omit_one_operand_loc (loc, type, arg1, arg0);
12027 /* Likewise for first arg, but note this only occurs here for
12028 TRUTH_OR_EXPR. */
12029 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12030 return omit_one_operand_loc (loc, type, arg0, arg1);
12031
12032 /* !X || X is always true. */
12033 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12034 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12035 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12036 /* X || !X is always true. */
12037 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12038 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12039 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12040
12041 /* (X && !Y) || (!X && Y) is X ^ Y */
12042 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12043 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12044 {
12045 tree a0, a1, l0, l1, n0, n1;
12046
12047 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12048 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12049
12050 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12051 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12052
12053 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12054 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12055
12056 if ((operand_equal_p (n0, a0, 0)
12057 && operand_equal_p (n1, a1, 0))
12058 || (operand_equal_p (n0, a1, 0)
12059 && operand_equal_p (n1, a0, 0)))
12060 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12061 }
12062
12063 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12064 != NULL_TREE)
12065 return tem;
12066
12067 return NULL_TREE;
12068
12069 case TRUTH_XOR_EXPR:
12070 /* If the second arg is constant zero, drop it. */
12071 if (integer_zerop (arg1))
12072 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12073 /* If the second arg is constant true, this is a logical inversion. */
12074 if (integer_onep (arg1))
12075 {
12076 tem = invert_truthvalue_loc (loc, arg0);
12077 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12078 }
12079 /* Identical arguments cancel to zero. */
12080 if (operand_equal_p (arg0, arg1, 0))
12081 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12082
12083 /* !X ^ X is always true. */
12084 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12085 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12086 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12087
12088 /* X ^ !X is always true. */
12089 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12090 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12091 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12092
12093 return NULL_TREE;
12094
12095 case EQ_EXPR:
12096 case NE_EXPR:
12097 STRIP_NOPS (arg0);
12098 STRIP_NOPS (arg1);
12099
12100 tem = fold_comparison (loc, code, type, op0, op1);
12101 if (tem != NULL_TREE)
12102 return tem;
12103
12104 /* bool_var != 0 becomes bool_var. */
12105 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12106 && code == NE_EXPR)
12107 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12108
12109 /* bool_var == 1 becomes bool_var. */
12110 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12111 && code == EQ_EXPR)
12112 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12113
12114 /* bool_var != 1 becomes !bool_var. */
12115 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12116 && code == NE_EXPR)
12117 return fold_convert_loc (loc, type,
12118 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12119 TREE_TYPE (arg0), arg0));
12120
12121 /* bool_var == 0 becomes !bool_var. */
12122 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12123 && code == EQ_EXPR)
12124 return fold_convert_loc (loc, type,
12125 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12126 TREE_TYPE (arg0), arg0));
12127
12128 /* !exp != 0 becomes !exp */
12129 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12130 && code == NE_EXPR)
12131 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12132
12133 /* If this is an equality comparison of the address of two non-weak,
12134 unaliased symbols neither of which are extern (since we do not
12135 have access to attributes for externs), then we know the result. */
12136 if (TREE_CODE (arg0) == ADDR_EXPR
12137 && DECL_P (TREE_OPERAND (arg0, 0))
12138 && TREE_CODE (arg1) == ADDR_EXPR
12139 && DECL_P (TREE_OPERAND (arg1, 0)))
12140 {
12141 int equal;
12142
12143 if (decl_in_symtab_p (TREE_OPERAND (arg0, 0))
12144 && decl_in_symtab_p (TREE_OPERAND (arg1, 0)))
12145 equal = symtab_node::get_create (TREE_OPERAND (arg0, 0))
12146 ->equal_address_to (symtab_node::get_create
12147 (TREE_OPERAND (arg1, 0)));
12148 else
12149 equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12150 if (equal != 2)
12151 return constant_boolean_node (equal
12152 ? code == EQ_EXPR : code != EQ_EXPR,
12153 type);
12154 }
12155
12156 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12157 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12158 && TREE_CODE (arg1) == INTEGER_CST
12159 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12160 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12161 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12162 fold_convert_loc (loc,
12163 TREE_TYPE (arg0),
12164 arg1),
12165 TREE_OPERAND (arg0, 1)));
12166
12167 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12168 if ((TREE_CODE (arg0) == PLUS_EXPR
12169 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12170 || TREE_CODE (arg0) == MINUS_EXPR)
12171 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12172 0)),
12173 arg1, 0)
12174 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12175 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12176 {
12177 tree val = TREE_OPERAND (arg0, 1);
12178 return omit_two_operands_loc (loc, type,
12179 fold_build2_loc (loc, code, type,
12180 val,
12181 build_int_cst (TREE_TYPE (val),
12182 0)),
12183 TREE_OPERAND (arg0, 0), arg1);
12184 }
12185
12186 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12187 if (TREE_CODE (arg0) == MINUS_EXPR
12188 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12189 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12190 1)),
12191 arg1, 0)
12192 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
12193 {
12194 return omit_two_operands_loc (loc, type,
12195 code == NE_EXPR
12196 ? boolean_true_node : boolean_false_node,
12197 TREE_OPERAND (arg0, 1), arg1);
12198 }
12199
12200 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12201 if (TREE_CODE (arg0) == ABS_EXPR
12202 && (integer_zerop (arg1) || real_zerop (arg1)))
12203 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12204
12205 /* If this is an EQ or NE comparison with zero and ARG0 is
12206 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12207 two operations, but the latter can be done in one less insn
12208 on machines that have only two-operand insns or on which a
12209 constant cannot be the first operand. */
12210 if (TREE_CODE (arg0) == BIT_AND_EXPR
12211 && integer_zerop (arg1))
12212 {
12213 tree arg00 = TREE_OPERAND (arg0, 0);
12214 tree arg01 = TREE_OPERAND (arg0, 1);
12215 if (TREE_CODE (arg00) == LSHIFT_EXPR
12216 && integer_onep (TREE_OPERAND (arg00, 0)))
12217 {
12218 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12219 arg01, TREE_OPERAND (arg00, 1));
12220 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12221 build_int_cst (TREE_TYPE (arg0), 1));
12222 return fold_build2_loc (loc, code, type,
12223 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12224 arg1);
12225 }
12226 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12227 && integer_onep (TREE_OPERAND (arg01, 0)))
12228 {
12229 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12230 arg00, TREE_OPERAND (arg01, 1));
12231 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12232 build_int_cst (TREE_TYPE (arg0), 1));
12233 return fold_build2_loc (loc, code, type,
12234 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12235 arg1);
12236 }
12237 }
12238
12239 /* If this is an NE or EQ comparison of zero against the result of a
12240 signed MOD operation whose second operand is a power of 2, make
12241 the MOD operation unsigned since it is simpler and equivalent. */
12242 if (integer_zerop (arg1)
12243 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12244 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12245 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12246 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12247 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12248 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12249 {
12250 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12251 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12252 fold_convert_loc (loc, newtype,
12253 TREE_OPERAND (arg0, 0)),
12254 fold_convert_loc (loc, newtype,
12255 TREE_OPERAND (arg0, 1)));
12256
12257 return fold_build2_loc (loc, code, type, newmod,
12258 fold_convert_loc (loc, newtype, arg1));
12259 }
12260
12261 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12262 C1 is a valid shift constant, and C2 is a power of two, i.e.
12263 a single bit. */
12264 if (TREE_CODE (arg0) == BIT_AND_EXPR
12265 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12266 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12267 == INTEGER_CST
12268 && integer_pow2p (TREE_OPERAND (arg0, 1))
12269 && integer_zerop (arg1))
12270 {
12271 tree itype = TREE_TYPE (arg0);
12272 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12273 prec = TYPE_PRECISION (itype);
12274
12275 /* Check for a valid shift count. */
12276 if (wi::ltu_p (arg001, prec))
12277 {
12278 tree arg01 = TREE_OPERAND (arg0, 1);
12279 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12280 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12281 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12282 can be rewritten as (X & (C2 << C1)) != 0. */
12283 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12284 {
12285 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12286 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12287 return fold_build2_loc (loc, code, type, tem,
12288 fold_convert_loc (loc, itype, arg1));
12289 }
12290 /* Otherwise, for signed (arithmetic) shifts,
12291 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12292 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12293 else if (!TYPE_UNSIGNED (itype))
12294 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12295 arg000, build_int_cst (itype, 0));
12296 /* Otherwise, of unsigned (logical) shifts,
12297 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12298 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12299 else
12300 return omit_one_operand_loc (loc, type,
12301 code == EQ_EXPR ? integer_one_node
12302 : integer_zero_node,
12303 arg000);
12304 }
12305 }
12306
12307 /* If we have (A & C) == C where C is a power of 2, convert this into
12308 (A & C) != 0. Similarly for NE_EXPR. */
12309 if (TREE_CODE (arg0) == BIT_AND_EXPR
12310 && integer_pow2p (TREE_OPERAND (arg0, 1))
12311 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12312 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12313 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12314 integer_zero_node));
12315
12316 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12317 bit, then fold the expression into A < 0 or A >= 0. */
12318 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12319 if (tem)
12320 return tem;
12321
12322 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12323 Similarly for NE_EXPR. */
12324 if (TREE_CODE (arg0) == BIT_AND_EXPR
12325 && TREE_CODE (arg1) == INTEGER_CST
12326 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12327 {
12328 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12329 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12330 TREE_OPERAND (arg0, 1));
12331 tree dandnotc
12332 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12333 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12334 notc);
12335 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12336 if (integer_nonzerop (dandnotc))
12337 return omit_one_operand_loc (loc, type, rslt, arg0);
12338 }
12339
12340 /* If this is a comparison of a field, we may be able to simplify it. */
12341 if ((TREE_CODE (arg0) == COMPONENT_REF
12342 || TREE_CODE (arg0) == BIT_FIELD_REF)
12343 /* Handle the constant case even without -O
12344 to make sure the warnings are given. */
12345 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12346 {
12347 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12348 if (t1)
12349 return t1;
12350 }
12351
12352 /* Optimize comparisons of strlen vs zero to a compare of the
12353 first character of the string vs zero. To wit,
12354 strlen(ptr) == 0 => *ptr == 0
12355 strlen(ptr) != 0 => *ptr != 0
12356 Other cases should reduce to one of these two (or a constant)
12357 due to the return value of strlen being unsigned. */
12358 if (TREE_CODE (arg0) == CALL_EXPR
12359 && integer_zerop (arg1))
12360 {
12361 tree fndecl = get_callee_fndecl (arg0);
12362
12363 if (fndecl
12364 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12365 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12366 && call_expr_nargs (arg0) == 1
12367 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12368 {
12369 tree iref = build_fold_indirect_ref_loc (loc,
12370 CALL_EXPR_ARG (arg0, 0));
12371 return fold_build2_loc (loc, code, type, iref,
12372 build_int_cst (TREE_TYPE (iref), 0));
12373 }
12374 }
12375
12376 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12377 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12378 if (TREE_CODE (arg0) == RSHIFT_EXPR
12379 && integer_zerop (arg1)
12380 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12381 {
12382 tree arg00 = TREE_OPERAND (arg0, 0);
12383 tree arg01 = TREE_OPERAND (arg0, 1);
12384 tree itype = TREE_TYPE (arg00);
12385 if (wi::eq_p (arg01, element_precision (itype) - 1))
12386 {
12387 if (TYPE_UNSIGNED (itype))
12388 {
12389 itype = signed_type_for (itype);
12390 arg00 = fold_convert_loc (loc, itype, arg00);
12391 }
12392 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12393 type, arg00, build_zero_cst (itype));
12394 }
12395 }
12396
12397 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12398 (X & C) == 0 when C is a single bit. */
12399 if (TREE_CODE (arg0) == BIT_AND_EXPR
12400 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12401 && integer_zerop (arg1)
12402 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12403 {
12404 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12405 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12406 TREE_OPERAND (arg0, 1));
12407 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12408 type, tem,
12409 fold_convert_loc (loc, TREE_TYPE (arg0),
12410 arg1));
12411 }
12412
12413 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12414 constant C is a power of two, i.e. a single bit. */
12415 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12416 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12417 && integer_zerop (arg1)
12418 && integer_pow2p (TREE_OPERAND (arg0, 1))
12419 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12420 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12421 {
12422 tree arg00 = TREE_OPERAND (arg0, 0);
12423 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12424 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12425 }
12426
12427 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12428 when is C is a power of two, i.e. a single bit. */
12429 if (TREE_CODE (arg0) == BIT_AND_EXPR
12430 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12431 && integer_zerop (arg1)
12432 && integer_pow2p (TREE_OPERAND (arg0, 1))
12433 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12434 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12435 {
12436 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12437 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12438 arg000, TREE_OPERAND (arg0, 1));
12439 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12440 tem, build_int_cst (TREE_TYPE (tem), 0));
12441 }
12442
12443 if (integer_zerop (arg1)
12444 && tree_expr_nonzero_p (arg0))
12445 {
12446 tree res = constant_boolean_node (code==NE_EXPR, type);
12447 return omit_one_operand_loc (loc, type, res, arg0);
12448 }
12449
12450 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12451 if (TREE_CODE (arg0) == BIT_AND_EXPR
12452 && TREE_CODE (arg1) == BIT_AND_EXPR)
12453 {
12454 tree arg00 = TREE_OPERAND (arg0, 0);
12455 tree arg01 = TREE_OPERAND (arg0, 1);
12456 tree arg10 = TREE_OPERAND (arg1, 0);
12457 tree arg11 = TREE_OPERAND (arg1, 1);
12458 tree itype = TREE_TYPE (arg0);
12459
12460 if (operand_equal_p (arg01, arg11, 0))
12461 return fold_build2_loc (loc, code, type,
12462 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12463 fold_build2_loc (loc,
12464 BIT_XOR_EXPR, itype,
12465 arg00, arg10),
12466 arg01),
12467 build_zero_cst (itype));
12468
12469 if (operand_equal_p (arg01, arg10, 0))
12470 return fold_build2_loc (loc, code, type,
12471 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12472 fold_build2_loc (loc,
12473 BIT_XOR_EXPR, itype,
12474 arg00, arg11),
12475 arg01),
12476 build_zero_cst (itype));
12477
12478 if (operand_equal_p (arg00, arg11, 0))
12479 return fold_build2_loc (loc, code, type,
12480 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12481 fold_build2_loc (loc,
12482 BIT_XOR_EXPR, itype,
12483 arg01, arg10),
12484 arg00),
12485 build_zero_cst (itype));
12486
12487 if (operand_equal_p (arg00, arg10, 0))
12488 return fold_build2_loc (loc, code, type,
12489 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12490 fold_build2_loc (loc,
12491 BIT_XOR_EXPR, itype,
12492 arg01, arg11),
12493 arg00),
12494 build_zero_cst (itype));
12495 }
12496
12497 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12498 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12499 {
12500 tree arg00 = TREE_OPERAND (arg0, 0);
12501 tree arg01 = TREE_OPERAND (arg0, 1);
12502 tree arg10 = TREE_OPERAND (arg1, 0);
12503 tree arg11 = TREE_OPERAND (arg1, 1);
12504 tree itype = TREE_TYPE (arg0);
12505
12506 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12507 operand_equal_p guarantees no side-effects so we don't need
12508 to use omit_one_operand on Z. */
12509 if (operand_equal_p (arg01, arg11, 0))
12510 return fold_build2_loc (loc, code, type, arg00,
12511 fold_convert_loc (loc, TREE_TYPE (arg00),
12512 arg10));
12513 if (operand_equal_p (arg01, arg10, 0))
12514 return fold_build2_loc (loc, code, type, arg00,
12515 fold_convert_loc (loc, TREE_TYPE (arg00),
12516 arg11));
12517 if (operand_equal_p (arg00, arg11, 0))
12518 return fold_build2_loc (loc, code, type, arg01,
12519 fold_convert_loc (loc, TREE_TYPE (arg01),
12520 arg10));
12521 if (operand_equal_p (arg00, arg10, 0))
12522 return fold_build2_loc (loc, code, type, arg01,
12523 fold_convert_loc (loc, TREE_TYPE (arg01),
12524 arg11));
12525
12526 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12527 if (TREE_CODE (arg01) == INTEGER_CST
12528 && TREE_CODE (arg11) == INTEGER_CST)
12529 {
12530 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12531 fold_convert_loc (loc, itype, arg11));
12532 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12533 return fold_build2_loc (loc, code, type, tem,
12534 fold_convert_loc (loc, itype, arg10));
12535 }
12536 }
12537
12538 /* Attempt to simplify equality/inequality comparisons of complex
12539 values. Only lower the comparison if the result is known or
12540 can be simplified to a single scalar comparison. */
12541 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12542 || TREE_CODE (arg0) == COMPLEX_CST)
12543 && (TREE_CODE (arg1) == COMPLEX_EXPR
12544 || TREE_CODE (arg1) == COMPLEX_CST))
12545 {
12546 tree real0, imag0, real1, imag1;
12547 tree rcond, icond;
12548
12549 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12550 {
12551 real0 = TREE_OPERAND (arg0, 0);
12552 imag0 = TREE_OPERAND (arg0, 1);
12553 }
12554 else
12555 {
12556 real0 = TREE_REALPART (arg0);
12557 imag0 = TREE_IMAGPART (arg0);
12558 }
12559
12560 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12561 {
12562 real1 = TREE_OPERAND (arg1, 0);
12563 imag1 = TREE_OPERAND (arg1, 1);
12564 }
12565 else
12566 {
12567 real1 = TREE_REALPART (arg1);
12568 imag1 = TREE_IMAGPART (arg1);
12569 }
12570
12571 rcond = fold_binary_loc (loc, code, type, real0, real1);
12572 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12573 {
12574 if (integer_zerop (rcond))
12575 {
12576 if (code == EQ_EXPR)
12577 return omit_two_operands_loc (loc, type, boolean_false_node,
12578 imag0, imag1);
12579 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12580 }
12581 else
12582 {
12583 if (code == NE_EXPR)
12584 return omit_two_operands_loc (loc, type, boolean_true_node,
12585 imag0, imag1);
12586 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12587 }
12588 }
12589
12590 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12591 if (icond && TREE_CODE (icond) == INTEGER_CST)
12592 {
12593 if (integer_zerop (icond))
12594 {
12595 if (code == EQ_EXPR)
12596 return omit_two_operands_loc (loc, type, boolean_false_node,
12597 real0, real1);
12598 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12599 }
12600 else
12601 {
12602 if (code == NE_EXPR)
12603 return omit_two_operands_loc (loc, type, boolean_true_node,
12604 real0, real1);
12605 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12606 }
12607 }
12608 }
12609
12610 return NULL_TREE;
12611
12612 case LT_EXPR:
12613 case GT_EXPR:
12614 case LE_EXPR:
12615 case GE_EXPR:
12616 tem = fold_comparison (loc, code, type, op0, op1);
12617 if (tem != NULL_TREE)
12618 return tem;
12619
12620 /* Transform comparisons of the form X +- C CMP X. */
12621 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12622 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12623 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12624 && !HONOR_SNANS (arg0))
12625 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12626 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12627 {
12628 tree arg01 = TREE_OPERAND (arg0, 1);
12629 enum tree_code code0 = TREE_CODE (arg0);
12630 int is_positive;
12631
12632 if (TREE_CODE (arg01) == REAL_CST)
12633 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12634 else
12635 is_positive = tree_int_cst_sgn (arg01);
12636
12637 /* (X - c) > X becomes false. */
12638 if (code == GT_EXPR
12639 && ((code0 == MINUS_EXPR && is_positive >= 0)
12640 || (code0 == PLUS_EXPR && is_positive <= 0)))
12641 {
12642 if (TREE_CODE (arg01) == INTEGER_CST
12643 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12644 fold_overflow_warning (("assuming signed overflow does not "
12645 "occur when assuming that (X - c) > X "
12646 "is always false"),
12647 WARN_STRICT_OVERFLOW_ALL);
12648 return constant_boolean_node (0, type);
12649 }
12650
12651 /* Likewise (X + c) < X becomes false. */
12652 if (code == LT_EXPR
12653 && ((code0 == PLUS_EXPR && is_positive >= 0)
12654 || (code0 == MINUS_EXPR && is_positive <= 0)))
12655 {
12656 if (TREE_CODE (arg01) == INTEGER_CST
12657 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12658 fold_overflow_warning (("assuming signed overflow does not "
12659 "occur when assuming that "
12660 "(X + c) < X is always false"),
12661 WARN_STRICT_OVERFLOW_ALL);
12662 return constant_boolean_node (0, type);
12663 }
12664
12665 /* Convert (X - c) <= X to true. */
12666 if (!HONOR_NANS (arg1)
12667 && code == LE_EXPR
12668 && ((code0 == MINUS_EXPR && is_positive >= 0)
12669 || (code0 == PLUS_EXPR && is_positive <= 0)))
12670 {
12671 if (TREE_CODE (arg01) == INTEGER_CST
12672 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12673 fold_overflow_warning (("assuming signed overflow does not "
12674 "occur when assuming that "
12675 "(X - c) <= X is always true"),
12676 WARN_STRICT_OVERFLOW_ALL);
12677 return constant_boolean_node (1, type);
12678 }
12679
12680 /* Convert (X + c) >= X to true. */
12681 if (!HONOR_NANS (arg1)
12682 && code == GE_EXPR
12683 && ((code0 == PLUS_EXPR && is_positive >= 0)
12684 || (code0 == MINUS_EXPR && is_positive <= 0)))
12685 {
12686 if (TREE_CODE (arg01) == INTEGER_CST
12687 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12688 fold_overflow_warning (("assuming signed overflow does not "
12689 "occur when assuming that "
12690 "(X + c) >= X is always true"),
12691 WARN_STRICT_OVERFLOW_ALL);
12692 return constant_boolean_node (1, type);
12693 }
12694
12695 if (TREE_CODE (arg01) == INTEGER_CST)
12696 {
12697 /* Convert X + c > X and X - c < X to true for integers. */
12698 if (code == GT_EXPR
12699 && ((code0 == PLUS_EXPR && is_positive > 0)
12700 || (code0 == MINUS_EXPR && is_positive < 0)))
12701 {
12702 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12703 fold_overflow_warning (("assuming signed overflow does "
12704 "not occur when assuming that "
12705 "(X + c) > X is always true"),
12706 WARN_STRICT_OVERFLOW_ALL);
12707 return constant_boolean_node (1, type);
12708 }
12709
12710 if (code == LT_EXPR
12711 && ((code0 == MINUS_EXPR && is_positive > 0)
12712 || (code0 == PLUS_EXPR && is_positive < 0)))
12713 {
12714 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12715 fold_overflow_warning (("assuming signed overflow does "
12716 "not occur when assuming that "
12717 "(X - c) < X is always true"),
12718 WARN_STRICT_OVERFLOW_ALL);
12719 return constant_boolean_node (1, type);
12720 }
12721
12722 /* Convert X + c <= X and X - c >= X to false for integers. */
12723 if (code == LE_EXPR
12724 && ((code0 == PLUS_EXPR && is_positive > 0)
12725 || (code0 == MINUS_EXPR && is_positive < 0)))
12726 {
12727 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12728 fold_overflow_warning (("assuming signed overflow does "
12729 "not occur when assuming that "
12730 "(X + c) <= X is always false"),
12731 WARN_STRICT_OVERFLOW_ALL);
12732 return constant_boolean_node (0, type);
12733 }
12734
12735 if (code == GE_EXPR
12736 && ((code0 == MINUS_EXPR && is_positive > 0)
12737 || (code0 == PLUS_EXPR && is_positive < 0)))
12738 {
12739 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12740 fold_overflow_warning (("assuming signed overflow does "
12741 "not occur when assuming that "
12742 "(X - c) >= X is always false"),
12743 WARN_STRICT_OVERFLOW_ALL);
12744 return constant_boolean_node (0, type);
12745 }
12746 }
12747 }
12748
12749 /* Comparisons with the highest or lowest possible integer of
12750 the specified precision will have known values. */
12751 {
12752 tree arg1_type = TREE_TYPE (arg1);
12753 unsigned int prec = TYPE_PRECISION (arg1_type);
12754
12755 if (TREE_CODE (arg1) == INTEGER_CST
12756 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12757 {
12758 wide_int max = wi::max_value (arg1_type);
12759 wide_int signed_max = wi::max_value (prec, SIGNED);
12760 wide_int min = wi::min_value (arg1_type);
12761
12762 if (wi::eq_p (arg1, max))
12763 switch (code)
12764 {
12765 case GT_EXPR:
12766 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12767
12768 case GE_EXPR:
12769 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12770
12771 case LE_EXPR:
12772 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12773
12774 case LT_EXPR:
12775 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12776
12777 /* The GE_EXPR and LT_EXPR cases above are not normally
12778 reached because of previous transformations. */
12779
12780 default:
12781 break;
12782 }
12783 else if (wi::eq_p (arg1, max - 1))
12784 switch (code)
12785 {
12786 case GT_EXPR:
12787 arg1 = const_binop (PLUS_EXPR, arg1,
12788 build_int_cst (TREE_TYPE (arg1), 1));
12789 return fold_build2_loc (loc, EQ_EXPR, type,
12790 fold_convert_loc (loc,
12791 TREE_TYPE (arg1), arg0),
12792 arg1);
12793 case LE_EXPR:
12794 arg1 = const_binop (PLUS_EXPR, arg1,
12795 build_int_cst (TREE_TYPE (arg1), 1));
12796 return fold_build2_loc (loc, NE_EXPR, type,
12797 fold_convert_loc (loc, TREE_TYPE (arg1),
12798 arg0),
12799 arg1);
12800 default:
12801 break;
12802 }
12803 else if (wi::eq_p (arg1, min))
12804 switch (code)
12805 {
12806 case LT_EXPR:
12807 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12808
12809 case LE_EXPR:
12810 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12811
12812 case GE_EXPR:
12813 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12814
12815 case GT_EXPR:
12816 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12817
12818 default:
12819 break;
12820 }
12821 else if (wi::eq_p (arg1, min + 1))
12822 switch (code)
12823 {
12824 case GE_EXPR:
12825 arg1 = const_binop (MINUS_EXPR, arg1,
12826 build_int_cst (TREE_TYPE (arg1), 1));
12827 return fold_build2_loc (loc, NE_EXPR, type,
12828 fold_convert_loc (loc,
12829 TREE_TYPE (arg1), arg0),
12830 arg1);
12831 case LT_EXPR:
12832 arg1 = const_binop (MINUS_EXPR, arg1,
12833 build_int_cst (TREE_TYPE (arg1), 1));
12834 return fold_build2_loc (loc, EQ_EXPR, type,
12835 fold_convert_loc (loc, TREE_TYPE (arg1),
12836 arg0),
12837 arg1);
12838 default:
12839 break;
12840 }
12841
12842 else if (wi::eq_p (arg1, signed_max)
12843 && TYPE_UNSIGNED (arg1_type)
12844 /* We will flip the signedness of the comparison operator
12845 associated with the mode of arg1, so the sign bit is
12846 specified by this mode. Check that arg1 is the signed
12847 max associated with this sign bit. */
12848 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
12849 /* signed_type does not work on pointer types. */
12850 && INTEGRAL_TYPE_P (arg1_type))
12851 {
12852 /* The following case also applies to X < signed_max+1
12853 and X >= signed_max+1 because previous transformations. */
12854 if (code == LE_EXPR || code == GT_EXPR)
12855 {
12856 tree st = signed_type_for (arg1_type);
12857 return fold_build2_loc (loc,
12858 code == LE_EXPR ? GE_EXPR : LT_EXPR,
12859 type, fold_convert_loc (loc, st, arg0),
12860 build_int_cst (st, 0));
12861 }
12862 }
12863 }
12864 }
12865
12866 /* If we are comparing an ABS_EXPR with a constant, we can
12867 convert all the cases into explicit comparisons, but they may
12868 well not be faster than doing the ABS and one comparison.
12869 But ABS (X) <= C is a range comparison, which becomes a subtraction
12870 and a comparison, and is probably faster. */
12871 if (code == LE_EXPR
12872 && TREE_CODE (arg1) == INTEGER_CST
12873 && TREE_CODE (arg0) == ABS_EXPR
12874 && ! TREE_SIDE_EFFECTS (arg0)
12875 && (0 != (tem = negate_expr (arg1)))
12876 && TREE_CODE (tem) == INTEGER_CST
12877 && !TREE_OVERFLOW (tem))
12878 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12879 build2 (GE_EXPR, type,
12880 TREE_OPERAND (arg0, 0), tem),
12881 build2 (LE_EXPR, type,
12882 TREE_OPERAND (arg0, 0), arg1));
12883
12884 /* Convert ABS_EXPR<x> >= 0 to true. */
12885 strict_overflow_p = false;
12886 if (code == GE_EXPR
12887 && (integer_zerop (arg1)
12888 || (! HONOR_NANS (arg0)
12889 && real_zerop (arg1)))
12890 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12891 {
12892 if (strict_overflow_p)
12893 fold_overflow_warning (("assuming signed overflow does not occur "
12894 "when simplifying comparison of "
12895 "absolute value and zero"),
12896 WARN_STRICT_OVERFLOW_CONDITIONAL);
12897 return omit_one_operand_loc (loc, type,
12898 constant_boolean_node (true, type),
12899 arg0);
12900 }
12901
12902 /* Convert ABS_EXPR<x> < 0 to false. */
12903 strict_overflow_p = false;
12904 if (code == LT_EXPR
12905 && (integer_zerop (arg1) || real_zerop (arg1))
12906 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12907 {
12908 if (strict_overflow_p)
12909 fold_overflow_warning (("assuming signed overflow does not occur "
12910 "when simplifying comparison of "
12911 "absolute value and zero"),
12912 WARN_STRICT_OVERFLOW_CONDITIONAL);
12913 return omit_one_operand_loc (loc, type,
12914 constant_boolean_node (false, type),
12915 arg0);
12916 }
12917
12918 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12919 and similarly for >= into !=. */
12920 if ((code == LT_EXPR || code == GE_EXPR)
12921 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12922 && TREE_CODE (arg1) == LSHIFT_EXPR
12923 && integer_onep (TREE_OPERAND (arg1, 0)))
12924 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12925 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12926 TREE_OPERAND (arg1, 1)),
12927 build_zero_cst (TREE_TYPE (arg0)));
12928
12929 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12930 otherwise Y might be >= # of bits in X's type and thus e.g.
12931 (unsigned char) (1 << Y) for Y 15 might be 0.
12932 If the cast is widening, then 1 << Y should have unsigned type,
12933 otherwise if Y is number of bits in the signed shift type minus 1,
12934 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12935 31 might be 0xffffffff80000000. */
12936 if ((code == LT_EXPR || code == GE_EXPR)
12937 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12938 && CONVERT_EXPR_P (arg1)
12939 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12940 && (element_precision (TREE_TYPE (arg1))
12941 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12942 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12943 || (element_precision (TREE_TYPE (arg1))
12944 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12945 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12946 {
12947 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12948 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12949 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12950 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12951 build_zero_cst (TREE_TYPE (arg0)));
12952 }
12953
12954 return NULL_TREE;
12955
12956 case UNORDERED_EXPR:
12957 case ORDERED_EXPR:
12958 case UNLT_EXPR:
12959 case UNLE_EXPR:
12960 case UNGT_EXPR:
12961 case UNGE_EXPR:
12962 case UNEQ_EXPR:
12963 case LTGT_EXPR:
12964 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12965 {
12966 t1 = fold_relational_const (code, type, arg0, arg1);
12967 if (t1 != NULL_TREE)
12968 return t1;
12969 }
12970
12971 /* If the first operand is NaN, the result is constant. */
12972 if (TREE_CODE (arg0) == REAL_CST
12973 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12974 && (code != LTGT_EXPR || ! flag_trapping_math))
12975 {
12976 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12977 ? integer_zero_node
12978 : integer_one_node;
12979 return omit_one_operand_loc (loc, type, t1, arg1);
12980 }
12981
12982 /* If the second operand is NaN, the result is constant. */
12983 if (TREE_CODE (arg1) == REAL_CST
12984 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12985 && (code != LTGT_EXPR || ! flag_trapping_math))
12986 {
12987 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12988 ? integer_zero_node
12989 : integer_one_node;
12990 return omit_one_operand_loc (loc, type, t1, arg0);
12991 }
12992
12993 /* Simplify unordered comparison of something with itself. */
12994 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12995 && operand_equal_p (arg0, arg1, 0))
12996 return constant_boolean_node (1, type);
12997
12998 if (code == LTGT_EXPR
12999 && !flag_trapping_math
13000 && operand_equal_p (arg0, arg1, 0))
13001 return constant_boolean_node (0, type);
13002
13003 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13004 {
13005 tree targ0 = strip_float_extensions (arg0);
13006 tree targ1 = strip_float_extensions (arg1);
13007 tree newtype = TREE_TYPE (targ0);
13008
13009 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13010 newtype = TREE_TYPE (targ1);
13011
13012 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13013 return fold_build2_loc (loc, code, type,
13014 fold_convert_loc (loc, newtype, targ0),
13015 fold_convert_loc (loc, newtype, targ1));
13016 }
13017
13018 return NULL_TREE;
13019
13020 case COMPOUND_EXPR:
13021 /* When pedantic, a compound expression can be neither an lvalue
13022 nor an integer constant expression. */
13023 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13024 return NULL_TREE;
13025 /* Don't let (0, 0) be null pointer constant. */
13026 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13027 : fold_convert_loc (loc, type, arg1);
13028 return pedantic_non_lvalue_loc (loc, tem);
13029
13030 case ASSERT_EXPR:
13031 /* An ASSERT_EXPR should never be passed to fold_binary. */
13032 gcc_unreachable ();
13033
13034 default:
13035 return NULL_TREE;
13036 } /* switch (code) */
13037 }
13038
13039 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13040 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13041 of GOTO_EXPR. */
13042
13043 static tree
13044 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13045 {
13046 switch (TREE_CODE (*tp))
13047 {
13048 case LABEL_EXPR:
13049 return *tp;
13050
13051 case GOTO_EXPR:
13052 *walk_subtrees = 0;
13053
13054 /* ... fall through ... */
13055
13056 default:
13057 return NULL_TREE;
13058 }
13059 }
13060
13061 /* Return whether the sub-tree ST contains a label which is accessible from
13062 outside the sub-tree. */
13063
13064 static bool
13065 contains_label_p (tree st)
13066 {
13067 return
13068 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13069 }
13070
13071 /* Fold a ternary expression of code CODE and type TYPE with operands
13072 OP0, OP1, and OP2. Return the folded expression if folding is
13073 successful. Otherwise, return NULL_TREE. */
13074
13075 tree
13076 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13077 tree op0, tree op1, tree op2)
13078 {
13079 tree tem;
13080 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13081 enum tree_code_class kind = TREE_CODE_CLASS (code);
13082
13083 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13084 && TREE_CODE_LENGTH (code) == 3);
13085
13086 /* If this is a commutative operation, and OP0 is a constant, move it
13087 to OP1 to reduce the number of tests below. */
13088 if (commutative_ternary_tree_code (code)
13089 && tree_swap_operands_p (op0, op1, true))
13090 return fold_build3_loc (loc, code, type, op1, op0, op2);
13091
13092 tem = generic_simplify (loc, code, type, op0, op1, op2);
13093 if (tem)
13094 return tem;
13095
13096 /* Strip any conversions that don't change the mode. This is safe
13097 for every expression, except for a comparison expression because
13098 its signedness is derived from its operands. So, in the latter
13099 case, only strip conversions that don't change the signedness.
13100
13101 Note that this is done as an internal manipulation within the
13102 constant folder, in order to find the simplest representation of
13103 the arguments so that their form can be studied. In any cases,
13104 the appropriate type conversions should be put back in the tree
13105 that will get out of the constant folder. */
13106 if (op0)
13107 {
13108 arg0 = op0;
13109 STRIP_NOPS (arg0);
13110 }
13111
13112 if (op1)
13113 {
13114 arg1 = op1;
13115 STRIP_NOPS (arg1);
13116 }
13117
13118 if (op2)
13119 {
13120 arg2 = op2;
13121 STRIP_NOPS (arg2);
13122 }
13123
13124 switch (code)
13125 {
13126 case COMPONENT_REF:
13127 if (TREE_CODE (arg0) == CONSTRUCTOR
13128 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13129 {
13130 unsigned HOST_WIDE_INT idx;
13131 tree field, value;
13132 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13133 if (field == arg1)
13134 return value;
13135 }
13136 return NULL_TREE;
13137
13138 case COND_EXPR:
13139 case VEC_COND_EXPR:
13140 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13141 so all simple results must be passed through pedantic_non_lvalue. */
13142 if (TREE_CODE (arg0) == INTEGER_CST)
13143 {
13144 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13145 tem = integer_zerop (arg0) ? op2 : op1;
13146 /* Only optimize constant conditions when the selected branch
13147 has the same type as the COND_EXPR. This avoids optimizing
13148 away "c ? x : throw", where the throw has a void type.
13149 Avoid throwing away that operand which contains label. */
13150 if ((!TREE_SIDE_EFFECTS (unused_op)
13151 || !contains_label_p (unused_op))
13152 && (! VOID_TYPE_P (TREE_TYPE (tem))
13153 || VOID_TYPE_P (type)))
13154 return pedantic_non_lvalue_loc (loc, tem);
13155 return NULL_TREE;
13156 }
13157 else if (TREE_CODE (arg0) == VECTOR_CST)
13158 {
13159 if ((TREE_CODE (arg1) == VECTOR_CST
13160 || TREE_CODE (arg1) == CONSTRUCTOR)
13161 && (TREE_CODE (arg2) == VECTOR_CST
13162 || TREE_CODE (arg2) == CONSTRUCTOR))
13163 {
13164 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13165 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
13166 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
13167 for (i = 0; i < nelts; i++)
13168 {
13169 tree val = VECTOR_CST_ELT (arg0, i);
13170 if (integer_all_onesp (val))
13171 sel[i] = i;
13172 else if (integer_zerop (val))
13173 sel[i] = nelts + i;
13174 else /* Currently unreachable. */
13175 return NULL_TREE;
13176 }
13177 tree t = fold_vec_perm (type, arg1, arg2, sel);
13178 if (t != NULL_TREE)
13179 return t;
13180 }
13181 }
13182
13183 /* If we have A op B ? A : C, we may be able to convert this to a
13184 simpler expression, depending on the operation and the values
13185 of B and C. Signed zeros prevent all of these transformations,
13186 for reasons given above each one.
13187
13188 Also try swapping the arguments and inverting the conditional. */
13189 if (COMPARISON_CLASS_P (arg0)
13190 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13191 arg1, TREE_OPERAND (arg0, 1))
13192 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
13193 {
13194 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13195 if (tem)
13196 return tem;
13197 }
13198
13199 if (COMPARISON_CLASS_P (arg0)
13200 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13201 op2,
13202 TREE_OPERAND (arg0, 1))
13203 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
13204 {
13205 location_t loc0 = expr_location_or (arg0, loc);
13206 tem = fold_invert_truthvalue (loc0, arg0);
13207 if (tem && COMPARISON_CLASS_P (tem))
13208 {
13209 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13210 if (tem)
13211 return tem;
13212 }
13213 }
13214
13215 /* If the second operand is simpler than the third, swap them
13216 since that produces better jump optimization results. */
13217 if (truth_value_p (TREE_CODE (arg0))
13218 && tree_swap_operands_p (op1, op2, false))
13219 {
13220 location_t loc0 = expr_location_or (arg0, loc);
13221 /* See if this can be inverted. If it can't, possibly because
13222 it was a floating-point inequality comparison, don't do
13223 anything. */
13224 tem = fold_invert_truthvalue (loc0, arg0);
13225 if (tem)
13226 return fold_build3_loc (loc, code, type, tem, op2, op1);
13227 }
13228
13229 /* Convert A ? 1 : 0 to simply A. */
13230 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13231 : (integer_onep (op1)
13232 && !VECTOR_TYPE_P (type)))
13233 && integer_zerop (op2)
13234 /* If we try to convert OP0 to our type, the
13235 call to fold will try to move the conversion inside
13236 a COND, which will recurse. In that case, the COND_EXPR
13237 is probably the best choice, so leave it alone. */
13238 && type == TREE_TYPE (arg0))
13239 return pedantic_non_lvalue_loc (loc, arg0);
13240
13241 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13242 over COND_EXPR in cases such as floating point comparisons. */
13243 if (integer_zerop (op1)
13244 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
13245 : (integer_onep (op2)
13246 && !VECTOR_TYPE_P (type)))
13247 && truth_value_p (TREE_CODE (arg0)))
13248 return pedantic_non_lvalue_loc (loc,
13249 fold_convert_loc (loc, type,
13250 invert_truthvalue_loc (loc,
13251 arg0)));
13252
13253 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13254 if (TREE_CODE (arg0) == LT_EXPR
13255 && integer_zerop (TREE_OPERAND (arg0, 1))
13256 && integer_zerop (op2)
13257 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13258 {
13259 /* sign_bit_p looks through both zero and sign extensions,
13260 but for this optimization only sign extensions are
13261 usable. */
13262 tree tem2 = TREE_OPERAND (arg0, 0);
13263 while (tem != tem2)
13264 {
13265 if (TREE_CODE (tem2) != NOP_EXPR
13266 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13267 {
13268 tem = NULL_TREE;
13269 break;
13270 }
13271 tem2 = TREE_OPERAND (tem2, 0);
13272 }
13273 /* sign_bit_p only checks ARG1 bits within A's precision.
13274 If <sign bit of A> has wider type than A, bits outside
13275 of A's precision in <sign bit of A> need to be checked.
13276 If they are all 0, this optimization needs to be done
13277 in unsigned A's type, if they are all 1 in signed A's type,
13278 otherwise this can't be done. */
13279 if (tem
13280 && TYPE_PRECISION (TREE_TYPE (tem))
13281 < TYPE_PRECISION (TREE_TYPE (arg1))
13282 && TYPE_PRECISION (TREE_TYPE (tem))
13283 < TYPE_PRECISION (type))
13284 {
13285 int inner_width, outer_width;
13286 tree tem_type;
13287
13288 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13289 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13290 if (outer_width > TYPE_PRECISION (type))
13291 outer_width = TYPE_PRECISION (type);
13292
13293 wide_int mask = wi::shifted_mask
13294 (inner_width, outer_width - inner_width, false,
13295 TYPE_PRECISION (TREE_TYPE (arg1)));
13296
13297 wide_int common = mask & arg1;
13298 if (common == mask)
13299 {
13300 tem_type = signed_type_for (TREE_TYPE (tem));
13301 tem = fold_convert_loc (loc, tem_type, tem);
13302 }
13303 else if (common == 0)
13304 {
13305 tem_type = unsigned_type_for (TREE_TYPE (tem));
13306 tem = fold_convert_loc (loc, tem_type, tem);
13307 }
13308 else
13309 tem = NULL;
13310 }
13311
13312 if (tem)
13313 return
13314 fold_convert_loc (loc, type,
13315 fold_build2_loc (loc, BIT_AND_EXPR,
13316 TREE_TYPE (tem), tem,
13317 fold_convert_loc (loc,
13318 TREE_TYPE (tem),
13319 arg1)));
13320 }
13321
13322 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13323 already handled above. */
13324 if (TREE_CODE (arg0) == BIT_AND_EXPR
13325 && integer_onep (TREE_OPERAND (arg0, 1))
13326 && integer_zerop (op2)
13327 && integer_pow2p (arg1))
13328 {
13329 tree tem = TREE_OPERAND (arg0, 0);
13330 STRIP_NOPS (tem);
13331 if (TREE_CODE (tem) == RSHIFT_EXPR
13332 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13333 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13334 tree_to_uhwi (TREE_OPERAND (tem, 1)))
13335 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13336 TREE_OPERAND (tem, 0), arg1);
13337 }
13338
13339 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13340 is probably obsolete because the first operand should be a
13341 truth value (that's why we have the two cases above), but let's
13342 leave it in until we can confirm this for all front-ends. */
13343 if (integer_zerop (op2)
13344 && TREE_CODE (arg0) == NE_EXPR
13345 && integer_zerop (TREE_OPERAND (arg0, 1))
13346 && integer_pow2p (arg1)
13347 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13348 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13349 arg1, OEP_ONLY_CONST))
13350 return pedantic_non_lvalue_loc (loc,
13351 fold_convert_loc (loc, type,
13352 TREE_OPERAND (arg0, 0)));
13353
13354 /* Disable the transformations below for vectors, since
13355 fold_binary_op_with_conditional_arg may undo them immediately,
13356 yielding an infinite loop. */
13357 if (code == VEC_COND_EXPR)
13358 return NULL_TREE;
13359
13360 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13361 if (integer_zerop (op2)
13362 && truth_value_p (TREE_CODE (arg0))
13363 && truth_value_p (TREE_CODE (arg1))
13364 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13365 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13366 : TRUTH_ANDIF_EXPR,
13367 type, fold_convert_loc (loc, type, arg0), arg1);
13368
13369 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13370 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13371 && truth_value_p (TREE_CODE (arg0))
13372 && truth_value_p (TREE_CODE (arg1))
13373 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13374 {
13375 location_t loc0 = expr_location_or (arg0, loc);
13376 /* Only perform transformation if ARG0 is easily inverted. */
13377 tem = fold_invert_truthvalue (loc0, arg0);
13378 if (tem)
13379 return fold_build2_loc (loc, code == VEC_COND_EXPR
13380 ? BIT_IOR_EXPR
13381 : TRUTH_ORIF_EXPR,
13382 type, fold_convert_loc (loc, type, tem),
13383 arg1);
13384 }
13385
13386 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13387 if (integer_zerop (arg1)
13388 && truth_value_p (TREE_CODE (arg0))
13389 && truth_value_p (TREE_CODE (op2))
13390 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13391 {
13392 location_t loc0 = expr_location_or (arg0, loc);
13393 /* Only perform transformation if ARG0 is easily inverted. */
13394 tem = fold_invert_truthvalue (loc0, arg0);
13395 if (tem)
13396 return fold_build2_loc (loc, code == VEC_COND_EXPR
13397 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13398 type, fold_convert_loc (loc, type, tem),
13399 op2);
13400 }
13401
13402 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13403 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13404 && truth_value_p (TREE_CODE (arg0))
13405 && truth_value_p (TREE_CODE (op2))
13406 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13407 return fold_build2_loc (loc, code == VEC_COND_EXPR
13408 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13409 type, fold_convert_loc (loc, type, arg0), op2);
13410
13411 return NULL_TREE;
13412
13413 case CALL_EXPR:
13414 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13415 of fold_ternary on them. */
13416 gcc_unreachable ();
13417
13418 case BIT_FIELD_REF:
13419 if ((TREE_CODE (arg0) == VECTOR_CST
13420 || (TREE_CODE (arg0) == CONSTRUCTOR
13421 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
13422 && (type == TREE_TYPE (TREE_TYPE (arg0))
13423 || (TREE_CODE (type) == VECTOR_TYPE
13424 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
13425 {
13426 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13427 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
13428 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13429 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13430
13431 if (n != 0
13432 && (idx % width) == 0
13433 && (n % width) == 0
13434 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13435 {
13436 idx = idx / width;
13437 n = n / width;
13438
13439 if (TREE_CODE (arg0) == VECTOR_CST)
13440 {
13441 if (n == 1)
13442 return VECTOR_CST_ELT (arg0, idx);
13443
13444 tree *vals = XALLOCAVEC (tree, n);
13445 for (unsigned i = 0; i < n; ++i)
13446 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
13447 return build_vector (type, vals);
13448 }
13449
13450 /* Constructor elements can be subvectors. */
13451 unsigned HOST_WIDE_INT k = 1;
13452 if (CONSTRUCTOR_NELTS (arg0) != 0)
13453 {
13454 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
13455 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
13456 k = TYPE_VECTOR_SUBPARTS (cons_elem);
13457 }
13458
13459 /* We keep an exact subset of the constructor elements. */
13460 if ((idx % k) == 0 && (n % k) == 0)
13461 {
13462 if (CONSTRUCTOR_NELTS (arg0) == 0)
13463 return build_constructor (type, NULL);
13464 idx /= k;
13465 n /= k;
13466 if (n == 1)
13467 {
13468 if (idx < CONSTRUCTOR_NELTS (arg0))
13469 return CONSTRUCTOR_ELT (arg0, idx)->value;
13470 return build_zero_cst (type);
13471 }
13472
13473 vec<constructor_elt, va_gc> *vals;
13474 vec_alloc (vals, n);
13475 for (unsigned i = 0;
13476 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
13477 ++i)
13478 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
13479 CONSTRUCTOR_ELT
13480 (arg0, idx + i)->value);
13481 return build_constructor (type, vals);
13482 }
13483 /* The bitfield references a single constructor element. */
13484 else if (idx + n <= (idx / k + 1) * k)
13485 {
13486 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
13487 return build_zero_cst (type);
13488 else if (n == k)
13489 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
13490 else
13491 return fold_build3_loc (loc, code, type,
13492 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
13493 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
13494 }
13495 }
13496 }
13497
13498 /* A bit-field-ref that referenced the full argument can be stripped. */
13499 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13500 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
13501 && integer_zerop (op2))
13502 return fold_convert_loc (loc, type, arg0);
13503
13504 /* On constants we can use native encode/interpret to constant
13505 fold (nearly) all BIT_FIELD_REFs. */
13506 if (CONSTANT_CLASS_P (arg0)
13507 && can_native_interpret_type_p (type)
13508 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
13509 /* This limitation should not be necessary, we just need to
13510 round this up to mode size. */
13511 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
13512 /* Need bit-shifting of the buffer to relax the following. */
13513 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
13514 {
13515 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13516 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13517 unsigned HOST_WIDE_INT clen;
13518 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
13519 /* ??? We cannot tell native_encode_expr to start at
13520 some random byte only. So limit us to a reasonable amount
13521 of work. */
13522 if (clen <= 4096)
13523 {
13524 unsigned char *b = XALLOCAVEC (unsigned char, clen);
13525 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
13526 if (len > 0
13527 && len * BITS_PER_UNIT >= bitpos + bitsize)
13528 {
13529 tree v = native_interpret_expr (type,
13530 b + bitpos / BITS_PER_UNIT,
13531 bitsize / BITS_PER_UNIT);
13532 if (v)
13533 return v;
13534 }
13535 }
13536 }
13537
13538 return NULL_TREE;
13539
13540 case FMA_EXPR:
13541 /* For integers we can decompose the FMA if possible. */
13542 if (TREE_CODE (arg0) == INTEGER_CST
13543 && TREE_CODE (arg1) == INTEGER_CST)
13544 return fold_build2_loc (loc, PLUS_EXPR, type,
13545 const_binop (MULT_EXPR, arg0, arg1), arg2);
13546 if (integer_zerop (arg2))
13547 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13548
13549 return fold_fma (loc, type, arg0, arg1, arg2);
13550
13551 case VEC_PERM_EXPR:
13552 if (TREE_CODE (arg2) == VECTOR_CST)
13553 {
13554 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
13555 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
13556 unsigned char *sel2 = sel + nelts;
13557 bool need_mask_canon = false;
13558 bool need_mask_canon2 = false;
13559 bool all_in_vec0 = true;
13560 bool all_in_vec1 = true;
13561 bool maybe_identity = true;
13562 bool single_arg = (op0 == op1);
13563 bool changed = false;
13564
13565 mask2 = 2 * nelts - 1;
13566 mask = single_arg ? (nelts - 1) : mask2;
13567 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
13568 for (i = 0; i < nelts; i++)
13569 {
13570 tree val = VECTOR_CST_ELT (arg2, i);
13571 if (TREE_CODE (val) != INTEGER_CST)
13572 return NULL_TREE;
13573
13574 /* Make sure that the perm value is in an acceptable
13575 range. */
13576 wide_int t = val;
13577 need_mask_canon |= wi::gtu_p (t, mask);
13578 need_mask_canon2 |= wi::gtu_p (t, mask2);
13579 sel[i] = t.to_uhwi () & mask;
13580 sel2[i] = t.to_uhwi () & mask2;
13581
13582 if (sel[i] < nelts)
13583 all_in_vec1 = false;
13584 else
13585 all_in_vec0 = false;
13586
13587 if ((sel[i] & (nelts-1)) != i)
13588 maybe_identity = false;
13589 }
13590
13591 if (maybe_identity)
13592 {
13593 if (all_in_vec0)
13594 return op0;
13595 if (all_in_vec1)
13596 return op1;
13597 }
13598
13599 if (all_in_vec0)
13600 op1 = op0;
13601 else if (all_in_vec1)
13602 {
13603 op0 = op1;
13604 for (i = 0; i < nelts; i++)
13605 sel[i] -= nelts;
13606 need_mask_canon = true;
13607 }
13608
13609 if ((TREE_CODE (op0) == VECTOR_CST
13610 || TREE_CODE (op0) == CONSTRUCTOR)
13611 && (TREE_CODE (op1) == VECTOR_CST
13612 || TREE_CODE (op1) == CONSTRUCTOR))
13613 {
13614 tree t = fold_vec_perm (type, op0, op1, sel);
13615 if (t != NULL_TREE)
13616 return t;
13617 }
13618
13619 if (op0 == op1 && !single_arg)
13620 changed = true;
13621
13622 /* Some targets are deficient and fail to expand a single
13623 argument permutation while still allowing an equivalent
13624 2-argument version. */
13625 if (need_mask_canon && arg2 == op2
13626 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
13627 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
13628 {
13629 need_mask_canon = need_mask_canon2;
13630 sel = sel2;
13631 }
13632
13633 if (need_mask_canon && arg2 == op2)
13634 {
13635 tree *tsel = XALLOCAVEC (tree, nelts);
13636 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
13637 for (i = 0; i < nelts; i++)
13638 tsel[i] = build_int_cst (eltype, sel[i]);
13639 op2 = build_vector (TREE_TYPE (arg2), tsel);
13640 changed = true;
13641 }
13642
13643 if (changed)
13644 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
13645 }
13646 return NULL_TREE;
13647
13648 default:
13649 return NULL_TREE;
13650 } /* switch (code) */
13651 }
13652
13653 /* Perform constant folding and related simplification of EXPR.
13654 The related simplifications include x*1 => x, x*0 => 0, etc.,
13655 and application of the associative law.
13656 NOP_EXPR conversions may be removed freely (as long as we
13657 are careful not to change the type of the overall expression).
13658 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13659 but we can constant-fold them if they have constant operands. */
13660
13661 #ifdef ENABLE_FOLD_CHECKING
13662 # define fold(x) fold_1 (x)
13663 static tree fold_1 (tree);
13664 static
13665 #endif
13666 tree
13667 fold (tree expr)
13668 {
13669 const tree t = expr;
13670 enum tree_code code = TREE_CODE (t);
13671 enum tree_code_class kind = TREE_CODE_CLASS (code);
13672 tree tem;
13673 location_t loc = EXPR_LOCATION (expr);
13674
13675 /* Return right away if a constant. */
13676 if (kind == tcc_constant)
13677 return t;
13678
13679 /* CALL_EXPR-like objects with variable numbers of operands are
13680 treated specially. */
13681 if (kind == tcc_vl_exp)
13682 {
13683 if (code == CALL_EXPR)
13684 {
13685 tem = fold_call_expr (loc, expr, false);
13686 return tem ? tem : expr;
13687 }
13688 return expr;
13689 }
13690
13691 if (IS_EXPR_CODE_CLASS (kind))
13692 {
13693 tree type = TREE_TYPE (t);
13694 tree op0, op1, op2;
13695
13696 switch (TREE_CODE_LENGTH (code))
13697 {
13698 case 1:
13699 op0 = TREE_OPERAND (t, 0);
13700 tem = fold_unary_loc (loc, code, type, op0);
13701 return tem ? tem : expr;
13702 case 2:
13703 op0 = TREE_OPERAND (t, 0);
13704 op1 = TREE_OPERAND (t, 1);
13705 tem = fold_binary_loc (loc, code, type, op0, op1);
13706 return tem ? tem : expr;
13707 case 3:
13708 op0 = TREE_OPERAND (t, 0);
13709 op1 = TREE_OPERAND (t, 1);
13710 op2 = TREE_OPERAND (t, 2);
13711 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13712 return tem ? tem : expr;
13713 default:
13714 break;
13715 }
13716 }
13717
13718 switch (code)
13719 {
13720 case ARRAY_REF:
13721 {
13722 tree op0 = TREE_OPERAND (t, 0);
13723 tree op1 = TREE_OPERAND (t, 1);
13724
13725 if (TREE_CODE (op1) == INTEGER_CST
13726 && TREE_CODE (op0) == CONSTRUCTOR
13727 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13728 {
13729 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
13730 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
13731 unsigned HOST_WIDE_INT begin = 0;
13732
13733 /* Find a matching index by means of a binary search. */
13734 while (begin != end)
13735 {
13736 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13737 tree index = (*elts)[middle].index;
13738
13739 if (TREE_CODE (index) == INTEGER_CST
13740 && tree_int_cst_lt (index, op1))
13741 begin = middle + 1;
13742 else if (TREE_CODE (index) == INTEGER_CST
13743 && tree_int_cst_lt (op1, index))
13744 end = middle;
13745 else if (TREE_CODE (index) == RANGE_EXPR
13746 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13747 begin = middle + 1;
13748 else if (TREE_CODE (index) == RANGE_EXPR
13749 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13750 end = middle;
13751 else
13752 return (*elts)[middle].value;
13753 }
13754 }
13755
13756 return t;
13757 }
13758
13759 /* Return a VECTOR_CST if possible. */
13760 case CONSTRUCTOR:
13761 {
13762 tree type = TREE_TYPE (t);
13763 if (TREE_CODE (type) != VECTOR_TYPE)
13764 return t;
13765
13766 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
13767 unsigned HOST_WIDE_INT idx, pos = 0;
13768 tree value;
13769
13770 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
13771 {
13772 if (!CONSTANT_CLASS_P (value))
13773 return t;
13774 if (TREE_CODE (value) == VECTOR_CST)
13775 {
13776 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
13777 vec[pos++] = VECTOR_CST_ELT (value, i);
13778 }
13779 else
13780 vec[pos++] = value;
13781 }
13782 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
13783 vec[pos] = build_zero_cst (TREE_TYPE (type));
13784
13785 return build_vector (type, vec);
13786 }
13787
13788 case CONST_DECL:
13789 return fold (DECL_INITIAL (t));
13790
13791 default:
13792 return t;
13793 } /* switch (code) */
13794 }
13795
13796 #ifdef ENABLE_FOLD_CHECKING
13797 #undef fold
13798
13799 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13800 hash_table<nofree_ptr_hash<const tree_node> > *);
13801 static void fold_check_failed (const_tree, const_tree);
13802 void print_fold_checksum (const_tree);
13803
13804 /* When --enable-checking=fold, compute a digest of expr before
13805 and after actual fold call to see if fold did not accidentally
13806 change original expr. */
13807
13808 tree
13809 fold (tree expr)
13810 {
13811 tree ret;
13812 struct md5_ctx ctx;
13813 unsigned char checksum_before[16], checksum_after[16];
13814 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13815
13816 md5_init_ctx (&ctx);
13817 fold_checksum_tree (expr, &ctx, &ht);
13818 md5_finish_ctx (&ctx, checksum_before);
13819 ht.empty ();
13820
13821 ret = fold_1 (expr);
13822
13823 md5_init_ctx (&ctx);
13824 fold_checksum_tree (expr, &ctx, &ht);
13825 md5_finish_ctx (&ctx, checksum_after);
13826
13827 if (memcmp (checksum_before, checksum_after, 16))
13828 fold_check_failed (expr, ret);
13829
13830 return ret;
13831 }
13832
13833 void
13834 print_fold_checksum (const_tree expr)
13835 {
13836 struct md5_ctx ctx;
13837 unsigned char checksum[16], cnt;
13838 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13839
13840 md5_init_ctx (&ctx);
13841 fold_checksum_tree (expr, &ctx, &ht);
13842 md5_finish_ctx (&ctx, checksum);
13843 for (cnt = 0; cnt < 16; ++cnt)
13844 fprintf (stderr, "%02x", checksum[cnt]);
13845 putc ('\n', stderr);
13846 }
13847
13848 static void
13849 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13850 {
13851 internal_error ("fold check: original tree changed by fold");
13852 }
13853
13854 static void
13855 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13856 hash_table<nofree_ptr_hash <const tree_node> > *ht)
13857 {
13858 const tree_node **slot;
13859 enum tree_code code;
13860 union tree_node buf;
13861 int i, len;
13862
13863 recursive_label:
13864 if (expr == NULL)
13865 return;
13866 slot = ht->find_slot (expr, INSERT);
13867 if (*slot != NULL)
13868 return;
13869 *slot = expr;
13870 code = TREE_CODE (expr);
13871 if (TREE_CODE_CLASS (code) == tcc_declaration
13872 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13873 {
13874 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13875 memcpy ((char *) &buf, expr, tree_size (expr));
13876 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13877 buf.decl_with_vis.symtab_node = NULL;
13878 expr = (tree) &buf;
13879 }
13880 else if (TREE_CODE_CLASS (code) == tcc_type
13881 && (TYPE_POINTER_TO (expr)
13882 || TYPE_REFERENCE_TO (expr)
13883 || TYPE_CACHED_VALUES_P (expr)
13884 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13885 || TYPE_NEXT_VARIANT (expr)))
13886 {
13887 /* Allow these fields to be modified. */
13888 tree tmp;
13889 memcpy ((char *) &buf, expr, tree_size (expr));
13890 expr = tmp = (tree) &buf;
13891 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13892 TYPE_POINTER_TO (tmp) = NULL;
13893 TYPE_REFERENCE_TO (tmp) = NULL;
13894 TYPE_NEXT_VARIANT (tmp) = NULL;
13895 if (TYPE_CACHED_VALUES_P (tmp))
13896 {
13897 TYPE_CACHED_VALUES_P (tmp) = 0;
13898 TYPE_CACHED_VALUES (tmp) = NULL;
13899 }
13900 }
13901 md5_process_bytes (expr, tree_size (expr), ctx);
13902 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13903 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13904 if (TREE_CODE_CLASS (code) != tcc_type
13905 && TREE_CODE_CLASS (code) != tcc_declaration
13906 && code != TREE_LIST
13907 && code != SSA_NAME
13908 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13909 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13910 switch (TREE_CODE_CLASS (code))
13911 {
13912 case tcc_constant:
13913 switch (code)
13914 {
13915 case STRING_CST:
13916 md5_process_bytes (TREE_STRING_POINTER (expr),
13917 TREE_STRING_LENGTH (expr), ctx);
13918 break;
13919 case COMPLEX_CST:
13920 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13921 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13922 break;
13923 case VECTOR_CST:
13924 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
13925 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
13926 break;
13927 default:
13928 break;
13929 }
13930 break;
13931 case tcc_exceptional:
13932 switch (code)
13933 {
13934 case TREE_LIST:
13935 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13936 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13937 expr = TREE_CHAIN (expr);
13938 goto recursive_label;
13939 break;
13940 case TREE_VEC:
13941 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13942 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13943 break;
13944 default:
13945 break;
13946 }
13947 break;
13948 case tcc_expression:
13949 case tcc_reference:
13950 case tcc_comparison:
13951 case tcc_unary:
13952 case tcc_binary:
13953 case tcc_statement:
13954 case tcc_vl_exp:
13955 len = TREE_OPERAND_LENGTH (expr);
13956 for (i = 0; i < len; ++i)
13957 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13958 break;
13959 case tcc_declaration:
13960 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13961 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13962 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13963 {
13964 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13965 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13966 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13967 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13968 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13969 }
13970
13971 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13972 {
13973 if (TREE_CODE (expr) == FUNCTION_DECL)
13974 {
13975 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13976 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13977 }
13978 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13979 }
13980 break;
13981 case tcc_type:
13982 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13983 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13984 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13985 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13986 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13987 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13988 if (INTEGRAL_TYPE_P (expr)
13989 || SCALAR_FLOAT_TYPE_P (expr))
13990 {
13991 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13992 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13993 }
13994 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13995 if (TREE_CODE (expr) == RECORD_TYPE
13996 || TREE_CODE (expr) == UNION_TYPE
13997 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13998 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13999 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14000 break;
14001 default:
14002 break;
14003 }
14004 }
14005
14006 /* Helper function for outputting the checksum of a tree T. When
14007 debugging with gdb, you can "define mynext" to be "next" followed
14008 by "call debug_fold_checksum (op0)", then just trace down till the
14009 outputs differ. */
14010
14011 DEBUG_FUNCTION void
14012 debug_fold_checksum (const_tree t)
14013 {
14014 int i;
14015 unsigned char checksum[16];
14016 struct md5_ctx ctx;
14017 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14018
14019 md5_init_ctx (&ctx);
14020 fold_checksum_tree (t, &ctx, &ht);
14021 md5_finish_ctx (&ctx, checksum);
14022 ht.empty ();
14023
14024 for (i = 0; i < 16; i++)
14025 fprintf (stderr, "%d ", checksum[i]);
14026
14027 fprintf (stderr, "\n");
14028 }
14029
14030 #endif
14031
14032 /* Fold a unary tree expression with code CODE of type TYPE with an
14033 operand OP0. LOC is the location of the resulting expression.
14034 Return a folded expression if successful. Otherwise, return a tree
14035 expression with code CODE of type TYPE with an operand OP0. */
14036
14037 tree
14038 fold_build1_stat_loc (location_t loc,
14039 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14040 {
14041 tree tem;
14042 #ifdef ENABLE_FOLD_CHECKING
14043 unsigned char checksum_before[16], checksum_after[16];
14044 struct md5_ctx ctx;
14045 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14046
14047 md5_init_ctx (&ctx);
14048 fold_checksum_tree (op0, &ctx, &ht);
14049 md5_finish_ctx (&ctx, checksum_before);
14050 ht.empty ();
14051 #endif
14052
14053 tem = fold_unary_loc (loc, code, type, op0);
14054 if (!tem)
14055 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14056
14057 #ifdef ENABLE_FOLD_CHECKING
14058 md5_init_ctx (&ctx);
14059 fold_checksum_tree (op0, &ctx, &ht);
14060 md5_finish_ctx (&ctx, checksum_after);
14061
14062 if (memcmp (checksum_before, checksum_after, 16))
14063 fold_check_failed (op0, tem);
14064 #endif
14065 return tem;
14066 }
14067
14068 /* Fold a binary tree expression with code CODE of type TYPE with
14069 operands OP0 and OP1. LOC is the location of the resulting
14070 expression. Return a folded expression if successful. Otherwise,
14071 return a tree expression with code CODE of type TYPE with operands
14072 OP0 and OP1. */
14073
14074 tree
14075 fold_build2_stat_loc (location_t loc,
14076 enum tree_code code, tree type, tree op0, tree op1
14077 MEM_STAT_DECL)
14078 {
14079 tree tem;
14080 #ifdef ENABLE_FOLD_CHECKING
14081 unsigned char checksum_before_op0[16],
14082 checksum_before_op1[16],
14083 checksum_after_op0[16],
14084 checksum_after_op1[16];
14085 struct md5_ctx ctx;
14086 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14087
14088 md5_init_ctx (&ctx);
14089 fold_checksum_tree (op0, &ctx, &ht);
14090 md5_finish_ctx (&ctx, checksum_before_op0);
14091 ht.empty ();
14092
14093 md5_init_ctx (&ctx);
14094 fold_checksum_tree (op1, &ctx, &ht);
14095 md5_finish_ctx (&ctx, checksum_before_op1);
14096 ht.empty ();
14097 #endif
14098
14099 tem = fold_binary_loc (loc, code, type, op0, op1);
14100 if (!tem)
14101 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14102
14103 #ifdef ENABLE_FOLD_CHECKING
14104 md5_init_ctx (&ctx);
14105 fold_checksum_tree (op0, &ctx, &ht);
14106 md5_finish_ctx (&ctx, checksum_after_op0);
14107 ht.empty ();
14108
14109 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14110 fold_check_failed (op0, tem);
14111
14112 md5_init_ctx (&ctx);
14113 fold_checksum_tree (op1, &ctx, &ht);
14114 md5_finish_ctx (&ctx, checksum_after_op1);
14115
14116 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14117 fold_check_failed (op1, tem);
14118 #endif
14119 return tem;
14120 }
14121
14122 /* Fold a ternary tree expression with code CODE of type TYPE with
14123 operands OP0, OP1, and OP2. Return a folded expression if
14124 successful. Otherwise, return a tree expression with code CODE of
14125 type TYPE with operands OP0, OP1, and OP2. */
14126
14127 tree
14128 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14129 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14130 {
14131 tree tem;
14132 #ifdef ENABLE_FOLD_CHECKING
14133 unsigned char checksum_before_op0[16],
14134 checksum_before_op1[16],
14135 checksum_before_op2[16],
14136 checksum_after_op0[16],
14137 checksum_after_op1[16],
14138 checksum_after_op2[16];
14139 struct md5_ctx ctx;
14140 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14141
14142 md5_init_ctx (&ctx);
14143 fold_checksum_tree (op0, &ctx, &ht);
14144 md5_finish_ctx (&ctx, checksum_before_op0);
14145 ht.empty ();
14146
14147 md5_init_ctx (&ctx);
14148 fold_checksum_tree (op1, &ctx, &ht);
14149 md5_finish_ctx (&ctx, checksum_before_op1);
14150 ht.empty ();
14151
14152 md5_init_ctx (&ctx);
14153 fold_checksum_tree (op2, &ctx, &ht);
14154 md5_finish_ctx (&ctx, checksum_before_op2);
14155 ht.empty ();
14156 #endif
14157
14158 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14159 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14160 if (!tem)
14161 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14162
14163 #ifdef ENABLE_FOLD_CHECKING
14164 md5_init_ctx (&ctx);
14165 fold_checksum_tree (op0, &ctx, &ht);
14166 md5_finish_ctx (&ctx, checksum_after_op0);
14167 ht.empty ();
14168
14169 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14170 fold_check_failed (op0, tem);
14171
14172 md5_init_ctx (&ctx);
14173 fold_checksum_tree (op1, &ctx, &ht);
14174 md5_finish_ctx (&ctx, checksum_after_op1);
14175 ht.empty ();
14176
14177 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14178 fold_check_failed (op1, tem);
14179
14180 md5_init_ctx (&ctx);
14181 fold_checksum_tree (op2, &ctx, &ht);
14182 md5_finish_ctx (&ctx, checksum_after_op2);
14183
14184 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14185 fold_check_failed (op2, tem);
14186 #endif
14187 return tem;
14188 }
14189
14190 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14191 arguments in ARGARRAY, and a null static chain.
14192 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14193 of type TYPE from the given operands as constructed by build_call_array. */
14194
14195 tree
14196 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14197 int nargs, tree *argarray)
14198 {
14199 tree tem;
14200 #ifdef ENABLE_FOLD_CHECKING
14201 unsigned char checksum_before_fn[16],
14202 checksum_before_arglist[16],
14203 checksum_after_fn[16],
14204 checksum_after_arglist[16];
14205 struct md5_ctx ctx;
14206 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14207 int i;
14208
14209 md5_init_ctx (&ctx);
14210 fold_checksum_tree (fn, &ctx, &ht);
14211 md5_finish_ctx (&ctx, checksum_before_fn);
14212 ht.empty ();
14213
14214 md5_init_ctx (&ctx);
14215 for (i = 0; i < nargs; i++)
14216 fold_checksum_tree (argarray[i], &ctx, &ht);
14217 md5_finish_ctx (&ctx, checksum_before_arglist);
14218 ht.empty ();
14219 #endif
14220
14221 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14222 if (!tem)
14223 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14224
14225 #ifdef ENABLE_FOLD_CHECKING
14226 md5_init_ctx (&ctx);
14227 fold_checksum_tree (fn, &ctx, &ht);
14228 md5_finish_ctx (&ctx, checksum_after_fn);
14229 ht.empty ();
14230
14231 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14232 fold_check_failed (fn, tem);
14233
14234 md5_init_ctx (&ctx);
14235 for (i = 0; i < nargs; i++)
14236 fold_checksum_tree (argarray[i], &ctx, &ht);
14237 md5_finish_ctx (&ctx, checksum_after_arglist);
14238
14239 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14240 fold_check_failed (NULL_TREE, tem);
14241 #endif
14242 return tem;
14243 }
14244
14245 /* Perform constant folding and related simplification of initializer
14246 expression EXPR. These behave identically to "fold_buildN" but ignore
14247 potential run-time traps and exceptions that fold must preserve. */
14248
14249 #define START_FOLD_INIT \
14250 int saved_signaling_nans = flag_signaling_nans;\
14251 int saved_trapping_math = flag_trapping_math;\
14252 int saved_rounding_math = flag_rounding_math;\
14253 int saved_trapv = flag_trapv;\
14254 int saved_folding_initializer = folding_initializer;\
14255 flag_signaling_nans = 0;\
14256 flag_trapping_math = 0;\
14257 flag_rounding_math = 0;\
14258 flag_trapv = 0;\
14259 folding_initializer = 1;
14260
14261 #define END_FOLD_INIT \
14262 flag_signaling_nans = saved_signaling_nans;\
14263 flag_trapping_math = saved_trapping_math;\
14264 flag_rounding_math = saved_rounding_math;\
14265 flag_trapv = saved_trapv;\
14266 folding_initializer = saved_folding_initializer;
14267
14268 tree
14269 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14270 tree type, tree op)
14271 {
14272 tree result;
14273 START_FOLD_INIT;
14274
14275 result = fold_build1_loc (loc, code, type, op);
14276
14277 END_FOLD_INIT;
14278 return result;
14279 }
14280
14281 tree
14282 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14283 tree type, tree op0, tree op1)
14284 {
14285 tree result;
14286 START_FOLD_INIT;
14287
14288 result = fold_build2_loc (loc, code, type, op0, op1);
14289
14290 END_FOLD_INIT;
14291 return result;
14292 }
14293
14294 tree
14295 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14296 int nargs, tree *argarray)
14297 {
14298 tree result;
14299 START_FOLD_INIT;
14300
14301 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14302
14303 END_FOLD_INIT;
14304 return result;
14305 }
14306
14307 #undef START_FOLD_INIT
14308 #undef END_FOLD_INIT
14309
14310 /* Determine if first argument is a multiple of second argument. Return 0 if
14311 it is not, or we cannot easily determined it to be.
14312
14313 An example of the sort of thing we care about (at this point; this routine
14314 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14315 fold cases do now) is discovering that
14316
14317 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14318
14319 is a multiple of
14320
14321 SAVE_EXPR (J * 8)
14322
14323 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14324
14325 This code also handles discovering that
14326
14327 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14328
14329 is a multiple of 8 so we don't have to worry about dealing with a
14330 possible remainder.
14331
14332 Note that we *look* inside a SAVE_EXPR only to determine how it was
14333 calculated; it is not safe for fold to do much of anything else with the
14334 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14335 at run time. For example, the latter example above *cannot* be implemented
14336 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14337 evaluation time of the original SAVE_EXPR is not necessarily the same at
14338 the time the new expression is evaluated. The only optimization of this
14339 sort that would be valid is changing
14340
14341 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14342
14343 divided by 8 to
14344
14345 SAVE_EXPR (I) * SAVE_EXPR (J)
14346
14347 (where the same SAVE_EXPR (J) is used in the original and the
14348 transformed version). */
14349
14350 int
14351 multiple_of_p (tree type, const_tree top, const_tree bottom)
14352 {
14353 if (operand_equal_p (top, bottom, 0))
14354 return 1;
14355
14356 if (TREE_CODE (type) != INTEGER_TYPE)
14357 return 0;
14358
14359 switch (TREE_CODE (top))
14360 {
14361 case BIT_AND_EXPR:
14362 /* Bitwise and provides a power of two multiple. If the mask is
14363 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14364 if (!integer_pow2p (bottom))
14365 return 0;
14366 /* FALLTHRU */
14367
14368 case MULT_EXPR:
14369 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14370 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14371
14372 case PLUS_EXPR:
14373 case MINUS_EXPR:
14374 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14375 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14376
14377 case LSHIFT_EXPR:
14378 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14379 {
14380 tree op1, t1;
14381
14382 op1 = TREE_OPERAND (top, 1);
14383 /* const_binop may not detect overflow correctly,
14384 so check for it explicitly here. */
14385 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
14386 && 0 != (t1 = fold_convert (type,
14387 const_binop (LSHIFT_EXPR,
14388 size_one_node,
14389 op1)))
14390 && !TREE_OVERFLOW (t1))
14391 return multiple_of_p (type, t1, bottom);
14392 }
14393 return 0;
14394
14395 case NOP_EXPR:
14396 /* Can't handle conversions from non-integral or wider integral type. */
14397 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14398 || (TYPE_PRECISION (type)
14399 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14400 return 0;
14401
14402 /* .. fall through ... */
14403
14404 case SAVE_EXPR:
14405 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14406
14407 case COND_EXPR:
14408 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14409 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14410
14411 case INTEGER_CST:
14412 if (TREE_CODE (bottom) != INTEGER_CST
14413 || integer_zerop (bottom)
14414 || (TYPE_UNSIGNED (type)
14415 && (tree_int_cst_sgn (top) < 0
14416 || tree_int_cst_sgn (bottom) < 0)))
14417 return 0;
14418 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14419 SIGNED);
14420
14421 default:
14422 return 0;
14423 }
14424 }
14425
14426 /* Return true if CODE or TYPE is known to be non-negative. */
14427
14428 static bool
14429 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14430 {
14431 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14432 && truth_value_p (code))
14433 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14434 have a signed:1 type (where the value is -1 and 0). */
14435 return true;
14436 return false;
14437 }
14438
14439 /* Return true if (CODE OP0) is known to be non-negative. If the return
14440 value is based on the assumption that signed overflow is undefined,
14441 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14442 *STRICT_OVERFLOW_P. */
14443
14444 bool
14445 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14446 bool *strict_overflow_p)
14447 {
14448 if (TYPE_UNSIGNED (type))
14449 return true;
14450
14451 switch (code)
14452 {
14453 case ABS_EXPR:
14454 /* We can't return 1 if flag_wrapv is set because
14455 ABS_EXPR<INT_MIN> = INT_MIN. */
14456 if (!ANY_INTEGRAL_TYPE_P (type))
14457 return true;
14458 if (TYPE_OVERFLOW_UNDEFINED (type))
14459 {
14460 *strict_overflow_p = true;
14461 return true;
14462 }
14463 break;
14464
14465 case NON_LVALUE_EXPR:
14466 case FLOAT_EXPR:
14467 case FIX_TRUNC_EXPR:
14468 return tree_expr_nonnegative_warnv_p (op0,
14469 strict_overflow_p);
14470
14471 CASE_CONVERT:
14472 {
14473 tree inner_type = TREE_TYPE (op0);
14474 tree outer_type = type;
14475
14476 if (TREE_CODE (outer_type) == REAL_TYPE)
14477 {
14478 if (TREE_CODE (inner_type) == REAL_TYPE)
14479 return tree_expr_nonnegative_warnv_p (op0,
14480 strict_overflow_p);
14481 if (INTEGRAL_TYPE_P (inner_type))
14482 {
14483 if (TYPE_UNSIGNED (inner_type))
14484 return true;
14485 return tree_expr_nonnegative_warnv_p (op0,
14486 strict_overflow_p);
14487 }
14488 }
14489 else if (INTEGRAL_TYPE_P (outer_type))
14490 {
14491 if (TREE_CODE (inner_type) == REAL_TYPE)
14492 return tree_expr_nonnegative_warnv_p (op0,
14493 strict_overflow_p);
14494 if (INTEGRAL_TYPE_P (inner_type))
14495 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14496 && TYPE_UNSIGNED (inner_type);
14497 }
14498 }
14499 break;
14500
14501 default:
14502 return tree_simple_nonnegative_warnv_p (code, type);
14503 }
14504
14505 /* We don't know sign of `t', so be conservative and return false. */
14506 return false;
14507 }
14508
14509 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14510 value is based on the assumption that signed overflow is undefined,
14511 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14512 *STRICT_OVERFLOW_P. */
14513
14514 bool
14515 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14516 tree op1, bool *strict_overflow_p)
14517 {
14518 if (TYPE_UNSIGNED (type))
14519 return true;
14520
14521 switch (code)
14522 {
14523 case POINTER_PLUS_EXPR:
14524 case PLUS_EXPR:
14525 if (FLOAT_TYPE_P (type))
14526 return (tree_expr_nonnegative_warnv_p (op0,
14527 strict_overflow_p)
14528 && tree_expr_nonnegative_warnv_p (op1,
14529 strict_overflow_p));
14530
14531 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14532 both unsigned and at least 2 bits shorter than the result. */
14533 if (TREE_CODE (type) == INTEGER_TYPE
14534 && TREE_CODE (op0) == NOP_EXPR
14535 && TREE_CODE (op1) == NOP_EXPR)
14536 {
14537 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14538 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14539 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14540 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14541 {
14542 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14543 TYPE_PRECISION (inner2)) + 1;
14544 return prec < TYPE_PRECISION (type);
14545 }
14546 }
14547 break;
14548
14549 case MULT_EXPR:
14550 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14551 {
14552 /* x * x is always non-negative for floating point x
14553 or without overflow. */
14554 if (operand_equal_p (op0, op1, 0)
14555 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
14556 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
14557 {
14558 if (ANY_INTEGRAL_TYPE_P (type)
14559 && TYPE_OVERFLOW_UNDEFINED (type))
14560 *strict_overflow_p = true;
14561 return true;
14562 }
14563 }
14564
14565 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14566 both unsigned and their total bits is shorter than the result. */
14567 if (TREE_CODE (type) == INTEGER_TYPE
14568 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14569 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14570 {
14571 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14572 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14573 : TREE_TYPE (op0);
14574 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14575 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14576 : TREE_TYPE (op1);
14577
14578 bool unsigned0 = TYPE_UNSIGNED (inner0);
14579 bool unsigned1 = TYPE_UNSIGNED (inner1);
14580
14581 if (TREE_CODE (op0) == INTEGER_CST)
14582 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14583
14584 if (TREE_CODE (op1) == INTEGER_CST)
14585 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14586
14587 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14588 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14589 {
14590 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14591 ? tree_int_cst_min_precision (op0, UNSIGNED)
14592 : TYPE_PRECISION (inner0);
14593
14594 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14595 ? tree_int_cst_min_precision (op1, UNSIGNED)
14596 : TYPE_PRECISION (inner1);
14597
14598 return precision0 + precision1 < TYPE_PRECISION (type);
14599 }
14600 }
14601 return false;
14602
14603 case BIT_AND_EXPR:
14604 case MAX_EXPR:
14605 return (tree_expr_nonnegative_warnv_p (op0,
14606 strict_overflow_p)
14607 || tree_expr_nonnegative_warnv_p (op1,
14608 strict_overflow_p));
14609
14610 case BIT_IOR_EXPR:
14611 case BIT_XOR_EXPR:
14612 case MIN_EXPR:
14613 case RDIV_EXPR:
14614 case TRUNC_DIV_EXPR:
14615 case CEIL_DIV_EXPR:
14616 case FLOOR_DIV_EXPR:
14617 case ROUND_DIV_EXPR:
14618 return (tree_expr_nonnegative_warnv_p (op0,
14619 strict_overflow_p)
14620 && tree_expr_nonnegative_warnv_p (op1,
14621 strict_overflow_p));
14622
14623 case TRUNC_MOD_EXPR:
14624 case CEIL_MOD_EXPR:
14625 case FLOOR_MOD_EXPR:
14626 case ROUND_MOD_EXPR:
14627 return tree_expr_nonnegative_warnv_p (op0,
14628 strict_overflow_p);
14629 default:
14630 return tree_simple_nonnegative_warnv_p (code, type);
14631 }
14632
14633 /* We don't know sign of `t', so be conservative and return false. */
14634 return false;
14635 }
14636
14637 /* Return true if T is known to be non-negative. If the return
14638 value is based on the assumption that signed overflow is undefined,
14639 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14640 *STRICT_OVERFLOW_P. */
14641
14642 bool
14643 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14644 {
14645 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14646 return true;
14647
14648 switch (TREE_CODE (t))
14649 {
14650 case INTEGER_CST:
14651 return tree_int_cst_sgn (t) >= 0;
14652
14653 case REAL_CST:
14654 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14655
14656 case FIXED_CST:
14657 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14658
14659 case COND_EXPR:
14660 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14661 strict_overflow_p)
14662 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14663 strict_overflow_p));
14664 default:
14665 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14666 TREE_TYPE (t));
14667 }
14668 /* We don't know sign of `t', so be conservative and return false. */
14669 return false;
14670 }
14671
14672 /* Return true if T is known to be non-negative. If the return
14673 value is based on the assumption that signed overflow is undefined,
14674 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14675 *STRICT_OVERFLOW_P. */
14676
14677 bool
14678 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14679 tree arg0, tree arg1, bool *strict_overflow_p)
14680 {
14681 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14682 switch (DECL_FUNCTION_CODE (fndecl))
14683 {
14684 CASE_FLT_FN (BUILT_IN_ACOS):
14685 CASE_FLT_FN (BUILT_IN_ACOSH):
14686 CASE_FLT_FN (BUILT_IN_CABS):
14687 CASE_FLT_FN (BUILT_IN_COSH):
14688 CASE_FLT_FN (BUILT_IN_ERFC):
14689 CASE_FLT_FN (BUILT_IN_EXP):
14690 CASE_FLT_FN (BUILT_IN_EXP10):
14691 CASE_FLT_FN (BUILT_IN_EXP2):
14692 CASE_FLT_FN (BUILT_IN_FABS):
14693 CASE_FLT_FN (BUILT_IN_FDIM):
14694 CASE_FLT_FN (BUILT_IN_HYPOT):
14695 CASE_FLT_FN (BUILT_IN_POW10):
14696 CASE_INT_FN (BUILT_IN_FFS):
14697 CASE_INT_FN (BUILT_IN_PARITY):
14698 CASE_INT_FN (BUILT_IN_POPCOUNT):
14699 CASE_INT_FN (BUILT_IN_CLZ):
14700 CASE_INT_FN (BUILT_IN_CLRSB):
14701 case BUILT_IN_BSWAP32:
14702 case BUILT_IN_BSWAP64:
14703 /* Always true. */
14704 return true;
14705
14706 CASE_FLT_FN (BUILT_IN_SQRT):
14707 /* sqrt(-0.0) is -0.0. */
14708 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
14709 return true;
14710 return tree_expr_nonnegative_warnv_p (arg0,
14711 strict_overflow_p);
14712
14713 CASE_FLT_FN (BUILT_IN_ASINH):
14714 CASE_FLT_FN (BUILT_IN_ATAN):
14715 CASE_FLT_FN (BUILT_IN_ATANH):
14716 CASE_FLT_FN (BUILT_IN_CBRT):
14717 CASE_FLT_FN (BUILT_IN_CEIL):
14718 CASE_FLT_FN (BUILT_IN_ERF):
14719 CASE_FLT_FN (BUILT_IN_EXPM1):
14720 CASE_FLT_FN (BUILT_IN_FLOOR):
14721 CASE_FLT_FN (BUILT_IN_FMOD):
14722 CASE_FLT_FN (BUILT_IN_FREXP):
14723 CASE_FLT_FN (BUILT_IN_ICEIL):
14724 CASE_FLT_FN (BUILT_IN_IFLOOR):
14725 CASE_FLT_FN (BUILT_IN_IRINT):
14726 CASE_FLT_FN (BUILT_IN_IROUND):
14727 CASE_FLT_FN (BUILT_IN_LCEIL):
14728 CASE_FLT_FN (BUILT_IN_LDEXP):
14729 CASE_FLT_FN (BUILT_IN_LFLOOR):
14730 CASE_FLT_FN (BUILT_IN_LLCEIL):
14731 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14732 CASE_FLT_FN (BUILT_IN_LLRINT):
14733 CASE_FLT_FN (BUILT_IN_LLROUND):
14734 CASE_FLT_FN (BUILT_IN_LRINT):
14735 CASE_FLT_FN (BUILT_IN_LROUND):
14736 CASE_FLT_FN (BUILT_IN_MODF):
14737 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14738 CASE_FLT_FN (BUILT_IN_RINT):
14739 CASE_FLT_FN (BUILT_IN_ROUND):
14740 CASE_FLT_FN (BUILT_IN_SCALB):
14741 CASE_FLT_FN (BUILT_IN_SCALBLN):
14742 CASE_FLT_FN (BUILT_IN_SCALBN):
14743 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14744 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14745 CASE_FLT_FN (BUILT_IN_SINH):
14746 CASE_FLT_FN (BUILT_IN_TANH):
14747 CASE_FLT_FN (BUILT_IN_TRUNC):
14748 /* True if the 1st argument is nonnegative. */
14749 return tree_expr_nonnegative_warnv_p (arg0,
14750 strict_overflow_p);
14751
14752 CASE_FLT_FN (BUILT_IN_FMAX):
14753 /* True if the 1st OR 2nd arguments are nonnegative. */
14754 return (tree_expr_nonnegative_warnv_p (arg0,
14755 strict_overflow_p)
14756 || (tree_expr_nonnegative_warnv_p (arg1,
14757 strict_overflow_p)));
14758
14759 CASE_FLT_FN (BUILT_IN_FMIN):
14760 /* True if the 1st AND 2nd arguments are nonnegative. */
14761 return (tree_expr_nonnegative_warnv_p (arg0,
14762 strict_overflow_p)
14763 && (tree_expr_nonnegative_warnv_p (arg1,
14764 strict_overflow_p)));
14765
14766 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14767 /* True if the 2nd argument is nonnegative. */
14768 return tree_expr_nonnegative_warnv_p (arg1,
14769 strict_overflow_p);
14770
14771 CASE_FLT_FN (BUILT_IN_POWI):
14772 /* True if the 1st argument is nonnegative or the second
14773 argument is an even integer. */
14774 if (TREE_CODE (arg1) == INTEGER_CST
14775 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14776 return true;
14777 return tree_expr_nonnegative_warnv_p (arg0,
14778 strict_overflow_p);
14779
14780 CASE_FLT_FN (BUILT_IN_POW):
14781 /* True if the 1st argument is nonnegative or the second
14782 argument is an even integer valued real. */
14783 if (TREE_CODE (arg1) == REAL_CST)
14784 {
14785 REAL_VALUE_TYPE c;
14786 HOST_WIDE_INT n;
14787
14788 c = TREE_REAL_CST (arg1);
14789 n = real_to_integer (&c);
14790 if ((n & 1) == 0)
14791 {
14792 REAL_VALUE_TYPE cint;
14793 real_from_integer (&cint, VOIDmode, n, SIGNED);
14794 if (real_identical (&c, &cint))
14795 return true;
14796 }
14797 }
14798 return tree_expr_nonnegative_warnv_p (arg0,
14799 strict_overflow_p);
14800
14801 default:
14802 break;
14803 }
14804 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14805 type);
14806 }
14807
14808 /* Return true if T is known to be non-negative. If the return
14809 value is based on the assumption that signed overflow is undefined,
14810 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14811 *STRICT_OVERFLOW_P. */
14812
14813 static bool
14814 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14815 {
14816 enum tree_code code = TREE_CODE (t);
14817 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14818 return true;
14819
14820 switch (code)
14821 {
14822 case TARGET_EXPR:
14823 {
14824 tree temp = TARGET_EXPR_SLOT (t);
14825 t = TARGET_EXPR_INITIAL (t);
14826
14827 /* If the initializer is non-void, then it's a normal expression
14828 that will be assigned to the slot. */
14829 if (!VOID_TYPE_P (t))
14830 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14831
14832 /* Otherwise, the initializer sets the slot in some way. One common
14833 way is an assignment statement at the end of the initializer. */
14834 while (1)
14835 {
14836 if (TREE_CODE (t) == BIND_EXPR)
14837 t = expr_last (BIND_EXPR_BODY (t));
14838 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14839 || TREE_CODE (t) == TRY_CATCH_EXPR)
14840 t = expr_last (TREE_OPERAND (t, 0));
14841 else if (TREE_CODE (t) == STATEMENT_LIST)
14842 t = expr_last (t);
14843 else
14844 break;
14845 }
14846 if (TREE_CODE (t) == MODIFY_EXPR
14847 && TREE_OPERAND (t, 0) == temp)
14848 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14849 strict_overflow_p);
14850
14851 return false;
14852 }
14853
14854 case CALL_EXPR:
14855 {
14856 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14857 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14858
14859 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14860 get_callee_fndecl (t),
14861 arg0,
14862 arg1,
14863 strict_overflow_p);
14864 }
14865 case COMPOUND_EXPR:
14866 case MODIFY_EXPR:
14867 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14868 strict_overflow_p);
14869 case BIND_EXPR:
14870 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14871 strict_overflow_p);
14872 case SAVE_EXPR:
14873 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14874 strict_overflow_p);
14875
14876 default:
14877 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14878 TREE_TYPE (t));
14879 }
14880
14881 /* We don't know sign of `t', so be conservative and return false. */
14882 return false;
14883 }
14884
14885 /* Return true if T is known to be non-negative. If the return
14886 value is based on the assumption that signed overflow is undefined,
14887 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14888 *STRICT_OVERFLOW_P. */
14889
14890 bool
14891 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14892 {
14893 enum tree_code code;
14894 if (t == error_mark_node)
14895 return false;
14896
14897 code = TREE_CODE (t);
14898 switch (TREE_CODE_CLASS (code))
14899 {
14900 case tcc_binary:
14901 case tcc_comparison:
14902 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14903 TREE_TYPE (t),
14904 TREE_OPERAND (t, 0),
14905 TREE_OPERAND (t, 1),
14906 strict_overflow_p);
14907
14908 case tcc_unary:
14909 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14910 TREE_TYPE (t),
14911 TREE_OPERAND (t, 0),
14912 strict_overflow_p);
14913
14914 case tcc_constant:
14915 case tcc_declaration:
14916 case tcc_reference:
14917 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14918
14919 default:
14920 break;
14921 }
14922
14923 switch (code)
14924 {
14925 case TRUTH_AND_EXPR:
14926 case TRUTH_OR_EXPR:
14927 case TRUTH_XOR_EXPR:
14928 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14929 TREE_TYPE (t),
14930 TREE_OPERAND (t, 0),
14931 TREE_OPERAND (t, 1),
14932 strict_overflow_p);
14933 case TRUTH_NOT_EXPR:
14934 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14935 TREE_TYPE (t),
14936 TREE_OPERAND (t, 0),
14937 strict_overflow_p);
14938
14939 case COND_EXPR:
14940 case CONSTRUCTOR:
14941 case OBJ_TYPE_REF:
14942 case ASSERT_EXPR:
14943 case ADDR_EXPR:
14944 case WITH_SIZE_EXPR:
14945 case SSA_NAME:
14946 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14947
14948 default:
14949 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14950 }
14951 }
14952
14953 /* Return true if `t' is known to be non-negative. Handle warnings
14954 about undefined signed overflow. */
14955
14956 bool
14957 tree_expr_nonnegative_p (tree t)
14958 {
14959 bool ret, strict_overflow_p;
14960
14961 strict_overflow_p = false;
14962 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14963 if (strict_overflow_p)
14964 fold_overflow_warning (("assuming signed overflow does not occur when "
14965 "determining that expression is always "
14966 "non-negative"),
14967 WARN_STRICT_OVERFLOW_MISC);
14968 return ret;
14969 }
14970
14971
14972 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14973 For floating point we further ensure that T is not denormal.
14974 Similar logic is present in nonzero_address in rtlanal.h.
14975
14976 If the return value is based on the assumption that signed overflow
14977 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14978 change *STRICT_OVERFLOW_P. */
14979
14980 bool
14981 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14982 bool *strict_overflow_p)
14983 {
14984 switch (code)
14985 {
14986 case ABS_EXPR:
14987 return tree_expr_nonzero_warnv_p (op0,
14988 strict_overflow_p);
14989
14990 case NOP_EXPR:
14991 {
14992 tree inner_type = TREE_TYPE (op0);
14993 tree outer_type = type;
14994
14995 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14996 && tree_expr_nonzero_warnv_p (op0,
14997 strict_overflow_p));
14998 }
14999 break;
15000
15001 case NON_LVALUE_EXPR:
15002 return tree_expr_nonzero_warnv_p (op0,
15003 strict_overflow_p);
15004
15005 default:
15006 break;
15007 }
15008
15009 return false;
15010 }
15011
15012 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15013 For floating point we further ensure that T is not denormal.
15014 Similar logic is present in nonzero_address in rtlanal.h.
15015
15016 If the return value is based on the assumption that signed overflow
15017 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15018 change *STRICT_OVERFLOW_P. */
15019
15020 bool
15021 tree_binary_nonzero_warnv_p (enum tree_code code,
15022 tree type,
15023 tree op0,
15024 tree op1, bool *strict_overflow_p)
15025 {
15026 bool sub_strict_overflow_p;
15027 switch (code)
15028 {
15029 case POINTER_PLUS_EXPR:
15030 case PLUS_EXPR:
15031 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15032 {
15033 /* With the presence of negative values it is hard
15034 to say something. */
15035 sub_strict_overflow_p = false;
15036 if (!tree_expr_nonnegative_warnv_p (op0,
15037 &sub_strict_overflow_p)
15038 || !tree_expr_nonnegative_warnv_p (op1,
15039 &sub_strict_overflow_p))
15040 return false;
15041 /* One of operands must be positive and the other non-negative. */
15042 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15043 overflows, on a twos-complement machine the sum of two
15044 nonnegative numbers can never be zero. */
15045 return (tree_expr_nonzero_warnv_p (op0,
15046 strict_overflow_p)
15047 || tree_expr_nonzero_warnv_p (op1,
15048 strict_overflow_p));
15049 }
15050 break;
15051
15052 case MULT_EXPR:
15053 if (TYPE_OVERFLOW_UNDEFINED (type))
15054 {
15055 if (tree_expr_nonzero_warnv_p (op0,
15056 strict_overflow_p)
15057 && tree_expr_nonzero_warnv_p (op1,
15058 strict_overflow_p))
15059 {
15060 *strict_overflow_p = true;
15061 return true;
15062 }
15063 }
15064 break;
15065
15066 case MIN_EXPR:
15067 sub_strict_overflow_p = false;
15068 if (tree_expr_nonzero_warnv_p (op0,
15069 &sub_strict_overflow_p)
15070 && tree_expr_nonzero_warnv_p (op1,
15071 &sub_strict_overflow_p))
15072 {
15073 if (sub_strict_overflow_p)
15074 *strict_overflow_p = true;
15075 }
15076 break;
15077
15078 case MAX_EXPR:
15079 sub_strict_overflow_p = false;
15080 if (tree_expr_nonzero_warnv_p (op0,
15081 &sub_strict_overflow_p))
15082 {
15083 if (sub_strict_overflow_p)
15084 *strict_overflow_p = true;
15085
15086 /* When both operands are nonzero, then MAX must be too. */
15087 if (tree_expr_nonzero_warnv_p (op1,
15088 strict_overflow_p))
15089 return true;
15090
15091 /* MAX where operand 0 is positive is positive. */
15092 return tree_expr_nonnegative_warnv_p (op0,
15093 strict_overflow_p);
15094 }
15095 /* MAX where operand 1 is positive is positive. */
15096 else if (tree_expr_nonzero_warnv_p (op1,
15097 &sub_strict_overflow_p)
15098 && tree_expr_nonnegative_warnv_p (op1,
15099 &sub_strict_overflow_p))
15100 {
15101 if (sub_strict_overflow_p)
15102 *strict_overflow_p = true;
15103 return true;
15104 }
15105 break;
15106
15107 case BIT_IOR_EXPR:
15108 return (tree_expr_nonzero_warnv_p (op1,
15109 strict_overflow_p)
15110 || tree_expr_nonzero_warnv_p (op0,
15111 strict_overflow_p));
15112
15113 default:
15114 break;
15115 }
15116
15117 return false;
15118 }
15119
15120 /* Return true when T is an address and is known to be nonzero.
15121 For floating point we further ensure that T is not denormal.
15122 Similar logic is present in nonzero_address in rtlanal.h.
15123
15124 If the return value is based on the assumption that signed overflow
15125 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15126 change *STRICT_OVERFLOW_P. */
15127
15128 bool
15129 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15130 {
15131 bool sub_strict_overflow_p;
15132 switch (TREE_CODE (t))
15133 {
15134 case INTEGER_CST:
15135 return !integer_zerop (t);
15136
15137 case ADDR_EXPR:
15138 {
15139 tree base = TREE_OPERAND (t, 0);
15140
15141 if (!DECL_P (base))
15142 base = get_base_address (base);
15143
15144 if (!base)
15145 return false;
15146
15147 /* For objects in symbol table check if we know they are non-zero.
15148 Don't do anything for variables and functions before symtab is built;
15149 it is quite possible that they will be declared weak later. */
15150 if (DECL_P (base) && decl_in_symtab_p (base))
15151 {
15152 struct symtab_node *symbol;
15153
15154 symbol = symtab_node::get_create (base);
15155 if (symbol)
15156 return symbol->nonzero_address ();
15157 else
15158 return false;
15159 }
15160
15161 /* Function local objects are never NULL. */
15162 if (DECL_P (base)
15163 && (DECL_CONTEXT (base)
15164 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15165 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
15166 return true;
15167
15168 /* Constants are never weak. */
15169 if (CONSTANT_CLASS_P (base))
15170 return true;
15171
15172 return false;
15173 }
15174
15175 case COND_EXPR:
15176 sub_strict_overflow_p = false;
15177 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15178 &sub_strict_overflow_p)
15179 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15180 &sub_strict_overflow_p))
15181 {
15182 if (sub_strict_overflow_p)
15183 *strict_overflow_p = true;
15184 return true;
15185 }
15186 break;
15187
15188 default:
15189 break;
15190 }
15191 return false;
15192 }
15193
15194 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15195 attempt to fold the expression to a constant without modifying TYPE,
15196 OP0 or OP1.
15197
15198 If the expression could be simplified to a constant, then return
15199 the constant. If the expression would not be simplified to a
15200 constant, then return NULL_TREE. */
15201
15202 tree
15203 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15204 {
15205 tree tem = fold_binary (code, type, op0, op1);
15206 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15207 }
15208
15209 /* Given the components of a unary expression CODE, TYPE and OP0,
15210 attempt to fold the expression to a constant without modifying
15211 TYPE or OP0.
15212
15213 If the expression could be simplified to a constant, then return
15214 the constant. If the expression would not be simplified to a
15215 constant, then return NULL_TREE. */
15216
15217 tree
15218 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15219 {
15220 tree tem = fold_unary (code, type, op0);
15221 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15222 }
15223
15224 /* If EXP represents referencing an element in a constant string
15225 (either via pointer arithmetic or array indexing), return the
15226 tree representing the value accessed, otherwise return NULL. */
15227
15228 tree
15229 fold_read_from_constant_string (tree exp)
15230 {
15231 if ((TREE_CODE (exp) == INDIRECT_REF
15232 || TREE_CODE (exp) == ARRAY_REF)
15233 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15234 {
15235 tree exp1 = TREE_OPERAND (exp, 0);
15236 tree index;
15237 tree string;
15238 location_t loc = EXPR_LOCATION (exp);
15239
15240 if (TREE_CODE (exp) == INDIRECT_REF)
15241 string = string_constant (exp1, &index);
15242 else
15243 {
15244 tree low_bound = array_ref_low_bound (exp);
15245 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15246
15247 /* Optimize the special-case of a zero lower bound.
15248
15249 We convert the low_bound to sizetype to avoid some problems
15250 with constant folding. (E.g. suppose the lower bound is 1,
15251 and its mode is QI. Without the conversion,l (ARRAY
15252 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15253 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15254 if (! integer_zerop (low_bound))
15255 index = size_diffop_loc (loc, index,
15256 fold_convert_loc (loc, sizetype, low_bound));
15257
15258 string = exp1;
15259 }
15260
15261 if (string
15262 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15263 && TREE_CODE (string) == STRING_CST
15264 && TREE_CODE (index) == INTEGER_CST
15265 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15266 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15267 == MODE_INT)
15268 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15269 return build_int_cst_type (TREE_TYPE (exp),
15270 (TREE_STRING_POINTER (string)
15271 [TREE_INT_CST_LOW (index)]));
15272 }
15273 return NULL;
15274 }
15275
15276 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15277 an integer constant, real, or fixed-point constant.
15278
15279 TYPE is the type of the result. */
15280
15281 static tree
15282 fold_negate_const (tree arg0, tree type)
15283 {
15284 tree t = NULL_TREE;
15285
15286 switch (TREE_CODE (arg0))
15287 {
15288 case INTEGER_CST:
15289 {
15290 bool overflow;
15291 wide_int val = wi::neg (arg0, &overflow);
15292 t = force_fit_type (type, val, 1,
15293 (overflow | TREE_OVERFLOW (arg0))
15294 && !TYPE_UNSIGNED (type));
15295 break;
15296 }
15297
15298 case REAL_CST:
15299 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15300 break;
15301
15302 case FIXED_CST:
15303 {
15304 FIXED_VALUE_TYPE f;
15305 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15306 &(TREE_FIXED_CST (arg0)), NULL,
15307 TYPE_SATURATING (type));
15308 t = build_fixed (type, f);
15309 /* Propagate overflow flags. */
15310 if (overflow_p | TREE_OVERFLOW (arg0))
15311 TREE_OVERFLOW (t) = 1;
15312 break;
15313 }
15314
15315 default:
15316 gcc_unreachable ();
15317 }
15318
15319 return t;
15320 }
15321
15322 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15323 an integer constant or real constant.
15324
15325 TYPE is the type of the result. */
15326
15327 tree
15328 fold_abs_const (tree arg0, tree type)
15329 {
15330 tree t = NULL_TREE;
15331
15332 switch (TREE_CODE (arg0))
15333 {
15334 case INTEGER_CST:
15335 {
15336 /* If the value is unsigned or non-negative, then the absolute value
15337 is the same as the ordinary value. */
15338 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
15339 t = arg0;
15340
15341 /* If the value is negative, then the absolute value is
15342 its negation. */
15343 else
15344 {
15345 bool overflow;
15346 wide_int val = wi::neg (arg0, &overflow);
15347 t = force_fit_type (type, val, -1,
15348 overflow | TREE_OVERFLOW (arg0));
15349 }
15350 }
15351 break;
15352
15353 case REAL_CST:
15354 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15355 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15356 else
15357 t = arg0;
15358 break;
15359
15360 default:
15361 gcc_unreachable ();
15362 }
15363
15364 return t;
15365 }
15366
15367 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15368 constant. TYPE is the type of the result. */
15369
15370 static tree
15371 fold_not_const (const_tree arg0, tree type)
15372 {
15373 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15374
15375 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
15376 }
15377
15378 /* Given CODE, a relational operator, the target type, TYPE and two
15379 constant operands OP0 and OP1, return the result of the
15380 relational operation. If the result is not a compile time
15381 constant, then return NULL_TREE. */
15382
15383 static tree
15384 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15385 {
15386 int result, invert;
15387
15388 /* From here on, the only cases we handle are when the result is
15389 known to be a constant. */
15390
15391 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15392 {
15393 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15394 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15395
15396 /* Handle the cases where either operand is a NaN. */
15397 if (real_isnan (c0) || real_isnan (c1))
15398 {
15399 switch (code)
15400 {
15401 case EQ_EXPR:
15402 case ORDERED_EXPR:
15403 result = 0;
15404 break;
15405
15406 case NE_EXPR:
15407 case UNORDERED_EXPR:
15408 case UNLT_EXPR:
15409 case UNLE_EXPR:
15410 case UNGT_EXPR:
15411 case UNGE_EXPR:
15412 case UNEQ_EXPR:
15413 result = 1;
15414 break;
15415
15416 case LT_EXPR:
15417 case LE_EXPR:
15418 case GT_EXPR:
15419 case GE_EXPR:
15420 case LTGT_EXPR:
15421 if (flag_trapping_math)
15422 return NULL_TREE;
15423 result = 0;
15424 break;
15425
15426 default:
15427 gcc_unreachable ();
15428 }
15429
15430 return constant_boolean_node (result, type);
15431 }
15432
15433 return constant_boolean_node (real_compare (code, c0, c1), type);
15434 }
15435
15436 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15437 {
15438 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15439 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15440 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15441 }
15442
15443 /* Handle equality/inequality of complex constants. */
15444 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15445 {
15446 tree rcond = fold_relational_const (code, type,
15447 TREE_REALPART (op0),
15448 TREE_REALPART (op1));
15449 tree icond = fold_relational_const (code, type,
15450 TREE_IMAGPART (op0),
15451 TREE_IMAGPART (op1));
15452 if (code == EQ_EXPR)
15453 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15454 else if (code == NE_EXPR)
15455 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15456 else
15457 return NULL_TREE;
15458 }
15459
15460 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15461 {
15462 unsigned count = VECTOR_CST_NELTS (op0);
15463 tree *elts = XALLOCAVEC (tree, count);
15464 gcc_assert (VECTOR_CST_NELTS (op1) == count
15465 && TYPE_VECTOR_SUBPARTS (type) == count);
15466
15467 for (unsigned i = 0; i < count; i++)
15468 {
15469 tree elem_type = TREE_TYPE (type);
15470 tree elem0 = VECTOR_CST_ELT (op0, i);
15471 tree elem1 = VECTOR_CST_ELT (op1, i);
15472
15473 tree tem = fold_relational_const (code, elem_type,
15474 elem0, elem1);
15475
15476 if (tem == NULL_TREE)
15477 return NULL_TREE;
15478
15479 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
15480 }
15481
15482 return build_vector (type, elts);
15483 }
15484
15485 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15486
15487 To compute GT, swap the arguments and do LT.
15488 To compute GE, do LT and invert the result.
15489 To compute LE, swap the arguments, do LT and invert the result.
15490 To compute NE, do EQ and invert the result.
15491
15492 Therefore, the code below must handle only EQ and LT. */
15493
15494 if (code == LE_EXPR || code == GT_EXPR)
15495 {
15496 std::swap (op0, op1);
15497 code = swap_tree_comparison (code);
15498 }
15499
15500 /* Note that it is safe to invert for real values here because we
15501 have already handled the one case that it matters. */
15502
15503 invert = 0;
15504 if (code == NE_EXPR || code == GE_EXPR)
15505 {
15506 invert = 1;
15507 code = invert_tree_comparison (code, false);
15508 }
15509
15510 /* Compute a result for LT or EQ if args permit;
15511 Otherwise return T. */
15512 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15513 {
15514 if (code == EQ_EXPR)
15515 result = tree_int_cst_equal (op0, op1);
15516 else
15517 result = tree_int_cst_lt (op0, op1);
15518 }
15519 else
15520 return NULL_TREE;
15521
15522 if (invert)
15523 result ^= 1;
15524 return constant_boolean_node (result, type);
15525 }
15526
15527 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15528 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15529 itself. */
15530
15531 tree
15532 fold_build_cleanup_point_expr (tree type, tree expr)
15533 {
15534 /* If the expression does not have side effects then we don't have to wrap
15535 it with a cleanup point expression. */
15536 if (!TREE_SIDE_EFFECTS (expr))
15537 return expr;
15538
15539 /* If the expression is a return, check to see if the expression inside the
15540 return has no side effects or the right hand side of the modify expression
15541 inside the return. If either don't have side effects set we don't need to
15542 wrap the expression in a cleanup point expression. Note we don't check the
15543 left hand side of the modify because it should always be a return decl. */
15544 if (TREE_CODE (expr) == RETURN_EXPR)
15545 {
15546 tree op = TREE_OPERAND (expr, 0);
15547 if (!op || !TREE_SIDE_EFFECTS (op))
15548 return expr;
15549 op = TREE_OPERAND (op, 1);
15550 if (!TREE_SIDE_EFFECTS (op))
15551 return expr;
15552 }
15553
15554 return build1 (CLEANUP_POINT_EXPR, type, expr);
15555 }
15556
15557 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15558 of an indirection through OP0, or NULL_TREE if no simplification is
15559 possible. */
15560
15561 tree
15562 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15563 {
15564 tree sub = op0;
15565 tree subtype;
15566
15567 STRIP_NOPS (sub);
15568 subtype = TREE_TYPE (sub);
15569 if (!POINTER_TYPE_P (subtype))
15570 return NULL_TREE;
15571
15572 if (TREE_CODE (sub) == ADDR_EXPR)
15573 {
15574 tree op = TREE_OPERAND (sub, 0);
15575 tree optype = TREE_TYPE (op);
15576 /* *&CONST_DECL -> to the value of the const decl. */
15577 if (TREE_CODE (op) == CONST_DECL)
15578 return DECL_INITIAL (op);
15579 /* *&p => p; make sure to handle *&"str"[cst] here. */
15580 if (type == optype)
15581 {
15582 tree fop = fold_read_from_constant_string (op);
15583 if (fop)
15584 return fop;
15585 else
15586 return op;
15587 }
15588 /* *(foo *)&fooarray => fooarray[0] */
15589 else if (TREE_CODE (optype) == ARRAY_TYPE
15590 && type == TREE_TYPE (optype)
15591 && (!in_gimple_form
15592 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15593 {
15594 tree type_domain = TYPE_DOMAIN (optype);
15595 tree min_val = size_zero_node;
15596 if (type_domain && TYPE_MIN_VALUE (type_domain))
15597 min_val = TYPE_MIN_VALUE (type_domain);
15598 if (in_gimple_form
15599 && TREE_CODE (min_val) != INTEGER_CST)
15600 return NULL_TREE;
15601 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15602 NULL_TREE, NULL_TREE);
15603 }
15604 /* *(foo *)&complexfoo => __real__ complexfoo */
15605 else if (TREE_CODE (optype) == COMPLEX_TYPE
15606 && type == TREE_TYPE (optype))
15607 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15608 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15609 else if (TREE_CODE (optype) == VECTOR_TYPE
15610 && type == TREE_TYPE (optype))
15611 {
15612 tree part_width = TYPE_SIZE (type);
15613 tree index = bitsize_int (0);
15614 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15615 }
15616 }
15617
15618 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15619 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15620 {
15621 tree op00 = TREE_OPERAND (sub, 0);
15622 tree op01 = TREE_OPERAND (sub, 1);
15623
15624 STRIP_NOPS (op00);
15625 if (TREE_CODE (op00) == ADDR_EXPR)
15626 {
15627 tree op00type;
15628 op00 = TREE_OPERAND (op00, 0);
15629 op00type = TREE_TYPE (op00);
15630
15631 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15632 if (TREE_CODE (op00type) == VECTOR_TYPE
15633 && type == TREE_TYPE (op00type))
15634 {
15635 HOST_WIDE_INT offset = tree_to_shwi (op01);
15636 tree part_width = TYPE_SIZE (type);
15637 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
15638 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15639 tree index = bitsize_int (indexi);
15640
15641 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
15642 return fold_build3_loc (loc,
15643 BIT_FIELD_REF, type, op00,
15644 part_width, index);
15645
15646 }
15647 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15648 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15649 && type == TREE_TYPE (op00type))
15650 {
15651 tree size = TYPE_SIZE_UNIT (type);
15652 if (tree_int_cst_equal (size, op01))
15653 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15654 }
15655 /* ((foo *)&fooarray)[1] => fooarray[1] */
15656 else if (TREE_CODE (op00type) == ARRAY_TYPE
15657 && type == TREE_TYPE (op00type))
15658 {
15659 tree type_domain = TYPE_DOMAIN (op00type);
15660 tree min_val = size_zero_node;
15661 if (type_domain && TYPE_MIN_VALUE (type_domain))
15662 min_val = TYPE_MIN_VALUE (type_domain);
15663 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15664 TYPE_SIZE_UNIT (type));
15665 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15666 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15667 NULL_TREE, NULL_TREE);
15668 }
15669 }
15670 }
15671
15672 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15673 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15674 && type == TREE_TYPE (TREE_TYPE (subtype))
15675 && (!in_gimple_form
15676 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15677 {
15678 tree type_domain;
15679 tree min_val = size_zero_node;
15680 sub = build_fold_indirect_ref_loc (loc, sub);
15681 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15682 if (type_domain && TYPE_MIN_VALUE (type_domain))
15683 min_val = TYPE_MIN_VALUE (type_domain);
15684 if (in_gimple_form
15685 && TREE_CODE (min_val) != INTEGER_CST)
15686 return NULL_TREE;
15687 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15688 NULL_TREE);
15689 }
15690
15691 return NULL_TREE;
15692 }
15693
15694 /* Builds an expression for an indirection through T, simplifying some
15695 cases. */
15696
15697 tree
15698 build_fold_indirect_ref_loc (location_t loc, tree t)
15699 {
15700 tree type = TREE_TYPE (TREE_TYPE (t));
15701 tree sub = fold_indirect_ref_1 (loc, type, t);
15702
15703 if (sub)
15704 return sub;
15705
15706 return build1_loc (loc, INDIRECT_REF, type, t);
15707 }
15708
15709 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15710
15711 tree
15712 fold_indirect_ref_loc (location_t loc, tree t)
15713 {
15714 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15715
15716 if (sub)
15717 return sub;
15718 else
15719 return t;
15720 }
15721
15722 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15723 whose result is ignored. The type of the returned tree need not be
15724 the same as the original expression. */
15725
15726 tree
15727 fold_ignored_result (tree t)
15728 {
15729 if (!TREE_SIDE_EFFECTS (t))
15730 return integer_zero_node;
15731
15732 for (;;)
15733 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15734 {
15735 case tcc_unary:
15736 t = TREE_OPERAND (t, 0);
15737 break;
15738
15739 case tcc_binary:
15740 case tcc_comparison:
15741 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15742 t = TREE_OPERAND (t, 0);
15743 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15744 t = TREE_OPERAND (t, 1);
15745 else
15746 return t;
15747 break;
15748
15749 case tcc_expression:
15750 switch (TREE_CODE (t))
15751 {
15752 case COMPOUND_EXPR:
15753 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15754 return t;
15755 t = TREE_OPERAND (t, 0);
15756 break;
15757
15758 case COND_EXPR:
15759 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15760 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15761 return t;
15762 t = TREE_OPERAND (t, 0);
15763 break;
15764
15765 default:
15766 return t;
15767 }
15768 break;
15769
15770 default:
15771 return t;
15772 }
15773 }
15774
15775 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15776
15777 tree
15778 round_up_loc (location_t loc, tree value, unsigned int divisor)
15779 {
15780 tree div = NULL_TREE;
15781
15782 if (divisor == 1)
15783 return value;
15784
15785 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15786 have to do anything. Only do this when we are not given a const,
15787 because in that case, this check is more expensive than just
15788 doing it. */
15789 if (TREE_CODE (value) != INTEGER_CST)
15790 {
15791 div = build_int_cst (TREE_TYPE (value), divisor);
15792
15793 if (multiple_of_p (TREE_TYPE (value), value, div))
15794 return value;
15795 }
15796
15797 /* If divisor is a power of two, simplify this to bit manipulation. */
15798 if (divisor == (divisor & -divisor))
15799 {
15800 if (TREE_CODE (value) == INTEGER_CST)
15801 {
15802 wide_int val = value;
15803 bool overflow_p;
15804
15805 if ((val & (divisor - 1)) == 0)
15806 return value;
15807
15808 overflow_p = TREE_OVERFLOW (value);
15809 val += divisor - 1;
15810 val &= - (int) divisor;
15811 if (val == 0)
15812 overflow_p = true;
15813
15814 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
15815 }
15816 else
15817 {
15818 tree t;
15819
15820 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15821 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15822 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
15823 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15824 }
15825 }
15826 else
15827 {
15828 if (!div)
15829 div = build_int_cst (TREE_TYPE (value), divisor);
15830 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15831 value = size_binop_loc (loc, MULT_EXPR, value, div);
15832 }
15833
15834 return value;
15835 }
15836
15837 /* Likewise, but round down. */
15838
15839 tree
15840 round_down_loc (location_t loc, tree value, int divisor)
15841 {
15842 tree div = NULL_TREE;
15843
15844 gcc_assert (divisor > 0);
15845 if (divisor == 1)
15846 return value;
15847
15848 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15849 have to do anything. Only do this when we are not given a const,
15850 because in that case, this check is more expensive than just
15851 doing it. */
15852 if (TREE_CODE (value) != INTEGER_CST)
15853 {
15854 div = build_int_cst (TREE_TYPE (value), divisor);
15855
15856 if (multiple_of_p (TREE_TYPE (value), value, div))
15857 return value;
15858 }
15859
15860 /* If divisor is a power of two, simplify this to bit manipulation. */
15861 if (divisor == (divisor & -divisor))
15862 {
15863 tree t;
15864
15865 t = build_int_cst (TREE_TYPE (value), -divisor);
15866 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15867 }
15868 else
15869 {
15870 if (!div)
15871 div = build_int_cst (TREE_TYPE (value), divisor);
15872 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15873 value = size_binop_loc (loc, MULT_EXPR, value, div);
15874 }
15875
15876 return value;
15877 }
15878
15879 /* Returns the pointer to the base of the object addressed by EXP and
15880 extracts the information about the offset of the access, storing it
15881 to PBITPOS and POFFSET. */
15882
15883 static tree
15884 split_address_to_core_and_offset (tree exp,
15885 HOST_WIDE_INT *pbitpos, tree *poffset)
15886 {
15887 tree core;
15888 machine_mode mode;
15889 int unsignedp, volatilep;
15890 HOST_WIDE_INT bitsize;
15891 location_t loc = EXPR_LOCATION (exp);
15892
15893 if (TREE_CODE (exp) == ADDR_EXPR)
15894 {
15895 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15896 poffset, &mode, &unsignedp, &volatilep,
15897 false);
15898 core = build_fold_addr_expr_loc (loc, core);
15899 }
15900 else
15901 {
15902 core = exp;
15903 *pbitpos = 0;
15904 *poffset = NULL_TREE;
15905 }
15906
15907 return core;
15908 }
15909
15910 /* Returns true if addresses of E1 and E2 differ by a constant, false
15911 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15912
15913 bool
15914 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15915 {
15916 tree core1, core2;
15917 HOST_WIDE_INT bitpos1, bitpos2;
15918 tree toffset1, toffset2, tdiff, type;
15919
15920 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15921 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15922
15923 if (bitpos1 % BITS_PER_UNIT != 0
15924 || bitpos2 % BITS_PER_UNIT != 0
15925 || !operand_equal_p (core1, core2, 0))
15926 return false;
15927
15928 if (toffset1 && toffset2)
15929 {
15930 type = TREE_TYPE (toffset1);
15931 if (type != TREE_TYPE (toffset2))
15932 toffset2 = fold_convert (type, toffset2);
15933
15934 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15935 if (!cst_and_fits_in_hwi (tdiff))
15936 return false;
15937
15938 *diff = int_cst_value (tdiff);
15939 }
15940 else if (toffset1 || toffset2)
15941 {
15942 /* If only one of the offsets is non-constant, the difference cannot
15943 be a constant. */
15944 return false;
15945 }
15946 else
15947 *diff = 0;
15948
15949 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15950 return true;
15951 }
15952
15953 /* Simplify the floating point expression EXP when the sign of the
15954 result is not significant. Return NULL_TREE if no simplification
15955 is possible. */
15956
15957 tree
15958 fold_strip_sign_ops (tree exp)
15959 {
15960 tree arg0, arg1;
15961 location_t loc = EXPR_LOCATION (exp);
15962
15963 switch (TREE_CODE (exp))
15964 {
15965 case ABS_EXPR:
15966 case NEGATE_EXPR:
15967 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15968 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15969
15970 case MULT_EXPR:
15971 case RDIV_EXPR:
15972 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
15973 return NULL_TREE;
15974 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15975 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15976 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15977 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15978 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15979 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15980 break;
15981
15982 case COMPOUND_EXPR:
15983 arg0 = TREE_OPERAND (exp, 0);
15984 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15985 if (arg1)
15986 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15987 break;
15988
15989 case COND_EXPR:
15990 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15991 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15992 if (arg0 || arg1)
15993 return fold_build3_loc (loc,
15994 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15995 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15996 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15997 break;
15998
15999 case CALL_EXPR:
16000 {
16001 const enum built_in_function fcode = builtin_mathfn_code (exp);
16002 switch (fcode)
16003 {
16004 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16005 /* Strip copysign function call, return the 1st argument. */
16006 arg0 = CALL_EXPR_ARG (exp, 0);
16007 arg1 = CALL_EXPR_ARG (exp, 1);
16008 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16009
16010 default:
16011 /* Strip sign ops from the argument of "odd" math functions. */
16012 if (negate_mathfn_p (fcode))
16013 {
16014 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16015 if (arg0)
16016 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16017 }
16018 break;
16019 }
16020 }
16021 break;
16022
16023 default:
16024 break;
16025 }
16026 return NULL_TREE;
16027 }
16028
16029 /* Return OFF converted to a pointer offset type suitable as offset for
16030 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16031 tree
16032 convert_to_ptrofftype_loc (location_t loc, tree off)
16033 {
16034 return fold_convert_loc (loc, sizetype, off);
16035 }
16036
16037 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16038 tree
16039 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16040 {
16041 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16042 ptr, convert_to_ptrofftype_loc (loc, off));
16043 }
16044
16045 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16046 tree
16047 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16048 {
16049 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16050 ptr, size_int (off));
16051 }