Branch data Line data Source code
1 : : /* Constant folding for calls to built-in and internal functions.
2 : : Copyright (C) 1988-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #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_VALUE_AT_ZERO. */
31 : : #include "builtins.h"
32 : : #include "gimple-expr.h"
33 : : #include "tree-vector-builder.h"
34 : :
35 : : /* Functions that test for certain constant types, abstracting away the
36 : : decision about whether to check for overflow. */
37 : :
38 : : static inline bool
39 : 34155264 : integer_cst_p (tree t)
40 : : {
41 : 34155264 : return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
42 : : }
43 : :
44 : : static inline bool
45 : 38543635 : real_cst_p (tree t)
46 : : {
47 : 38543635 : return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
48 : : }
49 : :
50 : : static inline bool
51 : 23307651 : complex_cst_p (tree t)
52 : : {
53 : 23307651 : return TREE_CODE (t) == COMPLEX_CST;
54 : : }
55 : :
56 : : /* Return true if ARG is a size_type_node constant.
57 : : Store it in *SIZE_OUT if so. */
58 : :
59 : : static inline bool
60 : 2632048 : size_t_cst_p (tree t, unsigned HOST_WIDE_INT *size_out)
61 : : {
62 : 2632048 : if (types_compatible_p (size_type_node, TREE_TYPE (t))
63 : 2631893 : && integer_cst_p (t)
64 : 4287461 : && tree_fits_uhwi_p (t))
65 : : {
66 : 1655413 : *size_out = tree_to_uhwi (t);
67 : 1655413 : return true;
68 : : }
69 : : return false;
70 : : }
71 : :
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. */
75 : :
76 : : tree
77 : 66368 : build_cmp_result (tree type, int res)
78 : : {
79 : 71080 : return build_int_cst (type, res < 0 ? -1 : res > 0 ? 1 : 0);
80 : : }
81 : :
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. */
86 : :
87 : : static bool
88 : 259087 : do_mpfr_ckconv (real_value *result, mpfr_srcptr m, bool inexact,
89 : : const real_format *format)
90 : : {
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 : 259087 : if (!mpfr_number_p (m)
95 : 243372 : || mpfr_overflow_p ()
96 : 243372 : || mpfr_underflow_p ()
97 : 498749 : || (flag_rounding_math && inexact))
98 : 19512 : return false;
99 : :
100 : 239575 : REAL_VALUE_TYPE tmp;
101 : 239575 : real_from_mpfr (&tmp, m, format, MPFR_RNDN);
102 : :
103 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
104 : : If the REAL_VALUE_TYPE is zero but the mpfr_t is not, then we
105 : : underflowed in the conversion. */
106 : 239575 : if (!real_isfinite (&tmp)
107 : 239575 : || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
108 : : return false;
109 : :
110 : 239575 : real_convert (result, format, &tmp);
111 : 239575 : return real_identical (result, &tmp);
112 : : }
113 : :
114 : : /* Try to evaluate:
115 : :
116 : : *RESULT = f (*ARG)
117 : :
118 : : in format FORMAT, given that FUNC is the MPFR implementation of f.
119 : : Return true on success. */
120 : :
121 : : static bool
122 : 121540 : 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)
125 : : {
126 : : /* To proceed, MPFR must exactly represent the target floating point
127 : : format, which only happens when the target base equals two. */
128 : 121540 : if (format->b != 2 || !real_isfinite (arg))
129 : 2862 : return false;
130 : :
131 : 118678 : int prec = format->p;
132 : 118678 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
133 : :
134 : 118678 : auto_mpfr m (prec);
135 : 118678 : mpfr_from_real (m, arg, MPFR_RNDN);
136 : 118678 : mpfr_clear_flags ();
137 : 118678 : bool inexact = func (m, m, rnd);
138 : 118678 : bool ok = do_mpfr_ckconv (result, m, inexact, format);
139 : :
140 : 118678 : return ok;
141 : 118678 : }
142 : :
143 : : /* Try to evaluate:
144 : :
145 : : *RESULT_SIN = sin (*ARG);
146 : : *RESULT_COS = cos (*ARG);
147 : :
148 : : for format FORMAT. Return true on success. */
149 : :
150 : : static bool
151 : 156 : do_mpfr_sincos (real_value *result_sin, real_value *result_cos,
152 : : const real_value *arg, const real_format *format)
153 : : {
154 : : /* To proceed, MPFR must exactly represent the target floating point
155 : : format, which only happens when the target base equals two. */
156 : 156 : if (format->b != 2 || !real_isfinite (arg))
157 : 0 : return false;
158 : :
159 : 156 : int prec = format->p;
160 : 156 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
161 : 156 : mpfr_t m, ms, mc;
162 : :
163 : 156 : mpfr_inits2 (prec, m, ms, mc, NULL);
164 : 156 : mpfr_from_real (m, arg, MPFR_RNDN);
165 : 156 : mpfr_clear_flags ();
166 : 156 : bool inexact = mpfr_sin_cos (ms, mc, m, rnd);
167 : 156 : bool ok = (do_mpfr_ckconv (result_sin, ms, inexact, format)
168 : 156 : && do_mpfr_ckconv (result_cos, mc, inexact, format));
169 : 156 : mpfr_clears (m, ms, mc, NULL);
170 : :
171 : 156 : return ok;
172 : : }
173 : :
174 : : /* Try to evaluate:
175 : :
176 : : *RESULT = f (*ARG0, *ARG1)
177 : :
178 : : in format FORMAT, given that FUNC is the MPFR implementation of f.
179 : : Return true on success. */
180 : :
181 : : static bool
182 : 147502 : do_mpfr_arg2 (real_value *result,
183 : : int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t),
184 : : const real_value *arg0, const real_value *arg1,
185 : : const real_format *format)
186 : : {
187 : : /* To proceed, MPFR must exactly represent the target floating point
188 : : format, which only happens when the target base equals two. */
189 : 147502 : if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
190 : 11684 : return false;
191 : :
192 : 135818 : int prec = format->p;
193 : 135818 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
194 : 135818 : mpfr_t m0, m1;
195 : :
196 : 135818 : mpfr_inits2 (prec, m0, m1, NULL);
197 : 135818 : mpfr_from_real (m0, arg0, MPFR_RNDN);
198 : 135818 : mpfr_from_real (m1, arg1, MPFR_RNDN);
199 : 135818 : mpfr_clear_flags ();
200 : 135818 : bool inexact = func (m0, m0, m1, rnd);
201 : 135818 : bool ok = do_mpfr_ckconv (result, m0, inexact, format);
202 : 135818 : mpfr_clears (m0, m1, NULL);
203 : :
204 : 135818 : return ok;
205 : : }
206 : :
207 : : /* Try to evaluate:
208 : :
209 : : *RESULT = f (ARG0, *ARG1)
210 : :
211 : : in format FORMAT, given that FUNC is the MPFR implementation of f.
212 : : Return true on success. */
213 : :
214 : : static bool
215 : 1743 : do_mpfr_arg2 (real_value *result,
216 : : int (*func) (mpfr_ptr, long, mpfr_srcptr, mpfr_rnd_t),
217 : : const wide_int_ref &arg0, const real_value *arg1,
218 : : const real_format *format)
219 : : {
220 : 1743 : if (format->b != 2 || !real_isfinite (arg1))
221 : 0 : return false;
222 : :
223 : 1743 : int prec = format->p;
224 : 1743 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
225 : :
226 : 1743 : auto_mpfr m (prec);
227 : 1743 : mpfr_from_real (m, arg1, MPFR_RNDN);
228 : 1743 : mpfr_clear_flags ();
229 : 1743 : bool inexact = func (m, arg0.to_shwi (), m, rnd);
230 : 1743 : bool ok = do_mpfr_ckconv (result, m, inexact, format);
231 : :
232 : 1743 : return ok;
233 : 1743 : }
234 : :
235 : : /* Try to evaluate:
236 : :
237 : : *RESULT = f (*ARG0, *ARG1, *ARG2)
238 : :
239 : : in format FORMAT, given that FUNC is the MPFR implementation of f.
240 : : Return true on success. */
241 : :
242 : : static bool
243 : 2536 : 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)
248 : : {
249 : : /* To proceed, MPFR must exactly represent the target floating point
250 : : format, which only happens when the target base equals two. */
251 : 2536 : if (format->b != 2
252 : 2536 : || !real_isfinite (arg0)
253 : 2536 : || !real_isfinite (arg1)
254 : 5072 : || !real_isfinite (arg2))
255 : 0 : return false;
256 : :
257 : 2536 : int prec = format->p;
258 : 2536 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
259 : 2536 : mpfr_t m0, m1, m2;
260 : :
261 : 2536 : mpfr_inits2 (prec, m0, m1, m2, NULL);
262 : 2536 : mpfr_from_real (m0, arg0, MPFR_RNDN);
263 : 2536 : mpfr_from_real (m1, arg1, MPFR_RNDN);
264 : 2536 : mpfr_from_real (m2, arg2, MPFR_RNDN);
265 : 2536 : mpfr_clear_flags ();
266 : 2536 : bool inexact = func (m0, m0, m1, m2, rnd);
267 : 2536 : bool ok = do_mpfr_ckconv (result, m0, inexact, format);
268 : 2536 : mpfr_clears (m0, m1, m2, NULL);
269 : :
270 : 2536 : return ok;
271 : : }
272 : :
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. */
278 : :
279 : : static bool
280 : 39649 : do_mpc_ckconv (real_value *result_real, real_value *result_imag,
281 : : mpc_srcptr m, bool inexact, const real_format *format)
282 : : {
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 : 39649 : if (!mpfr_number_p (mpc_realref (m))
287 : 33847 : || !mpfr_number_p (mpc_imagref (m))
288 : 33847 : || mpfr_overflow_p ()
289 : 33847 : || mpfr_underflow_p ()
290 : 69692 : || (flag_rounding_math && inexact))
291 : 9606 : return false;
292 : :
293 : 30043 : REAL_VALUE_TYPE tmp_real, tmp_imag;
294 : 30043 : real_from_mpfr (&tmp_real, mpc_realref (m), format, MPFR_RNDN);
295 : 30043 : real_from_mpfr (&tmp_imag, mpc_imagref (m), format, MPFR_RNDN);
296 : :
297 : : /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
298 : : If the REAL_VALUE_TYPE is zero but the mpfr_t is not, then we
299 : : underflowed in the conversion. */
300 : 30043 : if (!real_isfinite (&tmp_real)
301 : 30043 : || !real_isfinite (&tmp_imag)
302 : 30043 : || (tmp_real.cl == rvc_zero) != (mpfr_zero_p (mpc_realref (m)) != 0)
303 : 60086 : || (tmp_imag.cl == rvc_zero) != (mpfr_zero_p (mpc_imagref (m)) != 0))
304 : 0 : return false;
305 : :
306 : 30043 : real_convert (result_real, format, &tmp_real);
307 : 30043 : real_convert (result_imag, format, &tmp_imag);
308 : :
309 : 30043 : return (real_identical (result_real, &tmp_real)
310 : 30043 : && real_identical (result_imag, &tmp_imag));
311 : : }
312 : :
313 : : /* Try to evaluate:
314 : :
315 : : RESULT = f (ARG)
316 : :
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. */
320 : :
321 : : static bool
322 : 13884 : 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)
326 : : {
327 : : /* To proceed, MPFR must exactly represent the target floating point
328 : : format, which only happens when the target base equals two. */
329 : 13884 : if (format->b != 2
330 : 13884 : || !real_isfinite (arg_real)
331 : 25338 : || !real_isfinite (arg_imag))
332 : 2430 : return false;
333 : :
334 : 11454 : int prec = format->p;
335 : 11454 : mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
336 : 11454 : mpc_t m;
337 : :
338 : 11454 : mpc_init2 (m, prec);
339 : 11454 : mpfr_from_real (mpc_realref (m), arg_real, MPFR_RNDN);
340 : 11454 : mpfr_from_real (mpc_imagref (m), arg_imag, MPFR_RNDN);
341 : 11454 : mpfr_clear_flags ();
342 : 11454 : bool inexact = func (m, m, crnd);
343 : 11454 : bool ok = do_mpc_ckconv (result_real, result_imag, m, inexact, format);
344 : 11454 : mpc_clear (m);
345 : :
346 : 11454 : return ok;
347 : : }
348 : :
349 : : /* Try to evaluate:
350 : :
351 : : RESULT = f (ARG0, ARG1)
352 : :
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. */
356 : :
357 : : static bool
358 : 33055 : 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)
363 : : {
364 : 33055 : if (!real_isfinite (arg0_real)
365 : 30625 : || !real_isfinite (arg0_imag)
366 : 30625 : || !real_isfinite (arg1_real)
367 : 61250 : || !real_isfinite (arg1_imag))
368 : 4860 : return false;
369 : :
370 : 28195 : int prec = format->p;
371 : 28195 : mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
372 : 28195 : mpc_t m0, m1;
373 : :
374 : 28195 : mpc_init2 (m0, prec);
375 : 28195 : mpc_init2 (m1, prec);
376 : 28195 : mpfr_from_real (mpc_realref (m0), arg0_real, MPFR_RNDN);
377 : 28195 : mpfr_from_real (mpc_imagref (m0), arg0_imag, MPFR_RNDN);
378 : 28195 : mpfr_from_real (mpc_realref (m1), arg1_real, MPFR_RNDN);
379 : 28195 : mpfr_from_real (mpc_imagref (m1), arg1_imag, MPFR_RNDN);
380 : 28195 : mpfr_clear_flags ();
381 : 28195 : bool inexact = func (m0, m0, m1, crnd);
382 : 28195 : bool ok = do_mpc_ckconv (result_real, result_imag, m0, inexact, format);
383 : 28195 : mpc_clear (m0);
384 : 28195 : mpc_clear (m1);
385 : :
386 : 28195 : return ok;
387 : : }
388 : :
389 : : /* Try to evaluate:
390 : :
391 : : *RESULT = logb (*ARG)
392 : :
393 : : in format FORMAT. Return true on success. */
394 : :
395 : : static bool
396 : 4600 : fold_const_logb (real_value *result, const real_value *arg,
397 : : const real_format *format)
398 : : {
399 : 4600 : switch (arg->cl)
400 : : {
401 : 106 : case rvc_nan:
402 : : /* If arg is +-NaN, then return it. */
403 : 106 : *result = *arg;
404 : 106 : return true;
405 : :
406 : 116 : case rvc_inf:
407 : : /* If arg is +-Inf, then return +Inf. */
408 : 116 : *result = *arg;
409 : 116 : result->sign = 0;
410 : 116 : return true;
411 : :
412 : : case rvc_zero:
413 : : /* Zero may set errno and/or raise an exception. */
414 : : return false;
415 : :
416 : 1670 : 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 : 1670 : if (format->b == 2)
422 : : {
423 : 1670 : real_from_integer (result, format, REAL_EXP (arg) - 1, SIGNED);
424 : 1670 : return true;
425 : : }
426 : : return false;
427 : : }
428 : : }
429 : :
430 : : /* Try to evaluate:
431 : :
432 : : *RESULT = significand (*ARG)
433 : :
434 : : in format FORMAT. Return true on success. */
435 : :
436 : : static bool
437 : 960 : fold_const_significand (real_value *result, const real_value *arg,
438 : : const real_format *format)
439 : : {
440 : 0 : switch (arg->cl)
441 : : {
442 : 288 : case rvc_zero:
443 : 288 : case rvc_nan:
444 : 288 : case rvc_inf:
445 : : /* If arg is +-0, +-Inf or +-NaN, then return it. */
446 : 288 : *result = *arg;
447 : 288 : return true;
448 : :
449 : 672 : case rvc_normal:
450 : : /* For normal numbers, proceed iff radix == 2. */
451 : 672 : if (format->b == 2)
452 : : {
453 : 672 : *result = *arg;
454 : : /* In GCC, normalized significands are in the range [0.5, 1.0).
455 : : We want them to be [1.0, 2.0) so set the exponent to 1. */
456 : 672 : SET_REAL_EXP (result, 1);
457 : 672 : return true;
458 : : }
459 : : return false;
460 : : }
461 : : }
462 : :
463 : : /* Try to evaluate:
464 : :
465 : : *RESULT = f (*ARG)
466 : :
467 : : where FORMAT is the format of *ARG and PRECISION is the number of
468 : : significant bits in the result. Return true on success. */
469 : :
470 : : static bool
471 : 1640 : fold_const_conversion (wide_int *result,
472 : : void (*fn) (real_value *, format_helper,
473 : : const real_value *),
474 : : const real_value *arg, unsigned int precision,
475 : : const real_format *format)
476 : : {
477 : 1640 : if (!real_isfinite (arg))
478 : : return false;
479 : :
480 : 1640 : real_value rounded;
481 : 1640 : fn (&rounded, format, arg);
482 : :
483 : 1640 : bool fail = false;
484 : 1640 : *result = real_to_integer (&rounded, &fail, precision);
485 : 1640 : return !fail;
486 : : }
487 : :
488 : : /* Try to evaluate:
489 : :
490 : : *RESULT = pow (*ARG0, *ARG1)
491 : :
492 : : in format FORMAT. Return true on success. */
493 : :
494 : : static bool
495 : 129953 : fold_const_pow (real_value *result, const real_value *arg0,
496 : : const real_value *arg1, const real_format *format)
497 : : {
498 : 129953 : if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
499 : : return true;
500 : :
501 : : /* Check for an integer exponent. */
502 : 10749 : REAL_VALUE_TYPE cint1;
503 : 10749 : HOST_WIDE_INT n1 = real_to_integer (arg1);
504 : 10749 : real_from_integer (&cint1, VOIDmode, n1, SIGNED);
505 : : /* Attempt to evaluate pow at compile-time, unless this should
506 : : raise an exception. */
507 : 10749 : if (real_identical (arg1, &cint1)
508 : 10749 : && (n1 > 0
509 : 810 : || (!flag_trapping_math && !flag_errno_math)
510 : 810 : || !real_equal (arg0, &dconst0)))
511 : : {
512 : 1620 : bool inexact = real_powi (result, format, arg0, n1);
513 : : /* Avoid the folding if flag_signaling_nans is on. */
514 : 1620 : if (flag_unsafe_math_optimizations
515 : 1620 : || (!inexact
516 : 0 : && !(flag_signaling_nans
517 : 0 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))))
518 : 0 : return true;
519 : : }
520 : :
521 : : return false;
522 : : }
523 : :
524 : : /* Try to evaluate:
525 : :
526 : : *RESULT = nextafter (*ARG0, *ARG1)
527 : :
528 : : or
529 : :
530 : : *RESULT = nexttoward (*ARG0, *ARG1)
531 : :
532 : : in format FORMAT. Return true on success. */
533 : :
534 : : static bool
535 : 15313 : fold_const_nextafter (real_value *result, const real_value *arg0,
536 : : const real_value *arg1, const real_format *format)
537 : : {
538 : 15313 : if (REAL_VALUE_ISSIGNALING_NAN (*arg0)
539 : 15313 : || REAL_VALUE_ISSIGNALING_NAN (*arg1))
540 : 0 : return false;
541 : :
542 : : /* Don't handle composite modes, nor decimal, nor modes without
543 : : inf or denorm at least for now. */
544 : 15313 : if (format->pnan < format->p
545 : 15313 : || format->b == 10
546 : 15313 : || !format->has_inf
547 : 15313 : || !format->has_denorm)
548 : : return false;
549 : :
550 : 23953 : if (real_nextafter (result, format, arg0, arg1)
551 : : /* If raising underflow or overflow and setting errno to ERANGE,
552 : : fail if we care about those side-effects. */
553 : 15313 : && (flag_trapping_math || flag_errno_math))
554 : 8640 : return false;
555 : : /* Similarly for nextafter (0, 1) raising underflow. */
556 : 6673 : else if (flag_trapping_math
557 : 6166 : && arg0->cl == rvc_zero
558 : 5422 : && result->cl != rvc_zero)
559 : : return false;
560 : :
561 : 1431 : real_convert (result, format, result);
562 : :
563 : 1431 : return true;
564 : : }
565 : :
566 : : /* Try to evaluate:
567 : :
568 : : *RESULT = ldexp (*ARG0, ARG1)
569 : :
570 : : in format FORMAT. Return true on success. */
571 : :
572 : : static bool
573 : 26180 : fold_const_builtin_load_exponent (real_value *result, const real_value *arg0,
574 : : const wide_int_ref &arg1,
575 : : const real_format *format)
576 : : {
577 : : /* Bound the maximum adjustment to twice the range of the
578 : : mode's valid exponents. Use abs to ensure the range is
579 : : positive as a sanity check. */
580 : 26180 : int max_exp_adj = 2 * labs (format->emax - format->emin);
581 : :
582 : : /* The requested adjustment must be inside this range. This
583 : : is a preliminary cap to avoid things like overflow, we
584 : : may still fail to compute the result for other reasons. */
585 : 44222 : if (wi::les_p (arg1, -max_exp_adj) || wi::ges_p (arg1, max_exp_adj))
586 : 16144 : return false;
587 : :
588 : : /* Don't perform operation if we honor signaling NaNs and
589 : : operand is a signaling NaN. */
590 : 10036 : if (!flag_unsafe_math_optimizations
591 : 10033 : && flag_signaling_nans
592 : 10156 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
593 : : return false;
594 : :
595 : 10036 : REAL_VALUE_TYPE initial_result;
596 : 10036 : real_ldexp (&initial_result, arg0, arg1.to_shwi ());
597 : :
598 : : /* Ensure we didn't overflow. */
599 : 10036 : if (real_isinf (&initial_result))
600 : : return false;
601 : :
602 : : /* Only proceed if the target mode can hold the
603 : : resulting value. */
604 : 10016 : *result = real_value_truncate (format, initial_result);
605 : 10016 : return real_equal (&initial_result, result);
606 : : }
607 : :
608 : : /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
609 : : return type TYPE. QUIET is true if a quiet rather than signalling
610 : : NaN is required. */
611 : :
612 : : static tree
613 : 319367 : fold_const_builtin_nan (tree type, tree arg, bool quiet)
614 : : {
615 : 319367 : REAL_VALUE_TYPE real;
616 : 319367 : const char *str = c_getstr (arg);
617 : 319367 : if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
618 : 318892 : return build_real (type, real);
619 : : return NULL_TREE;
620 : : }
621 : :
622 : : /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE. */
623 : :
624 : : static tree
625 : 1992 : fold_const_reduction (tree type, tree arg, tree_code code)
626 : : {
627 : 1992 : unsigned HOST_WIDE_INT nelts;
628 : 1992 : if (TREE_CODE (arg) != VECTOR_CST
629 : 1992 : || !VECTOR_CST_NELTS (arg).is_constant (&nelts))
630 : 0 : return NULL_TREE;
631 : :
632 : 1992 : tree res = VECTOR_CST_ELT (arg, 0);
633 : 25544 : for (unsigned HOST_WIDE_INT i = 1; i < nelts; i++)
634 : : {
635 : 23552 : res = const_binop (code, type, res, VECTOR_CST_ELT (arg, i));
636 : 23552 : if (res == NULL_TREE || !CONSTANT_CLASS_P (res))
637 : : return NULL_TREE;
638 : : }
639 : : return res;
640 : : }
641 : :
642 : : /* Fold a call to IFN_VEC_CONVERT (ARG) returning TYPE. */
643 : :
644 : : static tree
645 : 138 : fold_const_vec_convert (tree ret_type, tree arg)
646 : : {
647 : 138 : enum tree_code code = NOP_EXPR;
648 : 138 : tree arg_type = TREE_TYPE (arg);
649 : 138 : if (TREE_CODE (arg) != VECTOR_CST)
650 : : return NULL_TREE;
651 : :
652 : 138 : gcc_checking_assert (VECTOR_TYPE_P (ret_type) && VECTOR_TYPE_P (arg_type));
653 : :
654 : 276 : if (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
655 : 211 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg_type)))
656 : : code = FIX_TRUNC_EXPR;
657 : 170 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
658 : 170 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (ret_type)))
659 : : code = FLOAT_EXPR;
660 : :
661 : : /* We can't handle steps directly when extending, since the
662 : : values need to wrap at the original precision first. */
663 : 138 : bool step_ok_p
664 : 276 : = (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
665 : 73 : && INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
666 : 158 : && (TYPE_PRECISION (TREE_TYPE (ret_type))
667 : 20 : <= TYPE_PRECISION (TREE_TYPE (arg_type))));
668 : 138 : tree_vector_builder elts;
669 : 138 : if (!elts.new_unary_operation (ret_type, arg, step_ok_p))
670 : : return NULL_TREE;
671 : :
672 : 138 : unsigned int count = elts.encoded_nelts ();
673 : 654 : for (unsigned int i = 0; i < count; ++i)
674 : : {
675 : 522 : tree elt = fold_unary (code, TREE_TYPE (ret_type),
676 : : VECTOR_CST_ELT (arg, i));
677 : 522 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
678 : 6 : return NULL_TREE;
679 : 516 : elts.quick_push (elt);
680 : : }
681 : :
682 : 132 : return elts.build ();
683 : 138 : }
684 : :
685 : : /* Try to evaluate:
686 : :
687 : : IFN_WHILE_ULT (ARG0, ARG1, (TYPE) { ... })
688 : :
689 : : Return the value on success and null on failure. */
690 : :
691 : : static tree
692 : 0 : fold_while_ult (tree type, poly_uint64 arg0, poly_uint64 arg1)
693 : : {
694 : 0 : if (known_ge (arg0, arg1))
695 : 0 : return build_zero_cst (type);
696 : :
697 : 0 : if (maybe_ge (arg0, arg1))
698 : : return NULL_TREE;
699 : :
700 : 0 : poly_uint64 diff = arg1 - arg0;
701 : 0 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
702 : 0 : if (known_ge (diff, nelts))
703 : 0 : return build_all_ones_cst (type);
704 : :
705 : 0 : unsigned HOST_WIDE_INT const_diff;
706 : 0 : if (known_le (diff, nelts) && diff.is_constant (&const_diff))
707 : : {
708 : 0 : tree minus_one = build_minus_one_cst (TREE_TYPE (type));
709 : 0 : tree zero = build_zero_cst (TREE_TYPE (type));
710 : 0 : return build_vector_a_then_b (type, const_diff, minus_one, zero);
711 : : }
712 : : return NULL_TREE;
713 : : }
714 : :
715 : : /* Try to evaluate:
716 : :
717 : : *RESULT = FN (*ARG)
718 : :
719 : : in format FORMAT. Return true on success. */
720 : :
721 : : static bool
722 : 175768 : fold_const_call_ss (real_value *result, combined_fn fn,
723 : : const real_value *arg, const real_format *format)
724 : : {
725 : 175768 : switch (fn)
726 : : {
727 : 23448 : CASE_CFN_SQRT:
728 : 23448 : CASE_CFN_SQRT_FN:
729 : 23448 : return (real_compare (GE_EXPR, arg, &dconst0)
730 : 23448 : && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
731 : :
732 : 512 : CASE_CFN_CBRT:
733 : 512 : CASE_CFN_CBRT_FN:
734 : 512 : return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
735 : :
736 : 2043 : CASE_CFN_ASIN:
737 : 2043 : CASE_CFN_ASIN_FN:
738 : 2043 : return (real_compare (GE_EXPR, arg, &dconstm1)
739 : 1233 : && real_compare (LE_EXPR, arg, &dconst1)
740 : 2458 : && do_mpfr_arg1 (result, mpfr_asin, arg, format));
741 : :
742 : 2094 : CASE_CFN_ACOS:
743 : 2094 : CASE_CFN_ACOS_FN:
744 : 2094 : return (real_compare (GE_EXPR, arg, &dconstm1)
745 : 1284 : && real_compare (LE_EXPR, arg, &dconst1)
746 : 2568 : && do_mpfr_arg1 (result, mpfr_acos, arg, format));
747 : :
748 : 551 : CASE_CFN_ATAN:
749 : 551 : CASE_CFN_ATAN_FN:
750 : 551 : return do_mpfr_arg1 (result, mpfr_atan, arg, format);
751 : :
752 : 312 : CASE_CFN_ASINH:
753 : 312 : CASE_CFN_ASINH_FN:
754 : 312 : return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
755 : :
756 : 1532 : CASE_CFN_ACOSH:
757 : 1532 : CASE_CFN_ACOSH_FN:
758 : 1532 : return (real_compare (GE_EXPR, arg, &dconst1)
759 : 1532 : && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
760 : :
761 : 1989 : CASE_CFN_ATANH:
762 : 1989 : CASE_CFN_ATANH_FN:
763 : 1989 : return (real_compare (GE_EXPR, arg, &dconstm1)
764 : 1989 : && real_compare (LE_EXPR, arg, &dconst1)
765 : 3978 : && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
766 : :
767 : 1019 : CASE_CFN_SIN:
768 : 1019 : CASE_CFN_SIN_FN:
769 : 1019 : return do_mpfr_arg1 (result, mpfr_sin, arg, format);
770 : :
771 : 845 : CASE_CFN_COS:
772 : 845 : CASE_CFN_COS_FN:
773 : 845 : return do_mpfr_arg1 (result, mpfr_cos, arg, format);
774 : :
775 : 557 : CASE_CFN_TAN:
776 : 557 : CASE_CFN_TAN_FN:
777 : 557 : return do_mpfr_arg1 (result, mpfr_tan, arg, format);
778 : :
779 : 394 : CASE_CFN_SINH:
780 : 394 : CASE_CFN_SINH_FN:
781 : 394 : return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
782 : :
783 : 394 : CASE_CFN_COSH:
784 : 394 : CASE_CFN_COSH_FN:
785 : 394 : return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
786 : :
787 : 349 : CASE_CFN_TANH:
788 : 349 : CASE_CFN_TANH_FN:
789 : 349 : return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
790 : :
791 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
792 : 55 : CASE_CFN_ACOSPI:
793 : 55 : CASE_CFN_ACOSPI_FN:
794 : 55 : return (real_compare (GE_EXPR, arg, &dconstm1)
795 : 55 : && real_compare (LE_EXPR, arg, &dconst1)
796 : 110 : && do_mpfr_arg1 (result, mpfr_acospi, arg, format));
797 : :
798 : 60 : CASE_CFN_ASINPI:
799 : 60 : CASE_CFN_ASINPI_FN:
800 : 60 : return (real_compare (GE_EXPR, arg, &dconstm1)
801 : 60 : && real_compare (LE_EXPR, arg, &dconst1)
802 : 120 : && do_mpfr_arg1 (result, mpfr_asinpi, arg, format));
803 : :
804 : 36 : CASE_CFN_ATANPI:
805 : 36 : CASE_CFN_ATANPI_FN:
806 : 36 : return do_mpfr_arg1 (result, mpfr_atanpi, arg, format);
807 : :
808 : 60 : CASE_CFN_COSPI:
809 : 60 : CASE_CFN_COSPI_FN:
810 : 60 : return do_mpfr_arg1 (result, mpfr_cospi, arg, format);
811 : :
812 : 60 : CASE_CFN_SINPI:
813 : 60 : CASE_CFN_SINPI_FN:
814 : 60 : return do_mpfr_arg1 (result, mpfr_sinpi, arg, format);
815 : :
816 : 72 : CASE_CFN_TANPI:
817 : 72 : CASE_CFN_TANPI_FN:
818 : 72 : return do_mpfr_arg1 (result, mpfr_tanpi, arg, format);
819 : : #endif
820 : :
821 : 519 : CASE_CFN_ERF:
822 : 519 : CASE_CFN_ERF_FN:
823 : 519 : return do_mpfr_arg1 (result, mpfr_erf, arg, format);
824 : :
825 : 558 : CASE_CFN_ERFC:
826 : 558 : CASE_CFN_ERFC_FN:
827 : 558 : return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
828 : :
829 : 5104 : CASE_CFN_TGAMMA:
830 : 5104 : CASE_CFN_TGAMMA_FN:
831 : 5104 : return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
832 : :
833 : 871 : CASE_CFN_EXP:
834 : 871 : CASE_CFN_EXP_FN:
835 : 871 : return do_mpfr_arg1 (result, mpfr_exp, arg, format);
836 : :
837 : 11110 : CASE_CFN_EXP2:
838 : 11110 : CASE_CFN_EXP2_FN:
839 : 11110 : return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
840 : :
841 : 698 : CASE_CFN_EXP10:
842 : 698 : CASE_CFN_POW10:
843 : 698 : return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
844 : :
845 : 327 : CASE_CFN_EXPM1:
846 : 327 : CASE_CFN_EXPM1_FN:
847 : 327 : return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
848 : :
849 : 72437 : CASE_CFN_LOG:
850 : 72437 : CASE_CFN_LOG_FN:
851 : 72437 : return (real_compare (GT_EXPR, arg, &dconst0)
852 : 72437 : && do_mpfr_arg1 (result, mpfr_log, arg, format));
853 : :
854 : 3276 : CASE_CFN_LOG2:
855 : 3276 : CASE_CFN_LOG2_FN:
856 : 3276 : return (real_compare (GT_EXPR, arg, &dconst0)
857 : 3276 : && do_mpfr_arg1 (result, mpfr_log2, arg, format));
858 : :
859 : 3273 : CASE_CFN_LOG10:
860 : 3273 : CASE_CFN_LOG10_FN:
861 : 3273 : return (real_compare (GT_EXPR, arg, &dconst0)
862 : 3273 : && do_mpfr_arg1 (result, mpfr_log10, arg, format));
863 : :
864 : 1993 : CASE_CFN_LOG1P:
865 : 1993 : CASE_CFN_LOG1P_FN:
866 : 1993 : return (real_compare (GT_EXPR, arg, &dconstm1)
867 : 1993 : && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
868 : :
869 : 221 : CASE_CFN_J0:
870 : 221 : return do_mpfr_arg1 (result, mpfr_j0, arg, format);
871 : :
872 : 221 : CASE_CFN_J1:
873 : 221 : return do_mpfr_arg1 (result, mpfr_j1, arg, format);
874 : :
875 : 2555 : CASE_CFN_Y0:
876 : 2555 : return (real_compare (GT_EXPR, arg, &dconst0)
877 : 2555 : && do_mpfr_arg1 (result, mpfr_y0, arg, format));
878 : :
879 : 2555 : CASE_CFN_Y1:
880 : 2555 : return (real_compare (GT_EXPR, arg, &dconst0)
881 : 2555 : && do_mpfr_arg1 (result, mpfr_y1, arg, format));
882 : :
883 : 365 : CASE_CFN_FLOOR:
884 : 365 : CASE_CFN_FLOOR_FN:
885 : 365 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
886 : : {
887 : 365 : real_floor (result, format, arg);
888 : 365 : return true;
889 : : }
890 : : return false;
891 : :
892 : 323 : CASE_CFN_CEIL:
893 : 323 : CASE_CFN_CEIL_FN:
894 : 323 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
895 : : {
896 : 323 : real_ceil (result, format, arg);
897 : 323 : return true;
898 : : }
899 : : return false;
900 : :
901 : 534 : CASE_CFN_TRUNC:
902 : 534 : CASE_CFN_TRUNC_FN:
903 : 534 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
904 : : {
905 : 534 : real_trunc (result, format, arg);
906 : 534 : return true;
907 : : }
908 : : return false;
909 : :
910 : 275 : CASE_CFN_ROUND:
911 : 275 : CASE_CFN_ROUND_FN:
912 : 275 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
913 : : {
914 : 275 : real_round (result, format, arg);
915 : 275 : return true;
916 : : }
917 : : return false;
918 : :
919 : 189 : CASE_CFN_ROUNDEVEN:
920 : 189 : CASE_CFN_ROUNDEVEN_FN:
921 : 189 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
922 : : {
923 : 189 : real_roundeven (result, format, arg);
924 : 189 : return true;
925 : : }
926 : : return false;
927 : :
928 : 4600 : CASE_CFN_LOGB:
929 : 4600 : CASE_CFN_LOGB_FN:
930 : 4600 : return fold_const_logb (result, arg, format);
931 : :
932 : 960 : CASE_CFN_SIGNIFICAND:
933 : 960 : return fold_const_significand (result, arg, format);
934 : :
935 : : default:
936 : : return false;
937 : : }
938 : : }
939 : :
940 : : /* Try to evaluate:
941 : :
942 : : *RESULT = FN (*ARG)
943 : :
944 : : where FORMAT is the format of ARG and PRECISION is the number of
945 : : significant bits in the result. Return true on success. */
946 : :
947 : : static bool
948 : 17973 : fold_const_call_ss (wide_int *result, combined_fn fn,
949 : : const real_value *arg, unsigned int precision,
950 : : const real_format *format)
951 : : {
952 : 17973 : switch (fn)
953 : : {
954 : 1885 : CASE_CFN_SIGNBIT:
955 : 1885 : if (real_isneg (arg))
956 : 1011 : *result = wi::one (precision);
957 : : else
958 : 874 : *result = wi::zero (precision);
959 : : return true;
960 : :
961 : 6755 : CASE_CFN_ILOGB:
962 : 6755 : CASE_CFN_ILOGB_FN:
963 : : /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
964 : : Proceed iff radix == 2. In GCC, normalized significands are in
965 : : the range [0.5, 1.0). We want the exponent as if they were
966 : : [1.0, 2.0) so get the exponent and subtract 1. */
967 : 6755 : if (arg->cl == rvc_normal && format->b == 2)
968 : : {
969 : 834 : *result = wi::shwi (REAL_EXP (arg) - 1, precision);
970 : 834 : return true;
971 : : }
972 : : return false;
973 : :
974 : 378 : CASE_CFN_ICEIL:
975 : 378 : CASE_CFN_LCEIL:
976 : 378 : CASE_CFN_LLCEIL:
977 : 378 : return fold_const_conversion (result, real_ceil, arg,
978 : 378 : precision, format);
979 : :
980 : 378 : CASE_CFN_LFLOOR:
981 : 378 : CASE_CFN_IFLOOR:
982 : 378 : CASE_CFN_LLFLOOR:
983 : 378 : return fold_const_conversion (result, real_floor, arg,
984 : 378 : precision, format);
985 : :
986 : 884 : CASE_CFN_IROUND:
987 : 884 : CASE_CFN_LROUND:
988 : 884 : CASE_CFN_LROUND_FN:
989 : 884 : CASE_CFN_LLROUND:
990 : 884 : CASE_CFN_LLROUND_FN:
991 : 884 : return fold_const_conversion (result, real_round, arg,
992 : 884 : precision, format);
993 : :
994 : : CASE_CFN_IRINT:
995 : : CASE_CFN_LRINT:
996 : : CASE_CFN_LRINT_FN:
997 : : CASE_CFN_LLRINT:
998 : : CASE_CFN_LLRINT_FN:
999 : : /* Not yet folded to a constant. */
1000 : : return false;
1001 : :
1002 : 1479 : CASE_CFN_FINITE:
1003 : 1479 : case CFN_BUILT_IN_FINITED32:
1004 : 1479 : case CFN_BUILT_IN_FINITED64:
1005 : 1479 : case CFN_BUILT_IN_FINITED128:
1006 : 1479 : case CFN_BUILT_IN_ISFINITE:
1007 : 1513 : *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
1008 : 1479 : return true;
1009 : :
1010 : 3808 : case CFN_BUILT_IN_ISSIGNALING:
1011 : 7445 : *result = wi::shwi (real_issignaling_nan (arg) ? 1 : 0, precision);
1012 : 3808 : return true;
1013 : :
1014 : 204 : CASE_CFN_ISINF:
1015 : 204 : case CFN_BUILT_IN_ISINFD32:
1016 : 204 : case CFN_BUILT_IN_ISINFD64:
1017 : 204 : case CFN_BUILT_IN_ISINFD128:
1018 : 204 : if (real_isinf (arg))
1019 : 295 : *result = wi::shwi (arg->sign ? -1 : 1, precision);
1020 : : else
1021 : 40 : *result = wi::shwi (0, precision);
1022 : : return true;
1023 : :
1024 : 1347 : CASE_CFN_ISNAN:
1025 : 1347 : case CFN_BUILT_IN_ISNAND32:
1026 : 1347 : case CFN_BUILT_IN_ISNAND64:
1027 : 1347 : case CFN_BUILT_IN_ISNAND128:
1028 : 2462 : *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
1029 : 1347 : return true;
1030 : :
1031 : : default:
1032 : : return false;
1033 : : }
1034 : : }
1035 : :
1036 : : /* Try to evaluate:
1037 : :
1038 : : *RESULT = FN (ARG)
1039 : :
1040 : : where ARG_TYPE is the type of ARG and PRECISION is the number of bits
1041 : : in the result. Return true on success. */
1042 : :
1043 : : static bool
1044 : 3365145 : fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
1045 : : unsigned int precision, tree arg_type)
1046 : : {
1047 : 3365145 : switch (fn)
1048 : : {
1049 : 493 : CASE_CFN_FFS:
1050 : 493 : case CFN_BUILT_IN_FFSG:
1051 : 493 : *result = wi::shwi (wi::ffs (arg), precision);
1052 : 493 : return true;
1053 : :
1054 : 1948 : CASE_CFN_CLZ:
1055 : 1948 : case CFN_BUILT_IN_CLZG:
1056 : 1948 : {
1057 : 1948 : int tmp;
1058 : 1948 : if (wi::ne_p (arg, 0))
1059 : 1609 : tmp = wi::clz (arg);
1060 : 339 : else if (TREE_CODE (arg_type) == BITINT_TYPE)
1061 : 9 : tmp = TYPE_PRECISION (arg_type);
1062 : 660 : else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1063 : : tmp))
1064 : 330 : tmp = TYPE_PRECISION (arg_type);
1065 : 1948 : *result = wi::shwi (tmp, precision);
1066 : 1948 : return true;
1067 : : }
1068 : :
1069 : 787 : CASE_CFN_CTZ:
1070 : 787 : case CFN_BUILT_IN_CTZG:
1071 : 787 : {
1072 : 787 : int tmp;
1073 : 787 : if (wi::ne_p (arg, 0))
1074 : 542 : tmp = wi::ctz (arg);
1075 : 245 : else if (TREE_CODE (arg_type) == BITINT_TYPE)
1076 : 0 : tmp = TYPE_PRECISION (arg_type);
1077 : 490 : else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1078 : : tmp))
1079 : 245 : tmp = TYPE_PRECISION (arg_type);
1080 : 787 : *result = wi::shwi (tmp, precision);
1081 : 787 : return true;
1082 : : }
1083 : :
1084 : 412 : CASE_CFN_CLRSB:
1085 : 412 : case CFN_BUILT_IN_CLRSBG:
1086 : 412 : *result = wi::shwi (wi::clrsb (arg), precision);
1087 : 412 : return true;
1088 : :
1089 : 2052 : CASE_CFN_POPCOUNT:
1090 : 2052 : case CFN_BUILT_IN_POPCOUNTG:
1091 : 2052 : *result = wi::shwi (wi::popcount (arg), precision);
1092 : 2052 : return true;
1093 : :
1094 : 545 : CASE_CFN_PARITY:
1095 : 545 : case CFN_BUILT_IN_PARITYG:
1096 : 545 : *result = wi::shwi (wi::parity (arg), precision);
1097 : 545 : return true;
1098 : :
1099 : 746 : case CFN_BUILT_IN_BSWAP16:
1100 : 746 : case CFN_BUILT_IN_BSWAP32:
1101 : 746 : case CFN_BUILT_IN_BSWAP64:
1102 : 746 : case CFN_BUILT_IN_BSWAP128:
1103 : 1492 : *result = wi::bswap (wide_int::from (arg, precision,
1104 : 1492 : TYPE_SIGN (arg_type)));
1105 : 746 : return true;
1106 : :
1107 : : default:
1108 : : return false;
1109 : : }
1110 : : }
1111 : :
1112 : : /* Try to evaluate:
1113 : :
1114 : : RESULT = FN (*ARG)
1115 : :
1116 : : where FORMAT is the format of ARG and of the real and imaginary parts
1117 : : of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
1118 : : true on success. */
1119 : :
1120 : : static bool
1121 : 156 : fold_const_call_cs (real_value *result_real, real_value *result_imag,
1122 : : combined_fn fn, const real_value *arg,
1123 : : const real_format *format)
1124 : : {
1125 : 0 : switch (fn)
1126 : : {
1127 : 156 : CASE_CFN_CEXPI:
1128 : : /* cexpi(x+yi) = cos(x)+sin(y)*i. */
1129 : 156 : return do_mpfr_sincos (result_imag, result_real, arg, format);
1130 : :
1131 : : default:
1132 : : return false;
1133 : : }
1134 : : }
1135 : :
1136 : : /* Try to evaluate:
1137 : :
1138 : : *RESULT = fn (ARG)
1139 : :
1140 : : where FORMAT is the format of RESULT and of the real and imaginary parts
1141 : : of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
1142 : : success. */
1143 : :
1144 : : static bool
1145 : 2282 : fold_const_call_sc (real_value *result, combined_fn fn,
1146 : : const real_value *arg_real, const real_value *arg_imag,
1147 : : const real_format *format)
1148 : : {
1149 : 0 : switch (fn)
1150 : : {
1151 : 1146 : CASE_CFN_CABS:
1152 : 1146 : CASE_CFN_CABS_FN:
1153 : 1146 : return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
1154 : :
1155 : : default:
1156 : : return false;
1157 : : }
1158 : : }
1159 : :
1160 : : /* Try to evaluate:
1161 : :
1162 : : RESULT = fn (ARG)
1163 : :
1164 : : where FORMAT is the format of the real and imaginary parts of RESULT
1165 : : (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
1166 : : Return true on success. */
1167 : :
1168 : : static bool
1169 : 38831 : fold_const_call_cc (real_value *result_real, real_value *result_imag,
1170 : : combined_fn fn, const real_value *arg_real,
1171 : : const real_value *arg_imag, const real_format *format)
1172 : : {
1173 : 38831 : switch (fn)
1174 : : {
1175 : 686 : CASE_CFN_CCOS:
1176 : 686 : CASE_CFN_CCOS_FN:
1177 : 686 : return do_mpc_arg1 (result_real, result_imag, mpc_cos,
1178 : 686 : arg_real, arg_imag, format);
1179 : :
1180 : 676 : CASE_CFN_CCOSH:
1181 : 676 : CASE_CFN_CCOSH_FN:
1182 : 676 : return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
1183 : 676 : arg_real, arg_imag, format);
1184 : :
1185 : 683 : CASE_CFN_CPROJ:
1186 : 683 : CASE_CFN_CPROJ_FN:
1187 : 683 : if (real_isinf (arg_real) || real_isinf (arg_imag))
1188 : : {
1189 : 436 : *result_real = dconstinf;
1190 : 436 : *result_imag = dconst0;
1191 : 436 : result_imag->sign = arg_imag->sign;
1192 : : }
1193 : : else
1194 : : {
1195 : 247 : *result_real = *arg_real;
1196 : 247 : *result_imag = *arg_imag;
1197 : : }
1198 : : return true;
1199 : :
1200 : 686 : CASE_CFN_CSIN:
1201 : 686 : CASE_CFN_CSIN_FN:
1202 : 686 : return do_mpc_arg1 (result_real, result_imag, mpc_sin,
1203 : 686 : arg_real, arg_imag, format);
1204 : :
1205 : 676 : CASE_CFN_CSINH:
1206 : 676 : CASE_CFN_CSINH_FN:
1207 : 676 : return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
1208 : 676 : arg_real, arg_imag, format);
1209 : :
1210 : 700 : CASE_CFN_CTAN:
1211 : 700 : CASE_CFN_CTAN_FN:
1212 : 700 : return do_mpc_arg1 (result_real, result_imag, mpc_tan,
1213 : 700 : arg_real, arg_imag, format);
1214 : :
1215 : 676 : CASE_CFN_CTANH:
1216 : 676 : CASE_CFN_CTANH_FN:
1217 : 676 : return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
1218 : 676 : arg_real, arg_imag, format);
1219 : :
1220 : 779 : CASE_CFN_CLOG:
1221 : 779 : CASE_CFN_CLOG_FN:
1222 : 779 : return do_mpc_arg1 (result_real, result_imag, mpc_log,
1223 : 779 : arg_real, arg_imag, format);
1224 : :
1225 : 3161 : CASE_CFN_CSQRT:
1226 : 3161 : CASE_CFN_CSQRT_FN:
1227 : 3161 : return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
1228 : 3161 : arg_real, arg_imag, format);
1229 : :
1230 : 679 : CASE_CFN_CASIN:
1231 : 679 : CASE_CFN_CASIN_FN:
1232 : 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asin,
1233 : 679 : arg_real, arg_imag, format);
1234 : :
1235 : 728 : CASE_CFN_CACOS:
1236 : 728 : CASE_CFN_CACOS_FN:
1237 : 728 : return do_mpc_arg1 (result_real, result_imag, mpc_acos,
1238 : 728 : arg_real, arg_imag, format);
1239 : :
1240 : 679 : CASE_CFN_CATAN:
1241 : 679 : CASE_CFN_CATAN_FN:
1242 : 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atan,
1243 : 679 : arg_real, arg_imag, format);
1244 : :
1245 : 679 : CASE_CFN_CASINH:
1246 : 679 : CASE_CFN_CASINH_FN:
1247 : 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
1248 : 679 : arg_real, arg_imag, format);
1249 : :
1250 : 745 : CASE_CFN_CACOSH:
1251 : 745 : CASE_CFN_CACOSH_FN:
1252 : 745 : return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
1253 : 745 : arg_real, arg_imag, format);
1254 : :
1255 : 679 : CASE_CFN_CATANH:
1256 : 679 : CASE_CFN_CATANH_FN:
1257 : 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1258 : 679 : arg_real, arg_imag, format);
1259 : :
1260 : 1655 : CASE_CFN_CEXP:
1261 : 1655 : CASE_CFN_CEXP_FN:
1262 : 1655 : return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1263 : 1655 : arg_real, arg_imag, format);
1264 : :
1265 : : default:
1266 : : return false;
1267 : : }
1268 : : }
1269 : :
1270 : : /* Subroutine of fold_const_call, with the same interface. Handle cases
1271 : : where the arguments and result are numerical. */
1272 : :
1273 : : static tree
1274 : 16101056 : fold_const_call_1 (combined_fn fn, tree type, tree arg)
1275 : : {
1276 : 16101056 : machine_mode mode = TYPE_MODE (type);
1277 : 16101056 : machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1278 : :
1279 : 16101056 : if (integer_cst_p (arg))
1280 : : {
1281 : 3598144 : if (SCALAR_INT_MODE_P (mode))
1282 : : {
1283 : 3365145 : wide_int result;
1284 : 3365145 : if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
1285 : 3365145 : TYPE_PRECISION (type), TREE_TYPE (arg)))
1286 : 6983 : return wide_int_to_tree (type, result);
1287 : 3365145 : }
1288 : 3591161 : return NULL_TREE;
1289 : : }
1290 : :
1291 : 12502912 : if (real_cst_p (arg))
1292 : : {
1293 : 193900 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1294 : 193900 : if (mode == arg_mode)
1295 : : {
1296 : : /* real -> real. */
1297 : 175768 : REAL_VALUE_TYPE result;
1298 : 175768 : if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1299 : 175768 : REAL_MODE_FORMAT (mode)))
1300 : 112325 : return build_real (type, result);
1301 : : }
1302 : 18132 : else if (COMPLEX_MODE_P (mode)
1303 : 18288 : && GET_MODE_INNER (mode) == arg_mode)
1304 : : {
1305 : : /* real -> complex real. */
1306 : 156 : REAL_VALUE_TYPE result_real, result_imag;
1307 : 312 : if (fold_const_call_cs (&result_real, &result_imag, fn,
1308 : 156 : TREE_REAL_CST_PTR (arg),
1309 : 156 : REAL_MODE_FORMAT (arg_mode)))
1310 : 312 : return build_complex (type,
1311 : 156 : build_real (TREE_TYPE (type), result_real),
1312 : 312 : build_real (TREE_TYPE (type), result_imag));
1313 : : }
1314 : 17976 : else if (INTEGRAL_TYPE_P (type))
1315 : : {
1316 : : /* real -> int. */
1317 : 17973 : wide_int result;
1318 : 17973 : if (fold_const_call_ss (&result, fn,
1319 : 17973 : TREE_REAL_CST_PTR (arg),
1320 : 17973 : TYPE_PRECISION (type),
1321 : 17973 : REAL_MODE_FORMAT (arg_mode)))
1322 : 11197 : return wide_int_to_tree (type, result);
1323 : 17973 : }
1324 : 70222 : return NULL_TREE;
1325 : : }
1326 : :
1327 : 12309012 : if (complex_cst_p (arg))
1328 : : {
1329 : 41113 : gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1330 : 41113 : machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1331 : 41113 : tree argr = TREE_REALPART (arg);
1332 : 41113 : tree argi = TREE_IMAGPART (arg);
1333 : 41113 : if (mode == arg_mode
1334 : 38831 : && real_cst_p (argr)
1335 : 79944 : && real_cst_p (argi))
1336 : : {
1337 : : /* complex real -> complex real. */
1338 : 38831 : REAL_VALUE_TYPE result_real, result_imag;
1339 : 77662 : if (fold_const_call_cc (&result_real, &result_imag, fn,
1340 : 38831 : TREE_REAL_CST_PTR (argr),
1341 : 38831 : TREE_REAL_CST_PTR (argi),
1342 : 38831 : REAL_MODE_FORMAT (inner_mode)))
1343 : 20942 : return build_complex (type,
1344 : 10471 : build_real (TREE_TYPE (type), result_real),
1345 : 20942 : build_real (TREE_TYPE (type), result_imag));
1346 : : }
1347 : 30642 : if (mode == inner_mode
1348 : 2282 : && real_cst_p (argr)
1349 : 32924 : && real_cst_p (argi))
1350 : : {
1351 : : /* complex real -> real. */
1352 : 2282 : REAL_VALUE_TYPE result;
1353 : 4564 : if (fold_const_call_sc (&result, fn,
1354 : 2282 : TREE_REAL_CST_PTR (argr),
1355 : 2282 : TREE_REAL_CST_PTR (argi),
1356 : 2282 : REAL_MODE_FORMAT (inner_mode)))
1357 : 1146 : return build_real (type, result);
1358 : : }
1359 : 29496 : return NULL_TREE;
1360 : : }
1361 : :
1362 : : return NULL_TREE;
1363 : : }
1364 : :
1365 : : /* Try to fold FN (ARG) to a constant. Return the constant on success,
1366 : : otherwise return null. TYPE is the type of the return value. */
1367 : :
1368 : : tree
1369 : 16893081 : fold_const_call (combined_fn fn, tree type, tree arg)
1370 : : {
1371 : 16893081 : switch (fn)
1372 : : {
1373 : 470528 : case CFN_BUILT_IN_STRLEN:
1374 : 470528 : if (const char *str = c_getstr (arg))
1375 : 46797 : return build_int_cst (type, strlen (str));
1376 : : return NULL_TREE;
1377 : :
1378 : 187273 : CASE_CFN_NAN:
1379 : 187273 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN):
1380 : 187273 : case CFN_BUILT_IN_NAND32:
1381 : 187273 : case CFN_BUILT_IN_NAND64:
1382 : 187273 : case CFN_BUILT_IN_NAND128:
1383 : 187273 : case CFN_BUILT_IN_NAND64X:
1384 : 187273 : return fold_const_builtin_nan (type, arg, true);
1385 : :
1386 : 132094 : CASE_CFN_NANS:
1387 : 132094 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
1388 : 132094 : case CFN_BUILT_IN_NANSF16B:
1389 : 132094 : case CFN_BUILT_IN_NANSD32:
1390 : 132094 : case CFN_BUILT_IN_NANSD64:
1391 : 132094 : case CFN_BUILT_IN_NANSD128:
1392 : 132094 : case CFN_BUILT_IN_NANSD64X:
1393 : 132094 : return fold_const_builtin_nan (type, arg, false);
1394 : :
1395 : 1648 : case CFN_REDUC_PLUS:
1396 : 1648 : return fold_const_reduction (type, arg, PLUS_EXPR);
1397 : :
1398 : 45 : case CFN_REDUC_MAX:
1399 : 45 : return fold_const_reduction (type, arg, MAX_EXPR);
1400 : :
1401 : 61 : case CFN_REDUC_MIN:
1402 : 61 : return fold_const_reduction (type, arg, MIN_EXPR);
1403 : :
1404 : 32 : case CFN_REDUC_AND:
1405 : 32 : return fold_const_reduction (type, arg, BIT_AND_EXPR);
1406 : :
1407 : 200 : case CFN_REDUC_IOR:
1408 : 200 : return fold_const_reduction (type, arg, BIT_IOR_EXPR);
1409 : :
1410 : 6 : case CFN_REDUC_XOR:
1411 : 6 : return fold_const_reduction (type, arg, BIT_XOR_EXPR);
1412 : :
1413 : 138 : case CFN_VEC_CONVERT:
1414 : 138 : return fold_const_vec_convert (type, arg);
1415 : :
1416 : 16101056 : default:
1417 : 16101056 : return fold_const_call_1 (fn, type, arg);
1418 : : }
1419 : : }
1420 : :
1421 : : /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1422 : : of type TYPE. */
1423 : :
1424 : : static tree
1425 : 0 : fold_const_fold_left (tree type, tree arg0, tree arg1, tree_code code)
1426 : : {
1427 : 0 : if (TREE_CODE (arg1) != VECTOR_CST)
1428 : : return NULL_TREE;
1429 : :
1430 : 0 : unsigned HOST_WIDE_INT nelts;
1431 : 0 : if (!VECTOR_CST_NELTS (arg1).is_constant (&nelts))
1432 : : return NULL_TREE;
1433 : :
1434 : 0 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
1435 : : {
1436 : 0 : arg0 = const_binop (code, type, arg0, VECTOR_CST_ELT (arg1, i));
1437 : 0 : if (arg0 == NULL_TREE || !CONSTANT_CLASS_P (arg0))
1438 : : return NULL_TREE;
1439 : : }
1440 : : return arg0;
1441 : : }
1442 : :
1443 : : /* Try to evaluate:
1444 : :
1445 : : *RESULT = FN (*ARG0, *ARG1)
1446 : :
1447 : : in format FORMAT. Return true on success. */
1448 : :
1449 : : static bool
1450 : 232307 : fold_const_call_sss (real_value *result, combined_fn fn,
1451 : : const real_value *arg0, const real_value *arg1,
1452 : : const real_format *format)
1453 : : {
1454 : 232307 : switch (fn)
1455 : : {
1456 : 6988 : CASE_CFN_DREM:
1457 : 6988 : CASE_CFN_REMAINDER:
1458 : 6988 : CASE_CFN_REMAINDER_FN:
1459 : 6988 : return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1460 : :
1461 : 970 : CASE_CFN_ATAN2:
1462 : 970 : CASE_CFN_ATAN2_FN:
1463 : 970 : return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1464 : :
1465 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
1466 : 84 : CASE_CFN_ATAN2PI:
1467 : 84 : CASE_CFN_ATAN2PI_FN:
1468 : 84 : return do_mpfr_arg2 (result, mpfr_atan2pi, arg0, arg1, format);
1469 : : #endif
1470 : :
1471 : 572 : CASE_CFN_FDIM:
1472 : 572 : CASE_CFN_FDIM_FN:
1473 : 572 : return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1474 : :
1475 : 517 : CASE_CFN_FMOD:
1476 : 517 : CASE_CFN_FMOD_FN:
1477 : 517 : return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
1478 : :
1479 : 578 : CASE_CFN_HYPOT:
1480 : 578 : CASE_CFN_HYPOT_FN:
1481 : 578 : return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1482 : :
1483 : 70638 : CASE_CFN_COPYSIGN:
1484 : 70638 : CASE_CFN_COPYSIGN_FN:
1485 : 70638 : *result = *arg0;
1486 : 70638 : real_copysign (result, arg1);
1487 : 70638 : return true;
1488 : :
1489 : 3347 : CASE_CFN_FMIN:
1490 : 3347 : CASE_CFN_FMIN_FN:
1491 : 3347 : return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1492 : :
1493 : 3347 : CASE_CFN_FMAX:
1494 : 3347 : CASE_CFN_FMAX_FN:
1495 : 3347 : return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1496 : :
1497 : 129953 : CASE_CFN_POW:
1498 : 129953 : CASE_CFN_POW_FN:
1499 : 129953 : return fold_const_pow (result, arg0, arg1, format);
1500 : :
1501 : 15313 : CASE_CFN_NEXTAFTER:
1502 : 15313 : CASE_CFN_NEXTAFTER_FN:
1503 : 15313 : case CFN_BUILT_IN_NEXTAFTERF16B:
1504 : 15313 : CASE_CFN_NEXTTOWARD:
1505 : 15313 : return fold_const_nextafter (result, arg0, arg1, format);
1506 : :
1507 : : default:
1508 : : return false;
1509 : : }
1510 : : }
1511 : :
1512 : : /* Try to evaluate:
1513 : :
1514 : : *RESULT = FN (*ARG0, ARG1)
1515 : :
1516 : : where FORMAT is the format of *RESULT and *ARG0. Return true on
1517 : : success. */
1518 : :
1519 : : static bool
1520 : 26569 : fold_const_call_sss (real_value *result, combined_fn fn,
1521 : : const real_value *arg0, const wide_int_ref &arg1,
1522 : : const real_format *format)
1523 : : {
1524 : 26569 : switch (fn)
1525 : : {
1526 : 8199 : CASE_CFN_LDEXP:
1527 : 8199 : CASE_CFN_LDEXP_FN:
1528 : 8199 : return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1529 : :
1530 : 17981 : CASE_CFN_SCALBN:
1531 : 17981 : CASE_CFN_SCALBN_FN:
1532 : 17981 : CASE_CFN_SCALBLN:
1533 : 17981 : CASE_CFN_SCALBLN_FN:
1534 : 17981 : return (format->b == 2
1535 : 17981 : && fold_const_builtin_load_exponent (result, arg0, arg1,
1536 : : format));
1537 : :
1538 : 380 : CASE_CFN_POWI:
1539 : : /* Avoid the folding if flag_signaling_nans is on and
1540 : : operand is a signaling NaN. */
1541 : 380 : if (!flag_unsafe_math_optimizations
1542 : 367 : && flag_signaling_nans
1543 : 395 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
1544 : : return false;
1545 : :
1546 : 380 : real_powi (result, format, arg0, arg1.to_shwi ());
1547 : 380 : return true;
1548 : :
1549 : : default:
1550 : : return false;
1551 : : }
1552 : : }
1553 : :
1554 : : /* Try to evaluate:
1555 : :
1556 : : *RESULT = FN (ARG0, *ARG1)
1557 : :
1558 : : where FORMAT is the format of *RESULT and *ARG1. Return true on
1559 : : success. */
1560 : :
1561 : : static bool
1562 : 6638 : fold_const_call_sss (real_value *result, combined_fn fn,
1563 : : const wide_int_ref &arg0, const real_value *arg1,
1564 : : const real_format *format)
1565 : : {
1566 : 6638 : switch (fn)
1567 : : {
1568 : 1208 : CASE_CFN_JN:
1569 : 1208 : return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1570 : :
1571 : 5421 : CASE_CFN_YN:
1572 : 5421 : return (real_compare (GT_EXPR, arg1, &dconst0)
1573 : 5421 : && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1574 : :
1575 : : default:
1576 : : return false;
1577 : : }
1578 : : }
1579 : :
1580 : : /* Try to evaluate:
1581 : :
1582 : : *RESULT = FN (ARG0, ARG1)
1583 : :
1584 : : where ARG_TYPE is the type of ARG0 and PRECISION is the number of bits in
1585 : : the result. Return true on success. */
1586 : :
1587 : : static bool
1588 : 147733 : fold_const_call_sss (wide_int *result, combined_fn fn,
1589 : : const wide_int_ref &arg0, const wide_int_ref &arg1,
1590 : : unsigned int precision, tree arg_type ATTRIBUTE_UNUSED)
1591 : : {
1592 : 147733 : switch (fn)
1593 : : {
1594 : 7003 : case CFN_CLZ:
1595 : 7003 : case CFN_BUILT_IN_CLZG:
1596 : 7003 : {
1597 : 7003 : int tmp;
1598 : 7003 : if (wi::ne_p (arg0, 0))
1599 : 6855 : tmp = wi::clz (arg0);
1600 : : else
1601 : 148 : tmp = arg1.to_shwi ();
1602 : 7003 : *result = wi::shwi (tmp, precision);
1603 : 7003 : return true;
1604 : : }
1605 : :
1606 : 4919 : case CFN_CTZ:
1607 : 4919 : case CFN_BUILT_IN_CTZG:
1608 : 4919 : {
1609 : 4919 : int tmp;
1610 : 4919 : if (wi::ne_p (arg0, 0))
1611 : 4815 : tmp = wi::ctz (arg0);
1612 : : else
1613 : 104 : tmp = arg1.to_shwi ();
1614 : 4919 : *result = wi::shwi (tmp, precision);
1615 : 4919 : return true;
1616 : : }
1617 : :
1618 : : default:
1619 : : return false;
1620 : : }
1621 : : }
1622 : :
1623 : : /* Try to evaluate:
1624 : :
1625 : : RESULT = fn (ARG0, ARG1)
1626 : :
1627 : : where FORMAT is the format of the real and imaginary parts of RESULT
1628 : : (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1629 : : and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1630 : :
1631 : : static bool
1632 : 33055 : fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1633 : : combined_fn fn, const real_value *arg0_real,
1634 : : const real_value *arg0_imag, const real_value *arg1_real,
1635 : : const real_value *arg1_imag, const real_format *format)
1636 : : {
1637 : 0 : switch (fn)
1638 : : {
1639 : 33055 : CASE_CFN_CPOW:
1640 : 33055 : CASE_CFN_CPOW_FN:
1641 : 33055 : return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1642 : 0 : arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1643 : :
1644 : : default:
1645 : : return false;
1646 : : }
1647 : : }
1648 : :
1649 : : /* Subroutine of fold_const_call, with the same interface. Handle cases
1650 : : where the arguments and result are numerical. */
1651 : :
1652 : : static tree
1653 : 14102423 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1654 : : {
1655 : 14102423 : machine_mode mode = TYPE_MODE (type);
1656 : 14102423 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1657 : 14102423 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1658 : :
1659 : 14102423 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
1660 : : {
1661 : 154216 : if (SCALAR_INT_MODE_P (mode))
1662 : : {
1663 : 147733 : wide_int result;
1664 : 147733 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1665 : 295466 : wi::to_wide (arg1), TYPE_PRECISION (type),
1666 : 147733 : TREE_TYPE (arg0)))
1667 : 11922 : return wide_int_to_tree (type, result);
1668 : 147733 : }
1669 : 142294 : return NULL_TREE;
1670 : : }
1671 : :
1672 : 13948207 : if (mode == arg0_mode
1673 : 9232261 : && real_cst_p (arg0)
1674 : 15241193 : && real_cst_p (arg1))
1675 : : {
1676 : 232307 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1677 : 232307 : REAL_VALUE_TYPE result;
1678 : 232307 : if (arg0_mode == arg1_mode)
1679 : : {
1680 : : /* real, real -> real. */
1681 : 464034 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1682 : 232017 : TREE_REAL_CST_PTR (arg1),
1683 : 232017 : REAL_MODE_FORMAT (mode)))
1684 : 196812 : return build_real (type, result);
1685 : : }
1686 : 290 : else if (arg1_mode == TYPE_MODE (long_double_type_node))
1687 : 290 : switch (fn)
1688 : : {
1689 : 290 : CASE_CFN_NEXTTOWARD:
1690 : : /* real, long double -> real. */
1691 : 580 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1692 : 290 : TREE_REAL_CST_PTR (arg1),
1693 : 290 : REAL_MODE_FORMAT (mode)))
1694 : 228 : return build_real (type, result);
1695 : : break;
1696 : : default:
1697 : : break;
1698 : : }
1699 : 35267 : return NULL_TREE;
1700 : : }
1701 : :
1702 : 13715900 : if (real_cst_p (arg0)
1703 : 13715900 : && integer_cst_p (arg1))
1704 : : {
1705 : 26572 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1706 : 26572 : if (mode == arg0_mode)
1707 : : {
1708 : : /* real, int -> real. */
1709 : 26569 : REAL_VALUE_TYPE result;
1710 : 26569 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1711 : 53138 : wi::to_wide (arg1),
1712 : 26569 : REAL_MODE_FORMAT (mode)))
1713 : 5502 : return build_real (type, result);
1714 : : }
1715 : 21070 : return NULL_TREE;
1716 : : }
1717 : :
1718 : 13689328 : if (integer_cst_p (arg0)
1719 : 13689328 : && real_cst_p (arg1))
1720 : : {
1721 : 6638 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1722 : 6638 : if (mode == arg1_mode)
1723 : : {
1724 : : /* int, real -> real. */
1725 : 6638 : REAL_VALUE_TYPE result;
1726 : 13276 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1727 : 6638 : TREE_REAL_CST_PTR (arg1),
1728 : 6638 : REAL_MODE_FORMAT (mode)))
1729 : 1743 : return build_real (type, result);
1730 : : }
1731 : 4895 : return NULL_TREE;
1732 : : }
1733 : :
1734 : 13682690 : if (arg0_mode == arg1_mode
1735 : 10965584 : && complex_cst_p (arg0)
1736 : 13715745 : && complex_cst_p (arg1))
1737 : : {
1738 : 33055 : gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1739 : 33055 : machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1740 : 33055 : tree arg0r = TREE_REALPART (arg0);
1741 : 33055 : tree arg0i = TREE_IMAGPART (arg0);
1742 : 33055 : tree arg1r = TREE_REALPART (arg1);
1743 : 33055 : tree arg1i = TREE_IMAGPART (arg1);
1744 : 33055 : if (mode == arg0_mode
1745 : 33055 : && real_cst_p (arg0r)
1746 : 33055 : && real_cst_p (arg0i)
1747 : 33055 : && real_cst_p (arg1r)
1748 : 66110 : && real_cst_p (arg1i))
1749 : : {
1750 : : /* complex real, complex real -> complex real. */
1751 : 33055 : REAL_VALUE_TYPE result_real, result_imag;
1752 : 66110 : if (fold_const_call_ccc (&result_real, &result_imag, fn,
1753 : 33055 : TREE_REAL_CST_PTR (arg0r),
1754 : 33055 : TREE_REAL_CST_PTR (arg0i),
1755 : 33055 : TREE_REAL_CST_PTR (arg1r),
1756 : 33055 : TREE_REAL_CST_PTR (arg1i),
1757 : 33055 : REAL_MODE_FORMAT (inner_mode)))
1758 : 21470 : return build_complex (type,
1759 : 10735 : build_real (TREE_TYPE (type), result_real),
1760 : 21470 : build_real (TREE_TYPE (type), result_imag));
1761 : : }
1762 : 22320 : return NULL_TREE;
1763 : : }
1764 : :
1765 : : return NULL_TREE;
1766 : : }
1767 : :
1768 : : /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1769 : : otherwise return null. TYPE is the type of the return value. */
1770 : :
1771 : : tree
1772 : 16756081 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1773 : : {
1774 : 16756081 : const char *p0, *p1;
1775 : 16756081 : char c;
1776 : 16756081 : tree_code subcode;
1777 : 16756081 : switch (fn)
1778 : : {
1779 : 2787 : case CFN_BUILT_IN_STRSPN:
1780 : 2787 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1781 : 123 : return build_int_cst (type, strspn (p0, p1));
1782 : : return NULL_TREE;
1783 : :
1784 : 2684 : case CFN_BUILT_IN_STRCSPN:
1785 : 2684 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1786 : 123 : return build_int_cst (type, strcspn (p0, p1));
1787 : : return NULL_TREE;
1788 : :
1789 : 2203487 : case CFN_BUILT_IN_STRCMP:
1790 : 2203487 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1791 : 24173 : return build_cmp_result (type, strcmp (p0, p1));
1792 : : return NULL_TREE;
1793 : :
1794 : 251 : case CFN_BUILT_IN_STRCASECMP:
1795 : 251 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1796 : : {
1797 : 7 : int r = strcmp (p0, p1);
1798 : 7 : if (r == 0)
1799 : 1 : return build_cmp_result (type, r);
1800 : : }
1801 : : return NULL_TREE;
1802 : :
1803 : 173566 : case CFN_BUILT_IN_INDEX:
1804 : 173566 : case CFN_BUILT_IN_STRCHR:
1805 : 173566 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1806 : : {
1807 : 136 : const char *r = strchr (p0, c);
1808 : 136 : if (r == NULL)
1809 : 32 : return build_int_cst (type, 0);
1810 : 104 : return fold_convert (type,
1811 : : fold_build_pointer_plus_hwi (arg0, r - p0));
1812 : : }
1813 : : return NULL_TREE;
1814 : :
1815 : 163708 : case CFN_BUILT_IN_RINDEX:
1816 : 163708 : case CFN_BUILT_IN_STRRCHR:
1817 : 163708 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1818 : : {
1819 : 103 : const char *r = strrchr (p0, c);
1820 : 103 : if (r == NULL)
1821 : 35 : return build_int_cst (type, 0);
1822 : 68 : return fold_convert (type,
1823 : : fold_build_pointer_plus_hwi (arg0, r - p0));
1824 : : }
1825 : : return NULL_TREE;
1826 : :
1827 : 90076 : case CFN_BUILT_IN_STRSTR:
1828 : 90076 : if ((p1 = c_getstr (arg1)))
1829 : : {
1830 : 6031 : if ((p0 = c_getstr (arg0)))
1831 : : {
1832 : 182 : const char *r = strstr (p0, p1);
1833 : 182 : if (r == NULL)
1834 : 38 : return build_int_cst (type, 0);
1835 : 144 : return fold_convert (type,
1836 : : fold_build_pointer_plus_hwi (arg0, r - p0));
1837 : : }
1838 : 5849 : if (*p1 == '\0')
1839 : 13 : return fold_convert (type, arg0);
1840 : : }
1841 : : return NULL_TREE;
1842 : :
1843 : 0 : case CFN_FOLD_LEFT_PLUS:
1844 : 0 : return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
1845 : :
1846 : 5625 : case CFN_UBSAN_CHECK_ADD:
1847 : 5625 : case CFN_ADD_OVERFLOW:
1848 : 5625 : subcode = PLUS_EXPR;
1849 : 5625 : goto arith_overflow;
1850 : :
1851 : 6116 : case CFN_UBSAN_CHECK_SUB:
1852 : 6116 : case CFN_SUB_OVERFLOW:
1853 : 6116 : subcode = MINUS_EXPR;
1854 : 6116 : goto arith_overflow;
1855 : :
1856 : 5358 : case CFN_UBSAN_CHECK_MUL:
1857 : 5358 : case CFN_MUL_OVERFLOW:
1858 : 5358 : subcode = MULT_EXPR;
1859 : 5358 : goto arith_overflow;
1860 : :
1861 : 17099 : arith_overflow:
1862 : 17099 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
1863 : : {
1864 : 16875 : tree itype
1865 : 16875 : = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type;
1866 : 16875 : bool ovf = false;
1867 : 16875 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
1868 : 16875 : fold_convert (itype, arg1));
1869 : 16875 : if (!r || TREE_CODE (r) != INTEGER_CST)
1870 : : return NULL_TREE;
1871 : 16875 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
1872 : : ovf = true;
1873 : 16875 : if (TREE_OVERFLOW (r))
1874 : 3402 : r = drop_tree_overflow (r);
1875 : 16875 : if (itype == type)
1876 : : {
1877 : 2678 : if (ovf)
1878 : : return NULL_TREE;
1879 : : return r;
1880 : : }
1881 : : else
1882 : 14197 : return build_complex (type, r, build_int_cst (itype, ovf));
1883 : : }
1884 : : return NULL_TREE;
1885 : :
1886 : 14102423 : default:
1887 : 14102423 : return fold_const_call_1 (fn, type, arg0, arg1);
1888 : : }
1889 : : }
1890 : :
1891 : : /* Try to evaluate:
1892 : :
1893 : : *RESULT = FN (*ARG0, *ARG1, *ARG2)
1894 : :
1895 : : in format FORMAT. Return true on success. */
1896 : :
1897 : : static bool
1898 : 2536 : fold_const_call_ssss (real_value *result, combined_fn fn,
1899 : : const real_value *arg0, const real_value *arg1,
1900 : : const real_value *arg2, const real_format *format)
1901 : : {
1902 : 2536 : switch (fn)
1903 : : {
1904 : 2536 : CASE_CFN_FMA:
1905 : 2536 : CASE_CFN_FMA_FN:
1906 : 2536 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
1907 : :
1908 : 0 : case CFN_FMS:
1909 : 0 : {
1910 : 0 : real_value new_arg2 = real_value_negate (arg2);
1911 : 0 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, &new_arg2, format);
1912 : : }
1913 : :
1914 : 0 : case CFN_FNMA:
1915 : 0 : {
1916 : 0 : real_value new_arg0 = real_value_negate (arg0);
1917 : 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1, arg2, format);
1918 : : }
1919 : :
1920 : 0 : case CFN_FNMS:
1921 : 0 : {
1922 : 0 : real_value new_arg0 = real_value_negate (arg0);
1923 : 0 : real_value new_arg2 = real_value_negate (arg2);
1924 : 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1,
1925 : : &new_arg2, format);
1926 : : }
1927 : :
1928 : : default:
1929 : : return false;
1930 : : }
1931 : : }
1932 : :
1933 : : /* Subroutine of fold_const_call, with the same interface. Handle cases
1934 : : where the arguments and result are numerical. */
1935 : :
1936 : : static tree
1937 : 3690328 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1938 : : {
1939 : 3690328 : machine_mode mode = TYPE_MODE (type);
1940 : 3690328 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1941 : 3690328 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1942 : 3690328 : machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
1943 : :
1944 : 3690328 : if (arg0_mode == arg1_mode
1945 : 3690328 : && arg0_mode == arg2_mode
1946 : 1511379 : && real_cst_p (arg0)
1947 : 2656 : && real_cst_p (arg1)
1948 : 3692912 : && real_cst_p (arg2))
1949 : : {
1950 : 2536 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1951 : 2536 : if (mode == arg0_mode)
1952 : : {
1953 : : /* real, real, real -> real. */
1954 : 2536 : REAL_VALUE_TYPE result;
1955 : 5072 : if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
1956 : 2536 : TREE_REAL_CST_PTR (arg1),
1957 : 2536 : TREE_REAL_CST_PTR (arg2),
1958 : 2536 : REAL_MODE_FORMAT (mode)))
1959 : 1062 : return build_real (type, result);
1960 : : }
1961 : 1474 : return NULL_TREE;
1962 : : }
1963 : :
1964 : : return NULL_TREE;
1965 : : }
1966 : :
1967 : : /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
1968 : : success, otherwise return null. TYPE is the type of the return value. */
1969 : :
1970 : : tree
1971 : 6322532 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1972 : : {
1973 : 6322532 : const char *p0, *p1;
1974 : 6322532 : char c;
1975 : 6322532 : unsigned HOST_WIDE_INT s0, s1, s2 = 0;
1976 : 6322532 : switch (fn)
1977 : : {
1978 : 44846 : case CFN_BUILT_IN_STRNCMP:
1979 : 44846 : if (!size_t_cst_p (arg2, &s2))
1980 : : return NULL_TREE;
1981 : 28885 : if (s2 == 0
1982 : 237 : && !TREE_SIDE_EFFECTS (arg0)
1983 : 29094 : && !TREE_SIDE_EFFECTS (arg1))
1984 : 209 : return build_int_cst (type, 0);
1985 : 28676 : else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1986 : 573 : return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX)));
1987 : : return NULL_TREE;
1988 : :
1989 : 7883 : case CFN_BUILT_IN_STRNCASECMP:
1990 : 7883 : if (!size_t_cst_p (arg2, &s2))
1991 : : return NULL_TREE;
1992 : 7562 : if (s2 == 0
1993 : 9 : && !TREE_SIDE_EFFECTS (arg0)
1994 : 7567 : && !TREE_SIDE_EFFECTS (arg1))
1995 : 5 : return build_int_cst (type, 0);
1996 : 7557 : else if ((p0 = c_getstr (arg0))
1997 : 728 : && (p1 = c_getstr (arg1))
1998 : 7701 : && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0)
1999 : 3 : return build_int_cst (type, 0);
2000 : : return NULL_TREE;
2001 : :
2002 : 2414242 : case CFN_BUILT_IN_BCMP:
2003 : 2414242 : case CFN_BUILT_IN_MEMCMP:
2004 : 2414242 : if (!size_t_cst_p (arg2, &s2))
2005 : : return NULL_TREE;
2006 : 1612048 : if (s2 == 0
2007 : 13077 : && !TREE_SIDE_EFFECTS (arg0)
2008 : 1625125 : && !TREE_SIDE_EFFECTS (arg1))
2009 : 13077 : return build_int_cst (type, 0);
2010 : 1598971 : if ((p0 = getbyterep (arg0, &s0))
2011 : 13854 : && (p1 = getbyterep (arg1, &s1))
2012 : 4736 : && s2 <= s0
2013 : 1602985 : && s2 <= s1)
2014 : 4012 : return build_cmp_result (type, memcmp (p0, p1, s2));
2015 : : return NULL_TREE;
2016 : :
2017 : 165077 : case CFN_BUILT_IN_MEMCHR:
2018 : 165077 : if (!size_t_cst_p (arg2, &s2))
2019 : : return NULL_TREE;
2020 : 6918 : if (s2 == 0
2021 : 150 : && !TREE_SIDE_EFFECTS (arg0)
2022 : 7051 : && !TREE_SIDE_EFFECTS (arg1))
2023 : 130 : return build_int_cst (type, 0);
2024 : 6788 : if ((p0 = getbyterep (arg0, &s0))
2025 : 1732 : && s2 <= s0
2026 : 8399 : && target_char_cst_p (arg1, &c))
2027 : : {
2028 : 591 : const char *r = (const char *) memchr (p0, c, s2);
2029 : 591 : if (r == NULL)
2030 : 241 : return build_int_cst (type, 0);
2031 : 350 : return fold_convert (type,
2032 : : fold_build_pointer_plus_hwi (arg0, r - p0));
2033 : : }
2034 : : return NULL_TREE;
2035 : :
2036 : 0 : case CFN_WHILE_ULT:
2037 : 0 : {
2038 : 0 : poly_uint64 parg0, parg1;
2039 : 0 : if (poly_int_tree_p (arg0, &parg0) && poly_int_tree_p (arg1, &parg1))
2040 : 0 : return fold_while_ult (type, parg0, parg1);
2041 : : return NULL_TREE;
2042 : : }
2043 : :
2044 : 156 : case CFN_UADDC:
2045 : 156 : case CFN_USUBC:
2046 : 156 : if (integer_cst_p (arg0) && integer_cst_p (arg1) && integer_cst_p (arg2))
2047 : : {
2048 : 156 : tree itype = TREE_TYPE (type);
2049 : 156 : bool ovf = false;
2050 : 156 : tree_code subcode = fn == CFN_UADDC ? PLUS_EXPR : MINUS_EXPR;
2051 : 156 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
2052 : 156 : fold_convert (itype, arg1));
2053 : 156 : if (!r)
2054 : : return NULL_TREE;
2055 : 156 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
2056 : : ovf = true;
2057 : 156 : tree r2 = int_const_binop (subcode, r, fold_convert (itype, arg2));
2058 : 156 : if (!r2 || TREE_CODE (r2) != INTEGER_CST)
2059 : : return NULL_TREE;
2060 : 156 : if (arith_overflowed_p (subcode, itype, r, arg2))
2061 : 36 : ovf = true;
2062 : 156 : if (TREE_OVERFLOW (r2))
2063 : 0 : r2 = drop_tree_overflow (r2);
2064 : 156 : return build_complex (type, r2, build_int_cst (itype, ovf));
2065 : : }
2066 : : return NULL_TREE;
2067 : :
2068 : 3690328 : default:
2069 : 3690328 : return fold_const_call_1 (fn, type, arg0, arg1, arg2);
2070 : : }
2071 : : }
|