1*38fd1498Szrj /* Constant folding for calls to built-in and internal functions.
2*38fd1498Szrj    Copyright (C) 1988-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "realmpfr.h"
24*38fd1498Szrj #include "tree.h"
25*38fd1498Szrj #include "stor-layout.h"
26*38fd1498Szrj #include "options.h"
27*38fd1498Szrj #include "fold-const.h"
28*38fd1498Szrj #include "fold-const-call.h"
29*38fd1498Szrj #include "case-cfn-macros.h"
30*38fd1498Szrj #include "tm.h" /* For C[LT]Z_DEFINED_AT_ZERO.  */
31*38fd1498Szrj #include "builtins.h"
32*38fd1498Szrj #include "gimple-expr.h"
33*38fd1498Szrj 
34*38fd1498Szrj /* Functions that test for certain constant types, abstracting away the
35*38fd1498Szrj    decision about whether to check for overflow.  */
36*38fd1498Szrj 
37*38fd1498Szrj static inline bool
integer_cst_p(tree t)38*38fd1498Szrj integer_cst_p (tree t)
39*38fd1498Szrj {
40*38fd1498Szrj   return TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t);
41*38fd1498Szrj }
42*38fd1498Szrj 
43*38fd1498Szrj static inline bool
real_cst_p(tree t)44*38fd1498Szrj real_cst_p (tree t)
45*38fd1498Szrj {
46*38fd1498Szrj   return TREE_CODE (t) == REAL_CST && !TREE_OVERFLOW (t);
47*38fd1498Szrj }
48*38fd1498Szrj 
49*38fd1498Szrj static inline bool
complex_cst_p(tree t)50*38fd1498Szrj complex_cst_p (tree t)
51*38fd1498Szrj {
52*38fd1498Szrj   return TREE_CODE (t) == COMPLEX_CST;
53*38fd1498Szrj }
54*38fd1498Szrj 
55*38fd1498Szrj /* Return true if ARG is a constant in the range of the host size_t.
56*38fd1498Szrj    Store it in *SIZE_OUT if so.  */
57*38fd1498Szrj 
58*38fd1498Szrj static inline bool
host_size_t_cst_p(tree t,size_t * size_out)59*38fd1498Szrj host_size_t_cst_p (tree t, size_t *size_out)
60*38fd1498Szrj {
61*38fd1498Szrj   if (types_compatible_p (size_type_node, TREE_TYPE (t))
62*38fd1498Szrj       && integer_cst_p (t)
63*38fd1498Szrj       && (wi::min_precision (wi::to_wide (t), UNSIGNED)
64*38fd1498Szrj 	  <= sizeof (size_t) * CHAR_BIT))
65*38fd1498Szrj     {
66*38fd1498Szrj       *size_out = tree_to_uhwi (t);
67*38fd1498Szrj       return true;
68*38fd1498Szrj     }
69*38fd1498Szrj   return false;
70*38fd1498Szrj }
71*38fd1498Szrj 
72*38fd1498Szrj /* RES is the result of a comparison in which < 0 means "less", 0 means
73*38fd1498Szrj    "equal" and > 0 means "more".  Canonicalize it to -1, 0 or 1 and
74*38fd1498Szrj    return it in type TYPE.  */
75*38fd1498Szrj 
76*38fd1498Szrj tree
build_cmp_result(tree type,int res)77*38fd1498Szrj build_cmp_result (tree type, int res)
78*38fd1498Szrj {
79*38fd1498Szrj   return build_int_cst (type, res < 0 ? -1 : res > 0 ? 1 : 0);
80*38fd1498Szrj }
81*38fd1498Szrj 
82*38fd1498Szrj /* M is the result of trying to constant-fold an expression (starting
83*38fd1498Szrj    with clear MPFR flags) and INEXACT says whether the result in M is
84*38fd1498Szrj    exact or inexact.  Return true if M can be used as a constant-folded
85*38fd1498Szrj    result in format FORMAT, storing the value in *RESULT if so.  */
86*38fd1498Szrj 
87*38fd1498Szrj static bool
do_mpfr_ckconv(real_value * result,mpfr_srcptr m,bool inexact,const real_format * format)88*38fd1498Szrj do_mpfr_ckconv (real_value *result, mpfr_srcptr m, bool inexact,
89*38fd1498Szrj 		const real_format *format)
90*38fd1498Szrj {
91*38fd1498Szrj   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
92*38fd1498Szrj      overflow/underflow occurred.  If -frounding-math, proceed iff the
93*38fd1498Szrj      result of calling FUNC was exact.  */
94*38fd1498Szrj   if (!mpfr_number_p (m)
95*38fd1498Szrj       || mpfr_overflow_p ()
96*38fd1498Szrj       || mpfr_underflow_p ()
97*38fd1498Szrj       || (flag_rounding_math && inexact))
98*38fd1498Szrj     return false;
99*38fd1498Szrj 
100*38fd1498Szrj   REAL_VALUE_TYPE tmp;
101*38fd1498Szrj   real_from_mpfr (&tmp, m, format, GMP_RNDN);
102*38fd1498Szrj 
103*38fd1498Szrj   /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
104*38fd1498Szrj      If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
105*38fd1498Szrj      underflowed in the conversion.  */
106*38fd1498Szrj   if (!real_isfinite (&tmp)
107*38fd1498Szrj       || ((tmp.cl == rvc_zero) != (mpfr_zero_p (m) != 0)))
108*38fd1498Szrj     return false;
109*38fd1498Szrj 
110*38fd1498Szrj   real_convert (result, format, &tmp);
111*38fd1498Szrj   return real_identical (result, &tmp);
112*38fd1498Szrj }
113*38fd1498Szrj 
114*38fd1498Szrj /* Try to evaluate:
115*38fd1498Szrj 
116*38fd1498Szrj       *RESULT = f (*ARG)
117*38fd1498Szrj 
118*38fd1498Szrj    in format FORMAT, given that FUNC is the MPFR implementation of f.
119*38fd1498Szrj    Return true on success.  */
120*38fd1498Szrj 
121*38fd1498Szrj static bool
do_mpfr_arg1(real_value * result,int (* func)(mpfr_ptr,mpfr_srcptr,mpfr_rnd_t),const real_value * arg,const real_format * format)122*38fd1498Szrj do_mpfr_arg1 (real_value *result,
123*38fd1498Szrj 	      int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_rnd_t),
124*38fd1498Szrj 	      const real_value *arg, const real_format *format)
125*38fd1498Szrj {
126*38fd1498Szrj   /* To proceed, MPFR must exactly represent the target floating point
127*38fd1498Szrj      format, which only happens when the target base equals two.  */
128*38fd1498Szrj   if (format->b != 2 || !real_isfinite (arg))
129*38fd1498Szrj     return false;
130*38fd1498Szrj 
131*38fd1498Szrj   int prec = format->p;
132*38fd1498Szrj   mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
133*38fd1498Szrj   mpfr_t m;
134*38fd1498Szrj 
135*38fd1498Szrj   mpfr_init2 (m, prec);
136*38fd1498Szrj   mpfr_from_real (m, arg, GMP_RNDN);
137*38fd1498Szrj   mpfr_clear_flags ();
138*38fd1498Szrj   bool inexact = func (m, m, rnd);
139*38fd1498Szrj   bool ok = do_mpfr_ckconv (result, m, inexact, format);
140*38fd1498Szrj   mpfr_clear (m);
141*38fd1498Szrj 
142*38fd1498Szrj   return ok;
143*38fd1498Szrj }
144*38fd1498Szrj 
145*38fd1498Szrj /* Try to evaluate:
146*38fd1498Szrj 
147*38fd1498Szrj       *RESULT_SIN = sin (*ARG);
148*38fd1498Szrj       *RESULT_COS = cos (*ARG);
149*38fd1498Szrj 
150*38fd1498Szrj    for format FORMAT.  Return true on success.  */
151*38fd1498Szrj 
152*38fd1498Szrj static bool
do_mpfr_sincos(real_value * result_sin,real_value * result_cos,const real_value * arg,const real_format * format)153*38fd1498Szrj do_mpfr_sincos (real_value *result_sin, real_value *result_cos,
154*38fd1498Szrj 		const real_value *arg, const real_format *format)
155*38fd1498Szrj {
156*38fd1498Szrj   /* To proceed, MPFR must exactly represent the target floating point
157*38fd1498Szrj      format, which only happens when the target base equals two.  */
158*38fd1498Szrj   if (format->b != 2 || !real_isfinite (arg))
159*38fd1498Szrj     return false;
160*38fd1498Szrj 
161*38fd1498Szrj   int prec = format->p;
162*38fd1498Szrj   mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
163*38fd1498Szrj   mpfr_t m, ms, mc;
164*38fd1498Szrj 
165*38fd1498Szrj   mpfr_inits2 (prec, m, ms, mc, NULL);
166*38fd1498Szrj   mpfr_from_real (m, arg, GMP_RNDN);
167*38fd1498Szrj   mpfr_clear_flags ();
168*38fd1498Szrj   bool inexact = mpfr_sin_cos (ms, mc, m, rnd);
169*38fd1498Szrj   bool ok = (do_mpfr_ckconv (result_sin, ms, inexact, format)
170*38fd1498Szrj 	     && do_mpfr_ckconv (result_cos, mc, inexact, format));
171*38fd1498Szrj   mpfr_clears (m, ms, mc, NULL);
172*38fd1498Szrj 
173*38fd1498Szrj   return ok;
174*38fd1498Szrj }
175*38fd1498Szrj 
176*38fd1498Szrj /* Try to evaluate:
177*38fd1498Szrj 
178*38fd1498Szrj       *RESULT = f (*ARG0, *ARG1)
179*38fd1498Szrj 
180*38fd1498Szrj    in format FORMAT, given that FUNC is the MPFR implementation of f.
181*38fd1498Szrj    Return true on success.  */
182*38fd1498Szrj 
183*38fd1498Szrj static bool
do_mpfr_arg2(real_value * result,int (* func)(mpfr_ptr,mpfr_srcptr,mpfr_srcptr,mpfr_rnd_t),const real_value * arg0,const real_value * arg1,const real_format * format)184*38fd1498Szrj do_mpfr_arg2 (real_value *result,
185*38fd1498Szrj 	      int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t),
186*38fd1498Szrj 	      const real_value *arg0, const real_value *arg1,
187*38fd1498Szrj 	      const real_format *format)
188*38fd1498Szrj {
189*38fd1498Szrj   /* To proceed, MPFR must exactly represent the target floating point
190*38fd1498Szrj      format, which only happens when the target base equals two.  */
191*38fd1498Szrj   if (format->b != 2 || !real_isfinite (arg0) || !real_isfinite (arg1))
192*38fd1498Szrj     return false;
193*38fd1498Szrj 
194*38fd1498Szrj   int prec = format->p;
195*38fd1498Szrj   mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
196*38fd1498Szrj   mpfr_t m0, m1;
197*38fd1498Szrj 
198*38fd1498Szrj   mpfr_inits2 (prec, m0, m1, NULL);
199*38fd1498Szrj   mpfr_from_real (m0, arg0, GMP_RNDN);
200*38fd1498Szrj   mpfr_from_real (m1, arg1, GMP_RNDN);
201*38fd1498Szrj   mpfr_clear_flags ();
202*38fd1498Szrj   bool inexact = func (m0, m0, m1, rnd);
203*38fd1498Szrj   bool ok = do_mpfr_ckconv (result, m0, inexact, format);
204*38fd1498Szrj   mpfr_clears (m0, m1, NULL);
205*38fd1498Szrj 
206*38fd1498Szrj   return ok;
207*38fd1498Szrj }
208*38fd1498Szrj 
209*38fd1498Szrj /* Try to evaluate:
210*38fd1498Szrj 
211*38fd1498Szrj       *RESULT = f (ARG0, *ARG1)
212*38fd1498Szrj 
213*38fd1498Szrj    in format FORMAT, given that FUNC is the MPFR implementation of f.
214*38fd1498Szrj    Return true on success.  */
215*38fd1498Szrj 
216*38fd1498Szrj static bool
do_mpfr_arg2(real_value * result,int (* func)(mpfr_ptr,long,mpfr_srcptr,mp_rnd_t),const wide_int_ref & arg0,const real_value * arg1,const real_format * format)217*38fd1498Szrj do_mpfr_arg2 (real_value *result,
218*38fd1498Szrj 	      int (*func) (mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
219*38fd1498Szrj 	      const wide_int_ref &arg0, const real_value *arg1,
220*38fd1498Szrj 	      const real_format *format)
221*38fd1498Szrj {
222*38fd1498Szrj   if (format->b != 2 || !real_isfinite (arg1))
223*38fd1498Szrj     return false;
224*38fd1498Szrj 
225*38fd1498Szrj   int prec = format->p;
226*38fd1498Szrj   mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
227*38fd1498Szrj   mpfr_t m;
228*38fd1498Szrj 
229*38fd1498Szrj   mpfr_init2 (m, prec);
230*38fd1498Szrj   mpfr_from_real (m, arg1, GMP_RNDN);
231*38fd1498Szrj   mpfr_clear_flags ();
232*38fd1498Szrj   bool inexact = func (m, arg0.to_shwi (), m, rnd);
233*38fd1498Szrj   bool ok = do_mpfr_ckconv (result, m, inexact, format);
234*38fd1498Szrj   mpfr_clear (m);
235*38fd1498Szrj 
236*38fd1498Szrj   return ok;
237*38fd1498Szrj }
238*38fd1498Szrj 
239*38fd1498Szrj /* Try to evaluate:
240*38fd1498Szrj 
241*38fd1498Szrj       *RESULT = f (*ARG0, *ARG1, *ARG2)
242*38fd1498Szrj 
243*38fd1498Szrj    in format FORMAT, given that FUNC is the MPFR implementation of f.
244*38fd1498Szrj    Return true on success.  */
245*38fd1498Szrj 
246*38fd1498Szrj static bool
do_mpfr_arg3(real_value * result,int (* func)(mpfr_ptr,mpfr_srcptr,mpfr_srcptr,mpfr_srcptr,mpfr_rnd_t),const real_value * arg0,const real_value * arg1,const real_value * arg2,const real_format * format)247*38fd1498Szrj do_mpfr_arg3 (real_value *result,
248*38fd1498Szrj 	      int (*func) (mpfr_ptr, mpfr_srcptr, mpfr_srcptr,
249*38fd1498Szrj 			   mpfr_srcptr, mpfr_rnd_t),
250*38fd1498Szrj 	      const real_value *arg0, const real_value *arg1,
251*38fd1498Szrj 	      const real_value *arg2, const real_format *format)
252*38fd1498Szrj {
253*38fd1498Szrj   /* To proceed, MPFR must exactly represent the target floating point
254*38fd1498Szrj      format, which only happens when the target base equals two.  */
255*38fd1498Szrj   if (format->b != 2
256*38fd1498Szrj       || !real_isfinite (arg0)
257*38fd1498Szrj       || !real_isfinite (arg1)
258*38fd1498Szrj       || !real_isfinite (arg2))
259*38fd1498Szrj     return false;
260*38fd1498Szrj 
261*38fd1498Szrj   int prec = format->p;
262*38fd1498Szrj   mp_rnd_t rnd = format->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
263*38fd1498Szrj   mpfr_t m0, m1, m2;
264*38fd1498Szrj 
265*38fd1498Szrj   mpfr_inits2 (prec, m0, m1, m2, NULL);
266*38fd1498Szrj   mpfr_from_real (m0, arg0, GMP_RNDN);
267*38fd1498Szrj   mpfr_from_real (m1, arg1, GMP_RNDN);
268*38fd1498Szrj   mpfr_from_real (m2, arg2, GMP_RNDN);
269*38fd1498Szrj   mpfr_clear_flags ();
270*38fd1498Szrj   bool inexact = func (m0, m0, m1, m2, rnd);
271*38fd1498Szrj   bool ok = do_mpfr_ckconv (result, m0, inexact, format);
272*38fd1498Szrj   mpfr_clears (m0, m1, m2, NULL);
273*38fd1498Szrj 
274*38fd1498Szrj   return ok;
275*38fd1498Szrj }
276*38fd1498Szrj 
277*38fd1498Szrj /* M is the result of trying to constant-fold an expression (starting
278*38fd1498Szrj    with clear MPFR flags) and INEXACT says whether the result in M is
279*38fd1498Szrj    exact or inexact.  Return true if M can be used as a constant-folded
280*38fd1498Szrj    result in which the real and imaginary parts have format FORMAT.
281*38fd1498Szrj    Store those parts in *RESULT_REAL and *RESULT_IMAG if so.  */
282*38fd1498Szrj 
283*38fd1498Szrj static bool
do_mpc_ckconv(real_value * result_real,real_value * result_imag,mpc_srcptr m,bool inexact,const real_format * format)284*38fd1498Szrj do_mpc_ckconv (real_value *result_real, real_value *result_imag,
285*38fd1498Szrj 	       mpc_srcptr m, bool inexact, const real_format *format)
286*38fd1498Szrj {
287*38fd1498Szrj   /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
288*38fd1498Szrj      overflow/underflow occurred.  If -frounding-math, proceed iff the
289*38fd1498Szrj      result of calling FUNC was exact.  */
290*38fd1498Szrj   if (!mpfr_number_p (mpc_realref (m))
291*38fd1498Szrj       || !mpfr_number_p (mpc_imagref (m))
292*38fd1498Szrj       || mpfr_overflow_p ()
293*38fd1498Szrj       || mpfr_underflow_p ()
294*38fd1498Szrj       || (flag_rounding_math && inexact))
295*38fd1498Szrj     return false;
296*38fd1498Szrj 
297*38fd1498Szrj   REAL_VALUE_TYPE tmp_real, tmp_imag;
298*38fd1498Szrj   real_from_mpfr (&tmp_real, mpc_realref (m), format, GMP_RNDN);
299*38fd1498Szrj   real_from_mpfr (&tmp_imag, mpc_imagref (m), format, GMP_RNDN);
300*38fd1498Szrj 
301*38fd1498Szrj   /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values.
302*38fd1498Szrj      If the REAL_VALUE_TYPE is zero but the mpft_t is not, then we
303*38fd1498Szrj      underflowed in the conversion.  */
304*38fd1498Szrj   if (!real_isfinite (&tmp_real)
305*38fd1498Szrj       || !real_isfinite (&tmp_imag)
306*38fd1498Szrj       || (tmp_real.cl == rvc_zero) != (mpfr_zero_p (mpc_realref (m)) != 0)
307*38fd1498Szrj       || (tmp_imag.cl == rvc_zero) != (mpfr_zero_p (mpc_imagref (m)) != 0))
308*38fd1498Szrj     return false;
309*38fd1498Szrj 
310*38fd1498Szrj   real_convert (result_real, format, &tmp_real);
311*38fd1498Szrj   real_convert (result_imag, format, &tmp_imag);
312*38fd1498Szrj 
313*38fd1498Szrj   return (real_identical (result_real, &tmp_real)
314*38fd1498Szrj 	  && real_identical (result_imag, &tmp_imag));
315*38fd1498Szrj }
316*38fd1498Szrj 
317*38fd1498Szrj /* Try to evaluate:
318*38fd1498Szrj 
319*38fd1498Szrj       RESULT = f (ARG)
320*38fd1498Szrj 
321*38fd1498Szrj    in format FORMAT, given that FUNC is the mpc implementation of f.
322*38fd1498Szrj    Return true on success.  Both RESULT and ARG are represented as
323*38fd1498Szrj    real and imaginary pairs.  */
324*38fd1498Szrj 
325*38fd1498Szrj static bool
do_mpc_arg1(real_value * result_real,real_value * result_imag,int (* func)(mpc_ptr,mpc_srcptr,mpc_rnd_t),const real_value * arg_real,const real_value * arg_imag,const real_format * format)326*38fd1498Szrj do_mpc_arg1 (real_value *result_real, real_value *result_imag,
327*38fd1498Szrj 	     int (*func) (mpc_ptr, mpc_srcptr, mpc_rnd_t),
328*38fd1498Szrj 	     const real_value *arg_real, const real_value *arg_imag,
329*38fd1498Szrj 	     const real_format *format)
330*38fd1498Szrj {
331*38fd1498Szrj   /* To proceed, MPFR must exactly represent the target floating point
332*38fd1498Szrj      format, which only happens when the target base equals two.  */
333*38fd1498Szrj   if (format->b != 2
334*38fd1498Szrj       || !real_isfinite (arg_real)
335*38fd1498Szrj       || !real_isfinite (arg_imag))
336*38fd1498Szrj     return false;
337*38fd1498Szrj 
338*38fd1498Szrj   int prec = format->p;
339*38fd1498Szrj   mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
340*38fd1498Szrj   mpc_t m;
341*38fd1498Szrj 
342*38fd1498Szrj   mpc_init2 (m, prec);
343*38fd1498Szrj   mpfr_from_real (mpc_realref (m), arg_real, GMP_RNDN);
344*38fd1498Szrj   mpfr_from_real (mpc_imagref (m), arg_imag, GMP_RNDN);
345*38fd1498Szrj   mpfr_clear_flags ();
346*38fd1498Szrj   bool inexact = func (m, m, crnd);
347*38fd1498Szrj   bool ok = do_mpc_ckconv (result_real, result_imag, m, inexact, format);
348*38fd1498Szrj   mpc_clear (m);
349*38fd1498Szrj 
350*38fd1498Szrj   return ok;
351*38fd1498Szrj }
352*38fd1498Szrj 
353*38fd1498Szrj /* Try to evaluate:
354*38fd1498Szrj 
355*38fd1498Szrj       RESULT = f (ARG0, ARG1)
356*38fd1498Szrj 
357*38fd1498Szrj    in format FORMAT, given that FUNC is the mpc implementation of f.
358*38fd1498Szrj    Return true on success.  RESULT, ARG0 and ARG1 are represented as
359*38fd1498Szrj    real and imaginary pairs.  */
360*38fd1498Szrj 
361*38fd1498Szrj static bool
do_mpc_arg2(real_value * result_real,real_value * result_imag,int (* func)(mpc_ptr,mpc_srcptr,mpc_srcptr,mpc_rnd_t),const real_value * arg0_real,const real_value * arg0_imag,const real_value * arg1_real,const real_value * arg1_imag,const real_format * format)362*38fd1498Szrj do_mpc_arg2 (real_value *result_real, real_value *result_imag,
363*38fd1498Szrj 	     int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t),
364*38fd1498Szrj 	     const real_value *arg0_real, const real_value *arg0_imag,
365*38fd1498Szrj 	     const real_value *arg1_real, const real_value *arg1_imag,
366*38fd1498Szrj 	     const real_format *format)
367*38fd1498Szrj {
368*38fd1498Szrj   if (!real_isfinite (arg0_real)
369*38fd1498Szrj       || !real_isfinite (arg0_imag)
370*38fd1498Szrj       || !real_isfinite (arg1_real)
371*38fd1498Szrj       || !real_isfinite (arg1_imag))
372*38fd1498Szrj     return false;
373*38fd1498Szrj 
374*38fd1498Szrj   int prec = format->p;
375*38fd1498Szrj   mpc_rnd_t crnd = format->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
376*38fd1498Szrj   mpc_t m0, m1;
377*38fd1498Szrj 
378*38fd1498Szrj   mpc_init2 (m0, prec);
379*38fd1498Szrj   mpc_init2 (m1, prec);
380*38fd1498Szrj   mpfr_from_real (mpc_realref (m0), arg0_real, GMP_RNDN);
381*38fd1498Szrj   mpfr_from_real (mpc_imagref (m0), arg0_imag, GMP_RNDN);
382*38fd1498Szrj   mpfr_from_real (mpc_realref (m1), arg1_real, GMP_RNDN);
383*38fd1498Szrj   mpfr_from_real (mpc_imagref (m1), arg1_imag, GMP_RNDN);
384*38fd1498Szrj   mpfr_clear_flags ();
385*38fd1498Szrj   bool inexact = func (m0, m0, m1, crnd);
386*38fd1498Szrj   bool ok = do_mpc_ckconv (result_real, result_imag, m0, inexact, format);
387*38fd1498Szrj   mpc_clear (m0);
388*38fd1498Szrj   mpc_clear (m1);
389*38fd1498Szrj 
390*38fd1498Szrj   return ok;
391*38fd1498Szrj }
392*38fd1498Szrj 
393*38fd1498Szrj /* Try to evaluate:
394*38fd1498Szrj 
395*38fd1498Szrj       *RESULT = logb (*ARG)
396*38fd1498Szrj 
397*38fd1498Szrj    in format FORMAT.  Return true on success.  */
398*38fd1498Szrj 
399*38fd1498Szrj static bool
fold_const_logb(real_value * result,const real_value * arg,const real_format * format)400*38fd1498Szrj fold_const_logb (real_value *result, const real_value *arg,
401*38fd1498Szrj 		 const real_format *format)
402*38fd1498Szrj {
403*38fd1498Szrj   switch (arg->cl)
404*38fd1498Szrj     {
405*38fd1498Szrj     case rvc_nan:
406*38fd1498Szrj       /* If arg is +-NaN, then return it.  */
407*38fd1498Szrj       *result = *arg;
408*38fd1498Szrj       return true;
409*38fd1498Szrj 
410*38fd1498Szrj     case rvc_inf:
411*38fd1498Szrj       /* If arg is +-Inf, then return +Inf.  */
412*38fd1498Szrj       *result = *arg;
413*38fd1498Szrj       result->sign = 0;
414*38fd1498Szrj       return true;
415*38fd1498Szrj 
416*38fd1498Szrj     case rvc_zero:
417*38fd1498Szrj       /* Zero may set errno and/or raise an exception.  */
418*38fd1498Szrj       return false;
419*38fd1498Szrj 
420*38fd1498Szrj     case rvc_normal:
421*38fd1498Szrj       /* For normal numbers, proceed iff radix == 2.  In GCC,
422*38fd1498Szrj 	 normalized significands are in the range [0.5, 1.0).  We
423*38fd1498Szrj 	 want the exponent as if they were [1.0, 2.0) so get the
424*38fd1498Szrj 	 exponent and subtract 1.  */
425*38fd1498Szrj       if (format->b == 2)
426*38fd1498Szrj 	{
427*38fd1498Szrj 	  real_from_integer (result, format, REAL_EXP (arg) - 1, SIGNED);
428*38fd1498Szrj 	  return true;
429*38fd1498Szrj 	}
430*38fd1498Szrj       return false;
431*38fd1498Szrj     }
432*38fd1498Szrj   gcc_unreachable ();
433*38fd1498Szrj }
434*38fd1498Szrj 
435*38fd1498Szrj /* Try to evaluate:
436*38fd1498Szrj 
437*38fd1498Szrj       *RESULT = significand (*ARG)
438*38fd1498Szrj 
439*38fd1498Szrj    in format FORMAT.  Return true on success.  */
440*38fd1498Szrj 
441*38fd1498Szrj static bool
fold_const_significand(real_value * result,const real_value * arg,const real_format * format)442*38fd1498Szrj fold_const_significand (real_value *result, const real_value *arg,
443*38fd1498Szrj 			const real_format *format)
444*38fd1498Szrj {
445*38fd1498Szrj   switch (arg->cl)
446*38fd1498Szrj     {
447*38fd1498Szrj     case rvc_zero:
448*38fd1498Szrj     case rvc_nan:
449*38fd1498Szrj     case rvc_inf:
450*38fd1498Szrj       /* If arg is +-0, +-Inf or +-NaN, then return it.  */
451*38fd1498Szrj       *result = *arg;
452*38fd1498Szrj       return true;
453*38fd1498Szrj 
454*38fd1498Szrj     case rvc_normal:
455*38fd1498Szrj       /* For normal numbers, proceed iff radix == 2.  */
456*38fd1498Szrj       if (format->b == 2)
457*38fd1498Szrj 	{
458*38fd1498Szrj 	  *result = *arg;
459*38fd1498Szrj 	  /* In GCC, normalized significands are in the range [0.5, 1.0).
460*38fd1498Szrj 	     We want them to be [1.0, 2.0) so set the exponent to 1.  */
461*38fd1498Szrj 	  SET_REAL_EXP (result, 1);
462*38fd1498Szrj 	  return true;
463*38fd1498Szrj 	}
464*38fd1498Szrj       return false;
465*38fd1498Szrj     }
466*38fd1498Szrj   gcc_unreachable ();
467*38fd1498Szrj }
468*38fd1498Szrj 
469*38fd1498Szrj /* Try to evaluate:
470*38fd1498Szrj 
471*38fd1498Szrj       *RESULT = f (*ARG)
472*38fd1498Szrj 
473*38fd1498Szrj    where FORMAT is the format of *ARG and PRECISION is the number of
474*38fd1498Szrj    significant bits in the result.  Return true on success.  */
475*38fd1498Szrj 
476*38fd1498Szrj static bool
fold_const_conversion(wide_int * result,void (* fn)(real_value *,format_helper,const real_value *),const real_value * arg,unsigned int precision,const real_format * format)477*38fd1498Szrj fold_const_conversion (wide_int *result,
478*38fd1498Szrj 		       void (*fn) (real_value *, format_helper,
479*38fd1498Szrj 				   const real_value *),
480*38fd1498Szrj 		       const real_value *arg, unsigned int precision,
481*38fd1498Szrj 		       const real_format *format)
482*38fd1498Szrj {
483*38fd1498Szrj   if (!real_isfinite (arg))
484*38fd1498Szrj     return false;
485*38fd1498Szrj 
486*38fd1498Szrj   real_value rounded;
487*38fd1498Szrj   fn (&rounded, format, arg);
488*38fd1498Szrj 
489*38fd1498Szrj   bool fail = false;
490*38fd1498Szrj   *result = real_to_integer (&rounded, &fail, precision);
491*38fd1498Szrj   return !fail;
492*38fd1498Szrj }
493*38fd1498Szrj 
494*38fd1498Szrj /* Try to evaluate:
495*38fd1498Szrj 
496*38fd1498Szrj       *RESULT = pow (*ARG0, *ARG1)
497*38fd1498Szrj 
498*38fd1498Szrj    in format FORMAT.  Return true on success.  */
499*38fd1498Szrj 
500*38fd1498Szrj static bool
fold_const_pow(real_value * result,const real_value * arg0,const real_value * arg1,const real_format * format)501*38fd1498Szrj fold_const_pow (real_value *result, const real_value *arg0,
502*38fd1498Szrj 		const real_value *arg1, const real_format *format)
503*38fd1498Szrj {
504*38fd1498Szrj   if (do_mpfr_arg2 (result, mpfr_pow, arg0, arg1, format))
505*38fd1498Szrj     return true;
506*38fd1498Szrj 
507*38fd1498Szrj   /* Check for an integer exponent.  */
508*38fd1498Szrj   REAL_VALUE_TYPE cint1;
509*38fd1498Szrj   HOST_WIDE_INT n1 = real_to_integer (arg1);
510*38fd1498Szrj   real_from_integer (&cint1, VOIDmode, n1, SIGNED);
511*38fd1498Szrj   /* Attempt to evaluate pow at compile-time, unless this should
512*38fd1498Szrj      raise an exception.  */
513*38fd1498Szrj   if (real_identical (arg1, &cint1)
514*38fd1498Szrj       && (n1 > 0
515*38fd1498Szrj 	  || (!flag_trapping_math && !flag_errno_math)
516*38fd1498Szrj 	  || !real_equal (arg0, &dconst0)))
517*38fd1498Szrj     {
518*38fd1498Szrj       bool inexact = real_powi (result, format, arg0, n1);
519*38fd1498Szrj       /* Avoid the folding if flag_signaling_nans is on.  */
520*38fd1498Szrj       if (flag_unsafe_math_optimizations
521*38fd1498Szrj 	  || (!inexact
522*38fd1498Szrj 	      && !(flag_signaling_nans
523*38fd1498Szrj 	           && REAL_VALUE_ISSIGNALING_NAN (*arg0))))
524*38fd1498Szrj 	return true;
525*38fd1498Szrj     }
526*38fd1498Szrj 
527*38fd1498Szrj   return false;
528*38fd1498Szrj }
529*38fd1498Szrj 
530*38fd1498Szrj /* Try to evaluate:
531*38fd1498Szrj 
532*38fd1498Szrj       *RESULT = ldexp (*ARG0, ARG1)
533*38fd1498Szrj 
534*38fd1498Szrj    in format FORMAT.  Return true on success.  */
535*38fd1498Szrj 
536*38fd1498Szrj static bool
fold_const_builtin_load_exponent(real_value * result,const real_value * arg0,const wide_int_ref & arg1,const real_format * format)537*38fd1498Szrj fold_const_builtin_load_exponent (real_value *result, const real_value *arg0,
538*38fd1498Szrj 				  const wide_int_ref &arg1,
539*38fd1498Szrj 				  const real_format *format)
540*38fd1498Szrj {
541*38fd1498Szrj   /* Bound the maximum adjustment to twice the range of the
542*38fd1498Szrj      mode's valid exponents.  Use abs to ensure the range is
543*38fd1498Szrj      positive as a sanity check.  */
544*38fd1498Szrj   int max_exp_adj = 2 * labs (format->emax - format->emin);
545*38fd1498Szrj 
546*38fd1498Szrj   /* The requested adjustment must be inside this range.  This
547*38fd1498Szrj      is a preliminary cap to avoid things like overflow, we
548*38fd1498Szrj      may still fail to compute the result for other reasons.  */
549*38fd1498Szrj   if (wi::les_p (arg1, -max_exp_adj) || wi::ges_p (arg1, max_exp_adj))
550*38fd1498Szrj     return false;
551*38fd1498Szrj 
552*38fd1498Szrj   /* Don't perform operation if we honor signaling NaNs and
553*38fd1498Szrj      operand is a signaling NaN.  */
554*38fd1498Szrj   if (!flag_unsafe_math_optimizations
555*38fd1498Szrj       && flag_signaling_nans
556*38fd1498Szrj       && REAL_VALUE_ISSIGNALING_NAN (*arg0))
557*38fd1498Szrj     return false;
558*38fd1498Szrj 
559*38fd1498Szrj   REAL_VALUE_TYPE initial_result;
560*38fd1498Szrj   real_ldexp (&initial_result, arg0, arg1.to_shwi ());
561*38fd1498Szrj 
562*38fd1498Szrj   /* Ensure we didn't overflow.  */
563*38fd1498Szrj   if (real_isinf (&initial_result))
564*38fd1498Szrj     return false;
565*38fd1498Szrj 
566*38fd1498Szrj   /* Only proceed if the target mode can hold the
567*38fd1498Szrj      resulting value.  */
568*38fd1498Szrj   *result = real_value_truncate (format, initial_result);
569*38fd1498Szrj   return real_equal (&initial_result, result);
570*38fd1498Szrj }
571*38fd1498Szrj 
572*38fd1498Szrj /* Fold a call to __builtin_nan or __builtin_nans with argument ARG and
573*38fd1498Szrj    return type TYPE.  QUIET is true if a quiet rather than signalling
574*38fd1498Szrj    NaN is required.  */
575*38fd1498Szrj 
576*38fd1498Szrj static tree
fold_const_builtin_nan(tree type,tree arg,bool quiet)577*38fd1498Szrj fold_const_builtin_nan (tree type, tree arg, bool quiet)
578*38fd1498Szrj {
579*38fd1498Szrj   REAL_VALUE_TYPE real;
580*38fd1498Szrj   const char *str = c_getstr (arg);
581*38fd1498Szrj   if (str && real_nan (&real, str, quiet, TYPE_MODE (type)))
582*38fd1498Szrj     return build_real (type, real);
583*38fd1498Szrj   return NULL_TREE;
584*38fd1498Szrj }
585*38fd1498Szrj 
586*38fd1498Szrj /* Fold a call to IFN_REDUC_<CODE> (ARG), returning a value of type TYPE.  */
587*38fd1498Szrj 
588*38fd1498Szrj static tree
fold_const_reduction(tree type,tree arg,tree_code code)589*38fd1498Szrj fold_const_reduction (tree type, tree arg, tree_code code)
590*38fd1498Szrj {
591*38fd1498Szrj   unsigned HOST_WIDE_INT nelts;
592*38fd1498Szrj   if (TREE_CODE (arg) != VECTOR_CST
593*38fd1498Szrj       || !VECTOR_CST_NELTS (arg).is_constant (&nelts))
594*38fd1498Szrj     return NULL_TREE;
595*38fd1498Szrj 
596*38fd1498Szrj   tree res = VECTOR_CST_ELT (arg, 0);
597*38fd1498Szrj   for (unsigned HOST_WIDE_INT i = 1; i < nelts; i++)
598*38fd1498Szrj     {
599*38fd1498Szrj       res = const_binop (code, type, res, VECTOR_CST_ELT (arg, i));
600*38fd1498Szrj       if (res == NULL_TREE || !CONSTANT_CLASS_P (res))
601*38fd1498Szrj 	return NULL_TREE;
602*38fd1498Szrj     }
603*38fd1498Szrj   return res;
604*38fd1498Szrj }
605*38fd1498Szrj 
606*38fd1498Szrj /* Try to evaluate:
607*38fd1498Szrj 
608*38fd1498Szrj       *RESULT = FN (*ARG)
609*38fd1498Szrj 
610*38fd1498Szrj    in format FORMAT.  Return true on success.  */
611*38fd1498Szrj 
612*38fd1498Szrj static bool
fold_const_call_ss(real_value * result,combined_fn fn,const real_value * arg,const real_format * format)613*38fd1498Szrj fold_const_call_ss (real_value *result, combined_fn fn,
614*38fd1498Szrj 		    const real_value *arg, const real_format *format)
615*38fd1498Szrj {
616*38fd1498Szrj   switch (fn)
617*38fd1498Szrj     {
618*38fd1498Szrj     CASE_CFN_SQRT:
619*38fd1498Szrj     CASE_CFN_SQRT_FN:
620*38fd1498Szrj       return (real_compare (GE_EXPR, arg, &dconst0)
621*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_sqrt, arg, format));
622*38fd1498Szrj 
623*38fd1498Szrj     CASE_CFN_CBRT:
624*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_cbrt, arg, format);
625*38fd1498Szrj 
626*38fd1498Szrj     CASE_CFN_ASIN:
627*38fd1498Szrj       return (real_compare (GE_EXPR, arg, &dconstm1)
628*38fd1498Szrj 	      && real_compare (LE_EXPR, arg, &dconst1)
629*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_asin, arg, format));
630*38fd1498Szrj 
631*38fd1498Szrj     CASE_CFN_ACOS:
632*38fd1498Szrj       return (real_compare (GE_EXPR, arg, &dconstm1)
633*38fd1498Szrj 	      && real_compare (LE_EXPR, arg, &dconst1)
634*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_acos, arg, format));
635*38fd1498Szrj 
636*38fd1498Szrj     CASE_CFN_ATAN:
637*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_atan, arg, format);
638*38fd1498Szrj 
639*38fd1498Szrj     CASE_CFN_ASINH:
640*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_asinh, arg, format);
641*38fd1498Szrj 
642*38fd1498Szrj     CASE_CFN_ACOSH:
643*38fd1498Szrj       return (real_compare (GE_EXPR, arg, &dconst1)
644*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_acosh, arg, format));
645*38fd1498Szrj 
646*38fd1498Szrj     CASE_CFN_ATANH:
647*38fd1498Szrj       return (real_compare (GE_EXPR, arg, &dconstm1)
648*38fd1498Szrj 	      && real_compare (LE_EXPR, arg, &dconst1)
649*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_atanh, arg, format));
650*38fd1498Szrj 
651*38fd1498Szrj     CASE_CFN_SIN:
652*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_sin, arg, format);
653*38fd1498Szrj 
654*38fd1498Szrj     CASE_CFN_COS:
655*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_cos, arg, format);
656*38fd1498Szrj 
657*38fd1498Szrj     CASE_CFN_TAN:
658*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_tan, arg, format);
659*38fd1498Szrj 
660*38fd1498Szrj     CASE_CFN_SINH:
661*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_sinh, arg, format);
662*38fd1498Szrj 
663*38fd1498Szrj     CASE_CFN_COSH:
664*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_cosh, arg, format);
665*38fd1498Szrj 
666*38fd1498Szrj     CASE_CFN_TANH:
667*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_tanh, arg, format);
668*38fd1498Szrj 
669*38fd1498Szrj     CASE_CFN_ERF:
670*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_erf, arg, format);
671*38fd1498Szrj 
672*38fd1498Szrj     CASE_CFN_ERFC:
673*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_erfc, arg, format);
674*38fd1498Szrj 
675*38fd1498Szrj     CASE_CFN_TGAMMA:
676*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_gamma, arg, format);
677*38fd1498Szrj 
678*38fd1498Szrj     CASE_CFN_EXP:
679*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_exp, arg, format);
680*38fd1498Szrj 
681*38fd1498Szrj     CASE_CFN_EXP2:
682*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_exp2, arg, format);
683*38fd1498Szrj 
684*38fd1498Szrj     CASE_CFN_EXP10:
685*38fd1498Szrj     CASE_CFN_POW10:
686*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_exp10, arg, format);
687*38fd1498Szrj 
688*38fd1498Szrj     CASE_CFN_EXPM1:
689*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_expm1, arg, format);
690*38fd1498Szrj 
691*38fd1498Szrj     CASE_CFN_LOG:
692*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconst0)
693*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_log, arg, format));
694*38fd1498Szrj 
695*38fd1498Szrj     CASE_CFN_LOG2:
696*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconst0)
697*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_log2, arg, format));
698*38fd1498Szrj 
699*38fd1498Szrj     CASE_CFN_LOG10:
700*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconst0)
701*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_log10, arg, format));
702*38fd1498Szrj 
703*38fd1498Szrj     CASE_CFN_LOG1P:
704*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconstm1)
705*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_log1p, arg, format));
706*38fd1498Szrj 
707*38fd1498Szrj     CASE_CFN_J0:
708*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_j0, arg, format);
709*38fd1498Szrj 
710*38fd1498Szrj     CASE_CFN_J1:
711*38fd1498Szrj       return do_mpfr_arg1 (result, mpfr_j1, arg, format);
712*38fd1498Szrj 
713*38fd1498Szrj     CASE_CFN_Y0:
714*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconst0)
715*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_y0, arg, format));
716*38fd1498Szrj 
717*38fd1498Szrj     CASE_CFN_Y1:
718*38fd1498Szrj       return (real_compare (GT_EXPR, arg, &dconst0)
719*38fd1498Szrj 	      && do_mpfr_arg1 (result, mpfr_y1, arg, format));
720*38fd1498Szrj 
721*38fd1498Szrj     CASE_CFN_FLOOR:
722*38fd1498Szrj     CASE_CFN_FLOOR_FN:
723*38fd1498Szrj       if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
724*38fd1498Szrj 	{
725*38fd1498Szrj 	  real_floor (result, format, arg);
726*38fd1498Szrj 	  return true;
727*38fd1498Szrj 	}
728*38fd1498Szrj       return false;
729*38fd1498Szrj 
730*38fd1498Szrj     CASE_CFN_CEIL:
731*38fd1498Szrj     CASE_CFN_CEIL_FN:
732*38fd1498Szrj       if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
733*38fd1498Szrj 	{
734*38fd1498Szrj 	  real_ceil (result, format, arg);
735*38fd1498Szrj 	  return true;
736*38fd1498Szrj 	}
737*38fd1498Szrj       return false;
738*38fd1498Szrj 
739*38fd1498Szrj     CASE_CFN_TRUNC:
740*38fd1498Szrj     CASE_CFN_TRUNC_FN:
741*38fd1498Szrj       real_trunc (result, format, arg);
742*38fd1498Szrj       return true;
743*38fd1498Szrj 
744*38fd1498Szrj     CASE_CFN_ROUND:
745*38fd1498Szrj     CASE_CFN_ROUND_FN:
746*38fd1498Szrj       if (!REAL_VALUE_ISNAN (*arg) || !flag_errno_math)
747*38fd1498Szrj 	{
748*38fd1498Szrj 	  real_round (result, format, arg);
749*38fd1498Szrj 	  return true;
750*38fd1498Szrj 	}
751*38fd1498Szrj       return false;
752*38fd1498Szrj 
753*38fd1498Szrj     CASE_CFN_LOGB:
754*38fd1498Szrj       return fold_const_logb (result, arg, format);
755*38fd1498Szrj 
756*38fd1498Szrj     CASE_CFN_SIGNIFICAND:
757*38fd1498Szrj       return fold_const_significand (result, arg, format);
758*38fd1498Szrj 
759*38fd1498Szrj     default:
760*38fd1498Szrj       return false;
761*38fd1498Szrj     }
762*38fd1498Szrj }
763*38fd1498Szrj 
764*38fd1498Szrj /* Try to evaluate:
765*38fd1498Szrj 
766*38fd1498Szrj       *RESULT = FN (*ARG)
767*38fd1498Szrj 
768*38fd1498Szrj    where FORMAT is the format of ARG and PRECISION is the number of
769*38fd1498Szrj    significant bits in the result.  Return true on success.  */
770*38fd1498Szrj 
771*38fd1498Szrj static bool
fold_const_call_ss(wide_int * result,combined_fn fn,const real_value * arg,unsigned int precision,const real_format * format)772*38fd1498Szrj fold_const_call_ss (wide_int *result, combined_fn fn,
773*38fd1498Szrj 		    const real_value *arg, unsigned int precision,
774*38fd1498Szrj 		    const real_format *format)
775*38fd1498Szrj {
776*38fd1498Szrj   switch (fn)
777*38fd1498Szrj     {
778*38fd1498Szrj     CASE_CFN_SIGNBIT:
779*38fd1498Szrj       if (real_isneg (arg))
780*38fd1498Szrj 	*result = wi::one (precision);
781*38fd1498Szrj       else
782*38fd1498Szrj 	*result = wi::zero (precision);
783*38fd1498Szrj       return true;
784*38fd1498Szrj 
785*38fd1498Szrj     CASE_CFN_ILOGB:
786*38fd1498Szrj       /* For ilogb we don't know FP_ILOGB0, so only handle normal values.
787*38fd1498Szrj 	 Proceed iff radix == 2.  In GCC, normalized significands are in
788*38fd1498Szrj 	 the range [0.5, 1.0).  We want the exponent as if they were
789*38fd1498Szrj 	 [1.0, 2.0) so get the exponent and subtract 1.  */
790*38fd1498Szrj       if (arg->cl == rvc_normal && format->b == 2)
791*38fd1498Szrj 	{
792*38fd1498Szrj 	  *result = wi::shwi (REAL_EXP (arg) - 1, precision);
793*38fd1498Szrj 	  return true;
794*38fd1498Szrj 	}
795*38fd1498Szrj       return false;
796*38fd1498Szrj 
797*38fd1498Szrj     CASE_CFN_ICEIL:
798*38fd1498Szrj     CASE_CFN_LCEIL:
799*38fd1498Szrj     CASE_CFN_LLCEIL:
800*38fd1498Szrj       return fold_const_conversion (result, real_ceil, arg,
801*38fd1498Szrj 				    precision, format);
802*38fd1498Szrj 
803*38fd1498Szrj     CASE_CFN_LFLOOR:
804*38fd1498Szrj     CASE_CFN_IFLOOR:
805*38fd1498Szrj     CASE_CFN_LLFLOOR:
806*38fd1498Szrj       return fold_const_conversion (result, real_floor, arg,
807*38fd1498Szrj 				    precision, format);
808*38fd1498Szrj 
809*38fd1498Szrj     CASE_CFN_IROUND:
810*38fd1498Szrj     CASE_CFN_LROUND:
811*38fd1498Szrj     CASE_CFN_LLROUND:
812*38fd1498Szrj       return fold_const_conversion (result, real_round, arg,
813*38fd1498Szrj 				    precision, format);
814*38fd1498Szrj 
815*38fd1498Szrj     CASE_CFN_IRINT:
816*38fd1498Szrj     CASE_CFN_LRINT:
817*38fd1498Szrj     CASE_CFN_LLRINT:
818*38fd1498Szrj       /* Not yet folded to a constant.  */
819*38fd1498Szrj       return false;
820*38fd1498Szrj 
821*38fd1498Szrj     CASE_CFN_FINITE:
822*38fd1498Szrj     case CFN_BUILT_IN_FINITED32:
823*38fd1498Szrj     case CFN_BUILT_IN_FINITED64:
824*38fd1498Szrj     case CFN_BUILT_IN_FINITED128:
825*38fd1498Szrj     case CFN_BUILT_IN_ISFINITE:
826*38fd1498Szrj       *result = wi::shwi (real_isfinite (arg) ? 1 : 0, precision);
827*38fd1498Szrj       return true;
828*38fd1498Szrj 
829*38fd1498Szrj     CASE_CFN_ISINF:
830*38fd1498Szrj     case CFN_BUILT_IN_ISINFD32:
831*38fd1498Szrj     case CFN_BUILT_IN_ISINFD64:
832*38fd1498Szrj     case CFN_BUILT_IN_ISINFD128:
833*38fd1498Szrj       if (real_isinf (arg))
834*38fd1498Szrj 	*result = wi::shwi (arg->sign ? -1 : 1, precision);
835*38fd1498Szrj       else
836*38fd1498Szrj 	*result = wi::shwi (0, precision);
837*38fd1498Szrj       return true;
838*38fd1498Szrj 
839*38fd1498Szrj     CASE_CFN_ISNAN:
840*38fd1498Szrj     case CFN_BUILT_IN_ISNAND32:
841*38fd1498Szrj     case CFN_BUILT_IN_ISNAND64:
842*38fd1498Szrj     case CFN_BUILT_IN_ISNAND128:
843*38fd1498Szrj       *result = wi::shwi (real_isnan (arg) ? 1 : 0, precision);
844*38fd1498Szrj       return true;
845*38fd1498Szrj 
846*38fd1498Szrj     default:
847*38fd1498Szrj       return false;
848*38fd1498Szrj     }
849*38fd1498Szrj }
850*38fd1498Szrj 
851*38fd1498Szrj /* Try to evaluate:
852*38fd1498Szrj 
853*38fd1498Szrj       *RESULT = FN (ARG)
854*38fd1498Szrj 
855*38fd1498Szrj    where ARG_TYPE is the type of ARG and PRECISION is the number of bits
856*38fd1498Szrj    in the result.  Return true on success.  */
857*38fd1498Szrj 
858*38fd1498Szrj static bool
fold_const_call_ss(wide_int * result,combined_fn fn,const wide_int_ref & arg,unsigned int precision,tree arg_type)859*38fd1498Szrj fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
860*38fd1498Szrj 		    unsigned int precision, tree arg_type)
861*38fd1498Szrj {
862*38fd1498Szrj   switch (fn)
863*38fd1498Szrj     {
864*38fd1498Szrj     CASE_CFN_FFS:
865*38fd1498Szrj       *result = wi::shwi (wi::ffs (arg), precision);
866*38fd1498Szrj       return true;
867*38fd1498Szrj 
868*38fd1498Szrj     CASE_CFN_CLZ:
869*38fd1498Szrj       {
870*38fd1498Szrj 	int tmp;
871*38fd1498Szrj 	if (wi::ne_p (arg, 0))
872*38fd1498Szrj 	  tmp = wi::clz (arg);
873*38fd1498Szrj 	else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
874*38fd1498Szrj 					     tmp))
875*38fd1498Szrj 	  tmp = TYPE_PRECISION (arg_type);
876*38fd1498Szrj 	*result = wi::shwi (tmp, precision);
877*38fd1498Szrj 	return true;
878*38fd1498Szrj       }
879*38fd1498Szrj 
880*38fd1498Szrj     CASE_CFN_CTZ:
881*38fd1498Szrj       {
882*38fd1498Szrj 	int tmp;
883*38fd1498Szrj 	if (wi::ne_p (arg, 0))
884*38fd1498Szrj 	  tmp = wi::ctz (arg);
885*38fd1498Szrj 	else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
886*38fd1498Szrj 					     tmp))
887*38fd1498Szrj 	  tmp = TYPE_PRECISION (arg_type);
888*38fd1498Szrj 	*result = wi::shwi (tmp, precision);
889*38fd1498Szrj 	return true;
890*38fd1498Szrj       }
891*38fd1498Szrj 
892*38fd1498Szrj     CASE_CFN_CLRSB:
893*38fd1498Szrj       *result = wi::shwi (wi::clrsb (arg), precision);
894*38fd1498Szrj       return true;
895*38fd1498Szrj 
896*38fd1498Szrj     CASE_CFN_POPCOUNT:
897*38fd1498Szrj       *result = wi::shwi (wi::popcount (arg), precision);
898*38fd1498Szrj       return true;
899*38fd1498Szrj 
900*38fd1498Szrj     CASE_CFN_PARITY:
901*38fd1498Szrj       *result = wi::shwi (wi::parity (arg), precision);
902*38fd1498Szrj       return true;
903*38fd1498Szrj 
904*38fd1498Szrj     case CFN_BUILT_IN_BSWAP16:
905*38fd1498Szrj     case CFN_BUILT_IN_BSWAP32:
906*38fd1498Szrj     case CFN_BUILT_IN_BSWAP64:
907*38fd1498Szrj       *result = wide_int::from (arg, precision, TYPE_SIGN (arg_type)).bswap ();
908*38fd1498Szrj       return true;
909*38fd1498Szrj 
910*38fd1498Szrj     default:
911*38fd1498Szrj       return false;
912*38fd1498Szrj     }
913*38fd1498Szrj }
914*38fd1498Szrj 
915*38fd1498Szrj /* Try to evaluate:
916*38fd1498Szrj 
917*38fd1498Szrj       RESULT = FN (*ARG)
918*38fd1498Szrj 
919*38fd1498Szrj    where FORMAT is the format of ARG and of the real and imaginary parts
920*38fd1498Szrj    of RESULT, passed as RESULT_REAL and RESULT_IMAG respectively.  Return
921*38fd1498Szrj    true on success.  */
922*38fd1498Szrj 
923*38fd1498Szrj static bool
fold_const_call_cs(real_value * result_real,real_value * result_imag,combined_fn fn,const real_value * arg,const real_format * format)924*38fd1498Szrj fold_const_call_cs (real_value *result_real, real_value *result_imag,
925*38fd1498Szrj 		    combined_fn fn, const real_value *arg,
926*38fd1498Szrj 		    const real_format *format)
927*38fd1498Szrj {
928*38fd1498Szrj   switch (fn)
929*38fd1498Szrj     {
930*38fd1498Szrj     CASE_CFN_CEXPI:
931*38fd1498Szrj       /* cexpi(x+yi) = cos(x)+sin(y)*i.  */
932*38fd1498Szrj       return do_mpfr_sincos (result_imag, result_real, arg, format);
933*38fd1498Szrj 
934*38fd1498Szrj     default:
935*38fd1498Szrj       return false;
936*38fd1498Szrj     }
937*38fd1498Szrj }
938*38fd1498Szrj 
939*38fd1498Szrj /* Try to evaluate:
940*38fd1498Szrj 
941*38fd1498Szrj       *RESULT = fn (ARG)
942*38fd1498Szrj 
943*38fd1498Szrj    where FORMAT is the format of RESULT and of the real and imaginary parts
944*38fd1498Szrj    of ARG, passed as ARG_REAL and ARG_IMAG respectively.  Return true on
945*38fd1498Szrj    success.  */
946*38fd1498Szrj 
947*38fd1498Szrj static bool
fold_const_call_sc(real_value * result,combined_fn fn,const real_value * arg_real,const real_value * arg_imag,const real_format * format)948*38fd1498Szrj fold_const_call_sc (real_value *result, combined_fn fn,
949*38fd1498Szrj 		    const real_value *arg_real, const real_value *arg_imag,
950*38fd1498Szrj 		    const real_format *format)
951*38fd1498Szrj {
952*38fd1498Szrj   switch (fn)
953*38fd1498Szrj     {
954*38fd1498Szrj     CASE_CFN_CABS:
955*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_hypot, arg_real, arg_imag, format);
956*38fd1498Szrj 
957*38fd1498Szrj     default:
958*38fd1498Szrj       return false;
959*38fd1498Szrj     }
960*38fd1498Szrj }
961*38fd1498Szrj 
962*38fd1498Szrj /* Try to evaluate:
963*38fd1498Szrj 
964*38fd1498Szrj       RESULT = fn (ARG)
965*38fd1498Szrj 
966*38fd1498Szrj    where FORMAT is the format of the real and imaginary parts of RESULT
967*38fd1498Szrj    (RESULT_REAL and RESULT_IMAG) and of ARG (ARG_REAL and ARG_IMAG).
968*38fd1498Szrj    Return true on success.  */
969*38fd1498Szrj 
970*38fd1498Szrj static bool
fold_const_call_cc(real_value * result_real,real_value * result_imag,combined_fn fn,const real_value * arg_real,const real_value * arg_imag,const real_format * format)971*38fd1498Szrj fold_const_call_cc (real_value *result_real, real_value *result_imag,
972*38fd1498Szrj 		    combined_fn fn, const real_value *arg_real,
973*38fd1498Szrj 		    const real_value *arg_imag, const real_format *format)
974*38fd1498Szrj {
975*38fd1498Szrj   switch (fn)
976*38fd1498Szrj     {
977*38fd1498Szrj     CASE_CFN_CCOS:
978*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_cos,
979*38fd1498Szrj 			  arg_real, arg_imag, format);
980*38fd1498Szrj 
981*38fd1498Szrj     CASE_CFN_CCOSH:
982*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_cosh,
983*38fd1498Szrj 			  arg_real, arg_imag, format);
984*38fd1498Szrj 
985*38fd1498Szrj     CASE_CFN_CPROJ:
986*38fd1498Szrj       if (real_isinf (arg_real) || real_isinf (arg_imag))
987*38fd1498Szrj 	{
988*38fd1498Szrj 	  real_inf (result_real);
989*38fd1498Szrj 	  *result_imag = dconst0;
990*38fd1498Szrj 	  result_imag->sign = arg_imag->sign;
991*38fd1498Szrj 	}
992*38fd1498Szrj       else
993*38fd1498Szrj 	{
994*38fd1498Szrj 	  *result_real = *arg_real;
995*38fd1498Szrj 	  *result_imag = *arg_imag;
996*38fd1498Szrj 	}
997*38fd1498Szrj       return true;
998*38fd1498Szrj 
999*38fd1498Szrj     CASE_CFN_CSIN:
1000*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_sin,
1001*38fd1498Szrj 			  arg_real, arg_imag, format);
1002*38fd1498Szrj 
1003*38fd1498Szrj     CASE_CFN_CSINH:
1004*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_sinh,
1005*38fd1498Szrj 			  arg_real, arg_imag, format);
1006*38fd1498Szrj 
1007*38fd1498Szrj     CASE_CFN_CTAN:
1008*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_tan,
1009*38fd1498Szrj 			  arg_real, arg_imag, format);
1010*38fd1498Szrj 
1011*38fd1498Szrj     CASE_CFN_CTANH:
1012*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_tanh,
1013*38fd1498Szrj 			  arg_real, arg_imag, format);
1014*38fd1498Szrj 
1015*38fd1498Szrj     CASE_CFN_CLOG:
1016*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_log,
1017*38fd1498Szrj 			  arg_real, arg_imag, format);
1018*38fd1498Szrj 
1019*38fd1498Szrj     CASE_CFN_CSQRT:
1020*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_sqrt,
1021*38fd1498Szrj 			  arg_real, arg_imag, format);
1022*38fd1498Szrj 
1023*38fd1498Szrj     CASE_CFN_CASIN:
1024*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_asin,
1025*38fd1498Szrj 			  arg_real, arg_imag, format);
1026*38fd1498Szrj 
1027*38fd1498Szrj     CASE_CFN_CACOS:
1028*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_acos,
1029*38fd1498Szrj 			  arg_real, arg_imag, format);
1030*38fd1498Szrj 
1031*38fd1498Szrj     CASE_CFN_CATAN:
1032*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_atan,
1033*38fd1498Szrj 			  arg_real, arg_imag, format);
1034*38fd1498Szrj 
1035*38fd1498Szrj     CASE_CFN_CASINH:
1036*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_asinh,
1037*38fd1498Szrj 			  arg_real, arg_imag, format);
1038*38fd1498Szrj 
1039*38fd1498Szrj     CASE_CFN_CACOSH:
1040*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_acosh,
1041*38fd1498Szrj 			  arg_real, arg_imag, format);
1042*38fd1498Szrj 
1043*38fd1498Szrj     CASE_CFN_CATANH:
1044*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_atanh,
1045*38fd1498Szrj 			  arg_real, arg_imag, format);
1046*38fd1498Szrj 
1047*38fd1498Szrj     CASE_CFN_CEXP:
1048*38fd1498Szrj       return do_mpc_arg1 (result_real, result_imag, mpc_exp,
1049*38fd1498Szrj 			  arg_real, arg_imag, format);
1050*38fd1498Szrj 
1051*38fd1498Szrj     default:
1052*38fd1498Szrj       return false;
1053*38fd1498Szrj     }
1054*38fd1498Szrj }
1055*38fd1498Szrj 
1056*38fd1498Szrj /* Subroutine of fold_const_call, with the same interface.  Handle cases
1057*38fd1498Szrj    where the arguments and result are numerical.  */
1058*38fd1498Szrj 
1059*38fd1498Szrj static tree
fold_const_call_1(combined_fn fn,tree type,tree arg)1060*38fd1498Szrj fold_const_call_1 (combined_fn fn, tree type, tree arg)
1061*38fd1498Szrj {
1062*38fd1498Szrj   machine_mode mode = TYPE_MODE (type);
1063*38fd1498Szrj   machine_mode arg_mode = TYPE_MODE (TREE_TYPE (arg));
1064*38fd1498Szrj 
1065*38fd1498Szrj   if (integer_cst_p (arg))
1066*38fd1498Szrj     {
1067*38fd1498Szrj       if (SCALAR_INT_MODE_P (mode))
1068*38fd1498Szrj 	{
1069*38fd1498Szrj 	  wide_int result;
1070*38fd1498Szrj 	  if (fold_const_call_ss (&result, fn, wi::to_wide (arg),
1071*38fd1498Szrj 				  TYPE_PRECISION (type), TREE_TYPE (arg)))
1072*38fd1498Szrj 	    return wide_int_to_tree (type, result);
1073*38fd1498Szrj 	}
1074*38fd1498Szrj       return NULL_TREE;
1075*38fd1498Szrj     }
1076*38fd1498Szrj 
1077*38fd1498Szrj   if (real_cst_p (arg))
1078*38fd1498Szrj     {
1079*38fd1498Szrj       gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg_mode));
1080*38fd1498Szrj       if (mode == arg_mode)
1081*38fd1498Szrj 	{
1082*38fd1498Szrj 	  /* real -> real.  */
1083*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1084*38fd1498Szrj 	  if (fold_const_call_ss (&result, fn, TREE_REAL_CST_PTR (arg),
1085*38fd1498Szrj 				  REAL_MODE_FORMAT (mode)))
1086*38fd1498Szrj 	    return build_real (type, result);
1087*38fd1498Szrj 	}
1088*38fd1498Szrj       else if (COMPLEX_MODE_P (mode)
1089*38fd1498Szrj 	       && GET_MODE_INNER (mode) == arg_mode)
1090*38fd1498Szrj 	{
1091*38fd1498Szrj 	  /* real -> complex real.  */
1092*38fd1498Szrj 	  REAL_VALUE_TYPE result_real, result_imag;
1093*38fd1498Szrj 	  if (fold_const_call_cs (&result_real, &result_imag, fn,
1094*38fd1498Szrj 				  TREE_REAL_CST_PTR (arg),
1095*38fd1498Szrj 				  REAL_MODE_FORMAT (arg_mode)))
1096*38fd1498Szrj 	    return build_complex (type,
1097*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_real),
1098*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_imag));
1099*38fd1498Szrj 	}
1100*38fd1498Szrj       else if (INTEGRAL_TYPE_P (type))
1101*38fd1498Szrj 	{
1102*38fd1498Szrj 	  /* real -> int.  */
1103*38fd1498Szrj 	  wide_int result;
1104*38fd1498Szrj 	  if (fold_const_call_ss (&result, fn,
1105*38fd1498Szrj 				  TREE_REAL_CST_PTR (arg),
1106*38fd1498Szrj 				  TYPE_PRECISION (type),
1107*38fd1498Szrj 				  REAL_MODE_FORMAT (arg_mode)))
1108*38fd1498Szrj 	    return wide_int_to_tree (type, result);
1109*38fd1498Szrj 	}
1110*38fd1498Szrj       return NULL_TREE;
1111*38fd1498Szrj     }
1112*38fd1498Szrj 
1113*38fd1498Szrj   if (complex_cst_p (arg))
1114*38fd1498Szrj     {
1115*38fd1498Szrj       gcc_checking_assert (COMPLEX_MODE_P (arg_mode));
1116*38fd1498Szrj       machine_mode inner_mode = GET_MODE_INNER (arg_mode);
1117*38fd1498Szrj       tree argr = TREE_REALPART (arg);
1118*38fd1498Szrj       tree argi = TREE_IMAGPART (arg);
1119*38fd1498Szrj       if (mode == arg_mode
1120*38fd1498Szrj 	  && real_cst_p (argr)
1121*38fd1498Szrj 	  && real_cst_p (argi))
1122*38fd1498Szrj 	{
1123*38fd1498Szrj 	  /* complex real -> complex real.  */
1124*38fd1498Szrj 	  REAL_VALUE_TYPE result_real, result_imag;
1125*38fd1498Szrj 	  if (fold_const_call_cc (&result_real, &result_imag, fn,
1126*38fd1498Szrj 				  TREE_REAL_CST_PTR (argr),
1127*38fd1498Szrj 				  TREE_REAL_CST_PTR (argi),
1128*38fd1498Szrj 				  REAL_MODE_FORMAT (inner_mode)))
1129*38fd1498Szrj 	    return build_complex (type,
1130*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_real),
1131*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_imag));
1132*38fd1498Szrj 	}
1133*38fd1498Szrj       if (mode == inner_mode
1134*38fd1498Szrj 	  && real_cst_p (argr)
1135*38fd1498Szrj 	  && real_cst_p (argi))
1136*38fd1498Szrj 	{
1137*38fd1498Szrj 	  /* complex real -> real.  */
1138*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1139*38fd1498Szrj 	  if (fold_const_call_sc (&result, fn,
1140*38fd1498Szrj 				  TREE_REAL_CST_PTR (argr),
1141*38fd1498Szrj 				  TREE_REAL_CST_PTR (argi),
1142*38fd1498Szrj 				  REAL_MODE_FORMAT (inner_mode)))
1143*38fd1498Szrj 	    return build_real (type, result);
1144*38fd1498Szrj 	}
1145*38fd1498Szrj       return NULL_TREE;
1146*38fd1498Szrj     }
1147*38fd1498Szrj 
1148*38fd1498Szrj   return NULL_TREE;
1149*38fd1498Szrj }
1150*38fd1498Szrj 
1151*38fd1498Szrj /* Try to fold FN (ARG) to a constant.  Return the constant on success,
1152*38fd1498Szrj    otherwise return null.  TYPE is the type of the return value.  */
1153*38fd1498Szrj 
1154*38fd1498Szrj tree
fold_const_call(combined_fn fn,tree type,tree arg)1155*38fd1498Szrj fold_const_call (combined_fn fn, tree type, tree arg)
1156*38fd1498Szrj {
1157*38fd1498Szrj   switch (fn)
1158*38fd1498Szrj     {
1159*38fd1498Szrj     case CFN_BUILT_IN_STRLEN:
1160*38fd1498Szrj       if (const char *str = c_getstr (arg))
1161*38fd1498Szrj 	return build_int_cst (type, strlen (str));
1162*38fd1498Szrj       return NULL_TREE;
1163*38fd1498Szrj 
1164*38fd1498Szrj     CASE_CFN_NAN:
1165*38fd1498Szrj     CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NAN):
1166*38fd1498Szrj     case CFN_BUILT_IN_NAND32:
1167*38fd1498Szrj     case CFN_BUILT_IN_NAND64:
1168*38fd1498Szrj     case CFN_BUILT_IN_NAND128:
1169*38fd1498Szrj       return fold_const_builtin_nan (type, arg, true);
1170*38fd1498Szrj 
1171*38fd1498Szrj     CASE_CFN_NANS:
1172*38fd1498Szrj     CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
1173*38fd1498Szrj       return fold_const_builtin_nan (type, arg, false);
1174*38fd1498Szrj 
1175*38fd1498Szrj     case CFN_REDUC_PLUS:
1176*38fd1498Szrj       return fold_const_reduction (type, arg, PLUS_EXPR);
1177*38fd1498Szrj 
1178*38fd1498Szrj     case CFN_REDUC_MAX:
1179*38fd1498Szrj       return fold_const_reduction (type, arg, MAX_EXPR);
1180*38fd1498Szrj 
1181*38fd1498Szrj     case CFN_REDUC_MIN:
1182*38fd1498Szrj       return fold_const_reduction (type, arg, MIN_EXPR);
1183*38fd1498Szrj 
1184*38fd1498Szrj     case CFN_REDUC_AND:
1185*38fd1498Szrj       return fold_const_reduction (type, arg, BIT_AND_EXPR);
1186*38fd1498Szrj 
1187*38fd1498Szrj     case CFN_REDUC_IOR:
1188*38fd1498Szrj       return fold_const_reduction (type, arg, BIT_IOR_EXPR);
1189*38fd1498Szrj 
1190*38fd1498Szrj     case CFN_REDUC_XOR:
1191*38fd1498Szrj       return fold_const_reduction (type, arg, BIT_XOR_EXPR);
1192*38fd1498Szrj 
1193*38fd1498Szrj     default:
1194*38fd1498Szrj       return fold_const_call_1 (fn, type, arg);
1195*38fd1498Szrj     }
1196*38fd1498Szrj }
1197*38fd1498Szrj 
1198*38fd1498Szrj /* Fold a call to IFN_FOLD_LEFT_<CODE> (ARG0, ARG1), returning a value
1199*38fd1498Szrj    of type TYPE.  */
1200*38fd1498Szrj 
1201*38fd1498Szrj static tree
fold_const_fold_left(tree type,tree arg0,tree arg1,tree_code code)1202*38fd1498Szrj fold_const_fold_left (tree type, tree arg0, tree arg1, tree_code code)
1203*38fd1498Szrj {
1204*38fd1498Szrj   if (TREE_CODE (arg1) != VECTOR_CST)
1205*38fd1498Szrj     return NULL_TREE;
1206*38fd1498Szrj 
1207*38fd1498Szrj   unsigned HOST_WIDE_INT nelts;
1208*38fd1498Szrj   if (!VECTOR_CST_NELTS (arg1).is_constant (&nelts))
1209*38fd1498Szrj     return NULL_TREE;
1210*38fd1498Szrj 
1211*38fd1498Szrj   for (unsigned HOST_WIDE_INT i = 0; i < nelts; i++)
1212*38fd1498Szrj     {
1213*38fd1498Szrj       arg0 = const_binop (code, type, arg0, VECTOR_CST_ELT (arg1, i));
1214*38fd1498Szrj       if (arg0 == NULL_TREE || !CONSTANT_CLASS_P (arg0))
1215*38fd1498Szrj 	return NULL_TREE;
1216*38fd1498Szrj     }
1217*38fd1498Szrj   return arg0;
1218*38fd1498Szrj }
1219*38fd1498Szrj 
1220*38fd1498Szrj /* Try to evaluate:
1221*38fd1498Szrj 
1222*38fd1498Szrj       *RESULT = FN (*ARG0, *ARG1)
1223*38fd1498Szrj 
1224*38fd1498Szrj    in format FORMAT.  Return true on success.  */
1225*38fd1498Szrj 
1226*38fd1498Szrj static bool
fold_const_call_sss(real_value * result,combined_fn fn,const real_value * arg0,const real_value * arg1,const real_format * format)1227*38fd1498Szrj fold_const_call_sss (real_value *result, combined_fn fn,
1228*38fd1498Szrj 		     const real_value *arg0, const real_value *arg1,
1229*38fd1498Szrj 		     const real_format *format)
1230*38fd1498Szrj {
1231*38fd1498Szrj   switch (fn)
1232*38fd1498Szrj     {
1233*38fd1498Szrj     CASE_CFN_DREM:
1234*38fd1498Szrj     CASE_CFN_REMAINDER:
1235*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_remainder, arg0, arg1, format);
1236*38fd1498Szrj 
1237*38fd1498Szrj     CASE_CFN_ATAN2:
1238*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_atan2, arg0, arg1, format);
1239*38fd1498Szrj 
1240*38fd1498Szrj     CASE_CFN_FDIM:
1241*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
1242*38fd1498Szrj 
1243*38fd1498Szrj     CASE_CFN_HYPOT:
1244*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
1245*38fd1498Szrj 
1246*38fd1498Szrj     CASE_CFN_COPYSIGN:
1247*38fd1498Szrj     CASE_CFN_COPYSIGN_FN:
1248*38fd1498Szrj       *result = *arg0;
1249*38fd1498Szrj       real_copysign (result, arg1);
1250*38fd1498Szrj       return true;
1251*38fd1498Szrj 
1252*38fd1498Szrj     CASE_CFN_FMIN:
1253*38fd1498Szrj     CASE_CFN_FMIN_FN:
1254*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_min, arg0, arg1, format);
1255*38fd1498Szrj 
1256*38fd1498Szrj     CASE_CFN_FMAX:
1257*38fd1498Szrj     CASE_CFN_FMAX_FN:
1258*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_max, arg0, arg1, format);
1259*38fd1498Szrj 
1260*38fd1498Szrj     CASE_CFN_POW:
1261*38fd1498Szrj       return fold_const_pow (result, arg0, arg1, format);
1262*38fd1498Szrj 
1263*38fd1498Szrj     default:
1264*38fd1498Szrj       return false;
1265*38fd1498Szrj     }
1266*38fd1498Szrj }
1267*38fd1498Szrj 
1268*38fd1498Szrj /* Try to evaluate:
1269*38fd1498Szrj 
1270*38fd1498Szrj       *RESULT = FN (*ARG0, ARG1)
1271*38fd1498Szrj 
1272*38fd1498Szrj    where FORMAT is the format of *RESULT and *ARG0.  Return true on
1273*38fd1498Szrj    success.  */
1274*38fd1498Szrj 
1275*38fd1498Szrj static bool
fold_const_call_sss(real_value * result,combined_fn fn,const real_value * arg0,const wide_int_ref & arg1,const real_format * format)1276*38fd1498Szrj fold_const_call_sss (real_value *result, combined_fn fn,
1277*38fd1498Szrj 		     const real_value *arg0, const wide_int_ref &arg1,
1278*38fd1498Szrj 		     const real_format *format)
1279*38fd1498Szrj {
1280*38fd1498Szrj   switch (fn)
1281*38fd1498Szrj     {
1282*38fd1498Szrj     CASE_CFN_LDEXP:
1283*38fd1498Szrj       return fold_const_builtin_load_exponent (result, arg0, arg1, format);
1284*38fd1498Szrj 
1285*38fd1498Szrj     CASE_CFN_SCALBN:
1286*38fd1498Szrj     CASE_CFN_SCALBLN:
1287*38fd1498Szrj       return (format->b == 2
1288*38fd1498Szrj 	      && fold_const_builtin_load_exponent (result, arg0, arg1,
1289*38fd1498Szrj 						   format));
1290*38fd1498Szrj 
1291*38fd1498Szrj     CASE_CFN_POWI:
1292*38fd1498Szrj       /* Avoid the folding if flag_signaling_nans is on and
1293*38fd1498Szrj          operand is a signaling NaN.  */
1294*38fd1498Szrj       if (!flag_unsafe_math_optimizations
1295*38fd1498Szrj 	  && flag_signaling_nans
1296*38fd1498Szrj 	  && REAL_VALUE_ISSIGNALING_NAN (*arg0))
1297*38fd1498Szrj         return false;
1298*38fd1498Szrj 
1299*38fd1498Szrj       real_powi (result, format, arg0, arg1.to_shwi ());
1300*38fd1498Szrj       return true;
1301*38fd1498Szrj 
1302*38fd1498Szrj     default:
1303*38fd1498Szrj       return false;
1304*38fd1498Szrj     }
1305*38fd1498Szrj }
1306*38fd1498Szrj 
1307*38fd1498Szrj /* Try to evaluate:
1308*38fd1498Szrj 
1309*38fd1498Szrj       *RESULT = FN (ARG0, *ARG1)
1310*38fd1498Szrj 
1311*38fd1498Szrj    where FORMAT is the format of *RESULT and *ARG1.  Return true on
1312*38fd1498Szrj    success.  */
1313*38fd1498Szrj 
1314*38fd1498Szrj static bool
fold_const_call_sss(real_value * result,combined_fn fn,const wide_int_ref & arg0,const real_value * arg1,const real_format * format)1315*38fd1498Szrj fold_const_call_sss (real_value *result, combined_fn fn,
1316*38fd1498Szrj 		     const wide_int_ref &arg0, const real_value *arg1,
1317*38fd1498Szrj 		     const real_format *format)
1318*38fd1498Szrj {
1319*38fd1498Szrj   switch (fn)
1320*38fd1498Szrj     {
1321*38fd1498Szrj     CASE_CFN_JN:
1322*38fd1498Szrj       return do_mpfr_arg2 (result, mpfr_jn, arg0, arg1, format);
1323*38fd1498Szrj 
1324*38fd1498Szrj     CASE_CFN_YN:
1325*38fd1498Szrj       return (real_compare (GT_EXPR, arg1, &dconst0)
1326*38fd1498Szrj 	      && do_mpfr_arg2 (result, mpfr_yn, arg0, arg1, format));
1327*38fd1498Szrj 
1328*38fd1498Szrj     default:
1329*38fd1498Szrj       return false;
1330*38fd1498Szrj     }
1331*38fd1498Szrj }
1332*38fd1498Szrj 
1333*38fd1498Szrj /* Try to evaluate:
1334*38fd1498Szrj 
1335*38fd1498Szrj       RESULT = fn (ARG0, ARG1)
1336*38fd1498Szrj 
1337*38fd1498Szrj    where FORMAT is the format of the real and imaginary parts of RESULT
1338*38fd1498Szrj    (RESULT_REAL and RESULT_IMAG), of ARG0 (ARG0_REAL and ARG0_IMAG)
1339*38fd1498Szrj    and of ARG1 (ARG1_REAL and ARG1_IMAG).  Return true on success.  */
1340*38fd1498Szrj 
1341*38fd1498Szrj static bool
fold_const_call_ccc(real_value * result_real,real_value * result_imag,combined_fn fn,const real_value * arg0_real,const real_value * arg0_imag,const real_value * arg1_real,const real_value * arg1_imag,const real_format * format)1342*38fd1498Szrj fold_const_call_ccc (real_value *result_real, real_value *result_imag,
1343*38fd1498Szrj 		     combined_fn fn, const real_value *arg0_real,
1344*38fd1498Szrj 		     const real_value *arg0_imag, const real_value *arg1_real,
1345*38fd1498Szrj 		     const real_value *arg1_imag, const real_format *format)
1346*38fd1498Szrj {
1347*38fd1498Szrj   switch (fn)
1348*38fd1498Szrj     {
1349*38fd1498Szrj     CASE_CFN_CPOW:
1350*38fd1498Szrj       return do_mpc_arg2 (result_real, result_imag, mpc_pow,
1351*38fd1498Szrj 			  arg0_real, arg0_imag, arg1_real, arg1_imag, format);
1352*38fd1498Szrj 
1353*38fd1498Szrj     default:
1354*38fd1498Szrj       return false;
1355*38fd1498Szrj     }
1356*38fd1498Szrj }
1357*38fd1498Szrj 
1358*38fd1498Szrj /* Subroutine of fold_const_call, with the same interface.  Handle cases
1359*38fd1498Szrj    where the arguments and result are numerical.  */
1360*38fd1498Szrj 
1361*38fd1498Szrj static tree
fold_const_call_1(combined_fn fn,tree type,tree arg0,tree arg1)1362*38fd1498Szrj fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1)
1363*38fd1498Szrj {
1364*38fd1498Szrj   machine_mode mode = TYPE_MODE (type);
1365*38fd1498Szrj   machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1366*38fd1498Szrj   machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1367*38fd1498Szrj 
1368*38fd1498Szrj   if (arg0_mode == arg1_mode
1369*38fd1498Szrj       && real_cst_p (arg0)
1370*38fd1498Szrj       && real_cst_p (arg1))
1371*38fd1498Szrj     {
1372*38fd1498Szrj       gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1373*38fd1498Szrj       if (mode == arg0_mode)
1374*38fd1498Szrj 	{
1375*38fd1498Szrj 	  /* real, real -> real.  */
1376*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1377*38fd1498Szrj 	  if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1378*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg1),
1379*38fd1498Szrj 				   REAL_MODE_FORMAT (mode)))
1380*38fd1498Szrj 	    return build_real (type, result);
1381*38fd1498Szrj 	}
1382*38fd1498Szrj       return NULL_TREE;
1383*38fd1498Szrj     }
1384*38fd1498Szrj 
1385*38fd1498Szrj   if (real_cst_p (arg0)
1386*38fd1498Szrj       && integer_cst_p (arg1))
1387*38fd1498Szrj     {
1388*38fd1498Szrj       gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1389*38fd1498Szrj       if (mode == arg0_mode)
1390*38fd1498Szrj 	{
1391*38fd1498Szrj 	  /* real, int -> real.  */
1392*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1393*38fd1498Szrj 	  if (fold_const_call_sss (&result, fn, TREE_REAL_CST_PTR (arg0),
1394*38fd1498Szrj 				   wi::to_wide (arg1),
1395*38fd1498Szrj 				   REAL_MODE_FORMAT (mode)))
1396*38fd1498Szrj 	    return build_real (type, result);
1397*38fd1498Szrj 	}
1398*38fd1498Szrj       return NULL_TREE;
1399*38fd1498Szrj     }
1400*38fd1498Szrj 
1401*38fd1498Szrj   if (integer_cst_p (arg0)
1402*38fd1498Szrj       && real_cst_p (arg1))
1403*38fd1498Szrj     {
1404*38fd1498Szrj       gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg1_mode));
1405*38fd1498Szrj       if (mode == arg1_mode)
1406*38fd1498Szrj 	{
1407*38fd1498Szrj 	  /* int, real -> real.  */
1408*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1409*38fd1498Szrj 	  if (fold_const_call_sss (&result, fn, wi::to_wide (arg0),
1410*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg1),
1411*38fd1498Szrj 				   REAL_MODE_FORMAT (mode)))
1412*38fd1498Szrj 	    return build_real (type, result);
1413*38fd1498Szrj 	}
1414*38fd1498Szrj       return NULL_TREE;
1415*38fd1498Szrj     }
1416*38fd1498Szrj 
1417*38fd1498Szrj   if (arg0_mode == arg1_mode
1418*38fd1498Szrj       && complex_cst_p (arg0)
1419*38fd1498Szrj       && complex_cst_p (arg1))
1420*38fd1498Szrj     {
1421*38fd1498Szrj       gcc_checking_assert (COMPLEX_MODE_P (arg0_mode));
1422*38fd1498Szrj       machine_mode inner_mode = GET_MODE_INNER (arg0_mode);
1423*38fd1498Szrj       tree arg0r = TREE_REALPART (arg0);
1424*38fd1498Szrj       tree arg0i = TREE_IMAGPART (arg0);
1425*38fd1498Szrj       tree arg1r = TREE_REALPART (arg1);
1426*38fd1498Szrj       tree arg1i = TREE_IMAGPART (arg1);
1427*38fd1498Szrj       if (mode == arg0_mode
1428*38fd1498Szrj 	  && real_cst_p (arg0r)
1429*38fd1498Szrj 	  && real_cst_p (arg0i)
1430*38fd1498Szrj 	  && real_cst_p (arg1r)
1431*38fd1498Szrj 	  && real_cst_p (arg1i))
1432*38fd1498Szrj 	{
1433*38fd1498Szrj 	  /* complex real, complex real -> complex real.  */
1434*38fd1498Szrj 	  REAL_VALUE_TYPE result_real, result_imag;
1435*38fd1498Szrj 	  if (fold_const_call_ccc (&result_real, &result_imag, fn,
1436*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg0r),
1437*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg0i),
1438*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg1r),
1439*38fd1498Szrj 				   TREE_REAL_CST_PTR (arg1i),
1440*38fd1498Szrj 				   REAL_MODE_FORMAT (inner_mode)))
1441*38fd1498Szrj 	    return build_complex (type,
1442*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_real),
1443*38fd1498Szrj 				  build_real (TREE_TYPE (type), result_imag));
1444*38fd1498Szrj 	}
1445*38fd1498Szrj       return NULL_TREE;
1446*38fd1498Szrj     }
1447*38fd1498Szrj 
1448*38fd1498Szrj   return NULL_TREE;
1449*38fd1498Szrj }
1450*38fd1498Szrj 
1451*38fd1498Szrj /* Try to fold FN (ARG0, ARG1) to a constant.  Return the constant on success,
1452*38fd1498Szrj    otherwise return null.  TYPE is the type of the return value.  */
1453*38fd1498Szrj 
1454*38fd1498Szrj tree
fold_const_call(combined_fn fn,tree type,tree arg0,tree arg1)1455*38fd1498Szrj fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1)
1456*38fd1498Szrj {
1457*38fd1498Szrj   const char *p0, *p1;
1458*38fd1498Szrj   char c;
1459*38fd1498Szrj   switch (fn)
1460*38fd1498Szrj     {
1461*38fd1498Szrj     case CFN_BUILT_IN_STRSPN:
1462*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1463*38fd1498Szrj 	return build_int_cst (type, strspn (p0, p1));
1464*38fd1498Szrj       return NULL_TREE;
1465*38fd1498Szrj 
1466*38fd1498Szrj     case CFN_BUILT_IN_STRCSPN:
1467*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1468*38fd1498Szrj 	return build_int_cst (type, strcspn (p0, p1));
1469*38fd1498Szrj       return NULL_TREE;
1470*38fd1498Szrj 
1471*38fd1498Szrj     case CFN_BUILT_IN_STRCMP:
1472*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1473*38fd1498Szrj 	return build_cmp_result (type, strcmp (p0, p1));
1474*38fd1498Szrj       return NULL_TREE;
1475*38fd1498Szrj 
1476*38fd1498Szrj     case CFN_BUILT_IN_STRCASECMP:
1477*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1478*38fd1498Szrj 	{
1479*38fd1498Szrj 	  int r = strcmp (p0, p1);
1480*38fd1498Szrj 	  if (r == 0)
1481*38fd1498Szrj 	    return build_cmp_result (type, r);
1482*38fd1498Szrj 	}
1483*38fd1498Szrj       return NULL_TREE;
1484*38fd1498Szrj 
1485*38fd1498Szrj     case CFN_BUILT_IN_INDEX:
1486*38fd1498Szrj     case CFN_BUILT_IN_STRCHR:
1487*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1488*38fd1498Szrj 	{
1489*38fd1498Szrj 	  const char *r = strchr (p0, c);
1490*38fd1498Szrj 	  if (r == NULL)
1491*38fd1498Szrj 	    return build_int_cst (type, 0);
1492*38fd1498Szrj 	  return fold_convert (type,
1493*38fd1498Szrj 			       fold_build_pointer_plus_hwi (arg0, r - p0));
1494*38fd1498Szrj 	}
1495*38fd1498Szrj       return NULL_TREE;
1496*38fd1498Szrj 
1497*38fd1498Szrj     case CFN_BUILT_IN_RINDEX:
1498*38fd1498Szrj     case CFN_BUILT_IN_STRRCHR:
1499*38fd1498Szrj       if ((p0 = c_getstr (arg0)) && target_char_cst_p (arg1, &c))
1500*38fd1498Szrj 	{
1501*38fd1498Szrj 	  const char *r = strrchr (p0, c);
1502*38fd1498Szrj 	  if (r == NULL)
1503*38fd1498Szrj 	    return build_int_cst (type, 0);
1504*38fd1498Szrj 	  return fold_convert (type,
1505*38fd1498Szrj 			       fold_build_pointer_plus_hwi (arg0, r - p0));
1506*38fd1498Szrj 	}
1507*38fd1498Szrj       return NULL_TREE;
1508*38fd1498Szrj 
1509*38fd1498Szrj     case CFN_BUILT_IN_STRSTR:
1510*38fd1498Szrj       if ((p1 = c_getstr (arg1)))
1511*38fd1498Szrj 	{
1512*38fd1498Szrj 	  if ((p0 = c_getstr (arg0)))
1513*38fd1498Szrj 	    {
1514*38fd1498Szrj 	      const char *r = strstr (p0, p1);
1515*38fd1498Szrj 	      if (r == NULL)
1516*38fd1498Szrj 		return build_int_cst (type, 0);
1517*38fd1498Szrj 	      return fold_convert (type,
1518*38fd1498Szrj 				   fold_build_pointer_plus_hwi (arg0, r - p0));
1519*38fd1498Szrj 	    }
1520*38fd1498Szrj 	  if (*p1 == '\0')
1521*38fd1498Szrj 	    return fold_convert (type, arg0);
1522*38fd1498Szrj 	}
1523*38fd1498Szrj       return NULL_TREE;
1524*38fd1498Szrj 
1525*38fd1498Szrj     case CFN_FOLD_LEFT_PLUS:
1526*38fd1498Szrj       return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR);
1527*38fd1498Szrj 
1528*38fd1498Szrj     default:
1529*38fd1498Szrj       return fold_const_call_1 (fn, type, arg0, arg1);
1530*38fd1498Szrj     }
1531*38fd1498Szrj }
1532*38fd1498Szrj 
1533*38fd1498Szrj /* Try to evaluate:
1534*38fd1498Szrj 
1535*38fd1498Szrj       *RESULT = FN (*ARG0, *ARG1, *ARG2)
1536*38fd1498Szrj 
1537*38fd1498Szrj    in format FORMAT.  Return true on success.  */
1538*38fd1498Szrj 
1539*38fd1498Szrj static bool
fold_const_call_ssss(real_value * result,combined_fn fn,const real_value * arg0,const real_value * arg1,const real_value * arg2,const real_format * format)1540*38fd1498Szrj fold_const_call_ssss (real_value *result, combined_fn fn,
1541*38fd1498Szrj 		      const real_value *arg0, const real_value *arg1,
1542*38fd1498Szrj 		      const real_value *arg2, const real_format *format)
1543*38fd1498Szrj {
1544*38fd1498Szrj   switch (fn)
1545*38fd1498Szrj     {
1546*38fd1498Szrj     CASE_CFN_FMA:
1547*38fd1498Szrj     CASE_CFN_FMA_FN:
1548*38fd1498Szrj       return do_mpfr_arg3 (result, mpfr_fma, arg0, arg1, arg2, format);
1549*38fd1498Szrj 
1550*38fd1498Szrj     default:
1551*38fd1498Szrj       return false;
1552*38fd1498Szrj     }
1553*38fd1498Szrj }
1554*38fd1498Szrj 
1555*38fd1498Szrj /* Subroutine of fold_const_call, with the same interface.  Handle cases
1556*38fd1498Szrj    where the arguments and result are numerical.  */
1557*38fd1498Szrj 
1558*38fd1498Szrj static tree
fold_const_call_1(combined_fn fn,tree type,tree arg0,tree arg1,tree arg2)1559*38fd1498Szrj fold_const_call_1 (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1560*38fd1498Szrj {
1561*38fd1498Szrj   machine_mode mode = TYPE_MODE (type);
1562*38fd1498Szrj   machine_mode arg0_mode = TYPE_MODE (TREE_TYPE (arg0));
1563*38fd1498Szrj   machine_mode arg1_mode = TYPE_MODE (TREE_TYPE (arg1));
1564*38fd1498Szrj   machine_mode arg2_mode = TYPE_MODE (TREE_TYPE (arg2));
1565*38fd1498Szrj 
1566*38fd1498Szrj   if (arg0_mode == arg1_mode
1567*38fd1498Szrj       && arg0_mode == arg2_mode
1568*38fd1498Szrj       && real_cst_p (arg0)
1569*38fd1498Szrj       && real_cst_p (arg1)
1570*38fd1498Szrj       && real_cst_p (arg2))
1571*38fd1498Szrj     {
1572*38fd1498Szrj       gcc_checking_assert (SCALAR_FLOAT_MODE_P (arg0_mode));
1573*38fd1498Szrj       if (mode == arg0_mode)
1574*38fd1498Szrj 	{
1575*38fd1498Szrj 	  /* real, real, real -> real.  */
1576*38fd1498Szrj 	  REAL_VALUE_TYPE result;
1577*38fd1498Szrj 	  if (fold_const_call_ssss (&result, fn, TREE_REAL_CST_PTR (arg0),
1578*38fd1498Szrj 				    TREE_REAL_CST_PTR (arg1),
1579*38fd1498Szrj 				    TREE_REAL_CST_PTR (arg2),
1580*38fd1498Szrj 				    REAL_MODE_FORMAT (mode)))
1581*38fd1498Szrj 	    return build_real (type, result);
1582*38fd1498Szrj 	}
1583*38fd1498Szrj       return NULL_TREE;
1584*38fd1498Szrj     }
1585*38fd1498Szrj 
1586*38fd1498Szrj   return NULL_TREE;
1587*38fd1498Szrj }
1588*38fd1498Szrj 
1589*38fd1498Szrj /* Try to fold FN (ARG0, ARG1, ARG2) to a constant.  Return the constant on
1590*38fd1498Szrj    success, otherwise return null.  TYPE is the type of the return value.  */
1591*38fd1498Szrj 
1592*38fd1498Szrj tree
fold_const_call(combined_fn fn,tree type,tree arg0,tree arg1,tree arg2)1593*38fd1498Szrj fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
1594*38fd1498Szrj {
1595*38fd1498Szrj   const char *p0, *p1;
1596*38fd1498Szrj   char c;
1597*38fd1498Szrj   unsigned HOST_WIDE_INT s0, s1;
1598*38fd1498Szrj   size_t s2 = 0;
1599*38fd1498Szrj   switch (fn)
1600*38fd1498Szrj     {
1601*38fd1498Szrj     case CFN_BUILT_IN_STRNCMP:
1602*38fd1498Szrj       if (!host_size_t_cst_p (arg2, &s2))
1603*38fd1498Szrj 	return NULL_TREE;
1604*38fd1498Szrj       if (s2 == 0
1605*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg0)
1606*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg1))
1607*38fd1498Szrj 	return build_int_cst (type, 0);
1608*38fd1498Szrj       else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
1609*38fd1498Szrj 	return build_int_cst (type, strncmp (p0, p1, s2));
1610*38fd1498Szrj       return NULL_TREE;
1611*38fd1498Szrj 
1612*38fd1498Szrj     case CFN_BUILT_IN_STRNCASECMP:
1613*38fd1498Szrj       if (!host_size_t_cst_p (arg2, &s2))
1614*38fd1498Szrj 	return NULL_TREE;
1615*38fd1498Szrj       if (s2 == 0
1616*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg0)
1617*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg1))
1618*38fd1498Szrj 	return build_int_cst (type, 0);
1619*38fd1498Szrj       else if ((p0 = c_getstr (arg0))
1620*38fd1498Szrj 	       && (p1 = c_getstr (arg1))
1621*38fd1498Szrj 	       && strncmp (p0, p1, s2) == 0)
1622*38fd1498Szrj 	return build_int_cst (type, 0);
1623*38fd1498Szrj       return NULL_TREE;
1624*38fd1498Szrj 
1625*38fd1498Szrj     case CFN_BUILT_IN_BCMP:
1626*38fd1498Szrj     case CFN_BUILT_IN_MEMCMP:
1627*38fd1498Szrj       if (!host_size_t_cst_p (arg2, &s2))
1628*38fd1498Szrj 	return NULL_TREE;
1629*38fd1498Szrj       if (s2 == 0
1630*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg0)
1631*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg1))
1632*38fd1498Szrj 	return build_int_cst (type, 0);
1633*38fd1498Szrj       if ((p0 = c_getstr (arg0, &s0))
1634*38fd1498Szrj 	  && (p1 = c_getstr (arg1, &s1))
1635*38fd1498Szrj 	  && s2 <= s0
1636*38fd1498Szrj 	  && s2 <= s1)
1637*38fd1498Szrj 	return build_cmp_result (type, memcmp (p0, p1, s2));
1638*38fd1498Szrj       return NULL_TREE;
1639*38fd1498Szrj 
1640*38fd1498Szrj     case CFN_BUILT_IN_MEMCHR:
1641*38fd1498Szrj       if (!host_size_t_cst_p (arg2, &s2))
1642*38fd1498Szrj 	return NULL_TREE;
1643*38fd1498Szrj       if (s2 == 0
1644*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg0)
1645*38fd1498Szrj 	  && !TREE_SIDE_EFFECTS (arg1))
1646*38fd1498Szrj 	return build_int_cst (type, 0);
1647*38fd1498Szrj       if ((p0 = c_getstr (arg0, &s0))
1648*38fd1498Szrj 	  && s2 <= s0
1649*38fd1498Szrj 	  && target_char_cst_p (arg1, &c))
1650*38fd1498Szrj 	{
1651*38fd1498Szrj 	  const char *r = (const char *) memchr (p0, c, s2);
1652*38fd1498Szrj 	  if (r == NULL)
1653*38fd1498Szrj 	    return build_int_cst (type, 0);
1654*38fd1498Szrj 	  return fold_convert (type,
1655*38fd1498Szrj 			       fold_build_pointer_plus_hwi (arg0, r - p0));
1656*38fd1498Szrj 	}
1657*38fd1498Szrj       return NULL_TREE;
1658*38fd1498Szrj 
1659*38fd1498Szrj     default:
1660*38fd1498Szrj       return fold_const_call_1 (fn, type, arg0, arg1, arg2);
1661*38fd1498Szrj     }
1662*38fd1498Szrj }
1663*38fd1498Szrj 
1664*38fd1498Szrj /* Fold a fma operation with arguments ARG[012].  */
1665*38fd1498Szrj 
1666*38fd1498Szrj tree
fold_fma(location_t,tree type,tree arg0,tree arg1,tree arg2)1667*38fd1498Szrj fold_fma (location_t, tree type, tree arg0, tree arg1, tree arg2)
1668*38fd1498Szrj {
1669*38fd1498Szrj   REAL_VALUE_TYPE result;
1670*38fd1498Szrj   if (real_cst_p (arg0)
1671*38fd1498Szrj       && real_cst_p (arg1)
1672*38fd1498Szrj       && real_cst_p (arg2)
1673*38fd1498Szrj       && do_mpfr_arg3 (&result, mpfr_fma, TREE_REAL_CST_PTR (arg0),
1674*38fd1498Szrj 		       TREE_REAL_CST_PTR (arg1), TREE_REAL_CST_PTR (arg2),
1675*38fd1498Szrj 		       REAL_MODE_FORMAT (TYPE_MODE (type))))
1676*38fd1498Szrj     return build_real (type, result);
1677*38fd1498Szrj 
1678*38fd1498Szrj   return NULL_TREE;
1679*38fd1498Szrj }
1680