Line data Source code
1 : /* Constant folding for calls to built-in and internal functions.
2 : Copyright (C) 1988-2026 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 41928693 : integer_cst_p (tree t)
40 : {
41 41928693 : return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
42 : }
43 :
44 : static inline bool
45 45534987 : real_cst_p (tree t)
46 : {
47 45534987 : return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
48 : }
49 :
50 : static inline bool
51 27771331 : complex_cst_p (tree t)
52 : {
53 27771331 : 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 2869723 : size_t_cst_p (tree t, unsigned HOST_WIDE_INT *size_out)
61 : {
62 2869723 : if (types_compatible_p (size_type_node, TREE_TYPE (t))
63 2869568 : && integer_cst_p (t)
64 4540717 : && tree_fits_uhwi_p (t))
65 : {
66 1670994 : *size_out = tree_to_uhwi (t);
67 1670994 : 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 66448 : build_cmp_result (tree type, int res)
78 : {
79 71082 : 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 243540 : 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 243540 : if (!mpfr_number_p (m)
95 228535 : || mpfr_overflow_p ()
96 228535 : || mpfr_underflow_p ()
97 468531 : || (flag_rounding_math && inexact))
98 : return false;
99 :
100 224950 : REAL_VALUE_TYPE tmp;
101 224950 : 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 224950 : if (!real_isfinite (&tmp)
107 224950 : || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
108 : return false;
109 :
110 224950 : real_convert (result, format, &tmp);
111 224950 : 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 109110 : 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 109110 : if (format->b != 2 || !real_isfinite (arg))
129 : return false;
130 :
131 106368 : int prec = format->p;
132 106368 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
133 :
134 106368 : auto_mpfr m (prec);
135 106368 : mpfr_from_real (m, arg, MPFR_RNDN);
136 106368 : mpfr_clear_flags ();
137 106368 : bool inexact = func (m, m, rnd);
138 106368 : bool ok = do_mpfr_ckconv (result, m, inexact, format);
139 :
140 106368 : return ok;
141 106368 : }
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 150 : 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 150 : if (format->b != 2 || !real_isfinite (arg))
157 : return false;
158 :
159 150 : int prec = format->p;
160 150 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
161 150 : mpfr_t m, ms, mc;
162 :
163 150 : mpfr_inits2 (prec, m, ms, mc, NULL);
164 150 : mpfr_from_real (m, arg, MPFR_RNDN);
165 150 : mpfr_clear_flags ();
166 150 : bool inexact = mpfr_sin_cos (ms, mc, m, rnd);
167 150 : bool ok = (do_mpfr_ckconv (result_sin, ms, inexact, format)
168 150 : && do_mpfr_ckconv (result_cos, mc, inexact, format));
169 150 : mpfr_clears (m, ms, mc, NULL);
170 :
171 150 : 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 143661 : 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 143661 : if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
190 : return false;
191 :
192 132583 : int prec = format->p;
193 132583 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
194 132583 : mpfr_t m0, m1;
195 :
196 132583 : mpfr_inits2 (prec, m0, m1, NULL);
197 132583 : mpfr_from_real (m0, arg0, MPFR_RNDN);
198 132583 : mpfr_from_real (m1, arg1, MPFR_RNDN);
199 132583 : mpfr_clear_flags ();
200 132583 : bool inexact = func (m0, m0, m1, rnd);
201 132583 : bool ok = do_mpfr_ckconv (result, m0, inexact, format);
202 132583 : mpfr_clears (m0, m1, NULL);
203 :
204 132583 : 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 1755 : 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 1755 : if (format->b != 2 || !real_isfinite (arg1))
221 : return false;
222 :
223 1755 : int prec = format->p;
224 1755 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
225 :
226 1755 : auto_mpfr m (prec);
227 1755 : mpfr_from_real (m, arg1, MPFR_RNDN);
228 1755 : mpfr_clear_flags ();
229 1755 : bool inexact = func (m, arg0.to_shwi (), m, rnd);
230 1755 : bool ok = do_mpfr_ckconv (result, m, inexact, format);
231 :
232 1755 : return ok;
233 1755 : }
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 2534 : 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 2534 : if (format->b != 2
252 2534 : || !real_isfinite (arg0)
253 2534 : || !real_isfinite (arg1)
254 5068 : || !real_isfinite (arg2))
255 : return false;
256 :
257 2534 : int prec = format->p;
258 2534 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
259 2534 : mpfr_t m0, m1, m2;
260 :
261 2534 : mpfr_inits2 (prec, m0, m1, m2, NULL);
262 2534 : mpfr_from_real (m0, arg0, MPFR_RNDN);
263 2534 : mpfr_from_real (m1, arg1, MPFR_RNDN);
264 2534 : mpfr_from_real (m2, arg2, MPFR_RNDN);
265 2534 : mpfr_clear_flags ();
266 2534 : bool inexact = func (m0, m0, m1, m2, rnd);
267 2534 : bool ok = do_mpfr_ckconv (result, m0, inexact, format);
268 2534 : mpfr_clears (m0, m1, m2, NULL);
269 :
270 2534 : 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 38816 : 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 38816 : if (!mpfr_number_p (mpc_realref (m))
287 33276 : || !mpfr_number_p (mpc_imagref (m))
288 33276 : || mpfr_overflow_p ()
289 33276 : || mpfr_underflow_p ()
290 68458 : || (flag_rounding_math && inexact))
291 : return false;
292 :
293 29642 : REAL_VALUE_TYPE tmp_real, tmp_imag;
294 29642 : real_from_mpfr (&tmp_real, mpc_realref (m), format, MPFR_RNDN);
295 29642 : 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 29642 : if (!real_isfinite (&tmp_real)
301 29642 : || !real_isfinite (&tmp_imag)
302 29642 : || (tmp_real.cl == rvc_zero) != (mpfr_zero_p (mpc_realref (m)) != 0)
303 59284 : || (tmp_imag.cl == rvc_zero) != (mpfr_zero_p (mpc_imagref (m)) != 0))
304 : return false;
305 :
306 29642 : real_convert (result_real, format, &tmp_real);
307 29642 : real_convert (result_imag, format, &tmp_imag);
308 :
309 29642 : return (real_identical (result_real, &tmp_real)
310 29642 : && 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 13715 : 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 13715 : if (format->b != 2
330 13715 : || !real_isfinite (arg_real)
331 25108 : || !real_isfinite (arg_imag))
332 : return false;
333 :
334 11393 : int prec = format->p;
335 11393 : mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
336 11393 : mpc_t m;
337 :
338 11393 : mpc_init2 (m, prec);
339 11393 : mpfr_from_real (mpc_realref (m), arg_real, MPFR_RNDN);
340 11393 : mpfr_from_real (mpc_imagref (m), arg_imag, MPFR_RNDN);
341 11393 : mpfr_clear_flags ();
342 11393 : bool inexact = func (m, m, crnd);
343 11393 : bool ok = do_mpc_ckconv (result_real, result_imag, m, inexact, format);
344 11393 : mpc_clear (m);
345 :
346 11393 : 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 32067 : 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 32067 : if (!real_isfinite (arg0_real)
365 29745 : || !real_isfinite (arg0_imag)
366 29745 : || !real_isfinite (arg1_real)
367 59490 : || !real_isfinite (arg1_imag))
368 : return false;
369 :
370 27423 : int prec = format->p;
371 27423 : mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
372 27423 : mpc_t m0, m1;
373 :
374 27423 : mpc_init2 (m0, prec);
375 27423 : mpc_init2 (m1, prec);
376 27423 : mpfr_from_real (mpc_realref (m0), arg0_real, MPFR_RNDN);
377 27423 : mpfr_from_real (mpc_imagref (m0), arg0_imag, MPFR_RNDN);
378 27423 : mpfr_from_real (mpc_realref (m1), arg1_real, MPFR_RNDN);
379 27423 : mpfr_from_real (mpc_imagref (m1), arg1_imag, MPFR_RNDN);
380 27423 : mpfr_clear_flags ();
381 27423 : bool inexact = func (m0, m0, m1, crnd);
382 27423 : bool ok = do_mpc_ckconv (result_real, result_imag, m0, inexact, format);
383 27423 : mpc_clear (m0);
384 27423 : mpc_clear (m1);
385 :
386 27423 : 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 4463 : fold_const_logb (real_value *result, const real_value *arg,
397 : const real_format *format)
398 : {
399 4463 : 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 1652 : 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 1652 : if (!real_isfinite (arg))
478 : return false;
479 :
480 1652 : real_value rounded;
481 1652 : fn (&rounded, format, arg);
482 :
483 1652 : bool fail = false;
484 1652 : *result = real_to_integer (&rounded, &fail, precision);
485 1652 : 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 126668 : fold_const_pow (real_value *result, const real_value *arg0,
496 : const real_value *arg1, const real_format *format)
497 : {
498 126668 : if (flag_signaling_nans
499 126668 : && (REAL_VALUE_ISSIGNALING_NAN (*arg0)
500 0 : || REAL_VALUE_ISSIGNALING_NAN (*arg1)))
501 : return false;
502 :
503 126668 : if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
504 : {
505 116291 : if (flag_errno_math)
506 115928 : switch (result->cl)
507 : {
508 : case rvc_inf:
509 : case rvc_nan:
510 : return false;
511 0 : case rvc_zero:
512 0 : return arg0->cl == rvc_zero;
513 : default:
514 : break;
515 : }
516 116291 : return true;
517 : }
518 :
519 10377 : if (!flag_errno_math
520 39 : && format->b == 2)
521 : {
522 : /* For any value of y (including NaN), if x is +1, 1.0 is returned. */
523 39 : if (real_equal (arg0, &dconst1))
524 : {
525 3 : *result = dconst1;
526 3 : return true;
527 : }
528 :
529 : /* For any value of x (including NaN), if y is +-0, 1.0 is returned. */
530 36 : if (arg1->cl == rvc_zero)
531 : {
532 6 : *result = dconst1;
533 6 : return true;
534 : }
535 :
536 : /* If x is -1 and y is +-Inf, 1.0 shall be returned. */
537 30 : if (arg1->cl == rvc_inf
538 30 : && real_equal (arg0, &dconstm1))
539 : {
540 2 : *result = dconst1;
541 2 : return true;
542 : }
543 :
544 : /* If |x| < 1, and y is -Inf, +Inf shall be returned. */
545 : /* If |x| > 1, and y is -Inf, +0 shall be returned. */
546 28 : if (arg1->cl == rvc_inf && arg1->sign == 1
547 6 : && arg0->cl == rvc_normal)
548 : {
549 4 : *result = REAL_EXP (arg0) <= 0 ? dconstinf : dconst0;
550 4 : return true;
551 : }
552 :
553 24 : if (arg1->cl == rvc_inf && arg1->sign == 1
554 2 : && arg0->cl == rvc_inf)
555 : {
556 2 : *result = dconst0;
557 2 : return true;
558 : }
559 :
560 : /* If |x| < 1, and y is +Inf, +0 shall be returned. */
561 : /* If |x| > 1, and y is +Inf, +Inf shall be returned. */
562 22 : if (arg1->cl == rvc_inf && arg1->sign == 0
563 6 : && arg0->cl == rvc_normal)
564 : {
565 4 : *result = REAL_EXP (arg0) <= 0 ? dconst0 : dconstinf;
566 4 : return true;
567 : }
568 :
569 18 : if (arg1->cl == rvc_inf && arg1->sign == 0
570 2 : && arg0->cl == rvc_inf)
571 : {
572 2 : *result = dconstinf;
573 2 : return true;
574 : }
575 :
576 : /* For y an odd integer < 0, if x is -Inf, -0 shall be returned. */
577 : /* For y < 0 and not an odd integer, if x is -Inf, +0 is returned. */
578 16 : if (arg0->cl == rvc_inf && arg0->sign == 1
579 7 : && arg1->cl == rvc_normal && arg1->sign == 1)
580 : {
581 2 : *result = real_isoddinteger (arg1, format) ? dconstm0 : dconst0;
582 2 : return true;
583 : }
584 :
585 : /* For y an odd integer > 0, if x is -Inf, -Inf shall be returned. */
586 : /* For y > 0 and not an odd integer, if x is -Inf, +Inf is returned. */
587 14 : if (arg0->cl == rvc_inf && arg0->sign == 1
588 5 : && arg1->cl == rvc_normal && arg1->sign == 0)
589 : {
590 5 : *result = real_isoddinteger (arg1, format) ? dconstninf : dconstinf;
591 5 : return true;
592 : }
593 :
594 : /* For y < 0, if x is +Inf, +0 shall be returned. */
595 : /* For y > 0, if x is +Inf, +Inf shall be returned. */
596 9 : if (arg0->cl == rvc_inf && arg0->sign == 0
597 4 : && arg1->cl == rvc_normal)
598 : {
599 4 : *result = arg1->sign ? dconst0 : dconstinf;
600 4 : return true;
601 : }
602 : }
603 :
604 : return false;
605 : }
606 :
607 : /* Try to evaluate:
608 :
609 : *RESULT = nextafter (*ARG0, *ARG1)
610 :
611 : or
612 :
613 : *RESULT = nexttoward (*ARG0, *ARG1)
614 :
615 : in format FORMAT. Return true on success. */
616 :
617 : static bool
618 12534 : fold_const_nextafter (real_value *result, const real_value *arg0,
619 : const real_value *arg1, const real_format *format)
620 : {
621 12534 : if (REAL_VALUE_ISSIGNALING_NAN (*arg0)
622 12534 : || REAL_VALUE_ISSIGNALING_NAN (*arg1))
623 : return false;
624 :
625 : /* Don't handle composite modes, nor decimal, nor modes without
626 : inf or denorm at least for now. */
627 12534 : if (format->pnan < format->p
628 12534 : || format->b == 10
629 12534 : || !format->has_inf
630 12534 : || !format->has_denorm)
631 : return false;
632 :
633 19418 : if (real_nextafter (result, format, arg0, arg1)
634 : /* If raising underflow or overflow and setting errno to ERANGE,
635 : fail if we care about those side-effects. */
636 12534 : && (flag_trapping_math || flag_errno_math))
637 : return false;
638 : /* Similarly for nextafter (0, 1) raising underflow. */
639 5650 : else if (flag_trapping_math
640 5143 : && arg0->cl == rvc_zero
641 4502 : && result->cl != rvc_zero)
642 : return false;
643 :
644 1328 : real_convert (result, format, result);
645 :
646 1328 : return true;
647 : }
648 :
649 : /* Try to evaluate:
650 :
651 : *RESULT = ldexp (*ARG0, ARG1)
652 :
653 : in format FORMAT. Return true on success. */
654 :
655 : static bool
656 25306 : fold_const_builtin_load_exponent (real_value *result, const real_value *arg0,
657 : const wide_int_ref &arg1,
658 : const real_format *format)
659 : {
660 : /* Bound the maximum adjustment to twice the range of the
661 : mode's valid exponents. Use abs to ensure the range is
662 : positive as a sanity check. */
663 25306 : int max_exp_adj = 2 * labs (format->emax - format->emin);
664 :
665 : /* The requested adjustment must be inside this range. This
666 : is a preliminary cap to avoid things like overflow, we
667 : may still fail to compute the result for other reasons. */
668 42826 : if (wi::les_p (arg1, -max_exp_adj) || wi::ges_p (arg1, max_exp_adj))
669 : return false;
670 :
671 : /* Don't perform operation if we honor signaling NaNs and
672 : operand is a signaling NaN. */
673 9824 : if (!flag_unsafe_math_optimizations
674 9819 : && flag_signaling_nans
675 9944 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
676 : return false;
677 :
678 9824 : REAL_VALUE_TYPE initial_result;
679 9824 : real_ldexp (&initial_result, arg0, arg1.to_shwi ());
680 :
681 : /* Ensure we didn't overflow. */
682 9824 : if (real_isinf (&initial_result))
683 : return false;
684 :
685 : /* Only proceed if the target mode can hold the
686 : resulting value. */
687 9804 : *result = real_value_truncate (format, initial_result);
688 9804 : return real_equal (&initial_result, result);
689 : }
690 :
691 : /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
692 : return type TYPE. QUIET is true if a quiet rather than signalling
693 : NaN is required. */
694 :
695 : static tree
696 395585 : fold_const_builtin_nan (tree type, tree arg, bool quiet)
697 : {
698 395585 : REAL_VALUE_TYPE real;
699 395585 : const char *str = c_getstr (arg);
700 395585 : if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
701 395116 : return build_real (type, real);
702 : return NULL_TREE;
703 : }
704 :
705 : /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE. */
706 :
707 : static tree
708 2068 : fold_const_reduction (tree type, tree arg, tree_code code)
709 : {
710 2068 : if (TREE_CODE (arg) != VECTOR_CST)
711 : return NULL_TREE;
712 :
713 4136 : bool idempotent_p = (code == MAX_EXPR
714 2068 : || code == MIN_EXPR
715 2068 : || code == BIT_AND_EXPR
716 : || code == BIT_IOR_EXPR);
717 :
718 2068 : unsigned HOST_WIDE_INT nelts = vector_cst_encoded_nelts (arg);
719 : /* Fall back to the exact element count for singleton sNaN encodings, since
720 : a known single-element vector performs no operation and so cannot raise an
721 : exception. */
722 2068 : bool singleton_snan_p
723 : = (nelts == 1
724 2068 : && tree_expr_maybe_signaling_nan_p (VECTOR_CST_ELT (arg, 0)));
725 :
726 2068 : if (idempotent_p
727 406 : && !VECTOR_CST_STEPPED_P (arg)
728 1134 : && multiple_p (VECTOR_CST_NELTS (arg), nelts)
729 2446 : && !singleton_snan_p)
730 : /* Operating on the first NELTS elements is enough. */
731 : ;
732 1690 : else if (code == BIT_XOR_EXPR
733 4 : && !VECTOR_CST_STEPPED_P (arg)
734 1694 : && multiple_p (VECTOR_CST_NELTS (arg), nelts * 2))
735 : {
736 2 : if (VECTOR_CST_DUPLICATE_P (arg))
737 : {
738 : /* Process two copies of a one-element-per-pattern encoding. */
739 : nelts *= 2;
740 : }
741 : else
742 0 : gcc_checking_assert (VECTOR_CST_NELTS_PER_PATTERN (arg) == 2);
743 : }
744 :
745 1688 : else if (!VECTOR_CST_NELTS (arg).is_constant (&nelts))
746 : return NULL_TREE;
747 :
748 2068 : tree res = VECTOR_CST_ELT (arg, 0);
749 27977 : for (unsigned HOST_WIDE_INT i = 1; i < nelts; i++)
750 : {
751 23841 : res = const_binop (code, type, res, VECTOR_CST_ELT (arg, i));
752 23841 : if (res == NULL_TREE || !CONSTANT_CLASS_P (res))
753 : return NULL_TREE;
754 : }
755 : return res;
756 : }
757 :
758 : /* Fold a call to IFN_VEC_CONVERT (ARG) returning TYPE. */
759 :
760 : static tree
761 6601 : fold_const_vec_convert (tree ret_type, tree arg)
762 : {
763 6601 : enum tree_code code = NOP_EXPR;
764 6601 : tree arg_type = TREE_TYPE (arg);
765 6601 : if (TREE_CODE (arg) != VECTOR_CST)
766 : return NULL_TREE;
767 :
768 6601 : gcc_checking_assert (VECTOR_TYPE_P (ret_type) && VECTOR_TYPE_P (arg_type));
769 :
770 13202 : if (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
771 13115 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg_type)))
772 : code = FIX_TRUNC_EXPR;
773 13080 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
774 13080 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (ret_type)))
775 : code = FLOAT_EXPR;
776 :
777 : /* We can't handle steps directly when extending, since the
778 : values need to wrap at the original precision first. */
779 6601 : bool step_ok_p
780 13202 : = (INTEGRAL_TYPE_P (TREE_TYPE (ret_type))
781 6514 : && INTEGRAL_TYPE_P (TREE_TYPE (arg_type))
782 13054 : && (TYPE_PRECISION (TREE_TYPE (ret_type))
783 6453 : <= TYPE_PRECISION (TREE_TYPE (arg_type))));
784 6601 : tree_vector_builder elts;
785 6601 : if (!elts.new_unary_operation (ret_type, arg, step_ok_p))
786 : return NULL_TREE;
787 :
788 6601 : unsigned int count = elts.encoded_nelts ();
789 18334 : for (unsigned int i = 0; i < count; ++i)
790 : {
791 11739 : tree elt = fold_unary (code, TREE_TYPE (ret_type),
792 : VECTOR_CST_ELT (arg, i));
793 11739 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
794 6 : return NULL_TREE;
795 11733 : elts.quick_push (elt);
796 : }
797 :
798 6595 : return elts.build ();
799 6601 : }
800 :
801 : /* Try to evaluate:
802 :
803 : IFN_WHILE_ULT (ARG0, ARG1, (TYPE) { ... })
804 :
805 : Return the value on success and null on failure. */
806 :
807 : static tree
808 0 : fold_while_ult (tree type, poly_uint64 arg0, poly_uint64 arg1)
809 : {
810 0 : if (known_ge (arg0, arg1))
811 0 : return build_zero_cst (type);
812 :
813 0 : if (maybe_ge (arg0, arg1))
814 : return NULL_TREE;
815 :
816 0 : poly_uint64 diff = arg1 - arg0;
817 0 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
818 0 : if (known_ge (diff, nelts))
819 0 : return build_all_ones_cst (type);
820 :
821 0 : unsigned HOST_WIDE_INT const_diff;
822 0 : if (known_le (diff, nelts) && diff.is_constant (&const_diff))
823 : {
824 0 : tree minus_one = build_minus_one_cst (TREE_TYPE (type));
825 0 : tree zero = build_zero_cst (TREE_TYPE (type));
826 0 : return build_vector_a_then_b (type, const_diff, minus_one, zero);
827 : }
828 : return NULL_TREE;
829 : }
830 :
831 : /* Try to evaluate:
832 :
833 : *RESULT = FN (*ARG)
834 :
835 : in format FORMAT. Return true on success. */
836 :
837 : static bool
838 162369 : fold_const_call_ss (real_value *result, combined_fn fn,
839 : const real_value *arg, const real_format *format)
840 : {
841 162369 : switch (fn)
842 : {
843 22966 : CASE_CFN_SQRT:
844 22966 : CASE_CFN_SQRT_FN:
845 22966 : return (real_compare (GE_EXPR, arg, &dconst0)
846 22966 : && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
847 :
848 512 : CASE_CFN_CBRT:
849 512 : CASE_CFN_CBRT_FN:
850 512 : return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
851 :
852 1976 : CASE_CFN_ASIN:
853 1976 : CASE_CFN_ASIN_FN:
854 1976 : return (real_compare (GE_EXPR, arg, &dconstm1)
855 1202 : && real_compare (LE_EXPR, arg, &dconst1)
856 2396 : && do_mpfr_arg1 (result, mpfr_asin, arg, format));
857 :
858 2027 : CASE_CFN_ACOS:
859 2027 : CASE_CFN_ACOS_FN:
860 2027 : return (real_compare (GE_EXPR, arg, &dconstm1)
861 1253 : && real_compare (LE_EXPR, arg, &dconst1)
862 2506 : && do_mpfr_arg1 (result, mpfr_acos, arg, format));
863 :
864 552 : CASE_CFN_ATAN:
865 552 : CASE_CFN_ATAN_FN:
866 552 : return do_mpfr_arg1 (result, mpfr_atan, arg, format);
867 :
868 312 : CASE_CFN_ASINH:
869 312 : CASE_CFN_ASINH_FN:
870 312 : return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
871 :
872 1454 : CASE_CFN_ACOSH:
873 1454 : CASE_CFN_ACOSH_FN:
874 1454 : return (real_compare (GE_EXPR, arg, &dconst1)
875 1454 : && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
876 :
877 1901 : CASE_CFN_ATANH:
878 1901 : CASE_CFN_ATANH_FN:
879 1901 : return (real_compare (GE_EXPR, arg, &dconstm1)
880 1901 : && real_compare (LE_EXPR, arg, &dconst1)
881 3802 : && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
882 :
883 979 : CASE_CFN_SIN:
884 979 : CASE_CFN_SIN_FN:
885 979 : return do_mpfr_arg1 (result, mpfr_sin, arg, format);
886 :
887 819 : CASE_CFN_COS:
888 819 : CASE_CFN_COS_FN:
889 819 : return do_mpfr_arg1 (result, mpfr_cos, arg, format);
890 :
891 487 : CASE_CFN_TAN:
892 487 : CASE_CFN_TAN_FN:
893 487 : return do_mpfr_arg1 (result, mpfr_tan, arg, format);
894 :
895 394 : CASE_CFN_SINH:
896 394 : CASE_CFN_SINH_FN:
897 394 : return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
898 :
899 399 : CASE_CFN_COSH:
900 399 : CASE_CFN_COSH_FN:
901 399 : return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
902 :
903 349 : CASE_CFN_TANH:
904 349 : CASE_CFN_TANH_FN:
905 349 : return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
906 :
907 : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
908 55 : CASE_CFN_ACOSPI:
909 55 : CASE_CFN_ACOSPI_FN:
910 55 : return (real_compare (GE_EXPR, arg, &dconstm1)
911 55 : && real_compare (LE_EXPR, arg, &dconst1)
912 110 : && do_mpfr_arg1 (result, mpfr_acospi, arg, format));
913 :
914 60 : CASE_CFN_ASINPI:
915 60 : CASE_CFN_ASINPI_FN:
916 60 : return (real_compare (GE_EXPR, arg, &dconstm1)
917 60 : && real_compare (LE_EXPR, arg, &dconst1)
918 120 : && do_mpfr_arg1 (result, mpfr_asinpi, arg, format));
919 :
920 36 : CASE_CFN_ATANPI:
921 36 : CASE_CFN_ATANPI_FN:
922 36 : return do_mpfr_arg1 (result, mpfr_atanpi, arg, format);
923 :
924 60 : CASE_CFN_COSPI:
925 60 : CASE_CFN_COSPI_FN:
926 60 : return do_mpfr_arg1 (result, mpfr_cospi, arg, format);
927 :
928 60 : CASE_CFN_SINPI:
929 60 : CASE_CFN_SINPI_FN:
930 60 : return do_mpfr_arg1 (result, mpfr_sinpi, arg, format);
931 :
932 72 : CASE_CFN_TANPI:
933 72 : CASE_CFN_TANPI_FN:
934 72 : return do_mpfr_arg1 (result, mpfr_tanpi, arg, format);
935 : #endif
936 :
937 511 : CASE_CFN_ERF:
938 511 : CASE_CFN_ERF_FN:
939 511 : return do_mpfr_arg1 (result, mpfr_erf, arg, format);
940 :
941 554 : CASE_CFN_ERFC:
942 554 : CASE_CFN_ERFC_FN:
943 554 : return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
944 :
945 4899 : CASE_CFN_TGAMMA:
946 4899 : CASE_CFN_TGAMMA_FN:
947 4899 : return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
948 :
949 870 : CASE_CFN_EXP:
950 870 : CASE_CFN_EXP_FN:
951 870 : return do_mpfr_arg1 (result, mpfr_exp, arg, format);
952 :
953 10784 : CASE_CFN_EXP2:
954 10784 : CASE_CFN_EXP2_FN:
955 10784 : return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
956 :
957 698 : CASE_CFN_EXP10:
958 698 : CASE_CFN_POW10:
959 698 : return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
960 :
961 327 : CASE_CFN_EXPM1:
962 327 : CASE_CFN_EXPM1_FN:
963 327 : return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
964 :
965 61065 : CASE_CFN_LOG:
966 61065 : CASE_CFN_LOG_FN:
967 61065 : return (real_compare (GT_EXPR, arg, &dconst0)
968 61065 : && do_mpfr_arg1 (result, mpfr_log, arg, format));
969 :
970 3130 : CASE_CFN_LOG2:
971 3130 : CASE_CFN_LOG2_FN:
972 3130 : return (real_compare (GT_EXPR, arg, &dconst0)
973 3130 : && do_mpfr_arg1 (result, mpfr_log2, arg, format));
974 :
975 3166 : CASE_CFN_LOG10:
976 3166 : CASE_CFN_LOG10_FN:
977 3166 : return (real_compare (GT_EXPR, arg, &dconst0)
978 3166 : && do_mpfr_arg1 (result, mpfr_log10, arg, format));
979 :
980 1921 : CASE_CFN_LOG1P:
981 1921 : CASE_CFN_LOG1P_FN:
982 1921 : return (real_compare (GT_EXPR, arg, &dconstm1)
983 1921 : && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
984 :
985 221 : CASE_CFN_J0:
986 221 : return do_mpfr_arg1 (result, mpfr_j0, arg, format);
987 :
988 221 : CASE_CFN_J1:
989 221 : return do_mpfr_arg1 (result, mpfr_j1, arg, format);
990 :
991 2447 : CASE_CFN_Y0:
992 2447 : return (real_compare (GT_EXPR, arg, &dconst0)
993 2447 : && do_mpfr_arg1 (result, mpfr_y0, arg, format));
994 :
995 2447 : CASE_CFN_Y1:
996 2447 : return (real_compare (GT_EXPR, arg, &dconst0)
997 2447 : && do_mpfr_arg1 (result, mpfr_y1, arg, format));
998 :
999 360 : CASE_CFN_FLOOR:
1000 360 : CASE_CFN_FLOOR_FN:
1001 360 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1002 : {
1003 360 : real_floor (result, format, arg);
1004 360 : return true;
1005 : }
1006 : return false;
1007 :
1008 323 : CASE_CFN_CEIL:
1009 323 : CASE_CFN_CEIL_FN:
1010 323 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1011 : {
1012 323 : real_ceil (result, format, arg);
1013 323 : return true;
1014 : }
1015 : return false;
1016 :
1017 544 : CASE_CFN_TRUNC:
1018 544 : CASE_CFN_TRUNC_FN:
1019 544 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1020 : {
1021 544 : real_trunc (result, format, arg);
1022 544 : return true;
1023 : }
1024 : return false;
1025 :
1026 264 : CASE_CFN_ROUND:
1027 264 : CASE_CFN_ROUND_FN:
1028 264 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1029 : {
1030 264 : real_round (result, format, arg);
1031 264 : return true;
1032 : }
1033 : return false;
1034 :
1035 189 : CASE_CFN_ROUNDEVEN:
1036 189 : CASE_CFN_ROUNDEVEN_FN:
1037 189 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1038 : {
1039 189 : real_roundeven (result, format, arg);
1040 189 : return true;
1041 : }
1042 : return false;
1043 :
1044 4463 : CASE_CFN_LOGB:
1045 4463 : CASE_CFN_LOGB_FN:
1046 4463 : return fold_const_logb (result, arg, format);
1047 :
1048 960 : CASE_CFN_SIGNIFICAND:
1049 960 : return fold_const_significand (result, arg, format);
1050 :
1051 : default:
1052 : return false;
1053 : }
1054 : }
1055 :
1056 : /* Try to evaluate:
1057 :
1058 : *RESULT = FN (*ARG)
1059 :
1060 : where FORMAT is the format of ARG and PRECISION is the number of
1061 : significant bits in the result. Return true on success. */
1062 :
1063 : static bool
1064 17481 : fold_const_call_ss (wide_int *result, combined_fn fn,
1065 : const real_value *arg, unsigned int precision,
1066 : const real_format *format)
1067 : {
1068 17481 : switch (fn)
1069 : {
1070 1853 : CASE_CFN_SIGNBIT:
1071 1853 : if (real_isneg (arg))
1072 986 : *result = wi::one (precision);
1073 : else
1074 867 : *result = wi::zero (precision);
1075 : return true;
1076 :
1077 6485 : CASE_CFN_ILOGB:
1078 6485 : CASE_CFN_ILOGB_FN:
1079 : /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
1080 : Proceed iff radix == 2. In GCC, normalized significands are in
1081 : the range [0.5, 1.0). We want the exponent as if they were
1082 : [1.0, 2.0) so get the exponent and subtract 1. */
1083 6485 : if (arg->cl == rvc_normal && format->b == 2)
1084 : {
1085 834 : *result = wi::shwi (REAL_EXP (arg) - 1, precision);
1086 834 : return true;
1087 : }
1088 : return false;
1089 :
1090 378 : CASE_CFN_ICEIL:
1091 378 : CASE_CFN_LCEIL:
1092 378 : CASE_CFN_LLCEIL:
1093 378 : return fold_const_conversion (result, real_ceil, arg,
1094 378 : precision, format);
1095 :
1096 378 : CASE_CFN_LFLOOR:
1097 378 : CASE_CFN_IFLOOR:
1098 378 : CASE_CFN_LLFLOOR:
1099 378 : return fold_const_conversion (result, real_floor, arg,
1100 378 : precision, format);
1101 :
1102 896 : CASE_CFN_IROUND:
1103 896 : CASE_CFN_LROUND:
1104 896 : CASE_CFN_LROUND_FN:
1105 896 : CASE_CFN_LLROUND:
1106 896 : CASE_CFN_LLROUND_FN:
1107 896 : return fold_const_conversion (result, real_round, arg,
1108 896 : precision, format);
1109 :
1110 : CASE_CFN_IRINT:
1111 : CASE_CFN_LRINT:
1112 : CASE_CFN_LRINT_FN:
1113 : CASE_CFN_LLRINT:
1114 : CASE_CFN_LLRINT_FN:
1115 : /* Not yet folded to a constant. */
1116 : return false;
1117 :
1118 1500 : CASE_CFN_FINITE:
1119 1500 : case CFN_BUILT_IN_FINITED32:
1120 1500 : case CFN_BUILT_IN_FINITED64:
1121 1500 : case CFN_BUILT_IN_FINITED128:
1122 1500 : case CFN_BUILT_IN_ISFINITE:
1123 1534 : *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
1124 1500 : return true;
1125 :
1126 3654 : case CFN_BUILT_IN_ISSIGNALING:
1127 7155 : *result = wi::shwi (real_issignaling_nan (arg) ? 1 : 0, precision);
1128 3654 : return true;
1129 :
1130 208 : CASE_CFN_ISINF:
1131 208 : case CFN_BUILT_IN_ISINFD32:
1132 208 : case CFN_BUILT_IN_ISINFD64:
1133 208 : case CFN_BUILT_IN_ISINFD128:
1134 208 : if (real_isinf (arg))
1135 299 : *result = wi::shwi (arg->sign ? -1 : 1, precision);
1136 : else
1137 42 : *result = wi::shwi (0, precision);
1138 : return true;
1139 :
1140 1341 : CASE_CFN_ISNAN:
1141 1341 : case CFN_BUILT_IN_ISNAND32:
1142 1341 : case CFN_BUILT_IN_ISNAND64:
1143 1341 : case CFN_BUILT_IN_ISNAND128:
1144 2456 : *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
1145 1341 : return true;
1146 :
1147 : default:
1148 : return false;
1149 : }
1150 : }
1151 :
1152 : /* Try to evaluate:
1153 :
1154 : *RESULT = FN (ARG)
1155 :
1156 : where ARG_TYPE is the type of ARG and PRECISION is the number of bits
1157 : in the result. Return true on success. */
1158 :
1159 : static bool
1160 2255617 : fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
1161 : unsigned int precision, tree arg_type)
1162 : {
1163 2255617 : switch (fn)
1164 : {
1165 491 : CASE_CFN_FFS:
1166 491 : case CFN_BUILT_IN_FFSG:
1167 491 : *result = wi::shwi (wi::ffs (arg), precision);
1168 491 : return true;
1169 :
1170 2332 : CASE_CFN_CLZ:
1171 2332 : case CFN_BUILT_IN_CLZG:
1172 2332 : {
1173 2332 : int tmp;
1174 2332 : if (wi::ne_p (arg, 0))
1175 1834 : tmp = wi::clz (arg);
1176 498 : else if (BITINT_TYPE_P (arg_type))
1177 9 : tmp = TYPE_PRECISION (arg_type);
1178 978 : else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1179 : tmp))
1180 489 : tmp = TYPE_PRECISION (arg_type);
1181 2332 : *result = wi::shwi (tmp, precision);
1182 2332 : return true;
1183 : }
1184 :
1185 1635 : CASE_CFN_CTZ:
1186 1635 : case CFN_BUILT_IN_CTZG:
1187 1635 : {
1188 1635 : int tmp;
1189 1635 : if (wi::ne_p (arg, 0))
1190 1391 : tmp = wi::ctz (arg);
1191 244 : else if (BITINT_TYPE_P (arg_type))
1192 0 : tmp = TYPE_PRECISION (arg_type);
1193 488 : else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1194 : tmp))
1195 244 : tmp = TYPE_PRECISION (arg_type);
1196 1635 : *result = wi::shwi (tmp, precision);
1197 1635 : return true;
1198 : }
1199 :
1200 412 : CASE_CFN_CLRSB:
1201 412 : case CFN_BUILT_IN_CLRSBG:
1202 412 : *result = wi::shwi (wi::clrsb (arg), precision);
1203 412 : return true;
1204 :
1205 41774 : CASE_CFN_POPCOUNT:
1206 41774 : case CFN_BUILT_IN_POPCOUNTG:
1207 41774 : *result = wi::shwi (wi::popcount (arg), precision);
1208 41774 : return true;
1209 :
1210 690 : CASE_CFN_PARITY:
1211 690 : case CFN_BUILT_IN_PARITYG:
1212 690 : *result = wi::shwi (wi::parity (arg), precision);
1213 690 : return true;
1214 :
1215 738 : CASE_CFN_BSWAP:
1216 1476 : *result = wi::bswap (wide_int::from (arg, precision,
1217 1476 : TYPE_SIGN (arg_type)));
1218 738 : return true;
1219 :
1220 80 : CASE_CFN_BITREVERSE:
1221 160 : *result = wi::bitreverse (wide_int::from (arg, precision,
1222 160 : TYPE_SIGN (arg_type)));
1223 80 : return true;
1224 :
1225 : default:
1226 : return false;
1227 : }
1228 : }
1229 :
1230 : /* Try to evaluate:
1231 :
1232 : RESULT = FN (*ARG)
1233 :
1234 : where FORMAT is the format of ARG and of the real and imaginary parts
1235 : of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
1236 : true on success. */
1237 :
1238 : static bool
1239 150 : fold_const_call_cs (real_value *result_real, real_value *result_imag,
1240 : combined_fn fn, const real_value *arg,
1241 : const real_format *format)
1242 : {
1243 0 : switch (fn)
1244 : {
1245 150 : CASE_CFN_CEXPI:
1246 : /* cexpi(x+yi) = cos(x)+sin(y)*i. */
1247 150 : return do_mpfr_sincos (result_imag, result_real, arg, format);
1248 :
1249 : default:
1250 : return false;
1251 : }
1252 : }
1253 :
1254 : /* Try to evaluate:
1255 :
1256 : *RESULT = fn (ARG)
1257 :
1258 : where FORMAT is the format of RESULT and of the real and imaginary parts
1259 : of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
1260 : success. */
1261 :
1262 : static bool
1263 2296 : fold_const_call_sc (real_value *result, combined_fn fn,
1264 : const real_value *arg_real, const real_value *arg_imag,
1265 : const real_format *format)
1266 : {
1267 0 : switch (fn)
1268 : {
1269 1160 : CASE_CFN_CABS:
1270 1160 : CASE_CFN_CABS_FN:
1271 1160 : return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
1272 :
1273 : default:
1274 : return false;
1275 : }
1276 : }
1277 :
1278 : /* Try to evaluate:
1279 :
1280 : RESULT = fn (ARG)
1281 :
1282 : where FORMAT is the format of the real and imaginary parts of RESULT
1283 : (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
1284 : Return true on success. */
1285 :
1286 : static bool
1287 39622 : fold_const_call_cc (real_value *result_real, real_value *result_imag,
1288 : combined_fn fn, const real_value *arg_real,
1289 : const real_value *arg_imag, const real_format *format)
1290 : {
1291 39622 : switch (fn)
1292 : {
1293 686 : CASE_CFN_CCOS:
1294 686 : CASE_CFN_CCOS_FN:
1295 686 : return do_mpc_arg1 (result_real, result_imag, mpc_cos,
1296 686 : arg_real, arg_imag, format);
1297 :
1298 676 : CASE_CFN_CCOSH:
1299 676 : CASE_CFN_CCOSH_FN:
1300 676 : return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
1301 676 : arg_real, arg_imag, format);
1302 :
1303 683 : CASE_CFN_CPROJ:
1304 683 : CASE_CFN_CPROJ_FN:
1305 683 : if (real_isinf (arg_real) || real_isinf (arg_imag))
1306 : {
1307 436 : *result_real = dconstinf;
1308 436 : *result_imag = dconst0;
1309 436 : result_imag->sign = arg_imag->sign;
1310 : }
1311 : else
1312 : {
1313 247 : *result_real = *arg_real;
1314 247 : *result_imag = *arg_imag;
1315 : }
1316 : return true;
1317 :
1318 701 : CASE_CFN_CSIN:
1319 701 : CASE_CFN_CSIN_FN:
1320 701 : return do_mpc_arg1 (result_real, result_imag, mpc_sin,
1321 701 : arg_real, arg_imag, format);
1322 :
1323 676 : CASE_CFN_CSINH:
1324 676 : CASE_CFN_CSINH_FN:
1325 676 : return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
1326 676 : arg_real, arg_imag, format);
1327 :
1328 698 : CASE_CFN_CTAN:
1329 698 : CASE_CFN_CTAN_FN:
1330 698 : return do_mpc_arg1 (result_real, result_imag, mpc_tan,
1331 698 : arg_real, arg_imag, format);
1332 :
1333 676 : CASE_CFN_CTANH:
1334 676 : CASE_CFN_CTANH_FN:
1335 676 : return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
1336 676 : arg_real, arg_imag, format);
1337 :
1338 777 : CASE_CFN_CLOG:
1339 777 : CASE_CFN_CLOG_FN:
1340 777 : return do_mpc_arg1 (result_real, result_imag, mpc_log,
1341 777 : arg_real, arg_imag, format);
1342 :
1343 3053 : CASE_CFN_CSQRT:
1344 3053 : CASE_CFN_CSQRT_FN:
1345 3053 : return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
1346 3053 : arg_real, arg_imag, format);
1347 :
1348 679 : CASE_CFN_CASIN:
1349 679 : CASE_CFN_CASIN_FN:
1350 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asin,
1351 679 : arg_real, arg_imag, format);
1352 :
1353 728 : CASE_CFN_CACOS:
1354 728 : CASE_CFN_CACOS_FN:
1355 728 : return do_mpc_arg1 (result_real, result_imag, mpc_acos,
1356 728 : arg_real, arg_imag, format);
1357 :
1358 679 : CASE_CFN_CATAN:
1359 679 : CASE_CFN_CATAN_FN:
1360 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atan,
1361 679 : arg_real, arg_imag, format);
1362 :
1363 679 : CASE_CFN_CASINH:
1364 679 : CASE_CFN_CASINH_FN:
1365 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
1366 679 : arg_real, arg_imag, format);
1367 :
1368 745 : CASE_CFN_CACOSH:
1369 745 : CASE_CFN_CACOSH_FN:
1370 745 : return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
1371 745 : arg_real, arg_imag, format);
1372 :
1373 679 : CASE_CFN_CATANH:
1374 679 : CASE_CFN_CATANH_FN:
1375 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1376 679 : arg_real, arg_imag, format);
1377 :
1378 1583 : CASE_CFN_CEXP:
1379 1583 : CASE_CFN_CEXP_FN:
1380 1583 : return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1381 1583 : arg_real, arg_imag, format);
1382 :
1383 : default:
1384 : return false;
1385 : }
1386 : }
1387 :
1388 : /* Subroutine of fold_const_call, with the same interface. Handle cases
1389 : where the arguments and result are numerical. */
1390 :
1391 : static tree
1392 20750154 : fold_const_call_1 (combined_fn fn, tree type, tree arg)
1393 : {
1394 20750154 : machine_mode mode = TYPE_MODE (type);
1395 20750154 : machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1396 :
1397 20750154 : if (integer_cst_p (arg))
1398 : {
1399 5975306 : if (INTEGRAL_TYPE_P (type))
1400 : {
1401 2255617 : wide_int result;
1402 2255617 : if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
1403 2255617 : TYPE_PRECISION (type), TREE_TYPE (arg)))
1404 48152 : return wide_int_to_tree (type, result);
1405 2255617 : }
1406 : return NULL_TREE;
1407 : }
1408 :
1409 14774848 : if (real_cst_p (arg))
1410 : {
1411 180003 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1412 180003 : if (mode == arg_mode)
1413 : {
1414 : /* real -> real. */
1415 162369 : REAL_VALUE_TYPE result;
1416 162369 : if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1417 162369 : REAL_MODE_FORMAT (mode)))
1418 100563 : return build_real (type, result);
1419 : }
1420 17634 : else if (COMPLEX_MODE_P (mode)
1421 17784 : && GET_MODE_INNER (mode) == arg_mode)
1422 : {
1423 : /* real -> complex real. */
1424 150 : REAL_VALUE_TYPE result_real, result_imag;
1425 300 : if (fold_const_call_cs (&result_real, &result_imag, fn,
1426 150 : TREE_REAL_CST_PTR (arg),
1427 150 : REAL_MODE_FORMAT (arg_mode)))
1428 300 : return build_complex (type,
1429 150 : build_real (TREE_TYPE (type), result_real),
1430 300 : build_real (TREE_TYPE (type), result_imag));
1431 : }
1432 17484 : else if (INTEGRAL_TYPE_P (type))
1433 : {
1434 : /* real -> int. */
1435 17481 : wide_int result;
1436 17481 : if (fold_const_call_ss (&result, fn,
1437 17481 : TREE_REAL_CST_PTR (arg),
1438 17481 : TYPE_PRECISION (type),
1439 17481 : REAL_MODE_FORMAT (arg_mode)))
1440 11042 : return wide_int_to_tree (type, result);
1441 17481 : }
1442 : return NULL_TREE;
1443 : }
1444 :
1445 14594845 : if (complex_cst_p (arg))
1446 : {
1447 41918 : gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1448 41918 : machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1449 41918 : tree argr = TREE_REALPART (arg);
1450 41918 : tree argi = TREE_IMAGPART (arg);
1451 41918 : if (mode == arg_mode
1452 39622 : && real_cst_p (argr)
1453 81540 : && real_cst_p (argi))
1454 : {
1455 : /* complex real -> complex real. */
1456 39622 : REAL_VALUE_TYPE result_real, result_imag;
1457 79244 : if (fold_const_call_cc (&result_real, &result_imag, fn,
1458 39622 : TREE_REAL_CST_PTR (argr),
1459 39622 : TREE_REAL_CST_PTR (argi),
1460 39622 : REAL_MODE_FORMAT (inner_mode)))
1461 20972 : return build_complex (type,
1462 10486 : build_real (TREE_TYPE (type), result_real),
1463 20972 : build_real (TREE_TYPE (type), result_imag));
1464 : }
1465 31432 : if (mode == inner_mode
1466 2296 : && real_cst_p (argr)
1467 33728 : && real_cst_p (argi))
1468 : {
1469 : /* complex real -> real. */
1470 2296 : REAL_VALUE_TYPE result;
1471 4592 : if (fold_const_call_sc (&result, fn,
1472 2296 : TREE_REAL_CST_PTR (argr),
1473 2296 : TREE_REAL_CST_PTR (argi),
1474 2296 : REAL_MODE_FORMAT (inner_mode)))
1475 1160 : return build_real (type, result);
1476 : }
1477 : return NULL_TREE;
1478 : }
1479 :
1480 : return NULL_TREE;
1481 : }
1482 :
1483 : /* Try to fold FN (ARG) to a constant. Return the constant on success,
1484 : otherwise return null. TYPE is the type of the return value. */
1485 :
1486 : tree
1487 21719607 : fold_const_call (combined_fn fn, tree type, tree arg)
1488 : {
1489 21719607 : switch (fn)
1490 : {
1491 565199 : case CFN_BUILT_IN_STRLEN:
1492 565199 : if (const char *str = c_getstr (arg))
1493 90806 : return build_int_cst (type, strlen (str));
1494 : return NULL_TREE;
1495 :
1496 229064 : CASE_CFN_NAN:
1497 229064 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN):
1498 229064 : case CFN_BUILT_IN_NAND32:
1499 229064 : case CFN_BUILT_IN_NAND64:
1500 229064 : case CFN_BUILT_IN_NAND128:
1501 229064 : case CFN_BUILT_IN_NAND64X:
1502 229064 : return fold_const_builtin_nan (type, arg, true);
1503 :
1504 166521 : CASE_CFN_NANS:
1505 166521 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
1506 166521 : case CFN_BUILT_IN_NANSF16B:
1507 166521 : case CFN_BUILT_IN_NANSD32:
1508 166521 : case CFN_BUILT_IN_NANSD64:
1509 166521 : case CFN_BUILT_IN_NANSD128:
1510 166521 : case CFN_BUILT_IN_NANSD64X:
1511 166521 : return fold_const_builtin_nan (type, arg, false);
1512 :
1513 1658 : case CFN_REDUC_PLUS:
1514 1658 : return fold_const_reduction (type, arg, PLUS_EXPR);
1515 :
1516 44 : case CFN_REDUC_MAX:
1517 44 : return fold_const_reduction (type, arg, MAX_EXPR);
1518 :
1519 67 : case CFN_REDUC_MIN:
1520 67 : return fold_const_reduction (type, arg, MIN_EXPR);
1521 :
1522 34 : case CFN_REDUC_AND:
1523 34 : return fold_const_reduction (type, arg, BIT_AND_EXPR);
1524 :
1525 261 : case CFN_REDUC_IOR:
1526 261 : return fold_const_reduction (type, arg, BIT_IOR_EXPR);
1527 :
1528 4 : case CFN_REDUC_XOR:
1529 4 : return fold_const_reduction (type, arg, BIT_XOR_EXPR);
1530 :
1531 6601 : case CFN_VEC_CONVERT:
1532 6601 : return fold_const_vec_convert (type, arg);
1533 :
1534 20750154 : default:
1535 20750154 : return fold_const_call_1 (fn, type, arg);
1536 : }
1537 : }
1538 :
1539 : /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1540 : of type TYPE. */
1541 :
1542 : static tree
1543 0 : fold_const_fold_left (tree type, tree arg0, tree arg1, tree_code code)
1544 : {
1545 0 : if (TREE_CODE (arg1) != VECTOR_CST)
1546 : return NULL_TREE;
1547 :
1548 0 : unsigned HOST_WIDE_INT nelts;
1549 0 : if (!VECTOR_CST_NELTS (arg1).is_constant (&nelts))
1550 : return NULL_TREE;
1551 :
1552 0 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
1553 : {
1554 0 : arg0 = const_binop (code, type, arg0, VECTOR_CST_ELT (arg1, i));
1555 0 : if (arg0 == NULL_TREE || !CONSTANT_CLASS_P (arg0))
1556 : return NULL_TREE;
1557 : }
1558 : return arg0;
1559 : }
1560 :
1561 : /* Fold a call to IFN_VEC_SHL_INSERT (ARG0, ARG1), returning a value
1562 : of type TYPE. */
1563 :
1564 : static tree
1565 0 : fold_const_vec_shl_insert (tree, tree arg0, tree arg1)
1566 : {
1567 0 : if (TREE_CODE (arg0) != VECTOR_CST)
1568 : return NULL_TREE;
1569 :
1570 : /* vec_shl_insert ( dup(CST), CST) -> dup (CST). */
1571 0 : if (tree elem = uniform_vector_p (arg0))
1572 : {
1573 0 : if (operand_equal_p (elem, arg1))
1574 0 : return arg0;
1575 : }
1576 :
1577 : return NULL_TREE;
1578 : }
1579 :
1580 : /* Fold a call to IFN_VEC_EXTRACT (ARG0, ARG1), returning a value
1581 : of type TYPE.
1582 :
1583 : Right now this is only handling uniform vectors, so ARG1 is not
1584 : used. But it could be easily adjusted in the future to handle
1585 : non-uniform vectors by extracting the relevant element. */
1586 :
1587 : static tree
1588 0 : fold_const_vec_extract (tree, tree arg0, tree)
1589 : {
1590 0 : if (TREE_CODE (arg0) != VECTOR_CST)
1591 : return NULL_TREE;
1592 :
1593 : /* vec_extract ( dup(CST), CST) -> dup (CST). */
1594 0 : if (tree elem = uniform_vector_p (arg0))
1595 : return elem;
1596 :
1597 : return NULL_TREE;
1598 : }
1599 :
1600 : /* Try to fold scalar integer IFN_SAT_ADD with operands OP0 and OP1. */
1601 :
1602 : static tree
1603 72 : fold_internal_fn_sat_add (tree type, tree op0, tree op1)
1604 : {
1605 72 : if (!INTEGRAL_NB_TYPE_P (type))
1606 : return NULL_TREE;
1607 :
1608 0 : if (TREE_CODE (op0) != INTEGER_CST
1609 0 : || TREE_CODE (op1) != INTEGER_CST)
1610 : return NULL_TREE;
1611 :
1612 0 : wi::overflow_type overflow;
1613 0 : unsigned int prec = TYPE_PRECISION (type);
1614 0 : wide_int result = wi::add (wi::to_wide (op0), wi::to_wide (op1),
1615 0 : TYPE_SIGN (type), &overflow);
1616 :
1617 0 : if (overflow != wi::OVF_NONE)
1618 : {
1619 0 : if (TYPE_UNSIGNED (type))
1620 0 : result = wi::max_value (prec, UNSIGNED);
1621 0 : else if (overflow == wi::OVF_OVERFLOW)
1622 0 : result = wi::max_value (prec, SIGNED);
1623 0 : else if (overflow == wi::OVF_UNDERFLOW)
1624 0 : result = wi::min_value (prec, SIGNED);
1625 : else
1626 : return NULL_TREE;
1627 : }
1628 :
1629 0 : return wide_int_to_tree (type, result);
1630 0 : }
1631 :
1632 : /* Try to evaluate:
1633 :
1634 : *RESULT = FN (*ARG0, *ARG1)
1635 :
1636 : in format FORMAT. Return true on success. */
1637 :
1638 : static bool
1639 225665 : fold_const_call_sss (real_value *result, combined_fn fn,
1640 : const real_value *arg0, const real_value *arg1,
1641 : const real_format *format)
1642 : {
1643 225665 : switch (fn)
1644 : {
1645 6774 : CASE_CFN_DREM:
1646 6774 : CASE_CFN_REMAINDER:
1647 6774 : CASE_CFN_REMAINDER_FN:
1648 6774 : return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1649 :
1650 986 : CASE_CFN_ATAN2:
1651 986 : CASE_CFN_ATAN2_FN:
1652 986 : return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1653 :
1654 : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
1655 84 : CASE_CFN_ATAN2PI:
1656 84 : CASE_CFN_ATAN2PI_FN:
1657 84 : return do_mpfr_arg2 (result, mpfr_atan2pi, arg0, arg1, format);
1658 : #endif
1659 :
1660 572 : CASE_CFN_FDIM:
1661 572 : CASE_CFN_FDIM_FN:
1662 572 : return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1663 :
1664 517 : CASE_CFN_FMOD:
1665 517 : CASE_CFN_FMOD_FN:
1666 517 : return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
1667 :
1668 578 : CASE_CFN_HYPOT:
1669 578 : CASE_CFN_HYPOT_FN:
1670 578 : return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1671 :
1672 70630 : CASE_CFN_COPYSIGN:
1673 70630 : CASE_CFN_COPYSIGN_FN:
1674 70630 : *result = *arg0;
1675 70630 : real_copysign (result, arg1);
1676 70630 : return true;
1677 :
1678 3161 : CASE_CFN_FMIN:
1679 3161 : CASE_CFN_FMIN_FN:
1680 3161 : return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1681 :
1682 3161 : CASE_CFN_FMAX:
1683 3161 : CASE_CFN_FMAX_FN:
1684 3161 : return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1685 :
1686 126668 : CASE_CFN_POW:
1687 126668 : CASE_CFN_POW_FN:
1688 126668 : return fold_const_pow (result, arg0, arg1, format);
1689 :
1690 12534 : CASE_CFN_NEXTAFTER:
1691 12534 : CASE_CFN_NEXTAFTER_FN:
1692 12534 : case CFN_BUILT_IN_NEXTAFTERF16B:
1693 12534 : CASE_CFN_NEXTTOWARD:
1694 12534 : return fold_const_nextafter (result, arg0, arg1, format);
1695 :
1696 : default:
1697 : return false;
1698 : }
1699 : }
1700 :
1701 : /* Try to evaluate:
1702 :
1703 : *RESULT = FN (*ARG0, ARG1)
1704 :
1705 : where FORMAT is the format of *RESULT and *ARG0. Return true on
1706 : success. */
1707 :
1708 : static bool
1709 25704 : fold_const_call_sss (real_value *result, combined_fn fn,
1710 : const real_value *arg0, const wide_int_ref &arg1,
1711 : const real_format *format)
1712 : {
1713 25704 : switch (fn)
1714 : {
1715 7914 : CASE_CFN_LDEXP:
1716 7914 : CASE_CFN_LDEXP_FN:
1717 7914 : return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1718 :
1719 17392 : CASE_CFN_SCALBN:
1720 17392 : CASE_CFN_SCALBN_FN:
1721 17392 : CASE_CFN_SCALBLN:
1722 17392 : CASE_CFN_SCALBLN_FN:
1723 17392 : return (format->b == 2
1724 17392 : && fold_const_builtin_load_exponent (result, arg0, arg1,
1725 : format));
1726 :
1727 389 : CASE_CFN_POWI:
1728 : /* Avoid the folding if flag_signaling_nans is on and
1729 : operand is a signaling NaN. */
1730 389 : if (!flag_unsafe_math_optimizations
1731 376 : && flag_signaling_nans
1732 404 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
1733 : return false;
1734 :
1735 389 : real_powi (result, format, arg0, arg1.to_shwi ());
1736 389 : return true;
1737 :
1738 : default:
1739 : return false;
1740 : }
1741 : }
1742 :
1743 : /* Try to evaluate:
1744 :
1745 : *RESULT = FN (ARG0, *ARG1)
1746 :
1747 : where FORMAT is the format of *RESULT and *ARG1. Return true on
1748 : success. */
1749 :
1750 : static bool
1751 6434 : fold_const_call_sss (real_value *result, combined_fn fn,
1752 : const wide_int_ref &arg0, const real_value *arg1,
1753 : const real_format *format)
1754 : {
1755 6434 : switch (fn)
1756 : {
1757 1220 : CASE_CFN_JN:
1758 1220 : return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1759 :
1760 5205 : CASE_CFN_YN:
1761 5205 : return (real_compare (GT_EXPR, arg1, &dconst0)
1762 5205 : && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1763 :
1764 : default:
1765 : return false;
1766 : }
1767 : }
1768 :
1769 : /* Try to evaluate:
1770 :
1771 : *RESULT = FN (ARG0, ARG1)
1772 :
1773 : where ARG_TYPE is the type of ARG0 and PRECISION is the number of bits in
1774 : the result. Return true on success. */
1775 :
1776 : static bool
1777 388234 : fold_const_call_sss (wide_int *result, combined_fn fn,
1778 : const wide_int_ref &arg0, const wide_int_ref &arg1,
1779 : unsigned int precision, tree arg_type ATTRIBUTE_UNUSED)
1780 : {
1781 388234 : switch (fn)
1782 : {
1783 16965 : case CFN_CLZ:
1784 16965 : case CFN_BUILT_IN_CLZG:
1785 16965 : {
1786 16965 : int tmp;
1787 16965 : if (wi::ne_p (arg0, 0))
1788 16841 : tmp = wi::clz (arg0);
1789 : else
1790 124 : tmp = arg1.to_shwi ();
1791 16965 : *result = wi::shwi (tmp, precision);
1792 16965 : return true;
1793 : }
1794 :
1795 11843 : case CFN_CTZ:
1796 11843 : case CFN_BUILT_IN_CTZG:
1797 11843 : {
1798 11843 : int tmp;
1799 11843 : if (wi::ne_p (arg0, 0))
1800 11751 : tmp = wi::ctz (arg0);
1801 : else
1802 92 : tmp = arg1.to_shwi ();
1803 11843 : *result = wi::shwi (tmp, precision);
1804 11843 : return true;
1805 : }
1806 :
1807 40 : case CFN_CLMUL:
1808 40 : *result = wi::clmul (arg0, arg1, precision);
1809 40 : return true;
1810 :
1811 : default:
1812 : return false;
1813 : }
1814 : }
1815 :
1816 : /* Try to evaluate:
1817 :
1818 : RESULT = fn (ARG0, ARG1)
1819 :
1820 : where FORMAT is the format of the real and imaginary parts of RESULT
1821 : (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1822 : and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1823 :
1824 : static bool
1825 32067 : fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1826 : combined_fn fn, const real_value *arg0_real,
1827 : const real_value *arg0_imag, const real_value *arg1_real,
1828 : const real_value *arg1_imag, const real_format *format)
1829 : {
1830 0 : switch (fn)
1831 : {
1832 32067 : CASE_CFN_CPOW:
1833 32067 : CASE_CFN_CPOW_FN:
1834 32067 : return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1835 0 : arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1836 :
1837 : default:
1838 : return false;
1839 : }
1840 : }
1841 :
1842 : /* Subroutine of fold_const_call, with the same interface. Handle cases
1843 : where the arguments and result are numerical. */
1844 :
1845 : static tree
1846 16743281 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1847 : {
1848 16743281 : machine_mode mode = TYPE_MODE (type);
1849 16743281 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1850 16743281 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1851 :
1852 16743281 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
1853 : {
1854 426746 : if (INTEGRAL_TYPE_P (type))
1855 : {
1856 388234 : wide_int result;
1857 388234 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1858 776468 : wi::to_wide (arg1), TYPE_PRECISION (type),
1859 388234 : TREE_TYPE (arg0)))
1860 28848 : return wide_int_to_tree (type, result);
1861 388234 : }
1862 : return NULL_TREE;
1863 : }
1864 :
1865 16316535 : if (mode == arg0_mode
1866 11130390 : && real_cst_p (arg0)
1867 17574362 : && real_cst_p (arg1))
1868 : {
1869 225665 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1870 225665 : REAL_VALUE_TYPE result;
1871 225665 : if (arg0_mode == arg1_mode)
1872 : {
1873 : /* real, real -> real. */
1874 450750 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1875 225375 : TREE_REAL_CST_PTR (arg1),
1876 225375 : REAL_MODE_FORMAT (mode)))
1877 193834 : return build_real (type, result);
1878 : }
1879 290 : else if (arg1_mode == TYPE_MODE (long_double_type_node))
1880 290 : switch (fn)
1881 : {
1882 290 : CASE_CFN_NEXTTOWARD:
1883 : /* real, long double -> real. */
1884 580 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1885 290 : TREE_REAL_CST_PTR (arg1),
1886 290 : REAL_MODE_FORMAT (mode)))
1887 228 : return build_real (type, result);
1888 : break;
1889 : default:
1890 : break;
1891 : }
1892 : return NULL_TREE;
1893 : }
1894 :
1895 16090870 : if (real_cst_p (arg0)
1896 16090870 : && integer_cst_p (arg1))
1897 : {
1898 25707 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1899 25707 : if (mode == arg0_mode)
1900 : {
1901 : /* real, int -> real. */
1902 25704 : REAL_VALUE_TYPE result;
1903 25704 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1904 51408 : wi::to_wide (arg1),
1905 25704 : REAL_MODE_FORMAT (mode)))
1906 5515 : return build_real (type, result);
1907 : }
1908 : return NULL_TREE;
1909 : }
1910 :
1911 16065163 : if (integer_cst_p (arg0)
1912 16065163 : && real_cst_p (arg1))
1913 : {
1914 6434 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1915 6434 : if (mode == arg1_mode)
1916 : {
1917 : /* int, real -> real. */
1918 6434 : REAL_VALUE_TYPE result;
1919 12868 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1920 6434 : TREE_REAL_CST_PTR (arg1),
1921 6434 : REAL_MODE_FORMAT (mode)))
1922 1755 : return build_real (type, result);
1923 : }
1924 : return NULL_TREE;
1925 : }
1926 :
1927 16058729 : if (arg0_mode == arg1_mode
1928 13144419 : && complex_cst_p (arg0)
1929 16090796 : && complex_cst_p (arg1))
1930 : {
1931 32067 : gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1932 32067 : machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1933 32067 : tree arg0r = TREE_REALPART (arg0);
1934 32067 : tree arg0i = TREE_IMAGPART (arg0);
1935 32067 : tree arg1r = TREE_REALPART (arg1);
1936 32067 : tree arg1i = TREE_IMAGPART (arg1);
1937 32067 : if (mode == arg0_mode
1938 32067 : && real_cst_p (arg0r)
1939 32067 : && real_cst_p (arg0i)
1940 32067 : && real_cst_p (arg1r)
1941 64134 : && real_cst_p (arg1i))
1942 : {
1943 : /* complex real, complex real -> complex real. */
1944 32067 : REAL_VALUE_TYPE result_real, result_imag;
1945 64134 : if (fold_const_call_ccc (&result_real, &result_imag, fn,
1946 32067 : TREE_REAL_CST_PTR (arg0r),
1947 32067 : TREE_REAL_CST_PTR (arg0i),
1948 32067 : TREE_REAL_CST_PTR (arg1r),
1949 32067 : TREE_REAL_CST_PTR (arg1i),
1950 32067 : REAL_MODE_FORMAT (inner_mode)))
1951 21470 : return build_complex (type,
1952 10735 : build_real (TREE_TYPE (type), result_real),
1953 21470 : build_real (TREE_TYPE (type), result_imag));
1954 : }
1955 : return NULL_TREE;
1956 : }
1957 :
1958 : return NULL_TREE;
1959 : }
1960 :
1961 : /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1962 : otherwise return null. TYPE is the type of the return value. */
1963 :
1964 : tree
1965 19529018 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1966 : {
1967 19529018 : const char *p0, *p1;
1968 19529018 : char c;
1969 19529018 : tree_code subcode;
1970 19529018 : switch (fn)
1971 : {
1972 2692 : case CFN_BUILT_IN_STRSPN:
1973 2692 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1974 123 : return build_int_cst (type, strspn (p0, p1));
1975 : return NULL_TREE;
1976 :
1977 2596 : case CFN_BUILT_IN_STRCSPN:
1978 2596 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1979 123 : return build_int_cst (type, strcspn (p0, p1));
1980 : return NULL_TREE;
1981 :
1982 2225438 : case CFN_BUILT_IN_STRCMP:
1983 2225438 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1984 24954 : return build_cmp_result (type, strcmp (p0, p1));
1985 : return NULL_TREE;
1986 :
1987 309 : case CFN_BUILT_IN_STRCASECMP:
1988 309 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1989 : {
1990 10 : int r = strcmp (p0, p1);
1991 10 : if (r == 0)
1992 1 : return build_cmp_result (type, r);
1993 : }
1994 : return NULL_TREE;
1995 :
1996 209144 : case CFN_BUILT_IN_INDEX:
1997 209144 : case CFN_BUILT_IN_STRCHR:
1998 209144 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1999 : {
2000 163 : const char *r = strchr (p0, c);
2001 163 : if (r == NULL)
2002 38 : return build_int_cst (type, 0);
2003 125 : return fold_convert (type,
2004 : fold_build_pointer_plus_hwi (arg0, r - p0));
2005 : }
2006 : return NULL_TREE;
2007 :
2008 199397 : case CFN_BUILT_IN_RINDEX:
2009 199397 : case CFN_BUILT_IN_STRRCHR:
2010 199397 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
2011 : {
2012 124 : const char *r = strrchr (p0, c);
2013 124 : if (r == NULL)
2014 35 : return build_int_cst (type, 0);
2015 89 : return fold_convert (type,
2016 : fold_build_pointer_plus_hwi (arg0, r - p0));
2017 : }
2018 : return NULL_TREE;
2019 :
2020 107916 : case CFN_BUILT_IN_STRSTR:
2021 107916 : if ((p1 = c_getstr (arg1)))
2022 : {
2023 6002 : if ((p0 = c_getstr (arg0)))
2024 : {
2025 209 : const char *r = strstr (p0, p1);
2026 209 : if (r == NULL)
2027 44 : return build_int_cst (type, 0);
2028 165 : return fold_convert (type,
2029 : fold_build_pointer_plus_hwi (arg0, r - p0));
2030 : }
2031 5793 : if (*p1 == '\0')
2032 13 : return fold_convert (type, arg0);
2033 : }
2034 : return NULL_TREE;
2035 :
2036 19607 : case CFN_BUILT_IN_STRNLEN:
2037 19607 : if ((p0 = c_getstr (arg0)))
2038 : {
2039 3178 : unsigned HOST_WIDE_INT s1 = 0;
2040 3178 : if (!size_t_cst_p (arg1, &s1))
2041 : return NULL_TREE;
2042 :
2043 442 : return build_int_cst (type, strnlen (p0, s1));
2044 : }
2045 : return NULL_TREE;
2046 :
2047 0 : case CFN_FOLD_LEFT_PLUS:
2048 0 : return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
2049 :
2050 0 : case CFN_VEC_SHL_INSERT:
2051 0 : return fold_const_vec_shl_insert (type, arg0, arg1);
2052 :
2053 0 : case CFN_VEC_EXTRACT:
2054 0 : return fold_const_vec_extract (type, arg0, arg1);
2055 :
2056 72 : case CFN_SAT_ADD:
2057 72 : return fold_internal_fn_sat_add (type, arg0, arg1);
2058 :
2059 6594 : case CFN_UBSAN_CHECK_ADD:
2060 6594 : case CFN_ADD_OVERFLOW:
2061 6594 : subcode = PLUS_EXPR;
2062 6594 : goto arith_overflow;
2063 :
2064 6391 : case CFN_UBSAN_CHECK_SUB:
2065 6391 : case CFN_SUB_OVERFLOW:
2066 6391 : subcode = MINUS_EXPR;
2067 6391 : goto arith_overflow;
2068 :
2069 5541 : case CFN_UBSAN_CHECK_MUL:
2070 5541 : case CFN_MUL_OVERFLOW:
2071 5541 : subcode = MULT_EXPR;
2072 5541 : goto arith_overflow;
2073 :
2074 18526 : arith_overflow:
2075 18526 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
2076 : {
2077 17656 : tree itype
2078 17656 : = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type;
2079 17656 : bool ovf = false;
2080 17656 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
2081 17656 : fold_convert (itype, arg1));
2082 17656 : if (!r || TREE_CODE (r) != INTEGER_CST)
2083 : return NULL_TREE;
2084 17656 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
2085 : ovf = true;
2086 17656 : if (TREE_OVERFLOW (r))
2087 3443 : r = drop_tree_overflow (r);
2088 17656 : if (itype == type)
2089 : {
2090 2803 : if (ovf)
2091 : return NULL_TREE;
2092 1780 : return r;
2093 : }
2094 : else
2095 14853 : return build_complex (type, r, build_int_cst (itype, ovf));
2096 : }
2097 : return NULL_TREE;
2098 :
2099 40 : case CFN_CLMUL_WIDEN:
2100 40 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
2101 : {
2102 40 : unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
2103 40 : wide_int res = wi::clmul (wi::to_wide (arg0), wi::to_wide (arg1),
2104 80 : prec * 2);
2105 40 : wide_int lo = wide_int::from (res, prec, UNSIGNED);
2106 40 : wide_int hi = wide_int::from (wi::rshift (res, prec, UNSIGNED),
2107 80 : prec, UNSIGNED);
2108 80 : return build_complex (type, wide_int_to_tree (TREE_TYPE (type), lo),
2109 80 : wide_int_to_tree (TREE_TYPE (type), hi));
2110 40 : }
2111 : return NULL_TREE;
2112 :
2113 16743281 : default:
2114 16743281 : return fold_const_call_1 (fn, type, arg0, arg1);
2115 : }
2116 : }
2117 :
2118 : /* Try to evaluate:
2119 :
2120 : *RESULT = FN (*ARG0, *ARG1, *ARG2)
2121 :
2122 : in format FORMAT. Return true on success. */
2123 :
2124 : static bool
2125 2534 : fold_const_call_ssss (real_value *result, combined_fn fn,
2126 : const real_value *arg0, const real_value *arg1,
2127 : const real_value *arg2, const real_format *format)
2128 : {
2129 2534 : switch (fn)
2130 : {
2131 2534 : CASE_CFN_FMA:
2132 2534 : CASE_CFN_FMA_FN:
2133 2534 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
2134 :
2135 0 : case CFN_FMS:
2136 0 : {
2137 0 : real_value new_arg2 = real_value_negate (arg2);
2138 0 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, &new_arg2, format);
2139 : }
2140 :
2141 0 : case CFN_FNMA:
2142 0 : {
2143 0 : real_value new_arg0 = real_value_negate (arg0);
2144 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1, arg2, format);
2145 : }
2146 :
2147 0 : case CFN_FNMS:
2148 0 : {
2149 0 : real_value new_arg0 = real_value_negate (arg0);
2150 0 : real_value new_arg2 = real_value_negate (arg2);
2151 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1,
2152 : &new_arg2, format);
2153 : }
2154 :
2155 : default:
2156 : return false;
2157 : }
2158 : }
2159 :
2160 : /* Subroutine of fold_const_call, with the same interface. Handle cases
2161 : where the arguments and result are numerical. */
2162 :
2163 : static tree
2164 4538447 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
2165 : {
2166 4538447 : machine_mode mode = TYPE_MODE (type);
2167 4538447 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
2168 4538447 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
2169 4538447 : machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
2170 :
2171 4538447 : if (arg0_mode == arg1_mode
2172 4538447 : && arg0_mode == arg2_mode
2173 1996424 : && real_cst_p (arg0)
2174 2712 : && real_cst_p (arg1)
2175 4541087 : && real_cst_p (arg2))
2176 : {
2177 2534 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
2178 2534 : if (mode == arg0_mode)
2179 : {
2180 : /* real, real, real -> real. */
2181 2534 : REAL_VALUE_TYPE result;
2182 5068 : if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
2183 2534 : TREE_REAL_CST_PTR (arg1),
2184 2534 : TREE_REAL_CST_PTR (arg2),
2185 2534 : REAL_MODE_FORMAT (mode)))
2186 1060 : return build_real (type, result);
2187 : }
2188 : return NULL_TREE;
2189 : }
2190 :
2191 : return NULL_TREE;
2192 : }
2193 :
2194 : /* Given a CRC polynomial POLYNOMIAL_ARG, the current CRC value in CRC_ARG and
2195 : new data item DATA_ARG. Compute the updated CRC result value in type TYPE.
2196 : The CRC direction is inferred from FN (forward vs reversed). */
2197 :
2198 : static tree
2199 728 : fold_const_crc (internal_fn fn, tree type, tree crc_arg, tree data_arg,
2200 : tree polynomial_arg)
2201 : {
2202 728 : if (!integer_cst_p (crc_arg)
2203 136 : || !integer_cst_p (data_arg)
2204 783 : || !integer_cst_p (polynomial_arg))
2205 : return NULL_TREE;
2206 :
2207 55 : unsigned int crc_bits = TYPE_PRECISION (type);
2208 55 : unsigned int data_bits = TYPE_PRECISION (TREE_TYPE (data_arg));
2209 :
2210 55 : if ((data_bits != 8 && data_bits != 16 && data_bits != 32 && data_bits != 64)
2211 55 : || (crc_bits != 8 && crc_bits != 16 && crc_bits != 32 && crc_bits != 64)
2212 55 : || data_bits > crc_bits)
2213 : return NULL_TREE;
2214 :
2215 55 : if (!tree_fits_uhwi_p (crc_arg)
2216 55 : || !tree_fits_uhwi_p (data_arg)
2217 55 : || !tree_fits_uhwi_p (polynomial_arg))
2218 : return NULL_TREE;
2219 :
2220 55 : unsigned HOST_WIDE_INT crc = tree_to_uhwi (crc_arg);
2221 55 : unsigned HOST_WIDE_INT data = tree_to_uhwi (data_arg);
2222 55 : unsigned HOST_WIDE_INT polynomial = tree_to_uhwi (polynomial_arg);
2223 :
2224 55 : if (fn == IFN_CRC_REV)
2225 36 : crc = calculate_reversed_crc (crc, data, polynomial, crc_bits, data_bits);
2226 : else
2227 19 : crc = calculate_crc (crc, data, polynomial, crc_bits, data_bits);
2228 :
2229 55 : return build_int_cstu (type, crc);
2230 : }
2231 :
2232 : /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
2233 : success, otherwise return null. TYPE is the type of the return value. */
2234 :
2235 : tree
2236 7405850 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
2237 : {
2238 7405850 : const char *p0, *p1;
2239 7405850 : char c;
2240 7405850 : unsigned HOST_WIDE_INT s0, s1, s2 = 0;
2241 7405850 : switch (fn)
2242 : {
2243 45129 : case CFN_BUILT_IN_STRNCMP:
2244 45129 : if (!size_t_cst_p (arg2, &s2))
2245 : return NULL_TREE;
2246 29114 : if (s2 == 0
2247 237 : && !TREE_SIDE_EFFECTS (arg0)
2248 29323 : && !TREE_SIDE_EFFECTS (arg1))
2249 209 : return build_int_cst (type, 0);
2250 28905 : else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
2251 573 : return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX)));
2252 : return NULL_TREE;
2253 :
2254 8471 : case CFN_BUILT_IN_STRNCASECMP:
2255 8471 : if (!size_t_cst_p (arg2, &s2))
2256 : return NULL_TREE;
2257 8150 : if (s2 == 0
2258 9 : && !TREE_SIDE_EFFECTS (arg0)
2259 8155 : && !TREE_SIDE_EFFECTS (arg1))
2260 5 : return build_int_cst (type, 0);
2261 8145 : else if ((p0 = c_getstr (arg0))
2262 724 : && (p1 = c_getstr (arg1))
2263 8285 : && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0)
2264 3 : return build_int_cst (type, 0);
2265 : return NULL_TREE;
2266 :
2267 2565453 : case CFN_BUILT_IN_BCMP:
2268 2565453 : case CFN_BUILT_IN_MEMCMP:
2269 2565453 : if (!size_t_cst_p (arg2, &s2))
2270 : return NULL_TREE;
2271 1625710 : if (s2 == 0
2272 13007 : && !TREE_SIDE_EFFECTS (arg0)
2273 1638717 : && !TREE_SIDE_EFFECTS (arg1))
2274 13007 : return build_int_cst (type, 0);
2275 1612703 : if ((p0 = getbyterep (arg0, &s0))
2276 12888 : && (p1 = getbyterep (arg1, &s1))
2277 3753 : && s2 <= s0
2278 1616016 : && s2 <= s1)
2279 3311 : return build_cmp_result (type, memcmp (p0, p1, s2));
2280 : return NULL_TREE;
2281 :
2282 247492 : case CFN_BUILT_IN_MEMCHR:
2283 247492 : if (!size_t_cst_p (arg2, &s2))
2284 : return NULL_TREE;
2285 7578 : if (s2 == 0
2286 151 : && !TREE_SIDE_EFFECTS (arg0)
2287 7712 : && !TREE_SIDE_EFFECTS (arg1))
2288 131 : return build_int_cst (type, 0);
2289 7447 : if ((p0 = getbyterep (arg0, &s0))
2290 2677 : && s2 <= s0
2291 10005 : && target_char_cst_p (arg1, &c))
2292 : {
2293 1009 : const char *r = (const char *) memchr (p0, c, s2);
2294 1009 : if (r == NULL)
2295 290 : return build_int_cst (type, 0);
2296 719 : return fold_convert (type,
2297 : fold_build_pointer_plus_hwi (arg0, r - p0));
2298 : }
2299 : return NULL_TREE;
2300 :
2301 0 : case CFN_WHILE_ULT:
2302 0 : {
2303 0 : poly_uint64 parg0, parg1;
2304 0 : if (poly_int_tree_p (arg0, &parg0) && poly_int_tree_p (arg1, &parg1))
2305 0 : return fold_while_ult (type, parg0, parg1);
2306 : return NULL_TREE;
2307 : }
2308 :
2309 130 : case CFN_UADDC:
2310 130 : case CFN_USUBC:
2311 130 : if (integer_cst_p (arg0) && integer_cst_p (arg1) && integer_cst_p (arg2))
2312 : {
2313 130 : tree itype = TREE_TYPE (type);
2314 130 : bool ovf = false;
2315 130 : tree_code subcode = fn == CFN_UADDC ? PLUS_EXPR : MINUS_EXPR;
2316 130 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
2317 130 : fold_convert (itype, arg1));
2318 130 : if (!r)
2319 : return NULL_TREE;
2320 130 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
2321 : ovf = true;
2322 130 : tree r2 = int_const_binop (subcode, r, fold_convert (itype, arg2));
2323 130 : if (!r2 || TREE_CODE (r2) != INTEGER_CST)
2324 : return NULL_TREE;
2325 130 : if (arith_overflowed_p (subcode, itype, r, arg2))
2326 0 : ovf = true;
2327 130 : if (TREE_OVERFLOW (r2))
2328 0 : r2 = drop_tree_overflow (r2);
2329 130 : return build_complex (type, r2, build_int_cst (itype, ovf));
2330 : }
2331 : return NULL_TREE;
2332 :
2333 728 : case CFN_BUILT_IN_CRC8_DATA8:
2334 728 : case CFN_BUILT_IN_CRC16_DATA8:
2335 728 : case CFN_BUILT_IN_CRC16_DATA16:
2336 728 : case CFN_BUILT_IN_CRC32_DATA8:
2337 728 : case CFN_BUILT_IN_CRC32_DATA16:
2338 728 : case CFN_BUILT_IN_CRC32_DATA32:
2339 728 : case CFN_BUILT_IN_CRC64_DATA8:
2340 728 : case CFN_BUILT_IN_CRC64_DATA16:
2341 728 : case CFN_BUILT_IN_CRC64_DATA32:
2342 728 : case CFN_BUILT_IN_CRC64_DATA64:
2343 728 : case CFN_BUILT_IN_REV_CRC8_DATA8:
2344 728 : case CFN_BUILT_IN_REV_CRC16_DATA8:
2345 728 : case CFN_BUILT_IN_REV_CRC16_DATA16:
2346 728 : case CFN_BUILT_IN_REV_CRC32_DATA8:
2347 728 : case CFN_BUILT_IN_REV_CRC32_DATA16:
2348 728 : case CFN_BUILT_IN_REV_CRC32_DATA32:
2349 728 : case CFN_BUILT_IN_REV_CRC64_DATA8:
2350 728 : case CFN_BUILT_IN_REV_CRC64_DATA16:
2351 728 : case CFN_BUILT_IN_REV_CRC64_DATA32:
2352 728 : case CFN_BUILT_IN_REV_CRC64_DATA64:
2353 728 : return fold_const_crc (associated_internal_fn (fn, type),
2354 728 : type, arg0, arg1, arg2);
2355 :
2356 4538447 : default:
2357 4538447 : return fold_const_call_1 (fn, type, arg0, arg1, arg2);
2358 : }
2359 : }
|