c++: over-eager friend matching [PR109649]
[official-gcc.git] / gcc / fold-const-call.cc
blobfa0b287cc8adfb16d62012d31aaaf4bf153c3e78
1 /* Constant folding for calls to built-in and internal functions.
2 Copyright (C) 1988-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "realmpfr.h"
24 #include "tree.h"
25 #include "stor-layout.h"
26 #include "options.h"
27 #include "fold-const.h"
28 #include "fold-const-call.h"
29 #include "case-cfn-macros.h"
30 #include "tm.h" /* For C[LT]Z_DEFINED_AT_ZERO. */
31 #include "builtins.h"
32 #include "gimple-expr.h"
33 #include "tree-vector-builder.h"
35 /* Functions that test for certain constant types, abstracting away the
36 decision about whether to check for overflow. */
38 static inline bool
39 integer_cst_p (tree t)
41 return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
44 static inline bool
45 real_cst_p (tree t)
47 return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
50 static inline bool
51 complex_cst_p (tree t)
53 return TREE_CODE (t) == COMPLEX_CST;
56 /* Return true if ARG is a size_type_node constant.
57 Store it in *SIZE_OUT if so. */
59 static inline bool
60 size_t_cst_p (tree t, unsigned HOST_WIDE_INT *size_out)
62 if (types_compatible_p (size_type_node, TREE_TYPE (t))
63 && integer_cst_p (t)
64 && tree_fits_uhwi_p (t))
66 *size_out = tree_to_uhwi (t);
67 return true;
69 return false;
72 /* RES is the result of a comparison in which < 0 means "less", 0 means
73 "equal" and > 0 means "more". Canonicalize it to -1, 0 or 1 and
74 return it in type TYPE. */
76 tree
77 build_cmp_result (tree type, int res)
79 return build_int_cst (type, res < 0 ? -1 : res > 0 ? 1 : 0);
82 /* M is the result of trying to constant-fold an expression (starting
83 with clear MPFR flags) and INEXACT says whether the result in M is
84 exact or inexact. Return true if M can be used as a constant-folded
85 result in format FORMAT, storing the value in *RESULT if so. */
87 static bool
88 do_mpfr_ckconv (real_value *result, mpfr_srcptr m, bool inexact,
89 const real_format *format)
91 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
92 overflow/underflow occurred. If -frounding-math, proceed iff the
93 result of calling FUNC was exact. */
94 if (!mpfr_number_p (m)
95 || mpfr_overflow_p ()
96 || mpfr_underflow_p ()
97 || (flag_rounding_math && inexact))
98 return false;
100 REAL_VALUE_TYPE tmp;
101 real_from_mpfr (&tmp, m, format, MPFR_RNDN);
103 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
104 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
105 underflowed in the conversion. */
106 if (!real_isfinite (&tmp)
107 || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
108 return false;
110 real_convert (result, format, &tmp);
111 return real_identical (result, &tmp);
114 /* Try to evaluate:
116 *RESULT = f (*ARG)
118 in format FORMAT, given that FUNC is the MPFR implementation of f.
119 Return true on success. */
121 static bool
122 do_mpfr_arg1 (real_value *result,
123 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_rnd_t),
124 const real_value *arg, const real_format *format)
126 /* To proceed, MPFR must exactly represent the target floating point
127 format, which only happens when the target base equals two. */
128 if (format->b != 2 || !real_isfinite (arg))
129 return false;
131 int prec = format->p;
132 mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
134 auto_mpfr m (prec);
135 mpfr_from_real (m, arg, MPFR_RNDN);
136 mpfr_clear_flags ();
137 bool inexact = func (m, m, rnd);
138 bool ok = do_mpfr_ckconv (result, m, inexact, format);
140 return ok;
143 /* Try to evaluate:
145 *RESULT_SIN = sin (*ARG);
146 *RESULT_COS = cos (*ARG);
148 for format FORMAT. Return true on success. */
150 static bool
151 do_mpfr_sincos (real_value *result_sin, real_value *result_cos,
152 const real_value *arg, const real_format *format)
154 /* To proceed, MPFR must exactly represent the target floating point
155 format, which only happens when the target base equals two. */
156 if (format->b != 2 || !real_isfinite (arg))
157 return false;
159 int prec = format->p;
160 mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
161 mpfr_t m, ms, mc;
163 mpfr_inits2 (prec, m, ms, mc, NULL);
164 mpfr_from_real (m, arg, MPFR_RNDN);
165 mpfr_clear_flags ();
166 bool inexact = mpfr_sin_cos (ms, mc, m, rnd);
167 bool ok = (do_mpfr_ckconv (result_sin, ms, inexact, format)
168 && do_mpfr_ckconv (result_cos, mc, inexact, format));
169 mpfr_clears (m, ms, mc, NULL);
171 return ok;
174 /* Try to evaluate:
176 *RESULT = f (*ARG0, *ARG1)
178 in format FORMAT, given that FUNC is the MPFR implementation of f.
179 Return true on success. */
181 static bool
182 do_mpfr_arg2 (real_value *result,
183 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t),
184 const real_value *arg0, const real_value *arg1,
185 const real_format *format)
187 /* To proceed, MPFR must exactly represent the target floating point
188 format, which only happens when the target base equals two. */
189 if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
190 return false;
192 int prec = format->p;
193 mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
194 mpfr_t m0, m1;
196 mpfr_inits2 (prec, m0, m1, NULL);
197 mpfr_from_real (m0, arg0, MPFR_RNDN);
198 mpfr_from_real (m1, arg1, MPFR_RNDN);
199 mpfr_clear_flags ();
200 bool inexact = func (m0, m0, m1, rnd);
201 bool ok = do_mpfr_ckconv (result, m0, inexact, format);
202 mpfr_clears (m0, m1, NULL);
204 return ok;
207 /* Try to evaluate:
209 *RESULT = f (ARG0, *ARG1)
211 in format FORMAT, given that FUNC is the MPFR implementation of f.
212 Return true on success. */
214 static bool
215 do_mpfr_arg2 (real_value *result,
216 int (*func) (mpfr_ptr, long, mpfr_srcptr, mpfr_rnd_t),
217 const wide_int_ref &arg0, const real_value *arg1,
218 const real_format *format)
220 if (format->b != 2 || !real_isfinite (arg1))
221 return false;
223 int prec = format->p;
224 mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
226 auto_mpfr m (prec);
227 mpfr_from_real (m, arg1, MPFR_RNDN);
228 mpfr_clear_flags ();
229 bool inexact = func (m, arg0.to_shwi (), m, rnd);
230 bool ok = do_mpfr_ckconv (result, m, inexact, format);
232 return ok;
235 /* Try to evaluate:
237 *RESULT = f (*ARG0, *ARG1, *ARG2)
239 in format FORMAT, given that FUNC is the MPFR implementation of f.
240 Return true on success. */
242 static bool
243 do_mpfr_arg3 (real_value *result,
244 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr,
245 mpfr_srcptr, mpfr_rnd_t),
246 const real_value *arg0, const real_value *arg1,
247 const real_value *arg2, const real_format *format)
249 /* To proceed, MPFR must exactly represent the target floating point
250 format, which only happens when the target base equals two. */
251 if (format->b != 2
252 || !real_isfinite (arg0)
253 || !real_isfinite (arg1)
254 || !real_isfinite (arg2))
255 return false;
257 int prec = format->p;
258 mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
259 mpfr_t m0, m1, m2;
261 mpfr_inits2 (prec, m0, m1, m2, NULL);
262 mpfr_from_real (m0, arg0, MPFR_RNDN);
263 mpfr_from_real (m1, arg1, MPFR_RNDN);
264 mpfr_from_real (m2, arg2, MPFR_RNDN);
265 mpfr_clear_flags ();
266 bool inexact = func (m0, m0, m1, m2, rnd);
267 bool ok = do_mpfr_ckconv (result, m0, inexact, format);
268 mpfr_clears (m0, m1, m2, NULL);
270 return ok;
273 /* M is the result of trying to constant-fold an expression (starting
274 with clear MPFR flags) and INEXACT says whether the result in M is
275 exact or inexact. Return true if M can be used as a constant-folded
276 result in which the real and imaginary parts have format FORMAT.
277 Store those parts in *RESULT_REAL and *RESULT_IMAG if so. */
279 static bool
280 do_mpc_ckconv (real_value *result_real, real_value *result_imag,
281 mpc_srcptr m, bool inexact, const real_format *format)
283 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
284 overflow/underflow occurred. If -frounding-math, proceed iff the
285 result of calling FUNC was exact. */
286 if (!mpfr_number_p (mpc_realref (m))
287 || !mpfr_number_p (mpc_imagref (m))
288 || mpfr_overflow_p ()
289 || mpfr_underflow_p ()
290 || (flag_rounding_math && inexact))
291 return false;
293 REAL_VALUE_TYPE tmp_real, tmp_imag;
294 real_from_mpfr (&tmp_real, mpc_realref (m), format, MPFR_RNDN);
295 real_from_mpfr (&tmp_imag, mpc_imagref (m), format, MPFR_RNDN);
297 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
298 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
299 underflowed in the conversion. */
300 if (!real_isfinite (&tmp_real)
301 || !real_isfinite (&tmp_imag)
302 || (tmp_real.cl == rvc_zero) != (mpfr_zero_p (mpc_realref (m)) != 0)
303 || (tmp_imag.cl == rvc_zero) != (mpfr_zero_p (mpc_imagref (m)) != 0))
304 return false;
306 real_convert (result_real, format, &tmp_real);
307 real_convert (result_imag, format, &tmp_imag);
309 return (real_identical (result_real, &tmp_real)
310 && real_identical (result_imag, &tmp_imag));
313 /* Try to evaluate:
315 RESULT = f (ARG)
317 in format FORMAT, given that FUNC is the mpc implementation of f.
318 Return true on success. Both RESULT and ARG are represented as
319 real and imaginary pairs. */
321 static bool
322 do_mpc_arg1 (real_value *result_real, real_value *result_imag,
323 int (*func) (mpc_ptr, mpc_srcptr, mpc_rnd_t),
324 const real_value *arg_real, const real_value *arg_imag,
325 const real_format *format)
327 /* To proceed, MPFR must exactly represent the target floating point
328 format, which only happens when the target base equals two. */
329 if (format->b != 2
330 || !real_isfinite (arg_real)
331 || !real_isfinite (arg_imag))
332 return false;
334 int prec = format->p;
335 mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
336 mpc_t m;
338 mpc_init2 (m, prec);
339 mpfr_from_real (mpc_realref (m), arg_real, MPFR_RNDN);
340 mpfr_from_real (mpc_imagref (m), arg_imag, MPFR_RNDN);
341 mpfr_clear_flags ();
342 bool inexact = func (m, m, crnd);
343 bool ok = do_mpc_ckconv (result_real, result_imag, m, inexact, format);
344 mpc_clear (m);
346 return ok;
349 /* Try to evaluate:
351 RESULT = f (ARG0, ARG1)
353 in format FORMAT, given that FUNC is the mpc implementation of f.
354 Return true on success. RESULT, ARG0 and ARG1 are represented as
355 real and imaginary pairs. */
357 static bool
358 do_mpc_arg2 (real_value *result_real, real_value *result_imag,
359 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t),
360 const real_value *arg0_real, const real_value *arg0_imag,
361 const real_value *arg1_real, const real_value *arg1_imag,
362 const real_format *format)
364 if (!real_isfinite (arg0_real)
365 || !real_isfinite (arg0_imag)
366 || !real_isfinite (arg1_real)
367 || !real_isfinite (arg1_imag))
368 return false;
370 int prec = format->p;
371 mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
372 mpc_t m0, m1;
374 mpc_init2 (m0, prec);
375 mpc_init2 (m1, prec);
376 mpfr_from_real (mpc_realref (m0), arg0_real, MPFR_RNDN);
377 mpfr_from_real (mpc_imagref (m0), arg0_imag, MPFR_RNDN);
378 mpfr_from_real (mpc_realref (m1), arg1_real, MPFR_RNDN);
379 mpfr_from_real (mpc_imagref (m1), arg1_imag, MPFR_RNDN);
380 mpfr_clear_flags ();
381 bool inexact = func (m0, m0, m1, crnd);
382 bool ok = do_mpc_ckconv (result_real, result_imag, m0, inexact, format);
383 mpc_clear (m0);
384 mpc_clear (m1);
386 return ok;
389 /* Try to evaluate:
391 *RESULT = logb (*ARG)
393 in format FORMAT. Return true on success. */
395 static bool
396 fold_const_logb (real_value *result, const real_value *arg,
397 const real_format *format)
399 switch (arg->cl)
401 case rvc_nan:
402 /* If arg is +-NaN, then return it. */
403 *result = *arg;
404 return true;
406 case rvc_inf:
407 /* If arg is +-Inf, then return +Inf. */
408 *result = *arg;
409 result->sign = 0;
410 return true;
412 case rvc_zero:
413 /* Zero may set errno and/or raise an exception. */
414 return false;
416 case rvc_normal:
417 /* For normal numbers, proceed iff radix == 2. In GCC,
418 normalized significands are in the range [0.5, 1.0). We
419 want the exponent as if they were [1.0, 2.0) so get the
420 exponent and subtract 1. */
421 if (format->b == 2)
423 real_from_integer (result, format, REAL_EXP (arg) - 1, SIGNED);
424 return true;
426 return false;
430 /* Try to evaluate:
432 *RESULT = significand (*ARG)
434 in format FORMAT. Return true on success. */
436 static bool
437 fold_const_significand (real_value *result, const real_value *arg,
438 const real_format *format)
440 switch (arg->cl)
442 case rvc_zero:
443 case rvc_nan:
444 case rvc_inf:
445 /* If arg is +-0, +-Inf or +-NaN, then return it. */
446 *result = *arg;
447 return true;
449 case rvc_normal:
450 /* For normal numbers, proceed iff radix == 2. */
451 if (format->b == 2)
453 *result = *arg;
454 /* In GCC, normalized significands are in the range [0.5, 1.0).
455 We want them to be [1.0, 2.0) so set the exponent to 1. */
456 SET_REAL_EXP (result, 1);
457 return true;
459 return false;
463 /* Try to evaluate:
465 *RESULT = f (*ARG)
467 where FORMAT is the format of *ARG and PRECISION is the number of
468 significant bits in the result. Return true on success. */
470 static bool
471 fold_const_conversion (wide_int *result,
472 void (*fn) (real_value *, format_helper,
473 const real_value *),
474 const real_value *arg, unsigned int precision,
475 const real_format *format)
477 if (!real_isfinite (arg))
478 return false;
480 real_value rounded;
481 fn (&rounded, format, arg);
483 bool fail = false;
484 *result = real_to_integer (&rounded, &fail, precision);
485 return !fail;
488 /* Try to evaluate:
490 *RESULT = pow (*ARG0, *ARG1)
492 in format FORMAT. Return true on success. */
494 static bool
495 fold_const_pow (real_value *result, const real_value *arg0,
496 const real_value *arg1, const real_format *format)
498 if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
499 return true;
501 /* Check for an integer exponent. */
502 REAL_VALUE_TYPE cint1;
503 HOST_WIDE_INT n1 = real_to_integer (arg1);
504 real_from_integer (&cint1, VOIDmode, n1, SIGNED);
505 /* Attempt to evaluate pow at compile-time, unless this should
506 raise an exception. */
507 if (real_identical (arg1, &cint1)
508 && (n1 > 0
509 || (!flag_trapping_math && !flag_errno_math)
510 || !real_equal (arg0, &dconst0)))
512 bool inexact = real_powi (result, format, arg0, n1);
513 /* Avoid the folding if flag_signaling_nans is on. */
514 if (flag_unsafe_math_optimizations
515 || (!inexact
516 && !(flag_signaling_nans
517 && REAL_VALUE_ISSIGNALING_NAN (*arg0))))
518 return true;
521 return false;
524 /* Try to evaluate:
526 *RESULT = nextafter (*ARG0, *ARG1)
530 *RESULT = nexttoward (*ARG0, *ARG1)
532 in format FORMAT. Return true on success. */
534 static bool
535 fold_const_nextafter (real_value *result, const real_value *arg0,
536 const real_value *arg1, const real_format *format)
538 if (REAL_VALUE_ISSIGNALING_NAN (*arg0)
539 || REAL_VALUE_ISSIGNALING_NAN (*arg1))
540 return false;
542 /* Don't handle composite modes, nor decimal, nor modes without
543 inf or denorm at least for now. */
544 if (format->pnan < format->p
545 || format->b == 10
546 || !format->has_inf
547 || !format->has_denorm)
548 return false;
550 if (real_nextafter (result, format, arg0, arg1)
551 /* If raising underflow or overflow and setting errno to ERANGE,
552 fail if we care about those side-effects. */
553 && (flag_trapping_math || flag_errno_math))
554 return false;
555 /* Similarly for nextafter (0, 1) raising underflow. */
556 else if (flag_trapping_math
557 && arg0->cl == rvc_zero
558 && result->cl != rvc_zero)
559 return false;
561 real_convert (result, format, result);
563 return true;
566 /* Try to evaluate:
568 *RESULT = ldexp (*ARG0, ARG1)
570 in format FORMAT. Return true on success. */
572 static bool
573 fold_const_builtin_load_exponent (real_value *result, const real_value *arg0,
574 const wide_int_ref &arg1,
575 const real_format *format)
577 /* Bound the maximum adjustment to twice the range of the
578 mode's valid exponents. Use abs to ensure the range is
579 positive as a sanity check. */
580 int max_exp_adj = 2 * labs (format->emax - format->emin);
582 /* The requested adjustment must be inside this range. This
583 is a preliminary cap to avoid things like overflow, we
584 may still fail to compute the result for other reasons. */
585 if (wi::les_p (arg1, -max_exp_adj) || wi::ges_p (arg1, max_exp_adj))
586 return false;
588 /* Don't perform operation if we honor signaling NaNs and
589 operand is a signaling NaN. */
590 if (!flag_unsafe_math_optimizations
591 && flag_signaling_nans
592 && REAL_VALUE_ISSIGNALING_NAN (*arg0))
593 return false;
595 REAL_VALUE_TYPE initial_result;
596 real_ldexp (&initial_result, arg0, arg1.to_shwi ());
598 /* Ensure we didn't overflow. */
599 if (real_isinf (&initial_result))
600 return false;
602 /* Only proceed if the target mode can hold the
603 resulting value. */
604 *result = real_value_truncate (format, initial_result);
605 return real_equal (&initial_result, result);
608 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
609 return type TYPE. QUIET is true if a quiet rather than signalling
610 NaN is required. */
612 static tree
613 fold_const_builtin_nan (tree type, tree arg, bool quiet)
615 REAL_VALUE_TYPE real;
616 const char *str = c_getstr (arg);
617 if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
618 return build_real (type, real);
619 return NULL_TREE;
622 /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE. */
624 static tree
625 fold_const_reduction (tree type, tree arg, tree_code code)
627 unsigned HOST_WIDE_INT nelts;
628 if (TREE_CODE (arg) != VECTOR_CST
629 || !VECTOR_CST_NELTS (arg).is_constant (&nelts))
630 return NULL_TREE;
632 tree res = VECTOR_CST_ELT (arg, 0);
633 for (unsigned HOST_WIDE_INT i = 1; i < nelts; i++)
635 res = const_binop (code, type, res, VECTOR_CST_ELT (arg, i));
636 if (res == NULL_TREE || !CONSTANT_CLASS_P (res))
637 return NULL_TREE;
639 return res;
642 /* Fold a call to IFN_VEC_CONVERT (ARG) returning TYPE. */
644 static tree
645 fold_const_vec_convert (tree ret_type, tree arg)
647 enum tree_code code = NOP_EXPR;
648 tree arg_type = TREE_TYPE (arg);
649 if (TREE_CODE (arg) != VECTOR_CST)
650 return NULL_TREE;
652 gcc_checking_assert (VECTOR_TYPE_P (ret_type) && VECTOR_TYPE_P (arg_type));
654 if (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
655 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg_type)))
656 code = FIX_TRUNC_EXPR;
657 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
658 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (ret_type)))
659 code = FLOAT_EXPR;
661 /* We can't handle steps directly when extending, since the
662 values need to wrap at the original precision first. */
663 bool step_ok_p
664 = (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
665 && INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
666 && (TYPE_PRECISION (TREE_TYPE (ret_type))
667 <= TYPE_PRECISION (TREE_TYPE (arg_type))));
668 tree_vector_builder elts;
669 if (!elts.new_unary_operation (ret_type, arg, step_ok_p))
670 return NULL_TREE;
672 unsigned int count = elts.encoded_nelts ();
673 for (unsigned int i = 0; i < count; ++i)
675 tree elt = fold_unary (code, TREE_TYPE (ret_type),
676 VECTOR_CST_ELT (arg, i));
677 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
678 return NULL_TREE;
679 elts.quick_push (elt);
682 return elts.build ();
685 /* Try to evaluate:
687 IFN_WHILE_ULT (ARG0, ARG1, (TYPE) { ... })
689 Return the value on success and null on failure. */
691 static tree
692 fold_while_ult (tree type, poly_uint64 arg0, poly_uint64 arg1)
694 if (known_ge (arg0, arg1))
695 return build_zero_cst (type);
697 if (maybe_ge (arg0, arg1))
698 return NULL_TREE;
700 poly_uint64 diff = arg1 - arg0;
701 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
702 if (known_ge (diff, nelts))
703 return build_all_ones_cst (type);
705 unsigned HOST_WIDE_INT const_diff;
706 if (known_le (diff, nelts) && diff.is_constant (&const_diff))
708 tree minus_one = build_minus_one_cst (TREE_TYPE (type));
709 tree zero = build_zero_cst (TREE_TYPE (type));
710 return build_vector_a_then_b (type, const_diff, minus_one, zero);
712 return NULL_TREE;
715 /* Try to evaluate:
717 *RESULT = FN (*ARG)
719 in format FORMAT. Return true on success. */
721 static bool
722 fold_const_call_ss (real_value *result, combined_fn fn,
723 const real_value *arg, const real_format *format)
725 switch (fn)
727 CASE_CFN_SQRT:
728 CASE_CFN_SQRT_FN:
729 return (real_compare (GE_EXPR, arg, &dconst0)
730 && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
732 CASE_CFN_CBRT:
733 CASE_CFN_CBRT_FN:
734 return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
736 CASE_CFN_ASIN:
737 CASE_CFN_ASIN_FN:
738 return (real_compare (GE_EXPR, arg, &dconstm1)
739 && real_compare (LE_EXPR, arg, &dconst1)
740 && do_mpfr_arg1 (result, mpfr_asin, arg, format));
742 CASE_CFN_ACOS:
743 CASE_CFN_ACOS_FN:
744 return (real_compare (GE_EXPR, arg, &dconstm1)
745 && real_compare (LE_EXPR, arg, &dconst1)
746 && do_mpfr_arg1 (result, mpfr_acos, arg, format));
748 CASE_CFN_ATAN:
749 CASE_CFN_ATAN_FN:
750 return do_mpfr_arg1 (result, mpfr_atan, arg, format);
752 CASE_CFN_ASINH:
753 CASE_CFN_ASINH_FN:
754 return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
756 CASE_CFN_ACOSH:
757 CASE_CFN_ACOSH_FN:
758 return (real_compare (GE_EXPR, arg, &dconst1)
759 && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
761 CASE_CFN_ATANH:
762 CASE_CFN_ATANH_FN:
763 return (real_compare (GE_EXPR, arg, &dconstm1)
764 && real_compare (LE_EXPR, arg, &dconst1)
765 && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
767 CASE_CFN_SIN:
768 CASE_CFN_SIN_FN:
769 return do_mpfr_arg1 (result, mpfr_sin, arg, format);
771 CASE_CFN_COS:
772 CASE_CFN_COS_FN:
773 return do_mpfr_arg1 (result, mpfr_cos, arg, format);
775 CASE_CFN_TAN:
776 CASE_CFN_TAN_FN:
777 return do_mpfr_arg1 (result, mpfr_tan, arg, format);
779 CASE_CFN_SINH:
780 CASE_CFN_SINH_FN:
781 return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
783 CASE_CFN_COSH:
784 CASE_CFN_COSH_FN:
785 return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
787 CASE_CFN_TANH:
788 CASE_CFN_TANH_FN:
789 return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
791 CASE_CFN_ERF:
792 CASE_CFN_ERF_FN:
793 return do_mpfr_arg1 (result, mpfr_erf, arg, format);
795 CASE_CFN_ERFC:
796 CASE_CFN_ERFC_FN:
797 return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
799 CASE_CFN_TGAMMA:
800 CASE_CFN_TGAMMA_FN:
801 return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
803 CASE_CFN_EXP:
804 CASE_CFN_EXP_FN:
805 return do_mpfr_arg1 (result, mpfr_exp, arg, format);
807 CASE_CFN_EXP2:
808 CASE_CFN_EXP2_FN:
809 return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
811 CASE_CFN_EXP10:
812 CASE_CFN_POW10:
813 return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
815 CASE_CFN_EXPM1:
816 CASE_CFN_EXPM1_FN:
817 return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
819 CASE_CFN_LOG:
820 CASE_CFN_LOG_FN:
821 return (real_compare (GT_EXPR, arg, &dconst0)
822 && do_mpfr_arg1 (result, mpfr_log, arg, format));
824 CASE_CFN_LOG2:
825 CASE_CFN_LOG2_FN:
826 return (real_compare (GT_EXPR, arg, &dconst0)
827 && do_mpfr_arg1 (result, mpfr_log2, arg, format));
829 CASE_CFN_LOG10:
830 CASE_CFN_LOG10_FN:
831 return (real_compare (GT_EXPR, arg, &dconst0)
832 && do_mpfr_arg1 (result, mpfr_log10, arg, format));
834 CASE_CFN_LOG1P:
835 CASE_CFN_LOG1P_FN:
836 return (real_compare (GT_EXPR, arg, &dconstm1)
837 && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
839 CASE_CFN_J0:
840 return do_mpfr_arg1 (result, mpfr_j0, arg, format);
842 CASE_CFN_J1:
843 return do_mpfr_arg1 (result, mpfr_j1, arg, format);
845 CASE_CFN_Y0:
846 return (real_compare (GT_EXPR, arg, &dconst0)
847 && do_mpfr_arg1 (result, mpfr_y0, arg, format));
849 CASE_CFN_Y1:
850 return (real_compare (GT_EXPR, arg, &dconst0)
851 && do_mpfr_arg1 (result, mpfr_y1, arg, format));
853 CASE_CFN_FLOOR:
854 CASE_CFN_FLOOR_FN:
855 if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
857 real_floor (result, format, arg);
858 return true;
860 return false;
862 CASE_CFN_CEIL:
863 CASE_CFN_CEIL_FN:
864 if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
866 real_ceil (result, format, arg);
867 return true;
869 return false;
871 CASE_CFN_TRUNC:
872 CASE_CFN_TRUNC_FN:
873 if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
875 real_trunc (result, format, arg);
876 return true;
878 return false;
880 CASE_CFN_ROUND:
881 CASE_CFN_ROUND_FN:
882 if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
884 real_round (result, format, arg);
885 return true;
887 return false;
889 CASE_CFN_ROUNDEVEN:
890 CASE_CFN_ROUNDEVEN_FN:
891 if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
893 real_roundeven (result, format, arg);
894 return true;
896 return false;
898 CASE_CFN_LOGB:
899 CASE_CFN_LOGB_FN:
900 return fold_const_logb (result, arg, format);
902 CASE_CFN_SIGNIFICAND:
903 return fold_const_significand (result, arg, format);
905 default:
906 return false;
910 /* Try to evaluate:
912 *RESULT = FN (*ARG)
914 where FORMAT is the format of ARG and PRECISION is the number of
915 significant bits in the result. Return true on success. */
917 static bool
918 fold_const_call_ss (wide_int *result, combined_fn fn,
919 const real_value *arg, unsigned int precision,
920 const real_format *format)
922 switch (fn)
924 CASE_CFN_SIGNBIT:
925 if (real_isneg (arg))
926 *result = wi::one (precision);
927 else
928 *result = wi::zero (precision);
929 return true;
931 CASE_CFN_ILOGB:
932 CASE_CFN_ILOGB_FN:
933 /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
934 Proceed iff radix == 2. In GCC, normalized significands are in
935 the range [0.5, 1.0). We want the exponent as if they were
936 [1.0, 2.0) so get the exponent and subtract 1. */
937 if (arg->cl == rvc_normal && format->b == 2)
939 *result = wi::shwi (REAL_EXP (arg) - 1, precision);
940 return true;
942 return false;
944 CASE_CFN_ICEIL:
945 CASE_CFN_LCEIL:
946 CASE_CFN_LLCEIL:
947 return fold_const_conversion (result, real_ceil, arg,
948 precision, format);
950 CASE_CFN_LFLOOR:
951 CASE_CFN_IFLOOR:
952 CASE_CFN_LLFLOOR:
953 return fold_const_conversion (result, real_floor, arg,
954 precision, format);
956 CASE_CFN_IROUND:
957 CASE_CFN_LROUND:
958 CASE_CFN_LROUND_FN:
959 CASE_CFN_LLROUND:
960 CASE_CFN_LLROUND_FN:
961 return fold_const_conversion (result, real_round, arg,
962 precision, format);
964 CASE_CFN_IRINT:
965 CASE_CFN_LRINT:
966 CASE_CFN_LRINT_FN:
967 CASE_CFN_LLRINT:
968 CASE_CFN_LLRINT_FN:
969 /* Not yet folded to a constant. */
970 return false;
972 CASE_CFN_FINITE:
973 case CFN_BUILT_IN_FINITED32:
974 case CFN_BUILT_IN_FINITED64:
975 case CFN_BUILT_IN_FINITED128:
976 case CFN_BUILT_IN_ISFINITE:
977 *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
978 return true;
980 case CFN_BUILT_IN_ISSIGNALING:
981 *result = wi::shwi (real_issignaling_nan (arg) ? 1 : 0, precision);
982 return true;
984 CASE_CFN_ISINF:
985 case CFN_BUILT_IN_ISINFD32:
986 case CFN_BUILT_IN_ISINFD64:
987 case CFN_BUILT_IN_ISINFD128:
988 if (real_isinf (arg))
989 *result = wi::shwi (arg->sign ? -1 : 1, precision);
990 else
991 *result = wi::shwi (0, precision);
992 return true;
994 CASE_CFN_ISNAN:
995 case CFN_BUILT_IN_ISNAND32:
996 case CFN_BUILT_IN_ISNAND64:
997 case CFN_BUILT_IN_ISNAND128:
998 *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
999 return true;
1001 default:
1002 return false;
1006 /* Try to evaluate:
1008 *RESULT = FN (ARG)
1010 where ARG_TYPE is the type of ARG and PRECISION is the number of bits
1011 in the result. Return true on success. */
1013 static bool
1014 fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
1015 unsigned int precision, tree arg_type)
1017 switch (fn)
1019 CASE_CFN_FFS:
1020 *result = wi::shwi (wi::ffs (arg), precision);
1021 return true;
1023 CASE_CFN_CLZ:
1025 int tmp;
1026 if (wi::ne_p (arg, 0))
1027 tmp = wi::clz (arg);
1028 else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1029 tmp))
1030 tmp = TYPE_PRECISION (arg_type);
1031 *result = wi::shwi (tmp, precision);
1032 return true;
1035 CASE_CFN_CTZ:
1037 int tmp;
1038 if (wi::ne_p (arg, 0))
1039 tmp = wi::ctz (arg);
1040 else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1041 tmp))
1042 tmp = TYPE_PRECISION (arg_type);
1043 *result = wi::shwi (tmp, precision);
1044 return true;
1047 CASE_CFN_CLRSB:
1048 *result = wi::shwi (wi::clrsb (arg), precision);
1049 return true;
1051 CASE_CFN_POPCOUNT:
1052 *result = wi::shwi (wi::popcount (arg), precision);
1053 return true;
1055 CASE_CFN_PARITY:
1056 *result = wi::shwi (wi::parity (arg), precision);
1057 return true;
1059 case CFN_BUILT_IN_BSWAP16:
1060 case CFN_BUILT_IN_BSWAP32:
1061 case CFN_BUILT_IN_BSWAP64:
1062 case CFN_BUILT_IN_BSWAP128:
1063 *result = wide_int::from (arg, precision, TYPE_SIGN (arg_type)).bswap ();
1064 return true;
1066 default:
1067 return false;
1071 /* Try to evaluate:
1073 RESULT = FN (*ARG)
1075 where FORMAT is the format of ARG and of the real and imaginary parts
1076 of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
1077 true on success. */
1079 static bool
1080 fold_const_call_cs (real_value *result_real, real_value *result_imag,
1081 combined_fn fn, const real_value *arg,
1082 const real_format *format)
1084 switch (fn)
1086 CASE_CFN_CEXPI:
1087 /* cexpi(x+yi) = cos(x)+sin(y)*i. */
1088 return do_mpfr_sincos (result_imag, result_real, arg, format);
1090 default:
1091 return false;
1095 /* Try to evaluate:
1097 *RESULT = fn (ARG)
1099 where FORMAT is the format of RESULT and of the real and imaginary parts
1100 of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
1101 success. */
1103 static bool
1104 fold_const_call_sc (real_value *result, combined_fn fn,
1105 const real_value *arg_real, const real_value *arg_imag,
1106 const real_format *format)
1108 switch (fn)
1110 CASE_CFN_CABS:
1111 CASE_CFN_CABS_FN:
1112 return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
1114 default:
1115 return false;
1119 /* Try to evaluate:
1121 RESULT = fn (ARG)
1123 where FORMAT is the format of the real and imaginary parts of RESULT
1124 (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
1125 Return true on success. */
1127 static bool
1128 fold_const_call_cc (real_value *result_real, real_value *result_imag,
1129 combined_fn fn, const real_value *arg_real,
1130 const real_value *arg_imag, const real_format *format)
1132 switch (fn)
1134 CASE_CFN_CCOS:
1135 CASE_CFN_CCOS_FN:
1136 return do_mpc_arg1 (result_real, result_imag, mpc_cos,
1137 arg_real, arg_imag, format);
1139 CASE_CFN_CCOSH:
1140 CASE_CFN_CCOSH_FN:
1141 return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
1142 arg_real, arg_imag, format);
1144 CASE_CFN_CPROJ:
1145 CASE_CFN_CPROJ_FN:
1146 if (real_isinf (arg_real) || real_isinf (arg_imag))
1148 *result_real = dconstinf;
1149 *result_imag = dconst0;
1150 result_imag->sign = arg_imag->sign;
1152 else
1154 *result_real = *arg_real;
1155 *result_imag = *arg_imag;
1157 return true;
1159 CASE_CFN_CSIN:
1160 CASE_CFN_CSIN_FN:
1161 return do_mpc_arg1 (result_real, result_imag, mpc_sin,
1162 arg_real, arg_imag, format);
1164 CASE_CFN_CSINH:
1165 CASE_CFN_CSINH_FN:
1166 return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
1167 arg_real, arg_imag, format);
1169 CASE_CFN_CTAN:
1170 CASE_CFN_CTAN_FN:
1171 return do_mpc_arg1 (result_real, result_imag, mpc_tan,
1172 arg_real, arg_imag, format);
1174 CASE_CFN_CTANH:
1175 CASE_CFN_CTANH_FN:
1176 return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
1177 arg_real, arg_imag, format);
1179 CASE_CFN_CLOG:
1180 CASE_CFN_CLOG_FN:
1181 return do_mpc_arg1 (result_real, result_imag, mpc_log,
1182 arg_real, arg_imag, format);
1184 CASE_CFN_CSQRT:
1185 CASE_CFN_CSQRT_FN:
1186 return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
1187 arg_real, arg_imag, format);
1189 CASE_CFN_CASIN:
1190 CASE_CFN_CASIN_FN:
1191 return do_mpc_arg1 (result_real, result_imag, mpc_asin,
1192 arg_real, arg_imag, format);
1194 CASE_CFN_CACOS:
1195 CASE_CFN_CACOS_FN:
1196 return do_mpc_arg1 (result_real, result_imag, mpc_acos,
1197 arg_real, arg_imag, format);
1199 CASE_CFN_CATAN:
1200 CASE_CFN_CATAN_FN:
1201 return do_mpc_arg1 (result_real, result_imag, mpc_atan,
1202 arg_real, arg_imag, format);
1204 CASE_CFN_CASINH:
1205 CASE_CFN_CASINH_FN:
1206 return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
1207 arg_real, arg_imag, format);
1209 CASE_CFN_CACOSH:
1210 CASE_CFN_CACOSH_FN:
1211 return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
1212 arg_real, arg_imag, format);
1214 CASE_CFN_CATANH:
1215 CASE_CFN_CATANH_FN:
1216 return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1217 arg_real, arg_imag, format);
1219 CASE_CFN_CEXP:
1220 CASE_CFN_CEXP_FN:
1221 return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1222 arg_real, arg_imag, format);
1224 default:
1225 return false;
1229 /* Subroutine of fold_const_call, with the same interface. Handle cases
1230 where the arguments and result are numerical. */
1232 static tree
1233 fold_const_call_1 (combined_fn fn, tree type, tree arg)
1235 machine_mode mode = TYPE_MODE (type);
1236 machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1238 if (integer_cst_p (arg))
1240 if (SCALAR_INT_MODE_P (mode))
1242 wide_int result;
1243 if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
1244 TYPE_PRECISION (type), TREE_TYPE (arg)))
1245 return wide_int_to_tree (type, result);
1247 return NULL_TREE;
1250 if (real_cst_p (arg))
1252 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1253 if (mode == arg_mode)
1255 /* real -> real. */
1256 REAL_VALUE_TYPE result;
1257 if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1258 REAL_MODE_FORMAT (mode)))
1259 return build_real (type, result);
1261 else if (COMPLEX_MODE_P (mode)
1262 && GET_MODE_INNER (mode) == arg_mode)
1264 /* real -> complex real. */
1265 REAL_VALUE_TYPE result_real, result_imag;
1266 if (fold_const_call_cs (&result_real, &result_imag, fn,
1267 TREE_REAL_CST_PTR (arg),
1268 REAL_MODE_FORMAT (arg_mode)))
1269 return build_complex (type,
1270 build_real (TREE_TYPE (type), result_real),
1271 build_real (TREE_TYPE (type), result_imag));
1273 else if (INTEGRAL_TYPE_P (type))
1275 /* real -> int. */
1276 wide_int result;
1277 if (fold_const_call_ss (&result, fn,
1278 TREE_REAL_CST_PTR (arg),
1279 TYPE_PRECISION (type),
1280 REAL_MODE_FORMAT (arg_mode)))
1281 return wide_int_to_tree (type, result);
1283 return NULL_TREE;
1286 if (complex_cst_p (arg))
1288 gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1289 machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1290 tree argr = TREE_REALPART (arg);
1291 tree argi = TREE_IMAGPART (arg);
1292 if (mode == arg_mode
1293 && real_cst_p (argr)
1294 && real_cst_p (argi))
1296 /* complex real -> complex real. */
1297 REAL_VALUE_TYPE result_real, result_imag;
1298 if (fold_const_call_cc (&result_real, &result_imag, fn,
1299 TREE_REAL_CST_PTR (argr),
1300 TREE_REAL_CST_PTR (argi),
1301 REAL_MODE_FORMAT (inner_mode)))
1302 return build_complex (type,
1303 build_real (TREE_TYPE (type), result_real),
1304 build_real (TREE_TYPE (type), result_imag));
1306 if (mode == inner_mode
1307 && real_cst_p (argr)
1308 && real_cst_p (argi))
1310 /* complex real -> real. */
1311 REAL_VALUE_TYPE result;
1312 if (fold_const_call_sc (&result, fn,
1313 TREE_REAL_CST_PTR (argr),
1314 TREE_REAL_CST_PTR (argi),
1315 REAL_MODE_FORMAT (inner_mode)))
1316 return build_real (type, result);
1318 return NULL_TREE;
1321 return NULL_TREE;
1324 /* Try to fold FN (ARG) to a constant. Return the constant on success,
1325 otherwise return null. TYPE is the type of the return value. */
1327 tree
1328 fold_const_call (combined_fn fn, tree type, tree arg)
1330 switch (fn)
1332 case CFN_BUILT_IN_STRLEN:
1333 if (const char *str = c_getstr (arg))
1334 return build_int_cst (type, strlen (str));
1335 return NULL_TREE;
1337 CASE_CFN_NAN:
1338 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN):
1339 case CFN_BUILT_IN_NAND32:
1340 case CFN_BUILT_IN_NAND64:
1341 case CFN_BUILT_IN_NAND128:
1342 return fold_const_builtin_nan (type, arg, true);
1344 CASE_CFN_NANS:
1345 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
1346 case CFN_BUILT_IN_NANSF16B:
1347 case CFN_BUILT_IN_NANSD32:
1348 case CFN_BUILT_IN_NANSD64:
1349 case CFN_BUILT_IN_NANSD128:
1350 return fold_const_builtin_nan (type, arg, false);
1352 case CFN_REDUC_PLUS:
1353 return fold_const_reduction (type, arg, PLUS_EXPR);
1355 case CFN_REDUC_MAX:
1356 return fold_const_reduction (type, arg, MAX_EXPR);
1358 case CFN_REDUC_MIN:
1359 return fold_const_reduction (type, arg, MIN_EXPR);
1361 case CFN_REDUC_AND:
1362 return fold_const_reduction (type, arg, BIT_AND_EXPR);
1364 case CFN_REDUC_IOR:
1365 return fold_const_reduction (type, arg, BIT_IOR_EXPR);
1367 case CFN_REDUC_XOR:
1368 return fold_const_reduction (type, arg, BIT_XOR_EXPR);
1370 case CFN_VEC_CONVERT:
1371 return fold_const_vec_convert (type, arg);
1373 default:
1374 return fold_const_call_1 (fn, type, arg);
1378 /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1379 of type TYPE. */
1381 static tree
1382 fold_const_fold_left (tree type, tree arg0, tree arg1, tree_code code)
1384 if (TREE_CODE (arg1) != VECTOR_CST)
1385 return NULL_TREE;
1387 unsigned HOST_WIDE_INT nelts;
1388 if (!VECTOR_CST_NELTS (arg1).is_constant (&nelts))
1389 return NULL_TREE;
1391 for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
1393 arg0 = const_binop (code, type, arg0, VECTOR_CST_ELT (arg1, i));
1394 if (arg0 == NULL_TREE || !CONSTANT_CLASS_P (arg0))
1395 return NULL_TREE;
1397 return arg0;
1400 /* Try to evaluate:
1402 *RESULT = FN (*ARG0, *ARG1)
1404 in format FORMAT. Return true on success. */
1406 static bool
1407 fold_const_call_sss (real_value *result, combined_fn fn,
1408 const real_value *arg0, const real_value *arg1,
1409 const real_format *format)
1411 switch (fn)
1413 CASE_CFN_DREM:
1414 CASE_CFN_REMAINDER:
1415 CASE_CFN_REMAINDER_FN:
1416 return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1418 CASE_CFN_ATAN2:
1419 CASE_CFN_ATAN2_FN:
1420 return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1422 CASE_CFN_FDIM:
1423 CASE_CFN_FDIM_FN:
1424 return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1426 CASE_CFN_FMOD:
1427 CASE_CFN_FMOD_FN:
1428 return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
1430 CASE_CFN_HYPOT:
1431 CASE_CFN_HYPOT_FN:
1432 return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1434 CASE_CFN_COPYSIGN:
1435 CASE_CFN_COPYSIGN_FN:
1436 *result = *arg0;
1437 real_copysign (result, arg1);
1438 return true;
1440 CASE_CFN_FMIN:
1441 CASE_CFN_FMIN_FN:
1442 return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1444 CASE_CFN_FMAX:
1445 CASE_CFN_FMAX_FN:
1446 return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1448 CASE_CFN_POW:
1449 CASE_CFN_POW_FN:
1450 return fold_const_pow (result, arg0, arg1, format);
1452 CASE_CFN_NEXTAFTER:
1453 CASE_CFN_NEXTAFTER_FN:
1454 case CFN_BUILT_IN_NEXTAFTERF16B:
1455 CASE_CFN_NEXTTOWARD:
1456 return fold_const_nextafter (result, arg0, arg1, format);
1458 default:
1459 return false;
1463 /* Try to evaluate:
1465 *RESULT = FN (*ARG0, ARG1)
1467 where FORMAT is the format of *RESULT and *ARG0. Return true on
1468 success. */
1470 static bool
1471 fold_const_call_sss (real_value *result, combined_fn fn,
1472 const real_value *arg0, const wide_int_ref &arg1,
1473 const real_format *format)
1475 switch (fn)
1477 CASE_CFN_LDEXP:
1478 CASE_CFN_LDEXP_FN:
1479 return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1481 CASE_CFN_SCALBN:
1482 CASE_CFN_SCALBN_FN:
1483 CASE_CFN_SCALBLN:
1484 CASE_CFN_SCALBLN_FN:
1485 return (format->b == 2
1486 && fold_const_builtin_load_exponent (result, arg0, arg1,
1487 format));
1489 CASE_CFN_POWI:
1490 /* Avoid the folding if flag_signaling_nans is on and
1491 operand is a signaling NaN. */
1492 if (!flag_unsafe_math_optimizations
1493 && flag_signaling_nans
1494 && REAL_VALUE_ISSIGNALING_NAN (*arg0))
1495 return false;
1497 real_powi (result, format, arg0, arg1.to_shwi ());
1498 return true;
1500 default:
1501 return false;
1505 /* Try to evaluate:
1507 *RESULT = FN (ARG0, *ARG1)
1509 where FORMAT is the format of *RESULT and *ARG1. Return true on
1510 success. */
1512 static bool
1513 fold_const_call_sss (real_value *result, combined_fn fn,
1514 const wide_int_ref &arg0, const real_value *arg1,
1515 const real_format *format)
1517 switch (fn)
1519 CASE_CFN_JN:
1520 return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1522 CASE_CFN_YN:
1523 return (real_compare (GT_EXPR, arg1, &dconst0)
1524 && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1526 default:
1527 return false;
1531 /* Try to evaluate:
1533 RESULT = fn (ARG0, ARG1)
1535 where FORMAT is the format of the real and imaginary parts of RESULT
1536 (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1537 and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1539 static bool
1540 fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1541 combined_fn fn, const real_value *arg0_real,
1542 const real_value *arg0_imag, const real_value *arg1_real,
1543 const real_value *arg1_imag, const real_format *format)
1545 switch (fn)
1547 CASE_CFN_CPOW:
1548 CASE_CFN_CPOW_FN:
1549 return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1550 arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1552 default:
1553 return false;
1557 /* Subroutine of fold_const_call, with the same interface. Handle cases
1558 where the arguments and result are numerical. */
1560 static tree
1561 fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1563 machine_mode mode = TYPE_MODE (type);
1564 machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1565 machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1567 if (mode == arg0_mode
1568 && real_cst_p (arg0)
1569 && real_cst_p (arg1))
1571 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1572 REAL_VALUE_TYPE result;
1573 if (arg0_mode == arg1_mode)
1575 /* real, real -> real. */
1576 if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1577 TREE_REAL_CST_PTR (arg1),
1578 REAL_MODE_FORMAT (mode)))
1579 return build_real (type, result);
1581 else if (arg1_mode == TYPE_MODE (long_double_type_node))
1582 switch (fn)
1584 CASE_CFN_NEXTTOWARD:
1585 /* real, long double -> real. */
1586 if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1587 TREE_REAL_CST_PTR (arg1),
1588 REAL_MODE_FORMAT (mode)))
1589 return build_real (type, result);
1590 break;
1591 default:
1592 break;
1594 return NULL_TREE;
1597 if (real_cst_p (arg0)
1598 && integer_cst_p (arg1))
1600 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1601 if (mode == arg0_mode)
1603 /* real, int -> real. */
1604 REAL_VALUE_TYPE result;
1605 if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1606 wi::to_wide (arg1),
1607 REAL_MODE_FORMAT (mode)))
1608 return build_real (type, result);
1610 return NULL_TREE;
1613 if (integer_cst_p (arg0)
1614 && real_cst_p (arg1))
1616 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1617 if (mode == arg1_mode)
1619 /* int, real -> real. */
1620 REAL_VALUE_TYPE result;
1621 if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1622 TREE_REAL_CST_PTR (arg1),
1623 REAL_MODE_FORMAT (mode)))
1624 return build_real (type, result);
1626 return NULL_TREE;
1629 if (arg0_mode == arg1_mode
1630 && complex_cst_p (arg0)
1631 && complex_cst_p (arg1))
1633 gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1634 machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1635 tree arg0r = TREE_REALPART (arg0);
1636 tree arg0i = TREE_IMAGPART (arg0);
1637 tree arg1r = TREE_REALPART (arg1);
1638 tree arg1i = TREE_IMAGPART (arg1);
1639 if (mode == arg0_mode
1640 && real_cst_p (arg0r)
1641 && real_cst_p (arg0i)
1642 && real_cst_p (arg1r)
1643 && real_cst_p (arg1i))
1645 /* complex real, complex real -> complex real. */
1646 REAL_VALUE_TYPE result_real, result_imag;
1647 if (fold_const_call_ccc (&result_real, &result_imag, fn,
1648 TREE_REAL_CST_PTR (arg0r),
1649 TREE_REAL_CST_PTR (arg0i),
1650 TREE_REAL_CST_PTR (arg1r),
1651 TREE_REAL_CST_PTR (arg1i),
1652 REAL_MODE_FORMAT (inner_mode)))
1653 return build_complex (type,
1654 build_real (TREE_TYPE (type), result_real),
1655 build_real (TREE_TYPE (type), result_imag));
1657 return NULL_TREE;
1660 return NULL_TREE;
1663 /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1664 otherwise return null. TYPE is the type of the return value. */
1666 tree
1667 fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1669 const char *p0, *p1;
1670 char c;
1671 switch (fn)
1673 case CFN_BUILT_IN_STRSPN:
1674 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1675 return build_int_cst (type, strspn (p0, p1));
1676 return NULL_TREE;
1678 case CFN_BUILT_IN_STRCSPN:
1679 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1680 return build_int_cst (type, strcspn (p0, p1));
1681 return NULL_TREE;
1683 case CFN_BUILT_IN_STRCMP:
1684 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1685 return build_cmp_result (type, strcmp (p0, p1));
1686 return NULL_TREE;
1688 case CFN_BUILT_IN_STRCASECMP:
1689 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1691 int r = strcmp (p0, p1);
1692 if (r == 0)
1693 return build_cmp_result (type, r);
1695 return NULL_TREE;
1697 case CFN_BUILT_IN_INDEX:
1698 case CFN_BUILT_IN_STRCHR:
1699 if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1701 const char *r = strchr (p0, c);
1702 if (r == NULL)
1703 return build_int_cst (type, 0);
1704 return fold_convert (type,
1705 fold_build_pointer_plus_hwi (arg0, r - p0));
1707 return NULL_TREE;
1709 case CFN_BUILT_IN_RINDEX:
1710 case CFN_BUILT_IN_STRRCHR:
1711 if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1713 const char *r = strrchr (p0, c);
1714 if (r == NULL)
1715 return build_int_cst (type, 0);
1716 return fold_convert (type,
1717 fold_build_pointer_plus_hwi (arg0, r - p0));
1719 return NULL_TREE;
1721 case CFN_BUILT_IN_STRSTR:
1722 if ((p1 = c_getstr (arg1)))
1724 if ((p0 = c_getstr (arg0)))
1726 const char *r = strstr (p0, p1);
1727 if (r == NULL)
1728 return build_int_cst (type, 0);
1729 return fold_convert (type,
1730 fold_build_pointer_plus_hwi (arg0, r - p0));
1732 if (*p1 == '\0')
1733 return fold_convert (type, arg0);
1735 return NULL_TREE;
1737 case CFN_FOLD_LEFT_PLUS:
1738 return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
1740 default:
1741 return fold_const_call_1 (fn, type, arg0, arg1);
1745 /* Try to evaluate:
1747 *RESULT = FN (*ARG0, *ARG1, *ARG2)
1749 in format FORMAT. Return true on success. */
1751 static bool
1752 fold_const_call_ssss (real_value *result, combined_fn fn,
1753 const real_value *arg0, const real_value *arg1,
1754 const real_value *arg2, const real_format *format)
1756 switch (fn)
1758 CASE_CFN_FMA:
1759 CASE_CFN_FMA_FN:
1760 return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
1762 case CFN_FMS:
1764 real_value new_arg2 = real_value_negate (arg2);
1765 return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, &new_arg2, format);
1768 case CFN_FNMA:
1770 real_value new_arg0 = real_value_negate (arg0);
1771 return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1, arg2, format);
1774 case CFN_FNMS:
1776 real_value new_arg0 = real_value_negate (arg0);
1777 real_value new_arg2 = real_value_negate (arg2);
1778 return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1,
1779 &new_arg2, format);
1782 default:
1783 return false;
1787 /* Subroutine of fold_const_call, with the same interface. Handle cases
1788 where the arguments and result are numerical. */
1790 static tree
1791 fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1793 machine_mode mode = TYPE_MODE (type);
1794 machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1795 machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1796 machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
1798 if (arg0_mode == arg1_mode
1799 && arg0_mode == arg2_mode
1800 && real_cst_p (arg0)
1801 && real_cst_p (arg1)
1802 && real_cst_p (arg2))
1804 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1805 if (mode == arg0_mode)
1807 /* real, real, real -> real. */
1808 REAL_VALUE_TYPE result;
1809 if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
1810 TREE_REAL_CST_PTR (arg1),
1811 TREE_REAL_CST_PTR (arg2),
1812 REAL_MODE_FORMAT (mode)))
1813 return build_real (type, result);
1815 return NULL_TREE;
1818 return NULL_TREE;
1821 /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
1822 success, otherwise return null. TYPE is the type of the return value. */
1824 tree
1825 fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1827 const char *p0, *p1;
1828 char c;
1829 unsigned HOST_WIDE_INT s0, s1, s2 = 0;
1830 switch (fn)
1832 case CFN_BUILT_IN_STRNCMP:
1833 if (!size_t_cst_p (arg2, &s2))
1834 return NULL_TREE;
1835 if (s2 == 0
1836 && !TREE_SIDE_EFFECTS (arg0)
1837 && !TREE_SIDE_EFFECTS (arg1))
1838 return build_int_cst (type, 0);
1839 else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1840 return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX)));
1841 return NULL_TREE;
1843 case CFN_BUILT_IN_STRNCASECMP:
1844 if (!size_t_cst_p (arg2, &s2))
1845 return NULL_TREE;
1846 if (s2 == 0
1847 && !TREE_SIDE_EFFECTS (arg0)
1848 && !TREE_SIDE_EFFECTS (arg1))
1849 return build_int_cst (type, 0);
1850 else if ((p0 = c_getstr (arg0))
1851 && (p1 = c_getstr (arg1))
1852 && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0)
1853 return build_int_cst (type, 0);
1854 return NULL_TREE;
1856 case CFN_BUILT_IN_BCMP:
1857 case CFN_BUILT_IN_MEMCMP:
1858 if (!size_t_cst_p (arg2, &s2))
1859 return NULL_TREE;
1860 if (s2 == 0
1861 && !TREE_SIDE_EFFECTS (arg0)
1862 && !TREE_SIDE_EFFECTS (arg1))
1863 return build_int_cst (type, 0);
1864 if ((p0 = getbyterep (arg0, &s0))
1865 && (p1 = getbyterep (arg1, &s1))
1866 && s2 <= s0
1867 && s2 <= s1)
1868 return build_cmp_result (type, memcmp (p0, p1, s2));
1869 return NULL_TREE;
1871 case CFN_BUILT_IN_MEMCHR:
1872 if (!size_t_cst_p (arg2, &s2))
1873 return NULL_TREE;
1874 if (s2 == 0
1875 && !TREE_SIDE_EFFECTS (arg0)
1876 && !TREE_SIDE_EFFECTS (arg1))
1877 return build_int_cst (type, 0);
1878 if ((p0 = getbyterep (arg0, &s0))
1879 && s2 <= s0
1880 && target_char_cst_p (arg1, &c))
1882 const char *r = (const char *) memchr (p0, c, s2);
1883 if (r == NULL)
1884 return build_int_cst (type, 0);
1885 return fold_convert (type,
1886 fold_build_pointer_plus_hwi (arg0, r - p0));
1888 return NULL_TREE;
1890 case CFN_WHILE_ULT:
1892 poly_uint64 parg0, parg1;
1893 if (poly_int_tree_p (arg0, &parg0) && poly_int_tree_p (arg1, &parg1))
1894 return fold_while_ult (type, parg0, parg1);
1895 return NULL_TREE;
1898 default:
1899 return fold_const_call_1 (fn, type, arg0, arg1, arg2);