fold-const.c (fold_comparison): Move X - Y CMP 0 -> X CMP Y...
[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 /* Disable this optimization for function pointer expressions
4492 on targets that require function pointer canonicalization. */
4493 if (targetm.have_canonicalize_funcptr_for_compare ()
4494 && TREE_CODE (etype) == POINTER_TYPE
4495 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4496 return NULL_TREE;
4497
4498 if (! in_p)
4499 {
4500 value = build_range_check (loc, type, exp, 1, low, high);
4501 if (value != 0)
4502 return invert_truthvalue_loc (loc, value);
4503
4504 return 0;
4505 }
4506
4507 if (low == 0 && high == 0)
4508 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4509
4510 if (low == 0)
4511 return fold_build2_loc (loc, LE_EXPR, type, exp,
4512 fold_convert_loc (loc, etype, high));
4513
4514 if (high == 0)
4515 return fold_build2_loc (loc, GE_EXPR, type, exp,
4516 fold_convert_loc (loc, etype, low));
4517
4518 if (operand_equal_p (low, high, 0))
4519 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4520 fold_convert_loc (loc, etype, low));
4521
4522 if (integer_zerop (low))
4523 {
4524 if (! TYPE_UNSIGNED (etype))
4525 {
4526 etype = unsigned_type_for (etype);
4527 high = fold_convert_loc (loc, etype, high);
4528 exp = fold_convert_loc (loc, etype, exp);
4529 }
4530 return build_range_check (loc, type, exp, 1, 0, high);
4531 }
4532
4533 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4534 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4535 {
4536 int prec = TYPE_PRECISION (etype);
4537
4538 if (wi::mask (prec - 1, false, prec) == high)
4539 {
4540 if (TYPE_UNSIGNED (etype))
4541 {
4542 tree signed_etype = signed_type_for (etype);
4543 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4544 etype
4545 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4546 else
4547 etype = signed_etype;
4548 exp = fold_convert_loc (loc, etype, exp);
4549 }
4550 return fold_build2_loc (loc, GT_EXPR, type, exp,
4551 build_int_cst (etype, 0));
4552 }
4553 }
4554
4555 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4556 This requires wrap-around arithmetics for the type of the expression.
4557 First make sure that arithmetics in this type is valid, then make sure
4558 that it wraps around. */
4559 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4560 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4561 TYPE_UNSIGNED (etype));
4562
4563 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4564 {
4565 tree utype, minv, maxv;
4566
4567 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4568 for the type in question, as we rely on this here. */
4569 utype = unsigned_type_for (etype);
4570 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4571 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4572 build_int_cst (TREE_TYPE (maxv), 1), 1);
4573 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4574
4575 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4576 minv, 1, maxv, 1)))
4577 etype = utype;
4578 else
4579 return 0;
4580 }
4581
4582 high = fold_convert_loc (loc, etype, high);
4583 low = fold_convert_loc (loc, etype, low);
4584 exp = fold_convert_loc (loc, etype, exp);
4585
4586 value = const_binop (MINUS_EXPR, high, low);
4587
4588
4589 if (POINTER_TYPE_P (etype))
4590 {
4591 if (value != 0 && !TREE_OVERFLOW (value))
4592 {
4593 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4594 return build_range_check (loc, type,
4595 fold_build_pointer_plus_loc (loc, exp, low),
4596 1, build_int_cst (etype, 0), value);
4597 }
4598 return 0;
4599 }
4600
4601 if (value != 0 && !TREE_OVERFLOW (value))
4602 return build_range_check (loc, type,
4603 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4604 1, build_int_cst (etype, 0), value);
4605
4606 return 0;
4607 }
4608 \f
4609 /* Return the predecessor of VAL in its type, handling the infinite case. */
4610
4611 static tree
4612 range_predecessor (tree val)
4613 {
4614 tree type = TREE_TYPE (val);
4615
4616 if (INTEGRAL_TYPE_P (type)
4617 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4618 return 0;
4619 else
4620 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4621 build_int_cst (TREE_TYPE (val), 1), 0);
4622 }
4623
4624 /* Return the successor of VAL in its type, handling the infinite case. */
4625
4626 static tree
4627 range_successor (tree val)
4628 {
4629 tree type = TREE_TYPE (val);
4630
4631 if (INTEGRAL_TYPE_P (type)
4632 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4633 return 0;
4634 else
4635 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4636 build_int_cst (TREE_TYPE (val), 1), 0);
4637 }
4638
4639 /* Given two ranges, see if we can merge them into one. Return 1 if we
4640 can, 0 if we can't. Set the output range into the specified parameters. */
4641
4642 bool
4643 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4644 tree high0, int in1_p, tree low1, tree high1)
4645 {
4646 int no_overlap;
4647 int subset;
4648 int temp;
4649 tree tem;
4650 int in_p;
4651 tree low, high;
4652 int lowequal = ((low0 == 0 && low1 == 0)
4653 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4654 low0, 0, low1, 0)));
4655 int highequal = ((high0 == 0 && high1 == 0)
4656 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4657 high0, 1, high1, 1)));
4658
4659 /* Make range 0 be the range that starts first, or ends last if they
4660 start at the same value. Swap them if it isn't. */
4661 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4662 low0, 0, low1, 0))
4663 || (lowequal
4664 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4665 high1, 1, high0, 1))))
4666 {
4667 temp = in0_p, in0_p = in1_p, in1_p = temp;
4668 tem = low0, low0 = low1, low1 = tem;
4669 tem = high0, high0 = high1, high1 = tem;
4670 }
4671
4672 /* Now flag two cases, whether the ranges are disjoint or whether the
4673 second range is totally subsumed in the first. Note that the tests
4674 below are simplified by the ones above. */
4675 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4676 high0, 1, low1, 0));
4677 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4678 high1, 1, high0, 1));
4679
4680 /* We now have four cases, depending on whether we are including or
4681 excluding the two ranges. */
4682 if (in0_p && in1_p)
4683 {
4684 /* If they don't overlap, the result is false. If the second range
4685 is a subset it is the result. Otherwise, the range is from the start
4686 of the second to the end of the first. */
4687 if (no_overlap)
4688 in_p = 0, low = high = 0;
4689 else if (subset)
4690 in_p = 1, low = low1, high = high1;
4691 else
4692 in_p = 1, low = low1, high = high0;
4693 }
4694
4695 else if (in0_p && ! in1_p)
4696 {
4697 /* If they don't overlap, the result is the first range. If they are
4698 equal, the result is false. If the second range is a subset of the
4699 first, and the ranges begin at the same place, we go from just after
4700 the end of the second range to the end of the first. If the second
4701 range is not a subset of the first, or if it is a subset and both
4702 ranges end at the same place, the range starts at the start of the
4703 first range and ends just before the second range.
4704 Otherwise, we can't describe this as a single range. */
4705 if (no_overlap)
4706 in_p = 1, low = low0, high = high0;
4707 else if (lowequal && highequal)
4708 in_p = 0, low = high = 0;
4709 else if (subset && lowequal)
4710 {
4711 low = range_successor (high1);
4712 high = high0;
4713 in_p = 1;
4714 if (low == 0)
4715 {
4716 /* We are in the weird situation where high0 > high1 but
4717 high1 has no successor. Punt. */
4718 return 0;
4719 }
4720 }
4721 else if (! subset || highequal)
4722 {
4723 low = low0;
4724 high = range_predecessor (low1);
4725 in_p = 1;
4726 if (high == 0)
4727 {
4728 /* low0 < low1 but low1 has no predecessor. Punt. */
4729 return 0;
4730 }
4731 }
4732 else
4733 return 0;
4734 }
4735
4736 else if (! in0_p && in1_p)
4737 {
4738 /* If they don't overlap, the result is the second range. If the second
4739 is a subset of the first, the result is false. Otherwise,
4740 the range starts just after the first range and ends at the
4741 end of the second. */
4742 if (no_overlap)
4743 in_p = 1, low = low1, high = high1;
4744 else if (subset || highequal)
4745 in_p = 0, low = high = 0;
4746 else
4747 {
4748 low = range_successor (high0);
4749 high = high1;
4750 in_p = 1;
4751 if (low == 0)
4752 {
4753 /* high1 > high0 but high0 has no successor. Punt. */
4754 return 0;
4755 }
4756 }
4757 }
4758
4759 else
4760 {
4761 /* The case where we are excluding both ranges. Here the complex case
4762 is if they don't overlap. In that case, the only time we have a
4763 range is if they are adjacent. If the second is a subset of the
4764 first, the result is the first. Otherwise, the range to exclude
4765 starts at the beginning of the first range and ends at the end of the
4766 second. */
4767 if (no_overlap)
4768 {
4769 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4770 range_successor (high0),
4771 1, low1, 0)))
4772 in_p = 0, low = low0, high = high1;
4773 else
4774 {
4775 /* Canonicalize - [min, x] into - [-, x]. */
4776 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4777 switch (TREE_CODE (TREE_TYPE (low0)))
4778 {
4779 case ENUMERAL_TYPE:
4780 if (TYPE_PRECISION (TREE_TYPE (low0))
4781 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4782 break;
4783 /* FALLTHROUGH */
4784 case INTEGER_TYPE:
4785 if (tree_int_cst_equal (low0,
4786 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4787 low0 = 0;
4788 break;
4789 case POINTER_TYPE:
4790 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4791 && integer_zerop (low0))
4792 low0 = 0;
4793 break;
4794 default:
4795 break;
4796 }
4797
4798 /* Canonicalize - [x, max] into - [x, -]. */
4799 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4800 switch (TREE_CODE (TREE_TYPE (high1)))
4801 {
4802 case ENUMERAL_TYPE:
4803 if (TYPE_PRECISION (TREE_TYPE (high1))
4804 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4805 break;
4806 /* FALLTHROUGH */
4807 case INTEGER_TYPE:
4808 if (tree_int_cst_equal (high1,
4809 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4810 high1 = 0;
4811 break;
4812 case POINTER_TYPE:
4813 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4814 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4815 high1, 1,
4816 build_int_cst (TREE_TYPE (high1), 1),
4817 1)))
4818 high1 = 0;
4819 break;
4820 default:
4821 break;
4822 }
4823
4824 /* The ranges might be also adjacent between the maximum and
4825 minimum values of the given type. For
4826 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4827 return + [x + 1, y - 1]. */
4828 if (low0 == 0 && high1 == 0)
4829 {
4830 low = range_successor (high0);
4831 high = range_predecessor (low1);
4832 if (low == 0 || high == 0)
4833 return 0;
4834
4835 in_p = 1;
4836 }
4837 else
4838 return 0;
4839 }
4840 }
4841 else if (subset)
4842 in_p = 0, low = low0, high = high0;
4843 else
4844 in_p = 0, low = low0, high = high1;
4845 }
4846
4847 *pin_p = in_p, *plow = low, *phigh = high;
4848 return 1;
4849 }
4850 \f
4851
4852 /* Subroutine of fold, looking inside expressions of the form
4853 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4854 of the COND_EXPR. This function is being used also to optimize
4855 A op B ? C : A, by reversing the comparison first.
4856
4857 Return a folded expression whose code is not a COND_EXPR
4858 anymore, or NULL_TREE if no folding opportunity is found. */
4859
4860 static tree
4861 fold_cond_expr_with_comparison (location_t loc, tree type,
4862 tree arg0, tree arg1, tree arg2)
4863 {
4864 enum tree_code comp_code = TREE_CODE (arg0);
4865 tree arg00 = TREE_OPERAND (arg0, 0);
4866 tree arg01 = TREE_OPERAND (arg0, 1);
4867 tree arg1_type = TREE_TYPE (arg1);
4868 tree tem;
4869
4870 STRIP_NOPS (arg1);
4871 STRIP_NOPS (arg2);
4872
4873 /* If we have A op 0 ? A : -A, consider applying the following
4874 transformations:
4875
4876 A == 0? A : -A same as -A
4877 A != 0? A : -A same as A
4878 A >= 0? A : -A same as abs (A)
4879 A > 0? A : -A same as abs (A)
4880 A <= 0? A : -A same as -abs (A)
4881 A < 0? A : -A same as -abs (A)
4882
4883 None of these transformations work for modes with signed
4884 zeros. If A is +/-0, the first two transformations will
4885 change the sign of the result (from +0 to -0, or vice
4886 versa). The last four will fix the sign of the result,
4887 even though the original expressions could be positive or
4888 negative, depending on the sign of A.
4889
4890 Note that all these transformations are correct if A is
4891 NaN, since the two alternatives (A and -A) are also NaNs. */
4892 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4893 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4894 ? real_zerop (arg01)
4895 : integer_zerop (arg01))
4896 && ((TREE_CODE (arg2) == NEGATE_EXPR
4897 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4898 /* In the case that A is of the form X-Y, '-A' (arg2) may
4899 have already been folded to Y-X, check for that. */
4900 || (TREE_CODE (arg1) == MINUS_EXPR
4901 && TREE_CODE (arg2) == MINUS_EXPR
4902 && operand_equal_p (TREE_OPERAND (arg1, 0),
4903 TREE_OPERAND (arg2, 1), 0)
4904 && operand_equal_p (TREE_OPERAND (arg1, 1),
4905 TREE_OPERAND (arg2, 0), 0))))
4906 switch (comp_code)
4907 {
4908 case EQ_EXPR:
4909 case UNEQ_EXPR:
4910 tem = fold_convert_loc (loc, arg1_type, arg1);
4911 return pedantic_non_lvalue_loc (loc,
4912 fold_convert_loc (loc, type,
4913 negate_expr (tem)));
4914 case NE_EXPR:
4915 case LTGT_EXPR:
4916 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4917 case UNGE_EXPR:
4918 case UNGT_EXPR:
4919 if (flag_trapping_math)
4920 break;
4921 /* Fall through. */
4922 case GE_EXPR:
4923 case GT_EXPR:
4924 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4925 arg1 = fold_convert_loc (loc, signed_type_for
4926 (TREE_TYPE (arg1)), arg1);
4927 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4928 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4929 case UNLE_EXPR:
4930 case UNLT_EXPR:
4931 if (flag_trapping_math)
4932 break;
4933 case LE_EXPR:
4934 case LT_EXPR:
4935 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4936 arg1 = fold_convert_loc (loc, signed_type_for
4937 (TREE_TYPE (arg1)), arg1);
4938 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4939 return negate_expr (fold_convert_loc (loc, type, tem));
4940 default:
4941 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4942 break;
4943 }
4944
4945 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4946 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4947 both transformations are correct when A is NaN: A != 0
4948 is then true, and A == 0 is false. */
4949
4950 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4951 && integer_zerop (arg01) && integer_zerop (arg2))
4952 {
4953 if (comp_code == NE_EXPR)
4954 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4955 else if (comp_code == EQ_EXPR)
4956 return build_zero_cst (type);
4957 }
4958
4959 /* Try some transformations of A op B ? A : B.
4960
4961 A == B? A : B same as B
4962 A != B? A : B same as A
4963 A >= B? A : B same as max (A, B)
4964 A > B? A : B same as max (B, A)
4965 A <= B? A : B same as min (A, B)
4966 A < B? A : B same as min (B, A)
4967
4968 As above, these transformations don't work in the presence
4969 of signed zeros. For example, if A and B are zeros of
4970 opposite sign, the first two transformations will change
4971 the sign of the result. In the last four, the original
4972 expressions give different results for (A=+0, B=-0) and
4973 (A=-0, B=+0), but the transformed expressions do not.
4974
4975 The first two transformations are correct if either A or B
4976 is a NaN. In the first transformation, the condition will
4977 be false, and B will indeed be chosen. In the case of the
4978 second transformation, the condition A != B will be true,
4979 and A will be chosen.
4980
4981 The conversions to max() and min() are not correct if B is
4982 a number and A is not. The conditions in the original
4983 expressions will be false, so all four give B. The min()
4984 and max() versions would give a NaN instead. */
4985 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4986 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4987 /* Avoid these transformations if the COND_EXPR may be used
4988 as an lvalue in the C++ front-end. PR c++/19199. */
4989 && (in_gimple_form
4990 || VECTOR_TYPE_P (type)
4991 || (! lang_GNU_CXX ()
4992 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4993 || ! maybe_lvalue_p (arg1)
4994 || ! maybe_lvalue_p (arg2)))
4995 {
4996 tree comp_op0 = arg00;
4997 tree comp_op1 = arg01;
4998 tree comp_type = TREE_TYPE (comp_op0);
4999
5000 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5001 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5002 {
5003 comp_type = type;
5004 comp_op0 = arg1;
5005 comp_op1 = arg2;
5006 }
5007
5008 switch (comp_code)
5009 {
5010 case EQ_EXPR:
5011 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5012 case NE_EXPR:
5013 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5014 case LE_EXPR:
5015 case LT_EXPR:
5016 case UNLE_EXPR:
5017 case UNLT_EXPR:
5018 /* In C++ a ?: expression can be an lvalue, so put the
5019 operand which will be used if they are equal first
5020 so that we can convert this back to the
5021 corresponding COND_EXPR. */
5022 if (!HONOR_NANS (arg1))
5023 {
5024 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5025 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5026 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5027 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5028 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5029 comp_op1, comp_op0);
5030 return pedantic_non_lvalue_loc (loc,
5031 fold_convert_loc (loc, type, tem));
5032 }
5033 break;
5034 case GE_EXPR:
5035 case GT_EXPR:
5036 case UNGE_EXPR:
5037 case UNGT_EXPR:
5038 if (!HONOR_NANS (arg1))
5039 {
5040 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5041 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5042 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5043 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5044 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5045 comp_op1, comp_op0);
5046 return pedantic_non_lvalue_loc (loc,
5047 fold_convert_loc (loc, type, tem));
5048 }
5049 break;
5050 case UNEQ_EXPR:
5051 if (!HONOR_NANS (arg1))
5052 return pedantic_non_lvalue_loc (loc,
5053 fold_convert_loc (loc, type, arg2));
5054 break;
5055 case LTGT_EXPR:
5056 if (!HONOR_NANS (arg1))
5057 return pedantic_non_lvalue_loc (loc,
5058 fold_convert_loc (loc, type, arg1));
5059 break;
5060 default:
5061 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5062 break;
5063 }
5064 }
5065
5066 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5067 we might still be able to simplify this. For example,
5068 if C1 is one less or one more than C2, this might have started
5069 out as a MIN or MAX and been transformed by this function.
5070 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5071
5072 if (INTEGRAL_TYPE_P (type)
5073 && TREE_CODE (arg01) == INTEGER_CST
5074 && TREE_CODE (arg2) == INTEGER_CST)
5075 switch (comp_code)
5076 {
5077 case EQ_EXPR:
5078 if (TREE_CODE (arg1) == INTEGER_CST)
5079 break;
5080 /* We can replace A with C1 in this case. */
5081 arg1 = fold_convert_loc (loc, type, arg01);
5082 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5083
5084 case LT_EXPR:
5085 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5086 MIN_EXPR, to preserve the signedness of the comparison. */
5087 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5088 OEP_ONLY_CONST)
5089 && operand_equal_p (arg01,
5090 const_binop (PLUS_EXPR, arg2,
5091 build_int_cst (type, 1)),
5092 OEP_ONLY_CONST))
5093 {
5094 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5095 fold_convert_loc (loc, TREE_TYPE (arg00),
5096 arg2));
5097 return pedantic_non_lvalue_loc (loc,
5098 fold_convert_loc (loc, type, tem));
5099 }
5100 break;
5101
5102 case LE_EXPR:
5103 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5104 as above. */
5105 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5106 OEP_ONLY_CONST)
5107 && operand_equal_p (arg01,
5108 const_binop (MINUS_EXPR, arg2,
5109 build_int_cst (type, 1)),
5110 OEP_ONLY_CONST))
5111 {
5112 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5113 fold_convert_loc (loc, TREE_TYPE (arg00),
5114 arg2));
5115 return pedantic_non_lvalue_loc (loc,
5116 fold_convert_loc (loc, type, tem));
5117 }
5118 break;
5119
5120 case GT_EXPR:
5121 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5122 MAX_EXPR, to preserve the signedness of the comparison. */
5123 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5124 OEP_ONLY_CONST)
5125 && operand_equal_p (arg01,
5126 const_binop (MINUS_EXPR, arg2,
5127 build_int_cst (type, 1)),
5128 OEP_ONLY_CONST))
5129 {
5130 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5131 fold_convert_loc (loc, TREE_TYPE (arg00),
5132 arg2));
5133 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5134 }
5135 break;
5136
5137 case GE_EXPR:
5138 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5139 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5140 OEP_ONLY_CONST)
5141 && operand_equal_p (arg01,
5142 const_binop (PLUS_EXPR, arg2,
5143 build_int_cst (type, 1)),
5144 OEP_ONLY_CONST))
5145 {
5146 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5147 fold_convert_loc (loc, TREE_TYPE (arg00),
5148 arg2));
5149 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5150 }
5151 break;
5152 case NE_EXPR:
5153 break;
5154 default:
5155 gcc_unreachable ();
5156 }
5157
5158 return NULL_TREE;
5159 }
5160
5161
5162 \f
5163 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5164 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5165 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5166 false) >= 2)
5167 #endif
5168
5169 /* EXP is some logical combination of boolean tests. See if we can
5170 merge it into some range test. Return the new tree if so. */
5171
5172 static tree
5173 fold_range_test (location_t loc, enum tree_code code, tree type,
5174 tree op0, tree op1)
5175 {
5176 int or_op = (code == TRUTH_ORIF_EXPR
5177 || code == TRUTH_OR_EXPR);
5178 int in0_p, in1_p, in_p;
5179 tree low0, low1, low, high0, high1, high;
5180 bool strict_overflow_p = false;
5181 tree tem, lhs, rhs;
5182 const char * const warnmsg = G_("assuming signed overflow does not occur "
5183 "when simplifying range test");
5184
5185 if (!INTEGRAL_TYPE_P (type))
5186 return 0;
5187
5188 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5189 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5190
5191 /* If this is an OR operation, invert both sides; we will invert
5192 again at the end. */
5193 if (or_op)
5194 in0_p = ! in0_p, in1_p = ! in1_p;
5195
5196 /* If both expressions are the same, if we can merge the ranges, and we
5197 can build the range test, return it or it inverted. If one of the
5198 ranges is always true or always false, consider it to be the same
5199 expression as the other. */
5200 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5201 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5202 in1_p, low1, high1)
5203 && 0 != (tem = (build_range_check (loc, type,
5204 lhs != 0 ? lhs
5205 : rhs != 0 ? rhs : integer_zero_node,
5206 in_p, low, high))))
5207 {
5208 if (strict_overflow_p)
5209 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5210 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5211 }
5212
5213 /* On machines where the branch cost is expensive, if this is a
5214 short-circuited branch and the underlying object on both sides
5215 is the same, make a non-short-circuit operation. */
5216 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5217 && lhs != 0 && rhs != 0
5218 && (code == TRUTH_ANDIF_EXPR
5219 || code == TRUTH_ORIF_EXPR)
5220 && operand_equal_p (lhs, rhs, 0))
5221 {
5222 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5223 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5224 which cases we can't do this. */
5225 if (simple_operand_p (lhs))
5226 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5227 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5228 type, op0, op1);
5229
5230 else if (!lang_hooks.decls.global_bindings_p ()
5231 && !CONTAINS_PLACEHOLDER_P (lhs))
5232 {
5233 tree common = save_expr (lhs);
5234
5235 if (0 != (lhs = build_range_check (loc, type, common,
5236 or_op ? ! in0_p : in0_p,
5237 low0, high0))
5238 && (0 != (rhs = build_range_check (loc, type, common,
5239 or_op ? ! in1_p : in1_p,
5240 low1, high1))))
5241 {
5242 if (strict_overflow_p)
5243 fold_overflow_warning (warnmsg,
5244 WARN_STRICT_OVERFLOW_COMPARISON);
5245 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5246 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5247 type, lhs, rhs);
5248 }
5249 }
5250 }
5251
5252 return 0;
5253 }
5254 \f
5255 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5256 bit value. Arrange things so the extra bits will be set to zero if and
5257 only if C is signed-extended to its full width. If MASK is nonzero,
5258 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5259
5260 static tree
5261 unextend (tree c, int p, int unsignedp, tree mask)
5262 {
5263 tree type = TREE_TYPE (c);
5264 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5265 tree temp;
5266
5267 if (p == modesize || unsignedp)
5268 return c;
5269
5270 /* We work by getting just the sign bit into the low-order bit, then
5271 into the high-order bit, then sign-extend. We then XOR that value
5272 with C. */
5273 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5274
5275 /* We must use a signed type in order to get an arithmetic right shift.
5276 However, we must also avoid introducing accidental overflows, so that
5277 a subsequent call to integer_zerop will work. Hence we must
5278 do the type conversion here. At this point, the constant is either
5279 zero or one, and the conversion to a signed type can never overflow.
5280 We could get an overflow if this conversion is done anywhere else. */
5281 if (TYPE_UNSIGNED (type))
5282 temp = fold_convert (signed_type_for (type), temp);
5283
5284 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5285 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5286 if (mask != 0)
5287 temp = const_binop (BIT_AND_EXPR, temp,
5288 fold_convert (TREE_TYPE (c), mask));
5289 /* If necessary, convert the type back to match the type of C. */
5290 if (TYPE_UNSIGNED (type))
5291 temp = fold_convert (type, temp);
5292
5293 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5294 }
5295 \f
5296 /* For an expression that has the form
5297 (A && B) || ~B
5298 or
5299 (A || B) && ~B,
5300 we can drop one of the inner expressions and simplify to
5301 A || ~B
5302 or
5303 A && ~B
5304 LOC is the location of the resulting expression. OP is the inner
5305 logical operation; the left-hand side in the examples above, while CMPOP
5306 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5307 removing a condition that guards another, as in
5308 (A != NULL && A->...) || A == NULL
5309 which we must not transform. If RHS_ONLY is true, only eliminate the
5310 right-most operand of the inner logical operation. */
5311
5312 static tree
5313 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5314 bool rhs_only)
5315 {
5316 tree type = TREE_TYPE (cmpop);
5317 enum tree_code code = TREE_CODE (cmpop);
5318 enum tree_code truthop_code = TREE_CODE (op);
5319 tree lhs = TREE_OPERAND (op, 0);
5320 tree rhs = TREE_OPERAND (op, 1);
5321 tree orig_lhs = lhs, orig_rhs = rhs;
5322 enum tree_code rhs_code = TREE_CODE (rhs);
5323 enum tree_code lhs_code = TREE_CODE (lhs);
5324 enum tree_code inv_code;
5325
5326 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5327 return NULL_TREE;
5328
5329 if (TREE_CODE_CLASS (code) != tcc_comparison)
5330 return NULL_TREE;
5331
5332 if (rhs_code == truthop_code)
5333 {
5334 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5335 if (newrhs != NULL_TREE)
5336 {
5337 rhs = newrhs;
5338 rhs_code = TREE_CODE (rhs);
5339 }
5340 }
5341 if (lhs_code == truthop_code && !rhs_only)
5342 {
5343 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5344 if (newlhs != NULL_TREE)
5345 {
5346 lhs = newlhs;
5347 lhs_code = TREE_CODE (lhs);
5348 }
5349 }
5350
5351 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5352 if (inv_code == rhs_code
5353 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5354 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5355 return lhs;
5356 if (!rhs_only && inv_code == lhs_code
5357 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5358 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5359 return rhs;
5360 if (rhs != orig_rhs || lhs != orig_lhs)
5361 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5362 lhs, rhs);
5363 return NULL_TREE;
5364 }
5365
5366 /* Find ways of folding logical expressions of LHS and RHS:
5367 Try to merge two comparisons to the same innermost item.
5368 Look for range tests like "ch >= '0' && ch <= '9'".
5369 Look for combinations of simple terms on machines with expensive branches
5370 and evaluate the RHS unconditionally.
5371
5372 For example, if we have p->a == 2 && p->b == 4 and we can make an
5373 object large enough to span both A and B, we can do this with a comparison
5374 against the object ANDed with the a mask.
5375
5376 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5377 operations to do this with one comparison.
5378
5379 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5380 function and the one above.
5381
5382 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5383 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5384
5385 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5386 two operands.
5387
5388 We return the simplified tree or 0 if no optimization is possible. */
5389
5390 static tree
5391 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5392 tree lhs, tree rhs)
5393 {
5394 /* If this is the "or" of two comparisons, we can do something if
5395 the comparisons are NE_EXPR. If this is the "and", we can do something
5396 if the comparisons are EQ_EXPR. I.e.,
5397 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5398
5399 WANTED_CODE is this operation code. For single bit fields, we can
5400 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5401 comparison for one-bit fields. */
5402
5403 enum tree_code wanted_code;
5404 enum tree_code lcode, rcode;
5405 tree ll_arg, lr_arg, rl_arg, rr_arg;
5406 tree ll_inner, lr_inner, rl_inner, rr_inner;
5407 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5408 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5409 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5410 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5411 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5412 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5413 machine_mode lnmode, rnmode;
5414 tree ll_mask, lr_mask, rl_mask, rr_mask;
5415 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5416 tree l_const, r_const;
5417 tree lntype, rntype, result;
5418 HOST_WIDE_INT first_bit, end_bit;
5419 int volatilep;
5420
5421 /* Start by getting the comparison codes. Fail if anything is volatile.
5422 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5423 it were surrounded with a NE_EXPR. */
5424
5425 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5426 return 0;
5427
5428 lcode = TREE_CODE (lhs);
5429 rcode = TREE_CODE (rhs);
5430
5431 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5432 {
5433 lhs = build2 (NE_EXPR, truth_type, lhs,
5434 build_int_cst (TREE_TYPE (lhs), 0));
5435 lcode = NE_EXPR;
5436 }
5437
5438 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5439 {
5440 rhs = build2 (NE_EXPR, truth_type, rhs,
5441 build_int_cst (TREE_TYPE (rhs), 0));
5442 rcode = NE_EXPR;
5443 }
5444
5445 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5446 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5447 return 0;
5448
5449 ll_arg = TREE_OPERAND (lhs, 0);
5450 lr_arg = TREE_OPERAND (lhs, 1);
5451 rl_arg = TREE_OPERAND (rhs, 0);
5452 rr_arg = TREE_OPERAND (rhs, 1);
5453
5454 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5455 if (simple_operand_p (ll_arg)
5456 && simple_operand_p (lr_arg))
5457 {
5458 if (operand_equal_p (ll_arg, rl_arg, 0)
5459 && operand_equal_p (lr_arg, rr_arg, 0))
5460 {
5461 result = combine_comparisons (loc, code, lcode, rcode,
5462 truth_type, ll_arg, lr_arg);
5463 if (result)
5464 return result;
5465 }
5466 else if (operand_equal_p (ll_arg, rr_arg, 0)
5467 && operand_equal_p (lr_arg, rl_arg, 0))
5468 {
5469 result = combine_comparisons (loc, code, lcode,
5470 swap_tree_comparison (rcode),
5471 truth_type, ll_arg, lr_arg);
5472 if (result)
5473 return result;
5474 }
5475 }
5476
5477 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5478 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5479
5480 /* If the RHS can be evaluated unconditionally and its operands are
5481 simple, it wins to evaluate the RHS unconditionally on machines
5482 with expensive branches. In this case, this isn't a comparison
5483 that can be merged. */
5484
5485 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5486 false) >= 2
5487 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5488 && simple_operand_p (rl_arg)
5489 && simple_operand_p (rr_arg))
5490 {
5491 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5492 if (code == TRUTH_OR_EXPR
5493 && lcode == NE_EXPR && integer_zerop (lr_arg)
5494 && rcode == NE_EXPR && integer_zerop (rr_arg)
5495 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5496 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5497 return build2_loc (loc, NE_EXPR, truth_type,
5498 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5499 ll_arg, rl_arg),
5500 build_int_cst (TREE_TYPE (ll_arg), 0));
5501
5502 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5503 if (code == TRUTH_AND_EXPR
5504 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5505 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5506 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5507 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5508 return build2_loc (loc, EQ_EXPR, truth_type,
5509 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5510 ll_arg, rl_arg),
5511 build_int_cst (TREE_TYPE (ll_arg), 0));
5512 }
5513
5514 /* See if the comparisons can be merged. Then get all the parameters for
5515 each side. */
5516
5517 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5518 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5519 return 0;
5520
5521 volatilep = 0;
5522 ll_inner = decode_field_reference (loc, ll_arg,
5523 &ll_bitsize, &ll_bitpos, &ll_mode,
5524 &ll_unsignedp, &volatilep, &ll_mask,
5525 &ll_and_mask);
5526 lr_inner = decode_field_reference (loc, lr_arg,
5527 &lr_bitsize, &lr_bitpos, &lr_mode,
5528 &lr_unsignedp, &volatilep, &lr_mask,
5529 &lr_and_mask);
5530 rl_inner = decode_field_reference (loc, rl_arg,
5531 &rl_bitsize, &rl_bitpos, &rl_mode,
5532 &rl_unsignedp, &volatilep, &rl_mask,
5533 &rl_and_mask);
5534 rr_inner = decode_field_reference (loc, rr_arg,
5535 &rr_bitsize, &rr_bitpos, &rr_mode,
5536 &rr_unsignedp, &volatilep, &rr_mask,
5537 &rr_and_mask);
5538
5539 /* It must be true that the inner operation on the lhs of each
5540 comparison must be the same if we are to be able to do anything.
5541 Then see if we have constants. If not, the same must be true for
5542 the rhs's. */
5543 if (volatilep || ll_inner == 0 || rl_inner == 0
5544 || ! operand_equal_p (ll_inner, rl_inner, 0))
5545 return 0;
5546
5547 if (TREE_CODE (lr_arg) == INTEGER_CST
5548 && TREE_CODE (rr_arg) == INTEGER_CST)
5549 l_const = lr_arg, r_const = rr_arg;
5550 else if (lr_inner == 0 || rr_inner == 0
5551 || ! operand_equal_p (lr_inner, rr_inner, 0))
5552 return 0;
5553 else
5554 l_const = r_const = 0;
5555
5556 /* If either comparison code is not correct for our logical operation,
5557 fail. However, we can convert a one-bit comparison against zero into
5558 the opposite comparison against that bit being set in the field. */
5559
5560 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5561 if (lcode != wanted_code)
5562 {
5563 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5564 {
5565 /* Make the left operand unsigned, since we are only interested
5566 in the value of one bit. Otherwise we are doing the wrong
5567 thing below. */
5568 ll_unsignedp = 1;
5569 l_const = ll_mask;
5570 }
5571 else
5572 return 0;
5573 }
5574
5575 /* This is analogous to the code for l_const above. */
5576 if (rcode != wanted_code)
5577 {
5578 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5579 {
5580 rl_unsignedp = 1;
5581 r_const = rl_mask;
5582 }
5583 else
5584 return 0;
5585 }
5586
5587 /* See if we can find a mode that contains both fields being compared on
5588 the left. If we can't, fail. Otherwise, update all constants and masks
5589 to be relative to a field of that size. */
5590 first_bit = MIN (ll_bitpos, rl_bitpos);
5591 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5592 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5593 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5594 volatilep);
5595 if (lnmode == VOIDmode)
5596 return 0;
5597
5598 lnbitsize = GET_MODE_BITSIZE (lnmode);
5599 lnbitpos = first_bit & ~ (lnbitsize - 1);
5600 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5601 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5602
5603 if (BYTES_BIG_ENDIAN)
5604 {
5605 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5606 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5607 }
5608
5609 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5610 size_int (xll_bitpos));
5611 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5612 size_int (xrl_bitpos));
5613
5614 if (l_const)
5615 {
5616 l_const = fold_convert_loc (loc, lntype, l_const);
5617 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5618 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5619 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5620 fold_build1_loc (loc, BIT_NOT_EXPR,
5621 lntype, ll_mask))))
5622 {
5623 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5624
5625 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5626 }
5627 }
5628 if (r_const)
5629 {
5630 r_const = fold_convert_loc (loc, lntype, r_const);
5631 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5632 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5633 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5634 fold_build1_loc (loc, BIT_NOT_EXPR,
5635 lntype, rl_mask))))
5636 {
5637 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5638
5639 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5640 }
5641 }
5642
5643 /* If the right sides are not constant, do the same for it. Also,
5644 disallow this optimization if a size or signedness mismatch occurs
5645 between the left and right sides. */
5646 if (l_const == 0)
5647 {
5648 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5649 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5650 /* Make sure the two fields on the right
5651 correspond to the left without being swapped. */
5652 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5653 return 0;
5654
5655 first_bit = MIN (lr_bitpos, rr_bitpos);
5656 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5657 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5658 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5659 volatilep);
5660 if (rnmode == VOIDmode)
5661 return 0;
5662
5663 rnbitsize = GET_MODE_BITSIZE (rnmode);
5664 rnbitpos = first_bit & ~ (rnbitsize - 1);
5665 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5666 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5667
5668 if (BYTES_BIG_ENDIAN)
5669 {
5670 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5671 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5672 }
5673
5674 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5675 rntype, lr_mask),
5676 size_int (xlr_bitpos));
5677 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5678 rntype, rr_mask),
5679 size_int (xrr_bitpos));
5680
5681 /* Make a mask that corresponds to both fields being compared.
5682 Do this for both items being compared. If the operands are the
5683 same size and the bits being compared are in the same position
5684 then we can do this by masking both and comparing the masked
5685 results. */
5686 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5687 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5688 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5689 {
5690 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5691 ll_unsignedp || rl_unsignedp);
5692 if (! all_ones_mask_p (ll_mask, lnbitsize))
5693 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5694
5695 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5696 lr_unsignedp || rr_unsignedp);
5697 if (! all_ones_mask_p (lr_mask, rnbitsize))
5698 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5699
5700 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5701 }
5702
5703 /* There is still another way we can do something: If both pairs of
5704 fields being compared are adjacent, we may be able to make a wider
5705 field containing them both.
5706
5707 Note that we still must mask the lhs/rhs expressions. Furthermore,
5708 the mask must be shifted to account for the shift done by
5709 make_bit_field_ref. */
5710 if ((ll_bitsize + ll_bitpos == rl_bitpos
5711 && lr_bitsize + lr_bitpos == rr_bitpos)
5712 || (ll_bitpos == rl_bitpos + rl_bitsize
5713 && lr_bitpos == rr_bitpos + rr_bitsize))
5714 {
5715 tree type;
5716
5717 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5718 ll_bitsize + rl_bitsize,
5719 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5720 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5721 lr_bitsize + rr_bitsize,
5722 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5723
5724 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5725 size_int (MIN (xll_bitpos, xrl_bitpos)));
5726 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5727 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5728
5729 /* Convert to the smaller type before masking out unwanted bits. */
5730 type = lntype;
5731 if (lntype != rntype)
5732 {
5733 if (lnbitsize > rnbitsize)
5734 {
5735 lhs = fold_convert_loc (loc, rntype, lhs);
5736 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5737 type = rntype;
5738 }
5739 else if (lnbitsize < rnbitsize)
5740 {
5741 rhs = fold_convert_loc (loc, lntype, rhs);
5742 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5743 type = lntype;
5744 }
5745 }
5746
5747 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5748 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5749
5750 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5751 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5752
5753 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5754 }
5755
5756 return 0;
5757 }
5758
5759 /* Handle the case of comparisons with constants. If there is something in
5760 common between the masks, those bits of the constants must be the same.
5761 If not, the condition is always false. Test for this to avoid generating
5762 incorrect code below. */
5763 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5764 if (! integer_zerop (result)
5765 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5766 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5767 {
5768 if (wanted_code == NE_EXPR)
5769 {
5770 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5771 return constant_boolean_node (true, truth_type);
5772 }
5773 else
5774 {
5775 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5776 return constant_boolean_node (false, truth_type);
5777 }
5778 }
5779
5780 /* Construct the expression we will return. First get the component
5781 reference we will make. Unless the mask is all ones the width of
5782 that field, perform the mask operation. Then compare with the
5783 merged constant. */
5784 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5785 ll_unsignedp || rl_unsignedp);
5786
5787 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5788 if (! all_ones_mask_p (ll_mask, lnbitsize))
5789 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5790
5791 return build2_loc (loc, wanted_code, truth_type, result,
5792 const_binop (BIT_IOR_EXPR, l_const, r_const));
5793 }
5794 \f
5795 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5796 constant. */
5797
5798 static tree
5799 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5800 tree op0, tree op1)
5801 {
5802 tree arg0 = op0;
5803 enum tree_code op_code;
5804 tree comp_const;
5805 tree minmax_const;
5806 int consts_equal, consts_lt;
5807 tree inner;
5808
5809 STRIP_SIGN_NOPS (arg0);
5810
5811 op_code = TREE_CODE (arg0);
5812 minmax_const = TREE_OPERAND (arg0, 1);
5813 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5814 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5815 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5816 inner = TREE_OPERAND (arg0, 0);
5817
5818 /* If something does not permit us to optimize, return the original tree. */
5819 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5820 || TREE_CODE (comp_const) != INTEGER_CST
5821 || TREE_OVERFLOW (comp_const)
5822 || TREE_CODE (minmax_const) != INTEGER_CST
5823 || TREE_OVERFLOW (minmax_const))
5824 return NULL_TREE;
5825
5826 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5827 and GT_EXPR, doing the rest with recursive calls using logical
5828 simplifications. */
5829 switch (code)
5830 {
5831 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5832 {
5833 tree tem
5834 = optimize_minmax_comparison (loc,
5835 invert_tree_comparison (code, false),
5836 type, op0, op1);
5837 if (tem)
5838 return invert_truthvalue_loc (loc, tem);
5839 return NULL_TREE;
5840 }
5841
5842 case GE_EXPR:
5843 return
5844 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5845 optimize_minmax_comparison
5846 (loc, EQ_EXPR, type, arg0, comp_const),
5847 optimize_minmax_comparison
5848 (loc, GT_EXPR, type, arg0, comp_const));
5849
5850 case EQ_EXPR:
5851 if (op_code == MAX_EXPR && consts_equal)
5852 /* MAX (X, 0) == 0 -> X <= 0 */
5853 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5854
5855 else if (op_code == MAX_EXPR && consts_lt)
5856 /* MAX (X, 0) == 5 -> X == 5 */
5857 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5858
5859 else if (op_code == MAX_EXPR)
5860 /* MAX (X, 0) == -1 -> false */
5861 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5862
5863 else if (consts_equal)
5864 /* MIN (X, 0) == 0 -> X >= 0 */
5865 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5866
5867 else if (consts_lt)
5868 /* MIN (X, 0) == 5 -> false */
5869 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5870
5871 else
5872 /* MIN (X, 0) == -1 -> X == -1 */
5873 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5874
5875 case GT_EXPR:
5876 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5877 /* MAX (X, 0) > 0 -> X > 0
5878 MAX (X, 0) > 5 -> X > 5 */
5879 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5880
5881 else if (op_code == MAX_EXPR)
5882 /* MAX (X, 0) > -1 -> true */
5883 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5884
5885 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5886 /* MIN (X, 0) > 0 -> false
5887 MIN (X, 0) > 5 -> false */
5888 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5889
5890 else
5891 /* MIN (X, 0) > -1 -> X > -1 */
5892 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5893
5894 default:
5895 return NULL_TREE;
5896 }
5897 }
5898 \f
5899 /* T is an integer expression that is being multiplied, divided, or taken a
5900 modulus (CODE says which and what kind of divide or modulus) by a
5901 constant C. See if we can eliminate that operation by folding it with
5902 other operations already in T. WIDE_TYPE, if non-null, is a type that
5903 should be used for the computation if wider than our type.
5904
5905 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5906 (X * 2) + (Y * 4). We must, however, be assured that either the original
5907 expression would not overflow or that overflow is undefined for the type
5908 in the language in question.
5909
5910 If we return a non-null expression, it is an equivalent form of the
5911 original computation, but need not be in the original type.
5912
5913 We set *STRICT_OVERFLOW_P to true if the return values depends on
5914 signed overflow being undefined. Otherwise we do not change
5915 *STRICT_OVERFLOW_P. */
5916
5917 static tree
5918 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5919 bool *strict_overflow_p)
5920 {
5921 /* To avoid exponential search depth, refuse to allow recursion past
5922 three levels. Beyond that (1) it's highly unlikely that we'll find
5923 something interesting and (2) we've probably processed it before
5924 when we built the inner expression. */
5925
5926 static int depth;
5927 tree ret;
5928
5929 if (depth > 3)
5930 return NULL;
5931
5932 depth++;
5933 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5934 depth--;
5935
5936 return ret;
5937 }
5938
5939 static tree
5940 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5941 bool *strict_overflow_p)
5942 {
5943 tree type = TREE_TYPE (t);
5944 enum tree_code tcode = TREE_CODE (t);
5945 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5946 > GET_MODE_SIZE (TYPE_MODE (type)))
5947 ? wide_type : type);
5948 tree t1, t2;
5949 int same_p = tcode == code;
5950 tree op0 = NULL_TREE, op1 = NULL_TREE;
5951 bool sub_strict_overflow_p;
5952
5953 /* Don't deal with constants of zero here; they confuse the code below. */
5954 if (integer_zerop (c))
5955 return NULL_TREE;
5956
5957 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5958 op0 = TREE_OPERAND (t, 0);
5959
5960 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5961 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5962
5963 /* Note that we need not handle conditional operations here since fold
5964 already handles those cases. So just do arithmetic here. */
5965 switch (tcode)
5966 {
5967 case INTEGER_CST:
5968 /* For a constant, we can always simplify if we are a multiply
5969 or (for divide and modulus) if it is a multiple of our constant. */
5970 if (code == MULT_EXPR
5971 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5972 return const_binop (code, fold_convert (ctype, t),
5973 fold_convert (ctype, c));
5974 break;
5975
5976 CASE_CONVERT: case NON_LVALUE_EXPR:
5977 /* If op0 is an expression ... */
5978 if ((COMPARISON_CLASS_P (op0)
5979 || UNARY_CLASS_P (op0)
5980 || BINARY_CLASS_P (op0)
5981 || VL_EXP_CLASS_P (op0)
5982 || EXPRESSION_CLASS_P (op0))
5983 /* ... and has wrapping overflow, and its type is smaller
5984 than ctype, then we cannot pass through as widening. */
5985 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5986 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5987 && (TYPE_PRECISION (ctype)
5988 > TYPE_PRECISION (TREE_TYPE (op0))))
5989 /* ... or this is a truncation (t is narrower than op0),
5990 then we cannot pass through this narrowing. */
5991 || (TYPE_PRECISION (type)
5992 < TYPE_PRECISION (TREE_TYPE (op0)))
5993 /* ... or signedness changes for division or modulus,
5994 then we cannot pass through this conversion. */
5995 || (code != MULT_EXPR
5996 && (TYPE_UNSIGNED (ctype)
5997 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5998 /* ... or has undefined overflow while the converted to
5999 type has not, we cannot do the operation in the inner type
6000 as that would introduce undefined overflow. */
6001 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6002 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6003 && !TYPE_OVERFLOW_UNDEFINED (type))))
6004 break;
6005
6006 /* Pass the constant down and see if we can make a simplification. If
6007 we can, replace this expression with the inner simplification for
6008 possible later conversion to our or some other type. */
6009 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6010 && TREE_CODE (t2) == INTEGER_CST
6011 && !TREE_OVERFLOW (t2)
6012 && (0 != (t1 = extract_muldiv (op0, t2, code,
6013 code == MULT_EXPR
6014 ? ctype : NULL_TREE,
6015 strict_overflow_p))))
6016 return t1;
6017 break;
6018
6019 case ABS_EXPR:
6020 /* If widening the type changes it from signed to unsigned, then we
6021 must avoid building ABS_EXPR itself as unsigned. */
6022 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6023 {
6024 tree cstype = (*signed_type_for) (ctype);
6025 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6026 != 0)
6027 {
6028 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6029 return fold_convert (ctype, t1);
6030 }
6031 break;
6032 }
6033 /* If the constant is negative, we cannot simplify this. */
6034 if (tree_int_cst_sgn (c) == -1)
6035 break;
6036 /* FALLTHROUGH */
6037 case NEGATE_EXPR:
6038 /* For division and modulus, type can't be unsigned, as e.g.
6039 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6040 For signed types, even with wrapping overflow, this is fine. */
6041 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6042 break;
6043 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6044 != 0)
6045 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6046 break;
6047
6048 case MIN_EXPR: case MAX_EXPR:
6049 /* If widening the type changes the signedness, then we can't perform
6050 this optimization as that changes the result. */
6051 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6052 break;
6053
6054 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6055 sub_strict_overflow_p = false;
6056 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6057 &sub_strict_overflow_p)) != 0
6058 && (t2 = extract_muldiv (op1, c, code, wide_type,
6059 &sub_strict_overflow_p)) != 0)
6060 {
6061 if (tree_int_cst_sgn (c) < 0)
6062 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6063 if (sub_strict_overflow_p)
6064 *strict_overflow_p = true;
6065 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6066 fold_convert (ctype, t2));
6067 }
6068 break;
6069
6070 case LSHIFT_EXPR: case RSHIFT_EXPR:
6071 /* If the second operand is constant, this is a multiplication
6072 or floor division, by a power of two, so we can treat it that
6073 way unless the multiplier or divisor overflows. Signed
6074 left-shift overflow is implementation-defined rather than
6075 undefined in C90, so do not convert signed left shift into
6076 multiplication. */
6077 if (TREE_CODE (op1) == INTEGER_CST
6078 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6079 /* const_binop may not detect overflow correctly,
6080 so check for it explicitly here. */
6081 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6082 && 0 != (t1 = fold_convert (ctype,
6083 const_binop (LSHIFT_EXPR,
6084 size_one_node,
6085 op1)))
6086 && !TREE_OVERFLOW (t1))
6087 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6088 ? MULT_EXPR : FLOOR_DIV_EXPR,
6089 ctype,
6090 fold_convert (ctype, op0),
6091 t1),
6092 c, code, wide_type, strict_overflow_p);
6093 break;
6094
6095 case PLUS_EXPR: case MINUS_EXPR:
6096 /* See if we can eliminate the operation on both sides. If we can, we
6097 can return a new PLUS or MINUS. If we can't, the only remaining
6098 cases where we can do anything are if the second operand is a
6099 constant. */
6100 sub_strict_overflow_p = false;
6101 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6102 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6103 if (t1 != 0 && t2 != 0
6104 && (code == MULT_EXPR
6105 /* If not multiplication, we can only do this if both operands
6106 are divisible by c. */
6107 || (multiple_of_p (ctype, op0, c)
6108 && multiple_of_p (ctype, op1, c))))
6109 {
6110 if (sub_strict_overflow_p)
6111 *strict_overflow_p = true;
6112 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6113 fold_convert (ctype, t2));
6114 }
6115
6116 /* If this was a subtraction, negate OP1 and set it to be an addition.
6117 This simplifies the logic below. */
6118 if (tcode == MINUS_EXPR)
6119 {
6120 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6121 /* If OP1 was not easily negatable, the constant may be OP0. */
6122 if (TREE_CODE (op0) == INTEGER_CST)
6123 {
6124 std::swap (op0, op1);
6125 std::swap (t1, t2);
6126 }
6127 }
6128
6129 if (TREE_CODE (op1) != INTEGER_CST)
6130 break;
6131
6132 /* If either OP1 or C are negative, this optimization is not safe for
6133 some of the division and remainder types while for others we need
6134 to change the code. */
6135 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6136 {
6137 if (code == CEIL_DIV_EXPR)
6138 code = FLOOR_DIV_EXPR;
6139 else if (code == FLOOR_DIV_EXPR)
6140 code = CEIL_DIV_EXPR;
6141 else if (code != MULT_EXPR
6142 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6143 break;
6144 }
6145
6146 /* If it's a multiply or a division/modulus operation of a multiple
6147 of our constant, do the operation and verify it doesn't overflow. */
6148 if (code == MULT_EXPR
6149 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6150 {
6151 op1 = const_binop (code, fold_convert (ctype, op1),
6152 fold_convert (ctype, c));
6153 /* We allow the constant to overflow with wrapping semantics. */
6154 if (op1 == 0
6155 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6156 break;
6157 }
6158 else
6159 break;
6160
6161 /* If we have an unsigned type, we cannot widen the operation since it
6162 will change the result if the original computation overflowed. */
6163 if (TYPE_UNSIGNED (ctype) && ctype != type)
6164 break;
6165
6166 /* If we were able to eliminate our operation from the first side,
6167 apply our operation to the second side and reform the PLUS. */
6168 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6169 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6170
6171 /* The last case is if we are a multiply. In that case, we can
6172 apply the distributive law to commute the multiply and addition
6173 if the multiplication of the constants doesn't overflow
6174 and overflow is defined. With undefined overflow
6175 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6176 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6177 return fold_build2 (tcode, ctype,
6178 fold_build2 (code, ctype,
6179 fold_convert (ctype, op0),
6180 fold_convert (ctype, c)),
6181 op1);
6182
6183 break;
6184
6185 case MULT_EXPR:
6186 /* We have a special case here if we are doing something like
6187 (C * 8) % 4 since we know that's zero. */
6188 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6189 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6190 /* If the multiplication can overflow we cannot optimize this. */
6191 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6192 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6193 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6194 {
6195 *strict_overflow_p = true;
6196 return omit_one_operand (type, integer_zero_node, op0);
6197 }
6198
6199 /* ... fall through ... */
6200
6201 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6202 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6203 /* If we can extract our operation from the LHS, do so and return a
6204 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6205 do something only if the second operand is a constant. */
6206 if (same_p
6207 && (t1 = extract_muldiv (op0, c, code, wide_type,
6208 strict_overflow_p)) != 0)
6209 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6210 fold_convert (ctype, op1));
6211 else if (tcode == MULT_EXPR && code == MULT_EXPR
6212 && (t1 = extract_muldiv (op1, c, code, wide_type,
6213 strict_overflow_p)) != 0)
6214 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6215 fold_convert (ctype, t1));
6216 else if (TREE_CODE (op1) != INTEGER_CST)
6217 return 0;
6218
6219 /* If these are the same operation types, we can associate them
6220 assuming no overflow. */
6221 if (tcode == code)
6222 {
6223 bool overflow_p = false;
6224 bool overflow_mul_p;
6225 signop sign = TYPE_SIGN (ctype);
6226 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6227 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6228 if (overflow_mul_p
6229 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6230 overflow_p = true;
6231 if (!overflow_p)
6232 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6233 wide_int_to_tree (ctype, mul));
6234 }
6235
6236 /* If these operations "cancel" each other, we have the main
6237 optimizations of this pass, which occur when either constant is a
6238 multiple of the other, in which case we replace this with either an
6239 operation or CODE or TCODE.
6240
6241 If we have an unsigned type, we cannot do this since it will change
6242 the result if the original computation overflowed. */
6243 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6244 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6245 || (tcode == MULT_EXPR
6246 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6247 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6248 && code != MULT_EXPR)))
6249 {
6250 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6251 {
6252 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6253 *strict_overflow_p = true;
6254 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6255 fold_convert (ctype,
6256 const_binop (TRUNC_DIV_EXPR,
6257 op1, c)));
6258 }
6259 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6260 {
6261 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6262 *strict_overflow_p = true;
6263 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6264 fold_convert (ctype,
6265 const_binop (TRUNC_DIV_EXPR,
6266 c, op1)));
6267 }
6268 }
6269 break;
6270
6271 default:
6272 break;
6273 }
6274
6275 return 0;
6276 }
6277 \f
6278 /* Return a node which has the indicated constant VALUE (either 0 or
6279 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6280 and is of the indicated TYPE. */
6281
6282 tree
6283 constant_boolean_node (bool value, tree type)
6284 {
6285 if (type == integer_type_node)
6286 return value ? integer_one_node : integer_zero_node;
6287 else if (type == boolean_type_node)
6288 return value ? boolean_true_node : boolean_false_node;
6289 else if (TREE_CODE (type) == VECTOR_TYPE)
6290 return build_vector_from_val (type,
6291 build_int_cst (TREE_TYPE (type),
6292 value ? -1 : 0));
6293 else
6294 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6295 }
6296
6297
6298 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6299 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6300 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6301 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6302 COND is the first argument to CODE; otherwise (as in the example
6303 given here), it is the second argument. TYPE is the type of the
6304 original expression. Return NULL_TREE if no simplification is
6305 possible. */
6306
6307 static tree
6308 fold_binary_op_with_conditional_arg (location_t loc,
6309 enum tree_code code,
6310 tree type, tree op0, tree op1,
6311 tree cond, tree arg, int cond_first_p)
6312 {
6313 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6314 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6315 tree test, true_value, false_value;
6316 tree lhs = NULL_TREE;
6317 tree rhs = NULL_TREE;
6318 enum tree_code cond_code = COND_EXPR;
6319
6320 if (TREE_CODE (cond) == COND_EXPR
6321 || TREE_CODE (cond) == VEC_COND_EXPR)
6322 {
6323 test = TREE_OPERAND (cond, 0);
6324 true_value = TREE_OPERAND (cond, 1);
6325 false_value = TREE_OPERAND (cond, 2);
6326 /* If this operand throws an expression, then it does not make
6327 sense to try to perform a logical or arithmetic operation
6328 involving it. */
6329 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6330 lhs = true_value;
6331 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6332 rhs = false_value;
6333 }
6334 else
6335 {
6336 tree testtype = TREE_TYPE (cond);
6337 test = cond;
6338 true_value = constant_boolean_node (true, testtype);
6339 false_value = constant_boolean_node (false, testtype);
6340 }
6341
6342 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6343 cond_code = VEC_COND_EXPR;
6344
6345 /* This transformation is only worthwhile if we don't have to wrap ARG
6346 in a SAVE_EXPR and the operation can be simplified without recursing
6347 on at least one of the branches once its pushed inside the COND_EXPR. */
6348 if (!TREE_CONSTANT (arg)
6349 && (TREE_SIDE_EFFECTS (arg)
6350 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6351 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6352 return NULL_TREE;
6353
6354 arg = fold_convert_loc (loc, arg_type, arg);
6355 if (lhs == 0)
6356 {
6357 true_value = fold_convert_loc (loc, cond_type, true_value);
6358 if (cond_first_p)
6359 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6360 else
6361 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6362 }
6363 if (rhs == 0)
6364 {
6365 false_value = fold_convert_loc (loc, cond_type, false_value);
6366 if (cond_first_p)
6367 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6368 else
6369 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6370 }
6371
6372 /* Check that we have simplified at least one of the branches. */
6373 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6374 return NULL_TREE;
6375
6376 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6377 }
6378
6379 \f
6380 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6381
6382 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6383 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6384 ADDEND is the same as X.
6385
6386 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6387 and finite. The problematic cases are when X is zero, and its mode
6388 has signed zeros. In the case of rounding towards -infinity,
6389 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6390 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6391
6392 bool
6393 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6394 {
6395 if (!real_zerop (addend))
6396 return false;
6397
6398 /* Don't allow the fold with -fsignaling-nans. */
6399 if (HONOR_SNANS (element_mode (type)))
6400 return false;
6401
6402 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6403 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6404 return true;
6405
6406 /* In a vector or complex, we would need to check the sign of all zeros. */
6407 if (TREE_CODE (addend) != REAL_CST)
6408 return false;
6409
6410 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6411 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6412 negate = !negate;
6413
6414 /* The mode has signed zeros, and we have to honor their sign.
6415 In this situation, there is only one case we can return true for.
6416 X - 0 is the same as X unless rounding towards -infinity is
6417 supported. */
6418 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6419 }
6420
6421 /* Subroutine of fold() that checks comparisons of built-in math
6422 functions against real constants.
6423
6424 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6425 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6426 is the type of the result and ARG0 and ARG1 are the operands of the
6427 comparison. ARG1 must be a TREE_REAL_CST.
6428
6429 The function returns the constant folded tree if a simplification
6430 can be made, and NULL_TREE otherwise. */
6431
6432 static tree
6433 fold_mathfn_compare (location_t loc,
6434 enum built_in_function fcode, enum tree_code code,
6435 tree type, tree arg0, tree arg1)
6436 {
6437 REAL_VALUE_TYPE c;
6438
6439 if (BUILTIN_SQRT_P (fcode))
6440 {
6441 tree arg = CALL_EXPR_ARG (arg0, 0);
6442 machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6443
6444 c = TREE_REAL_CST (arg1);
6445 if (REAL_VALUE_NEGATIVE (c))
6446 {
6447 /* sqrt(x) < y is always false, if y is negative. */
6448 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6449 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6450
6451 /* sqrt(x) > y is always true, if y is negative and we
6452 don't care about NaNs, i.e. negative values of x. */
6453 if (code == NE_EXPR || !HONOR_NANS (mode))
6454 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6455
6456 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6457 return fold_build2_loc (loc, GE_EXPR, type, arg,
6458 build_real (TREE_TYPE (arg), dconst0));
6459 }
6460 else if (code == GT_EXPR || code == GE_EXPR)
6461 {
6462 REAL_VALUE_TYPE c2;
6463
6464 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6465 real_convert (&c2, mode, &c2);
6466
6467 if (REAL_VALUE_ISINF (c2))
6468 {
6469 /* sqrt(x) > y is x == +Inf, when y is very large. */
6470 if (HONOR_INFINITIES (mode))
6471 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6472 build_real (TREE_TYPE (arg), c2));
6473
6474 /* sqrt(x) > y is always false, when y is very large
6475 and we don't care about infinities. */
6476 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6477 }
6478
6479 /* sqrt(x) > c is the same as x > c*c. */
6480 return fold_build2_loc (loc, code, type, arg,
6481 build_real (TREE_TYPE (arg), c2));
6482 }
6483 else if (code == LT_EXPR || code == LE_EXPR)
6484 {
6485 REAL_VALUE_TYPE c2;
6486
6487 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6488 real_convert (&c2, mode, &c2);
6489
6490 if (REAL_VALUE_ISINF (c2))
6491 {
6492 /* sqrt(x) < y is always true, when y is a very large
6493 value and we don't care about NaNs or Infinities. */
6494 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6495 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6496
6497 /* sqrt(x) < y is x != +Inf when y is very large and we
6498 don't care about NaNs. */
6499 if (! HONOR_NANS (mode))
6500 return fold_build2_loc (loc, NE_EXPR, type, arg,
6501 build_real (TREE_TYPE (arg), c2));
6502
6503 /* sqrt(x) < y is x >= 0 when y is very large and we
6504 don't care about Infinities. */
6505 if (! HONOR_INFINITIES (mode))
6506 return fold_build2_loc (loc, GE_EXPR, type, arg,
6507 build_real (TREE_TYPE (arg), dconst0));
6508
6509 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6510 arg = save_expr (arg);
6511 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6512 fold_build2_loc (loc, GE_EXPR, type, arg,
6513 build_real (TREE_TYPE (arg),
6514 dconst0)),
6515 fold_build2_loc (loc, NE_EXPR, type, arg,
6516 build_real (TREE_TYPE (arg),
6517 c2)));
6518 }
6519
6520 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6521 if (! HONOR_NANS (mode))
6522 return fold_build2_loc (loc, code, type, arg,
6523 build_real (TREE_TYPE (arg), c2));
6524
6525 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6526 arg = save_expr (arg);
6527 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6528 fold_build2_loc (loc, GE_EXPR, type, arg,
6529 build_real (TREE_TYPE (arg),
6530 dconst0)),
6531 fold_build2_loc (loc, code, type, arg,
6532 build_real (TREE_TYPE (arg),
6533 c2)));
6534 }
6535 }
6536
6537 return NULL_TREE;
6538 }
6539
6540 /* Subroutine of fold() that optimizes comparisons against Infinities,
6541 either +Inf or -Inf.
6542
6543 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6544 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6545 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6546
6547 The function returns the constant folded tree if a simplification
6548 can be made, and NULL_TREE otherwise. */
6549
6550 static tree
6551 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6552 tree arg0, tree arg1)
6553 {
6554 machine_mode mode;
6555 REAL_VALUE_TYPE max;
6556 tree temp;
6557 bool neg;
6558
6559 mode = TYPE_MODE (TREE_TYPE (arg0));
6560
6561 /* For negative infinity swap the sense of the comparison. */
6562 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6563 if (neg)
6564 code = swap_tree_comparison (code);
6565
6566 switch (code)
6567 {
6568 case GT_EXPR:
6569 /* x > +Inf is always false, if with ignore sNANs. */
6570 if (HONOR_SNANS (mode))
6571 return NULL_TREE;
6572 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6573
6574 case LE_EXPR:
6575 /* x <= +Inf is always true, if we don't case about NaNs. */
6576 if (! HONOR_NANS (mode))
6577 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6578
6579 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6580 arg0 = save_expr (arg0);
6581 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6582
6583 case EQ_EXPR:
6584 case GE_EXPR:
6585 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6586 real_maxval (&max, neg, mode);
6587 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6588 arg0, build_real (TREE_TYPE (arg0), max));
6589
6590 case LT_EXPR:
6591 /* x < +Inf is always equal to x <= DBL_MAX. */
6592 real_maxval (&max, neg, mode);
6593 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6594 arg0, build_real (TREE_TYPE (arg0), max));
6595
6596 case NE_EXPR:
6597 /* x != +Inf is always equal to !(x > DBL_MAX). */
6598 real_maxval (&max, neg, mode);
6599 if (! HONOR_NANS (mode))
6600 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6601 arg0, build_real (TREE_TYPE (arg0), max));
6602
6603 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6604 arg0, build_real (TREE_TYPE (arg0), max));
6605 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6606
6607 default:
6608 break;
6609 }
6610
6611 return NULL_TREE;
6612 }
6613
6614 /* Subroutine of fold() that optimizes comparisons of a division by
6615 a nonzero integer constant against an integer constant, i.e.
6616 X/C1 op C2.
6617
6618 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6619 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6620 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6621
6622 The function returns the constant folded tree if a simplification
6623 can be made, and NULL_TREE otherwise. */
6624
6625 static tree
6626 fold_div_compare (location_t loc,
6627 enum tree_code code, tree type, tree arg0, tree arg1)
6628 {
6629 tree prod, tmp, hi, lo;
6630 tree arg00 = TREE_OPERAND (arg0, 0);
6631 tree arg01 = TREE_OPERAND (arg0, 1);
6632 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6633 bool neg_overflow = false;
6634 bool overflow;
6635
6636 /* We have to do this the hard way to detect unsigned overflow.
6637 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6638 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6639 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6640 neg_overflow = false;
6641
6642 if (sign == UNSIGNED)
6643 {
6644 tmp = int_const_binop (MINUS_EXPR, arg01,
6645 build_int_cst (TREE_TYPE (arg01), 1));
6646 lo = prod;
6647
6648 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6649 val = wi::add (prod, tmp, sign, &overflow);
6650 hi = force_fit_type (TREE_TYPE (arg00), val,
6651 -1, overflow | TREE_OVERFLOW (prod));
6652 }
6653 else if (tree_int_cst_sgn (arg01) >= 0)
6654 {
6655 tmp = int_const_binop (MINUS_EXPR, arg01,
6656 build_int_cst (TREE_TYPE (arg01), 1));
6657 switch (tree_int_cst_sgn (arg1))
6658 {
6659 case -1:
6660 neg_overflow = true;
6661 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6662 hi = prod;
6663 break;
6664
6665 case 0:
6666 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6667 hi = tmp;
6668 break;
6669
6670 case 1:
6671 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6672 lo = prod;
6673 break;
6674
6675 default:
6676 gcc_unreachable ();
6677 }
6678 }
6679 else
6680 {
6681 /* A negative divisor reverses the relational operators. */
6682 code = swap_tree_comparison (code);
6683
6684 tmp = int_const_binop (PLUS_EXPR, arg01,
6685 build_int_cst (TREE_TYPE (arg01), 1));
6686 switch (tree_int_cst_sgn (arg1))
6687 {
6688 case -1:
6689 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6690 lo = prod;
6691 break;
6692
6693 case 0:
6694 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6695 lo = tmp;
6696 break;
6697
6698 case 1:
6699 neg_overflow = true;
6700 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6701 hi = prod;
6702 break;
6703
6704 default:
6705 gcc_unreachable ();
6706 }
6707 }
6708
6709 switch (code)
6710 {
6711 case EQ_EXPR:
6712 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6713 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6714 if (TREE_OVERFLOW (hi))
6715 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6716 if (TREE_OVERFLOW (lo))
6717 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6718 return build_range_check (loc, type, arg00, 1, lo, hi);
6719
6720 case NE_EXPR:
6721 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6722 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6723 if (TREE_OVERFLOW (hi))
6724 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6725 if (TREE_OVERFLOW (lo))
6726 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6727 return build_range_check (loc, type, arg00, 0, lo, hi);
6728
6729 case LT_EXPR:
6730 if (TREE_OVERFLOW (lo))
6731 {
6732 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6733 return omit_one_operand_loc (loc, type, tmp, arg00);
6734 }
6735 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6736
6737 case LE_EXPR:
6738 if (TREE_OVERFLOW (hi))
6739 {
6740 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6741 return omit_one_operand_loc (loc, type, tmp, arg00);
6742 }
6743 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6744
6745 case GT_EXPR:
6746 if (TREE_OVERFLOW (hi))
6747 {
6748 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6749 return omit_one_operand_loc (loc, type, tmp, arg00);
6750 }
6751 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6752
6753 case GE_EXPR:
6754 if (TREE_OVERFLOW (lo))
6755 {
6756 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6757 return omit_one_operand_loc (loc, type, tmp, arg00);
6758 }
6759 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6760
6761 default:
6762 break;
6763 }
6764
6765 return NULL_TREE;
6766 }
6767
6768
6769 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6770 equality/inequality test, then return a simplified form of the test
6771 using a sign testing. Otherwise return NULL. TYPE is the desired
6772 result type. */
6773
6774 static tree
6775 fold_single_bit_test_into_sign_test (location_t loc,
6776 enum tree_code code, tree arg0, tree arg1,
6777 tree result_type)
6778 {
6779 /* If this is testing a single bit, we can optimize the test. */
6780 if ((code == NE_EXPR || code == EQ_EXPR)
6781 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6782 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6783 {
6784 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6785 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6786 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6787
6788 if (arg00 != NULL_TREE
6789 /* This is only a win if casting to a signed type is cheap,
6790 i.e. when arg00's type is not a partial mode. */
6791 && TYPE_PRECISION (TREE_TYPE (arg00))
6792 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6793 {
6794 tree stype = signed_type_for (TREE_TYPE (arg00));
6795 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6796 result_type,
6797 fold_convert_loc (loc, stype, arg00),
6798 build_int_cst (stype, 0));
6799 }
6800 }
6801
6802 return NULL_TREE;
6803 }
6804
6805 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6806 equality/inequality test, then return a simplified form of
6807 the test using shifts and logical operations. Otherwise return
6808 NULL. TYPE is the desired result type. */
6809
6810 tree
6811 fold_single_bit_test (location_t loc, enum tree_code code,
6812 tree arg0, tree arg1, tree result_type)
6813 {
6814 /* If this is testing a single bit, we can optimize the test. */
6815 if ((code == NE_EXPR || code == EQ_EXPR)
6816 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6817 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6818 {
6819 tree inner = TREE_OPERAND (arg0, 0);
6820 tree type = TREE_TYPE (arg0);
6821 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6822 machine_mode operand_mode = TYPE_MODE (type);
6823 int ops_unsigned;
6824 tree signed_type, unsigned_type, intermediate_type;
6825 tree tem, one;
6826
6827 /* First, see if we can fold the single bit test into a sign-bit
6828 test. */
6829 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6830 result_type);
6831 if (tem)
6832 return tem;
6833
6834 /* Otherwise we have (A & C) != 0 where C is a single bit,
6835 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6836 Similarly for (A & C) == 0. */
6837
6838 /* If INNER is a right shift of a constant and it plus BITNUM does
6839 not overflow, adjust BITNUM and INNER. */
6840 if (TREE_CODE (inner) == RSHIFT_EXPR
6841 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6842 && bitnum < TYPE_PRECISION (type)
6843 && wi::ltu_p (TREE_OPERAND (inner, 1),
6844 TYPE_PRECISION (type) - bitnum))
6845 {
6846 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6847 inner = TREE_OPERAND (inner, 0);
6848 }
6849
6850 /* If we are going to be able to omit the AND below, we must do our
6851 operations as unsigned. If we must use the AND, we have a choice.
6852 Normally unsigned is faster, but for some machines signed is. */
6853 #ifdef LOAD_EXTEND_OP
6854 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6855 && !flag_syntax_only) ? 0 : 1;
6856 #else
6857 ops_unsigned = 1;
6858 #endif
6859
6860 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6861 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6862 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6863 inner = fold_convert_loc (loc, intermediate_type, inner);
6864
6865 if (bitnum != 0)
6866 inner = build2 (RSHIFT_EXPR, intermediate_type,
6867 inner, size_int (bitnum));
6868
6869 one = build_int_cst (intermediate_type, 1);
6870
6871 if (code == EQ_EXPR)
6872 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6873
6874 /* Put the AND last so it can combine with more things. */
6875 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6876
6877 /* Make sure to return the proper type. */
6878 inner = fold_convert_loc (loc, result_type, inner);
6879
6880 return inner;
6881 }
6882 return NULL_TREE;
6883 }
6884
6885 /* Check whether we are allowed to reorder operands arg0 and arg1,
6886 such that the evaluation of arg1 occurs before arg0. */
6887
6888 static bool
6889 reorder_operands_p (const_tree arg0, const_tree arg1)
6890 {
6891 if (! flag_evaluation_order)
6892 return true;
6893 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6894 return true;
6895 return ! TREE_SIDE_EFFECTS (arg0)
6896 && ! TREE_SIDE_EFFECTS (arg1);
6897 }
6898
6899 /* Test whether it is preferable two swap two operands, ARG0 and
6900 ARG1, for example because ARG0 is an integer constant and ARG1
6901 isn't. If REORDER is true, only recommend swapping if we can
6902 evaluate the operands in reverse order. */
6903
6904 bool
6905 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6906 {
6907 if (CONSTANT_CLASS_P (arg1))
6908 return 0;
6909 if (CONSTANT_CLASS_P (arg0))
6910 return 1;
6911
6912 STRIP_NOPS (arg0);
6913 STRIP_NOPS (arg1);
6914
6915 if (TREE_CONSTANT (arg1))
6916 return 0;
6917 if (TREE_CONSTANT (arg0))
6918 return 1;
6919
6920 if (reorder && flag_evaluation_order
6921 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6922 return 0;
6923
6924 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6925 for commutative and comparison operators. Ensuring a canonical
6926 form allows the optimizers to find additional redundancies without
6927 having to explicitly check for both orderings. */
6928 if (TREE_CODE (arg0) == SSA_NAME
6929 && TREE_CODE (arg1) == SSA_NAME
6930 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6931 return 1;
6932
6933 /* Put SSA_NAMEs last. */
6934 if (TREE_CODE (arg1) == SSA_NAME)
6935 return 0;
6936 if (TREE_CODE (arg0) == SSA_NAME)
6937 return 1;
6938
6939 /* Put variables last. */
6940 if (DECL_P (arg1))
6941 return 0;
6942 if (DECL_P (arg0))
6943 return 1;
6944
6945 return 0;
6946 }
6947
6948 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6949 ARG0 is extended to a wider type. */
6950
6951 static tree
6952 fold_widened_comparison (location_t loc, enum tree_code code,
6953 tree type, tree arg0, tree arg1)
6954 {
6955 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6956 tree arg1_unw;
6957 tree shorter_type, outer_type;
6958 tree min, max;
6959 bool above, below;
6960
6961 if (arg0_unw == arg0)
6962 return NULL_TREE;
6963 shorter_type = TREE_TYPE (arg0_unw);
6964
6965 /* Disable this optimization if we're casting a function pointer
6966 type on targets that require function pointer canonicalization. */
6967 if (targetm.have_canonicalize_funcptr_for_compare ()
6968 && TREE_CODE (shorter_type) == POINTER_TYPE
6969 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6970 return NULL_TREE;
6971
6972 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6973 return NULL_TREE;
6974
6975 arg1_unw = get_unwidened (arg1, NULL_TREE);
6976
6977 /* If possible, express the comparison in the shorter mode. */
6978 if ((code == EQ_EXPR || code == NE_EXPR
6979 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6980 && (TREE_TYPE (arg1_unw) == shorter_type
6981 || ((TYPE_PRECISION (shorter_type)
6982 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6983 && (TYPE_UNSIGNED (shorter_type)
6984 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6985 || (TREE_CODE (arg1_unw) == INTEGER_CST
6986 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6987 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6988 && int_fits_type_p (arg1_unw, shorter_type))))
6989 return fold_build2_loc (loc, code, type, arg0_unw,
6990 fold_convert_loc (loc, shorter_type, arg1_unw));
6991
6992 if (TREE_CODE (arg1_unw) != INTEGER_CST
6993 || TREE_CODE (shorter_type) != INTEGER_TYPE
6994 || !int_fits_type_p (arg1_unw, shorter_type))
6995 return NULL_TREE;
6996
6997 /* If we are comparing with the integer that does not fit into the range
6998 of the shorter type, the result is known. */
6999 outer_type = TREE_TYPE (arg1_unw);
7000 min = lower_bound_in_type (outer_type, shorter_type);
7001 max = upper_bound_in_type (outer_type, shorter_type);
7002
7003 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7004 max, arg1_unw));
7005 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7006 arg1_unw, min));
7007
7008 switch (code)
7009 {
7010 case EQ_EXPR:
7011 if (above || below)
7012 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7013 break;
7014
7015 case NE_EXPR:
7016 if (above || below)
7017 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7018 break;
7019
7020 case LT_EXPR:
7021 case LE_EXPR:
7022 if (above)
7023 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7024 else if (below)
7025 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7026
7027 case GT_EXPR:
7028 case GE_EXPR:
7029 if (above)
7030 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7031 else if (below)
7032 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7033
7034 default:
7035 break;
7036 }
7037
7038 return NULL_TREE;
7039 }
7040
7041 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7042 ARG0 just the signedness is changed. */
7043
7044 static tree
7045 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7046 tree arg0, tree arg1)
7047 {
7048 tree arg0_inner;
7049 tree inner_type, outer_type;
7050
7051 if (!CONVERT_EXPR_P (arg0))
7052 return NULL_TREE;
7053
7054 outer_type = TREE_TYPE (arg0);
7055 arg0_inner = TREE_OPERAND (arg0, 0);
7056 inner_type = TREE_TYPE (arg0_inner);
7057
7058 /* Disable this optimization if we're casting a function pointer
7059 type on targets that require function pointer canonicalization. */
7060 if (targetm.have_canonicalize_funcptr_for_compare ()
7061 && TREE_CODE (inner_type) == POINTER_TYPE
7062 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7063 return NULL_TREE;
7064
7065 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7066 return NULL_TREE;
7067
7068 if (TREE_CODE (arg1) != INTEGER_CST
7069 && !(CONVERT_EXPR_P (arg1)
7070 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7071 return NULL_TREE;
7072
7073 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7074 && code != NE_EXPR
7075 && code != EQ_EXPR)
7076 return NULL_TREE;
7077
7078 if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7079 return NULL_TREE;
7080
7081 if (TREE_CODE (arg1) == INTEGER_CST)
7082 arg1 = force_fit_type (inner_type, wi::to_widest (arg1), 0,
7083 TREE_OVERFLOW (arg1));
7084 else
7085 arg1 = fold_convert_loc (loc, inner_type, arg1);
7086
7087 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7088 }
7089
7090
7091 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7092 means A >= Y && A != MAX, but in this case we know that
7093 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7094
7095 static tree
7096 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7097 {
7098 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7099
7100 if (TREE_CODE (bound) == LT_EXPR)
7101 a = TREE_OPERAND (bound, 0);
7102 else if (TREE_CODE (bound) == GT_EXPR)
7103 a = TREE_OPERAND (bound, 1);
7104 else
7105 return NULL_TREE;
7106
7107 typea = TREE_TYPE (a);
7108 if (!INTEGRAL_TYPE_P (typea)
7109 && !POINTER_TYPE_P (typea))
7110 return NULL_TREE;
7111
7112 if (TREE_CODE (ineq) == LT_EXPR)
7113 {
7114 a1 = TREE_OPERAND (ineq, 1);
7115 y = TREE_OPERAND (ineq, 0);
7116 }
7117 else if (TREE_CODE (ineq) == GT_EXPR)
7118 {
7119 a1 = TREE_OPERAND (ineq, 0);
7120 y = TREE_OPERAND (ineq, 1);
7121 }
7122 else
7123 return NULL_TREE;
7124
7125 if (TREE_TYPE (a1) != typea)
7126 return NULL_TREE;
7127
7128 if (POINTER_TYPE_P (typea))
7129 {
7130 /* Convert the pointer types into integer before taking the difference. */
7131 tree ta = fold_convert_loc (loc, ssizetype, a);
7132 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7133 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7134 }
7135 else
7136 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7137
7138 if (!diff || !integer_onep (diff))
7139 return NULL_TREE;
7140
7141 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7142 }
7143
7144 /* Fold a sum or difference of at least one multiplication.
7145 Returns the folded tree or NULL if no simplification could be made. */
7146
7147 static tree
7148 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7149 tree arg0, tree arg1)
7150 {
7151 tree arg00, arg01, arg10, arg11;
7152 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7153
7154 /* (A * C) +- (B * C) -> (A+-B) * C.
7155 (A * C) +- A -> A * (C+-1).
7156 We are most concerned about the case where C is a constant,
7157 but other combinations show up during loop reduction. Since
7158 it is not difficult, try all four possibilities. */
7159
7160 if (TREE_CODE (arg0) == MULT_EXPR)
7161 {
7162 arg00 = TREE_OPERAND (arg0, 0);
7163 arg01 = TREE_OPERAND (arg0, 1);
7164 }
7165 else if (TREE_CODE (arg0) == INTEGER_CST)
7166 {
7167 arg00 = build_one_cst (type);
7168 arg01 = arg0;
7169 }
7170 else
7171 {
7172 /* We cannot generate constant 1 for fract. */
7173 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7174 return NULL_TREE;
7175 arg00 = arg0;
7176 arg01 = build_one_cst (type);
7177 }
7178 if (TREE_CODE (arg1) == MULT_EXPR)
7179 {
7180 arg10 = TREE_OPERAND (arg1, 0);
7181 arg11 = TREE_OPERAND (arg1, 1);
7182 }
7183 else if (TREE_CODE (arg1) == INTEGER_CST)
7184 {
7185 arg10 = build_one_cst (type);
7186 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7187 the purpose of this canonicalization. */
7188 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
7189 && negate_expr_p (arg1)
7190 && code == PLUS_EXPR)
7191 {
7192 arg11 = negate_expr (arg1);
7193 code = MINUS_EXPR;
7194 }
7195 else
7196 arg11 = arg1;
7197 }
7198 else
7199 {
7200 /* We cannot generate constant 1 for fract. */
7201 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7202 return NULL_TREE;
7203 arg10 = arg1;
7204 arg11 = build_one_cst (type);
7205 }
7206 same = NULL_TREE;
7207
7208 if (operand_equal_p (arg01, arg11, 0))
7209 same = arg01, alt0 = arg00, alt1 = arg10;
7210 else if (operand_equal_p (arg00, arg10, 0))
7211 same = arg00, alt0 = arg01, alt1 = arg11;
7212 else if (operand_equal_p (arg00, arg11, 0))
7213 same = arg00, alt0 = arg01, alt1 = arg10;
7214 else if (operand_equal_p (arg01, arg10, 0))
7215 same = arg01, alt0 = arg00, alt1 = arg11;
7216
7217 /* No identical multiplicands; see if we can find a common
7218 power-of-two factor in non-power-of-two multiplies. This
7219 can help in multi-dimensional array access. */
7220 else if (tree_fits_shwi_p (arg01)
7221 && tree_fits_shwi_p (arg11))
7222 {
7223 HOST_WIDE_INT int01, int11, tmp;
7224 bool swap = false;
7225 tree maybe_same;
7226 int01 = tree_to_shwi (arg01);
7227 int11 = tree_to_shwi (arg11);
7228
7229 /* Move min of absolute values to int11. */
7230 if (absu_hwi (int01) < absu_hwi (int11))
7231 {
7232 tmp = int01, int01 = int11, int11 = tmp;
7233 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7234 maybe_same = arg01;
7235 swap = true;
7236 }
7237 else
7238 maybe_same = arg11;
7239
7240 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7241 /* The remainder should not be a constant, otherwise we
7242 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7243 increased the number of multiplications necessary. */
7244 && TREE_CODE (arg10) != INTEGER_CST)
7245 {
7246 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7247 build_int_cst (TREE_TYPE (arg00),
7248 int01 / int11));
7249 alt1 = arg10;
7250 same = maybe_same;
7251 if (swap)
7252 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7253 }
7254 }
7255
7256 if (same)
7257 return fold_build2_loc (loc, MULT_EXPR, type,
7258 fold_build2_loc (loc, code, type,
7259 fold_convert_loc (loc, type, alt0),
7260 fold_convert_loc (loc, type, alt1)),
7261 fold_convert_loc (loc, type, same));
7262
7263 return NULL_TREE;
7264 }
7265
7266 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7267 specified by EXPR into the buffer PTR of length LEN bytes.
7268 Return the number of bytes placed in the buffer, or zero
7269 upon failure. */
7270
7271 static int
7272 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7273 {
7274 tree type = TREE_TYPE (expr);
7275 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7276 int byte, offset, word, words;
7277 unsigned char value;
7278
7279 if ((off == -1 && total_bytes > len)
7280 || off >= total_bytes)
7281 return 0;
7282 if (off == -1)
7283 off = 0;
7284 words = total_bytes / UNITS_PER_WORD;
7285
7286 for (byte = 0; byte < total_bytes; byte++)
7287 {
7288 int bitpos = byte * BITS_PER_UNIT;
7289 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7290 number of bytes. */
7291 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7292
7293 if (total_bytes > UNITS_PER_WORD)
7294 {
7295 word = byte / UNITS_PER_WORD;
7296 if (WORDS_BIG_ENDIAN)
7297 word = (words - 1) - word;
7298 offset = word * UNITS_PER_WORD;
7299 if (BYTES_BIG_ENDIAN)
7300 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7301 else
7302 offset += byte % UNITS_PER_WORD;
7303 }
7304 else
7305 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7306 if (offset >= off
7307 && offset - off < len)
7308 ptr[offset - off] = value;
7309 }
7310 return MIN (len, total_bytes - off);
7311 }
7312
7313
7314 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7315 specified by EXPR into the buffer PTR of length LEN bytes.
7316 Return the number of bytes placed in the buffer, or zero
7317 upon failure. */
7318
7319 static int
7320 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7321 {
7322 tree type = TREE_TYPE (expr);
7323 machine_mode mode = TYPE_MODE (type);
7324 int total_bytes = GET_MODE_SIZE (mode);
7325 FIXED_VALUE_TYPE value;
7326 tree i_value, i_type;
7327
7328 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7329 return 0;
7330
7331 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7332
7333 if (NULL_TREE == i_type
7334 || TYPE_PRECISION (i_type) != total_bytes)
7335 return 0;
7336
7337 value = TREE_FIXED_CST (expr);
7338 i_value = double_int_to_tree (i_type, value.data);
7339
7340 return native_encode_int (i_value, ptr, len, off);
7341 }
7342
7343
7344 /* Subroutine of native_encode_expr. Encode the REAL_CST
7345 specified by EXPR into the buffer PTR of length LEN bytes.
7346 Return the number of bytes placed in the buffer, or zero
7347 upon failure. */
7348
7349 static int
7350 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7351 {
7352 tree type = TREE_TYPE (expr);
7353 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7354 int byte, offset, word, words, bitpos;
7355 unsigned char value;
7356
7357 /* There are always 32 bits in each long, no matter the size of
7358 the hosts long. We handle floating point representations with
7359 up to 192 bits. */
7360 long tmp[6];
7361
7362 if ((off == -1 && total_bytes > len)
7363 || off >= total_bytes)
7364 return 0;
7365 if (off == -1)
7366 off = 0;
7367 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7368
7369 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7370
7371 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7372 bitpos += BITS_PER_UNIT)
7373 {
7374 byte = (bitpos / BITS_PER_UNIT) & 3;
7375 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7376
7377 if (UNITS_PER_WORD < 4)
7378 {
7379 word = byte / UNITS_PER_WORD;
7380 if (WORDS_BIG_ENDIAN)
7381 word = (words - 1) - word;
7382 offset = word * UNITS_PER_WORD;
7383 if (BYTES_BIG_ENDIAN)
7384 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7385 else
7386 offset += byte % UNITS_PER_WORD;
7387 }
7388 else
7389 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7390 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7391 if (offset >= off
7392 && offset - off < len)
7393 ptr[offset - off] = value;
7394 }
7395 return MIN (len, total_bytes - off);
7396 }
7397
7398 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7399 specified by EXPR into the buffer PTR of length LEN bytes.
7400 Return the number of bytes placed in the buffer, or zero
7401 upon failure. */
7402
7403 static int
7404 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7405 {
7406 int rsize, isize;
7407 tree part;
7408
7409 part = TREE_REALPART (expr);
7410 rsize = native_encode_expr (part, ptr, len, off);
7411 if (off == -1
7412 && rsize == 0)
7413 return 0;
7414 part = TREE_IMAGPART (expr);
7415 if (off != -1)
7416 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7417 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7418 if (off == -1
7419 && isize != rsize)
7420 return 0;
7421 return rsize + isize;
7422 }
7423
7424
7425 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7426 specified by EXPR into the buffer PTR of length LEN bytes.
7427 Return the number of bytes placed in the buffer, or zero
7428 upon failure. */
7429
7430 static int
7431 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7432 {
7433 unsigned i, count;
7434 int size, offset;
7435 tree itype, elem;
7436
7437 offset = 0;
7438 count = VECTOR_CST_NELTS (expr);
7439 itype = TREE_TYPE (TREE_TYPE (expr));
7440 size = GET_MODE_SIZE (TYPE_MODE (itype));
7441 for (i = 0; i < count; i++)
7442 {
7443 if (off >= size)
7444 {
7445 off -= size;
7446 continue;
7447 }
7448 elem = VECTOR_CST_ELT (expr, i);
7449 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7450 if ((off == -1 && res != size)
7451 || res == 0)
7452 return 0;
7453 offset += res;
7454 if (offset >= len)
7455 return offset;
7456 if (off != -1)
7457 off = 0;
7458 }
7459 return offset;
7460 }
7461
7462
7463 /* Subroutine of native_encode_expr. Encode the STRING_CST
7464 specified by EXPR into the buffer PTR of length LEN bytes.
7465 Return the number of bytes placed in the buffer, or zero
7466 upon failure. */
7467
7468 static int
7469 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7470 {
7471 tree type = TREE_TYPE (expr);
7472 HOST_WIDE_INT total_bytes;
7473
7474 if (TREE_CODE (type) != ARRAY_TYPE
7475 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7476 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7477 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7478 return 0;
7479 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7480 if ((off == -1 && total_bytes > len)
7481 || off >= total_bytes)
7482 return 0;
7483 if (off == -1)
7484 off = 0;
7485 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7486 {
7487 int written = 0;
7488 if (off < TREE_STRING_LENGTH (expr))
7489 {
7490 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7491 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7492 }
7493 memset (ptr + written, 0,
7494 MIN (total_bytes - written, len - written));
7495 }
7496 else
7497 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7498 return MIN (total_bytes - off, len);
7499 }
7500
7501
7502 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7503 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7504 buffer PTR of length LEN bytes. If OFF is not -1 then start
7505 the encoding at byte offset OFF and encode at most LEN bytes.
7506 Return the number of bytes placed in the buffer, or zero upon failure. */
7507
7508 int
7509 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7510 {
7511 switch (TREE_CODE (expr))
7512 {
7513 case INTEGER_CST:
7514 return native_encode_int (expr, ptr, len, off);
7515
7516 case REAL_CST:
7517 return native_encode_real (expr, ptr, len, off);
7518
7519 case FIXED_CST:
7520 return native_encode_fixed (expr, ptr, len, off);
7521
7522 case COMPLEX_CST:
7523 return native_encode_complex (expr, ptr, len, off);
7524
7525 case VECTOR_CST:
7526 return native_encode_vector (expr, ptr, len, off);
7527
7528 case STRING_CST:
7529 return native_encode_string (expr, ptr, len, off);
7530
7531 default:
7532 return 0;
7533 }
7534 }
7535
7536
7537 /* Subroutine of native_interpret_expr. Interpret the contents of
7538 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7539 If the buffer cannot be interpreted, return NULL_TREE. */
7540
7541 static tree
7542 native_interpret_int (tree type, const unsigned char *ptr, int len)
7543 {
7544 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7545
7546 if (total_bytes > len
7547 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7548 return NULL_TREE;
7549
7550 wide_int result = wi::from_buffer (ptr, total_bytes);
7551
7552 return wide_int_to_tree (type, result);
7553 }
7554
7555
7556 /* Subroutine of native_interpret_expr. Interpret the contents of
7557 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7558 If the buffer cannot be interpreted, return NULL_TREE. */
7559
7560 static tree
7561 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7562 {
7563 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7564 double_int result;
7565 FIXED_VALUE_TYPE fixed_value;
7566
7567 if (total_bytes > len
7568 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7569 return NULL_TREE;
7570
7571 result = double_int::from_buffer (ptr, total_bytes);
7572 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7573
7574 return build_fixed (type, fixed_value);
7575 }
7576
7577
7578 /* Subroutine of native_interpret_expr. Interpret the contents of
7579 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7580 If the buffer cannot be interpreted, return NULL_TREE. */
7581
7582 static tree
7583 native_interpret_real (tree type, const unsigned char *ptr, int len)
7584 {
7585 machine_mode mode = TYPE_MODE (type);
7586 int total_bytes = GET_MODE_SIZE (mode);
7587 int byte, offset, word, words, bitpos;
7588 unsigned char value;
7589 /* There are always 32 bits in each long, no matter the size of
7590 the hosts long. We handle floating point representations with
7591 up to 192 bits. */
7592 REAL_VALUE_TYPE r;
7593 long tmp[6];
7594
7595 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7596 if (total_bytes > len || total_bytes > 24)
7597 return NULL_TREE;
7598 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7599
7600 memset (tmp, 0, sizeof (tmp));
7601 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7602 bitpos += BITS_PER_UNIT)
7603 {
7604 byte = (bitpos / BITS_PER_UNIT) & 3;
7605 if (UNITS_PER_WORD < 4)
7606 {
7607 word = byte / UNITS_PER_WORD;
7608 if (WORDS_BIG_ENDIAN)
7609 word = (words - 1) - word;
7610 offset = word * UNITS_PER_WORD;
7611 if (BYTES_BIG_ENDIAN)
7612 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7613 else
7614 offset += byte % UNITS_PER_WORD;
7615 }
7616 else
7617 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7618 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7619
7620 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7621 }
7622
7623 real_from_target (&r, tmp, mode);
7624 return build_real (type, r);
7625 }
7626
7627
7628 /* Subroutine of native_interpret_expr. Interpret the contents of
7629 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7630 If the buffer cannot be interpreted, return NULL_TREE. */
7631
7632 static tree
7633 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7634 {
7635 tree etype, rpart, ipart;
7636 int size;
7637
7638 etype = TREE_TYPE (type);
7639 size = GET_MODE_SIZE (TYPE_MODE (etype));
7640 if (size * 2 > len)
7641 return NULL_TREE;
7642 rpart = native_interpret_expr (etype, ptr, size);
7643 if (!rpart)
7644 return NULL_TREE;
7645 ipart = native_interpret_expr (etype, ptr+size, size);
7646 if (!ipart)
7647 return NULL_TREE;
7648 return build_complex (type, rpart, ipart);
7649 }
7650
7651
7652 /* Subroutine of native_interpret_expr. Interpret the contents of
7653 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7654 If the buffer cannot be interpreted, return NULL_TREE. */
7655
7656 static tree
7657 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7658 {
7659 tree etype, elem;
7660 int i, size, count;
7661 tree *elements;
7662
7663 etype = TREE_TYPE (type);
7664 size = GET_MODE_SIZE (TYPE_MODE (etype));
7665 count = TYPE_VECTOR_SUBPARTS (type);
7666 if (size * count > len)
7667 return NULL_TREE;
7668
7669 elements = XALLOCAVEC (tree, count);
7670 for (i = count - 1; i >= 0; i--)
7671 {
7672 elem = native_interpret_expr (etype, ptr+(i*size), size);
7673 if (!elem)
7674 return NULL_TREE;
7675 elements[i] = elem;
7676 }
7677 return build_vector (type, elements);
7678 }
7679
7680
7681 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7682 the buffer PTR of length LEN as a constant of type TYPE. For
7683 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7684 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7685 return NULL_TREE. */
7686
7687 tree
7688 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7689 {
7690 switch (TREE_CODE (type))
7691 {
7692 case INTEGER_TYPE:
7693 case ENUMERAL_TYPE:
7694 case BOOLEAN_TYPE:
7695 case POINTER_TYPE:
7696 case REFERENCE_TYPE:
7697 return native_interpret_int (type, ptr, len);
7698
7699 case REAL_TYPE:
7700 return native_interpret_real (type, ptr, len);
7701
7702 case FIXED_POINT_TYPE:
7703 return native_interpret_fixed (type, ptr, len);
7704
7705 case COMPLEX_TYPE:
7706 return native_interpret_complex (type, ptr, len);
7707
7708 case VECTOR_TYPE:
7709 return native_interpret_vector (type, ptr, len);
7710
7711 default:
7712 return NULL_TREE;
7713 }
7714 }
7715
7716 /* Returns true if we can interpret the contents of a native encoding
7717 as TYPE. */
7718
7719 static bool
7720 can_native_interpret_type_p (tree type)
7721 {
7722 switch (TREE_CODE (type))
7723 {
7724 case INTEGER_TYPE:
7725 case ENUMERAL_TYPE:
7726 case BOOLEAN_TYPE:
7727 case POINTER_TYPE:
7728 case REFERENCE_TYPE:
7729 case FIXED_POINT_TYPE:
7730 case REAL_TYPE:
7731 case COMPLEX_TYPE:
7732 case VECTOR_TYPE:
7733 return true;
7734 default:
7735 return false;
7736 }
7737 }
7738
7739 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7740 TYPE at compile-time. If we're unable to perform the conversion
7741 return NULL_TREE. */
7742
7743 static tree
7744 fold_view_convert_expr (tree type, tree expr)
7745 {
7746 /* We support up to 512-bit values (for V8DFmode). */
7747 unsigned char buffer[64];
7748 int len;
7749
7750 /* Check that the host and target are sane. */
7751 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7752 return NULL_TREE;
7753
7754 len = native_encode_expr (expr, buffer, sizeof (buffer));
7755 if (len == 0)
7756 return NULL_TREE;
7757
7758 return native_interpret_expr (type, buffer, len);
7759 }
7760
7761 /* Build an expression for the address of T. Folds away INDIRECT_REF
7762 to avoid confusing the gimplify process. */
7763
7764 tree
7765 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7766 {
7767 /* The size of the object is not relevant when talking about its address. */
7768 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7769 t = TREE_OPERAND (t, 0);
7770
7771 if (TREE_CODE (t) == INDIRECT_REF)
7772 {
7773 t = TREE_OPERAND (t, 0);
7774
7775 if (TREE_TYPE (t) != ptrtype)
7776 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7777 }
7778 else if (TREE_CODE (t) == MEM_REF
7779 && integer_zerop (TREE_OPERAND (t, 1)))
7780 return TREE_OPERAND (t, 0);
7781 else if (TREE_CODE (t) == MEM_REF
7782 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7783 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7784 TREE_OPERAND (t, 0),
7785 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7786 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7787 {
7788 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7789
7790 if (TREE_TYPE (t) != ptrtype)
7791 t = fold_convert_loc (loc, ptrtype, t);
7792 }
7793 else
7794 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7795
7796 return t;
7797 }
7798
7799 /* Build an expression for the address of T. */
7800
7801 tree
7802 build_fold_addr_expr_loc (location_t loc, tree t)
7803 {
7804 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7805
7806 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7807 }
7808
7809 /* Fold a unary expression of code CODE and type TYPE with operand
7810 OP0. Return the folded expression if folding is successful.
7811 Otherwise, return NULL_TREE. */
7812
7813 tree
7814 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7815 {
7816 tree tem;
7817 tree arg0;
7818 enum tree_code_class kind = TREE_CODE_CLASS (code);
7819
7820 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7821 && TREE_CODE_LENGTH (code) == 1);
7822
7823 arg0 = op0;
7824 if (arg0)
7825 {
7826 if (CONVERT_EXPR_CODE_P (code)
7827 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7828 {
7829 /* Don't use STRIP_NOPS, because signedness of argument type
7830 matters. */
7831 STRIP_SIGN_NOPS (arg0);
7832 }
7833 else
7834 {
7835 /* Strip any conversions that don't change the mode. This
7836 is safe for every expression, except for a comparison
7837 expression because its signedness is derived from its
7838 operands.
7839
7840 Note that this is done as an internal manipulation within
7841 the constant folder, in order to find the simplest
7842 representation of the arguments so that their form can be
7843 studied. In any cases, the appropriate type conversions
7844 should be put back in the tree that will get out of the
7845 constant folder. */
7846 STRIP_NOPS (arg0);
7847 }
7848
7849 if (CONSTANT_CLASS_P (arg0))
7850 {
7851 tree tem = const_unop (code, type, arg0);
7852 if (tem)
7853 {
7854 if (TREE_TYPE (tem) != type)
7855 tem = fold_convert_loc (loc, type, tem);
7856 return tem;
7857 }
7858 }
7859 }
7860
7861 tem = generic_simplify (loc, code, type, op0);
7862 if (tem)
7863 return tem;
7864
7865 if (TREE_CODE_CLASS (code) == tcc_unary)
7866 {
7867 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7868 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7869 fold_build1_loc (loc, code, type,
7870 fold_convert_loc (loc, TREE_TYPE (op0),
7871 TREE_OPERAND (arg0, 1))));
7872 else if (TREE_CODE (arg0) == COND_EXPR)
7873 {
7874 tree arg01 = TREE_OPERAND (arg0, 1);
7875 tree arg02 = TREE_OPERAND (arg0, 2);
7876 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7877 arg01 = fold_build1_loc (loc, code, type,
7878 fold_convert_loc (loc,
7879 TREE_TYPE (op0), arg01));
7880 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7881 arg02 = fold_build1_loc (loc, code, type,
7882 fold_convert_loc (loc,
7883 TREE_TYPE (op0), arg02));
7884 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7885 arg01, arg02);
7886
7887 /* If this was a conversion, and all we did was to move into
7888 inside the COND_EXPR, bring it back out. But leave it if
7889 it is a conversion from integer to integer and the
7890 result precision is no wider than a word since such a
7891 conversion is cheap and may be optimized away by combine,
7892 while it couldn't if it were outside the COND_EXPR. Then return
7893 so we don't get into an infinite recursion loop taking the
7894 conversion out and then back in. */
7895
7896 if ((CONVERT_EXPR_CODE_P (code)
7897 || code == NON_LVALUE_EXPR)
7898 && TREE_CODE (tem) == COND_EXPR
7899 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7900 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7901 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7902 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7903 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7904 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7905 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7906 && (INTEGRAL_TYPE_P
7907 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7908 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7909 || flag_syntax_only))
7910 tem = build1_loc (loc, code, type,
7911 build3 (COND_EXPR,
7912 TREE_TYPE (TREE_OPERAND
7913 (TREE_OPERAND (tem, 1), 0)),
7914 TREE_OPERAND (tem, 0),
7915 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7916 TREE_OPERAND (TREE_OPERAND (tem, 2),
7917 0)));
7918 return tem;
7919 }
7920 }
7921
7922 switch (code)
7923 {
7924 case NON_LVALUE_EXPR:
7925 if (!maybe_lvalue_p (op0))
7926 return fold_convert_loc (loc, type, op0);
7927 return NULL_TREE;
7928
7929 CASE_CONVERT:
7930 case FLOAT_EXPR:
7931 case FIX_TRUNC_EXPR:
7932 if (COMPARISON_CLASS_P (op0))
7933 {
7934 /* If we have (type) (a CMP b) and type is an integral type, return
7935 new expression involving the new type. Canonicalize
7936 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7937 non-integral type.
7938 Do not fold the result as that would not simplify further, also
7939 folding again results in recursions. */
7940 if (TREE_CODE (type) == BOOLEAN_TYPE)
7941 return build2_loc (loc, TREE_CODE (op0), type,
7942 TREE_OPERAND (op0, 0),
7943 TREE_OPERAND (op0, 1));
7944 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7945 && TREE_CODE (type) != VECTOR_TYPE)
7946 return build3_loc (loc, COND_EXPR, type, op0,
7947 constant_boolean_node (true, type),
7948 constant_boolean_node (false, type));
7949 }
7950
7951 /* Handle (T *)&A.B.C for A being of type T and B and C
7952 living at offset zero. This occurs frequently in
7953 C++ upcasting and then accessing the base. */
7954 if (TREE_CODE (op0) == ADDR_EXPR
7955 && POINTER_TYPE_P (type)
7956 && handled_component_p (TREE_OPERAND (op0, 0)))
7957 {
7958 HOST_WIDE_INT bitsize, bitpos;
7959 tree offset;
7960 machine_mode mode;
7961 int unsignedp, volatilep;
7962 tree base = TREE_OPERAND (op0, 0);
7963 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7964 &mode, &unsignedp, &volatilep, false);
7965 /* If the reference was to a (constant) zero offset, we can use
7966 the address of the base if it has the same base type
7967 as the result type and the pointer type is unqualified. */
7968 if (! offset && bitpos == 0
7969 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7970 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7971 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7972 return fold_convert_loc (loc, type,
7973 build_fold_addr_expr_loc (loc, base));
7974 }
7975
7976 if (TREE_CODE (op0) == MODIFY_EXPR
7977 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7978 /* Detect assigning a bitfield. */
7979 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7980 && DECL_BIT_FIELD
7981 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7982 {
7983 /* Don't leave an assignment inside a conversion
7984 unless assigning a bitfield. */
7985 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7986 /* First do the assignment, then return converted constant. */
7987 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7988 TREE_NO_WARNING (tem) = 1;
7989 TREE_USED (tem) = 1;
7990 return tem;
7991 }
7992
7993 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7994 constants (if x has signed type, the sign bit cannot be set
7995 in c). This folds extension into the BIT_AND_EXPR.
7996 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7997 very likely don't have maximal range for their precision and this
7998 transformation effectively doesn't preserve non-maximal ranges. */
7999 if (TREE_CODE (type) == INTEGER_TYPE
8000 && TREE_CODE (op0) == BIT_AND_EXPR
8001 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8002 {
8003 tree and_expr = op0;
8004 tree and0 = TREE_OPERAND (and_expr, 0);
8005 tree and1 = TREE_OPERAND (and_expr, 1);
8006 int change = 0;
8007
8008 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8009 || (TYPE_PRECISION (type)
8010 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8011 change = 1;
8012 else if (TYPE_PRECISION (TREE_TYPE (and1))
8013 <= HOST_BITS_PER_WIDE_INT
8014 && tree_fits_uhwi_p (and1))
8015 {
8016 unsigned HOST_WIDE_INT cst;
8017
8018 cst = tree_to_uhwi (and1);
8019 cst &= HOST_WIDE_INT_M1U
8020 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8021 change = (cst == 0);
8022 #ifdef LOAD_EXTEND_OP
8023 if (change
8024 && !flag_syntax_only
8025 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8026 == ZERO_EXTEND))
8027 {
8028 tree uns = unsigned_type_for (TREE_TYPE (and0));
8029 and0 = fold_convert_loc (loc, uns, and0);
8030 and1 = fold_convert_loc (loc, uns, and1);
8031 }
8032 #endif
8033 }
8034 if (change)
8035 {
8036 tem = force_fit_type (type, wi::to_widest (and1), 0,
8037 TREE_OVERFLOW (and1));
8038 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8039 fold_convert_loc (loc, type, and0), tem);
8040 }
8041 }
8042
8043 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8044 when one of the new casts will fold away. Conservatively we assume
8045 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8046 if (POINTER_TYPE_P (type)
8047 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8048 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
8049 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8050 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8051 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8052 {
8053 tree arg00 = TREE_OPERAND (arg0, 0);
8054 tree arg01 = TREE_OPERAND (arg0, 1);
8055
8056 return fold_build_pointer_plus_loc
8057 (loc, fold_convert_loc (loc, type, arg00), arg01);
8058 }
8059
8060 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8061 of the same precision, and X is an integer type not narrower than
8062 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8063 if (INTEGRAL_TYPE_P (type)
8064 && TREE_CODE (op0) == BIT_NOT_EXPR
8065 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8066 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8067 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8068 {
8069 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8070 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8071 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8072 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8073 fold_convert_loc (loc, type, tem));
8074 }
8075
8076 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8077 type of X and Y (integer types only). */
8078 if (INTEGRAL_TYPE_P (type)
8079 && TREE_CODE (op0) == MULT_EXPR
8080 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8081 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8082 {
8083 /* Be careful not to introduce new overflows. */
8084 tree mult_type;
8085 if (TYPE_OVERFLOW_WRAPS (type))
8086 mult_type = type;
8087 else
8088 mult_type = unsigned_type_for (type);
8089
8090 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8091 {
8092 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8093 fold_convert_loc (loc, mult_type,
8094 TREE_OPERAND (op0, 0)),
8095 fold_convert_loc (loc, mult_type,
8096 TREE_OPERAND (op0, 1)));
8097 return fold_convert_loc (loc, type, tem);
8098 }
8099 }
8100
8101 return NULL_TREE;
8102
8103 case VIEW_CONVERT_EXPR:
8104 if (TREE_CODE (op0) == MEM_REF)
8105 return fold_build2_loc (loc, MEM_REF, type,
8106 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8107
8108 return NULL_TREE;
8109
8110 case NEGATE_EXPR:
8111 tem = fold_negate_expr (loc, arg0);
8112 if (tem)
8113 return fold_convert_loc (loc, type, tem);
8114 return NULL_TREE;
8115
8116 case ABS_EXPR:
8117 /* Convert fabs((double)float) into (double)fabsf(float). */
8118 if (TREE_CODE (arg0) == NOP_EXPR
8119 && TREE_CODE (type) == REAL_TYPE)
8120 {
8121 tree targ0 = strip_float_extensions (arg0);
8122 if (targ0 != arg0)
8123 return fold_convert_loc (loc, type,
8124 fold_build1_loc (loc, ABS_EXPR,
8125 TREE_TYPE (targ0),
8126 targ0));
8127 }
8128
8129 /* Strip sign ops from argument. */
8130 if (TREE_CODE (type) == REAL_TYPE)
8131 {
8132 tem = fold_strip_sign_ops (arg0);
8133 if (tem)
8134 return fold_build1_loc (loc, ABS_EXPR, type,
8135 fold_convert_loc (loc, type, tem));
8136 }
8137 return NULL_TREE;
8138
8139 case CONJ_EXPR:
8140 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8141 return fold_convert_loc (loc, type, arg0);
8142 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8143 {
8144 tree itype = TREE_TYPE (type);
8145 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8146 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8147 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8148 negate_expr (ipart));
8149 }
8150 if (TREE_CODE (arg0) == CONJ_EXPR)
8151 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8152 return NULL_TREE;
8153
8154 case BIT_NOT_EXPR:
8155 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8156 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8157 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8158 fold_convert_loc (loc, type,
8159 TREE_OPERAND (arg0, 0)))))
8160 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8161 fold_convert_loc (loc, type,
8162 TREE_OPERAND (arg0, 1)));
8163 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8164 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8165 fold_convert_loc (loc, type,
8166 TREE_OPERAND (arg0, 1)))))
8167 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8168 fold_convert_loc (loc, type,
8169 TREE_OPERAND (arg0, 0)), tem);
8170
8171 return NULL_TREE;
8172
8173 case TRUTH_NOT_EXPR:
8174 /* Note that the operand of this must be an int
8175 and its values must be 0 or 1.
8176 ("true" is a fixed value perhaps depending on the language,
8177 but we don't handle values other than 1 correctly yet.) */
8178 tem = fold_truth_not_expr (loc, arg0);
8179 if (!tem)
8180 return NULL_TREE;
8181 return fold_convert_loc (loc, type, tem);
8182
8183 case REALPART_EXPR:
8184 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8185 return fold_convert_loc (loc, type, arg0);
8186 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8187 {
8188 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8189 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8190 fold_build1_loc (loc, REALPART_EXPR, itype,
8191 TREE_OPERAND (arg0, 0)),
8192 fold_build1_loc (loc, REALPART_EXPR, itype,
8193 TREE_OPERAND (arg0, 1)));
8194 return fold_convert_loc (loc, type, tem);
8195 }
8196 if (TREE_CODE (arg0) == CONJ_EXPR)
8197 {
8198 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8199 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8200 TREE_OPERAND (arg0, 0));
8201 return fold_convert_loc (loc, type, tem);
8202 }
8203 if (TREE_CODE (arg0) == CALL_EXPR)
8204 {
8205 tree fn = get_callee_fndecl (arg0);
8206 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8207 switch (DECL_FUNCTION_CODE (fn))
8208 {
8209 CASE_FLT_FN (BUILT_IN_CEXPI):
8210 fn = mathfn_built_in (type, BUILT_IN_COS);
8211 if (fn)
8212 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8213 break;
8214
8215 default:
8216 break;
8217 }
8218 }
8219 return NULL_TREE;
8220
8221 case IMAGPART_EXPR:
8222 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8223 return build_zero_cst (type);
8224 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8225 {
8226 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8227 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8228 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8229 TREE_OPERAND (arg0, 0)),
8230 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8231 TREE_OPERAND (arg0, 1)));
8232 return fold_convert_loc (loc, type, tem);
8233 }
8234 if (TREE_CODE (arg0) == CONJ_EXPR)
8235 {
8236 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8237 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8238 return fold_convert_loc (loc, type, negate_expr (tem));
8239 }
8240 if (TREE_CODE (arg0) == CALL_EXPR)
8241 {
8242 tree fn = get_callee_fndecl (arg0);
8243 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8244 switch (DECL_FUNCTION_CODE (fn))
8245 {
8246 CASE_FLT_FN (BUILT_IN_CEXPI):
8247 fn = mathfn_built_in (type, BUILT_IN_SIN);
8248 if (fn)
8249 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8250 break;
8251
8252 default:
8253 break;
8254 }
8255 }
8256 return NULL_TREE;
8257
8258 case INDIRECT_REF:
8259 /* Fold *&X to X if X is an lvalue. */
8260 if (TREE_CODE (op0) == ADDR_EXPR)
8261 {
8262 tree op00 = TREE_OPERAND (op0, 0);
8263 if ((TREE_CODE (op00) == VAR_DECL
8264 || TREE_CODE (op00) == PARM_DECL
8265 || TREE_CODE (op00) == RESULT_DECL)
8266 && !TREE_READONLY (op00))
8267 return op00;
8268 }
8269 return NULL_TREE;
8270
8271 default:
8272 return NULL_TREE;
8273 } /* switch (code) */
8274 }
8275
8276
8277 /* If the operation was a conversion do _not_ mark a resulting constant
8278 with TREE_OVERFLOW if the original constant was not. These conversions
8279 have implementation defined behavior and retaining the TREE_OVERFLOW
8280 flag here would confuse later passes such as VRP. */
8281 tree
8282 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8283 tree type, tree op0)
8284 {
8285 tree res = fold_unary_loc (loc, code, type, op0);
8286 if (res
8287 && TREE_CODE (res) == INTEGER_CST
8288 && TREE_CODE (op0) == INTEGER_CST
8289 && CONVERT_EXPR_CODE_P (code))
8290 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8291
8292 return res;
8293 }
8294
8295 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8296 operands OP0 and OP1. LOC is the location of the resulting expression.
8297 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8298 Return the folded expression if folding is successful. Otherwise,
8299 return NULL_TREE. */
8300 static tree
8301 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8302 tree arg0, tree arg1, tree op0, tree op1)
8303 {
8304 tree tem;
8305
8306 /* We only do these simplifications if we are optimizing. */
8307 if (!optimize)
8308 return NULL_TREE;
8309
8310 /* Check for things like (A || B) && (A || C). We can convert this
8311 to A || (B && C). Note that either operator can be any of the four
8312 truth and/or operations and the transformation will still be
8313 valid. Also note that we only care about order for the
8314 ANDIF and ORIF operators. If B contains side effects, this
8315 might change the truth-value of A. */
8316 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8317 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8318 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8319 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8320 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8321 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8322 {
8323 tree a00 = TREE_OPERAND (arg0, 0);
8324 tree a01 = TREE_OPERAND (arg0, 1);
8325 tree a10 = TREE_OPERAND (arg1, 0);
8326 tree a11 = TREE_OPERAND (arg1, 1);
8327 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8328 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8329 && (code == TRUTH_AND_EXPR
8330 || code == TRUTH_OR_EXPR));
8331
8332 if (operand_equal_p (a00, a10, 0))
8333 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8334 fold_build2_loc (loc, code, type, a01, a11));
8335 else if (commutative && operand_equal_p (a00, a11, 0))
8336 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8337 fold_build2_loc (loc, code, type, a01, a10));
8338 else if (commutative && operand_equal_p (a01, a10, 0))
8339 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8340 fold_build2_loc (loc, code, type, a00, a11));
8341
8342 /* This case if tricky because we must either have commutative
8343 operators or else A10 must not have side-effects. */
8344
8345 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8346 && operand_equal_p (a01, a11, 0))
8347 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8348 fold_build2_loc (loc, code, type, a00, a10),
8349 a01);
8350 }
8351
8352 /* See if we can build a range comparison. */
8353 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8354 return tem;
8355
8356 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8357 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8358 {
8359 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8360 if (tem)
8361 return fold_build2_loc (loc, code, type, tem, arg1);
8362 }
8363
8364 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8365 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8366 {
8367 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8368 if (tem)
8369 return fold_build2_loc (loc, code, type, arg0, tem);
8370 }
8371
8372 /* Check for the possibility of merging component references. If our
8373 lhs is another similar operation, try to merge its rhs with our
8374 rhs. Then try to merge our lhs and rhs. */
8375 if (TREE_CODE (arg0) == code
8376 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8377 TREE_OPERAND (arg0, 1), arg1)))
8378 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8379
8380 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8381 return tem;
8382
8383 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8384 && (code == TRUTH_AND_EXPR
8385 || code == TRUTH_ANDIF_EXPR
8386 || code == TRUTH_OR_EXPR
8387 || code == TRUTH_ORIF_EXPR))
8388 {
8389 enum tree_code ncode, icode;
8390
8391 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8392 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8393 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8394
8395 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8396 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8397 We don't want to pack more than two leafs to a non-IF AND/OR
8398 expression.
8399 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8400 equal to IF-CODE, then we don't want to add right-hand operand.
8401 If the inner right-hand side of left-hand operand has
8402 side-effects, or isn't simple, then we can't add to it,
8403 as otherwise we might destroy if-sequence. */
8404 if (TREE_CODE (arg0) == icode
8405 && simple_operand_p_2 (arg1)
8406 /* Needed for sequence points to handle trappings, and
8407 side-effects. */
8408 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8409 {
8410 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8411 arg1);
8412 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8413 tem);
8414 }
8415 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8416 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8417 else if (TREE_CODE (arg1) == icode
8418 && simple_operand_p_2 (arg0)
8419 /* Needed for sequence points to handle trappings, and
8420 side-effects. */
8421 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8422 {
8423 tem = fold_build2_loc (loc, ncode, type,
8424 arg0, TREE_OPERAND (arg1, 0));
8425 return fold_build2_loc (loc, icode, type, tem,
8426 TREE_OPERAND (arg1, 1));
8427 }
8428 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8429 into (A OR B).
8430 For sequence point consistancy, we need to check for trapping,
8431 and side-effects. */
8432 else if (code == icode && simple_operand_p_2 (arg0)
8433 && simple_operand_p_2 (arg1))
8434 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8435 }
8436
8437 return NULL_TREE;
8438 }
8439
8440 /* Fold a binary expression of code CODE and type TYPE with operands
8441 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8442 Return the folded expression if folding is successful. Otherwise,
8443 return NULL_TREE. */
8444
8445 static tree
8446 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8447 {
8448 enum tree_code compl_code;
8449
8450 if (code == MIN_EXPR)
8451 compl_code = MAX_EXPR;
8452 else if (code == MAX_EXPR)
8453 compl_code = MIN_EXPR;
8454 else
8455 gcc_unreachable ();
8456
8457 /* MIN (MAX (a, b), b) == b. */
8458 if (TREE_CODE (op0) == compl_code
8459 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8460 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8461
8462 /* MIN (MAX (b, a), b) == b. */
8463 if (TREE_CODE (op0) == compl_code
8464 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8465 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8466 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8467
8468 /* MIN (a, MAX (a, b)) == a. */
8469 if (TREE_CODE (op1) == compl_code
8470 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8471 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8472 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8473
8474 /* MIN (a, MAX (b, a)) == a. */
8475 if (TREE_CODE (op1) == compl_code
8476 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8477 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8478 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8479
8480 return NULL_TREE;
8481 }
8482
8483 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8484 by changing CODE to reduce the magnitude of constants involved in
8485 ARG0 of the comparison.
8486 Returns a canonicalized comparison tree if a simplification was
8487 possible, otherwise returns NULL_TREE.
8488 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8489 valid if signed overflow is undefined. */
8490
8491 static tree
8492 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8493 tree arg0, tree arg1,
8494 bool *strict_overflow_p)
8495 {
8496 enum tree_code code0 = TREE_CODE (arg0);
8497 tree t, cst0 = NULL_TREE;
8498 int sgn0;
8499 bool swap = false;
8500
8501 /* Match A +- CST code arg1 and CST code arg1. We can change the
8502 first form only if overflow is undefined. */
8503 if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8504 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8505 /* In principle pointers also have undefined overflow behavior,
8506 but that causes problems elsewhere. */
8507 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8508 && (code0 == MINUS_EXPR
8509 || code0 == PLUS_EXPR)
8510 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8511 || code0 == INTEGER_CST))
8512 return NULL_TREE;
8513
8514 /* Identify the constant in arg0 and its sign. */
8515 if (code0 == INTEGER_CST)
8516 cst0 = arg0;
8517 else
8518 cst0 = TREE_OPERAND (arg0, 1);
8519 sgn0 = tree_int_cst_sgn (cst0);
8520
8521 /* Overflowed constants and zero will cause problems. */
8522 if (integer_zerop (cst0)
8523 || TREE_OVERFLOW (cst0))
8524 return NULL_TREE;
8525
8526 /* See if we can reduce the magnitude of the constant in
8527 arg0 by changing the comparison code. */
8528 if (code0 == INTEGER_CST)
8529 {
8530 /* CST <= arg1 -> CST-1 < arg1. */
8531 if (code == LE_EXPR && sgn0 == 1)
8532 code = LT_EXPR;
8533 /* -CST < arg1 -> -CST-1 <= arg1. */
8534 else if (code == LT_EXPR && sgn0 == -1)
8535 code = LE_EXPR;
8536 /* CST > arg1 -> CST-1 >= arg1. */
8537 else if (code == GT_EXPR && sgn0 == 1)
8538 code = GE_EXPR;
8539 /* -CST >= arg1 -> -CST-1 > arg1. */
8540 else if (code == GE_EXPR && sgn0 == -1)
8541 code = GT_EXPR;
8542 else
8543 return NULL_TREE;
8544 /* arg1 code' CST' might be more canonical. */
8545 swap = true;
8546 }
8547 else
8548 {
8549 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8550 if (code == LT_EXPR
8551 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8552 code = LE_EXPR;
8553 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8554 else if (code == GT_EXPR
8555 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8556 code = GE_EXPR;
8557 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8558 else if (code == LE_EXPR
8559 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8560 code = LT_EXPR;
8561 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8562 else if (code == GE_EXPR
8563 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8564 code = GT_EXPR;
8565 else
8566 return NULL_TREE;
8567 *strict_overflow_p = true;
8568 }
8569
8570 /* Now build the constant reduced in magnitude. But not if that
8571 would produce one outside of its types range. */
8572 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8573 && ((sgn0 == 1
8574 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8575 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8576 || (sgn0 == -1
8577 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8578 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8579 /* We cannot swap the comparison here as that would cause us to
8580 endlessly recurse. */
8581 return NULL_TREE;
8582
8583 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8584 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8585 if (code0 != INTEGER_CST)
8586 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8587 t = fold_convert (TREE_TYPE (arg1), t);
8588
8589 /* If swapping might yield to a more canonical form, do so. */
8590 if (swap)
8591 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8592 else
8593 return fold_build2_loc (loc, code, type, t, arg1);
8594 }
8595
8596 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8597 overflow further. Try to decrease the magnitude of constants involved
8598 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8599 and put sole constants at the second argument position.
8600 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8601
8602 static tree
8603 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8604 tree arg0, tree arg1)
8605 {
8606 tree t;
8607 bool strict_overflow_p;
8608 const char * const warnmsg = G_("assuming signed overflow does not occur "
8609 "when reducing constant in comparison");
8610
8611 /* Try canonicalization by simplifying arg0. */
8612 strict_overflow_p = false;
8613 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8614 &strict_overflow_p);
8615 if (t)
8616 {
8617 if (strict_overflow_p)
8618 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8619 return t;
8620 }
8621
8622 /* Try canonicalization by simplifying arg1 using the swapped
8623 comparison. */
8624 code = swap_tree_comparison (code);
8625 strict_overflow_p = false;
8626 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8627 &strict_overflow_p);
8628 if (t && strict_overflow_p)
8629 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8630 return t;
8631 }
8632
8633 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8634 space. This is used to avoid issuing overflow warnings for
8635 expressions like &p->x which can not wrap. */
8636
8637 static bool
8638 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8639 {
8640 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8641 return true;
8642
8643 if (bitpos < 0)
8644 return true;
8645
8646 wide_int wi_offset;
8647 int precision = TYPE_PRECISION (TREE_TYPE (base));
8648 if (offset == NULL_TREE)
8649 wi_offset = wi::zero (precision);
8650 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8651 return true;
8652 else
8653 wi_offset = offset;
8654
8655 bool overflow;
8656 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8657 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8658 if (overflow)
8659 return true;
8660
8661 if (!wi::fits_uhwi_p (total))
8662 return true;
8663
8664 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8665 if (size <= 0)
8666 return true;
8667
8668 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8669 array. */
8670 if (TREE_CODE (base) == ADDR_EXPR)
8671 {
8672 HOST_WIDE_INT base_size;
8673
8674 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8675 if (base_size > 0 && size < base_size)
8676 size = base_size;
8677 }
8678
8679 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8680 }
8681
8682 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8683 kind INTEGER_CST. This makes sure to properly sign-extend the
8684 constant. */
8685
8686 static HOST_WIDE_INT
8687 size_low_cst (const_tree t)
8688 {
8689 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8690 int prec = TYPE_PRECISION (TREE_TYPE (t));
8691 if (prec < HOST_BITS_PER_WIDE_INT)
8692 return sext_hwi (w, prec);
8693 return w;
8694 }
8695
8696 /* Subroutine of fold_binary. This routine performs all of the
8697 transformations that are common to the equality/inequality
8698 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8699 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8700 fold_binary should call fold_binary. Fold a comparison with
8701 tree code CODE and type TYPE with operands OP0 and OP1. Return
8702 the folded comparison or NULL_TREE. */
8703
8704 static tree
8705 fold_comparison (location_t loc, enum tree_code code, tree type,
8706 tree op0, tree op1)
8707 {
8708 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8709 tree arg0, arg1, tem;
8710
8711 arg0 = op0;
8712 arg1 = op1;
8713
8714 STRIP_SIGN_NOPS (arg0);
8715 STRIP_SIGN_NOPS (arg1);
8716
8717 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8718 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8719 && (equality_code
8720 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8721 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8722 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8723 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8724 && TREE_CODE (arg1) == INTEGER_CST
8725 && !TREE_OVERFLOW (arg1))
8726 {
8727 const enum tree_code
8728 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8729 tree const1 = TREE_OPERAND (arg0, 1);
8730 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8731 tree variable = TREE_OPERAND (arg0, 0);
8732 tree new_const = int_const_binop (reverse_op, const2, const1);
8733
8734 /* If the constant operation overflowed this can be
8735 simplified as a comparison against INT_MAX/INT_MIN. */
8736 if (TREE_OVERFLOW (new_const)
8737 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8738 {
8739 int const1_sgn = tree_int_cst_sgn (const1);
8740 enum tree_code code2 = code;
8741
8742 /* Get the sign of the constant on the lhs if the
8743 operation were VARIABLE + CONST1. */
8744 if (TREE_CODE (arg0) == MINUS_EXPR)
8745 const1_sgn = -const1_sgn;
8746
8747 /* The sign of the constant determines if we overflowed
8748 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8749 Canonicalize to the INT_MIN overflow by swapping the comparison
8750 if necessary. */
8751 if (const1_sgn == -1)
8752 code2 = swap_tree_comparison (code);
8753
8754 /* We now can look at the canonicalized case
8755 VARIABLE + 1 CODE2 INT_MIN
8756 and decide on the result. */
8757 switch (code2)
8758 {
8759 case EQ_EXPR:
8760 case LT_EXPR:
8761 case LE_EXPR:
8762 return
8763 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8764
8765 case NE_EXPR:
8766 case GE_EXPR:
8767 case GT_EXPR:
8768 return
8769 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8770
8771 default:
8772 gcc_unreachable ();
8773 }
8774 }
8775 else
8776 {
8777 if (!equality_code)
8778 fold_overflow_warning ("assuming signed overflow does not occur "
8779 "when changing X +- C1 cmp C2 to "
8780 "X cmp C2 -+ C1",
8781 WARN_STRICT_OVERFLOW_COMPARISON);
8782 return fold_build2_loc (loc, code, type, variable, new_const);
8783 }
8784 }
8785
8786 /* For comparisons of pointers we can decompose it to a compile time
8787 comparison of the base objects and the offsets into the object.
8788 This requires at least one operand being an ADDR_EXPR or a
8789 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8790 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8791 && (TREE_CODE (arg0) == ADDR_EXPR
8792 || TREE_CODE (arg1) == ADDR_EXPR
8793 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8794 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8795 {
8796 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8797 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8798 machine_mode mode;
8799 int volatilep, unsignedp;
8800 bool indirect_base0 = false, indirect_base1 = false;
8801
8802 /* Get base and offset for the access. Strip ADDR_EXPR for
8803 get_inner_reference, but put it back by stripping INDIRECT_REF
8804 off the base object if possible. indirect_baseN will be true
8805 if baseN is not an address but refers to the object itself. */
8806 base0 = arg0;
8807 if (TREE_CODE (arg0) == ADDR_EXPR)
8808 {
8809 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8810 &bitsize, &bitpos0, &offset0, &mode,
8811 &unsignedp, &volatilep, false);
8812 if (TREE_CODE (base0) == INDIRECT_REF)
8813 base0 = TREE_OPERAND (base0, 0);
8814 else
8815 indirect_base0 = true;
8816 }
8817 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8818 {
8819 base0 = TREE_OPERAND (arg0, 0);
8820 STRIP_SIGN_NOPS (base0);
8821 if (TREE_CODE (base0) == ADDR_EXPR)
8822 {
8823 base0 = TREE_OPERAND (base0, 0);
8824 indirect_base0 = true;
8825 }
8826 offset0 = TREE_OPERAND (arg0, 1);
8827 if (tree_fits_shwi_p (offset0))
8828 {
8829 HOST_WIDE_INT off = size_low_cst (offset0);
8830 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8831 * BITS_PER_UNIT)
8832 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8833 {
8834 bitpos0 = off * BITS_PER_UNIT;
8835 offset0 = NULL_TREE;
8836 }
8837 }
8838 }
8839
8840 base1 = arg1;
8841 if (TREE_CODE (arg1) == ADDR_EXPR)
8842 {
8843 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8844 &bitsize, &bitpos1, &offset1, &mode,
8845 &unsignedp, &volatilep, false);
8846 if (TREE_CODE (base1) == INDIRECT_REF)
8847 base1 = TREE_OPERAND (base1, 0);
8848 else
8849 indirect_base1 = true;
8850 }
8851 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8852 {
8853 base1 = TREE_OPERAND (arg1, 0);
8854 STRIP_SIGN_NOPS (base1);
8855 if (TREE_CODE (base1) == ADDR_EXPR)
8856 {
8857 base1 = TREE_OPERAND (base1, 0);
8858 indirect_base1 = true;
8859 }
8860 offset1 = TREE_OPERAND (arg1, 1);
8861 if (tree_fits_shwi_p (offset1))
8862 {
8863 HOST_WIDE_INT off = size_low_cst (offset1);
8864 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8865 * BITS_PER_UNIT)
8866 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8867 {
8868 bitpos1 = off * BITS_PER_UNIT;
8869 offset1 = NULL_TREE;
8870 }
8871 }
8872 }
8873
8874 /* A local variable can never be pointed to by
8875 the default SSA name of an incoming parameter. */
8876 if ((TREE_CODE (arg0) == ADDR_EXPR
8877 && indirect_base0
8878 && TREE_CODE (base0) == VAR_DECL
8879 && auto_var_in_fn_p (base0, current_function_decl)
8880 && !indirect_base1
8881 && TREE_CODE (base1) == SSA_NAME
8882 && SSA_NAME_IS_DEFAULT_DEF (base1)
8883 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
8884 || (TREE_CODE (arg1) == ADDR_EXPR
8885 && indirect_base1
8886 && TREE_CODE (base1) == VAR_DECL
8887 && auto_var_in_fn_p (base1, current_function_decl)
8888 && !indirect_base0
8889 && TREE_CODE (base0) == SSA_NAME
8890 && SSA_NAME_IS_DEFAULT_DEF (base0)
8891 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
8892 {
8893 if (code == NE_EXPR)
8894 return constant_boolean_node (1, type);
8895 else if (code == EQ_EXPR)
8896 return constant_boolean_node (0, type);
8897 }
8898 /* If we have equivalent bases we might be able to simplify. */
8899 else if (indirect_base0 == indirect_base1
8900 && operand_equal_p (base0, base1, 0))
8901 {
8902 /* We can fold this expression to a constant if the non-constant
8903 offset parts are equal. */
8904 if ((offset0 == offset1
8905 || (offset0 && offset1
8906 && operand_equal_p (offset0, offset1, 0)))
8907 && (code == EQ_EXPR
8908 || code == NE_EXPR
8909 || (indirect_base0 && DECL_P (base0))
8910 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8911
8912 {
8913 if (!equality_code
8914 && bitpos0 != bitpos1
8915 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8916 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8917 fold_overflow_warning (("assuming pointer wraparound does not "
8918 "occur when comparing P +- C1 with "
8919 "P +- C2"),
8920 WARN_STRICT_OVERFLOW_CONDITIONAL);
8921
8922 switch (code)
8923 {
8924 case EQ_EXPR:
8925 return constant_boolean_node (bitpos0 == bitpos1, type);
8926 case NE_EXPR:
8927 return constant_boolean_node (bitpos0 != bitpos1, type);
8928 case LT_EXPR:
8929 return constant_boolean_node (bitpos0 < bitpos1, type);
8930 case LE_EXPR:
8931 return constant_boolean_node (bitpos0 <= bitpos1, type);
8932 case GE_EXPR:
8933 return constant_boolean_node (bitpos0 >= bitpos1, type);
8934 case GT_EXPR:
8935 return constant_boolean_node (bitpos0 > bitpos1, type);
8936 default:;
8937 }
8938 }
8939 /* We can simplify the comparison to a comparison of the variable
8940 offset parts if the constant offset parts are equal.
8941 Be careful to use signed sizetype here because otherwise we
8942 mess with array offsets in the wrong way. This is possible
8943 because pointer arithmetic is restricted to retain within an
8944 object and overflow on pointer differences is undefined as of
8945 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8946 else if (bitpos0 == bitpos1
8947 && (equality_code
8948 || (indirect_base0 && DECL_P (base0))
8949 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8950 {
8951 /* By converting to signed sizetype we cover middle-end pointer
8952 arithmetic which operates on unsigned pointer types of size
8953 type size and ARRAY_REF offsets which are properly sign or
8954 zero extended from their type in case it is narrower than
8955 sizetype. */
8956 if (offset0 == NULL_TREE)
8957 offset0 = build_int_cst (ssizetype, 0);
8958 else
8959 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8960 if (offset1 == NULL_TREE)
8961 offset1 = build_int_cst (ssizetype, 0);
8962 else
8963 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8964
8965 if (!equality_code
8966 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8967 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8968 fold_overflow_warning (("assuming pointer wraparound does not "
8969 "occur when comparing P +- C1 with "
8970 "P +- C2"),
8971 WARN_STRICT_OVERFLOW_COMPARISON);
8972
8973 return fold_build2_loc (loc, code, type, offset0, offset1);
8974 }
8975 }
8976 /* For non-equal bases we can simplify if they are addresses
8977 declarations with different addresses. */
8978 else if (indirect_base0 && indirect_base1
8979 /* We know that !operand_equal_p (base0, base1, 0)
8980 because the if condition was false. But make
8981 sure two decls are not the same. */
8982 && base0 != base1
8983 && TREE_CODE (arg0) == ADDR_EXPR
8984 && TREE_CODE (arg1) == ADDR_EXPR
8985 && DECL_P (base0)
8986 && DECL_P (base1)
8987 /* Watch for aliases. */
8988 && (!decl_in_symtab_p (base0)
8989 || !decl_in_symtab_p (base1)
8990 || !symtab_node::get_create (base0)->equal_address_to
8991 (symtab_node::get_create (base1))))
8992 {
8993 if (code == EQ_EXPR)
8994 return omit_two_operands_loc (loc, type, boolean_false_node,
8995 arg0, arg1);
8996 else if (code == NE_EXPR)
8997 return omit_two_operands_loc (loc, type, boolean_true_node,
8998 arg0, arg1);
8999 }
9000 /* For equal offsets we can simplify to a comparison of the
9001 base addresses. */
9002 else if (bitpos0 == bitpos1
9003 && (indirect_base0
9004 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9005 && (indirect_base1
9006 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9007 && ((offset0 == offset1)
9008 || (offset0 && offset1
9009 && operand_equal_p (offset0, offset1, 0))))
9010 {
9011 if (indirect_base0)
9012 base0 = build_fold_addr_expr_loc (loc, base0);
9013 if (indirect_base1)
9014 base1 = build_fold_addr_expr_loc (loc, base1);
9015 return fold_build2_loc (loc, code, type, base0, base1);
9016 }
9017 }
9018
9019 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9020 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9021 the resulting offset is smaller in absolute value than the
9022 original one and has the same sign. */
9023 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9024 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9025 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9026 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9027 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9028 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9029 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9030 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9031 {
9032 tree const1 = TREE_OPERAND (arg0, 1);
9033 tree const2 = TREE_OPERAND (arg1, 1);
9034 tree variable1 = TREE_OPERAND (arg0, 0);
9035 tree variable2 = TREE_OPERAND (arg1, 0);
9036 tree cst;
9037 const char * const warnmsg = G_("assuming signed overflow does not "
9038 "occur when combining constants around "
9039 "a comparison");
9040
9041 /* Put the constant on the side where it doesn't overflow and is
9042 of lower absolute value and of same sign than before. */
9043 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9044 ? MINUS_EXPR : PLUS_EXPR,
9045 const2, const1);
9046 if (!TREE_OVERFLOW (cst)
9047 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
9048 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
9049 {
9050 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9051 return fold_build2_loc (loc, code, type,
9052 variable1,
9053 fold_build2_loc (loc, TREE_CODE (arg1),
9054 TREE_TYPE (arg1),
9055 variable2, cst));
9056 }
9057
9058 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9059 ? MINUS_EXPR : PLUS_EXPR,
9060 const1, const2);
9061 if (!TREE_OVERFLOW (cst)
9062 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
9063 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
9064 {
9065 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9066 return fold_build2_loc (loc, code, type,
9067 fold_build2_loc (loc, TREE_CODE (arg0),
9068 TREE_TYPE (arg0),
9069 variable1, cst),
9070 variable2);
9071 }
9072 }
9073
9074 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9075 if (tem)
9076 return tem;
9077
9078 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9079 {
9080 tree targ0 = strip_float_extensions (arg0);
9081 tree targ1 = strip_float_extensions (arg1);
9082 tree newtype = TREE_TYPE (targ0);
9083
9084 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9085 newtype = TREE_TYPE (targ1);
9086
9087 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9088 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9089 return fold_build2_loc (loc, code, type,
9090 fold_convert_loc (loc, newtype, targ0),
9091 fold_convert_loc (loc, newtype, targ1));
9092
9093 if (TREE_CODE (arg1) == REAL_CST)
9094 {
9095 REAL_VALUE_TYPE cst;
9096 cst = TREE_REAL_CST (arg1);
9097
9098 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9099 /* a CMP (-0) -> a CMP 0 */
9100 if (REAL_VALUE_MINUS_ZERO (cst))
9101 return fold_build2_loc (loc, code, type, arg0,
9102 build_real (TREE_TYPE (arg1), dconst0));
9103
9104 /* x != NaN is always true, other ops are always false. */
9105 if (REAL_VALUE_ISNAN (cst)
9106 && ! HONOR_SNANS (arg1))
9107 {
9108 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9109 return omit_one_operand_loc (loc, type, tem, arg0);
9110 }
9111
9112 /* Fold comparisons against infinity. */
9113 if (REAL_VALUE_ISINF (cst)
9114 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9115 {
9116 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9117 if (tem != NULL_TREE)
9118 return tem;
9119 }
9120 }
9121
9122 /* If this is a comparison of a real constant with a PLUS_EXPR
9123 or a MINUS_EXPR of a real constant, we can convert it into a
9124 comparison with a revised real constant as long as no overflow
9125 occurs when unsafe_math_optimizations are enabled. */
9126 if (flag_unsafe_math_optimizations
9127 && TREE_CODE (arg1) == REAL_CST
9128 && (TREE_CODE (arg0) == PLUS_EXPR
9129 || TREE_CODE (arg0) == MINUS_EXPR)
9130 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9131 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9132 ? MINUS_EXPR : PLUS_EXPR,
9133 arg1, TREE_OPERAND (arg0, 1)))
9134 && !TREE_OVERFLOW (tem))
9135 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9136
9137 /* Likewise, we can simplify a comparison of a real constant with
9138 a MINUS_EXPR whose first operand is also a real constant, i.e.
9139 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9140 floating-point types only if -fassociative-math is set. */
9141 if (flag_associative_math
9142 && TREE_CODE (arg1) == REAL_CST
9143 && TREE_CODE (arg0) == MINUS_EXPR
9144 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9145 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9146 arg1))
9147 && !TREE_OVERFLOW (tem))
9148 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9149 TREE_OPERAND (arg0, 1), tem);
9150
9151 /* Fold comparisons against built-in math functions. */
9152 if (TREE_CODE (arg1) == REAL_CST
9153 && flag_unsafe_math_optimizations
9154 && ! flag_errno_math)
9155 {
9156 enum built_in_function fcode = builtin_mathfn_code (arg0);
9157
9158 if (fcode != END_BUILTINS)
9159 {
9160 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9161 if (tem != NULL_TREE)
9162 return tem;
9163 }
9164 }
9165 }
9166
9167 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9168 && CONVERT_EXPR_P (arg0))
9169 {
9170 /* If we are widening one operand of an integer comparison,
9171 see if the other operand is similarly being widened. Perhaps we
9172 can do the comparison in the narrower type. */
9173 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9174 if (tem)
9175 return tem;
9176
9177 /* Or if we are changing signedness. */
9178 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9179 if (tem)
9180 return tem;
9181 }
9182
9183 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9184 constant, we can simplify it. */
9185 if (TREE_CODE (arg1) == INTEGER_CST
9186 && (TREE_CODE (arg0) == MIN_EXPR
9187 || TREE_CODE (arg0) == MAX_EXPR)
9188 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9189 {
9190 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9191 if (tem)
9192 return tem;
9193 }
9194
9195 /* If we are comparing an expression that just has comparisons
9196 of two integer values, arithmetic expressions of those comparisons,
9197 and constants, we can simplify it. There are only three cases
9198 to check: the two values can either be equal, the first can be
9199 greater, or the second can be greater. Fold the expression for
9200 those three values. Since each value must be 0 or 1, we have
9201 eight possibilities, each of which corresponds to the constant 0
9202 or 1 or one of the six possible comparisons.
9203
9204 This handles common cases like (a > b) == 0 but also handles
9205 expressions like ((x > y) - (y > x)) > 0, which supposedly
9206 occur in macroized code. */
9207
9208 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9209 {
9210 tree cval1 = 0, cval2 = 0;
9211 int save_p = 0;
9212
9213 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9214 /* Don't handle degenerate cases here; they should already
9215 have been handled anyway. */
9216 && cval1 != 0 && cval2 != 0
9217 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9218 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9219 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9220 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9221 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9222 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9223 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9224 {
9225 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9226 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9227
9228 /* We can't just pass T to eval_subst in case cval1 or cval2
9229 was the same as ARG1. */
9230
9231 tree high_result
9232 = fold_build2_loc (loc, code, type,
9233 eval_subst (loc, arg0, cval1, maxval,
9234 cval2, minval),
9235 arg1);
9236 tree equal_result
9237 = fold_build2_loc (loc, code, type,
9238 eval_subst (loc, arg0, cval1, maxval,
9239 cval2, maxval),
9240 arg1);
9241 tree low_result
9242 = fold_build2_loc (loc, code, type,
9243 eval_subst (loc, arg0, cval1, minval,
9244 cval2, maxval),
9245 arg1);
9246
9247 /* All three of these results should be 0 or 1. Confirm they are.
9248 Then use those values to select the proper code to use. */
9249
9250 if (TREE_CODE (high_result) == INTEGER_CST
9251 && TREE_CODE (equal_result) == INTEGER_CST
9252 && TREE_CODE (low_result) == INTEGER_CST)
9253 {
9254 /* Make a 3-bit mask with the high-order bit being the
9255 value for `>', the next for '=', and the low for '<'. */
9256 switch ((integer_onep (high_result) * 4)
9257 + (integer_onep (equal_result) * 2)
9258 + integer_onep (low_result))
9259 {
9260 case 0:
9261 /* Always false. */
9262 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9263 case 1:
9264 code = LT_EXPR;
9265 break;
9266 case 2:
9267 code = EQ_EXPR;
9268 break;
9269 case 3:
9270 code = LE_EXPR;
9271 break;
9272 case 4:
9273 code = GT_EXPR;
9274 break;
9275 case 5:
9276 code = NE_EXPR;
9277 break;
9278 case 6:
9279 code = GE_EXPR;
9280 break;
9281 case 7:
9282 /* Always true. */
9283 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9284 }
9285
9286 if (save_p)
9287 {
9288 tem = save_expr (build2 (code, type, cval1, cval2));
9289 SET_EXPR_LOCATION (tem, loc);
9290 return tem;
9291 }
9292 return fold_build2_loc (loc, code, type, cval1, cval2);
9293 }
9294 }
9295 }
9296
9297 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9298 into a single range test. */
9299 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9300 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9301 && TREE_CODE (arg1) == INTEGER_CST
9302 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9303 && !integer_zerop (TREE_OPERAND (arg0, 1))
9304 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9305 && !TREE_OVERFLOW (arg1))
9306 {
9307 tem = fold_div_compare (loc, code, type, arg0, arg1);
9308 if (tem != NULL_TREE)
9309 return tem;
9310 }
9311
9312 return NULL_TREE;
9313 }
9314
9315
9316 /* Subroutine of fold_binary. Optimize complex multiplications of the
9317 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9318 argument EXPR represents the expression "z" of type TYPE. */
9319
9320 static tree
9321 fold_mult_zconjz (location_t loc, tree type, tree expr)
9322 {
9323 tree itype = TREE_TYPE (type);
9324 tree rpart, ipart, tem;
9325
9326 if (TREE_CODE (expr) == COMPLEX_EXPR)
9327 {
9328 rpart = TREE_OPERAND (expr, 0);
9329 ipart = TREE_OPERAND (expr, 1);
9330 }
9331 else if (TREE_CODE (expr) == COMPLEX_CST)
9332 {
9333 rpart = TREE_REALPART (expr);
9334 ipart = TREE_IMAGPART (expr);
9335 }
9336 else
9337 {
9338 expr = save_expr (expr);
9339 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9340 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9341 }
9342
9343 rpart = save_expr (rpart);
9344 ipart = save_expr (ipart);
9345 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9346 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9347 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9348 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9349 build_zero_cst (itype));
9350 }
9351
9352
9353 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9354 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9355 guarantees that P and N have the same least significant log2(M) bits.
9356 N is not otherwise constrained. In particular, N is not normalized to
9357 0 <= N < M as is common. In general, the precise value of P is unknown.
9358 M is chosen as large as possible such that constant N can be determined.
9359
9360 Returns M and sets *RESIDUE to N.
9361
9362 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9363 account. This is not always possible due to PR 35705.
9364 */
9365
9366 static unsigned HOST_WIDE_INT
9367 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9368 bool allow_func_align)
9369 {
9370 enum tree_code code;
9371
9372 *residue = 0;
9373
9374 code = TREE_CODE (expr);
9375 if (code == ADDR_EXPR)
9376 {
9377 unsigned int bitalign;
9378 get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue);
9379 *residue /= BITS_PER_UNIT;
9380 return bitalign / BITS_PER_UNIT;
9381 }
9382 else if (code == POINTER_PLUS_EXPR)
9383 {
9384 tree op0, op1;
9385 unsigned HOST_WIDE_INT modulus;
9386 enum tree_code inner_code;
9387
9388 op0 = TREE_OPERAND (expr, 0);
9389 STRIP_NOPS (op0);
9390 modulus = get_pointer_modulus_and_residue (op0, residue,
9391 allow_func_align);
9392
9393 op1 = TREE_OPERAND (expr, 1);
9394 STRIP_NOPS (op1);
9395 inner_code = TREE_CODE (op1);
9396 if (inner_code == INTEGER_CST)
9397 {
9398 *residue += TREE_INT_CST_LOW (op1);
9399 return modulus;
9400 }
9401 else if (inner_code == MULT_EXPR)
9402 {
9403 op1 = TREE_OPERAND (op1, 1);
9404 if (TREE_CODE (op1) == INTEGER_CST)
9405 {
9406 unsigned HOST_WIDE_INT align;
9407
9408 /* Compute the greatest power-of-2 divisor of op1. */
9409 align = TREE_INT_CST_LOW (op1);
9410 align &= -align;
9411
9412 /* If align is non-zero and less than *modulus, replace
9413 *modulus with align., If align is 0, then either op1 is 0
9414 or the greatest power-of-2 divisor of op1 doesn't fit in an
9415 unsigned HOST_WIDE_INT. In either case, no additional
9416 constraint is imposed. */
9417 if (align)
9418 modulus = MIN (modulus, align);
9419
9420 return modulus;
9421 }
9422 }
9423 }
9424
9425 /* If we get here, we were unable to determine anything useful about the
9426 expression. */
9427 return 1;
9428 }
9429
9430 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9431 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9432
9433 static bool
9434 vec_cst_ctor_to_array (tree arg, tree *elts)
9435 {
9436 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9437
9438 if (TREE_CODE (arg) == VECTOR_CST)
9439 {
9440 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
9441 elts[i] = VECTOR_CST_ELT (arg, i);
9442 }
9443 else if (TREE_CODE (arg) == CONSTRUCTOR)
9444 {
9445 constructor_elt *elt;
9446
9447 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9448 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9449 return false;
9450 else
9451 elts[i] = elt->value;
9452 }
9453 else
9454 return false;
9455 for (; i < nelts; i++)
9456 elts[i]
9457 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9458 return true;
9459 }
9460
9461 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9462 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9463 NULL_TREE otherwise. */
9464
9465 static tree
9466 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9467 {
9468 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9469 tree *elts;
9470 bool need_ctor = false;
9471
9472 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9473 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9474 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9475 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9476 return NULL_TREE;
9477
9478 elts = XALLOCAVEC (tree, nelts * 3);
9479 if (!vec_cst_ctor_to_array (arg0, elts)
9480 || !vec_cst_ctor_to_array (arg1, elts + nelts))
9481 return NULL_TREE;
9482
9483 for (i = 0; i < nelts; i++)
9484 {
9485 if (!CONSTANT_CLASS_P (elts[sel[i]]))
9486 need_ctor = true;
9487 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9488 }
9489
9490 if (need_ctor)
9491 {
9492 vec<constructor_elt, va_gc> *v;
9493 vec_alloc (v, nelts);
9494 for (i = 0; i < nelts; i++)
9495 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9496 return build_constructor (type, v);
9497 }
9498 else
9499 return build_vector (type, &elts[2 * nelts]);
9500 }
9501
9502 /* Try to fold a pointer difference of type TYPE two address expressions of
9503 array references AREF0 and AREF1 using location LOC. Return a
9504 simplified expression for the difference or NULL_TREE. */
9505
9506 static tree
9507 fold_addr_of_array_ref_difference (location_t loc, tree type,
9508 tree aref0, tree aref1)
9509 {
9510 tree base0 = TREE_OPERAND (aref0, 0);
9511 tree base1 = TREE_OPERAND (aref1, 0);
9512 tree base_offset = build_int_cst (type, 0);
9513
9514 /* If the bases are array references as well, recurse. If the bases
9515 are pointer indirections compute the difference of the pointers.
9516 If the bases are equal, we are set. */
9517 if ((TREE_CODE (base0) == ARRAY_REF
9518 && TREE_CODE (base1) == ARRAY_REF
9519 && (base_offset
9520 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9521 || (INDIRECT_REF_P (base0)
9522 && INDIRECT_REF_P (base1)
9523 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9524 TREE_OPERAND (base0, 0),
9525 TREE_OPERAND (base1, 0))))
9526 || operand_equal_p (base0, base1, 0))
9527 {
9528 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9529 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9530 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9531 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9532 return fold_build2_loc (loc, PLUS_EXPR, type,
9533 base_offset,
9534 fold_build2_loc (loc, MULT_EXPR, type,
9535 diff, esz));
9536 }
9537 return NULL_TREE;
9538 }
9539
9540 /* If the real or vector real constant CST of type TYPE has an exact
9541 inverse, return it, else return NULL. */
9542
9543 tree
9544 exact_inverse (tree type, tree cst)
9545 {
9546 REAL_VALUE_TYPE r;
9547 tree unit_type, *elts;
9548 machine_mode mode;
9549 unsigned vec_nelts, i;
9550
9551 switch (TREE_CODE (cst))
9552 {
9553 case REAL_CST:
9554 r = TREE_REAL_CST (cst);
9555
9556 if (exact_real_inverse (TYPE_MODE (type), &r))
9557 return build_real (type, r);
9558
9559 return NULL_TREE;
9560
9561 case VECTOR_CST:
9562 vec_nelts = VECTOR_CST_NELTS (cst);
9563 elts = XALLOCAVEC (tree, vec_nelts);
9564 unit_type = TREE_TYPE (type);
9565 mode = TYPE_MODE (unit_type);
9566
9567 for (i = 0; i < vec_nelts; i++)
9568 {
9569 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9570 if (!exact_real_inverse (mode, &r))
9571 return NULL_TREE;
9572 elts[i] = build_real (unit_type, r);
9573 }
9574
9575 return build_vector (type, elts);
9576
9577 default:
9578 return NULL_TREE;
9579 }
9580 }
9581
9582 /* Mask out the tz least significant bits of X of type TYPE where
9583 tz is the number of trailing zeroes in Y. */
9584 static wide_int
9585 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9586 {
9587 int tz = wi::ctz (y);
9588 if (tz > 0)
9589 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9590 return x;
9591 }
9592
9593 /* Return true when T is an address and is known to be nonzero.
9594 For floating point we further ensure that T is not denormal.
9595 Similar logic is present in nonzero_address in rtlanal.h.
9596
9597 If the return value is based on the assumption that signed overflow
9598 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9599 change *STRICT_OVERFLOW_P. */
9600
9601 static bool
9602 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9603 {
9604 tree type = TREE_TYPE (t);
9605 enum tree_code code;
9606
9607 /* Doing something useful for floating point would need more work. */
9608 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9609 return false;
9610
9611 code = TREE_CODE (t);
9612 switch (TREE_CODE_CLASS (code))
9613 {
9614 case tcc_unary:
9615 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9616 strict_overflow_p);
9617 case tcc_binary:
9618 case tcc_comparison:
9619 return tree_binary_nonzero_warnv_p (code, type,
9620 TREE_OPERAND (t, 0),
9621 TREE_OPERAND (t, 1),
9622 strict_overflow_p);
9623 case tcc_constant:
9624 case tcc_declaration:
9625 case tcc_reference:
9626 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9627
9628 default:
9629 break;
9630 }
9631
9632 switch (code)
9633 {
9634 case TRUTH_NOT_EXPR:
9635 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9636 strict_overflow_p);
9637
9638 case TRUTH_AND_EXPR:
9639 case TRUTH_OR_EXPR:
9640 case TRUTH_XOR_EXPR:
9641 return tree_binary_nonzero_warnv_p (code, type,
9642 TREE_OPERAND (t, 0),
9643 TREE_OPERAND (t, 1),
9644 strict_overflow_p);
9645
9646 case COND_EXPR:
9647 case CONSTRUCTOR:
9648 case OBJ_TYPE_REF:
9649 case ASSERT_EXPR:
9650 case ADDR_EXPR:
9651 case WITH_SIZE_EXPR:
9652 case SSA_NAME:
9653 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9654
9655 case COMPOUND_EXPR:
9656 case MODIFY_EXPR:
9657 case BIND_EXPR:
9658 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9659 strict_overflow_p);
9660
9661 case SAVE_EXPR:
9662 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9663 strict_overflow_p);
9664
9665 case CALL_EXPR:
9666 {
9667 tree fndecl = get_callee_fndecl (t);
9668 if (!fndecl) return false;
9669 if (flag_delete_null_pointer_checks && !flag_check_new
9670 && DECL_IS_OPERATOR_NEW (fndecl)
9671 && !TREE_NOTHROW (fndecl))
9672 return true;
9673 if (flag_delete_null_pointer_checks
9674 && lookup_attribute ("returns_nonnull",
9675 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9676 return true;
9677 return alloca_call_p (t);
9678 }
9679
9680 default:
9681 break;
9682 }
9683 return false;
9684 }
9685
9686 /* Return true when T is an address and is known to be nonzero.
9687 Handle warnings about undefined signed overflow. */
9688
9689 static bool
9690 tree_expr_nonzero_p (tree t)
9691 {
9692 bool ret, strict_overflow_p;
9693
9694 strict_overflow_p = false;
9695 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9696 if (strict_overflow_p)
9697 fold_overflow_warning (("assuming signed overflow does not occur when "
9698 "determining that expression is always "
9699 "non-zero"),
9700 WARN_STRICT_OVERFLOW_MISC);
9701 return ret;
9702 }
9703
9704 /* Fold a binary expression of code CODE and type TYPE with operands
9705 OP0 and OP1. LOC is the location of the resulting expression.
9706 Return the folded expression if folding is successful. Otherwise,
9707 return NULL_TREE. */
9708
9709 tree
9710 fold_binary_loc (location_t loc,
9711 enum tree_code code, tree type, tree op0, tree op1)
9712 {
9713 enum tree_code_class kind = TREE_CODE_CLASS (code);
9714 tree arg0, arg1, tem;
9715 tree t1 = NULL_TREE;
9716 bool strict_overflow_p;
9717 unsigned int prec;
9718
9719 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9720 && TREE_CODE_LENGTH (code) == 2
9721 && op0 != NULL_TREE
9722 && op1 != NULL_TREE);
9723
9724 arg0 = op0;
9725 arg1 = op1;
9726
9727 /* Strip any conversions that don't change the mode. This is
9728 safe for every expression, except for a comparison expression
9729 because its signedness is derived from its operands. So, in
9730 the latter case, only strip conversions that don't change the
9731 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9732 preserved.
9733
9734 Note that this is done as an internal manipulation within the
9735 constant folder, in order to find the simplest representation
9736 of the arguments so that their form can be studied. In any
9737 cases, the appropriate type conversions should be put back in
9738 the tree that will get out of the constant folder. */
9739
9740 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9741 {
9742 STRIP_SIGN_NOPS (arg0);
9743 STRIP_SIGN_NOPS (arg1);
9744 }
9745 else
9746 {
9747 STRIP_NOPS (arg0);
9748 STRIP_NOPS (arg1);
9749 }
9750
9751 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9752 constant but we can't do arithmetic on them. */
9753 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9754 {
9755 tem = const_binop (code, type, arg0, arg1);
9756 if (tem != NULL_TREE)
9757 {
9758 if (TREE_TYPE (tem) != type)
9759 tem = fold_convert_loc (loc, type, tem);
9760 return tem;
9761 }
9762 }
9763
9764 /* If this is a commutative operation, and ARG0 is a constant, move it
9765 to ARG1 to reduce the number of tests below. */
9766 if (commutative_tree_code (code)
9767 && tree_swap_operands_p (arg0, arg1, true))
9768 return fold_build2_loc (loc, code, type, op1, op0);
9769
9770 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9771 to ARG1 to reduce the number of tests below. */
9772 if (kind == tcc_comparison
9773 && tree_swap_operands_p (arg0, arg1, true))
9774 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9775
9776 tem = generic_simplify (loc, code, type, op0, op1);
9777 if (tem)
9778 return tem;
9779
9780 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9781
9782 First check for cases where an arithmetic operation is applied to a
9783 compound, conditional, or comparison operation. Push the arithmetic
9784 operation inside the compound or conditional to see if any folding
9785 can then be done. Convert comparison to conditional for this purpose.
9786 The also optimizes non-constant cases that used to be done in
9787 expand_expr.
9788
9789 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9790 one of the operands is a comparison and the other is a comparison, a
9791 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9792 code below would make the expression more complex. Change it to a
9793 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9794 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9795
9796 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9797 || code == EQ_EXPR || code == NE_EXPR)
9798 && TREE_CODE (type) != VECTOR_TYPE
9799 && ((truth_value_p (TREE_CODE (arg0))
9800 && (truth_value_p (TREE_CODE (arg1))
9801 || (TREE_CODE (arg1) == BIT_AND_EXPR
9802 && integer_onep (TREE_OPERAND (arg1, 1)))))
9803 || (truth_value_p (TREE_CODE (arg1))
9804 && (truth_value_p (TREE_CODE (arg0))
9805 || (TREE_CODE (arg0) == BIT_AND_EXPR
9806 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9807 {
9808 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9809 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9810 : TRUTH_XOR_EXPR,
9811 boolean_type_node,
9812 fold_convert_loc (loc, boolean_type_node, arg0),
9813 fold_convert_loc (loc, boolean_type_node, arg1));
9814
9815 if (code == EQ_EXPR)
9816 tem = invert_truthvalue_loc (loc, tem);
9817
9818 return fold_convert_loc (loc, type, tem);
9819 }
9820
9821 if (TREE_CODE_CLASS (code) == tcc_binary
9822 || TREE_CODE_CLASS (code) == tcc_comparison)
9823 {
9824 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9825 {
9826 tem = fold_build2_loc (loc, code, type,
9827 fold_convert_loc (loc, TREE_TYPE (op0),
9828 TREE_OPERAND (arg0, 1)), op1);
9829 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9830 tem);
9831 }
9832 if (TREE_CODE (arg1) == COMPOUND_EXPR
9833 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9834 {
9835 tem = fold_build2_loc (loc, code, type, op0,
9836 fold_convert_loc (loc, TREE_TYPE (op1),
9837 TREE_OPERAND (arg1, 1)));
9838 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9839 tem);
9840 }
9841
9842 if (TREE_CODE (arg0) == COND_EXPR
9843 || TREE_CODE (arg0) == VEC_COND_EXPR
9844 || COMPARISON_CLASS_P (arg0))
9845 {
9846 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9847 arg0, arg1,
9848 /*cond_first_p=*/1);
9849 if (tem != NULL_TREE)
9850 return tem;
9851 }
9852
9853 if (TREE_CODE (arg1) == COND_EXPR
9854 || TREE_CODE (arg1) == VEC_COND_EXPR
9855 || COMPARISON_CLASS_P (arg1))
9856 {
9857 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9858 arg1, arg0,
9859 /*cond_first_p=*/0);
9860 if (tem != NULL_TREE)
9861 return tem;
9862 }
9863 }
9864
9865 switch (code)
9866 {
9867 case MEM_REF:
9868 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9869 if (TREE_CODE (arg0) == ADDR_EXPR
9870 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9871 {
9872 tree iref = TREE_OPERAND (arg0, 0);
9873 return fold_build2 (MEM_REF, type,
9874 TREE_OPERAND (iref, 0),
9875 int_const_binop (PLUS_EXPR, arg1,
9876 TREE_OPERAND (iref, 1)));
9877 }
9878
9879 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9880 if (TREE_CODE (arg0) == ADDR_EXPR
9881 && handled_component_p (TREE_OPERAND (arg0, 0)))
9882 {
9883 tree base;
9884 HOST_WIDE_INT coffset;
9885 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9886 &coffset);
9887 if (!base)
9888 return NULL_TREE;
9889 return fold_build2 (MEM_REF, type,
9890 build_fold_addr_expr (base),
9891 int_const_binop (PLUS_EXPR, arg1,
9892 size_int (coffset)));
9893 }
9894
9895 return NULL_TREE;
9896
9897 case POINTER_PLUS_EXPR:
9898 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9899 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9900 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9901 return fold_convert_loc (loc, type,
9902 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9903 fold_convert_loc (loc, sizetype,
9904 arg1),
9905 fold_convert_loc (loc, sizetype,
9906 arg0)));
9907
9908 return NULL_TREE;
9909
9910 case PLUS_EXPR:
9911 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9912 {
9913 /* X + (X / CST) * -CST is X % CST. */
9914 if (TREE_CODE (arg1) == MULT_EXPR
9915 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9916 && operand_equal_p (arg0,
9917 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9918 {
9919 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9920 tree cst1 = TREE_OPERAND (arg1, 1);
9921 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9922 cst1, cst0);
9923 if (sum && integer_zerop (sum))
9924 return fold_convert_loc (loc, type,
9925 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9926 TREE_TYPE (arg0), arg0,
9927 cst0));
9928 }
9929 }
9930
9931 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9932 one. Make sure the type is not saturating and has the signedness of
9933 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9934 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9935 if ((TREE_CODE (arg0) == MULT_EXPR
9936 || TREE_CODE (arg1) == MULT_EXPR)
9937 && !TYPE_SATURATING (type)
9938 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9939 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9940 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9941 {
9942 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9943 if (tem)
9944 return tem;
9945 }
9946
9947 if (! FLOAT_TYPE_P (type))
9948 {
9949 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9950 with a constant, and the two constants have no bits in common,
9951 we should treat this as a BIT_IOR_EXPR since this may produce more
9952 simplifications. */
9953 if (TREE_CODE (arg0) == BIT_AND_EXPR
9954 && TREE_CODE (arg1) == BIT_AND_EXPR
9955 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9956 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9957 && wi::bit_and (TREE_OPERAND (arg0, 1),
9958 TREE_OPERAND (arg1, 1)) == 0)
9959 {
9960 code = BIT_IOR_EXPR;
9961 goto bit_ior;
9962 }
9963
9964 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9965 (plus (plus (mult) (mult)) (foo)) so that we can
9966 take advantage of the factoring cases below. */
9967 if (ANY_INTEGRAL_TYPE_P (type)
9968 && TYPE_OVERFLOW_WRAPS (type)
9969 && (((TREE_CODE (arg0) == PLUS_EXPR
9970 || TREE_CODE (arg0) == MINUS_EXPR)
9971 && TREE_CODE (arg1) == MULT_EXPR)
9972 || ((TREE_CODE (arg1) == PLUS_EXPR
9973 || TREE_CODE (arg1) == MINUS_EXPR)
9974 && TREE_CODE (arg0) == MULT_EXPR)))
9975 {
9976 tree parg0, parg1, parg, marg;
9977 enum tree_code pcode;
9978
9979 if (TREE_CODE (arg1) == MULT_EXPR)
9980 parg = arg0, marg = arg1;
9981 else
9982 parg = arg1, marg = arg0;
9983 pcode = TREE_CODE (parg);
9984 parg0 = TREE_OPERAND (parg, 0);
9985 parg1 = TREE_OPERAND (parg, 1);
9986 STRIP_NOPS (parg0);
9987 STRIP_NOPS (parg1);
9988
9989 if (TREE_CODE (parg0) == MULT_EXPR
9990 && TREE_CODE (parg1) != MULT_EXPR)
9991 return fold_build2_loc (loc, pcode, type,
9992 fold_build2_loc (loc, PLUS_EXPR, type,
9993 fold_convert_loc (loc, type,
9994 parg0),
9995 fold_convert_loc (loc, type,
9996 marg)),
9997 fold_convert_loc (loc, type, parg1));
9998 if (TREE_CODE (parg0) != MULT_EXPR
9999 && TREE_CODE (parg1) == MULT_EXPR)
10000 return
10001 fold_build2_loc (loc, PLUS_EXPR, type,
10002 fold_convert_loc (loc, type, parg0),
10003 fold_build2_loc (loc, pcode, type,
10004 fold_convert_loc (loc, type, marg),
10005 fold_convert_loc (loc, type,
10006 parg1)));
10007 }
10008 }
10009 else
10010 {
10011 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10012 to __complex__ ( x, y ). This is not the same for SNaNs or
10013 if signed zeros are involved. */
10014 if (!HONOR_SNANS (element_mode (arg0))
10015 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10016 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10017 {
10018 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10019 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10020 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10021 bool arg0rz = false, arg0iz = false;
10022 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10023 || (arg0i && (arg0iz = real_zerop (arg0i))))
10024 {
10025 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10026 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10027 if (arg0rz && arg1i && real_zerop (arg1i))
10028 {
10029 tree rp = arg1r ? arg1r
10030 : build1 (REALPART_EXPR, rtype, arg1);
10031 tree ip = arg0i ? arg0i
10032 : build1 (IMAGPART_EXPR, rtype, arg0);
10033 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10034 }
10035 else if (arg0iz && arg1r && real_zerop (arg1r))
10036 {
10037 tree rp = arg0r ? arg0r
10038 : build1 (REALPART_EXPR, rtype, arg0);
10039 tree ip = arg1i ? arg1i
10040 : build1 (IMAGPART_EXPR, rtype, arg1);
10041 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10042 }
10043 }
10044 }
10045
10046 if (flag_unsafe_math_optimizations
10047 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10048 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10049 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10050 return tem;
10051
10052 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10053 We associate floats only if the user has specified
10054 -fassociative-math. */
10055 if (flag_associative_math
10056 && TREE_CODE (arg1) == PLUS_EXPR
10057 && TREE_CODE (arg0) != MULT_EXPR)
10058 {
10059 tree tree10 = TREE_OPERAND (arg1, 0);
10060 tree tree11 = TREE_OPERAND (arg1, 1);
10061 if (TREE_CODE (tree11) == MULT_EXPR
10062 && TREE_CODE (tree10) == MULT_EXPR)
10063 {
10064 tree tree0;
10065 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10066 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10067 }
10068 }
10069 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10070 We associate floats only if the user has specified
10071 -fassociative-math. */
10072 if (flag_associative_math
10073 && TREE_CODE (arg0) == PLUS_EXPR
10074 && TREE_CODE (arg1) != MULT_EXPR)
10075 {
10076 tree tree00 = TREE_OPERAND (arg0, 0);
10077 tree tree01 = TREE_OPERAND (arg0, 1);
10078 if (TREE_CODE (tree01) == MULT_EXPR
10079 && TREE_CODE (tree00) == MULT_EXPR)
10080 {
10081 tree tree0;
10082 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10083 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10084 }
10085 }
10086 }
10087
10088 bit_rotate:
10089 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10090 is a rotate of A by C1 bits. */
10091 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10092 is a rotate of A by B bits. */
10093 {
10094 enum tree_code code0, code1;
10095 tree rtype;
10096 code0 = TREE_CODE (arg0);
10097 code1 = TREE_CODE (arg1);
10098 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10099 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10100 && operand_equal_p (TREE_OPERAND (arg0, 0),
10101 TREE_OPERAND (arg1, 0), 0)
10102 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10103 TYPE_UNSIGNED (rtype))
10104 /* Only create rotates in complete modes. Other cases are not
10105 expanded properly. */
10106 && (element_precision (rtype)
10107 == element_precision (TYPE_MODE (rtype))))
10108 {
10109 tree tree01, tree11;
10110 enum tree_code code01, code11;
10111
10112 tree01 = TREE_OPERAND (arg0, 1);
10113 tree11 = TREE_OPERAND (arg1, 1);
10114 STRIP_NOPS (tree01);
10115 STRIP_NOPS (tree11);
10116 code01 = TREE_CODE (tree01);
10117 code11 = TREE_CODE (tree11);
10118 if (code01 == INTEGER_CST
10119 && code11 == INTEGER_CST
10120 && (wi::to_widest (tree01) + wi::to_widest (tree11)
10121 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10122 {
10123 tem = build2_loc (loc, LROTATE_EXPR,
10124 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10125 TREE_OPERAND (arg0, 0),
10126 code0 == LSHIFT_EXPR
10127 ? TREE_OPERAND (arg0, 1)
10128 : TREE_OPERAND (arg1, 1));
10129 return fold_convert_loc (loc, type, tem);
10130 }
10131 else if (code11 == MINUS_EXPR)
10132 {
10133 tree tree110, tree111;
10134 tree110 = TREE_OPERAND (tree11, 0);
10135 tree111 = TREE_OPERAND (tree11, 1);
10136 STRIP_NOPS (tree110);
10137 STRIP_NOPS (tree111);
10138 if (TREE_CODE (tree110) == INTEGER_CST
10139 && 0 == compare_tree_int (tree110,
10140 element_precision
10141 (TREE_TYPE (TREE_OPERAND
10142 (arg0, 0))))
10143 && operand_equal_p (tree01, tree111, 0))
10144 return
10145 fold_convert_loc (loc, type,
10146 build2 ((code0 == LSHIFT_EXPR
10147 ? LROTATE_EXPR
10148 : RROTATE_EXPR),
10149 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10150 TREE_OPERAND (arg0, 0),
10151 TREE_OPERAND (arg0, 1)));
10152 }
10153 else if (code01 == MINUS_EXPR)
10154 {
10155 tree tree010, tree011;
10156 tree010 = TREE_OPERAND (tree01, 0);
10157 tree011 = TREE_OPERAND (tree01, 1);
10158 STRIP_NOPS (tree010);
10159 STRIP_NOPS (tree011);
10160 if (TREE_CODE (tree010) == INTEGER_CST
10161 && 0 == compare_tree_int (tree010,
10162 element_precision
10163 (TREE_TYPE (TREE_OPERAND
10164 (arg0, 0))))
10165 && operand_equal_p (tree11, tree011, 0))
10166 return fold_convert_loc
10167 (loc, type,
10168 build2 ((code0 != LSHIFT_EXPR
10169 ? LROTATE_EXPR
10170 : RROTATE_EXPR),
10171 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10172 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
10173 }
10174 }
10175 }
10176
10177 associate:
10178 /* In most languages, can't associate operations on floats through
10179 parentheses. Rather than remember where the parentheses were, we
10180 don't associate floats at all, unless the user has specified
10181 -fassociative-math.
10182 And, we need to make sure type is not saturating. */
10183
10184 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10185 && !TYPE_SATURATING (type))
10186 {
10187 tree var0, con0, lit0, minus_lit0;
10188 tree var1, con1, lit1, minus_lit1;
10189 tree atype = type;
10190 bool ok = true;
10191
10192 /* Split both trees into variables, constants, and literals. Then
10193 associate each group together, the constants with literals,
10194 then the result with variables. This increases the chances of
10195 literals being recombined later and of generating relocatable
10196 expressions for the sum of a constant and literal. */
10197 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10198 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10199 code == MINUS_EXPR);
10200
10201 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10202 if (code == MINUS_EXPR)
10203 code = PLUS_EXPR;
10204
10205 /* With undefined overflow prefer doing association in a type
10206 which wraps on overflow, if that is one of the operand types. */
10207 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10208 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10209 {
10210 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10211 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10212 atype = TREE_TYPE (arg0);
10213 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10214 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
10215 atype = TREE_TYPE (arg1);
10216 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
10217 }
10218
10219 /* With undefined overflow we can only associate constants with one
10220 variable, and constants whose association doesn't overflow. */
10221 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10222 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
10223 {
10224 if (var0 && var1)
10225 {
10226 tree tmp0 = var0;
10227 tree tmp1 = var1;
10228
10229 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10230 tmp0 = TREE_OPERAND (tmp0, 0);
10231 if (CONVERT_EXPR_P (tmp0)
10232 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10233 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10234 <= TYPE_PRECISION (atype)))
10235 tmp0 = TREE_OPERAND (tmp0, 0);
10236 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10237 tmp1 = TREE_OPERAND (tmp1, 0);
10238 if (CONVERT_EXPR_P (tmp1)
10239 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10240 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10241 <= TYPE_PRECISION (atype)))
10242 tmp1 = TREE_OPERAND (tmp1, 0);
10243 /* The only case we can still associate with two variables
10244 is if they are the same, modulo negation and bit-pattern
10245 preserving conversions. */
10246 if (!operand_equal_p (tmp0, tmp1, 0))
10247 ok = false;
10248 }
10249 }
10250
10251 /* Only do something if we found more than two objects. Otherwise,
10252 nothing has changed and we risk infinite recursion. */
10253 if (ok
10254 && (2 < ((var0 != 0) + (var1 != 0)
10255 + (con0 != 0) + (con1 != 0)
10256 + (lit0 != 0) + (lit1 != 0)
10257 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10258 {
10259 bool any_overflows = false;
10260 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
10261 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
10262 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
10263 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
10264 var0 = associate_trees (loc, var0, var1, code, atype);
10265 con0 = associate_trees (loc, con0, con1, code, atype);
10266 lit0 = associate_trees (loc, lit0, lit1, code, atype);
10267 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
10268 code, atype);
10269
10270 /* Preserve the MINUS_EXPR if the negative part of the literal is
10271 greater than the positive part. Otherwise, the multiplicative
10272 folding code (i.e extract_muldiv) may be fooled in case
10273 unsigned constants are subtracted, like in the following
10274 example: ((X*2 + 4) - 8U)/2. */
10275 if (minus_lit0 && lit0)
10276 {
10277 if (TREE_CODE (lit0) == INTEGER_CST
10278 && TREE_CODE (minus_lit0) == INTEGER_CST
10279 && tree_int_cst_lt (lit0, minus_lit0))
10280 {
10281 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10282 MINUS_EXPR, atype);
10283 lit0 = 0;
10284 }
10285 else
10286 {
10287 lit0 = associate_trees (loc, lit0, minus_lit0,
10288 MINUS_EXPR, atype);
10289 minus_lit0 = 0;
10290 }
10291 }
10292
10293 /* Don't introduce overflows through reassociation. */
10294 if (!any_overflows
10295 && ((lit0 && TREE_OVERFLOW_P (lit0))
10296 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
10297 return NULL_TREE;
10298
10299 if (minus_lit0)
10300 {
10301 if (con0 == 0)
10302 return
10303 fold_convert_loc (loc, type,
10304 associate_trees (loc, var0, minus_lit0,
10305 MINUS_EXPR, atype));
10306 else
10307 {
10308 con0 = associate_trees (loc, con0, minus_lit0,
10309 MINUS_EXPR, atype);
10310 return
10311 fold_convert_loc (loc, type,
10312 associate_trees (loc, var0, con0,
10313 PLUS_EXPR, atype));
10314 }
10315 }
10316
10317 con0 = associate_trees (loc, con0, lit0, code, atype);
10318 return
10319 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10320 code, atype));
10321 }
10322 }
10323
10324 return NULL_TREE;
10325
10326 case MINUS_EXPR:
10327 /* Pointer simplifications for subtraction, simple reassociations. */
10328 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10329 {
10330 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10331 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10332 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10333 {
10334 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10335 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10336 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10337 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10338 return fold_build2_loc (loc, PLUS_EXPR, type,
10339 fold_build2_loc (loc, MINUS_EXPR, type,
10340 arg00, arg10),
10341 fold_build2_loc (loc, MINUS_EXPR, type,
10342 arg01, arg11));
10343 }
10344 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10345 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10346 {
10347 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10348 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10349 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10350 fold_convert_loc (loc, type, arg1));
10351 if (tmp)
10352 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10353 }
10354 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
10355 simplifies. */
10356 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10357 {
10358 tree arg10 = fold_convert_loc (loc, type,
10359 TREE_OPERAND (arg1, 0));
10360 tree arg11 = fold_convert_loc (loc, type,
10361 TREE_OPERAND (arg1, 1));
10362 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
10363 fold_convert_loc (loc, type, arg0),
10364 arg10);
10365 if (tmp)
10366 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
10367 }
10368 }
10369 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10370 if (TREE_CODE (arg0) == NEGATE_EXPR
10371 && negate_expr_p (arg1)
10372 && reorder_operands_p (arg0, arg1))
10373 return fold_build2_loc (loc, MINUS_EXPR, type,
10374 fold_convert_loc (loc, type,
10375 negate_expr (arg1)),
10376 fold_convert_loc (loc, type,
10377 TREE_OPERAND (arg0, 0)));
10378
10379 if (! FLOAT_TYPE_P (type))
10380 {
10381 /* Fold A - (A & B) into ~B & A. */
10382 if (!TREE_SIDE_EFFECTS (arg0)
10383 && TREE_CODE (arg1) == BIT_AND_EXPR)
10384 {
10385 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10386 {
10387 tree arg10 = fold_convert_loc (loc, type,
10388 TREE_OPERAND (arg1, 0));
10389 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10390 fold_build1_loc (loc, BIT_NOT_EXPR,
10391 type, arg10),
10392 fold_convert_loc (loc, type, arg0));
10393 }
10394 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10395 {
10396 tree arg11 = fold_convert_loc (loc,
10397 type, TREE_OPERAND (arg1, 1));
10398 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10399 fold_build1_loc (loc, BIT_NOT_EXPR,
10400 type, arg11),
10401 fold_convert_loc (loc, type, arg0));
10402 }
10403 }
10404
10405 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10406 any power of 2 minus 1. */
10407 if (TREE_CODE (arg0) == BIT_AND_EXPR
10408 && TREE_CODE (arg1) == BIT_AND_EXPR
10409 && operand_equal_p (TREE_OPERAND (arg0, 0),
10410 TREE_OPERAND (arg1, 0), 0))
10411 {
10412 tree mask0 = TREE_OPERAND (arg0, 1);
10413 tree mask1 = TREE_OPERAND (arg1, 1);
10414 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10415
10416 if (operand_equal_p (tem, mask1, 0))
10417 {
10418 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10419 TREE_OPERAND (arg0, 0), mask1);
10420 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10421 }
10422 }
10423 }
10424
10425 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10426 __complex__ ( x, -y ). This is not the same for SNaNs or if
10427 signed zeros are involved. */
10428 if (!HONOR_SNANS (element_mode (arg0))
10429 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10430 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10431 {
10432 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10433 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10434 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10435 bool arg0rz = false, arg0iz = false;
10436 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10437 || (arg0i && (arg0iz = real_zerop (arg0i))))
10438 {
10439 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10440 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10441 if (arg0rz && arg1i && real_zerop (arg1i))
10442 {
10443 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10444 arg1r ? arg1r
10445 : build1 (REALPART_EXPR, rtype, arg1));
10446 tree ip = arg0i ? arg0i
10447 : build1 (IMAGPART_EXPR, rtype, arg0);
10448 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10449 }
10450 else if (arg0iz && arg1r && real_zerop (arg1r))
10451 {
10452 tree rp = arg0r ? arg0r
10453 : build1 (REALPART_EXPR, rtype, arg0);
10454 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10455 arg1i ? arg1i
10456 : build1 (IMAGPART_EXPR, rtype, arg1));
10457 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10458 }
10459 }
10460 }
10461
10462 /* A - B -> A + (-B) if B is easily negatable. */
10463 if (negate_expr_p (arg1)
10464 && !TYPE_OVERFLOW_SANITIZED (type)
10465 && ((FLOAT_TYPE_P (type)
10466 /* Avoid this transformation if B is a positive REAL_CST. */
10467 && (TREE_CODE (arg1) != REAL_CST
10468 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10469 || INTEGRAL_TYPE_P (type)))
10470 return fold_build2_loc (loc, PLUS_EXPR, type,
10471 fold_convert_loc (loc, type, arg0),
10472 fold_convert_loc (loc, type,
10473 negate_expr (arg1)));
10474
10475 /* Fold &a[i] - &a[j] to i-j. */
10476 if (TREE_CODE (arg0) == ADDR_EXPR
10477 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10478 && TREE_CODE (arg1) == ADDR_EXPR
10479 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10480 {
10481 tree tem = fold_addr_of_array_ref_difference (loc, type,
10482 TREE_OPERAND (arg0, 0),
10483 TREE_OPERAND (arg1, 0));
10484 if (tem)
10485 return tem;
10486 }
10487
10488 if (FLOAT_TYPE_P (type)
10489 && flag_unsafe_math_optimizations
10490 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10491 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10492 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10493 return tem;
10494
10495 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10496 one. Make sure the type is not saturating and has the signedness of
10497 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10498 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10499 if ((TREE_CODE (arg0) == MULT_EXPR
10500 || TREE_CODE (arg1) == MULT_EXPR)
10501 && !TYPE_SATURATING (type)
10502 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10503 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10504 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10505 {
10506 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10507 if (tem)
10508 return tem;
10509 }
10510
10511 goto associate;
10512
10513 case MULT_EXPR:
10514 /* (-A) * (-B) -> A * B */
10515 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10516 return fold_build2_loc (loc, MULT_EXPR, type,
10517 fold_convert_loc (loc, type,
10518 TREE_OPERAND (arg0, 0)),
10519 fold_convert_loc (loc, type,
10520 negate_expr (arg1)));
10521 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10522 return fold_build2_loc (loc, MULT_EXPR, type,
10523 fold_convert_loc (loc, type,
10524 negate_expr (arg0)),
10525 fold_convert_loc (loc, type,
10526 TREE_OPERAND (arg1, 0)));
10527
10528 if (! FLOAT_TYPE_P (type))
10529 {
10530 /* Transform x * -C into -x * C if x is easily negatable. */
10531 if (TREE_CODE (arg1) == INTEGER_CST
10532 && tree_int_cst_sgn (arg1) == -1
10533 && negate_expr_p (arg0)
10534 && (tem = negate_expr (arg1)) != arg1
10535 && !TREE_OVERFLOW (tem))
10536 return fold_build2_loc (loc, MULT_EXPR, type,
10537 fold_convert_loc (loc, type,
10538 negate_expr (arg0)),
10539 tem);
10540
10541 /* (a * (1 << b)) is (a << b) */
10542 if (TREE_CODE (arg1) == LSHIFT_EXPR
10543 && integer_onep (TREE_OPERAND (arg1, 0)))
10544 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10545 TREE_OPERAND (arg1, 1));
10546 if (TREE_CODE (arg0) == LSHIFT_EXPR
10547 && integer_onep (TREE_OPERAND (arg0, 0)))
10548 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10549 TREE_OPERAND (arg0, 1));
10550
10551 /* (A + A) * C -> A * 2 * C */
10552 if (TREE_CODE (arg0) == PLUS_EXPR
10553 && TREE_CODE (arg1) == INTEGER_CST
10554 && operand_equal_p (TREE_OPERAND (arg0, 0),
10555 TREE_OPERAND (arg0, 1), 0))
10556 return fold_build2_loc (loc, MULT_EXPR, type,
10557 omit_one_operand_loc (loc, type,
10558 TREE_OPERAND (arg0, 0),
10559 TREE_OPERAND (arg0, 1)),
10560 fold_build2_loc (loc, MULT_EXPR, type,
10561 build_int_cst (type, 2) , arg1));
10562
10563 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10564 sign-changing only. */
10565 if (TREE_CODE (arg1) == INTEGER_CST
10566 && TREE_CODE (arg0) == EXACT_DIV_EXPR
10567 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
10568 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10569
10570 strict_overflow_p = false;
10571 if (TREE_CODE (arg1) == INTEGER_CST
10572 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10573 &strict_overflow_p)))
10574 {
10575 if (strict_overflow_p)
10576 fold_overflow_warning (("assuming signed overflow does not "
10577 "occur when simplifying "
10578 "multiplication"),
10579 WARN_STRICT_OVERFLOW_MISC);
10580 return fold_convert_loc (loc, type, tem);
10581 }
10582
10583 /* Optimize z * conj(z) for integer complex numbers. */
10584 if (TREE_CODE (arg0) == CONJ_EXPR
10585 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10586 return fold_mult_zconjz (loc, type, arg1);
10587 if (TREE_CODE (arg1) == CONJ_EXPR
10588 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10589 return fold_mult_zconjz (loc, type, arg0);
10590 }
10591 else
10592 {
10593 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10594 the result for floating point types due to rounding so it is applied
10595 only if -fassociative-math was specify. */
10596 if (flag_associative_math
10597 && TREE_CODE (arg0) == RDIV_EXPR
10598 && TREE_CODE (arg1) == REAL_CST
10599 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10600 {
10601 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10602 arg1);
10603 if (tem)
10604 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10605 TREE_OPERAND (arg0, 1));
10606 }
10607
10608 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10609 if (operand_equal_p (arg0, arg1, 0))
10610 {
10611 tree tem = fold_strip_sign_ops (arg0);
10612 if (tem != NULL_TREE)
10613 {
10614 tem = fold_convert_loc (loc, type, tem);
10615 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10616 }
10617 }
10618
10619 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10620 This is not the same for NaNs or if signed zeros are
10621 involved. */
10622 if (!HONOR_NANS (arg0)
10623 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10624 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10625 && TREE_CODE (arg1) == COMPLEX_CST
10626 && real_zerop (TREE_REALPART (arg1)))
10627 {
10628 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10629 if (real_onep (TREE_IMAGPART (arg1)))
10630 return
10631 fold_build2_loc (loc, COMPLEX_EXPR, type,
10632 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10633 rtype, arg0)),
10634 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10635 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10636 return
10637 fold_build2_loc (loc, COMPLEX_EXPR, type,
10638 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10639 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10640 rtype, arg0)));
10641 }
10642
10643 /* Optimize z * conj(z) for floating point complex numbers.
10644 Guarded by flag_unsafe_math_optimizations as non-finite
10645 imaginary components don't produce scalar results. */
10646 if (flag_unsafe_math_optimizations
10647 && TREE_CODE (arg0) == CONJ_EXPR
10648 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10649 return fold_mult_zconjz (loc, type, arg1);
10650 if (flag_unsafe_math_optimizations
10651 && TREE_CODE (arg1) == CONJ_EXPR
10652 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10653 return fold_mult_zconjz (loc, type, arg0);
10654
10655 if (flag_unsafe_math_optimizations)
10656 {
10657 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10658 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10659
10660 /* Optimizations of root(...)*root(...). */
10661 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10662 {
10663 tree rootfn, arg;
10664 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10665 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10666
10667 /* Optimize sqrt(x)*sqrt(x) as x. */
10668 if (BUILTIN_SQRT_P (fcode0)
10669 && operand_equal_p (arg00, arg10, 0)
10670 && ! HONOR_SNANS (element_mode (type)))
10671 return arg00;
10672
10673 /* Optimize root(x)*root(y) as root(x*y). */
10674 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10675 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10676 return build_call_expr_loc (loc, rootfn, 1, arg);
10677 }
10678
10679 /* Optimize expN(x)*expN(y) as expN(x+y). */
10680 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10681 {
10682 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10683 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10684 CALL_EXPR_ARG (arg0, 0),
10685 CALL_EXPR_ARG (arg1, 0));
10686 return build_call_expr_loc (loc, expfn, 1, arg);
10687 }
10688
10689 /* Optimizations of pow(...)*pow(...). */
10690 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10691 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10692 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10693 {
10694 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10695 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10696 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10697 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10698
10699 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10700 if (operand_equal_p (arg01, arg11, 0))
10701 {
10702 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10703 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10704 arg00, arg10);
10705 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10706 }
10707
10708 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10709 if (operand_equal_p (arg00, arg10, 0))
10710 {
10711 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10712 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10713 arg01, arg11);
10714 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10715 }
10716 }
10717
10718 /* Optimize tan(x)*cos(x) as sin(x). */
10719 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10720 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10721 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10722 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10723 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10724 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10725 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10726 CALL_EXPR_ARG (arg1, 0), 0))
10727 {
10728 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10729
10730 if (sinfn != NULL_TREE)
10731 return build_call_expr_loc (loc, sinfn, 1,
10732 CALL_EXPR_ARG (arg0, 0));
10733 }
10734
10735 /* Optimize x*pow(x,c) as pow(x,c+1). */
10736 if (fcode1 == BUILT_IN_POW
10737 || fcode1 == BUILT_IN_POWF
10738 || fcode1 == BUILT_IN_POWL)
10739 {
10740 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10741 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10742 if (TREE_CODE (arg11) == REAL_CST
10743 && !TREE_OVERFLOW (arg11)
10744 && operand_equal_p (arg0, arg10, 0))
10745 {
10746 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10747 REAL_VALUE_TYPE c;
10748 tree arg;
10749
10750 c = TREE_REAL_CST (arg11);
10751 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10752 arg = build_real (type, c);
10753 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10754 }
10755 }
10756
10757 /* Optimize pow(x,c)*x as pow(x,c+1). */
10758 if (fcode0 == BUILT_IN_POW
10759 || fcode0 == BUILT_IN_POWF
10760 || fcode0 == BUILT_IN_POWL)
10761 {
10762 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10763 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10764 if (TREE_CODE (arg01) == REAL_CST
10765 && !TREE_OVERFLOW (arg01)
10766 && operand_equal_p (arg1, arg00, 0))
10767 {
10768 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10769 REAL_VALUE_TYPE c;
10770 tree arg;
10771
10772 c = TREE_REAL_CST (arg01);
10773 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10774 arg = build_real (type, c);
10775 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10776 }
10777 }
10778
10779 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10780 if (!in_gimple_form
10781 && optimize
10782 && operand_equal_p (arg0, arg1, 0))
10783 {
10784 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10785
10786 if (powfn)
10787 {
10788 tree arg = build_real (type, dconst2);
10789 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10790 }
10791 }
10792 }
10793 }
10794 goto associate;
10795
10796 case BIT_IOR_EXPR:
10797 bit_ior:
10798 /* Canonicalize (X & C1) | C2. */
10799 if (TREE_CODE (arg0) == BIT_AND_EXPR
10800 && TREE_CODE (arg1) == INTEGER_CST
10801 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10802 {
10803 int width = TYPE_PRECISION (type), w;
10804 wide_int c1 = TREE_OPERAND (arg0, 1);
10805 wide_int c2 = arg1;
10806
10807 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10808 if ((c1 & c2) == c1)
10809 return omit_one_operand_loc (loc, type, arg1,
10810 TREE_OPERAND (arg0, 0));
10811
10812 wide_int msk = wi::mask (width, false,
10813 TYPE_PRECISION (TREE_TYPE (arg1)));
10814
10815 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10816 if (msk.and_not (c1 | c2) == 0)
10817 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10818 TREE_OPERAND (arg0, 0), arg1);
10819
10820 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10821 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10822 mode which allows further optimizations. */
10823 c1 &= msk;
10824 c2 &= msk;
10825 wide_int c3 = c1.and_not (c2);
10826 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10827 {
10828 wide_int mask = wi::mask (w, false,
10829 TYPE_PRECISION (type));
10830 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10831 {
10832 c3 = mask;
10833 break;
10834 }
10835 }
10836
10837 if (c3 != c1)
10838 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10839 fold_build2_loc (loc, BIT_AND_EXPR, type,
10840 TREE_OPERAND (arg0, 0),
10841 wide_int_to_tree (type,
10842 c3)),
10843 arg1);
10844 }
10845
10846 /* (X & ~Y) | (~X & Y) is X ^ Y */
10847 if (TREE_CODE (arg0) == BIT_AND_EXPR
10848 && TREE_CODE (arg1) == BIT_AND_EXPR)
10849 {
10850 tree a0, a1, l0, l1, n0, n1;
10851
10852 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10853 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10854
10855 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10856 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10857
10858 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10859 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10860
10861 if ((operand_equal_p (n0, a0, 0)
10862 && operand_equal_p (n1, a1, 0))
10863 || (operand_equal_p (n0, a1, 0)
10864 && operand_equal_p (n1, a0, 0)))
10865 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10866 }
10867
10868 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10869 if (t1 != NULL_TREE)
10870 return t1;
10871
10872 /* See if this can be simplified into a rotate first. If that
10873 is unsuccessful continue in the association code. */
10874 goto bit_rotate;
10875
10876 case BIT_XOR_EXPR:
10877 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10878 if (TREE_CODE (arg0) == BIT_AND_EXPR
10879 && INTEGRAL_TYPE_P (type)
10880 && integer_onep (TREE_OPERAND (arg0, 1))
10881 && integer_onep (arg1))
10882 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10883 build_zero_cst (TREE_TYPE (arg0)));
10884
10885 /* See if this can be simplified into a rotate first. If that
10886 is unsuccessful continue in the association code. */
10887 goto bit_rotate;
10888
10889 case BIT_AND_EXPR:
10890 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
10891 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
10892 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
10893 || (TREE_CODE (arg0) == EQ_EXPR
10894 && integer_zerop (TREE_OPERAND (arg0, 1))))
10895 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10896 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10897
10898 /* X & ~X , X & (X == 0), and X & !X are always zero. */
10899 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
10900 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
10901 || (TREE_CODE (arg1) == EQ_EXPR
10902 && integer_zerop (TREE_OPERAND (arg1, 1))))
10903 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10904 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10905
10906 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10907 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10908 && INTEGRAL_TYPE_P (type)
10909 && integer_onep (TREE_OPERAND (arg0, 1))
10910 && integer_onep (arg1))
10911 {
10912 tree tem2;
10913 tem = TREE_OPERAND (arg0, 0);
10914 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10915 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10916 tem, tem2);
10917 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10918 build_zero_cst (TREE_TYPE (tem)));
10919 }
10920 /* Fold ~X & 1 as (X & 1) == 0. */
10921 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10922 && INTEGRAL_TYPE_P (type)
10923 && integer_onep (arg1))
10924 {
10925 tree tem2;
10926 tem = TREE_OPERAND (arg0, 0);
10927 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10928 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10929 tem, tem2);
10930 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10931 build_zero_cst (TREE_TYPE (tem)));
10932 }
10933 /* Fold !X & 1 as X == 0. */
10934 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10935 && integer_onep (arg1))
10936 {
10937 tem = TREE_OPERAND (arg0, 0);
10938 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10939 build_zero_cst (TREE_TYPE (tem)));
10940 }
10941
10942 /* Fold (X ^ Y) & Y as ~X & Y. */
10943 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10944 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10945 {
10946 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10947 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10948 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10949 fold_convert_loc (loc, type, arg1));
10950 }
10951 /* Fold (X ^ Y) & X as ~Y & X. */
10952 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10953 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10954 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10955 {
10956 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10957 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10958 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10959 fold_convert_loc (loc, type, arg1));
10960 }
10961 /* Fold X & (X ^ Y) as X & ~Y. */
10962 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10963 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10964 {
10965 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10966 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10967 fold_convert_loc (loc, type, arg0),
10968 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10969 }
10970 /* Fold X & (Y ^ X) as ~Y & X. */
10971 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10972 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10973 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10974 {
10975 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10976 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10977 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10978 fold_convert_loc (loc, type, arg0));
10979 }
10980
10981 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10982 multiple of 1 << CST. */
10983 if (TREE_CODE (arg1) == INTEGER_CST)
10984 {
10985 wide_int cst1 = arg1;
10986 wide_int ncst1 = -cst1;
10987 if ((cst1 & ncst1) == ncst1
10988 && multiple_of_p (type, arg0,
10989 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10990 return fold_convert_loc (loc, type, arg0);
10991 }
10992
10993 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10994 bits from CST2. */
10995 if (TREE_CODE (arg1) == INTEGER_CST
10996 && TREE_CODE (arg0) == MULT_EXPR
10997 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10998 {
10999 wide_int warg1 = arg1;
11000 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
11001
11002 if (masked == 0)
11003 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11004 arg0, arg1);
11005 else if (masked != warg1)
11006 {
11007 /* Avoid the transform if arg1 is a mask of some
11008 mode which allows further optimizations. */
11009 int pop = wi::popcount (warg1);
11010 if (!(pop >= BITS_PER_UNIT
11011 && exact_log2 (pop) != -1
11012 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11013 return fold_build2_loc (loc, code, type, op0,
11014 wide_int_to_tree (type, masked));
11015 }
11016 }
11017
11018 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11019 ((A & N) + B) & M -> (A + B) & M
11020 Similarly if (N & M) == 0,
11021 ((A | N) + B) & M -> (A + B) & M
11022 and for - instead of + (or unary - instead of +)
11023 and/or ^ instead of |.
11024 If B is constant and (B & M) == 0, fold into A & M. */
11025 if (TREE_CODE (arg1) == INTEGER_CST)
11026 {
11027 wide_int cst1 = arg1;
11028 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
11029 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11030 && (TREE_CODE (arg0) == PLUS_EXPR
11031 || TREE_CODE (arg0) == MINUS_EXPR
11032 || TREE_CODE (arg0) == NEGATE_EXPR)
11033 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11034 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11035 {
11036 tree pmop[2];
11037 int which = 0;
11038 wide_int cst0;
11039
11040 /* Now we know that arg0 is (C + D) or (C - D) or
11041 -C and arg1 (M) is == (1LL << cst) - 1.
11042 Store C into PMOP[0] and D into PMOP[1]. */
11043 pmop[0] = TREE_OPERAND (arg0, 0);
11044 pmop[1] = NULL;
11045 if (TREE_CODE (arg0) != NEGATE_EXPR)
11046 {
11047 pmop[1] = TREE_OPERAND (arg0, 1);
11048 which = 1;
11049 }
11050
11051 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
11052 which = -1;
11053
11054 for (; which >= 0; which--)
11055 switch (TREE_CODE (pmop[which]))
11056 {
11057 case BIT_AND_EXPR:
11058 case BIT_IOR_EXPR:
11059 case BIT_XOR_EXPR:
11060 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11061 != INTEGER_CST)
11062 break;
11063 cst0 = TREE_OPERAND (pmop[which], 1);
11064 cst0 &= cst1;
11065 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11066 {
11067 if (cst0 != cst1)
11068 break;
11069 }
11070 else if (cst0 != 0)
11071 break;
11072 /* If C or D is of the form (A & N) where
11073 (N & M) == M, or of the form (A | N) or
11074 (A ^ N) where (N & M) == 0, replace it with A. */
11075 pmop[which] = TREE_OPERAND (pmop[which], 0);
11076 break;
11077 case INTEGER_CST:
11078 /* If C or D is a N where (N & M) == 0, it can be
11079 omitted (assumed 0). */
11080 if ((TREE_CODE (arg0) == PLUS_EXPR
11081 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11082 && (cst1 & pmop[which]) == 0)
11083 pmop[which] = NULL;
11084 break;
11085 default:
11086 break;
11087 }
11088
11089 /* Only build anything new if we optimized one or both arguments
11090 above. */
11091 if (pmop[0] != TREE_OPERAND (arg0, 0)
11092 || (TREE_CODE (arg0) != NEGATE_EXPR
11093 && pmop[1] != TREE_OPERAND (arg0, 1)))
11094 {
11095 tree utype = TREE_TYPE (arg0);
11096 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11097 {
11098 /* Perform the operations in a type that has defined
11099 overflow behavior. */
11100 utype = unsigned_type_for (TREE_TYPE (arg0));
11101 if (pmop[0] != NULL)
11102 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11103 if (pmop[1] != NULL)
11104 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11105 }
11106
11107 if (TREE_CODE (arg0) == NEGATE_EXPR)
11108 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11109 else if (TREE_CODE (arg0) == PLUS_EXPR)
11110 {
11111 if (pmop[0] != NULL && pmop[1] != NULL)
11112 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11113 pmop[0], pmop[1]);
11114 else if (pmop[0] != NULL)
11115 tem = pmop[0];
11116 else if (pmop[1] != NULL)
11117 tem = pmop[1];
11118 else
11119 return build_int_cst (type, 0);
11120 }
11121 else if (pmop[0] == NULL)
11122 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11123 else
11124 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11125 pmop[0], pmop[1]);
11126 /* TEM is now the new binary +, - or unary - replacement. */
11127 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11128 fold_convert_loc (loc, utype, arg1));
11129 return fold_convert_loc (loc, type, tem);
11130 }
11131 }
11132 }
11133
11134 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11135 if (t1 != NULL_TREE)
11136 return t1;
11137 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11138 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11139 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11140 {
11141 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11142
11143 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
11144 if (mask == -1)
11145 return
11146 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11147 }
11148
11149 /* If arg0 is derived from the address of an object or function, we may
11150 be able to fold this expression using the object or function's
11151 alignment. */
11152 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && tree_fits_uhwi_p (arg1))
11153 {
11154 unsigned HOST_WIDE_INT modulus, residue;
11155 unsigned HOST_WIDE_INT low = tree_to_uhwi (arg1);
11156
11157 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11158 integer_onep (arg1));
11159
11160 /* This works because modulus is a power of 2. If this weren't the
11161 case, we'd have to replace it by its greatest power-of-2
11162 divisor: modulus & -modulus. */
11163 if (low < modulus)
11164 return build_int_cst (type, residue & low);
11165 }
11166
11167 goto associate;
11168
11169 case RDIV_EXPR:
11170 /* Don't touch a floating-point divide by zero unless the mode
11171 of the constant can represent infinity. */
11172 if (TREE_CODE (arg1) == REAL_CST
11173 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11174 && real_zerop (arg1))
11175 return NULL_TREE;
11176
11177 /* (-A) / (-B) -> A / B */
11178 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11179 return fold_build2_loc (loc, RDIV_EXPR, type,
11180 TREE_OPERAND (arg0, 0),
11181 negate_expr (arg1));
11182 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11183 return fold_build2_loc (loc, RDIV_EXPR, type,
11184 negate_expr (arg0),
11185 TREE_OPERAND (arg1, 0));
11186
11187 /* Convert A/B/C to A/(B*C). */
11188 if (flag_reciprocal_math
11189 && TREE_CODE (arg0) == RDIV_EXPR)
11190 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11191 fold_build2_loc (loc, MULT_EXPR, type,
11192 TREE_OPERAND (arg0, 1), arg1));
11193
11194 /* Convert A/(B/C) to (A/B)*C. */
11195 if (flag_reciprocal_math
11196 && TREE_CODE (arg1) == RDIV_EXPR)
11197 return fold_build2_loc (loc, MULT_EXPR, type,
11198 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11199 TREE_OPERAND (arg1, 0)),
11200 TREE_OPERAND (arg1, 1));
11201
11202 /* Convert C1/(X*C2) into (C1/C2)/X. */
11203 if (flag_reciprocal_math
11204 && TREE_CODE (arg1) == MULT_EXPR
11205 && TREE_CODE (arg0) == REAL_CST
11206 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11207 {
11208 tree tem = const_binop (RDIV_EXPR, arg0,
11209 TREE_OPERAND (arg1, 1));
11210 if (tem)
11211 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11212 TREE_OPERAND (arg1, 0));
11213 }
11214
11215 if (flag_unsafe_math_optimizations)
11216 {
11217 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11218 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11219
11220 /* Optimize sin(x)/cos(x) as tan(x). */
11221 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11222 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11223 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11224 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11225 CALL_EXPR_ARG (arg1, 0), 0))
11226 {
11227 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11228
11229 if (tanfn != NULL_TREE)
11230 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11231 }
11232
11233 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11234 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11235 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11236 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11237 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11238 CALL_EXPR_ARG (arg1, 0), 0))
11239 {
11240 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11241
11242 if (tanfn != NULL_TREE)
11243 {
11244 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11245 CALL_EXPR_ARG (arg0, 0));
11246 return fold_build2_loc (loc, RDIV_EXPR, type,
11247 build_real (type, dconst1), tmp);
11248 }
11249 }
11250
11251 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11252 NaNs or Infinities. */
11253 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11254 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11255 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11256 {
11257 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11258 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11259
11260 if (! HONOR_NANS (arg00)
11261 && ! HONOR_INFINITIES (element_mode (arg00))
11262 && operand_equal_p (arg00, arg01, 0))
11263 {
11264 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11265
11266 if (cosfn != NULL_TREE)
11267 return build_call_expr_loc (loc, cosfn, 1, arg00);
11268 }
11269 }
11270
11271 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11272 NaNs or Infinities. */
11273 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11274 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11275 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11276 {
11277 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11278 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11279
11280 if (! HONOR_NANS (arg00)
11281 && ! HONOR_INFINITIES (element_mode (arg00))
11282 && operand_equal_p (arg00, arg01, 0))
11283 {
11284 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11285
11286 if (cosfn != NULL_TREE)
11287 {
11288 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11289 return fold_build2_loc (loc, RDIV_EXPR, type,
11290 build_real (type, dconst1),
11291 tmp);
11292 }
11293 }
11294 }
11295
11296 /* Optimize pow(x,c)/x as pow(x,c-1). */
11297 if (fcode0 == BUILT_IN_POW
11298 || fcode0 == BUILT_IN_POWF
11299 || fcode0 == BUILT_IN_POWL)
11300 {
11301 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11302 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11303 if (TREE_CODE (arg01) == REAL_CST
11304 && !TREE_OVERFLOW (arg01)
11305 && operand_equal_p (arg1, arg00, 0))
11306 {
11307 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11308 REAL_VALUE_TYPE c;
11309 tree arg;
11310
11311 c = TREE_REAL_CST (arg01);
11312 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11313 arg = build_real (type, c);
11314 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11315 }
11316 }
11317
11318 /* Optimize a/root(b/c) into a*root(c/b). */
11319 if (BUILTIN_ROOT_P (fcode1))
11320 {
11321 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11322
11323 if (TREE_CODE (rootarg) == RDIV_EXPR)
11324 {
11325 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11326 tree b = TREE_OPERAND (rootarg, 0);
11327 tree c = TREE_OPERAND (rootarg, 1);
11328
11329 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11330
11331 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11332 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11333 }
11334 }
11335
11336 /* Optimize x/expN(y) into x*expN(-y). */
11337 if (BUILTIN_EXPONENT_P (fcode1))
11338 {
11339 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11340 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11341 arg1 = build_call_expr_loc (loc,
11342 expfn, 1,
11343 fold_convert_loc (loc, type, arg));
11344 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11345 }
11346
11347 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11348 if (fcode1 == BUILT_IN_POW
11349 || fcode1 == BUILT_IN_POWF
11350 || fcode1 == BUILT_IN_POWL)
11351 {
11352 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11353 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11354 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11355 tree neg11 = fold_convert_loc (loc, type,
11356 negate_expr (arg11));
11357 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11358 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11359 }
11360 }
11361 return NULL_TREE;
11362
11363 case TRUNC_DIV_EXPR:
11364 /* Optimize (X & (-A)) / A where A is a power of 2,
11365 to X >> log2(A) */
11366 if (TREE_CODE (arg0) == BIT_AND_EXPR
11367 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11368 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11369 {
11370 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11371 arg1, TREE_OPERAND (arg0, 1));
11372 if (sum && integer_zerop (sum)) {
11373 tree pow2 = build_int_cst (integer_type_node,
11374 wi::exact_log2 (arg1));
11375 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11376 TREE_OPERAND (arg0, 0), pow2);
11377 }
11378 }
11379
11380 /* Fall through */
11381
11382 case FLOOR_DIV_EXPR:
11383 /* Simplify A / (B << N) where A and B are positive and B is
11384 a power of 2, to A >> (N + log2(B)). */
11385 strict_overflow_p = false;
11386 if (TREE_CODE (arg1) == LSHIFT_EXPR
11387 && (TYPE_UNSIGNED (type)
11388 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11389 {
11390 tree sval = TREE_OPERAND (arg1, 0);
11391 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11392 {
11393 tree sh_cnt = TREE_OPERAND (arg1, 1);
11394 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11395 wi::exact_log2 (sval));
11396
11397 if (strict_overflow_p)
11398 fold_overflow_warning (("assuming signed overflow does not "
11399 "occur when simplifying A / (B << N)"),
11400 WARN_STRICT_OVERFLOW_MISC);
11401
11402 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11403 sh_cnt, pow2);
11404 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11405 fold_convert_loc (loc, type, arg0), sh_cnt);
11406 }
11407 }
11408
11409 /* Fall through */
11410
11411 case ROUND_DIV_EXPR:
11412 case CEIL_DIV_EXPR:
11413 case EXACT_DIV_EXPR:
11414 if (integer_zerop (arg1))
11415 return NULL_TREE;
11416
11417 /* Convert -A / -B to A / B when the type is signed and overflow is
11418 undefined. */
11419 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11420 && TREE_CODE (arg0) == NEGATE_EXPR
11421 && negate_expr_p (arg1))
11422 {
11423 if (INTEGRAL_TYPE_P (type))
11424 fold_overflow_warning (("assuming signed overflow does not occur "
11425 "when distributing negation across "
11426 "division"),
11427 WARN_STRICT_OVERFLOW_MISC);
11428 return fold_build2_loc (loc, code, type,
11429 fold_convert_loc (loc, type,
11430 TREE_OPERAND (arg0, 0)),
11431 fold_convert_loc (loc, type,
11432 negate_expr (arg1)));
11433 }
11434 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11435 && TREE_CODE (arg1) == NEGATE_EXPR
11436 && negate_expr_p (arg0))
11437 {
11438 if (INTEGRAL_TYPE_P (type))
11439 fold_overflow_warning (("assuming signed overflow does not occur "
11440 "when distributing negation across "
11441 "division"),
11442 WARN_STRICT_OVERFLOW_MISC);
11443 return fold_build2_loc (loc, code, type,
11444 fold_convert_loc (loc, type,
11445 negate_expr (arg0)),
11446 fold_convert_loc (loc, type,
11447 TREE_OPERAND (arg1, 0)));
11448 }
11449
11450 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11451 operation, EXACT_DIV_EXPR.
11452
11453 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11454 At one time others generated faster code, it's not clear if they do
11455 after the last round to changes to the DIV code in expmed.c. */
11456 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11457 && multiple_of_p (type, arg0, arg1))
11458 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11459
11460 strict_overflow_p = false;
11461 if (TREE_CODE (arg1) == INTEGER_CST
11462 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11463 &strict_overflow_p)))
11464 {
11465 if (strict_overflow_p)
11466 fold_overflow_warning (("assuming signed overflow does not occur "
11467 "when simplifying division"),
11468 WARN_STRICT_OVERFLOW_MISC);
11469 return fold_convert_loc (loc, type, tem);
11470 }
11471
11472 return NULL_TREE;
11473
11474 case CEIL_MOD_EXPR:
11475 case FLOOR_MOD_EXPR:
11476 case ROUND_MOD_EXPR:
11477 case TRUNC_MOD_EXPR:
11478 strict_overflow_p = false;
11479 if (TREE_CODE (arg1) == INTEGER_CST
11480 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11481 &strict_overflow_p)))
11482 {
11483 if (strict_overflow_p)
11484 fold_overflow_warning (("assuming signed overflow does not occur "
11485 "when simplifying modulus"),
11486 WARN_STRICT_OVERFLOW_MISC);
11487 return fold_convert_loc (loc, type, tem);
11488 }
11489
11490 return NULL_TREE;
11491
11492 case LROTATE_EXPR:
11493 case RROTATE_EXPR:
11494 case RSHIFT_EXPR:
11495 case LSHIFT_EXPR:
11496 /* Since negative shift count is not well-defined,
11497 don't try to compute it in the compiler. */
11498 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11499 return NULL_TREE;
11500
11501 prec = element_precision (type);
11502
11503 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11504 if (TREE_CODE (op0) == code && tree_fits_uhwi_p (arg1)
11505 && tree_to_uhwi (arg1) < prec
11506 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11507 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11508 {
11509 unsigned int low = (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11510 + tree_to_uhwi (arg1));
11511
11512 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11513 being well defined. */
11514 if (low >= prec)
11515 {
11516 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11517 low = low % prec;
11518 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11519 return omit_one_operand_loc (loc, type, build_zero_cst (type),
11520 TREE_OPERAND (arg0, 0));
11521 else
11522 low = prec - 1;
11523 }
11524
11525 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11526 build_int_cst (TREE_TYPE (arg1), low));
11527 }
11528
11529 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11530 into x & ((unsigned)-1 >> c) for unsigned types. */
11531 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11532 || (TYPE_UNSIGNED (type)
11533 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11534 && tree_fits_uhwi_p (arg1)
11535 && tree_to_uhwi (arg1) < prec
11536 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11537 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11538 {
11539 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11540 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
11541 tree lshift;
11542 tree arg00;
11543
11544 if (low0 == low1)
11545 {
11546 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11547
11548 lshift = build_minus_one_cst (type);
11549 lshift = const_binop (code, lshift, arg1);
11550
11551 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11552 }
11553 }
11554
11555 /* If we have a rotate of a bit operation with the rotate count and
11556 the second operand of the bit operation both constant,
11557 permute the two operations. */
11558 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11559 && (TREE_CODE (arg0) == BIT_AND_EXPR
11560 || TREE_CODE (arg0) == BIT_IOR_EXPR
11561 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11562 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11563 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11564 fold_build2_loc (loc, code, type,
11565 TREE_OPERAND (arg0, 0), arg1),
11566 fold_build2_loc (loc, code, type,
11567 TREE_OPERAND (arg0, 1), arg1));
11568
11569 /* Two consecutive rotates adding up to the some integer
11570 multiple of the precision of the type can be ignored. */
11571 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11572 && TREE_CODE (arg0) == RROTATE_EXPR
11573 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11574 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
11575 prec) == 0)
11576 return TREE_OPERAND (arg0, 0);
11577
11578 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11579 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11580 if the latter can be further optimized. */
11581 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11582 && TREE_CODE (arg0) == BIT_AND_EXPR
11583 && TREE_CODE (arg1) == INTEGER_CST
11584 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11585 {
11586 tree mask = fold_build2_loc (loc, code, type,
11587 fold_convert_loc (loc, type,
11588 TREE_OPERAND (arg0, 1)),
11589 arg1);
11590 tree shift = fold_build2_loc (loc, code, type,
11591 fold_convert_loc (loc, type,
11592 TREE_OPERAND (arg0, 0)),
11593 arg1);
11594 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11595 if (tem)
11596 return tem;
11597 }
11598
11599 return NULL_TREE;
11600
11601 case MIN_EXPR:
11602 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11603 if (tem)
11604 return tem;
11605 goto associate;
11606
11607 case MAX_EXPR:
11608 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11609 if (tem)
11610 return tem;
11611 goto associate;
11612
11613 case TRUTH_ANDIF_EXPR:
11614 /* Note that the operands of this must be ints
11615 and their values must be 0 or 1.
11616 ("true" is a fixed value perhaps depending on the language.) */
11617 /* If first arg is constant zero, return it. */
11618 if (integer_zerop (arg0))
11619 return fold_convert_loc (loc, type, arg0);
11620 case TRUTH_AND_EXPR:
11621 /* If either arg is constant true, drop it. */
11622 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11623 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11624 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11625 /* Preserve sequence points. */
11626 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11627 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11628 /* If second arg is constant zero, result is zero, but first arg
11629 must be evaluated. */
11630 if (integer_zerop (arg1))
11631 return omit_one_operand_loc (loc, type, arg1, arg0);
11632 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11633 case will be handled here. */
11634 if (integer_zerop (arg0))
11635 return omit_one_operand_loc (loc, type, arg0, arg1);
11636
11637 /* !X && X is always false. */
11638 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11639 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11640 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11641 /* X && !X is always false. */
11642 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11643 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11644 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11645
11646 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11647 means A >= Y && A != MAX, but in this case we know that
11648 A < X <= MAX. */
11649
11650 if (!TREE_SIDE_EFFECTS (arg0)
11651 && !TREE_SIDE_EFFECTS (arg1))
11652 {
11653 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11654 if (tem && !operand_equal_p (tem, arg0, 0))
11655 return fold_build2_loc (loc, code, type, tem, arg1);
11656
11657 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11658 if (tem && !operand_equal_p (tem, arg1, 0))
11659 return fold_build2_loc (loc, code, type, arg0, tem);
11660 }
11661
11662 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11663 != NULL_TREE)
11664 return tem;
11665
11666 return NULL_TREE;
11667
11668 case TRUTH_ORIF_EXPR:
11669 /* Note that the operands of this must be ints
11670 and their values must be 0 or true.
11671 ("true" is a fixed value perhaps depending on the language.) */
11672 /* If first arg is constant true, return it. */
11673 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11674 return fold_convert_loc (loc, type, arg0);
11675 case TRUTH_OR_EXPR:
11676 /* If either arg is constant zero, drop it. */
11677 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11678 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11679 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11680 /* Preserve sequence points. */
11681 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11682 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11683 /* If second arg is constant true, result is true, but we must
11684 evaluate first arg. */
11685 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11686 return omit_one_operand_loc (loc, type, arg1, arg0);
11687 /* Likewise for first arg, but note this only occurs here for
11688 TRUTH_OR_EXPR. */
11689 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11690 return omit_one_operand_loc (loc, type, arg0, arg1);
11691
11692 /* !X || X is always true. */
11693 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11694 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11695 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11696 /* X || !X is always true. */
11697 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11698 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11699 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11700
11701 /* (X && !Y) || (!X && Y) is X ^ Y */
11702 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
11703 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
11704 {
11705 tree a0, a1, l0, l1, n0, n1;
11706
11707 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11708 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11709
11710 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11711 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11712
11713 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
11714 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
11715
11716 if ((operand_equal_p (n0, a0, 0)
11717 && operand_equal_p (n1, a1, 0))
11718 || (operand_equal_p (n0, a1, 0)
11719 && operand_equal_p (n1, a0, 0)))
11720 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
11721 }
11722
11723 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11724 != NULL_TREE)
11725 return tem;
11726
11727 return NULL_TREE;
11728
11729 case TRUTH_XOR_EXPR:
11730 /* If the second arg is constant zero, drop it. */
11731 if (integer_zerop (arg1))
11732 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11733 /* If the second arg is constant true, this is a logical inversion. */
11734 if (integer_onep (arg1))
11735 {
11736 tem = invert_truthvalue_loc (loc, arg0);
11737 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11738 }
11739 /* Identical arguments cancel to zero. */
11740 if (operand_equal_p (arg0, arg1, 0))
11741 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11742
11743 /* !X ^ X is always true. */
11744 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11745 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11746 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11747
11748 /* X ^ !X is always true. */
11749 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11750 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11751 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11752
11753 return NULL_TREE;
11754
11755 case EQ_EXPR:
11756 case NE_EXPR:
11757 STRIP_NOPS (arg0);
11758 STRIP_NOPS (arg1);
11759
11760 tem = fold_comparison (loc, code, type, op0, op1);
11761 if (tem != NULL_TREE)
11762 return tem;
11763
11764 /* bool_var != 0 becomes bool_var. */
11765 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11766 && code == NE_EXPR)
11767 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11768
11769 /* bool_var == 1 becomes bool_var. */
11770 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11771 && code == EQ_EXPR)
11772 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11773
11774 /* bool_var != 1 becomes !bool_var. */
11775 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11776 && code == NE_EXPR)
11777 return fold_convert_loc (loc, type,
11778 fold_build1_loc (loc, TRUTH_NOT_EXPR,
11779 TREE_TYPE (arg0), arg0));
11780
11781 /* bool_var == 0 becomes !bool_var. */
11782 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11783 && code == EQ_EXPR)
11784 return fold_convert_loc (loc, type,
11785 fold_build1_loc (loc, TRUTH_NOT_EXPR,
11786 TREE_TYPE (arg0), arg0));
11787
11788 /* !exp != 0 becomes !exp */
11789 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
11790 && code == NE_EXPR)
11791 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11792
11793 /* If this is an equality comparison of the address of two non-weak,
11794 unaliased symbols neither of which are extern (since we do not
11795 have access to attributes for externs), then we know the result. */
11796 if (TREE_CODE (arg0) == ADDR_EXPR
11797 && DECL_P (TREE_OPERAND (arg0, 0))
11798 && TREE_CODE (arg1) == ADDR_EXPR
11799 && DECL_P (TREE_OPERAND (arg1, 0)))
11800 {
11801 int equal;
11802
11803 if (decl_in_symtab_p (TREE_OPERAND (arg0, 0))
11804 && decl_in_symtab_p (TREE_OPERAND (arg1, 0)))
11805 equal = symtab_node::get_create (TREE_OPERAND (arg0, 0))
11806 ->equal_address_to (symtab_node::get_create
11807 (TREE_OPERAND (arg1, 0)));
11808 else
11809 equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11810 if (equal != 2)
11811 return constant_boolean_node (equal
11812 ? code == EQ_EXPR : code != EQ_EXPR,
11813 type);
11814 }
11815
11816 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11817 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11818 && TREE_CODE (arg1) == INTEGER_CST
11819 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11820 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11821 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
11822 fold_convert_loc (loc,
11823 TREE_TYPE (arg0),
11824 arg1),
11825 TREE_OPERAND (arg0, 1)));
11826
11827 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
11828 if ((TREE_CODE (arg0) == PLUS_EXPR
11829 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
11830 || TREE_CODE (arg0) == MINUS_EXPR)
11831 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
11832 0)),
11833 arg1, 0)
11834 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11835 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11836 {
11837 tree val = TREE_OPERAND (arg0, 1);
11838 return omit_two_operands_loc (loc, type,
11839 fold_build2_loc (loc, code, type,
11840 val,
11841 build_int_cst (TREE_TYPE (val),
11842 0)),
11843 TREE_OPERAND (arg0, 0), arg1);
11844 }
11845
11846 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
11847 if (TREE_CODE (arg0) == MINUS_EXPR
11848 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
11849 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
11850 1)),
11851 arg1, 0)
11852 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
11853 {
11854 return omit_two_operands_loc (loc, type,
11855 code == NE_EXPR
11856 ? boolean_true_node : boolean_false_node,
11857 TREE_OPERAND (arg0, 1), arg1);
11858 }
11859
11860 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11861 if (TREE_CODE (arg0) == ABS_EXPR
11862 && (integer_zerop (arg1) || real_zerop (arg1)))
11863 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
11864
11865 /* If this is an EQ or NE comparison with zero and ARG0 is
11866 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11867 two operations, but the latter can be done in one less insn
11868 on machines that have only two-operand insns or on which a
11869 constant cannot be the first operand. */
11870 if (TREE_CODE (arg0) == BIT_AND_EXPR
11871 && integer_zerop (arg1))
11872 {
11873 tree arg00 = TREE_OPERAND (arg0, 0);
11874 tree arg01 = TREE_OPERAND (arg0, 1);
11875 if (TREE_CODE (arg00) == LSHIFT_EXPR
11876 && integer_onep (TREE_OPERAND (arg00, 0)))
11877 {
11878 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
11879 arg01, TREE_OPERAND (arg00, 1));
11880 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11881 build_int_cst (TREE_TYPE (arg0), 1));
11882 return fold_build2_loc (loc, code, type,
11883 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
11884 arg1);
11885 }
11886 else if (TREE_CODE (arg01) == LSHIFT_EXPR
11887 && integer_onep (TREE_OPERAND (arg01, 0)))
11888 {
11889 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
11890 arg00, TREE_OPERAND (arg01, 1));
11891 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11892 build_int_cst (TREE_TYPE (arg0), 1));
11893 return fold_build2_loc (loc, code, type,
11894 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
11895 arg1);
11896 }
11897 }
11898
11899 /* If this is an NE or EQ comparison of zero against the result of a
11900 signed MOD operation whose second operand is a power of 2, make
11901 the MOD operation unsigned since it is simpler and equivalent. */
11902 if (integer_zerop (arg1)
11903 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11904 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11905 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11906 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11907 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11908 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11909 {
11910 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11911 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
11912 fold_convert_loc (loc, newtype,
11913 TREE_OPERAND (arg0, 0)),
11914 fold_convert_loc (loc, newtype,
11915 TREE_OPERAND (arg0, 1)));
11916
11917 return fold_build2_loc (loc, code, type, newmod,
11918 fold_convert_loc (loc, newtype, arg1));
11919 }
11920
11921 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11922 C1 is a valid shift constant, and C2 is a power of two, i.e.
11923 a single bit. */
11924 if (TREE_CODE (arg0) == BIT_AND_EXPR
11925 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11926 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11927 == INTEGER_CST
11928 && integer_pow2p (TREE_OPERAND (arg0, 1))
11929 && integer_zerop (arg1))
11930 {
11931 tree itype = TREE_TYPE (arg0);
11932 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11933 prec = TYPE_PRECISION (itype);
11934
11935 /* Check for a valid shift count. */
11936 if (wi::ltu_p (arg001, prec))
11937 {
11938 tree arg01 = TREE_OPERAND (arg0, 1);
11939 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11940 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11941 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11942 can be rewritten as (X & (C2 << C1)) != 0. */
11943 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11944 {
11945 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
11946 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
11947 return fold_build2_loc (loc, code, type, tem,
11948 fold_convert_loc (loc, itype, arg1));
11949 }
11950 /* Otherwise, for signed (arithmetic) shifts,
11951 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11952 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11953 else if (!TYPE_UNSIGNED (itype))
11954 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11955 arg000, build_int_cst (itype, 0));
11956 /* Otherwise, of unsigned (logical) shifts,
11957 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11958 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11959 else
11960 return omit_one_operand_loc (loc, type,
11961 code == EQ_EXPR ? integer_one_node
11962 : integer_zero_node,
11963 arg000);
11964 }
11965 }
11966
11967 /* If we have (A & C) == C where C is a power of 2, convert this into
11968 (A & C) != 0. Similarly for NE_EXPR. */
11969 if (TREE_CODE (arg0) == BIT_AND_EXPR
11970 && integer_pow2p (TREE_OPERAND (arg0, 1))
11971 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11972 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11973 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
11974 integer_zero_node));
11975
11976 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11977 bit, then fold the expression into A < 0 or A >= 0. */
11978 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
11979 if (tem)
11980 return tem;
11981
11982 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11983 Similarly for NE_EXPR. */
11984 if (TREE_CODE (arg0) == BIT_AND_EXPR
11985 && TREE_CODE (arg1) == INTEGER_CST
11986 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11987 {
11988 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
11989 TREE_TYPE (TREE_OPERAND (arg0, 1)),
11990 TREE_OPERAND (arg0, 1));
11991 tree dandnotc
11992 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
11993 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
11994 notc);
11995 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11996 if (integer_nonzerop (dandnotc))
11997 return omit_one_operand_loc (loc, type, rslt, arg0);
11998 }
11999
12000 /* If this is a comparison of a field, we may be able to simplify it. */
12001 if ((TREE_CODE (arg0) == COMPONENT_REF
12002 || TREE_CODE (arg0) == BIT_FIELD_REF)
12003 /* Handle the constant case even without -O
12004 to make sure the warnings are given. */
12005 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12006 {
12007 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12008 if (t1)
12009 return t1;
12010 }
12011
12012 /* Optimize comparisons of strlen vs zero to a compare of the
12013 first character of the string vs zero. To wit,
12014 strlen(ptr) == 0 => *ptr == 0
12015 strlen(ptr) != 0 => *ptr != 0
12016 Other cases should reduce to one of these two (or a constant)
12017 due to the return value of strlen being unsigned. */
12018 if (TREE_CODE (arg0) == CALL_EXPR
12019 && integer_zerop (arg1))
12020 {
12021 tree fndecl = get_callee_fndecl (arg0);
12022
12023 if (fndecl
12024 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12025 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12026 && call_expr_nargs (arg0) == 1
12027 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12028 {
12029 tree iref = build_fold_indirect_ref_loc (loc,
12030 CALL_EXPR_ARG (arg0, 0));
12031 return fold_build2_loc (loc, code, type, iref,
12032 build_int_cst (TREE_TYPE (iref), 0));
12033 }
12034 }
12035
12036 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12037 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12038 if (TREE_CODE (arg0) == RSHIFT_EXPR
12039 && integer_zerop (arg1)
12040 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12041 {
12042 tree arg00 = TREE_OPERAND (arg0, 0);
12043 tree arg01 = TREE_OPERAND (arg0, 1);
12044 tree itype = TREE_TYPE (arg00);
12045 if (wi::eq_p (arg01, element_precision (itype) - 1))
12046 {
12047 if (TYPE_UNSIGNED (itype))
12048 {
12049 itype = signed_type_for (itype);
12050 arg00 = fold_convert_loc (loc, itype, arg00);
12051 }
12052 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12053 type, arg00, build_zero_cst (itype));
12054 }
12055 }
12056
12057 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12058 (X & C) == 0 when C is a single bit. */
12059 if (TREE_CODE (arg0) == BIT_AND_EXPR
12060 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12061 && integer_zerop (arg1)
12062 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12063 {
12064 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12065 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12066 TREE_OPERAND (arg0, 1));
12067 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12068 type, tem,
12069 fold_convert_loc (loc, TREE_TYPE (arg0),
12070 arg1));
12071 }
12072
12073 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12074 constant C is a power of two, i.e. a single bit. */
12075 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12076 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12077 && integer_zerop (arg1)
12078 && integer_pow2p (TREE_OPERAND (arg0, 1))
12079 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12080 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12081 {
12082 tree arg00 = TREE_OPERAND (arg0, 0);
12083 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12084 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12085 }
12086
12087 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12088 when is C is a power of two, i.e. a single bit. */
12089 if (TREE_CODE (arg0) == BIT_AND_EXPR
12090 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12091 && integer_zerop (arg1)
12092 && integer_pow2p (TREE_OPERAND (arg0, 1))
12093 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12094 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12095 {
12096 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12097 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12098 arg000, TREE_OPERAND (arg0, 1));
12099 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12100 tem, build_int_cst (TREE_TYPE (tem), 0));
12101 }
12102
12103 if (integer_zerop (arg1)
12104 && tree_expr_nonzero_p (arg0))
12105 {
12106 tree res = constant_boolean_node (code==NE_EXPR, type);
12107 return omit_one_operand_loc (loc, type, res, arg0);
12108 }
12109
12110 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12111 if (TREE_CODE (arg0) == BIT_AND_EXPR
12112 && TREE_CODE (arg1) == BIT_AND_EXPR)
12113 {
12114 tree arg00 = TREE_OPERAND (arg0, 0);
12115 tree arg01 = TREE_OPERAND (arg0, 1);
12116 tree arg10 = TREE_OPERAND (arg1, 0);
12117 tree arg11 = TREE_OPERAND (arg1, 1);
12118 tree itype = TREE_TYPE (arg0);
12119
12120 if (operand_equal_p (arg01, arg11, 0))
12121 return fold_build2_loc (loc, code, type,
12122 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12123 fold_build2_loc (loc,
12124 BIT_XOR_EXPR, itype,
12125 arg00, arg10),
12126 arg01),
12127 build_zero_cst (itype));
12128
12129 if (operand_equal_p (arg01, arg10, 0))
12130 return fold_build2_loc (loc, code, type,
12131 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12132 fold_build2_loc (loc,
12133 BIT_XOR_EXPR, itype,
12134 arg00, arg11),
12135 arg01),
12136 build_zero_cst (itype));
12137
12138 if (operand_equal_p (arg00, arg11, 0))
12139 return fold_build2_loc (loc, code, type,
12140 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12141 fold_build2_loc (loc,
12142 BIT_XOR_EXPR, itype,
12143 arg01, arg10),
12144 arg00),
12145 build_zero_cst (itype));
12146
12147 if (operand_equal_p (arg00, arg10, 0))
12148 return fold_build2_loc (loc, code, type,
12149 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12150 fold_build2_loc (loc,
12151 BIT_XOR_EXPR, itype,
12152 arg01, arg11),
12153 arg00),
12154 build_zero_cst (itype));
12155 }
12156
12157 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12158 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12159 {
12160 tree arg00 = TREE_OPERAND (arg0, 0);
12161 tree arg01 = TREE_OPERAND (arg0, 1);
12162 tree arg10 = TREE_OPERAND (arg1, 0);
12163 tree arg11 = TREE_OPERAND (arg1, 1);
12164 tree itype = TREE_TYPE (arg0);
12165
12166 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12167 operand_equal_p guarantees no side-effects so we don't need
12168 to use omit_one_operand on Z. */
12169 if (operand_equal_p (arg01, arg11, 0))
12170 return fold_build2_loc (loc, code, type, arg00,
12171 fold_convert_loc (loc, TREE_TYPE (arg00),
12172 arg10));
12173 if (operand_equal_p (arg01, arg10, 0))
12174 return fold_build2_loc (loc, code, type, arg00,
12175 fold_convert_loc (loc, TREE_TYPE (arg00),
12176 arg11));
12177 if (operand_equal_p (arg00, arg11, 0))
12178 return fold_build2_loc (loc, code, type, arg01,
12179 fold_convert_loc (loc, TREE_TYPE (arg01),
12180 arg10));
12181 if (operand_equal_p (arg00, arg10, 0))
12182 return fold_build2_loc (loc, code, type, arg01,
12183 fold_convert_loc (loc, TREE_TYPE (arg01),
12184 arg11));
12185
12186 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12187 if (TREE_CODE (arg01) == INTEGER_CST
12188 && TREE_CODE (arg11) == INTEGER_CST)
12189 {
12190 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12191 fold_convert_loc (loc, itype, arg11));
12192 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12193 return fold_build2_loc (loc, code, type, tem,
12194 fold_convert_loc (loc, itype, arg10));
12195 }
12196 }
12197
12198 /* Attempt to simplify equality/inequality comparisons of complex
12199 values. Only lower the comparison if the result is known or
12200 can be simplified to a single scalar comparison. */
12201 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12202 || TREE_CODE (arg0) == COMPLEX_CST)
12203 && (TREE_CODE (arg1) == COMPLEX_EXPR
12204 || TREE_CODE (arg1) == COMPLEX_CST))
12205 {
12206 tree real0, imag0, real1, imag1;
12207 tree rcond, icond;
12208
12209 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12210 {
12211 real0 = TREE_OPERAND (arg0, 0);
12212 imag0 = TREE_OPERAND (arg0, 1);
12213 }
12214 else
12215 {
12216 real0 = TREE_REALPART (arg0);
12217 imag0 = TREE_IMAGPART (arg0);
12218 }
12219
12220 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12221 {
12222 real1 = TREE_OPERAND (arg1, 0);
12223 imag1 = TREE_OPERAND (arg1, 1);
12224 }
12225 else
12226 {
12227 real1 = TREE_REALPART (arg1);
12228 imag1 = TREE_IMAGPART (arg1);
12229 }
12230
12231 rcond = fold_binary_loc (loc, code, type, real0, real1);
12232 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12233 {
12234 if (integer_zerop (rcond))
12235 {
12236 if (code == EQ_EXPR)
12237 return omit_two_operands_loc (loc, type, boolean_false_node,
12238 imag0, imag1);
12239 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12240 }
12241 else
12242 {
12243 if (code == NE_EXPR)
12244 return omit_two_operands_loc (loc, type, boolean_true_node,
12245 imag0, imag1);
12246 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12247 }
12248 }
12249
12250 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12251 if (icond && TREE_CODE (icond) == INTEGER_CST)
12252 {
12253 if (integer_zerop (icond))
12254 {
12255 if (code == EQ_EXPR)
12256 return omit_two_operands_loc (loc, type, boolean_false_node,
12257 real0, real1);
12258 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12259 }
12260 else
12261 {
12262 if (code == NE_EXPR)
12263 return omit_two_operands_loc (loc, type, boolean_true_node,
12264 real0, real1);
12265 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12266 }
12267 }
12268 }
12269
12270 return NULL_TREE;
12271
12272 case LT_EXPR:
12273 case GT_EXPR:
12274 case LE_EXPR:
12275 case GE_EXPR:
12276 tem = fold_comparison (loc, code, type, op0, op1);
12277 if (tem != NULL_TREE)
12278 return tem;
12279
12280 /* Transform comparisons of the form X +- C CMP X. */
12281 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12282 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12283 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12284 && !HONOR_SNANS (arg0))
12285 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12286 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12287 {
12288 tree arg01 = TREE_OPERAND (arg0, 1);
12289 enum tree_code code0 = TREE_CODE (arg0);
12290 int is_positive;
12291
12292 if (TREE_CODE (arg01) == REAL_CST)
12293 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12294 else
12295 is_positive = tree_int_cst_sgn (arg01);
12296
12297 /* (X - c) > X becomes false. */
12298 if (code == GT_EXPR
12299 && ((code0 == MINUS_EXPR && is_positive >= 0)
12300 || (code0 == PLUS_EXPR && is_positive <= 0)))
12301 {
12302 if (TREE_CODE (arg01) == INTEGER_CST
12303 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12304 fold_overflow_warning (("assuming signed overflow does not "
12305 "occur when assuming that (X - c) > X "
12306 "is always false"),
12307 WARN_STRICT_OVERFLOW_ALL);
12308 return constant_boolean_node (0, type);
12309 }
12310
12311 /* Likewise (X + c) < X becomes false. */
12312 if (code == LT_EXPR
12313 && ((code0 == PLUS_EXPR && is_positive >= 0)
12314 || (code0 == MINUS_EXPR && is_positive <= 0)))
12315 {
12316 if (TREE_CODE (arg01) == INTEGER_CST
12317 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12318 fold_overflow_warning (("assuming signed overflow does not "
12319 "occur when assuming that "
12320 "(X + c) < X is always false"),
12321 WARN_STRICT_OVERFLOW_ALL);
12322 return constant_boolean_node (0, type);
12323 }
12324
12325 /* Convert (X - c) <= X to true. */
12326 if (!HONOR_NANS (arg1)
12327 && code == LE_EXPR
12328 && ((code0 == MINUS_EXPR && is_positive >= 0)
12329 || (code0 == PLUS_EXPR && is_positive <= 0)))
12330 {
12331 if (TREE_CODE (arg01) == INTEGER_CST
12332 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12333 fold_overflow_warning (("assuming signed overflow does not "
12334 "occur when assuming that "
12335 "(X - c) <= X is always true"),
12336 WARN_STRICT_OVERFLOW_ALL);
12337 return constant_boolean_node (1, type);
12338 }
12339
12340 /* Convert (X + c) >= X to true. */
12341 if (!HONOR_NANS (arg1)
12342 && code == GE_EXPR
12343 && ((code0 == PLUS_EXPR && is_positive >= 0)
12344 || (code0 == MINUS_EXPR && is_positive <= 0)))
12345 {
12346 if (TREE_CODE (arg01) == INTEGER_CST
12347 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12348 fold_overflow_warning (("assuming signed overflow does not "
12349 "occur when assuming that "
12350 "(X + c) >= X is always true"),
12351 WARN_STRICT_OVERFLOW_ALL);
12352 return constant_boolean_node (1, type);
12353 }
12354
12355 if (TREE_CODE (arg01) == INTEGER_CST)
12356 {
12357 /* Convert X + c > X and X - c < X to true for integers. */
12358 if (code == GT_EXPR
12359 && ((code0 == PLUS_EXPR && is_positive > 0)
12360 || (code0 == MINUS_EXPR && is_positive < 0)))
12361 {
12362 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12363 fold_overflow_warning (("assuming signed overflow does "
12364 "not occur when assuming that "
12365 "(X + c) > X is always true"),
12366 WARN_STRICT_OVERFLOW_ALL);
12367 return constant_boolean_node (1, type);
12368 }
12369
12370 if (code == LT_EXPR
12371 && ((code0 == MINUS_EXPR && is_positive > 0)
12372 || (code0 == PLUS_EXPR && is_positive < 0)))
12373 {
12374 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12375 fold_overflow_warning (("assuming signed overflow does "
12376 "not occur when assuming that "
12377 "(X - c) < X is always true"),
12378 WARN_STRICT_OVERFLOW_ALL);
12379 return constant_boolean_node (1, type);
12380 }
12381
12382 /* Convert X + c <= X and X - c >= X to false for integers. */
12383 if (code == LE_EXPR
12384 && ((code0 == PLUS_EXPR && is_positive > 0)
12385 || (code0 == MINUS_EXPR && is_positive < 0)))
12386 {
12387 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12388 fold_overflow_warning (("assuming signed overflow does "
12389 "not occur when assuming that "
12390 "(X + c) <= X is always false"),
12391 WARN_STRICT_OVERFLOW_ALL);
12392 return constant_boolean_node (0, type);
12393 }
12394
12395 if (code == GE_EXPR
12396 && ((code0 == MINUS_EXPR && is_positive > 0)
12397 || (code0 == PLUS_EXPR && is_positive < 0)))
12398 {
12399 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12400 fold_overflow_warning (("assuming signed overflow does "
12401 "not occur when assuming that "
12402 "(X - c) >= X is always false"),
12403 WARN_STRICT_OVERFLOW_ALL);
12404 return constant_boolean_node (0, type);
12405 }
12406 }
12407 }
12408
12409 /* Comparisons with the highest or lowest possible integer of
12410 the specified precision will have known values. */
12411 {
12412 tree arg1_type = TREE_TYPE (arg1);
12413 unsigned int prec = TYPE_PRECISION (arg1_type);
12414
12415 if (TREE_CODE (arg1) == INTEGER_CST
12416 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12417 {
12418 wide_int max = wi::max_value (arg1_type);
12419 wide_int signed_max = wi::max_value (prec, SIGNED);
12420 wide_int min = wi::min_value (arg1_type);
12421
12422 if (wi::eq_p (arg1, max))
12423 switch (code)
12424 {
12425 case GT_EXPR:
12426 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12427
12428 case GE_EXPR:
12429 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12430
12431 case LE_EXPR:
12432 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12433
12434 case LT_EXPR:
12435 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12436
12437 /* The GE_EXPR and LT_EXPR cases above are not normally
12438 reached because of previous transformations. */
12439
12440 default:
12441 break;
12442 }
12443 else if (wi::eq_p (arg1, max - 1))
12444 switch (code)
12445 {
12446 case GT_EXPR:
12447 arg1 = const_binop (PLUS_EXPR, arg1,
12448 build_int_cst (TREE_TYPE (arg1), 1));
12449 return fold_build2_loc (loc, EQ_EXPR, type,
12450 fold_convert_loc (loc,
12451 TREE_TYPE (arg1), arg0),
12452 arg1);
12453 case LE_EXPR:
12454 arg1 = const_binop (PLUS_EXPR, arg1,
12455 build_int_cst (TREE_TYPE (arg1), 1));
12456 return fold_build2_loc (loc, NE_EXPR, type,
12457 fold_convert_loc (loc, TREE_TYPE (arg1),
12458 arg0),
12459 arg1);
12460 default:
12461 break;
12462 }
12463 else if (wi::eq_p (arg1, min))
12464 switch (code)
12465 {
12466 case LT_EXPR:
12467 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12468
12469 case LE_EXPR:
12470 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12471
12472 case GE_EXPR:
12473 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12474
12475 case GT_EXPR:
12476 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12477
12478 default:
12479 break;
12480 }
12481 else if (wi::eq_p (arg1, min + 1))
12482 switch (code)
12483 {
12484 case GE_EXPR:
12485 arg1 = const_binop (MINUS_EXPR, arg1,
12486 build_int_cst (TREE_TYPE (arg1), 1));
12487 return fold_build2_loc (loc, NE_EXPR, type,
12488 fold_convert_loc (loc,
12489 TREE_TYPE (arg1), arg0),
12490 arg1);
12491 case LT_EXPR:
12492 arg1 = const_binop (MINUS_EXPR, arg1,
12493 build_int_cst (TREE_TYPE (arg1), 1));
12494 return fold_build2_loc (loc, EQ_EXPR, type,
12495 fold_convert_loc (loc, TREE_TYPE (arg1),
12496 arg0),
12497 arg1);
12498 default:
12499 break;
12500 }
12501
12502 else if (wi::eq_p (arg1, signed_max)
12503 && TYPE_UNSIGNED (arg1_type)
12504 /* We will flip the signedness of the comparison operator
12505 associated with the mode of arg1, so the sign bit is
12506 specified by this mode. Check that arg1 is the signed
12507 max associated with this sign bit. */
12508 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
12509 /* signed_type does not work on pointer types. */
12510 && INTEGRAL_TYPE_P (arg1_type))
12511 {
12512 /* The following case also applies to X < signed_max+1
12513 and X >= signed_max+1 because previous transformations. */
12514 if (code == LE_EXPR || code == GT_EXPR)
12515 {
12516 tree st = signed_type_for (arg1_type);
12517 return fold_build2_loc (loc,
12518 code == LE_EXPR ? GE_EXPR : LT_EXPR,
12519 type, fold_convert_loc (loc, st, arg0),
12520 build_int_cst (st, 0));
12521 }
12522 }
12523 }
12524 }
12525
12526 /* If we are comparing an ABS_EXPR with a constant, we can
12527 convert all the cases into explicit comparisons, but they may
12528 well not be faster than doing the ABS and one comparison.
12529 But ABS (X) <= C is a range comparison, which becomes a subtraction
12530 and a comparison, and is probably faster. */
12531 if (code == LE_EXPR
12532 && TREE_CODE (arg1) == INTEGER_CST
12533 && TREE_CODE (arg0) == ABS_EXPR
12534 && ! TREE_SIDE_EFFECTS (arg0)
12535 && (0 != (tem = negate_expr (arg1)))
12536 && TREE_CODE (tem) == INTEGER_CST
12537 && !TREE_OVERFLOW (tem))
12538 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12539 build2 (GE_EXPR, type,
12540 TREE_OPERAND (arg0, 0), tem),
12541 build2 (LE_EXPR, type,
12542 TREE_OPERAND (arg0, 0), arg1));
12543
12544 /* Convert ABS_EXPR<x> >= 0 to true. */
12545 strict_overflow_p = false;
12546 if (code == GE_EXPR
12547 && (integer_zerop (arg1)
12548 || (! HONOR_NANS (arg0)
12549 && real_zerop (arg1)))
12550 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12551 {
12552 if (strict_overflow_p)
12553 fold_overflow_warning (("assuming signed overflow does not occur "
12554 "when simplifying comparison of "
12555 "absolute value and zero"),
12556 WARN_STRICT_OVERFLOW_CONDITIONAL);
12557 return omit_one_operand_loc (loc, type,
12558 constant_boolean_node (true, type),
12559 arg0);
12560 }
12561
12562 /* Convert ABS_EXPR<x> < 0 to false. */
12563 strict_overflow_p = false;
12564 if (code == LT_EXPR
12565 && (integer_zerop (arg1) || real_zerop (arg1))
12566 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12567 {
12568 if (strict_overflow_p)
12569 fold_overflow_warning (("assuming signed overflow does not occur "
12570 "when simplifying comparison of "
12571 "absolute value and zero"),
12572 WARN_STRICT_OVERFLOW_CONDITIONAL);
12573 return omit_one_operand_loc (loc, type,
12574 constant_boolean_node (false, type),
12575 arg0);
12576 }
12577
12578 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12579 and similarly for >= into !=. */
12580 if ((code == LT_EXPR || code == GE_EXPR)
12581 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12582 && TREE_CODE (arg1) == LSHIFT_EXPR
12583 && integer_onep (TREE_OPERAND (arg1, 0)))
12584 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12585 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12586 TREE_OPERAND (arg1, 1)),
12587 build_zero_cst (TREE_TYPE (arg0)));
12588
12589 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12590 otherwise Y might be >= # of bits in X's type and thus e.g.
12591 (unsigned char) (1 << Y) for Y 15 might be 0.
12592 If the cast is widening, then 1 << Y should have unsigned type,
12593 otherwise if Y is number of bits in the signed shift type minus 1,
12594 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12595 31 might be 0xffffffff80000000. */
12596 if ((code == LT_EXPR || code == GE_EXPR)
12597 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12598 && CONVERT_EXPR_P (arg1)
12599 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12600 && (element_precision (TREE_TYPE (arg1))
12601 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12602 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12603 || (element_precision (TREE_TYPE (arg1))
12604 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12605 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12606 {
12607 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12608 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12609 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12610 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12611 build_zero_cst (TREE_TYPE (arg0)));
12612 }
12613
12614 return NULL_TREE;
12615
12616 case UNORDERED_EXPR:
12617 case ORDERED_EXPR:
12618 case UNLT_EXPR:
12619 case UNLE_EXPR:
12620 case UNGT_EXPR:
12621 case UNGE_EXPR:
12622 case UNEQ_EXPR:
12623 case LTGT_EXPR:
12624 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12625 {
12626 t1 = fold_relational_const (code, type, arg0, arg1);
12627 if (t1 != NULL_TREE)
12628 return t1;
12629 }
12630
12631 /* If the first operand is NaN, the result is constant. */
12632 if (TREE_CODE (arg0) == REAL_CST
12633 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12634 && (code != LTGT_EXPR || ! flag_trapping_math))
12635 {
12636 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12637 ? integer_zero_node
12638 : integer_one_node;
12639 return omit_one_operand_loc (loc, type, t1, arg1);
12640 }
12641
12642 /* If the second operand is NaN, the result is constant. */
12643 if (TREE_CODE (arg1) == REAL_CST
12644 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12645 && (code != LTGT_EXPR || ! flag_trapping_math))
12646 {
12647 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12648 ? integer_zero_node
12649 : integer_one_node;
12650 return omit_one_operand_loc (loc, type, t1, arg0);
12651 }
12652
12653 /* Simplify unordered comparison of something with itself. */
12654 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12655 && operand_equal_p (arg0, arg1, 0))
12656 return constant_boolean_node (1, type);
12657
12658 if (code == LTGT_EXPR
12659 && !flag_trapping_math
12660 && operand_equal_p (arg0, arg1, 0))
12661 return constant_boolean_node (0, type);
12662
12663 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12664 {
12665 tree targ0 = strip_float_extensions (arg0);
12666 tree targ1 = strip_float_extensions (arg1);
12667 tree newtype = TREE_TYPE (targ0);
12668
12669 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12670 newtype = TREE_TYPE (targ1);
12671
12672 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12673 return fold_build2_loc (loc, code, type,
12674 fold_convert_loc (loc, newtype, targ0),
12675 fold_convert_loc (loc, newtype, targ1));
12676 }
12677
12678 return NULL_TREE;
12679
12680 case COMPOUND_EXPR:
12681 /* When pedantic, a compound expression can be neither an lvalue
12682 nor an integer constant expression. */
12683 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12684 return NULL_TREE;
12685 /* Don't let (0, 0) be null pointer constant. */
12686 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12687 : fold_convert_loc (loc, type, arg1);
12688 return pedantic_non_lvalue_loc (loc, tem);
12689
12690 case ASSERT_EXPR:
12691 /* An ASSERT_EXPR should never be passed to fold_binary. */
12692 gcc_unreachable ();
12693
12694 default:
12695 return NULL_TREE;
12696 } /* switch (code) */
12697 }
12698
12699 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12700 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
12701 of GOTO_EXPR. */
12702
12703 static tree
12704 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
12705 {
12706 switch (TREE_CODE (*tp))
12707 {
12708 case LABEL_EXPR:
12709 return *tp;
12710
12711 case GOTO_EXPR:
12712 *walk_subtrees = 0;
12713
12714 /* ... fall through ... */
12715
12716 default:
12717 return NULL_TREE;
12718 }
12719 }
12720
12721 /* Return whether the sub-tree ST contains a label which is accessible from
12722 outside the sub-tree. */
12723
12724 static bool
12725 contains_label_p (tree st)
12726 {
12727 return
12728 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
12729 }
12730
12731 /* Fold a ternary expression of code CODE and type TYPE with operands
12732 OP0, OP1, and OP2. Return the folded expression if folding is
12733 successful. Otherwise, return NULL_TREE. */
12734
12735 tree
12736 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12737 tree op0, tree op1, tree op2)
12738 {
12739 tree tem;
12740 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12741 enum tree_code_class kind = TREE_CODE_CLASS (code);
12742
12743 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12744 && TREE_CODE_LENGTH (code) == 3);
12745
12746 /* If this is a commutative operation, and OP0 is a constant, move it
12747 to OP1 to reduce the number of tests below. */
12748 if (commutative_ternary_tree_code (code)
12749 && tree_swap_operands_p (op0, op1, true))
12750 return fold_build3_loc (loc, code, type, op1, op0, op2);
12751
12752 tem = generic_simplify (loc, code, type, op0, op1, op2);
12753 if (tem)
12754 return tem;
12755
12756 /* Strip any conversions that don't change the mode. This is safe
12757 for every expression, except for a comparison expression because
12758 its signedness is derived from its operands. So, in the latter
12759 case, only strip conversions that don't change the signedness.
12760
12761 Note that this is done as an internal manipulation within the
12762 constant folder, in order to find the simplest representation of
12763 the arguments so that their form can be studied. In any cases,
12764 the appropriate type conversions should be put back in the tree
12765 that will get out of the constant folder. */
12766 if (op0)
12767 {
12768 arg0 = op0;
12769 STRIP_NOPS (arg0);
12770 }
12771
12772 if (op1)
12773 {
12774 arg1 = op1;
12775 STRIP_NOPS (arg1);
12776 }
12777
12778 if (op2)
12779 {
12780 arg2 = op2;
12781 STRIP_NOPS (arg2);
12782 }
12783
12784 switch (code)
12785 {
12786 case COMPONENT_REF:
12787 if (TREE_CODE (arg0) == CONSTRUCTOR
12788 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12789 {
12790 unsigned HOST_WIDE_INT idx;
12791 tree field, value;
12792 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12793 if (field == arg1)
12794 return value;
12795 }
12796 return NULL_TREE;
12797
12798 case COND_EXPR:
12799 case VEC_COND_EXPR:
12800 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12801 so all simple results must be passed through pedantic_non_lvalue. */
12802 if (TREE_CODE (arg0) == INTEGER_CST)
12803 {
12804 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12805 tem = integer_zerop (arg0) ? op2 : op1;
12806 /* Only optimize constant conditions when the selected branch
12807 has the same type as the COND_EXPR. This avoids optimizing
12808 away "c ? x : throw", where the throw has a void type.
12809 Avoid throwing away that operand which contains label. */
12810 if ((!TREE_SIDE_EFFECTS (unused_op)
12811 || !contains_label_p (unused_op))
12812 && (! VOID_TYPE_P (TREE_TYPE (tem))
12813 || VOID_TYPE_P (type)))
12814 return pedantic_non_lvalue_loc (loc, tem);
12815 return NULL_TREE;
12816 }
12817 else if (TREE_CODE (arg0) == VECTOR_CST)
12818 {
12819 if ((TREE_CODE (arg1) == VECTOR_CST
12820 || TREE_CODE (arg1) == CONSTRUCTOR)
12821 && (TREE_CODE (arg2) == VECTOR_CST
12822 || TREE_CODE (arg2) == CONSTRUCTOR))
12823 {
12824 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
12825 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
12826 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
12827 for (i = 0; i < nelts; i++)
12828 {
12829 tree val = VECTOR_CST_ELT (arg0, i);
12830 if (integer_all_onesp (val))
12831 sel[i] = i;
12832 else if (integer_zerop (val))
12833 sel[i] = nelts + i;
12834 else /* Currently unreachable. */
12835 return NULL_TREE;
12836 }
12837 tree t = fold_vec_perm (type, arg1, arg2, sel);
12838 if (t != NULL_TREE)
12839 return t;
12840 }
12841 }
12842
12843 /* If we have A op B ? A : C, we may be able to convert this to a
12844 simpler expression, depending on the operation and the values
12845 of B and C. Signed zeros prevent all of these transformations,
12846 for reasons given above each one.
12847
12848 Also try swapping the arguments and inverting the conditional. */
12849 if (COMPARISON_CLASS_P (arg0)
12850 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12851 arg1, TREE_OPERAND (arg0, 1))
12852 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
12853 {
12854 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
12855 if (tem)
12856 return tem;
12857 }
12858
12859 if (COMPARISON_CLASS_P (arg0)
12860 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12861 op2,
12862 TREE_OPERAND (arg0, 1))
12863 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
12864 {
12865 location_t loc0 = expr_location_or (arg0, loc);
12866 tem = fold_invert_truthvalue (loc0, arg0);
12867 if (tem && COMPARISON_CLASS_P (tem))
12868 {
12869 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
12870 if (tem)
12871 return tem;
12872 }
12873 }
12874
12875 /* If the second operand is simpler than the third, swap them
12876 since that produces better jump optimization results. */
12877 if (truth_value_p (TREE_CODE (arg0))
12878 && tree_swap_operands_p (op1, op2, false))
12879 {
12880 location_t loc0 = expr_location_or (arg0, loc);
12881 /* See if this can be inverted. If it can't, possibly because
12882 it was a floating-point inequality comparison, don't do
12883 anything. */
12884 tem = fold_invert_truthvalue (loc0, arg0);
12885 if (tem)
12886 return fold_build3_loc (loc, code, type, tem, op2, op1);
12887 }
12888
12889 /* Convert A ? 1 : 0 to simply A. */
12890 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12891 : (integer_onep (op1)
12892 && !VECTOR_TYPE_P (type)))
12893 && integer_zerop (op2)
12894 /* If we try to convert OP0 to our type, the
12895 call to fold will try to move the conversion inside
12896 a COND, which will recurse. In that case, the COND_EXPR
12897 is probably the best choice, so leave it alone. */
12898 && type == TREE_TYPE (arg0))
12899 return pedantic_non_lvalue_loc (loc, arg0);
12900
12901 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12902 over COND_EXPR in cases such as floating point comparisons. */
12903 if (integer_zerop (op1)
12904 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
12905 : (integer_onep (op2)
12906 && !VECTOR_TYPE_P (type)))
12907 && truth_value_p (TREE_CODE (arg0)))
12908 return pedantic_non_lvalue_loc (loc,
12909 fold_convert_loc (loc, type,
12910 invert_truthvalue_loc (loc,
12911 arg0)));
12912
12913 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12914 if (TREE_CODE (arg0) == LT_EXPR
12915 && integer_zerop (TREE_OPERAND (arg0, 1))
12916 && integer_zerop (op2)
12917 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12918 {
12919 /* sign_bit_p looks through both zero and sign extensions,
12920 but for this optimization only sign extensions are
12921 usable. */
12922 tree tem2 = TREE_OPERAND (arg0, 0);
12923 while (tem != tem2)
12924 {
12925 if (TREE_CODE (tem2) != NOP_EXPR
12926 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12927 {
12928 tem = NULL_TREE;
12929 break;
12930 }
12931 tem2 = TREE_OPERAND (tem2, 0);
12932 }
12933 /* sign_bit_p only checks ARG1 bits within A's precision.
12934 If <sign bit of A> has wider type than A, bits outside
12935 of A's precision in <sign bit of A> need to be checked.
12936 If they are all 0, this optimization needs to be done
12937 in unsigned A's type, if they are all 1 in signed A's type,
12938 otherwise this can't be done. */
12939 if (tem
12940 && TYPE_PRECISION (TREE_TYPE (tem))
12941 < TYPE_PRECISION (TREE_TYPE (arg1))
12942 && TYPE_PRECISION (TREE_TYPE (tem))
12943 < TYPE_PRECISION (type))
12944 {
12945 int inner_width, outer_width;
12946 tree tem_type;
12947
12948 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12949 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12950 if (outer_width > TYPE_PRECISION (type))
12951 outer_width = TYPE_PRECISION (type);
12952
12953 wide_int mask = wi::shifted_mask
12954 (inner_width, outer_width - inner_width, false,
12955 TYPE_PRECISION (TREE_TYPE (arg1)));
12956
12957 wide_int common = mask & arg1;
12958 if (common == mask)
12959 {
12960 tem_type = signed_type_for (TREE_TYPE (tem));
12961 tem = fold_convert_loc (loc, tem_type, tem);
12962 }
12963 else if (common == 0)
12964 {
12965 tem_type = unsigned_type_for (TREE_TYPE (tem));
12966 tem = fold_convert_loc (loc, tem_type, tem);
12967 }
12968 else
12969 tem = NULL;
12970 }
12971
12972 if (tem)
12973 return
12974 fold_convert_loc (loc, type,
12975 fold_build2_loc (loc, BIT_AND_EXPR,
12976 TREE_TYPE (tem), tem,
12977 fold_convert_loc (loc,
12978 TREE_TYPE (tem),
12979 arg1)));
12980 }
12981
12982 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12983 already handled above. */
12984 if (TREE_CODE (arg0) == BIT_AND_EXPR
12985 && integer_onep (TREE_OPERAND (arg0, 1))
12986 && integer_zerop (op2)
12987 && integer_pow2p (arg1))
12988 {
12989 tree tem = TREE_OPERAND (arg0, 0);
12990 STRIP_NOPS (tem);
12991 if (TREE_CODE (tem) == RSHIFT_EXPR
12992 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12993 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12994 tree_to_uhwi (TREE_OPERAND (tem, 1)))
12995 return fold_build2_loc (loc, BIT_AND_EXPR, type,
12996 TREE_OPERAND (tem, 0), arg1);
12997 }
12998
12999 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13000 is probably obsolete because the first operand should be a
13001 truth value (that's why we have the two cases above), but let's
13002 leave it in until we can confirm this for all front-ends. */
13003 if (integer_zerop (op2)
13004 && TREE_CODE (arg0) == NE_EXPR
13005 && integer_zerop (TREE_OPERAND (arg0, 1))
13006 && integer_pow2p (arg1)
13007 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13008 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13009 arg1, OEP_ONLY_CONST))
13010 return pedantic_non_lvalue_loc (loc,
13011 fold_convert_loc (loc, type,
13012 TREE_OPERAND (arg0, 0)));
13013
13014 /* Disable the transformations below for vectors, since
13015 fold_binary_op_with_conditional_arg may undo them immediately,
13016 yielding an infinite loop. */
13017 if (code == VEC_COND_EXPR)
13018 return NULL_TREE;
13019
13020 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13021 if (integer_zerop (op2)
13022 && truth_value_p (TREE_CODE (arg0))
13023 && truth_value_p (TREE_CODE (arg1))
13024 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13025 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13026 : TRUTH_ANDIF_EXPR,
13027 type, fold_convert_loc (loc, type, arg0), arg1);
13028
13029 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13030 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13031 && truth_value_p (TREE_CODE (arg0))
13032 && truth_value_p (TREE_CODE (arg1))
13033 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13034 {
13035 location_t loc0 = expr_location_or (arg0, loc);
13036 /* Only perform transformation if ARG0 is easily inverted. */
13037 tem = fold_invert_truthvalue (loc0, arg0);
13038 if (tem)
13039 return fold_build2_loc (loc, code == VEC_COND_EXPR
13040 ? BIT_IOR_EXPR
13041 : TRUTH_ORIF_EXPR,
13042 type, fold_convert_loc (loc, type, tem),
13043 arg1);
13044 }
13045
13046 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13047 if (integer_zerop (arg1)
13048 && truth_value_p (TREE_CODE (arg0))
13049 && truth_value_p (TREE_CODE (op2))
13050 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13051 {
13052 location_t loc0 = expr_location_or (arg0, loc);
13053 /* Only perform transformation if ARG0 is easily inverted. */
13054 tem = fold_invert_truthvalue (loc0, arg0);
13055 if (tem)
13056 return fold_build2_loc (loc, code == VEC_COND_EXPR
13057 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13058 type, fold_convert_loc (loc, type, tem),
13059 op2);
13060 }
13061
13062 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13063 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13064 && truth_value_p (TREE_CODE (arg0))
13065 && truth_value_p (TREE_CODE (op2))
13066 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13067 return fold_build2_loc (loc, code == VEC_COND_EXPR
13068 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13069 type, fold_convert_loc (loc, type, arg0), op2);
13070
13071 return NULL_TREE;
13072
13073 case CALL_EXPR:
13074 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13075 of fold_ternary on them. */
13076 gcc_unreachable ();
13077
13078 case BIT_FIELD_REF:
13079 if ((TREE_CODE (arg0) == VECTOR_CST
13080 || (TREE_CODE (arg0) == CONSTRUCTOR
13081 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
13082 && (type == TREE_TYPE (TREE_TYPE (arg0))
13083 || (TREE_CODE (type) == VECTOR_TYPE
13084 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
13085 {
13086 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13087 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
13088 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13089 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13090
13091 if (n != 0
13092 && (idx % width) == 0
13093 && (n % width) == 0
13094 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13095 {
13096 idx = idx / width;
13097 n = n / width;
13098
13099 if (TREE_CODE (arg0) == VECTOR_CST)
13100 {
13101 if (n == 1)
13102 return VECTOR_CST_ELT (arg0, idx);
13103
13104 tree *vals = XALLOCAVEC (tree, n);
13105 for (unsigned i = 0; i < n; ++i)
13106 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
13107 return build_vector (type, vals);
13108 }
13109
13110 /* Constructor elements can be subvectors. */
13111 unsigned HOST_WIDE_INT k = 1;
13112 if (CONSTRUCTOR_NELTS (arg0) != 0)
13113 {
13114 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
13115 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
13116 k = TYPE_VECTOR_SUBPARTS (cons_elem);
13117 }
13118
13119 /* We keep an exact subset of the constructor elements. */
13120 if ((idx % k) == 0 && (n % k) == 0)
13121 {
13122 if (CONSTRUCTOR_NELTS (arg0) == 0)
13123 return build_constructor (type, NULL);
13124 idx /= k;
13125 n /= k;
13126 if (n == 1)
13127 {
13128 if (idx < CONSTRUCTOR_NELTS (arg0))
13129 return CONSTRUCTOR_ELT (arg0, idx)->value;
13130 return build_zero_cst (type);
13131 }
13132
13133 vec<constructor_elt, va_gc> *vals;
13134 vec_alloc (vals, n);
13135 for (unsigned i = 0;
13136 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
13137 ++i)
13138 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
13139 CONSTRUCTOR_ELT
13140 (arg0, idx + i)->value);
13141 return build_constructor (type, vals);
13142 }
13143 /* The bitfield references a single constructor element. */
13144 else if (idx + n <= (idx / k + 1) * k)
13145 {
13146 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
13147 return build_zero_cst (type);
13148 else if (n == k)
13149 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
13150 else
13151 return fold_build3_loc (loc, code, type,
13152 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
13153 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
13154 }
13155 }
13156 }
13157
13158 /* A bit-field-ref that referenced the full argument can be stripped. */
13159 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13160 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
13161 && integer_zerop (op2))
13162 return fold_convert_loc (loc, type, arg0);
13163
13164 /* On constants we can use native encode/interpret to constant
13165 fold (nearly) all BIT_FIELD_REFs. */
13166 if (CONSTANT_CLASS_P (arg0)
13167 && can_native_interpret_type_p (type)
13168 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
13169 /* This limitation should not be necessary, we just need to
13170 round this up to mode size. */
13171 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
13172 /* Need bit-shifting of the buffer to relax the following. */
13173 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
13174 {
13175 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13176 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13177 unsigned HOST_WIDE_INT clen;
13178 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
13179 /* ??? We cannot tell native_encode_expr to start at
13180 some random byte only. So limit us to a reasonable amount
13181 of work. */
13182 if (clen <= 4096)
13183 {
13184 unsigned char *b = XALLOCAVEC (unsigned char, clen);
13185 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
13186 if (len > 0
13187 && len * BITS_PER_UNIT >= bitpos + bitsize)
13188 {
13189 tree v = native_interpret_expr (type,
13190 b + bitpos / BITS_PER_UNIT,
13191 bitsize / BITS_PER_UNIT);
13192 if (v)
13193 return v;
13194 }
13195 }
13196 }
13197
13198 return NULL_TREE;
13199
13200 case FMA_EXPR:
13201 /* For integers we can decompose the FMA if possible. */
13202 if (TREE_CODE (arg0) == INTEGER_CST
13203 && TREE_CODE (arg1) == INTEGER_CST)
13204 return fold_build2_loc (loc, PLUS_EXPR, type,
13205 const_binop (MULT_EXPR, arg0, arg1), arg2);
13206 if (integer_zerop (arg2))
13207 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13208
13209 return fold_fma (loc, type, arg0, arg1, arg2);
13210
13211 case VEC_PERM_EXPR:
13212 if (TREE_CODE (arg2) == VECTOR_CST)
13213 {
13214 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
13215 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
13216 unsigned char *sel2 = sel + nelts;
13217 bool need_mask_canon = false;
13218 bool need_mask_canon2 = false;
13219 bool all_in_vec0 = true;
13220 bool all_in_vec1 = true;
13221 bool maybe_identity = true;
13222 bool single_arg = (op0 == op1);
13223 bool changed = false;
13224
13225 mask2 = 2 * nelts - 1;
13226 mask = single_arg ? (nelts - 1) : mask2;
13227 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
13228 for (i = 0; i < nelts; i++)
13229 {
13230 tree val = VECTOR_CST_ELT (arg2, i);
13231 if (TREE_CODE (val) != INTEGER_CST)
13232 return NULL_TREE;
13233
13234 /* Make sure that the perm value is in an acceptable
13235 range. */
13236 wide_int t = val;
13237 need_mask_canon |= wi::gtu_p (t, mask);
13238 need_mask_canon2 |= wi::gtu_p (t, mask2);
13239 sel[i] = t.to_uhwi () & mask;
13240 sel2[i] = t.to_uhwi () & mask2;
13241
13242 if (sel[i] < nelts)
13243 all_in_vec1 = false;
13244 else
13245 all_in_vec0 = false;
13246
13247 if ((sel[i] & (nelts-1)) != i)
13248 maybe_identity = false;
13249 }
13250
13251 if (maybe_identity)
13252 {
13253 if (all_in_vec0)
13254 return op0;
13255 if (all_in_vec1)
13256 return op1;
13257 }
13258
13259 if (all_in_vec0)
13260 op1 = op0;
13261 else if (all_in_vec1)
13262 {
13263 op0 = op1;
13264 for (i = 0; i < nelts; i++)
13265 sel[i] -= nelts;
13266 need_mask_canon = true;
13267 }
13268
13269 if ((TREE_CODE (op0) == VECTOR_CST
13270 || TREE_CODE (op0) == CONSTRUCTOR)
13271 && (TREE_CODE (op1) == VECTOR_CST
13272 || TREE_CODE (op1) == CONSTRUCTOR))
13273 {
13274 tree t = fold_vec_perm (type, op0, op1, sel);
13275 if (t != NULL_TREE)
13276 return t;
13277 }
13278
13279 if (op0 == op1 && !single_arg)
13280 changed = true;
13281
13282 /* Some targets are deficient and fail to expand a single
13283 argument permutation while still allowing an equivalent
13284 2-argument version. */
13285 if (need_mask_canon && arg2 == op2
13286 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
13287 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
13288 {
13289 need_mask_canon = need_mask_canon2;
13290 sel = sel2;
13291 }
13292
13293 if (need_mask_canon && arg2 == op2)
13294 {
13295 tree *tsel = XALLOCAVEC (tree, nelts);
13296 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
13297 for (i = 0; i < nelts; i++)
13298 tsel[i] = build_int_cst (eltype, sel[i]);
13299 op2 = build_vector (TREE_TYPE (arg2), tsel);
13300 changed = true;
13301 }
13302
13303 if (changed)
13304 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
13305 }
13306 return NULL_TREE;
13307
13308 default:
13309 return NULL_TREE;
13310 } /* switch (code) */
13311 }
13312
13313 /* Perform constant folding and related simplification of EXPR.
13314 The related simplifications include x*1 => x, x*0 => 0, etc.,
13315 and application of the associative law.
13316 NOP_EXPR conversions may be removed freely (as long as we
13317 are careful not to change the type of the overall expression).
13318 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13319 but we can constant-fold them if they have constant operands. */
13320
13321 #ifdef ENABLE_FOLD_CHECKING
13322 # define fold(x) fold_1 (x)
13323 static tree fold_1 (tree);
13324 static
13325 #endif
13326 tree
13327 fold (tree expr)
13328 {
13329 const tree t = expr;
13330 enum tree_code code = TREE_CODE (t);
13331 enum tree_code_class kind = TREE_CODE_CLASS (code);
13332 tree tem;
13333 location_t loc = EXPR_LOCATION (expr);
13334
13335 /* Return right away if a constant. */
13336 if (kind == tcc_constant)
13337 return t;
13338
13339 /* CALL_EXPR-like objects with variable numbers of operands are
13340 treated specially. */
13341 if (kind == tcc_vl_exp)
13342 {
13343 if (code == CALL_EXPR)
13344 {
13345 tem = fold_call_expr (loc, expr, false);
13346 return tem ? tem : expr;
13347 }
13348 return expr;
13349 }
13350
13351 if (IS_EXPR_CODE_CLASS (kind))
13352 {
13353 tree type = TREE_TYPE (t);
13354 tree op0, op1, op2;
13355
13356 switch (TREE_CODE_LENGTH (code))
13357 {
13358 case 1:
13359 op0 = TREE_OPERAND (t, 0);
13360 tem = fold_unary_loc (loc, code, type, op0);
13361 return tem ? tem : expr;
13362 case 2:
13363 op0 = TREE_OPERAND (t, 0);
13364 op1 = TREE_OPERAND (t, 1);
13365 tem = fold_binary_loc (loc, code, type, op0, op1);
13366 return tem ? tem : expr;
13367 case 3:
13368 op0 = TREE_OPERAND (t, 0);
13369 op1 = TREE_OPERAND (t, 1);
13370 op2 = TREE_OPERAND (t, 2);
13371 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13372 return tem ? tem : expr;
13373 default:
13374 break;
13375 }
13376 }
13377
13378 switch (code)
13379 {
13380 case ARRAY_REF:
13381 {
13382 tree op0 = TREE_OPERAND (t, 0);
13383 tree op1 = TREE_OPERAND (t, 1);
13384
13385 if (TREE_CODE (op1) == INTEGER_CST
13386 && TREE_CODE (op0) == CONSTRUCTOR
13387 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13388 {
13389 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
13390 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
13391 unsigned HOST_WIDE_INT begin = 0;
13392
13393 /* Find a matching index by means of a binary search. */
13394 while (begin != end)
13395 {
13396 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13397 tree index = (*elts)[middle].index;
13398
13399 if (TREE_CODE (index) == INTEGER_CST
13400 && tree_int_cst_lt (index, op1))
13401 begin = middle + 1;
13402 else if (TREE_CODE (index) == INTEGER_CST
13403 && tree_int_cst_lt (op1, index))
13404 end = middle;
13405 else if (TREE_CODE (index) == RANGE_EXPR
13406 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13407 begin = middle + 1;
13408 else if (TREE_CODE (index) == RANGE_EXPR
13409 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13410 end = middle;
13411 else
13412 return (*elts)[middle].value;
13413 }
13414 }
13415
13416 return t;
13417 }
13418
13419 /* Return a VECTOR_CST if possible. */
13420 case CONSTRUCTOR:
13421 {
13422 tree type = TREE_TYPE (t);
13423 if (TREE_CODE (type) != VECTOR_TYPE)
13424 return t;
13425
13426 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
13427 unsigned HOST_WIDE_INT idx, pos = 0;
13428 tree value;
13429
13430 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
13431 {
13432 if (!CONSTANT_CLASS_P (value))
13433 return t;
13434 if (TREE_CODE (value) == VECTOR_CST)
13435 {
13436 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
13437 vec[pos++] = VECTOR_CST_ELT (value, i);
13438 }
13439 else
13440 vec[pos++] = value;
13441 }
13442 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
13443 vec[pos] = build_zero_cst (TREE_TYPE (type));
13444
13445 return build_vector (type, vec);
13446 }
13447
13448 case CONST_DECL:
13449 return fold (DECL_INITIAL (t));
13450
13451 default:
13452 return t;
13453 } /* switch (code) */
13454 }
13455
13456 #ifdef ENABLE_FOLD_CHECKING
13457 #undef fold
13458
13459 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13460 hash_table<nofree_ptr_hash<const tree_node> > *);
13461 static void fold_check_failed (const_tree, const_tree);
13462 void print_fold_checksum (const_tree);
13463
13464 /* When --enable-checking=fold, compute a digest of expr before
13465 and after actual fold call to see if fold did not accidentally
13466 change original expr. */
13467
13468 tree
13469 fold (tree expr)
13470 {
13471 tree ret;
13472 struct md5_ctx ctx;
13473 unsigned char checksum_before[16], checksum_after[16];
13474 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13475
13476 md5_init_ctx (&ctx);
13477 fold_checksum_tree (expr, &ctx, &ht);
13478 md5_finish_ctx (&ctx, checksum_before);
13479 ht.empty ();
13480
13481 ret = fold_1 (expr);
13482
13483 md5_init_ctx (&ctx);
13484 fold_checksum_tree (expr, &ctx, &ht);
13485 md5_finish_ctx (&ctx, checksum_after);
13486
13487 if (memcmp (checksum_before, checksum_after, 16))
13488 fold_check_failed (expr, ret);
13489
13490 return ret;
13491 }
13492
13493 void
13494 print_fold_checksum (const_tree expr)
13495 {
13496 struct md5_ctx ctx;
13497 unsigned char checksum[16], cnt;
13498 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13499
13500 md5_init_ctx (&ctx);
13501 fold_checksum_tree (expr, &ctx, &ht);
13502 md5_finish_ctx (&ctx, checksum);
13503 for (cnt = 0; cnt < 16; ++cnt)
13504 fprintf (stderr, "%02x", checksum[cnt]);
13505 putc ('\n', stderr);
13506 }
13507
13508 static void
13509 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13510 {
13511 internal_error ("fold check: original tree changed by fold");
13512 }
13513
13514 static void
13515 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13516 hash_table<nofree_ptr_hash <const tree_node> > *ht)
13517 {
13518 const tree_node **slot;
13519 enum tree_code code;
13520 union tree_node buf;
13521 int i, len;
13522
13523 recursive_label:
13524 if (expr == NULL)
13525 return;
13526 slot = ht->find_slot (expr, INSERT);
13527 if (*slot != NULL)
13528 return;
13529 *slot = expr;
13530 code = TREE_CODE (expr);
13531 if (TREE_CODE_CLASS (code) == tcc_declaration
13532 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13533 {
13534 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13535 memcpy ((char *) &buf, expr, tree_size (expr));
13536 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13537 buf.decl_with_vis.symtab_node = NULL;
13538 expr = (tree) &buf;
13539 }
13540 else if (TREE_CODE_CLASS (code) == tcc_type
13541 && (TYPE_POINTER_TO (expr)
13542 || TYPE_REFERENCE_TO (expr)
13543 || TYPE_CACHED_VALUES_P (expr)
13544 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13545 || TYPE_NEXT_VARIANT (expr)))
13546 {
13547 /* Allow these fields to be modified. */
13548 tree tmp;
13549 memcpy ((char *) &buf, expr, tree_size (expr));
13550 expr = tmp = (tree) &buf;
13551 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13552 TYPE_POINTER_TO (tmp) = NULL;
13553 TYPE_REFERENCE_TO (tmp) = NULL;
13554 TYPE_NEXT_VARIANT (tmp) = NULL;
13555 if (TYPE_CACHED_VALUES_P (tmp))
13556 {
13557 TYPE_CACHED_VALUES_P (tmp) = 0;
13558 TYPE_CACHED_VALUES (tmp) = NULL;
13559 }
13560 }
13561 md5_process_bytes (expr, tree_size (expr), ctx);
13562 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13563 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13564 if (TREE_CODE_CLASS (code) != tcc_type
13565 && TREE_CODE_CLASS (code) != tcc_declaration
13566 && code != TREE_LIST
13567 && code != SSA_NAME
13568 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13569 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13570 switch (TREE_CODE_CLASS (code))
13571 {
13572 case tcc_constant:
13573 switch (code)
13574 {
13575 case STRING_CST:
13576 md5_process_bytes (TREE_STRING_POINTER (expr),
13577 TREE_STRING_LENGTH (expr), ctx);
13578 break;
13579 case COMPLEX_CST:
13580 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13581 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13582 break;
13583 case VECTOR_CST:
13584 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
13585 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
13586 break;
13587 default:
13588 break;
13589 }
13590 break;
13591 case tcc_exceptional:
13592 switch (code)
13593 {
13594 case TREE_LIST:
13595 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13596 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13597 expr = TREE_CHAIN (expr);
13598 goto recursive_label;
13599 break;
13600 case TREE_VEC:
13601 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13602 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13603 break;
13604 default:
13605 break;
13606 }
13607 break;
13608 case tcc_expression:
13609 case tcc_reference:
13610 case tcc_comparison:
13611 case tcc_unary:
13612 case tcc_binary:
13613 case tcc_statement:
13614 case tcc_vl_exp:
13615 len = TREE_OPERAND_LENGTH (expr);
13616 for (i = 0; i < len; ++i)
13617 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13618 break;
13619 case tcc_declaration:
13620 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13621 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13622 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13623 {
13624 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13625 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13626 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13627 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13628 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13629 }
13630
13631 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13632 {
13633 if (TREE_CODE (expr) == FUNCTION_DECL)
13634 {
13635 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13636 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13637 }
13638 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13639 }
13640 break;
13641 case tcc_type:
13642 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13643 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13644 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13645 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13646 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13647 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13648 if (INTEGRAL_TYPE_P (expr)
13649 || SCALAR_FLOAT_TYPE_P (expr))
13650 {
13651 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13652 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13653 }
13654 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13655 if (TREE_CODE (expr) == RECORD_TYPE
13656 || TREE_CODE (expr) == UNION_TYPE
13657 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13658 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13659 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13660 break;
13661 default:
13662 break;
13663 }
13664 }
13665
13666 /* Helper function for outputting the checksum of a tree T. When
13667 debugging with gdb, you can "define mynext" to be "next" followed
13668 by "call debug_fold_checksum (op0)", then just trace down till the
13669 outputs differ. */
13670
13671 DEBUG_FUNCTION void
13672 debug_fold_checksum (const_tree t)
13673 {
13674 int i;
13675 unsigned char checksum[16];
13676 struct md5_ctx ctx;
13677 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13678
13679 md5_init_ctx (&ctx);
13680 fold_checksum_tree (t, &ctx, &ht);
13681 md5_finish_ctx (&ctx, checksum);
13682 ht.empty ();
13683
13684 for (i = 0; i < 16; i++)
13685 fprintf (stderr, "%d ", checksum[i]);
13686
13687 fprintf (stderr, "\n");
13688 }
13689
13690 #endif
13691
13692 /* Fold a unary tree expression with code CODE of type TYPE with an
13693 operand OP0. LOC is the location of the resulting expression.
13694 Return a folded expression if successful. Otherwise, return a tree
13695 expression with code CODE of type TYPE with an operand OP0. */
13696
13697 tree
13698 fold_build1_stat_loc (location_t loc,
13699 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13700 {
13701 tree tem;
13702 #ifdef ENABLE_FOLD_CHECKING
13703 unsigned char checksum_before[16], checksum_after[16];
13704 struct md5_ctx ctx;
13705 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13706
13707 md5_init_ctx (&ctx);
13708 fold_checksum_tree (op0, &ctx, &ht);
13709 md5_finish_ctx (&ctx, checksum_before);
13710 ht.empty ();
13711 #endif
13712
13713 tem = fold_unary_loc (loc, code, type, op0);
13714 if (!tem)
13715 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13716
13717 #ifdef ENABLE_FOLD_CHECKING
13718 md5_init_ctx (&ctx);
13719 fold_checksum_tree (op0, &ctx, &ht);
13720 md5_finish_ctx (&ctx, checksum_after);
13721
13722 if (memcmp (checksum_before, checksum_after, 16))
13723 fold_check_failed (op0, tem);
13724 #endif
13725 return tem;
13726 }
13727
13728 /* Fold a binary tree expression with code CODE of type TYPE with
13729 operands OP0 and OP1. LOC is the location of the resulting
13730 expression. Return a folded expression if successful. Otherwise,
13731 return a tree expression with code CODE of type TYPE with operands
13732 OP0 and OP1. */
13733
13734 tree
13735 fold_build2_stat_loc (location_t loc,
13736 enum tree_code code, tree type, tree op0, tree op1
13737 MEM_STAT_DECL)
13738 {
13739 tree tem;
13740 #ifdef ENABLE_FOLD_CHECKING
13741 unsigned char checksum_before_op0[16],
13742 checksum_before_op1[16],
13743 checksum_after_op0[16],
13744 checksum_after_op1[16];
13745 struct md5_ctx ctx;
13746 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13747
13748 md5_init_ctx (&ctx);
13749 fold_checksum_tree (op0, &ctx, &ht);
13750 md5_finish_ctx (&ctx, checksum_before_op0);
13751 ht.empty ();
13752
13753 md5_init_ctx (&ctx);
13754 fold_checksum_tree (op1, &ctx, &ht);
13755 md5_finish_ctx (&ctx, checksum_before_op1);
13756 ht.empty ();
13757 #endif
13758
13759 tem = fold_binary_loc (loc, code, type, op0, op1);
13760 if (!tem)
13761 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13762
13763 #ifdef ENABLE_FOLD_CHECKING
13764 md5_init_ctx (&ctx);
13765 fold_checksum_tree (op0, &ctx, &ht);
13766 md5_finish_ctx (&ctx, checksum_after_op0);
13767 ht.empty ();
13768
13769 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13770 fold_check_failed (op0, tem);
13771
13772 md5_init_ctx (&ctx);
13773 fold_checksum_tree (op1, &ctx, &ht);
13774 md5_finish_ctx (&ctx, checksum_after_op1);
13775
13776 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13777 fold_check_failed (op1, tem);
13778 #endif
13779 return tem;
13780 }
13781
13782 /* Fold a ternary tree expression with code CODE of type TYPE with
13783 operands OP0, OP1, and OP2. Return a folded expression if
13784 successful. Otherwise, return a tree expression with code CODE of
13785 type TYPE with operands OP0, OP1, and OP2. */
13786
13787 tree
13788 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
13789 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13790 {
13791 tree tem;
13792 #ifdef ENABLE_FOLD_CHECKING
13793 unsigned char checksum_before_op0[16],
13794 checksum_before_op1[16],
13795 checksum_before_op2[16],
13796 checksum_after_op0[16],
13797 checksum_after_op1[16],
13798 checksum_after_op2[16];
13799 struct md5_ctx ctx;
13800 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13801
13802 md5_init_ctx (&ctx);
13803 fold_checksum_tree (op0, &ctx, &ht);
13804 md5_finish_ctx (&ctx, checksum_before_op0);
13805 ht.empty ();
13806
13807 md5_init_ctx (&ctx);
13808 fold_checksum_tree (op1, &ctx, &ht);
13809 md5_finish_ctx (&ctx, checksum_before_op1);
13810 ht.empty ();
13811
13812 md5_init_ctx (&ctx);
13813 fold_checksum_tree (op2, &ctx, &ht);
13814 md5_finish_ctx (&ctx, checksum_before_op2);
13815 ht.empty ();
13816 #endif
13817
13818 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13819 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13820 if (!tem)
13821 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13822
13823 #ifdef ENABLE_FOLD_CHECKING
13824 md5_init_ctx (&ctx);
13825 fold_checksum_tree (op0, &ctx, &ht);
13826 md5_finish_ctx (&ctx, checksum_after_op0);
13827 ht.empty ();
13828
13829 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13830 fold_check_failed (op0, tem);
13831
13832 md5_init_ctx (&ctx);
13833 fold_checksum_tree (op1, &ctx, &ht);
13834 md5_finish_ctx (&ctx, checksum_after_op1);
13835 ht.empty ();
13836
13837 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13838 fold_check_failed (op1, tem);
13839
13840 md5_init_ctx (&ctx);
13841 fold_checksum_tree (op2, &ctx, &ht);
13842 md5_finish_ctx (&ctx, checksum_after_op2);
13843
13844 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13845 fold_check_failed (op2, tem);
13846 #endif
13847 return tem;
13848 }
13849
13850 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13851 arguments in ARGARRAY, and a null static chain.
13852 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13853 of type TYPE from the given operands as constructed by build_call_array. */
13854
13855 tree
13856 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13857 int nargs, tree *argarray)
13858 {
13859 tree tem;
13860 #ifdef ENABLE_FOLD_CHECKING
13861 unsigned char checksum_before_fn[16],
13862 checksum_before_arglist[16],
13863 checksum_after_fn[16],
13864 checksum_after_arglist[16];
13865 struct md5_ctx ctx;
13866 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13867 int i;
13868
13869 md5_init_ctx (&ctx);
13870 fold_checksum_tree (fn, &ctx, &ht);
13871 md5_finish_ctx (&ctx, checksum_before_fn);
13872 ht.empty ();
13873
13874 md5_init_ctx (&ctx);
13875 for (i = 0; i < nargs; i++)
13876 fold_checksum_tree (argarray[i], &ctx, &ht);
13877 md5_finish_ctx (&ctx, checksum_before_arglist);
13878 ht.empty ();
13879 #endif
13880
13881 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13882 if (!tem)
13883 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13884
13885 #ifdef ENABLE_FOLD_CHECKING
13886 md5_init_ctx (&ctx);
13887 fold_checksum_tree (fn, &ctx, &ht);
13888 md5_finish_ctx (&ctx, checksum_after_fn);
13889 ht.empty ();
13890
13891 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13892 fold_check_failed (fn, tem);
13893
13894 md5_init_ctx (&ctx);
13895 for (i = 0; i < nargs; i++)
13896 fold_checksum_tree (argarray[i], &ctx, &ht);
13897 md5_finish_ctx (&ctx, checksum_after_arglist);
13898
13899 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13900 fold_check_failed (NULL_TREE, tem);
13901 #endif
13902 return tem;
13903 }
13904
13905 /* Perform constant folding and related simplification of initializer
13906 expression EXPR. These behave identically to "fold_buildN" but ignore
13907 potential run-time traps and exceptions that fold must preserve. */
13908
13909 #define START_FOLD_INIT \
13910 int saved_signaling_nans = flag_signaling_nans;\
13911 int saved_trapping_math = flag_trapping_math;\
13912 int saved_rounding_math = flag_rounding_math;\
13913 int saved_trapv = flag_trapv;\
13914 int saved_folding_initializer = folding_initializer;\
13915 flag_signaling_nans = 0;\
13916 flag_trapping_math = 0;\
13917 flag_rounding_math = 0;\
13918 flag_trapv = 0;\
13919 folding_initializer = 1;
13920
13921 #define END_FOLD_INIT \
13922 flag_signaling_nans = saved_signaling_nans;\
13923 flag_trapping_math = saved_trapping_math;\
13924 flag_rounding_math = saved_rounding_math;\
13925 flag_trapv = saved_trapv;\
13926 folding_initializer = saved_folding_initializer;
13927
13928 tree
13929 fold_build1_initializer_loc (location_t loc, enum tree_code code,
13930 tree type, tree op)
13931 {
13932 tree result;
13933 START_FOLD_INIT;
13934
13935 result = fold_build1_loc (loc, code, type, op);
13936
13937 END_FOLD_INIT;
13938 return result;
13939 }
13940
13941 tree
13942 fold_build2_initializer_loc (location_t loc, enum tree_code code,
13943 tree type, tree op0, tree op1)
13944 {
13945 tree result;
13946 START_FOLD_INIT;
13947
13948 result = fold_build2_loc (loc, code, type, op0, op1);
13949
13950 END_FOLD_INIT;
13951 return result;
13952 }
13953
13954 tree
13955 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13956 int nargs, tree *argarray)
13957 {
13958 tree result;
13959 START_FOLD_INIT;
13960
13961 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13962
13963 END_FOLD_INIT;
13964 return result;
13965 }
13966
13967 #undef START_FOLD_INIT
13968 #undef END_FOLD_INIT
13969
13970 /* Determine if first argument is a multiple of second argument. Return 0 if
13971 it is not, or we cannot easily determined it to be.
13972
13973 An example of the sort of thing we care about (at this point; this routine
13974 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13975 fold cases do now) is discovering that
13976
13977 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13978
13979 is a multiple of
13980
13981 SAVE_EXPR (J * 8)
13982
13983 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13984
13985 This code also handles discovering that
13986
13987 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13988
13989 is a multiple of 8 so we don't have to worry about dealing with a
13990 possible remainder.
13991
13992 Note that we *look* inside a SAVE_EXPR only to determine how it was
13993 calculated; it is not safe for fold to do much of anything else with the
13994 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13995 at run time. For example, the latter example above *cannot* be implemented
13996 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13997 evaluation time of the original SAVE_EXPR is not necessarily the same at
13998 the time the new expression is evaluated. The only optimization of this
13999 sort that would be valid is changing
14000
14001 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14002
14003 divided by 8 to
14004
14005 SAVE_EXPR (I) * SAVE_EXPR (J)
14006
14007 (where the same SAVE_EXPR (J) is used in the original and the
14008 transformed version). */
14009
14010 int
14011 multiple_of_p (tree type, const_tree top, const_tree bottom)
14012 {
14013 if (operand_equal_p (top, bottom, 0))
14014 return 1;
14015
14016 if (TREE_CODE (type) != INTEGER_TYPE)
14017 return 0;
14018
14019 switch (TREE_CODE (top))
14020 {
14021 case BIT_AND_EXPR:
14022 /* Bitwise and provides a power of two multiple. If the mask is
14023 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14024 if (!integer_pow2p (bottom))
14025 return 0;
14026 /* FALLTHRU */
14027
14028 case MULT_EXPR:
14029 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14030 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14031
14032 case PLUS_EXPR:
14033 case MINUS_EXPR:
14034 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14035 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14036
14037 case LSHIFT_EXPR:
14038 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14039 {
14040 tree op1, t1;
14041
14042 op1 = TREE_OPERAND (top, 1);
14043 /* const_binop may not detect overflow correctly,
14044 so check for it explicitly here. */
14045 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
14046 && 0 != (t1 = fold_convert (type,
14047 const_binop (LSHIFT_EXPR,
14048 size_one_node,
14049 op1)))
14050 && !TREE_OVERFLOW (t1))
14051 return multiple_of_p (type, t1, bottom);
14052 }
14053 return 0;
14054
14055 case NOP_EXPR:
14056 /* Can't handle conversions from non-integral or wider integral type. */
14057 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14058 || (TYPE_PRECISION (type)
14059 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14060 return 0;
14061
14062 /* .. fall through ... */
14063
14064 case SAVE_EXPR:
14065 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14066
14067 case COND_EXPR:
14068 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14069 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14070
14071 case INTEGER_CST:
14072 if (TREE_CODE (bottom) != INTEGER_CST
14073 || integer_zerop (bottom)
14074 || (TYPE_UNSIGNED (type)
14075 && (tree_int_cst_sgn (top) < 0
14076 || tree_int_cst_sgn (bottom) < 0)))
14077 return 0;
14078 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14079 SIGNED);
14080
14081 default:
14082 return 0;
14083 }
14084 }
14085
14086 /* Return true if CODE or TYPE is known to be non-negative. */
14087
14088 static bool
14089 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14090 {
14091 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14092 && truth_value_p (code))
14093 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14094 have a signed:1 type (where the value is -1 and 0). */
14095 return true;
14096 return false;
14097 }
14098
14099 /* Return true if (CODE OP0) is known to be non-negative. If the return
14100 value is based on the assumption that signed overflow is undefined,
14101 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14102 *STRICT_OVERFLOW_P. */
14103
14104 bool
14105 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14106 bool *strict_overflow_p)
14107 {
14108 if (TYPE_UNSIGNED (type))
14109 return true;
14110
14111 switch (code)
14112 {
14113 case ABS_EXPR:
14114 /* We can't return 1 if flag_wrapv is set because
14115 ABS_EXPR<INT_MIN> = INT_MIN. */
14116 if (!ANY_INTEGRAL_TYPE_P (type))
14117 return true;
14118 if (TYPE_OVERFLOW_UNDEFINED (type))
14119 {
14120 *strict_overflow_p = true;
14121 return true;
14122 }
14123 break;
14124
14125 case NON_LVALUE_EXPR:
14126 case FLOAT_EXPR:
14127 case FIX_TRUNC_EXPR:
14128 return tree_expr_nonnegative_warnv_p (op0,
14129 strict_overflow_p);
14130
14131 CASE_CONVERT:
14132 {
14133 tree inner_type = TREE_TYPE (op0);
14134 tree outer_type = type;
14135
14136 if (TREE_CODE (outer_type) == REAL_TYPE)
14137 {
14138 if (TREE_CODE (inner_type) == REAL_TYPE)
14139 return tree_expr_nonnegative_warnv_p (op0,
14140 strict_overflow_p);
14141 if (INTEGRAL_TYPE_P (inner_type))
14142 {
14143 if (TYPE_UNSIGNED (inner_type))
14144 return true;
14145 return tree_expr_nonnegative_warnv_p (op0,
14146 strict_overflow_p);
14147 }
14148 }
14149 else if (INTEGRAL_TYPE_P (outer_type))
14150 {
14151 if (TREE_CODE (inner_type) == REAL_TYPE)
14152 return tree_expr_nonnegative_warnv_p (op0,
14153 strict_overflow_p);
14154 if (INTEGRAL_TYPE_P (inner_type))
14155 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14156 && TYPE_UNSIGNED (inner_type);
14157 }
14158 }
14159 break;
14160
14161 default:
14162 return tree_simple_nonnegative_warnv_p (code, type);
14163 }
14164
14165 /* We don't know sign of `t', so be conservative and return false. */
14166 return false;
14167 }
14168
14169 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14170 value is based on the assumption that signed overflow is undefined,
14171 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14172 *STRICT_OVERFLOW_P. */
14173
14174 bool
14175 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14176 tree op1, bool *strict_overflow_p)
14177 {
14178 if (TYPE_UNSIGNED (type))
14179 return true;
14180
14181 switch (code)
14182 {
14183 case POINTER_PLUS_EXPR:
14184 case PLUS_EXPR:
14185 if (FLOAT_TYPE_P (type))
14186 return (tree_expr_nonnegative_warnv_p (op0,
14187 strict_overflow_p)
14188 && tree_expr_nonnegative_warnv_p (op1,
14189 strict_overflow_p));
14190
14191 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14192 both unsigned and at least 2 bits shorter than the result. */
14193 if (TREE_CODE (type) == INTEGER_TYPE
14194 && TREE_CODE (op0) == NOP_EXPR
14195 && TREE_CODE (op1) == NOP_EXPR)
14196 {
14197 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14198 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14199 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14200 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14201 {
14202 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14203 TYPE_PRECISION (inner2)) + 1;
14204 return prec < TYPE_PRECISION (type);
14205 }
14206 }
14207 break;
14208
14209 case MULT_EXPR:
14210 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14211 {
14212 /* x * x is always non-negative for floating point x
14213 or without overflow. */
14214 if (operand_equal_p (op0, op1, 0)
14215 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
14216 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
14217 {
14218 if (ANY_INTEGRAL_TYPE_P (type)
14219 && TYPE_OVERFLOW_UNDEFINED (type))
14220 *strict_overflow_p = true;
14221 return true;
14222 }
14223 }
14224
14225 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14226 both unsigned and their total bits is shorter than the result. */
14227 if (TREE_CODE (type) == INTEGER_TYPE
14228 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14229 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14230 {
14231 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14232 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14233 : TREE_TYPE (op0);
14234 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14235 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14236 : TREE_TYPE (op1);
14237
14238 bool unsigned0 = TYPE_UNSIGNED (inner0);
14239 bool unsigned1 = TYPE_UNSIGNED (inner1);
14240
14241 if (TREE_CODE (op0) == INTEGER_CST)
14242 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14243
14244 if (TREE_CODE (op1) == INTEGER_CST)
14245 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14246
14247 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14248 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14249 {
14250 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14251 ? tree_int_cst_min_precision (op0, UNSIGNED)
14252 : TYPE_PRECISION (inner0);
14253
14254 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14255 ? tree_int_cst_min_precision (op1, UNSIGNED)
14256 : TYPE_PRECISION (inner1);
14257
14258 return precision0 + precision1 < TYPE_PRECISION (type);
14259 }
14260 }
14261 return false;
14262
14263 case BIT_AND_EXPR:
14264 case MAX_EXPR:
14265 return (tree_expr_nonnegative_warnv_p (op0,
14266 strict_overflow_p)
14267 || tree_expr_nonnegative_warnv_p (op1,
14268 strict_overflow_p));
14269
14270 case BIT_IOR_EXPR:
14271 case BIT_XOR_EXPR:
14272 case MIN_EXPR:
14273 case RDIV_EXPR:
14274 case TRUNC_DIV_EXPR:
14275 case CEIL_DIV_EXPR:
14276 case FLOOR_DIV_EXPR:
14277 case ROUND_DIV_EXPR:
14278 return (tree_expr_nonnegative_warnv_p (op0,
14279 strict_overflow_p)
14280 && tree_expr_nonnegative_warnv_p (op1,
14281 strict_overflow_p));
14282
14283 case TRUNC_MOD_EXPR:
14284 case CEIL_MOD_EXPR:
14285 case FLOOR_MOD_EXPR:
14286 case ROUND_MOD_EXPR:
14287 return tree_expr_nonnegative_warnv_p (op0,
14288 strict_overflow_p);
14289 default:
14290 return tree_simple_nonnegative_warnv_p (code, type);
14291 }
14292
14293 /* We don't know sign of `t', so be conservative and return false. */
14294 return false;
14295 }
14296
14297 /* Return true if T is known to be non-negative. If the return
14298 value is based on the assumption that signed overflow is undefined,
14299 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14300 *STRICT_OVERFLOW_P. */
14301
14302 bool
14303 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14304 {
14305 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14306 return true;
14307
14308 switch (TREE_CODE (t))
14309 {
14310 case INTEGER_CST:
14311 return tree_int_cst_sgn (t) >= 0;
14312
14313 case REAL_CST:
14314 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14315
14316 case FIXED_CST:
14317 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14318
14319 case COND_EXPR:
14320 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14321 strict_overflow_p)
14322 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14323 strict_overflow_p));
14324 default:
14325 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14326 TREE_TYPE (t));
14327 }
14328 /* We don't know sign of `t', so be conservative and return false. */
14329 return false;
14330 }
14331
14332 /* Return true if T is known to be non-negative. If the return
14333 value is based on the assumption that signed overflow is undefined,
14334 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14335 *STRICT_OVERFLOW_P. */
14336
14337 bool
14338 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14339 tree arg0, tree arg1, bool *strict_overflow_p)
14340 {
14341 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14342 switch (DECL_FUNCTION_CODE (fndecl))
14343 {
14344 CASE_FLT_FN (BUILT_IN_ACOS):
14345 CASE_FLT_FN (BUILT_IN_ACOSH):
14346 CASE_FLT_FN (BUILT_IN_CABS):
14347 CASE_FLT_FN (BUILT_IN_COSH):
14348 CASE_FLT_FN (BUILT_IN_ERFC):
14349 CASE_FLT_FN (BUILT_IN_EXP):
14350 CASE_FLT_FN (BUILT_IN_EXP10):
14351 CASE_FLT_FN (BUILT_IN_EXP2):
14352 CASE_FLT_FN (BUILT_IN_FABS):
14353 CASE_FLT_FN (BUILT_IN_FDIM):
14354 CASE_FLT_FN (BUILT_IN_HYPOT):
14355 CASE_FLT_FN (BUILT_IN_POW10):
14356 CASE_INT_FN (BUILT_IN_FFS):
14357 CASE_INT_FN (BUILT_IN_PARITY):
14358 CASE_INT_FN (BUILT_IN_POPCOUNT):
14359 CASE_INT_FN (BUILT_IN_CLZ):
14360 CASE_INT_FN (BUILT_IN_CLRSB):
14361 case BUILT_IN_BSWAP32:
14362 case BUILT_IN_BSWAP64:
14363 /* Always true. */
14364 return true;
14365
14366 CASE_FLT_FN (BUILT_IN_SQRT):
14367 /* sqrt(-0.0) is -0.0. */
14368 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
14369 return true;
14370 return tree_expr_nonnegative_warnv_p (arg0,
14371 strict_overflow_p);
14372
14373 CASE_FLT_FN (BUILT_IN_ASINH):
14374 CASE_FLT_FN (BUILT_IN_ATAN):
14375 CASE_FLT_FN (BUILT_IN_ATANH):
14376 CASE_FLT_FN (BUILT_IN_CBRT):
14377 CASE_FLT_FN (BUILT_IN_CEIL):
14378 CASE_FLT_FN (BUILT_IN_ERF):
14379 CASE_FLT_FN (BUILT_IN_EXPM1):
14380 CASE_FLT_FN (BUILT_IN_FLOOR):
14381 CASE_FLT_FN (BUILT_IN_FMOD):
14382 CASE_FLT_FN (BUILT_IN_FREXP):
14383 CASE_FLT_FN (BUILT_IN_ICEIL):
14384 CASE_FLT_FN (BUILT_IN_IFLOOR):
14385 CASE_FLT_FN (BUILT_IN_IRINT):
14386 CASE_FLT_FN (BUILT_IN_IROUND):
14387 CASE_FLT_FN (BUILT_IN_LCEIL):
14388 CASE_FLT_FN (BUILT_IN_LDEXP):
14389 CASE_FLT_FN (BUILT_IN_LFLOOR):
14390 CASE_FLT_FN (BUILT_IN_LLCEIL):
14391 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14392 CASE_FLT_FN (BUILT_IN_LLRINT):
14393 CASE_FLT_FN (BUILT_IN_LLROUND):
14394 CASE_FLT_FN (BUILT_IN_LRINT):
14395 CASE_FLT_FN (BUILT_IN_LROUND):
14396 CASE_FLT_FN (BUILT_IN_MODF):
14397 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14398 CASE_FLT_FN (BUILT_IN_RINT):
14399 CASE_FLT_FN (BUILT_IN_ROUND):
14400 CASE_FLT_FN (BUILT_IN_SCALB):
14401 CASE_FLT_FN (BUILT_IN_SCALBLN):
14402 CASE_FLT_FN (BUILT_IN_SCALBN):
14403 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14404 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14405 CASE_FLT_FN (BUILT_IN_SINH):
14406 CASE_FLT_FN (BUILT_IN_TANH):
14407 CASE_FLT_FN (BUILT_IN_TRUNC):
14408 /* True if the 1st argument is nonnegative. */
14409 return tree_expr_nonnegative_warnv_p (arg0,
14410 strict_overflow_p);
14411
14412 CASE_FLT_FN (BUILT_IN_FMAX):
14413 /* True if the 1st OR 2nd arguments are nonnegative. */
14414 return (tree_expr_nonnegative_warnv_p (arg0,
14415 strict_overflow_p)
14416 || (tree_expr_nonnegative_warnv_p (arg1,
14417 strict_overflow_p)));
14418
14419 CASE_FLT_FN (BUILT_IN_FMIN):
14420 /* True if the 1st AND 2nd arguments are nonnegative. */
14421 return (tree_expr_nonnegative_warnv_p (arg0,
14422 strict_overflow_p)
14423 && (tree_expr_nonnegative_warnv_p (arg1,
14424 strict_overflow_p)));
14425
14426 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14427 /* True if the 2nd argument is nonnegative. */
14428 return tree_expr_nonnegative_warnv_p (arg1,
14429 strict_overflow_p);
14430
14431 CASE_FLT_FN (BUILT_IN_POWI):
14432 /* True if the 1st argument is nonnegative or the second
14433 argument is an even integer. */
14434 if (TREE_CODE (arg1) == INTEGER_CST
14435 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14436 return true;
14437 return tree_expr_nonnegative_warnv_p (arg0,
14438 strict_overflow_p);
14439
14440 CASE_FLT_FN (BUILT_IN_POW):
14441 /* True if the 1st argument is nonnegative or the second
14442 argument is an even integer valued real. */
14443 if (TREE_CODE (arg1) == REAL_CST)
14444 {
14445 REAL_VALUE_TYPE c;
14446 HOST_WIDE_INT n;
14447
14448 c = TREE_REAL_CST (arg1);
14449 n = real_to_integer (&c);
14450 if ((n & 1) == 0)
14451 {
14452 REAL_VALUE_TYPE cint;
14453 real_from_integer (&cint, VOIDmode, n, SIGNED);
14454 if (real_identical (&c, &cint))
14455 return true;
14456 }
14457 }
14458 return tree_expr_nonnegative_warnv_p (arg0,
14459 strict_overflow_p);
14460
14461 default:
14462 break;
14463 }
14464 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14465 type);
14466 }
14467
14468 /* Return true if T is known to be non-negative. If the return
14469 value is based on the assumption that signed overflow is undefined,
14470 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14471 *STRICT_OVERFLOW_P. */
14472
14473 static bool
14474 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14475 {
14476 enum tree_code code = TREE_CODE (t);
14477 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14478 return true;
14479
14480 switch (code)
14481 {
14482 case TARGET_EXPR:
14483 {
14484 tree temp = TARGET_EXPR_SLOT (t);
14485 t = TARGET_EXPR_INITIAL (t);
14486
14487 /* If the initializer is non-void, then it's a normal expression
14488 that will be assigned to the slot. */
14489 if (!VOID_TYPE_P (t))
14490 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14491
14492 /* Otherwise, the initializer sets the slot in some way. One common
14493 way is an assignment statement at the end of the initializer. */
14494 while (1)
14495 {
14496 if (TREE_CODE (t) == BIND_EXPR)
14497 t = expr_last (BIND_EXPR_BODY (t));
14498 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14499 || TREE_CODE (t) == TRY_CATCH_EXPR)
14500 t = expr_last (TREE_OPERAND (t, 0));
14501 else if (TREE_CODE (t) == STATEMENT_LIST)
14502 t = expr_last (t);
14503 else
14504 break;
14505 }
14506 if (TREE_CODE (t) == MODIFY_EXPR
14507 && TREE_OPERAND (t, 0) == temp)
14508 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14509 strict_overflow_p);
14510
14511 return false;
14512 }
14513
14514 case CALL_EXPR:
14515 {
14516 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14517 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14518
14519 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14520 get_callee_fndecl (t),
14521 arg0,
14522 arg1,
14523 strict_overflow_p);
14524 }
14525 case COMPOUND_EXPR:
14526 case MODIFY_EXPR:
14527 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14528 strict_overflow_p);
14529 case BIND_EXPR:
14530 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14531 strict_overflow_p);
14532 case SAVE_EXPR:
14533 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14534 strict_overflow_p);
14535
14536 default:
14537 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14538 TREE_TYPE (t));
14539 }
14540
14541 /* We don't know sign of `t', so be conservative and return false. */
14542 return false;
14543 }
14544
14545 /* Return true if T is known to be non-negative. If the return
14546 value is based on the assumption that signed overflow is undefined,
14547 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14548 *STRICT_OVERFLOW_P. */
14549
14550 bool
14551 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14552 {
14553 enum tree_code code;
14554 if (t == error_mark_node)
14555 return false;
14556
14557 code = TREE_CODE (t);
14558 switch (TREE_CODE_CLASS (code))
14559 {
14560 case tcc_binary:
14561 case tcc_comparison:
14562 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14563 TREE_TYPE (t),
14564 TREE_OPERAND (t, 0),
14565 TREE_OPERAND (t, 1),
14566 strict_overflow_p);
14567
14568 case tcc_unary:
14569 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14570 TREE_TYPE (t),
14571 TREE_OPERAND (t, 0),
14572 strict_overflow_p);
14573
14574 case tcc_constant:
14575 case tcc_declaration:
14576 case tcc_reference:
14577 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14578
14579 default:
14580 break;
14581 }
14582
14583 switch (code)
14584 {
14585 case TRUTH_AND_EXPR:
14586 case TRUTH_OR_EXPR:
14587 case TRUTH_XOR_EXPR:
14588 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14589 TREE_TYPE (t),
14590 TREE_OPERAND (t, 0),
14591 TREE_OPERAND (t, 1),
14592 strict_overflow_p);
14593 case TRUTH_NOT_EXPR:
14594 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14595 TREE_TYPE (t),
14596 TREE_OPERAND (t, 0),
14597 strict_overflow_p);
14598
14599 case COND_EXPR:
14600 case CONSTRUCTOR:
14601 case OBJ_TYPE_REF:
14602 case ASSERT_EXPR:
14603 case ADDR_EXPR:
14604 case WITH_SIZE_EXPR:
14605 case SSA_NAME:
14606 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14607
14608 default:
14609 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14610 }
14611 }
14612
14613 /* Return true if `t' is known to be non-negative. Handle warnings
14614 about undefined signed overflow. */
14615
14616 bool
14617 tree_expr_nonnegative_p (tree t)
14618 {
14619 bool ret, strict_overflow_p;
14620
14621 strict_overflow_p = false;
14622 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14623 if (strict_overflow_p)
14624 fold_overflow_warning (("assuming signed overflow does not occur when "
14625 "determining that expression is always "
14626 "non-negative"),
14627 WARN_STRICT_OVERFLOW_MISC);
14628 return ret;
14629 }
14630
14631
14632 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14633 For floating point we further ensure that T is not denormal.
14634 Similar logic is present in nonzero_address in rtlanal.h.
14635
14636 If the return value is based on the assumption that signed overflow
14637 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14638 change *STRICT_OVERFLOW_P. */
14639
14640 bool
14641 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14642 bool *strict_overflow_p)
14643 {
14644 switch (code)
14645 {
14646 case ABS_EXPR:
14647 return tree_expr_nonzero_warnv_p (op0,
14648 strict_overflow_p);
14649
14650 case NOP_EXPR:
14651 {
14652 tree inner_type = TREE_TYPE (op0);
14653 tree outer_type = type;
14654
14655 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14656 && tree_expr_nonzero_warnv_p (op0,
14657 strict_overflow_p));
14658 }
14659 break;
14660
14661 case NON_LVALUE_EXPR:
14662 return tree_expr_nonzero_warnv_p (op0,
14663 strict_overflow_p);
14664
14665 default:
14666 break;
14667 }
14668
14669 return false;
14670 }
14671
14672 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14673 For floating point we further ensure that T is not denormal.
14674 Similar logic is present in nonzero_address in rtlanal.h.
14675
14676 If the return value is based on the assumption that signed overflow
14677 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14678 change *STRICT_OVERFLOW_P. */
14679
14680 bool
14681 tree_binary_nonzero_warnv_p (enum tree_code code,
14682 tree type,
14683 tree op0,
14684 tree op1, bool *strict_overflow_p)
14685 {
14686 bool sub_strict_overflow_p;
14687 switch (code)
14688 {
14689 case POINTER_PLUS_EXPR:
14690 case PLUS_EXPR:
14691 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
14692 {
14693 /* With the presence of negative values it is hard
14694 to say something. */
14695 sub_strict_overflow_p = false;
14696 if (!tree_expr_nonnegative_warnv_p (op0,
14697 &sub_strict_overflow_p)
14698 || !tree_expr_nonnegative_warnv_p (op1,
14699 &sub_strict_overflow_p))
14700 return false;
14701 /* One of operands must be positive and the other non-negative. */
14702 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14703 overflows, on a twos-complement machine the sum of two
14704 nonnegative numbers can never be zero. */
14705 return (tree_expr_nonzero_warnv_p (op0,
14706 strict_overflow_p)
14707 || tree_expr_nonzero_warnv_p (op1,
14708 strict_overflow_p));
14709 }
14710 break;
14711
14712 case MULT_EXPR:
14713 if (TYPE_OVERFLOW_UNDEFINED (type))
14714 {
14715 if (tree_expr_nonzero_warnv_p (op0,
14716 strict_overflow_p)
14717 && tree_expr_nonzero_warnv_p (op1,
14718 strict_overflow_p))
14719 {
14720 *strict_overflow_p = true;
14721 return true;
14722 }
14723 }
14724 break;
14725
14726 case MIN_EXPR:
14727 sub_strict_overflow_p = false;
14728 if (tree_expr_nonzero_warnv_p (op0,
14729 &sub_strict_overflow_p)
14730 && tree_expr_nonzero_warnv_p (op1,
14731 &sub_strict_overflow_p))
14732 {
14733 if (sub_strict_overflow_p)
14734 *strict_overflow_p = true;
14735 }
14736 break;
14737
14738 case MAX_EXPR:
14739 sub_strict_overflow_p = false;
14740 if (tree_expr_nonzero_warnv_p (op0,
14741 &sub_strict_overflow_p))
14742 {
14743 if (sub_strict_overflow_p)
14744 *strict_overflow_p = true;
14745
14746 /* When both operands are nonzero, then MAX must be too. */
14747 if (tree_expr_nonzero_warnv_p (op1,
14748 strict_overflow_p))
14749 return true;
14750
14751 /* MAX where operand 0 is positive is positive. */
14752 return tree_expr_nonnegative_warnv_p (op0,
14753 strict_overflow_p);
14754 }
14755 /* MAX where operand 1 is positive is positive. */
14756 else if (tree_expr_nonzero_warnv_p (op1,
14757 &sub_strict_overflow_p)
14758 && tree_expr_nonnegative_warnv_p (op1,
14759 &sub_strict_overflow_p))
14760 {
14761 if (sub_strict_overflow_p)
14762 *strict_overflow_p = true;
14763 return true;
14764 }
14765 break;
14766
14767 case BIT_IOR_EXPR:
14768 return (tree_expr_nonzero_warnv_p (op1,
14769 strict_overflow_p)
14770 || tree_expr_nonzero_warnv_p (op0,
14771 strict_overflow_p));
14772
14773 default:
14774 break;
14775 }
14776
14777 return false;
14778 }
14779
14780 /* Return true when T is an address and is known to be nonzero.
14781 For floating point we further ensure that T is not denormal.
14782 Similar logic is present in nonzero_address in rtlanal.h.
14783
14784 If the return value is based on the assumption that signed overflow
14785 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14786 change *STRICT_OVERFLOW_P. */
14787
14788 bool
14789 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14790 {
14791 bool sub_strict_overflow_p;
14792 switch (TREE_CODE (t))
14793 {
14794 case INTEGER_CST:
14795 return !integer_zerop (t);
14796
14797 case ADDR_EXPR:
14798 {
14799 tree base = TREE_OPERAND (t, 0);
14800
14801 if (!DECL_P (base))
14802 base = get_base_address (base);
14803
14804 if (!base)
14805 return false;
14806
14807 /* For objects in symbol table check if we know they are non-zero.
14808 Don't do anything for variables and functions before symtab is built;
14809 it is quite possible that they will be declared weak later. */
14810 if (DECL_P (base) && decl_in_symtab_p (base))
14811 {
14812 struct symtab_node *symbol;
14813
14814 symbol = symtab_node::get_create (base);
14815 if (symbol)
14816 return symbol->nonzero_address ();
14817 else
14818 return false;
14819 }
14820
14821 /* Function local objects are never NULL. */
14822 if (DECL_P (base)
14823 && (DECL_CONTEXT (base)
14824 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
14825 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
14826 return true;
14827
14828 /* Constants are never weak. */
14829 if (CONSTANT_CLASS_P (base))
14830 return true;
14831
14832 return false;
14833 }
14834
14835 case COND_EXPR:
14836 sub_strict_overflow_p = false;
14837 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14838 &sub_strict_overflow_p)
14839 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14840 &sub_strict_overflow_p))
14841 {
14842 if (sub_strict_overflow_p)
14843 *strict_overflow_p = true;
14844 return true;
14845 }
14846 break;
14847
14848 default:
14849 break;
14850 }
14851 return false;
14852 }
14853
14854 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14855 attempt to fold the expression to a constant without modifying TYPE,
14856 OP0 or OP1.
14857
14858 If the expression could be simplified to a constant, then return
14859 the constant. If the expression would not be simplified to a
14860 constant, then return NULL_TREE. */
14861
14862 tree
14863 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14864 {
14865 tree tem = fold_binary (code, type, op0, op1);
14866 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14867 }
14868
14869 /* Given the components of a unary expression CODE, TYPE and OP0,
14870 attempt to fold the expression to a constant without modifying
14871 TYPE or OP0.
14872
14873 If the expression could be simplified to a constant, then return
14874 the constant. If the expression would not be simplified to a
14875 constant, then return NULL_TREE. */
14876
14877 tree
14878 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14879 {
14880 tree tem = fold_unary (code, type, op0);
14881 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14882 }
14883
14884 /* If EXP represents referencing an element in a constant string
14885 (either via pointer arithmetic or array indexing), return the
14886 tree representing the value accessed, otherwise return NULL. */
14887
14888 tree
14889 fold_read_from_constant_string (tree exp)
14890 {
14891 if ((TREE_CODE (exp) == INDIRECT_REF
14892 || TREE_CODE (exp) == ARRAY_REF)
14893 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14894 {
14895 tree exp1 = TREE_OPERAND (exp, 0);
14896 tree index;
14897 tree string;
14898 location_t loc = EXPR_LOCATION (exp);
14899
14900 if (TREE_CODE (exp) == INDIRECT_REF)
14901 string = string_constant (exp1, &index);
14902 else
14903 {
14904 tree low_bound = array_ref_low_bound (exp);
14905 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
14906
14907 /* Optimize the special-case of a zero lower bound.
14908
14909 We convert the low_bound to sizetype to avoid some problems
14910 with constant folding. (E.g. suppose the lower bound is 1,
14911 and its mode is QI. Without the conversion,l (ARRAY
14912 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14913 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
14914 if (! integer_zerop (low_bound))
14915 index = size_diffop_loc (loc, index,
14916 fold_convert_loc (loc, sizetype, low_bound));
14917
14918 string = exp1;
14919 }
14920
14921 if (string
14922 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14923 && TREE_CODE (string) == STRING_CST
14924 && TREE_CODE (index) == INTEGER_CST
14925 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14926 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14927 == MODE_INT)
14928 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14929 return build_int_cst_type (TREE_TYPE (exp),
14930 (TREE_STRING_POINTER (string)
14931 [TREE_INT_CST_LOW (index)]));
14932 }
14933 return NULL;
14934 }
14935
14936 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14937 an integer constant, real, or fixed-point constant.
14938
14939 TYPE is the type of the result. */
14940
14941 static tree
14942 fold_negate_const (tree arg0, tree type)
14943 {
14944 tree t = NULL_TREE;
14945
14946 switch (TREE_CODE (arg0))
14947 {
14948 case INTEGER_CST:
14949 {
14950 bool overflow;
14951 wide_int val = wi::neg (arg0, &overflow);
14952 t = force_fit_type (type, val, 1,
14953 (overflow | TREE_OVERFLOW (arg0))
14954 && !TYPE_UNSIGNED (type));
14955 break;
14956 }
14957
14958 case REAL_CST:
14959 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
14960 break;
14961
14962 case FIXED_CST:
14963 {
14964 FIXED_VALUE_TYPE f;
14965 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14966 &(TREE_FIXED_CST (arg0)), NULL,
14967 TYPE_SATURATING (type));
14968 t = build_fixed (type, f);
14969 /* Propagate overflow flags. */
14970 if (overflow_p | TREE_OVERFLOW (arg0))
14971 TREE_OVERFLOW (t) = 1;
14972 break;
14973 }
14974
14975 default:
14976 gcc_unreachable ();
14977 }
14978
14979 return t;
14980 }
14981
14982 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14983 an integer constant or real constant.
14984
14985 TYPE is the type of the result. */
14986
14987 tree
14988 fold_abs_const (tree arg0, tree type)
14989 {
14990 tree t = NULL_TREE;
14991
14992 switch (TREE_CODE (arg0))
14993 {
14994 case INTEGER_CST:
14995 {
14996 /* If the value is unsigned or non-negative, then the absolute value
14997 is the same as the ordinary value. */
14998 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
14999 t = arg0;
15000
15001 /* If the value is negative, then the absolute value is
15002 its negation. */
15003 else
15004 {
15005 bool overflow;
15006 wide_int val = wi::neg (arg0, &overflow);
15007 t = force_fit_type (type, val, -1,
15008 overflow | TREE_OVERFLOW (arg0));
15009 }
15010 }
15011 break;
15012
15013 case REAL_CST:
15014 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15015 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15016 else
15017 t = arg0;
15018 break;
15019
15020 default:
15021 gcc_unreachable ();
15022 }
15023
15024 return t;
15025 }
15026
15027 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15028 constant. TYPE is the type of the result. */
15029
15030 static tree
15031 fold_not_const (const_tree arg0, tree type)
15032 {
15033 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15034
15035 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
15036 }
15037
15038 /* Given CODE, a relational operator, the target type, TYPE and two
15039 constant operands OP0 and OP1, return the result of the
15040 relational operation. If the result is not a compile time
15041 constant, then return NULL_TREE. */
15042
15043 static tree
15044 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15045 {
15046 int result, invert;
15047
15048 /* From here on, the only cases we handle are when the result is
15049 known to be a constant. */
15050
15051 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15052 {
15053 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15054 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15055
15056 /* Handle the cases where either operand is a NaN. */
15057 if (real_isnan (c0) || real_isnan (c1))
15058 {
15059 switch (code)
15060 {
15061 case EQ_EXPR:
15062 case ORDERED_EXPR:
15063 result = 0;
15064 break;
15065
15066 case NE_EXPR:
15067 case UNORDERED_EXPR:
15068 case UNLT_EXPR:
15069 case UNLE_EXPR:
15070 case UNGT_EXPR:
15071 case UNGE_EXPR:
15072 case UNEQ_EXPR:
15073 result = 1;
15074 break;
15075
15076 case LT_EXPR:
15077 case LE_EXPR:
15078 case GT_EXPR:
15079 case GE_EXPR:
15080 case LTGT_EXPR:
15081 if (flag_trapping_math)
15082 return NULL_TREE;
15083 result = 0;
15084 break;
15085
15086 default:
15087 gcc_unreachable ();
15088 }
15089
15090 return constant_boolean_node (result, type);
15091 }
15092
15093 return constant_boolean_node (real_compare (code, c0, c1), type);
15094 }
15095
15096 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15097 {
15098 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15099 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15100 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15101 }
15102
15103 /* Handle equality/inequality of complex constants. */
15104 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15105 {
15106 tree rcond = fold_relational_const (code, type,
15107 TREE_REALPART (op0),
15108 TREE_REALPART (op1));
15109 tree icond = fold_relational_const (code, type,
15110 TREE_IMAGPART (op0),
15111 TREE_IMAGPART (op1));
15112 if (code == EQ_EXPR)
15113 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15114 else if (code == NE_EXPR)
15115 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15116 else
15117 return NULL_TREE;
15118 }
15119
15120 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15121 {
15122 unsigned count = VECTOR_CST_NELTS (op0);
15123 tree *elts = XALLOCAVEC (tree, count);
15124 gcc_assert (VECTOR_CST_NELTS (op1) == count
15125 && TYPE_VECTOR_SUBPARTS (type) == count);
15126
15127 for (unsigned i = 0; i < count; i++)
15128 {
15129 tree elem_type = TREE_TYPE (type);
15130 tree elem0 = VECTOR_CST_ELT (op0, i);
15131 tree elem1 = VECTOR_CST_ELT (op1, i);
15132
15133 tree tem = fold_relational_const (code, elem_type,
15134 elem0, elem1);
15135
15136 if (tem == NULL_TREE)
15137 return NULL_TREE;
15138
15139 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
15140 }
15141
15142 return build_vector (type, elts);
15143 }
15144
15145 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15146
15147 To compute GT, swap the arguments and do LT.
15148 To compute GE, do LT and invert the result.
15149 To compute LE, swap the arguments, do LT and invert the result.
15150 To compute NE, do EQ and invert the result.
15151
15152 Therefore, the code below must handle only EQ and LT. */
15153
15154 if (code == LE_EXPR || code == GT_EXPR)
15155 {
15156 std::swap (op0, op1);
15157 code = swap_tree_comparison (code);
15158 }
15159
15160 /* Note that it is safe to invert for real values here because we
15161 have already handled the one case that it matters. */
15162
15163 invert = 0;
15164 if (code == NE_EXPR || code == GE_EXPR)
15165 {
15166 invert = 1;
15167 code = invert_tree_comparison (code, false);
15168 }
15169
15170 /* Compute a result for LT or EQ if args permit;
15171 Otherwise return T. */
15172 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15173 {
15174 if (code == EQ_EXPR)
15175 result = tree_int_cst_equal (op0, op1);
15176 else
15177 result = tree_int_cst_lt (op0, op1);
15178 }
15179 else
15180 return NULL_TREE;
15181
15182 if (invert)
15183 result ^= 1;
15184 return constant_boolean_node (result, type);
15185 }
15186
15187 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15188 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15189 itself. */
15190
15191 tree
15192 fold_build_cleanup_point_expr (tree type, tree expr)
15193 {
15194 /* If the expression does not have side effects then we don't have to wrap
15195 it with a cleanup point expression. */
15196 if (!TREE_SIDE_EFFECTS (expr))
15197 return expr;
15198
15199 /* If the expression is a return, check to see if the expression inside the
15200 return has no side effects or the right hand side of the modify expression
15201 inside the return. If either don't have side effects set we don't need to
15202 wrap the expression in a cleanup point expression. Note we don't check the
15203 left hand side of the modify because it should always be a return decl. */
15204 if (TREE_CODE (expr) == RETURN_EXPR)
15205 {
15206 tree op = TREE_OPERAND (expr, 0);
15207 if (!op || !TREE_SIDE_EFFECTS (op))
15208 return expr;
15209 op = TREE_OPERAND (op, 1);
15210 if (!TREE_SIDE_EFFECTS (op))
15211 return expr;
15212 }
15213
15214 return build1 (CLEANUP_POINT_EXPR, type, expr);
15215 }
15216
15217 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15218 of an indirection through OP0, or NULL_TREE if no simplification is
15219 possible. */
15220
15221 tree
15222 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15223 {
15224 tree sub = op0;
15225 tree subtype;
15226
15227 STRIP_NOPS (sub);
15228 subtype = TREE_TYPE (sub);
15229 if (!POINTER_TYPE_P (subtype))
15230 return NULL_TREE;
15231
15232 if (TREE_CODE (sub) == ADDR_EXPR)
15233 {
15234 tree op = TREE_OPERAND (sub, 0);
15235 tree optype = TREE_TYPE (op);
15236 /* *&CONST_DECL -> to the value of the const decl. */
15237 if (TREE_CODE (op) == CONST_DECL)
15238 return DECL_INITIAL (op);
15239 /* *&p => p; make sure to handle *&"str"[cst] here. */
15240 if (type == optype)
15241 {
15242 tree fop = fold_read_from_constant_string (op);
15243 if (fop)
15244 return fop;
15245 else
15246 return op;
15247 }
15248 /* *(foo *)&fooarray => fooarray[0] */
15249 else if (TREE_CODE (optype) == ARRAY_TYPE
15250 && type == TREE_TYPE (optype)
15251 && (!in_gimple_form
15252 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15253 {
15254 tree type_domain = TYPE_DOMAIN (optype);
15255 tree min_val = size_zero_node;
15256 if (type_domain && TYPE_MIN_VALUE (type_domain))
15257 min_val = TYPE_MIN_VALUE (type_domain);
15258 if (in_gimple_form
15259 && TREE_CODE (min_val) != INTEGER_CST)
15260 return NULL_TREE;
15261 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15262 NULL_TREE, NULL_TREE);
15263 }
15264 /* *(foo *)&complexfoo => __real__ complexfoo */
15265 else if (TREE_CODE (optype) == COMPLEX_TYPE
15266 && type == TREE_TYPE (optype))
15267 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15268 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15269 else if (TREE_CODE (optype) == VECTOR_TYPE
15270 && type == TREE_TYPE (optype))
15271 {
15272 tree part_width = TYPE_SIZE (type);
15273 tree index = bitsize_int (0);
15274 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15275 }
15276 }
15277
15278 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15279 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15280 {
15281 tree op00 = TREE_OPERAND (sub, 0);
15282 tree op01 = TREE_OPERAND (sub, 1);
15283
15284 STRIP_NOPS (op00);
15285 if (TREE_CODE (op00) == ADDR_EXPR)
15286 {
15287 tree op00type;
15288 op00 = TREE_OPERAND (op00, 0);
15289 op00type = TREE_TYPE (op00);
15290
15291 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15292 if (TREE_CODE (op00type) == VECTOR_TYPE
15293 && type == TREE_TYPE (op00type))
15294 {
15295 HOST_WIDE_INT offset = tree_to_shwi (op01);
15296 tree part_width = TYPE_SIZE (type);
15297 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
15298 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15299 tree index = bitsize_int (indexi);
15300
15301 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
15302 return fold_build3_loc (loc,
15303 BIT_FIELD_REF, type, op00,
15304 part_width, index);
15305
15306 }
15307 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15308 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15309 && type == TREE_TYPE (op00type))
15310 {
15311 tree size = TYPE_SIZE_UNIT (type);
15312 if (tree_int_cst_equal (size, op01))
15313 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15314 }
15315 /* ((foo *)&fooarray)[1] => fooarray[1] */
15316 else if (TREE_CODE (op00type) == ARRAY_TYPE
15317 && type == TREE_TYPE (op00type))
15318 {
15319 tree type_domain = TYPE_DOMAIN (op00type);
15320 tree min_val = size_zero_node;
15321 if (type_domain && TYPE_MIN_VALUE (type_domain))
15322 min_val = TYPE_MIN_VALUE (type_domain);
15323 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15324 TYPE_SIZE_UNIT (type));
15325 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15326 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15327 NULL_TREE, NULL_TREE);
15328 }
15329 }
15330 }
15331
15332 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15333 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15334 && type == TREE_TYPE (TREE_TYPE (subtype))
15335 && (!in_gimple_form
15336 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15337 {
15338 tree type_domain;
15339 tree min_val = size_zero_node;
15340 sub = build_fold_indirect_ref_loc (loc, sub);
15341 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15342 if (type_domain && TYPE_MIN_VALUE (type_domain))
15343 min_val = TYPE_MIN_VALUE (type_domain);
15344 if (in_gimple_form
15345 && TREE_CODE (min_val) != INTEGER_CST)
15346 return NULL_TREE;
15347 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15348 NULL_TREE);
15349 }
15350
15351 return NULL_TREE;
15352 }
15353
15354 /* Builds an expression for an indirection through T, simplifying some
15355 cases. */
15356
15357 tree
15358 build_fold_indirect_ref_loc (location_t loc, tree t)
15359 {
15360 tree type = TREE_TYPE (TREE_TYPE (t));
15361 tree sub = fold_indirect_ref_1 (loc, type, t);
15362
15363 if (sub)
15364 return sub;
15365
15366 return build1_loc (loc, INDIRECT_REF, type, t);
15367 }
15368
15369 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15370
15371 tree
15372 fold_indirect_ref_loc (location_t loc, tree t)
15373 {
15374 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15375
15376 if (sub)
15377 return sub;
15378 else
15379 return t;
15380 }
15381
15382 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15383 whose result is ignored. The type of the returned tree need not be
15384 the same as the original expression. */
15385
15386 tree
15387 fold_ignored_result (tree t)
15388 {
15389 if (!TREE_SIDE_EFFECTS (t))
15390 return integer_zero_node;
15391
15392 for (;;)
15393 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15394 {
15395 case tcc_unary:
15396 t = TREE_OPERAND (t, 0);
15397 break;
15398
15399 case tcc_binary:
15400 case tcc_comparison:
15401 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15402 t = TREE_OPERAND (t, 0);
15403 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15404 t = TREE_OPERAND (t, 1);
15405 else
15406 return t;
15407 break;
15408
15409 case tcc_expression:
15410 switch (TREE_CODE (t))
15411 {
15412 case COMPOUND_EXPR:
15413 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15414 return t;
15415 t = TREE_OPERAND (t, 0);
15416 break;
15417
15418 case COND_EXPR:
15419 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15420 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15421 return t;
15422 t = TREE_OPERAND (t, 0);
15423 break;
15424
15425 default:
15426 return t;
15427 }
15428 break;
15429
15430 default:
15431 return t;
15432 }
15433 }
15434
15435 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15436
15437 tree
15438 round_up_loc (location_t loc, tree value, unsigned int divisor)
15439 {
15440 tree div = NULL_TREE;
15441
15442 if (divisor == 1)
15443 return value;
15444
15445 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15446 have to do anything. Only do this when we are not given a const,
15447 because in that case, this check is more expensive than just
15448 doing it. */
15449 if (TREE_CODE (value) != INTEGER_CST)
15450 {
15451 div = build_int_cst (TREE_TYPE (value), divisor);
15452
15453 if (multiple_of_p (TREE_TYPE (value), value, div))
15454 return value;
15455 }
15456
15457 /* If divisor is a power of two, simplify this to bit manipulation. */
15458 if (divisor == (divisor & -divisor))
15459 {
15460 if (TREE_CODE (value) == INTEGER_CST)
15461 {
15462 wide_int val = value;
15463 bool overflow_p;
15464
15465 if ((val & (divisor - 1)) == 0)
15466 return value;
15467
15468 overflow_p = TREE_OVERFLOW (value);
15469 val += divisor - 1;
15470 val &= - (int) divisor;
15471 if (val == 0)
15472 overflow_p = true;
15473
15474 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
15475 }
15476 else
15477 {
15478 tree t;
15479
15480 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15481 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15482 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
15483 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15484 }
15485 }
15486 else
15487 {
15488 if (!div)
15489 div = build_int_cst (TREE_TYPE (value), divisor);
15490 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15491 value = size_binop_loc (loc, MULT_EXPR, value, div);
15492 }
15493
15494 return value;
15495 }
15496
15497 /* Likewise, but round down. */
15498
15499 tree
15500 round_down_loc (location_t loc, tree value, int divisor)
15501 {
15502 tree div = NULL_TREE;
15503
15504 gcc_assert (divisor > 0);
15505 if (divisor == 1)
15506 return value;
15507
15508 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15509 have to do anything. Only do this when we are not given a const,
15510 because in that case, this check is more expensive than just
15511 doing it. */
15512 if (TREE_CODE (value) != INTEGER_CST)
15513 {
15514 div = build_int_cst (TREE_TYPE (value), divisor);
15515
15516 if (multiple_of_p (TREE_TYPE (value), value, div))
15517 return value;
15518 }
15519
15520 /* If divisor is a power of two, simplify this to bit manipulation. */
15521 if (divisor == (divisor & -divisor))
15522 {
15523 tree t;
15524
15525 t = build_int_cst (TREE_TYPE (value), -divisor);
15526 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15527 }
15528 else
15529 {
15530 if (!div)
15531 div = build_int_cst (TREE_TYPE (value), divisor);
15532 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15533 value = size_binop_loc (loc, MULT_EXPR, value, div);
15534 }
15535
15536 return value;
15537 }
15538
15539 /* Returns the pointer to the base of the object addressed by EXP and
15540 extracts the information about the offset of the access, storing it
15541 to PBITPOS and POFFSET. */
15542
15543 static tree
15544 split_address_to_core_and_offset (tree exp,
15545 HOST_WIDE_INT *pbitpos, tree *poffset)
15546 {
15547 tree core;
15548 machine_mode mode;
15549 int unsignedp, volatilep;
15550 HOST_WIDE_INT bitsize;
15551 location_t loc = EXPR_LOCATION (exp);
15552
15553 if (TREE_CODE (exp) == ADDR_EXPR)
15554 {
15555 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15556 poffset, &mode, &unsignedp, &volatilep,
15557 false);
15558 core = build_fold_addr_expr_loc (loc, core);
15559 }
15560 else
15561 {
15562 core = exp;
15563 *pbitpos = 0;
15564 *poffset = NULL_TREE;
15565 }
15566
15567 return core;
15568 }
15569
15570 /* Returns true if addresses of E1 and E2 differ by a constant, false
15571 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15572
15573 bool
15574 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15575 {
15576 tree core1, core2;
15577 HOST_WIDE_INT bitpos1, bitpos2;
15578 tree toffset1, toffset2, tdiff, type;
15579
15580 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15581 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15582
15583 if (bitpos1 % BITS_PER_UNIT != 0
15584 || bitpos2 % BITS_PER_UNIT != 0
15585 || !operand_equal_p (core1, core2, 0))
15586 return false;
15587
15588 if (toffset1 && toffset2)
15589 {
15590 type = TREE_TYPE (toffset1);
15591 if (type != TREE_TYPE (toffset2))
15592 toffset2 = fold_convert (type, toffset2);
15593
15594 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15595 if (!cst_and_fits_in_hwi (tdiff))
15596 return false;
15597
15598 *diff = int_cst_value (tdiff);
15599 }
15600 else if (toffset1 || toffset2)
15601 {
15602 /* If only one of the offsets is non-constant, the difference cannot
15603 be a constant. */
15604 return false;
15605 }
15606 else
15607 *diff = 0;
15608
15609 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15610 return true;
15611 }
15612
15613 /* Simplify the floating point expression EXP when the sign of the
15614 result is not significant. Return NULL_TREE if no simplification
15615 is possible. */
15616
15617 tree
15618 fold_strip_sign_ops (tree exp)
15619 {
15620 tree arg0, arg1;
15621 location_t loc = EXPR_LOCATION (exp);
15622
15623 switch (TREE_CODE (exp))
15624 {
15625 case ABS_EXPR:
15626 case NEGATE_EXPR:
15627 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15628 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15629
15630 case MULT_EXPR:
15631 case RDIV_EXPR:
15632 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
15633 return NULL_TREE;
15634 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15635 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15636 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15637 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15638 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15639 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15640 break;
15641
15642 case COMPOUND_EXPR:
15643 arg0 = TREE_OPERAND (exp, 0);
15644 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15645 if (arg1)
15646 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15647 break;
15648
15649 case COND_EXPR:
15650 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15651 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15652 if (arg0 || arg1)
15653 return fold_build3_loc (loc,
15654 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15655 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15656 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15657 break;
15658
15659 case CALL_EXPR:
15660 {
15661 const enum built_in_function fcode = builtin_mathfn_code (exp);
15662 switch (fcode)
15663 {
15664 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15665 /* Strip copysign function call, return the 1st argument. */
15666 arg0 = CALL_EXPR_ARG (exp, 0);
15667 arg1 = CALL_EXPR_ARG (exp, 1);
15668 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
15669
15670 default:
15671 /* Strip sign ops from the argument of "odd" math functions. */
15672 if (negate_mathfn_p (fcode))
15673 {
15674 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15675 if (arg0)
15676 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
15677 }
15678 break;
15679 }
15680 }
15681 break;
15682
15683 default:
15684 break;
15685 }
15686 return NULL_TREE;
15687 }
15688
15689 /* Return OFF converted to a pointer offset type suitable as offset for
15690 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
15691 tree
15692 convert_to_ptrofftype_loc (location_t loc, tree off)
15693 {
15694 return fold_convert_loc (loc, sizetype, off);
15695 }
15696
15697 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
15698 tree
15699 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
15700 {
15701 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
15702 ptr, convert_to_ptrofftype_loc (loc, off));
15703 }
15704
15705 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
15706 tree
15707 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
15708 {
15709 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
15710 ptr, size_int (off));
15711 }