fold-const.c (fold_widened_comparison): Remove.
[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 "backend.h"
47 #include "predict.h"
48 #include "tree.h"
49 #include "gimple.h"
50 #include "rtl.h"
51 #include "flags.h"
52 #include "alias.h"
53 #include "fold-const.h"
54 #include "stor-layout.h"
55 #include "calls.h"
56 #include "tree-iterator.h"
57 #include "realmpfr.h"
58 #include "insn-config.h"
59 #include "expmed.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "emit-rtl.h"
63 #include "varasm.h"
64 #include "stmt.h"
65 #include "expr.h"
66 #include "tm_p.h"
67 #include "target.h"
68 #include "diagnostic-core.h"
69 #include "intl.h"
70 #include "langhooks.h"
71 #include "md5.h"
72 #include "internal-fn.h"
73 #include "tree-eh.h"
74 #include "gimplify.h"
75 #include "tree-dfa.h"
76 #include "builtins.h"
77 #include "cgraph.h"
78 #include "generic-match.h"
79 #include "optabs.h"
80
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
83 #endif
84
85 /* Nonzero if we are folding constants inside an initializer; zero
86 otherwise. */
87 int folding_initializer = 0;
88
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
93 COMPCODE_FALSE = 0,
94 COMPCODE_LT = 1,
95 COMPCODE_EQ = 2,
96 COMPCODE_LE = 3,
97 COMPCODE_GT = 4,
98 COMPCODE_LTGT = 5,
99 COMPCODE_GE = 6,
100 COMPCODE_ORD = 7,
101 COMPCODE_UNORD = 8,
102 COMPCODE_UNLT = 9,
103 COMPCODE_UNEQ = 10,
104 COMPCODE_UNLE = 11,
105 COMPCODE_UNGT = 12,
106 COMPCODE_NE = 13,
107 COMPCODE_UNGE = 14,
108 COMPCODE_TRUE = 15
109 };
110
111 static bool negate_mathfn_p (enum built_in_function);
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int operand_equal_for_comparison_p (tree, tree, tree);
119 static int twoval_comparison_p (tree, tree *, tree *, int *);
120 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
121 static tree make_bit_field_ref (location_t, tree, tree,
122 HOST_WIDE_INT, HOST_WIDE_INT, int);
123 static tree optimize_bit_field_compare (location_t, enum tree_code,
124 tree, tree, tree);
125 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
126 HOST_WIDE_INT *,
127 machine_mode *, int *, int *,
128 tree *, tree *);
129 static int simple_operand_p (const_tree);
130 static bool simple_operand_p_2 (tree);
131 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
132 static tree range_predecessor (tree);
133 static tree range_successor (tree);
134 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
135 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
136 static tree unextend (tree, int, int, tree);
137 static tree optimize_minmax_comparison (location_t, enum tree_code,
138 tree, tree, tree);
139 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
140 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
141 static tree fold_binary_op_with_conditional_arg (location_t,
142 enum tree_code, tree,
143 tree, tree,
144 tree, tree, int);
145 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
146 static bool reorder_operands_p (const_tree, const_tree);
147 static tree fold_negate_const (tree, tree);
148 static tree fold_not_const (const_tree, tree);
149 static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 static tree fold_convert_const (enum tree_code, tree, tree);
151 static tree fold_view_convert_expr (tree, tree);
152 static bool vec_cst_ctor_to_array (tree, tree *);
153
154
155 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
156 Otherwise, return LOC. */
157
158 static location_t
159 expr_location_or (tree t, location_t loc)
160 {
161 location_t tloc = EXPR_LOCATION (t);
162 return tloc == UNKNOWN_LOCATION ? loc : tloc;
163 }
164
165 /* Similar to protected_set_expr_location, but never modify x in place,
166 if location can and needs to be set, unshare it. */
167
168 static inline tree
169 protected_set_expr_location_unshare (tree x, location_t loc)
170 {
171 if (CAN_HAVE_LOCATION_P (x)
172 && EXPR_LOCATION (x) != loc
173 && !(TREE_CODE (x) == SAVE_EXPR
174 || TREE_CODE (x) == TARGET_EXPR
175 || TREE_CODE (x) == BIND_EXPR))
176 {
177 x = copy_node (x);
178 SET_EXPR_LOCATION (x, loc);
179 }
180 return x;
181 }
182 \f
183 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
184 division and returns the quotient. Otherwise returns
185 NULL_TREE. */
186
187 tree
188 div_if_zero_remainder (const_tree arg1, const_tree arg2)
189 {
190 widest_int quo;
191
192 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
193 SIGNED, &quo))
194 return wide_int_to_tree (TREE_TYPE (arg1), quo);
195
196 return NULL_TREE;
197 }
198 \f
199 /* This is nonzero if we should defer warnings about undefined
200 overflow. This facility exists because these warnings are a
201 special case. The code to estimate loop iterations does not want
202 to issue any warnings, since it works with expressions which do not
203 occur in user code. Various bits of cleanup code call fold(), but
204 only use the result if it has certain characteristics (e.g., is a
205 constant); that code only wants to issue a warning if the result is
206 used. */
207
208 static int fold_deferring_overflow_warnings;
209
210 /* If a warning about undefined overflow is deferred, this is the
211 warning. Note that this may cause us to turn two warnings into
212 one, but that is fine since it is sufficient to only give one
213 warning per expression. */
214
215 static const char* fold_deferred_overflow_warning;
216
217 /* If a warning about undefined overflow is deferred, this is the
218 level at which the warning should be emitted. */
219
220 static enum warn_strict_overflow_code fold_deferred_overflow_code;
221
222 /* Start deferring overflow warnings. We could use a stack here to
223 permit nested calls, but at present it is not necessary. */
224
225 void
226 fold_defer_overflow_warnings (void)
227 {
228 ++fold_deferring_overflow_warnings;
229 }
230
231 /* Stop deferring overflow warnings. If there is a pending warning,
232 and ISSUE is true, then issue the warning if appropriate. STMT is
233 the statement with which the warning should be associated (used for
234 location information); STMT may be NULL. CODE is the level of the
235 warning--a warn_strict_overflow_code value. This function will use
236 the smaller of CODE and the deferred code when deciding whether to
237 issue the warning. CODE may be zero to mean to always use the
238 deferred code. */
239
240 void
241 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
242 {
243 const char *warnmsg;
244 location_t locus;
245
246 gcc_assert (fold_deferring_overflow_warnings > 0);
247 --fold_deferring_overflow_warnings;
248 if (fold_deferring_overflow_warnings > 0)
249 {
250 if (fold_deferred_overflow_warning != NULL
251 && code != 0
252 && code < (int) fold_deferred_overflow_code)
253 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
254 return;
255 }
256
257 warnmsg = fold_deferred_overflow_warning;
258 fold_deferred_overflow_warning = NULL;
259
260 if (!issue || warnmsg == NULL)
261 return;
262
263 if (gimple_no_warning_p (stmt))
264 return;
265
266 /* Use the smallest code level when deciding to issue the
267 warning. */
268 if (code == 0 || code > (int) fold_deferred_overflow_code)
269 code = fold_deferred_overflow_code;
270
271 if (!issue_strict_overflow_warning (code))
272 return;
273
274 if (stmt == NULL)
275 locus = input_location;
276 else
277 locus = gimple_location (stmt);
278 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
279 }
280
281 /* Stop deferring overflow warnings, ignoring any deferred
282 warnings. */
283
284 void
285 fold_undefer_and_ignore_overflow_warnings (void)
286 {
287 fold_undefer_overflow_warnings (false, NULL, 0);
288 }
289
290 /* Whether we are deferring overflow warnings. */
291
292 bool
293 fold_deferring_overflow_warnings_p (void)
294 {
295 return fold_deferring_overflow_warnings > 0;
296 }
297
298 /* This is called when we fold something based on the fact that signed
299 overflow is undefined. */
300
301 static void
302 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
303 {
304 if (fold_deferring_overflow_warnings > 0)
305 {
306 if (fold_deferred_overflow_warning == NULL
307 || wc < fold_deferred_overflow_code)
308 {
309 fold_deferred_overflow_warning = gmsgid;
310 fold_deferred_overflow_code = wc;
311 }
312 }
313 else if (issue_strict_overflow_warning (wc))
314 warning (OPT_Wstrict_overflow, gmsgid);
315 }
316 \f
317 /* Return true if the built-in mathematical function specified by CODE
318 is odd, i.e. -f(x) == f(-x). */
319
320 static bool
321 negate_mathfn_p (enum built_in_function code)
322 {
323 switch (code)
324 {
325 CASE_FLT_FN (BUILT_IN_ASIN):
326 CASE_FLT_FN (BUILT_IN_ASINH):
327 CASE_FLT_FN (BUILT_IN_ATAN):
328 CASE_FLT_FN (BUILT_IN_ATANH):
329 CASE_FLT_FN (BUILT_IN_CASIN):
330 CASE_FLT_FN (BUILT_IN_CASINH):
331 CASE_FLT_FN (BUILT_IN_CATAN):
332 CASE_FLT_FN (BUILT_IN_CATANH):
333 CASE_FLT_FN (BUILT_IN_CBRT):
334 CASE_FLT_FN (BUILT_IN_CPROJ):
335 CASE_FLT_FN (BUILT_IN_CSIN):
336 CASE_FLT_FN (BUILT_IN_CSINH):
337 CASE_FLT_FN (BUILT_IN_CTAN):
338 CASE_FLT_FN (BUILT_IN_CTANH):
339 CASE_FLT_FN (BUILT_IN_ERF):
340 CASE_FLT_FN (BUILT_IN_LLROUND):
341 CASE_FLT_FN (BUILT_IN_LROUND):
342 CASE_FLT_FN (BUILT_IN_ROUND):
343 CASE_FLT_FN (BUILT_IN_SIN):
344 CASE_FLT_FN (BUILT_IN_SINH):
345 CASE_FLT_FN (BUILT_IN_TAN):
346 CASE_FLT_FN (BUILT_IN_TANH):
347 CASE_FLT_FN (BUILT_IN_TRUNC):
348 return true;
349
350 CASE_FLT_FN (BUILT_IN_LLRINT):
351 CASE_FLT_FN (BUILT_IN_LRINT):
352 CASE_FLT_FN (BUILT_IN_NEARBYINT):
353 CASE_FLT_FN (BUILT_IN_RINT):
354 return !flag_rounding_math;
355
356 default:
357 break;
358 }
359 return false;
360 }
361
362 /* Check whether we may negate an integer constant T without causing
363 overflow. */
364
365 bool
366 may_negate_without_overflow_p (const_tree t)
367 {
368 tree type;
369
370 gcc_assert (TREE_CODE (t) == INTEGER_CST);
371
372 type = TREE_TYPE (t);
373 if (TYPE_UNSIGNED (type))
374 return false;
375
376 return !wi::only_sign_bit_p (t);
377 }
378
379 /* Determine whether an expression T can be cheaply negated using
380 the function negate_expr without introducing undefined overflow. */
381
382 static bool
383 negate_expr_p (tree t)
384 {
385 tree type;
386
387 if (t == 0)
388 return false;
389
390 type = TREE_TYPE (t);
391
392 STRIP_SIGN_NOPS (t);
393 switch (TREE_CODE (t))
394 {
395 case INTEGER_CST:
396 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
397 return true;
398
399 /* Check that -CST will not overflow type. */
400 return may_negate_without_overflow_p (t);
401 case BIT_NOT_EXPR:
402 return (INTEGRAL_TYPE_P (type)
403 && TYPE_OVERFLOW_WRAPS (type));
404
405 case FIXED_CST:
406 return true;
407
408 case NEGATE_EXPR:
409 return !TYPE_OVERFLOW_SANITIZED (type);
410
411 case REAL_CST:
412 /* We want to canonicalize to positive real constants. Pretend
413 that only negative ones can be easily negated. */
414 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
415
416 case COMPLEX_CST:
417 return negate_expr_p (TREE_REALPART (t))
418 && negate_expr_p (TREE_IMAGPART (t));
419
420 case VECTOR_CST:
421 {
422 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
423 return true;
424
425 int count = TYPE_VECTOR_SUBPARTS (type), i;
426
427 for (i = 0; i < count; i++)
428 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
429 return false;
430
431 return true;
432 }
433
434 case COMPLEX_EXPR:
435 return negate_expr_p (TREE_OPERAND (t, 0))
436 && negate_expr_p (TREE_OPERAND (t, 1));
437
438 case CONJ_EXPR:
439 return negate_expr_p (TREE_OPERAND (t, 0));
440
441 case PLUS_EXPR:
442 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
443 || HONOR_SIGNED_ZEROS (element_mode (type)))
444 return false;
445 /* -(A + B) -> (-B) - A. */
446 if (negate_expr_p (TREE_OPERAND (t, 1))
447 && reorder_operands_p (TREE_OPERAND (t, 0),
448 TREE_OPERAND (t, 1)))
449 return true;
450 /* -(A + B) -> (-A) - B. */
451 return negate_expr_p (TREE_OPERAND (t, 0));
452
453 case MINUS_EXPR:
454 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
455 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
456 && !HONOR_SIGNED_ZEROS (element_mode (type))
457 && reorder_operands_p (TREE_OPERAND (t, 0),
458 TREE_OPERAND (t, 1));
459
460 case MULT_EXPR:
461 if (TYPE_UNSIGNED (TREE_TYPE (t)))
462 break;
463
464 /* Fall through. */
465
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
471
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 /* In general we can't negate A / B, because if A is INT_MIN and
476 B is 1, we may turn this into INT_MIN / -1 which is undefined
477 and actually traps on some architectures. But if overflow is
478 undefined, we can negate, because - (INT_MIN / 1) is an
479 overflow. */
480 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
481 {
482 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
483 break;
484 /* If overflow is undefined then we have to be careful because
485 we ask whether it's ok to associate the negate with the
486 division which is not ok for example for
487 -((a - b) / c) where (-(a - b)) / c may invoke undefined
488 overflow because of negating INT_MIN. So do not use
489 negate_expr_p here but open-code the two important cases. */
490 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
491 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
492 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
493 return true;
494 }
495 else if (negate_expr_p (TREE_OPERAND (t, 0)))
496 return true;
497 return negate_expr_p (TREE_OPERAND (t, 1));
498
499 case NOP_EXPR:
500 /* Negate -((double)float) as (double)(-float). */
501 if (TREE_CODE (type) == REAL_TYPE)
502 {
503 tree tem = strip_float_extensions (t);
504 if (tem != t)
505 return negate_expr_p (tem);
506 }
507 break;
508
509 case CALL_EXPR:
510 /* Negate -f(x) as f(-x). */
511 if (negate_mathfn_p (builtin_mathfn_code (t)))
512 return negate_expr_p (CALL_EXPR_ARG (t, 0));
513 break;
514
515 case RSHIFT_EXPR:
516 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
517 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
518 {
519 tree op1 = TREE_OPERAND (t, 1);
520 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
521 return true;
522 }
523 break;
524
525 default:
526 break;
527 }
528 return false;
529 }
530
531 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
532 simplification is possible.
533 If negate_expr_p would return true for T, NULL_TREE will never be
534 returned. */
535
536 static tree
537 fold_negate_expr (location_t loc, tree t)
538 {
539 tree type = TREE_TYPE (t);
540 tree tem;
541
542 switch (TREE_CODE (t))
543 {
544 /* Convert - (~A) to A + 1. */
545 case BIT_NOT_EXPR:
546 if (INTEGRAL_TYPE_P (type))
547 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
548 build_one_cst (type));
549 break;
550
551 case INTEGER_CST:
552 tem = fold_negate_const (t, type);
553 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
554 || (ANY_INTEGRAL_TYPE_P (type)
555 && !TYPE_OVERFLOW_TRAPS (type)
556 && TYPE_OVERFLOW_WRAPS (type))
557 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
558 return tem;
559 break;
560
561 case REAL_CST:
562 tem = fold_negate_const (t, type);
563 return tem;
564
565 case FIXED_CST:
566 tem = fold_negate_const (t, type);
567 return tem;
568
569 case COMPLEX_CST:
570 {
571 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
572 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
573 if (rpart && ipart)
574 return build_complex (type, rpart, ipart);
575 }
576 break;
577
578 case VECTOR_CST:
579 {
580 int count = TYPE_VECTOR_SUBPARTS (type), i;
581 tree *elts = XALLOCAVEC (tree, count);
582
583 for (i = 0; i < count; i++)
584 {
585 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
586 if (elts[i] == NULL_TREE)
587 return NULL_TREE;
588 }
589
590 return build_vector (type, elts);
591 }
592
593 case COMPLEX_EXPR:
594 if (negate_expr_p (t))
595 return fold_build2_loc (loc, COMPLEX_EXPR, type,
596 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
597 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
598 break;
599
600 case CONJ_EXPR:
601 if (negate_expr_p (t))
602 return fold_build1_loc (loc, CONJ_EXPR, type,
603 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
604 break;
605
606 case NEGATE_EXPR:
607 if (!TYPE_OVERFLOW_SANITIZED (type))
608 return TREE_OPERAND (t, 0);
609 break;
610
611 case PLUS_EXPR:
612 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
613 && !HONOR_SIGNED_ZEROS (element_mode (type)))
614 {
615 /* -(A + B) -> (-B) - A. */
616 if (negate_expr_p (TREE_OPERAND (t, 1))
617 && reorder_operands_p (TREE_OPERAND (t, 0),
618 TREE_OPERAND (t, 1)))
619 {
620 tem = negate_expr (TREE_OPERAND (t, 1));
621 return fold_build2_loc (loc, MINUS_EXPR, type,
622 tem, TREE_OPERAND (t, 0));
623 }
624
625 /* -(A + B) -> (-A) - B. */
626 if (negate_expr_p (TREE_OPERAND (t, 0)))
627 {
628 tem = negate_expr (TREE_OPERAND (t, 0));
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 tem, TREE_OPERAND (t, 1));
631 }
632 }
633 break;
634
635 case MINUS_EXPR:
636 /* - (A - B) -> B - A */
637 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
638 && !HONOR_SIGNED_ZEROS (element_mode (type))
639 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
640 return fold_build2_loc (loc, MINUS_EXPR, type,
641 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
642 break;
643
644 case MULT_EXPR:
645 if (TYPE_UNSIGNED (type))
646 break;
647
648 /* Fall through. */
649
650 case RDIV_EXPR:
651 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
652 {
653 tem = TREE_OPERAND (t, 1);
654 if (negate_expr_p (tem))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 TREE_OPERAND (t, 0), negate_expr (tem));
657 tem = TREE_OPERAND (t, 0);
658 if (negate_expr_p (tem))
659 return fold_build2_loc (loc, TREE_CODE (t), type,
660 negate_expr (tem), TREE_OPERAND (t, 1));
661 }
662 break;
663
664 case TRUNC_DIV_EXPR:
665 case ROUND_DIV_EXPR:
666 case EXACT_DIV_EXPR:
667 /* In general we can't negate A / B, because if A is INT_MIN and
668 B is 1, we may turn this into INT_MIN / -1 which is undefined
669 and actually traps on some architectures. But if overflow is
670 undefined, we can negate, because - (INT_MIN / 1) is an
671 overflow. */
672 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
673 {
674 const char * const warnmsg = G_("assuming signed overflow does not "
675 "occur when negating a division");
676 tem = TREE_OPERAND (t, 1);
677 if (negate_expr_p (tem))
678 {
679 if (INTEGRAL_TYPE_P (type)
680 && (TREE_CODE (tem) != INTEGER_CST
681 || integer_onep (tem)))
682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683 return fold_build2_loc (loc, TREE_CODE (t), type,
684 TREE_OPERAND (t, 0), negate_expr (tem));
685 }
686 /* If overflow is undefined then we have to be careful because
687 we ask whether it's ok to associate the negate with the
688 division which is not ok for example for
689 -((a - b) / c) where (-(a - b)) / c may invoke undefined
690 overflow because of negating INT_MIN. So do not use
691 negate_expr_p here but open-code the two important cases. */
692 tem = TREE_OPERAND (t, 0);
693 if ((INTEGRAL_TYPE_P (type)
694 && (TREE_CODE (tem) == NEGATE_EXPR
695 || (TREE_CODE (tem) == INTEGER_CST
696 && may_negate_without_overflow_p (tem))))
697 || !INTEGRAL_TYPE_P (type))
698 return fold_build2_loc (loc, TREE_CODE (t), type,
699 negate_expr (tem), TREE_OPERAND (t, 1));
700 }
701 break;
702
703 case NOP_EXPR:
704 /* Convert -((double)float) into (double)(-float). */
705 if (TREE_CODE (type) == REAL_TYPE)
706 {
707 tem = strip_float_extensions (t);
708 if (tem != t && negate_expr_p (tem))
709 return fold_convert_loc (loc, type, negate_expr (tem));
710 }
711 break;
712
713 case CALL_EXPR:
714 /* Negate -f(x) as f(-x). */
715 if (negate_mathfn_p (builtin_mathfn_code (t))
716 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
717 {
718 tree fndecl, arg;
719
720 fndecl = get_callee_fndecl (t);
721 arg = negate_expr (CALL_EXPR_ARG (t, 0));
722 return build_call_expr_loc (loc, fndecl, 1, arg);
723 }
724 break;
725
726 case RSHIFT_EXPR:
727 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
728 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
729 {
730 tree op1 = TREE_OPERAND (t, 1);
731 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
732 {
733 tree ntype = TYPE_UNSIGNED (type)
734 ? signed_type_for (type)
735 : unsigned_type_for (type);
736 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
737 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
738 return fold_convert_loc (loc, type, temp);
739 }
740 }
741 break;
742
743 default:
744 break;
745 }
746
747 return NULL_TREE;
748 }
749
750 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
751 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
752 return NULL_TREE. */
753
754 static tree
755 negate_expr (tree t)
756 {
757 tree type, tem;
758 location_t loc;
759
760 if (t == NULL_TREE)
761 return NULL_TREE;
762
763 loc = EXPR_LOCATION (t);
764 type = TREE_TYPE (t);
765 STRIP_SIGN_NOPS (t);
766
767 tem = fold_negate_expr (loc, t);
768 if (!tem)
769 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
770 return fold_convert_loc (loc, type, tem);
771 }
772 \f
773 /* Split a tree IN into a constant, literal and variable parts that could be
774 combined with CODE to make IN. "constant" means an expression with
775 TREE_CONSTANT but that isn't an actual constant. CODE must be a
776 commutative arithmetic operation. Store the constant part into *CONP,
777 the literal in *LITP and return the variable part. If a part isn't
778 present, set it to null. If the tree does not decompose in this way,
779 return the entire tree as the variable part and the other parts as null.
780
781 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
782 case, we negate an operand that was subtracted. Except if it is a
783 literal for which we use *MINUS_LITP instead.
784
785 If NEGATE_P is true, we are negating all of IN, again except a literal
786 for which we use *MINUS_LITP instead.
787
788 If IN is itself a literal or constant, return it as appropriate.
789
790 Note that we do not guarantee that any of the three values will be the
791 same type as IN, but they will have the same signedness and mode. */
792
793 static tree
794 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
795 tree *minus_litp, int negate_p)
796 {
797 tree var = 0;
798
799 *conp = 0;
800 *litp = 0;
801 *minus_litp = 0;
802
803 /* Strip any conversions that don't change the machine mode or signedness. */
804 STRIP_SIGN_NOPS (in);
805
806 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
807 || TREE_CODE (in) == FIXED_CST)
808 *litp = in;
809 else if (TREE_CODE (in) == code
810 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
811 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
812 /* We can associate addition and subtraction together (even
813 though the C standard doesn't say so) for integers because
814 the value is not affected. For reals, the value might be
815 affected, so we can't. */
816 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
817 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
818 {
819 tree op0 = TREE_OPERAND (in, 0);
820 tree op1 = TREE_OPERAND (in, 1);
821 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
822 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
823
824 /* First see if either of the operands is a literal, then a constant. */
825 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
826 || TREE_CODE (op0) == FIXED_CST)
827 *litp = op0, op0 = 0;
828 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
829 || TREE_CODE (op1) == FIXED_CST)
830 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
831
832 if (op0 != 0 && TREE_CONSTANT (op0))
833 *conp = op0, op0 = 0;
834 else if (op1 != 0 && TREE_CONSTANT (op1))
835 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
836
837 /* If we haven't dealt with either operand, this is not a case we can
838 decompose. Otherwise, VAR is either of the ones remaining, if any. */
839 if (op0 != 0 && op1 != 0)
840 var = in;
841 else if (op0 != 0)
842 var = op0;
843 else
844 var = op1, neg_var_p = neg1_p;
845
846 /* Now do any needed negations. */
847 if (neg_litp_p)
848 *minus_litp = *litp, *litp = 0;
849 if (neg_conp_p)
850 *conp = negate_expr (*conp);
851 if (neg_var_p)
852 var = negate_expr (var);
853 }
854 else if (TREE_CODE (in) == BIT_NOT_EXPR
855 && code == PLUS_EXPR)
856 {
857 /* -X - 1 is folded to ~X, undo that here. */
858 *minus_litp = build_one_cst (TREE_TYPE (in));
859 var = negate_expr (TREE_OPERAND (in, 0));
860 }
861 else if (TREE_CONSTANT (in))
862 *conp = in;
863 else
864 var = in;
865
866 if (negate_p)
867 {
868 if (*litp)
869 *minus_litp = *litp, *litp = 0;
870 else if (*minus_litp)
871 *litp = *minus_litp, *minus_litp = 0;
872 *conp = negate_expr (*conp);
873 var = negate_expr (var);
874 }
875
876 return var;
877 }
878
879 /* Re-associate trees split by the above function. T1 and T2 are
880 either expressions to associate or null. Return the new
881 expression, if any. LOC is the location of the new expression. If
882 we build an operation, do it in TYPE and with CODE. */
883
884 static tree
885 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
886 {
887 if (t1 == 0)
888 return t2;
889 else if (t2 == 0)
890 return t1;
891
892 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
893 try to fold this since we will have infinite recursion. But do
894 deal with any NEGATE_EXPRs. */
895 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
896 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
897 {
898 if (code == PLUS_EXPR)
899 {
900 if (TREE_CODE (t1) == NEGATE_EXPR)
901 return build2_loc (loc, MINUS_EXPR, type,
902 fold_convert_loc (loc, type, t2),
903 fold_convert_loc (loc, type,
904 TREE_OPERAND (t1, 0)));
905 else if (TREE_CODE (t2) == NEGATE_EXPR)
906 return build2_loc (loc, MINUS_EXPR, type,
907 fold_convert_loc (loc, type, t1),
908 fold_convert_loc (loc, type,
909 TREE_OPERAND (t2, 0)));
910 else if (integer_zerop (t2))
911 return fold_convert_loc (loc, type, t1);
912 }
913 else if (code == MINUS_EXPR)
914 {
915 if (integer_zerop (t2))
916 return fold_convert_loc (loc, type, t1);
917 }
918
919 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
920 fold_convert_loc (loc, type, t2));
921 }
922
923 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
924 fold_convert_loc (loc, type, t2));
925 }
926 \f
927 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
928 for use in int_const_binop, size_binop and size_diffop. */
929
930 static bool
931 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
932 {
933 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
934 return false;
935 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
936 return false;
937
938 switch (code)
939 {
940 case LSHIFT_EXPR:
941 case RSHIFT_EXPR:
942 case LROTATE_EXPR:
943 case RROTATE_EXPR:
944 return true;
945
946 default:
947 break;
948 }
949
950 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
951 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
952 && TYPE_MODE (type1) == TYPE_MODE (type2);
953 }
954
955
956 /* Combine two integer constants ARG1 and ARG2 under operation CODE
957 to produce a new constant. Return NULL_TREE if we don't know how
958 to evaluate CODE at compile-time. */
959
960 static tree
961 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
962 int overflowable)
963 {
964 wide_int res;
965 tree t;
966 tree type = TREE_TYPE (arg1);
967 signop sign = TYPE_SIGN (type);
968 bool overflow = false;
969
970 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
971 TYPE_SIGN (TREE_TYPE (parg2)));
972
973 switch (code)
974 {
975 case BIT_IOR_EXPR:
976 res = wi::bit_or (arg1, arg2);
977 break;
978
979 case BIT_XOR_EXPR:
980 res = wi::bit_xor (arg1, arg2);
981 break;
982
983 case BIT_AND_EXPR:
984 res = wi::bit_and (arg1, arg2);
985 break;
986
987 case RSHIFT_EXPR:
988 case LSHIFT_EXPR:
989 if (wi::neg_p (arg2))
990 {
991 arg2 = -arg2;
992 if (code == RSHIFT_EXPR)
993 code = LSHIFT_EXPR;
994 else
995 code = RSHIFT_EXPR;
996 }
997
998 if (code == RSHIFT_EXPR)
999 /* It's unclear from the C standard whether shifts can overflow.
1000 The following code ignores overflow; perhaps a C standard
1001 interpretation ruling is needed. */
1002 res = wi::rshift (arg1, arg2, sign);
1003 else
1004 res = wi::lshift (arg1, arg2);
1005 break;
1006
1007 case RROTATE_EXPR:
1008 case LROTATE_EXPR:
1009 if (wi::neg_p (arg2))
1010 {
1011 arg2 = -arg2;
1012 if (code == RROTATE_EXPR)
1013 code = LROTATE_EXPR;
1014 else
1015 code = RROTATE_EXPR;
1016 }
1017
1018 if (code == RROTATE_EXPR)
1019 res = wi::rrotate (arg1, arg2);
1020 else
1021 res = wi::lrotate (arg1, arg2);
1022 break;
1023
1024 case PLUS_EXPR:
1025 res = wi::add (arg1, arg2, sign, &overflow);
1026 break;
1027
1028 case MINUS_EXPR:
1029 res = wi::sub (arg1, arg2, sign, &overflow);
1030 break;
1031
1032 case MULT_EXPR:
1033 res = wi::mul (arg1, arg2, sign, &overflow);
1034 break;
1035
1036 case MULT_HIGHPART_EXPR:
1037 res = wi::mul_high (arg1, arg2, sign);
1038 break;
1039
1040 case TRUNC_DIV_EXPR:
1041 case EXACT_DIV_EXPR:
1042 if (arg2 == 0)
1043 return NULL_TREE;
1044 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1045 break;
1046
1047 case FLOOR_DIV_EXPR:
1048 if (arg2 == 0)
1049 return NULL_TREE;
1050 res = wi::div_floor (arg1, arg2, sign, &overflow);
1051 break;
1052
1053 case CEIL_DIV_EXPR:
1054 if (arg2 == 0)
1055 return NULL_TREE;
1056 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1057 break;
1058
1059 case ROUND_DIV_EXPR:
1060 if (arg2 == 0)
1061 return NULL_TREE;
1062 res = wi::div_round (arg1, arg2, sign, &overflow);
1063 break;
1064
1065 case TRUNC_MOD_EXPR:
1066 if (arg2 == 0)
1067 return NULL_TREE;
1068 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1069 break;
1070
1071 case FLOOR_MOD_EXPR:
1072 if (arg2 == 0)
1073 return NULL_TREE;
1074 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1075 break;
1076
1077 case CEIL_MOD_EXPR:
1078 if (arg2 == 0)
1079 return NULL_TREE;
1080 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1081 break;
1082
1083 case ROUND_MOD_EXPR:
1084 if (arg2 == 0)
1085 return NULL_TREE;
1086 res = wi::mod_round (arg1, arg2, sign, &overflow);
1087 break;
1088
1089 case MIN_EXPR:
1090 res = wi::min (arg1, arg2, sign);
1091 break;
1092
1093 case MAX_EXPR:
1094 res = wi::max (arg1, arg2, sign);
1095 break;
1096
1097 default:
1098 return NULL_TREE;
1099 }
1100
1101 t = force_fit_type (type, res, overflowable,
1102 (((sign == SIGNED || overflowable == -1)
1103 && overflow)
1104 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1105
1106 return t;
1107 }
1108
1109 tree
1110 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1111 {
1112 return int_const_binop_1 (code, arg1, arg2, 1);
1113 }
1114
1115 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1116 constant. We assume ARG1 and ARG2 have the same data type, or at least
1117 are the same kind of constant and the same machine mode. Return zero if
1118 combining the constants is not allowed in the current operating mode. */
1119
1120 static tree
1121 const_binop (enum tree_code code, tree arg1, tree arg2)
1122 {
1123 /* Sanity check for the recursive cases. */
1124 if (!arg1 || !arg2)
1125 return NULL_TREE;
1126
1127 STRIP_NOPS (arg1);
1128 STRIP_NOPS (arg2);
1129
1130 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1131 {
1132 if (code == POINTER_PLUS_EXPR)
1133 return int_const_binop (PLUS_EXPR,
1134 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1135
1136 return int_const_binop (code, arg1, arg2);
1137 }
1138
1139 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1140 {
1141 machine_mode mode;
1142 REAL_VALUE_TYPE d1;
1143 REAL_VALUE_TYPE d2;
1144 REAL_VALUE_TYPE value;
1145 REAL_VALUE_TYPE result;
1146 bool inexact;
1147 tree t, type;
1148
1149 /* The following codes are handled by real_arithmetic. */
1150 switch (code)
1151 {
1152 case PLUS_EXPR:
1153 case MINUS_EXPR:
1154 case MULT_EXPR:
1155 case RDIV_EXPR:
1156 case MIN_EXPR:
1157 case MAX_EXPR:
1158 break;
1159
1160 default:
1161 return NULL_TREE;
1162 }
1163
1164 d1 = TREE_REAL_CST (arg1);
1165 d2 = TREE_REAL_CST (arg2);
1166
1167 type = TREE_TYPE (arg1);
1168 mode = TYPE_MODE (type);
1169
1170 /* Don't perform operation if we honor signaling NaNs and
1171 either operand is a NaN. */
1172 if (HONOR_SNANS (mode)
1173 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1174 return NULL_TREE;
1175
1176 /* Don't perform operation if it would raise a division
1177 by zero exception. */
1178 if (code == RDIV_EXPR
1179 && REAL_VALUES_EQUAL (d2, dconst0)
1180 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1181 return NULL_TREE;
1182
1183 /* If either operand is a NaN, just return it. Otherwise, set up
1184 for floating-point trap; we return an overflow. */
1185 if (REAL_VALUE_ISNAN (d1))
1186 return arg1;
1187 else if (REAL_VALUE_ISNAN (d2))
1188 return arg2;
1189
1190 inexact = real_arithmetic (&value, code, &d1, &d2);
1191 real_convert (&result, mode, &value);
1192
1193 /* Don't constant fold this floating point operation if
1194 the result has overflowed and flag_trapping_math. */
1195 if (flag_trapping_math
1196 && MODE_HAS_INFINITIES (mode)
1197 && REAL_VALUE_ISINF (result)
1198 && !REAL_VALUE_ISINF (d1)
1199 && !REAL_VALUE_ISINF (d2))
1200 return NULL_TREE;
1201
1202 /* Don't constant fold this floating point operation if the
1203 result may dependent upon the run-time rounding mode and
1204 flag_rounding_math is set, or if GCC's software emulation
1205 is unable to accurately represent the result. */
1206 if ((flag_rounding_math
1207 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1208 && (inexact || !real_identical (&result, &value)))
1209 return NULL_TREE;
1210
1211 t = build_real (type, result);
1212
1213 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1214 return t;
1215 }
1216
1217 if (TREE_CODE (arg1) == FIXED_CST)
1218 {
1219 FIXED_VALUE_TYPE f1;
1220 FIXED_VALUE_TYPE f2;
1221 FIXED_VALUE_TYPE result;
1222 tree t, type;
1223 int sat_p;
1224 bool overflow_p;
1225
1226 /* The following codes are handled by fixed_arithmetic. */
1227 switch (code)
1228 {
1229 case PLUS_EXPR:
1230 case MINUS_EXPR:
1231 case MULT_EXPR:
1232 case TRUNC_DIV_EXPR:
1233 if (TREE_CODE (arg2) != FIXED_CST)
1234 return NULL_TREE;
1235 f2 = TREE_FIXED_CST (arg2);
1236 break;
1237
1238 case LSHIFT_EXPR:
1239 case RSHIFT_EXPR:
1240 {
1241 if (TREE_CODE (arg2) != INTEGER_CST)
1242 return NULL_TREE;
1243 wide_int w2 = arg2;
1244 f2.data.high = w2.elt (1);
1245 f2.data.low = w2.elt (0);
1246 f2.mode = SImode;
1247 }
1248 break;
1249
1250 default:
1251 return NULL_TREE;
1252 }
1253
1254 f1 = TREE_FIXED_CST (arg1);
1255 type = TREE_TYPE (arg1);
1256 sat_p = TYPE_SATURATING (type);
1257 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1258 t = build_fixed (type, result);
1259 /* Propagate overflow flags. */
1260 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1261 TREE_OVERFLOW (t) = 1;
1262 return t;
1263 }
1264
1265 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1266 {
1267 tree type = TREE_TYPE (arg1);
1268 tree r1 = TREE_REALPART (arg1);
1269 tree i1 = TREE_IMAGPART (arg1);
1270 tree r2 = TREE_REALPART (arg2);
1271 tree i2 = TREE_IMAGPART (arg2);
1272 tree real, imag;
1273
1274 switch (code)
1275 {
1276 case PLUS_EXPR:
1277 case MINUS_EXPR:
1278 real = const_binop (code, r1, r2);
1279 imag = const_binop (code, i1, i2);
1280 break;
1281
1282 case MULT_EXPR:
1283 if (COMPLEX_FLOAT_TYPE_P (type))
1284 return do_mpc_arg2 (arg1, arg2, type,
1285 /* do_nonfinite= */ folding_initializer,
1286 mpc_mul);
1287
1288 real = const_binop (MINUS_EXPR,
1289 const_binop (MULT_EXPR, r1, r2),
1290 const_binop (MULT_EXPR, i1, i2));
1291 imag = const_binop (PLUS_EXPR,
1292 const_binop (MULT_EXPR, r1, i2),
1293 const_binop (MULT_EXPR, i1, r2));
1294 break;
1295
1296 case RDIV_EXPR:
1297 if (COMPLEX_FLOAT_TYPE_P (type))
1298 return do_mpc_arg2 (arg1, arg2, type,
1299 /* do_nonfinite= */ folding_initializer,
1300 mpc_div);
1301 /* Fallthru ... */
1302 case TRUNC_DIV_EXPR:
1303 case CEIL_DIV_EXPR:
1304 case FLOOR_DIV_EXPR:
1305 case ROUND_DIV_EXPR:
1306 if (flag_complex_method == 0)
1307 {
1308 /* Keep this algorithm in sync with
1309 tree-complex.c:expand_complex_div_straight().
1310
1311 Expand complex division to scalars, straightforward algorithm.
1312 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1313 t = br*br + bi*bi
1314 */
1315 tree magsquared
1316 = const_binop (PLUS_EXPR,
1317 const_binop (MULT_EXPR, r2, r2),
1318 const_binop (MULT_EXPR, i2, i2));
1319 tree t1
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r1, r2),
1322 const_binop (MULT_EXPR, i1, i2));
1323 tree t2
1324 = const_binop (MINUS_EXPR,
1325 const_binop (MULT_EXPR, i1, r2),
1326 const_binop (MULT_EXPR, r1, i2));
1327
1328 real = const_binop (code, t1, magsquared);
1329 imag = const_binop (code, t2, magsquared);
1330 }
1331 else
1332 {
1333 /* Keep this algorithm in sync with
1334 tree-complex.c:expand_complex_div_wide().
1335
1336 Expand complex division to scalars, modified algorithm to minimize
1337 overflow with wide input ranges. */
1338 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1339 fold_abs_const (r2, TREE_TYPE (type)),
1340 fold_abs_const (i2, TREE_TYPE (type)));
1341
1342 if (integer_nonzerop (compare))
1343 {
1344 /* In the TRUE branch, we compute
1345 ratio = br/bi;
1346 div = (br * ratio) + bi;
1347 tr = (ar * ratio) + ai;
1348 ti = (ai * ratio) - ar;
1349 tr = tr / div;
1350 ti = ti / div; */
1351 tree ratio = const_binop (code, r2, i2);
1352 tree div = const_binop (PLUS_EXPR, i2,
1353 const_binop (MULT_EXPR, r2, ratio));
1354 real = const_binop (MULT_EXPR, r1, ratio);
1355 real = const_binop (PLUS_EXPR, real, i1);
1356 real = const_binop (code, real, div);
1357
1358 imag = const_binop (MULT_EXPR, i1, ratio);
1359 imag = const_binop (MINUS_EXPR, imag, r1);
1360 imag = const_binop (code, imag, div);
1361 }
1362 else
1363 {
1364 /* In the FALSE branch, we compute
1365 ratio = d/c;
1366 divisor = (d * ratio) + c;
1367 tr = (b * ratio) + a;
1368 ti = b - (a * ratio);
1369 tr = tr / div;
1370 ti = ti / div; */
1371 tree ratio = const_binop (code, i2, r2);
1372 tree div = const_binop (PLUS_EXPR, r2,
1373 const_binop (MULT_EXPR, i2, ratio));
1374
1375 real = const_binop (MULT_EXPR, i1, ratio);
1376 real = const_binop (PLUS_EXPR, real, r1);
1377 real = const_binop (code, real, div);
1378
1379 imag = const_binop (MULT_EXPR, r1, ratio);
1380 imag = const_binop (MINUS_EXPR, i1, imag);
1381 imag = const_binop (code, imag, div);
1382 }
1383 }
1384 break;
1385
1386 default:
1387 return NULL_TREE;
1388 }
1389
1390 if (real && imag)
1391 return build_complex (type, real, imag);
1392 }
1393
1394 if (TREE_CODE (arg1) == VECTOR_CST
1395 && TREE_CODE (arg2) == VECTOR_CST)
1396 {
1397 tree type = TREE_TYPE (arg1);
1398 int count = TYPE_VECTOR_SUBPARTS (type), i;
1399 tree *elts = XALLOCAVEC (tree, count);
1400
1401 for (i = 0; i < count; i++)
1402 {
1403 tree elem1 = VECTOR_CST_ELT (arg1, i);
1404 tree elem2 = VECTOR_CST_ELT (arg2, i);
1405
1406 elts[i] = const_binop (code, elem1, elem2);
1407
1408 /* It is possible that const_binop cannot handle the given
1409 code and return NULL_TREE */
1410 if (elts[i] == NULL_TREE)
1411 return NULL_TREE;
1412 }
1413
1414 return build_vector (type, elts);
1415 }
1416
1417 /* Shifts allow a scalar offset for a vector. */
1418 if (TREE_CODE (arg1) == VECTOR_CST
1419 && TREE_CODE (arg2) == INTEGER_CST)
1420 {
1421 tree type = TREE_TYPE (arg1);
1422 int count = TYPE_VECTOR_SUBPARTS (type), i;
1423 tree *elts = XALLOCAVEC (tree, count);
1424
1425 for (i = 0; i < count; i++)
1426 {
1427 tree elem1 = VECTOR_CST_ELT (arg1, i);
1428
1429 elts[i] = const_binop (code, elem1, arg2);
1430
1431 /* It is possible that const_binop cannot handle the given
1432 code and return NULL_TREE. */
1433 if (elts[i] == NULL_TREE)
1434 return NULL_TREE;
1435 }
1436
1437 return build_vector (type, elts);
1438 }
1439 return NULL_TREE;
1440 }
1441
1442 /* Overload that adds a TYPE parameter to be able to dispatch
1443 to fold_relational_const. */
1444
1445 tree
1446 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1447 {
1448 if (TREE_CODE_CLASS (code) == tcc_comparison)
1449 return fold_relational_const (code, type, arg1, arg2);
1450
1451 /* ??? Until we make the const_binop worker take the type of the
1452 result as argument put those cases that need it here. */
1453 switch (code)
1454 {
1455 case COMPLEX_EXPR:
1456 if ((TREE_CODE (arg1) == REAL_CST
1457 && TREE_CODE (arg2) == REAL_CST)
1458 || (TREE_CODE (arg1) == INTEGER_CST
1459 && TREE_CODE (arg2) == INTEGER_CST))
1460 return build_complex (type, arg1, arg2);
1461 return NULL_TREE;
1462
1463 case VEC_PACK_TRUNC_EXPR:
1464 case VEC_PACK_FIX_TRUNC_EXPR:
1465 {
1466 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1467 tree *elts;
1468
1469 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1470 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1471 if (TREE_CODE (arg1) != VECTOR_CST
1472 || TREE_CODE (arg2) != VECTOR_CST)
1473 return NULL_TREE;
1474
1475 elts = XALLOCAVEC (tree, nelts);
1476 if (!vec_cst_ctor_to_array (arg1, elts)
1477 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1478 return NULL_TREE;
1479
1480 for (i = 0; i < nelts; i++)
1481 {
1482 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1483 ? NOP_EXPR : FIX_TRUNC_EXPR,
1484 TREE_TYPE (type), elts[i]);
1485 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1486 return NULL_TREE;
1487 }
1488
1489 return build_vector (type, elts);
1490 }
1491
1492 case VEC_WIDEN_MULT_LO_EXPR:
1493 case VEC_WIDEN_MULT_HI_EXPR:
1494 case VEC_WIDEN_MULT_EVEN_EXPR:
1495 case VEC_WIDEN_MULT_ODD_EXPR:
1496 {
1497 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1498 unsigned int out, ofs, scale;
1499 tree *elts;
1500
1501 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1502 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1503 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1504 return NULL_TREE;
1505
1506 elts = XALLOCAVEC (tree, nelts * 4);
1507 if (!vec_cst_ctor_to_array (arg1, elts)
1508 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1509 return NULL_TREE;
1510
1511 if (code == VEC_WIDEN_MULT_LO_EXPR)
1512 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1513 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1514 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1515 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1516 scale = 1, ofs = 0;
1517 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1518 scale = 1, ofs = 1;
1519
1520 for (out = 0; out < nelts; out++)
1521 {
1522 unsigned int in1 = (out << scale) + ofs;
1523 unsigned int in2 = in1 + nelts * 2;
1524 tree t1, t2;
1525
1526 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1527 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1528
1529 if (t1 == NULL_TREE || t2 == NULL_TREE)
1530 return NULL_TREE;
1531 elts[out] = const_binop (MULT_EXPR, t1, t2);
1532 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1533 return NULL_TREE;
1534 }
1535
1536 return build_vector (type, elts);
1537 }
1538
1539 default:;
1540 }
1541
1542 if (TREE_CODE_CLASS (code) != tcc_binary)
1543 return NULL_TREE;
1544
1545 /* Make sure type and arg0 have the same saturating flag. */
1546 gcc_checking_assert (TYPE_SATURATING (type)
1547 == TYPE_SATURATING (TREE_TYPE (arg1)));
1548
1549 return const_binop (code, arg1, arg2);
1550 }
1551
1552 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1553 Return zero if computing the constants is not possible. */
1554
1555 tree
1556 const_unop (enum tree_code code, tree type, tree arg0)
1557 {
1558 switch (code)
1559 {
1560 CASE_CONVERT:
1561 case FLOAT_EXPR:
1562 case FIX_TRUNC_EXPR:
1563 case FIXED_CONVERT_EXPR:
1564 return fold_convert_const (code, type, arg0);
1565
1566 case ADDR_SPACE_CONVERT_EXPR:
1567 if (integer_zerop (arg0))
1568 return fold_convert_const (code, type, arg0);
1569 break;
1570
1571 case VIEW_CONVERT_EXPR:
1572 return fold_view_convert_expr (type, arg0);
1573
1574 case NEGATE_EXPR:
1575 {
1576 /* Can't call fold_negate_const directly here as that doesn't
1577 handle all cases and we might not be able to negate some
1578 constants. */
1579 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1580 if (tem && CONSTANT_CLASS_P (tem))
1581 return tem;
1582 break;
1583 }
1584
1585 case ABS_EXPR:
1586 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1587 return fold_abs_const (arg0, type);
1588 break;
1589
1590 case CONJ_EXPR:
1591 if (TREE_CODE (arg0) == COMPLEX_CST)
1592 {
1593 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1594 TREE_TYPE (type));
1595 return build_complex (type, TREE_REALPART (arg0), ipart);
1596 }
1597 break;
1598
1599 case BIT_NOT_EXPR:
1600 if (TREE_CODE (arg0) == INTEGER_CST)
1601 return fold_not_const (arg0, type);
1602 /* Perform BIT_NOT_EXPR on each element individually. */
1603 else if (TREE_CODE (arg0) == VECTOR_CST)
1604 {
1605 tree *elements;
1606 tree elem;
1607 unsigned count = VECTOR_CST_NELTS (arg0), i;
1608
1609 elements = XALLOCAVEC (tree, count);
1610 for (i = 0; i < count; i++)
1611 {
1612 elem = VECTOR_CST_ELT (arg0, i);
1613 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1614 if (elem == NULL_TREE)
1615 break;
1616 elements[i] = elem;
1617 }
1618 if (i == count)
1619 return build_vector (type, elements);
1620 }
1621 break;
1622
1623 case TRUTH_NOT_EXPR:
1624 if (TREE_CODE (arg0) == INTEGER_CST)
1625 return constant_boolean_node (integer_zerop (arg0), type);
1626 break;
1627
1628 case REALPART_EXPR:
1629 if (TREE_CODE (arg0) == COMPLEX_CST)
1630 return fold_convert (type, TREE_REALPART (arg0));
1631 break;
1632
1633 case IMAGPART_EXPR:
1634 if (TREE_CODE (arg0) == COMPLEX_CST)
1635 return fold_convert (type, TREE_IMAGPART (arg0));
1636 break;
1637
1638 case VEC_UNPACK_LO_EXPR:
1639 case VEC_UNPACK_HI_EXPR:
1640 case VEC_UNPACK_FLOAT_LO_EXPR:
1641 case VEC_UNPACK_FLOAT_HI_EXPR:
1642 {
1643 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1644 tree *elts;
1645 enum tree_code subcode;
1646
1647 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1648 if (TREE_CODE (arg0) != VECTOR_CST)
1649 return NULL_TREE;
1650
1651 elts = XALLOCAVEC (tree, nelts * 2);
1652 if (!vec_cst_ctor_to_array (arg0, elts))
1653 return NULL_TREE;
1654
1655 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1656 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1657 elts += nelts;
1658
1659 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1660 subcode = NOP_EXPR;
1661 else
1662 subcode = FLOAT_EXPR;
1663
1664 for (i = 0; i < nelts; i++)
1665 {
1666 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1667 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1668 return NULL_TREE;
1669 }
1670
1671 return build_vector (type, elts);
1672 }
1673
1674 case REDUC_MIN_EXPR:
1675 case REDUC_MAX_EXPR:
1676 case REDUC_PLUS_EXPR:
1677 {
1678 unsigned int nelts, i;
1679 tree *elts;
1680 enum tree_code subcode;
1681
1682 if (TREE_CODE (arg0) != VECTOR_CST)
1683 return NULL_TREE;
1684 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1685
1686 elts = XALLOCAVEC (tree, nelts);
1687 if (!vec_cst_ctor_to_array (arg0, elts))
1688 return NULL_TREE;
1689
1690 switch (code)
1691 {
1692 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1693 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1694 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1695 default: gcc_unreachable ();
1696 }
1697
1698 for (i = 1; i < nelts; i++)
1699 {
1700 elts[0] = const_binop (subcode, elts[0], elts[i]);
1701 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1702 return NULL_TREE;
1703 }
1704
1705 return elts[0];
1706 }
1707
1708 default:
1709 break;
1710 }
1711
1712 return NULL_TREE;
1713 }
1714
1715 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1716 indicates which particular sizetype to create. */
1717
1718 tree
1719 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1720 {
1721 return build_int_cst (sizetype_tab[(int) kind], number);
1722 }
1723 \f
1724 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1725 is a tree code. The type of the result is taken from the operands.
1726 Both must be equivalent integer types, ala int_binop_types_match_p.
1727 If the operands are constant, so is the result. */
1728
1729 tree
1730 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1731 {
1732 tree type = TREE_TYPE (arg0);
1733
1734 if (arg0 == error_mark_node || arg1 == error_mark_node)
1735 return error_mark_node;
1736
1737 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1738 TREE_TYPE (arg1)));
1739
1740 /* Handle the special case of two integer constants faster. */
1741 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1742 {
1743 /* And some specific cases even faster than that. */
1744 if (code == PLUS_EXPR)
1745 {
1746 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1747 return arg1;
1748 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1749 return arg0;
1750 }
1751 else if (code == MINUS_EXPR)
1752 {
1753 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1754 return arg0;
1755 }
1756 else if (code == MULT_EXPR)
1757 {
1758 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1759 return arg1;
1760 }
1761
1762 /* Handle general case of two integer constants. For sizetype
1763 constant calculations we always want to know about overflow,
1764 even in the unsigned case. */
1765 return int_const_binop_1 (code, arg0, arg1, -1);
1766 }
1767
1768 return fold_build2_loc (loc, code, type, arg0, arg1);
1769 }
1770
1771 /* Given two values, either both of sizetype or both of bitsizetype,
1772 compute the difference between the two values. Return the value
1773 in signed type corresponding to the type of the operands. */
1774
1775 tree
1776 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1777 {
1778 tree type = TREE_TYPE (arg0);
1779 tree ctype;
1780
1781 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1782 TREE_TYPE (arg1)));
1783
1784 /* If the type is already signed, just do the simple thing. */
1785 if (!TYPE_UNSIGNED (type))
1786 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1787
1788 if (type == sizetype)
1789 ctype = ssizetype;
1790 else if (type == bitsizetype)
1791 ctype = sbitsizetype;
1792 else
1793 ctype = signed_type_for (type);
1794
1795 /* If either operand is not a constant, do the conversions to the signed
1796 type and subtract. The hardware will do the right thing with any
1797 overflow in the subtraction. */
1798 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1799 return size_binop_loc (loc, MINUS_EXPR,
1800 fold_convert_loc (loc, ctype, arg0),
1801 fold_convert_loc (loc, ctype, arg1));
1802
1803 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1804 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1805 overflow) and negate (which can't either). Special-case a result
1806 of zero while we're here. */
1807 if (tree_int_cst_equal (arg0, arg1))
1808 return build_int_cst (ctype, 0);
1809 else if (tree_int_cst_lt (arg1, arg0))
1810 return fold_convert_loc (loc, ctype,
1811 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1812 else
1813 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1814 fold_convert_loc (loc, ctype,
1815 size_binop_loc (loc,
1816 MINUS_EXPR,
1817 arg1, arg0)));
1818 }
1819 \f
1820 /* A subroutine of fold_convert_const handling conversions of an
1821 INTEGER_CST to another integer type. */
1822
1823 static tree
1824 fold_convert_const_int_from_int (tree type, const_tree arg1)
1825 {
1826 /* Given an integer constant, make new constant with new type,
1827 appropriately sign-extended or truncated. Use widest_int
1828 so that any extension is done according ARG1's type. */
1829 return force_fit_type (type, wi::to_widest (arg1),
1830 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1831 TREE_OVERFLOW (arg1));
1832 }
1833
1834 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1835 to an integer type. */
1836
1837 static tree
1838 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1839 {
1840 bool overflow = false;
1841 tree t;
1842
1843 /* The following code implements the floating point to integer
1844 conversion rules required by the Java Language Specification,
1845 that IEEE NaNs are mapped to zero and values that overflow
1846 the target precision saturate, i.e. values greater than
1847 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1848 are mapped to INT_MIN. These semantics are allowed by the
1849 C and C++ standards that simply state that the behavior of
1850 FP-to-integer conversion is unspecified upon overflow. */
1851
1852 wide_int val;
1853 REAL_VALUE_TYPE r;
1854 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1855
1856 switch (code)
1857 {
1858 case FIX_TRUNC_EXPR:
1859 real_trunc (&r, VOIDmode, &x);
1860 break;
1861
1862 default:
1863 gcc_unreachable ();
1864 }
1865
1866 /* If R is NaN, return zero and show we have an overflow. */
1867 if (REAL_VALUE_ISNAN (r))
1868 {
1869 overflow = true;
1870 val = wi::zero (TYPE_PRECISION (type));
1871 }
1872
1873 /* See if R is less than the lower bound or greater than the
1874 upper bound. */
1875
1876 if (! overflow)
1877 {
1878 tree lt = TYPE_MIN_VALUE (type);
1879 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1880 if (REAL_VALUES_LESS (r, l))
1881 {
1882 overflow = true;
1883 val = lt;
1884 }
1885 }
1886
1887 if (! overflow)
1888 {
1889 tree ut = TYPE_MAX_VALUE (type);
1890 if (ut)
1891 {
1892 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1893 if (REAL_VALUES_LESS (u, r))
1894 {
1895 overflow = true;
1896 val = ut;
1897 }
1898 }
1899 }
1900
1901 if (! overflow)
1902 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1903
1904 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1905 return t;
1906 }
1907
1908 /* A subroutine of fold_convert_const handling conversions of a
1909 FIXED_CST to an integer type. */
1910
1911 static tree
1912 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1913 {
1914 tree t;
1915 double_int temp, temp_trunc;
1916 unsigned int mode;
1917
1918 /* Right shift FIXED_CST to temp by fbit. */
1919 temp = TREE_FIXED_CST (arg1).data;
1920 mode = TREE_FIXED_CST (arg1).mode;
1921 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1922 {
1923 temp = temp.rshift (GET_MODE_FBIT (mode),
1924 HOST_BITS_PER_DOUBLE_INT,
1925 SIGNED_FIXED_POINT_MODE_P (mode));
1926
1927 /* Left shift temp to temp_trunc by fbit. */
1928 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1929 HOST_BITS_PER_DOUBLE_INT,
1930 SIGNED_FIXED_POINT_MODE_P (mode));
1931 }
1932 else
1933 {
1934 temp = double_int_zero;
1935 temp_trunc = double_int_zero;
1936 }
1937
1938 /* If FIXED_CST is negative, we need to round the value toward 0.
1939 By checking if the fractional bits are not zero to add 1 to temp. */
1940 if (SIGNED_FIXED_POINT_MODE_P (mode)
1941 && temp_trunc.is_negative ()
1942 && TREE_FIXED_CST (arg1).data != temp_trunc)
1943 temp += double_int_one;
1944
1945 /* Given a fixed-point constant, make new constant with new type,
1946 appropriately sign-extended or truncated. */
1947 t = force_fit_type (type, temp, -1,
1948 (temp.is_negative ()
1949 && (TYPE_UNSIGNED (type)
1950 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1951 | TREE_OVERFLOW (arg1));
1952
1953 return t;
1954 }
1955
1956 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1957 to another floating point type. */
1958
1959 static tree
1960 fold_convert_const_real_from_real (tree type, const_tree arg1)
1961 {
1962 REAL_VALUE_TYPE value;
1963 tree t;
1964
1965 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1966 t = build_real (type, value);
1967
1968 /* If converting an infinity or NAN to a representation that doesn't
1969 have one, set the overflow bit so that we can produce some kind of
1970 error message at the appropriate point if necessary. It's not the
1971 most user-friendly message, but it's better than nothing. */
1972 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1973 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1974 TREE_OVERFLOW (t) = 1;
1975 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1976 && !MODE_HAS_NANS (TYPE_MODE (type)))
1977 TREE_OVERFLOW (t) = 1;
1978 /* Regular overflow, conversion produced an infinity in a mode that
1979 can't represent them. */
1980 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1981 && REAL_VALUE_ISINF (value)
1982 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1983 TREE_OVERFLOW (t) = 1;
1984 else
1985 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1986 return t;
1987 }
1988
1989 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1990 to a floating point type. */
1991
1992 static tree
1993 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1994 {
1995 REAL_VALUE_TYPE value;
1996 tree t;
1997
1998 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1999 t = build_real (type, value);
2000
2001 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2002 return t;
2003 }
2004
2005 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2006 to another fixed-point type. */
2007
2008 static tree
2009 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2010 {
2011 FIXED_VALUE_TYPE value;
2012 tree t;
2013 bool overflow_p;
2014
2015 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2016 TYPE_SATURATING (type));
2017 t = build_fixed (type, value);
2018
2019 /* Propagate overflow flags. */
2020 if (overflow_p | TREE_OVERFLOW (arg1))
2021 TREE_OVERFLOW (t) = 1;
2022 return t;
2023 }
2024
2025 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2026 to a fixed-point type. */
2027
2028 static tree
2029 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2030 {
2031 FIXED_VALUE_TYPE value;
2032 tree t;
2033 bool overflow_p;
2034 double_int di;
2035
2036 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2037
2038 di.low = TREE_INT_CST_ELT (arg1, 0);
2039 if (TREE_INT_CST_NUNITS (arg1) == 1)
2040 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2041 else
2042 di.high = TREE_INT_CST_ELT (arg1, 1);
2043
2044 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2045 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2046 TYPE_SATURATING (type));
2047 t = build_fixed (type, value);
2048
2049 /* Propagate overflow flags. */
2050 if (overflow_p | TREE_OVERFLOW (arg1))
2051 TREE_OVERFLOW (t) = 1;
2052 return t;
2053 }
2054
2055 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2056 to a fixed-point type. */
2057
2058 static tree
2059 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2060 {
2061 FIXED_VALUE_TYPE value;
2062 tree t;
2063 bool overflow_p;
2064
2065 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2066 &TREE_REAL_CST (arg1),
2067 TYPE_SATURATING (type));
2068 t = build_fixed (type, value);
2069
2070 /* Propagate overflow flags. */
2071 if (overflow_p | TREE_OVERFLOW (arg1))
2072 TREE_OVERFLOW (t) = 1;
2073 return t;
2074 }
2075
2076 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2077 type TYPE. If no simplification can be done return NULL_TREE. */
2078
2079 static tree
2080 fold_convert_const (enum tree_code code, tree type, tree arg1)
2081 {
2082 if (TREE_TYPE (arg1) == type)
2083 return arg1;
2084
2085 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2086 || TREE_CODE (type) == OFFSET_TYPE)
2087 {
2088 if (TREE_CODE (arg1) == INTEGER_CST)
2089 return fold_convert_const_int_from_int (type, arg1);
2090 else if (TREE_CODE (arg1) == REAL_CST)
2091 return fold_convert_const_int_from_real (code, type, arg1);
2092 else if (TREE_CODE (arg1) == FIXED_CST)
2093 return fold_convert_const_int_from_fixed (type, arg1);
2094 }
2095 else if (TREE_CODE (type) == REAL_TYPE)
2096 {
2097 if (TREE_CODE (arg1) == INTEGER_CST)
2098 return build_real_from_int_cst (type, arg1);
2099 else if (TREE_CODE (arg1) == REAL_CST)
2100 return fold_convert_const_real_from_real (type, arg1);
2101 else if (TREE_CODE (arg1) == FIXED_CST)
2102 return fold_convert_const_real_from_fixed (type, arg1);
2103 }
2104 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2105 {
2106 if (TREE_CODE (arg1) == FIXED_CST)
2107 return fold_convert_const_fixed_from_fixed (type, arg1);
2108 else if (TREE_CODE (arg1) == INTEGER_CST)
2109 return fold_convert_const_fixed_from_int (type, arg1);
2110 else if (TREE_CODE (arg1) == REAL_CST)
2111 return fold_convert_const_fixed_from_real (type, arg1);
2112 }
2113 return NULL_TREE;
2114 }
2115
2116 /* Construct a vector of zero elements of vector type TYPE. */
2117
2118 static tree
2119 build_zero_vector (tree type)
2120 {
2121 tree t;
2122
2123 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2124 return build_vector_from_val (type, t);
2125 }
2126
2127 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2128
2129 bool
2130 fold_convertible_p (const_tree type, const_tree arg)
2131 {
2132 tree orig = TREE_TYPE (arg);
2133
2134 if (type == orig)
2135 return true;
2136
2137 if (TREE_CODE (arg) == ERROR_MARK
2138 || TREE_CODE (type) == ERROR_MARK
2139 || TREE_CODE (orig) == ERROR_MARK)
2140 return false;
2141
2142 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2143 return true;
2144
2145 switch (TREE_CODE (type))
2146 {
2147 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2148 case POINTER_TYPE: case REFERENCE_TYPE:
2149 case OFFSET_TYPE:
2150 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2151 || TREE_CODE (orig) == OFFSET_TYPE)
2152 return true;
2153 return (TREE_CODE (orig) == VECTOR_TYPE
2154 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2155
2156 case REAL_TYPE:
2157 case FIXED_POINT_TYPE:
2158 case COMPLEX_TYPE:
2159 case VECTOR_TYPE:
2160 case VOID_TYPE:
2161 return TREE_CODE (type) == TREE_CODE (orig);
2162
2163 default:
2164 return false;
2165 }
2166 }
2167
2168 /* Convert expression ARG to type TYPE. Used by the middle-end for
2169 simple conversions in preference to calling the front-end's convert. */
2170
2171 tree
2172 fold_convert_loc (location_t loc, tree type, tree arg)
2173 {
2174 tree orig = TREE_TYPE (arg);
2175 tree tem;
2176
2177 if (type == orig)
2178 return arg;
2179
2180 if (TREE_CODE (arg) == ERROR_MARK
2181 || TREE_CODE (type) == ERROR_MARK
2182 || TREE_CODE (orig) == ERROR_MARK)
2183 return error_mark_node;
2184
2185 switch (TREE_CODE (type))
2186 {
2187 case POINTER_TYPE:
2188 case REFERENCE_TYPE:
2189 /* Handle conversions between pointers to different address spaces. */
2190 if (POINTER_TYPE_P (orig)
2191 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2192 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2193 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2194 /* fall through */
2195
2196 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2197 case OFFSET_TYPE:
2198 if (TREE_CODE (arg) == INTEGER_CST)
2199 {
2200 tem = fold_convert_const (NOP_EXPR, type, arg);
2201 if (tem != NULL_TREE)
2202 return tem;
2203 }
2204 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2205 || TREE_CODE (orig) == OFFSET_TYPE)
2206 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2207 if (TREE_CODE (orig) == COMPLEX_TYPE)
2208 return fold_convert_loc (loc, type,
2209 fold_build1_loc (loc, REALPART_EXPR,
2210 TREE_TYPE (orig), arg));
2211 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2212 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2213 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2214
2215 case REAL_TYPE:
2216 if (TREE_CODE (arg) == INTEGER_CST)
2217 {
2218 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2219 if (tem != NULL_TREE)
2220 return tem;
2221 }
2222 else if (TREE_CODE (arg) == REAL_CST)
2223 {
2224 tem = fold_convert_const (NOP_EXPR, type, arg);
2225 if (tem != NULL_TREE)
2226 return tem;
2227 }
2228 else if (TREE_CODE (arg) == FIXED_CST)
2229 {
2230 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2231 if (tem != NULL_TREE)
2232 return tem;
2233 }
2234
2235 switch (TREE_CODE (orig))
2236 {
2237 case INTEGER_TYPE:
2238 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2239 case POINTER_TYPE: case REFERENCE_TYPE:
2240 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2241
2242 case REAL_TYPE:
2243 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2244
2245 case FIXED_POINT_TYPE:
2246 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2247
2248 case COMPLEX_TYPE:
2249 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2250 return fold_convert_loc (loc, type, tem);
2251
2252 default:
2253 gcc_unreachable ();
2254 }
2255
2256 case FIXED_POINT_TYPE:
2257 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2258 || TREE_CODE (arg) == REAL_CST)
2259 {
2260 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2261 if (tem != NULL_TREE)
2262 goto fold_convert_exit;
2263 }
2264
2265 switch (TREE_CODE (orig))
2266 {
2267 case FIXED_POINT_TYPE:
2268 case INTEGER_TYPE:
2269 case ENUMERAL_TYPE:
2270 case BOOLEAN_TYPE:
2271 case REAL_TYPE:
2272 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2273
2274 case COMPLEX_TYPE:
2275 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2276 return fold_convert_loc (loc, type, tem);
2277
2278 default:
2279 gcc_unreachable ();
2280 }
2281
2282 case COMPLEX_TYPE:
2283 switch (TREE_CODE (orig))
2284 {
2285 case INTEGER_TYPE:
2286 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2287 case POINTER_TYPE: case REFERENCE_TYPE:
2288 case REAL_TYPE:
2289 case FIXED_POINT_TYPE:
2290 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2291 fold_convert_loc (loc, TREE_TYPE (type), arg),
2292 fold_convert_loc (loc, TREE_TYPE (type),
2293 integer_zero_node));
2294 case COMPLEX_TYPE:
2295 {
2296 tree rpart, ipart;
2297
2298 if (TREE_CODE (arg) == COMPLEX_EXPR)
2299 {
2300 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2301 TREE_OPERAND (arg, 0));
2302 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2303 TREE_OPERAND (arg, 1));
2304 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2305 }
2306
2307 arg = save_expr (arg);
2308 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2309 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2310 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2311 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2312 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2313 }
2314
2315 default:
2316 gcc_unreachable ();
2317 }
2318
2319 case VECTOR_TYPE:
2320 if (integer_zerop (arg))
2321 return build_zero_vector (type);
2322 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2323 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2324 || TREE_CODE (orig) == VECTOR_TYPE);
2325 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2326
2327 case VOID_TYPE:
2328 tem = fold_ignored_result (arg);
2329 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2330
2331 default:
2332 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2333 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2334 gcc_unreachable ();
2335 }
2336 fold_convert_exit:
2337 protected_set_expr_location_unshare (tem, loc);
2338 return tem;
2339 }
2340 \f
2341 /* Return false if expr can be assumed not to be an lvalue, true
2342 otherwise. */
2343
2344 static bool
2345 maybe_lvalue_p (const_tree x)
2346 {
2347 /* We only need to wrap lvalue tree codes. */
2348 switch (TREE_CODE (x))
2349 {
2350 case VAR_DECL:
2351 case PARM_DECL:
2352 case RESULT_DECL:
2353 case LABEL_DECL:
2354 case FUNCTION_DECL:
2355 case SSA_NAME:
2356
2357 case COMPONENT_REF:
2358 case MEM_REF:
2359 case INDIRECT_REF:
2360 case ARRAY_REF:
2361 case ARRAY_RANGE_REF:
2362 case BIT_FIELD_REF:
2363 case OBJ_TYPE_REF:
2364
2365 case REALPART_EXPR:
2366 case IMAGPART_EXPR:
2367 case PREINCREMENT_EXPR:
2368 case PREDECREMENT_EXPR:
2369 case SAVE_EXPR:
2370 case TRY_CATCH_EXPR:
2371 case WITH_CLEANUP_EXPR:
2372 case COMPOUND_EXPR:
2373 case MODIFY_EXPR:
2374 case TARGET_EXPR:
2375 case COND_EXPR:
2376 case BIND_EXPR:
2377 break;
2378
2379 default:
2380 /* Assume the worst for front-end tree codes. */
2381 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2382 break;
2383 return false;
2384 }
2385
2386 return true;
2387 }
2388
2389 /* Return an expr equal to X but certainly not valid as an lvalue. */
2390
2391 tree
2392 non_lvalue_loc (location_t loc, tree x)
2393 {
2394 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2395 us. */
2396 if (in_gimple_form)
2397 return x;
2398
2399 if (! maybe_lvalue_p (x))
2400 return x;
2401 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2402 }
2403
2404 /* When pedantic, return an expr equal to X but certainly not valid as a
2405 pedantic lvalue. Otherwise, return X. */
2406
2407 static tree
2408 pedantic_non_lvalue_loc (location_t loc, tree x)
2409 {
2410 return protected_set_expr_location_unshare (x, loc);
2411 }
2412 \f
2413 /* Given a tree comparison code, return the code that is the logical inverse.
2414 It is generally not safe to do this for floating-point comparisons, except
2415 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2416 ERROR_MARK in this case. */
2417
2418 enum tree_code
2419 invert_tree_comparison (enum tree_code code, bool honor_nans)
2420 {
2421 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2422 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2423 return ERROR_MARK;
2424
2425 switch (code)
2426 {
2427 case EQ_EXPR:
2428 return NE_EXPR;
2429 case NE_EXPR:
2430 return EQ_EXPR;
2431 case GT_EXPR:
2432 return honor_nans ? UNLE_EXPR : LE_EXPR;
2433 case GE_EXPR:
2434 return honor_nans ? UNLT_EXPR : LT_EXPR;
2435 case LT_EXPR:
2436 return honor_nans ? UNGE_EXPR : GE_EXPR;
2437 case LE_EXPR:
2438 return honor_nans ? UNGT_EXPR : GT_EXPR;
2439 case LTGT_EXPR:
2440 return UNEQ_EXPR;
2441 case UNEQ_EXPR:
2442 return LTGT_EXPR;
2443 case UNGT_EXPR:
2444 return LE_EXPR;
2445 case UNGE_EXPR:
2446 return LT_EXPR;
2447 case UNLT_EXPR:
2448 return GE_EXPR;
2449 case UNLE_EXPR:
2450 return GT_EXPR;
2451 case ORDERED_EXPR:
2452 return UNORDERED_EXPR;
2453 case UNORDERED_EXPR:
2454 return ORDERED_EXPR;
2455 default:
2456 gcc_unreachable ();
2457 }
2458 }
2459
2460 /* Similar, but return the comparison that results if the operands are
2461 swapped. This is safe for floating-point. */
2462
2463 enum tree_code
2464 swap_tree_comparison (enum tree_code code)
2465 {
2466 switch (code)
2467 {
2468 case EQ_EXPR:
2469 case NE_EXPR:
2470 case ORDERED_EXPR:
2471 case UNORDERED_EXPR:
2472 case LTGT_EXPR:
2473 case UNEQ_EXPR:
2474 return code;
2475 case GT_EXPR:
2476 return LT_EXPR;
2477 case GE_EXPR:
2478 return LE_EXPR;
2479 case LT_EXPR:
2480 return GT_EXPR;
2481 case LE_EXPR:
2482 return GE_EXPR;
2483 case UNGT_EXPR:
2484 return UNLT_EXPR;
2485 case UNGE_EXPR:
2486 return UNLE_EXPR;
2487 case UNLT_EXPR:
2488 return UNGT_EXPR;
2489 case UNLE_EXPR:
2490 return UNGE_EXPR;
2491 default:
2492 gcc_unreachable ();
2493 }
2494 }
2495
2496
2497 /* Convert a comparison tree code from an enum tree_code representation
2498 into a compcode bit-based encoding. This function is the inverse of
2499 compcode_to_comparison. */
2500
2501 static enum comparison_code
2502 comparison_to_compcode (enum tree_code code)
2503 {
2504 switch (code)
2505 {
2506 case LT_EXPR:
2507 return COMPCODE_LT;
2508 case EQ_EXPR:
2509 return COMPCODE_EQ;
2510 case LE_EXPR:
2511 return COMPCODE_LE;
2512 case GT_EXPR:
2513 return COMPCODE_GT;
2514 case NE_EXPR:
2515 return COMPCODE_NE;
2516 case GE_EXPR:
2517 return COMPCODE_GE;
2518 case ORDERED_EXPR:
2519 return COMPCODE_ORD;
2520 case UNORDERED_EXPR:
2521 return COMPCODE_UNORD;
2522 case UNLT_EXPR:
2523 return COMPCODE_UNLT;
2524 case UNEQ_EXPR:
2525 return COMPCODE_UNEQ;
2526 case UNLE_EXPR:
2527 return COMPCODE_UNLE;
2528 case UNGT_EXPR:
2529 return COMPCODE_UNGT;
2530 case LTGT_EXPR:
2531 return COMPCODE_LTGT;
2532 case UNGE_EXPR:
2533 return COMPCODE_UNGE;
2534 default:
2535 gcc_unreachable ();
2536 }
2537 }
2538
2539 /* Convert a compcode bit-based encoding of a comparison operator back
2540 to GCC's enum tree_code representation. This function is the
2541 inverse of comparison_to_compcode. */
2542
2543 static enum tree_code
2544 compcode_to_comparison (enum comparison_code code)
2545 {
2546 switch (code)
2547 {
2548 case COMPCODE_LT:
2549 return LT_EXPR;
2550 case COMPCODE_EQ:
2551 return EQ_EXPR;
2552 case COMPCODE_LE:
2553 return LE_EXPR;
2554 case COMPCODE_GT:
2555 return GT_EXPR;
2556 case COMPCODE_NE:
2557 return NE_EXPR;
2558 case COMPCODE_GE:
2559 return GE_EXPR;
2560 case COMPCODE_ORD:
2561 return ORDERED_EXPR;
2562 case COMPCODE_UNORD:
2563 return UNORDERED_EXPR;
2564 case COMPCODE_UNLT:
2565 return UNLT_EXPR;
2566 case COMPCODE_UNEQ:
2567 return UNEQ_EXPR;
2568 case COMPCODE_UNLE:
2569 return UNLE_EXPR;
2570 case COMPCODE_UNGT:
2571 return UNGT_EXPR;
2572 case COMPCODE_LTGT:
2573 return LTGT_EXPR;
2574 case COMPCODE_UNGE:
2575 return UNGE_EXPR;
2576 default:
2577 gcc_unreachable ();
2578 }
2579 }
2580
2581 /* Return a tree for the comparison which is the combination of
2582 doing the AND or OR (depending on CODE) of the two operations LCODE
2583 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2584 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2585 if this makes the transformation invalid. */
2586
2587 tree
2588 combine_comparisons (location_t loc,
2589 enum tree_code code, enum tree_code lcode,
2590 enum tree_code rcode, tree truth_type,
2591 tree ll_arg, tree lr_arg)
2592 {
2593 bool honor_nans = HONOR_NANS (ll_arg);
2594 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2595 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2596 int compcode;
2597
2598 switch (code)
2599 {
2600 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2601 compcode = lcompcode & rcompcode;
2602 break;
2603
2604 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2605 compcode = lcompcode | rcompcode;
2606 break;
2607
2608 default:
2609 return NULL_TREE;
2610 }
2611
2612 if (!honor_nans)
2613 {
2614 /* Eliminate unordered comparisons, as well as LTGT and ORD
2615 which are not used unless the mode has NaNs. */
2616 compcode &= ~COMPCODE_UNORD;
2617 if (compcode == COMPCODE_LTGT)
2618 compcode = COMPCODE_NE;
2619 else if (compcode == COMPCODE_ORD)
2620 compcode = COMPCODE_TRUE;
2621 }
2622 else if (flag_trapping_math)
2623 {
2624 /* Check that the original operation and the optimized ones will trap
2625 under the same condition. */
2626 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2627 && (lcompcode != COMPCODE_EQ)
2628 && (lcompcode != COMPCODE_ORD);
2629 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2630 && (rcompcode != COMPCODE_EQ)
2631 && (rcompcode != COMPCODE_ORD);
2632 bool trap = (compcode & COMPCODE_UNORD) == 0
2633 && (compcode != COMPCODE_EQ)
2634 && (compcode != COMPCODE_ORD);
2635
2636 /* In a short-circuited boolean expression the LHS might be
2637 such that the RHS, if evaluated, will never trap. For
2638 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2639 if neither x nor y is NaN. (This is a mixed blessing: for
2640 example, the expression above will never trap, hence
2641 optimizing it to x < y would be invalid). */
2642 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2643 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2644 rtrap = false;
2645
2646 /* If the comparison was short-circuited, and only the RHS
2647 trapped, we may now generate a spurious trap. */
2648 if (rtrap && !ltrap
2649 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2650 return NULL_TREE;
2651
2652 /* If we changed the conditions that cause a trap, we lose. */
2653 if ((ltrap || rtrap) != trap)
2654 return NULL_TREE;
2655 }
2656
2657 if (compcode == COMPCODE_TRUE)
2658 return constant_boolean_node (true, truth_type);
2659 else if (compcode == COMPCODE_FALSE)
2660 return constant_boolean_node (false, truth_type);
2661 else
2662 {
2663 enum tree_code tcode;
2664
2665 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2666 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2667 }
2668 }
2669 \f
2670 /* Return nonzero if two operands (typically of the same tree node)
2671 are necessarily equal. If either argument has side-effects this
2672 function returns zero. FLAGS modifies behavior as follows:
2673
2674 If OEP_ONLY_CONST is set, only return nonzero for constants.
2675 This function tests whether the operands are indistinguishable;
2676 it does not test whether they are equal using C's == operation.
2677 The distinction is important for IEEE floating point, because
2678 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2679 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2680
2681 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2682 even though it may hold multiple values during a function.
2683 This is because a GCC tree node guarantees that nothing else is
2684 executed between the evaluation of its "operands" (which may often
2685 be evaluated in arbitrary order). Hence if the operands themselves
2686 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2687 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2688 unset means assuming isochronic (or instantaneous) tree equivalence.
2689 Unless comparing arbitrary expression trees, such as from different
2690 statements, this flag can usually be left unset.
2691
2692 If OEP_PURE_SAME is set, then pure functions with identical arguments
2693 are considered the same. It is used when the caller has other ways
2694 to ensure that global memory is unchanged in between. */
2695
2696 int
2697 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2698 {
2699 /* If either is ERROR_MARK, they aren't equal. */
2700 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2701 || TREE_TYPE (arg0) == error_mark_node
2702 || TREE_TYPE (arg1) == error_mark_node)
2703 return 0;
2704
2705 /* Similar, if either does not have a type (like a released SSA name),
2706 they aren't equal. */
2707 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2708 return 0;
2709
2710 /* Check equality of integer constants before bailing out due to
2711 precision differences. */
2712 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2713 return tree_int_cst_equal (arg0, arg1);
2714
2715 /* If both types don't have the same signedness, then we can't consider
2716 them equal. We must check this before the STRIP_NOPS calls
2717 because they may change the signedness of the arguments. As pointers
2718 strictly don't have a signedness, require either two pointers or
2719 two non-pointers as well. */
2720 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2721 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2722 return 0;
2723
2724 /* We cannot consider pointers to different address space equal. */
2725 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2726 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2727 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2728 return 0;
2729
2730 /* If both types don't have the same precision, then it is not safe
2731 to strip NOPs. */
2732 if (element_precision (TREE_TYPE (arg0))
2733 != element_precision (TREE_TYPE (arg1)))
2734 return 0;
2735
2736 STRIP_NOPS (arg0);
2737 STRIP_NOPS (arg1);
2738
2739 /* In case both args are comparisons but with different comparison
2740 code, try to swap the comparison operands of one arg to produce
2741 a match and compare that variant. */
2742 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2743 && COMPARISON_CLASS_P (arg0)
2744 && COMPARISON_CLASS_P (arg1))
2745 {
2746 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2747
2748 if (TREE_CODE (arg0) == swap_code)
2749 return operand_equal_p (TREE_OPERAND (arg0, 0),
2750 TREE_OPERAND (arg1, 1), flags)
2751 && operand_equal_p (TREE_OPERAND (arg0, 1),
2752 TREE_OPERAND (arg1, 0), flags);
2753 }
2754
2755 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2756 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2757 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2758 return 0;
2759
2760 /* This is needed for conversions and for COMPONENT_REF.
2761 Might as well play it safe and always test this. */
2762 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2763 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2764 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2765 return 0;
2766
2767 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2768 We don't care about side effects in that case because the SAVE_EXPR
2769 takes care of that for us. In all other cases, two expressions are
2770 equal if they have no side effects. If we have two identical
2771 expressions with side effects that should be treated the same due
2772 to the only side effects being identical SAVE_EXPR's, that will
2773 be detected in the recursive calls below.
2774 If we are taking an invariant address of two identical objects
2775 they are necessarily equal as well. */
2776 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2777 && (TREE_CODE (arg0) == SAVE_EXPR
2778 || (flags & OEP_CONSTANT_ADDRESS_OF)
2779 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2780 return 1;
2781
2782 /* Next handle constant cases, those for which we can return 1 even
2783 if ONLY_CONST is set. */
2784 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2785 switch (TREE_CODE (arg0))
2786 {
2787 case INTEGER_CST:
2788 return tree_int_cst_equal (arg0, arg1);
2789
2790 case FIXED_CST:
2791 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2792 TREE_FIXED_CST (arg1));
2793
2794 case REAL_CST:
2795 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2796 TREE_REAL_CST (arg1)))
2797 return 1;
2798
2799
2800 if (!HONOR_SIGNED_ZEROS (arg0))
2801 {
2802 /* If we do not distinguish between signed and unsigned zero,
2803 consider them equal. */
2804 if (real_zerop (arg0) && real_zerop (arg1))
2805 return 1;
2806 }
2807 return 0;
2808
2809 case VECTOR_CST:
2810 {
2811 unsigned i;
2812
2813 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2814 return 0;
2815
2816 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2817 {
2818 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2819 VECTOR_CST_ELT (arg1, i), flags))
2820 return 0;
2821 }
2822 return 1;
2823 }
2824
2825 case COMPLEX_CST:
2826 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2827 flags)
2828 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2829 flags));
2830
2831 case STRING_CST:
2832 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2833 && ! memcmp (TREE_STRING_POINTER (arg0),
2834 TREE_STRING_POINTER (arg1),
2835 TREE_STRING_LENGTH (arg0)));
2836
2837 case ADDR_EXPR:
2838 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2839 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2840 ? OEP_CONSTANT_ADDRESS_OF | OEP_ADDRESS_OF : 0);
2841 default:
2842 break;
2843 }
2844
2845 if (flags & OEP_ONLY_CONST)
2846 return 0;
2847
2848 /* Define macros to test an operand from arg0 and arg1 for equality and a
2849 variant that allows null and views null as being different from any
2850 non-null value. In the latter case, if either is null, the both
2851 must be; otherwise, do the normal comparison. */
2852 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2853 TREE_OPERAND (arg1, N), flags)
2854
2855 #define OP_SAME_WITH_NULL(N) \
2856 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2857 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2858
2859 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2860 {
2861 case tcc_unary:
2862 /* Two conversions are equal only if signedness and modes match. */
2863 switch (TREE_CODE (arg0))
2864 {
2865 CASE_CONVERT:
2866 case FIX_TRUNC_EXPR:
2867 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2868 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2869 return 0;
2870 break;
2871 default:
2872 break;
2873 }
2874
2875 return OP_SAME (0);
2876
2877
2878 case tcc_comparison:
2879 case tcc_binary:
2880 if (OP_SAME (0) && OP_SAME (1))
2881 return 1;
2882
2883 /* For commutative ops, allow the other order. */
2884 return (commutative_tree_code (TREE_CODE (arg0))
2885 && operand_equal_p (TREE_OPERAND (arg0, 0),
2886 TREE_OPERAND (arg1, 1), flags)
2887 && operand_equal_p (TREE_OPERAND (arg0, 1),
2888 TREE_OPERAND (arg1, 0), flags));
2889
2890 case tcc_reference:
2891 /* If either of the pointer (or reference) expressions we are
2892 dereferencing contain a side effect, these cannot be equal,
2893 but their addresses can be. */
2894 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2895 && (TREE_SIDE_EFFECTS (arg0)
2896 || TREE_SIDE_EFFECTS (arg1)))
2897 return 0;
2898
2899 switch (TREE_CODE (arg0))
2900 {
2901 case INDIRECT_REF:
2902 if (!(flags & OEP_ADDRESS_OF)
2903 && (TYPE_ALIGN (TREE_TYPE (arg0))
2904 != TYPE_ALIGN (TREE_TYPE (arg1))))
2905 return 0;
2906 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2907 return OP_SAME (0);
2908
2909 case REALPART_EXPR:
2910 case IMAGPART_EXPR:
2911 return OP_SAME (0);
2912
2913 case TARGET_MEM_REF:
2914 case MEM_REF:
2915 /* Require equal access sizes, and similar pointer types.
2916 We can have incomplete types for array references of
2917 variable-sized arrays from the Fortran frontend
2918 though. Also verify the types are compatible. */
2919 if (!((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2920 || (TYPE_SIZE (TREE_TYPE (arg0))
2921 && TYPE_SIZE (TREE_TYPE (arg1))
2922 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2923 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2924 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2925 && ((flags & OEP_ADDRESS_OF)
2926 || (alias_ptr_types_compatible_p
2927 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2928 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2929 && (MR_DEPENDENCE_CLIQUE (arg0)
2930 == MR_DEPENDENCE_CLIQUE (arg1))
2931 && (MR_DEPENDENCE_BASE (arg0)
2932 == MR_DEPENDENCE_BASE (arg1))
2933 && (TYPE_ALIGN (TREE_TYPE (arg0))
2934 == TYPE_ALIGN (TREE_TYPE (arg1)))))))
2935 return 0;
2936 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2937 return (OP_SAME (0) && OP_SAME (1)
2938 /* TARGET_MEM_REF require equal extra operands. */
2939 && (TREE_CODE (arg0) != TARGET_MEM_REF
2940 || (OP_SAME_WITH_NULL (2)
2941 && OP_SAME_WITH_NULL (3)
2942 && OP_SAME_WITH_NULL (4))));
2943
2944 case ARRAY_REF:
2945 case ARRAY_RANGE_REF:
2946 /* Operands 2 and 3 may be null.
2947 Compare the array index by value if it is constant first as we
2948 may have different types but same value here. */
2949 if (!OP_SAME (0))
2950 return 0;
2951 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2952 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2953 TREE_OPERAND (arg1, 1))
2954 || OP_SAME (1))
2955 && OP_SAME_WITH_NULL (2)
2956 && OP_SAME_WITH_NULL (3));
2957
2958 case COMPONENT_REF:
2959 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2960 may be NULL when we're called to compare MEM_EXPRs. */
2961 if (!OP_SAME_WITH_NULL (0)
2962 || !OP_SAME (1))
2963 return 0;
2964 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2965 return OP_SAME_WITH_NULL (2);
2966
2967 case BIT_FIELD_REF:
2968 if (!OP_SAME (0))
2969 return 0;
2970 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2971 return OP_SAME (1) && OP_SAME (2);
2972
2973 default:
2974 return 0;
2975 }
2976
2977 case tcc_expression:
2978 switch (TREE_CODE (arg0))
2979 {
2980 case ADDR_EXPR:
2981 return operand_equal_p (TREE_OPERAND (arg0, 0),
2982 TREE_OPERAND (arg1, 0),
2983 flags | OEP_ADDRESS_OF);
2984
2985 case TRUTH_NOT_EXPR:
2986 return OP_SAME (0);
2987
2988 case TRUTH_ANDIF_EXPR:
2989 case TRUTH_ORIF_EXPR:
2990 return OP_SAME (0) && OP_SAME (1);
2991
2992 case FMA_EXPR:
2993 case WIDEN_MULT_PLUS_EXPR:
2994 case WIDEN_MULT_MINUS_EXPR:
2995 if (!OP_SAME (2))
2996 return 0;
2997 /* The multiplcation operands are commutative. */
2998 /* FALLTHRU */
2999
3000 case TRUTH_AND_EXPR:
3001 case TRUTH_OR_EXPR:
3002 case TRUTH_XOR_EXPR:
3003 if (OP_SAME (0) && OP_SAME (1))
3004 return 1;
3005
3006 /* Otherwise take into account this is a commutative operation. */
3007 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3008 TREE_OPERAND (arg1, 1), flags)
3009 && operand_equal_p (TREE_OPERAND (arg0, 1),
3010 TREE_OPERAND (arg1, 0), flags));
3011
3012 case COND_EXPR:
3013 case VEC_COND_EXPR:
3014 case DOT_PROD_EXPR:
3015 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3016
3017 default:
3018 return 0;
3019 }
3020
3021 case tcc_vl_exp:
3022 switch (TREE_CODE (arg0))
3023 {
3024 case CALL_EXPR:
3025 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3026 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3027 /* If not both CALL_EXPRs are either internal or normal function
3028 functions, then they are not equal. */
3029 return 0;
3030 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3031 {
3032 /* If the CALL_EXPRs call different internal functions, then they
3033 are not equal. */
3034 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3035 return 0;
3036 }
3037 else
3038 {
3039 /* If the CALL_EXPRs call different functions, then they are not
3040 equal. */
3041 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3042 flags))
3043 return 0;
3044 }
3045
3046 {
3047 unsigned int cef = call_expr_flags (arg0);
3048 if (flags & OEP_PURE_SAME)
3049 cef &= ECF_CONST | ECF_PURE;
3050 else
3051 cef &= ECF_CONST;
3052 if (!cef)
3053 return 0;
3054 }
3055
3056 /* Now see if all the arguments are the same. */
3057 {
3058 const_call_expr_arg_iterator iter0, iter1;
3059 const_tree a0, a1;
3060 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3061 a1 = first_const_call_expr_arg (arg1, &iter1);
3062 a0 && a1;
3063 a0 = next_const_call_expr_arg (&iter0),
3064 a1 = next_const_call_expr_arg (&iter1))
3065 if (! operand_equal_p (a0, a1, flags))
3066 return 0;
3067
3068 /* If we get here and both argument lists are exhausted
3069 then the CALL_EXPRs are equal. */
3070 return ! (a0 || a1);
3071 }
3072 default:
3073 return 0;
3074 }
3075
3076 case tcc_declaration:
3077 /* Consider __builtin_sqrt equal to sqrt. */
3078 return (TREE_CODE (arg0) == FUNCTION_DECL
3079 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3080 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3081 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3082
3083 default:
3084 return 0;
3085 }
3086
3087 #undef OP_SAME
3088 #undef OP_SAME_WITH_NULL
3089 }
3090 \f
3091 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3092 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3093
3094 When in doubt, return 0. */
3095
3096 static int
3097 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3098 {
3099 int unsignedp1, unsignedpo;
3100 tree primarg0, primarg1, primother;
3101 unsigned int correct_width;
3102
3103 if (operand_equal_p (arg0, arg1, 0))
3104 return 1;
3105
3106 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3107 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3108 return 0;
3109
3110 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3111 and see if the inner values are the same. This removes any
3112 signedness comparison, which doesn't matter here. */
3113 primarg0 = arg0, primarg1 = arg1;
3114 STRIP_NOPS (primarg0);
3115 STRIP_NOPS (primarg1);
3116 if (operand_equal_p (primarg0, primarg1, 0))
3117 return 1;
3118
3119 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3120 actual comparison operand, ARG0.
3121
3122 First throw away any conversions to wider types
3123 already present in the operands. */
3124
3125 primarg1 = get_narrower (arg1, &unsignedp1);
3126 primother = get_narrower (other, &unsignedpo);
3127
3128 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3129 if (unsignedp1 == unsignedpo
3130 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3131 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3132 {
3133 tree type = TREE_TYPE (arg0);
3134
3135 /* Make sure shorter operand is extended the right way
3136 to match the longer operand. */
3137 primarg1 = fold_convert (signed_or_unsigned_type_for
3138 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3139
3140 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3141 return 1;
3142 }
3143
3144 return 0;
3145 }
3146 \f
3147 /* See if ARG is an expression that is either a comparison or is performing
3148 arithmetic on comparisons. The comparisons must only be comparing
3149 two different values, which will be stored in *CVAL1 and *CVAL2; if
3150 they are nonzero it means that some operands have already been found.
3151 No variables may be used anywhere else in the expression except in the
3152 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3153 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3154
3155 If this is true, return 1. Otherwise, return zero. */
3156
3157 static int
3158 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3159 {
3160 enum tree_code code = TREE_CODE (arg);
3161 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3162
3163 /* We can handle some of the tcc_expression cases here. */
3164 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3165 tclass = tcc_unary;
3166 else if (tclass == tcc_expression
3167 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3168 || code == COMPOUND_EXPR))
3169 tclass = tcc_binary;
3170
3171 else if (tclass == tcc_expression && code == SAVE_EXPR
3172 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3173 {
3174 /* If we've already found a CVAL1 or CVAL2, this expression is
3175 two complex to handle. */
3176 if (*cval1 || *cval2)
3177 return 0;
3178
3179 tclass = tcc_unary;
3180 *save_p = 1;
3181 }
3182
3183 switch (tclass)
3184 {
3185 case tcc_unary:
3186 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3187
3188 case tcc_binary:
3189 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3190 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3191 cval1, cval2, save_p));
3192
3193 case tcc_constant:
3194 return 1;
3195
3196 case tcc_expression:
3197 if (code == COND_EXPR)
3198 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3199 cval1, cval2, save_p)
3200 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3201 cval1, cval2, save_p)
3202 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3203 cval1, cval2, save_p));
3204 return 0;
3205
3206 case tcc_comparison:
3207 /* First see if we can handle the first operand, then the second. For
3208 the second operand, we know *CVAL1 can't be zero. It must be that
3209 one side of the comparison is each of the values; test for the
3210 case where this isn't true by failing if the two operands
3211 are the same. */
3212
3213 if (operand_equal_p (TREE_OPERAND (arg, 0),
3214 TREE_OPERAND (arg, 1), 0))
3215 return 0;
3216
3217 if (*cval1 == 0)
3218 *cval1 = TREE_OPERAND (arg, 0);
3219 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3220 ;
3221 else if (*cval2 == 0)
3222 *cval2 = TREE_OPERAND (arg, 0);
3223 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3224 ;
3225 else
3226 return 0;
3227
3228 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3229 ;
3230 else if (*cval2 == 0)
3231 *cval2 = TREE_OPERAND (arg, 1);
3232 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3233 ;
3234 else
3235 return 0;
3236
3237 return 1;
3238
3239 default:
3240 return 0;
3241 }
3242 }
3243 \f
3244 /* ARG is a tree that is known to contain just arithmetic operations and
3245 comparisons. Evaluate the operations in the tree substituting NEW0 for
3246 any occurrence of OLD0 as an operand of a comparison and likewise for
3247 NEW1 and OLD1. */
3248
3249 static tree
3250 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3251 tree old1, tree new1)
3252 {
3253 tree type = TREE_TYPE (arg);
3254 enum tree_code code = TREE_CODE (arg);
3255 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3256
3257 /* We can handle some of the tcc_expression cases here. */
3258 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3259 tclass = tcc_unary;
3260 else if (tclass == tcc_expression
3261 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3262 tclass = tcc_binary;
3263
3264 switch (tclass)
3265 {
3266 case tcc_unary:
3267 return fold_build1_loc (loc, code, type,
3268 eval_subst (loc, TREE_OPERAND (arg, 0),
3269 old0, new0, old1, new1));
3270
3271 case tcc_binary:
3272 return fold_build2_loc (loc, code, type,
3273 eval_subst (loc, TREE_OPERAND (arg, 0),
3274 old0, new0, old1, new1),
3275 eval_subst (loc, TREE_OPERAND (arg, 1),
3276 old0, new0, old1, new1));
3277
3278 case tcc_expression:
3279 switch (code)
3280 {
3281 case SAVE_EXPR:
3282 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3283 old1, new1);
3284
3285 case COMPOUND_EXPR:
3286 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3287 old1, new1);
3288
3289 case COND_EXPR:
3290 return fold_build3_loc (loc, code, type,
3291 eval_subst (loc, TREE_OPERAND (arg, 0),
3292 old0, new0, old1, new1),
3293 eval_subst (loc, TREE_OPERAND (arg, 1),
3294 old0, new0, old1, new1),
3295 eval_subst (loc, TREE_OPERAND (arg, 2),
3296 old0, new0, old1, new1));
3297 default:
3298 break;
3299 }
3300 /* Fall through - ??? */
3301
3302 case tcc_comparison:
3303 {
3304 tree arg0 = TREE_OPERAND (arg, 0);
3305 tree arg1 = TREE_OPERAND (arg, 1);
3306
3307 /* We need to check both for exact equality and tree equality. The
3308 former will be true if the operand has a side-effect. In that
3309 case, we know the operand occurred exactly once. */
3310
3311 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3312 arg0 = new0;
3313 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3314 arg0 = new1;
3315
3316 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3317 arg1 = new0;
3318 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3319 arg1 = new1;
3320
3321 return fold_build2_loc (loc, code, type, arg0, arg1);
3322 }
3323
3324 default:
3325 return arg;
3326 }
3327 }
3328 \f
3329 /* Return a tree for the case when the result of an expression is RESULT
3330 converted to TYPE and OMITTED was previously an operand of the expression
3331 but is now not needed (e.g., we folded OMITTED * 0).
3332
3333 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3334 the conversion of RESULT to TYPE. */
3335
3336 tree
3337 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3338 {
3339 tree t = fold_convert_loc (loc, type, result);
3340
3341 /* If the resulting operand is an empty statement, just return the omitted
3342 statement casted to void. */
3343 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3344 return build1_loc (loc, NOP_EXPR, void_type_node,
3345 fold_ignored_result (omitted));
3346
3347 if (TREE_SIDE_EFFECTS (omitted))
3348 return build2_loc (loc, COMPOUND_EXPR, type,
3349 fold_ignored_result (omitted), t);
3350
3351 return non_lvalue_loc (loc, t);
3352 }
3353
3354 /* Return a tree for the case when the result of an expression is RESULT
3355 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3356 of the expression but are now not needed.
3357
3358 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3359 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3360 evaluated before OMITTED2. Otherwise, if neither has side effects,
3361 just do the conversion of RESULT to TYPE. */
3362
3363 tree
3364 omit_two_operands_loc (location_t loc, tree type, tree result,
3365 tree omitted1, tree omitted2)
3366 {
3367 tree t = fold_convert_loc (loc, type, result);
3368
3369 if (TREE_SIDE_EFFECTS (omitted2))
3370 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3371 if (TREE_SIDE_EFFECTS (omitted1))
3372 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3373
3374 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3375 }
3376
3377 \f
3378 /* Return a simplified tree node for the truth-negation of ARG. This
3379 never alters ARG itself. We assume that ARG is an operation that
3380 returns a truth value (0 or 1).
3381
3382 FIXME: one would think we would fold the result, but it causes
3383 problems with the dominator optimizer. */
3384
3385 static tree
3386 fold_truth_not_expr (location_t loc, tree arg)
3387 {
3388 tree type = TREE_TYPE (arg);
3389 enum tree_code code = TREE_CODE (arg);
3390 location_t loc1, loc2;
3391
3392 /* If this is a comparison, we can simply invert it, except for
3393 floating-point non-equality comparisons, in which case we just
3394 enclose a TRUTH_NOT_EXPR around what we have. */
3395
3396 if (TREE_CODE_CLASS (code) == tcc_comparison)
3397 {
3398 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3399 if (FLOAT_TYPE_P (op_type)
3400 && flag_trapping_math
3401 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3402 && code != NE_EXPR && code != EQ_EXPR)
3403 return NULL_TREE;
3404
3405 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3406 if (code == ERROR_MARK)
3407 return NULL_TREE;
3408
3409 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3410 TREE_OPERAND (arg, 1));
3411 }
3412
3413 switch (code)
3414 {
3415 case INTEGER_CST:
3416 return constant_boolean_node (integer_zerop (arg), type);
3417
3418 case TRUTH_AND_EXPR:
3419 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3420 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3421 return build2_loc (loc, TRUTH_OR_EXPR, type,
3422 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3423 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3424
3425 case TRUTH_OR_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_AND_EXPR, type,
3429 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3430 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3431
3432 case TRUTH_XOR_EXPR:
3433 /* Here we can invert either operand. We invert the first operand
3434 unless the second operand is a TRUTH_NOT_EXPR in which case our
3435 result is the XOR of the first operand with the inside of the
3436 negation of the second operand. */
3437
3438 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3439 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3440 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3441 else
3442 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3443 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3444 TREE_OPERAND (arg, 1));
3445
3446 case TRUTH_ANDIF_EXPR:
3447 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3448 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3449 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3450 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3451 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3452
3453 case TRUTH_ORIF_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_ANDIF_EXPR, type,
3457 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3458 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3459
3460 case TRUTH_NOT_EXPR:
3461 return TREE_OPERAND (arg, 0);
3462
3463 case COND_EXPR:
3464 {
3465 tree arg1 = TREE_OPERAND (arg, 1);
3466 tree arg2 = TREE_OPERAND (arg, 2);
3467
3468 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3469 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3470
3471 /* A COND_EXPR may have a throw as one operand, which
3472 then has void type. Just leave void operands
3473 as they are. */
3474 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3475 VOID_TYPE_P (TREE_TYPE (arg1))
3476 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3477 VOID_TYPE_P (TREE_TYPE (arg2))
3478 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3479 }
3480
3481 case COMPOUND_EXPR:
3482 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3483 return build2_loc (loc, COMPOUND_EXPR, type,
3484 TREE_OPERAND (arg, 0),
3485 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3486
3487 case NON_LVALUE_EXPR:
3488 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3489 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3490
3491 CASE_CONVERT:
3492 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3493 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3494
3495 /* ... fall through ... */
3496
3497 case FLOAT_EXPR:
3498 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3499 return build1_loc (loc, TREE_CODE (arg), type,
3500 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3501
3502 case BIT_AND_EXPR:
3503 if (!integer_onep (TREE_OPERAND (arg, 1)))
3504 return NULL_TREE;
3505 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3506
3507 case SAVE_EXPR:
3508 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3509
3510 case CLEANUP_POINT_EXPR:
3511 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3512 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3513 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3514
3515 default:
3516 return NULL_TREE;
3517 }
3518 }
3519
3520 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3521 assume that ARG is an operation that returns a truth value (0 or 1
3522 for scalars, 0 or -1 for vectors). Return the folded expression if
3523 folding is successful. Otherwise, return NULL_TREE. */
3524
3525 static tree
3526 fold_invert_truthvalue (location_t loc, tree arg)
3527 {
3528 tree type = TREE_TYPE (arg);
3529 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3530 ? BIT_NOT_EXPR
3531 : TRUTH_NOT_EXPR,
3532 type, arg);
3533 }
3534
3535 /* Return a simplified tree node for the truth-negation of ARG. This
3536 never alters ARG itself. We assume that ARG is an operation that
3537 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3538
3539 tree
3540 invert_truthvalue_loc (location_t loc, tree arg)
3541 {
3542 if (TREE_CODE (arg) == ERROR_MARK)
3543 return arg;
3544
3545 tree type = TREE_TYPE (arg);
3546 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3547 ? BIT_NOT_EXPR
3548 : TRUTH_NOT_EXPR,
3549 type, arg);
3550 }
3551
3552 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3553 with code CODE. This optimization is unsafe. */
3554 static tree
3555 distribute_real_division (location_t loc, enum tree_code code, tree type,
3556 tree arg0, tree arg1)
3557 {
3558 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3559 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3560
3561 /* (A / C) +- (B / C) -> (A +- B) / C. */
3562 if (mul0 == mul1
3563 && operand_equal_p (TREE_OPERAND (arg0, 1),
3564 TREE_OPERAND (arg1, 1), 0))
3565 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3566 fold_build2_loc (loc, code, type,
3567 TREE_OPERAND (arg0, 0),
3568 TREE_OPERAND (arg1, 0)),
3569 TREE_OPERAND (arg0, 1));
3570
3571 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3572 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3573 TREE_OPERAND (arg1, 0), 0)
3574 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3575 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3576 {
3577 REAL_VALUE_TYPE r0, r1;
3578 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3579 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3580 if (!mul0)
3581 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3582 if (!mul1)
3583 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3584 real_arithmetic (&r0, code, &r0, &r1);
3585 return fold_build2_loc (loc, MULT_EXPR, type,
3586 TREE_OPERAND (arg0, 0),
3587 build_real (type, r0));
3588 }
3589
3590 return NULL_TREE;
3591 }
3592 \f
3593 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3594 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3595
3596 static tree
3597 make_bit_field_ref (location_t loc, tree inner, tree type,
3598 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3599 {
3600 tree result, bftype;
3601
3602 if (bitpos == 0)
3603 {
3604 tree size = TYPE_SIZE (TREE_TYPE (inner));
3605 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3606 || POINTER_TYPE_P (TREE_TYPE (inner)))
3607 && tree_fits_shwi_p (size)
3608 && tree_to_shwi (size) == bitsize)
3609 return fold_convert_loc (loc, type, inner);
3610 }
3611
3612 bftype = type;
3613 if (TYPE_PRECISION (bftype) != bitsize
3614 || TYPE_UNSIGNED (bftype) == !unsignedp)
3615 bftype = build_nonstandard_integer_type (bitsize, 0);
3616
3617 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3618 size_int (bitsize), bitsize_int (bitpos));
3619
3620 if (bftype != type)
3621 result = fold_convert_loc (loc, type, result);
3622
3623 return result;
3624 }
3625
3626 /* Optimize a bit-field compare.
3627
3628 There are two cases: First is a compare against a constant and the
3629 second is a comparison of two items where the fields are at the same
3630 bit position relative to the start of a chunk (byte, halfword, word)
3631 large enough to contain it. In these cases we can avoid the shift
3632 implicit in bitfield extractions.
3633
3634 For constants, we emit a compare of the shifted constant with the
3635 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3636 compared. For two fields at the same position, we do the ANDs with the
3637 similar mask and compare the result of the ANDs.
3638
3639 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3640 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3641 are the left and right operands of the comparison, respectively.
3642
3643 If the optimization described above can be done, we return the resulting
3644 tree. Otherwise we return zero. */
3645
3646 static tree
3647 optimize_bit_field_compare (location_t loc, enum tree_code code,
3648 tree compare_type, tree lhs, tree rhs)
3649 {
3650 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3651 tree type = TREE_TYPE (lhs);
3652 tree unsigned_type;
3653 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3654 machine_mode lmode, rmode, nmode;
3655 int lunsignedp, runsignedp;
3656 int lvolatilep = 0, rvolatilep = 0;
3657 tree linner, rinner = NULL_TREE;
3658 tree mask;
3659 tree offset;
3660
3661 /* Get all the information about the extractions being done. If the bit size
3662 if the same as the size of the underlying object, we aren't doing an
3663 extraction at all and so can do nothing. We also don't want to
3664 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3665 then will no longer be able to replace it. */
3666 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3667 &lunsignedp, &lvolatilep, false);
3668 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3669 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3670 return 0;
3671
3672 if (!const_p)
3673 {
3674 /* If this is not a constant, we can only do something if bit positions,
3675 sizes, and signedness are the same. */
3676 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3677 &runsignedp, &rvolatilep, false);
3678
3679 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3680 || lunsignedp != runsignedp || offset != 0
3681 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3682 return 0;
3683 }
3684
3685 /* See if we can find a mode to refer to this field. We should be able to,
3686 but fail if we can't. */
3687 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3688 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3689 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3690 TYPE_ALIGN (TREE_TYPE (rinner))),
3691 word_mode, false);
3692 if (nmode == VOIDmode)
3693 return 0;
3694
3695 /* Set signed and unsigned types of the precision of this mode for the
3696 shifts below. */
3697 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3698
3699 /* Compute the bit position and size for the new reference and our offset
3700 within it. If the new reference is the same size as the original, we
3701 won't optimize anything, so return zero. */
3702 nbitsize = GET_MODE_BITSIZE (nmode);
3703 nbitpos = lbitpos & ~ (nbitsize - 1);
3704 lbitpos -= nbitpos;
3705 if (nbitsize == lbitsize)
3706 return 0;
3707
3708 if (BYTES_BIG_ENDIAN)
3709 lbitpos = nbitsize - lbitsize - lbitpos;
3710
3711 /* Make the mask to be used against the extracted field. */
3712 mask = build_int_cst_type (unsigned_type, -1);
3713 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3714 mask = const_binop (RSHIFT_EXPR, mask,
3715 size_int (nbitsize - lbitsize - lbitpos));
3716
3717 if (! const_p)
3718 /* If not comparing with constant, just rework the comparison
3719 and return. */
3720 return fold_build2_loc (loc, code, compare_type,
3721 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3722 make_bit_field_ref (loc, linner,
3723 unsigned_type,
3724 nbitsize, nbitpos,
3725 1),
3726 mask),
3727 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3728 make_bit_field_ref (loc, rinner,
3729 unsigned_type,
3730 nbitsize, nbitpos,
3731 1),
3732 mask));
3733
3734 /* Otherwise, we are handling the constant case. See if the constant is too
3735 big for the field. Warn and return a tree of for 0 (false) if so. We do
3736 this not only for its own sake, but to avoid having to test for this
3737 error case below. If we didn't, we might generate wrong code.
3738
3739 For unsigned fields, the constant shifted right by the field length should
3740 be all zero. For signed fields, the high-order bits should agree with
3741 the sign bit. */
3742
3743 if (lunsignedp)
3744 {
3745 if (wi::lrshift (rhs, lbitsize) != 0)
3746 {
3747 warning (0, "comparison is always %d due to width of bit-field",
3748 code == NE_EXPR);
3749 return constant_boolean_node (code == NE_EXPR, compare_type);
3750 }
3751 }
3752 else
3753 {
3754 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3755 if (tem != 0 && tem != -1)
3756 {
3757 warning (0, "comparison is always %d due to width of bit-field",
3758 code == NE_EXPR);
3759 return constant_boolean_node (code == NE_EXPR, compare_type);
3760 }
3761 }
3762
3763 /* Single-bit compares should always be against zero. */
3764 if (lbitsize == 1 && ! integer_zerop (rhs))
3765 {
3766 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3767 rhs = build_int_cst (type, 0);
3768 }
3769
3770 /* Make a new bitfield reference, shift the constant over the
3771 appropriate number of bits and mask it with the computed mask
3772 (in case this was a signed field). If we changed it, make a new one. */
3773 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3774
3775 rhs = const_binop (BIT_AND_EXPR,
3776 const_binop (LSHIFT_EXPR,
3777 fold_convert_loc (loc, unsigned_type, rhs),
3778 size_int (lbitpos)),
3779 mask);
3780
3781 lhs = build2_loc (loc, code, compare_type,
3782 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3783 return lhs;
3784 }
3785 \f
3786 /* Subroutine for fold_truth_andor_1: decode a field reference.
3787
3788 If EXP is a comparison reference, we return the innermost reference.
3789
3790 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3791 set to the starting bit number.
3792
3793 If the innermost field can be completely contained in a mode-sized
3794 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3795
3796 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3797 otherwise it is not changed.
3798
3799 *PUNSIGNEDP is set to the signedness of the field.
3800
3801 *PMASK is set to the mask used. This is either contained in a
3802 BIT_AND_EXPR or derived from the width of the field.
3803
3804 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3805
3806 Return 0 if this is not a component reference or is one that we can't
3807 do anything with. */
3808
3809 static tree
3810 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3811 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3812 int *punsignedp, int *pvolatilep,
3813 tree *pmask, tree *pand_mask)
3814 {
3815 tree outer_type = 0;
3816 tree and_mask = 0;
3817 tree mask, inner, offset;
3818 tree unsigned_type;
3819 unsigned int precision;
3820
3821 /* All the optimizations using this function assume integer fields.
3822 There are problems with FP fields since the type_for_size call
3823 below can fail for, e.g., XFmode. */
3824 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3825 return 0;
3826
3827 /* We are interested in the bare arrangement of bits, so strip everything
3828 that doesn't affect the machine mode. However, record the type of the
3829 outermost expression if it may matter below. */
3830 if (CONVERT_EXPR_P (exp)
3831 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3832 outer_type = TREE_TYPE (exp);
3833 STRIP_NOPS (exp);
3834
3835 if (TREE_CODE (exp) == BIT_AND_EXPR)
3836 {
3837 and_mask = TREE_OPERAND (exp, 1);
3838 exp = TREE_OPERAND (exp, 0);
3839 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3840 if (TREE_CODE (and_mask) != INTEGER_CST)
3841 return 0;
3842 }
3843
3844 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3845 punsignedp, pvolatilep, false);
3846 if ((inner == exp && and_mask == 0)
3847 || *pbitsize < 0 || offset != 0
3848 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3849 return 0;
3850
3851 /* If the number of bits in the reference is the same as the bitsize of
3852 the outer type, then the outer type gives the signedness. Otherwise
3853 (in case of a small bitfield) the signedness is unchanged. */
3854 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3855 *punsignedp = TYPE_UNSIGNED (outer_type);
3856
3857 /* Compute the mask to access the bitfield. */
3858 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3859 precision = TYPE_PRECISION (unsigned_type);
3860
3861 mask = build_int_cst_type (unsigned_type, -1);
3862
3863 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3864 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3865
3866 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3867 if (and_mask != 0)
3868 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3869 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3870
3871 *pmask = mask;
3872 *pand_mask = and_mask;
3873 return inner;
3874 }
3875
3876 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3877 bit positions and MASK is SIGNED. */
3878
3879 static int
3880 all_ones_mask_p (const_tree mask, unsigned int size)
3881 {
3882 tree type = TREE_TYPE (mask);
3883 unsigned int precision = TYPE_PRECISION (type);
3884
3885 /* If this function returns true when the type of the mask is
3886 UNSIGNED, then there will be errors. In particular see
3887 gcc.c-torture/execute/990326-1.c. There does not appear to be
3888 any documentation paper trail as to why this is so. But the pre
3889 wide-int worked with that restriction and it has been preserved
3890 here. */
3891 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3892 return false;
3893
3894 return wi::mask (size, false, precision) == mask;
3895 }
3896
3897 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3898 represents the sign bit of EXP's type. If EXP represents a sign
3899 or zero extension, also test VAL against the unextended type.
3900 The return value is the (sub)expression whose sign bit is VAL,
3901 or NULL_TREE otherwise. */
3902
3903 tree
3904 sign_bit_p (tree exp, const_tree val)
3905 {
3906 int width;
3907 tree t;
3908
3909 /* Tree EXP must have an integral type. */
3910 t = TREE_TYPE (exp);
3911 if (! INTEGRAL_TYPE_P (t))
3912 return NULL_TREE;
3913
3914 /* Tree VAL must be an integer constant. */
3915 if (TREE_CODE (val) != INTEGER_CST
3916 || TREE_OVERFLOW (val))
3917 return NULL_TREE;
3918
3919 width = TYPE_PRECISION (t);
3920 if (wi::only_sign_bit_p (val, width))
3921 return exp;
3922
3923 /* Handle extension from a narrower type. */
3924 if (TREE_CODE (exp) == NOP_EXPR
3925 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3926 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3927
3928 return NULL_TREE;
3929 }
3930
3931 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3932 to be evaluated unconditionally. */
3933
3934 static int
3935 simple_operand_p (const_tree exp)
3936 {
3937 /* Strip any conversions that don't change the machine mode. */
3938 STRIP_NOPS (exp);
3939
3940 return (CONSTANT_CLASS_P (exp)
3941 || TREE_CODE (exp) == SSA_NAME
3942 || (DECL_P (exp)
3943 && ! TREE_ADDRESSABLE (exp)
3944 && ! TREE_THIS_VOLATILE (exp)
3945 && ! DECL_NONLOCAL (exp)
3946 /* Don't regard global variables as simple. They may be
3947 allocated in ways unknown to the compiler (shared memory,
3948 #pragma weak, etc). */
3949 && ! TREE_PUBLIC (exp)
3950 && ! DECL_EXTERNAL (exp)
3951 /* Weakrefs are not safe to be read, since they can be NULL.
3952 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3953 have DECL_WEAK flag set. */
3954 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
3955 /* Loading a static variable is unduly expensive, but global
3956 registers aren't expensive. */
3957 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3958 }
3959
3960 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3961 to be evaluated unconditionally.
3962 I addition to simple_operand_p, we assume that comparisons, conversions,
3963 and logic-not operations are simple, if their operands are simple, too. */
3964
3965 static bool
3966 simple_operand_p_2 (tree exp)
3967 {
3968 enum tree_code code;
3969
3970 if (TREE_SIDE_EFFECTS (exp)
3971 || tree_could_trap_p (exp))
3972 return false;
3973
3974 while (CONVERT_EXPR_P (exp))
3975 exp = TREE_OPERAND (exp, 0);
3976
3977 code = TREE_CODE (exp);
3978
3979 if (TREE_CODE_CLASS (code) == tcc_comparison)
3980 return (simple_operand_p (TREE_OPERAND (exp, 0))
3981 && simple_operand_p (TREE_OPERAND (exp, 1)));
3982
3983 if (code == TRUTH_NOT_EXPR)
3984 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3985
3986 return simple_operand_p (exp);
3987 }
3988
3989 \f
3990 /* The following functions are subroutines to fold_range_test and allow it to
3991 try to change a logical combination of comparisons into a range test.
3992
3993 For example, both
3994 X == 2 || X == 3 || X == 4 || X == 5
3995 and
3996 X >= 2 && X <= 5
3997 are converted to
3998 (unsigned) (X - 2) <= 3
3999
4000 We describe each set of comparisons as being either inside or outside
4001 a range, using a variable named like IN_P, and then describe the
4002 range with a lower and upper bound. If one of the bounds is omitted,
4003 it represents either the highest or lowest value of the type.
4004
4005 In the comments below, we represent a range by two numbers in brackets
4006 preceded by a "+" to designate being inside that range, or a "-" to
4007 designate being outside that range, so the condition can be inverted by
4008 flipping the prefix. An omitted bound is represented by a "-". For
4009 example, "- [-, 10]" means being outside the range starting at the lowest
4010 possible value and ending at 10, in other words, being greater than 10.
4011 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4012 always false.
4013
4014 We set up things so that the missing bounds are handled in a consistent
4015 manner so neither a missing bound nor "true" and "false" need to be
4016 handled using a special case. */
4017
4018 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4019 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4020 and UPPER1_P are nonzero if the respective argument is an upper bound
4021 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4022 must be specified for a comparison. ARG1 will be converted to ARG0's
4023 type if both are specified. */
4024
4025 static tree
4026 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4027 tree arg1, int upper1_p)
4028 {
4029 tree tem;
4030 int result;
4031 int sgn0, sgn1;
4032
4033 /* If neither arg represents infinity, do the normal operation.
4034 Else, if not a comparison, return infinity. Else handle the special
4035 comparison rules. Note that most of the cases below won't occur, but
4036 are handled for consistency. */
4037
4038 if (arg0 != 0 && arg1 != 0)
4039 {
4040 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4041 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4042 STRIP_NOPS (tem);
4043 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4044 }
4045
4046 if (TREE_CODE_CLASS (code) != tcc_comparison)
4047 return 0;
4048
4049 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4050 for neither. In real maths, we cannot assume open ended ranges are
4051 the same. But, this is computer arithmetic, where numbers are finite.
4052 We can therefore make the transformation of any unbounded range with
4053 the value Z, Z being greater than any representable number. This permits
4054 us to treat unbounded ranges as equal. */
4055 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4056 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4057 switch (code)
4058 {
4059 case EQ_EXPR:
4060 result = sgn0 == sgn1;
4061 break;
4062 case NE_EXPR:
4063 result = sgn0 != sgn1;
4064 break;
4065 case LT_EXPR:
4066 result = sgn0 < sgn1;
4067 break;
4068 case LE_EXPR:
4069 result = sgn0 <= sgn1;
4070 break;
4071 case GT_EXPR:
4072 result = sgn0 > sgn1;
4073 break;
4074 case GE_EXPR:
4075 result = sgn0 >= sgn1;
4076 break;
4077 default:
4078 gcc_unreachable ();
4079 }
4080
4081 return constant_boolean_node (result, type);
4082 }
4083 \f
4084 /* Helper routine for make_range. Perform one step for it, return
4085 new expression if the loop should continue or NULL_TREE if it should
4086 stop. */
4087
4088 tree
4089 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4090 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4091 bool *strict_overflow_p)
4092 {
4093 tree arg0_type = TREE_TYPE (arg0);
4094 tree n_low, n_high, low = *p_low, high = *p_high;
4095 int in_p = *p_in_p, n_in_p;
4096
4097 switch (code)
4098 {
4099 case TRUTH_NOT_EXPR:
4100 /* We can only do something if the range is testing for zero. */
4101 if (low == NULL_TREE || high == NULL_TREE
4102 || ! integer_zerop (low) || ! integer_zerop (high))
4103 return NULL_TREE;
4104 *p_in_p = ! in_p;
4105 return arg0;
4106
4107 case EQ_EXPR: case NE_EXPR:
4108 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4109 /* We can only do something if the range is testing for zero
4110 and if the second operand is an integer constant. Note that
4111 saying something is "in" the range we make is done by
4112 complementing IN_P since it will set in the initial case of
4113 being not equal to zero; "out" is leaving it alone. */
4114 if (low == NULL_TREE || high == NULL_TREE
4115 || ! integer_zerop (low) || ! integer_zerop (high)
4116 || TREE_CODE (arg1) != INTEGER_CST)
4117 return NULL_TREE;
4118
4119 switch (code)
4120 {
4121 case NE_EXPR: /* - [c, c] */
4122 low = high = arg1;
4123 break;
4124 case EQ_EXPR: /* + [c, c] */
4125 in_p = ! in_p, low = high = arg1;
4126 break;
4127 case GT_EXPR: /* - [-, c] */
4128 low = 0, high = arg1;
4129 break;
4130 case GE_EXPR: /* + [c, -] */
4131 in_p = ! in_p, low = arg1, high = 0;
4132 break;
4133 case LT_EXPR: /* - [c, -] */
4134 low = arg1, high = 0;
4135 break;
4136 case LE_EXPR: /* + [-, c] */
4137 in_p = ! in_p, low = 0, high = arg1;
4138 break;
4139 default:
4140 gcc_unreachable ();
4141 }
4142
4143 /* If this is an unsigned comparison, we also know that EXP is
4144 greater than or equal to zero. We base the range tests we make
4145 on that fact, so we record it here so we can parse existing
4146 range tests. We test arg0_type since often the return type
4147 of, e.g. EQ_EXPR, is boolean. */
4148 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4149 {
4150 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4151 in_p, low, high, 1,
4152 build_int_cst (arg0_type, 0),
4153 NULL_TREE))
4154 return NULL_TREE;
4155
4156 in_p = n_in_p, low = n_low, high = n_high;
4157
4158 /* If the high bound is missing, but we have a nonzero low
4159 bound, reverse the range so it goes from zero to the low bound
4160 minus 1. */
4161 if (high == 0 && low && ! integer_zerop (low))
4162 {
4163 in_p = ! in_p;
4164 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4165 build_int_cst (TREE_TYPE (low), 1), 0);
4166 low = build_int_cst (arg0_type, 0);
4167 }
4168 }
4169
4170 *p_low = low;
4171 *p_high = high;
4172 *p_in_p = in_p;
4173 return arg0;
4174
4175 case NEGATE_EXPR:
4176 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4177 low and high are non-NULL, then normalize will DTRT. */
4178 if (!TYPE_UNSIGNED (arg0_type)
4179 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4180 {
4181 if (low == NULL_TREE)
4182 low = TYPE_MIN_VALUE (arg0_type);
4183 if (high == NULL_TREE)
4184 high = TYPE_MAX_VALUE (arg0_type);
4185 }
4186
4187 /* (-x) IN [a,b] -> x in [-b, -a] */
4188 n_low = range_binop (MINUS_EXPR, exp_type,
4189 build_int_cst (exp_type, 0),
4190 0, high, 1);
4191 n_high = range_binop (MINUS_EXPR, exp_type,
4192 build_int_cst (exp_type, 0),
4193 0, low, 0);
4194 if (n_high != 0 && TREE_OVERFLOW (n_high))
4195 return NULL_TREE;
4196 goto normalize;
4197
4198 case BIT_NOT_EXPR:
4199 /* ~ X -> -X - 1 */
4200 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4201 build_int_cst (exp_type, 1));
4202
4203 case PLUS_EXPR:
4204 case MINUS_EXPR:
4205 if (TREE_CODE (arg1) != INTEGER_CST)
4206 return NULL_TREE;
4207
4208 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4209 move a constant to the other side. */
4210 if (!TYPE_UNSIGNED (arg0_type)
4211 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4212 return NULL_TREE;
4213
4214 /* If EXP is signed, any overflow in the computation is undefined,
4215 so we don't worry about it so long as our computations on
4216 the bounds don't overflow. For unsigned, overflow is defined
4217 and this is exactly the right thing. */
4218 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4219 arg0_type, low, 0, arg1, 0);
4220 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4221 arg0_type, high, 1, arg1, 0);
4222 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4223 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4224 return NULL_TREE;
4225
4226 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4227 *strict_overflow_p = true;
4228
4229 normalize:
4230 /* Check for an unsigned range which has wrapped around the maximum
4231 value thus making n_high < n_low, and normalize it. */
4232 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4233 {
4234 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4235 build_int_cst (TREE_TYPE (n_high), 1), 0);
4236 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4237 build_int_cst (TREE_TYPE (n_low), 1), 0);
4238
4239 /* If the range is of the form +/- [ x+1, x ], we won't
4240 be able to normalize it. But then, it represents the
4241 whole range or the empty set, so make it
4242 +/- [ -, - ]. */
4243 if (tree_int_cst_equal (n_low, low)
4244 && tree_int_cst_equal (n_high, high))
4245 low = high = 0;
4246 else
4247 in_p = ! in_p;
4248 }
4249 else
4250 low = n_low, high = n_high;
4251
4252 *p_low = low;
4253 *p_high = high;
4254 *p_in_p = in_p;
4255 return arg0;
4256
4257 CASE_CONVERT:
4258 case NON_LVALUE_EXPR:
4259 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4260 return NULL_TREE;
4261
4262 if (! INTEGRAL_TYPE_P (arg0_type)
4263 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4264 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4265 return NULL_TREE;
4266
4267 n_low = low, n_high = high;
4268
4269 if (n_low != 0)
4270 n_low = fold_convert_loc (loc, arg0_type, n_low);
4271
4272 if (n_high != 0)
4273 n_high = fold_convert_loc (loc, arg0_type, n_high);
4274
4275 /* If we're converting arg0 from an unsigned type, to exp,
4276 a signed type, we will be doing the comparison as unsigned.
4277 The tests above have already verified that LOW and HIGH
4278 are both positive.
4279
4280 So we have to ensure that we will handle large unsigned
4281 values the same way that the current signed bounds treat
4282 negative values. */
4283
4284 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4285 {
4286 tree high_positive;
4287 tree equiv_type;
4288 /* For fixed-point modes, we need to pass the saturating flag
4289 as the 2nd parameter. */
4290 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4291 equiv_type
4292 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4293 TYPE_SATURATING (arg0_type));
4294 else
4295 equiv_type
4296 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4297
4298 /* A range without an upper bound is, naturally, unbounded.
4299 Since convert would have cropped a very large value, use
4300 the max value for the destination type. */
4301 high_positive
4302 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4303 : TYPE_MAX_VALUE (arg0_type);
4304
4305 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4306 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4307 fold_convert_loc (loc, arg0_type,
4308 high_positive),
4309 build_int_cst (arg0_type, 1));
4310
4311 /* If the low bound is specified, "and" the range with the
4312 range for which the original unsigned value will be
4313 positive. */
4314 if (low != 0)
4315 {
4316 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4317 1, fold_convert_loc (loc, arg0_type,
4318 integer_zero_node),
4319 high_positive))
4320 return NULL_TREE;
4321
4322 in_p = (n_in_p == in_p);
4323 }
4324 else
4325 {
4326 /* Otherwise, "or" the range with the range of the input
4327 that will be interpreted as negative. */
4328 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4329 1, fold_convert_loc (loc, arg0_type,
4330 integer_zero_node),
4331 high_positive))
4332 return NULL_TREE;
4333
4334 in_p = (in_p != n_in_p);
4335 }
4336 }
4337
4338 *p_low = n_low;
4339 *p_high = n_high;
4340 *p_in_p = in_p;
4341 return arg0;
4342
4343 default:
4344 return NULL_TREE;
4345 }
4346 }
4347
4348 /* Given EXP, a logical expression, set the range it is testing into
4349 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4350 actually being tested. *PLOW and *PHIGH will be made of the same
4351 type as the returned expression. If EXP is not a comparison, we
4352 will most likely not be returning a useful value and range. Set
4353 *STRICT_OVERFLOW_P to true if the return value is only valid
4354 because signed overflow is undefined; otherwise, do not change
4355 *STRICT_OVERFLOW_P. */
4356
4357 tree
4358 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4359 bool *strict_overflow_p)
4360 {
4361 enum tree_code code;
4362 tree arg0, arg1 = NULL_TREE;
4363 tree exp_type, nexp;
4364 int in_p;
4365 tree low, high;
4366 location_t loc = EXPR_LOCATION (exp);
4367
4368 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4369 and see if we can refine the range. Some of the cases below may not
4370 happen, but it doesn't seem worth worrying about this. We "continue"
4371 the outer loop when we've changed something; otherwise we "break"
4372 the switch, which will "break" the while. */
4373
4374 in_p = 0;
4375 low = high = build_int_cst (TREE_TYPE (exp), 0);
4376
4377 while (1)
4378 {
4379 code = TREE_CODE (exp);
4380 exp_type = TREE_TYPE (exp);
4381 arg0 = NULL_TREE;
4382
4383 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4384 {
4385 if (TREE_OPERAND_LENGTH (exp) > 0)
4386 arg0 = TREE_OPERAND (exp, 0);
4387 if (TREE_CODE_CLASS (code) == tcc_binary
4388 || TREE_CODE_CLASS (code) == tcc_comparison
4389 || (TREE_CODE_CLASS (code) == tcc_expression
4390 && TREE_OPERAND_LENGTH (exp) > 1))
4391 arg1 = TREE_OPERAND (exp, 1);
4392 }
4393 if (arg0 == NULL_TREE)
4394 break;
4395
4396 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4397 &high, &in_p, strict_overflow_p);
4398 if (nexp == NULL_TREE)
4399 break;
4400 exp = nexp;
4401 }
4402
4403 /* If EXP is a constant, we can evaluate whether this is true or false. */
4404 if (TREE_CODE (exp) == INTEGER_CST)
4405 {
4406 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4407 exp, 0, low, 0))
4408 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4409 exp, 1, high, 1)));
4410 low = high = 0;
4411 exp = 0;
4412 }
4413
4414 *pin_p = in_p, *plow = low, *phigh = high;
4415 return exp;
4416 }
4417 \f
4418 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4419 type, TYPE, return an expression to test if EXP is in (or out of, depending
4420 on IN_P) the range. Return 0 if the test couldn't be created. */
4421
4422 tree
4423 build_range_check (location_t loc, tree type, tree exp, int in_p,
4424 tree low, tree high)
4425 {
4426 tree etype = TREE_TYPE (exp), value;
4427
4428 /* Disable this optimization for function pointer expressions
4429 on targets that require function pointer canonicalization. */
4430 if (targetm.have_canonicalize_funcptr_for_compare ()
4431 && TREE_CODE (etype) == POINTER_TYPE
4432 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4433 return NULL_TREE;
4434
4435 if (! in_p)
4436 {
4437 value = build_range_check (loc, type, exp, 1, low, high);
4438 if (value != 0)
4439 return invert_truthvalue_loc (loc, value);
4440
4441 return 0;
4442 }
4443
4444 if (low == 0 && high == 0)
4445 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4446
4447 if (low == 0)
4448 return fold_build2_loc (loc, LE_EXPR, type, exp,
4449 fold_convert_loc (loc, etype, high));
4450
4451 if (high == 0)
4452 return fold_build2_loc (loc, GE_EXPR, type, exp,
4453 fold_convert_loc (loc, etype, low));
4454
4455 if (operand_equal_p (low, high, 0))
4456 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4457 fold_convert_loc (loc, etype, low));
4458
4459 if (integer_zerop (low))
4460 {
4461 if (! TYPE_UNSIGNED (etype))
4462 {
4463 etype = unsigned_type_for (etype);
4464 high = fold_convert_loc (loc, etype, high);
4465 exp = fold_convert_loc (loc, etype, exp);
4466 }
4467 return build_range_check (loc, type, exp, 1, 0, high);
4468 }
4469
4470 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4471 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4472 {
4473 int prec = TYPE_PRECISION (etype);
4474
4475 if (wi::mask (prec - 1, false, prec) == high)
4476 {
4477 if (TYPE_UNSIGNED (etype))
4478 {
4479 tree signed_etype = signed_type_for (etype);
4480 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4481 etype
4482 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4483 else
4484 etype = signed_etype;
4485 exp = fold_convert_loc (loc, etype, exp);
4486 }
4487 return fold_build2_loc (loc, GT_EXPR, type, exp,
4488 build_int_cst (etype, 0));
4489 }
4490 }
4491
4492 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4493 This requires wrap-around arithmetics for the type of the expression.
4494 First make sure that arithmetics in this type is valid, then make sure
4495 that it wraps around. */
4496 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4497 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4498 TYPE_UNSIGNED (etype));
4499
4500 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4501 {
4502 tree utype, minv, maxv;
4503
4504 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4505 for the type in question, as we rely on this here. */
4506 utype = unsigned_type_for (etype);
4507 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4508 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4509 build_int_cst (TREE_TYPE (maxv), 1), 1);
4510 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4511
4512 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4513 minv, 1, maxv, 1)))
4514 etype = utype;
4515 else
4516 return 0;
4517 }
4518
4519 high = fold_convert_loc (loc, etype, high);
4520 low = fold_convert_loc (loc, etype, low);
4521 exp = fold_convert_loc (loc, etype, exp);
4522
4523 value = const_binop (MINUS_EXPR, high, low);
4524
4525
4526 if (POINTER_TYPE_P (etype))
4527 {
4528 if (value != 0 && !TREE_OVERFLOW (value))
4529 {
4530 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4531 return build_range_check (loc, type,
4532 fold_build_pointer_plus_loc (loc, exp, low),
4533 1, build_int_cst (etype, 0), value);
4534 }
4535 return 0;
4536 }
4537
4538 if (value != 0 && !TREE_OVERFLOW (value))
4539 return build_range_check (loc, type,
4540 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4541 1, build_int_cst (etype, 0), value);
4542
4543 return 0;
4544 }
4545 \f
4546 /* Return the predecessor of VAL in its type, handling the infinite case. */
4547
4548 static tree
4549 range_predecessor (tree val)
4550 {
4551 tree type = TREE_TYPE (val);
4552
4553 if (INTEGRAL_TYPE_P (type)
4554 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4555 return 0;
4556 else
4557 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4558 build_int_cst (TREE_TYPE (val), 1), 0);
4559 }
4560
4561 /* Return the successor of VAL in its type, handling the infinite case. */
4562
4563 static tree
4564 range_successor (tree val)
4565 {
4566 tree type = TREE_TYPE (val);
4567
4568 if (INTEGRAL_TYPE_P (type)
4569 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4570 return 0;
4571 else
4572 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4573 build_int_cst (TREE_TYPE (val), 1), 0);
4574 }
4575
4576 /* Given two ranges, see if we can merge them into one. Return 1 if we
4577 can, 0 if we can't. Set the output range into the specified parameters. */
4578
4579 bool
4580 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4581 tree high0, int in1_p, tree low1, tree high1)
4582 {
4583 int no_overlap;
4584 int subset;
4585 int temp;
4586 tree tem;
4587 int in_p;
4588 tree low, high;
4589 int lowequal = ((low0 == 0 && low1 == 0)
4590 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4591 low0, 0, low1, 0)));
4592 int highequal = ((high0 == 0 && high1 == 0)
4593 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4594 high0, 1, high1, 1)));
4595
4596 /* Make range 0 be the range that starts first, or ends last if they
4597 start at the same value. Swap them if it isn't. */
4598 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4599 low0, 0, low1, 0))
4600 || (lowequal
4601 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4602 high1, 1, high0, 1))))
4603 {
4604 temp = in0_p, in0_p = in1_p, in1_p = temp;
4605 tem = low0, low0 = low1, low1 = tem;
4606 tem = high0, high0 = high1, high1 = tem;
4607 }
4608
4609 /* Now flag two cases, whether the ranges are disjoint or whether the
4610 second range is totally subsumed in the first. Note that the tests
4611 below are simplified by the ones above. */
4612 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4613 high0, 1, low1, 0));
4614 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4615 high1, 1, high0, 1));
4616
4617 /* We now have four cases, depending on whether we are including or
4618 excluding the two ranges. */
4619 if (in0_p && in1_p)
4620 {
4621 /* If they don't overlap, the result is false. If the second range
4622 is a subset it is the result. Otherwise, the range is from the start
4623 of the second to the end of the first. */
4624 if (no_overlap)
4625 in_p = 0, low = high = 0;
4626 else if (subset)
4627 in_p = 1, low = low1, high = high1;
4628 else
4629 in_p = 1, low = low1, high = high0;
4630 }
4631
4632 else if (in0_p && ! in1_p)
4633 {
4634 /* If they don't overlap, the result is the first range. If they are
4635 equal, the result is false. If the second range is a subset of the
4636 first, and the ranges begin at the same place, we go from just after
4637 the end of the second range to the end of the first. If the second
4638 range is not a subset of the first, or if it is a subset and both
4639 ranges end at the same place, the range starts at the start of the
4640 first range and ends just before the second range.
4641 Otherwise, we can't describe this as a single range. */
4642 if (no_overlap)
4643 in_p = 1, low = low0, high = high0;
4644 else if (lowequal && highequal)
4645 in_p = 0, low = high = 0;
4646 else if (subset && lowequal)
4647 {
4648 low = range_successor (high1);
4649 high = high0;
4650 in_p = 1;
4651 if (low == 0)
4652 {
4653 /* We are in the weird situation where high0 > high1 but
4654 high1 has no successor. Punt. */
4655 return 0;
4656 }
4657 }
4658 else if (! subset || highequal)
4659 {
4660 low = low0;
4661 high = range_predecessor (low1);
4662 in_p = 1;
4663 if (high == 0)
4664 {
4665 /* low0 < low1 but low1 has no predecessor. Punt. */
4666 return 0;
4667 }
4668 }
4669 else
4670 return 0;
4671 }
4672
4673 else if (! in0_p && in1_p)
4674 {
4675 /* If they don't overlap, the result is the second range. If the second
4676 is a subset of the first, the result is false. Otherwise,
4677 the range starts just after the first range and ends at the
4678 end of the second. */
4679 if (no_overlap)
4680 in_p = 1, low = low1, high = high1;
4681 else if (subset || highequal)
4682 in_p = 0, low = high = 0;
4683 else
4684 {
4685 low = range_successor (high0);
4686 high = high1;
4687 in_p = 1;
4688 if (low == 0)
4689 {
4690 /* high1 > high0 but high0 has no successor. Punt. */
4691 return 0;
4692 }
4693 }
4694 }
4695
4696 else
4697 {
4698 /* The case where we are excluding both ranges. Here the complex case
4699 is if they don't overlap. In that case, the only time we have a
4700 range is if they are adjacent. If the second is a subset of the
4701 first, the result is the first. Otherwise, the range to exclude
4702 starts at the beginning of the first range and ends at the end of the
4703 second. */
4704 if (no_overlap)
4705 {
4706 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4707 range_successor (high0),
4708 1, low1, 0)))
4709 in_p = 0, low = low0, high = high1;
4710 else
4711 {
4712 /* Canonicalize - [min, x] into - [-, x]. */
4713 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4714 switch (TREE_CODE (TREE_TYPE (low0)))
4715 {
4716 case ENUMERAL_TYPE:
4717 if (TYPE_PRECISION (TREE_TYPE (low0))
4718 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4719 break;
4720 /* FALLTHROUGH */
4721 case INTEGER_TYPE:
4722 if (tree_int_cst_equal (low0,
4723 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4724 low0 = 0;
4725 break;
4726 case POINTER_TYPE:
4727 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4728 && integer_zerop (low0))
4729 low0 = 0;
4730 break;
4731 default:
4732 break;
4733 }
4734
4735 /* Canonicalize - [x, max] into - [x, -]. */
4736 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4737 switch (TREE_CODE (TREE_TYPE (high1)))
4738 {
4739 case ENUMERAL_TYPE:
4740 if (TYPE_PRECISION (TREE_TYPE (high1))
4741 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4742 break;
4743 /* FALLTHROUGH */
4744 case INTEGER_TYPE:
4745 if (tree_int_cst_equal (high1,
4746 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4747 high1 = 0;
4748 break;
4749 case POINTER_TYPE:
4750 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4751 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4752 high1, 1,
4753 build_int_cst (TREE_TYPE (high1), 1),
4754 1)))
4755 high1 = 0;
4756 break;
4757 default:
4758 break;
4759 }
4760
4761 /* The ranges might be also adjacent between the maximum and
4762 minimum values of the given type. For
4763 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4764 return + [x + 1, y - 1]. */
4765 if (low0 == 0 && high1 == 0)
4766 {
4767 low = range_successor (high0);
4768 high = range_predecessor (low1);
4769 if (low == 0 || high == 0)
4770 return 0;
4771
4772 in_p = 1;
4773 }
4774 else
4775 return 0;
4776 }
4777 }
4778 else if (subset)
4779 in_p = 0, low = low0, high = high0;
4780 else
4781 in_p = 0, low = low0, high = high1;
4782 }
4783
4784 *pin_p = in_p, *plow = low, *phigh = high;
4785 return 1;
4786 }
4787 \f
4788
4789 /* Subroutine of fold, looking inside expressions of the form
4790 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4791 of the COND_EXPR. This function is being used also to optimize
4792 A op B ? C : A, by reversing the comparison first.
4793
4794 Return a folded expression whose code is not a COND_EXPR
4795 anymore, or NULL_TREE if no folding opportunity is found. */
4796
4797 static tree
4798 fold_cond_expr_with_comparison (location_t loc, tree type,
4799 tree arg0, tree arg1, tree arg2)
4800 {
4801 enum tree_code comp_code = TREE_CODE (arg0);
4802 tree arg00 = TREE_OPERAND (arg0, 0);
4803 tree arg01 = TREE_OPERAND (arg0, 1);
4804 tree arg1_type = TREE_TYPE (arg1);
4805 tree tem;
4806
4807 STRIP_NOPS (arg1);
4808 STRIP_NOPS (arg2);
4809
4810 /* If we have A op 0 ? A : -A, consider applying the following
4811 transformations:
4812
4813 A == 0? A : -A same as -A
4814 A != 0? A : -A same as A
4815 A >= 0? A : -A same as abs (A)
4816 A > 0? A : -A same as abs (A)
4817 A <= 0? A : -A same as -abs (A)
4818 A < 0? A : -A same as -abs (A)
4819
4820 None of these transformations work for modes with signed
4821 zeros. If A is +/-0, the first two transformations will
4822 change the sign of the result (from +0 to -0, or vice
4823 versa). The last four will fix the sign of the result,
4824 even though the original expressions could be positive or
4825 negative, depending on the sign of A.
4826
4827 Note that all these transformations are correct if A is
4828 NaN, since the two alternatives (A and -A) are also NaNs. */
4829 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4830 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4831 ? real_zerop (arg01)
4832 : integer_zerop (arg01))
4833 && ((TREE_CODE (arg2) == NEGATE_EXPR
4834 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4835 /* In the case that A is of the form X-Y, '-A' (arg2) may
4836 have already been folded to Y-X, check for that. */
4837 || (TREE_CODE (arg1) == MINUS_EXPR
4838 && TREE_CODE (arg2) == MINUS_EXPR
4839 && operand_equal_p (TREE_OPERAND (arg1, 0),
4840 TREE_OPERAND (arg2, 1), 0)
4841 && operand_equal_p (TREE_OPERAND (arg1, 1),
4842 TREE_OPERAND (arg2, 0), 0))))
4843 switch (comp_code)
4844 {
4845 case EQ_EXPR:
4846 case UNEQ_EXPR:
4847 tem = fold_convert_loc (loc, arg1_type, arg1);
4848 return pedantic_non_lvalue_loc (loc,
4849 fold_convert_loc (loc, type,
4850 negate_expr (tem)));
4851 case NE_EXPR:
4852 case LTGT_EXPR:
4853 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4854 case UNGE_EXPR:
4855 case UNGT_EXPR:
4856 if (flag_trapping_math)
4857 break;
4858 /* Fall through. */
4859 case GE_EXPR:
4860 case GT_EXPR:
4861 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4862 arg1 = fold_convert_loc (loc, signed_type_for
4863 (TREE_TYPE (arg1)), arg1);
4864 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4865 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4866 case UNLE_EXPR:
4867 case UNLT_EXPR:
4868 if (flag_trapping_math)
4869 break;
4870 case LE_EXPR:
4871 case LT_EXPR:
4872 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4873 arg1 = fold_convert_loc (loc, signed_type_for
4874 (TREE_TYPE (arg1)), arg1);
4875 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4876 return negate_expr (fold_convert_loc (loc, type, tem));
4877 default:
4878 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4879 break;
4880 }
4881
4882 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4883 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4884 both transformations are correct when A is NaN: A != 0
4885 is then true, and A == 0 is false. */
4886
4887 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4888 && integer_zerop (arg01) && integer_zerop (arg2))
4889 {
4890 if (comp_code == NE_EXPR)
4891 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4892 else if (comp_code == EQ_EXPR)
4893 return build_zero_cst (type);
4894 }
4895
4896 /* Try some transformations of A op B ? A : B.
4897
4898 A == B? A : B same as B
4899 A != B? A : B same as A
4900 A >= B? A : B same as max (A, B)
4901 A > B? A : B same as max (B, A)
4902 A <= B? A : B same as min (A, B)
4903 A < B? A : B same as min (B, A)
4904
4905 As above, these transformations don't work in the presence
4906 of signed zeros. For example, if A and B are zeros of
4907 opposite sign, the first two transformations will change
4908 the sign of the result. In the last four, the original
4909 expressions give different results for (A=+0, B=-0) and
4910 (A=-0, B=+0), but the transformed expressions do not.
4911
4912 The first two transformations are correct if either A or B
4913 is a NaN. In the first transformation, the condition will
4914 be false, and B will indeed be chosen. In the case of the
4915 second transformation, the condition A != B will be true,
4916 and A will be chosen.
4917
4918 The conversions to max() and min() are not correct if B is
4919 a number and A is not. The conditions in the original
4920 expressions will be false, so all four give B. The min()
4921 and max() versions would give a NaN instead. */
4922 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4923 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4924 /* Avoid these transformations if the COND_EXPR may be used
4925 as an lvalue in the C++ front-end. PR c++/19199. */
4926 && (in_gimple_form
4927 || VECTOR_TYPE_P (type)
4928 || (! lang_GNU_CXX ()
4929 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4930 || ! maybe_lvalue_p (arg1)
4931 || ! maybe_lvalue_p (arg2)))
4932 {
4933 tree comp_op0 = arg00;
4934 tree comp_op1 = arg01;
4935 tree comp_type = TREE_TYPE (comp_op0);
4936
4937 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4938 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4939 {
4940 comp_type = type;
4941 comp_op0 = arg1;
4942 comp_op1 = arg2;
4943 }
4944
4945 switch (comp_code)
4946 {
4947 case EQ_EXPR:
4948 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4949 case NE_EXPR:
4950 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4951 case LE_EXPR:
4952 case LT_EXPR:
4953 case UNLE_EXPR:
4954 case UNLT_EXPR:
4955 /* In C++ a ?: expression can be an lvalue, so put the
4956 operand which will be used if they are equal first
4957 so that we can convert this back to the
4958 corresponding COND_EXPR. */
4959 if (!HONOR_NANS (arg1))
4960 {
4961 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4962 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4963 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4964 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4965 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4966 comp_op1, comp_op0);
4967 return pedantic_non_lvalue_loc (loc,
4968 fold_convert_loc (loc, type, tem));
4969 }
4970 break;
4971 case GE_EXPR:
4972 case GT_EXPR:
4973 case UNGE_EXPR:
4974 case UNGT_EXPR:
4975 if (!HONOR_NANS (arg1))
4976 {
4977 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4978 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4979 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4980 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4981 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4982 comp_op1, comp_op0);
4983 return pedantic_non_lvalue_loc (loc,
4984 fold_convert_loc (loc, type, tem));
4985 }
4986 break;
4987 case UNEQ_EXPR:
4988 if (!HONOR_NANS (arg1))
4989 return pedantic_non_lvalue_loc (loc,
4990 fold_convert_loc (loc, type, arg2));
4991 break;
4992 case LTGT_EXPR:
4993 if (!HONOR_NANS (arg1))
4994 return pedantic_non_lvalue_loc (loc,
4995 fold_convert_loc (loc, type, arg1));
4996 break;
4997 default:
4998 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4999 break;
5000 }
5001 }
5002
5003 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5004 we might still be able to simplify this. For example,
5005 if C1 is one less or one more than C2, this might have started
5006 out as a MIN or MAX and been transformed by this function.
5007 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5008
5009 if (INTEGRAL_TYPE_P (type)
5010 && TREE_CODE (arg01) == INTEGER_CST
5011 && TREE_CODE (arg2) == INTEGER_CST)
5012 switch (comp_code)
5013 {
5014 case EQ_EXPR:
5015 if (TREE_CODE (arg1) == INTEGER_CST)
5016 break;
5017 /* We can replace A with C1 in this case. */
5018 arg1 = fold_convert_loc (loc, type, arg01);
5019 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5020
5021 case LT_EXPR:
5022 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5023 MIN_EXPR, to preserve the signedness of the comparison. */
5024 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5025 OEP_ONLY_CONST)
5026 && operand_equal_p (arg01,
5027 const_binop (PLUS_EXPR, arg2,
5028 build_int_cst (type, 1)),
5029 OEP_ONLY_CONST))
5030 {
5031 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5032 fold_convert_loc (loc, TREE_TYPE (arg00),
5033 arg2));
5034 return pedantic_non_lvalue_loc (loc,
5035 fold_convert_loc (loc, type, tem));
5036 }
5037 break;
5038
5039 case LE_EXPR:
5040 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5041 as above. */
5042 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5043 OEP_ONLY_CONST)
5044 && operand_equal_p (arg01,
5045 const_binop (MINUS_EXPR, arg2,
5046 build_int_cst (type, 1)),
5047 OEP_ONLY_CONST))
5048 {
5049 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5050 fold_convert_loc (loc, TREE_TYPE (arg00),
5051 arg2));
5052 return pedantic_non_lvalue_loc (loc,
5053 fold_convert_loc (loc, type, tem));
5054 }
5055 break;
5056
5057 case GT_EXPR:
5058 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5059 MAX_EXPR, to preserve the signedness of the comparison. */
5060 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5061 OEP_ONLY_CONST)
5062 && operand_equal_p (arg01,
5063 const_binop (MINUS_EXPR, arg2,
5064 build_int_cst (type, 1)),
5065 OEP_ONLY_CONST))
5066 {
5067 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5068 fold_convert_loc (loc, TREE_TYPE (arg00),
5069 arg2));
5070 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5071 }
5072 break;
5073
5074 case GE_EXPR:
5075 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5076 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5077 OEP_ONLY_CONST)
5078 && operand_equal_p (arg01,
5079 const_binop (PLUS_EXPR, arg2,
5080 build_int_cst (type, 1)),
5081 OEP_ONLY_CONST))
5082 {
5083 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5084 fold_convert_loc (loc, TREE_TYPE (arg00),
5085 arg2));
5086 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5087 }
5088 break;
5089 case NE_EXPR:
5090 break;
5091 default:
5092 gcc_unreachable ();
5093 }
5094
5095 return NULL_TREE;
5096 }
5097
5098
5099 \f
5100 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5101 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5102 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5103 false) >= 2)
5104 #endif
5105
5106 /* EXP is some logical combination of boolean tests. See if we can
5107 merge it into some range test. Return the new tree if so. */
5108
5109 static tree
5110 fold_range_test (location_t loc, enum tree_code code, tree type,
5111 tree op0, tree op1)
5112 {
5113 int or_op = (code == TRUTH_ORIF_EXPR
5114 || code == TRUTH_OR_EXPR);
5115 int in0_p, in1_p, in_p;
5116 tree low0, low1, low, high0, high1, high;
5117 bool strict_overflow_p = false;
5118 tree tem, lhs, rhs;
5119 const char * const warnmsg = G_("assuming signed overflow does not occur "
5120 "when simplifying range test");
5121
5122 if (!INTEGRAL_TYPE_P (type))
5123 return 0;
5124
5125 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5126 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5127
5128 /* If this is an OR operation, invert both sides; we will invert
5129 again at the end. */
5130 if (or_op)
5131 in0_p = ! in0_p, in1_p = ! in1_p;
5132
5133 /* If both expressions are the same, if we can merge the ranges, and we
5134 can build the range test, return it or it inverted. If one of the
5135 ranges is always true or always false, consider it to be the same
5136 expression as the other. */
5137 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5138 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5139 in1_p, low1, high1)
5140 && 0 != (tem = (build_range_check (loc, type,
5141 lhs != 0 ? lhs
5142 : rhs != 0 ? rhs : integer_zero_node,
5143 in_p, low, high))))
5144 {
5145 if (strict_overflow_p)
5146 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5147 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5148 }
5149
5150 /* On machines where the branch cost is expensive, if this is a
5151 short-circuited branch and the underlying object on both sides
5152 is the same, make a non-short-circuit operation. */
5153 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5154 && lhs != 0 && rhs != 0
5155 && (code == TRUTH_ANDIF_EXPR
5156 || code == TRUTH_ORIF_EXPR)
5157 && operand_equal_p (lhs, rhs, 0))
5158 {
5159 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5160 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5161 which cases we can't do this. */
5162 if (simple_operand_p (lhs))
5163 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5164 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5165 type, op0, op1);
5166
5167 else if (!lang_hooks.decls.global_bindings_p ()
5168 && !CONTAINS_PLACEHOLDER_P (lhs))
5169 {
5170 tree common = save_expr (lhs);
5171
5172 if (0 != (lhs = build_range_check (loc, type, common,
5173 or_op ? ! in0_p : in0_p,
5174 low0, high0))
5175 && (0 != (rhs = build_range_check (loc, type, common,
5176 or_op ? ! in1_p : in1_p,
5177 low1, high1))))
5178 {
5179 if (strict_overflow_p)
5180 fold_overflow_warning (warnmsg,
5181 WARN_STRICT_OVERFLOW_COMPARISON);
5182 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5183 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5184 type, lhs, rhs);
5185 }
5186 }
5187 }
5188
5189 return 0;
5190 }
5191 \f
5192 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5193 bit value. Arrange things so the extra bits will be set to zero if and
5194 only if C is signed-extended to its full width. If MASK is nonzero,
5195 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5196
5197 static tree
5198 unextend (tree c, int p, int unsignedp, tree mask)
5199 {
5200 tree type = TREE_TYPE (c);
5201 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5202 tree temp;
5203
5204 if (p == modesize || unsignedp)
5205 return c;
5206
5207 /* We work by getting just the sign bit into the low-order bit, then
5208 into the high-order bit, then sign-extend. We then XOR that value
5209 with C. */
5210 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5211
5212 /* We must use a signed type in order to get an arithmetic right shift.
5213 However, we must also avoid introducing accidental overflows, so that
5214 a subsequent call to integer_zerop will work. Hence we must
5215 do the type conversion here. At this point, the constant is either
5216 zero or one, and the conversion to a signed type can never overflow.
5217 We could get an overflow if this conversion is done anywhere else. */
5218 if (TYPE_UNSIGNED (type))
5219 temp = fold_convert (signed_type_for (type), temp);
5220
5221 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5222 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5223 if (mask != 0)
5224 temp = const_binop (BIT_AND_EXPR, temp,
5225 fold_convert (TREE_TYPE (c), mask));
5226 /* If necessary, convert the type back to match the type of C. */
5227 if (TYPE_UNSIGNED (type))
5228 temp = fold_convert (type, temp);
5229
5230 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5231 }
5232 \f
5233 /* For an expression that has the form
5234 (A && B) || ~B
5235 or
5236 (A || B) && ~B,
5237 we can drop one of the inner expressions and simplify to
5238 A || ~B
5239 or
5240 A && ~B
5241 LOC is the location of the resulting expression. OP is the inner
5242 logical operation; the left-hand side in the examples above, while CMPOP
5243 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5244 removing a condition that guards another, as in
5245 (A != NULL && A->...) || A == NULL
5246 which we must not transform. If RHS_ONLY is true, only eliminate the
5247 right-most operand of the inner logical operation. */
5248
5249 static tree
5250 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5251 bool rhs_only)
5252 {
5253 tree type = TREE_TYPE (cmpop);
5254 enum tree_code code = TREE_CODE (cmpop);
5255 enum tree_code truthop_code = TREE_CODE (op);
5256 tree lhs = TREE_OPERAND (op, 0);
5257 tree rhs = TREE_OPERAND (op, 1);
5258 tree orig_lhs = lhs, orig_rhs = rhs;
5259 enum tree_code rhs_code = TREE_CODE (rhs);
5260 enum tree_code lhs_code = TREE_CODE (lhs);
5261 enum tree_code inv_code;
5262
5263 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5264 return NULL_TREE;
5265
5266 if (TREE_CODE_CLASS (code) != tcc_comparison)
5267 return NULL_TREE;
5268
5269 if (rhs_code == truthop_code)
5270 {
5271 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5272 if (newrhs != NULL_TREE)
5273 {
5274 rhs = newrhs;
5275 rhs_code = TREE_CODE (rhs);
5276 }
5277 }
5278 if (lhs_code == truthop_code && !rhs_only)
5279 {
5280 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5281 if (newlhs != NULL_TREE)
5282 {
5283 lhs = newlhs;
5284 lhs_code = TREE_CODE (lhs);
5285 }
5286 }
5287
5288 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5289 if (inv_code == rhs_code
5290 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5291 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5292 return lhs;
5293 if (!rhs_only && inv_code == lhs_code
5294 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5295 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5296 return rhs;
5297 if (rhs != orig_rhs || lhs != orig_lhs)
5298 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5299 lhs, rhs);
5300 return NULL_TREE;
5301 }
5302
5303 /* Find ways of folding logical expressions of LHS and RHS:
5304 Try to merge two comparisons to the same innermost item.
5305 Look for range tests like "ch >= '0' && ch <= '9'".
5306 Look for combinations of simple terms on machines with expensive branches
5307 and evaluate the RHS unconditionally.
5308
5309 For example, if we have p->a == 2 && p->b == 4 and we can make an
5310 object large enough to span both A and B, we can do this with a comparison
5311 against the object ANDed with the a mask.
5312
5313 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5314 operations to do this with one comparison.
5315
5316 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5317 function and the one above.
5318
5319 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5320 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5321
5322 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5323 two operands.
5324
5325 We return the simplified tree or 0 if no optimization is possible. */
5326
5327 static tree
5328 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5329 tree lhs, tree rhs)
5330 {
5331 /* If this is the "or" of two comparisons, we can do something if
5332 the comparisons are NE_EXPR. If this is the "and", we can do something
5333 if the comparisons are EQ_EXPR. I.e.,
5334 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5335
5336 WANTED_CODE is this operation code. For single bit fields, we can
5337 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5338 comparison for one-bit fields. */
5339
5340 enum tree_code wanted_code;
5341 enum tree_code lcode, rcode;
5342 tree ll_arg, lr_arg, rl_arg, rr_arg;
5343 tree ll_inner, lr_inner, rl_inner, rr_inner;
5344 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5345 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5346 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5347 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5348 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5349 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5350 machine_mode lnmode, rnmode;
5351 tree ll_mask, lr_mask, rl_mask, rr_mask;
5352 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5353 tree l_const, r_const;
5354 tree lntype, rntype, result;
5355 HOST_WIDE_INT first_bit, end_bit;
5356 int volatilep;
5357
5358 /* Start by getting the comparison codes. Fail if anything is volatile.
5359 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5360 it were surrounded with a NE_EXPR. */
5361
5362 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5363 return 0;
5364
5365 lcode = TREE_CODE (lhs);
5366 rcode = TREE_CODE (rhs);
5367
5368 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5369 {
5370 lhs = build2 (NE_EXPR, truth_type, lhs,
5371 build_int_cst (TREE_TYPE (lhs), 0));
5372 lcode = NE_EXPR;
5373 }
5374
5375 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5376 {
5377 rhs = build2 (NE_EXPR, truth_type, rhs,
5378 build_int_cst (TREE_TYPE (rhs), 0));
5379 rcode = NE_EXPR;
5380 }
5381
5382 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5383 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5384 return 0;
5385
5386 ll_arg = TREE_OPERAND (lhs, 0);
5387 lr_arg = TREE_OPERAND (lhs, 1);
5388 rl_arg = TREE_OPERAND (rhs, 0);
5389 rr_arg = TREE_OPERAND (rhs, 1);
5390
5391 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5392 if (simple_operand_p (ll_arg)
5393 && simple_operand_p (lr_arg))
5394 {
5395 if (operand_equal_p (ll_arg, rl_arg, 0)
5396 && operand_equal_p (lr_arg, rr_arg, 0))
5397 {
5398 result = combine_comparisons (loc, code, lcode, rcode,
5399 truth_type, ll_arg, lr_arg);
5400 if (result)
5401 return result;
5402 }
5403 else if (operand_equal_p (ll_arg, rr_arg, 0)
5404 && operand_equal_p (lr_arg, rl_arg, 0))
5405 {
5406 result = combine_comparisons (loc, code, lcode,
5407 swap_tree_comparison (rcode),
5408 truth_type, ll_arg, lr_arg);
5409 if (result)
5410 return result;
5411 }
5412 }
5413
5414 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5415 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5416
5417 /* If the RHS can be evaluated unconditionally and its operands are
5418 simple, it wins to evaluate the RHS unconditionally on machines
5419 with expensive branches. In this case, this isn't a comparison
5420 that can be merged. */
5421
5422 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5423 false) >= 2
5424 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5425 && simple_operand_p (rl_arg)
5426 && simple_operand_p (rr_arg))
5427 {
5428 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5429 if (code == TRUTH_OR_EXPR
5430 && lcode == NE_EXPR && integer_zerop (lr_arg)
5431 && rcode == NE_EXPR && integer_zerop (rr_arg)
5432 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5433 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5434 return build2_loc (loc, NE_EXPR, truth_type,
5435 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5436 ll_arg, rl_arg),
5437 build_int_cst (TREE_TYPE (ll_arg), 0));
5438
5439 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5440 if (code == TRUTH_AND_EXPR
5441 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5442 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5443 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5444 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5445 return build2_loc (loc, EQ_EXPR, truth_type,
5446 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5447 ll_arg, rl_arg),
5448 build_int_cst (TREE_TYPE (ll_arg), 0));
5449 }
5450
5451 /* See if the comparisons can be merged. Then get all the parameters for
5452 each side. */
5453
5454 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5455 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5456 return 0;
5457
5458 volatilep = 0;
5459 ll_inner = decode_field_reference (loc, ll_arg,
5460 &ll_bitsize, &ll_bitpos, &ll_mode,
5461 &ll_unsignedp, &volatilep, &ll_mask,
5462 &ll_and_mask);
5463 lr_inner = decode_field_reference (loc, lr_arg,
5464 &lr_bitsize, &lr_bitpos, &lr_mode,
5465 &lr_unsignedp, &volatilep, &lr_mask,
5466 &lr_and_mask);
5467 rl_inner = decode_field_reference (loc, rl_arg,
5468 &rl_bitsize, &rl_bitpos, &rl_mode,
5469 &rl_unsignedp, &volatilep, &rl_mask,
5470 &rl_and_mask);
5471 rr_inner = decode_field_reference (loc, rr_arg,
5472 &rr_bitsize, &rr_bitpos, &rr_mode,
5473 &rr_unsignedp, &volatilep, &rr_mask,
5474 &rr_and_mask);
5475
5476 /* It must be true that the inner operation on the lhs of each
5477 comparison must be the same if we are to be able to do anything.
5478 Then see if we have constants. If not, the same must be true for
5479 the rhs's. */
5480 if (volatilep || ll_inner == 0 || rl_inner == 0
5481 || ! operand_equal_p (ll_inner, rl_inner, 0))
5482 return 0;
5483
5484 if (TREE_CODE (lr_arg) == INTEGER_CST
5485 && TREE_CODE (rr_arg) == INTEGER_CST)
5486 l_const = lr_arg, r_const = rr_arg;
5487 else if (lr_inner == 0 || rr_inner == 0
5488 || ! operand_equal_p (lr_inner, rr_inner, 0))
5489 return 0;
5490 else
5491 l_const = r_const = 0;
5492
5493 /* If either comparison code is not correct for our logical operation,
5494 fail. However, we can convert a one-bit comparison against zero into
5495 the opposite comparison against that bit being set in the field. */
5496
5497 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5498 if (lcode != wanted_code)
5499 {
5500 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5501 {
5502 /* Make the left operand unsigned, since we are only interested
5503 in the value of one bit. Otherwise we are doing the wrong
5504 thing below. */
5505 ll_unsignedp = 1;
5506 l_const = ll_mask;
5507 }
5508 else
5509 return 0;
5510 }
5511
5512 /* This is analogous to the code for l_const above. */
5513 if (rcode != wanted_code)
5514 {
5515 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5516 {
5517 rl_unsignedp = 1;
5518 r_const = rl_mask;
5519 }
5520 else
5521 return 0;
5522 }
5523
5524 /* See if we can find a mode that contains both fields being compared on
5525 the left. If we can't, fail. Otherwise, update all constants and masks
5526 to be relative to a field of that size. */
5527 first_bit = MIN (ll_bitpos, rl_bitpos);
5528 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5529 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5530 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5531 volatilep);
5532 if (lnmode == VOIDmode)
5533 return 0;
5534
5535 lnbitsize = GET_MODE_BITSIZE (lnmode);
5536 lnbitpos = first_bit & ~ (lnbitsize - 1);
5537 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5538 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5539
5540 if (BYTES_BIG_ENDIAN)
5541 {
5542 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5543 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5544 }
5545
5546 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5547 size_int (xll_bitpos));
5548 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5549 size_int (xrl_bitpos));
5550
5551 if (l_const)
5552 {
5553 l_const = fold_convert_loc (loc, lntype, l_const);
5554 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5555 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5556 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5557 fold_build1_loc (loc, BIT_NOT_EXPR,
5558 lntype, ll_mask))))
5559 {
5560 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5561
5562 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5563 }
5564 }
5565 if (r_const)
5566 {
5567 r_const = fold_convert_loc (loc, lntype, r_const);
5568 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5569 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5570 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5571 fold_build1_loc (loc, BIT_NOT_EXPR,
5572 lntype, rl_mask))))
5573 {
5574 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5575
5576 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5577 }
5578 }
5579
5580 /* If the right sides are not constant, do the same for it. Also,
5581 disallow this optimization if a size or signedness mismatch occurs
5582 between the left and right sides. */
5583 if (l_const == 0)
5584 {
5585 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5586 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5587 /* Make sure the two fields on the right
5588 correspond to the left without being swapped. */
5589 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5590 return 0;
5591
5592 first_bit = MIN (lr_bitpos, rr_bitpos);
5593 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5594 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5595 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5596 volatilep);
5597 if (rnmode == VOIDmode)
5598 return 0;
5599
5600 rnbitsize = GET_MODE_BITSIZE (rnmode);
5601 rnbitpos = first_bit & ~ (rnbitsize - 1);
5602 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5603 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5604
5605 if (BYTES_BIG_ENDIAN)
5606 {
5607 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5608 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5609 }
5610
5611 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5612 rntype, lr_mask),
5613 size_int (xlr_bitpos));
5614 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5615 rntype, rr_mask),
5616 size_int (xrr_bitpos));
5617
5618 /* Make a mask that corresponds to both fields being compared.
5619 Do this for both items being compared. If the operands are the
5620 same size and the bits being compared are in the same position
5621 then we can do this by masking both and comparing the masked
5622 results. */
5623 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5624 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5625 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5626 {
5627 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5628 ll_unsignedp || rl_unsignedp);
5629 if (! all_ones_mask_p (ll_mask, lnbitsize))
5630 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5631
5632 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5633 lr_unsignedp || rr_unsignedp);
5634 if (! all_ones_mask_p (lr_mask, rnbitsize))
5635 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5636
5637 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5638 }
5639
5640 /* There is still another way we can do something: If both pairs of
5641 fields being compared are adjacent, we may be able to make a wider
5642 field containing them both.
5643
5644 Note that we still must mask the lhs/rhs expressions. Furthermore,
5645 the mask must be shifted to account for the shift done by
5646 make_bit_field_ref. */
5647 if ((ll_bitsize + ll_bitpos == rl_bitpos
5648 && lr_bitsize + lr_bitpos == rr_bitpos)
5649 || (ll_bitpos == rl_bitpos + rl_bitsize
5650 && lr_bitpos == rr_bitpos + rr_bitsize))
5651 {
5652 tree type;
5653
5654 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5655 ll_bitsize + rl_bitsize,
5656 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5657 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5658 lr_bitsize + rr_bitsize,
5659 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5660
5661 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5662 size_int (MIN (xll_bitpos, xrl_bitpos)));
5663 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5664 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5665
5666 /* Convert to the smaller type before masking out unwanted bits. */
5667 type = lntype;
5668 if (lntype != rntype)
5669 {
5670 if (lnbitsize > rnbitsize)
5671 {
5672 lhs = fold_convert_loc (loc, rntype, lhs);
5673 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5674 type = rntype;
5675 }
5676 else if (lnbitsize < rnbitsize)
5677 {
5678 rhs = fold_convert_loc (loc, lntype, rhs);
5679 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5680 type = lntype;
5681 }
5682 }
5683
5684 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5685 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5686
5687 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5688 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5689
5690 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5691 }
5692
5693 return 0;
5694 }
5695
5696 /* Handle the case of comparisons with constants. If there is something in
5697 common between the masks, those bits of the constants must be the same.
5698 If not, the condition is always false. Test for this to avoid generating
5699 incorrect code below. */
5700 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5701 if (! integer_zerop (result)
5702 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5703 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5704 {
5705 if (wanted_code == NE_EXPR)
5706 {
5707 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5708 return constant_boolean_node (true, truth_type);
5709 }
5710 else
5711 {
5712 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5713 return constant_boolean_node (false, truth_type);
5714 }
5715 }
5716
5717 /* Construct the expression we will return. First get the component
5718 reference we will make. Unless the mask is all ones the width of
5719 that field, perform the mask operation. Then compare with the
5720 merged constant. */
5721 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5722 ll_unsignedp || rl_unsignedp);
5723
5724 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5725 if (! all_ones_mask_p (ll_mask, lnbitsize))
5726 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5727
5728 return build2_loc (loc, wanted_code, truth_type, result,
5729 const_binop (BIT_IOR_EXPR, l_const, r_const));
5730 }
5731 \f
5732 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5733 constant. */
5734
5735 static tree
5736 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5737 tree op0, tree op1)
5738 {
5739 tree arg0 = op0;
5740 enum tree_code op_code;
5741 tree comp_const;
5742 tree minmax_const;
5743 int consts_equal, consts_lt;
5744 tree inner;
5745
5746 STRIP_SIGN_NOPS (arg0);
5747
5748 op_code = TREE_CODE (arg0);
5749 minmax_const = TREE_OPERAND (arg0, 1);
5750 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5751 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5752 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5753 inner = TREE_OPERAND (arg0, 0);
5754
5755 /* If something does not permit us to optimize, return the original tree. */
5756 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5757 || TREE_CODE (comp_const) != INTEGER_CST
5758 || TREE_OVERFLOW (comp_const)
5759 || TREE_CODE (minmax_const) != INTEGER_CST
5760 || TREE_OVERFLOW (minmax_const))
5761 return NULL_TREE;
5762
5763 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5764 and GT_EXPR, doing the rest with recursive calls using logical
5765 simplifications. */
5766 switch (code)
5767 {
5768 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5769 {
5770 tree tem
5771 = optimize_minmax_comparison (loc,
5772 invert_tree_comparison (code, false),
5773 type, op0, op1);
5774 if (tem)
5775 return invert_truthvalue_loc (loc, tem);
5776 return NULL_TREE;
5777 }
5778
5779 case GE_EXPR:
5780 return
5781 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5782 optimize_minmax_comparison
5783 (loc, EQ_EXPR, type, arg0, comp_const),
5784 optimize_minmax_comparison
5785 (loc, GT_EXPR, type, arg0, comp_const));
5786
5787 case EQ_EXPR:
5788 if (op_code == MAX_EXPR && consts_equal)
5789 /* MAX (X, 0) == 0 -> X <= 0 */
5790 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5791
5792 else if (op_code == MAX_EXPR && consts_lt)
5793 /* MAX (X, 0) == 5 -> X == 5 */
5794 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5795
5796 else if (op_code == MAX_EXPR)
5797 /* MAX (X, 0) == -1 -> false */
5798 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5799
5800 else if (consts_equal)
5801 /* MIN (X, 0) == 0 -> X >= 0 */
5802 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5803
5804 else if (consts_lt)
5805 /* MIN (X, 0) == 5 -> false */
5806 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5807
5808 else
5809 /* MIN (X, 0) == -1 -> X == -1 */
5810 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5811
5812 case GT_EXPR:
5813 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5814 /* MAX (X, 0) > 0 -> X > 0
5815 MAX (X, 0) > 5 -> X > 5 */
5816 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5817
5818 else if (op_code == MAX_EXPR)
5819 /* MAX (X, 0) > -1 -> true */
5820 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5821
5822 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5823 /* MIN (X, 0) > 0 -> false
5824 MIN (X, 0) > 5 -> false */
5825 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5826
5827 else
5828 /* MIN (X, 0) > -1 -> X > -1 */
5829 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5830
5831 default:
5832 return NULL_TREE;
5833 }
5834 }
5835 \f
5836 /* T is an integer expression that is being multiplied, divided, or taken a
5837 modulus (CODE says which and what kind of divide or modulus) by a
5838 constant C. See if we can eliminate that operation by folding it with
5839 other operations already in T. WIDE_TYPE, if non-null, is a type that
5840 should be used for the computation if wider than our type.
5841
5842 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5843 (X * 2) + (Y * 4). We must, however, be assured that either the original
5844 expression would not overflow or that overflow is undefined for the type
5845 in the language in question.
5846
5847 If we return a non-null expression, it is an equivalent form of the
5848 original computation, but need not be in the original type.
5849
5850 We set *STRICT_OVERFLOW_P to true if the return values depends on
5851 signed overflow being undefined. Otherwise we do not change
5852 *STRICT_OVERFLOW_P. */
5853
5854 static tree
5855 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5856 bool *strict_overflow_p)
5857 {
5858 /* To avoid exponential search depth, refuse to allow recursion past
5859 three levels. Beyond that (1) it's highly unlikely that we'll find
5860 something interesting and (2) we've probably processed it before
5861 when we built the inner expression. */
5862
5863 static int depth;
5864 tree ret;
5865
5866 if (depth > 3)
5867 return NULL;
5868
5869 depth++;
5870 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5871 depth--;
5872
5873 return ret;
5874 }
5875
5876 static tree
5877 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5878 bool *strict_overflow_p)
5879 {
5880 tree type = TREE_TYPE (t);
5881 enum tree_code tcode = TREE_CODE (t);
5882 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5883 > GET_MODE_SIZE (TYPE_MODE (type)))
5884 ? wide_type : type);
5885 tree t1, t2;
5886 int same_p = tcode == code;
5887 tree op0 = NULL_TREE, op1 = NULL_TREE;
5888 bool sub_strict_overflow_p;
5889
5890 /* Don't deal with constants of zero here; they confuse the code below. */
5891 if (integer_zerop (c))
5892 return NULL_TREE;
5893
5894 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5895 op0 = TREE_OPERAND (t, 0);
5896
5897 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5898 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5899
5900 /* Note that we need not handle conditional operations here since fold
5901 already handles those cases. So just do arithmetic here. */
5902 switch (tcode)
5903 {
5904 case INTEGER_CST:
5905 /* For a constant, we can always simplify if we are a multiply
5906 or (for divide and modulus) if it is a multiple of our constant. */
5907 if (code == MULT_EXPR
5908 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5909 return const_binop (code, fold_convert (ctype, t),
5910 fold_convert (ctype, c));
5911 break;
5912
5913 CASE_CONVERT: case NON_LVALUE_EXPR:
5914 /* If op0 is an expression ... */
5915 if ((COMPARISON_CLASS_P (op0)
5916 || UNARY_CLASS_P (op0)
5917 || BINARY_CLASS_P (op0)
5918 || VL_EXP_CLASS_P (op0)
5919 || EXPRESSION_CLASS_P (op0))
5920 /* ... and has wrapping overflow, and its type is smaller
5921 than ctype, then we cannot pass through as widening. */
5922 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5923 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5924 && (TYPE_PRECISION (ctype)
5925 > TYPE_PRECISION (TREE_TYPE (op0))))
5926 /* ... or this is a truncation (t is narrower than op0),
5927 then we cannot pass through this narrowing. */
5928 || (TYPE_PRECISION (type)
5929 < TYPE_PRECISION (TREE_TYPE (op0)))
5930 /* ... or signedness changes for division or modulus,
5931 then we cannot pass through this conversion. */
5932 || (code != MULT_EXPR
5933 && (TYPE_UNSIGNED (ctype)
5934 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5935 /* ... or has undefined overflow while the converted to
5936 type has not, we cannot do the operation in the inner type
5937 as that would introduce undefined overflow. */
5938 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5939 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
5940 && !TYPE_OVERFLOW_UNDEFINED (type))))
5941 break;
5942
5943 /* Pass the constant down and see if we can make a simplification. If
5944 we can, replace this expression with the inner simplification for
5945 possible later conversion to our or some other type. */
5946 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5947 && TREE_CODE (t2) == INTEGER_CST
5948 && !TREE_OVERFLOW (t2)
5949 && (0 != (t1 = extract_muldiv (op0, t2, code,
5950 code == MULT_EXPR
5951 ? ctype : NULL_TREE,
5952 strict_overflow_p))))
5953 return t1;
5954 break;
5955
5956 case ABS_EXPR:
5957 /* If widening the type changes it from signed to unsigned, then we
5958 must avoid building ABS_EXPR itself as unsigned. */
5959 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5960 {
5961 tree cstype = (*signed_type_for) (ctype);
5962 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5963 != 0)
5964 {
5965 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5966 return fold_convert (ctype, t1);
5967 }
5968 break;
5969 }
5970 /* If the constant is negative, we cannot simplify this. */
5971 if (tree_int_cst_sgn (c) == -1)
5972 break;
5973 /* FALLTHROUGH */
5974 case NEGATE_EXPR:
5975 /* For division and modulus, type can't be unsigned, as e.g.
5976 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5977 For signed types, even with wrapping overflow, this is fine. */
5978 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
5979 break;
5980 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5981 != 0)
5982 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5983 break;
5984
5985 case MIN_EXPR: case MAX_EXPR:
5986 /* If widening the type changes the signedness, then we can't perform
5987 this optimization as that changes the result. */
5988 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5989 break;
5990
5991 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5992 sub_strict_overflow_p = false;
5993 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5994 &sub_strict_overflow_p)) != 0
5995 && (t2 = extract_muldiv (op1, c, code, wide_type,
5996 &sub_strict_overflow_p)) != 0)
5997 {
5998 if (tree_int_cst_sgn (c) < 0)
5999 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6000 if (sub_strict_overflow_p)
6001 *strict_overflow_p = true;
6002 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6003 fold_convert (ctype, t2));
6004 }
6005 break;
6006
6007 case LSHIFT_EXPR: case RSHIFT_EXPR:
6008 /* If the second operand is constant, this is a multiplication
6009 or floor division, by a power of two, so we can treat it that
6010 way unless the multiplier or divisor overflows. Signed
6011 left-shift overflow is implementation-defined rather than
6012 undefined in C90, so do not convert signed left shift into
6013 multiplication. */
6014 if (TREE_CODE (op1) == INTEGER_CST
6015 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6016 /* const_binop may not detect overflow correctly,
6017 so check for it explicitly here. */
6018 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6019 && 0 != (t1 = fold_convert (ctype,
6020 const_binop (LSHIFT_EXPR,
6021 size_one_node,
6022 op1)))
6023 && !TREE_OVERFLOW (t1))
6024 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6025 ? MULT_EXPR : FLOOR_DIV_EXPR,
6026 ctype,
6027 fold_convert (ctype, op0),
6028 t1),
6029 c, code, wide_type, strict_overflow_p);
6030 break;
6031
6032 case PLUS_EXPR: case MINUS_EXPR:
6033 /* See if we can eliminate the operation on both sides. If we can, we
6034 can return a new PLUS or MINUS. If we can't, the only remaining
6035 cases where we can do anything are if the second operand is a
6036 constant. */
6037 sub_strict_overflow_p = false;
6038 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6039 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6040 if (t1 != 0 && t2 != 0
6041 && (code == MULT_EXPR
6042 /* If not multiplication, we can only do this if both operands
6043 are divisible by c. */
6044 || (multiple_of_p (ctype, op0, c)
6045 && multiple_of_p (ctype, op1, c))))
6046 {
6047 if (sub_strict_overflow_p)
6048 *strict_overflow_p = true;
6049 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6050 fold_convert (ctype, t2));
6051 }
6052
6053 /* If this was a subtraction, negate OP1 and set it to be an addition.
6054 This simplifies the logic below. */
6055 if (tcode == MINUS_EXPR)
6056 {
6057 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6058 /* If OP1 was not easily negatable, the constant may be OP0. */
6059 if (TREE_CODE (op0) == INTEGER_CST)
6060 {
6061 std::swap (op0, op1);
6062 std::swap (t1, t2);
6063 }
6064 }
6065
6066 if (TREE_CODE (op1) != INTEGER_CST)
6067 break;
6068
6069 /* If either OP1 or C are negative, this optimization is not safe for
6070 some of the division and remainder types while for others we need
6071 to change the code. */
6072 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6073 {
6074 if (code == CEIL_DIV_EXPR)
6075 code = FLOOR_DIV_EXPR;
6076 else if (code == FLOOR_DIV_EXPR)
6077 code = CEIL_DIV_EXPR;
6078 else if (code != MULT_EXPR
6079 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6080 break;
6081 }
6082
6083 /* If it's a multiply or a division/modulus operation of a multiple
6084 of our constant, do the operation and verify it doesn't overflow. */
6085 if (code == MULT_EXPR
6086 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6087 {
6088 op1 = const_binop (code, fold_convert (ctype, op1),
6089 fold_convert (ctype, c));
6090 /* We allow the constant to overflow with wrapping semantics. */
6091 if (op1 == 0
6092 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6093 break;
6094 }
6095 else
6096 break;
6097
6098 /* If we have an unsigned type, we cannot widen the operation since it
6099 will change the result if the original computation overflowed. */
6100 if (TYPE_UNSIGNED (ctype) && ctype != type)
6101 break;
6102
6103 /* If we were able to eliminate our operation from the first side,
6104 apply our operation to the second side and reform the PLUS. */
6105 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6106 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6107
6108 /* The last case is if we are a multiply. In that case, we can
6109 apply the distributive law to commute the multiply and addition
6110 if the multiplication of the constants doesn't overflow
6111 and overflow is defined. With undefined overflow
6112 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6113 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6114 return fold_build2 (tcode, ctype,
6115 fold_build2 (code, ctype,
6116 fold_convert (ctype, op0),
6117 fold_convert (ctype, c)),
6118 op1);
6119
6120 break;
6121
6122 case MULT_EXPR:
6123 /* We have a special case here if we are doing something like
6124 (C * 8) % 4 since we know that's zero. */
6125 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6126 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6127 /* If the multiplication can overflow we cannot optimize this. */
6128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6129 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6130 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6131 {
6132 *strict_overflow_p = true;
6133 return omit_one_operand (type, integer_zero_node, op0);
6134 }
6135
6136 /* ... fall through ... */
6137
6138 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6139 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6140 /* If we can extract our operation from the LHS, do so and return a
6141 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6142 do something only if the second operand is a constant. */
6143 if (same_p
6144 && (t1 = extract_muldiv (op0, c, code, wide_type,
6145 strict_overflow_p)) != 0)
6146 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6147 fold_convert (ctype, op1));
6148 else if (tcode == MULT_EXPR && code == MULT_EXPR
6149 && (t1 = extract_muldiv (op1, c, code, wide_type,
6150 strict_overflow_p)) != 0)
6151 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6152 fold_convert (ctype, t1));
6153 else if (TREE_CODE (op1) != INTEGER_CST)
6154 return 0;
6155
6156 /* If these are the same operation types, we can associate them
6157 assuming no overflow. */
6158 if (tcode == code)
6159 {
6160 bool overflow_p = false;
6161 bool overflow_mul_p;
6162 signop sign = TYPE_SIGN (ctype);
6163 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6164 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6165 if (overflow_mul_p
6166 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6167 overflow_p = true;
6168 if (!overflow_p)
6169 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6170 wide_int_to_tree (ctype, mul));
6171 }
6172
6173 /* If these operations "cancel" each other, we have the main
6174 optimizations of this pass, which occur when either constant is a
6175 multiple of the other, in which case we replace this with either an
6176 operation or CODE or TCODE.
6177
6178 If we have an unsigned type, we cannot do this since it will change
6179 the result if the original computation overflowed. */
6180 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6181 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6182 || (tcode == MULT_EXPR
6183 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6184 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6185 && code != MULT_EXPR)))
6186 {
6187 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6188 {
6189 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6190 *strict_overflow_p = true;
6191 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6192 fold_convert (ctype,
6193 const_binop (TRUNC_DIV_EXPR,
6194 op1, c)));
6195 }
6196 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6197 {
6198 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6199 *strict_overflow_p = true;
6200 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6201 fold_convert (ctype,
6202 const_binop (TRUNC_DIV_EXPR,
6203 c, op1)));
6204 }
6205 }
6206 break;
6207
6208 default:
6209 break;
6210 }
6211
6212 return 0;
6213 }
6214 \f
6215 /* Return a node which has the indicated constant VALUE (either 0 or
6216 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6217 and is of the indicated TYPE. */
6218
6219 tree
6220 constant_boolean_node (bool value, tree type)
6221 {
6222 if (type == integer_type_node)
6223 return value ? integer_one_node : integer_zero_node;
6224 else if (type == boolean_type_node)
6225 return value ? boolean_true_node : boolean_false_node;
6226 else if (TREE_CODE (type) == VECTOR_TYPE)
6227 return build_vector_from_val (type,
6228 build_int_cst (TREE_TYPE (type),
6229 value ? -1 : 0));
6230 else
6231 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6232 }
6233
6234
6235 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6236 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6237 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6238 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6239 COND is the first argument to CODE; otherwise (as in the example
6240 given here), it is the second argument. TYPE is the type of the
6241 original expression. Return NULL_TREE if no simplification is
6242 possible. */
6243
6244 static tree
6245 fold_binary_op_with_conditional_arg (location_t loc,
6246 enum tree_code code,
6247 tree type, tree op0, tree op1,
6248 tree cond, tree arg, int cond_first_p)
6249 {
6250 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6251 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6252 tree test, true_value, false_value;
6253 tree lhs = NULL_TREE;
6254 tree rhs = NULL_TREE;
6255 enum tree_code cond_code = COND_EXPR;
6256
6257 if (TREE_CODE (cond) == COND_EXPR
6258 || TREE_CODE (cond) == VEC_COND_EXPR)
6259 {
6260 test = TREE_OPERAND (cond, 0);
6261 true_value = TREE_OPERAND (cond, 1);
6262 false_value = TREE_OPERAND (cond, 2);
6263 /* If this operand throws an expression, then it does not make
6264 sense to try to perform a logical or arithmetic operation
6265 involving it. */
6266 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6267 lhs = true_value;
6268 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6269 rhs = false_value;
6270 }
6271 else
6272 {
6273 tree testtype = TREE_TYPE (cond);
6274 test = cond;
6275 true_value = constant_boolean_node (true, testtype);
6276 false_value = constant_boolean_node (false, testtype);
6277 }
6278
6279 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6280 cond_code = VEC_COND_EXPR;
6281
6282 /* This transformation is only worthwhile if we don't have to wrap ARG
6283 in a SAVE_EXPR and the operation can be simplified without recursing
6284 on at least one of the branches once its pushed inside the COND_EXPR. */
6285 if (!TREE_CONSTANT (arg)
6286 && (TREE_SIDE_EFFECTS (arg)
6287 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6288 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6289 return NULL_TREE;
6290
6291 arg = fold_convert_loc (loc, arg_type, arg);
6292 if (lhs == 0)
6293 {
6294 true_value = fold_convert_loc (loc, cond_type, true_value);
6295 if (cond_first_p)
6296 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6297 else
6298 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6299 }
6300 if (rhs == 0)
6301 {
6302 false_value = fold_convert_loc (loc, cond_type, false_value);
6303 if (cond_first_p)
6304 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6305 else
6306 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6307 }
6308
6309 /* Check that we have simplified at least one of the branches. */
6310 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6311 return NULL_TREE;
6312
6313 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6314 }
6315
6316 \f
6317 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6318
6319 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6320 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6321 ADDEND is the same as X.
6322
6323 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6324 and finite. The problematic cases are when X is zero, and its mode
6325 has signed zeros. In the case of rounding towards -infinity,
6326 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6327 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6328
6329 bool
6330 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6331 {
6332 if (!real_zerop (addend))
6333 return false;
6334
6335 /* Don't allow the fold with -fsignaling-nans. */
6336 if (HONOR_SNANS (element_mode (type)))
6337 return false;
6338
6339 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6340 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6341 return true;
6342
6343 /* In a vector or complex, we would need to check the sign of all zeros. */
6344 if (TREE_CODE (addend) != REAL_CST)
6345 return false;
6346
6347 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6348 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6349 negate = !negate;
6350
6351 /* The mode has signed zeros, and we have to honor their sign.
6352 In this situation, there is only one case we can return true for.
6353 X - 0 is the same as X unless rounding towards -infinity is
6354 supported. */
6355 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6356 }
6357
6358 /* Subroutine of fold() that optimizes comparisons of a division by
6359 a nonzero integer constant against an integer constant, i.e.
6360 X/C1 op C2.
6361
6362 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6363 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6364 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6365
6366 The function returns the constant folded tree if a simplification
6367 can be made, and NULL_TREE otherwise. */
6368
6369 static tree
6370 fold_div_compare (location_t loc,
6371 enum tree_code code, tree type, tree arg0, tree arg1)
6372 {
6373 tree prod, tmp, hi, lo;
6374 tree arg00 = TREE_OPERAND (arg0, 0);
6375 tree arg01 = TREE_OPERAND (arg0, 1);
6376 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6377 bool neg_overflow = false;
6378 bool overflow;
6379
6380 /* We have to do this the hard way to detect unsigned overflow.
6381 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6382 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6383 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6384 neg_overflow = false;
6385
6386 if (sign == UNSIGNED)
6387 {
6388 tmp = int_const_binop (MINUS_EXPR, arg01,
6389 build_int_cst (TREE_TYPE (arg01), 1));
6390 lo = prod;
6391
6392 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6393 val = wi::add (prod, tmp, sign, &overflow);
6394 hi = force_fit_type (TREE_TYPE (arg00), val,
6395 -1, overflow | TREE_OVERFLOW (prod));
6396 }
6397 else if (tree_int_cst_sgn (arg01) >= 0)
6398 {
6399 tmp = int_const_binop (MINUS_EXPR, arg01,
6400 build_int_cst (TREE_TYPE (arg01), 1));
6401 switch (tree_int_cst_sgn (arg1))
6402 {
6403 case -1:
6404 neg_overflow = true;
6405 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6406 hi = prod;
6407 break;
6408
6409 case 0:
6410 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6411 hi = tmp;
6412 break;
6413
6414 case 1:
6415 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6416 lo = prod;
6417 break;
6418
6419 default:
6420 gcc_unreachable ();
6421 }
6422 }
6423 else
6424 {
6425 /* A negative divisor reverses the relational operators. */
6426 code = swap_tree_comparison (code);
6427
6428 tmp = int_const_binop (PLUS_EXPR, arg01,
6429 build_int_cst (TREE_TYPE (arg01), 1));
6430 switch (tree_int_cst_sgn (arg1))
6431 {
6432 case -1:
6433 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6434 lo = prod;
6435 break;
6436
6437 case 0:
6438 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6439 lo = tmp;
6440 break;
6441
6442 case 1:
6443 neg_overflow = true;
6444 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6445 hi = prod;
6446 break;
6447
6448 default:
6449 gcc_unreachable ();
6450 }
6451 }
6452
6453 switch (code)
6454 {
6455 case EQ_EXPR:
6456 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6457 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6458 if (TREE_OVERFLOW (hi))
6459 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6460 if (TREE_OVERFLOW (lo))
6461 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6462 return build_range_check (loc, type, arg00, 1, lo, hi);
6463
6464 case NE_EXPR:
6465 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6466 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6467 if (TREE_OVERFLOW (hi))
6468 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6469 if (TREE_OVERFLOW (lo))
6470 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6471 return build_range_check (loc, type, arg00, 0, lo, hi);
6472
6473 case LT_EXPR:
6474 if (TREE_OVERFLOW (lo))
6475 {
6476 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6477 return omit_one_operand_loc (loc, type, tmp, arg00);
6478 }
6479 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6480
6481 case LE_EXPR:
6482 if (TREE_OVERFLOW (hi))
6483 {
6484 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6485 return omit_one_operand_loc (loc, type, tmp, arg00);
6486 }
6487 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6488
6489 case GT_EXPR:
6490 if (TREE_OVERFLOW (hi))
6491 {
6492 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6493 return omit_one_operand_loc (loc, type, tmp, arg00);
6494 }
6495 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6496
6497 case GE_EXPR:
6498 if (TREE_OVERFLOW (lo))
6499 {
6500 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6501 return omit_one_operand_loc (loc, type, tmp, arg00);
6502 }
6503 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6504
6505 default:
6506 break;
6507 }
6508
6509 return NULL_TREE;
6510 }
6511
6512
6513 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6514 equality/inequality test, then return a simplified form of the test
6515 using a sign testing. Otherwise return NULL. TYPE is the desired
6516 result type. */
6517
6518 static tree
6519 fold_single_bit_test_into_sign_test (location_t loc,
6520 enum tree_code code, tree arg0, tree arg1,
6521 tree result_type)
6522 {
6523 /* If this is testing a single bit, we can optimize the test. */
6524 if ((code == NE_EXPR || code == EQ_EXPR)
6525 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6526 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6527 {
6528 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6529 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6530 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6531
6532 if (arg00 != NULL_TREE
6533 /* This is only a win if casting to a signed type is cheap,
6534 i.e. when arg00's type is not a partial mode. */
6535 && TYPE_PRECISION (TREE_TYPE (arg00))
6536 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6537 {
6538 tree stype = signed_type_for (TREE_TYPE (arg00));
6539 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6540 result_type,
6541 fold_convert_loc (loc, stype, arg00),
6542 build_int_cst (stype, 0));
6543 }
6544 }
6545
6546 return NULL_TREE;
6547 }
6548
6549 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6550 equality/inequality test, then return a simplified form of
6551 the test using shifts and logical operations. Otherwise return
6552 NULL. TYPE is the desired result type. */
6553
6554 tree
6555 fold_single_bit_test (location_t loc, enum tree_code code,
6556 tree arg0, tree arg1, tree result_type)
6557 {
6558 /* If this is testing a single bit, we can optimize the test. */
6559 if ((code == NE_EXPR || code == EQ_EXPR)
6560 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6561 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6562 {
6563 tree inner = TREE_OPERAND (arg0, 0);
6564 tree type = TREE_TYPE (arg0);
6565 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6566 machine_mode operand_mode = TYPE_MODE (type);
6567 int ops_unsigned;
6568 tree signed_type, unsigned_type, intermediate_type;
6569 tree tem, one;
6570
6571 /* First, see if we can fold the single bit test into a sign-bit
6572 test. */
6573 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6574 result_type);
6575 if (tem)
6576 return tem;
6577
6578 /* Otherwise we have (A & C) != 0 where C is a single bit,
6579 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6580 Similarly for (A & C) == 0. */
6581
6582 /* If INNER is a right shift of a constant and it plus BITNUM does
6583 not overflow, adjust BITNUM and INNER. */
6584 if (TREE_CODE (inner) == RSHIFT_EXPR
6585 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6586 && bitnum < TYPE_PRECISION (type)
6587 && wi::ltu_p (TREE_OPERAND (inner, 1),
6588 TYPE_PRECISION (type) - bitnum))
6589 {
6590 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6591 inner = TREE_OPERAND (inner, 0);
6592 }
6593
6594 /* If we are going to be able to omit the AND below, we must do our
6595 operations as unsigned. If we must use the AND, we have a choice.
6596 Normally unsigned is faster, but for some machines signed is. */
6597 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6598 && !flag_syntax_only) ? 0 : 1;
6599
6600 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6601 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6602 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6603 inner = fold_convert_loc (loc, intermediate_type, inner);
6604
6605 if (bitnum != 0)
6606 inner = build2 (RSHIFT_EXPR, intermediate_type,
6607 inner, size_int (bitnum));
6608
6609 one = build_int_cst (intermediate_type, 1);
6610
6611 if (code == EQ_EXPR)
6612 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6613
6614 /* Put the AND last so it can combine with more things. */
6615 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6616
6617 /* Make sure to return the proper type. */
6618 inner = fold_convert_loc (loc, result_type, inner);
6619
6620 return inner;
6621 }
6622 return NULL_TREE;
6623 }
6624
6625 /* Check whether we are allowed to reorder operands arg0 and arg1,
6626 such that the evaluation of arg1 occurs before arg0. */
6627
6628 static bool
6629 reorder_operands_p (const_tree arg0, const_tree arg1)
6630 {
6631 if (! flag_evaluation_order)
6632 return true;
6633 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6634 return true;
6635 return ! TREE_SIDE_EFFECTS (arg0)
6636 && ! TREE_SIDE_EFFECTS (arg1);
6637 }
6638
6639 /* Test whether it is preferable two swap two operands, ARG0 and
6640 ARG1, for example because ARG0 is an integer constant and ARG1
6641 isn't. If REORDER is true, only recommend swapping if we can
6642 evaluate the operands in reverse order. */
6643
6644 bool
6645 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6646 {
6647 if (CONSTANT_CLASS_P (arg1))
6648 return 0;
6649 if (CONSTANT_CLASS_P (arg0))
6650 return 1;
6651
6652 STRIP_NOPS (arg0);
6653 STRIP_NOPS (arg1);
6654
6655 if (TREE_CONSTANT (arg1))
6656 return 0;
6657 if (TREE_CONSTANT (arg0))
6658 return 1;
6659
6660 if (reorder && flag_evaluation_order
6661 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6662 return 0;
6663
6664 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6665 for commutative and comparison operators. Ensuring a canonical
6666 form allows the optimizers to find additional redundancies without
6667 having to explicitly check for both orderings. */
6668 if (TREE_CODE (arg0) == SSA_NAME
6669 && TREE_CODE (arg1) == SSA_NAME
6670 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6671 return 1;
6672
6673 /* Put SSA_NAMEs last. */
6674 if (TREE_CODE (arg1) == SSA_NAME)
6675 return 0;
6676 if (TREE_CODE (arg0) == SSA_NAME)
6677 return 1;
6678
6679 /* Put variables last. */
6680 if (DECL_P (arg1))
6681 return 0;
6682 if (DECL_P (arg0))
6683 return 1;
6684
6685 return 0;
6686 }
6687
6688
6689 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6690 means A >= Y && A != MAX, but in this case we know that
6691 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6692
6693 static tree
6694 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6695 {
6696 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6697
6698 if (TREE_CODE (bound) == LT_EXPR)
6699 a = TREE_OPERAND (bound, 0);
6700 else if (TREE_CODE (bound) == GT_EXPR)
6701 a = TREE_OPERAND (bound, 1);
6702 else
6703 return NULL_TREE;
6704
6705 typea = TREE_TYPE (a);
6706 if (!INTEGRAL_TYPE_P (typea)
6707 && !POINTER_TYPE_P (typea))
6708 return NULL_TREE;
6709
6710 if (TREE_CODE (ineq) == LT_EXPR)
6711 {
6712 a1 = TREE_OPERAND (ineq, 1);
6713 y = TREE_OPERAND (ineq, 0);
6714 }
6715 else if (TREE_CODE (ineq) == GT_EXPR)
6716 {
6717 a1 = TREE_OPERAND (ineq, 0);
6718 y = TREE_OPERAND (ineq, 1);
6719 }
6720 else
6721 return NULL_TREE;
6722
6723 if (TREE_TYPE (a1) != typea)
6724 return NULL_TREE;
6725
6726 if (POINTER_TYPE_P (typea))
6727 {
6728 /* Convert the pointer types into integer before taking the difference. */
6729 tree ta = fold_convert_loc (loc, ssizetype, a);
6730 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6731 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6732 }
6733 else
6734 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6735
6736 if (!diff || !integer_onep (diff))
6737 return NULL_TREE;
6738
6739 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6740 }
6741
6742 /* Fold a sum or difference of at least one multiplication.
6743 Returns the folded tree or NULL if no simplification could be made. */
6744
6745 static tree
6746 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6747 tree arg0, tree arg1)
6748 {
6749 tree arg00, arg01, arg10, arg11;
6750 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6751
6752 /* (A * C) +- (B * C) -> (A+-B) * C.
6753 (A * C) +- A -> A * (C+-1).
6754 We are most concerned about the case where C is a constant,
6755 but other combinations show up during loop reduction. Since
6756 it is not difficult, try all four possibilities. */
6757
6758 if (TREE_CODE (arg0) == MULT_EXPR)
6759 {
6760 arg00 = TREE_OPERAND (arg0, 0);
6761 arg01 = TREE_OPERAND (arg0, 1);
6762 }
6763 else if (TREE_CODE (arg0) == INTEGER_CST)
6764 {
6765 arg00 = build_one_cst (type);
6766 arg01 = arg0;
6767 }
6768 else
6769 {
6770 /* We cannot generate constant 1 for fract. */
6771 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6772 return NULL_TREE;
6773 arg00 = arg0;
6774 arg01 = build_one_cst (type);
6775 }
6776 if (TREE_CODE (arg1) == MULT_EXPR)
6777 {
6778 arg10 = TREE_OPERAND (arg1, 0);
6779 arg11 = TREE_OPERAND (arg1, 1);
6780 }
6781 else if (TREE_CODE (arg1) == INTEGER_CST)
6782 {
6783 arg10 = build_one_cst (type);
6784 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6785 the purpose of this canonicalization. */
6786 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6787 && negate_expr_p (arg1)
6788 && code == PLUS_EXPR)
6789 {
6790 arg11 = negate_expr (arg1);
6791 code = MINUS_EXPR;
6792 }
6793 else
6794 arg11 = arg1;
6795 }
6796 else
6797 {
6798 /* We cannot generate constant 1 for fract. */
6799 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6800 return NULL_TREE;
6801 arg10 = arg1;
6802 arg11 = build_one_cst (type);
6803 }
6804 same = NULL_TREE;
6805
6806 if (operand_equal_p (arg01, arg11, 0))
6807 same = arg01, alt0 = arg00, alt1 = arg10;
6808 else if (operand_equal_p (arg00, arg10, 0))
6809 same = arg00, alt0 = arg01, alt1 = arg11;
6810 else if (operand_equal_p (arg00, arg11, 0))
6811 same = arg00, alt0 = arg01, alt1 = arg10;
6812 else if (operand_equal_p (arg01, arg10, 0))
6813 same = arg01, alt0 = arg00, alt1 = arg11;
6814
6815 /* No identical multiplicands; see if we can find a common
6816 power-of-two factor in non-power-of-two multiplies. This
6817 can help in multi-dimensional array access. */
6818 else if (tree_fits_shwi_p (arg01)
6819 && tree_fits_shwi_p (arg11))
6820 {
6821 HOST_WIDE_INT int01, int11, tmp;
6822 bool swap = false;
6823 tree maybe_same;
6824 int01 = tree_to_shwi (arg01);
6825 int11 = tree_to_shwi (arg11);
6826
6827 /* Move min of absolute values to int11. */
6828 if (absu_hwi (int01) < absu_hwi (int11))
6829 {
6830 tmp = int01, int01 = int11, int11 = tmp;
6831 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6832 maybe_same = arg01;
6833 swap = true;
6834 }
6835 else
6836 maybe_same = arg11;
6837
6838 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6839 /* The remainder should not be a constant, otherwise we
6840 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6841 increased the number of multiplications necessary. */
6842 && TREE_CODE (arg10) != INTEGER_CST)
6843 {
6844 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6845 build_int_cst (TREE_TYPE (arg00),
6846 int01 / int11));
6847 alt1 = arg10;
6848 same = maybe_same;
6849 if (swap)
6850 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6851 }
6852 }
6853
6854 if (same)
6855 return fold_build2_loc (loc, MULT_EXPR, type,
6856 fold_build2_loc (loc, code, type,
6857 fold_convert_loc (loc, type, alt0),
6858 fold_convert_loc (loc, type, alt1)),
6859 fold_convert_loc (loc, type, same));
6860
6861 return NULL_TREE;
6862 }
6863
6864 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6865 specified by EXPR into the buffer PTR of length LEN bytes.
6866 Return the number of bytes placed in the buffer, or zero
6867 upon failure. */
6868
6869 static int
6870 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6871 {
6872 tree type = TREE_TYPE (expr);
6873 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6874 int byte, offset, word, words;
6875 unsigned char value;
6876
6877 if ((off == -1 && total_bytes > len)
6878 || off >= total_bytes)
6879 return 0;
6880 if (off == -1)
6881 off = 0;
6882 words = total_bytes / UNITS_PER_WORD;
6883
6884 for (byte = 0; byte < total_bytes; byte++)
6885 {
6886 int bitpos = byte * BITS_PER_UNIT;
6887 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6888 number of bytes. */
6889 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
6890
6891 if (total_bytes > UNITS_PER_WORD)
6892 {
6893 word = byte / UNITS_PER_WORD;
6894 if (WORDS_BIG_ENDIAN)
6895 word = (words - 1) - word;
6896 offset = word * UNITS_PER_WORD;
6897 if (BYTES_BIG_ENDIAN)
6898 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6899 else
6900 offset += byte % UNITS_PER_WORD;
6901 }
6902 else
6903 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6904 if (offset >= off
6905 && offset - off < len)
6906 ptr[offset - off] = value;
6907 }
6908 return MIN (len, total_bytes - off);
6909 }
6910
6911
6912 /* Subroutine of native_encode_expr. Encode the FIXED_CST
6913 specified by EXPR into the buffer PTR of length LEN bytes.
6914 Return the number of bytes placed in the buffer, or zero
6915 upon failure. */
6916
6917 static int
6918 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
6919 {
6920 tree type = TREE_TYPE (expr);
6921 machine_mode mode = TYPE_MODE (type);
6922 int total_bytes = GET_MODE_SIZE (mode);
6923 FIXED_VALUE_TYPE value;
6924 tree i_value, i_type;
6925
6926 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
6927 return 0;
6928
6929 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
6930
6931 if (NULL_TREE == i_type
6932 || TYPE_PRECISION (i_type) != total_bytes)
6933 return 0;
6934
6935 value = TREE_FIXED_CST (expr);
6936 i_value = double_int_to_tree (i_type, value.data);
6937
6938 return native_encode_int (i_value, ptr, len, off);
6939 }
6940
6941
6942 /* Subroutine of native_encode_expr. Encode the REAL_CST
6943 specified by EXPR into the buffer PTR of length LEN bytes.
6944 Return the number of bytes placed in the buffer, or zero
6945 upon failure. */
6946
6947 static int
6948 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
6949 {
6950 tree type = TREE_TYPE (expr);
6951 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6952 int byte, offset, word, words, bitpos;
6953 unsigned char value;
6954
6955 /* There are always 32 bits in each long, no matter the size of
6956 the hosts long. We handle floating point representations with
6957 up to 192 bits. */
6958 long tmp[6];
6959
6960 if ((off == -1 && total_bytes > len)
6961 || off >= total_bytes)
6962 return 0;
6963 if (off == -1)
6964 off = 0;
6965 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
6966
6967 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
6968
6969 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
6970 bitpos += BITS_PER_UNIT)
6971 {
6972 byte = (bitpos / BITS_PER_UNIT) & 3;
6973 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
6974
6975 if (UNITS_PER_WORD < 4)
6976 {
6977 word = byte / UNITS_PER_WORD;
6978 if (WORDS_BIG_ENDIAN)
6979 word = (words - 1) - word;
6980 offset = word * UNITS_PER_WORD;
6981 if (BYTES_BIG_ENDIAN)
6982 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6983 else
6984 offset += byte % UNITS_PER_WORD;
6985 }
6986 else
6987 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
6988 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
6989 if (offset >= off
6990 && offset - off < len)
6991 ptr[offset - off] = value;
6992 }
6993 return MIN (len, total_bytes - off);
6994 }
6995
6996 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
6997 specified by EXPR into the buffer PTR of length LEN bytes.
6998 Return the number of bytes placed in the buffer, or zero
6999 upon failure. */
7000
7001 static int
7002 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7003 {
7004 int rsize, isize;
7005 tree part;
7006
7007 part = TREE_REALPART (expr);
7008 rsize = native_encode_expr (part, ptr, len, off);
7009 if (off == -1
7010 && rsize == 0)
7011 return 0;
7012 part = TREE_IMAGPART (expr);
7013 if (off != -1)
7014 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7015 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7016 if (off == -1
7017 && isize != rsize)
7018 return 0;
7019 return rsize + isize;
7020 }
7021
7022
7023 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7024 specified by EXPR into the buffer PTR of length LEN bytes.
7025 Return the number of bytes placed in the buffer, or zero
7026 upon failure. */
7027
7028 static int
7029 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7030 {
7031 unsigned i, count;
7032 int size, offset;
7033 tree itype, elem;
7034
7035 offset = 0;
7036 count = VECTOR_CST_NELTS (expr);
7037 itype = TREE_TYPE (TREE_TYPE (expr));
7038 size = GET_MODE_SIZE (TYPE_MODE (itype));
7039 for (i = 0; i < count; i++)
7040 {
7041 if (off >= size)
7042 {
7043 off -= size;
7044 continue;
7045 }
7046 elem = VECTOR_CST_ELT (expr, i);
7047 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7048 if ((off == -1 && res != size)
7049 || res == 0)
7050 return 0;
7051 offset += res;
7052 if (offset >= len)
7053 return offset;
7054 if (off != -1)
7055 off = 0;
7056 }
7057 return offset;
7058 }
7059
7060
7061 /* Subroutine of native_encode_expr. Encode the STRING_CST
7062 specified by EXPR into the buffer PTR of length LEN bytes.
7063 Return the number of bytes placed in the buffer, or zero
7064 upon failure. */
7065
7066 static int
7067 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7068 {
7069 tree type = TREE_TYPE (expr);
7070 HOST_WIDE_INT total_bytes;
7071
7072 if (TREE_CODE (type) != ARRAY_TYPE
7073 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7074 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7075 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7076 return 0;
7077 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7078 if ((off == -1 && total_bytes > len)
7079 || off >= total_bytes)
7080 return 0;
7081 if (off == -1)
7082 off = 0;
7083 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7084 {
7085 int written = 0;
7086 if (off < TREE_STRING_LENGTH (expr))
7087 {
7088 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7089 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7090 }
7091 memset (ptr + written, 0,
7092 MIN (total_bytes - written, len - written));
7093 }
7094 else
7095 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7096 return MIN (total_bytes - off, len);
7097 }
7098
7099
7100 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7101 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7102 buffer PTR of length LEN bytes. If OFF is not -1 then start
7103 the encoding at byte offset OFF and encode at most LEN bytes.
7104 Return the number of bytes placed in the buffer, or zero upon failure. */
7105
7106 int
7107 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7108 {
7109 switch (TREE_CODE (expr))
7110 {
7111 case INTEGER_CST:
7112 return native_encode_int (expr, ptr, len, off);
7113
7114 case REAL_CST:
7115 return native_encode_real (expr, ptr, len, off);
7116
7117 case FIXED_CST:
7118 return native_encode_fixed (expr, ptr, len, off);
7119
7120 case COMPLEX_CST:
7121 return native_encode_complex (expr, ptr, len, off);
7122
7123 case VECTOR_CST:
7124 return native_encode_vector (expr, ptr, len, off);
7125
7126 case STRING_CST:
7127 return native_encode_string (expr, ptr, len, off);
7128
7129 default:
7130 return 0;
7131 }
7132 }
7133
7134
7135 /* Subroutine of native_interpret_expr. Interpret the contents of
7136 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7137 If the buffer cannot be interpreted, return NULL_TREE. */
7138
7139 static tree
7140 native_interpret_int (tree type, const unsigned char *ptr, int len)
7141 {
7142 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7143
7144 if (total_bytes > len
7145 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7146 return NULL_TREE;
7147
7148 wide_int result = wi::from_buffer (ptr, total_bytes);
7149
7150 return wide_int_to_tree (type, result);
7151 }
7152
7153
7154 /* Subroutine of native_interpret_expr. Interpret the contents of
7155 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7156 If the buffer cannot be interpreted, return NULL_TREE. */
7157
7158 static tree
7159 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7160 {
7161 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7162 double_int result;
7163 FIXED_VALUE_TYPE fixed_value;
7164
7165 if (total_bytes > len
7166 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7167 return NULL_TREE;
7168
7169 result = double_int::from_buffer (ptr, total_bytes);
7170 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7171
7172 return build_fixed (type, fixed_value);
7173 }
7174
7175
7176 /* Subroutine of native_interpret_expr. Interpret the contents of
7177 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7178 If the buffer cannot be interpreted, return NULL_TREE. */
7179
7180 static tree
7181 native_interpret_real (tree type, const unsigned char *ptr, int len)
7182 {
7183 machine_mode mode = TYPE_MODE (type);
7184 int total_bytes = GET_MODE_SIZE (mode);
7185 int byte, offset, word, words, bitpos;
7186 unsigned char value;
7187 /* There are always 32 bits in each long, no matter the size of
7188 the hosts long. We handle floating point representations with
7189 up to 192 bits. */
7190 REAL_VALUE_TYPE r;
7191 long tmp[6];
7192
7193 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7194 if (total_bytes > len || total_bytes > 24)
7195 return NULL_TREE;
7196 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7197
7198 memset (tmp, 0, sizeof (tmp));
7199 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7200 bitpos += BITS_PER_UNIT)
7201 {
7202 byte = (bitpos / BITS_PER_UNIT) & 3;
7203 if (UNITS_PER_WORD < 4)
7204 {
7205 word = byte / UNITS_PER_WORD;
7206 if (WORDS_BIG_ENDIAN)
7207 word = (words - 1) - word;
7208 offset = word * UNITS_PER_WORD;
7209 if (BYTES_BIG_ENDIAN)
7210 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7211 else
7212 offset += byte % UNITS_PER_WORD;
7213 }
7214 else
7215 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7216 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7217
7218 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7219 }
7220
7221 real_from_target (&r, tmp, mode);
7222 return build_real (type, r);
7223 }
7224
7225
7226 /* Subroutine of native_interpret_expr. Interpret the contents of
7227 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7228 If the buffer cannot be interpreted, return NULL_TREE. */
7229
7230 static tree
7231 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7232 {
7233 tree etype, rpart, ipart;
7234 int size;
7235
7236 etype = TREE_TYPE (type);
7237 size = GET_MODE_SIZE (TYPE_MODE (etype));
7238 if (size * 2 > len)
7239 return NULL_TREE;
7240 rpart = native_interpret_expr (etype, ptr, size);
7241 if (!rpart)
7242 return NULL_TREE;
7243 ipart = native_interpret_expr (etype, ptr+size, size);
7244 if (!ipart)
7245 return NULL_TREE;
7246 return build_complex (type, rpart, ipart);
7247 }
7248
7249
7250 /* Subroutine of native_interpret_expr. Interpret the contents of
7251 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7252 If the buffer cannot be interpreted, return NULL_TREE. */
7253
7254 static tree
7255 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7256 {
7257 tree etype, elem;
7258 int i, size, count;
7259 tree *elements;
7260
7261 etype = TREE_TYPE (type);
7262 size = GET_MODE_SIZE (TYPE_MODE (etype));
7263 count = TYPE_VECTOR_SUBPARTS (type);
7264 if (size * count > len)
7265 return NULL_TREE;
7266
7267 elements = XALLOCAVEC (tree, count);
7268 for (i = count - 1; i >= 0; i--)
7269 {
7270 elem = native_interpret_expr (etype, ptr+(i*size), size);
7271 if (!elem)
7272 return NULL_TREE;
7273 elements[i] = elem;
7274 }
7275 return build_vector (type, elements);
7276 }
7277
7278
7279 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7280 the buffer PTR of length LEN as a constant of type TYPE. For
7281 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7282 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7283 return NULL_TREE. */
7284
7285 tree
7286 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7287 {
7288 switch (TREE_CODE (type))
7289 {
7290 case INTEGER_TYPE:
7291 case ENUMERAL_TYPE:
7292 case BOOLEAN_TYPE:
7293 case POINTER_TYPE:
7294 case REFERENCE_TYPE:
7295 return native_interpret_int (type, ptr, len);
7296
7297 case REAL_TYPE:
7298 return native_interpret_real (type, ptr, len);
7299
7300 case FIXED_POINT_TYPE:
7301 return native_interpret_fixed (type, ptr, len);
7302
7303 case COMPLEX_TYPE:
7304 return native_interpret_complex (type, ptr, len);
7305
7306 case VECTOR_TYPE:
7307 return native_interpret_vector (type, ptr, len);
7308
7309 default:
7310 return NULL_TREE;
7311 }
7312 }
7313
7314 /* Returns true if we can interpret the contents of a native encoding
7315 as TYPE. */
7316
7317 static bool
7318 can_native_interpret_type_p (tree type)
7319 {
7320 switch (TREE_CODE (type))
7321 {
7322 case INTEGER_TYPE:
7323 case ENUMERAL_TYPE:
7324 case BOOLEAN_TYPE:
7325 case POINTER_TYPE:
7326 case REFERENCE_TYPE:
7327 case FIXED_POINT_TYPE:
7328 case REAL_TYPE:
7329 case COMPLEX_TYPE:
7330 case VECTOR_TYPE:
7331 return true;
7332 default:
7333 return false;
7334 }
7335 }
7336
7337 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7338 TYPE at compile-time. If we're unable to perform the conversion
7339 return NULL_TREE. */
7340
7341 static tree
7342 fold_view_convert_expr (tree type, tree expr)
7343 {
7344 /* We support up to 512-bit values (for V8DFmode). */
7345 unsigned char buffer[64];
7346 int len;
7347
7348 /* Check that the host and target are sane. */
7349 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7350 return NULL_TREE;
7351
7352 len = native_encode_expr (expr, buffer, sizeof (buffer));
7353 if (len == 0)
7354 return NULL_TREE;
7355
7356 return native_interpret_expr (type, buffer, len);
7357 }
7358
7359 /* Build an expression for the address of T. Folds away INDIRECT_REF
7360 to avoid confusing the gimplify process. */
7361
7362 tree
7363 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7364 {
7365 /* The size of the object is not relevant when talking about its address. */
7366 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7367 t = TREE_OPERAND (t, 0);
7368
7369 if (TREE_CODE (t) == INDIRECT_REF)
7370 {
7371 t = TREE_OPERAND (t, 0);
7372
7373 if (TREE_TYPE (t) != ptrtype)
7374 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7375 }
7376 else if (TREE_CODE (t) == MEM_REF
7377 && integer_zerop (TREE_OPERAND (t, 1)))
7378 return TREE_OPERAND (t, 0);
7379 else if (TREE_CODE (t) == MEM_REF
7380 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7381 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7382 TREE_OPERAND (t, 0),
7383 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7384 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7385 {
7386 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7387
7388 if (TREE_TYPE (t) != ptrtype)
7389 t = fold_convert_loc (loc, ptrtype, t);
7390 }
7391 else
7392 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7393
7394 return t;
7395 }
7396
7397 /* Build an expression for the address of T. */
7398
7399 tree
7400 build_fold_addr_expr_loc (location_t loc, tree t)
7401 {
7402 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7403
7404 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7405 }
7406
7407 /* Fold a unary expression of code CODE and type TYPE with operand
7408 OP0. Return the folded expression if folding is successful.
7409 Otherwise, return NULL_TREE. */
7410
7411 tree
7412 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7413 {
7414 tree tem;
7415 tree arg0;
7416 enum tree_code_class kind = TREE_CODE_CLASS (code);
7417
7418 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7419 && TREE_CODE_LENGTH (code) == 1);
7420
7421 arg0 = op0;
7422 if (arg0)
7423 {
7424 if (CONVERT_EXPR_CODE_P (code)
7425 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7426 {
7427 /* Don't use STRIP_NOPS, because signedness of argument type
7428 matters. */
7429 STRIP_SIGN_NOPS (arg0);
7430 }
7431 else
7432 {
7433 /* Strip any conversions that don't change the mode. This
7434 is safe for every expression, except for a comparison
7435 expression because its signedness is derived from its
7436 operands.
7437
7438 Note that this is done as an internal manipulation within
7439 the constant folder, in order to find the simplest
7440 representation of the arguments so that their form can be
7441 studied. In any cases, the appropriate type conversions
7442 should be put back in the tree that will get out of the
7443 constant folder. */
7444 STRIP_NOPS (arg0);
7445 }
7446
7447 if (CONSTANT_CLASS_P (arg0))
7448 {
7449 tree tem = const_unop (code, type, arg0);
7450 if (tem)
7451 {
7452 if (TREE_TYPE (tem) != type)
7453 tem = fold_convert_loc (loc, type, tem);
7454 return tem;
7455 }
7456 }
7457 }
7458
7459 tem = generic_simplify (loc, code, type, op0);
7460 if (tem)
7461 return tem;
7462
7463 if (TREE_CODE_CLASS (code) == tcc_unary)
7464 {
7465 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7466 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7467 fold_build1_loc (loc, code, type,
7468 fold_convert_loc (loc, TREE_TYPE (op0),
7469 TREE_OPERAND (arg0, 1))));
7470 else if (TREE_CODE (arg0) == COND_EXPR)
7471 {
7472 tree arg01 = TREE_OPERAND (arg0, 1);
7473 tree arg02 = TREE_OPERAND (arg0, 2);
7474 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7475 arg01 = fold_build1_loc (loc, code, type,
7476 fold_convert_loc (loc,
7477 TREE_TYPE (op0), arg01));
7478 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7479 arg02 = fold_build1_loc (loc, code, type,
7480 fold_convert_loc (loc,
7481 TREE_TYPE (op0), arg02));
7482 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7483 arg01, arg02);
7484
7485 /* If this was a conversion, and all we did was to move into
7486 inside the COND_EXPR, bring it back out. But leave it if
7487 it is a conversion from integer to integer and the
7488 result precision is no wider than a word since such a
7489 conversion is cheap and may be optimized away by combine,
7490 while it couldn't if it were outside the COND_EXPR. Then return
7491 so we don't get into an infinite recursion loop taking the
7492 conversion out and then back in. */
7493
7494 if ((CONVERT_EXPR_CODE_P (code)
7495 || code == NON_LVALUE_EXPR)
7496 && TREE_CODE (tem) == COND_EXPR
7497 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7498 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7499 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7500 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7501 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7502 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7503 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7504 && (INTEGRAL_TYPE_P
7505 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7506 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7507 || flag_syntax_only))
7508 tem = build1_loc (loc, code, type,
7509 build3 (COND_EXPR,
7510 TREE_TYPE (TREE_OPERAND
7511 (TREE_OPERAND (tem, 1), 0)),
7512 TREE_OPERAND (tem, 0),
7513 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7514 TREE_OPERAND (TREE_OPERAND (tem, 2),
7515 0)));
7516 return tem;
7517 }
7518 }
7519
7520 switch (code)
7521 {
7522 case NON_LVALUE_EXPR:
7523 if (!maybe_lvalue_p (op0))
7524 return fold_convert_loc (loc, type, op0);
7525 return NULL_TREE;
7526
7527 CASE_CONVERT:
7528 case FLOAT_EXPR:
7529 case FIX_TRUNC_EXPR:
7530 if (COMPARISON_CLASS_P (op0))
7531 {
7532 /* If we have (type) (a CMP b) and type is an integral type, return
7533 new expression involving the new type. Canonicalize
7534 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7535 non-integral type.
7536 Do not fold the result as that would not simplify further, also
7537 folding again results in recursions. */
7538 if (TREE_CODE (type) == BOOLEAN_TYPE)
7539 return build2_loc (loc, TREE_CODE (op0), type,
7540 TREE_OPERAND (op0, 0),
7541 TREE_OPERAND (op0, 1));
7542 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7543 && TREE_CODE (type) != VECTOR_TYPE)
7544 return build3_loc (loc, COND_EXPR, type, op0,
7545 constant_boolean_node (true, type),
7546 constant_boolean_node (false, type));
7547 }
7548
7549 /* Handle (T *)&A.B.C for A being of type T and B and C
7550 living at offset zero. This occurs frequently in
7551 C++ upcasting and then accessing the base. */
7552 if (TREE_CODE (op0) == ADDR_EXPR
7553 && POINTER_TYPE_P (type)
7554 && handled_component_p (TREE_OPERAND (op0, 0)))
7555 {
7556 HOST_WIDE_INT bitsize, bitpos;
7557 tree offset;
7558 machine_mode mode;
7559 int unsignedp, volatilep;
7560 tree base = TREE_OPERAND (op0, 0);
7561 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7562 &mode, &unsignedp, &volatilep, false);
7563 /* If the reference was to a (constant) zero offset, we can use
7564 the address of the base if it has the same base type
7565 as the result type and the pointer type is unqualified. */
7566 if (! offset && bitpos == 0
7567 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7568 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7569 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7570 return fold_convert_loc (loc, type,
7571 build_fold_addr_expr_loc (loc, base));
7572 }
7573
7574 if (TREE_CODE (op0) == MODIFY_EXPR
7575 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7576 /* Detect assigning a bitfield. */
7577 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7578 && DECL_BIT_FIELD
7579 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7580 {
7581 /* Don't leave an assignment inside a conversion
7582 unless assigning a bitfield. */
7583 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7584 /* First do the assignment, then return converted constant. */
7585 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7586 TREE_NO_WARNING (tem) = 1;
7587 TREE_USED (tem) = 1;
7588 return tem;
7589 }
7590
7591 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7592 constants (if x has signed type, the sign bit cannot be set
7593 in c). This folds extension into the BIT_AND_EXPR.
7594 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7595 very likely don't have maximal range for their precision and this
7596 transformation effectively doesn't preserve non-maximal ranges. */
7597 if (TREE_CODE (type) == INTEGER_TYPE
7598 && TREE_CODE (op0) == BIT_AND_EXPR
7599 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7600 {
7601 tree and_expr = op0;
7602 tree and0 = TREE_OPERAND (and_expr, 0);
7603 tree and1 = TREE_OPERAND (and_expr, 1);
7604 int change = 0;
7605
7606 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7607 || (TYPE_PRECISION (type)
7608 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7609 change = 1;
7610 else if (TYPE_PRECISION (TREE_TYPE (and1))
7611 <= HOST_BITS_PER_WIDE_INT
7612 && tree_fits_uhwi_p (and1))
7613 {
7614 unsigned HOST_WIDE_INT cst;
7615
7616 cst = tree_to_uhwi (and1);
7617 cst &= HOST_WIDE_INT_M1U
7618 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7619 change = (cst == 0);
7620 if (change
7621 && !flag_syntax_only
7622 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7623 == ZERO_EXTEND))
7624 {
7625 tree uns = unsigned_type_for (TREE_TYPE (and0));
7626 and0 = fold_convert_loc (loc, uns, and0);
7627 and1 = fold_convert_loc (loc, uns, and1);
7628 }
7629 }
7630 if (change)
7631 {
7632 tem = force_fit_type (type, wi::to_widest (and1), 0,
7633 TREE_OVERFLOW (and1));
7634 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7635 fold_convert_loc (loc, type, and0), tem);
7636 }
7637 }
7638
7639 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7640 when one of the new casts will fold away. Conservatively we assume
7641 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7642 if (POINTER_TYPE_P (type)
7643 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7644 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
7645 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7646 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7647 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7648 {
7649 tree arg00 = TREE_OPERAND (arg0, 0);
7650 tree arg01 = TREE_OPERAND (arg0, 1);
7651
7652 return fold_build_pointer_plus_loc
7653 (loc, fold_convert_loc (loc, type, arg00), arg01);
7654 }
7655
7656 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7657 of the same precision, and X is an integer type not narrower than
7658 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7659 if (INTEGRAL_TYPE_P (type)
7660 && TREE_CODE (op0) == BIT_NOT_EXPR
7661 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7662 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7663 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7664 {
7665 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7666 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7667 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7668 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7669 fold_convert_loc (loc, type, tem));
7670 }
7671
7672 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7673 type of X and Y (integer types only). */
7674 if (INTEGRAL_TYPE_P (type)
7675 && TREE_CODE (op0) == MULT_EXPR
7676 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7677 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7678 {
7679 /* Be careful not to introduce new overflows. */
7680 tree mult_type;
7681 if (TYPE_OVERFLOW_WRAPS (type))
7682 mult_type = type;
7683 else
7684 mult_type = unsigned_type_for (type);
7685
7686 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7687 {
7688 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7689 fold_convert_loc (loc, mult_type,
7690 TREE_OPERAND (op0, 0)),
7691 fold_convert_loc (loc, mult_type,
7692 TREE_OPERAND (op0, 1)));
7693 return fold_convert_loc (loc, type, tem);
7694 }
7695 }
7696
7697 return NULL_TREE;
7698
7699 case VIEW_CONVERT_EXPR:
7700 if (TREE_CODE (op0) == MEM_REF)
7701 return fold_build2_loc (loc, MEM_REF, type,
7702 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7703
7704 return NULL_TREE;
7705
7706 case NEGATE_EXPR:
7707 tem = fold_negate_expr (loc, arg0);
7708 if (tem)
7709 return fold_convert_loc (loc, type, tem);
7710 return NULL_TREE;
7711
7712 case ABS_EXPR:
7713 /* Convert fabs((double)float) into (double)fabsf(float). */
7714 if (TREE_CODE (arg0) == NOP_EXPR
7715 && TREE_CODE (type) == REAL_TYPE)
7716 {
7717 tree targ0 = strip_float_extensions (arg0);
7718 if (targ0 != arg0)
7719 return fold_convert_loc (loc, type,
7720 fold_build1_loc (loc, ABS_EXPR,
7721 TREE_TYPE (targ0),
7722 targ0));
7723 }
7724
7725 /* Strip sign ops from argument. */
7726 if (TREE_CODE (type) == REAL_TYPE)
7727 {
7728 tem = fold_strip_sign_ops (arg0);
7729 if (tem)
7730 return fold_build1_loc (loc, ABS_EXPR, type,
7731 fold_convert_loc (loc, type, tem));
7732 }
7733 return NULL_TREE;
7734
7735 case CONJ_EXPR:
7736 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7737 return fold_convert_loc (loc, type, arg0);
7738 if (TREE_CODE (arg0) == COMPLEX_EXPR)
7739 {
7740 tree itype = TREE_TYPE (type);
7741 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
7742 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
7743 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
7744 negate_expr (ipart));
7745 }
7746 if (TREE_CODE (arg0) == CONJ_EXPR)
7747 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
7748 return NULL_TREE;
7749
7750 case BIT_NOT_EXPR:
7751 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7752 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7753 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7754 fold_convert_loc (loc, type,
7755 TREE_OPERAND (arg0, 0)))))
7756 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7757 fold_convert_loc (loc, type,
7758 TREE_OPERAND (arg0, 1)));
7759 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7760 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7761 fold_convert_loc (loc, type,
7762 TREE_OPERAND (arg0, 1)))))
7763 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7764 fold_convert_loc (loc, type,
7765 TREE_OPERAND (arg0, 0)), tem);
7766
7767 return NULL_TREE;
7768
7769 case TRUTH_NOT_EXPR:
7770 /* Note that the operand of this must be an int
7771 and its values must be 0 or 1.
7772 ("true" is a fixed value perhaps depending on the language,
7773 but we don't handle values other than 1 correctly yet.) */
7774 tem = fold_truth_not_expr (loc, arg0);
7775 if (!tem)
7776 return NULL_TREE;
7777 return fold_convert_loc (loc, type, tem);
7778
7779 case REALPART_EXPR:
7780 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7781 return fold_convert_loc (loc, type, arg0);
7782 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7783 {
7784 tree itype = TREE_TYPE (TREE_TYPE (arg0));
7785 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
7786 fold_build1_loc (loc, REALPART_EXPR, itype,
7787 TREE_OPERAND (arg0, 0)),
7788 fold_build1_loc (loc, REALPART_EXPR, itype,
7789 TREE_OPERAND (arg0, 1)));
7790 return fold_convert_loc (loc, type, tem);
7791 }
7792 if (TREE_CODE (arg0) == CONJ_EXPR)
7793 {
7794 tree itype = TREE_TYPE (TREE_TYPE (arg0));
7795 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
7796 TREE_OPERAND (arg0, 0));
7797 return fold_convert_loc (loc, type, tem);
7798 }
7799 if (TREE_CODE (arg0) == CALL_EXPR)
7800 {
7801 tree fn = get_callee_fndecl (arg0);
7802 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7803 switch (DECL_FUNCTION_CODE (fn))
7804 {
7805 CASE_FLT_FN (BUILT_IN_CEXPI):
7806 fn = mathfn_built_in (type, BUILT_IN_COS);
7807 if (fn)
7808 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
7809 break;
7810
7811 default:
7812 break;
7813 }
7814 }
7815 return NULL_TREE;
7816
7817 case IMAGPART_EXPR:
7818 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7819 return build_zero_cst (type);
7820 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7821 {
7822 tree itype = TREE_TYPE (TREE_TYPE (arg0));
7823 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
7824 fold_build1_loc (loc, IMAGPART_EXPR, itype,
7825 TREE_OPERAND (arg0, 0)),
7826 fold_build1_loc (loc, IMAGPART_EXPR, itype,
7827 TREE_OPERAND (arg0, 1)));
7828 return fold_convert_loc (loc, type, tem);
7829 }
7830 if (TREE_CODE (arg0) == CONJ_EXPR)
7831 {
7832 tree itype = TREE_TYPE (TREE_TYPE (arg0));
7833 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
7834 return fold_convert_loc (loc, type, negate_expr (tem));
7835 }
7836 if (TREE_CODE (arg0) == CALL_EXPR)
7837 {
7838 tree fn = get_callee_fndecl (arg0);
7839 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7840 switch (DECL_FUNCTION_CODE (fn))
7841 {
7842 CASE_FLT_FN (BUILT_IN_CEXPI):
7843 fn = mathfn_built_in (type, BUILT_IN_SIN);
7844 if (fn)
7845 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
7846 break;
7847
7848 default:
7849 break;
7850 }
7851 }
7852 return NULL_TREE;
7853
7854 case INDIRECT_REF:
7855 /* Fold *&X to X if X is an lvalue. */
7856 if (TREE_CODE (op0) == ADDR_EXPR)
7857 {
7858 tree op00 = TREE_OPERAND (op0, 0);
7859 if ((TREE_CODE (op00) == VAR_DECL
7860 || TREE_CODE (op00) == PARM_DECL
7861 || TREE_CODE (op00) == RESULT_DECL)
7862 && !TREE_READONLY (op00))
7863 return op00;
7864 }
7865 return NULL_TREE;
7866
7867 default:
7868 return NULL_TREE;
7869 } /* switch (code) */
7870 }
7871
7872
7873 /* If the operation was a conversion do _not_ mark a resulting constant
7874 with TREE_OVERFLOW if the original constant was not. These conversions
7875 have implementation defined behavior and retaining the TREE_OVERFLOW
7876 flag here would confuse later passes such as VRP. */
7877 tree
7878 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7879 tree type, tree op0)
7880 {
7881 tree res = fold_unary_loc (loc, code, type, op0);
7882 if (res
7883 && TREE_CODE (res) == INTEGER_CST
7884 && TREE_CODE (op0) == INTEGER_CST
7885 && CONVERT_EXPR_CODE_P (code))
7886 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7887
7888 return res;
7889 }
7890
7891 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7892 operands OP0 and OP1. LOC is the location of the resulting expression.
7893 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7894 Return the folded expression if folding is successful. Otherwise,
7895 return NULL_TREE. */
7896 static tree
7897 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7898 tree arg0, tree arg1, tree op0, tree op1)
7899 {
7900 tree tem;
7901
7902 /* We only do these simplifications if we are optimizing. */
7903 if (!optimize)
7904 return NULL_TREE;
7905
7906 /* Check for things like (A || B) && (A || C). We can convert this
7907 to A || (B && C). Note that either operator can be any of the four
7908 truth and/or operations and the transformation will still be
7909 valid. Also note that we only care about order for the
7910 ANDIF and ORIF operators. If B contains side effects, this
7911 might change the truth-value of A. */
7912 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7913 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7914 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7915 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7916 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7917 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7918 {
7919 tree a00 = TREE_OPERAND (arg0, 0);
7920 tree a01 = TREE_OPERAND (arg0, 1);
7921 tree a10 = TREE_OPERAND (arg1, 0);
7922 tree a11 = TREE_OPERAND (arg1, 1);
7923 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7924 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7925 && (code == TRUTH_AND_EXPR
7926 || code == TRUTH_OR_EXPR));
7927
7928 if (operand_equal_p (a00, a10, 0))
7929 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7930 fold_build2_loc (loc, code, type, a01, a11));
7931 else if (commutative && operand_equal_p (a00, a11, 0))
7932 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7933 fold_build2_loc (loc, code, type, a01, a10));
7934 else if (commutative && operand_equal_p (a01, a10, 0))
7935 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7936 fold_build2_loc (loc, code, type, a00, a11));
7937
7938 /* This case if tricky because we must either have commutative
7939 operators or else A10 must not have side-effects. */
7940
7941 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7942 && operand_equal_p (a01, a11, 0))
7943 return fold_build2_loc (loc, TREE_CODE (arg0), type,
7944 fold_build2_loc (loc, code, type, a00, a10),
7945 a01);
7946 }
7947
7948 /* See if we can build a range comparison. */
7949 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
7950 return tem;
7951
7952 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
7953 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
7954 {
7955 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
7956 if (tem)
7957 return fold_build2_loc (loc, code, type, tem, arg1);
7958 }
7959
7960 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
7961 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
7962 {
7963 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
7964 if (tem)
7965 return fold_build2_loc (loc, code, type, arg0, tem);
7966 }
7967
7968 /* Check for the possibility of merging component references. If our
7969 lhs is another similar operation, try to merge its rhs with our
7970 rhs. Then try to merge our lhs and rhs. */
7971 if (TREE_CODE (arg0) == code
7972 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
7973 TREE_OPERAND (arg0, 1), arg1)))
7974 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
7975
7976 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
7977 return tem;
7978
7979 if (LOGICAL_OP_NON_SHORT_CIRCUIT
7980 && (code == TRUTH_AND_EXPR
7981 || code == TRUTH_ANDIF_EXPR
7982 || code == TRUTH_OR_EXPR
7983 || code == TRUTH_ORIF_EXPR))
7984 {
7985 enum tree_code ncode, icode;
7986
7987 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
7988 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
7989 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
7990
7991 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
7992 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
7993 We don't want to pack more than two leafs to a non-IF AND/OR
7994 expression.
7995 If tree-code of left-hand operand isn't an AND/OR-IF code and not
7996 equal to IF-CODE, then we don't want to add right-hand operand.
7997 If the inner right-hand side of left-hand operand has
7998 side-effects, or isn't simple, then we can't add to it,
7999 as otherwise we might destroy if-sequence. */
8000 if (TREE_CODE (arg0) == icode
8001 && simple_operand_p_2 (arg1)
8002 /* Needed for sequence points to handle trappings, and
8003 side-effects. */
8004 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8005 {
8006 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8007 arg1);
8008 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8009 tem);
8010 }
8011 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8012 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8013 else if (TREE_CODE (arg1) == icode
8014 && simple_operand_p_2 (arg0)
8015 /* Needed for sequence points to handle trappings, and
8016 side-effects. */
8017 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8018 {
8019 tem = fold_build2_loc (loc, ncode, type,
8020 arg0, TREE_OPERAND (arg1, 0));
8021 return fold_build2_loc (loc, icode, type, tem,
8022 TREE_OPERAND (arg1, 1));
8023 }
8024 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8025 into (A OR B).
8026 For sequence point consistancy, we need to check for trapping,
8027 and side-effects. */
8028 else if (code == icode && simple_operand_p_2 (arg0)
8029 && simple_operand_p_2 (arg1))
8030 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8031 }
8032
8033 return NULL_TREE;
8034 }
8035
8036 /* Fold a binary expression of code CODE and type TYPE with operands
8037 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8038 Return the folded expression if folding is successful. Otherwise,
8039 return NULL_TREE. */
8040
8041 static tree
8042 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8043 {
8044 enum tree_code compl_code;
8045
8046 if (code == MIN_EXPR)
8047 compl_code = MAX_EXPR;
8048 else if (code == MAX_EXPR)
8049 compl_code = MIN_EXPR;
8050 else
8051 gcc_unreachable ();
8052
8053 /* MIN (MAX (a, b), b) == b. */
8054 if (TREE_CODE (op0) == compl_code
8055 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8056 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8057
8058 /* MIN (MAX (b, a), b) == b. */
8059 if (TREE_CODE (op0) == compl_code
8060 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8061 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8062 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8063
8064 /* MIN (a, MAX (a, b)) == a. */
8065 if (TREE_CODE (op1) == compl_code
8066 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8067 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8068 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8069
8070 /* MIN (a, MAX (b, a)) == a. */
8071 if (TREE_CODE (op1) == compl_code
8072 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8073 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8074 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8075
8076 return NULL_TREE;
8077 }
8078
8079 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8080 by changing CODE to reduce the magnitude of constants involved in
8081 ARG0 of the comparison.
8082 Returns a canonicalized comparison tree if a simplification was
8083 possible, otherwise returns NULL_TREE.
8084 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8085 valid if signed overflow is undefined. */
8086
8087 static tree
8088 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8089 tree arg0, tree arg1,
8090 bool *strict_overflow_p)
8091 {
8092 enum tree_code code0 = TREE_CODE (arg0);
8093 tree t, cst0 = NULL_TREE;
8094 int sgn0;
8095 bool swap = false;
8096
8097 /* Match A +- CST code arg1 and CST code arg1. We can change the
8098 first form only if overflow is undefined. */
8099 if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8100 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8101 /* In principle pointers also have undefined overflow behavior,
8102 but that causes problems elsewhere. */
8103 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8104 && (code0 == MINUS_EXPR
8105 || code0 == PLUS_EXPR)
8106 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8107 || code0 == INTEGER_CST))
8108 return NULL_TREE;
8109
8110 /* Identify the constant in arg0 and its sign. */
8111 if (code0 == INTEGER_CST)
8112 cst0 = arg0;
8113 else
8114 cst0 = TREE_OPERAND (arg0, 1);
8115 sgn0 = tree_int_cst_sgn (cst0);
8116
8117 /* Overflowed constants and zero will cause problems. */
8118 if (integer_zerop (cst0)
8119 || TREE_OVERFLOW (cst0))
8120 return NULL_TREE;
8121
8122 /* See if we can reduce the magnitude of the constant in
8123 arg0 by changing the comparison code. */
8124 if (code0 == INTEGER_CST)
8125 {
8126 /* CST <= arg1 -> CST-1 < arg1. */
8127 if (code == LE_EXPR && sgn0 == 1)
8128 code = LT_EXPR;
8129 /* -CST < arg1 -> -CST-1 <= arg1. */
8130 else if (code == LT_EXPR && sgn0 == -1)
8131 code = LE_EXPR;
8132 /* CST > arg1 -> CST-1 >= arg1. */
8133 else if (code == GT_EXPR && sgn0 == 1)
8134 code = GE_EXPR;
8135 /* -CST >= arg1 -> -CST-1 > arg1. */
8136 else if (code == GE_EXPR && sgn0 == -1)
8137 code = GT_EXPR;
8138 else
8139 return NULL_TREE;
8140 /* arg1 code' CST' might be more canonical. */
8141 swap = true;
8142 }
8143 else
8144 {
8145 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8146 if (code == LT_EXPR
8147 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8148 code = LE_EXPR;
8149 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8150 else if (code == GT_EXPR
8151 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8152 code = GE_EXPR;
8153 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8154 else if (code == LE_EXPR
8155 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8156 code = LT_EXPR;
8157 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8158 else if (code == GE_EXPR
8159 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8160 code = GT_EXPR;
8161 else
8162 return NULL_TREE;
8163 *strict_overflow_p = true;
8164 }
8165
8166 /* Now build the constant reduced in magnitude. But not if that
8167 would produce one outside of its types range. */
8168 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8169 && ((sgn0 == 1
8170 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8171 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8172 || (sgn0 == -1
8173 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8174 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8175 /* We cannot swap the comparison here as that would cause us to
8176 endlessly recurse. */
8177 return NULL_TREE;
8178
8179 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8180 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8181 if (code0 != INTEGER_CST)
8182 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8183 t = fold_convert (TREE_TYPE (arg1), t);
8184
8185 /* If swapping might yield to a more canonical form, do so. */
8186 if (swap)
8187 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8188 else
8189 return fold_build2_loc (loc, code, type, t, arg1);
8190 }
8191
8192 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8193 overflow further. Try to decrease the magnitude of constants involved
8194 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8195 and put sole constants at the second argument position.
8196 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8197
8198 static tree
8199 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8200 tree arg0, tree arg1)
8201 {
8202 tree t;
8203 bool strict_overflow_p;
8204 const char * const warnmsg = G_("assuming signed overflow does not occur "
8205 "when reducing constant in comparison");
8206
8207 /* Try canonicalization by simplifying arg0. */
8208 strict_overflow_p = false;
8209 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8210 &strict_overflow_p);
8211 if (t)
8212 {
8213 if (strict_overflow_p)
8214 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8215 return t;
8216 }
8217
8218 /* Try canonicalization by simplifying arg1 using the swapped
8219 comparison. */
8220 code = swap_tree_comparison (code);
8221 strict_overflow_p = false;
8222 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8223 &strict_overflow_p);
8224 if (t && strict_overflow_p)
8225 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8226 return t;
8227 }
8228
8229 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8230 space. This is used to avoid issuing overflow warnings for
8231 expressions like &p->x which can not wrap. */
8232
8233 static bool
8234 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8235 {
8236 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8237 return true;
8238
8239 if (bitpos < 0)
8240 return true;
8241
8242 wide_int wi_offset;
8243 int precision = TYPE_PRECISION (TREE_TYPE (base));
8244 if (offset == NULL_TREE)
8245 wi_offset = wi::zero (precision);
8246 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8247 return true;
8248 else
8249 wi_offset = offset;
8250
8251 bool overflow;
8252 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8253 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8254 if (overflow)
8255 return true;
8256
8257 if (!wi::fits_uhwi_p (total))
8258 return true;
8259
8260 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8261 if (size <= 0)
8262 return true;
8263
8264 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8265 array. */
8266 if (TREE_CODE (base) == ADDR_EXPR)
8267 {
8268 HOST_WIDE_INT base_size;
8269
8270 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8271 if (base_size > 0 && size < base_size)
8272 size = base_size;
8273 }
8274
8275 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8276 }
8277
8278 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8279 kind INTEGER_CST. This makes sure to properly sign-extend the
8280 constant. */
8281
8282 static HOST_WIDE_INT
8283 size_low_cst (const_tree t)
8284 {
8285 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8286 int prec = TYPE_PRECISION (TREE_TYPE (t));
8287 if (prec < HOST_BITS_PER_WIDE_INT)
8288 return sext_hwi (w, prec);
8289 return w;
8290 }
8291
8292 /* Subroutine of fold_binary. This routine performs all of the
8293 transformations that are common to the equality/inequality
8294 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8295 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8296 fold_binary should call fold_binary. Fold a comparison with
8297 tree code CODE and type TYPE with operands OP0 and OP1. Return
8298 the folded comparison or NULL_TREE. */
8299
8300 static tree
8301 fold_comparison (location_t loc, enum tree_code code, tree type,
8302 tree op0, tree op1)
8303 {
8304 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8305 tree arg0, arg1, tem;
8306
8307 arg0 = op0;
8308 arg1 = op1;
8309
8310 STRIP_SIGN_NOPS (arg0);
8311 STRIP_SIGN_NOPS (arg1);
8312
8313 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8314 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8315 && (equality_code
8316 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8317 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8318 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8319 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8320 && TREE_CODE (arg1) == INTEGER_CST
8321 && !TREE_OVERFLOW (arg1))
8322 {
8323 const enum tree_code
8324 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8325 tree const1 = TREE_OPERAND (arg0, 1);
8326 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8327 tree variable = TREE_OPERAND (arg0, 0);
8328 tree new_const = int_const_binop (reverse_op, const2, const1);
8329
8330 /* If the constant operation overflowed this can be
8331 simplified as a comparison against INT_MAX/INT_MIN. */
8332 if (TREE_OVERFLOW (new_const)
8333 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8334 {
8335 int const1_sgn = tree_int_cst_sgn (const1);
8336 enum tree_code code2 = code;
8337
8338 /* Get the sign of the constant on the lhs if the
8339 operation were VARIABLE + CONST1. */
8340 if (TREE_CODE (arg0) == MINUS_EXPR)
8341 const1_sgn = -const1_sgn;
8342
8343 /* The sign of the constant determines if we overflowed
8344 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8345 Canonicalize to the INT_MIN overflow by swapping the comparison
8346 if necessary. */
8347 if (const1_sgn == -1)
8348 code2 = swap_tree_comparison (code);
8349
8350 /* We now can look at the canonicalized case
8351 VARIABLE + 1 CODE2 INT_MIN
8352 and decide on the result. */
8353 switch (code2)
8354 {
8355 case EQ_EXPR:
8356 case LT_EXPR:
8357 case LE_EXPR:
8358 return
8359 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8360
8361 case NE_EXPR:
8362 case GE_EXPR:
8363 case GT_EXPR:
8364 return
8365 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8366
8367 default:
8368 gcc_unreachable ();
8369 }
8370 }
8371 else
8372 {
8373 if (!equality_code)
8374 fold_overflow_warning ("assuming signed overflow does not occur "
8375 "when changing X +- C1 cmp C2 to "
8376 "X cmp C2 -+ C1",
8377 WARN_STRICT_OVERFLOW_COMPARISON);
8378 return fold_build2_loc (loc, code, type, variable, new_const);
8379 }
8380 }
8381
8382 /* For comparisons of pointers we can decompose it to a compile time
8383 comparison of the base objects and the offsets into the object.
8384 This requires at least one operand being an ADDR_EXPR or a
8385 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8386 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8387 && (TREE_CODE (arg0) == ADDR_EXPR
8388 || TREE_CODE (arg1) == ADDR_EXPR
8389 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8390 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8391 {
8392 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8393 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8394 machine_mode mode;
8395 int volatilep, unsignedp;
8396 bool indirect_base0 = false, indirect_base1 = false;
8397
8398 /* Get base and offset for the access. Strip ADDR_EXPR for
8399 get_inner_reference, but put it back by stripping INDIRECT_REF
8400 off the base object if possible. indirect_baseN will be true
8401 if baseN is not an address but refers to the object itself. */
8402 base0 = arg0;
8403 if (TREE_CODE (arg0) == ADDR_EXPR)
8404 {
8405 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8406 &bitsize, &bitpos0, &offset0, &mode,
8407 &unsignedp, &volatilep, false);
8408 if (TREE_CODE (base0) == INDIRECT_REF)
8409 base0 = TREE_OPERAND (base0, 0);
8410 else
8411 indirect_base0 = true;
8412 }
8413 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8414 {
8415 base0 = TREE_OPERAND (arg0, 0);
8416 STRIP_SIGN_NOPS (base0);
8417 if (TREE_CODE (base0) == ADDR_EXPR)
8418 {
8419 base0 = TREE_OPERAND (base0, 0);
8420 indirect_base0 = true;
8421 }
8422 offset0 = TREE_OPERAND (arg0, 1);
8423 if (tree_fits_shwi_p (offset0))
8424 {
8425 HOST_WIDE_INT off = size_low_cst (offset0);
8426 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8427 * BITS_PER_UNIT)
8428 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8429 {
8430 bitpos0 = off * BITS_PER_UNIT;
8431 offset0 = NULL_TREE;
8432 }
8433 }
8434 }
8435
8436 base1 = arg1;
8437 if (TREE_CODE (arg1) == ADDR_EXPR)
8438 {
8439 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8440 &bitsize, &bitpos1, &offset1, &mode,
8441 &unsignedp, &volatilep, false);
8442 if (TREE_CODE (base1) == INDIRECT_REF)
8443 base1 = TREE_OPERAND (base1, 0);
8444 else
8445 indirect_base1 = true;
8446 }
8447 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8448 {
8449 base1 = TREE_OPERAND (arg1, 0);
8450 STRIP_SIGN_NOPS (base1);
8451 if (TREE_CODE (base1) == ADDR_EXPR)
8452 {
8453 base1 = TREE_OPERAND (base1, 0);
8454 indirect_base1 = true;
8455 }
8456 offset1 = TREE_OPERAND (arg1, 1);
8457 if (tree_fits_shwi_p (offset1))
8458 {
8459 HOST_WIDE_INT off = size_low_cst (offset1);
8460 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8461 * BITS_PER_UNIT)
8462 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8463 {
8464 bitpos1 = off * BITS_PER_UNIT;
8465 offset1 = NULL_TREE;
8466 }
8467 }
8468 }
8469
8470 /* A local variable can never be pointed to by
8471 the default SSA name of an incoming parameter. */
8472 if ((TREE_CODE (arg0) == ADDR_EXPR
8473 && indirect_base0
8474 && TREE_CODE (base0) == VAR_DECL
8475 && auto_var_in_fn_p (base0, current_function_decl)
8476 && !indirect_base1
8477 && TREE_CODE (base1) == SSA_NAME
8478 && SSA_NAME_IS_DEFAULT_DEF (base1)
8479 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
8480 || (TREE_CODE (arg1) == ADDR_EXPR
8481 && indirect_base1
8482 && TREE_CODE (base1) == VAR_DECL
8483 && auto_var_in_fn_p (base1, current_function_decl)
8484 && !indirect_base0
8485 && TREE_CODE (base0) == SSA_NAME
8486 && SSA_NAME_IS_DEFAULT_DEF (base0)
8487 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
8488 {
8489 if (code == NE_EXPR)
8490 return constant_boolean_node (1, type);
8491 else if (code == EQ_EXPR)
8492 return constant_boolean_node (0, type);
8493 }
8494 /* If we have equivalent bases we might be able to simplify. */
8495 else if (indirect_base0 == indirect_base1
8496 && operand_equal_p (base0, base1, 0))
8497 {
8498 /* We can fold this expression to a constant if the non-constant
8499 offset parts are equal. */
8500 if ((offset0 == offset1
8501 || (offset0 && offset1
8502 && operand_equal_p (offset0, offset1, 0)))
8503 && (code == EQ_EXPR
8504 || code == NE_EXPR
8505 || (indirect_base0 && DECL_P (base0))
8506 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8507
8508 {
8509 if (!equality_code
8510 && bitpos0 != bitpos1
8511 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8512 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8513 fold_overflow_warning (("assuming pointer wraparound does not "
8514 "occur when comparing P +- C1 with "
8515 "P +- C2"),
8516 WARN_STRICT_OVERFLOW_CONDITIONAL);
8517
8518 switch (code)
8519 {
8520 case EQ_EXPR:
8521 return constant_boolean_node (bitpos0 == bitpos1, type);
8522 case NE_EXPR:
8523 return constant_boolean_node (bitpos0 != bitpos1, type);
8524 case LT_EXPR:
8525 return constant_boolean_node (bitpos0 < bitpos1, type);
8526 case LE_EXPR:
8527 return constant_boolean_node (bitpos0 <= bitpos1, type);
8528 case GE_EXPR:
8529 return constant_boolean_node (bitpos0 >= bitpos1, type);
8530 case GT_EXPR:
8531 return constant_boolean_node (bitpos0 > bitpos1, type);
8532 default:;
8533 }
8534 }
8535 /* We can simplify the comparison to a comparison of the variable
8536 offset parts if the constant offset parts are equal.
8537 Be careful to use signed sizetype here because otherwise we
8538 mess with array offsets in the wrong way. This is possible
8539 because pointer arithmetic is restricted to retain within an
8540 object and overflow on pointer differences is undefined as of
8541 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8542 else if (bitpos0 == bitpos1
8543 && (equality_code
8544 || (indirect_base0 && DECL_P (base0))
8545 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8546 {
8547 /* By converting to signed sizetype we cover middle-end pointer
8548 arithmetic which operates on unsigned pointer types of size
8549 type size and ARRAY_REF offsets which are properly sign or
8550 zero extended from their type in case it is narrower than
8551 sizetype. */
8552 if (offset0 == NULL_TREE)
8553 offset0 = build_int_cst (ssizetype, 0);
8554 else
8555 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8556 if (offset1 == NULL_TREE)
8557 offset1 = build_int_cst (ssizetype, 0);
8558 else
8559 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8560
8561 if (!equality_code
8562 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8563 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8564 fold_overflow_warning (("assuming pointer wraparound does not "
8565 "occur when comparing P +- C1 with "
8566 "P +- C2"),
8567 WARN_STRICT_OVERFLOW_COMPARISON);
8568
8569 return fold_build2_loc (loc, code, type, offset0, offset1);
8570 }
8571 }
8572 /* For non-equal bases we can simplify if they are addresses
8573 declarations with different addresses. */
8574 else if (indirect_base0 && indirect_base1
8575 /* We know that !operand_equal_p (base0, base1, 0)
8576 because the if condition was false. But make
8577 sure two decls are not the same. */
8578 && base0 != base1
8579 && TREE_CODE (arg0) == ADDR_EXPR
8580 && TREE_CODE (arg1) == ADDR_EXPR
8581 && DECL_P (base0)
8582 && DECL_P (base1)
8583 /* Watch for aliases. */
8584 && (!decl_in_symtab_p (base0)
8585 || !decl_in_symtab_p (base1)
8586 || !symtab_node::get_create (base0)->equal_address_to
8587 (symtab_node::get_create (base1))))
8588 {
8589 if (code == EQ_EXPR)
8590 return omit_two_operands_loc (loc, type, boolean_false_node,
8591 arg0, arg1);
8592 else if (code == NE_EXPR)
8593 return omit_two_operands_loc (loc, type, boolean_true_node,
8594 arg0, arg1);
8595 }
8596 /* For equal offsets we can simplify to a comparison of the
8597 base addresses. */
8598 else if (bitpos0 == bitpos1
8599 && (indirect_base0
8600 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8601 && (indirect_base1
8602 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8603 && ((offset0 == offset1)
8604 || (offset0 && offset1
8605 && operand_equal_p (offset0, offset1, 0))))
8606 {
8607 if (indirect_base0)
8608 base0 = build_fold_addr_expr_loc (loc, base0);
8609 if (indirect_base1)
8610 base1 = build_fold_addr_expr_loc (loc, base1);
8611 return fold_build2_loc (loc, code, type, base0, base1);
8612 }
8613 }
8614
8615 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8616 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8617 the resulting offset is smaller in absolute value than the
8618 original one and has the same sign. */
8619 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8620 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8621 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8622 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8623 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8624 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8625 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8626 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8627 {
8628 tree const1 = TREE_OPERAND (arg0, 1);
8629 tree const2 = TREE_OPERAND (arg1, 1);
8630 tree variable1 = TREE_OPERAND (arg0, 0);
8631 tree variable2 = TREE_OPERAND (arg1, 0);
8632 tree cst;
8633 const char * const warnmsg = G_("assuming signed overflow does not "
8634 "occur when combining constants around "
8635 "a comparison");
8636
8637 /* Put the constant on the side where it doesn't overflow and is
8638 of lower absolute value and of same sign than before. */
8639 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8640 ? MINUS_EXPR : PLUS_EXPR,
8641 const2, const1);
8642 if (!TREE_OVERFLOW (cst)
8643 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8644 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8645 {
8646 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8647 return fold_build2_loc (loc, code, type,
8648 variable1,
8649 fold_build2_loc (loc, TREE_CODE (arg1),
8650 TREE_TYPE (arg1),
8651 variable2, cst));
8652 }
8653
8654 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8655 ? MINUS_EXPR : PLUS_EXPR,
8656 const1, const2);
8657 if (!TREE_OVERFLOW (cst)
8658 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8659 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8660 {
8661 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8662 return fold_build2_loc (loc, code, type,
8663 fold_build2_loc (loc, TREE_CODE (arg0),
8664 TREE_TYPE (arg0),
8665 variable1, cst),
8666 variable2);
8667 }
8668 }
8669
8670 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8671 if (tem)
8672 return tem;
8673
8674 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8675 constant, we can simplify it. */
8676 if (TREE_CODE (arg1) == INTEGER_CST
8677 && (TREE_CODE (arg0) == MIN_EXPR
8678 || TREE_CODE (arg0) == MAX_EXPR)
8679 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8680 {
8681 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8682 if (tem)
8683 return tem;
8684 }
8685
8686 /* If we are comparing an expression that just has comparisons
8687 of two integer values, arithmetic expressions of those comparisons,
8688 and constants, we can simplify it. There are only three cases
8689 to check: the two values can either be equal, the first can be
8690 greater, or the second can be greater. Fold the expression for
8691 those three values. Since each value must be 0 or 1, we have
8692 eight possibilities, each of which corresponds to the constant 0
8693 or 1 or one of the six possible comparisons.
8694
8695 This handles common cases like (a > b) == 0 but also handles
8696 expressions like ((x > y) - (y > x)) > 0, which supposedly
8697 occur in macroized code. */
8698
8699 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8700 {
8701 tree cval1 = 0, cval2 = 0;
8702 int save_p = 0;
8703
8704 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8705 /* Don't handle degenerate cases here; they should already
8706 have been handled anyway. */
8707 && cval1 != 0 && cval2 != 0
8708 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8709 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8710 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8711 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8712 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8713 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8714 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8715 {
8716 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8717 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8718
8719 /* We can't just pass T to eval_subst in case cval1 or cval2
8720 was the same as ARG1. */
8721
8722 tree high_result
8723 = fold_build2_loc (loc, code, type,
8724 eval_subst (loc, arg0, cval1, maxval,
8725 cval2, minval),
8726 arg1);
8727 tree equal_result
8728 = fold_build2_loc (loc, code, type,
8729 eval_subst (loc, arg0, cval1, maxval,
8730 cval2, maxval),
8731 arg1);
8732 tree low_result
8733 = fold_build2_loc (loc, code, type,
8734 eval_subst (loc, arg0, cval1, minval,
8735 cval2, maxval),
8736 arg1);
8737
8738 /* All three of these results should be 0 or 1. Confirm they are.
8739 Then use those values to select the proper code to use. */
8740
8741 if (TREE_CODE (high_result) == INTEGER_CST
8742 && TREE_CODE (equal_result) == INTEGER_CST
8743 && TREE_CODE (low_result) == INTEGER_CST)
8744 {
8745 /* Make a 3-bit mask with the high-order bit being the
8746 value for `>', the next for '=', and the low for '<'. */
8747 switch ((integer_onep (high_result) * 4)
8748 + (integer_onep (equal_result) * 2)
8749 + integer_onep (low_result))
8750 {
8751 case 0:
8752 /* Always false. */
8753 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8754 case 1:
8755 code = LT_EXPR;
8756 break;
8757 case 2:
8758 code = EQ_EXPR;
8759 break;
8760 case 3:
8761 code = LE_EXPR;
8762 break;
8763 case 4:
8764 code = GT_EXPR;
8765 break;
8766 case 5:
8767 code = NE_EXPR;
8768 break;
8769 case 6:
8770 code = GE_EXPR;
8771 break;
8772 case 7:
8773 /* Always true. */
8774 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8775 }
8776
8777 if (save_p)
8778 {
8779 tem = save_expr (build2 (code, type, cval1, cval2));
8780 SET_EXPR_LOCATION (tem, loc);
8781 return tem;
8782 }
8783 return fold_build2_loc (loc, code, type, cval1, cval2);
8784 }
8785 }
8786 }
8787
8788 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8789 into a single range test. */
8790 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8791 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8792 && TREE_CODE (arg1) == INTEGER_CST
8793 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8794 && !integer_zerop (TREE_OPERAND (arg0, 1))
8795 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8796 && !TREE_OVERFLOW (arg1))
8797 {
8798 tem = fold_div_compare (loc, code, type, arg0, arg1);
8799 if (tem != NULL_TREE)
8800 return tem;
8801 }
8802
8803 return NULL_TREE;
8804 }
8805
8806
8807 /* Subroutine of fold_binary. Optimize complex multiplications of the
8808 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8809 argument EXPR represents the expression "z" of type TYPE. */
8810
8811 static tree
8812 fold_mult_zconjz (location_t loc, tree type, tree expr)
8813 {
8814 tree itype = TREE_TYPE (type);
8815 tree rpart, ipart, tem;
8816
8817 if (TREE_CODE (expr) == COMPLEX_EXPR)
8818 {
8819 rpart = TREE_OPERAND (expr, 0);
8820 ipart = TREE_OPERAND (expr, 1);
8821 }
8822 else if (TREE_CODE (expr) == COMPLEX_CST)
8823 {
8824 rpart = TREE_REALPART (expr);
8825 ipart = TREE_IMAGPART (expr);
8826 }
8827 else
8828 {
8829 expr = save_expr (expr);
8830 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8831 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8832 }
8833
8834 rpart = save_expr (rpart);
8835 ipart = save_expr (ipart);
8836 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8837 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8838 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8839 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8840 build_zero_cst (itype));
8841 }
8842
8843
8844 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8845 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8846
8847 static bool
8848 vec_cst_ctor_to_array (tree arg, tree *elts)
8849 {
8850 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8851
8852 if (TREE_CODE (arg) == VECTOR_CST)
8853 {
8854 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8855 elts[i] = VECTOR_CST_ELT (arg, i);
8856 }
8857 else if (TREE_CODE (arg) == CONSTRUCTOR)
8858 {
8859 constructor_elt *elt;
8860
8861 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8862 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8863 return false;
8864 else
8865 elts[i] = elt->value;
8866 }
8867 else
8868 return false;
8869 for (; i < nelts; i++)
8870 elts[i]
8871 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8872 return true;
8873 }
8874
8875 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8876 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8877 NULL_TREE otherwise. */
8878
8879 static tree
8880 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8881 {
8882 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8883 tree *elts;
8884 bool need_ctor = false;
8885
8886 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8887 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8888 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8889 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8890 return NULL_TREE;
8891
8892 elts = XALLOCAVEC (tree, nelts * 3);
8893 if (!vec_cst_ctor_to_array (arg0, elts)
8894 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8895 return NULL_TREE;
8896
8897 for (i = 0; i < nelts; i++)
8898 {
8899 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8900 need_ctor = true;
8901 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8902 }
8903
8904 if (need_ctor)
8905 {
8906 vec<constructor_elt, va_gc> *v;
8907 vec_alloc (v, nelts);
8908 for (i = 0; i < nelts; i++)
8909 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8910 return build_constructor (type, v);
8911 }
8912 else
8913 return build_vector (type, &elts[2 * nelts]);
8914 }
8915
8916 /* Try to fold a pointer difference of type TYPE two address expressions of
8917 array references AREF0 and AREF1 using location LOC. Return a
8918 simplified expression for the difference or NULL_TREE. */
8919
8920 static tree
8921 fold_addr_of_array_ref_difference (location_t loc, tree type,
8922 tree aref0, tree aref1)
8923 {
8924 tree base0 = TREE_OPERAND (aref0, 0);
8925 tree base1 = TREE_OPERAND (aref1, 0);
8926 tree base_offset = build_int_cst (type, 0);
8927
8928 /* If the bases are array references as well, recurse. If the bases
8929 are pointer indirections compute the difference of the pointers.
8930 If the bases are equal, we are set. */
8931 if ((TREE_CODE (base0) == ARRAY_REF
8932 && TREE_CODE (base1) == ARRAY_REF
8933 && (base_offset
8934 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8935 || (INDIRECT_REF_P (base0)
8936 && INDIRECT_REF_P (base1)
8937 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
8938 TREE_OPERAND (base0, 0),
8939 TREE_OPERAND (base1, 0))))
8940 || operand_equal_p (base0, base1, 0))
8941 {
8942 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8943 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8944 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8945 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8946 return fold_build2_loc (loc, PLUS_EXPR, type,
8947 base_offset,
8948 fold_build2_loc (loc, MULT_EXPR, type,
8949 diff, esz));
8950 }
8951 return NULL_TREE;
8952 }
8953
8954 /* If the real or vector real constant CST of type TYPE has an exact
8955 inverse, return it, else return NULL. */
8956
8957 tree
8958 exact_inverse (tree type, tree cst)
8959 {
8960 REAL_VALUE_TYPE r;
8961 tree unit_type, *elts;
8962 machine_mode mode;
8963 unsigned vec_nelts, i;
8964
8965 switch (TREE_CODE (cst))
8966 {
8967 case REAL_CST:
8968 r = TREE_REAL_CST (cst);
8969
8970 if (exact_real_inverse (TYPE_MODE (type), &r))
8971 return build_real (type, r);
8972
8973 return NULL_TREE;
8974
8975 case VECTOR_CST:
8976 vec_nelts = VECTOR_CST_NELTS (cst);
8977 elts = XALLOCAVEC (tree, vec_nelts);
8978 unit_type = TREE_TYPE (type);
8979 mode = TYPE_MODE (unit_type);
8980
8981 for (i = 0; i < vec_nelts; i++)
8982 {
8983 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8984 if (!exact_real_inverse (mode, &r))
8985 return NULL_TREE;
8986 elts[i] = build_real (unit_type, r);
8987 }
8988
8989 return build_vector (type, elts);
8990
8991 default:
8992 return NULL_TREE;
8993 }
8994 }
8995
8996 /* Mask out the tz least significant bits of X of type TYPE where
8997 tz is the number of trailing zeroes in Y. */
8998 static wide_int
8999 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9000 {
9001 int tz = wi::ctz (y);
9002 if (tz > 0)
9003 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9004 return x;
9005 }
9006
9007 /* Return true when T is an address and is known to be nonzero.
9008 For floating point we further ensure that T is not denormal.
9009 Similar logic is present in nonzero_address in rtlanal.h.
9010
9011 If the return value is based on the assumption that signed overflow
9012 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9013 change *STRICT_OVERFLOW_P. */
9014
9015 static bool
9016 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9017 {
9018 tree type = TREE_TYPE (t);
9019 enum tree_code code;
9020
9021 /* Doing something useful for floating point would need more work. */
9022 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9023 return false;
9024
9025 code = TREE_CODE (t);
9026 switch (TREE_CODE_CLASS (code))
9027 {
9028 case tcc_unary:
9029 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9030 strict_overflow_p);
9031 case tcc_binary:
9032 case tcc_comparison:
9033 return tree_binary_nonzero_warnv_p (code, type,
9034 TREE_OPERAND (t, 0),
9035 TREE_OPERAND (t, 1),
9036 strict_overflow_p);
9037 case tcc_constant:
9038 case tcc_declaration:
9039 case tcc_reference:
9040 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9041
9042 default:
9043 break;
9044 }
9045
9046 switch (code)
9047 {
9048 case TRUTH_NOT_EXPR:
9049 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9050 strict_overflow_p);
9051
9052 case TRUTH_AND_EXPR:
9053 case TRUTH_OR_EXPR:
9054 case TRUTH_XOR_EXPR:
9055 return tree_binary_nonzero_warnv_p (code, type,
9056 TREE_OPERAND (t, 0),
9057 TREE_OPERAND (t, 1),
9058 strict_overflow_p);
9059
9060 case COND_EXPR:
9061 case CONSTRUCTOR:
9062 case OBJ_TYPE_REF:
9063 case ASSERT_EXPR:
9064 case ADDR_EXPR:
9065 case WITH_SIZE_EXPR:
9066 case SSA_NAME:
9067 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9068
9069 case COMPOUND_EXPR:
9070 case MODIFY_EXPR:
9071 case BIND_EXPR:
9072 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9073 strict_overflow_p);
9074
9075 case SAVE_EXPR:
9076 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9077 strict_overflow_p);
9078
9079 case CALL_EXPR:
9080 {
9081 tree fndecl = get_callee_fndecl (t);
9082 if (!fndecl) return false;
9083 if (flag_delete_null_pointer_checks && !flag_check_new
9084 && DECL_IS_OPERATOR_NEW (fndecl)
9085 && !TREE_NOTHROW (fndecl))
9086 return true;
9087 if (flag_delete_null_pointer_checks
9088 && lookup_attribute ("returns_nonnull",
9089 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9090 return true;
9091 return alloca_call_p (t);
9092 }
9093
9094 default:
9095 break;
9096 }
9097 return false;
9098 }
9099
9100 /* Return true when T is an address and is known to be nonzero.
9101 Handle warnings about undefined signed overflow. */
9102
9103 static bool
9104 tree_expr_nonzero_p (tree t)
9105 {
9106 bool ret, strict_overflow_p;
9107
9108 strict_overflow_p = false;
9109 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9110 if (strict_overflow_p)
9111 fold_overflow_warning (("assuming signed overflow does not occur when "
9112 "determining that expression is always "
9113 "non-zero"),
9114 WARN_STRICT_OVERFLOW_MISC);
9115 return ret;
9116 }
9117
9118 /* Fold a binary expression of code CODE and type TYPE with operands
9119 OP0 and OP1. LOC is the location of the resulting expression.
9120 Return the folded expression if folding is successful. Otherwise,
9121 return NULL_TREE. */
9122
9123 tree
9124 fold_binary_loc (location_t loc,
9125 enum tree_code code, tree type, tree op0, tree op1)
9126 {
9127 enum tree_code_class kind = TREE_CODE_CLASS (code);
9128 tree arg0, arg1, tem;
9129 tree t1 = NULL_TREE;
9130 bool strict_overflow_p;
9131 unsigned int prec;
9132
9133 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9134 && TREE_CODE_LENGTH (code) == 2
9135 && op0 != NULL_TREE
9136 && op1 != NULL_TREE);
9137
9138 arg0 = op0;
9139 arg1 = op1;
9140
9141 /* Strip any conversions that don't change the mode. This is
9142 safe for every expression, except for a comparison expression
9143 because its signedness is derived from its operands. So, in
9144 the latter case, only strip conversions that don't change the
9145 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9146 preserved.
9147
9148 Note that this is done as an internal manipulation within the
9149 constant folder, in order to find the simplest representation
9150 of the arguments so that their form can be studied. In any
9151 cases, the appropriate type conversions should be put back in
9152 the tree that will get out of the constant folder. */
9153
9154 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9155 {
9156 STRIP_SIGN_NOPS (arg0);
9157 STRIP_SIGN_NOPS (arg1);
9158 }
9159 else
9160 {
9161 STRIP_NOPS (arg0);
9162 STRIP_NOPS (arg1);
9163 }
9164
9165 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9166 constant but we can't do arithmetic on them. */
9167 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9168 {
9169 tem = const_binop (code, type, arg0, arg1);
9170 if (tem != NULL_TREE)
9171 {
9172 if (TREE_TYPE (tem) != type)
9173 tem = fold_convert_loc (loc, type, tem);
9174 return tem;
9175 }
9176 }
9177
9178 /* If this is a commutative operation, and ARG0 is a constant, move it
9179 to ARG1 to reduce the number of tests below. */
9180 if (commutative_tree_code (code)
9181 && tree_swap_operands_p (arg0, arg1, true))
9182 return fold_build2_loc (loc, code, type, op1, op0);
9183
9184 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9185 to ARG1 to reduce the number of tests below. */
9186 if (kind == tcc_comparison
9187 && tree_swap_operands_p (arg0, arg1, true))
9188 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9189
9190 tem = generic_simplify (loc, code, type, op0, op1);
9191 if (tem)
9192 return tem;
9193
9194 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9195
9196 First check for cases where an arithmetic operation is applied to a
9197 compound, conditional, or comparison operation. Push the arithmetic
9198 operation inside the compound or conditional to see if any folding
9199 can then be done. Convert comparison to conditional for this purpose.
9200 The also optimizes non-constant cases that used to be done in
9201 expand_expr.
9202
9203 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9204 one of the operands is a comparison and the other is a comparison, a
9205 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9206 code below would make the expression more complex. Change it to a
9207 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9208 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9209
9210 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9211 || code == EQ_EXPR || code == NE_EXPR)
9212 && TREE_CODE (type) != VECTOR_TYPE
9213 && ((truth_value_p (TREE_CODE (arg0))
9214 && (truth_value_p (TREE_CODE (arg1))
9215 || (TREE_CODE (arg1) == BIT_AND_EXPR
9216 && integer_onep (TREE_OPERAND (arg1, 1)))))
9217 || (truth_value_p (TREE_CODE (arg1))
9218 && (truth_value_p (TREE_CODE (arg0))
9219 || (TREE_CODE (arg0) == BIT_AND_EXPR
9220 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9221 {
9222 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9223 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9224 : TRUTH_XOR_EXPR,
9225 boolean_type_node,
9226 fold_convert_loc (loc, boolean_type_node, arg0),
9227 fold_convert_loc (loc, boolean_type_node, arg1));
9228
9229 if (code == EQ_EXPR)
9230 tem = invert_truthvalue_loc (loc, tem);
9231
9232 return fold_convert_loc (loc, type, tem);
9233 }
9234
9235 if (TREE_CODE_CLASS (code) == tcc_binary
9236 || TREE_CODE_CLASS (code) == tcc_comparison)
9237 {
9238 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9239 {
9240 tem = fold_build2_loc (loc, code, type,
9241 fold_convert_loc (loc, TREE_TYPE (op0),
9242 TREE_OPERAND (arg0, 1)), op1);
9243 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9244 tem);
9245 }
9246 if (TREE_CODE (arg1) == COMPOUND_EXPR
9247 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9248 {
9249 tem = fold_build2_loc (loc, code, type, op0,
9250 fold_convert_loc (loc, TREE_TYPE (op1),
9251 TREE_OPERAND (arg1, 1)));
9252 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9253 tem);
9254 }
9255
9256 if (TREE_CODE (arg0) == COND_EXPR
9257 || TREE_CODE (arg0) == VEC_COND_EXPR
9258 || COMPARISON_CLASS_P (arg0))
9259 {
9260 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9261 arg0, arg1,
9262 /*cond_first_p=*/1);
9263 if (tem != NULL_TREE)
9264 return tem;
9265 }
9266
9267 if (TREE_CODE (arg1) == COND_EXPR
9268 || TREE_CODE (arg1) == VEC_COND_EXPR
9269 || COMPARISON_CLASS_P (arg1))
9270 {
9271 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9272 arg1, arg0,
9273 /*cond_first_p=*/0);
9274 if (tem != NULL_TREE)
9275 return tem;
9276 }
9277 }
9278
9279 switch (code)
9280 {
9281 case MEM_REF:
9282 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9283 if (TREE_CODE (arg0) == ADDR_EXPR
9284 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9285 {
9286 tree iref = TREE_OPERAND (arg0, 0);
9287 return fold_build2 (MEM_REF, type,
9288 TREE_OPERAND (iref, 0),
9289 int_const_binop (PLUS_EXPR, arg1,
9290 TREE_OPERAND (iref, 1)));
9291 }
9292
9293 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9294 if (TREE_CODE (arg0) == ADDR_EXPR
9295 && handled_component_p (TREE_OPERAND (arg0, 0)))
9296 {
9297 tree base;
9298 HOST_WIDE_INT coffset;
9299 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9300 &coffset);
9301 if (!base)
9302 return NULL_TREE;
9303 return fold_build2 (MEM_REF, type,
9304 build_fold_addr_expr (base),
9305 int_const_binop (PLUS_EXPR, arg1,
9306 size_int (coffset)));
9307 }
9308
9309 return NULL_TREE;
9310
9311 case POINTER_PLUS_EXPR:
9312 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9313 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9314 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9315 return fold_convert_loc (loc, type,
9316 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9317 fold_convert_loc (loc, sizetype,
9318 arg1),
9319 fold_convert_loc (loc, sizetype,
9320 arg0)));
9321
9322 return NULL_TREE;
9323
9324 case PLUS_EXPR:
9325 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9326 {
9327 /* X + (X / CST) * -CST is X % CST. */
9328 if (TREE_CODE (arg1) == MULT_EXPR
9329 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9330 && operand_equal_p (arg0,
9331 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9332 {
9333 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9334 tree cst1 = TREE_OPERAND (arg1, 1);
9335 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9336 cst1, cst0);
9337 if (sum && integer_zerop (sum))
9338 return fold_convert_loc (loc, type,
9339 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9340 TREE_TYPE (arg0), arg0,
9341 cst0));
9342 }
9343 }
9344
9345 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9346 one. Make sure the type is not saturating and has the signedness of
9347 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9348 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9349 if ((TREE_CODE (arg0) == MULT_EXPR
9350 || TREE_CODE (arg1) == MULT_EXPR)
9351 && !TYPE_SATURATING (type)
9352 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9353 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9354 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9355 {
9356 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9357 if (tem)
9358 return tem;
9359 }
9360
9361 if (! FLOAT_TYPE_P (type))
9362 {
9363 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9364 (plus (plus (mult) (mult)) (foo)) so that we can
9365 take advantage of the factoring cases below. */
9366 if (ANY_INTEGRAL_TYPE_P (type)
9367 && TYPE_OVERFLOW_WRAPS (type)
9368 && (((TREE_CODE (arg0) == PLUS_EXPR
9369 || TREE_CODE (arg0) == MINUS_EXPR)
9370 && TREE_CODE (arg1) == MULT_EXPR)
9371 || ((TREE_CODE (arg1) == PLUS_EXPR
9372 || TREE_CODE (arg1) == MINUS_EXPR)
9373 && TREE_CODE (arg0) == MULT_EXPR)))
9374 {
9375 tree parg0, parg1, parg, marg;
9376 enum tree_code pcode;
9377
9378 if (TREE_CODE (arg1) == MULT_EXPR)
9379 parg = arg0, marg = arg1;
9380 else
9381 parg = arg1, marg = arg0;
9382 pcode = TREE_CODE (parg);
9383 parg0 = TREE_OPERAND (parg, 0);
9384 parg1 = TREE_OPERAND (parg, 1);
9385 STRIP_NOPS (parg0);
9386 STRIP_NOPS (parg1);
9387
9388 if (TREE_CODE (parg0) == MULT_EXPR
9389 && TREE_CODE (parg1) != MULT_EXPR)
9390 return fold_build2_loc (loc, pcode, type,
9391 fold_build2_loc (loc, PLUS_EXPR, type,
9392 fold_convert_loc (loc, type,
9393 parg0),
9394 fold_convert_loc (loc, type,
9395 marg)),
9396 fold_convert_loc (loc, type, parg1));
9397 if (TREE_CODE (parg0) != MULT_EXPR
9398 && TREE_CODE (parg1) == MULT_EXPR)
9399 return
9400 fold_build2_loc (loc, PLUS_EXPR, type,
9401 fold_convert_loc (loc, type, parg0),
9402 fold_build2_loc (loc, pcode, type,
9403 fold_convert_loc (loc, type, marg),
9404 fold_convert_loc (loc, type,
9405 parg1)));
9406 }
9407 }
9408 else
9409 {
9410 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9411 to __complex__ ( x, y ). This is not the same for SNaNs or
9412 if signed zeros are involved. */
9413 if (!HONOR_SNANS (element_mode (arg0))
9414 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9415 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9416 {
9417 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9418 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9419 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9420 bool arg0rz = false, arg0iz = false;
9421 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9422 || (arg0i && (arg0iz = real_zerop (arg0i))))
9423 {
9424 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9425 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9426 if (arg0rz && arg1i && real_zerop (arg1i))
9427 {
9428 tree rp = arg1r ? arg1r
9429 : build1 (REALPART_EXPR, rtype, arg1);
9430 tree ip = arg0i ? arg0i
9431 : build1 (IMAGPART_EXPR, rtype, arg0);
9432 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9433 }
9434 else if (arg0iz && arg1r && real_zerop (arg1r))
9435 {
9436 tree rp = arg0r ? arg0r
9437 : build1 (REALPART_EXPR, rtype, arg0);
9438 tree ip = arg1i ? arg1i
9439 : build1 (IMAGPART_EXPR, rtype, arg1);
9440 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9441 }
9442 }
9443 }
9444
9445 if (flag_unsafe_math_optimizations
9446 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9447 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9448 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9449 return tem;
9450
9451 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9452 We associate floats only if the user has specified
9453 -fassociative-math. */
9454 if (flag_associative_math
9455 && TREE_CODE (arg1) == PLUS_EXPR
9456 && TREE_CODE (arg0) != MULT_EXPR)
9457 {
9458 tree tree10 = TREE_OPERAND (arg1, 0);
9459 tree tree11 = TREE_OPERAND (arg1, 1);
9460 if (TREE_CODE (tree11) == MULT_EXPR
9461 && TREE_CODE (tree10) == MULT_EXPR)
9462 {
9463 tree tree0;
9464 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9465 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9466 }
9467 }
9468 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9469 We associate floats only if the user has specified
9470 -fassociative-math. */
9471 if (flag_associative_math
9472 && TREE_CODE (arg0) == PLUS_EXPR
9473 && TREE_CODE (arg1) != MULT_EXPR)
9474 {
9475 tree tree00 = TREE_OPERAND (arg0, 0);
9476 tree tree01 = TREE_OPERAND (arg0, 1);
9477 if (TREE_CODE (tree01) == MULT_EXPR
9478 && TREE_CODE (tree00) == MULT_EXPR)
9479 {
9480 tree tree0;
9481 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9482 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9483 }
9484 }
9485 }
9486
9487 bit_rotate:
9488 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9489 is a rotate of A by C1 bits. */
9490 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9491 is a rotate of A by B bits. */
9492 {
9493 enum tree_code code0, code1;
9494 tree rtype;
9495 code0 = TREE_CODE (arg0);
9496 code1 = TREE_CODE (arg1);
9497 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9498 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9499 && operand_equal_p (TREE_OPERAND (arg0, 0),
9500 TREE_OPERAND (arg1, 0), 0)
9501 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9502 TYPE_UNSIGNED (rtype))
9503 /* Only create rotates in complete modes. Other cases are not
9504 expanded properly. */
9505 && (element_precision (rtype)
9506 == element_precision (TYPE_MODE (rtype))))
9507 {
9508 tree tree01, tree11;
9509 enum tree_code code01, code11;
9510
9511 tree01 = TREE_OPERAND (arg0, 1);
9512 tree11 = TREE_OPERAND (arg1, 1);
9513 STRIP_NOPS (tree01);
9514 STRIP_NOPS (tree11);
9515 code01 = TREE_CODE (tree01);
9516 code11 = TREE_CODE (tree11);
9517 if (code01 == INTEGER_CST
9518 && code11 == INTEGER_CST
9519 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9520 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9521 {
9522 tem = build2_loc (loc, LROTATE_EXPR,
9523 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9524 TREE_OPERAND (arg0, 0),
9525 code0 == LSHIFT_EXPR
9526 ? TREE_OPERAND (arg0, 1)
9527 : TREE_OPERAND (arg1, 1));
9528 return fold_convert_loc (loc, type, tem);
9529 }
9530 else if (code11 == MINUS_EXPR)
9531 {
9532 tree tree110, tree111;
9533 tree110 = TREE_OPERAND (tree11, 0);
9534 tree111 = TREE_OPERAND (tree11, 1);
9535 STRIP_NOPS (tree110);
9536 STRIP_NOPS (tree111);
9537 if (TREE_CODE (tree110) == INTEGER_CST
9538 && 0 == compare_tree_int (tree110,
9539 element_precision
9540 (TREE_TYPE (TREE_OPERAND
9541 (arg0, 0))))
9542 && operand_equal_p (tree01, tree111, 0))
9543 return
9544 fold_convert_loc (loc, type,
9545 build2 ((code0 == LSHIFT_EXPR
9546 ? LROTATE_EXPR
9547 : RROTATE_EXPR),
9548 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9549 TREE_OPERAND (arg0, 0),
9550 TREE_OPERAND (arg0, 1)));
9551 }
9552 else if (code01 == MINUS_EXPR)
9553 {
9554 tree tree010, tree011;
9555 tree010 = TREE_OPERAND (tree01, 0);
9556 tree011 = TREE_OPERAND (tree01, 1);
9557 STRIP_NOPS (tree010);
9558 STRIP_NOPS (tree011);
9559 if (TREE_CODE (tree010) == INTEGER_CST
9560 && 0 == compare_tree_int (tree010,
9561 element_precision
9562 (TREE_TYPE (TREE_OPERAND
9563 (arg0, 0))))
9564 && operand_equal_p (tree11, tree011, 0))
9565 return fold_convert_loc
9566 (loc, type,
9567 build2 ((code0 != LSHIFT_EXPR
9568 ? LROTATE_EXPR
9569 : RROTATE_EXPR),
9570 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9571 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9572 }
9573 }
9574 }
9575
9576 associate:
9577 /* In most languages, can't associate operations on floats through
9578 parentheses. Rather than remember where the parentheses were, we
9579 don't associate floats at all, unless the user has specified
9580 -fassociative-math.
9581 And, we need to make sure type is not saturating. */
9582
9583 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9584 && !TYPE_SATURATING (type))
9585 {
9586 tree var0, con0, lit0, minus_lit0;
9587 tree var1, con1, lit1, minus_lit1;
9588 tree atype = type;
9589 bool ok = true;
9590
9591 /* Split both trees into variables, constants, and literals. Then
9592 associate each group together, the constants with literals,
9593 then the result with variables. This increases the chances of
9594 literals being recombined later and of generating relocatable
9595 expressions for the sum of a constant and literal. */
9596 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9597 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9598 code == MINUS_EXPR);
9599
9600 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9601 if (code == MINUS_EXPR)
9602 code = PLUS_EXPR;
9603
9604 /* With undefined overflow prefer doing association in a type
9605 which wraps on overflow, if that is one of the operand types. */
9606 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9607 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9608 {
9609 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9610 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9611 atype = TREE_TYPE (arg0);
9612 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9613 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9614 atype = TREE_TYPE (arg1);
9615 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9616 }
9617
9618 /* With undefined overflow we can only associate constants with one
9619 variable, and constants whose association doesn't overflow. */
9620 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9621 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9622 {
9623 if (var0 && var1)
9624 {
9625 tree tmp0 = var0;
9626 tree tmp1 = var1;
9627
9628 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9629 tmp0 = TREE_OPERAND (tmp0, 0);
9630 if (CONVERT_EXPR_P (tmp0)
9631 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9632 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9633 <= TYPE_PRECISION (atype)))
9634 tmp0 = TREE_OPERAND (tmp0, 0);
9635 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9636 tmp1 = TREE_OPERAND (tmp1, 0);
9637 if (CONVERT_EXPR_P (tmp1)
9638 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9639 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9640 <= TYPE_PRECISION (atype)))
9641 tmp1 = TREE_OPERAND (tmp1, 0);
9642 /* The only case we can still associate with two variables
9643 is if they are the same, modulo negation and bit-pattern
9644 preserving conversions. */
9645 if (!operand_equal_p (tmp0, tmp1, 0))
9646 ok = false;
9647 }
9648 }
9649
9650 /* Only do something if we found more than two objects. Otherwise,
9651 nothing has changed and we risk infinite recursion. */
9652 if (ok
9653 && (2 < ((var0 != 0) + (var1 != 0)
9654 + (con0 != 0) + (con1 != 0)
9655 + (lit0 != 0) + (lit1 != 0)
9656 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9657 {
9658 bool any_overflows = false;
9659 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9660 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9661 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9662 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9663 var0 = associate_trees (loc, var0, var1, code, atype);
9664 con0 = associate_trees (loc, con0, con1, code, atype);
9665 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9666 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9667 code, atype);
9668
9669 /* Preserve the MINUS_EXPR if the negative part of the literal is
9670 greater than the positive part. Otherwise, the multiplicative
9671 folding code (i.e extract_muldiv) may be fooled in case
9672 unsigned constants are subtracted, like in the following
9673 example: ((X*2 + 4) - 8U)/2. */
9674 if (minus_lit0 && lit0)
9675 {
9676 if (TREE_CODE (lit0) == INTEGER_CST
9677 && TREE_CODE (minus_lit0) == INTEGER_CST
9678 && tree_int_cst_lt (lit0, minus_lit0))
9679 {
9680 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9681 MINUS_EXPR, atype);
9682 lit0 = 0;
9683 }
9684 else
9685 {
9686 lit0 = associate_trees (loc, lit0, minus_lit0,
9687 MINUS_EXPR, atype);
9688 minus_lit0 = 0;
9689 }
9690 }
9691
9692 /* Don't introduce overflows through reassociation. */
9693 if (!any_overflows
9694 && ((lit0 && TREE_OVERFLOW_P (lit0))
9695 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9696 return NULL_TREE;
9697
9698 if (minus_lit0)
9699 {
9700 if (con0 == 0)
9701 return
9702 fold_convert_loc (loc, type,
9703 associate_trees (loc, var0, minus_lit0,
9704 MINUS_EXPR, atype));
9705 else
9706 {
9707 con0 = associate_trees (loc, con0, minus_lit0,
9708 MINUS_EXPR, atype);
9709 return
9710 fold_convert_loc (loc, type,
9711 associate_trees (loc, var0, con0,
9712 PLUS_EXPR, atype));
9713 }
9714 }
9715
9716 con0 = associate_trees (loc, con0, lit0, code, atype);
9717 return
9718 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9719 code, atype));
9720 }
9721 }
9722
9723 return NULL_TREE;
9724
9725 case MINUS_EXPR:
9726 /* Pointer simplifications for subtraction, simple reassociations. */
9727 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9728 {
9729 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9730 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9731 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9732 {
9733 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9734 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9735 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9736 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9737 return fold_build2_loc (loc, PLUS_EXPR, type,
9738 fold_build2_loc (loc, MINUS_EXPR, type,
9739 arg00, arg10),
9740 fold_build2_loc (loc, MINUS_EXPR, type,
9741 arg01, arg11));
9742 }
9743 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9744 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9745 {
9746 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9747 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9748 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
9749 fold_convert_loc (loc, type, arg1));
9750 if (tmp)
9751 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
9752 }
9753 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
9754 simplifies. */
9755 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9756 {
9757 tree arg10 = fold_convert_loc (loc, type,
9758 TREE_OPERAND (arg1, 0));
9759 tree arg11 = fold_convert_loc (loc, type,
9760 TREE_OPERAND (arg1, 1));
9761 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
9762 fold_convert_loc (loc, type, arg0),
9763 arg10);
9764 if (tmp)
9765 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
9766 }
9767 }
9768 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9769 if (TREE_CODE (arg0) == NEGATE_EXPR
9770 && negate_expr_p (arg1)
9771 && reorder_operands_p (arg0, arg1))
9772 return fold_build2_loc (loc, MINUS_EXPR, type,
9773 fold_convert_loc (loc, type,
9774 negate_expr (arg1)),
9775 fold_convert_loc (loc, type,
9776 TREE_OPERAND (arg0, 0)));
9777
9778 if (! FLOAT_TYPE_P (type))
9779 {
9780 /* Fold A - (A & B) into ~B & A. */
9781 if (!TREE_SIDE_EFFECTS (arg0)
9782 && TREE_CODE (arg1) == BIT_AND_EXPR)
9783 {
9784 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9785 {
9786 tree arg10 = fold_convert_loc (loc, type,
9787 TREE_OPERAND (arg1, 0));
9788 return fold_build2_loc (loc, BIT_AND_EXPR, type,
9789 fold_build1_loc (loc, BIT_NOT_EXPR,
9790 type, arg10),
9791 fold_convert_loc (loc, type, arg0));
9792 }
9793 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9794 {
9795 tree arg11 = fold_convert_loc (loc,
9796 type, TREE_OPERAND (arg1, 1));
9797 return fold_build2_loc (loc, BIT_AND_EXPR, type,
9798 fold_build1_loc (loc, BIT_NOT_EXPR,
9799 type, arg11),
9800 fold_convert_loc (loc, type, arg0));
9801 }
9802 }
9803
9804 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9805 any power of 2 minus 1. */
9806 if (TREE_CODE (arg0) == BIT_AND_EXPR
9807 && TREE_CODE (arg1) == BIT_AND_EXPR
9808 && operand_equal_p (TREE_OPERAND (arg0, 0),
9809 TREE_OPERAND (arg1, 0), 0))
9810 {
9811 tree mask0 = TREE_OPERAND (arg0, 1);
9812 tree mask1 = TREE_OPERAND (arg1, 1);
9813 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
9814
9815 if (operand_equal_p (tem, mask1, 0))
9816 {
9817 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
9818 TREE_OPERAND (arg0, 0), mask1);
9819 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
9820 }
9821 }
9822 }
9823
9824 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9825 __complex__ ( x, -y ). This is not the same for SNaNs or if
9826 signed zeros are involved. */
9827 if (!HONOR_SNANS (element_mode (arg0))
9828 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9829 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9830 {
9831 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9832 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9833 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9834 bool arg0rz = false, arg0iz = false;
9835 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9836 || (arg0i && (arg0iz = real_zerop (arg0i))))
9837 {
9838 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9839 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9840 if (arg0rz && arg1i && real_zerop (arg1i))
9841 {
9842 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9843 arg1r ? arg1r
9844 : build1 (REALPART_EXPR, rtype, arg1));
9845 tree ip = arg0i ? arg0i
9846 : build1 (IMAGPART_EXPR, rtype, arg0);
9847 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9848 }
9849 else if (arg0iz && arg1r && real_zerop (arg1r))
9850 {
9851 tree rp = arg0r ? arg0r
9852 : build1 (REALPART_EXPR, rtype, arg0);
9853 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9854 arg1i ? arg1i
9855 : build1 (IMAGPART_EXPR, rtype, arg1));
9856 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9857 }
9858 }
9859 }
9860
9861 /* A - B -> A + (-B) if B is easily negatable. */
9862 if (negate_expr_p (arg1)
9863 && !TYPE_OVERFLOW_SANITIZED (type)
9864 && ((FLOAT_TYPE_P (type)
9865 /* Avoid this transformation if B is a positive REAL_CST. */
9866 && (TREE_CODE (arg1) != REAL_CST
9867 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9868 || INTEGRAL_TYPE_P (type)))
9869 return fold_build2_loc (loc, PLUS_EXPR, type,
9870 fold_convert_loc (loc, type, arg0),
9871 fold_convert_loc (loc, type,
9872 negate_expr (arg1)));
9873
9874 /* Fold &a[i] - &a[j] to i-j. */
9875 if (TREE_CODE (arg0) == ADDR_EXPR
9876 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9877 && TREE_CODE (arg1) == ADDR_EXPR
9878 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9879 {
9880 tree tem = fold_addr_of_array_ref_difference (loc, type,
9881 TREE_OPERAND (arg0, 0),
9882 TREE_OPERAND (arg1, 0));
9883 if (tem)
9884 return tem;
9885 }
9886
9887 if (FLOAT_TYPE_P (type)
9888 && flag_unsafe_math_optimizations
9889 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9890 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9891 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9892 return tem;
9893
9894 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9895 one. Make sure the type is not saturating and has the signedness of
9896 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9897 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9898 if ((TREE_CODE (arg0) == MULT_EXPR
9899 || TREE_CODE (arg1) == MULT_EXPR)
9900 && !TYPE_SATURATING (type)
9901 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9902 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9903 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9904 {
9905 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9906 if (tem)
9907 return tem;
9908 }
9909
9910 goto associate;
9911
9912 case MULT_EXPR:
9913 /* (-A) * (-B) -> A * B */
9914 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9915 return fold_build2_loc (loc, MULT_EXPR, type,
9916 fold_convert_loc (loc, type,
9917 TREE_OPERAND (arg0, 0)),
9918 fold_convert_loc (loc, type,
9919 negate_expr (arg1)));
9920 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9921 return fold_build2_loc (loc, MULT_EXPR, type,
9922 fold_convert_loc (loc, type,
9923 negate_expr (arg0)),
9924 fold_convert_loc (loc, type,
9925 TREE_OPERAND (arg1, 0)));
9926
9927 if (! FLOAT_TYPE_P (type))
9928 {
9929 /* Transform x * -C into -x * C if x is easily negatable. */
9930 if (TREE_CODE (arg1) == INTEGER_CST
9931 && tree_int_cst_sgn (arg1) == -1
9932 && negate_expr_p (arg0)
9933 && (tem = negate_expr (arg1)) != arg1
9934 && !TREE_OVERFLOW (tem))
9935 return fold_build2_loc (loc, MULT_EXPR, type,
9936 fold_convert_loc (loc, type,
9937 negate_expr (arg0)),
9938 tem);
9939
9940 /* (a * (1 << b)) is (a << b) */
9941 if (TREE_CODE (arg1) == LSHIFT_EXPR
9942 && integer_onep (TREE_OPERAND (arg1, 0)))
9943 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
9944 TREE_OPERAND (arg1, 1));
9945 if (TREE_CODE (arg0) == LSHIFT_EXPR
9946 && integer_onep (TREE_OPERAND (arg0, 0)))
9947 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
9948 TREE_OPERAND (arg0, 1));
9949
9950 /* (A + A) * C -> A * 2 * C */
9951 if (TREE_CODE (arg0) == PLUS_EXPR
9952 && TREE_CODE (arg1) == INTEGER_CST
9953 && operand_equal_p (TREE_OPERAND (arg0, 0),
9954 TREE_OPERAND (arg0, 1), 0))
9955 return fold_build2_loc (loc, MULT_EXPR, type,
9956 omit_one_operand_loc (loc, type,
9957 TREE_OPERAND (arg0, 0),
9958 TREE_OPERAND (arg0, 1)),
9959 fold_build2_loc (loc, MULT_EXPR, type,
9960 build_int_cst (type, 2) , arg1));
9961
9962 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9963 sign-changing only. */
9964 if (TREE_CODE (arg1) == INTEGER_CST
9965 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9966 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9967 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9968
9969 strict_overflow_p = false;
9970 if (TREE_CODE (arg1) == INTEGER_CST
9971 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9972 &strict_overflow_p)))
9973 {
9974 if (strict_overflow_p)
9975 fold_overflow_warning (("assuming signed overflow does not "
9976 "occur when simplifying "
9977 "multiplication"),
9978 WARN_STRICT_OVERFLOW_MISC);
9979 return fold_convert_loc (loc, type, tem);
9980 }
9981
9982 /* Optimize z * conj(z) for integer complex numbers. */
9983 if (TREE_CODE (arg0) == CONJ_EXPR
9984 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9985 return fold_mult_zconjz (loc, type, arg1);
9986 if (TREE_CODE (arg1) == CONJ_EXPR
9987 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9988 return fold_mult_zconjz (loc, type, arg0);
9989 }
9990 else
9991 {
9992 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
9993 the result for floating point types due to rounding so it is applied
9994 only if -fassociative-math was specify. */
9995 if (flag_associative_math
9996 && TREE_CODE (arg0) == RDIV_EXPR
9997 && TREE_CODE (arg1) == REAL_CST
9998 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
9999 {
10000 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10001 arg1);
10002 if (tem)
10003 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10004 TREE_OPERAND (arg0, 1));
10005 }
10006
10007 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10008 if (operand_equal_p (arg0, arg1, 0))
10009 {
10010 tree tem = fold_strip_sign_ops (arg0);
10011 if (tem != NULL_TREE)
10012 {
10013 tem = fold_convert_loc (loc, type, tem);
10014 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10015 }
10016 }
10017
10018 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10019 This is not the same for NaNs or if signed zeros are
10020 involved. */
10021 if (!HONOR_NANS (arg0)
10022 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10023 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10024 && TREE_CODE (arg1) == COMPLEX_CST
10025 && real_zerop (TREE_REALPART (arg1)))
10026 {
10027 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10028 if (real_onep (TREE_IMAGPART (arg1)))
10029 return
10030 fold_build2_loc (loc, COMPLEX_EXPR, type,
10031 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10032 rtype, arg0)),
10033 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10034 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10035 return
10036 fold_build2_loc (loc, COMPLEX_EXPR, type,
10037 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10038 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10039 rtype, arg0)));
10040 }
10041
10042 /* Optimize z * conj(z) for floating point complex numbers.
10043 Guarded by flag_unsafe_math_optimizations as non-finite
10044 imaginary components don't produce scalar results. */
10045 if (flag_unsafe_math_optimizations
10046 && TREE_CODE (arg0) == CONJ_EXPR
10047 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10048 return fold_mult_zconjz (loc, type, arg1);
10049 if (flag_unsafe_math_optimizations
10050 && TREE_CODE (arg1) == CONJ_EXPR
10051 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10052 return fold_mult_zconjz (loc, type, arg0);
10053
10054 if (flag_unsafe_math_optimizations)
10055 {
10056 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10057 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10058
10059 /* Optimizations of root(...)*root(...). */
10060 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10061 {
10062 tree rootfn, arg;
10063 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10064 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10065
10066 /* Optimize sqrt(x)*sqrt(x) as x. */
10067 if (BUILTIN_SQRT_P (fcode0)
10068 && operand_equal_p (arg00, arg10, 0)
10069 && ! HONOR_SNANS (element_mode (type)))
10070 return arg00;
10071
10072 /* Optimize root(x)*root(y) as root(x*y). */
10073 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10074 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10075 return build_call_expr_loc (loc, rootfn, 1, arg);
10076 }
10077
10078 /* Optimize expN(x)*expN(y) as expN(x+y). */
10079 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10080 {
10081 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10082 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10083 CALL_EXPR_ARG (arg0, 0),
10084 CALL_EXPR_ARG (arg1, 0));
10085 return build_call_expr_loc (loc, expfn, 1, arg);
10086 }
10087
10088 /* Optimizations of pow(...)*pow(...). */
10089 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10090 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10091 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10092 {
10093 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10094 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10095 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10096 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10097
10098 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10099 if (operand_equal_p (arg01, arg11, 0))
10100 {
10101 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10102 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10103 arg00, arg10);
10104 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10105 }
10106
10107 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10108 if (operand_equal_p (arg00, arg10, 0))
10109 {
10110 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10111 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10112 arg01, arg11);
10113 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10114 }
10115 }
10116
10117 /* Optimize tan(x)*cos(x) as sin(x). */
10118 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10119 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10120 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10121 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10122 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10123 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10124 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10125 CALL_EXPR_ARG (arg1, 0), 0))
10126 {
10127 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10128
10129 if (sinfn != NULL_TREE)
10130 return build_call_expr_loc (loc, sinfn, 1,
10131 CALL_EXPR_ARG (arg0, 0));
10132 }
10133
10134 /* Optimize x*pow(x,c) as pow(x,c+1). */
10135 if (fcode1 == BUILT_IN_POW
10136 || fcode1 == BUILT_IN_POWF
10137 || fcode1 == BUILT_IN_POWL)
10138 {
10139 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10140 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10141 if (TREE_CODE (arg11) == REAL_CST
10142 && !TREE_OVERFLOW (arg11)
10143 && operand_equal_p (arg0, arg10, 0))
10144 {
10145 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10146 REAL_VALUE_TYPE c;
10147 tree arg;
10148
10149 c = TREE_REAL_CST (arg11);
10150 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10151 arg = build_real (type, c);
10152 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10153 }
10154 }
10155
10156 /* Optimize pow(x,c)*x as pow(x,c+1). */
10157 if (fcode0 == BUILT_IN_POW
10158 || fcode0 == BUILT_IN_POWF
10159 || fcode0 == BUILT_IN_POWL)
10160 {
10161 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10162 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10163 if (TREE_CODE (arg01) == REAL_CST
10164 && !TREE_OVERFLOW (arg01)
10165 && operand_equal_p (arg1, arg00, 0))
10166 {
10167 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10168 REAL_VALUE_TYPE c;
10169 tree arg;
10170
10171 c = TREE_REAL_CST (arg01);
10172 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10173 arg = build_real (type, c);
10174 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10175 }
10176 }
10177
10178 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10179 if (!in_gimple_form
10180 && optimize
10181 && operand_equal_p (arg0, arg1, 0))
10182 {
10183 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10184
10185 if (powfn)
10186 {
10187 tree arg = build_real (type, dconst2);
10188 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10189 }
10190 }
10191 }
10192 }
10193 goto associate;
10194
10195 case BIT_IOR_EXPR:
10196 /* Canonicalize (X & C1) | C2. */
10197 if (TREE_CODE (arg0) == BIT_AND_EXPR
10198 && TREE_CODE (arg1) == INTEGER_CST
10199 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10200 {
10201 int width = TYPE_PRECISION (type), w;
10202 wide_int c1 = TREE_OPERAND (arg0, 1);
10203 wide_int c2 = arg1;
10204
10205 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10206 if ((c1 & c2) == c1)
10207 return omit_one_operand_loc (loc, type, arg1,
10208 TREE_OPERAND (arg0, 0));
10209
10210 wide_int msk = wi::mask (width, false,
10211 TYPE_PRECISION (TREE_TYPE (arg1)));
10212
10213 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10214 if (msk.and_not (c1 | c2) == 0)
10215 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10216 TREE_OPERAND (arg0, 0), arg1);
10217
10218 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10219 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10220 mode which allows further optimizations. */
10221 c1 &= msk;
10222 c2 &= msk;
10223 wide_int c3 = c1.and_not (c2);
10224 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10225 {
10226 wide_int mask = wi::mask (w, false,
10227 TYPE_PRECISION (type));
10228 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10229 {
10230 c3 = mask;
10231 break;
10232 }
10233 }
10234
10235 if (c3 != c1)
10236 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10237 fold_build2_loc (loc, BIT_AND_EXPR, type,
10238 TREE_OPERAND (arg0, 0),
10239 wide_int_to_tree (type,
10240 c3)),
10241 arg1);
10242 }
10243
10244 /* (X & ~Y) | (~X & Y) is X ^ Y */
10245 if (TREE_CODE (arg0) == BIT_AND_EXPR
10246 && TREE_CODE (arg1) == BIT_AND_EXPR)
10247 {
10248 tree a0, a1, l0, l1, n0, n1;
10249
10250 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10251 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10252
10253 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10254 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10255
10256 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
10257 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
10258
10259 if ((operand_equal_p (n0, a0, 0)
10260 && operand_equal_p (n1, a1, 0))
10261 || (operand_equal_p (n0, a1, 0)
10262 && operand_equal_p (n1, a0, 0)))
10263 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
10264 }
10265
10266 /* See if this can be simplified into a rotate first. If that
10267 is unsuccessful continue in the association code. */
10268 goto bit_rotate;
10269
10270 case BIT_XOR_EXPR:
10271 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10272 if (TREE_CODE (arg0) == BIT_AND_EXPR
10273 && INTEGRAL_TYPE_P (type)
10274 && integer_onep (TREE_OPERAND (arg0, 1))
10275 && integer_onep (arg1))
10276 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10277 build_zero_cst (TREE_TYPE (arg0)));
10278
10279 /* See if this can be simplified into a rotate first. If that
10280 is unsuccessful continue in the association code. */
10281 goto bit_rotate;
10282
10283 case BIT_AND_EXPR:
10284 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
10285 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
10286 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
10287 || (TREE_CODE (arg0) == EQ_EXPR
10288 && integer_zerop (TREE_OPERAND (arg0, 1))))
10289 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10290 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10291
10292 /* X & ~X , X & (X == 0), and X & !X are always zero. */
10293 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
10294 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
10295 || (TREE_CODE (arg1) == EQ_EXPR
10296 && integer_zerop (TREE_OPERAND (arg1, 1))))
10297 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10298 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10299
10300 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10301 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10302 && INTEGRAL_TYPE_P (type)
10303 && integer_onep (TREE_OPERAND (arg0, 1))
10304 && integer_onep (arg1))
10305 {
10306 tree tem2;
10307 tem = TREE_OPERAND (arg0, 0);
10308 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10309 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10310 tem, tem2);
10311 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10312 build_zero_cst (TREE_TYPE (tem)));
10313 }
10314 /* Fold ~X & 1 as (X & 1) == 0. */
10315 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10316 && INTEGRAL_TYPE_P (type)
10317 && integer_onep (arg1))
10318 {
10319 tree tem2;
10320 tem = TREE_OPERAND (arg0, 0);
10321 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10322 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10323 tem, tem2);
10324 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10325 build_zero_cst (TREE_TYPE (tem)));
10326 }
10327 /* Fold !X & 1 as X == 0. */
10328 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10329 && integer_onep (arg1))
10330 {
10331 tem = TREE_OPERAND (arg0, 0);
10332 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10333 build_zero_cst (TREE_TYPE (tem)));
10334 }
10335
10336 /* Fold (X ^ Y) & Y as ~X & Y. */
10337 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10338 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10339 {
10340 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10341 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10342 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10343 fold_convert_loc (loc, type, arg1));
10344 }
10345 /* Fold (X ^ Y) & X as ~Y & X. */
10346 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10347 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10348 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10349 {
10350 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10351 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10352 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10353 fold_convert_loc (loc, type, arg1));
10354 }
10355 /* Fold X & (X ^ Y) as X & ~Y. */
10356 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10357 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10358 {
10359 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10360 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10361 fold_convert_loc (loc, type, arg0),
10362 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10363 }
10364 /* Fold X & (Y ^ X) as ~Y & X. */
10365 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10366 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10367 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10368 {
10369 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10370 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10371 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10372 fold_convert_loc (loc, type, arg0));
10373 }
10374
10375 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10376 multiple of 1 << CST. */
10377 if (TREE_CODE (arg1) == INTEGER_CST)
10378 {
10379 wide_int cst1 = arg1;
10380 wide_int ncst1 = -cst1;
10381 if ((cst1 & ncst1) == ncst1
10382 && multiple_of_p (type, arg0,
10383 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10384 return fold_convert_loc (loc, type, arg0);
10385 }
10386
10387 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10388 bits from CST2. */
10389 if (TREE_CODE (arg1) == INTEGER_CST
10390 && TREE_CODE (arg0) == MULT_EXPR
10391 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10392 {
10393 wide_int warg1 = arg1;
10394 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10395
10396 if (masked == 0)
10397 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10398 arg0, arg1);
10399 else if (masked != warg1)
10400 {
10401 /* Avoid the transform if arg1 is a mask of some
10402 mode which allows further optimizations. */
10403 int pop = wi::popcount (warg1);
10404 if (!(pop >= BITS_PER_UNIT
10405 && exact_log2 (pop) != -1
10406 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10407 return fold_build2_loc (loc, code, type, op0,
10408 wide_int_to_tree (type, masked));
10409 }
10410 }
10411
10412 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10413 ((A & N) + B) & M -> (A + B) & M
10414 Similarly if (N & M) == 0,
10415 ((A | N) + B) & M -> (A + B) & M
10416 and for - instead of + (or unary - instead of +)
10417 and/or ^ instead of |.
10418 If B is constant and (B & M) == 0, fold into A & M. */
10419 if (TREE_CODE (arg1) == INTEGER_CST)
10420 {
10421 wide_int cst1 = arg1;
10422 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10423 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10424 && (TREE_CODE (arg0) == PLUS_EXPR
10425 || TREE_CODE (arg0) == MINUS_EXPR
10426 || TREE_CODE (arg0) == NEGATE_EXPR)
10427 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10428 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10429 {
10430 tree pmop[2];
10431 int which = 0;
10432 wide_int cst0;
10433
10434 /* Now we know that arg0 is (C + D) or (C - D) or
10435 -C and arg1 (M) is == (1LL << cst) - 1.
10436 Store C into PMOP[0] and D into PMOP[1]. */
10437 pmop[0] = TREE_OPERAND (arg0, 0);
10438 pmop[1] = NULL;
10439 if (TREE_CODE (arg0) != NEGATE_EXPR)
10440 {
10441 pmop[1] = TREE_OPERAND (arg0, 1);
10442 which = 1;
10443 }
10444
10445 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10446 which = -1;
10447
10448 for (; which >= 0; which--)
10449 switch (TREE_CODE (pmop[which]))
10450 {
10451 case BIT_AND_EXPR:
10452 case BIT_IOR_EXPR:
10453 case BIT_XOR_EXPR:
10454 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10455 != INTEGER_CST)
10456 break;
10457 cst0 = TREE_OPERAND (pmop[which], 1);
10458 cst0 &= cst1;
10459 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10460 {
10461 if (cst0 != cst1)
10462 break;
10463 }
10464 else if (cst0 != 0)
10465 break;
10466 /* If C or D is of the form (A & N) where
10467 (N & M) == M, or of the form (A | N) or
10468 (A ^ N) where (N & M) == 0, replace it with A. */
10469 pmop[which] = TREE_OPERAND (pmop[which], 0);
10470 break;
10471 case INTEGER_CST:
10472 /* If C or D is a N where (N & M) == 0, it can be
10473 omitted (assumed 0). */
10474 if ((TREE_CODE (arg0) == PLUS_EXPR
10475 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10476 && (cst1 & pmop[which]) == 0)
10477 pmop[which] = NULL;
10478 break;
10479 default:
10480 break;
10481 }
10482
10483 /* Only build anything new if we optimized one or both arguments
10484 above. */
10485 if (pmop[0] != TREE_OPERAND (arg0, 0)
10486 || (TREE_CODE (arg0) != NEGATE_EXPR
10487 && pmop[1] != TREE_OPERAND (arg0, 1)))
10488 {
10489 tree utype = TREE_TYPE (arg0);
10490 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10491 {
10492 /* Perform the operations in a type that has defined
10493 overflow behavior. */
10494 utype = unsigned_type_for (TREE_TYPE (arg0));
10495 if (pmop[0] != NULL)
10496 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10497 if (pmop[1] != NULL)
10498 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10499 }
10500
10501 if (TREE_CODE (arg0) == NEGATE_EXPR)
10502 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10503 else if (TREE_CODE (arg0) == PLUS_EXPR)
10504 {
10505 if (pmop[0] != NULL && pmop[1] != NULL)
10506 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10507 pmop[0], pmop[1]);
10508 else if (pmop[0] != NULL)
10509 tem = pmop[0];
10510 else if (pmop[1] != NULL)
10511 tem = pmop[1];
10512 else
10513 return build_int_cst (type, 0);
10514 }
10515 else if (pmop[0] == NULL)
10516 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10517 else
10518 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10519 pmop[0], pmop[1]);
10520 /* TEM is now the new binary +, - or unary - replacement. */
10521 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10522 fold_convert_loc (loc, utype, arg1));
10523 return fold_convert_loc (loc, type, tem);
10524 }
10525 }
10526 }
10527
10528 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10529 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10530 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10531 {
10532 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10533
10534 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10535 if (mask == -1)
10536 return
10537 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10538 }
10539
10540 goto associate;
10541
10542 case RDIV_EXPR:
10543 /* Don't touch a floating-point divide by zero unless the mode
10544 of the constant can represent infinity. */
10545 if (TREE_CODE (arg1) == REAL_CST
10546 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10547 && real_zerop (arg1))
10548 return NULL_TREE;
10549
10550 /* (-A) / (-B) -> A / B */
10551 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10552 return fold_build2_loc (loc, RDIV_EXPR, type,
10553 TREE_OPERAND (arg0, 0),
10554 negate_expr (arg1));
10555 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10556 return fold_build2_loc (loc, RDIV_EXPR, type,
10557 negate_expr (arg0),
10558 TREE_OPERAND (arg1, 0));
10559
10560 /* Convert A/B/C to A/(B*C). */
10561 if (flag_reciprocal_math
10562 && TREE_CODE (arg0) == RDIV_EXPR)
10563 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10564 fold_build2_loc (loc, MULT_EXPR, type,
10565 TREE_OPERAND (arg0, 1), arg1));
10566
10567 /* Convert A/(B/C) to (A/B)*C. */
10568 if (flag_reciprocal_math
10569 && TREE_CODE (arg1) == RDIV_EXPR)
10570 return fold_build2_loc (loc, MULT_EXPR, type,
10571 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
10572 TREE_OPERAND (arg1, 0)),
10573 TREE_OPERAND (arg1, 1));
10574
10575 /* Convert C1/(X*C2) into (C1/C2)/X. */
10576 if (flag_reciprocal_math
10577 && TREE_CODE (arg1) == MULT_EXPR
10578 && TREE_CODE (arg0) == REAL_CST
10579 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10580 {
10581 tree tem = const_binop (RDIV_EXPR, arg0,
10582 TREE_OPERAND (arg1, 1));
10583 if (tem)
10584 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10585 TREE_OPERAND (arg1, 0));
10586 }
10587
10588 if (flag_unsafe_math_optimizations)
10589 {
10590 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10591 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10592
10593 /* Optimize sin(x)/cos(x) as tan(x). */
10594 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10595 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10596 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
10597 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10598 CALL_EXPR_ARG (arg1, 0), 0))
10599 {
10600 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10601
10602 if (tanfn != NULL_TREE)
10603 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
10604 }
10605
10606 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
10607 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
10608 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
10609 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
10610 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10611 CALL_EXPR_ARG (arg1, 0), 0))
10612 {
10613 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10614
10615 if (tanfn != NULL_TREE)
10616 {
10617 tree tmp = build_call_expr_loc (loc, tanfn, 1,
10618 CALL_EXPR_ARG (arg0, 0));
10619 return fold_build2_loc (loc, RDIV_EXPR, type,
10620 build_real (type, dconst1), tmp);
10621 }
10622 }
10623
10624 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
10625 NaNs or Infinities. */
10626 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
10627 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
10628 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
10629 {
10630 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10631 tree arg01 = CALL_EXPR_ARG (arg1, 0);
10632
10633 if (! HONOR_NANS (arg00)
10634 && ! HONOR_INFINITIES (element_mode (arg00))
10635 && operand_equal_p (arg00, arg01, 0))
10636 {
10637 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10638
10639 if (cosfn != NULL_TREE)
10640 return build_call_expr_loc (loc, cosfn, 1, arg00);
10641 }
10642 }
10643
10644 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
10645 NaNs or Infinities. */
10646 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
10647 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
10648 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
10649 {
10650 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10651 tree arg01 = CALL_EXPR_ARG (arg1, 0);
10652
10653 if (! HONOR_NANS (arg00)
10654 && ! HONOR_INFINITIES (element_mode (arg00))
10655 && operand_equal_p (arg00, arg01, 0))
10656 {
10657 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10658
10659 if (cosfn != NULL_TREE)
10660 {
10661 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
10662 return fold_build2_loc (loc, RDIV_EXPR, type,
10663 build_real (type, dconst1),
10664 tmp);
10665 }
10666 }
10667 }
10668
10669 /* Optimize pow(x,c)/x as pow(x,c-1). */
10670 if (fcode0 == BUILT_IN_POW
10671 || fcode0 == BUILT_IN_POWF
10672 || fcode0 == BUILT_IN_POWL)
10673 {
10674 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10675 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10676 if (TREE_CODE (arg01) == REAL_CST
10677 && !TREE_OVERFLOW (arg01)
10678 && operand_equal_p (arg1, arg00, 0))
10679 {
10680 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10681 REAL_VALUE_TYPE c;
10682 tree arg;
10683
10684 c = TREE_REAL_CST (arg01);
10685 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
10686 arg = build_real (type, c);
10687 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10688 }
10689 }
10690
10691 /* Optimize a/root(b/c) into a*root(c/b). */
10692 if (BUILTIN_ROOT_P (fcode1))
10693 {
10694 tree rootarg = CALL_EXPR_ARG (arg1, 0);
10695
10696 if (TREE_CODE (rootarg) == RDIV_EXPR)
10697 {
10698 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10699 tree b = TREE_OPERAND (rootarg, 0);
10700 tree c = TREE_OPERAND (rootarg, 1);
10701
10702 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
10703
10704 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
10705 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
10706 }
10707 }
10708
10709 /* Optimize x/expN(y) into x*expN(-y). */
10710 if (BUILTIN_EXPONENT_P (fcode1))
10711 {
10712 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10713 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
10714 arg1 = build_call_expr_loc (loc,
10715 expfn, 1,
10716 fold_convert_loc (loc, type, arg));
10717 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
10718 }
10719
10720 /* Optimize x/pow(y,z) into x*pow(y,-z). */
10721 if (fcode1 == BUILT_IN_POW
10722 || fcode1 == BUILT_IN_POWF
10723 || fcode1 == BUILT_IN_POWL)
10724 {
10725 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10726 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10727 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10728 tree neg11 = fold_convert_loc (loc, type,
10729 negate_expr (arg11));
10730 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
10731 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
10732 }
10733 }
10734 return NULL_TREE;
10735
10736 case TRUNC_DIV_EXPR:
10737 /* Optimize (X & (-A)) / A where A is a power of 2,
10738 to X >> log2(A) */
10739 if (TREE_CODE (arg0) == BIT_AND_EXPR
10740 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
10741 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
10742 {
10743 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
10744 arg1, TREE_OPERAND (arg0, 1));
10745 if (sum && integer_zerop (sum)) {
10746 tree pow2 = build_int_cst (integer_type_node,
10747 wi::exact_log2 (arg1));
10748 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10749 TREE_OPERAND (arg0, 0), pow2);
10750 }
10751 }
10752
10753 /* Fall through */
10754
10755 case FLOOR_DIV_EXPR:
10756 /* Simplify A / (B << N) where A and B are positive and B is
10757 a power of 2, to A >> (N + log2(B)). */
10758 strict_overflow_p = false;
10759 if (TREE_CODE (arg1) == LSHIFT_EXPR
10760 && (TYPE_UNSIGNED (type)
10761 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10762 {
10763 tree sval = TREE_OPERAND (arg1, 0);
10764 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10765 {
10766 tree sh_cnt = TREE_OPERAND (arg1, 1);
10767 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10768 wi::exact_log2 (sval));
10769
10770 if (strict_overflow_p)
10771 fold_overflow_warning (("assuming signed overflow does not "
10772 "occur when simplifying A / (B << N)"),
10773 WARN_STRICT_OVERFLOW_MISC);
10774
10775 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10776 sh_cnt, pow2);
10777 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10778 fold_convert_loc (loc, type, arg0), sh_cnt);
10779 }
10780 }
10781
10782 /* Fall through */
10783
10784 case ROUND_DIV_EXPR:
10785 case CEIL_DIV_EXPR:
10786 case EXACT_DIV_EXPR:
10787 if (integer_zerop (arg1))
10788 return NULL_TREE;
10789
10790 /* Convert -A / -B to A / B when the type is signed and overflow is
10791 undefined. */
10792 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10793 && TREE_CODE (arg0) == NEGATE_EXPR
10794 && negate_expr_p (arg1))
10795 {
10796 if (INTEGRAL_TYPE_P (type))
10797 fold_overflow_warning (("assuming signed overflow does not occur "
10798 "when distributing negation across "
10799 "division"),
10800 WARN_STRICT_OVERFLOW_MISC);
10801 return fold_build2_loc (loc, code, type,
10802 fold_convert_loc (loc, type,
10803 TREE_OPERAND (arg0, 0)),
10804 fold_convert_loc (loc, type,
10805 negate_expr (arg1)));
10806 }
10807 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10808 && TREE_CODE (arg1) == NEGATE_EXPR
10809 && negate_expr_p (arg0))
10810 {
10811 if (INTEGRAL_TYPE_P (type))
10812 fold_overflow_warning (("assuming signed overflow does not occur "
10813 "when distributing negation across "
10814 "division"),
10815 WARN_STRICT_OVERFLOW_MISC);
10816 return fold_build2_loc (loc, code, type,
10817 fold_convert_loc (loc, type,
10818 negate_expr (arg0)),
10819 fold_convert_loc (loc, type,
10820 TREE_OPERAND (arg1, 0)));
10821 }
10822
10823 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10824 operation, EXACT_DIV_EXPR.
10825
10826 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10827 At one time others generated faster code, it's not clear if they do
10828 after the last round to changes to the DIV code in expmed.c. */
10829 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10830 && multiple_of_p (type, arg0, arg1))
10831 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
10832
10833 strict_overflow_p = false;
10834 if (TREE_CODE (arg1) == INTEGER_CST
10835 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10836 &strict_overflow_p)))
10837 {
10838 if (strict_overflow_p)
10839 fold_overflow_warning (("assuming signed overflow does not occur "
10840 "when simplifying division"),
10841 WARN_STRICT_OVERFLOW_MISC);
10842 return fold_convert_loc (loc, type, tem);
10843 }
10844
10845 return NULL_TREE;
10846
10847 case CEIL_MOD_EXPR:
10848 case FLOOR_MOD_EXPR:
10849 case ROUND_MOD_EXPR:
10850 case TRUNC_MOD_EXPR:
10851 strict_overflow_p = false;
10852 if (TREE_CODE (arg1) == INTEGER_CST
10853 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10854 &strict_overflow_p)))
10855 {
10856 if (strict_overflow_p)
10857 fold_overflow_warning (("assuming signed overflow does not occur "
10858 "when simplifying modulus"),
10859 WARN_STRICT_OVERFLOW_MISC);
10860 return fold_convert_loc (loc, type, tem);
10861 }
10862
10863 return NULL_TREE;
10864
10865 case LROTATE_EXPR:
10866 case RROTATE_EXPR:
10867 case RSHIFT_EXPR:
10868 case LSHIFT_EXPR:
10869 /* Since negative shift count is not well-defined,
10870 don't try to compute it in the compiler. */
10871 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10872 return NULL_TREE;
10873
10874 prec = element_precision (type);
10875
10876 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
10877 into x & ((unsigned)-1 >> c) for unsigned types. */
10878 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
10879 || (TYPE_UNSIGNED (type)
10880 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
10881 && tree_fits_uhwi_p (arg1)
10882 && tree_to_uhwi (arg1) < prec
10883 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
10884 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
10885 {
10886 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
10887 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
10888 tree lshift;
10889 tree arg00;
10890
10891 if (low0 == low1)
10892 {
10893 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10894
10895 lshift = build_minus_one_cst (type);
10896 lshift = const_binop (code, lshift, arg1);
10897
10898 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
10899 }
10900 }
10901
10902 /* If we have a rotate of a bit operation with the rotate count and
10903 the second operand of the bit operation both constant,
10904 permute the two operations. */
10905 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10906 && (TREE_CODE (arg0) == BIT_AND_EXPR
10907 || TREE_CODE (arg0) == BIT_IOR_EXPR
10908 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10909 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10910 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10911 fold_build2_loc (loc, code, type,
10912 TREE_OPERAND (arg0, 0), arg1),
10913 fold_build2_loc (loc, code, type,
10914 TREE_OPERAND (arg0, 1), arg1));
10915
10916 /* Two consecutive rotates adding up to the some integer
10917 multiple of the precision of the type can be ignored. */
10918 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10919 && TREE_CODE (arg0) == RROTATE_EXPR
10920 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10921 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10922 prec) == 0)
10923 return TREE_OPERAND (arg0, 0);
10924
10925 return NULL_TREE;
10926
10927 case MIN_EXPR:
10928 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
10929 if (tem)
10930 return tem;
10931 goto associate;
10932
10933 case MAX_EXPR:
10934 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
10935 if (tem)
10936 return tem;
10937 goto associate;
10938
10939 case TRUTH_ANDIF_EXPR:
10940 /* Note that the operands of this must be ints
10941 and their values must be 0 or 1.
10942 ("true" is a fixed value perhaps depending on the language.) */
10943 /* If first arg is constant zero, return it. */
10944 if (integer_zerop (arg0))
10945 return fold_convert_loc (loc, type, arg0);
10946 case TRUTH_AND_EXPR:
10947 /* If either arg is constant true, drop it. */
10948 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10949 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10950 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10951 /* Preserve sequence points. */
10952 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10953 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10954 /* If second arg is constant zero, result is zero, but first arg
10955 must be evaluated. */
10956 if (integer_zerop (arg1))
10957 return omit_one_operand_loc (loc, type, arg1, arg0);
10958 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10959 case will be handled here. */
10960 if (integer_zerop (arg0))
10961 return omit_one_operand_loc (loc, type, arg0, arg1);
10962
10963 /* !X && X is always false. */
10964 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10965 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10966 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10967 /* X && !X is always false. */
10968 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10969 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10970 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10971
10972 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10973 means A >= Y && A != MAX, but in this case we know that
10974 A < X <= MAX. */
10975
10976 if (!TREE_SIDE_EFFECTS (arg0)
10977 && !TREE_SIDE_EFFECTS (arg1))
10978 {
10979 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10980 if (tem && !operand_equal_p (tem, arg0, 0))
10981 return fold_build2_loc (loc, code, type, tem, arg1);
10982
10983 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10984 if (tem && !operand_equal_p (tem, arg1, 0))
10985 return fold_build2_loc (loc, code, type, arg0, tem);
10986 }
10987
10988 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10989 != NULL_TREE)
10990 return tem;
10991
10992 return NULL_TREE;
10993
10994 case TRUTH_ORIF_EXPR:
10995 /* Note that the operands of this must be ints
10996 and their values must be 0 or true.
10997 ("true" is a fixed value perhaps depending on the language.) */
10998 /* If first arg is constant true, return it. */
10999 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11000 return fold_convert_loc (loc, type, arg0);
11001 case TRUTH_OR_EXPR:
11002 /* If either arg is constant zero, drop it. */
11003 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11004 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11005 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11006 /* Preserve sequence points. */
11007 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11008 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11009 /* If second arg is constant true, result is true, but we must
11010 evaluate first arg. */
11011 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11012 return omit_one_operand_loc (loc, type, arg1, arg0);
11013 /* Likewise for first arg, but note this only occurs here for
11014 TRUTH_OR_EXPR. */
11015 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11016 return omit_one_operand_loc (loc, type, arg0, arg1);
11017
11018 /* !X || X is always true. */
11019 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11020 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11021 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11022 /* X || !X is always true. */
11023 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11024 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11025 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11026
11027 /* (X && !Y) || (!X && Y) is X ^ Y */
11028 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
11029 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
11030 {
11031 tree a0, a1, l0, l1, n0, n1;
11032
11033 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11034 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11035
11036 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11037 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11038
11039 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
11040 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
11041
11042 if ((operand_equal_p (n0, a0, 0)
11043 && operand_equal_p (n1, a1, 0))
11044 || (operand_equal_p (n0, a1, 0)
11045 && operand_equal_p (n1, a0, 0)))
11046 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
11047 }
11048
11049 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11050 != NULL_TREE)
11051 return tem;
11052
11053 return NULL_TREE;
11054
11055 case TRUTH_XOR_EXPR:
11056 /* If the second arg is constant zero, drop it. */
11057 if (integer_zerop (arg1))
11058 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11059 /* If the second arg is constant true, this is a logical inversion. */
11060 if (integer_onep (arg1))
11061 {
11062 tem = invert_truthvalue_loc (loc, arg0);
11063 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11064 }
11065 /* Identical arguments cancel to zero. */
11066 if (operand_equal_p (arg0, arg1, 0))
11067 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11068
11069 /* !X ^ X is always true. */
11070 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11071 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11072 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11073
11074 /* X ^ !X is always true. */
11075 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11076 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11077 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11078
11079 return NULL_TREE;
11080
11081 case EQ_EXPR:
11082 case NE_EXPR:
11083 STRIP_NOPS (arg0);
11084 STRIP_NOPS (arg1);
11085
11086 tem = fold_comparison (loc, code, type, op0, op1);
11087 if (tem != NULL_TREE)
11088 return tem;
11089
11090 /* bool_var != 1 becomes !bool_var. */
11091 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11092 && code == NE_EXPR)
11093 return fold_convert_loc (loc, type,
11094 fold_build1_loc (loc, TRUTH_NOT_EXPR,
11095 TREE_TYPE (arg0), arg0));
11096
11097 /* bool_var == 0 becomes !bool_var. */
11098 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11099 && code == EQ_EXPR)
11100 return fold_convert_loc (loc, type,
11101 fold_build1_loc (loc, TRUTH_NOT_EXPR,
11102 TREE_TYPE (arg0), arg0));
11103
11104 /* !exp != 0 becomes !exp */
11105 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
11106 && code == NE_EXPR)
11107 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11108
11109 /* If this is an equality comparison of the address of two non-weak,
11110 unaliased symbols neither of which are extern (since we do not
11111 have access to attributes for externs), then we know the result. */
11112 if (TREE_CODE (arg0) == ADDR_EXPR
11113 && DECL_P (TREE_OPERAND (arg0, 0))
11114 && TREE_CODE (arg1) == ADDR_EXPR
11115 && DECL_P (TREE_OPERAND (arg1, 0)))
11116 {
11117 int equal;
11118
11119 if (decl_in_symtab_p (TREE_OPERAND (arg0, 0))
11120 && decl_in_symtab_p (TREE_OPERAND (arg1, 0)))
11121 equal = symtab_node::get_create (TREE_OPERAND (arg0, 0))
11122 ->equal_address_to (symtab_node::get_create
11123 (TREE_OPERAND (arg1, 0)));
11124 else
11125 equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11126 if (equal != 2)
11127 return constant_boolean_node (equal
11128 ? code == EQ_EXPR : code != EQ_EXPR,
11129 type);
11130 }
11131
11132 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11133 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11134 && TREE_CODE (arg1) == INTEGER_CST
11135 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11136 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11137 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
11138 fold_convert_loc (loc,
11139 TREE_TYPE (arg0),
11140 arg1),
11141 TREE_OPERAND (arg0, 1)));
11142
11143 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
11144 if ((TREE_CODE (arg0) == PLUS_EXPR
11145 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
11146 || TREE_CODE (arg0) == MINUS_EXPR)
11147 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
11148 0)),
11149 arg1, 0)
11150 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11151 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11152 {
11153 tree val = TREE_OPERAND (arg0, 1);
11154 return omit_two_operands_loc (loc, type,
11155 fold_build2_loc (loc, code, type,
11156 val,
11157 build_int_cst (TREE_TYPE (val),
11158 0)),
11159 TREE_OPERAND (arg0, 0), arg1);
11160 }
11161
11162 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
11163 if (TREE_CODE (arg0) == MINUS_EXPR
11164 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
11165 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
11166 1)),
11167 arg1, 0)
11168 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
11169 {
11170 return omit_two_operands_loc (loc, type,
11171 code == NE_EXPR
11172 ? boolean_true_node : boolean_false_node,
11173 TREE_OPERAND (arg0, 1), arg1);
11174 }
11175
11176 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11177 if (TREE_CODE (arg0) == ABS_EXPR
11178 && (integer_zerop (arg1) || real_zerop (arg1)))
11179 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
11180
11181 /* If this is an EQ or NE comparison with zero and ARG0 is
11182 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11183 two operations, but the latter can be done in one less insn
11184 on machines that have only two-operand insns or on which a
11185 constant cannot be the first operand. */
11186 if (TREE_CODE (arg0) == BIT_AND_EXPR
11187 && integer_zerop (arg1))
11188 {
11189 tree arg00 = TREE_OPERAND (arg0, 0);
11190 tree arg01 = TREE_OPERAND (arg0, 1);
11191 if (TREE_CODE (arg00) == LSHIFT_EXPR
11192 && integer_onep (TREE_OPERAND (arg00, 0)))
11193 {
11194 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
11195 arg01, TREE_OPERAND (arg00, 1));
11196 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11197 build_int_cst (TREE_TYPE (arg0), 1));
11198 return fold_build2_loc (loc, code, type,
11199 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
11200 arg1);
11201 }
11202 else if (TREE_CODE (arg01) == LSHIFT_EXPR
11203 && integer_onep (TREE_OPERAND (arg01, 0)))
11204 {
11205 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
11206 arg00, TREE_OPERAND (arg01, 1));
11207 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11208 build_int_cst (TREE_TYPE (arg0), 1));
11209 return fold_build2_loc (loc, code, type,
11210 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
11211 arg1);
11212 }
11213 }
11214
11215 /* If this is an NE or EQ comparison of zero against the result of a
11216 signed MOD operation whose second operand is a power of 2, make
11217 the MOD operation unsigned since it is simpler and equivalent. */
11218 if (integer_zerop (arg1)
11219 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11220 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11221 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11222 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11223 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11224 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11225 {
11226 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11227 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
11228 fold_convert_loc (loc, newtype,
11229 TREE_OPERAND (arg0, 0)),
11230 fold_convert_loc (loc, newtype,
11231 TREE_OPERAND (arg0, 1)));
11232
11233 return fold_build2_loc (loc, code, type, newmod,
11234 fold_convert_loc (loc, newtype, arg1));
11235 }
11236
11237 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11238 C1 is a valid shift constant, and C2 is a power of two, i.e.
11239 a single bit. */
11240 if (TREE_CODE (arg0) == BIT_AND_EXPR
11241 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11242 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11243 == INTEGER_CST
11244 && integer_pow2p (TREE_OPERAND (arg0, 1))
11245 && integer_zerop (arg1))
11246 {
11247 tree itype = TREE_TYPE (arg0);
11248 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11249 prec = TYPE_PRECISION (itype);
11250
11251 /* Check for a valid shift count. */
11252 if (wi::ltu_p (arg001, prec))
11253 {
11254 tree arg01 = TREE_OPERAND (arg0, 1);
11255 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11256 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11257 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11258 can be rewritten as (X & (C2 << C1)) != 0. */
11259 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11260 {
11261 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
11262 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
11263 return fold_build2_loc (loc, code, type, tem,
11264 fold_convert_loc (loc, itype, arg1));
11265 }
11266 /* Otherwise, for signed (arithmetic) shifts,
11267 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11268 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11269 else if (!TYPE_UNSIGNED (itype))
11270 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11271 arg000, build_int_cst (itype, 0));
11272 /* Otherwise, of unsigned (logical) shifts,
11273 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11274 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11275 else
11276 return omit_one_operand_loc (loc, type,
11277 code == EQ_EXPR ? integer_one_node
11278 : integer_zero_node,
11279 arg000);
11280 }
11281 }
11282
11283 /* If we have (A & C) == C where C is a power of 2, convert this into
11284 (A & C) != 0. Similarly for NE_EXPR. */
11285 if (TREE_CODE (arg0) == BIT_AND_EXPR
11286 && integer_pow2p (TREE_OPERAND (arg0, 1))
11287 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11288 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11289 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
11290 integer_zero_node));
11291
11292 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11293 bit, then fold the expression into A < 0 or A >= 0. */
11294 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
11295 if (tem)
11296 return tem;
11297
11298 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11299 Similarly for NE_EXPR. */
11300 if (TREE_CODE (arg0) == BIT_AND_EXPR
11301 && TREE_CODE (arg1) == INTEGER_CST
11302 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11303 {
11304 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
11305 TREE_TYPE (TREE_OPERAND (arg0, 1)),
11306 TREE_OPERAND (arg0, 1));
11307 tree dandnotc
11308 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
11309 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
11310 notc);
11311 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11312 if (integer_nonzerop (dandnotc))
11313 return omit_one_operand_loc (loc, type, rslt, arg0);
11314 }
11315
11316 /* If this is a comparison of a field, we may be able to simplify it. */
11317 if ((TREE_CODE (arg0) == COMPONENT_REF
11318 || TREE_CODE (arg0) == BIT_FIELD_REF)
11319 /* Handle the constant case even without -O
11320 to make sure the warnings are given. */
11321 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
11322 {
11323 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
11324 if (t1)
11325 return t1;
11326 }
11327
11328 /* Optimize comparisons of strlen vs zero to a compare of the
11329 first character of the string vs zero. To wit,
11330 strlen(ptr) == 0 => *ptr == 0
11331 strlen(ptr) != 0 => *ptr != 0
11332 Other cases should reduce to one of these two (or a constant)
11333 due to the return value of strlen being unsigned. */
11334 if (TREE_CODE (arg0) == CALL_EXPR
11335 && integer_zerop (arg1))
11336 {
11337 tree fndecl = get_callee_fndecl (arg0);
11338
11339 if (fndecl
11340 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11341 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
11342 && call_expr_nargs (arg0) == 1
11343 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
11344 {
11345 tree iref = build_fold_indirect_ref_loc (loc,
11346 CALL_EXPR_ARG (arg0, 0));
11347 return fold_build2_loc (loc, code, type, iref,
11348 build_int_cst (TREE_TYPE (iref), 0));
11349 }
11350 }
11351
11352 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11353 of X. Similarly fold (X >> C) == 0 into X >= 0. */
11354 if (TREE_CODE (arg0) == RSHIFT_EXPR
11355 && integer_zerop (arg1)
11356 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11357 {
11358 tree arg00 = TREE_OPERAND (arg0, 0);
11359 tree arg01 = TREE_OPERAND (arg0, 1);
11360 tree itype = TREE_TYPE (arg00);
11361 if (wi::eq_p (arg01, element_precision (itype) - 1))
11362 {
11363 if (TYPE_UNSIGNED (itype))
11364 {
11365 itype = signed_type_for (itype);
11366 arg00 = fold_convert_loc (loc, itype, arg00);
11367 }
11368 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11369 type, arg00, build_zero_cst (itype));
11370 }
11371 }
11372
11373 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11374 (X & C) == 0 when C is a single bit. */
11375 if (TREE_CODE (arg0) == BIT_AND_EXPR
11376 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11377 && integer_zerop (arg1)
11378 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11379 {
11380 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
11381 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11382 TREE_OPERAND (arg0, 1));
11383 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11384 type, tem,
11385 fold_convert_loc (loc, TREE_TYPE (arg0),
11386 arg1));
11387 }
11388
11389 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11390 constant C is a power of two, i.e. a single bit. */
11391 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11392 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11393 && integer_zerop (arg1)
11394 && integer_pow2p (TREE_OPERAND (arg0, 1))
11395 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11396 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11397 {
11398 tree arg00 = TREE_OPERAND (arg0, 0);
11399 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11400 arg00, build_int_cst (TREE_TYPE (arg00), 0));
11401 }
11402
11403 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
11404 when is C is a power of two, i.e. a single bit. */
11405 if (TREE_CODE (arg0) == BIT_AND_EXPR
11406 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
11407 && integer_zerop (arg1)
11408 && integer_pow2p (TREE_OPERAND (arg0, 1))
11409 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11410 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11411 {
11412 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11413 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
11414 arg000, TREE_OPERAND (arg0, 1));
11415 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11416 tem, build_int_cst (TREE_TYPE (tem), 0));
11417 }
11418
11419 if (integer_zerop (arg1)
11420 && tree_expr_nonzero_p (arg0))
11421 {
11422 tree res = constant_boolean_node (code==NE_EXPR, type);
11423 return omit_one_operand_loc (loc, type, res, arg0);
11424 }
11425
11426 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
11427 if (TREE_CODE (arg0) == BIT_AND_EXPR
11428 && TREE_CODE (arg1) == BIT_AND_EXPR)
11429 {
11430 tree arg00 = TREE_OPERAND (arg0, 0);
11431 tree arg01 = TREE_OPERAND (arg0, 1);
11432 tree arg10 = TREE_OPERAND (arg1, 0);
11433 tree arg11 = TREE_OPERAND (arg1, 1);
11434 tree itype = TREE_TYPE (arg0);
11435
11436 if (operand_equal_p (arg01, arg11, 0))
11437 return fold_build2_loc (loc, code, type,
11438 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11439 fold_build2_loc (loc,
11440 BIT_XOR_EXPR, itype,
11441 arg00, arg10),
11442 arg01),
11443 build_zero_cst (itype));
11444
11445 if (operand_equal_p (arg01, arg10, 0))
11446 return fold_build2_loc (loc, code, type,
11447 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11448 fold_build2_loc (loc,
11449 BIT_XOR_EXPR, itype,
11450 arg00, arg11),
11451 arg01),
11452 build_zero_cst (itype));
11453
11454 if (operand_equal_p (arg00, arg11, 0))
11455 return fold_build2_loc (loc, code, type,
11456 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11457 fold_build2_loc (loc,
11458 BIT_XOR_EXPR, itype,
11459 arg01, arg10),
11460 arg00),
11461 build_zero_cst (itype));
11462
11463 if (operand_equal_p (arg00, arg10, 0))
11464 return fold_build2_loc (loc, code, type,
11465 fold_build2_loc (loc, BIT_AND_EXPR, itype,
11466 fold_build2_loc (loc,
11467 BIT_XOR_EXPR, itype,
11468 arg01, arg11),
11469 arg00),
11470 build_zero_cst (itype));
11471 }
11472
11473 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11474 && TREE_CODE (arg1) == BIT_XOR_EXPR)
11475 {
11476 tree arg00 = TREE_OPERAND (arg0, 0);
11477 tree arg01 = TREE_OPERAND (arg0, 1);
11478 tree arg10 = TREE_OPERAND (arg1, 0);
11479 tree arg11 = TREE_OPERAND (arg1, 1);
11480 tree itype = TREE_TYPE (arg0);
11481
11482 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
11483 operand_equal_p guarantees no side-effects so we don't need
11484 to use omit_one_operand on Z. */
11485 if (operand_equal_p (arg01, arg11, 0))
11486 return fold_build2_loc (loc, code, type, arg00,
11487 fold_convert_loc (loc, TREE_TYPE (arg00),
11488 arg10));
11489 if (operand_equal_p (arg01, arg10, 0))
11490 return fold_build2_loc (loc, code, type, arg00,
11491 fold_convert_loc (loc, TREE_TYPE (arg00),
11492 arg11));
11493 if (operand_equal_p (arg00, arg11, 0))
11494 return fold_build2_loc (loc, code, type, arg01,
11495 fold_convert_loc (loc, TREE_TYPE (arg01),
11496 arg10));
11497 if (operand_equal_p (arg00, arg10, 0))
11498 return fold_build2_loc (loc, code, type, arg01,
11499 fold_convert_loc (loc, TREE_TYPE (arg01),
11500 arg11));
11501
11502 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11503 if (TREE_CODE (arg01) == INTEGER_CST
11504 && TREE_CODE (arg11) == INTEGER_CST)
11505 {
11506 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
11507 fold_convert_loc (loc, itype, arg11));
11508 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
11509 return fold_build2_loc (loc, code, type, tem,
11510 fold_convert_loc (loc, itype, arg10));
11511 }
11512 }
11513
11514 /* Attempt to simplify equality/inequality comparisons of complex
11515 values. Only lower the comparison if the result is known or
11516 can be simplified to a single scalar comparison. */
11517 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11518 || TREE_CODE (arg0) == COMPLEX_CST)
11519 && (TREE_CODE (arg1) == COMPLEX_EXPR
11520 || TREE_CODE (arg1) == COMPLEX_CST))
11521 {
11522 tree real0, imag0, real1, imag1;
11523 tree rcond, icond;
11524
11525 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11526 {
11527 real0 = TREE_OPERAND (arg0, 0);
11528 imag0 = TREE_OPERAND (arg0, 1);
11529 }
11530 else
11531 {
11532 real0 = TREE_REALPART (arg0);
11533 imag0 = TREE_IMAGPART (arg0);
11534 }
11535
11536 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11537 {
11538 real1 = TREE_OPERAND (arg1, 0);
11539 imag1 = TREE_OPERAND (arg1, 1);
11540 }
11541 else
11542 {
11543 real1 = TREE_REALPART (arg1);
11544 imag1 = TREE_IMAGPART (arg1);
11545 }
11546
11547 rcond = fold_binary_loc (loc, code, type, real0, real1);
11548 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11549 {
11550 if (integer_zerop (rcond))
11551 {
11552 if (code == EQ_EXPR)
11553 return omit_two_operands_loc (loc, type, boolean_false_node,
11554 imag0, imag1);
11555 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11556 }
11557 else
11558 {
11559 if (code == NE_EXPR)
11560 return omit_two_operands_loc (loc, type, boolean_true_node,
11561 imag0, imag1);
11562 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11563 }
11564 }
11565
11566 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11567 if (icond && TREE_CODE (icond) == INTEGER_CST)
11568 {
11569 if (integer_zerop (icond))
11570 {
11571 if (code == EQ_EXPR)
11572 return omit_two_operands_loc (loc, type, boolean_false_node,
11573 real0, real1);
11574 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11575 }
11576 else
11577 {
11578 if (code == NE_EXPR)
11579 return omit_two_operands_loc (loc, type, boolean_true_node,
11580 real0, real1);
11581 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11582 }
11583 }
11584 }
11585
11586 return NULL_TREE;
11587
11588 case LT_EXPR:
11589 case GT_EXPR:
11590 case LE_EXPR:
11591 case GE_EXPR:
11592 tem = fold_comparison (loc, code, type, op0, op1);
11593 if (tem != NULL_TREE)
11594 return tem;
11595
11596 /* Transform comparisons of the form X +- C CMP X. */
11597 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11598 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11599 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11600 && !HONOR_SNANS (arg0))
11601 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11602 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11603 {
11604 tree arg01 = TREE_OPERAND (arg0, 1);
11605 enum tree_code code0 = TREE_CODE (arg0);
11606 int is_positive;
11607
11608 if (TREE_CODE (arg01) == REAL_CST)
11609 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11610 else
11611 is_positive = tree_int_cst_sgn (arg01);
11612
11613 /* (X - c) > X becomes false. */
11614 if (code == GT_EXPR
11615 && ((code0 == MINUS_EXPR && is_positive >= 0)
11616 || (code0 == PLUS_EXPR && is_positive <= 0)))
11617 {
11618 if (TREE_CODE (arg01) == INTEGER_CST
11619 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11620 fold_overflow_warning (("assuming signed overflow does not "
11621 "occur when assuming that (X - c) > X "
11622 "is always false"),
11623 WARN_STRICT_OVERFLOW_ALL);
11624 return constant_boolean_node (0, type);
11625 }
11626
11627 /* Likewise (X + c) < X becomes false. */
11628 if (code == LT_EXPR
11629 && ((code0 == PLUS_EXPR && is_positive >= 0)
11630 || (code0 == MINUS_EXPR && is_positive <= 0)))
11631 {
11632 if (TREE_CODE (arg01) == INTEGER_CST
11633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11634 fold_overflow_warning (("assuming signed overflow does not "
11635 "occur when assuming that "
11636 "(X + c) < X is always false"),
11637 WARN_STRICT_OVERFLOW_ALL);
11638 return constant_boolean_node (0, type);
11639 }
11640
11641 /* Convert (X - c) <= X to true. */
11642 if (!HONOR_NANS (arg1)
11643 && code == LE_EXPR
11644 && ((code0 == MINUS_EXPR && is_positive >= 0)
11645 || (code0 == PLUS_EXPR && is_positive <= 0)))
11646 {
11647 if (TREE_CODE (arg01) == INTEGER_CST
11648 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11649 fold_overflow_warning (("assuming signed overflow does not "
11650 "occur when assuming that "
11651 "(X - c) <= X is always true"),
11652 WARN_STRICT_OVERFLOW_ALL);
11653 return constant_boolean_node (1, type);
11654 }
11655
11656 /* Convert (X + c) >= X to true. */
11657 if (!HONOR_NANS (arg1)
11658 && code == GE_EXPR
11659 && ((code0 == PLUS_EXPR && is_positive >= 0)
11660 || (code0 == MINUS_EXPR && is_positive <= 0)))
11661 {
11662 if (TREE_CODE (arg01) == INTEGER_CST
11663 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11664 fold_overflow_warning (("assuming signed overflow does not "
11665 "occur when assuming that "
11666 "(X + c) >= X is always true"),
11667 WARN_STRICT_OVERFLOW_ALL);
11668 return constant_boolean_node (1, type);
11669 }
11670
11671 if (TREE_CODE (arg01) == INTEGER_CST)
11672 {
11673 /* Convert X + c > X and X - c < X to true for integers. */
11674 if (code == GT_EXPR
11675 && ((code0 == PLUS_EXPR && is_positive > 0)
11676 || (code0 == MINUS_EXPR && is_positive < 0)))
11677 {
11678 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11679 fold_overflow_warning (("assuming signed overflow does "
11680 "not occur when assuming that "
11681 "(X + c) > X is always true"),
11682 WARN_STRICT_OVERFLOW_ALL);
11683 return constant_boolean_node (1, type);
11684 }
11685
11686 if (code == LT_EXPR
11687 && ((code0 == MINUS_EXPR && is_positive > 0)
11688 || (code0 == PLUS_EXPR && is_positive < 0)))
11689 {
11690 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11691 fold_overflow_warning (("assuming signed overflow does "
11692 "not occur when assuming that "
11693 "(X - c) < X is always true"),
11694 WARN_STRICT_OVERFLOW_ALL);
11695 return constant_boolean_node (1, type);
11696 }
11697
11698 /* Convert X + c <= X and X - c >= X to false for integers. */
11699 if (code == LE_EXPR
11700 && ((code0 == PLUS_EXPR && is_positive > 0)
11701 || (code0 == MINUS_EXPR && is_positive < 0)))
11702 {
11703 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11704 fold_overflow_warning (("assuming signed overflow does "
11705 "not occur when assuming that "
11706 "(X + c) <= X is always false"),
11707 WARN_STRICT_OVERFLOW_ALL);
11708 return constant_boolean_node (0, type);
11709 }
11710
11711 if (code == GE_EXPR
11712 && ((code0 == MINUS_EXPR && is_positive > 0)
11713 || (code0 == PLUS_EXPR && is_positive < 0)))
11714 {
11715 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11716 fold_overflow_warning (("assuming signed overflow does "
11717 "not occur when assuming that "
11718 "(X - c) >= X is always false"),
11719 WARN_STRICT_OVERFLOW_ALL);
11720 return constant_boolean_node (0, type);
11721 }
11722 }
11723 }
11724
11725 /* Comparisons with the highest or lowest possible integer of
11726 the specified precision will have known values. */
11727 {
11728 tree arg1_type = TREE_TYPE (arg1);
11729 unsigned int prec = TYPE_PRECISION (arg1_type);
11730
11731 if (TREE_CODE (arg1) == INTEGER_CST
11732 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
11733 {
11734 wide_int max = wi::max_value (arg1_type);
11735 wide_int signed_max = wi::max_value (prec, SIGNED);
11736 wide_int min = wi::min_value (arg1_type);
11737
11738 if (wi::eq_p (arg1, max))
11739 switch (code)
11740 {
11741 case GT_EXPR:
11742 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11743
11744 case GE_EXPR:
11745 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
11746
11747 case LE_EXPR:
11748 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11749
11750 case LT_EXPR:
11751 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
11752
11753 /* The GE_EXPR and LT_EXPR cases above are not normally
11754 reached because of previous transformations. */
11755
11756 default:
11757 break;
11758 }
11759 else if (wi::eq_p (arg1, max - 1))
11760 switch (code)
11761 {
11762 case GT_EXPR:
11763 arg1 = const_binop (PLUS_EXPR, arg1,
11764 build_int_cst (TREE_TYPE (arg1), 1));
11765 return fold_build2_loc (loc, EQ_EXPR, type,
11766 fold_convert_loc (loc,
11767 TREE_TYPE (arg1), arg0),
11768 arg1);
11769 case LE_EXPR:
11770 arg1 = const_binop (PLUS_EXPR, arg1,
11771 build_int_cst (TREE_TYPE (arg1), 1));
11772 return fold_build2_loc (loc, NE_EXPR, type,
11773 fold_convert_loc (loc, TREE_TYPE (arg1),
11774 arg0),
11775 arg1);
11776 default:
11777 break;
11778 }
11779 else if (wi::eq_p (arg1, min))
11780 switch (code)
11781 {
11782 case LT_EXPR:
11783 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11784
11785 case LE_EXPR:
11786 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
11787
11788 case GE_EXPR:
11789 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11790
11791 case GT_EXPR:
11792 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
11793
11794 default:
11795 break;
11796 }
11797 else if (wi::eq_p (arg1, min + 1))
11798 switch (code)
11799 {
11800 case GE_EXPR:
11801 arg1 = const_binop (MINUS_EXPR, arg1,
11802 build_int_cst (TREE_TYPE (arg1), 1));
11803 return fold_build2_loc (loc, NE_EXPR, type,
11804 fold_convert_loc (loc,
11805 TREE_TYPE (arg1), arg0),
11806 arg1);
11807 case LT_EXPR:
11808 arg1 = const_binop (MINUS_EXPR, arg1,
11809 build_int_cst (TREE_TYPE (arg1), 1));
11810 return fold_build2_loc (loc, EQ_EXPR, type,
11811 fold_convert_loc (loc, TREE_TYPE (arg1),
11812 arg0),
11813 arg1);
11814 default:
11815 break;
11816 }
11817
11818 else if (wi::eq_p (arg1, signed_max)
11819 && TYPE_UNSIGNED (arg1_type)
11820 /* We will flip the signedness of the comparison operator
11821 associated with the mode of arg1, so the sign bit is
11822 specified by this mode. Check that arg1 is the signed
11823 max associated with this sign bit. */
11824 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
11825 /* signed_type does not work on pointer types. */
11826 && INTEGRAL_TYPE_P (arg1_type))
11827 {
11828 /* The following case also applies to X < signed_max+1
11829 and X >= signed_max+1 because previous transformations. */
11830 if (code == LE_EXPR || code == GT_EXPR)
11831 {
11832 tree st = signed_type_for (arg1_type);
11833 return fold_build2_loc (loc,
11834 code == LE_EXPR ? GE_EXPR : LT_EXPR,
11835 type, fold_convert_loc (loc, st, arg0),
11836 build_int_cst (st, 0));
11837 }
11838 }
11839 }
11840 }
11841
11842 /* If we are comparing an ABS_EXPR with a constant, we can
11843 convert all the cases into explicit comparisons, but they may
11844 well not be faster than doing the ABS and one comparison.
11845 But ABS (X) <= C is a range comparison, which becomes a subtraction
11846 and a comparison, and is probably faster. */
11847 if (code == LE_EXPR
11848 && TREE_CODE (arg1) == INTEGER_CST
11849 && TREE_CODE (arg0) == ABS_EXPR
11850 && ! TREE_SIDE_EFFECTS (arg0)
11851 && (0 != (tem = negate_expr (arg1)))
11852 && TREE_CODE (tem) == INTEGER_CST
11853 && !TREE_OVERFLOW (tem))
11854 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11855 build2 (GE_EXPR, type,
11856 TREE_OPERAND (arg0, 0), tem),
11857 build2 (LE_EXPR, type,
11858 TREE_OPERAND (arg0, 0), arg1));
11859
11860 /* Convert ABS_EXPR<x> >= 0 to true. */
11861 strict_overflow_p = false;
11862 if (code == GE_EXPR
11863 && (integer_zerop (arg1)
11864 || (! HONOR_NANS (arg0)
11865 && real_zerop (arg1)))
11866 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11867 {
11868 if (strict_overflow_p)
11869 fold_overflow_warning (("assuming signed overflow does not occur "
11870 "when simplifying comparison of "
11871 "absolute value and zero"),
11872 WARN_STRICT_OVERFLOW_CONDITIONAL);
11873 return omit_one_operand_loc (loc, type,
11874 constant_boolean_node (true, type),
11875 arg0);
11876 }
11877
11878 /* Convert ABS_EXPR<x> < 0 to false. */
11879 strict_overflow_p = false;
11880 if (code == LT_EXPR
11881 && (integer_zerop (arg1) || real_zerop (arg1))
11882 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11883 {
11884 if (strict_overflow_p)
11885 fold_overflow_warning (("assuming signed overflow does not occur "
11886 "when simplifying comparison of "
11887 "absolute value and zero"),
11888 WARN_STRICT_OVERFLOW_CONDITIONAL);
11889 return omit_one_operand_loc (loc, type,
11890 constant_boolean_node (false, type),
11891 arg0);
11892 }
11893
11894 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11895 and similarly for >= into !=. */
11896 if ((code == LT_EXPR || code == GE_EXPR)
11897 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11898 && TREE_CODE (arg1) == LSHIFT_EXPR
11899 && integer_onep (TREE_OPERAND (arg1, 0)))
11900 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11901 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11902 TREE_OPERAND (arg1, 1)),
11903 build_zero_cst (TREE_TYPE (arg0)));
11904
11905 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11906 otherwise Y might be >= # of bits in X's type and thus e.g.
11907 (unsigned char) (1 << Y) for Y 15 might be 0.
11908 If the cast is widening, then 1 << Y should have unsigned type,
11909 otherwise if Y is number of bits in the signed shift type minus 1,
11910 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11911 31 might be 0xffffffff80000000. */
11912 if ((code == LT_EXPR || code == GE_EXPR)
11913 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11914 && CONVERT_EXPR_P (arg1)
11915 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11916 && (element_precision (TREE_TYPE (arg1))
11917 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11918 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11919 || (element_precision (TREE_TYPE (arg1))
11920 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11921 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11922 {
11923 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11924 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11925 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11926 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11927 build_zero_cst (TREE_TYPE (arg0)));
11928 }
11929
11930 return NULL_TREE;
11931
11932 case UNORDERED_EXPR:
11933 case ORDERED_EXPR:
11934 case UNLT_EXPR:
11935 case UNLE_EXPR:
11936 case UNGT_EXPR:
11937 case UNGE_EXPR:
11938 case UNEQ_EXPR:
11939 case LTGT_EXPR:
11940 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
11941 {
11942 t1 = fold_relational_const (code, type, arg0, arg1);
11943 if (t1 != NULL_TREE)
11944 return t1;
11945 }
11946
11947 /* If the first operand is NaN, the result is constant. */
11948 if (TREE_CODE (arg0) == REAL_CST
11949 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
11950 && (code != LTGT_EXPR || ! flag_trapping_math))
11951 {
11952 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
11953 ? integer_zero_node
11954 : integer_one_node;
11955 return omit_one_operand_loc (loc, type, t1, arg1);
11956 }
11957
11958 /* If the second operand is NaN, the result is constant. */
11959 if (TREE_CODE (arg1) == REAL_CST
11960 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
11961 && (code != LTGT_EXPR || ! flag_trapping_math))
11962 {
11963 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
11964 ? integer_zero_node
11965 : integer_one_node;
11966 return omit_one_operand_loc (loc, type, t1, arg0);
11967 }
11968
11969 /* Simplify unordered comparison of something with itself. */
11970 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
11971 && operand_equal_p (arg0, arg1, 0))
11972 return constant_boolean_node (1, type);
11973
11974 if (code == LTGT_EXPR
11975 && !flag_trapping_math
11976 && operand_equal_p (arg0, arg1, 0))
11977 return constant_boolean_node (0, type);
11978
11979 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11980 {
11981 tree targ0 = strip_float_extensions (arg0);
11982 tree targ1 = strip_float_extensions (arg1);
11983 tree newtype = TREE_TYPE (targ0);
11984
11985 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11986 newtype = TREE_TYPE (targ1);
11987
11988 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11989 return fold_build2_loc (loc, code, type,
11990 fold_convert_loc (loc, newtype, targ0),
11991 fold_convert_loc (loc, newtype, targ1));
11992 }
11993
11994 return NULL_TREE;
11995
11996 case COMPOUND_EXPR:
11997 /* When pedantic, a compound expression can be neither an lvalue
11998 nor an integer constant expression. */
11999 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12000 return NULL_TREE;
12001 /* Don't let (0, 0) be null pointer constant. */
12002 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12003 : fold_convert_loc (loc, type, arg1);
12004 return pedantic_non_lvalue_loc (loc, tem);
12005
12006 case ASSERT_EXPR:
12007 /* An ASSERT_EXPR should never be passed to fold_binary. */
12008 gcc_unreachable ();
12009
12010 default:
12011 return NULL_TREE;
12012 } /* switch (code) */
12013 }
12014
12015 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12016 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
12017 of GOTO_EXPR. */
12018
12019 static tree
12020 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
12021 {
12022 switch (TREE_CODE (*tp))
12023 {
12024 case LABEL_EXPR:
12025 return *tp;
12026
12027 case GOTO_EXPR:
12028 *walk_subtrees = 0;
12029
12030 /* ... fall through ... */
12031
12032 default:
12033 return NULL_TREE;
12034 }
12035 }
12036
12037 /* Return whether the sub-tree ST contains a label which is accessible from
12038 outside the sub-tree. */
12039
12040 static bool
12041 contains_label_p (tree st)
12042 {
12043 return
12044 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
12045 }
12046
12047 /* Fold a ternary expression of code CODE and type TYPE with operands
12048 OP0, OP1, and OP2. Return the folded expression if folding is
12049 successful. Otherwise, return NULL_TREE. */
12050
12051 tree
12052 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12053 tree op0, tree op1, tree op2)
12054 {
12055 tree tem;
12056 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12057 enum tree_code_class kind = TREE_CODE_CLASS (code);
12058
12059 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12060 && TREE_CODE_LENGTH (code) == 3);
12061
12062 /* If this is a commutative operation, and OP0 is a constant, move it
12063 to OP1 to reduce the number of tests below. */
12064 if (commutative_ternary_tree_code (code)
12065 && tree_swap_operands_p (op0, op1, true))
12066 return fold_build3_loc (loc, code, type, op1, op0, op2);
12067
12068 tem = generic_simplify (loc, code, type, op0, op1, op2);
12069 if (tem)
12070 return tem;
12071
12072 /* Strip any conversions that don't change the mode. This is safe
12073 for every expression, except for a comparison expression because
12074 its signedness is derived from its operands. So, in the latter
12075 case, only strip conversions that don't change the signedness.
12076
12077 Note that this is done as an internal manipulation within the
12078 constant folder, in order to find the simplest representation of
12079 the arguments so that their form can be studied. In any cases,
12080 the appropriate type conversions should be put back in the tree
12081 that will get out of the constant folder. */
12082 if (op0)
12083 {
12084 arg0 = op0;
12085 STRIP_NOPS (arg0);
12086 }
12087
12088 if (op1)
12089 {
12090 arg1 = op1;
12091 STRIP_NOPS (arg1);
12092 }
12093
12094 if (op2)
12095 {
12096 arg2 = op2;
12097 STRIP_NOPS (arg2);
12098 }
12099
12100 switch (code)
12101 {
12102 case COMPONENT_REF:
12103 if (TREE_CODE (arg0) == CONSTRUCTOR
12104 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12105 {
12106 unsigned HOST_WIDE_INT idx;
12107 tree field, value;
12108 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12109 if (field == arg1)
12110 return value;
12111 }
12112 return NULL_TREE;
12113
12114 case COND_EXPR:
12115 case VEC_COND_EXPR:
12116 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12117 so all simple results must be passed through pedantic_non_lvalue. */
12118 if (TREE_CODE (arg0) == INTEGER_CST)
12119 {
12120 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12121 tem = integer_zerop (arg0) ? op2 : op1;
12122 /* Only optimize constant conditions when the selected branch
12123 has the same type as the COND_EXPR. This avoids optimizing
12124 away "c ? x : throw", where the throw has a void type.
12125 Avoid throwing away that operand which contains label. */
12126 if ((!TREE_SIDE_EFFECTS (unused_op)
12127 || !contains_label_p (unused_op))
12128 && (! VOID_TYPE_P (TREE_TYPE (tem))
12129 || VOID_TYPE_P (type)))
12130 return pedantic_non_lvalue_loc (loc, tem);
12131 return NULL_TREE;
12132 }
12133 else if (TREE_CODE (arg0) == VECTOR_CST)
12134 {
12135 if ((TREE_CODE (arg1) == VECTOR_CST
12136 || TREE_CODE (arg1) == CONSTRUCTOR)
12137 && (TREE_CODE (arg2) == VECTOR_CST
12138 || TREE_CODE (arg2) == CONSTRUCTOR))
12139 {
12140 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
12141 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
12142 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
12143 for (i = 0; i < nelts; i++)
12144 {
12145 tree val = VECTOR_CST_ELT (arg0, i);
12146 if (integer_all_onesp (val))
12147 sel[i] = i;
12148 else if (integer_zerop (val))
12149 sel[i] = nelts + i;
12150 else /* Currently unreachable. */
12151 return NULL_TREE;
12152 }
12153 tree t = fold_vec_perm (type, arg1, arg2, sel);
12154 if (t != NULL_TREE)
12155 return t;
12156 }
12157 }
12158
12159 /* If we have A op B ? A : C, we may be able to convert this to a
12160 simpler expression, depending on the operation and the values
12161 of B and C. Signed zeros prevent all of these transformations,
12162 for reasons given above each one.
12163
12164 Also try swapping the arguments and inverting the conditional. */
12165 if (COMPARISON_CLASS_P (arg0)
12166 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12167 arg1, TREE_OPERAND (arg0, 1))
12168 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
12169 {
12170 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
12171 if (tem)
12172 return tem;
12173 }
12174
12175 if (COMPARISON_CLASS_P (arg0)
12176 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12177 op2,
12178 TREE_OPERAND (arg0, 1))
12179 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
12180 {
12181 location_t loc0 = expr_location_or (arg0, loc);
12182 tem = fold_invert_truthvalue (loc0, arg0);
12183 if (tem && COMPARISON_CLASS_P (tem))
12184 {
12185 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
12186 if (tem)
12187 return tem;
12188 }
12189 }
12190
12191 /* If the second operand is simpler than the third, swap them
12192 since that produces better jump optimization results. */
12193 if (truth_value_p (TREE_CODE (arg0))
12194 && tree_swap_operands_p (op1, op2, false))
12195 {
12196 location_t loc0 = expr_location_or (arg0, loc);
12197 /* See if this can be inverted. If it can't, possibly because
12198 it was a floating-point inequality comparison, don't do
12199 anything. */
12200 tem = fold_invert_truthvalue (loc0, arg0);
12201 if (tem)
12202 return fold_build3_loc (loc, code, type, tem, op2, op1);
12203 }
12204
12205 /* Convert A ? 1 : 0 to simply A. */
12206 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12207 : (integer_onep (op1)
12208 && !VECTOR_TYPE_P (type)))
12209 && integer_zerop (op2)
12210 /* If we try to convert OP0 to our type, the
12211 call to fold will try to move the conversion inside
12212 a COND, which will recurse. In that case, the COND_EXPR
12213 is probably the best choice, so leave it alone. */
12214 && type == TREE_TYPE (arg0))
12215 return pedantic_non_lvalue_loc (loc, arg0);
12216
12217 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12218 over COND_EXPR in cases such as floating point comparisons. */
12219 if (integer_zerop (op1)
12220 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
12221 : (integer_onep (op2)
12222 && !VECTOR_TYPE_P (type)))
12223 && truth_value_p (TREE_CODE (arg0)))
12224 return pedantic_non_lvalue_loc (loc,
12225 fold_convert_loc (loc, type,
12226 invert_truthvalue_loc (loc,
12227 arg0)));
12228
12229 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12230 if (TREE_CODE (arg0) == LT_EXPR
12231 && integer_zerop (TREE_OPERAND (arg0, 1))
12232 && integer_zerop (op2)
12233 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12234 {
12235 /* sign_bit_p looks through both zero and sign extensions,
12236 but for this optimization only sign extensions are
12237 usable. */
12238 tree tem2 = TREE_OPERAND (arg0, 0);
12239 while (tem != tem2)
12240 {
12241 if (TREE_CODE (tem2) != NOP_EXPR
12242 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12243 {
12244 tem = NULL_TREE;
12245 break;
12246 }
12247 tem2 = TREE_OPERAND (tem2, 0);
12248 }
12249 /* sign_bit_p only checks ARG1 bits within A's precision.
12250 If <sign bit of A> has wider type than A, bits outside
12251 of A's precision in <sign bit of A> need to be checked.
12252 If they are all 0, this optimization needs to be done
12253 in unsigned A's type, if they are all 1 in signed A's type,
12254 otherwise this can't be done. */
12255 if (tem
12256 && TYPE_PRECISION (TREE_TYPE (tem))
12257 < TYPE_PRECISION (TREE_TYPE (arg1))
12258 && TYPE_PRECISION (TREE_TYPE (tem))
12259 < TYPE_PRECISION (type))
12260 {
12261 int inner_width, outer_width;
12262 tree tem_type;
12263
12264 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12265 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12266 if (outer_width > TYPE_PRECISION (type))
12267 outer_width = TYPE_PRECISION (type);
12268
12269 wide_int mask = wi::shifted_mask
12270 (inner_width, outer_width - inner_width, false,
12271 TYPE_PRECISION (TREE_TYPE (arg1)));
12272
12273 wide_int common = mask & arg1;
12274 if (common == mask)
12275 {
12276 tem_type = signed_type_for (TREE_TYPE (tem));
12277 tem = fold_convert_loc (loc, tem_type, tem);
12278 }
12279 else if (common == 0)
12280 {
12281 tem_type = unsigned_type_for (TREE_TYPE (tem));
12282 tem = fold_convert_loc (loc, tem_type, tem);
12283 }
12284 else
12285 tem = NULL;
12286 }
12287
12288 if (tem)
12289 return
12290 fold_convert_loc (loc, type,
12291 fold_build2_loc (loc, BIT_AND_EXPR,
12292 TREE_TYPE (tem), tem,
12293 fold_convert_loc (loc,
12294 TREE_TYPE (tem),
12295 arg1)));
12296 }
12297
12298 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12299 already handled above. */
12300 if (TREE_CODE (arg0) == BIT_AND_EXPR
12301 && integer_onep (TREE_OPERAND (arg0, 1))
12302 && integer_zerop (op2)
12303 && integer_pow2p (arg1))
12304 {
12305 tree tem = TREE_OPERAND (arg0, 0);
12306 STRIP_NOPS (tem);
12307 if (TREE_CODE (tem) == RSHIFT_EXPR
12308 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12309 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12310 tree_to_uhwi (TREE_OPERAND (tem, 1)))
12311 return fold_build2_loc (loc, BIT_AND_EXPR, type,
12312 TREE_OPERAND (tem, 0), arg1);
12313 }
12314
12315 /* A & N ? N : 0 is simply A & N if N is a power of two. This
12316 is probably obsolete because the first operand should be a
12317 truth value (that's why we have the two cases above), but let's
12318 leave it in until we can confirm this for all front-ends. */
12319 if (integer_zerop (op2)
12320 && TREE_CODE (arg0) == NE_EXPR
12321 && integer_zerop (TREE_OPERAND (arg0, 1))
12322 && integer_pow2p (arg1)
12323 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12324 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12325 arg1, OEP_ONLY_CONST))
12326 return pedantic_non_lvalue_loc (loc,
12327 fold_convert_loc (loc, type,
12328 TREE_OPERAND (arg0, 0)));
12329
12330 /* Disable the transformations below for vectors, since
12331 fold_binary_op_with_conditional_arg may undo them immediately,
12332 yielding an infinite loop. */
12333 if (code == VEC_COND_EXPR)
12334 return NULL_TREE;
12335
12336 /* Convert A ? B : 0 into A && B if A and B are truth values. */
12337 if (integer_zerop (op2)
12338 && truth_value_p (TREE_CODE (arg0))
12339 && truth_value_p (TREE_CODE (arg1))
12340 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12341 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12342 : TRUTH_ANDIF_EXPR,
12343 type, fold_convert_loc (loc, type, arg0), arg1);
12344
12345 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12346 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12347 && truth_value_p (TREE_CODE (arg0))
12348 && truth_value_p (TREE_CODE (arg1))
12349 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12350 {
12351 location_t loc0 = expr_location_or (arg0, loc);
12352 /* Only perform transformation if ARG0 is easily inverted. */
12353 tem = fold_invert_truthvalue (loc0, arg0);
12354 if (tem)
12355 return fold_build2_loc (loc, code == VEC_COND_EXPR
12356 ? BIT_IOR_EXPR
12357 : TRUTH_ORIF_EXPR,
12358 type, fold_convert_loc (loc, type, tem),
12359 arg1);
12360 }
12361
12362 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12363 if (integer_zerop (arg1)
12364 && truth_value_p (TREE_CODE (arg0))
12365 && truth_value_p (TREE_CODE (op2))
12366 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12367 {
12368 location_t loc0 = expr_location_or (arg0, loc);
12369 /* Only perform transformation if ARG0 is easily inverted. */
12370 tem = fold_invert_truthvalue (loc0, arg0);
12371 if (tem)
12372 return fold_build2_loc (loc, code == VEC_COND_EXPR
12373 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
12374 type, fold_convert_loc (loc, type, tem),
12375 op2);
12376 }
12377
12378 /* Convert A ? 1 : B into A || B if A and B are truth values. */
12379 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
12380 && truth_value_p (TREE_CODE (arg0))
12381 && truth_value_p (TREE_CODE (op2))
12382 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12383 return fold_build2_loc (loc, code == VEC_COND_EXPR
12384 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
12385 type, fold_convert_loc (loc, type, arg0), op2);
12386
12387 return NULL_TREE;
12388
12389 case CALL_EXPR:
12390 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
12391 of fold_ternary on them. */
12392 gcc_unreachable ();
12393
12394 case BIT_FIELD_REF:
12395 if ((TREE_CODE (arg0) == VECTOR_CST
12396 || (TREE_CODE (arg0) == CONSTRUCTOR
12397 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
12398 && (type == TREE_TYPE (TREE_TYPE (arg0))
12399 || (TREE_CODE (type) == VECTOR_TYPE
12400 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
12401 {
12402 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
12403 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
12404 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
12405 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
12406
12407 if (n != 0
12408 && (idx % width) == 0
12409 && (n % width) == 0
12410 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
12411 {
12412 idx = idx / width;
12413 n = n / width;
12414
12415 if (TREE_CODE (arg0) == VECTOR_CST)
12416 {
12417 if (n == 1)
12418 return VECTOR_CST_ELT (arg0, idx);
12419
12420 tree *vals = XALLOCAVEC (tree, n);
12421 for (unsigned i = 0; i < n; ++i)
12422 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
12423 return build_vector (type, vals);
12424 }
12425
12426 /* Constructor elements can be subvectors. */
12427 unsigned HOST_WIDE_INT k = 1;
12428 if (CONSTRUCTOR_NELTS (arg0) != 0)
12429 {
12430 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
12431 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
12432 k = TYPE_VECTOR_SUBPARTS (cons_elem);
12433 }
12434
12435 /* We keep an exact subset of the constructor elements. */
12436 if ((idx % k) == 0 && (n % k) == 0)
12437 {
12438 if (CONSTRUCTOR_NELTS (arg0) == 0)
12439 return build_constructor (type, NULL);
12440 idx /= k;
12441 n /= k;
12442 if (n == 1)
12443 {
12444 if (idx < CONSTRUCTOR_NELTS (arg0))
12445 return CONSTRUCTOR_ELT (arg0, idx)->value;
12446 return build_zero_cst (type);
12447 }
12448
12449 vec<constructor_elt, va_gc> *vals;
12450 vec_alloc (vals, n);
12451 for (unsigned i = 0;
12452 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
12453 ++i)
12454 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
12455 CONSTRUCTOR_ELT
12456 (arg0, idx + i)->value);
12457 return build_constructor (type, vals);
12458 }
12459 /* The bitfield references a single constructor element. */
12460 else if (idx + n <= (idx / k + 1) * k)
12461 {
12462 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
12463 return build_zero_cst (type);
12464 else if (n == k)
12465 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
12466 else
12467 return fold_build3_loc (loc, code, type,
12468 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
12469 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
12470 }
12471 }
12472 }
12473
12474 /* A bit-field-ref that referenced the full argument can be stripped. */
12475 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12476 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
12477 && integer_zerop (op2))
12478 return fold_convert_loc (loc, type, arg0);
12479
12480 /* On constants we can use native encode/interpret to constant
12481 fold (nearly) all BIT_FIELD_REFs. */
12482 if (CONSTANT_CLASS_P (arg0)
12483 && can_native_interpret_type_p (type)
12484 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
12485 /* This limitation should not be necessary, we just need to
12486 round this up to mode size. */
12487 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
12488 /* Need bit-shifting of the buffer to relax the following. */
12489 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
12490 {
12491 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
12492 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
12493 unsigned HOST_WIDE_INT clen;
12494 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
12495 /* ??? We cannot tell native_encode_expr to start at
12496 some random byte only. So limit us to a reasonable amount
12497 of work. */
12498 if (clen <= 4096)
12499 {
12500 unsigned char *b = XALLOCAVEC (unsigned char, clen);
12501 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
12502 if (len > 0
12503 && len * BITS_PER_UNIT >= bitpos + bitsize)
12504 {
12505 tree v = native_interpret_expr (type,
12506 b + bitpos / BITS_PER_UNIT,
12507 bitsize / BITS_PER_UNIT);
12508 if (v)
12509 return v;
12510 }
12511 }
12512 }
12513
12514 return NULL_TREE;
12515
12516 case FMA_EXPR:
12517 /* For integers we can decompose the FMA if possible. */
12518 if (TREE_CODE (arg0) == INTEGER_CST
12519 && TREE_CODE (arg1) == INTEGER_CST)
12520 return fold_build2_loc (loc, PLUS_EXPR, type,
12521 const_binop (MULT_EXPR, arg0, arg1), arg2);
12522 if (integer_zerop (arg2))
12523 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12524
12525 return fold_fma (loc, type, arg0, arg1, arg2);
12526
12527 case VEC_PERM_EXPR:
12528 if (TREE_CODE (arg2) == VECTOR_CST)
12529 {
12530 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
12531 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
12532 unsigned char *sel2 = sel + nelts;
12533 bool need_mask_canon = false;
12534 bool need_mask_canon2 = false;
12535 bool all_in_vec0 = true;
12536 bool all_in_vec1 = true;
12537 bool maybe_identity = true;
12538 bool single_arg = (op0 == op1);
12539 bool changed = false;
12540
12541 mask2 = 2 * nelts - 1;
12542 mask = single_arg ? (nelts - 1) : mask2;
12543 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
12544 for (i = 0; i < nelts; i++)
12545 {
12546 tree val = VECTOR_CST_ELT (arg2, i);
12547 if (TREE_CODE (val) != INTEGER_CST)
12548 return NULL_TREE;
12549
12550 /* Make sure that the perm value is in an acceptable
12551 range. */
12552 wide_int t = val;
12553 need_mask_canon |= wi::gtu_p (t, mask);
12554 need_mask_canon2 |= wi::gtu_p (t, mask2);
12555 sel[i] = t.to_uhwi () & mask;
12556 sel2[i] = t.to_uhwi () & mask2;
12557
12558 if (sel[i] < nelts)
12559 all_in_vec1 = false;
12560 else
12561 all_in_vec0 = false;
12562
12563 if ((sel[i] & (nelts-1)) != i)
12564 maybe_identity = false;
12565 }
12566
12567 if (maybe_identity)
12568 {
12569 if (all_in_vec0)
12570 return op0;
12571 if (all_in_vec1)
12572 return op1;
12573 }
12574
12575 if (all_in_vec0)
12576 op1 = op0;
12577 else if (all_in_vec1)
12578 {
12579 op0 = op1;
12580 for (i = 0; i < nelts; i++)
12581 sel[i] -= nelts;
12582 need_mask_canon = true;
12583 }
12584
12585 if ((TREE_CODE (op0) == VECTOR_CST
12586 || TREE_CODE (op0) == CONSTRUCTOR)
12587 && (TREE_CODE (op1) == VECTOR_CST
12588 || TREE_CODE (op1) == CONSTRUCTOR))
12589 {
12590 tree t = fold_vec_perm (type, op0, op1, sel);
12591 if (t != NULL_TREE)
12592 return t;
12593 }
12594
12595 if (op0 == op1 && !single_arg)
12596 changed = true;
12597
12598 /* Some targets are deficient and fail to expand a single
12599 argument permutation while still allowing an equivalent
12600 2-argument version. */
12601 if (need_mask_canon && arg2 == op2
12602 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
12603 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
12604 {
12605 need_mask_canon = need_mask_canon2;
12606 sel = sel2;
12607 }
12608
12609 if (need_mask_canon && arg2 == op2)
12610 {
12611 tree *tsel = XALLOCAVEC (tree, nelts);
12612 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
12613 for (i = 0; i < nelts; i++)
12614 tsel[i] = build_int_cst (eltype, sel[i]);
12615 op2 = build_vector (TREE_TYPE (arg2), tsel);
12616 changed = true;
12617 }
12618
12619 if (changed)
12620 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
12621 }
12622 return NULL_TREE;
12623
12624 default:
12625 return NULL_TREE;
12626 } /* switch (code) */
12627 }
12628
12629 /* Perform constant folding and related simplification of EXPR.
12630 The related simplifications include x*1 => x, x*0 => 0, etc.,
12631 and application of the associative law.
12632 NOP_EXPR conversions may be removed freely (as long as we
12633 are careful not to change the type of the overall expression).
12634 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12635 but we can constant-fold them if they have constant operands. */
12636
12637 #ifdef ENABLE_FOLD_CHECKING
12638 # define fold(x) fold_1 (x)
12639 static tree fold_1 (tree);
12640 static
12641 #endif
12642 tree
12643 fold (tree expr)
12644 {
12645 const tree t = expr;
12646 enum tree_code code = TREE_CODE (t);
12647 enum tree_code_class kind = TREE_CODE_CLASS (code);
12648 tree tem;
12649 location_t loc = EXPR_LOCATION (expr);
12650
12651 /* Return right away if a constant. */
12652 if (kind == tcc_constant)
12653 return t;
12654
12655 /* CALL_EXPR-like objects with variable numbers of operands are
12656 treated specially. */
12657 if (kind == tcc_vl_exp)
12658 {
12659 if (code == CALL_EXPR)
12660 {
12661 tem = fold_call_expr (loc, expr, false);
12662 return tem ? tem : expr;
12663 }
12664 return expr;
12665 }
12666
12667 if (IS_EXPR_CODE_CLASS (kind))
12668 {
12669 tree type = TREE_TYPE (t);
12670 tree op0, op1, op2;
12671
12672 switch (TREE_CODE_LENGTH (code))
12673 {
12674 case 1:
12675 op0 = TREE_OPERAND (t, 0);
12676 tem = fold_unary_loc (loc, code, type, op0);
12677 return tem ? tem : expr;
12678 case 2:
12679 op0 = TREE_OPERAND (t, 0);
12680 op1 = TREE_OPERAND (t, 1);
12681 tem = fold_binary_loc (loc, code, type, op0, op1);
12682 return tem ? tem : expr;
12683 case 3:
12684 op0 = TREE_OPERAND (t, 0);
12685 op1 = TREE_OPERAND (t, 1);
12686 op2 = TREE_OPERAND (t, 2);
12687 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12688 return tem ? tem : expr;
12689 default:
12690 break;
12691 }
12692 }
12693
12694 switch (code)
12695 {
12696 case ARRAY_REF:
12697 {
12698 tree op0 = TREE_OPERAND (t, 0);
12699 tree op1 = TREE_OPERAND (t, 1);
12700
12701 if (TREE_CODE (op1) == INTEGER_CST
12702 && TREE_CODE (op0) == CONSTRUCTOR
12703 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12704 {
12705 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
12706 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
12707 unsigned HOST_WIDE_INT begin = 0;
12708
12709 /* Find a matching index by means of a binary search. */
12710 while (begin != end)
12711 {
12712 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
12713 tree index = (*elts)[middle].index;
12714
12715 if (TREE_CODE (index) == INTEGER_CST
12716 && tree_int_cst_lt (index, op1))
12717 begin = middle + 1;
12718 else if (TREE_CODE (index) == INTEGER_CST
12719 && tree_int_cst_lt (op1, index))
12720 end = middle;
12721 else if (TREE_CODE (index) == RANGE_EXPR
12722 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
12723 begin = middle + 1;
12724 else if (TREE_CODE (index) == RANGE_EXPR
12725 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
12726 end = middle;
12727 else
12728 return (*elts)[middle].value;
12729 }
12730 }
12731
12732 return t;
12733 }
12734
12735 /* Return a VECTOR_CST if possible. */
12736 case CONSTRUCTOR:
12737 {
12738 tree type = TREE_TYPE (t);
12739 if (TREE_CODE (type) != VECTOR_TYPE)
12740 return t;
12741
12742 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
12743 unsigned HOST_WIDE_INT idx, pos = 0;
12744 tree value;
12745
12746 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
12747 {
12748 if (!CONSTANT_CLASS_P (value))
12749 return t;
12750 if (TREE_CODE (value) == VECTOR_CST)
12751 {
12752 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
12753 vec[pos++] = VECTOR_CST_ELT (value, i);
12754 }
12755 else
12756 vec[pos++] = value;
12757 }
12758 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
12759 vec[pos] = build_zero_cst (TREE_TYPE (type));
12760
12761 return build_vector (type, vec);
12762 }
12763
12764 case CONST_DECL:
12765 return fold (DECL_INITIAL (t));
12766
12767 default:
12768 return t;
12769 } /* switch (code) */
12770 }
12771
12772 #ifdef ENABLE_FOLD_CHECKING
12773 #undef fold
12774
12775 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12776 hash_table<nofree_ptr_hash<const tree_node> > *);
12777 static void fold_check_failed (const_tree, const_tree);
12778 void print_fold_checksum (const_tree);
12779
12780 /* When --enable-checking=fold, compute a digest of expr before
12781 and after actual fold call to see if fold did not accidentally
12782 change original expr. */
12783
12784 tree
12785 fold (tree expr)
12786 {
12787 tree ret;
12788 struct md5_ctx ctx;
12789 unsigned char checksum_before[16], checksum_after[16];
12790 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12791
12792 md5_init_ctx (&ctx);
12793 fold_checksum_tree (expr, &ctx, &ht);
12794 md5_finish_ctx (&ctx, checksum_before);
12795 ht.empty ();
12796
12797 ret = fold_1 (expr);
12798
12799 md5_init_ctx (&ctx);
12800 fold_checksum_tree (expr, &ctx, &ht);
12801 md5_finish_ctx (&ctx, checksum_after);
12802
12803 if (memcmp (checksum_before, checksum_after, 16))
12804 fold_check_failed (expr, ret);
12805
12806 return ret;
12807 }
12808
12809 void
12810 print_fold_checksum (const_tree expr)
12811 {
12812 struct md5_ctx ctx;
12813 unsigned char checksum[16], cnt;
12814 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12815
12816 md5_init_ctx (&ctx);
12817 fold_checksum_tree (expr, &ctx, &ht);
12818 md5_finish_ctx (&ctx, checksum);
12819 for (cnt = 0; cnt < 16; ++cnt)
12820 fprintf (stderr, "%02x", checksum[cnt]);
12821 putc ('\n', stderr);
12822 }
12823
12824 static void
12825 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12826 {
12827 internal_error ("fold check: original tree changed by fold");
12828 }
12829
12830 static void
12831 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12832 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12833 {
12834 const tree_node **slot;
12835 enum tree_code code;
12836 union tree_node buf;
12837 int i, len;
12838
12839 recursive_label:
12840 if (expr == NULL)
12841 return;
12842 slot = ht->find_slot (expr, INSERT);
12843 if (*slot != NULL)
12844 return;
12845 *slot = expr;
12846 code = TREE_CODE (expr);
12847 if (TREE_CODE_CLASS (code) == tcc_declaration
12848 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12849 {
12850 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12851 memcpy ((char *) &buf, expr, tree_size (expr));
12852 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12853 buf.decl_with_vis.symtab_node = NULL;
12854 expr = (tree) &buf;
12855 }
12856 else if (TREE_CODE_CLASS (code) == tcc_type
12857 && (TYPE_POINTER_TO (expr)
12858 || TYPE_REFERENCE_TO (expr)
12859 || TYPE_CACHED_VALUES_P (expr)
12860 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12861 || TYPE_NEXT_VARIANT (expr)))
12862 {
12863 /* Allow these fields to be modified. */
12864 tree tmp;
12865 memcpy ((char *) &buf, expr, tree_size (expr));
12866 expr = tmp = (tree) &buf;
12867 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12868 TYPE_POINTER_TO (tmp) = NULL;
12869 TYPE_REFERENCE_TO (tmp) = NULL;
12870 TYPE_NEXT_VARIANT (tmp) = NULL;
12871 if (TYPE_CACHED_VALUES_P (tmp))
12872 {
12873 TYPE_CACHED_VALUES_P (tmp) = 0;
12874 TYPE_CACHED_VALUES (tmp) = NULL;
12875 }
12876 }
12877 md5_process_bytes (expr, tree_size (expr), ctx);
12878 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12879 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12880 if (TREE_CODE_CLASS (code) != tcc_type
12881 && TREE_CODE_CLASS (code) != tcc_declaration
12882 && code != TREE_LIST
12883 && code != SSA_NAME
12884 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12885 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12886 switch (TREE_CODE_CLASS (code))
12887 {
12888 case tcc_constant:
12889 switch (code)
12890 {
12891 case STRING_CST:
12892 md5_process_bytes (TREE_STRING_POINTER (expr),
12893 TREE_STRING_LENGTH (expr), ctx);
12894 break;
12895 case COMPLEX_CST:
12896 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12897 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12898 break;
12899 case VECTOR_CST:
12900 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12901 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12902 break;
12903 default:
12904 break;
12905 }
12906 break;
12907 case tcc_exceptional:
12908 switch (code)
12909 {
12910 case TREE_LIST:
12911 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12912 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12913 expr = TREE_CHAIN (expr);
12914 goto recursive_label;
12915 break;
12916 case TREE_VEC:
12917 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12918 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12919 break;
12920 default:
12921 break;
12922 }
12923 break;
12924 case tcc_expression:
12925 case tcc_reference:
12926 case tcc_comparison:
12927 case tcc_unary:
12928 case tcc_binary:
12929 case tcc_statement:
12930 case tcc_vl_exp:
12931 len = TREE_OPERAND_LENGTH (expr);
12932 for (i = 0; i < len; ++i)
12933 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12934 break;
12935 case tcc_declaration:
12936 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12937 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12938 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12939 {
12940 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12941 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12942 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12943 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12944 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12945 }
12946
12947 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12948 {
12949 if (TREE_CODE (expr) == FUNCTION_DECL)
12950 {
12951 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12952 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12953 }
12954 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12955 }
12956 break;
12957 case tcc_type:
12958 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12959 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12960 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12961 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12962 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12963 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12964 if (INTEGRAL_TYPE_P (expr)
12965 || SCALAR_FLOAT_TYPE_P (expr))
12966 {
12967 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12968 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12969 }
12970 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12971 if (TREE_CODE (expr) == RECORD_TYPE
12972 || TREE_CODE (expr) == UNION_TYPE
12973 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12974 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12975 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12976 break;
12977 default:
12978 break;
12979 }
12980 }
12981
12982 /* Helper function for outputting the checksum of a tree T. When
12983 debugging with gdb, you can "define mynext" to be "next" followed
12984 by "call debug_fold_checksum (op0)", then just trace down till the
12985 outputs differ. */
12986
12987 DEBUG_FUNCTION void
12988 debug_fold_checksum (const_tree t)
12989 {
12990 int i;
12991 unsigned char checksum[16];
12992 struct md5_ctx ctx;
12993 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12994
12995 md5_init_ctx (&ctx);
12996 fold_checksum_tree (t, &ctx, &ht);
12997 md5_finish_ctx (&ctx, checksum);
12998 ht.empty ();
12999
13000 for (i = 0; i < 16; i++)
13001 fprintf (stderr, "%d ", checksum[i]);
13002
13003 fprintf (stderr, "\n");
13004 }
13005
13006 #endif
13007
13008 /* Fold a unary tree expression with code CODE of type TYPE with an
13009 operand OP0. LOC is the location of the resulting expression.
13010 Return a folded expression if successful. Otherwise, return a tree
13011 expression with code CODE of type TYPE with an operand OP0. */
13012
13013 tree
13014 fold_build1_stat_loc (location_t loc,
13015 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13016 {
13017 tree tem;
13018 #ifdef ENABLE_FOLD_CHECKING
13019 unsigned char checksum_before[16], checksum_after[16];
13020 struct md5_ctx ctx;
13021 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13022
13023 md5_init_ctx (&ctx);
13024 fold_checksum_tree (op0, &ctx, &ht);
13025 md5_finish_ctx (&ctx, checksum_before);
13026 ht.empty ();
13027 #endif
13028
13029 tem = fold_unary_loc (loc, code, type, op0);
13030 if (!tem)
13031 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13032
13033 #ifdef ENABLE_FOLD_CHECKING
13034 md5_init_ctx (&ctx);
13035 fold_checksum_tree (op0, &ctx, &ht);
13036 md5_finish_ctx (&ctx, checksum_after);
13037
13038 if (memcmp (checksum_before, checksum_after, 16))
13039 fold_check_failed (op0, tem);
13040 #endif
13041 return tem;
13042 }
13043
13044 /* Fold a binary tree expression with code CODE of type TYPE with
13045 operands OP0 and OP1. LOC is the location of the resulting
13046 expression. Return a folded expression if successful. Otherwise,
13047 return a tree expression with code CODE of type TYPE with operands
13048 OP0 and OP1. */
13049
13050 tree
13051 fold_build2_stat_loc (location_t loc,
13052 enum tree_code code, tree type, tree op0, tree op1
13053 MEM_STAT_DECL)
13054 {
13055 tree tem;
13056 #ifdef ENABLE_FOLD_CHECKING
13057 unsigned char checksum_before_op0[16],
13058 checksum_before_op1[16],
13059 checksum_after_op0[16],
13060 checksum_after_op1[16];
13061 struct md5_ctx ctx;
13062 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13063
13064 md5_init_ctx (&ctx);
13065 fold_checksum_tree (op0, &ctx, &ht);
13066 md5_finish_ctx (&ctx, checksum_before_op0);
13067 ht.empty ();
13068
13069 md5_init_ctx (&ctx);
13070 fold_checksum_tree (op1, &ctx, &ht);
13071 md5_finish_ctx (&ctx, checksum_before_op1);
13072 ht.empty ();
13073 #endif
13074
13075 tem = fold_binary_loc (loc, code, type, op0, op1);
13076 if (!tem)
13077 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13078
13079 #ifdef ENABLE_FOLD_CHECKING
13080 md5_init_ctx (&ctx);
13081 fold_checksum_tree (op0, &ctx, &ht);
13082 md5_finish_ctx (&ctx, checksum_after_op0);
13083 ht.empty ();
13084
13085 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13086 fold_check_failed (op0, tem);
13087
13088 md5_init_ctx (&ctx);
13089 fold_checksum_tree (op1, &ctx, &ht);
13090 md5_finish_ctx (&ctx, checksum_after_op1);
13091
13092 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13093 fold_check_failed (op1, tem);
13094 #endif
13095 return tem;
13096 }
13097
13098 /* Fold a ternary tree expression with code CODE of type TYPE with
13099 operands OP0, OP1, and OP2. Return a folded expression if
13100 successful. Otherwise, return a tree expression with code CODE of
13101 type TYPE with operands OP0, OP1, and OP2. */
13102
13103 tree
13104 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
13105 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13106 {
13107 tree tem;
13108 #ifdef ENABLE_FOLD_CHECKING
13109 unsigned char checksum_before_op0[16],
13110 checksum_before_op1[16],
13111 checksum_before_op2[16],
13112 checksum_after_op0[16],
13113 checksum_after_op1[16],
13114 checksum_after_op2[16];
13115 struct md5_ctx ctx;
13116 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13117
13118 md5_init_ctx (&ctx);
13119 fold_checksum_tree (op0, &ctx, &ht);
13120 md5_finish_ctx (&ctx, checksum_before_op0);
13121 ht.empty ();
13122
13123 md5_init_ctx (&ctx);
13124 fold_checksum_tree (op1, &ctx, &ht);
13125 md5_finish_ctx (&ctx, checksum_before_op1);
13126 ht.empty ();
13127
13128 md5_init_ctx (&ctx);
13129 fold_checksum_tree (op2, &ctx, &ht);
13130 md5_finish_ctx (&ctx, checksum_before_op2);
13131 ht.empty ();
13132 #endif
13133
13134 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13135 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13136 if (!tem)
13137 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13138
13139 #ifdef ENABLE_FOLD_CHECKING
13140 md5_init_ctx (&ctx);
13141 fold_checksum_tree (op0, &ctx, &ht);
13142 md5_finish_ctx (&ctx, checksum_after_op0);
13143 ht.empty ();
13144
13145 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13146 fold_check_failed (op0, tem);
13147
13148 md5_init_ctx (&ctx);
13149 fold_checksum_tree (op1, &ctx, &ht);
13150 md5_finish_ctx (&ctx, checksum_after_op1);
13151 ht.empty ();
13152
13153 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13154 fold_check_failed (op1, tem);
13155
13156 md5_init_ctx (&ctx);
13157 fold_checksum_tree (op2, &ctx, &ht);
13158 md5_finish_ctx (&ctx, checksum_after_op2);
13159
13160 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13161 fold_check_failed (op2, tem);
13162 #endif
13163 return tem;
13164 }
13165
13166 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13167 arguments in ARGARRAY, and a null static chain.
13168 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13169 of type TYPE from the given operands as constructed by build_call_array. */
13170
13171 tree
13172 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13173 int nargs, tree *argarray)
13174 {
13175 tree tem;
13176 #ifdef ENABLE_FOLD_CHECKING
13177 unsigned char checksum_before_fn[16],
13178 checksum_before_arglist[16],
13179 checksum_after_fn[16],
13180 checksum_after_arglist[16];
13181 struct md5_ctx ctx;
13182 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13183 int i;
13184
13185 md5_init_ctx (&ctx);
13186 fold_checksum_tree (fn, &ctx, &ht);
13187 md5_finish_ctx (&ctx, checksum_before_fn);
13188 ht.empty ();
13189
13190 md5_init_ctx (&ctx);
13191 for (i = 0; i < nargs; i++)
13192 fold_checksum_tree (argarray[i], &ctx, &ht);
13193 md5_finish_ctx (&ctx, checksum_before_arglist);
13194 ht.empty ();
13195 #endif
13196
13197 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13198 if (!tem)
13199 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13200
13201 #ifdef ENABLE_FOLD_CHECKING
13202 md5_init_ctx (&ctx);
13203 fold_checksum_tree (fn, &ctx, &ht);
13204 md5_finish_ctx (&ctx, checksum_after_fn);
13205 ht.empty ();
13206
13207 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13208 fold_check_failed (fn, tem);
13209
13210 md5_init_ctx (&ctx);
13211 for (i = 0; i < nargs; i++)
13212 fold_checksum_tree (argarray[i], &ctx, &ht);
13213 md5_finish_ctx (&ctx, checksum_after_arglist);
13214
13215 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13216 fold_check_failed (NULL_TREE, tem);
13217 #endif
13218 return tem;
13219 }
13220
13221 /* Perform constant folding and related simplification of initializer
13222 expression EXPR. These behave identically to "fold_buildN" but ignore
13223 potential run-time traps and exceptions that fold must preserve. */
13224
13225 #define START_FOLD_INIT \
13226 int saved_signaling_nans = flag_signaling_nans;\
13227 int saved_trapping_math = flag_trapping_math;\
13228 int saved_rounding_math = flag_rounding_math;\
13229 int saved_trapv = flag_trapv;\
13230 int saved_folding_initializer = folding_initializer;\
13231 flag_signaling_nans = 0;\
13232 flag_trapping_math = 0;\
13233 flag_rounding_math = 0;\
13234 flag_trapv = 0;\
13235 folding_initializer = 1;
13236
13237 #define END_FOLD_INIT \
13238 flag_signaling_nans = saved_signaling_nans;\
13239 flag_trapping_math = saved_trapping_math;\
13240 flag_rounding_math = saved_rounding_math;\
13241 flag_trapv = saved_trapv;\
13242 folding_initializer = saved_folding_initializer;
13243
13244 tree
13245 fold_build1_initializer_loc (location_t loc, enum tree_code code,
13246 tree type, tree op)
13247 {
13248 tree result;
13249 START_FOLD_INIT;
13250
13251 result = fold_build1_loc (loc, code, type, op);
13252
13253 END_FOLD_INIT;
13254 return result;
13255 }
13256
13257 tree
13258 fold_build2_initializer_loc (location_t loc, enum tree_code code,
13259 tree type, tree op0, tree op1)
13260 {
13261 tree result;
13262 START_FOLD_INIT;
13263
13264 result = fold_build2_loc (loc, code, type, op0, op1);
13265
13266 END_FOLD_INIT;
13267 return result;
13268 }
13269
13270 tree
13271 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13272 int nargs, tree *argarray)
13273 {
13274 tree result;
13275 START_FOLD_INIT;
13276
13277 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13278
13279 END_FOLD_INIT;
13280 return result;
13281 }
13282
13283 #undef START_FOLD_INIT
13284 #undef END_FOLD_INIT
13285
13286 /* Determine if first argument is a multiple of second argument. Return 0 if
13287 it is not, or we cannot easily determined it to be.
13288
13289 An example of the sort of thing we care about (at this point; this routine
13290 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13291 fold cases do now) is discovering that
13292
13293 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13294
13295 is a multiple of
13296
13297 SAVE_EXPR (J * 8)
13298
13299 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13300
13301 This code also handles discovering that
13302
13303 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13304
13305 is a multiple of 8 so we don't have to worry about dealing with a
13306 possible remainder.
13307
13308 Note that we *look* inside a SAVE_EXPR only to determine how it was
13309 calculated; it is not safe for fold to do much of anything else with the
13310 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13311 at run time. For example, the latter example above *cannot* be implemented
13312 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13313 evaluation time of the original SAVE_EXPR is not necessarily the same at
13314 the time the new expression is evaluated. The only optimization of this
13315 sort that would be valid is changing
13316
13317 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13318
13319 divided by 8 to
13320
13321 SAVE_EXPR (I) * SAVE_EXPR (J)
13322
13323 (where the same SAVE_EXPR (J) is used in the original and the
13324 transformed version). */
13325
13326 int
13327 multiple_of_p (tree type, const_tree top, const_tree bottom)
13328 {
13329 if (operand_equal_p (top, bottom, 0))
13330 return 1;
13331
13332 if (TREE_CODE (type) != INTEGER_TYPE)
13333 return 0;
13334
13335 switch (TREE_CODE (top))
13336 {
13337 case BIT_AND_EXPR:
13338 /* Bitwise and provides a power of two multiple. If the mask is
13339 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13340 if (!integer_pow2p (bottom))
13341 return 0;
13342 /* FALLTHRU */
13343
13344 case MULT_EXPR:
13345 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13346 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13347
13348 case PLUS_EXPR:
13349 case MINUS_EXPR:
13350 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13351 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13352
13353 case LSHIFT_EXPR:
13354 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13355 {
13356 tree op1, t1;
13357
13358 op1 = TREE_OPERAND (top, 1);
13359 /* const_binop may not detect overflow correctly,
13360 so check for it explicitly here. */
13361 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
13362 && 0 != (t1 = fold_convert (type,
13363 const_binop (LSHIFT_EXPR,
13364 size_one_node,
13365 op1)))
13366 && !TREE_OVERFLOW (t1))
13367 return multiple_of_p (type, t1, bottom);
13368 }
13369 return 0;
13370
13371 case NOP_EXPR:
13372 /* Can't handle conversions from non-integral or wider integral type. */
13373 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13374 || (TYPE_PRECISION (type)
13375 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13376 return 0;
13377
13378 /* .. fall through ... */
13379
13380 case SAVE_EXPR:
13381 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13382
13383 case COND_EXPR:
13384 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
13385 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
13386
13387 case INTEGER_CST:
13388 if (TREE_CODE (bottom) != INTEGER_CST
13389 || integer_zerop (bottom)
13390 || (TYPE_UNSIGNED (type)
13391 && (tree_int_cst_sgn (top) < 0
13392 || tree_int_cst_sgn (bottom) < 0)))
13393 return 0;
13394 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
13395 SIGNED);
13396
13397 default:
13398 return 0;
13399 }
13400 }
13401
13402 /* Return true if CODE or TYPE is known to be non-negative. */
13403
13404 static bool
13405 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13406 {
13407 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13408 && truth_value_p (code))
13409 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13410 have a signed:1 type (where the value is -1 and 0). */
13411 return true;
13412 return false;
13413 }
13414
13415 /* Return true if (CODE OP0) is known to be non-negative. If the return
13416 value is based on the assumption that signed overflow is undefined,
13417 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13418 *STRICT_OVERFLOW_P. */
13419
13420 bool
13421 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13422 bool *strict_overflow_p)
13423 {
13424 if (TYPE_UNSIGNED (type))
13425 return true;
13426
13427 switch (code)
13428 {
13429 case ABS_EXPR:
13430 /* We can't return 1 if flag_wrapv is set because
13431 ABS_EXPR<INT_MIN> = INT_MIN. */
13432 if (!ANY_INTEGRAL_TYPE_P (type))
13433 return true;
13434 if (TYPE_OVERFLOW_UNDEFINED (type))
13435 {
13436 *strict_overflow_p = true;
13437 return true;
13438 }
13439 break;
13440
13441 case NON_LVALUE_EXPR:
13442 case FLOAT_EXPR:
13443 case FIX_TRUNC_EXPR:
13444 return tree_expr_nonnegative_warnv_p (op0,
13445 strict_overflow_p);
13446
13447 CASE_CONVERT:
13448 {
13449 tree inner_type = TREE_TYPE (op0);
13450 tree outer_type = type;
13451
13452 if (TREE_CODE (outer_type) == REAL_TYPE)
13453 {
13454 if (TREE_CODE (inner_type) == REAL_TYPE)
13455 return tree_expr_nonnegative_warnv_p (op0,
13456 strict_overflow_p);
13457 if (INTEGRAL_TYPE_P (inner_type))
13458 {
13459 if (TYPE_UNSIGNED (inner_type))
13460 return true;
13461 return tree_expr_nonnegative_warnv_p (op0,
13462 strict_overflow_p);
13463 }
13464 }
13465 else if (INTEGRAL_TYPE_P (outer_type))
13466 {
13467 if (TREE_CODE (inner_type) == REAL_TYPE)
13468 return tree_expr_nonnegative_warnv_p (op0,
13469 strict_overflow_p);
13470 if (INTEGRAL_TYPE_P (inner_type))
13471 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13472 && TYPE_UNSIGNED (inner_type);
13473 }
13474 }
13475 break;
13476
13477 default:
13478 return tree_simple_nonnegative_warnv_p (code, type);
13479 }
13480
13481 /* We don't know sign of `t', so be conservative and return false. */
13482 return false;
13483 }
13484
13485 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
13486 value is based on the assumption that signed overflow is undefined,
13487 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13488 *STRICT_OVERFLOW_P. */
13489
13490 bool
13491 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13492 tree op1, bool *strict_overflow_p)
13493 {
13494 if (TYPE_UNSIGNED (type))
13495 return true;
13496
13497 switch (code)
13498 {
13499 case POINTER_PLUS_EXPR:
13500 case PLUS_EXPR:
13501 if (FLOAT_TYPE_P (type))
13502 return (tree_expr_nonnegative_warnv_p (op0,
13503 strict_overflow_p)
13504 && tree_expr_nonnegative_warnv_p (op1,
13505 strict_overflow_p));
13506
13507 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13508 both unsigned and at least 2 bits shorter than the result. */
13509 if (TREE_CODE (type) == INTEGER_TYPE
13510 && TREE_CODE (op0) == NOP_EXPR
13511 && TREE_CODE (op1) == NOP_EXPR)
13512 {
13513 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13514 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13515 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13516 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13517 {
13518 unsigned int prec = MAX (TYPE_PRECISION (inner1),
13519 TYPE_PRECISION (inner2)) + 1;
13520 return prec < TYPE_PRECISION (type);
13521 }
13522 }
13523 break;
13524
13525 case MULT_EXPR:
13526 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
13527 {
13528 /* x * x is always non-negative for floating point x
13529 or without overflow. */
13530 if (operand_equal_p (op0, op1, 0)
13531 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
13532 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
13533 {
13534 if (ANY_INTEGRAL_TYPE_P (type)
13535 && TYPE_OVERFLOW_UNDEFINED (type))
13536 *strict_overflow_p = true;
13537 return true;
13538 }
13539 }
13540
13541 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13542 both unsigned and their total bits is shorter than the result. */
13543 if (TREE_CODE (type) == INTEGER_TYPE
13544 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
13545 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
13546 {
13547 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
13548 ? TREE_TYPE (TREE_OPERAND (op0, 0))
13549 : TREE_TYPE (op0);
13550 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
13551 ? TREE_TYPE (TREE_OPERAND (op1, 0))
13552 : TREE_TYPE (op1);
13553
13554 bool unsigned0 = TYPE_UNSIGNED (inner0);
13555 bool unsigned1 = TYPE_UNSIGNED (inner1);
13556
13557 if (TREE_CODE (op0) == INTEGER_CST)
13558 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
13559
13560 if (TREE_CODE (op1) == INTEGER_CST)
13561 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
13562
13563 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
13564 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
13565 {
13566 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
13567 ? tree_int_cst_min_precision (op0, UNSIGNED)
13568 : TYPE_PRECISION (inner0);
13569
13570 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
13571 ? tree_int_cst_min_precision (op1, UNSIGNED)
13572 : TYPE_PRECISION (inner1);
13573
13574 return precision0 + precision1 < TYPE_PRECISION (type);
13575 }
13576 }
13577 return false;
13578
13579 case BIT_AND_EXPR:
13580 case MAX_EXPR:
13581 return (tree_expr_nonnegative_warnv_p (op0,
13582 strict_overflow_p)
13583 || tree_expr_nonnegative_warnv_p (op1,
13584 strict_overflow_p));
13585
13586 case BIT_IOR_EXPR:
13587 case BIT_XOR_EXPR:
13588 case MIN_EXPR:
13589 case RDIV_EXPR:
13590 case TRUNC_DIV_EXPR:
13591 case CEIL_DIV_EXPR:
13592 case FLOOR_DIV_EXPR:
13593 case ROUND_DIV_EXPR:
13594 return (tree_expr_nonnegative_warnv_p (op0,
13595 strict_overflow_p)
13596 && tree_expr_nonnegative_warnv_p (op1,
13597 strict_overflow_p));
13598
13599 case TRUNC_MOD_EXPR:
13600 case CEIL_MOD_EXPR:
13601 case FLOOR_MOD_EXPR:
13602 case ROUND_MOD_EXPR:
13603 return tree_expr_nonnegative_warnv_p (op0,
13604 strict_overflow_p);
13605 default:
13606 return tree_simple_nonnegative_warnv_p (code, type);
13607 }
13608
13609 /* We don't know sign of `t', so be conservative and return false. */
13610 return false;
13611 }
13612
13613 /* Return true if T is known to be non-negative. If the return
13614 value is based on the assumption that signed overflow is undefined,
13615 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13616 *STRICT_OVERFLOW_P. */
13617
13618 bool
13619 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13620 {
13621 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13622 return true;
13623
13624 switch (TREE_CODE (t))
13625 {
13626 case INTEGER_CST:
13627 return tree_int_cst_sgn (t) >= 0;
13628
13629 case REAL_CST:
13630 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13631
13632 case FIXED_CST:
13633 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13634
13635 case COND_EXPR:
13636 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13637 strict_overflow_p)
13638 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
13639 strict_overflow_p));
13640 default:
13641 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
13642 TREE_TYPE (t));
13643 }
13644 /* We don't know sign of `t', so be conservative and return false. */
13645 return false;
13646 }
13647
13648 /* Return true if T is known to be non-negative. If the return
13649 value is based on the assumption that signed overflow is undefined,
13650 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13651 *STRICT_OVERFLOW_P. */
13652
13653 bool
13654 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
13655 tree arg0, tree arg1, bool *strict_overflow_p)
13656 {
13657 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
13658 switch (DECL_FUNCTION_CODE (fndecl))
13659 {
13660 CASE_FLT_FN (BUILT_IN_ACOS):
13661 CASE_FLT_FN (BUILT_IN_ACOSH):
13662 CASE_FLT_FN (BUILT_IN_CABS):
13663 CASE_FLT_FN (BUILT_IN_COSH):
13664 CASE_FLT_FN (BUILT_IN_ERFC):
13665 CASE_FLT_FN (BUILT_IN_EXP):
13666 CASE_FLT_FN (BUILT_IN_EXP10):
13667 CASE_FLT_FN (BUILT_IN_EXP2):
13668 CASE_FLT_FN (BUILT_IN_FABS):
13669 CASE_FLT_FN (BUILT_IN_FDIM):
13670 CASE_FLT_FN (BUILT_IN_HYPOT):
13671 CASE_FLT_FN (BUILT_IN_POW10):
13672 CASE_INT_FN (BUILT_IN_FFS):
13673 CASE_INT_FN (BUILT_IN_PARITY):
13674 CASE_INT_FN (BUILT_IN_POPCOUNT):
13675 CASE_INT_FN (BUILT_IN_CLZ):
13676 CASE_INT_FN (BUILT_IN_CLRSB):
13677 case BUILT_IN_BSWAP32:
13678 case BUILT_IN_BSWAP64:
13679 /* Always true. */
13680 return true;
13681
13682 CASE_FLT_FN (BUILT_IN_SQRT):
13683 /* sqrt(-0.0) is -0.0. */
13684 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13685 return true;
13686 return tree_expr_nonnegative_warnv_p (arg0,
13687 strict_overflow_p);
13688
13689 CASE_FLT_FN (BUILT_IN_ASINH):
13690 CASE_FLT_FN (BUILT_IN_ATAN):
13691 CASE_FLT_FN (BUILT_IN_ATANH):
13692 CASE_FLT_FN (BUILT_IN_CBRT):
13693 CASE_FLT_FN (BUILT_IN_CEIL):
13694 CASE_FLT_FN (BUILT_IN_ERF):
13695 CASE_FLT_FN (BUILT_IN_EXPM1):
13696 CASE_FLT_FN (BUILT_IN_FLOOR):
13697 CASE_FLT_FN (BUILT_IN_FMOD):
13698 CASE_FLT_FN (BUILT_IN_FREXP):
13699 CASE_FLT_FN (BUILT_IN_ICEIL):
13700 CASE_FLT_FN (BUILT_IN_IFLOOR):
13701 CASE_FLT_FN (BUILT_IN_IRINT):
13702 CASE_FLT_FN (BUILT_IN_IROUND):
13703 CASE_FLT_FN (BUILT_IN_LCEIL):
13704 CASE_FLT_FN (BUILT_IN_LDEXP):
13705 CASE_FLT_FN (BUILT_IN_LFLOOR):
13706 CASE_FLT_FN (BUILT_IN_LLCEIL):
13707 CASE_FLT_FN (BUILT_IN_LLFLOOR):
13708 CASE_FLT_FN (BUILT_IN_LLRINT):
13709 CASE_FLT_FN (BUILT_IN_LLROUND):
13710 CASE_FLT_FN (BUILT_IN_LRINT):
13711 CASE_FLT_FN (BUILT_IN_LROUND):
13712 CASE_FLT_FN (BUILT_IN_MODF):
13713 CASE_FLT_FN (BUILT_IN_NEARBYINT):
13714 CASE_FLT_FN (BUILT_IN_RINT):
13715 CASE_FLT_FN (BUILT_IN_ROUND):
13716 CASE_FLT_FN (BUILT_IN_SCALB):
13717 CASE_FLT_FN (BUILT_IN_SCALBLN):
13718 CASE_FLT_FN (BUILT_IN_SCALBN):
13719 CASE_FLT_FN (BUILT_IN_SIGNBIT):
13720 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
13721 CASE_FLT_FN (BUILT_IN_SINH):
13722 CASE_FLT_FN (BUILT_IN_TANH):
13723 CASE_FLT_FN (BUILT_IN_TRUNC):
13724 /* True if the 1st argument is nonnegative. */
13725 return tree_expr_nonnegative_warnv_p (arg0,
13726 strict_overflow_p);
13727
13728 CASE_FLT_FN (BUILT_IN_FMAX):
13729 /* True if the 1st OR 2nd arguments are nonnegative. */
13730 return (tree_expr_nonnegative_warnv_p (arg0,
13731 strict_overflow_p)
13732 || (tree_expr_nonnegative_warnv_p (arg1,
13733 strict_overflow_p)));
13734
13735 CASE_FLT_FN (BUILT_IN_FMIN):
13736 /* True if the 1st AND 2nd arguments are nonnegative. */
13737 return (tree_expr_nonnegative_warnv_p (arg0,
13738 strict_overflow_p)
13739 && (tree_expr_nonnegative_warnv_p (arg1,
13740 strict_overflow_p)));
13741
13742 CASE_FLT_FN (BUILT_IN_COPYSIGN):
13743 /* True if the 2nd argument is nonnegative. */
13744 return tree_expr_nonnegative_warnv_p (arg1,
13745 strict_overflow_p);
13746
13747 CASE_FLT_FN (BUILT_IN_POWI):
13748 /* True if the 1st argument is nonnegative or the second
13749 argument is an even integer. */
13750 if (TREE_CODE (arg1) == INTEGER_CST
13751 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13752 return true;
13753 return tree_expr_nonnegative_warnv_p (arg0,
13754 strict_overflow_p);
13755
13756 CASE_FLT_FN (BUILT_IN_POW):
13757 /* True if the 1st argument is nonnegative or the second
13758 argument is an even integer valued real. */
13759 if (TREE_CODE (arg1) == REAL_CST)
13760 {
13761 REAL_VALUE_TYPE c;
13762 HOST_WIDE_INT n;
13763
13764 c = TREE_REAL_CST (arg1);
13765 n = real_to_integer (&c);
13766 if ((n & 1) == 0)
13767 {
13768 REAL_VALUE_TYPE cint;
13769 real_from_integer (&cint, VOIDmode, n, SIGNED);
13770 if (real_identical (&c, &cint))
13771 return true;
13772 }
13773 }
13774 return tree_expr_nonnegative_warnv_p (arg0,
13775 strict_overflow_p);
13776
13777 default:
13778 break;
13779 }
13780 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
13781 type);
13782 }
13783
13784 /* Return true if T is known to be non-negative. If the return
13785 value is based on the assumption that signed overflow is undefined,
13786 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13787 *STRICT_OVERFLOW_P. */
13788
13789 static bool
13790 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13791 {
13792 enum tree_code code = TREE_CODE (t);
13793 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13794 return true;
13795
13796 switch (code)
13797 {
13798 case TARGET_EXPR:
13799 {
13800 tree temp = TARGET_EXPR_SLOT (t);
13801 t = TARGET_EXPR_INITIAL (t);
13802
13803 /* If the initializer is non-void, then it's a normal expression
13804 that will be assigned to the slot. */
13805 if (!VOID_TYPE_P (t))
13806 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13807
13808 /* Otherwise, the initializer sets the slot in some way. One common
13809 way is an assignment statement at the end of the initializer. */
13810 while (1)
13811 {
13812 if (TREE_CODE (t) == BIND_EXPR)
13813 t = expr_last (BIND_EXPR_BODY (t));
13814 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13815 || TREE_CODE (t) == TRY_CATCH_EXPR)
13816 t = expr_last (TREE_OPERAND (t, 0));
13817 else if (TREE_CODE (t) == STATEMENT_LIST)
13818 t = expr_last (t);
13819 else
13820 break;
13821 }
13822 if (TREE_CODE (t) == MODIFY_EXPR
13823 && TREE_OPERAND (t, 0) == temp)
13824 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13825 strict_overflow_p);
13826
13827 return false;
13828 }
13829
13830 case CALL_EXPR:
13831 {
13832 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13833 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13834
13835 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13836 get_callee_fndecl (t),
13837 arg0,
13838 arg1,
13839 strict_overflow_p);
13840 }
13841 case COMPOUND_EXPR:
13842 case MODIFY_EXPR:
13843 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13844 strict_overflow_p);
13845 case BIND_EXPR:
13846 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13847 strict_overflow_p);
13848 case SAVE_EXPR:
13849 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13850 strict_overflow_p);
13851
13852 default:
13853 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
13854 TREE_TYPE (t));
13855 }
13856
13857 /* We don't know sign of `t', so be conservative and return false. */
13858 return false;
13859 }
13860
13861 /* Return true if T is known to be non-negative. If the return
13862 value is based on the assumption that signed overflow is undefined,
13863 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13864 *STRICT_OVERFLOW_P. */
13865
13866 bool
13867 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13868 {
13869 enum tree_code code;
13870 if (t == error_mark_node)
13871 return false;
13872
13873 code = TREE_CODE (t);
13874 switch (TREE_CODE_CLASS (code))
13875 {
13876 case tcc_binary:
13877 case tcc_comparison:
13878 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13879 TREE_TYPE (t),
13880 TREE_OPERAND (t, 0),
13881 TREE_OPERAND (t, 1),
13882 strict_overflow_p);
13883
13884 case tcc_unary:
13885 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13886 TREE_TYPE (t),
13887 TREE_OPERAND (t, 0),
13888 strict_overflow_p);
13889
13890 case tcc_constant:
13891 case tcc_declaration:
13892 case tcc_reference:
13893 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13894
13895 default:
13896 break;
13897 }
13898
13899 switch (code)
13900 {
13901 case TRUTH_AND_EXPR:
13902 case TRUTH_OR_EXPR:
13903 case TRUTH_XOR_EXPR:
13904 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13905 TREE_TYPE (t),
13906 TREE_OPERAND (t, 0),
13907 TREE_OPERAND (t, 1),
13908 strict_overflow_p);
13909 case TRUTH_NOT_EXPR:
13910 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13911 TREE_TYPE (t),
13912 TREE_OPERAND (t, 0),
13913 strict_overflow_p);
13914
13915 case COND_EXPR:
13916 case CONSTRUCTOR:
13917 case OBJ_TYPE_REF:
13918 case ASSERT_EXPR:
13919 case ADDR_EXPR:
13920 case WITH_SIZE_EXPR:
13921 case SSA_NAME:
13922 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13923
13924 default:
13925 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
13926 }
13927 }
13928
13929 /* Return true if `t' is known to be non-negative. Handle warnings
13930 about undefined signed overflow. */
13931
13932 bool
13933 tree_expr_nonnegative_p (tree t)
13934 {
13935 bool ret, strict_overflow_p;
13936
13937 strict_overflow_p = false;
13938 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13939 if (strict_overflow_p)
13940 fold_overflow_warning (("assuming signed overflow does not occur when "
13941 "determining that expression is always "
13942 "non-negative"),
13943 WARN_STRICT_OVERFLOW_MISC);
13944 return ret;
13945 }
13946
13947
13948 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13949 For floating point we further ensure that T is not denormal.
13950 Similar logic is present in nonzero_address in rtlanal.h.
13951
13952 If the return value is based on the assumption that signed overflow
13953 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13954 change *STRICT_OVERFLOW_P. */
13955
13956 bool
13957 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13958 bool *strict_overflow_p)
13959 {
13960 switch (code)
13961 {
13962 case ABS_EXPR:
13963 return tree_expr_nonzero_warnv_p (op0,
13964 strict_overflow_p);
13965
13966 case NOP_EXPR:
13967 {
13968 tree inner_type = TREE_TYPE (op0);
13969 tree outer_type = type;
13970
13971 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13972 && tree_expr_nonzero_warnv_p (op0,
13973 strict_overflow_p));
13974 }
13975 break;
13976
13977 case NON_LVALUE_EXPR:
13978 return tree_expr_nonzero_warnv_p (op0,
13979 strict_overflow_p);
13980
13981 default:
13982 break;
13983 }
13984
13985 return false;
13986 }
13987
13988 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13989 For floating point we further ensure that T is not denormal.
13990 Similar logic is present in nonzero_address in rtlanal.h.
13991
13992 If the return value is based on the assumption that signed overflow
13993 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13994 change *STRICT_OVERFLOW_P. */
13995
13996 bool
13997 tree_binary_nonzero_warnv_p (enum tree_code code,
13998 tree type,
13999 tree op0,
14000 tree op1, bool *strict_overflow_p)
14001 {
14002 bool sub_strict_overflow_p;
14003 switch (code)
14004 {
14005 case POINTER_PLUS_EXPR:
14006 case PLUS_EXPR:
14007 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
14008 {
14009 /* With the presence of negative values it is hard
14010 to say something. */
14011 sub_strict_overflow_p = false;
14012 if (!tree_expr_nonnegative_warnv_p (op0,
14013 &sub_strict_overflow_p)
14014 || !tree_expr_nonnegative_warnv_p (op1,
14015 &sub_strict_overflow_p))
14016 return false;
14017 /* One of operands must be positive and the other non-negative. */
14018 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14019 overflows, on a twos-complement machine the sum of two
14020 nonnegative numbers can never be zero. */
14021 return (tree_expr_nonzero_warnv_p (op0,
14022 strict_overflow_p)
14023 || tree_expr_nonzero_warnv_p (op1,
14024 strict_overflow_p));
14025 }
14026 break;
14027
14028 case MULT_EXPR:
14029 if (TYPE_OVERFLOW_UNDEFINED (type))
14030 {
14031 if (tree_expr_nonzero_warnv_p (op0,
14032 strict_overflow_p)
14033 && tree_expr_nonzero_warnv_p (op1,
14034 strict_overflow_p))
14035 {
14036 *strict_overflow_p = true;
14037 return true;
14038 }
14039 }
14040 break;
14041
14042 case MIN_EXPR:
14043 sub_strict_overflow_p = false;
14044 if (tree_expr_nonzero_warnv_p (op0,
14045 &sub_strict_overflow_p)
14046 && tree_expr_nonzero_warnv_p (op1,
14047 &sub_strict_overflow_p))
14048 {
14049 if (sub_strict_overflow_p)
14050 *strict_overflow_p = true;
14051 }
14052 break;
14053
14054 case MAX_EXPR:
14055 sub_strict_overflow_p = false;
14056 if (tree_expr_nonzero_warnv_p (op0,
14057 &sub_strict_overflow_p))
14058 {
14059 if (sub_strict_overflow_p)
14060 *strict_overflow_p = true;
14061
14062 /* When both operands are nonzero, then MAX must be too. */
14063 if (tree_expr_nonzero_warnv_p (op1,
14064 strict_overflow_p))
14065 return true;
14066
14067 /* MAX where operand 0 is positive is positive. */
14068 return tree_expr_nonnegative_warnv_p (op0,
14069 strict_overflow_p);
14070 }
14071 /* MAX where operand 1 is positive is positive. */
14072 else if (tree_expr_nonzero_warnv_p (op1,
14073 &sub_strict_overflow_p)
14074 && tree_expr_nonnegative_warnv_p (op1,
14075 &sub_strict_overflow_p))
14076 {
14077 if (sub_strict_overflow_p)
14078 *strict_overflow_p = true;
14079 return true;
14080 }
14081 break;
14082
14083 case BIT_IOR_EXPR:
14084 return (tree_expr_nonzero_warnv_p (op1,
14085 strict_overflow_p)
14086 || tree_expr_nonzero_warnv_p (op0,
14087 strict_overflow_p));
14088
14089 default:
14090 break;
14091 }
14092
14093 return false;
14094 }
14095
14096 /* Return true when T is an address and is known to be nonzero.
14097 For floating point we further ensure that T is not denormal.
14098 Similar logic is present in nonzero_address in rtlanal.h.
14099
14100 If the return value is based on the assumption that signed overflow
14101 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14102 change *STRICT_OVERFLOW_P. */
14103
14104 bool
14105 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14106 {
14107 bool sub_strict_overflow_p;
14108 switch (TREE_CODE (t))
14109 {
14110 case INTEGER_CST:
14111 return !integer_zerop (t);
14112
14113 case ADDR_EXPR:
14114 {
14115 tree base = TREE_OPERAND (t, 0);
14116
14117 if (!DECL_P (base))
14118 base = get_base_address (base);
14119
14120 if (!base)
14121 return false;
14122
14123 /* For objects in symbol table check if we know they are non-zero.
14124 Don't do anything for variables and functions before symtab is built;
14125 it is quite possible that they will be declared weak later. */
14126 if (DECL_P (base) && decl_in_symtab_p (base))
14127 {
14128 struct symtab_node *symbol;
14129
14130 symbol = symtab_node::get_create (base);
14131 if (symbol)
14132 return symbol->nonzero_address ();
14133 else
14134 return false;
14135 }
14136
14137 /* Function local objects are never NULL. */
14138 if (DECL_P (base)
14139 && (DECL_CONTEXT (base)
14140 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
14141 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
14142 return true;
14143
14144 /* Constants are never weak. */
14145 if (CONSTANT_CLASS_P (base))
14146 return true;
14147
14148 return false;
14149 }
14150
14151 case COND_EXPR:
14152 sub_strict_overflow_p = false;
14153 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14154 &sub_strict_overflow_p)
14155 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14156 &sub_strict_overflow_p))
14157 {
14158 if (sub_strict_overflow_p)
14159 *strict_overflow_p = true;
14160 return true;
14161 }
14162 break;
14163
14164 default:
14165 break;
14166 }
14167 return false;
14168 }
14169
14170 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14171 attempt to fold the expression to a constant without modifying TYPE,
14172 OP0 or OP1.
14173
14174 If the expression could be simplified to a constant, then return
14175 the constant. If the expression would not be simplified to a
14176 constant, then return NULL_TREE. */
14177
14178 tree
14179 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14180 {
14181 tree tem = fold_binary (code, type, op0, op1);
14182 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14183 }
14184
14185 /* Given the components of a unary expression CODE, TYPE and OP0,
14186 attempt to fold the expression to a constant without modifying
14187 TYPE or OP0.
14188
14189 If the expression could be simplified to a constant, then return
14190 the constant. If the expression would not be simplified to a
14191 constant, then return NULL_TREE. */
14192
14193 tree
14194 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14195 {
14196 tree tem = fold_unary (code, type, op0);
14197 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14198 }
14199
14200 /* If EXP represents referencing an element in a constant string
14201 (either via pointer arithmetic or array indexing), return the
14202 tree representing the value accessed, otherwise return NULL. */
14203
14204 tree
14205 fold_read_from_constant_string (tree exp)
14206 {
14207 if ((TREE_CODE (exp) == INDIRECT_REF
14208 || TREE_CODE (exp) == ARRAY_REF)
14209 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14210 {
14211 tree exp1 = TREE_OPERAND (exp, 0);
14212 tree index;
14213 tree string;
14214 location_t loc = EXPR_LOCATION (exp);
14215
14216 if (TREE_CODE (exp) == INDIRECT_REF)
14217 string = string_constant (exp1, &index);
14218 else
14219 {
14220 tree low_bound = array_ref_low_bound (exp);
14221 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
14222
14223 /* Optimize the special-case of a zero lower bound.
14224
14225 We convert the low_bound to sizetype to avoid some problems
14226 with constant folding. (E.g. suppose the lower bound is 1,
14227 and its mode is QI. Without the conversion,l (ARRAY
14228 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14229 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
14230 if (! integer_zerop (low_bound))
14231 index = size_diffop_loc (loc, index,
14232 fold_convert_loc (loc, sizetype, low_bound));
14233
14234 string = exp1;
14235 }
14236
14237 if (string
14238 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14239 && TREE_CODE (string) == STRING_CST
14240 && TREE_CODE (index) == INTEGER_CST
14241 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14242 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14243 == MODE_INT)
14244 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14245 return build_int_cst_type (TREE_TYPE (exp),
14246 (TREE_STRING_POINTER (string)
14247 [TREE_INT_CST_LOW (index)]));
14248 }
14249 return NULL;
14250 }
14251
14252 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14253 an integer constant, real, or fixed-point constant.
14254
14255 TYPE is the type of the result. */
14256
14257 static tree
14258 fold_negate_const (tree arg0, tree type)
14259 {
14260 tree t = NULL_TREE;
14261
14262 switch (TREE_CODE (arg0))
14263 {
14264 case INTEGER_CST:
14265 {
14266 bool overflow;
14267 wide_int val = wi::neg (arg0, &overflow);
14268 t = force_fit_type (type, val, 1,
14269 (overflow | TREE_OVERFLOW (arg0))
14270 && !TYPE_UNSIGNED (type));
14271 break;
14272 }
14273
14274 case REAL_CST:
14275 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
14276 break;
14277
14278 case FIXED_CST:
14279 {
14280 FIXED_VALUE_TYPE f;
14281 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14282 &(TREE_FIXED_CST (arg0)), NULL,
14283 TYPE_SATURATING (type));
14284 t = build_fixed (type, f);
14285 /* Propagate overflow flags. */
14286 if (overflow_p | TREE_OVERFLOW (arg0))
14287 TREE_OVERFLOW (t) = 1;
14288 break;
14289 }
14290
14291 default:
14292 gcc_unreachable ();
14293 }
14294
14295 return t;
14296 }
14297
14298 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14299 an integer constant or real constant.
14300
14301 TYPE is the type of the result. */
14302
14303 tree
14304 fold_abs_const (tree arg0, tree type)
14305 {
14306 tree t = NULL_TREE;
14307
14308 switch (TREE_CODE (arg0))
14309 {
14310 case INTEGER_CST:
14311 {
14312 /* If the value is unsigned or non-negative, then the absolute value
14313 is the same as the ordinary value. */
14314 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
14315 t = arg0;
14316
14317 /* If the value is negative, then the absolute value is
14318 its negation. */
14319 else
14320 {
14321 bool overflow;
14322 wide_int val = wi::neg (arg0, &overflow);
14323 t = force_fit_type (type, val, -1,
14324 overflow | TREE_OVERFLOW (arg0));
14325 }
14326 }
14327 break;
14328
14329 case REAL_CST:
14330 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14331 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
14332 else
14333 t = arg0;
14334 break;
14335
14336 default:
14337 gcc_unreachable ();
14338 }
14339
14340 return t;
14341 }
14342
14343 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14344 constant. TYPE is the type of the result. */
14345
14346 static tree
14347 fold_not_const (const_tree arg0, tree type)
14348 {
14349 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14350
14351 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
14352 }
14353
14354 /* Given CODE, a relational operator, the target type, TYPE and two
14355 constant operands OP0 and OP1, return the result of the
14356 relational operation. If the result is not a compile time
14357 constant, then return NULL_TREE. */
14358
14359 static tree
14360 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14361 {
14362 int result, invert;
14363
14364 /* From here on, the only cases we handle are when the result is
14365 known to be a constant. */
14366
14367 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14368 {
14369 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14370 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14371
14372 /* Handle the cases where either operand is a NaN. */
14373 if (real_isnan (c0) || real_isnan (c1))
14374 {
14375 switch (code)
14376 {
14377 case EQ_EXPR:
14378 case ORDERED_EXPR:
14379 result = 0;
14380 break;
14381
14382 case NE_EXPR:
14383 case UNORDERED_EXPR:
14384 case UNLT_EXPR:
14385 case UNLE_EXPR:
14386 case UNGT_EXPR:
14387 case UNGE_EXPR:
14388 case UNEQ_EXPR:
14389 result = 1;
14390 break;
14391
14392 case LT_EXPR:
14393 case LE_EXPR:
14394 case GT_EXPR:
14395 case GE_EXPR:
14396 case LTGT_EXPR:
14397 if (flag_trapping_math)
14398 return NULL_TREE;
14399 result = 0;
14400 break;
14401
14402 default:
14403 gcc_unreachable ();
14404 }
14405
14406 return constant_boolean_node (result, type);
14407 }
14408
14409 return constant_boolean_node (real_compare (code, c0, c1), type);
14410 }
14411
14412 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14413 {
14414 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14415 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14416 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14417 }
14418
14419 /* Handle equality/inequality of complex constants. */
14420 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14421 {
14422 tree rcond = fold_relational_const (code, type,
14423 TREE_REALPART (op0),
14424 TREE_REALPART (op1));
14425 tree icond = fold_relational_const (code, type,
14426 TREE_IMAGPART (op0),
14427 TREE_IMAGPART (op1));
14428 if (code == EQ_EXPR)
14429 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14430 else if (code == NE_EXPR)
14431 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14432 else
14433 return NULL_TREE;
14434 }
14435
14436 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14437 {
14438 unsigned count = VECTOR_CST_NELTS (op0);
14439 tree *elts = XALLOCAVEC (tree, count);
14440 gcc_assert (VECTOR_CST_NELTS (op1) == count
14441 && TYPE_VECTOR_SUBPARTS (type) == count);
14442
14443 for (unsigned i = 0; i < count; i++)
14444 {
14445 tree elem_type = TREE_TYPE (type);
14446 tree elem0 = VECTOR_CST_ELT (op0, i);
14447 tree elem1 = VECTOR_CST_ELT (op1, i);
14448
14449 tree tem = fold_relational_const (code, elem_type,
14450 elem0, elem1);
14451
14452 if (tem == NULL_TREE)
14453 return NULL_TREE;
14454
14455 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
14456 }
14457
14458 return build_vector (type, elts);
14459 }
14460
14461 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14462
14463 To compute GT, swap the arguments and do LT.
14464 To compute GE, do LT and invert the result.
14465 To compute LE, swap the arguments, do LT and invert the result.
14466 To compute NE, do EQ and invert the result.
14467
14468 Therefore, the code below must handle only EQ and LT. */
14469
14470 if (code == LE_EXPR || code == GT_EXPR)
14471 {
14472 std::swap (op0, op1);
14473 code = swap_tree_comparison (code);
14474 }
14475
14476 /* Note that it is safe to invert for real values here because we
14477 have already handled the one case that it matters. */
14478
14479 invert = 0;
14480 if (code == NE_EXPR || code == GE_EXPR)
14481 {
14482 invert = 1;
14483 code = invert_tree_comparison (code, false);
14484 }
14485
14486 /* Compute a result for LT or EQ if args permit;
14487 Otherwise return T. */
14488 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14489 {
14490 if (code == EQ_EXPR)
14491 result = tree_int_cst_equal (op0, op1);
14492 else
14493 result = tree_int_cst_lt (op0, op1);
14494 }
14495 else
14496 return NULL_TREE;
14497
14498 if (invert)
14499 result ^= 1;
14500 return constant_boolean_node (result, type);
14501 }
14502
14503 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14504 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14505 itself. */
14506
14507 tree
14508 fold_build_cleanup_point_expr (tree type, tree expr)
14509 {
14510 /* If the expression does not have side effects then we don't have to wrap
14511 it with a cleanup point expression. */
14512 if (!TREE_SIDE_EFFECTS (expr))
14513 return expr;
14514
14515 /* If the expression is a return, check to see if the expression inside the
14516 return has no side effects or the right hand side of the modify expression
14517 inside the return. If either don't have side effects set we don't need to
14518 wrap the expression in a cleanup point expression. Note we don't check the
14519 left hand side of the modify because it should always be a return decl. */
14520 if (TREE_CODE (expr) == RETURN_EXPR)
14521 {
14522 tree op = TREE_OPERAND (expr, 0);
14523 if (!op || !TREE_SIDE_EFFECTS (op))
14524 return expr;
14525 op = TREE_OPERAND (op, 1);
14526 if (!TREE_SIDE_EFFECTS (op))
14527 return expr;
14528 }
14529
14530 return build1 (CLEANUP_POINT_EXPR, type, expr);
14531 }
14532
14533 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14534 of an indirection through OP0, or NULL_TREE if no simplification is
14535 possible. */
14536
14537 tree
14538 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14539 {
14540 tree sub = op0;
14541 tree subtype;
14542
14543 STRIP_NOPS (sub);
14544 subtype = TREE_TYPE (sub);
14545 if (!POINTER_TYPE_P (subtype))
14546 return NULL_TREE;
14547
14548 if (TREE_CODE (sub) == ADDR_EXPR)
14549 {
14550 tree op = TREE_OPERAND (sub, 0);
14551 tree optype = TREE_TYPE (op);
14552 /* *&CONST_DECL -> to the value of the const decl. */
14553 if (TREE_CODE (op) == CONST_DECL)
14554 return DECL_INITIAL (op);
14555 /* *&p => p; make sure to handle *&"str"[cst] here. */
14556 if (type == optype)
14557 {
14558 tree fop = fold_read_from_constant_string (op);
14559 if (fop)
14560 return fop;
14561 else
14562 return op;
14563 }
14564 /* *(foo *)&fooarray => fooarray[0] */
14565 else if (TREE_CODE (optype) == ARRAY_TYPE
14566 && type == TREE_TYPE (optype)
14567 && (!in_gimple_form
14568 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14569 {
14570 tree type_domain = TYPE_DOMAIN (optype);
14571 tree min_val = size_zero_node;
14572 if (type_domain && TYPE_MIN_VALUE (type_domain))
14573 min_val = TYPE_MIN_VALUE (type_domain);
14574 if (in_gimple_form
14575 && TREE_CODE (min_val) != INTEGER_CST)
14576 return NULL_TREE;
14577 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14578 NULL_TREE, NULL_TREE);
14579 }
14580 /* *(foo *)&complexfoo => __real__ complexfoo */
14581 else if (TREE_CODE (optype) == COMPLEX_TYPE
14582 && type == TREE_TYPE (optype))
14583 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14584 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14585 else if (TREE_CODE (optype) == VECTOR_TYPE
14586 && type == TREE_TYPE (optype))
14587 {
14588 tree part_width = TYPE_SIZE (type);
14589 tree index = bitsize_int (0);
14590 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14591 }
14592 }
14593
14594 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14595 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14596 {
14597 tree op00 = TREE_OPERAND (sub, 0);
14598 tree op01 = TREE_OPERAND (sub, 1);
14599
14600 STRIP_NOPS (op00);
14601 if (TREE_CODE (op00) == ADDR_EXPR)
14602 {
14603 tree op00type;
14604 op00 = TREE_OPERAND (op00, 0);
14605 op00type = TREE_TYPE (op00);
14606
14607 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14608 if (TREE_CODE (op00type) == VECTOR_TYPE
14609 && type == TREE_TYPE (op00type))
14610 {
14611 HOST_WIDE_INT offset = tree_to_shwi (op01);
14612 tree part_width = TYPE_SIZE (type);
14613 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
14614 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14615 tree index = bitsize_int (indexi);
14616
14617 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
14618 return fold_build3_loc (loc,
14619 BIT_FIELD_REF, type, op00,
14620 part_width, index);
14621
14622 }
14623 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14624 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14625 && type == TREE_TYPE (op00type))
14626 {
14627 tree size = TYPE_SIZE_UNIT (type);
14628 if (tree_int_cst_equal (size, op01))
14629 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14630 }
14631 /* ((foo *)&fooarray)[1] => fooarray[1] */
14632 else if (TREE_CODE (op00type) == ARRAY_TYPE
14633 && type == TREE_TYPE (op00type))
14634 {
14635 tree type_domain = TYPE_DOMAIN (op00type);
14636 tree min_val = size_zero_node;
14637 if (type_domain && TYPE_MIN_VALUE (type_domain))
14638 min_val = TYPE_MIN_VALUE (type_domain);
14639 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14640 TYPE_SIZE_UNIT (type));
14641 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14642 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14643 NULL_TREE, NULL_TREE);
14644 }
14645 }
14646 }
14647
14648 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14649 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14650 && type == TREE_TYPE (TREE_TYPE (subtype))
14651 && (!in_gimple_form
14652 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14653 {
14654 tree type_domain;
14655 tree min_val = size_zero_node;
14656 sub = build_fold_indirect_ref_loc (loc, sub);
14657 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14658 if (type_domain && TYPE_MIN_VALUE (type_domain))
14659 min_val = TYPE_MIN_VALUE (type_domain);
14660 if (in_gimple_form
14661 && TREE_CODE (min_val) != INTEGER_CST)
14662 return NULL_TREE;
14663 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14664 NULL_TREE);
14665 }
14666
14667 return NULL_TREE;
14668 }
14669
14670 /* Builds an expression for an indirection through T, simplifying some
14671 cases. */
14672
14673 tree
14674 build_fold_indirect_ref_loc (location_t loc, tree t)
14675 {
14676 tree type = TREE_TYPE (TREE_TYPE (t));
14677 tree sub = fold_indirect_ref_1 (loc, type, t);
14678
14679 if (sub)
14680 return sub;
14681
14682 return build1_loc (loc, INDIRECT_REF, type, t);
14683 }
14684
14685 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14686
14687 tree
14688 fold_indirect_ref_loc (location_t loc, tree t)
14689 {
14690 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14691
14692 if (sub)
14693 return sub;
14694 else
14695 return t;
14696 }
14697
14698 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14699 whose result is ignored. The type of the returned tree need not be
14700 the same as the original expression. */
14701
14702 tree
14703 fold_ignored_result (tree t)
14704 {
14705 if (!TREE_SIDE_EFFECTS (t))
14706 return integer_zero_node;
14707
14708 for (;;)
14709 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14710 {
14711 case tcc_unary:
14712 t = TREE_OPERAND (t, 0);
14713 break;
14714
14715 case tcc_binary:
14716 case tcc_comparison:
14717 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14718 t = TREE_OPERAND (t, 0);
14719 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14720 t = TREE_OPERAND (t, 1);
14721 else
14722 return t;
14723 break;
14724
14725 case tcc_expression:
14726 switch (TREE_CODE (t))
14727 {
14728 case COMPOUND_EXPR:
14729 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14730 return t;
14731 t = TREE_OPERAND (t, 0);
14732 break;
14733
14734 case COND_EXPR:
14735 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14736 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14737 return t;
14738 t = TREE_OPERAND (t, 0);
14739 break;
14740
14741 default:
14742 return t;
14743 }
14744 break;
14745
14746 default:
14747 return t;
14748 }
14749 }
14750
14751 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14752
14753 tree
14754 round_up_loc (location_t loc, tree value, unsigned int divisor)
14755 {
14756 tree div = NULL_TREE;
14757
14758 if (divisor == 1)
14759 return value;
14760
14761 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14762 have to do anything. Only do this when we are not given a const,
14763 because in that case, this check is more expensive than just
14764 doing it. */
14765 if (TREE_CODE (value) != INTEGER_CST)
14766 {
14767 div = build_int_cst (TREE_TYPE (value), divisor);
14768
14769 if (multiple_of_p (TREE_TYPE (value), value, div))
14770 return value;
14771 }
14772
14773 /* If divisor is a power of two, simplify this to bit manipulation. */
14774 if (divisor == (divisor & -divisor))
14775 {
14776 if (TREE_CODE (value) == INTEGER_CST)
14777 {
14778 wide_int val = value;
14779 bool overflow_p;
14780
14781 if ((val & (divisor - 1)) == 0)
14782 return value;
14783
14784 overflow_p = TREE_OVERFLOW (value);
14785 val += divisor - 1;
14786 val &= - (int) divisor;
14787 if (val == 0)
14788 overflow_p = true;
14789
14790 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14791 }
14792 else
14793 {
14794 tree t;
14795
14796 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14797 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14798 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14799 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14800 }
14801 }
14802 else
14803 {
14804 if (!div)
14805 div = build_int_cst (TREE_TYPE (value), divisor);
14806 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14807 value = size_binop_loc (loc, MULT_EXPR, value, div);
14808 }
14809
14810 return value;
14811 }
14812
14813 /* Likewise, but round down. */
14814
14815 tree
14816 round_down_loc (location_t loc, tree value, int divisor)
14817 {
14818 tree div = NULL_TREE;
14819
14820 gcc_assert (divisor > 0);
14821 if (divisor == 1)
14822 return value;
14823
14824 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14825 have to do anything. Only do this when we are not given a const,
14826 because in that case, this check is more expensive than just
14827 doing it. */
14828 if (TREE_CODE (value) != INTEGER_CST)
14829 {
14830 div = build_int_cst (TREE_TYPE (value), divisor);
14831
14832 if (multiple_of_p (TREE_TYPE (value), value, div))
14833 return value;
14834 }
14835
14836 /* If divisor is a power of two, simplify this to bit manipulation. */
14837 if (divisor == (divisor & -divisor))
14838 {
14839 tree t;
14840
14841 t = build_int_cst (TREE_TYPE (value), -divisor);
14842 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14843 }
14844 else
14845 {
14846 if (!div)
14847 div = build_int_cst (TREE_TYPE (value), divisor);
14848 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14849 value = size_binop_loc (loc, MULT_EXPR, value, div);
14850 }
14851
14852 return value;
14853 }
14854
14855 /* Returns the pointer to the base of the object addressed by EXP and
14856 extracts the information about the offset of the access, storing it
14857 to PBITPOS and POFFSET. */
14858
14859 static tree
14860 split_address_to_core_and_offset (tree exp,
14861 HOST_WIDE_INT *pbitpos, tree *poffset)
14862 {
14863 tree core;
14864 machine_mode mode;
14865 int unsignedp, volatilep;
14866 HOST_WIDE_INT bitsize;
14867 location_t loc = EXPR_LOCATION (exp);
14868
14869 if (TREE_CODE (exp) == ADDR_EXPR)
14870 {
14871 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14872 poffset, &mode, &unsignedp, &volatilep,
14873 false);
14874 core = build_fold_addr_expr_loc (loc, core);
14875 }
14876 else
14877 {
14878 core = exp;
14879 *pbitpos = 0;
14880 *poffset = NULL_TREE;
14881 }
14882
14883 return core;
14884 }
14885
14886 /* Returns true if addresses of E1 and E2 differ by a constant, false
14887 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14888
14889 bool
14890 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14891 {
14892 tree core1, core2;
14893 HOST_WIDE_INT bitpos1, bitpos2;
14894 tree toffset1, toffset2, tdiff, type;
14895
14896 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14897 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14898
14899 if (bitpos1 % BITS_PER_UNIT != 0
14900 || bitpos2 % BITS_PER_UNIT != 0
14901 || !operand_equal_p (core1, core2, 0))
14902 return false;
14903
14904 if (toffset1 && toffset2)
14905 {
14906 type = TREE_TYPE (toffset1);
14907 if (type != TREE_TYPE (toffset2))
14908 toffset2 = fold_convert (type, toffset2);
14909
14910 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14911 if (!cst_and_fits_in_hwi (tdiff))
14912 return false;
14913
14914 *diff = int_cst_value (tdiff);
14915 }
14916 else if (toffset1 || toffset2)
14917 {
14918 /* If only one of the offsets is non-constant, the difference cannot
14919 be a constant. */
14920 return false;
14921 }
14922 else
14923 *diff = 0;
14924
14925 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14926 return true;
14927 }
14928
14929 /* Simplify the floating point expression EXP when the sign of the
14930 result is not significant. Return NULL_TREE if no simplification
14931 is possible. */
14932
14933 tree
14934 fold_strip_sign_ops (tree exp)
14935 {
14936 tree arg0, arg1;
14937 location_t loc = EXPR_LOCATION (exp);
14938
14939 switch (TREE_CODE (exp))
14940 {
14941 case ABS_EXPR:
14942 case NEGATE_EXPR:
14943 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14944 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
14945
14946 case MULT_EXPR:
14947 case RDIV_EXPR:
14948 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
14949 return NULL_TREE;
14950 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14951 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14952 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
14953 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
14954 arg0 ? arg0 : TREE_OPERAND (exp, 0),
14955 arg1 ? arg1 : TREE_OPERAND (exp, 1));
14956 break;
14957
14958 case COMPOUND_EXPR:
14959 arg0 = TREE_OPERAND (exp, 0);
14960 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14961 if (arg1)
14962 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
14963 break;
14964
14965 case COND_EXPR:
14966 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14967 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
14968 if (arg0 || arg1)
14969 return fold_build3_loc (loc,
14970 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
14971 arg0 ? arg0 : TREE_OPERAND (exp, 1),
14972 arg1 ? arg1 : TREE_OPERAND (exp, 2));
14973 break;
14974
14975 case CALL_EXPR:
14976 {
14977 const enum built_in_function fcode = builtin_mathfn_code (exp);
14978 switch (fcode)
14979 {
14980 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14981 /* Strip copysign function call, return the 1st argument. */
14982 arg0 = CALL_EXPR_ARG (exp, 0);
14983 arg1 = CALL_EXPR_ARG (exp, 1);
14984 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
14985
14986 default:
14987 /* Strip sign ops from the argument of "odd" math functions. */
14988 if (negate_mathfn_p (fcode))
14989 {
14990 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
14991 if (arg0)
14992 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
14993 }
14994 break;
14995 }
14996 }
14997 break;
14998
14999 default:
15000 break;
15001 }
15002 return NULL_TREE;
15003 }
15004
15005 /* Return OFF converted to a pointer offset type suitable as offset for
15006 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
15007 tree
15008 convert_to_ptrofftype_loc (location_t loc, tree off)
15009 {
15010 return fold_convert_loc (loc, sizetype, off);
15011 }
15012
15013 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
15014 tree
15015 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
15016 {
15017 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
15018 ptr, convert_to_ptrofftype_loc (loc, off));
15019 }
15020
15021 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
15022 tree
15023 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
15024 {
15025 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
15026 ptr, size_int (off));
15027 }