1 /* Constant folding for calls to built-in and internal functions.
2 Copyright (C) 1988-2019 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
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
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/>. */
22 #include "coretypes.h"
25 #include "stor-layout.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 #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. */
39 integer_cst_p (tree t
)
41 return TREE_CODE (t
) == INTEGER_CST
&& !TREE_OVERFLOW (t
);
47 return TREE_CODE (t
) == REAL_CST
&& !TREE_OVERFLOW (t
);
51 complex_cst_p (tree t
)
53 return TREE_CODE (t
) == COMPLEX_CST
;
56 /* Return true if ARG is a constant in the range of the host size_t.
57 Store it in *SIZE_OUT if so. */
60 host_size_t_cst_p (tree t
, size_t *size_out
)
62 if (types_compatible_p (size_type_node
, TREE_TYPE (t
))
64 && (wi::min_precision (wi::to_wide (t
), UNSIGNED
)
65 <= sizeof (size_t) * CHAR_BIT
))
67 *size_out
= tree_to_uhwi (t
);
73 /* RES is the result of a comparison in which < 0 means "less", 0 means
74 "equal" and > 0 means "more". Canonicalize it to -1, 0 or 1 and
75 return it in type TYPE. */
78 build_cmp_result (tree type
, int res
)
80 return build_int_cst (type
, res
< 0 ? -1 : res
> 0 ? 1 : 0);
83 /* M is the result of trying to constant-fold an expression (starting
84 with clear MPFR flags) and INEXACT says whether the result in M is
85 exact or inexact. Return true if M can be used as a constant-folded
86 result in format FORMAT, storing the value in *RESULT if so. */
89 do_mpfr_ckconv (real_value
*result
, mpfr_srcptr m
, bool inexact
,
90 const real_format
*format
)
92 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
93 overflow/underflow occurred. If -frounding-math, proceed iff the
94 result of calling FUNC was exact. */
95 if (!mpfr_number_p (m
)
97 || mpfr_underflow_p ()
98 || (flag_rounding_math
&& inexact
))
102 real_from_mpfr (&tmp
, m
, format
, GMP_RNDN
);
104 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
105 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
106 underflowed in the conversion. */
107 if (!real_isfinite (&tmp
)
108 || ((tmp
.cl
== rvc_zero
) != (mpfr_zero_p (m
) != 0)))
111 real_convert (result
, format
, &tmp
);
112 return real_identical (result
, &tmp
);
119 in format FORMAT, given that FUNC is the MPFR implementation of f.
120 Return true on success. */
123 do_mpfr_arg1 (real_value
*result
,
124 int (*func
) (mpfr_ptr
, mpfr_srcptr
, mpfr_rnd_t
),
125 const real_value
*arg
, const real_format
*format
)
127 /* To proceed, MPFR must exactly represent the target floating point
128 format, which only happens when the target base equals two. */
129 if (format
->b
!= 2 || !real_isfinite (arg
))
132 int prec
= format
->p
;
133 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
136 mpfr_init2 (m
, prec
);
137 mpfr_from_real (m
, arg
, GMP_RNDN
);
139 bool inexact
= func (m
, m
, rnd
);
140 bool ok
= do_mpfr_ckconv (result
, m
, inexact
, format
);
148 *RESULT_SIN = sin (*ARG);
149 *RESULT_COS = cos (*ARG);
151 for format FORMAT. Return true on success. */
154 do_mpfr_sincos (real_value
*result_sin
, real_value
*result_cos
,
155 const real_value
*arg
, const real_format
*format
)
157 /* To proceed, MPFR must exactly represent the target floating point
158 format, which only happens when the target base equals two. */
159 if (format
->b
!= 2 || !real_isfinite (arg
))
162 int prec
= format
->p
;
163 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
166 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
167 mpfr_from_real (m
, arg
, GMP_RNDN
);
169 bool inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
170 bool ok
= (do_mpfr_ckconv (result_sin
, ms
, inexact
, format
)
171 && do_mpfr_ckconv (result_cos
, mc
, inexact
, format
));
172 mpfr_clears (m
, ms
, mc
, NULL
);
179 *RESULT = f (*ARG0, *ARG1)
181 in format FORMAT, given that FUNC is the MPFR implementation of f.
182 Return true on success. */
185 do_mpfr_arg2 (real_value
*result
,
186 int (*func
) (mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_rnd_t
),
187 const real_value
*arg0
, const real_value
*arg1
,
188 const real_format
*format
)
190 /* To proceed, MPFR must exactly represent the target floating point
191 format, which only happens when the target base equals two. */
192 if (format
->b
!= 2 || !real_isfinite (arg0
) || !real_isfinite (arg1
))
195 int prec
= format
->p
;
196 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
199 mpfr_inits2 (prec
, m0
, m1
, NULL
);
200 mpfr_from_real (m0
, arg0
, GMP_RNDN
);
201 mpfr_from_real (m1
, arg1
, GMP_RNDN
);
203 bool inexact
= func (m0
, m0
, m1
, rnd
);
204 bool ok
= do_mpfr_ckconv (result
, m0
, inexact
, format
);
205 mpfr_clears (m0
, m1
, NULL
);
212 *RESULT = f (ARG0, *ARG1)
214 in format FORMAT, given that FUNC is the MPFR implementation of f.
215 Return true on success. */
218 do_mpfr_arg2 (real_value
*result
,
219 int (*func
) (mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
220 const wide_int_ref
&arg0
, const real_value
*arg1
,
221 const real_format
*format
)
223 if (format
->b
!= 2 || !real_isfinite (arg1
))
226 int prec
= format
->p
;
227 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
230 mpfr_init2 (m
, prec
);
231 mpfr_from_real (m
, arg1
, GMP_RNDN
);
233 bool inexact
= func (m
, arg0
.to_shwi (), m
, rnd
);
234 bool ok
= do_mpfr_ckconv (result
, m
, inexact
, format
);
242 *RESULT = f (*ARG0, *ARG1, *ARG2)
244 in format FORMAT, given that FUNC is the MPFR implementation of f.
245 Return true on success. */
248 do_mpfr_arg3 (real_value
*result
,
249 int (*func
) (mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
,
250 mpfr_srcptr
, mpfr_rnd_t
),
251 const real_value
*arg0
, const real_value
*arg1
,
252 const real_value
*arg2
, const real_format
*format
)
254 /* To proceed, MPFR must exactly represent the target floating point
255 format, which only happens when the target base equals two. */
257 || !real_isfinite (arg0
)
258 || !real_isfinite (arg1
)
259 || !real_isfinite (arg2
))
262 int prec
= format
->p
;
263 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
266 mpfr_inits2 (prec
, m0
, m1
, m2
, NULL
);
267 mpfr_from_real (m0
, arg0
, GMP_RNDN
);
268 mpfr_from_real (m1
, arg1
, GMP_RNDN
);
269 mpfr_from_real (m2
, arg2
, GMP_RNDN
);
271 bool inexact
= func (m0
, m0
, m1
, m2
, rnd
);
272 bool ok
= do_mpfr_ckconv (result
, m0
, inexact
, format
);
273 mpfr_clears (m0
, m1
, m2
, NULL
);
278 /* M is the result of trying to constant-fold an expression (starting
279 with clear MPFR flags) and INEXACT says whether the result in M is
280 exact or inexact. Return true if M can be used as a constant-folded
281 result in which the real and imaginary parts have format FORMAT.
282 Store those parts in *RESULT_REAL and *RESULT_IMAG if so. */
285 do_mpc_ckconv (real_value
*result_real
, real_value
*result_imag
,
286 mpc_srcptr m
, bool inexact
, const real_format
*format
)
288 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
289 overflow/underflow occurred. If -frounding-math, proceed iff the
290 result of calling FUNC was exact. */
291 if (!mpfr_number_p (mpc_realref (m
))
292 || !mpfr_number_p (mpc_imagref (m
))
293 || mpfr_overflow_p ()
294 || mpfr_underflow_p ()
295 || (flag_rounding_math
&& inexact
))
298 REAL_VALUE_TYPE tmp_real
, tmp_imag
;
299 real_from_mpfr (&tmp_real
, mpc_realref (m
), format
, GMP_RNDN
);
300 real_from_mpfr (&tmp_imag
, mpc_imagref (m
), format
, GMP_RNDN
);
302 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
303 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
304 underflowed in the conversion. */
305 if (!real_isfinite (&tmp_real
)
306 || !real_isfinite (&tmp_imag
)
307 || (tmp_real
.cl
== rvc_zero
) != (mpfr_zero_p (mpc_realref (m
)) != 0)
308 || (tmp_imag
.cl
== rvc_zero
) != (mpfr_zero_p (mpc_imagref (m
)) != 0))
311 real_convert (result_real
, format
, &tmp_real
);
312 real_convert (result_imag
, format
, &tmp_imag
);
314 return (real_identical (result_real
, &tmp_real
)
315 && real_identical (result_imag
, &tmp_imag
));
322 in format FORMAT, given that FUNC is the mpc implementation of f.
323 Return true on success. Both RESULT and ARG are represented as
324 real and imaginary pairs. */
327 do_mpc_arg1 (real_value
*result_real
, real_value
*result_imag
,
328 int (*func
) (mpc_ptr
, mpc_srcptr
, mpc_rnd_t
),
329 const real_value
*arg_real
, const real_value
*arg_imag
,
330 const real_format
*format
)
332 /* To proceed, MPFR must exactly represent the target floating point
333 format, which only happens when the target base equals two. */
335 || !real_isfinite (arg_real
)
336 || !real_isfinite (arg_imag
))
339 int prec
= format
->p
;
340 mpc_rnd_t crnd
= format
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
344 mpfr_from_real (mpc_realref (m
), arg_real
, GMP_RNDN
);
345 mpfr_from_real (mpc_imagref (m
), arg_imag
, GMP_RNDN
);
347 bool inexact
= func (m
, m
, crnd
);
348 bool ok
= do_mpc_ckconv (result_real
, result_imag
, m
, inexact
, format
);
356 RESULT = f (ARG0, ARG1)
358 in format FORMAT, given that FUNC is the mpc implementation of f.
359 Return true on success. RESULT, ARG0 and ARG1 are represented as
360 real and imaginary pairs. */
363 do_mpc_arg2 (real_value
*result_real
, real_value
*result_imag
,
364 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
),
365 const real_value
*arg0_real
, const real_value
*arg0_imag
,
366 const real_value
*arg1_real
, const real_value
*arg1_imag
,
367 const real_format
*format
)
369 if (!real_isfinite (arg0_real
)
370 || !real_isfinite (arg0_imag
)
371 || !real_isfinite (arg1_real
)
372 || !real_isfinite (arg1_imag
))
375 int prec
= format
->p
;
376 mpc_rnd_t crnd
= format
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
379 mpc_init2 (m0
, prec
);
380 mpc_init2 (m1
, prec
);
381 mpfr_from_real (mpc_realref (m0
), arg0_real
, GMP_RNDN
);
382 mpfr_from_real (mpc_imagref (m0
), arg0_imag
, GMP_RNDN
);
383 mpfr_from_real (mpc_realref (m1
), arg1_real
, GMP_RNDN
);
384 mpfr_from_real (mpc_imagref (m1
), arg1_imag
, GMP_RNDN
);
386 bool inexact
= func (m0
, m0
, m1
, crnd
);
387 bool ok
= do_mpc_ckconv (result_real
, result_imag
, m0
, inexact
, format
);
396 *RESULT = logb (*ARG)
398 in format FORMAT. Return true on success. */
401 fold_const_logb (real_value
*result
, const real_value
*arg
,
402 const real_format
*format
)
407 /* If arg is +-NaN, then return it. */
412 /* If arg is +-Inf, then return +Inf. */
418 /* Zero may set errno and/or raise an exception. */
422 /* For normal numbers, proceed iff radix == 2. In GCC,
423 normalized significands are in the range [0.5, 1.0). We
424 want the exponent as if they were [1.0, 2.0) so get the
425 exponent and subtract 1. */
428 real_from_integer (result
, format
, REAL_EXP (arg
) - 1, SIGNED
);
438 *RESULT = significand (*ARG)
440 in format FORMAT. Return true on success. */
443 fold_const_significand (real_value
*result
, const real_value
*arg
,
444 const real_format
*format
)
451 /* If arg is +-0, +-Inf or +-NaN, then return it. */
456 /* For normal numbers, proceed iff radix == 2. */
460 /* In GCC, normalized significands are in the range [0.5, 1.0).
461 We want them to be [1.0, 2.0) so set the exponent to 1. */
462 SET_REAL_EXP (result
, 1);
474 where FORMAT is the format of *ARG and PRECISION is the number of
475 significant bits in the result. Return true on success. */
478 fold_const_conversion (wide_int
*result
,
479 void (*fn
) (real_value
*, format_helper
,
481 const real_value
*arg
, unsigned int precision
,
482 const real_format
*format
)
484 if (!real_isfinite (arg
))
488 fn (&rounded
, format
, arg
);
491 *result
= real_to_integer (&rounded
, &fail
, precision
);
497 *RESULT = pow (*ARG0, *ARG1)
499 in format FORMAT. Return true on success. */
502 fold_const_pow (real_value
*result
, const real_value
*arg0
,
503 const real_value
*arg1
, const real_format
*format
)
505 if (do_mpfr_arg2 (result
, mpfr_pow
, arg0
, arg1
, format
))
508 /* Check for an integer exponent. */
509 REAL_VALUE_TYPE cint1
;
510 HOST_WIDE_INT n1
= real_to_integer (arg1
);
511 real_from_integer (&cint1
, VOIDmode
, n1
, SIGNED
);
512 /* Attempt to evaluate pow at compile-time, unless this should
513 raise an exception. */
514 if (real_identical (arg1
, &cint1
)
516 || (!flag_trapping_math
&& !flag_errno_math
)
517 || !real_equal (arg0
, &dconst0
)))
519 bool inexact
= real_powi (result
, format
, arg0
, n1
);
520 /* Avoid the folding if flag_signaling_nans is on. */
521 if (flag_unsafe_math_optimizations
523 && !(flag_signaling_nans
524 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))))
533 *RESULT = nextafter (*ARG0, *ARG1)
537 *RESULT = nexttoward (*ARG0, *ARG1)
539 in format FORMAT. Return true on success. */
542 fold_const_nextafter (real_value
*result
, const real_value
*arg0
,
543 const real_value
*arg1
, const real_format
*format
)
545 if (REAL_VALUE_ISSIGNALING_NAN (*arg0
)
546 || REAL_VALUE_ISSIGNALING_NAN (*arg1
))
549 /* Don't handle composite modes, nor decimal, nor modes without
550 inf or denorm at least for now. */
551 if (format
->pnan
< format
->p
554 || !format
->has_denorm
)
557 if (real_nextafter (result
, format
, arg0
, arg1
)
558 /* If raising underflow or overflow and setting errno to ERANGE,
559 fail if we care about those side-effects. */
560 && (flag_trapping_math
|| flag_errno_math
))
562 /* Similarly for nextafter (0, 1) raising underflow. */
563 else if (flag_trapping_math
564 && arg0
->cl
== rvc_zero
565 && result
->cl
!= rvc_zero
)
568 real_convert (result
, format
, result
);
575 *RESULT = ldexp (*ARG0, ARG1)
577 in format FORMAT. Return true on success. */
580 fold_const_builtin_load_exponent (real_value
*result
, const real_value
*arg0
,
581 const wide_int_ref
&arg1
,
582 const real_format
*format
)
584 /* Bound the maximum adjustment to twice the range of the
585 mode's valid exponents. Use abs to ensure the range is
586 positive as a sanity check. */
587 int max_exp_adj
= 2 * labs (format
->emax
- format
->emin
);
589 /* The requested adjustment must be inside this range. This
590 is a preliminary cap to avoid things like overflow, we
591 may still fail to compute the result for other reasons. */
592 if (wi::les_p (arg1
, -max_exp_adj
) || wi::ges_p (arg1
, max_exp_adj
))
595 /* Don't perform operation if we honor signaling NaNs and
596 operand is a signaling NaN. */
597 if (!flag_unsafe_math_optimizations
598 && flag_signaling_nans
599 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))
602 REAL_VALUE_TYPE initial_result
;
603 real_ldexp (&initial_result
, arg0
, arg1
.to_shwi ());
605 /* Ensure we didn't overflow. */
606 if (real_isinf (&initial_result
))
609 /* Only proceed if the target mode can hold the
611 *result
= real_value_truncate (format
, initial_result
);
612 return real_equal (&initial_result
, result
);
615 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
616 return type TYPE. QUIET is true if a quiet rather than signalling
620 fold_const_builtin_nan (tree type
, tree arg
, bool quiet
)
622 REAL_VALUE_TYPE real
;
623 const char *str
= c_getstr (arg
);
624 if (str
&& real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
625 return build_real (type
, real
);
629 /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE. */
632 fold_const_reduction (tree type
, tree arg
, tree_code code
)
634 unsigned HOST_WIDE_INT nelts
;
635 if (TREE_CODE (arg
) != VECTOR_CST
636 || !VECTOR_CST_NELTS (arg
).is_constant (&nelts
))
639 tree res
= VECTOR_CST_ELT (arg
, 0);
640 for (unsigned HOST_WIDE_INT i
= 1; i
< nelts
; i
++)
642 res
= const_binop (code
, type
, res
, VECTOR_CST_ELT (arg
, i
));
643 if (res
== NULL_TREE
|| !CONSTANT_CLASS_P (res
))
649 /* Fold a call to IFN_VEC_CONVERT (ARG) returning TYPE. */
652 fold_const_vec_convert (tree ret_type
, tree arg
)
654 enum tree_code code
= NOP_EXPR
;
655 tree arg_type
= TREE_TYPE (arg
);
656 if (TREE_CODE (arg
) != VECTOR_CST
)
659 gcc_checking_assert (VECTOR_TYPE_P (ret_type
) && VECTOR_TYPE_P (arg_type
));
661 if (INTEGRAL_TYPE_P (TREE_TYPE (ret_type
))
662 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg_type
)))
663 code
= FIX_TRUNC_EXPR
;
664 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg_type
))
665 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (ret_type
)))
668 /* We can't handle steps directly when extending, since the
669 values need to wrap at the original precision first. */
671 = (INTEGRAL_TYPE_P (TREE_TYPE (ret_type
))
672 && INTEGRAL_TYPE_P (TREE_TYPE (arg_type
))
673 && (TYPE_PRECISION (TREE_TYPE (ret_type
))
674 <= TYPE_PRECISION (TREE_TYPE (arg_type
))));
675 tree_vector_builder elts
;
676 if (!elts
.new_unary_operation (ret_type
, arg
, step_ok_p
))
679 unsigned int count
= elts
.encoded_nelts ();
680 for (unsigned int i
= 0; i
< count
; ++i
)
682 tree elt
= fold_unary (code
, TREE_TYPE (ret_type
),
683 VECTOR_CST_ELT (arg
, i
));
684 if (elt
== NULL_TREE
|| !CONSTANT_CLASS_P (elt
))
686 elts
.quick_push (elt
);
689 return elts
.build ();
696 in format FORMAT. Return true on success. */
699 fold_const_call_ss (real_value
*result
, combined_fn fn
,
700 const real_value
*arg
, const real_format
*format
)
706 return (real_compare (GE_EXPR
, arg
, &dconst0
)
707 && do_mpfr_arg1 (result
, mpfr_sqrt
, arg
, format
));
710 return do_mpfr_arg1 (result
, mpfr_cbrt
, arg
, format
);
713 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
714 && real_compare (LE_EXPR
, arg
, &dconst1
)
715 && do_mpfr_arg1 (result
, mpfr_asin
, arg
, format
));
718 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
719 && real_compare (LE_EXPR
, arg
, &dconst1
)
720 && do_mpfr_arg1 (result
, mpfr_acos
, arg
, format
));
723 return do_mpfr_arg1 (result
, mpfr_atan
, arg
, format
);
726 return do_mpfr_arg1 (result
, mpfr_asinh
, arg
, format
);
729 return (real_compare (GE_EXPR
, arg
, &dconst1
)
730 && do_mpfr_arg1 (result
, mpfr_acosh
, arg
, format
));
733 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
734 && real_compare (LE_EXPR
, arg
, &dconst1
)
735 && do_mpfr_arg1 (result
, mpfr_atanh
, arg
, format
));
738 return do_mpfr_arg1 (result
, mpfr_sin
, arg
, format
);
741 return do_mpfr_arg1 (result
, mpfr_cos
, arg
, format
);
744 return do_mpfr_arg1 (result
, mpfr_tan
, arg
, format
);
747 return do_mpfr_arg1 (result
, mpfr_sinh
, arg
, format
);
750 return do_mpfr_arg1 (result
, mpfr_cosh
, arg
, format
);
753 return do_mpfr_arg1 (result
, mpfr_tanh
, arg
, format
);
756 return do_mpfr_arg1 (result
, mpfr_erf
, arg
, format
);
759 return do_mpfr_arg1 (result
, mpfr_erfc
, arg
, format
);
762 return do_mpfr_arg1 (result
, mpfr_gamma
, arg
, format
);
765 return do_mpfr_arg1 (result
, mpfr_exp
, arg
, format
);
768 return do_mpfr_arg1 (result
, mpfr_exp2
, arg
, format
);
772 return do_mpfr_arg1 (result
, mpfr_exp10
, arg
, format
);
775 return do_mpfr_arg1 (result
, mpfr_expm1
, arg
, format
);
778 return (real_compare (GT_EXPR
, arg
, &dconst0
)
779 && do_mpfr_arg1 (result
, mpfr_log
, arg
, format
));
782 return (real_compare (GT_EXPR
, arg
, &dconst0
)
783 && do_mpfr_arg1 (result
, mpfr_log2
, arg
, format
));
786 return (real_compare (GT_EXPR
, arg
, &dconst0
)
787 && do_mpfr_arg1 (result
, mpfr_log10
, arg
, format
));
790 return (real_compare (GT_EXPR
, arg
, &dconstm1
)
791 && do_mpfr_arg1 (result
, mpfr_log1p
, arg
, format
));
794 return do_mpfr_arg1 (result
, mpfr_j0
, arg
, format
);
797 return do_mpfr_arg1 (result
, mpfr_j1
, arg
, format
);
800 return (real_compare (GT_EXPR
, arg
, &dconst0
)
801 && do_mpfr_arg1 (result
, mpfr_y0
, arg
, format
));
804 return (real_compare (GT_EXPR
, arg
, &dconst0
)
805 && do_mpfr_arg1 (result
, mpfr_y1
, arg
, format
));
809 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
811 real_floor (result
, format
, arg
);
818 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
820 real_ceil (result
, format
, arg
);
827 real_trunc (result
, format
, arg
);
832 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
834 real_round (result
, format
, arg
);
840 return fold_const_logb (result
, arg
, format
);
842 CASE_CFN_SIGNIFICAND
:
843 return fold_const_significand (result
, arg
, format
);
854 where FORMAT is the format of ARG and PRECISION is the number of
855 significant bits in the result. Return true on success. */
858 fold_const_call_ss (wide_int
*result
, combined_fn fn
,
859 const real_value
*arg
, unsigned int precision
,
860 const real_format
*format
)
865 if (real_isneg (arg
))
866 *result
= wi::one (precision
);
868 *result
= wi::zero (precision
);
872 /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
873 Proceed iff radix == 2. In GCC, normalized significands are in
874 the range [0.5, 1.0). We want the exponent as if they were
875 [1.0, 2.0) so get the exponent and subtract 1. */
876 if (arg
->cl
== rvc_normal
&& format
->b
== 2)
878 *result
= wi::shwi (REAL_EXP (arg
) - 1, precision
);
886 return fold_const_conversion (result
, real_ceil
, arg
,
892 return fold_const_conversion (result
, real_floor
, arg
,
898 return fold_const_conversion (result
, real_round
, arg
,
904 /* Not yet folded to a constant. */
908 case CFN_BUILT_IN_FINITED32
:
909 case CFN_BUILT_IN_FINITED64
:
910 case CFN_BUILT_IN_FINITED128
:
911 case CFN_BUILT_IN_ISFINITE
:
912 *result
= wi::shwi (real_isfinite (arg
) ? 1 : 0, precision
);
916 case CFN_BUILT_IN_ISINFD32
:
917 case CFN_BUILT_IN_ISINFD64
:
918 case CFN_BUILT_IN_ISINFD128
:
919 if (real_isinf (arg
))
920 *result
= wi::shwi (arg
->sign
? -1 : 1, precision
);
922 *result
= wi::shwi (0, precision
);
926 case CFN_BUILT_IN_ISNAND32
:
927 case CFN_BUILT_IN_ISNAND64
:
928 case CFN_BUILT_IN_ISNAND128
:
929 *result
= wi::shwi (real_isnan (arg
) ? 1 : 0, precision
);
941 where ARG_TYPE is the type of ARG and PRECISION is the number of bits
942 in the result. Return true on success. */
945 fold_const_call_ss (wide_int
*result
, combined_fn fn
, const wide_int_ref
&arg
,
946 unsigned int precision
, tree arg_type
)
951 *result
= wi::shwi (wi::ffs (arg
), precision
);
957 if (wi::ne_p (arg
, 0))
959 else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type
),
961 tmp
= TYPE_PRECISION (arg_type
);
962 *result
= wi::shwi (tmp
, precision
);
969 if (wi::ne_p (arg
, 0))
971 else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type
),
973 tmp
= TYPE_PRECISION (arg_type
);
974 *result
= wi::shwi (tmp
, precision
);
979 *result
= wi::shwi (wi::clrsb (arg
), precision
);
983 *result
= wi::shwi (wi::popcount (arg
), precision
);
987 *result
= wi::shwi (wi::parity (arg
), precision
);
990 case CFN_BUILT_IN_BSWAP16
:
991 case CFN_BUILT_IN_BSWAP32
:
992 case CFN_BUILT_IN_BSWAP64
:
993 *result
= wide_int::from (arg
, precision
, TYPE_SIGN (arg_type
)).bswap ();
1005 where FORMAT is the format of ARG and of the real and imaginary parts
1006 of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
1010 fold_const_call_cs (real_value
*result_real
, real_value
*result_imag
,
1011 combined_fn fn
, const real_value
*arg
,
1012 const real_format
*format
)
1017 /* cexpi(x+yi) = cos(x)+sin(y)*i. */
1018 return do_mpfr_sincos (result_imag
, result_real
, arg
, format
);
1029 where FORMAT is the format of RESULT and of the real and imaginary parts
1030 of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
1034 fold_const_call_sc (real_value
*result
, combined_fn fn
,
1035 const real_value
*arg_real
, const real_value
*arg_imag
,
1036 const real_format
*format
)
1041 return do_mpfr_arg2 (result
, mpfr_hypot
, arg_real
, arg_imag
, format
);
1052 where FORMAT is the format of the real and imaginary parts of RESULT
1053 (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
1054 Return true on success. */
1057 fold_const_call_cc (real_value
*result_real
, real_value
*result_imag
,
1058 combined_fn fn
, const real_value
*arg_real
,
1059 const real_value
*arg_imag
, const real_format
*format
)
1064 return do_mpc_arg1 (result_real
, result_imag
, mpc_cos
,
1065 arg_real
, arg_imag
, format
);
1068 return do_mpc_arg1 (result_real
, result_imag
, mpc_cosh
,
1069 arg_real
, arg_imag
, format
);
1072 if (real_isinf (arg_real
) || real_isinf (arg_imag
))
1074 real_inf (result_real
);
1075 *result_imag
= dconst0
;
1076 result_imag
->sign
= arg_imag
->sign
;
1080 *result_real
= *arg_real
;
1081 *result_imag
= *arg_imag
;
1086 return do_mpc_arg1 (result_real
, result_imag
, mpc_sin
,
1087 arg_real
, arg_imag
, format
);
1090 return do_mpc_arg1 (result_real
, result_imag
, mpc_sinh
,
1091 arg_real
, arg_imag
, format
);
1094 return do_mpc_arg1 (result_real
, result_imag
, mpc_tan
,
1095 arg_real
, arg_imag
, format
);
1098 return do_mpc_arg1 (result_real
, result_imag
, mpc_tanh
,
1099 arg_real
, arg_imag
, format
);
1102 return do_mpc_arg1 (result_real
, result_imag
, mpc_log
,
1103 arg_real
, arg_imag
, format
);
1106 return do_mpc_arg1 (result_real
, result_imag
, mpc_sqrt
,
1107 arg_real
, arg_imag
, format
);
1110 return do_mpc_arg1 (result_real
, result_imag
, mpc_asin
,
1111 arg_real
, arg_imag
, format
);
1114 return do_mpc_arg1 (result_real
, result_imag
, mpc_acos
,
1115 arg_real
, arg_imag
, format
);
1118 return do_mpc_arg1 (result_real
, result_imag
, mpc_atan
,
1119 arg_real
, arg_imag
, format
);
1122 return do_mpc_arg1 (result_real
, result_imag
, mpc_asinh
,
1123 arg_real
, arg_imag
, format
);
1126 return do_mpc_arg1 (result_real
, result_imag
, mpc_acosh
,
1127 arg_real
, arg_imag
, format
);
1130 return do_mpc_arg1 (result_real
, result_imag
, mpc_atanh
,
1131 arg_real
, arg_imag
, format
);
1134 return do_mpc_arg1 (result_real
, result_imag
, mpc_exp
,
1135 arg_real
, arg_imag
, format
);
1142 /* Subroutine of fold_const_call, with the same interface. Handle cases
1143 where the arguments and result are numerical. */
1146 fold_const_call_1 (combined_fn fn
, tree type
, tree arg
)
1148 machine_mode mode
= TYPE_MODE (type
);
1149 machine_mode arg_mode
= TYPE_MODE (TREE_TYPE (arg
));
1151 if (integer_cst_p (arg
))
1153 if (SCALAR_INT_MODE_P (mode
))
1156 if (fold_const_call_ss (&result
, fn
, wi::to_wide (arg
),
1157 TYPE_PRECISION (type
), TREE_TYPE (arg
)))
1158 return wide_int_to_tree (type
, result
);
1163 if (real_cst_p (arg
))
1165 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode
));
1166 if (mode
== arg_mode
)
1169 REAL_VALUE_TYPE result
;
1170 if (fold_const_call_ss (&result
, fn
, TREE_REAL_CST_PTR (arg
),
1171 REAL_MODE_FORMAT (mode
)))
1172 return build_real (type
, result
);
1174 else if (COMPLEX_MODE_P (mode
)
1175 && GET_MODE_INNER (mode
) == arg_mode
)
1177 /* real -> complex real. */
1178 REAL_VALUE_TYPE result_real
, result_imag
;
1179 if (fold_const_call_cs (&result_real
, &result_imag
, fn
,
1180 TREE_REAL_CST_PTR (arg
),
1181 REAL_MODE_FORMAT (arg_mode
)))
1182 return build_complex (type
,
1183 build_real (TREE_TYPE (type
), result_real
),
1184 build_real (TREE_TYPE (type
), result_imag
));
1186 else if (INTEGRAL_TYPE_P (type
))
1190 if (fold_const_call_ss (&result
, fn
,
1191 TREE_REAL_CST_PTR (arg
),
1192 TYPE_PRECISION (type
),
1193 REAL_MODE_FORMAT (arg_mode
)))
1194 return wide_int_to_tree (type
, result
);
1199 if (complex_cst_p (arg
))
1201 gcc_checking_assert (COMPLEX_MODE_P (arg_mode
));
1202 machine_mode inner_mode
= GET_MODE_INNER (arg_mode
);
1203 tree argr
= TREE_REALPART (arg
);
1204 tree argi
= TREE_IMAGPART (arg
);
1205 if (mode
== arg_mode
1206 && real_cst_p (argr
)
1207 && real_cst_p (argi
))
1209 /* complex real -> complex real. */
1210 REAL_VALUE_TYPE result_real
, result_imag
;
1211 if (fold_const_call_cc (&result_real
, &result_imag
, fn
,
1212 TREE_REAL_CST_PTR (argr
),
1213 TREE_REAL_CST_PTR (argi
),
1214 REAL_MODE_FORMAT (inner_mode
)))
1215 return build_complex (type
,
1216 build_real (TREE_TYPE (type
), result_real
),
1217 build_real (TREE_TYPE (type
), result_imag
));
1219 if (mode
== inner_mode
1220 && real_cst_p (argr
)
1221 && real_cst_p (argi
))
1223 /* complex real -> real. */
1224 REAL_VALUE_TYPE result
;
1225 if (fold_const_call_sc (&result
, fn
,
1226 TREE_REAL_CST_PTR (argr
),
1227 TREE_REAL_CST_PTR (argi
),
1228 REAL_MODE_FORMAT (inner_mode
)))
1229 return build_real (type
, result
);
1237 /* Try to fold FN (ARG) to a constant. Return the constant on success,
1238 otherwise return null. TYPE is the type of the return value. */
1241 fold_const_call (combined_fn fn
, tree type
, tree arg
)
1245 case CFN_BUILT_IN_STRLEN
:
1246 if (const char *str
= c_getstr (arg
))
1247 return build_int_cst (type
, strlen (str
));
1251 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN
):
1252 case CFN_BUILT_IN_NAND32
:
1253 case CFN_BUILT_IN_NAND64
:
1254 case CFN_BUILT_IN_NAND128
:
1255 return fold_const_builtin_nan (type
, arg
, true);
1258 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS
):
1259 return fold_const_builtin_nan (type
, arg
, false);
1261 case CFN_REDUC_PLUS
:
1262 return fold_const_reduction (type
, arg
, PLUS_EXPR
);
1265 return fold_const_reduction (type
, arg
, MAX_EXPR
);
1268 return fold_const_reduction (type
, arg
, MIN_EXPR
);
1271 return fold_const_reduction (type
, arg
, BIT_AND_EXPR
);
1274 return fold_const_reduction (type
, arg
, BIT_IOR_EXPR
);
1277 return fold_const_reduction (type
, arg
, BIT_XOR_EXPR
);
1279 case CFN_VEC_CONVERT
:
1280 return fold_const_vec_convert (type
, arg
);
1283 return fold_const_call_1 (fn
, type
, arg
);
1287 /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1291 fold_const_fold_left (tree type
, tree arg0
, tree arg1
, tree_code code
)
1293 if (TREE_CODE (arg1
) != VECTOR_CST
)
1296 unsigned HOST_WIDE_INT nelts
;
1297 if (!VECTOR_CST_NELTS (arg1
).is_constant (&nelts
))
1300 for (unsigned HOST_WIDE_INT i
= 0; i
< nelts
; i
++)
1302 arg0
= const_binop (code
, type
, arg0
, VECTOR_CST_ELT (arg1
, i
));
1303 if (arg0
== NULL_TREE
|| !CONSTANT_CLASS_P (arg0
))
1311 *RESULT = FN (*ARG0, *ARG1)
1313 in format FORMAT. Return true on success. */
1316 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1317 const real_value
*arg0
, const real_value
*arg1
,
1318 const real_format
*format
)
1324 return do_mpfr_arg2 (result
, mpfr_remainder
, arg0
, arg1
, format
);
1327 return do_mpfr_arg2 (result
, mpfr_atan2
, arg0
, arg1
, format
);
1330 return do_mpfr_arg2 (result
, mpfr_dim
, arg0
, arg1
, format
);
1333 return do_mpfr_arg2 (result
, mpfr_hypot
, arg0
, arg1
, format
);
1336 CASE_CFN_COPYSIGN_FN
:
1338 real_copysign (result
, arg1
);
1343 return do_mpfr_arg2 (result
, mpfr_min
, arg0
, arg1
, format
);
1347 return do_mpfr_arg2 (result
, mpfr_max
, arg0
, arg1
, format
);
1350 return fold_const_pow (result
, arg0
, arg1
, format
);
1353 CASE_CFN_NEXTTOWARD
:
1354 return fold_const_nextafter (result
, arg0
, arg1
, format
);
1363 *RESULT = FN (*ARG0, ARG1)
1365 where FORMAT is the format of *RESULT and *ARG0. Return true on
1369 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1370 const real_value
*arg0
, const wide_int_ref
&arg1
,
1371 const real_format
*format
)
1376 return fold_const_builtin_load_exponent (result
, arg0
, arg1
, format
);
1380 return (format
->b
== 2
1381 && fold_const_builtin_load_exponent (result
, arg0
, arg1
,
1385 /* Avoid the folding if flag_signaling_nans is on and
1386 operand is a signaling NaN. */
1387 if (!flag_unsafe_math_optimizations
1388 && flag_signaling_nans
1389 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))
1392 real_powi (result
, format
, arg0
, arg1
.to_shwi ());
1402 *RESULT = FN (ARG0, *ARG1)
1404 where FORMAT is the format of *RESULT and *ARG1. Return true on
1408 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1409 const wide_int_ref
&arg0
, const real_value
*arg1
,
1410 const real_format
*format
)
1415 return do_mpfr_arg2 (result
, mpfr_jn
, arg0
, arg1
, format
);
1418 return (real_compare (GT_EXPR
, arg1
, &dconst0
)
1419 && do_mpfr_arg2 (result
, mpfr_yn
, arg0
, arg1
, format
));
1428 RESULT = fn (ARG0, ARG1)
1430 where FORMAT is the format of the real and imaginary parts of RESULT
1431 (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1432 and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1435 fold_const_call_ccc (real_value
*result_real
, real_value
*result_imag
,
1436 combined_fn fn
, const real_value
*arg0_real
,
1437 const real_value
*arg0_imag
, const real_value
*arg1_real
,
1438 const real_value
*arg1_imag
, const real_format
*format
)
1443 return do_mpc_arg2 (result_real
, result_imag
, mpc_pow
,
1444 arg0_real
, arg0_imag
, arg1_real
, arg1_imag
, format
);
1451 /* Subroutine of fold_const_call, with the same interface. Handle cases
1452 where the arguments and result are numerical. */
1455 fold_const_call_1 (combined_fn fn
, tree type
, tree arg0
, tree arg1
)
1457 machine_mode mode
= TYPE_MODE (type
);
1458 machine_mode arg0_mode
= TYPE_MODE (TREE_TYPE (arg0
));
1459 machine_mode arg1_mode
= TYPE_MODE (TREE_TYPE (arg1
));
1461 if (mode
== arg0_mode
1462 && real_cst_p (arg0
)
1463 && real_cst_p (arg1
))
1465 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1466 REAL_VALUE_TYPE result
;
1467 if (arg0_mode
== arg1_mode
)
1469 /* real, real -> real. */
1470 if (fold_const_call_sss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1471 TREE_REAL_CST_PTR (arg1
),
1472 REAL_MODE_FORMAT (mode
)))
1473 return build_real (type
, result
);
1475 else if (arg1_mode
== TYPE_MODE (long_double_type_node
))
1478 CASE_CFN_NEXTTOWARD
:
1479 /* real, long double -> real. */
1480 if (fold_const_call_sss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1481 TREE_REAL_CST_PTR (arg1
),
1482 REAL_MODE_FORMAT (mode
)))
1483 return build_real (type
, result
);
1491 if (real_cst_p (arg0
)
1492 && integer_cst_p (arg1
))
1494 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1495 if (mode
== arg0_mode
)
1497 /* real, int -> real. */
1498 REAL_VALUE_TYPE result
;
1499 if (fold_const_call_sss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1501 REAL_MODE_FORMAT (mode
)))
1502 return build_real (type
, result
);
1507 if (integer_cst_p (arg0
)
1508 && real_cst_p (arg1
))
1510 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode
));
1511 if (mode
== arg1_mode
)
1513 /* int, real -> real. */
1514 REAL_VALUE_TYPE result
;
1515 if (fold_const_call_sss (&result
, fn
, wi::to_wide (arg0
),
1516 TREE_REAL_CST_PTR (arg1
),
1517 REAL_MODE_FORMAT (mode
)))
1518 return build_real (type
, result
);
1523 if (arg0_mode
== arg1_mode
1524 && complex_cst_p (arg0
)
1525 && complex_cst_p (arg1
))
1527 gcc_checking_assert (COMPLEX_MODE_P (arg0_mode
));
1528 machine_mode inner_mode
= GET_MODE_INNER (arg0_mode
);
1529 tree arg0r
= TREE_REALPART (arg0
);
1530 tree arg0i
= TREE_IMAGPART (arg0
);
1531 tree arg1r
= TREE_REALPART (arg1
);
1532 tree arg1i
= TREE_IMAGPART (arg1
);
1533 if (mode
== arg0_mode
1534 && real_cst_p (arg0r
)
1535 && real_cst_p (arg0i
)
1536 && real_cst_p (arg1r
)
1537 && real_cst_p (arg1i
))
1539 /* complex real, complex real -> complex real. */
1540 REAL_VALUE_TYPE result_real
, result_imag
;
1541 if (fold_const_call_ccc (&result_real
, &result_imag
, fn
,
1542 TREE_REAL_CST_PTR (arg0r
),
1543 TREE_REAL_CST_PTR (arg0i
),
1544 TREE_REAL_CST_PTR (arg1r
),
1545 TREE_REAL_CST_PTR (arg1i
),
1546 REAL_MODE_FORMAT (inner_mode
)))
1547 return build_complex (type
,
1548 build_real (TREE_TYPE (type
), result_real
),
1549 build_real (TREE_TYPE (type
), result_imag
));
1557 /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1558 otherwise return null. TYPE is the type of the return value. */
1561 fold_const_call (combined_fn fn
, tree type
, tree arg0
, tree arg1
)
1563 const char *p0
, *p1
;
1567 case CFN_BUILT_IN_STRSPN
:
1568 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1569 return build_int_cst (type
, strspn (p0
, p1
));
1572 case CFN_BUILT_IN_STRCSPN
:
1573 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1574 return build_int_cst (type
, strcspn (p0
, p1
));
1577 case CFN_BUILT_IN_STRCMP
:
1578 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1579 return build_cmp_result (type
, strcmp (p0
, p1
));
1582 case CFN_BUILT_IN_STRCASECMP
:
1583 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1585 int r
= strcmp (p0
, p1
);
1587 return build_cmp_result (type
, r
);
1591 case CFN_BUILT_IN_INDEX
:
1592 case CFN_BUILT_IN_STRCHR
:
1593 if ((p0
= c_getstr (arg0
)) && target_char_cst_p (arg1
, &c
))
1595 const char *r
= strchr (p0
, c
);
1597 return build_int_cst (type
, 0);
1598 return fold_convert (type
,
1599 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1603 case CFN_BUILT_IN_RINDEX
:
1604 case CFN_BUILT_IN_STRRCHR
:
1605 if ((p0
= c_getstr (arg0
)) && target_char_cst_p (arg1
, &c
))
1607 const char *r
= strrchr (p0
, c
);
1609 return build_int_cst (type
, 0);
1610 return fold_convert (type
,
1611 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1615 case CFN_BUILT_IN_STRSTR
:
1616 if ((p1
= c_getstr (arg1
)))
1618 if ((p0
= c_getstr (arg0
)))
1620 const char *r
= strstr (p0
, p1
);
1622 return build_int_cst (type
, 0);
1623 return fold_convert (type
,
1624 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1627 return fold_convert (type
, arg0
);
1631 case CFN_FOLD_LEFT_PLUS
:
1632 return fold_const_fold_left (type
, arg0
, arg1
, PLUS_EXPR
);
1635 return fold_const_call_1 (fn
, type
, arg0
, arg1
);
1641 *RESULT = FN (*ARG0, *ARG1, *ARG2)
1643 in format FORMAT. Return true on success. */
1646 fold_const_call_ssss (real_value
*result
, combined_fn fn
,
1647 const real_value
*arg0
, const real_value
*arg1
,
1648 const real_value
*arg2
, const real_format
*format
)
1654 return do_mpfr_arg3 (result
, mpfr_fma
, arg0
, arg1
, arg2
, format
);
1658 real_value new_arg2
= real_value_negate (arg2
);
1659 return do_mpfr_arg3 (result
, mpfr_fma
, arg0
, arg1
, &new_arg2
, format
);
1664 real_value new_arg0
= real_value_negate (arg0
);
1665 return do_mpfr_arg3 (result
, mpfr_fma
, &new_arg0
, arg1
, arg2
, format
);
1670 real_value new_arg0
= real_value_negate (arg0
);
1671 real_value new_arg2
= real_value_negate (arg2
);
1672 return do_mpfr_arg3 (result
, mpfr_fma
, &new_arg0
, arg1
,
1681 /* Subroutine of fold_const_call, with the same interface. Handle cases
1682 where the arguments and result are numerical. */
1685 fold_const_call_1 (combined_fn fn
, tree type
, tree arg0
, tree arg1
, tree arg2
)
1687 machine_mode mode
= TYPE_MODE (type
);
1688 machine_mode arg0_mode
= TYPE_MODE (TREE_TYPE (arg0
));
1689 machine_mode arg1_mode
= TYPE_MODE (TREE_TYPE (arg1
));
1690 machine_mode arg2_mode
= TYPE_MODE (TREE_TYPE (arg2
));
1692 if (arg0_mode
== arg1_mode
1693 && arg0_mode
== arg2_mode
1694 && real_cst_p (arg0
)
1695 && real_cst_p (arg1
)
1696 && real_cst_p (arg2
))
1698 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1699 if (mode
== arg0_mode
)
1701 /* real, real, real -> real. */
1702 REAL_VALUE_TYPE result
;
1703 if (fold_const_call_ssss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1704 TREE_REAL_CST_PTR (arg1
),
1705 TREE_REAL_CST_PTR (arg2
),
1706 REAL_MODE_FORMAT (mode
)))
1707 return build_real (type
, result
);
1715 /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
1716 success, otherwise return null. TYPE is the type of the return value. */
1719 fold_const_call (combined_fn fn
, tree type
, tree arg0
, tree arg1
, tree arg2
)
1721 const char *p0
, *p1
;
1723 unsigned HOST_WIDE_INT s0
, s1
;
1727 case CFN_BUILT_IN_STRNCMP
:
1728 if (!host_size_t_cst_p (arg2
, &s2
))
1731 && !TREE_SIDE_EFFECTS (arg0
)
1732 && !TREE_SIDE_EFFECTS (arg1
))
1733 return build_int_cst (type
, 0);
1734 else if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1735 return build_int_cst (type
, strncmp (p0
, p1
, s2
));
1738 case CFN_BUILT_IN_STRNCASECMP
:
1739 if (!host_size_t_cst_p (arg2
, &s2
))
1742 && !TREE_SIDE_EFFECTS (arg0
)
1743 && !TREE_SIDE_EFFECTS (arg1
))
1744 return build_int_cst (type
, 0);
1745 else if ((p0
= c_getstr (arg0
))
1746 && (p1
= c_getstr (arg1
))
1747 && strncmp (p0
, p1
, s2
) == 0)
1748 return build_int_cst (type
, 0);
1751 case CFN_BUILT_IN_BCMP
:
1752 case CFN_BUILT_IN_MEMCMP
:
1753 if (!host_size_t_cst_p (arg2
, &s2
))
1756 && !TREE_SIDE_EFFECTS (arg0
)
1757 && !TREE_SIDE_EFFECTS (arg1
))
1758 return build_int_cst (type
, 0);
1759 if ((p0
= c_getstr (arg0
, &s0
))
1760 && (p1
= c_getstr (arg1
, &s1
))
1763 return build_cmp_result (type
, memcmp (p0
, p1
, s2
));
1766 case CFN_BUILT_IN_MEMCHR
:
1767 if (!host_size_t_cst_p (arg2
, &s2
))
1770 && !TREE_SIDE_EFFECTS (arg0
)
1771 && !TREE_SIDE_EFFECTS (arg1
))
1772 return build_int_cst (type
, 0);
1773 if ((p0
= c_getstr (arg0
, &s0
))
1775 && target_char_cst_p (arg1
, &c
))
1777 const char *r
= (const char *) memchr (p0
, c
, s2
);
1779 return build_int_cst (type
, 0);
1780 return fold_convert (type
,
1781 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1786 return fold_const_call_1 (fn
, type
, arg0
, arg1
, arg2
);