1 /* Constant folding for calls to built-in and internal functions.
2 Copyright (C) 1988-2018 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"
34 /* Functions that test for certain constant types, abstracting away the
35 decision about whether to check for overflow. */
38 integer_cst_p (tree t
)
40 return TREE_CODE (t
) == INTEGER_CST
&& !TREE_OVERFLOW (t
);
46 return TREE_CODE (t
) == REAL_CST
&& !TREE_OVERFLOW (t
);
50 complex_cst_p (tree t
)
52 return TREE_CODE (t
) == COMPLEX_CST
;
55 /* Return true if ARG is a constant in the range of the host size_t.
56 Store it in *SIZE_OUT if so. */
59 host_size_t_cst_p (tree t
, size_t *size_out
)
61 if (types_compatible_p (size_type_node
, TREE_TYPE (t
))
63 && (wi::min_precision (wi::to_wide (t
), UNSIGNED
)
64 <= sizeof (size_t) * CHAR_BIT
))
66 *size_out
= tree_to_uhwi (t
);
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. */
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. */
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
)
96 || mpfr_underflow_p ()
97 || (flag_rounding_math
&& inexact
))
101 real_from_mpfr (&tmp
, m
, format
, GMP_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)))
110 real_convert (result
, format
, &tmp
);
111 return real_identical (result
, &tmp
);
118 in format FORMAT, given that FUNC is the MPFR implementation of f.
119 Return true on success. */
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
))
131 int prec
= format
->p
;
132 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
135 mpfr_init2 (m
, prec
);
136 mpfr_from_real (m
, arg
, GMP_RNDN
);
138 bool inexact
= func (m
, m
, rnd
);
139 bool ok
= do_mpfr_ckconv (result
, m
, inexact
, format
);
147 *RESULT_SIN = sin (*ARG);
148 *RESULT_COS = cos (*ARG);
150 for format FORMAT. Return true on success. */
153 do_mpfr_sincos (real_value
*result_sin
, real_value
*result_cos
,
154 const real_value
*arg
, const real_format
*format
)
156 /* To proceed, MPFR must exactly represent the target floating point
157 format, which only happens when the target base equals two. */
158 if (format
->b
!= 2 || !real_isfinite (arg
))
161 int prec
= format
->p
;
162 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
165 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
166 mpfr_from_real (m
, arg
, GMP_RNDN
);
168 bool inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
169 bool ok
= (do_mpfr_ckconv (result_sin
, ms
, inexact
, format
)
170 && do_mpfr_ckconv (result_cos
, mc
, inexact
, format
));
171 mpfr_clears (m
, ms
, mc
, NULL
);
178 *RESULT = f (*ARG0, *ARG1)
180 in format FORMAT, given that FUNC is the MPFR implementation of f.
181 Return true on success. */
184 do_mpfr_arg2 (real_value
*result
,
185 int (*func
) (mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_rnd_t
),
186 const real_value
*arg0
, const real_value
*arg1
,
187 const real_format
*format
)
189 /* To proceed, MPFR must exactly represent the target floating point
190 format, which only happens when the target base equals two. */
191 if (format
->b
!= 2 || !real_isfinite (arg0
) || !real_isfinite (arg1
))
194 int prec
= format
->p
;
195 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
198 mpfr_inits2 (prec
, m0
, m1
, NULL
);
199 mpfr_from_real (m0
, arg0
, GMP_RNDN
);
200 mpfr_from_real (m1
, arg1
, GMP_RNDN
);
202 bool inexact
= func (m0
, m0
, m1
, rnd
);
203 bool ok
= do_mpfr_ckconv (result
, m0
, inexact
, format
);
204 mpfr_clears (m0
, m1
, NULL
);
211 *RESULT = f (ARG0, *ARG1)
213 in format FORMAT, given that FUNC is the MPFR implementation of f.
214 Return true on success. */
217 do_mpfr_arg2 (real_value
*result
,
218 int (*func
) (mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
219 const wide_int_ref
&arg0
, const real_value
*arg1
,
220 const real_format
*format
)
222 if (format
->b
!= 2 || !real_isfinite (arg1
))
225 int prec
= format
->p
;
226 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
229 mpfr_init2 (m
, prec
);
230 mpfr_from_real (m
, arg1
, GMP_RNDN
);
232 bool inexact
= func (m
, arg0
.to_shwi (), m
, rnd
);
233 bool ok
= do_mpfr_ckconv (result
, m
, inexact
, format
);
241 *RESULT = f (*ARG0, *ARG1, *ARG2)
243 in format FORMAT, given that FUNC is the MPFR implementation of f.
244 Return true on success. */
247 do_mpfr_arg3 (real_value
*result
,
248 int (*func
) (mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
,
249 mpfr_srcptr
, mpfr_rnd_t
),
250 const real_value
*arg0
, const real_value
*arg1
,
251 const real_value
*arg2
, const real_format
*format
)
253 /* To proceed, MPFR must exactly represent the target floating point
254 format, which only happens when the target base equals two. */
256 || !real_isfinite (arg0
)
257 || !real_isfinite (arg1
)
258 || !real_isfinite (arg2
))
261 int prec
= format
->p
;
262 mp_rnd_t rnd
= format
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
265 mpfr_inits2 (prec
, m0
, m1
, m2
, NULL
);
266 mpfr_from_real (m0
, arg0
, GMP_RNDN
);
267 mpfr_from_real (m1
, arg1
, GMP_RNDN
);
268 mpfr_from_real (m2
, arg2
, GMP_RNDN
);
270 bool inexact
= func (m0
, m0
, m1
, m2
, rnd
);
271 bool ok
= do_mpfr_ckconv (result
, m0
, inexact
, format
);
272 mpfr_clears (m0
, m1
, m2
, NULL
);
277 /* M is the result of trying to constant-fold an expression (starting
278 with clear MPFR flags) and INEXACT says whether the result in M is
279 exact or inexact. Return true if M can be used as a constant-folded
280 result in which the real and imaginary parts have format FORMAT.
281 Store those parts in *RESULT_REAL and *RESULT_IMAG if so. */
284 do_mpc_ckconv (real_value
*result_real
, real_value
*result_imag
,
285 mpc_srcptr m
, bool inexact
, const real_format
*format
)
287 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
288 overflow/underflow occurred. If -frounding-math, proceed iff the
289 result of calling FUNC was exact. */
290 if (!mpfr_number_p (mpc_realref (m
))
291 || !mpfr_number_p (mpc_imagref (m
))
292 || mpfr_overflow_p ()
293 || mpfr_underflow_p ()
294 || (flag_rounding_math
&& inexact
))
297 REAL_VALUE_TYPE tmp_real
, tmp_imag
;
298 real_from_mpfr (&tmp_real
, mpc_realref (m
), format
, GMP_RNDN
);
299 real_from_mpfr (&tmp_imag
, mpc_imagref (m
), format
, GMP_RNDN
);
301 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
302 If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
303 underflowed in the conversion. */
304 if (!real_isfinite (&tmp_real
)
305 || !real_isfinite (&tmp_imag
)
306 || (tmp_real
.cl
== rvc_zero
) != (mpfr_zero_p (mpc_realref (m
)) != 0)
307 || (tmp_imag
.cl
== rvc_zero
) != (mpfr_zero_p (mpc_imagref (m
)) != 0))
310 real_convert (result_real
, format
, &tmp_real
);
311 real_convert (result_imag
, format
, &tmp_imag
);
313 return (real_identical (result_real
, &tmp_real
)
314 && real_identical (result_imag
, &tmp_imag
));
321 in format FORMAT, given that FUNC is the mpc implementation of f.
322 Return true on success. Both RESULT and ARG are represented as
323 real and imaginary pairs. */
326 do_mpc_arg1 (real_value
*result_real
, real_value
*result_imag
,
327 int (*func
) (mpc_ptr
, mpc_srcptr
, mpc_rnd_t
),
328 const real_value
*arg_real
, const real_value
*arg_imag
,
329 const real_format
*format
)
331 /* To proceed, MPFR must exactly represent the target floating point
332 format, which only happens when the target base equals two. */
334 || !real_isfinite (arg_real
)
335 || !real_isfinite (arg_imag
))
338 int prec
= format
->p
;
339 mpc_rnd_t crnd
= format
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
343 mpfr_from_real (mpc_realref (m
), arg_real
, GMP_RNDN
);
344 mpfr_from_real (mpc_imagref (m
), arg_imag
, GMP_RNDN
);
346 bool inexact
= func (m
, m
, crnd
);
347 bool ok
= do_mpc_ckconv (result_real
, result_imag
, m
, inexact
, format
);
355 RESULT = f (ARG0, ARG1)
357 in format FORMAT, given that FUNC is the mpc implementation of f.
358 Return true on success. RESULT, ARG0 and ARG1 are represented as
359 real and imaginary pairs. */
362 do_mpc_arg2 (real_value
*result_real
, real_value
*result_imag
,
363 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
),
364 const real_value
*arg0_real
, const real_value
*arg0_imag
,
365 const real_value
*arg1_real
, const real_value
*arg1_imag
,
366 const real_format
*format
)
368 if (!real_isfinite (arg0_real
)
369 || !real_isfinite (arg0_imag
)
370 || !real_isfinite (arg1_real
)
371 || !real_isfinite (arg1_imag
))
374 int prec
= format
->p
;
375 mpc_rnd_t crnd
= format
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
378 mpc_init2 (m0
, prec
);
379 mpc_init2 (m1
, prec
);
380 mpfr_from_real (mpc_realref (m0
), arg0_real
, GMP_RNDN
);
381 mpfr_from_real (mpc_imagref (m0
), arg0_imag
, GMP_RNDN
);
382 mpfr_from_real (mpc_realref (m1
), arg1_real
, GMP_RNDN
);
383 mpfr_from_real (mpc_imagref (m1
), arg1_imag
, GMP_RNDN
);
385 bool inexact
= func (m0
, m0
, m1
, crnd
);
386 bool ok
= do_mpc_ckconv (result_real
, result_imag
, m0
, inexact
, format
);
395 *RESULT = logb (*ARG)
397 in format FORMAT. Return true on success. */
400 fold_const_logb (real_value
*result
, const real_value
*arg
,
401 const real_format
*format
)
406 /* If arg is +-NaN, then return it. */
411 /* If arg is +-Inf, then return +Inf. */
417 /* Zero may set errno and/or raise an exception. */
421 /* For normal numbers, proceed iff radix == 2. In GCC,
422 normalized significands are in the range [0.5, 1.0). We
423 want the exponent as if they were [1.0, 2.0) so get the
424 exponent and subtract 1. */
427 real_from_integer (result
, format
, REAL_EXP (arg
) - 1, SIGNED
);
437 *RESULT = significand (*ARG)
439 in format FORMAT. Return true on success. */
442 fold_const_significand (real_value
*result
, const real_value
*arg
,
443 const real_format
*format
)
450 /* If arg is +-0, +-Inf or +-NaN, then return it. */
455 /* For normal numbers, proceed iff radix == 2. */
459 /* In GCC, normalized significands are in the range [0.5, 1.0).
460 We want them to be [1.0, 2.0) so set the exponent to 1. */
461 SET_REAL_EXP (result
, 1);
473 where FORMAT is the format of *ARG and PRECISION is the number of
474 significant bits in the result. Return true on success. */
477 fold_const_conversion (wide_int
*result
,
478 void (*fn
) (real_value
*, format_helper
,
480 const real_value
*arg
, unsigned int precision
,
481 const real_format
*format
)
483 if (!real_isfinite (arg
))
487 fn (&rounded
, format
, arg
);
490 *result
= real_to_integer (&rounded
, &fail
, precision
);
496 *RESULT = pow (*ARG0, *ARG1)
498 in format FORMAT. Return true on success. */
501 fold_const_pow (real_value
*result
, const real_value
*arg0
,
502 const real_value
*arg1
, const real_format
*format
)
504 if (do_mpfr_arg2 (result
, mpfr_pow
, arg0
, arg1
, format
))
507 /* Check for an integer exponent. */
508 REAL_VALUE_TYPE cint1
;
509 HOST_WIDE_INT n1
= real_to_integer (arg1
);
510 real_from_integer (&cint1
, VOIDmode
, n1
, SIGNED
);
511 /* Attempt to evaluate pow at compile-time, unless this should
512 raise an exception. */
513 if (real_identical (arg1
, &cint1
)
515 || (!flag_trapping_math
&& !flag_errno_math
)
516 || !real_equal (arg0
, &dconst0
)))
518 bool inexact
= real_powi (result
, format
, arg0
, n1
);
519 /* Avoid the folding if flag_signaling_nans is on. */
520 if (flag_unsafe_math_optimizations
522 && !(flag_signaling_nans
523 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))))
532 *RESULT = ldexp (*ARG0, ARG1)
534 in format FORMAT. Return true on success. */
537 fold_const_builtin_load_exponent (real_value
*result
, const real_value
*arg0
,
538 const wide_int_ref
&arg1
,
539 const real_format
*format
)
541 /* Bound the maximum adjustment to twice the range of the
542 mode's valid exponents. Use abs to ensure the range is
543 positive as a sanity check. */
544 int max_exp_adj
= 2 * labs (format
->emax
- format
->emin
);
546 /* The requested adjustment must be inside this range. This
547 is a preliminary cap to avoid things like overflow, we
548 may still fail to compute the result for other reasons. */
549 if (wi::les_p (arg1
, -max_exp_adj
) || wi::ges_p (arg1
, max_exp_adj
))
552 /* Don't perform operation if we honor signaling NaNs and
553 operand is a signaling NaN. */
554 if (!flag_unsafe_math_optimizations
555 && flag_signaling_nans
556 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))
559 REAL_VALUE_TYPE initial_result
;
560 real_ldexp (&initial_result
, arg0
, arg1
.to_shwi ());
562 /* Ensure we didn't overflow. */
563 if (real_isinf (&initial_result
))
566 /* Only proceed if the target mode can hold the
568 *result
= real_value_truncate (format
, initial_result
);
569 return real_equal (&initial_result
, result
);
572 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
573 return type TYPE. QUIET is true if a quiet rather than signalling
577 fold_const_builtin_nan (tree type
, tree arg
, bool quiet
)
579 REAL_VALUE_TYPE real
;
580 const char *str
= c_getstr (arg
);
581 if (str
&& real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
582 return build_real (type
, real
);
586 /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE. */
589 fold_const_reduction (tree type
, tree arg
, tree_code code
)
591 unsigned HOST_WIDE_INT nelts
;
592 if (TREE_CODE (arg
) != VECTOR_CST
593 || !VECTOR_CST_NELTS (arg
).is_constant (&nelts
))
596 tree res
= VECTOR_CST_ELT (arg
, 0);
597 for (unsigned HOST_WIDE_INT i
= 1; i
< nelts
; i
++)
599 res
= const_binop (code
, type
, res
, VECTOR_CST_ELT (arg
, i
));
600 if (res
== NULL_TREE
|| !CONSTANT_CLASS_P (res
))
610 in format FORMAT. Return true on success. */
613 fold_const_call_ss (real_value
*result
, combined_fn fn
,
614 const real_value
*arg
, const real_format
*format
)
620 return (real_compare (GE_EXPR
, arg
, &dconst0
)
621 && do_mpfr_arg1 (result
, mpfr_sqrt
, arg
, format
));
624 return do_mpfr_arg1 (result
, mpfr_cbrt
, arg
, format
);
627 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
628 && real_compare (LE_EXPR
, arg
, &dconst1
)
629 && do_mpfr_arg1 (result
, mpfr_asin
, arg
, format
));
632 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
633 && real_compare (LE_EXPR
, arg
, &dconst1
)
634 && do_mpfr_arg1 (result
, mpfr_acos
, arg
, format
));
637 return do_mpfr_arg1 (result
, mpfr_atan
, arg
, format
);
640 return do_mpfr_arg1 (result
, mpfr_asinh
, arg
, format
);
643 return (real_compare (GE_EXPR
, arg
, &dconst1
)
644 && do_mpfr_arg1 (result
, mpfr_acosh
, arg
, format
));
647 return (real_compare (GE_EXPR
, arg
, &dconstm1
)
648 && real_compare (LE_EXPR
, arg
, &dconst1
)
649 && do_mpfr_arg1 (result
, mpfr_atanh
, arg
, format
));
652 return do_mpfr_arg1 (result
, mpfr_sin
, arg
, format
);
655 return do_mpfr_arg1 (result
, mpfr_cos
, arg
, format
);
658 return do_mpfr_arg1 (result
, mpfr_tan
, arg
, format
);
661 return do_mpfr_arg1 (result
, mpfr_sinh
, arg
, format
);
664 return do_mpfr_arg1 (result
, mpfr_cosh
, arg
, format
);
667 return do_mpfr_arg1 (result
, mpfr_tanh
, arg
, format
);
670 return do_mpfr_arg1 (result
, mpfr_erf
, arg
, format
);
673 return do_mpfr_arg1 (result
, mpfr_erfc
, arg
, format
);
676 return do_mpfr_arg1 (result
, mpfr_gamma
, arg
, format
);
679 return do_mpfr_arg1 (result
, mpfr_exp
, arg
, format
);
682 return do_mpfr_arg1 (result
, mpfr_exp2
, arg
, format
);
686 return do_mpfr_arg1 (result
, mpfr_exp10
, arg
, format
);
689 return do_mpfr_arg1 (result
, mpfr_expm1
, arg
, format
);
692 return (real_compare (GT_EXPR
, arg
, &dconst0
)
693 && do_mpfr_arg1 (result
, mpfr_log
, arg
, format
));
696 return (real_compare (GT_EXPR
, arg
, &dconst0
)
697 && do_mpfr_arg1 (result
, mpfr_log2
, arg
, format
));
700 return (real_compare (GT_EXPR
, arg
, &dconst0
)
701 && do_mpfr_arg1 (result
, mpfr_log10
, arg
, format
));
704 return (real_compare (GT_EXPR
, arg
, &dconstm1
)
705 && do_mpfr_arg1 (result
, mpfr_log1p
, arg
, format
));
708 return do_mpfr_arg1 (result
, mpfr_j0
, arg
, format
);
711 return do_mpfr_arg1 (result
, mpfr_j1
, arg
, format
);
714 return (real_compare (GT_EXPR
, arg
, &dconst0
)
715 && do_mpfr_arg1 (result
, mpfr_y0
, arg
, format
));
718 return (real_compare (GT_EXPR
, arg
, &dconst0
)
719 && do_mpfr_arg1 (result
, mpfr_y1
, arg
, format
));
723 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
725 real_floor (result
, format
, arg
);
732 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
734 real_ceil (result
, format
, arg
);
741 real_trunc (result
, format
, arg
);
746 if (!REAL_VALUE_ISNAN (*arg
) || !flag_errno_math
)
748 real_round (result
, format
, arg
);
754 return fold_const_logb (result
, arg
, format
);
756 CASE_CFN_SIGNIFICAND
:
757 return fold_const_significand (result
, arg
, format
);
768 where FORMAT is the format of ARG and PRECISION is the number of
769 significant bits in the result. Return true on success. */
772 fold_const_call_ss (wide_int
*result
, combined_fn fn
,
773 const real_value
*arg
, unsigned int precision
,
774 const real_format
*format
)
779 if (real_isneg (arg
))
780 *result
= wi::one (precision
);
782 *result
= wi::zero (precision
);
786 /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
787 Proceed iff radix == 2. In GCC, normalized significands are in
788 the range [0.5, 1.0). We want the exponent as if they were
789 [1.0, 2.0) so get the exponent and subtract 1. */
790 if (arg
->cl
== rvc_normal
&& format
->b
== 2)
792 *result
= wi::shwi (REAL_EXP (arg
) - 1, precision
);
800 return fold_const_conversion (result
, real_ceil
, arg
,
806 return fold_const_conversion (result
, real_floor
, arg
,
812 return fold_const_conversion (result
, real_round
, arg
,
818 /* Not yet folded to a constant. */
822 case CFN_BUILT_IN_FINITED32
:
823 case CFN_BUILT_IN_FINITED64
:
824 case CFN_BUILT_IN_FINITED128
:
825 case CFN_BUILT_IN_ISFINITE
:
826 *result
= wi::shwi (real_isfinite (arg
) ? 1 : 0, precision
);
830 case CFN_BUILT_IN_ISINFD32
:
831 case CFN_BUILT_IN_ISINFD64
:
832 case CFN_BUILT_IN_ISINFD128
:
833 if (real_isinf (arg
))
834 *result
= wi::shwi (arg
->sign
? -1 : 1, precision
);
836 *result
= wi::shwi (0, precision
);
840 case CFN_BUILT_IN_ISNAND32
:
841 case CFN_BUILT_IN_ISNAND64
:
842 case CFN_BUILT_IN_ISNAND128
:
843 *result
= wi::shwi (real_isnan (arg
) ? 1 : 0, precision
);
855 where ARG_TYPE is the type of ARG and PRECISION is the number of bits
856 in the result. Return true on success. */
859 fold_const_call_ss (wide_int
*result
, combined_fn fn
, const wide_int_ref
&arg
,
860 unsigned int precision
, tree arg_type
)
865 *result
= wi::shwi (wi::ffs (arg
), precision
);
871 if (wi::ne_p (arg
, 0))
873 else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type
),
875 tmp
= TYPE_PRECISION (arg_type
);
876 *result
= wi::shwi (tmp
, precision
);
883 if (wi::ne_p (arg
, 0))
885 else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type
),
887 tmp
= TYPE_PRECISION (arg_type
);
888 *result
= wi::shwi (tmp
, precision
);
893 *result
= wi::shwi (wi::clrsb (arg
), precision
);
897 *result
= wi::shwi (wi::popcount (arg
), precision
);
901 *result
= wi::shwi (wi::parity (arg
), precision
);
904 case CFN_BUILT_IN_BSWAP16
:
905 case CFN_BUILT_IN_BSWAP32
:
906 case CFN_BUILT_IN_BSWAP64
:
907 *result
= wide_int::from (arg
, precision
, TYPE_SIGN (arg_type
)).bswap ();
919 where FORMAT is the format of ARG and of the real and imaginary parts
920 of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
924 fold_const_call_cs (real_value
*result_real
, real_value
*result_imag
,
925 combined_fn fn
, const real_value
*arg
,
926 const real_format
*format
)
931 /* cexpi(x+yi) = cos(x)+sin(y)*i. */
932 return do_mpfr_sincos (result_imag
, result_real
, arg
, format
);
943 where FORMAT is the format of RESULT and of the real and imaginary parts
944 of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
948 fold_const_call_sc (real_value
*result
, combined_fn fn
,
949 const real_value
*arg_real
, const real_value
*arg_imag
,
950 const real_format
*format
)
955 return do_mpfr_arg2 (result
, mpfr_hypot
, arg_real
, arg_imag
, format
);
966 where FORMAT is the format of the real and imaginary parts of RESULT
967 (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
968 Return true on success. */
971 fold_const_call_cc (real_value
*result_real
, real_value
*result_imag
,
972 combined_fn fn
, const real_value
*arg_real
,
973 const real_value
*arg_imag
, const real_format
*format
)
978 return do_mpc_arg1 (result_real
, result_imag
, mpc_cos
,
979 arg_real
, arg_imag
, format
);
982 return do_mpc_arg1 (result_real
, result_imag
, mpc_cosh
,
983 arg_real
, arg_imag
, format
);
986 if (real_isinf (arg_real
) || real_isinf (arg_imag
))
988 real_inf (result_real
);
989 *result_imag
= dconst0
;
990 result_imag
->sign
= arg_imag
->sign
;
994 *result_real
= *arg_real
;
995 *result_imag
= *arg_imag
;
1000 return do_mpc_arg1 (result_real
, result_imag
, mpc_sin
,
1001 arg_real
, arg_imag
, format
);
1004 return do_mpc_arg1 (result_real
, result_imag
, mpc_sinh
,
1005 arg_real
, arg_imag
, format
);
1008 return do_mpc_arg1 (result_real
, result_imag
, mpc_tan
,
1009 arg_real
, arg_imag
, format
);
1012 return do_mpc_arg1 (result_real
, result_imag
, mpc_tanh
,
1013 arg_real
, arg_imag
, format
);
1016 return do_mpc_arg1 (result_real
, result_imag
, mpc_log
,
1017 arg_real
, arg_imag
, format
);
1020 return do_mpc_arg1 (result_real
, result_imag
, mpc_sqrt
,
1021 arg_real
, arg_imag
, format
);
1024 return do_mpc_arg1 (result_real
, result_imag
, mpc_asin
,
1025 arg_real
, arg_imag
, format
);
1028 return do_mpc_arg1 (result_real
, result_imag
, mpc_acos
,
1029 arg_real
, arg_imag
, format
);
1032 return do_mpc_arg1 (result_real
, result_imag
, mpc_atan
,
1033 arg_real
, arg_imag
, format
);
1036 return do_mpc_arg1 (result_real
, result_imag
, mpc_asinh
,
1037 arg_real
, arg_imag
, format
);
1040 return do_mpc_arg1 (result_real
, result_imag
, mpc_acosh
,
1041 arg_real
, arg_imag
, format
);
1044 return do_mpc_arg1 (result_real
, result_imag
, mpc_atanh
,
1045 arg_real
, arg_imag
, format
);
1048 return do_mpc_arg1 (result_real
, result_imag
, mpc_exp
,
1049 arg_real
, arg_imag
, format
);
1056 /* Subroutine of fold_const_call, with the same interface. Handle cases
1057 where the arguments and result are numerical. */
1060 fold_const_call_1 (combined_fn fn
, tree type
, tree arg
)
1062 machine_mode mode
= TYPE_MODE (type
);
1063 machine_mode arg_mode
= TYPE_MODE (TREE_TYPE (arg
));
1065 if (integer_cst_p (arg
))
1067 if (SCALAR_INT_MODE_P (mode
))
1070 if (fold_const_call_ss (&result
, fn
, wi::to_wide (arg
),
1071 TYPE_PRECISION (type
), TREE_TYPE (arg
)))
1072 return wide_int_to_tree (type
, result
);
1077 if (real_cst_p (arg
))
1079 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode
));
1080 if (mode
== arg_mode
)
1083 REAL_VALUE_TYPE result
;
1084 if (fold_const_call_ss (&result
, fn
, TREE_REAL_CST_PTR (arg
),
1085 REAL_MODE_FORMAT (mode
)))
1086 return build_real (type
, result
);
1088 else if (COMPLEX_MODE_P (mode
)
1089 && GET_MODE_INNER (mode
) == arg_mode
)
1091 /* real -> complex real. */
1092 REAL_VALUE_TYPE result_real
, result_imag
;
1093 if (fold_const_call_cs (&result_real
, &result_imag
, fn
,
1094 TREE_REAL_CST_PTR (arg
),
1095 REAL_MODE_FORMAT (arg_mode
)))
1096 return build_complex (type
,
1097 build_real (TREE_TYPE (type
), result_real
),
1098 build_real (TREE_TYPE (type
), result_imag
));
1100 else if (INTEGRAL_TYPE_P (type
))
1104 if (fold_const_call_ss (&result
, fn
,
1105 TREE_REAL_CST_PTR (arg
),
1106 TYPE_PRECISION (type
),
1107 REAL_MODE_FORMAT (arg_mode
)))
1108 return wide_int_to_tree (type
, result
);
1113 if (complex_cst_p (arg
))
1115 gcc_checking_assert (COMPLEX_MODE_P (arg_mode
));
1116 machine_mode inner_mode
= GET_MODE_INNER (arg_mode
);
1117 tree argr
= TREE_REALPART (arg
);
1118 tree argi
= TREE_IMAGPART (arg
);
1119 if (mode
== arg_mode
1120 && real_cst_p (argr
)
1121 && real_cst_p (argi
))
1123 /* complex real -> complex real. */
1124 REAL_VALUE_TYPE result_real
, result_imag
;
1125 if (fold_const_call_cc (&result_real
, &result_imag
, fn
,
1126 TREE_REAL_CST_PTR (argr
),
1127 TREE_REAL_CST_PTR (argi
),
1128 REAL_MODE_FORMAT (inner_mode
)))
1129 return build_complex (type
,
1130 build_real (TREE_TYPE (type
), result_real
),
1131 build_real (TREE_TYPE (type
), result_imag
));
1133 if (mode
== inner_mode
1134 && real_cst_p (argr
)
1135 && real_cst_p (argi
))
1137 /* complex real -> real. */
1138 REAL_VALUE_TYPE result
;
1139 if (fold_const_call_sc (&result
, fn
,
1140 TREE_REAL_CST_PTR (argr
),
1141 TREE_REAL_CST_PTR (argi
),
1142 REAL_MODE_FORMAT (inner_mode
)))
1143 return build_real (type
, result
);
1151 /* Try to fold FN (ARG) to a constant. Return the constant on success,
1152 otherwise return null. TYPE is the type of the return value. */
1155 fold_const_call (combined_fn fn
, tree type
, tree arg
)
1159 case CFN_BUILT_IN_STRLEN
:
1160 if (const char *str
= c_getstr (arg
))
1161 return build_int_cst (type
, strlen (str
));
1165 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN
):
1166 case CFN_BUILT_IN_NAND32
:
1167 case CFN_BUILT_IN_NAND64
:
1168 case CFN_BUILT_IN_NAND128
:
1169 return fold_const_builtin_nan (type
, arg
, true);
1172 CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS
):
1173 return fold_const_builtin_nan (type
, arg
, false);
1175 case CFN_REDUC_PLUS
:
1176 return fold_const_reduction (type
, arg
, PLUS_EXPR
);
1179 return fold_const_reduction (type
, arg
, MAX_EXPR
);
1182 return fold_const_reduction (type
, arg
, MIN_EXPR
);
1185 return fold_const_call_1 (fn
, type
, arg
);
1191 *RESULT = FN (*ARG0, *ARG1)
1193 in format FORMAT. Return true on success. */
1196 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1197 const real_value
*arg0
, const real_value
*arg1
,
1198 const real_format
*format
)
1204 return do_mpfr_arg2 (result
, mpfr_remainder
, arg0
, arg1
, format
);
1207 return do_mpfr_arg2 (result
, mpfr_atan2
, arg0
, arg1
, format
);
1210 return do_mpfr_arg2 (result
, mpfr_dim
, arg0
, arg1
, format
);
1213 return do_mpfr_arg2 (result
, mpfr_hypot
, arg0
, arg1
, format
);
1216 CASE_CFN_COPYSIGN_FN
:
1218 real_copysign (result
, arg1
);
1223 return do_mpfr_arg2 (result
, mpfr_min
, arg0
, arg1
, format
);
1227 return do_mpfr_arg2 (result
, mpfr_max
, arg0
, arg1
, format
);
1230 return fold_const_pow (result
, arg0
, arg1
, format
);
1239 *RESULT = FN (*ARG0, ARG1)
1241 where FORMAT is the format of *RESULT and *ARG0. Return true on
1245 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1246 const real_value
*arg0
, const wide_int_ref
&arg1
,
1247 const real_format
*format
)
1252 return fold_const_builtin_load_exponent (result
, arg0
, arg1
, format
);
1256 return (format
->b
== 2
1257 && fold_const_builtin_load_exponent (result
, arg0
, arg1
,
1261 /* Avoid the folding if flag_signaling_nans is on and
1262 operand is a signaling NaN. */
1263 if (!flag_unsafe_math_optimizations
1264 && flag_signaling_nans
1265 && REAL_VALUE_ISSIGNALING_NAN (*arg0
))
1268 real_powi (result
, format
, arg0
, arg1
.to_shwi ());
1278 *RESULT = FN (ARG0, *ARG1)
1280 where FORMAT is the format of *RESULT and *ARG1. Return true on
1284 fold_const_call_sss (real_value
*result
, combined_fn fn
,
1285 const wide_int_ref
&arg0
, const real_value
*arg1
,
1286 const real_format
*format
)
1291 return do_mpfr_arg2 (result
, mpfr_jn
, arg0
, arg1
, format
);
1294 return (real_compare (GT_EXPR
, arg1
, &dconst0
)
1295 && do_mpfr_arg2 (result
, mpfr_yn
, arg0
, arg1
, format
));
1304 RESULT = fn (ARG0, ARG1)
1306 where FORMAT is the format of the real and imaginary parts of RESULT
1307 (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1308 and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1311 fold_const_call_ccc (real_value
*result_real
, real_value
*result_imag
,
1312 combined_fn fn
, const real_value
*arg0_real
,
1313 const real_value
*arg0_imag
, const real_value
*arg1_real
,
1314 const real_value
*arg1_imag
, const real_format
*format
)
1319 return do_mpc_arg2 (result_real
, result_imag
, mpc_pow
,
1320 arg0_real
, arg0_imag
, arg1_real
, arg1_imag
, format
);
1327 /* Subroutine of fold_const_call, with the same interface. Handle cases
1328 where the arguments and result are numerical. */
1331 fold_const_call_1 (combined_fn fn
, tree type
, tree arg0
, tree arg1
)
1333 machine_mode mode
= TYPE_MODE (type
);
1334 machine_mode arg0_mode
= TYPE_MODE (TREE_TYPE (arg0
));
1335 machine_mode arg1_mode
= TYPE_MODE (TREE_TYPE (arg1
));
1337 if (arg0_mode
== arg1_mode
1338 && real_cst_p (arg0
)
1339 && real_cst_p (arg1
))
1341 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1342 if (mode
== arg0_mode
)
1344 /* real, real -> real. */
1345 REAL_VALUE_TYPE result
;
1346 if (fold_const_call_sss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1347 TREE_REAL_CST_PTR (arg1
),
1348 REAL_MODE_FORMAT (mode
)))
1349 return build_real (type
, result
);
1354 if (real_cst_p (arg0
)
1355 && integer_cst_p (arg1
))
1357 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1358 if (mode
== arg0_mode
)
1360 /* real, int -> real. */
1361 REAL_VALUE_TYPE result
;
1362 if (fold_const_call_sss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1364 REAL_MODE_FORMAT (mode
)))
1365 return build_real (type
, result
);
1370 if (integer_cst_p (arg0
)
1371 && real_cst_p (arg1
))
1373 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode
));
1374 if (mode
== arg1_mode
)
1376 /* int, real -> real. */
1377 REAL_VALUE_TYPE result
;
1378 if (fold_const_call_sss (&result
, fn
, wi::to_wide (arg0
),
1379 TREE_REAL_CST_PTR (arg1
),
1380 REAL_MODE_FORMAT (mode
)))
1381 return build_real (type
, result
);
1386 if (arg0_mode
== arg1_mode
1387 && complex_cst_p (arg0
)
1388 && complex_cst_p (arg1
))
1390 gcc_checking_assert (COMPLEX_MODE_P (arg0_mode
));
1391 machine_mode inner_mode
= GET_MODE_INNER (arg0_mode
);
1392 tree arg0r
= TREE_REALPART (arg0
);
1393 tree arg0i
= TREE_IMAGPART (arg0
);
1394 tree arg1r
= TREE_REALPART (arg1
);
1395 tree arg1i
= TREE_IMAGPART (arg1
);
1396 if (mode
== arg0_mode
1397 && real_cst_p (arg0r
)
1398 && real_cst_p (arg0i
)
1399 && real_cst_p (arg1r
)
1400 && real_cst_p (arg1i
))
1402 /* complex real, complex real -> complex real. */
1403 REAL_VALUE_TYPE result_real
, result_imag
;
1404 if (fold_const_call_ccc (&result_real
, &result_imag
, fn
,
1405 TREE_REAL_CST_PTR (arg0r
),
1406 TREE_REAL_CST_PTR (arg0i
),
1407 TREE_REAL_CST_PTR (arg1r
),
1408 TREE_REAL_CST_PTR (arg1i
),
1409 REAL_MODE_FORMAT (inner_mode
)))
1410 return build_complex (type
,
1411 build_real (TREE_TYPE (type
), result_real
),
1412 build_real (TREE_TYPE (type
), result_imag
));
1420 /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1421 otherwise return null. TYPE is the type of the return value. */
1424 fold_const_call (combined_fn fn
, tree type
, tree arg0
, tree arg1
)
1426 const char *p0
, *p1
;
1430 case CFN_BUILT_IN_STRSPN
:
1431 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1432 return build_int_cst (type
, strspn (p0
, p1
));
1435 case CFN_BUILT_IN_STRCSPN
:
1436 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1437 return build_int_cst (type
, strcspn (p0
, p1
));
1440 case CFN_BUILT_IN_STRCMP
:
1441 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1442 return build_cmp_result (type
, strcmp (p0
, p1
));
1445 case CFN_BUILT_IN_STRCASECMP
:
1446 if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1448 int r
= strcmp (p0
, p1
);
1450 return build_cmp_result (type
, r
);
1454 case CFN_BUILT_IN_INDEX
:
1455 case CFN_BUILT_IN_STRCHR
:
1456 if ((p0
= c_getstr (arg0
)) && target_char_cst_p (arg1
, &c
))
1458 const char *r
= strchr (p0
, c
);
1460 return build_int_cst (type
, 0);
1461 return fold_convert (type
,
1462 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1466 case CFN_BUILT_IN_RINDEX
:
1467 case CFN_BUILT_IN_STRRCHR
:
1468 if ((p0
= c_getstr (arg0
)) && target_char_cst_p (arg1
, &c
))
1470 const char *r
= strrchr (p0
, c
);
1472 return build_int_cst (type
, 0);
1473 return fold_convert (type
,
1474 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1478 case CFN_BUILT_IN_STRSTR
:
1479 if ((p1
= c_getstr (arg1
)))
1481 if ((p0
= c_getstr (arg0
)))
1483 const char *r
= strstr (p0
, p1
);
1485 return build_int_cst (type
, 0);
1486 return fold_convert (type
,
1487 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1490 return fold_convert (type
, arg0
);
1495 return fold_const_call_1 (fn
, type
, arg0
, arg1
);
1501 *RESULT = FN (*ARG0, *ARG1, *ARG2)
1503 in format FORMAT. Return true on success. */
1506 fold_const_call_ssss (real_value
*result
, combined_fn fn
,
1507 const real_value
*arg0
, const real_value
*arg1
,
1508 const real_value
*arg2
, const real_format
*format
)
1514 return do_mpfr_arg3 (result
, mpfr_fma
, arg0
, arg1
, arg2
, format
);
1521 /* Subroutine of fold_const_call, with the same interface. Handle cases
1522 where the arguments and result are numerical. */
1525 fold_const_call_1 (combined_fn fn
, tree type
, tree arg0
, tree arg1
, tree arg2
)
1527 machine_mode mode
= TYPE_MODE (type
);
1528 machine_mode arg0_mode
= TYPE_MODE (TREE_TYPE (arg0
));
1529 machine_mode arg1_mode
= TYPE_MODE (TREE_TYPE (arg1
));
1530 machine_mode arg2_mode
= TYPE_MODE (TREE_TYPE (arg2
));
1532 if (arg0_mode
== arg1_mode
1533 && arg0_mode
== arg2_mode
1534 && real_cst_p (arg0
)
1535 && real_cst_p (arg1
)
1536 && real_cst_p (arg2
))
1538 gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode
));
1539 if (mode
== arg0_mode
)
1541 /* real, real, real -> real. */
1542 REAL_VALUE_TYPE result
;
1543 if (fold_const_call_ssss (&result
, fn
, TREE_REAL_CST_PTR (arg0
),
1544 TREE_REAL_CST_PTR (arg1
),
1545 TREE_REAL_CST_PTR (arg2
),
1546 REAL_MODE_FORMAT (mode
)))
1547 return build_real (type
, result
);
1555 /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
1556 success, otherwise return null. TYPE is the type of the return value. */
1559 fold_const_call (combined_fn fn
, tree type
, tree arg0
, tree arg1
, tree arg2
)
1561 const char *p0
, *p1
;
1563 unsigned HOST_WIDE_INT s0
, s1
;
1567 case CFN_BUILT_IN_STRNCMP
:
1568 if (!host_size_t_cst_p (arg2
, &s2
))
1571 && !TREE_SIDE_EFFECTS (arg0
)
1572 && !TREE_SIDE_EFFECTS (arg1
))
1573 return build_int_cst (type
, 0);
1574 else if ((p0
= c_getstr (arg0
)) && (p1
= c_getstr (arg1
)))
1575 return build_int_cst (type
, strncmp (p0
, p1
, s2
));
1578 case CFN_BUILT_IN_STRNCASECMP
:
1579 if (!host_size_t_cst_p (arg2
, &s2
))
1582 && !TREE_SIDE_EFFECTS (arg0
)
1583 && !TREE_SIDE_EFFECTS (arg1
))
1584 return build_int_cst (type
, 0);
1585 else if ((p0
= c_getstr (arg0
))
1586 && (p1
= c_getstr (arg1
))
1587 && strncmp (p0
, p1
, s2
) == 0)
1588 return build_int_cst (type
, 0);
1591 case CFN_BUILT_IN_BCMP
:
1592 case CFN_BUILT_IN_MEMCMP
:
1593 if (!host_size_t_cst_p (arg2
, &s2
))
1596 && !TREE_SIDE_EFFECTS (arg0
)
1597 && !TREE_SIDE_EFFECTS (arg1
))
1598 return build_int_cst (type
, 0);
1599 if ((p0
= c_getstr (arg0
, &s0
))
1600 && (p1
= c_getstr (arg1
, &s1
))
1603 return build_cmp_result (type
, memcmp (p0
, p1
, s2
));
1606 case CFN_BUILT_IN_MEMCHR
:
1607 if (!host_size_t_cst_p (arg2
, &s2
))
1610 && !TREE_SIDE_EFFECTS (arg0
)
1611 && !TREE_SIDE_EFFECTS (arg1
))
1612 return build_int_cst (type
, 0);
1613 if ((p0
= c_getstr (arg0
, &s0
))
1615 && target_char_cst_p (arg1
, &c
))
1617 const char *r
= (const char *) memchr (p0
, c
, s2
);
1619 return build_int_cst (type
, 0);
1620 return fold_convert (type
,
1621 fold_build_pointer_plus_hwi (arg0
, r
- p0
));
1626 return fold_const_call_1 (fn
, type
, arg0
, arg1
, arg2
);
1630 /* Fold a fma operation with arguments ARG[012]. */
1633 fold_fma (location_t
, tree type
, tree arg0
, tree arg1
, tree arg2
)
1635 REAL_VALUE_TYPE result
;
1636 if (real_cst_p (arg0
)
1637 && real_cst_p (arg1
)
1638 && real_cst_p (arg2
)
1639 && do_mpfr_arg3 (&result
, mpfr_fma
, TREE_REAL_CST_PTR (arg0
),
1640 TREE_REAL_CST_PTR (arg1
), TREE_REAL_CST_PTR (arg2
),
1641 REAL_MODE_FORMAT (TYPE_MODE (type
))))
1642 return build_real (type
, result
);