Fix cygwin performance loss on linpack.
[official-gcc.git] / gcc / fold-const-call.c
blob94801d23fde99d5626832f235de099081e7797bc
1 /* Constant folding for calls to built-in and internal functions.
2 Copyright (C) 1988-2015 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. */
32 /* Functions that test for certain constant types, abstracting away the
33 decision about whether to check for overflow. */
35 static inline bool
36 integer_cst_p (tree t)
38 return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
41 static inline bool
42 real_cst_p (tree t)
44 return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
47 static inline bool
48 complex_cst_p (tree t)
50 return TREE_CODE (t) == COMPLEX_CST;
53 /* Return true if ARG is a constant in the range of the host size_t.
54 Store it in *SIZE_OUT if so. */
56 static inline bool
57 host_size_t_cst_p (tree t, size_t *size_out)
59 if (integer_cst_p (t)
60 && wi::min_precision (t, UNSIGNED) <= sizeof (size_t) * CHAR_BIT)
62 *size_out = tree_to_uhwi (t);
63 return true;
65 return false;
68 /* RES is the result of a comparison in which < 0 means "less", 0 means
69 "equal" and > 0 means "more". Canonicalize it to -1, 0 or 1 and
70 return it in type TYPE. */
72 static inline tree
73 build_cmp_result (tree type, int res)
75 return build_int_cst (type, res < 0 ? -1 : res > 0 ? 1 : 0);
78 /* M is the result of trying to constant-fold an expression (starting
79 with clear MPFR flags) and INEXACT says whether the result in M is
80 exact or inexact. Return true if M can be used as a constant-folded
81 result in format FORMAT, storing the value in *RESULT if so. */
83 static bool
84 do_mpfr_ckconv (real_value *result, mpfr_srcptr m, bool inexact,
85 const real_format *format)
87 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
88 overflow/underflow occurred. If -frounding-math, proceed iff the
89 result of calling FUNC was exact. */
90 if (!mpfr_number_p (m)
91 || mpfr_overflow_p ()
92 || mpfr_underflow_p ()
93 || (flag_rounding_math && inexact))
94 return false;
96 REAL_VALUE_TYPE tmp;
97 real_from_mpfr (&tmp, m, format, GMP_RNDN);
99 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
100 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
101 underflowed in the conversion. */
102 if (!real_isfinite (&tmp)
103 || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
104 return false;
106 real_convert (result, format, &tmp);
107 return real_identical (result, &tmp);
110 /* Try to evaluate:
112 *RESULT = f (*ARG)
114 in format FORMAT, given that FUNC is the MPFR implementation of f.
115 Return true on success. */
117 static bool
118 do_mpfr_arg1 (real_value *result,
119 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_rnd_t),
120 const real_value *arg, const real_format *format)
122 /* To proceed, MPFR must exactly represent the target floating point
123 format, which only happens when the target base equals two. */
124 if (format->b != 2 || !real_isfinite (arg))
125 return false;
127 int prec = format->p;
128 mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
129 mpfr_t m;
131 mpfr_init2 (m, prec);
132 mpfr_from_real (m, arg, GMP_RNDN);
133 mpfr_clear_flags ();
134 bool inexact = func (m, m, rnd);
135 bool ok = do_mpfr_ckconv (result, m, inexact, format);
136 mpfr_clear (m);
138 return ok;
141 /* Try to evaluate:
143 *RESULT_SIN = sin (*ARG);
144 *RESULT_COS = cos (*ARG);
146 for format FORMAT. Return true on success. */
148 static bool
149 do_mpfr_sincos (real_value *result_sin, real_value *result_cos,
150 const real_value *arg, const real_format *format)
152 /* To proceed, MPFR must exactly represent the target floating point
153 format, which only happens when the target base equals two. */
154 if (format->b != 2 || !real_isfinite (arg))
155 return false;
157 int prec = format->p;
158 mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
159 mpfr_t m, ms, mc;
161 mpfr_inits2 (prec, m, ms, mc, NULL);
162 mpfr_from_real (m, arg, GMP_RNDN);
163 mpfr_clear_flags ();
164 bool inexact = mpfr_sin_cos (ms, mc, m, rnd);
165 bool ok = (do_mpfr_ckconv (result_sin, ms, inexact, format)
166 && do_mpfr_ckconv (result_cos, mc, inexact, format));
167 mpfr_clears (m, ms, mc, NULL);
169 return ok;
172 /* Try to evaluate:
174 *RESULT = f (*ARG0, *ARG1)
176 in format FORMAT, given that FUNC is the MPFR implementation of f.
177 Return true on success. */
179 static bool
180 do_mpfr_arg2 (real_value *result,
181 int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t),
182 const real_value *arg0, const real_value *arg1,
183 const real_format *format)
185 /* To proceed, MPFR must exactly represent the target floating point
186 format, which only happens when the target base equals two. */
187 if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
188 return false;
190 int prec = format->p;
191 mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
192 mpfr_t m0, m1;
194 mpfr_inits2 (prec, m0, m1, NULL);
195 mpfr_from_real (m0, arg0, GMP_RNDN);
196 mpfr_from_real (m1, arg1, GMP_RNDN);
197 mpfr_clear_flags ();
198 bool inexact = func (m0, m0, m1, rnd);
199 bool ok = do_mpfr_ckconv (result, m0, inexact, format);
200 mpfr_clears (m0, m1, NULL);
202 return ok;
205 /* Try to evaluate:
207 *RESULT = f (ARG0, *ARG1)
209 in format FORMAT, given that FUNC is the MPFR implementation of f.
210 Return true on success. */
212 static bool
213 do_mpfr_arg2 (real_value *result,
214 int (*func) (mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
215 const wide_int_ref &arg0, const real_value *arg1,
216 const real_format *format)
218 if (format->b != 2 || !real_isfinite (arg1))
219 return false;
221 int prec = format->p;
222 mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
223 mpfr_t m;
225 mpfr_init2 (m, prec);
226 mpfr_from_real (m, arg1, GMP_RNDN);
227 mpfr_clear_flags ();
228 bool inexact = func (m, arg0.to_shwi (), m, rnd);
229 bool ok = do_mpfr_ckconv (result, m, inexact, format);
230 mpfr_clear (m);
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 mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
259 mpfr_t m0, m1, m2;
261 mpfr_inits2 (prec, m0, m1, m2, NULL);
262 mpfr_from_real (m0, arg0, GMP_RNDN);
263 mpfr_from_real (m1, arg1, GMP_RNDN);
264 mpfr_from_real (m2, arg2, GMP_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, GMP_RNDN);
295 real_from_mpfr (&tmp_imag, mpc_imagref (m), format, GMP_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, GMP_RNDN);
340 mpfr_from_real (mpc_imagref (m), arg_imag, GMP_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, GMP_RNDN);
377 mpfr_from_real (mpc_imagref (m0), arg0_imag, GMP_RNDN);
378 mpfr_from_real (mpc_realref (m1), arg1_real, GMP_RNDN);
379 mpfr_from_real (mpc_imagref (m1), arg1_imag, GMP_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;
428 gcc_unreachable ();
431 /* Try to evaluate:
433 *RESULT = significand (*ARG)
435 in format FORMAT. Return true on success. */
437 static bool
438 fold_const_significand (real_value *result, const real_value *arg,
439 const real_format *format)
441 switch (arg->cl)
443 case rvc_zero:
444 case rvc_nan:
445 case rvc_inf:
446 /* If arg is +-0, +-Inf or +-NaN, then return it. */
447 *result = *arg;
448 return true;
450 case rvc_normal:
451 /* For normal numbers, proceed iff radix == 2. */
452 if (format->b == 2)
454 *result = *arg;
455 /* In GCC, normalized significands are in the range [0.5, 1.0).
456 We want them to be [1.0, 2.0) so set the exponent to 1. */
457 SET_REAL_EXP (result, 1);
458 return true;
460 return false;
462 gcc_unreachable ();
465 /* Try to evaluate:
467 *RESULT = f (*ARG)
469 where FORMAT is the format of *ARG and PRECISION is the number of
470 significant bits in the result. Return true on success. */
472 static bool
473 fold_const_conversion (wide_int *result,
474 void (*fn) (real_value *, format_helper,
475 const real_value *),
476 const real_value *arg, unsigned int precision,
477 const real_format *format)
479 if (!real_isfinite (arg))
480 return false;
482 real_value rounded;
483 fn (&rounded, format, arg);
485 bool fail = false;
486 *result = real_to_integer (&rounded, &fail, precision);
487 return !fail;
490 /* Try to evaluate:
492 *RESULT = pow (*ARG0, *ARG1)
494 in format FORMAT. Return true on success. */
496 static bool
497 fold_const_pow (real_value *result, const real_value *arg0,
498 const real_value *arg1, const real_format *format)
500 if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
501 return true;
503 /* Check for an integer exponent. */
504 REAL_VALUE_TYPE cint1;
505 HOST_WIDE_INT n1 = real_to_integer (arg1);
506 real_from_integer (&cint1, VOIDmode, n1, SIGNED);
507 /* Attempt to evaluate pow at compile-time, unless this should
508 raise an exception. */
509 if (real_identical (arg1, &cint1)
510 && (n1 > 0
511 || (!flag_trapping_math && !flag_errno_math)
512 || !real_equal (arg0, &dconst0)))
514 bool inexact = real_powi (result, format, arg0, n1);
515 if (flag_unsafe_math_optimizations || !inexact)
516 return true;
519 return false;
522 /* Try to evaluate:
524 *RESULT = ldexp (*ARG0, ARG1)
526 in format FORMAT. Return true on success. */
528 static bool
529 fold_const_builtin_load_exponent (real_value *result, const real_value *arg0,
530 const wide_int_ref &arg1,
531 const real_format *format)
533 /* Bound the maximum adjustment to twice the range of the
534 mode's valid exponents. Use abs to ensure the range is
535 positive as a sanity check. */
536 int max_exp_adj = 2 * labs (format->emax - format->emin);
538 /* The requested adjustment must be inside this range. This
539 is a preliminary cap to avoid things like overflow, we
540 may still fail to compute the result for other reasons. */
541 if (wi::les_p (arg1, -max_exp_adj) || wi::ges_p (arg1, max_exp_adj))
542 return false;
544 REAL_VALUE_TYPE initial_result;
545 real_ldexp (&initial_result, arg0, arg1.to_shwi ());
547 /* Ensure we didn't overflow. */
548 if (real_isinf (&initial_result))
549 return false;
551 /* Only proceed if the target mode can hold the
552 resulting value. */
553 *result = real_value_truncate (format, initial_result);
554 return real_equal (&initial_result, result);
557 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
558 return type TYPE. QUIET is true if a quiet rather than signalling
559 NaN is required. */
561 static tree
562 fold_const_builtin_nan (tree type, tree arg, bool quiet)
564 REAL_VALUE_TYPE real;
565 const char *str = c_getstr (arg);
566 if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
567 return build_real (type, real);
568 return NULL_TREE;
571 /* Try to evaluate:
573 *RESULT = FN (*ARG)
575 in format FORMAT. Return true on success. */
577 static bool
578 fold_const_call_ss (real_value *result, combined_fn fn,
579 const real_value *arg, const real_format *format)
581 switch (fn)
583 CASE_CFN_SQRT:
584 return (real_compare (GE_EXPR, arg, &dconst0)
585 && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
587 CASE_CFN_CBRT:
588 return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
590 CASE_CFN_ASIN:
591 return (real_compare (GE_EXPR, arg, &dconstm1)
592 && real_compare (LE_EXPR, arg, &dconst1)
593 && do_mpfr_arg1 (result, mpfr_asin, arg, format));
595 CASE_CFN_ACOS:
596 return (real_compare (GE_EXPR, arg, &dconstm1)
597 && real_compare (LE_EXPR, arg, &dconst1)
598 && do_mpfr_arg1 (result, mpfr_acos, arg, format));
600 CASE_CFN_ATAN:
601 return do_mpfr_arg1 (result, mpfr_atan, arg, format);
603 CASE_CFN_ASINH:
604 return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
606 CASE_CFN_ACOSH:
607 return (real_compare (GE_EXPR, arg, &dconst1)
608 && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
610 CASE_CFN_ATANH:
611 return (real_compare (GE_EXPR, arg, &dconstm1)
612 && real_compare (LE_EXPR, arg, &dconst1)
613 && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
615 CASE_CFN_SIN:
616 return do_mpfr_arg1 (result, mpfr_sin, arg, format);
618 CASE_CFN_COS:
619 return do_mpfr_arg1 (result, mpfr_cos, arg, format);
621 CASE_CFN_TAN:
622 return do_mpfr_arg1 (result, mpfr_tan, arg, format);
624 CASE_CFN_SINH:
625 return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
627 CASE_CFN_COSH:
628 return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
630 CASE_CFN_TANH:
631 return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
633 CASE_CFN_ERF:
634 return do_mpfr_arg1 (result, mpfr_erf, arg, format);
636 CASE_CFN_ERFC:
637 return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
639 CASE_CFN_TGAMMA:
640 return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
642 CASE_CFN_EXP:
643 return do_mpfr_arg1 (result, mpfr_exp, arg, format);
645 CASE_CFN_EXP2:
646 return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
648 CASE_CFN_EXP10:
649 CASE_CFN_POW10:
650 return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
652 CASE_CFN_EXPM1:
653 return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
655 CASE_CFN_LOG:
656 return (real_compare (GT_EXPR, arg, &dconst0)
657 && do_mpfr_arg1 (result, mpfr_log, arg, format));
659 CASE_CFN_LOG2:
660 return (real_compare (GT_EXPR, arg, &dconst0)
661 && do_mpfr_arg1 (result, mpfr_log2, arg, format));
663 CASE_CFN_LOG10:
664 return (real_compare (GT_EXPR, arg, &dconst0)
665 && do_mpfr_arg1 (result, mpfr_log10, arg, format));
667 CASE_CFN_LOG1P:
668 return (real_compare (GT_EXPR, arg, &dconstm1)
669 && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
671 CASE_CFN_J0:
672 return do_mpfr_arg1 (result, mpfr_j0, arg, format);
674 CASE_CFN_J1:
675 return do_mpfr_arg1 (result, mpfr_j1, arg, format);
677 CASE_CFN_Y0:
678 return (real_compare (GT_EXPR, arg, &dconst0)
679 && do_mpfr_arg1 (result, mpfr_y0, arg, format));
681 CASE_CFN_Y1:
682 return (real_compare (GT_EXPR, arg, &dconst0)
683 && do_mpfr_arg1 (result, mpfr_y1, arg, format));
685 CASE_CFN_FLOOR:
686 if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
688 real_floor (result, format, arg);
689 return true;
691 return false;
693 CASE_CFN_CEIL:
694 if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
696 real_ceil (result, format, arg);
697 return true;
699 return false;
701 CASE_CFN_TRUNC:
702 real_trunc (result, format, arg);
703 return true;
705 CASE_CFN_ROUND:
706 if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
708 real_round (result, format, arg);
709 return true;
711 return false;
713 CASE_CFN_LOGB:
714 return fold_const_logb (result, arg, format);
716 CASE_CFN_SIGNIFICAND:
717 return fold_const_significand (result, arg, format);
719 default:
720 return false;
724 /* Try to evaluate:
726 *RESULT = FN (*ARG)
728 where FORMAT is the format of ARG and PRECISION is the number of
729 significant bits in the result. Return true on success. */
731 static bool
732 fold_const_call_ss (wide_int *result, combined_fn fn,
733 const real_value *arg, unsigned int precision,
734 const real_format *format)
736 switch (fn)
738 CASE_CFN_SIGNBIT:
739 if (real_isneg (arg))
740 *result = wi::one (precision);
741 else
742 *result = wi::zero (precision);
743 return true;
745 CASE_CFN_ILOGB:
746 /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
747 Proceed iff radix == 2. In GCC, normalized significands are in
748 the range [0.5, 1.0). We want the exponent as if they were
749 [1.0, 2.0) so get the exponent and subtract 1. */
750 if (arg->cl == rvc_normal && format->b == 2)
752 *result = wi::shwi (REAL_EXP (arg) - 1, precision);
753 return true;
755 return false;
757 CASE_CFN_ICEIL:
758 CASE_CFN_LCEIL:
759 CASE_CFN_LLCEIL:
760 return fold_const_conversion (result, real_ceil, arg,
761 precision, format);
763 CASE_CFN_LFLOOR:
764 CASE_CFN_IFLOOR:
765 CASE_CFN_LLFLOOR:
766 return fold_const_conversion (result, real_floor, arg,
767 precision, format);
769 CASE_CFN_IROUND:
770 CASE_CFN_LROUND:
771 CASE_CFN_LLROUND:
772 return fold_const_conversion (result, real_round, arg,
773 precision, format);
775 CASE_CFN_IRINT:
776 CASE_CFN_LRINT:
777 CASE_CFN_LLRINT:
778 /* Not yet folded to a constant. */
779 return false;
781 CASE_CFN_FINITE:
782 case CFN_BUILT_IN_FINITED32:
783 case CFN_BUILT_IN_FINITED64:
784 case CFN_BUILT_IN_FINITED128:
785 case CFN_BUILT_IN_ISFINITE:
786 *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
787 return true;
789 CASE_CFN_ISINF:
790 case CFN_BUILT_IN_ISINFD32:
791 case CFN_BUILT_IN_ISINFD64:
792 case CFN_BUILT_IN_ISINFD128:
793 if (real_isinf (arg))
794 *result = wi::shwi (arg->sign ? -1 : 1, precision);
795 else
796 *result = wi::shwi (0, precision);
797 return true;
799 CASE_CFN_ISNAN:
800 case CFN_BUILT_IN_ISNAND32:
801 case CFN_BUILT_IN_ISNAND64:
802 case CFN_BUILT_IN_ISNAND128:
803 *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
804 return true;
806 default:
807 return false;
811 /* Try to evaluate:
813 *RESULT = FN (ARG)
815 where ARG_TYPE is the type of ARG and PRECISION is the number of bits
816 in the result. Return true on success. */
818 static bool
819 fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
820 unsigned int precision, tree arg_type)
822 switch (fn)
824 CASE_CFN_FFS:
825 *result = wi::shwi (wi::ffs (arg), precision);
826 return true;
828 CASE_CFN_CLZ:
830 int tmp;
831 if (wi::ne_p (arg, 0))
832 tmp = wi::clz (arg);
833 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (arg_type), tmp))
834 tmp = TYPE_PRECISION (arg_type);
835 *result = wi::shwi (tmp, precision);
836 return true;
839 CASE_CFN_CTZ:
841 int tmp;
842 if (wi::ne_p (arg, 0))
843 tmp = wi::ctz (arg);
844 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (arg_type), tmp))
845 tmp = TYPE_PRECISION (arg_type);
846 *result = wi::shwi (tmp, precision);
847 return true;
850 CASE_CFN_CLRSB:
851 *result = wi::shwi (wi::clrsb (arg), precision);
852 return true;
854 CASE_CFN_POPCOUNT:
855 *result = wi::shwi (wi::popcount (arg), precision);
856 return true;
858 CASE_CFN_PARITY:
859 *result = wi::shwi (wi::parity (arg), precision);
860 return true;
862 case CFN_BUILT_IN_BSWAP16:
863 case CFN_BUILT_IN_BSWAP32:
864 case CFN_BUILT_IN_BSWAP64:
865 *result = wide_int::from (arg, precision, TYPE_SIGN (arg_type)).bswap ();
866 return true;
868 default:
869 return false;
873 /* Try to evaluate:
875 RESULT = FN (*ARG)
877 where FORMAT is the format of ARG and of the real and imaginary parts
878 of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
879 true on success. */
881 static bool
882 fold_const_call_cs (real_value *result_real, real_value *result_imag,
883 combined_fn fn, const real_value *arg,
884 const real_format *format)
886 switch (fn)
888 CASE_CFN_CEXPI:
889 /* cexpi(x+yi) = cos(x)+sin(y)*i. */
890 return do_mpfr_sincos (result_imag, result_real, arg, format);
892 default:
893 return false;
897 /* Try to evaluate:
899 *RESULT = fn (ARG)
901 where FORMAT is the format of RESULT and of the real and imaginary parts
902 of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
903 success. */
905 static bool
906 fold_const_call_sc (real_value *result, combined_fn fn,
907 const real_value *arg_real, const real_value *arg_imag,
908 const real_format *format)
910 switch (fn)
912 CASE_CFN_CABS:
913 return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
915 default:
916 return false;
920 /* Try to evaluate:
922 RESULT = fn (ARG)
924 where FORMAT is the format of the real and imaginary parts of RESULT
925 (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
926 Return true on success. */
928 static bool
929 fold_const_call_cc (real_value *result_real, real_value *result_imag,
930 combined_fn fn, const real_value *arg_real,
931 const real_value *arg_imag, const real_format *format)
933 switch (fn)
935 CASE_CFN_CCOS:
936 return do_mpc_arg1 (result_real, result_imag, mpc_cos,
937 arg_real, arg_imag, format);
939 CASE_CFN_CCOSH:
940 return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
941 arg_real, arg_imag, format);
943 CASE_CFN_CPROJ:
944 if (real_isinf (arg_real) || real_isinf (arg_imag))
946 real_inf (result_real);
947 *result_imag = dconst0;
948 result_imag->sign = arg_imag->sign;
950 else
952 *result_real = *arg_real;
953 *result_imag = *arg_imag;
955 return true;
957 CASE_CFN_CSIN:
958 return do_mpc_arg1 (result_real, result_imag, mpc_sin,
959 arg_real, arg_imag, format);
961 CASE_CFN_CSINH:
962 return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
963 arg_real, arg_imag, format);
965 CASE_CFN_CTAN:
966 return do_mpc_arg1 (result_real, result_imag, mpc_tan,
967 arg_real, arg_imag, format);
969 CASE_CFN_CTANH:
970 return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
971 arg_real, arg_imag, format);
973 CASE_CFN_CLOG:
974 return do_mpc_arg1 (result_real, result_imag, mpc_log,
975 arg_real, arg_imag, format);
977 CASE_CFN_CSQRT:
978 return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
979 arg_real, arg_imag, format);
981 CASE_CFN_CASIN:
982 return do_mpc_arg1 (result_real, result_imag, mpc_asin,
983 arg_real, arg_imag, format);
985 CASE_CFN_CACOS:
986 return do_mpc_arg1 (result_real, result_imag, mpc_acos,
987 arg_real, arg_imag, format);
989 CASE_CFN_CATAN:
990 return do_mpc_arg1 (result_real, result_imag, mpc_atan,
991 arg_real, arg_imag, format);
993 CASE_CFN_CASINH:
994 return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
995 arg_real, arg_imag, format);
997 CASE_CFN_CACOSH:
998 return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
999 arg_real, arg_imag, format);
1001 CASE_CFN_CATANH:
1002 return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1003 arg_real, arg_imag, format);
1005 CASE_CFN_CEXP:
1006 return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1007 arg_real, arg_imag, format);
1009 default:
1010 return false;
1014 /* Subroutine of fold_const_call, with the same interface. Handle cases
1015 where the arguments and result are numerical. */
1017 static tree
1018 fold_const_call_1 (combined_fn fn, tree type, tree arg)
1020 machine_mode mode = TYPE_MODE (type);
1021 machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1023 if (integer_cst_p (arg))
1025 if (SCALAR_INT_MODE_P (mode))
1027 wide_int result;
1028 if (fold_const_call_ss (&result, fn, arg, TYPE_PRECISION (type),
1029 TREE_TYPE (arg)))
1030 return wide_int_to_tree (type, result);
1032 return NULL_TREE;
1035 if (real_cst_p (arg))
1037 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1038 if (mode == arg_mode)
1040 /* real -> real. */
1041 REAL_VALUE_TYPE result;
1042 if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1043 REAL_MODE_FORMAT (mode)))
1044 return build_real (type, result);
1046 else if (COMPLEX_MODE_P (mode)
1047 && GET_MODE_INNER (mode) == arg_mode)
1049 /* real -> complex real. */
1050 REAL_VALUE_TYPE result_real, result_imag;
1051 if (fold_const_call_cs (&result_real, &result_imag, fn,
1052 TREE_REAL_CST_PTR (arg),
1053 REAL_MODE_FORMAT (arg_mode)))
1054 return build_complex (type,
1055 build_real (TREE_TYPE (type), result_real),
1056 build_real (TREE_TYPE (type), result_imag));
1058 else if (INTEGRAL_TYPE_P (type))
1060 /* real -> int. */
1061 wide_int result;
1062 if (fold_const_call_ss (&result, fn,
1063 TREE_REAL_CST_PTR (arg),
1064 TYPE_PRECISION (type),
1065 REAL_MODE_FORMAT (arg_mode)))
1066 return wide_int_to_tree (type, result);
1068 return NULL_TREE;
1071 if (complex_cst_p (arg))
1073 gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1074 machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1075 tree argr = TREE_REALPART (arg);
1076 tree argi = TREE_IMAGPART (arg);
1077 if (mode == arg_mode
1078 && real_cst_p (argr)
1079 && real_cst_p (argi))
1081 /* complex real -> complex real. */
1082 REAL_VALUE_TYPE result_real, result_imag;
1083 if (fold_const_call_cc (&result_real, &result_imag, fn,
1084 TREE_REAL_CST_PTR (argr),
1085 TREE_REAL_CST_PTR (argi),
1086 REAL_MODE_FORMAT (inner_mode)))
1087 return build_complex (type,
1088 build_real (TREE_TYPE (type), result_real),
1089 build_real (TREE_TYPE (type), result_imag));
1091 if (mode == inner_mode
1092 && real_cst_p (argr)
1093 && real_cst_p (argi))
1095 /* complex real -> real. */
1096 REAL_VALUE_TYPE result;
1097 if (fold_const_call_sc (&result, fn,
1098 TREE_REAL_CST_PTR (argr),
1099 TREE_REAL_CST_PTR (argi),
1100 REAL_MODE_FORMAT (inner_mode)))
1101 return build_real (type, result);
1103 return NULL_TREE;
1106 return NULL_TREE;
1109 /* Try to fold FN (ARG) to a constant. Return the constant on success,
1110 otherwise return null. TYPE is the type of the return value. */
1112 tree
1113 fold_const_call (combined_fn fn, tree type, tree arg)
1115 switch (fn)
1117 case CFN_BUILT_IN_STRLEN:
1118 if (const char *str = c_getstr (arg))
1119 return build_int_cst (type, strlen (str));
1120 return NULL_TREE;
1122 CASE_CFN_NAN:
1123 case CFN_BUILT_IN_NAND32:
1124 case CFN_BUILT_IN_NAND64:
1125 case CFN_BUILT_IN_NAND128:
1126 return fold_const_builtin_nan (type, arg, true);
1128 CASE_CFN_NANS:
1129 return fold_const_builtin_nan (type, arg, false);
1131 default:
1132 return fold_const_call_1 (fn, type, arg);
1136 /* Try to evaluate:
1138 *RESULT = FN (*ARG0, *ARG1)
1140 in format FORMAT. Return true on success. */
1142 static bool
1143 fold_const_call_sss (real_value *result, combined_fn fn,
1144 const real_value *arg0, const real_value *arg1,
1145 const real_format *format)
1147 switch (fn)
1149 CASE_CFN_DREM:
1150 CASE_CFN_REMAINDER:
1151 return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1153 CASE_CFN_ATAN2:
1154 return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1156 CASE_CFN_FDIM:
1157 return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1159 CASE_CFN_HYPOT:
1160 return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1162 CASE_CFN_COPYSIGN:
1163 *result = *arg0;
1164 real_copysign (result, arg1);
1165 return true;
1167 CASE_CFN_FMIN:
1168 return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1170 CASE_CFN_FMAX:
1171 return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1173 CASE_CFN_POW:
1174 return fold_const_pow (result, arg0, arg1, format);
1176 default:
1177 return false;
1181 /* Try to evaluate:
1183 *RESULT = FN (*ARG0, ARG1)
1185 where FORMAT is the format of *RESULT and *ARG0. Return true on
1186 success. */
1188 static bool
1189 fold_const_call_sss (real_value *result, combined_fn fn,
1190 const real_value *arg0, const wide_int_ref &arg1,
1191 const real_format *format)
1193 switch (fn)
1195 CASE_CFN_LDEXP:
1196 return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1198 CASE_CFN_SCALBN:
1199 CASE_CFN_SCALBLN:
1200 return (format->b == 2
1201 && fold_const_builtin_load_exponent (result, arg0, arg1,
1202 format));
1204 CASE_CFN_POWI:
1205 real_powi (result, format, arg0, arg1.to_shwi ());
1206 return true;
1208 default:
1209 return false;
1213 /* Try to evaluate:
1215 *RESULT = FN (ARG0, *ARG1)
1217 where FORMAT is the format of *RESULT and *ARG1. Return true on
1218 success. */
1220 static bool
1221 fold_const_call_sss (real_value *result, combined_fn fn,
1222 const wide_int_ref &arg0, const real_value *arg1,
1223 const real_format *format)
1225 switch (fn)
1227 CASE_CFN_JN:
1228 return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1230 CASE_CFN_YN:
1231 return (real_compare (GT_EXPR, arg1, &dconst0)
1232 && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1234 default:
1235 return false;
1239 /* Try to evaluate:
1241 RESULT = fn (ARG0, ARG1)
1243 where FORMAT is the format of the real and imaginary parts of RESULT
1244 (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1245 and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1247 static bool
1248 fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1249 combined_fn fn, const real_value *arg0_real,
1250 const real_value *arg0_imag, const real_value *arg1_real,
1251 const real_value *arg1_imag, const real_format *format)
1253 switch (fn)
1255 CASE_CFN_CPOW:
1256 return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1257 arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1259 default:
1260 return false;
1264 /* Subroutine of fold_const_call, with the same interface. Handle cases
1265 where the arguments and result are numerical. */
1267 static tree
1268 fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1270 machine_mode mode = TYPE_MODE (type);
1271 machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1272 machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1274 if (arg0_mode == arg1_mode
1275 && real_cst_p (arg0)
1276 && real_cst_p (arg1))
1278 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1279 if (mode == arg0_mode)
1281 /* real, real -> real. */
1282 REAL_VALUE_TYPE result;
1283 if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1284 TREE_REAL_CST_PTR (arg1),
1285 REAL_MODE_FORMAT (mode)))
1286 return build_real (type, result);
1288 return NULL_TREE;
1291 if (real_cst_p (arg0)
1292 && integer_cst_p (arg1))
1294 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1295 if (mode == arg0_mode)
1297 /* real, int -> real. */
1298 REAL_VALUE_TYPE result;
1299 if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1300 arg1, REAL_MODE_FORMAT (mode)))
1301 return build_real (type, result);
1303 return NULL_TREE;
1306 if (integer_cst_p (arg0)
1307 && real_cst_p (arg1))
1309 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1310 if (mode == arg1_mode)
1312 /* int, real -> real. */
1313 REAL_VALUE_TYPE result;
1314 if (fold_const_call_sss (&result, fn, arg0,
1315 TREE_REAL_CST_PTR (arg1),
1316 REAL_MODE_FORMAT (mode)))
1317 return build_real (type, result);
1319 return NULL_TREE;
1322 if (arg0_mode == arg1_mode
1323 && complex_cst_p (arg0)
1324 && complex_cst_p (arg1))
1326 gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1327 machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1328 tree arg0r = TREE_REALPART (arg0);
1329 tree arg0i = TREE_IMAGPART (arg0);
1330 tree arg1r = TREE_REALPART (arg1);
1331 tree arg1i = TREE_IMAGPART (arg1);
1332 if (mode == arg0_mode
1333 && real_cst_p (arg0r)
1334 && real_cst_p (arg0i)
1335 && real_cst_p (arg1r)
1336 && real_cst_p (arg1i))
1338 /* complex real, complex real -> complex real. */
1339 REAL_VALUE_TYPE result_real, result_imag;
1340 if (fold_const_call_ccc (&result_real, &result_imag, fn,
1341 TREE_REAL_CST_PTR (arg0r),
1342 TREE_REAL_CST_PTR (arg0i),
1343 TREE_REAL_CST_PTR (arg1r),
1344 TREE_REAL_CST_PTR (arg1i),
1345 REAL_MODE_FORMAT (inner_mode)))
1346 return build_complex (type,
1347 build_real (TREE_TYPE (type), result_real),
1348 build_real (TREE_TYPE (type), result_imag));
1350 return NULL_TREE;
1353 return NULL_TREE;
1356 /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1357 otherwise return null. TYPE is the type of the return value. */
1359 tree
1360 fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1362 const char *p0, *p1;
1363 switch (fn)
1365 case CFN_BUILT_IN_STRSPN:
1366 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1367 return build_int_cst (type, strspn (p0, p1));
1368 return NULL_TREE;
1370 case CFN_BUILT_IN_STRCSPN:
1371 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1372 return build_int_cst (type, strcspn (p0, p1));
1373 return NULL_TREE;
1375 case CFN_BUILT_IN_STRCMP:
1376 if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1377 return build_cmp_result (type, strcmp (p0, p1));
1378 return NULL_TREE;
1380 default:
1381 return fold_const_call_1 (fn, type, arg0, arg1);
1385 /* Try to evaluate:
1387 *RESULT = FN (*ARG0, *ARG1, *ARG2)
1389 in format FORMAT. Return true on success. */
1391 static bool
1392 fold_const_call_ssss (real_value *result, combined_fn fn,
1393 const real_value *arg0, const real_value *arg1,
1394 const real_value *arg2, const real_format *format)
1396 switch (fn)
1398 CASE_CFN_FMA:
1399 return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
1401 default:
1402 return false;
1406 /* Subroutine of fold_const_call, with the same interface. Handle cases
1407 where the arguments and result are numerical. */
1409 static tree
1410 fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1412 machine_mode mode = TYPE_MODE (type);
1413 machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1414 machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1415 machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
1417 if (arg0_mode == arg1_mode
1418 && arg0_mode == arg2_mode
1419 && real_cst_p (arg0)
1420 && real_cst_p (arg1)
1421 && real_cst_p (arg2))
1423 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1424 if (mode == arg0_mode)
1426 /* real, real, real -> real. */
1427 REAL_VALUE_TYPE result;
1428 if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
1429 TREE_REAL_CST_PTR (arg1),
1430 TREE_REAL_CST_PTR (arg2),
1431 REAL_MODE_FORMAT (mode)))
1432 return build_real (type, result);
1434 return NULL_TREE;
1437 return NULL_TREE;
1440 /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
1441 success, otherwise return null. TYPE is the type of the return value. */
1443 tree
1444 fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1446 const char *p0, *p1;
1447 size_t s2;
1448 switch (fn)
1450 case CFN_BUILT_IN_STRNCMP:
1451 if ((p0 = c_getstr (arg0))
1452 && (p1 = c_getstr (arg1))
1453 && host_size_t_cst_p (arg2, &s2))
1454 return build_int_cst (type, strncmp (p0, p1, s2));
1455 return NULL_TREE;
1457 case CFN_BUILT_IN_BCMP:
1458 case CFN_BUILT_IN_MEMCMP:
1459 if ((p0 = c_getstr (arg0))
1460 && (p1 = c_getstr (arg1))
1461 && host_size_t_cst_p (arg2, &s2)
1462 && s2 <= strlen (p0)
1463 && s2 <= strlen (p1))
1464 return build_cmp_result (type, memcmp (p0, p1, s2));
1465 return NULL_TREE;
1467 default:
1468 return fold_const_call_1 (fn, type, arg0, arg1, arg2);
1472 /* Fold a fma operation with arguments ARG[012]. */
1474 tree
1475 fold_fma (location_t, tree type, tree arg0, tree arg1, tree arg2)
1477 REAL_VALUE_TYPE result;
1478 if (real_cst_p (arg0)
1479 && real_cst_p (arg1)
1480 && real_cst_p (arg2)
1481 && do_mpfr_arg3 (&result, mpfr_fma, TREE_REAL_CST_PTR (arg0),
1482 TREE_REAL_CST_PTR (arg1), TREE_REAL_CST_PTR (arg2),
1483 REAL_MODE_FORMAT (TYPE_MODE (type))))
1484 return build_real (type, result);
1486 return NULL_TREE;