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 42090431 : integer_cst_p (tree t)
40 : {
41 42090431 : return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
42 : }
43 :
44 : static inline bool
45 45645340 : real_cst_p (tree t)
46 : {
47 45645340 : return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
48 : }
49 :
50 : static inline bool
51 27864765 : complex_cst_p (tree t)
52 : {
53 27864765 : 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 2871735 : size_t_cst_p (tree t, unsigned HOST_WIDE_INT *size_out)
61 : {
62 2871735 : if (types_compatible_p (size_type_node, TREE_TYPE (t))
63 2871580 : && integer_cst_p (t)
64 4548191 : && tree_fits_uhwi_p (t))
65 : {
66 1676456 : *size_out = tree_to_uhwi (t);
67 1676456 : 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 66409 : build_cmp_result (tree type, int res)
78 : {
79 71044 : 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 243495 : 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 243495 : if (!mpfr_number_p (m)
95 228490 : || mpfr_overflow_p ()
96 228490 : || mpfr_underflow_p ()
97 468441 : || (flag_rounding_math && inexact))
98 : return false;
99 :
100 224905 : REAL_VALUE_TYPE tmp;
101 224905 : 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 224905 : if (!real_isfinite (&tmp)
107 224905 : || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
108 : return false;
109 :
110 224905 : real_convert (result, format, &tmp);
111 224905 : 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 109098 : 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 109098 : if (format->b != 2 || !real_isfinite (arg))
129 : return false;
130 :
131 106356 : int prec = format->p;
132 106356 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
133 :
134 106356 : auto_mpfr m (prec);
135 106356 : mpfr_from_real (m, arg, MPFR_RNDN);
136 106356 : mpfr_clear_flags ();
137 106356 : bool inexact = func (m, m, rnd);
138 106356 : bool ok = do_mpfr_ckconv (result, m, inexact, format);
139 :
140 106356 : return ok;
141 106356 : }
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 143628 : 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 143628 : if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
190 : return false;
191 :
192 132550 : int prec = format->p;
193 132550 : mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN;
194 132550 : mpfr_t m0, m1;
195 :
196 132550 : mpfr_inits2 (prec, m0, m1, NULL);
197 132550 : mpfr_from_real (m0, arg0, MPFR_RNDN);
198 132550 : mpfr_from_real (m1, arg1, MPFR_RNDN);
199 132550 : mpfr_clear_flags ();
200 132550 : bool inexact = func (m0, m0, m1, rnd);
201 132550 : bool ok = do_mpfr_ckconv (result, m0, inexact, format);
202 132550 : mpfr_clears (m0, m1, NULL);
203 :
204 132550 : 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 4464 : fold_const_logb (real_value *result, const real_value *arg,
397 : const real_format *format)
398 : {
399 4464 : switch (arg->cl)
400 : {
401 107 : case rvc_nan:
402 : /* If arg is +-NaN, then return it. */
403 107 : *result = *arg;
404 107 : 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 1690 : 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 1690 : if (!real_isfinite (arg))
478 : return false;
479 :
480 1690 : real_value rounded;
481 1690 : fn (&rounded, format, arg);
482 :
483 1690 : bool fail = false;
484 1690 : *result = real_to_integer (&rounded, &fail, precision);
485 1690 : 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 126598 : fold_const_pow (real_value *result, const real_value *arg0,
496 : const real_value *arg1, const real_format *format)
497 : {
498 126598 : if (flag_signaling_nans
499 126598 : && (REAL_VALUE_ISSIGNALING_NAN (*arg0)
500 0 : || REAL_VALUE_ISSIGNALING_NAN (*arg1)))
501 : return false;
502 :
503 126598 : if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
504 : {
505 116221 : if (flag_errno_math)
506 115856 : 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 116221 : 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 12600 : fold_const_nextafter (real_value *result, const real_value *arg0,
619 : const real_value *arg1, const real_format *format)
620 : {
621 12600 : if (REAL_VALUE_ISSIGNALING_NAN (*arg0)
622 12600 : || 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 12600 : if (format->pnan < format->p
628 12600 : || format->b == 10
629 12600 : || !format->has_inf
630 12600 : || !format->has_denorm)
631 : return false;
632 :
633 19484 : 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 12600 : && (flag_trapping_math || flag_errno_math))
637 : return false;
638 : /* Similarly for nextafter (0, 1) raising underflow. */
639 5716 : else if (flag_trapping_math
640 5209 : && arg0->cl == rvc_zero
641 4568 : && 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 25357 : 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 25357 : 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 42928 : 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 9875 : if (!flag_unsafe_math_optimizations
674 9870 : && flag_signaling_nans
675 9995 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
676 : return false;
677 :
678 9875 : REAL_VALUE_TYPE initial_result;
679 9875 : real_ldexp (&initial_result, arg0, arg1.to_shwi ());
680 :
681 : /* Ensure we didn't overflow. */
682 9875 : if (real_isinf (&initial_result))
683 : return false;
684 :
685 : /* Only proceed if the target mode can hold the
686 : resulting value. */
687 9855 : *result = real_value_truncate (format, initial_result);
688 9855 : 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 395345 : fold_const_builtin_nan (tree type, tree arg, bool quiet)
697 : {
698 395345 : REAL_VALUE_TYPE real;
699 395345 : const char *str = c_getstr (arg);
700 395345 : if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
701 394876 : 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 2066 : fold_const_reduction (tree type, tree arg, tree_code code)
709 : {
710 2066 : if (TREE_CODE (arg) != VECTOR_CST)
711 : return NULL_TREE;
712 :
713 4132 : bool idempotent_p = (code == MAX_EXPR
714 2066 : || code == MIN_EXPR
715 2066 : || code == BIT_AND_EXPR
716 : || code == BIT_IOR_EXPR);
717 :
718 2066 : 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 2066 : bool singleton_snan_p
723 : = (nelts == 1
724 2066 : && tree_expr_maybe_signaling_nan_p (VECTOR_CST_ELT (arg, 0)));
725 :
726 2066 : if (idempotent_p
727 402 : && !VECTOR_CST_STEPPED_P (arg)
728 1122 : && multiple_p (VECTOR_CST_NELTS (arg), nelts)
729 2440 : && !singleton_snan_p)
730 : /* Operating on the first NELTS elements is enough. */
731 : ;
732 1692 : else if (code == BIT_XOR_EXPR
733 2 : && !VECTOR_CST_STEPPED_P (arg)
734 1692 : && multiple_p (VECTOR_CST_NELTS (arg), nelts * 2))
735 : {
736 0 : 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 1692 : else if (!VECTOR_CST_NELTS (arg).is_constant (&nelts))
746 : return NULL_TREE;
747 :
748 2066 : tree res = VECTOR_CST_ELT (arg, 0);
749 27983 : for (unsigned HOST_WIDE_INT i = 1; i < nelts; i++)
750 : {
751 23851 : res = const_binop (code, type, res, VECTOR_CST_ELT (arg, i));
752 23851 : 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 162546 : fold_const_call_ss (real_value *result, combined_fn fn,
839 : const real_value *arg, const real_format *format)
840 : {
841 162546 : if (flag_signaling_nans && REAL_VALUE_ISSIGNALING_NAN (*arg))
842 : return false;
843 :
844 162474 : switch (fn)
845 : {
846 22959 : CASE_CFN_SQRT:
847 22959 : CASE_CFN_SQRT_FN:
848 22959 : return (real_compare (GE_EXPR, arg, &dconst0)
849 22959 : && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
850 :
851 512 : CASE_CFN_CBRT:
852 512 : CASE_CFN_CBRT_FN:
853 512 : return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
854 :
855 1976 : CASE_CFN_ASIN:
856 1976 : CASE_CFN_ASIN_FN:
857 1976 : return (real_compare (GE_EXPR, arg, &dconstm1)
858 1202 : && real_compare (LE_EXPR, arg, &dconst1)
859 2396 : && do_mpfr_arg1 (result, mpfr_asin, arg, format));
860 :
861 2027 : CASE_CFN_ACOS:
862 2027 : CASE_CFN_ACOS_FN:
863 2027 : return (real_compare (GE_EXPR, arg, &dconstm1)
864 1253 : && real_compare (LE_EXPR, arg, &dconst1)
865 2506 : && do_mpfr_arg1 (result, mpfr_acos, arg, format));
866 :
867 552 : CASE_CFN_ATAN:
868 552 : CASE_CFN_ATAN_FN:
869 552 : return do_mpfr_arg1 (result, mpfr_atan, arg, format);
870 :
871 312 : CASE_CFN_ASINH:
872 312 : CASE_CFN_ASINH_FN:
873 312 : return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
874 :
875 1454 : CASE_CFN_ACOSH:
876 1454 : CASE_CFN_ACOSH_FN:
877 1454 : return (real_compare (GE_EXPR, arg, &dconst1)
878 1454 : && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
879 :
880 1901 : CASE_CFN_ATANH:
881 1901 : CASE_CFN_ATANH_FN:
882 1901 : return (real_compare (GE_EXPR, arg, &dconstm1)
883 1901 : && real_compare (LE_EXPR, arg, &dconst1)
884 3802 : && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
885 :
886 979 : CASE_CFN_SIN:
887 979 : CASE_CFN_SIN_FN:
888 979 : return do_mpfr_arg1 (result, mpfr_sin, arg, format);
889 :
890 821 : CASE_CFN_COS:
891 821 : CASE_CFN_COS_FN:
892 821 : return do_mpfr_arg1 (result, mpfr_cos, arg, format);
893 :
894 487 : CASE_CFN_TAN:
895 487 : CASE_CFN_TAN_FN:
896 487 : return do_mpfr_arg1 (result, mpfr_tan, arg, format);
897 :
898 394 : CASE_CFN_SINH:
899 394 : CASE_CFN_SINH_FN:
900 394 : return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
901 :
902 399 : CASE_CFN_COSH:
903 399 : CASE_CFN_COSH_FN:
904 399 : return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
905 :
906 349 : CASE_CFN_TANH:
907 349 : CASE_CFN_TANH_FN:
908 349 : return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
909 :
910 : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
911 55 : CASE_CFN_ACOSPI:
912 55 : CASE_CFN_ACOSPI_FN:
913 55 : return (real_compare (GE_EXPR, arg, &dconstm1)
914 55 : && real_compare (LE_EXPR, arg, &dconst1)
915 110 : && do_mpfr_arg1 (result, mpfr_acospi, arg, format));
916 :
917 60 : CASE_CFN_ASINPI:
918 60 : CASE_CFN_ASINPI_FN:
919 60 : return (real_compare (GE_EXPR, arg, &dconstm1)
920 60 : && real_compare (LE_EXPR, arg, &dconst1)
921 120 : && do_mpfr_arg1 (result, mpfr_asinpi, arg, format));
922 :
923 36 : CASE_CFN_ATANPI:
924 36 : CASE_CFN_ATANPI_FN:
925 36 : return do_mpfr_arg1 (result, mpfr_atanpi, arg, format);
926 :
927 60 : CASE_CFN_COSPI:
928 60 : CASE_CFN_COSPI_FN:
929 60 : return do_mpfr_arg1 (result, mpfr_cospi, arg, format);
930 :
931 60 : CASE_CFN_SINPI:
932 60 : CASE_CFN_SINPI_FN:
933 60 : return do_mpfr_arg1 (result, mpfr_sinpi, arg, format);
934 :
935 72 : CASE_CFN_TANPI:
936 72 : CASE_CFN_TANPI_FN:
937 72 : return do_mpfr_arg1 (result, mpfr_tanpi, arg, format);
938 : #endif
939 :
940 511 : CASE_CFN_ERF:
941 511 : CASE_CFN_ERF_FN:
942 511 : return do_mpfr_arg1 (result, mpfr_erf, arg, format);
943 :
944 554 : CASE_CFN_ERFC:
945 554 : CASE_CFN_ERFC_FN:
946 554 : return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
947 :
948 4924 : CASE_CFN_TGAMMA:
949 4924 : CASE_CFN_TGAMMA_FN:
950 4924 : return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
951 :
952 870 : CASE_CFN_EXP:
953 870 : CASE_CFN_EXP_FN:
954 870 : return do_mpfr_arg1 (result, mpfr_exp, arg, format);
955 :
956 10784 : CASE_CFN_EXP2:
957 10784 : CASE_CFN_EXP2_FN:
958 10784 : return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
959 :
960 698 : CASE_CFN_EXP10:
961 698 : CASE_CFN_POW10:
962 698 : return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
963 :
964 327 : CASE_CFN_EXPM1:
965 327 : CASE_CFN_EXPM1_FN:
966 327 : return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
967 :
968 61033 : CASE_CFN_LOG:
969 61033 : CASE_CFN_LOG_FN:
970 61033 : return (real_compare (GT_EXPR, arg, &dconst0)
971 61033 : && do_mpfr_arg1 (result, mpfr_log, arg, format));
972 :
973 3130 : CASE_CFN_LOG2:
974 3130 : CASE_CFN_LOG2_FN:
975 3130 : return (real_compare (GT_EXPR, arg, &dconst0)
976 3130 : && do_mpfr_arg1 (result, mpfr_log2, arg, format));
977 :
978 3166 : CASE_CFN_LOG10:
979 3166 : CASE_CFN_LOG10_FN:
980 3166 : return (real_compare (GT_EXPR, arg, &dconst0)
981 3166 : && do_mpfr_arg1 (result, mpfr_log10, arg, format));
982 :
983 1921 : CASE_CFN_LOG1P:
984 1921 : CASE_CFN_LOG1P_FN:
985 1921 : return (real_compare (GT_EXPR, arg, &dconstm1)
986 1921 : && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
987 :
988 221 : CASE_CFN_J0:
989 221 : return do_mpfr_arg1 (result, mpfr_j0, arg, format);
990 :
991 221 : CASE_CFN_J1:
992 221 : return do_mpfr_arg1 (result, mpfr_j1, arg, format);
993 :
994 2447 : CASE_CFN_Y0:
995 2447 : return (real_compare (GT_EXPR, arg, &dconst0)
996 2447 : && do_mpfr_arg1 (result, mpfr_y0, arg, format));
997 :
998 2447 : CASE_CFN_Y1:
999 2447 : return (real_compare (GT_EXPR, arg, &dconst0)
1000 2447 : && do_mpfr_arg1 (result, mpfr_y1, arg, format));
1001 :
1002 356 : CASE_CFN_FLOOR:
1003 356 : CASE_CFN_FLOOR_FN:
1004 356 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1005 : {
1006 356 : real_floor (result, format, arg);
1007 356 : return true;
1008 : }
1009 : return false;
1010 :
1011 323 : CASE_CFN_CEIL:
1012 323 : CASE_CFN_CEIL_FN:
1013 323 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1014 : {
1015 323 : real_ceil (result, format, arg);
1016 323 : return true;
1017 : }
1018 : return false;
1019 :
1020 604 : CASE_CFN_TRUNC:
1021 604 : CASE_CFN_TRUNC_FN:
1022 604 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1023 : {
1024 604 : real_trunc (result, format, arg);
1025 604 : return true;
1026 : }
1027 : return false;
1028 :
1029 324 : CASE_CFN_ROUND:
1030 324 : CASE_CFN_ROUND_FN:
1031 324 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1032 : {
1033 324 : real_round (result, format, arg);
1034 324 : return true;
1035 : }
1036 : return false;
1037 :
1038 189 : CASE_CFN_ROUNDEVEN:
1039 189 : CASE_CFN_ROUNDEVEN_FN:
1040 189 : if (!REAL_VALUE_ISSIGNALING_NAN (*arg))
1041 : {
1042 189 : real_roundeven (result, format, arg);
1043 189 : return true;
1044 : }
1045 : return false;
1046 :
1047 4464 : CASE_CFN_LOGB:
1048 4464 : CASE_CFN_LOGB_FN:
1049 4464 : return fold_const_logb (result, arg, format);
1050 :
1051 960 : CASE_CFN_SIGNIFICAND:
1052 960 : return fold_const_significand (result, arg, format);
1053 :
1054 : default:
1055 : return false;
1056 : }
1057 : }
1058 :
1059 : /* Try to evaluate:
1060 :
1061 : *RESULT = FN (*ARG)
1062 :
1063 : where FORMAT is the format of ARG and PRECISION is the number of
1064 : significant bits in the result. Return true on success. */
1065 :
1066 : static bool
1067 17656 : fold_const_call_ss (wide_int *result, combined_fn fn,
1068 : const real_value *arg, unsigned int precision,
1069 : const real_format *format)
1070 : {
1071 17656 : switch (fn)
1072 : {
1073 1930 : CASE_CFN_SIGNBIT:
1074 1930 : if (real_isneg (arg))
1075 994 : *result = wi::one (precision);
1076 : else
1077 936 : *result = wi::zero (precision);
1078 : return true;
1079 :
1080 6485 : CASE_CFN_ILOGB:
1081 6485 : CASE_CFN_ILOGB_FN:
1082 : /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
1083 : Proceed iff radix == 2. In GCC, normalized significands are in
1084 : the range [0.5, 1.0). We want the exponent as if they were
1085 : [1.0, 2.0) so get the exponent and subtract 1. */
1086 6485 : if (arg->cl == rvc_normal && format->b == 2)
1087 : {
1088 834 : *result = wi::shwi (REAL_EXP (arg) - 1, precision);
1089 834 : return true;
1090 : }
1091 : return false;
1092 :
1093 378 : CASE_CFN_ICEIL:
1094 378 : CASE_CFN_LCEIL:
1095 378 : CASE_CFN_LLCEIL:
1096 378 : return fold_const_conversion (result, real_ceil, arg,
1097 378 : precision, format);
1098 :
1099 378 : CASE_CFN_LFLOOR:
1100 378 : CASE_CFN_IFLOOR:
1101 378 : CASE_CFN_LLFLOOR:
1102 378 : return fold_const_conversion (result, real_floor, arg,
1103 378 : precision, format);
1104 :
1105 934 : CASE_CFN_IROUND:
1106 934 : CASE_CFN_LROUND:
1107 934 : CASE_CFN_LROUND_FN:
1108 934 : CASE_CFN_LLROUND:
1109 934 : CASE_CFN_LLROUND_FN:
1110 934 : return fold_const_conversion (result, real_round, arg,
1111 934 : precision, format);
1112 :
1113 : CASE_CFN_IRINT:
1114 : CASE_CFN_LRINT:
1115 : CASE_CFN_LRINT_FN:
1116 : CASE_CFN_LLRINT:
1117 : CASE_CFN_LLRINT_FN:
1118 : /* Not yet folded to a constant. */
1119 : return false;
1120 :
1121 1560 : CASE_CFN_FINITE:
1122 1560 : case CFN_BUILT_IN_FINITED32:
1123 1560 : case CFN_BUILT_IN_FINITED64:
1124 1560 : case CFN_BUILT_IN_FINITED128:
1125 1560 : case CFN_BUILT_IN_ISFINITE:
1126 1594 : *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
1127 1560 : return true;
1128 :
1129 3654 : case CFN_BUILT_IN_ISSIGNALING:
1130 7155 : *result = wi::shwi (real_issignaling_nan (arg) ? 1 : 0, precision);
1131 3654 : return true;
1132 :
1133 208 : CASE_CFN_ISINF:
1134 208 : case CFN_BUILT_IN_ISINFD32:
1135 208 : case CFN_BUILT_IN_ISINFD64:
1136 208 : case CFN_BUILT_IN_ISINFD128:
1137 208 : if (real_isinf (arg))
1138 299 : *result = wi::shwi (arg->sign ? -1 : 1, precision);
1139 : else
1140 42 : *result = wi::shwi (0, precision);
1141 : return true;
1142 :
1143 1341 : CASE_CFN_ISNAN:
1144 1341 : case CFN_BUILT_IN_ISNAND32:
1145 1341 : case CFN_BUILT_IN_ISNAND64:
1146 1341 : case CFN_BUILT_IN_ISNAND128:
1147 2456 : *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
1148 1341 : return true;
1149 :
1150 : default:
1151 : return false;
1152 : }
1153 : }
1154 :
1155 : /* Try to evaluate:
1156 :
1157 : *RESULT = FN (ARG)
1158 :
1159 : where ARG_TYPE is the type of ARG and PRECISION is the number of bits
1160 : in the result. Return true on success. */
1161 :
1162 : static bool
1163 2256796 : fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
1164 : unsigned int precision, tree arg_type)
1165 : {
1166 2256796 : switch (fn)
1167 : {
1168 491 : CASE_CFN_FFS:
1169 491 : case CFN_BUILT_IN_FFSG:
1170 491 : *result = wi::shwi (wi::ffs (arg), precision);
1171 491 : return true;
1172 :
1173 2179 : CASE_CFN_CLZ:
1174 2179 : case CFN_BUILT_IN_CLZG:
1175 2179 : {
1176 2179 : int tmp;
1177 2179 : if (wi::ne_p (arg, 0))
1178 1830 : tmp = wi::clz (arg);
1179 349 : else if (BITINT_TYPE_P (arg_type))
1180 9 : tmp = TYPE_PRECISION (arg_type);
1181 680 : else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1182 : tmp))
1183 340 : tmp = TYPE_PRECISION (arg_type);
1184 2179 : *result = wi::shwi (tmp, precision);
1185 2179 : return true;
1186 : }
1187 :
1188 1635 : CASE_CFN_CTZ:
1189 1635 : case CFN_BUILT_IN_CTZG:
1190 1635 : {
1191 1635 : int tmp;
1192 1635 : if (wi::ne_p (arg, 0))
1193 1391 : tmp = wi::ctz (arg);
1194 244 : else if (BITINT_TYPE_P (arg_type))
1195 0 : tmp = TYPE_PRECISION (arg_type);
1196 488 : else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
1197 : tmp))
1198 244 : tmp = TYPE_PRECISION (arg_type);
1199 1635 : *result = wi::shwi (tmp, precision);
1200 1635 : return true;
1201 : }
1202 :
1203 412 : CASE_CFN_CLRSB:
1204 412 : case CFN_BUILT_IN_CLRSBG:
1205 412 : *result = wi::shwi (wi::clrsb (arg), precision);
1206 412 : return true;
1207 :
1208 41961 : CASE_CFN_POPCOUNT:
1209 41961 : case CFN_BUILT_IN_POPCOUNTG:
1210 41961 : *result = wi::shwi (wi::popcount (arg), precision);
1211 41961 : return true;
1212 :
1213 690 : CASE_CFN_PARITY:
1214 690 : case CFN_BUILT_IN_PARITYG:
1215 690 : *result = wi::shwi (wi::parity (arg), precision);
1216 690 : return true;
1217 :
1218 738 : CASE_CFN_BSWAP:
1219 1476 : *result = wi::bswap (wide_int::from (arg, precision,
1220 1476 : TYPE_SIGN (arg_type)));
1221 738 : return true;
1222 :
1223 80 : CASE_CFN_BITREVERSE:
1224 160 : *result = wi::bitreverse (wide_int::from (arg, precision,
1225 160 : TYPE_SIGN (arg_type)));
1226 80 : return true;
1227 :
1228 : default:
1229 : return false;
1230 : }
1231 : }
1232 :
1233 : /* Try to evaluate:
1234 :
1235 : RESULT = FN (*ARG)
1236 :
1237 : where FORMAT is the format of ARG and of the real and imaginary parts
1238 : of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively. Return
1239 : true on success. */
1240 :
1241 : static bool
1242 150 : fold_const_call_cs (real_value *result_real, real_value *result_imag,
1243 : combined_fn fn, const real_value *arg,
1244 : const real_format *format)
1245 : {
1246 0 : switch (fn)
1247 : {
1248 150 : CASE_CFN_CEXPI:
1249 : /* cexpi(x+yi) = cos(x)+sin(y)*i. */
1250 150 : return do_mpfr_sincos (result_imag, result_real, arg, format);
1251 :
1252 : default:
1253 : return false;
1254 : }
1255 : }
1256 :
1257 : /* Try to evaluate:
1258 :
1259 : *RESULT = fn (ARG)
1260 :
1261 : where FORMAT is the format of RESULT and of the real and imaginary parts
1262 : of ARG, passed as ARG_REAL and ARG_IMAG respectively. Return true on
1263 : success. */
1264 :
1265 : static bool
1266 2313 : fold_const_call_sc (real_value *result, combined_fn fn,
1267 : const real_value *arg_real, const real_value *arg_imag,
1268 : const real_format *format)
1269 : {
1270 0 : switch (fn)
1271 : {
1272 1177 : CASE_CFN_CABS:
1273 1177 : CASE_CFN_CABS_FN:
1274 1177 : return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
1275 :
1276 : default:
1277 : return false;
1278 : }
1279 : }
1280 :
1281 : /* Try to evaluate:
1282 :
1283 : RESULT = fn (ARG)
1284 :
1285 : where FORMAT is the format of the real and imaginary parts of RESULT
1286 : (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
1287 : Return true on success. */
1288 :
1289 : static bool
1290 39622 : fold_const_call_cc (real_value *result_real, real_value *result_imag,
1291 : combined_fn fn, const real_value *arg_real,
1292 : const real_value *arg_imag, const real_format *format)
1293 : {
1294 39622 : switch (fn)
1295 : {
1296 686 : CASE_CFN_CCOS:
1297 686 : CASE_CFN_CCOS_FN:
1298 686 : return do_mpc_arg1 (result_real, result_imag, mpc_cos,
1299 686 : arg_real, arg_imag, format);
1300 :
1301 676 : CASE_CFN_CCOSH:
1302 676 : CASE_CFN_CCOSH_FN:
1303 676 : return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
1304 676 : arg_real, arg_imag, format);
1305 :
1306 683 : CASE_CFN_CPROJ:
1307 683 : CASE_CFN_CPROJ_FN:
1308 683 : if (real_isinf (arg_real) || real_isinf (arg_imag))
1309 : {
1310 436 : *result_real = dconstinf;
1311 436 : *result_imag = dconst0;
1312 436 : result_imag->sign = arg_imag->sign;
1313 : }
1314 : else
1315 : {
1316 247 : *result_real = *arg_real;
1317 247 : *result_imag = *arg_imag;
1318 : }
1319 : return true;
1320 :
1321 701 : CASE_CFN_CSIN:
1322 701 : CASE_CFN_CSIN_FN:
1323 701 : return do_mpc_arg1 (result_real, result_imag, mpc_sin,
1324 701 : arg_real, arg_imag, format);
1325 :
1326 676 : CASE_CFN_CSINH:
1327 676 : CASE_CFN_CSINH_FN:
1328 676 : return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
1329 676 : arg_real, arg_imag, format);
1330 :
1331 698 : CASE_CFN_CTAN:
1332 698 : CASE_CFN_CTAN_FN:
1333 698 : return do_mpc_arg1 (result_real, result_imag, mpc_tan,
1334 698 : arg_real, arg_imag, format);
1335 :
1336 676 : CASE_CFN_CTANH:
1337 676 : CASE_CFN_CTANH_FN:
1338 676 : return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
1339 676 : arg_real, arg_imag, format);
1340 :
1341 777 : CASE_CFN_CLOG:
1342 777 : CASE_CFN_CLOG_FN:
1343 777 : return do_mpc_arg1 (result_real, result_imag, mpc_log,
1344 777 : arg_real, arg_imag, format);
1345 :
1346 3053 : CASE_CFN_CSQRT:
1347 3053 : CASE_CFN_CSQRT_FN:
1348 3053 : return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
1349 3053 : arg_real, arg_imag, format);
1350 :
1351 679 : CASE_CFN_CASIN:
1352 679 : CASE_CFN_CASIN_FN:
1353 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asin,
1354 679 : arg_real, arg_imag, format);
1355 :
1356 728 : CASE_CFN_CACOS:
1357 728 : CASE_CFN_CACOS_FN:
1358 728 : return do_mpc_arg1 (result_real, result_imag, mpc_acos,
1359 728 : arg_real, arg_imag, format);
1360 :
1361 679 : CASE_CFN_CATAN:
1362 679 : CASE_CFN_CATAN_FN:
1363 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atan,
1364 679 : arg_real, arg_imag, format);
1365 :
1366 679 : CASE_CFN_CASINH:
1367 679 : CASE_CFN_CASINH_FN:
1368 679 : return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
1369 679 : arg_real, arg_imag, format);
1370 :
1371 745 : CASE_CFN_CACOSH:
1372 745 : CASE_CFN_CACOSH_FN:
1373 745 : return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
1374 745 : arg_real, arg_imag, format);
1375 :
1376 679 : CASE_CFN_CATANH:
1377 679 : CASE_CFN_CATANH_FN:
1378 679 : return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1379 679 : arg_real, arg_imag, format);
1380 :
1381 1583 : CASE_CFN_CEXP:
1382 1583 : CASE_CFN_CEXP_FN:
1383 1583 : return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1384 1583 : arg_real, arg_imag, format);
1385 :
1386 : default:
1387 : return false;
1388 : }
1389 : }
1390 :
1391 : /* Subroutine of fold_const_call, with the same interface. Handle cases
1392 : where the arguments and result are numerical. */
1393 :
1394 : static tree
1395 20894023 : fold_const_call_1 (combined_fn fn, tree type, tree arg)
1396 : {
1397 20894023 : machine_mode mode = TYPE_MODE (type);
1398 20894023 : machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1399 :
1400 20894023 : if (integer_cst_p (arg))
1401 : {
1402 6031887 : if (INTEGRAL_TYPE_P (type))
1403 : {
1404 2256796 : wide_int result;
1405 2256796 : if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
1406 2256796 : TYPE_PRECISION (type), TREE_TYPE (arg)))
1407 48186 : return wide_int_to_tree (type, result);
1408 2256796 : }
1409 : return NULL_TREE;
1410 : }
1411 :
1412 14862136 : if (real_cst_p (arg))
1413 : {
1414 180355 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1415 180355 : if (mode == arg_mode)
1416 : {
1417 : /* real -> real. */
1418 162546 : REAL_VALUE_TYPE result;
1419 162546 : if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1420 162546 : REAL_MODE_FORMAT (mode)))
1421 100668 : return build_real (type, result);
1422 : }
1423 17809 : else if (COMPLEX_MODE_P (mode)
1424 17959 : && GET_MODE_INNER (mode) == arg_mode)
1425 : {
1426 : /* real -> complex real. */
1427 150 : REAL_VALUE_TYPE result_real, result_imag;
1428 300 : if (fold_const_call_cs (&result_real, &result_imag, fn,
1429 150 : TREE_REAL_CST_PTR (arg),
1430 150 : REAL_MODE_FORMAT (arg_mode)))
1431 300 : return build_complex (type,
1432 150 : build_real (TREE_TYPE (type), result_real),
1433 300 : build_real (TREE_TYPE (type), result_imag));
1434 : }
1435 17659 : else if (INTEGRAL_TYPE_P (type))
1436 : {
1437 : /* real -> int. */
1438 17656 : wide_int result;
1439 17656 : if (fold_const_call_ss (&result, fn,
1440 17656 : TREE_REAL_CST_PTR (arg),
1441 17656 : TYPE_PRECISION (type),
1442 17656 : REAL_MODE_FORMAT (arg_mode)))
1443 11217 : return wide_int_to_tree (type, result);
1444 17656 : }
1445 : return NULL_TREE;
1446 : }
1447 :
1448 14681781 : if (complex_cst_p (arg))
1449 : {
1450 41935 : gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1451 41935 : machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1452 41935 : tree argr = TREE_REALPART (arg);
1453 41935 : tree argi = TREE_IMAGPART (arg);
1454 41935 : if (mode == arg_mode
1455 39622 : && real_cst_p (argr)
1456 81557 : && real_cst_p (argi))
1457 : {
1458 : /* complex real -> complex real. */
1459 39622 : REAL_VALUE_TYPE result_real, result_imag;
1460 79244 : if (fold_const_call_cc (&result_real, &result_imag, fn,
1461 39622 : TREE_REAL_CST_PTR (argr),
1462 39622 : TREE_REAL_CST_PTR (argi),
1463 39622 : REAL_MODE_FORMAT (inner_mode)))
1464 20972 : return build_complex (type,
1465 10486 : build_real (TREE_TYPE (type), result_real),
1466 20972 : build_real (TREE_TYPE (type), result_imag));
1467 : }
1468 31449 : if (mode == inner_mode
1469 2313 : && real_cst_p (argr)
1470 33762 : && real_cst_p (argi))
1471 : {
1472 : /* complex real -> real. */
1473 2313 : REAL_VALUE_TYPE result;
1474 4626 : if (fold_const_call_sc (&result, fn,
1475 2313 : TREE_REAL_CST_PTR (argr),
1476 2313 : TREE_REAL_CST_PTR (argi),
1477 2313 : REAL_MODE_FORMAT (inner_mode)))
1478 1177 : return build_real (type, result);
1479 : }
1480 : return NULL_TREE;
1481 : }
1482 :
1483 : return NULL_TREE;
1484 : }
1485 :
1486 : /* Try to fold FN (ARG) to a constant. Return the constant on success,
1487 : otherwise return null. TYPE is the type of the return value. */
1488 :
1489 : tree
1490 21862427 : fold_const_call (combined_fn fn, tree type, tree arg)
1491 : {
1492 21862427 : switch (fn)
1493 : {
1494 564392 : case CFN_BUILT_IN_STRLEN:
1495 564392 : if (const char *str = c_getstr (arg))
1496 89973 : return build_int_cst (type, strlen (str));
1497 : return NULL_TREE;
1498 :
1499 228905 : CASE_CFN_NAN:
1500 228905 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN):
1501 228905 : case CFN_BUILT_IN_NAND32:
1502 228905 : case CFN_BUILT_IN_NAND64:
1503 228905 : case CFN_BUILT_IN_NAND128:
1504 228905 : case CFN_BUILT_IN_NAND64X:
1505 228905 : return fold_const_builtin_nan (type, arg, true);
1506 :
1507 166440 : CASE_CFN_NANS:
1508 166440 : CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
1509 166440 : case CFN_BUILT_IN_NANSF16B:
1510 166440 : case CFN_BUILT_IN_NANSD32:
1511 166440 : case CFN_BUILT_IN_NANSD64:
1512 166440 : case CFN_BUILT_IN_NANSD128:
1513 166440 : case CFN_BUILT_IN_NANSD64X:
1514 166440 : return fold_const_builtin_nan (type, arg, false);
1515 :
1516 1662 : case CFN_REDUC_PLUS:
1517 1662 : return fold_const_reduction (type, arg, PLUS_EXPR);
1518 :
1519 44 : case CFN_REDUC_MAX:
1520 44 : return fold_const_reduction (type, arg, MAX_EXPR);
1521 :
1522 67 : case CFN_REDUC_MIN:
1523 67 : return fold_const_reduction (type, arg, MIN_EXPR);
1524 :
1525 32 : case CFN_REDUC_AND:
1526 32 : return fold_const_reduction (type, arg, BIT_AND_EXPR);
1527 :
1528 259 : case CFN_REDUC_IOR:
1529 259 : return fold_const_reduction (type, arg, BIT_IOR_EXPR);
1530 :
1531 2 : case CFN_REDUC_XOR:
1532 2 : return fold_const_reduction (type, arg, BIT_XOR_EXPR);
1533 :
1534 6601 : case CFN_VEC_CONVERT:
1535 6601 : return fold_const_vec_convert (type, arg);
1536 :
1537 20894023 : default:
1538 20894023 : return fold_const_call_1 (fn, type, arg);
1539 : }
1540 : }
1541 :
1542 : /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1543 : of type TYPE. */
1544 :
1545 : static tree
1546 0 : fold_const_fold_left (tree type, tree arg0, tree arg1, tree_code code)
1547 : {
1548 0 : if (TREE_CODE (arg1) != VECTOR_CST)
1549 : return NULL_TREE;
1550 :
1551 0 : unsigned HOST_WIDE_INT nelts;
1552 0 : if (!VECTOR_CST_NELTS (arg1).is_constant (&nelts))
1553 : return NULL_TREE;
1554 :
1555 0 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
1556 : {
1557 0 : arg0 = const_binop (code, type, arg0, VECTOR_CST_ELT (arg1, i));
1558 0 : if (arg0 == NULL_TREE || !CONSTANT_CLASS_P (arg0))
1559 : return NULL_TREE;
1560 : }
1561 : return arg0;
1562 : }
1563 :
1564 : /* Fold a call to IFN_VEC_SHL_INSERT (ARG0, ARG1), returning a value
1565 : of type TYPE. */
1566 :
1567 : static tree
1568 0 : fold_const_vec_shl_insert (tree, tree arg0, tree arg1)
1569 : {
1570 0 : if (TREE_CODE (arg0) != VECTOR_CST)
1571 : return NULL_TREE;
1572 :
1573 : /* vec_shl_insert ( dup(CST), CST) -> dup (CST). */
1574 0 : if (tree elem = uniform_vector_p (arg0))
1575 : {
1576 0 : if (operand_equal_p (elem, arg1))
1577 0 : return arg0;
1578 : }
1579 :
1580 : return NULL_TREE;
1581 : }
1582 :
1583 : /* Fold a call to IFN_VEC_EXTRACT (ARG0, ARG1), returning a value
1584 : of type TYPE.
1585 :
1586 : Right now this is only handling uniform vectors, so ARG1 is not
1587 : used. But it could be easily adjusted in the future to handle
1588 : non-uniform vectors by extracting the relevant element. */
1589 :
1590 : static tree
1591 0 : fold_const_vec_extract (tree, tree arg0, tree)
1592 : {
1593 0 : if (TREE_CODE (arg0) != VECTOR_CST)
1594 : return NULL_TREE;
1595 :
1596 : /* vec_extract ( dup(CST), CST) -> dup (CST). */
1597 0 : if (tree elem = uniform_vector_p (arg0))
1598 : return elem;
1599 :
1600 : return NULL_TREE;
1601 : }
1602 :
1603 : /* Try to fold scalar integer IFN_SAT_ADD with operands OP0 and OP1. */
1604 :
1605 : static tree
1606 72 : fold_internal_fn_sat_add (tree type, tree op0, tree op1)
1607 : {
1608 72 : if (!INTEGRAL_NB_TYPE_P (type))
1609 : return NULL_TREE;
1610 :
1611 0 : if (TREE_CODE (op0) != INTEGER_CST
1612 0 : || TREE_CODE (op1) != INTEGER_CST)
1613 : return NULL_TREE;
1614 :
1615 0 : wi::overflow_type overflow;
1616 0 : unsigned int prec = TYPE_PRECISION (type);
1617 0 : wide_int result = wi::add (wi::to_wide (op0), wi::to_wide (op1),
1618 0 : TYPE_SIGN (type), &overflow);
1619 :
1620 0 : if (overflow != wi::OVF_NONE)
1621 : {
1622 0 : if (TYPE_UNSIGNED (type))
1623 0 : result = wi::max_value (prec, UNSIGNED);
1624 0 : else if (overflow == wi::OVF_OVERFLOW)
1625 0 : result = wi::max_value (prec, SIGNED);
1626 0 : else if (overflow == wi::OVF_UNDERFLOW)
1627 0 : result = wi::min_value (prec, SIGNED);
1628 : else
1629 : return NULL_TREE;
1630 : }
1631 :
1632 0 : return wide_int_to_tree (type, result);
1633 0 : }
1634 :
1635 : /* Try to evaluate:
1636 :
1637 : *RESULT = FN (*ARG0, *ARG1)
1638 :
1639 : in format FORMAT. Return true on success. */
1640 :
1641 : static bool
1642 225681 : fold_const_call_sss (real_value *result, combined_fn fn,
1643 : const real_value *arg0, const real_value *arg1,
1644 : const real_format *format)
1645 : {
1646 225681 : switch (fn)
1647 : {
1648 6774 : CASE_CFN_DREM:
1649 6774 : CASE_CFN_REMAINDER:
1650 6774 : CASE_CFN_REMAINDER_FN:
1651 6774 : return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1652 :
1653 1006 : CASE_CFN_ATAN2:
1654 1006 : CASE_CFN_ATAN2_FN:
1655 1006 : return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1656 :
1657 : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
1658 84 : CASE_CFN_ATAN2PI:
1659 84 : CASE_CFN_ATAN2PI_FN:
1660 84 : return do_mpfr_arg2 (result, mpfr_atan2pi, arg0, arg1, format);
1661 : #endif
1662 :
1663 572 : CASE_CFN_FDIM:
1664 572 : CASE_CFN_FDIM_FN:
1665 572 : return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1666 :
1667 517 : CASE_CFN_FMOD:
1668 517 : CASE_CFN_FMOD_FN:
1669 517 : return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
1670 :
1671 578 : CASE_CFN_HYPOT:
1672 578 : CASE_CFN_HYPOT_FN:
1673 578 : return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1674 :
1675 70630 : CASE_CFN_COPYSIGN:
1676 70630 : CASE_CFN_COPYSIGN_FN:
1677 70630 : *result = *arg0;
1678 70630 : real_copysign (result, arg1);
1679 70630 : return true;
1680 :
1681 3161 : CASE_CFN_FMIN:
1682 3161 : CASE_CFN_FMIN_FN:
1683 3161 : return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1684 :
1685 3161 : CASE_CFN_FMAX:
1686 3161 : CASE_CFN_FMAX_FN:
1687 3161 : return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1688 :
1689 126598 : CASE_CFN_POW:
1690 126598 : CASE_CFN_POW_FN:
1691 126598 : return fold_const_pow (result, arg0, arg1, format);
1692 :
1693 12600 : CASE_CFN_NEXTAFTER:
1694 12600 : CASE_CFN_NEXTAFTER_FN:
1695 12600 : case CFN_BUILT_IN_NEXTAFTERF16B:
1696 12600 : CASE_CFN_NEXTTOWARD:
1697 12600 : return fold_const_nextafter (result, arg0, arg1, format);
1698 :
1699 : default:
1700 : return false;
1701 : }
1702 : }
1703 :
1704 : /* Try to evaluate:
1705 :
1706 : *RESULT = FN (*ARG0, ARG1)
1707 :
1708 : where FORMAT is the format of *RESULT and *ARG0. Return true on
1709 : success. */
1710 :
1711 : static bool
1712 25808 : fold_const_call_sss (real_value *result, combined_fn fn,
1713 : const real_value *arg0, const wide_int_ref &arg1,
1714 : const real_format *format)
1715 : {
1716 25808 : switch (fn)
1717 : {
1718 7914 : CASE_CFN_LDEXP:
1719 7914 : CASE_CFN_LDEXP_FN:
1720 7914 : return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1721 :
1722 17443 : CASE_CFN_SCALBN:
1723 17443 : CASE_CFN_SCALBN_FN:
1724 17443 : CASE_CFN_SCALBLN:
1725 17443 : CASE_CFN_SCALBLN_FN:
1726 17443 : return (format->b == 2
1727 17443 : && fold_const_builtin_load_exponent (result, arg0, arg1,
1728 : format));
1729 :
1730 442 : CASE_CFN_POWI:
1731 : /* Avoid the folding if flag_signaling_nans is on and
1732 : operand is a signaling NaN. */
1733 442 : if (!flag_unsafe_math_optimizations
1734 429 : && flag_signaling_nans
1735 457 : && REAL_VALUE_ISSIGNALING_NAN (*arg0))
1736 : return false;
1737 :
1738 442 : real_powi (result, format, arg0, arg1.to_shwi ());
1739 442 : return true;
1740 :
1741 : default:
1742 : return false;
1743 : }
1744 : }
1745 :
1746 : /* Try to evaluate:
1747 :
1748 : *RESULT = FN (ARG0, *ARG1)
1749 :
1750 : where FORMAT is the format of *RESULT and *ARG1. Return true on
1751 : success. */
1752 :
1753 : static bool
1754 6434 : fold_const_call_sss (real_value *result, combined_fn fn,
1755 : const wide_int_ref &arg0, const real_value *arg1,
1756 : const real_format *format)
1757 : {
1758 6434 : switch (fn)
1759 : {
1760 1220 : CASE_CFN_JN:
1761 1220 : return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1762 :
1763 5205 : CASE_CFN_YN:
1764 5205 : return (real_compare (GT_EXPR, arg1, &dconst0)
1765 5205 : && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1766 :
1767 : default:
1768 : return false;
1769 : }
1770 : }
1771 :
1772 : /* Try to evaluate:
1773 :
1774 : *RESULT = FN (ARG0, ARG1)
1775 :
1776 : where ARG_TYPE is the type of ARG0 and PRECISION is the number of bits in
1777 : the result. Return true on success. */
1778 :
1779 : static bool
1780 388295 : fold_const_call_sss (wide_int *result, combined_fn fn,
1781 : const wide_int_ref &arg0, const wide_int_ref &arg1,
1782 : unsigned int precision, tree arg_type ATTRIBUTE_UNUSED)
1783 : {
1784 388295 : switch (fn)
1785 : {
1786 16962 : case CFN_CLZ:
1787 16962 : case CFN_BUILT_IN_CLZG:
1788 16962 : {
1789 16962 : int tmp;
1790 16962 : if (wi::ne_p (arg0, 0))
1791 16828 : tmp = wi::clz (arg0);
1792 : else
1793 134 : tmp = arg1.to_shwi ();
1794 16962 : *result = wi::shwi (tmp, precision);
1795 16962 : return true;
1796 : }
1797 :
1798 11862 : case CFN_CTZ:
1799 11862 : case CFN_BUILT_IN_CTZG:
1800 11862 : {
1801 11862 : int tmp;
1802 11862 : if (wi::ne_p (arg0, 0))
1803 11748 : tmp = wi::ctz (arg0);
1804 : else
1805 114 : tmp = arg1.to_shwi ();
1806 11862 : *result = wi::shwi (tmp, precision);
1807 11862 : return true;
1808 : }
1809 :
1810 45 : case CFN_CLMUL:
1811 45 : *result = wi::clmul (arg0, arg1, precision);
1812 45 : return true;
1813 :
1814 24 : case CFN_BITCOMPRESS:
1815 24 : *result = wi::bit_compress (arg0, arg1, precision);
1816 24 : return true;
1817 :
1818 24 : case CFN_BITEXPAND:
1819 24 : *result = wi::bit_expand (arg0, arg1, precision);
1820 24 : return true;
1821 :
1822 : default:
1823 : return false;
1824 : }
1825 : }
1826 :
1827 : /* Try to evaluate:
1828 :
1829 : RESULT = fn (ARG0, ARG1)
1830 :
1831 : where FORMAT is the format of the real and imaginary parts of RESULT
1832 : (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1833 : and of ARG1 (ARG1_REAL and ARG1_IMAG). Return true on success. */
1834 :
1835 : static bool
1836 32067 : fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1837 : combined_fn fn, const real_value *arg0_real,
1838 : const real_value *arg0_imag, const real_value *arg1_real,
1839 : const real_value *arg1_imag, const real_format *format)
1840 : {
1841 0 : switch (fn)
1842 : {
1843 32067 : CASE_CFN_CPOW:
1844 32067 : CASE_CFN_CPOW_FN:
1845 32067 : return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1846 0 : arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1847 :
1848 : default:
1849 : return false;
1850 : }
1851 : }
1852 :
1853 : /* Subroutine of fold_const_call, with the same interface. Handle cases
1854 : where the arguments and result are numerical. */
1855 :
1856 : static tree
1857 16756910 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1858 : {
1859 16756910 : machine_mode mode = TYPE_MODE (type);
1860 16756910 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1861 16756910 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1862 :
1863 16756910 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
1864 : {
1865 427821 : if (INTEGRAL_TYPE_P (type))
1866 : {
1867 388295 : wide_int result;
1868 388295 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1869 776590 : wi::to_wide (arg1), TYPE_PRECISION (type),
1870 388295 : TREE_TYPE (arg0)))
1871 28917 : return wide_int_to_tree (type, result);
1872 388295 : }
1873 : return NULL_TREE;
1874 : }
1875 :
1876 16329089 : if (mode == arg0_mode
1877 11135910 : && real_cst_p (arg0)
1878 17587312 : && real_cst_p (arg1))
1879 : {
1880 225681 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1881 225681 : REAL_VALUE_TYPE result;
1882 225681 : if (arg0_mode == arg1_mode)
1883 : {
1884 : /* real, real -> real. */
1885 450782 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1886 225391 : TREE_REAL_CST_PTR (arg1),
1887 225391 : REAL_MODE_FORMAT (mode)))
1888 193784 : return build_real (type, result);
1889 : }
1890 290 : else if (arg1_mode == TYPE_MODE (long_double_type_node))
1891 290 : switch (fn)
1892 : {
1893 290 : CASE_CFN_NEXTTOWARD:
1894 : /* real, long double -> real. */
1895 580 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1896 290 : TREE_REAL_CST_PTR (arg1),
1897 290 : REAL_MODE_FORMAT (mode)))
1898 228 : return build_real (type, result);
1899 : break;
1900 : default:
1901 : break;
1902 : }
1903 : return NULL_TREE;
1904 : }
1905 :
1906 16103408 : if (real_cst_p (arg0)
1907 16103408 : && integer_cst_p (arg1))
1908 : {
1909 25811 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1910 25811 : if (mode == arg0_mode)
1911 : {
1912 : /* real, int -> real. */
1913 25808 : REAL_VALUE_TYPE result;
1914 25808 : if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1915 51616 : wi::to_wide (arg1),
1916 25808 : REAL_MODE_FORMAT (mode)))
1917 5619 : return build_real (type, result);
1918 : }
1919 : return NULL_TREE;
1920 : }
1921 :
1922 16077597 : if (integer_cst_p (arg0)
1923 16077597 : && real_cst_p (arg1))
1924 : {
1925 6434 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1926 6434 : if (mode == arg1_mode)
1927 : {
1928 : /* int, real -> real. */
1929 6434 : REAL_VALUE_TYPE result;
1930 12868 : if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1931 6434 : TREE_REAL_CST_PTR (arg1),
1932 6434 : REAL_MODE_FORMAT (mode)))
1933 1755 : return build_real (type, result);
1934 : }
1935 : return NULL_TREE;
1936 : }
1937 :
1938 16071163 : if (arg0_mode == arg1_mode
1939 13150917 : && complex_cst_p (arg0)
1940 16103230 : && complex_cst_p (arg1))
1941 : {
1942 32067 : gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1943 32067 : machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1944 32067 : tree arg0r = TREE_REALPART (arg0);
1945 32067 : tree arg0i = TREE_IMAGPART (arg0);
1946 32067 : tree arg1r = TREE_REALPART (arg1);
1947 32067 : tree arg1i = TREE_IMAGPART (arg1);
1948 32067 : if (mode == arg0_mode
1949 32067 : && real_cst_p (arg0r)
1950 32067 : && real_cst_p (arg0i)
1951 32067 : && real_cst_p (arg1r)
1952 64134 : && real_cst_p (arg1i))
1953 : {
1954 : /* complex real, complex real -> complex real. */
1955 32067 : REAL_VALUE_TYPE result_real, result_imag;
1956 64134 : if (fold_const_call_ccc (&result_real, &result_imag, fn,
1957 32067 : TREE_REAL_CST_PTR (arg0r),
1958 32067 : TREE_REAL_CST_PTR (arg0i),
1959 32067 : TREE_REAL_CST_PTR (arg1r),
1960 32067 : TREE_REAL_CST_PTR (arg1i),
1961 32067 : REAL_MODE_FORMAT (inner_mode)))
1962 21470 : return build_complex (type,
1963 10735 : build_real (TREE_TYPE (type), result_real),
1964 21470 : build_real (TREE_TYPE (type), result_imag));
1965 : }
1966 : return NULL_TREE;
1967 : }
1968 :
1969 : return NULL_TREE;
1970 : }
1971 :
1972 : /* Try to fold FN (ARG0, ARG1) to a constant. Return the constant on success,
1973 : otherwise return null. TYPE is the type of the return value. */
1974 :
1975 : tree
1976 19543140 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1977 : {
1978 19543140 : const char *p0, *p1;
1979 19543140 : char c;
1980 19543140 : tree_code subcode;
1981 19543140 : switch (fn)
1982 : {
1983 2692 : case CFN_BUILT_IN_STRSPN:
1984 2692 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1985 123 : return build_int_cst (type, strspn (p0, p1));
1986 : return NULL_TREE;
1987 :
1988 2596 : case CFN_BUILT_IN_STRCSPN:
1989 2596 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1990 123 : return build_int_cst (type, strcspn (p0, p1));
1991 : return NULL_TREE;
1992 :
1993 2225458 : case CFN_BUILT_IN_STRCMP:
1994 2225458 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1995 24925 : return build_cmp_result (type, strcmp (p0, p1));
1996 : return NULL_TREE;
1997 :
1998 309 : case CFN_BUILT_IN_STRCASECMP:
1999 309 : if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
2000 : {
2001 10 : int r = strcmp (p0, p1);
2002 10 : if (r == 0)
2003 1 : return build_cmp_result (type, r);
2004 : }
2005 : return NULL_TREE;
2006 :
2007 209243 : case CFN_BUILT_IN_INDEX:
2008 209243 : case CFN_BUILT_IN_STRCHR:
2009 209243 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
2010 : {
2011 163 : const char *r = strchr (p0, c);
2012 163 : if (r == NULL)
2013 38 : return build_int_cst (type, 0);
2014 125 : return fold_convert (type,
2015 : fold_build_pointer_plus_hwi (arg0, r - p0));
2016 : }
2017 : return NULL_TREE;
2018 :
2019 199525 : case CFN_BUILT_IN_RINDEX:
2020 199525 : case CFN_BUILT_IN_STRRCHR:
2021 199525 : if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
2022 : {
2023 124 : const char *r = strrchr (p0, c);
2024 124 : if (r == NULL)
2025 35 : return build_int_cst (type, 0);
2026 89 : return fold_convert (type,
2027 : fold_build_pointer_plus_hwi (arg0, r - p0));
2028 : }
2029 : return NULL_TREE;
2030 :
2031 108086 : case CFN_BUILT_IN_STRSTR:
2032 108086 : if ((p1 = c_getstr (arg1)))
2033 : {
2034 6066 : if ((p0 = c_getstr (arg0)))
2035 : {
2036 209 : const char *r = strstr (p0, p1);
2037 209 : if (r == NULL)
2038 44 : return build_int_cst (type, 0);
2039 165 : return fold_convert (type,
2040 : fold_build_pointer_plus_hwi (arg0, r - p0));
2041 : }
2042 5857 : if (*p1 == '\0')
2043 13 : return fold_convert (type, arg0);
2044 : }
2045 : return NULL_TREE;
2046 :
2047 19646 : case CFN_BUILT_IN_STRNLEN:
2048 19646 : if ((p0 = c_getstr (arg0)))
2049 : {
2050 3178 : unsigned HOST_WIDE_INT s1 = 0;
2051 3178 : if (!size_t_cst_p (arg1, &s1))
2052 : return NULL_TREE;
2053 :
2054 442 : return build_int_cst (type, strnlen (p0, s1));
2055 : }
2056 : return NULL_TREE;
2057 :
2058 0 : case CFN_FOLD_LEFT_PLUS:
2059 0 : return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
2060 :
2061 0 : case CFN_VEC_SHL_INSERT:
2062 0 : return fold_const_vec_shl_insert (type, arg0, arg1);
2063 :
2064 0 : case CFN_VEC_EXTRACT:
2065 0 : return fold_const_vec_extract (type, arg0, arg1);
2066 :
2067 72 : case CFN_SAT_ADD:
2068 72 : return fold_internal_fn_sat_add (type, arg0, arg1);
2069 :
2070 6604 : case CFN_UBSAN_CHECK_ADD:
2071 6604 : case CFN_ADD_OVERFLOW:
2072 6604 : subcode = PLUS_EXPR;
2073 6604 : goto arith_overflow;
2074 :
2075 6403 : case CFN_UBSAN_CHECK_SUB:
2076 6403 : case CFN_SUB_OVERFLOW:
2077 6403 : subcode = MINUS_EXPR;
2078 6403 : goto arith_overflow;
2079 :
2080 5551 : case CFN_UBSAN_CHECK_MUL:
2081 5551 : case CFN_MUL_OVERFLOW:
2082 5551 : subcode = MULT_EXPR;
2083 5551 : goto arith_overflow;
2084 :
2085 18558 : arith_overflow:
2086 18558 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
2087 : {
2088 17688 : tree itype
2089 17688 : = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type;
2090 17688 : bool ovf = false;
2091 17688 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
2092 17688 : fold_convert (itype, arg1));
2093 17688 : if (!r || TREE_CODE (r) != INTEGER_CST)
2094 : return NULL_TREE;
2095 17688 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
2096 : ovf = true;
2097 17688 : if (TREE_OVERFLOW (r))
2098 3445 : r = drop_tree_overflow (r);
2099 17688 : if (itype == type)
2100 : {
2101 2833 : if (ovf)
2102 : return NULL_TREE;
2103 1810 : return r;
2104 : }
2105 : else
2106 14855 : return build_complex (type, r, build_int_cst (itype, ovf));
2107 : }
2108 : return NULL_TREE;
2109 :
2110 45 : case CFN_CLMUL_WIDEN:
2111 45 : if (integer_cst_p (arg0) && integer_cst_p (arg1))
2112 : {
2113 45 : unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
2114 45 : wide_int res = wi::clmul (wi::to_wide (arg0), wi::to_wide (arg1),
2115 90 : prec * 2);
2116 45 : wide_int lo = wide_int::from (res, prec, UNSIGNED);
2117 45 : wide_int hi = wide_int::from (wi::rshift (res, prec, UNSIGNED),
2118 90 : prec, UNSIGNED);
2119 90 : return build_complex (type, wide_int_to_tree (TREE_TYPE (type), lo),
2120 90 : wide_int_to_tree (TREE_TYPE (type), hi));
2121 45 : }
2122 : return NULL_TREE;
2123 :
2124 16756910 : default:
2125 16756910 : return fold_const_call_1 (fn, type, arg0, arg1);
2126 : }
2127 : }
2128 :
2129 : /* Try to evaluate:
2130 :
2131 : *RESULT = FN (*ARG0, *ARG1, *ARG2)
2132 :
2133 : in format FORMAT. Return true on success. */
2134 :
2135 : static bool
2136 2534 : fold_const_call_ssss (real_value *result, combined_fn fn,
2137 : const real_value *arg0, const real_value *arg1,
2138 : const real_value *arg2, const real_format *format)
2139 : {
2140 2534 : switch (fn)
2141 : {
2142 2534 : CASE_CFN_FMA:
2143 2534 : CASE_CFN_FMA_FN:
2144 2534 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
2145 :
2146 0 : case CFN_FMS:
2147 0 : {
2148 0 : real_value new_arg2 = real_value_negate (arg2);
2149 0 : return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, &new_arg2, format);
2150 : }
2151 :
2152 0 : case CFN_FNMA:
2153 0 : {
2154 0 : real_value new_arg0 = real_value_negate (arg0);
2155 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1, arg2, format);
2156 : }
2157 :
2158 0 : case CFN_FNMS:
2159 0 : {
2160 0 : real_value new_arg0 = real_value_negate (arg0);
2161 0 : real_value new_arg2 = real_value_negate (arg2);
2162 0 : return do_mpfr_arg3 (result, mpfr_fma, &new_arg0, arg1,
2163 : &new_arg2, format);
2164 : }
2165 :
2166 : default:
2167 : return false;
2168 : }
2169 : }
2170 :
2171 : /* Subroutine of fold_const_call, with the same interface. Handle cases
2172 : where the arguments and result are numerical. */
2173 :
2174 : static tree
2175 4556738 : fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
2176 : {
2177 4556738 : machine_mode mode = TYPE_MODE (type);
2178 4556738 : machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
2179 4556738 : machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
2180 4556738 : machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
2181 :
2182 4556738 : if (arg0_mode == arg1_mode
2183 4556738 : && arg0_mode == arg2_mode
2184 2000986 : && real_cst_p (arg0)
2185 2712 : && real_cst_p (arg1)
2186 4559378 : && real_cst_p (arg2))
2187 : {
2188 2534 : gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
2189 2534 : if (mode == arg0_mode)
2190 : {
2191 : /* real, real, real -> real. */
2192 2534 : REAL_VALUE_TYPE result;
2193 5068 : if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
2194 2534 : TREE_REAL_CST_PTR (arg1),
2195 2534 : TREE_REAL_CST_PTR (arg2),
2196 2534 : REAL_MODE_FORMAT (mode)))
2197 1060 : return build_real (type, result);
2198 : }
2199 : return NULL_TREE;
2200 : }
2201 :
2202 : return NULL_TREE;
2203 : }
2204 :
2205 : /* Given a CRC polynomial POLYNOMIAL_ARG, the current CRC value in CRC_ARG and
2206 : new data item DATA_ARG. Compute the updated CRC result value in type TYPE.
2207 : The CRC direction is inferred from FN (forward vs reversed). */
2208 :
2209 : static tree
2210 728 : fold_const_crc (internal_fn fn, tree type, tree crc_arg, tree data_arg,
2211 : tree polynomial_arg)
2212 : {
2213 728 : if (!integer_cst_p (crc_arg)
2214 136 : || !integer_cst_p (data_arg)
2215 783 : || !integer_cst_p (polynomial_arg))
2216 : return NULL_TREE;
2217 :
2218 55 : unsigned int crc_bits = TYPE_PRECISION (type);
2219 55 : unsigned int data_bits = TYPE_PRECISION (TREE_TYPE (data_arg));
2220 :
2221 55 : if ((data_bits != 8 && data_bits != 16 && data_bits != 32 && data_bits != 64)
2222 55 : || (crc_bits != 8 && crc_bits != 16 && crc_bits != 32 && crc_bits != 64)
2223 55 : || data_bits > crc_bits)
2224 : return NULL_TREE;
2225 :
2226 55 : if (!tree_fits_uhwi_p (crc_arg)
2227 55 : || !tree_fits_uhwi_p (data_arg)
2228 55 : || !tree_fits_uhwi_p (polynomial_arg))
2229 : return NULL_TREE;
2230 :
2231 55 : unsigned HOST_WIDE_INT crc = tree_to_uhwi (crc_arg);
2232 55 : unsigned HOST_WIDE_INT data = tree_to_uhwi (data_arg);
2233 55 : unsigned HOST_WIDE_INT polynomial = tree_to_uhwi (polynomial_arg);
2234 :
2235 55 : if (fn == IFN_CRC_REV)
2236 36 : crc = calculate_reversed_crc (crc, data, polynomial, crc_bits, data_bits);
2237 : else
2238 19 : crc = calculate_crc (crc, data, polynomial, crc_bits, data_bits);
2239 :
2240 55 : return build_int_cstu (type, crc);
2241 : }
2242 :
2243 : /* Try to fold FN (ARG0, ARG1, ARG2) to a constant. Return the constant on
2244 : success, otherwise return null. TYPE is the type of the return value. */
2245 :
2246 : tree
2247 7426381 : fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
2248 : {
2249 7426381 : const char *p0, *p1;
2250 7426381 : char c;
2251 7426381 : unsigned HOST_WIDE_INT s0, s1, s2 = 0;
2252 7426381 : switch (fn)
2253 : {
2254 45214 : case CFN_BUILT_IN_STRNCMP:
2255 45214 : if (!size_t_cst_p (arg2, &s2))
2256 : return NULL_TREE;
2257 29172 : if (s2 == 0
2258 237 : && !TREE_SIDE_EFFECTS (arg0)
2259 29381 : && !TREE_SIDE_EFFECTS (arg1))
2260 209 : return build_int_cst (type, 0);
2261 28963 : else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
2262 573 : return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX)));
2263 : return NULL_TREE;
2264 :
2265 8535 : case CFN_BUILT_IN_STRNCASECMP:
2266 8535 : if (!size_t_cst_p (arg2, &s2))
2267 : return NULL_TREE;
2268 8214 : if (s2 == 0
2269 9 : && !TREE_SIDE_EFFECTS (arg0)
2270 8219 : && !TREE_SIDE_EFFECTS (arg1))
2271 5 : return build_int_cst (type, 0);
2272 8209 : else if ((p0 = c_getstr (arg0))
2273 724 : && (p1 = c_getstr (arg1))
2274 8349 : && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0)
2275 3 : return build_int_cst (type, 0);
2276 : return NULL_TREE;
2277 :
2278 2568832 : case CFN_BUILT_IN_BCMP:
2279 2568832 : case CFN_BUILT_IN_MEMCMP:
2280 2568832 : if (!size_t_cst_p (arg2, &s2))
2281 : return NULL_TREE;
2282 1631062 : if (s2 == 0
2283 13007 : && !TREE_SIDE_EFFECTS (arg0)
2284 1644069 : && !TREE_SIDE_EFFECTS (arg1))
2285 13007 : return build_int_cst (type, 0);
2286 1618055 : if ((p0 = getbyterep (arg0, &s0))
2287 12878 : && (p1 = getbyterep (arg1, &s1))
2288 3743 : && s2 <= s0
2289 1621358 : && s2 <= s1)
2290 3301 : return build_cmp_result (type, memcmp (p0, p1, s2));
2291 : return NULL_TREE;
2292 :
2293 245976 : case CFN_BUILT_IN_MEMCHR:
2294 245976 : if (!size_t_cst_p (arg2, &s2))
2295 : return NULL_TREE;
2296 7566 : if (s2 == 0
2297 151 : && !TREE_SIDE_EFFECTS (arg0)
2298 7700 : && !TREE_SIDE_EFFECTS (arg1))
2299 131 : return build_int_cst (type, 0);
2300 7435 : if ((p0 = getbyterep (arg0, &s0))
2301 2671 : && s2 <= s0
2302 9987 : && target_char_cst_p (arg1, &c))
2303 : {
2304 1009 : const char *r = (const char *) memchr (p0, c, s2);
2305 1009 : if (r == NULL)
2306 290 : return build_int_cst (type, 0);
2307 719 : return fold_convert (type,
2308 : fold_build_pointer_plus_hwi (arg0, r - p0));
2309 : }
2310 : return NULL_TREE;
2311 :
2312 0 : case CFN_WHILE_ULT:
2313 0 : {
2314 0 : poly_uint64 parg0, parg1;
2315 0 : if (poly_int_tree_p (arg0, &parg0) && poly_int_tree_p (arg1, &parg1))
2316 0 : return fold_while_ult (type, parg0, parg1);
2317 : return NULL_TREE;
2318 : }
2319 :
2320 358 : case CFN_UADDC:
2321 358 : case CFN_USUBC:
2322 358 : if (integer_cst_p (arg0) && integer_cst_p (arg1) && integer_cst_p (arg2))
2323 : {
2324 358 : tree itype = TREE_TYPE (type);
2325 358 : bool ovf = false;
2326 358 : tree_code subcode = fn == CFN_UADDC ? PLUS_EXPR : MINUS_EXPR;
2327 358 : tree r = int_const_binop (subcode, fold_convert (itype, arg0),
2328 358 : fold_convert (itype, arg1));
2329 358 : if (!r)
2330 : return NULL_TREE;
2331 358 : if (arith_overflowed_p (subcode, itype, arg0, arg1))
2332 : ovf = true;
2333 358 : tree r2 = int_const_binop (subcode, r, fold_convert (itype, arg2));
2334 358 : if (!r2 || TREE_CODE (r2) != INTEGER_CST)
2335 : return NULL_TREE;
2336 358 : if (arith_overflowed_p (subcode, itype, r, arg2))
2337 0 : ovf = true;
2338 358 : if (TREE_OVERFLOW (r2))
2339 0 : r2 = drop_tree_overflow (r2);
2340 358 : return build_complex (type, r2, build_int_cst (itype, ovf));
2341 : }
2342 : return NULL_TREE;
2343 :
2344 728 : case CFN_BUILT_IN_CRC8_DATA8:
2345 728 : case CFN_BUILT_IN_CRC16_DATA8:
2346 728 : case CFN_BUILT_IN_CRC16_DATA16:
2347 728 : case CFN_BUILT_IN_CRC32_DATA8:
2348 728 : case CFN_BUILT_IN_CRC32_DATA16:
2349 728 : case CFN_BUILT_IN_CRC32_DATA32:
2350 728 : case CFN_BUILT_IN_CRC64_DATA8:
2351 728 : case CFN_BUILT_IN_CRC64_DATA16:
2352 728 : case CFN_BUILT_IN_CRC64_DATA32:
2353 728 : case CFN_BUILT_IN_CRC64_DATA64:
2354 728 : case CFN_BUILT_IN_REV_CRC8_DATA8:
2355 728 : case CFN_BUILT_IN_REV_CRC16_DATA8:
2356 728 : case CFN_BUILT_IN_REV_CRC16_DATA16:
2357 728 : case CFN_BUILT_IN_REV_CRC32_DATA8:
2358 728 : case CFN_BUILT_IN_REV_CRC32_DATA16:
2359 728 : case CFN_BUILT_IN_REV_CRC32_DATA32:
2360 728 : case CFN_BUILT_IN_REV_CRC64_DATA8:
2361 728 : case CFN_BUILT_IN_REV_CRC64_DATA16:
2362 728 : case CFN_BUILT_IN_REV_CRC64_DATA32:
2363 728 : case CFN_BUILT_IN_REV_CRC64_DATA64:
2364 728 : return fold_const_crc (associated_internal_fn (fn, type),
2365 728 : type, arg0, arg1, arg2);
2366 :
2367 4556738 : default:
2368 4556738 : return fold_const_call_1 (fn, type, arg0, arg1, arg2);
2369 : }
2370 : }
|