xref: /dragonfly/contrib/gcc-8.0/gcc/convert.c (revision 38fd1498)
1*38fd1498Szrj /* Utility routines for data type conversion for GCC.
2*38fd1498Szrj    Copyright (C) 1987-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 
21*38fd1498Szrj /* These routines are somewhat language-independent utility function
22*38fd1498Szrj    intended to be called by the language-specific convert () functions.  */
23*38fd1498Szrj 
24*38fd1498Szrj #include "config.h"
25*38fd1498Szrj #include "system.h"
26*38fd1498Szrj #include "coretypes.h"
27*38fd1498Szrj #include "target.h"
28*38fd1498Szrj #include "tree.h"
29*38fd1498Szrj #include "diagnostic-core.h"
30*38fd1498Szrj #include "fold-const.h"
31*38fd1498Szrj #include "stor-layout.h"
32*38fd1498Szrj #include "convert.h"
33*38fd1498Szrj #include "langhooks.h"
34*38fd1498Szrj #include "builtins.h"
35*38fd1498Szrj #include "ubsan.h"
36*38fd1498Szrj #include "stringpool.h"
37*38fd1498Szrj #include "attribs.h"
38*38fd1498Szrj #include "asan.h"
39*38fd1498Szrj 
40*38fd1498Szrj #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
41*38fd1498Szrj   ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR)	     \
42*38fd1498Szrj    : build1_loc (LOC, CODE, TYPE, EXPR))
43*38fd1498Szrj #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
44*38fd1498Szrj   ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2)	     \
45*38fd1498Szrj    : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
46*38fd1498Szrj 
47*38fd1498Szrj /* Convert EXPR to some pointer or reference type TYPE.
48*38fd1498Szrj    EXPR must be pointer, reference, integer, enumeral, or literal zero;
49*38fd1498Szrj    in other cases error is called.  If FOLD_P is true, try to fold the
50*38fd1498Szrj    expression.  */
51*38fd1498Szrj 
52*38fd1498Szrj static tree
convert_to_pointer_1(tree type,tree expr,bool fold_p)53*38fd1498Szrj convert_to_pointer_1 (tree type, tree expr, bool fold_p)
54*38fd1498Szrj {
55*38fd1498Szrj   location_t loc = EXPR_LOCATION (expr);
56*38fd1498Szrj   if (TREE_TYPE (expr) == type)
57*38fd1498Szrj     return expr;
58*38fd1498Szrj 
59*38fd1498Szrj   switch (TREE_CODE (TREE_TYPE (expr)))
60*38fd1498Szrj     {
61*38fd1498Szrj     case POINTER_TYPE:
62*38fd1498Szrj     case REFERENCE_TYPE:
63*38fd1498Szrj       {
64*38fd1498Szrj         /* If the pointers point to different address spaces, conversion needs
65*38fd1498Szrj 	   to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR.  */
66*38fd1498Szrj 	addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
67*38fd1498Szrj 	addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
68*38fd1498Szrj 
69*38fd1498Szrj 	if (to_as == from_as)
70*38fd1498Szrj 	  return maybe_fold_build1_loc (fold_p, loc, NOP_EXPR, type, expr);
71*38fd1498Szrj 	else
72*38fd1498Szrj 	  return maybe_fold_build1_loc (fold_p, loc, ADDR_SPACE_CONVERT_EXPR,
73*38fd1498Szrj 					type, expr);
74*38fd1498Szrj       }
75*38fd1498Szrj 
76*38fd1498Szrj     case INTEGER_TYPE:
77*38fd1498Szrj     case ENUMERAL_TYPE:
78*38fd1498Szrj     case BOOLEAN_TYPE:
79*38fd1498Szrj       {
80*38fd1498Szrj 	/* If the input precision differs from the target pointer type
81*38fd1498Szrj 	   precision, first convert the input expression to an integer type of
82*38fd1498Szrj 	   the target precision.  Some targets, e.g. VMS, need several pointer
83*38fd1498Szrj 	   sizes to coexist so the latter isn't necessarily POINTER_SIZE.  */
84*38fd1498Szrj 	unsigned int pprec = TYPE_PRECISION (type);
85*38fd1498Szrj 	unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
86*38fd1498Szrj 
87*38fd1498Szrj 	if (eprec != pprec)
88*38fd1498Szrj 	  expr
89*38fd1498Szrj 	    = maybe_fold_build1_loc (fold_p, loc, NOP_EXPR,
90*38fd1498Szrj 				     lang_hooks.types.type_for_size (pprec, 0),
91*38fd1498Szrj 				     expr);
92*38fd1498Szrj       }
93*38fd1498Szrj       return maybe_fold_build1_loc (fold_p, loc, CONVERT_EXPR, type, expr);
94*38fd1498Szrj 
95*38fd1498Szrj     default:
96*38fd1498Szrj       error ("cannot convert to a pointer type");
97*38fd1498Szrj       return convert_to_pointer_1 (type, integer_zero_node, fold_p);
98*38fd1498Szrj     }
99*38fd1498Szrj }
100*38fd1498Szrj 
101*38fd1498Szrj /* A wrapper around convert_to_pointer_1 that always folds the
102*38fd1498Szrj    expression.  */
103*38fd1498Szrj 
104*38fd1498Szrj tree
convert_to_pointer(tree type,tree expr)105*38fd1498Szrj convert_to_pointer (tree type, tree expr)
106*38fd1498Szrj {
107*38fd1498Szrj   return convert_to_pointer_1 (type, expr, true);
108*38fd1498Szrj }
109*38fd1498Szrj 
110*38fd1498Szrj /* A wrapper around convert_to_pointer_1 that only folds the
111*38fd1498Szrj    expression if DOFOLD, or if it is CONSTANT_CLASS_P.  */
112*38fd1498Szrj 
113*38fd1498Szrj tree
convert_to_pointer_maybe_fold(tree type,tree expr,bool dofold)114*38fd1498Szrj convert_to_pointer_maybe_fold (tree type, tree expr, bool dofold)
115*38fd1498Szrj {
116*38fd1498Szrj   return convert_to_pointer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
117*38fd1498Szrj }
118*38fd1498Szrj 
119*38fd1498Szrj /* Convert EXPR to some floating-point type TYPE.
120*38fd1498Szrj 
121*38fd1498Szrj    EXPR must be float, fixed-point, integer, or enumeral;
122*38fd1498Szrj    in other cases error is called.  If FOLD_P is true, try to fold
123*38fd1498Szrj    the expression.  */
124*38fd1498Szrj 
125*38fd1498Szrj static tree
convert_to_real_1(tree type,tree expr,bool fold_p)126*38fd1498Szrj convert_to_real_1 (tree type, tree expr, bool fold_p)
127*38fd1498Szrj {
128*38fd1498Szrj   enum built_in_function fcode = builtin_mathfn_code (expr);
129*38fd1498Szrj   tree itype = TREE_TYPE (expr);
130*38fd1498Szrj   location_t loc = EXPR_LOCATION (expr);
131*38fd1498Szrj 
132*38fd1498Szrj   if (TREE_CODE (expr) == COMPOUND_EXPR)
133*38fd1498Szrj     {
134*38fd1498Szrj       tree t = convert_to_real_1 (type, TREE_OPERAND (expr, 1), fold_p);
135*38fd1498Szrj       if (t == TREE_OPERAND (expr, 1))
136*38fd1498Szrj 	return expr;
137*38fd1498Szrj       return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
138*38fd1498Szrj 			 TREE_OPERAND (expr, 0), t);
139*38fd1498Szrj     }
140*38fd1498Szrj 
141*38fd1498Szrj   /* Disable until we figure out how to decide whether the functions are
142*38fd1498Szrj      present in runtime.  */
143*38fd1498Szrj   /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
144*38fd1498Szrj   if (optimize
145*38fd1498Szrj       && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
146*38fd1498Szrj           || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
147*38fd1498Szrj     {
148*38fd1498Szrj       switch (fcode)
149*38fd1498Szrj         {
150*38fd1498Szrj #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
151*38fd1498Szrj 	  CASE_MATHFN (COSH)
152*38fd1498Szrj 	  CASE_MATHFN (EXP)
153*38fd1498Szrj 	  CASE_MATHFN (EXP10)
154*38fd1498Szrj 	  CASE_MATHFN (EXP2)
155*38fd1498Szrj  	  CASE_MATHFN (EXPM1)
156*38fd1498Szrj 	  CASE_MATHFN (GAMMA)
157*38fd1498Szrj 	  CASE_MATHFN (J0)
158*38fd1498Szrj 	  CASE_MATHFN (J1)
159*38fd1498Szrj 	  CASE_MATHFN (LGAMMA)
160*38fd1498Szrj 	  CASE_MATHFN (POW10)
161*38fd1498Szrj 	  CASE_MATHFN (SINH)
162*38fd1498Szrj 	  CASE_MATHFN (TGAMMA)
163*38fd1498Szrj 	  CASE_MATHFN (Y0)
164*38fd1498Szrj 	  CASE_MATHFN (Y1)
165*38fd1498Szrj 	    /* The above functions may set errno differently with float
166*38fd1498Szrj 	       input or output so this transformation is not safe with
167*38fd1498Szrj 	       -fmath-errno.  */
168*38fd1498Szrj 	    if (flag_errno_math)
169*38fd1498Szrj 	      break;
170*38fd1498Szrj 	    gcc_fallthrough ();
171*38fd1498Szrj 	  CASE_MATHFN (ACOS)
172*38fd1498Szrj 	  CASE_MATHFN (ACOSH)
173*38fd1498Szrj 	  CASE_MATHFN (ASIN)
174*38fd1498Szrj  	  CASE_MATHFN (ASINH)
175*38fd1498Szrj  	  CASE_MATHFN (ATAN)
176*38fd1498Szrj 	  CASE_MATHFN (ATANH)
177*38fd1498Szrj  	  CASE_MATHFN (CBRT)
178*38fd1498Szrj  	  CASE_MATHFN (COS)
179*38fd1498Szrj  	  CASE_MATHFN (ERF)
180*38fd1498Szrj  	  CASE_MATHFN (ERFC)
181*38fd1498Szrj 	  CASE_MATHFN (LOG)
182*38fd1498Szrj 	  CASE_MATHFN (LOG10)
183*38fd1498Szrj 	  CASE_MATHFN (LOG2)
184*38fd1498Szrj  	  CASE_MATHFN (LOG1P)
185*38fd1498Szrj  	  CASE_MATHFN (SIN)
186*38fd1498Szrj  	  CASE_MATHFN (TAN)
187*38fd1498Szrj  	  CASE_MATHFN (TANH)
188*38fd1498Szrj 	    /* The above functions are not safe to do this conversion.  */
189*38fd1498Szrj 	    if (!flag_unsafe_math_optimizations)
190*38fd1498Szrj 	      break;
191*38fd1498Szrj 	    gcc_fallthrough ();
192*38fd1498Szrj 	  CASE_MATHFN (SQRT)
193*38fd1498Szrj 	  CASE_MATHFN (FABS)
194*38fd1498Szrj 	  CASE_MATHFN (LOGB)
195*38fd1498Szrj #undef CASE_MATHFN
196*38fd1498Szrj 	    {
197*38fd1498Szrj 	      tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
198*38fd1498Szrj 	      tree newtype = type;
199*38fd1498Szrj 
200*38fd1498Szrj 	      /* We have (outertype)sqrt((innertype)x).  Choose the wider mode from
201*38fd1498Szrj 		 the both as the safe type for operation.  */
202*38fd1498Szrj 	      if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
203*38fd1498Szrj 		newtype = TREE_TYPE (arg0);
204*38fd1498Szrj 
205*38fd1498Szrj 	      /* We consider to convert
206*38fd1498Szrj 
207*38fd1498Szrj 		     (T1) sqrtT2 ((T2) exprT3)
208*38fd1498Szrj 		 to
209*38fd1498Szrj 		     (T1) sqrtT4 ((T4) exprT3)
210*38fd1498Szrj 
211*38fd1498Szrj 		  , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
212*38fd1498Szrj 		 and T4 is NEWTYPE.  All those types are of floating point types.
213*38fd1498Szrj 		 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
214*38fd1498Szrj 		 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
215*38fd1498Szrj 		 T2 and T4.  See the following URL for a reference:
216*38fd1498Szrj 		 http://stackoverflow.com/questions/9235456/determining-
217*38fd1498Szrj                  floating-point-square-root
218*38fd1498Szrj 		 */
219*38fd1498Szrj 	      if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
220*38fd1498Szrj 		  && !flag_unsafe_math_optimizations)
221*38fd1498Szrj 		{
222*38fd1498Szrj 		  /* The following conversion is unsafe even the precision condition
223*38fd1498Szrj 		     below is satisfied:
224*38fd1498Szrj 
225*38fd1498Szrj 		     (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
226*38fd1498Szrj 		    */
227*38fd1498Szrj 		  if (TYPE_MODE (type) != TYPE_MODE (newtype))
228*38fd1498Szrj 		    break;
229*38fd1498Szrj 
230*38fd1498Szrj 		  int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
231*38fd1498Szrj 		  int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
232*38fd1498Szrj 		  if (p1 < p2 * 2 + 2)
233*38fd1498Szrj 		    break;
234*38fd1498Szrj 		}
235*38fd1498Szrj 
236*38fd1498Szrj 	      /* Be careful about integer to fp conversions.
237*38fd1498Szrj 		 These may overflow still.  */
238*38fd1498Szrj 	      if (FLOAT_TYPE_P (TREE_TYPE (arg0))
239*38fd1498Szrj 		  && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
240*38fd1498Szrj 		  && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
241*38fd1498Szrj 		      || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
242*38fd1498Szrj 		{
243*38fd1498Szrj 		  tree fn = mathfn_built_in (newtype, fcode);
244*38fd1498Szrj 		  if (fn)
245*38fd1498Szrj 		    {
246*38fd1498Szrj 		      tree arg = convert_to_real_1 (newtype, arg0, fold_p);
247*38fd1498Szrj 		      expr = build_call_expr (fn, 1, arg);
248*38fd1498Szrj 		      if (newtype == type)
249*38fd1498Szrj 			return expr;
250*38fd1498Szrj 		    }
251*38fd1498Szrj 		}
252*38fd1498Szrj 	    }
253*38fd1498Szrj 	default:
254*38fd1498Szrj 	  break;
255*38fd1498Szrj 	}
256*38fd1498Szrj     }
257*38fd1498Szrj 
258*38fd1498Szrj   /* Propagate the cast into the operation.  */
259*38fd1498Szrj   if (itype != type && FLOAT_TYPE_P (type))
260*38fd1498Szrj     switch (TREE_CODE (expr))
261*38fd1498Szrj       {
262*38fd1498Szrj 	/* Convert (float)-x into -(float)x.  This is safe for
263*38fd1498Szrj 	   round-to-nearest rounding mode when the inner type is float.  */
264*38fd1498Szrj 	case ABS_EXPR:
265*38fd1498Szrj 	case NEGATE_EXPR:
266*38fd1498Szrj 	  if (!flag_rounding_math
267*38fd1498Szrj 	      && FLOAT_TYPE_P (itype)
268*38fd1498Szrj 	      && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
269*38fd1498Szrj 	    {
270*38fd1498Szrj 	      tree arg = convert_to_real_1 (type, TREE_OPERAND (expr, 0),
271*38fd1498Szrj 					    fold_p);
272*38fd1498Szrj 	      return build1 (TREE_CODE (expr), type, arg);
273*38fd1498Szrj 	    }
274*38fd1498Szrj 	  break;
275*38fd1498Szrj 	/* Convert (outertype)((innertype0)a+(innertype1)b)
276*38fd1498Szrj 	   into ((newtype)a+(newtype)b) where newtype
277*38fd1498Szrj 	   is the widest mode from all of these.  */
278*38fd1498Szrj 	case PLUS_EXPR:
279*38fd1498Szrj 	case MINUS_EXPR:
280*38fd1498Szrj 	case MULT_EXPR:
281*38fd1498Szrj 	case RDIV_EXPR:
282*38fd1498Szrj 	   {
283*38fd1498Szrj 	     tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
284*38fd1498Szrj 	     tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
285*38fd1498Szrj 
286*38fd1498Szrj 	     if (FLOAT_TYPE_P (TREE_TYPE (arg0))
287*38fd1498Szrj 		 && FLOAT_TYPE_P (TREE_TYPE (arg1))
288*38fd1498Szrj 		 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
289*38fd1498Szrj 	       {
290*38fd1498Szrj 		  tree newtype = type;
291*38fd1498Szrj 
292*38fd1498Szrj 		  if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
293*38fd1498Szrj 		      || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
294*38fd1498Szrj 		      || TYPE_MODE (type) == SDmode)
295*38fd1498Szrj 		    newtype = dfloat32_type_node;
296*38fd1498Szrj 		  if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
297*38fd1498Szrj 		      || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
298*38fd1498Szrj 		      || TYPE_MODE (type) == DDmode)
299*38fd1498Szrj 		    newtype = dfloat64_type_node;
300*38fd1498Szrj 		  if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
301*38fd1498Szrj 		      || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
302*38fd1498Szrj 		      || TYPE_MODE (type) == TDmode)
303*38fd1498Szrj                     newtype = dfloat128_type_node;
304*38fd1498Szrj 		  if (newtype == dfloat32_type_node
305*38fd1498Szrj 		      || newtype == dfloat64_type_node
306*38fd1498Szrj 		      || newtype == dfloat128_type_node)
307*38fd1498Szrj 		    {
308*38fd1498Szrj 		      expr = build2 (TREE_CODE (expr), newtype,
309*38fd1498Szrj 				     convert_to_real_1 (newtype, arg0,
310*38fd1498Szrj 							fold_p),
311*38fd1498Szrj 				     convert_to_real_1 (newtype, arg1,
312*38fd1498Szrj 							fold_p));
313*38fd1498Szrj 		      if (newtype == type)
314*38fd1498Szrj 			return expr;
315*38fd1498Szrj 		      break;
316*38fd1498Szrj 		    }
317*38fd1498Szrj 
318*38fd1498Szrj 		  if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
319*38fd1498Szrj 		    newtype = TREE_TYPE (arg0);
320*38fd1498Szrj 		  if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
321*38fd1498Szrj 		    newtype = TREE_TYPE (arg1);
322*38fd1498Szrj 		  /* Sometimes this transformation is safe (cannot
323*38fd1498Szrj 		     change results through affecting double rounding
324*38fd1498Szrj 		     cases) and sometimes it is not.  If NEWTYPE is
325*38fd1498Szrj 		     wider than TYPE, e.g. (float)((long double)double
326*38fd1498Szrj 		     + (long double)double) converted to
327*38fd1498Szrj 		     (float)(double + double), the transformation is
328*38fd1498Szrj 		     unsafe regardless of the details of the types
329*38fd1498Szrj 		     involved; double rounding can arise if the result
330*38fd1498Szrj 		     of NEWTYPE arithmetic is a NEWTYPE value half way
331*38fd1498Szrj 		     between two representable TYPE values but the
332*38fd1498Szrj 		     exact value is sufficiently different (in the
333*38fd1498Szrj 		     right direction) for this difference to be
334*38fd1498Szrj 		     visible in ITYPE arithmetic.  If NEWTYPE is the
335*38fd1498Szrj 		     same as TYPE, however, the transformation may be
336*38fd1498Szrj 		     safe depending on the types involved: it is safe
337*38fd1498Szrj 		     if the ITYPE has strictly more than twice as many
338*38fd1498Szrj 		     mantissa bits as TYPE, can represent infinities
339*38fd1498Szrj 		     and NaNs if the TYPE can, and has sufficient
340*38fd1498Szrj 		     exponent range for the product or ratio of two
341*38fd1498Szrj 		     values representable in the TYPE to be within the
342*38fd1498Szrj 		     range of normal values of ITYPE.  */
343*38fd1498Szrj 		  if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
344*38fd1498Szrj 		      && (flag_unsafe_math_optimizations
345*38fd1498Szrj 			  || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
346*38fd1498Szrj 			      && real_can_shorten_arithmetic (TYPE_MODE (itype),
347*38fd1498Szrj 							      TYPE_MODE (type))
348*38fd1498Szrj 			      && !excess_precision_type (newtype))))
349*38fd1498Szrj 		    {
350*38fd1498Szrj 		      expr = build2 (TREE_CODE (expr), newtype,
351*38fd1498Szrj 				     convert_to_real_1 (newtype, arg0,
352*38fd1498Szrj 							fold_p),
353*38fd1498Szrj 				     convert_to_real_1 (newtype, arg1,
354*38fd1498Szrj 							fold_p));
355*38fd1498Szrj 		      if (newtype == type)
356*38fd1498Szrj 			return expr;
357*38fd1498Szrj 		    }
358*38fd1498Szrj 	       }
359*38fd1498Szrj 	   }
360*38fd1498Szrj 	  break;
361*38fd1498Szrj 	default:
362*38fd1498Szrj 	  break;
363*38fd1498Szrj       }
364*38fd1498Szrj 
365*38fd1498Szrj   switch (TREE_CODE (TREE_TYPE (expr)))
366*38fd1498Szrj     {
367*38fd1498Szrj     case REAL_TYPE:
368*38fd1498Szrj       /* Ignore the conversion if we don't need to store intermediate
369*38fd1498Szrj 	 results and neither type is a decimal float.  */
370*38fd1498Szrj       return build1_loc (loc,
371*38fd1498Szrj 			 (flag_float_store
372*38fd1498Szrj 			  || DECIMAL_FLOAT_TYPE_P (type)
373*38fd1498Szrj 			  || DECIMAL_FLOAT_TYPE_P (itype))
374*38fd1498Szrj 			 ? CONVERT_EXPR : NOP_EXPR, type, expr);
375*38fd1498Szrj 
376*38fd1498Szrj     case INTEGER_TYPE:
377*38fd1498Szrj     case ENUMERAL_TYPE:
378*38fd1498Szrj     case BOOLEAN_TYPE:
379*38fd1498Szrj       return build1 (FLOAT_EXPR, type, expr);
380*38fd1498Szrj 
381*38fd1498Szrj     case FIXED_POINT_TYPE:
382*38fd1498Szrj       return build1 (FIXED_CONVERT_EXPR, type, expr);
383*38fd1498Szrj 
384*38fd1498Szrj     case COMPLEX_TYPE:
385*38fd1498Szrj       return convert (type,
386*38fd1498Szrj 		      maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
387*38fd1498Szrj 					     TREE_TYPE (TREE_TYPE (expr)),
388*38fd1498Szrj 					     expr));
389*38fd1498Szrj 
390*38fd1498Szrj     case POINTER_TYPE:
391*38fd1498Szrj     case REFERENCE_TYPE:
392*38fd1498Szrj       error ("pointer value used where a floating point value was expected");
393*38fd1498Szrj       return convert_to_real_1 (type, integer_zero_node, fold_p);
394*38fd1498Szrj 
395*38fd1498Szrj     default:
396*38fd1498Szrj       error ("aggregate value used where a float was expected");
397*38fd1498Szrj       return convert_to_real_1 (type, integer_zero_node, fold_p);
398*38fd1498Szrj     }
399*38fd1498Szrj }
400*38fd1498Szrj 
401*38fd1498Szrj /* A wrapper around convert_to_real_1 that always folds the
402*38fd1498Szrj    expression.  */
403*38fd1498Szrj 
404*38fd1498Szrj tree
convert_to_real(tree type,tree expr)405*38fd1498Szrj convert_to_real (tree type, tree expr)
406*38fd1498Szrj {
407*38fd1498Szrj   return convert_to_real_1 (type, expr, true);
408*38fd1498Szrj }
409*38fd1498Szrj 
410*38fd1498Szrj /* A wrapper around convert_to_real_1 that only folds the
411*38fd1498Szrj    expression if DOFOLD, or if it is CONSTANT_CLASS_P.  */
412*38fd1498Szrj 
413*38fd1498Szrj tree
convert_to_real_maybe_fold(tree type,tree expr,bool dofold)414*38fd1498Szrj convert_to_real_maybe_fold (tree type, tree expr, bool dofold)
415*38fd1498Szrj {
416*38fd1498Szrj   return convert_to_real_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
417*38fd1498Szrj }
418*38fd1498Szrj 
419*38fd1498Szrj /* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
420*38fd1498Szrj    result in TYPE.  */
421*38fd1498Szrj 
422*38fd1498Szrj static tree
do_narrow(location_t loc,enum tree_code ex_form,tree type,tree arg0,tree arg1,tree expr,unsigned inprec,unsigned outprec,bool dofold)423*38fd1498Szrj do_narrow (location_t loc,
424*38fd1498Szrj 	   enum tree_code ex_form, tree type, tree arg0, tree arg1,
425*38fd1498Szrj 	   tree expr, unsigned inprec, unsigned outprec, bool dofold)
426*38fd1498Szrj {
427*38fd1498Szrj   /* Do the arithmetic in type TYPEX,
428*38fd1498Szrj      then convert result to TYPE.  */
429*38fd1498Szrj   tree typex = type;
430*38fd1498Szrj 
431*38fd1498Szrj   /* Can't do arithmetic in enumeral types
432*38fd1498Szrj      so use an integer type that will hold the values.  */
433*38fd1498Szrj   if (TREE_CODE (typex) == ENUMERAL_TYPE)
434*38fd1498Szrj     typex = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
435*38fd1498Szrj 					    TYPE_UNSIGNED (typex));
436*38fd1498Szrj 
437*38fd1498Szrj   /* The type demotion below might cause doing unsigned arithmetic
438*38fd1498Szrj      instead of signed, and thus hide overflow bugs.  */
439*38fd1498Szrj   if ((ex_form == PLUS_EXPR || ex_form == MINUS_EXPR)
440*38fd1498Szrj       && !TYPE_UNSIGNED (typex)
441*38fd1498Szrj       && sanitize_flags_p (SANITIZE_SI_OVERFLOW))
442*38fd1498Szrj     return NULL_TREE;
443*38fd1498Szrj 
444*38fd1498Szrj   /* But now perhaps TYPEX is as wide as INPREC.
445*38fd1498Szrj      In that case, do nothing special here.
446*38fd1498Szrj      (Otherwise would recurse infinitely in convert.  */
447*38fd1498Szrj   if (TYPE_PRECISION (typex) != inprec)
448*38fd1498Szrj     {
449*38fd1498Szrj       /* Don't do unsigned arithmetic where signed was wanted,
450*38fd1498Szrj 	 or vice versa.
451*38fd1498Szrj 	 Exception: if both of the original operands were
452*38fd1498Szrj 	 unsigned then we can safely do the work as unsigned.
453*38fd1498Szrj 	 Exception: shift operations take their type solely
454*38fd1498Szrj 	 from the first argument.
455*38fd1498Szrj 	 Exception: the LSHIFT_EXPR case above requires that
456*38fd1498Szrj 	 we perform this operation unsigned lest we produce
457*38fd1498Szrj 	 signed-overflow undefinedness.
458*38fd1498Szrj 	 And we may need to do it as unsigned
459*38fd1498Szrj 	 if we truncate to the original size.  */
460*38fd1498Szrj       if (TYPE_UNSIGNED (TREE_TYPE (expr))
461*38fd1498Szrj 	  || (TYPE_UNSIGNED (TREE_TYPE (arg0))
462*38fd1498Szrj 	      && (TYPE_UNSIGNED (TREE_TYPE (arg1))
463*38fd1498Szrj 		  || ex_form == LSHIFT_EXPR
464*38fd1498Szrj 		  || ex_form == RSHIFT_EXPR
465*38fd1498Szrj 		  || ex_form == LROTATE_EXPR
466*38fd1498Szrj 		  || ex_form == RROTATE_EXPR))
467*38fd1498Szrj 	  || ex_form == LSHIFT_EXPR
468*38fd1498Szrj 	  /* If we have !flag_wrapv, and either ARG0 or
469*38fd1498Szrj 	     ARG1 is of a signed type, we have to do
470*38fd1498Szrj 	     PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
471*38fd1498Szrj 	     type in case the operation in outprec precision
472*38fd1498Szrj 	     could overflow.  Otherwise, we would introduce
473*38fd1498Szrj 	     signed-overflow undefinedness.  */
474*38fd1498Szrj 	  || ((!(INTEGRAL_TYPE_P (TREE_TYPE (arg0))
475*38fd1498Szrj 		 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
476*38fd1498Szrj 	       || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1))
477*38fd1498Szrj 		    && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))))
478*38fd1498Szrj 	      && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
479*38fd1498Szrj 		   > outprec)
480*38fd1498Szrj 		  || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
481*38fd1498Szrj 		      > outprec))
482*38fd1498Szrj 	      && (ex_form == PLUS_EXPR
483*38fd1498Szrj 		  || ex_form == MINUS_EXPR
484*38fd1498Szrj 		  || ex_form == MULT_EXPR)))
485*38fd1498Szrj 	{
486*38fd1498Szrj 	  if (!TYPE_UNSIGNED (typex))
487*38fd1498Szrj 	    typex = unsigned_type_for (typex);
488*38fd1498Szrj 	}
489*38fd1498Szrj       else
490*38fd1498Szrj 	{
491*38fd1498Szrj 	  if (TYPE_UNSIGNED (typex))
492*38fd1498Szrj 	    typex = signed_type_for (typex);
493*38fd1498Szrj 	}
494*38fd1498Szrj       /* We should do away with all this once we have a proper
495*38fd1498Szrj 	 type promotion/demotion pass, see PR45397.  */
496*38fd1498Szrj       expr = maybe_fold_build2_loc (dofold, loc, ex_form, typex,
497*38fd1498Szrj 				    convert (typex, arg0),
498*38fd1498Szrj 				    convert (typex, arg1));
499*38fd1498Szrj       return convert (type, expr);
500*38fd1498Szrj     }
501*38fd1498Szrj 
502*38fd1498Szrj   return NULL_TREE;
503*38fd1498Szrj }
504*38fd1498Szrj 
505*38fd1498Szrj /* Convert EXPR to some integer (or enum) type TYPE.
506*38fd1498Szrj 
507*38fd1498Szrj    EXPR must be pointer, integer, discrete (enum, char, or bool), float,
508*38fd1498Szrj    fixed-point or vector; in other cases error is called.
509*38fd1498Szrj 
510*38fd1498Szrj    If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
511*38fd1498Szrj 
512*38fd1498Szrj    The result of this is always supposed to be a newly created tree node
513*38fd1498Szrj    not in use in any existing structure.  */
514*38fd1498Szrj 
515*38fd1498Szrj static tree
convert_to_integer_1(tree type,tree expr,bool dofold)516*38fd1498Szrj convert_to_integer_1 (tree type, tree expr, bool dofold)
517*38fd1498Szrj {
518*38fd1498Szrj   enum tree_code ex_form = TREE_CODE (expr);
519*38fd1498Szrj   tree intype = TREE_TYPE (expr);
520*38fd1498Szrj   unsigned int inprec = element_precision (intype);
521*38fd1498Szrj   unsigned int outprec = element_precision (type);
522*38fd1498Szrj   location_t loc = EXPR_LOCATION (expr);
523*38fd1498Szrj 
524*38fd1498Szrj   /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
525*38fd1498Szrj      be.  Consider `enum E = { a, b = (enum E) 3 };'.  */
526*38fd1498Szrj   if (!COMPLETE_TYPE_P (type))
527*38fd1498Szrj     {
528*38fd1498Szrj       error ("conversion to incomplete type");
529*38fd1498Szrj       return error_mark_node;
530*38fd1498Szrj     }
531*38fd1498Szrj 
532*38fd1498Szrj   if (ex_form == COMPOUND_EXPR)
533*38fd1498Szrj     {
534*38fd1498Szrj       tree t = convert_to_integer_1 (type, TREE_OPERAND (expr, 1), dofold);
535*38fd1498Szrj       if (t == TREE_OPERAND (expr, 1))
536*38fd1498Szrj 	return expr;
537*38fd1498Szrj       return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
538*38fd1498Szrj 			 TREE_OPERAND (expr, 0), t);
539*38fd1498Szrj     }
540*38fd1498Szrj 
541*38fd1498Szrj   /* Convert e.g. (long)round(d) -> lround(d).  */
542*38fd1498Szrj   /* If we're converting to char, we may encounter differing behavior
543*38fd1498Szrj      between converting from double->char vs double->long->char.
544*38fd1498Szrj      We're in "undefined" territory but we prefer to be conservative,
545*38fd1498Szrj      so only proceed in "unsafe" math mode.  */
546*38fd1498Szrj   if (optimize
547*38fd1498Szrj       && (flag_unsafe_math_optimizations
548*38fd1498Szrj 	  || (long_integer_type_node
549*38fd1498Szrj 	      && outprec >= TYPE_PRECISION (long_integer_type_node))))
550*38fd1498Szrj     {
551*38fd1498Szrj       tree s_expr = strip_float_extensions (expr);
552*38fd1498Szrj       tree s_intype = TREE_TYPE (s_expr);
553*38fd1498Szrj       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
554*38fd1498Szrj       tree fn = 0;
555*38fd1498Szrj 
556*38fd1498Szrj       switch (fcode)
557*38fd1498Szrj         {
558*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_CEIL):
559*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
560*38fd1498Szrj 	  /* Only convert in ISO C99 mode.  */
561*38fd1498Szrj 	  if (!targetm.libc_has_function (function_c99_misc))
562*38fd1498Szrj 	    break;
563*38fd1498Szrj 	  if (outprec < TYPE_PRECISION (integer_type_node)
564*38fd1498Szrj 	      || (outprec == TYPE_PRECISION (integer_type_node)
565*38fd1498Szrj 		  && !TYPE_UNSIGNED (type)))
566*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
567*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_integer_type_node)
568*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
569*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
570*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
571*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
572*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
573*38fd1498Szrj 	  break;
574*38fd1498Szrj 
575*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_FLOOR):
576*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
577*38fd1498Szrj 	  /* Only convert in ISO C99 mode.  */
578*38fd1498Szrj 	  if (!targetm.libc_has_function (function_c99_misc))
579*38fd1498Szrj 	    break;
580*38fd1498Szrj 	  if (outprec < TYPE_PRECISION (integer_type_node)
581*38fd1498Szrj 	      || (outprec == TYPE_PRECISION (integer_type_node)
582*38fd1498Szrj 		  && !TYPE_UNSIGNED (type)))
583*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
584*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_integer_type_node)
585*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
586*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
587*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
588*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
589*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
590*38fd1498Szrj 	  break;
591*38fd1498Szrj 
592*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_ROUND):
593*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
594*38fd1498Szrj 	  /* Only convert in ISO C99 mode and with -fno-math-errno.  */
595*38fd1498Szrj 	  if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
596*38fd1498Szrj 	    break;
597*38fd1498Szrj 	  if (outprec < TYPE_PRECISION (integer_type_node)
598*38fd1498Szrj 	      || (outprec == TYPE_PRECISION (integer_type_node)
599*38fd1498Szrj 		  && !TYPE_UNSIGNED (type)))
600*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
601*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_integer_type_node)
602*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
603*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
604*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
605*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
606*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
607*38fd1498Szrj 	  break;
608*38fd1498Szrj 
609*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_NEARBYINT):
610*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
611*38fd1498Szrj 	  /* Only convert nearbyint* if we can ignore math exceptions.  */
612*38fd1498Szrj 	  if (flag_trapping_math)
613*38fd1498Szrj 	    break;
614*38fd1498Szrj 	  gcc_fallthrough ();
615*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_RINT):
616*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
617*38fd1498Szrj 	  /* Only convert in ISO C99 mode and with -fno-math-errno.  */
618*38fd1498Szrj 	  if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
619*38fd1498Szrj 	    break;
620*38fd1498Szrj 	  if (outprec < TYPE_PRECISION (integer_type_node)
621*38fd1498Szrj 	      || (outprec == TYPE_PRECISION (integer_type_node)
622*38fd1498Szrj 		  && !TYPE_UNSIGNED (type)))
623*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
624*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_integer_type_node)
625*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
626*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
627*38fd1498Szrj 	  else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
628*38fd1498Szrj 		   && !TYPE_UNSIGNED (type))
629*38fd1498Szrj 	    fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
630*38fd1498Szrj 	  break;
631*38fd1498Szrj 
632*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_TRUNC):
633*38fd1498Szrj 	CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
634*38fd1498Szrj 	  return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0), dofold);
635*38fd1498Szrj 
636*38fd1498Szrj 	default:
637*38fd1498Szrj 	  break;
638*38fd1498Szrj 	}
639*38fd1498Szrj 
640*38fd1498Szrj       if (fn)
641*38fd1498Szrj         {
642*38fd1498Szrj 	  tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
643*38fd1498Szrj 	  return convert_to_integer_1 (type, newexpr, dofold);
644*38fd1498Szrj 	}
645*38fd1498Szrj     }
646*38fd1498Szrj 
647*38fd1498Szrj   /* Convert (int)logb(d) -> ilogb(d).  */
648*38fd1498Szrj   if (optimize
649*38fd1498Szrj       && flag_unsafe_math_optimizations
650*38fd1498Szrj       && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
651*38fd1498Szrj       && integer_type_node
652*38fd1498Szrj       && (outprec > TYPE_PRECISION (integer_type_node)
653*38fd1498Szrj 	  || (outprec == TYPE_PRECISION (integer_type_node)
654*38fd1498Szrj 	      && !TYPE_UNSIGNED (type))))
655*38fd1498Szrj     {
656*38fd1498Szrj       tree s_expr = strip_float_extensions (expr);
657*38fd1498Szrj       tree s_intype = TREE_TYPE (s_expr);
658*38fd1498Szrj       const enum built_in_function fcode = builtin_mathfn_code (s_expr);
659*38fd1498Szrj       tree fn = 0;
660*38fd1498Szrj 
661*38fd1498Szrj       switch (fcode)
662*38fd1498Szrj 	{
663*38fd1498Szrj 	CASE_FLT_FN (BUILT_IN_LOGB):
664*38fd1498Szrj 	  fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
665*38fd1498Szrj 	  break;
666*38fd1498Szrj 
667*38fd1498Szrj 	default:
668*38fd1498Szrj 	  break;
669*38fd1498Szrj 	}
670*38fd1498Szrj 
671*38fd1498Szrj       if (fn)
672*38fd1498Szrj         {
673*38fd1498Szrj 	  tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
674*38fd1498Szrj 	  return convert_to_integer_1 (type, newexpr, dofold);
675*38fd1498Szrj 	}
676*38fd1498Szrj     }
677*38fd1498Szrj 
678*38fd1498Szrj   switch (TREE_CODE (intype))
679*38fd1498Szrj     {
680*38fd1498Szrj     case POINTER_TYPE:
681*38fd1498Szrj     case REFERENCE_TYPE:
682*38fd1498Szrj       if (integer_zerop (expr) && !TREE_OVERFLOW (expr))
683*38fd1498Szrj 	return build_int_cst (type, 0);
684*38fd1498Szrj 
685*38fd1498Szrj       /* Convert to an unsigned integer of the correct width first, and from
686*38fd1498Szrj 	 there widen/truncate to the required type.  Some targets support the
687*38fd1498Szrj 	 coexistence of multiple valid pointer sizes, so fetch the one we need
688*38fd1498Szrj 	 from the type.  */
689*38fd1498Szrj       if (!dofold)
690*38fd1498Szrj 	return build1 (CONVERT_EXPR, type, expr);
691*38fd1498Szrj       expr = fold_build1 (CONVERT_EXPR,
692*38fd1498Szrj 			  lang_hooks.types.type_for_size
693*38fd1498Szrj 			    (TYPE_PRECISION (intype), 0),
694*38fd1498Szrj 			  expr);
695*38fd1498Szrj       return fold_convert (type, expr);
696*38fd1498Szrj 
697*38fd1498Szrj     case INTEGER_TYPE:
698*38fd1498Szrj     case ENUMERAL_TYPE:
699*38fd1498Szrj     case BOOLEAN_TYPE:
700*38fd1498Szrj     case OFFSET_TYPE:
701*38fd1498Szrj       /* If this is a logical operation, which just returns 0 or 1, we can
702*38fd1498Szrj 	 change the type of the expression.  */
703*38fd1498Szrj 
704*38fd1498Szrj       if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
705*38fd1498Szrj 	{
706*38fd1498Szrj 	  expr = copy_node (expr);
707*38fd1498Szrj 	  TREE_TYPE (expr) = type;
708*38fd1498Szrj 	  return expr;
709*38fd1498Szrj 	}
710*38fd1498Szrj 
711*38fd1498Szrj       /* If we are widening the type, put in an explicit conversion.
712*38fd1498Szrj 	 Similarly if we are not changing the width.  After this, we know
713*38fd1498Szrj 	 we are truncating EXPR.  */
714*38fd1498Szrj 
715*38fd1498Szrj       else if (outprec >= inprec)
716*38fd1498Szrj 	{
717*38fd1498Szrj 	  enum tree_code code;
718*38fd1498Szrj 
719*38fd1498Szrj 	  /* If the precision of the EXPR's type is K bits and the
720*38fd1498Szrj 	     destination mode has more bits, and the sign is changing,
721*38fd1498Szrj 	     it is not safe to use a NOP_EXPR.  For example, suppose
722*38fd1498Szrj 	     that EXPR's type is a 3-bit unsigned integer type, the
723*38fd1498Szrj 	     TYPE is a 3-bit signed integer type, and the machine mode
724*38fd1498Szrj 	     for the types is 8-bit QImode.  In that case, the
725*38fd1498Szrj 	     conversion necessitates an explicit sign-extension.  In
726*38fd1498Szrj 	     the signed-to-unsigned case the high-order bits have to
727*38fd1498Szrj 	     be cleared.  */
728*38fd1498Szrj 	  if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
729*38fd1498Szrj 	      && !type_has_mode_precision_p (TREE_TYPE (expr)))
730*38fd1498Szrj 	    code = CONVERT_EXPR;
731*38fd1498Szrj 	  else
732*38fd1498Szrj 	    code = NOP_EXPR;
733*38fd1498Szrj 
734*38fd1498Szrj 	  return maybe_fold_build1_loc (dofold, loc, code, type, expr);
735*38fd1498Szrj 	}
736*38fd1498Szrj 
737*38fd1498Szrj       /* If TYPE is an enumeral type or a type with a precision less
738*38fd1498Szrj 	 than the number of bits in its mode, do the conversion to the
739*38fd1498Szrj 	 type corresponding to its mode, then do a nop conversion
740*38fd1498Szrj 	 to TYPE.  */
741*38fd1498Szrj       else if (TREE_CODE (type) == ENUMERAL_TYPE
742*38fd1498Szrj 	       || maybe_ne (outprec, GET_MODE_PRECISION (TYPE_MODE (type))))
743*38fd1498Szrj 	{
744*38fd1498Szrj 	  expr
745*38fd1498Szrj 	    = convert_to_integer_1 (lang_hooks.types.type_for_mode
746*38fd1498Szrj 				    (TYPE_MODE (type), TYPE_UNSIGNED (type)),
747*38fd1498Szrj 				    expr, dofold);
748*38fd1498Szrj 	  return maybe_fold_build1_loc (dofold, loc, NOP_EXPR, type, expr);
749*38fd1498Szrj 	}
750*38fd1498Szrj 
751*38fd1498Szrj       /* Here detect when we can distribute the truncation down past some
752*38fd1498Szrj 	 arithmetic.  For example, if adding two longs and converting to an
753*38fd1498Szrj 	 int, we can equally well convert both to ints and then add.
754*38fd1498Szrj 	 For the operations handled here, such truncation distribution
755*38fd1498Szrj 	 is always safe.
756*38fd1498Szrj 	 It is desirable in these cases:
757*38fd1498Szrj 	 1) when truncating down to full-word from a larger size
758*38fd1498Szrj 	 2) when truncating takes no work.
759*38fd1498Szrj 	 3) when at least one operand of the arithmetic has been extended
760*38fd1498Szrj 	 (as by C's default conversions).  In this case we need two conversions
761*38fd1498Szrj 	 if we do the arithmetic as already requested, so we might as well
762*38fd1498Szrj 	 truncate both and then combine.  Perhaps that way we need only one.
763*38fd1498Szrj 
764*38fd1498Szrj 	 Note that in general we cannot do the arithmetic in a type
765*38fd1498Szrj 	 shorter than the desired result of conversion, even if the operands
766*38fd1498Szrj 	 are both extended from a shorter type, because they might overflow
767*38fd1498Szrj 	 if combined in that type.  The exceptions to this--the times when
768*38fd1498Szrj 	 two narrow values can be combined in their narrow type even to
769*38fd1498Szrj 	 make a wider result--are handled by "shorten" in build_binary_op.  */
770*38fd1498Szrj 
771*38fd1498Szrj       if (dofold)
772*38fd1498Szrj 	switch (ex_form)
773*38fd1498Szrj 	  {
774*38fd1498Szrj 	  case RSHIFT_EXPR:
775*38fd1498Szrj 	    /* We can pass truncation down through right shifting
776*38fd1498Szrj 	       when the shift count is a nonpositive constant.  */
777*38fd1498Szrj 	    if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
778*38fd1498Szrj 		&& tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
779*38fd1498Szrj 	      goto trunc1;
780*38fd1498Szrj 	    break;
781*38fd1498Szrj 
782*38fd1498Szrj 	  case LSHIFT_EXPR:
783*38fd1498Szrj 	    /* We can pass truncation down through left shifting
784*38fd1498Szrj 	       when the shift count is a nonnegative constant and
785*38fd1498Szrj 	       the target type is unsigned.  */
786*38fd1498Szrj 	    if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
787*38fd1498Szrj 		&& tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
788*38fd1498Szrj 		&& TYPE_UNSIGNED (type)
789*38fd1498Szrj 		&& TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
790*38fd1498Szrj 	      {
791*38fd1498Szrj 		/* If shift count is less than the width of the truncated type,
792*38fd1498Szrj 		   really shift.  */
793*38fd1498Szrj 		if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
794*38fd1498Szrj 		  /* In this case, shifting is like multiplication.  */
795*38fd1498Szrj 		  goto trunc1;
796*38fd1498Szrj 		else
797*38fd1498Szrj 		  {
798*38fd1498Szrj 		    /* If it is >= that width, result is zero.
799*38fd1498Szrj 		       Handling this with trunc1 would give the wrong result:
800*38fd1498Szrj 		       (int) ((long long) a << 32) is well defined (as 0)
801*38fd1498Szrj 		       but (int) a << 32 is undefined and would get a
802*38fd1498Szrj 		       warning.  */
803*38fd1498Szrj 
804*38fd1498Szrj 		    tree t = build_int_cst (type, 0);
805*38fd1498Szrj 
806*38fd1498Szrj 		    /* If the original expression had side-effects, we must
807*38fd1498Szrj 		       preserve it.  */
808*38fd1498Szrj 		    if (TREE_SIDE_EFFECTS (expr))
809*38fd1498Szrj 		      return build2 (COMPOUND_EXPR, type, expr, t);
810*38fd1498Szrj 		    else
811*38fd1498Szrj 		      return t;
812*38fd1498Szrj 		  }
813*38fd1498Szrj 	      }
814*38fd1498Szrj 	    break;
815*38fd1498Szrj 
816*38fd1498Szrj 	  case TRUNC_DIV_EXPR:
817*38fd1498Szrj 	    {
818*38fd1498Szrj 	      tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), NULL_TREE);
819*38fd1498Szrj 	      tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), NULL_TREE);
820*38fd1498Szrj 
821*38fd1498Szrj 	      /* Don't distribute unless the output precision is at least as
822*38fd1498Szrj 		 big as the actual inputs and it has the same signedness.  */
823*38fd1498Szrj 	      if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
824*38fd1498Szrj 		  && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
825*38fd1498Szrj 		  /* If signedness of arg0 and arg1 don't match,
826*38fd1498Szrj 		     we can't necessarily find a type to compare them in.  */
827*38fd1498Szrj 		  && (TYPE_UNSIGNED (TREE_TYPE (arg0))
828*38fd1498Szrj 		      == TYPE_UNSIGNED (TREE_TYPE (arg1)))
829*38fd1498Szrj 		  /* Do not change the sign of the division.  */
830*38fd1498Szrj 		  && (TYPE_UNSIGNED (TREE_TYPE (expr))
831*38fd1498Szrj 		      == TYPE_UNSIGNED (TREE_TYPE (arg0)))
832*38fd1498Szrj 		  /* Either require unsigned division or a division by
833*38fd1498Szrj 		     a constant that is not -1.  */
834*38fd1498Szrj 		  && (TYPE_UNSIGNED (TREE_TYPE (arg0))
835*38fd1498Szrj 		      || (TREE_CODE (arg1) == INTEGER_CST
836*38fd1498Szrj 			  && !integer_all_onesp (arg1))))
837*38fd1498Szrj 		{
838*38fd1498Szrj 		  tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
839*38fd1498Szrj 					expr, inprec, outprec, dofold);
840*38fd1498Szrj 		  if (tem)
841*38fd1498Szrj 		    return tem;
842*38fd1498Szrj 		}
843*38fd1498Szrj 	      break;
844*38fd1498Szrj 	    }
845*38fd1498Szrj 
846*38fd1498Szrj 	  case MAX_EXPR:
847*38fd1498Szrj 	  case MIN_EXPR:
848*38fd1498Szrj 	  case MULT_EXPR:
849*38fd1498Szrj 	    {
850*38fd1498Szrj 	      tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
851*38fd1498Szrj 	      tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
852*38fd1498Szrj 
853*38fd1498Szrj 	      /* Don't distribute unless the output precision is at least as
854*38fd1498Szrj 		 big as the actual inputs.  Otherwise, the comparison of the
855*38fd1498Szrj 		 truncated values will be wrong.  */
856*38fd1498Szrj 	      if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
857*38fd1498Szrj 		  && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
858*38fd1498Szrj 		  /* If signedness of arg0 and arg1 don't match,
859*38fd1498Szrj 		     we can't necessarily find a type to compare them in.  */
860*38fd1498Szrj 		  && (TYPE_UNSIGNED (TREE_TYPE (arg0))
861*38fd1498Szrj 		      == TYPE_UNSIGNED (TREE_TYPE (arg1))))
862*38fd1498Szrj 		goto trunc1;
863*38fd1498Szrj 	      break;
864*38fd1498Szrj 	    }
865*38fd1498Szrj 
866*38fd1498Szrj 	  case PLUS_EXPR:
867*38fd1498Szrj 	  case MINUS_EXPR:
868*38fd1498Szrj 	  case BIT_AND_EXPR:
869*38fd1498Szrj 	  case BIT_IOR_EXPR:
870*38fd1498Szrj 	  case BIT_XOR_EXPR:
871*38fd1498Szrj 	  trunc1:
872*38fd1498Szrj 	    {
873*38fd1498Szrj 	      tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
874*38fd1498Szrj 	      tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
875*38fd1498Szrj 
876*38fd1498Szrj 	      /* Do not try to narrow operands of pointer subtraction;
877*38fd1498Szrj 		 that will interfere with other folding.  */
878*38fd1498Szrj 	      if (ex_form == MINUS_EXPR
879*38fd1498Szrj 		  && CONVERT_EXPR_P (arg0)
880*38fd1498Szrj 		  && CONVERT_EXPR_P (arg1)
881*38fd1498Szrj 		  && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
882*38fd1498Szrj 		  && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
883*38fd1498Szrj 		break;
884*38fd1498Szrj 
885*38fd1498Szrj 	      if (outprec >= BITS_PER_WORD
886*38fd1498Szrj 		  || targetm.truly_noop_truncation (outprec, inprec)
887*38fd1498Szrj 		  || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
888*38fd1498Szrj 		  || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
889*38fd1498Szrj 		{
890*38fd1498Szrj 		  tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
891*38fd1498Szrj 					expr, inprec, outprec, dofold);
892*38fd1498Szrj 		  if (tem)
893*38fd1498Szrj 		    return tem;
894*38fd1498Szrj 		}
895*38fd1498Szrj 	    }
896*38fd1498Szrj 	    break;
897*38fd1498Szrj 
898*38fd1498Szrj 	  case NEGATE_EXPR:
899*38fd1498Szrj 	    /* Using unsigned arithmetic for signed types may hide overflow
900*38fd1498Szrj 	       bugs.  */
901*38fd1498Szrj 	    if (!TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (expr, 0)))
902*38fd1498Szrj 		&& sanitize_flags_p (SANITIZE_SI_OVERFLOW))
903*38fd1498Szrj 	      break;
904*38fd1498Szrj 	    /* Fall through.  */
905*38fd1498Szrj 	  case BIT_NOT_EXPR:
906*38fd1498Szrj 	    /* This is not correct for ABS_EXPR,
907*38fd1498Szrj 	       since we must test the sign before truncation.  */
908*38fd1498Szrj 	    {
909*38fd1498Szrj 	      /* Do the arithmetic in type TYPEX,
910*38fd1498Szrj 		 then convert result to TYPE.  */
911*38fd1498Szrj 	      tree typex = type;
912*38fd1498Szrj 
913*38fd1498Szrj 	      /* Can't do arithmetic in enumeral types
914*38fd1498Szrj 		 so use an integer type that will hold the values.  */
915*38fd1498Szrj 	      if (TREE_CODE (typex) == ENUMERAL_TYPE)
916*38fd1498Szrj 		typex
917*38fd1498Szrj 		  = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
918*38fd1498Szrj 						    TYPE_UNSIGNED (typex));
919*38fd1498Szrj 
920*38fd1498Szrj 	      if (!TYPE_UNSIGNED (typex))
921*38fd1498Szrj 		typex = unsigned_type_for (typex);
922*38fd1498Szrj 	      return convert (type,
923*38fd1498Szrj 			      fold_build1 (ex_form, typex,
924*38fd1498Szrj 					   convert (typex,
925*38fd1498Szrj 						    TREE_OPERAND (expr, 0))));
926*38fd1498Szrj 	    }
927*38fd1498Szrj 
928*38fd1498Szrj 	  CASE_CONVERT:
929*38fd1498Szrj 	    {
930*38fd1498Szrj 	      tree argtype = TREE_TYPE (TREE_OPERAND (expr, 0));
931*38fd1498Szrj 	      /* Don't introduce a "can't convert between vector values
932*38fd1498Szrj 		 of different size" error.  */
933*38fd1498Szrj 	      if (TREE_CODE (argtype) == VECTOR_TYPE
934*38fd1498Szrj 		  && maybe_ne (GET_MODE_SIZE (TYPE_MODE (argtype)),
935*38fd1498Szrj 			       GET_MODE_SIZE (TYPE_MODE (type))))
936*38fd1498Szrj 		break;
937*38fd1498Szrj 	    }
938*38fd1498Szrj 	    /* If truncating after truncating, might as well do all at once.
939*38fd1498Szrj 	       If truncating after extending, we may get rid of wasted work.  */
940*38fd1498Szrj 	    return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
941*38fd1498Szrj 
942*38fd1498Szrj 	  case COND_EXPR:
943*38fd1498Szrj 	    /* It is sometimes worthwhile to push the narrowing down through
944*38fd1498Szrj 	       the conditional and never loses.  A COND_EXPR may have a throw
945*38fd1498Szrj 	       as one operand, which then has void type.  Just leave void
946*38fd1498Szrj 	       operands as they are.  */
947*38fd1498Szrj 	    return
948*38fd1498Szrj 	      fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
949*38fd1498Szrj 			   VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
950*38fd1498Szrj 			   ? TREE_OPERAND (expr, 1)
951*38fd1498Szrj 			   : convert (type, TREE_OPERAND (expr, 1)),
952*38fd1498Szrj 			   VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
953*38fd1498Szrj 			   ? TREE_OPERAND (expr, 2)
954*38fd1498Szrj 			   : convert (type, TREE_OPERAND (expr, 2)));
955*38fd1498Szrj 
956*38fd1498Szrj 	  default:
957*38fd1498Szrj 	    break;
958*38fd1498Szrj 	  }
959*38fd1498Szrj 
960*38fd1498Szrj       /* When parsing long initializers, we might end up with a lot of casts.
961*38fd1498Szrj 	 Shortcut this.  */
962*38fd1498Szrj       if (TREE_CODE (expr) == INTEGER_CST)
963*38fd1498Szrj 	return fold_convert (type, expr);
964*38fd1498Szrj       return build1 (CONVERT_EXPR, type, expr);
965*38fd1498Szrj 
966*38fd1498Szrj     case REAL_TYPE:
967*38fd1498Szrj       if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
968*38fd1498Szrj 	  && current_function_decl != NULL_TREE)
969*38fd1498Szrj 	{
970*38fd1498Szrj 	  expr = save_expr (expr);
971*38fd1498Szrj 	  tree check = ubsan_instrument_float_cast (loc, type, expr);
972*38fd1498Szrj 	  expr = build1 (FIX_TRUNC_EXPR, type, expr);
973*38fd1498Szrj 	  if (check == NULL_TREE)
974*38fd1498Szrj 	    return expr;
975*38fd1498Szrj 	  return maybe_fold_build2_loc (dofold, loc, COMPOUND_EXPR,
976*38fd1498Szrj 					TREE_TYPE (expr), check, expr);
977*38fd1498Szrj 	}
978*38fd1498Szrj       else
979*38fd1498Szrj 	return build1 (FIX_TRUNC_EXPR, type, expr);
980*38fd1498Szrj 
981*38fd1498Szrj     case FIXED_POINT_TYPE:
982*38fd1498Szrj       return build1 (FIXED_CONVERT_EXPR, type, expr);
983*38fd1498Szrj 
984*38fd1498Szrj     case COMPLEX_TYPE:
985*38fd1498Szrj       expr = maybe_fold_build1_loc (dofold, loc, REALPART_EXPR,
986*38fd1498Szrj 				    TREE_TYPE (TREE_TYPE (expr)), expr);
987*38fd1498Szrj       return convert (type, expr);
988*38fd1498Szrj 
989*38fd1498Szrj     case VECTOR_TYPE:
990*38fd1498Szrj       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
991*38fd1498Szrj 	{
992*38fd1498Szrj 	  error ("can%'t convert a vector of type %qT"
993*38fd1498Szrj 		 " to type %qT which has different size",
994*38fd1498Szrj 		 TREE_TYPE (expr), type);
995*38fd1498Szrj 	  return error_mark_node;
996*38fd1498Szrj 	}
997*38fd1498Szrj       return build1 (VIEW_CONVERT_EXPR, type, expr);
998*38fd1498Szrj 
999*38fd1498Szrj     default:
1000*38fd1498Szrj       error ("aggregate value used where an integer was expected");
1001*38fd1498Szrj       return convert (type, integer_zero_node);
1002*38fd1498Szrj     }
1003*38fd1498Szrj }
1004*38fd1498Szrj 
1005*38fd1498Szrj /* Convert EXPR to some integer (or enum) type TYPE.
1006*38fd1498Szrj 
1007*38fd1498Szrj    EXPR must be pointer, integer, discrete (enum, char, or bool), float,
1008*38fd1498Szrj    fixed-point or vector; in other cases error is called.
1009*38fd1498Szrj 
1010*38fd1498Szrj    The result of this is always supposed to be a newly created tree node
1011*38fd1498Szrj    not in use in any existing structure.  */
1012*38fd1498Szrj 
1013*38fd1498Szrj tree
convert_to_integer(tree type,tree expr)1014*38fd1498Szrj convert_to_integer (tree type, tree expr)
1015*38fd1498Szrj {
1016*38fd1498Szrj   return convert_to_integer_1 (type, expr, true);
1017*38fd1498Szrj }
1018*38fd1498Szrj 
1019*38fd1498Szrj /* A wrapper around convert_to_complex_1 that only folds the
1020*38fd1498Szrj    expression if DOFOLD, or if it is CONSTANT_CLASS_P.  */
1021*38fd1498Szrj 
1022*38fd1498Szrj tree
convert_to_integer_maybe_fold(tree type,tree expr,bool dofold)1023*38fd1498Szrj convert_to_integer_maybe_fold (tree type, tree expr, bool dofold)
1024*38fd1498Szrj {
1025*38fd1498Szrj   return convert_to_integer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
1026*38fd1498Szrj }
1027*38fd1498Szrj 
1028*38fd1498Szrj /* Convert EXPR to the complex type TYPE in the usual ways.  If FOLD_P is
1029*38fd1498Szrj    true, try to fold the expression.  */
1030*38fd1498Szrj 
1031*38fd1498Szrj static tree
convert_to_complex_1(tree type,tree expr,bool fold_p)1032*38fd1498Szrj convert_to_complex_1 (tree type, tree expr, bool fold_p)
1033*38fd1498Szrj {
1034*38fd1498Szrj   location_t loc = EXPR_LOCATION (expr);
1035*38fd1498Szrj   tree subtype = TREE_TYPE (type);
1036*38fd1498Szrj 
1037*38fd1498Szrj   switch (TREE_CODE (TREE_TYPE (expr)))
1038*38fd1498Szrj     {
1039*38fd1498Szrj     case REAL_TYPE:
1040*38fd1498Szrj     case FIXED_POINT_TYPE:
1041*38fd1498Szrj     case INTEGER_TYPE:
1042*38fd1498Szrj     case ENUMERAL_TYPE:
1043*38fd1498Szrj     case BOOLEAN_TYPE:
1044*38fd1498Szrj       return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
1045*38fd1498Szrj 		     convert (subtype, integer_zero_node));
1046*38fd1498Szrj 
1047*38fd1498Szrj     case COMPLEX_TYPE:
1048*38fd1498Szrj       {
1049*38fd1498Szrj 	tree elt_type = TREE_TYPE (TREE_TYPE (expr));
1050*38fd1498Szrj 
1051*38fd1498Szrj 	if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
1052*38fd1498Szrj 	  return expr;
1053*38fd1498Szrj 	else if (TREE_CODE (expr) == COMPOUND_EXPR)
1054*38fd1498Szrj 	  {
1055*38fd1498Szrj 	    tree t = convert_to_complex_1 (type, TREE_OPERAND (expr, 1),
1056*38fd1498Szrj 					   fold_p);
1057*38fd1498Szrj 	    if (t == TREE_OPERAND (expr, 1))
1058*38fd1498Szrj 	      return expr;
1059*38fd1498Szrj 	    return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
1060*38fd1498Szrj 			       TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
1061*38fd1498Szrj 	  }
1062*38fd1498Szrj 	else if (TREE_CODE (expr) == COMPLEX_EXPR)
1063*38fd1498Szrj 	  return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1064*38fd1498Szrj 					convert (subtype,
1065*38fd1498Szrj 						 TREE_OPERAND (expr, 0)),
1066*38fd1498Szrj 					convert (subtype,
1067*38fd1498Szrj 						 TREE_OPERAND (expr, 1)));
1068*38fd1498Szrj 	else
1069*38fd1498Szrj 	  {
1070*38fd1498Szrj 	    expr = save_expr (expr);
1071*38fd1498Szrj 	    tree realp = maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
1072*38fd1498Szrj 						TREE_TYPE (TREE_TYPE (expr)),
1073*38fd1498Szrj 						expr);
1074*38fd1498Szrj 	    tree imagp = maybe_fold_build1_loc (fold_p, loc, IMAGPART_EXPR,
1075*38fd1498Szrj 						TREE_TYPE (TREE_TYPE (expr)),
1076*38fd1498Szrj 						expr);
1077*38fd1498Szrj 	    return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1078*38fd1498Szrj 					  convert (subtype, realp),
1079*38fd1498Szrj 					  convert (subtype, imagp));
1080*38fd1498Szrj 	  }
1081*38fd1498Szrj       }
1082*38fd1498Szrj 
1083*38fd1498Szrj     case POINTER_TYPE:
1084*38fd1498Szrj     case REFERENCE_TYPE:
1085*38fd1498Szrj       error ("pointer value used where a complex was expected");
1086*38fd1498Szrj       return convert_to_complex_1 (type, integer_zero_node, fold_p);
1087*38fd1498Szrj 
1088*38fd1498Szrj     default:
1089*38fd1498Szrj       error ("aggregate value used where a complex was expected");
1090*38fd1498Szrj       return convert_to_complex_1 (type, integer_zero_node, fold_p);
1091*38fd1498Szrj     }
1092*38fd1498Szrj }
1093*38fd1498Szrj 
1094*38fd1498Szrj /* A wrapper around convert_to_complex_1 that always folds the
1095*38fd1498Szrj    expression.  */
1096*38fd1498Szrj 
1097*38fd1498Szrj tree
convert_to_complex(tree type,tree expr)1098*38fd1498Szrj convert_to_complex (tree type, tree expr)
1099*38fd1498Szrj {
1100*38fd1498Szrj   return convert_to_complex_1 (type, expr, true);
1101*38fd1498Szrj }
1102*38fd1498Szrj 
1103*38fd1498Szrj /* A wrapper around convert_to_complex_1 that only folds the
1104*38fd1498Szrj    expression if DOFOLD, or if it is CONSTANT_CLASS_P.  */
1105*38fd1498Szrj 
1106*38fd1498Szrj tree
convert_to_complex_maybe_fold(tree type,tree expr,bool dofold)1107*38fd1498Szrj convert_to_complex_maybe_fold (tree type, tree expr, bool dofold)
1108*38fd1498Szrj {
1109*38fd1498Szrj   return convert_to_complex_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
1110*38fd1498Szrj }
1111*38fd1498Szrj 
1112*38fd1498Szrj /* Convert EXPR to the vector type TYPE in the usual ways.  */
1113*38fd1498Szrj 
1114*38fd1498Szrj tree
convert_to_vector(tree type,tree expr)1115*38fd1498Szrj convert_to_vector (tree type, tree expr)
1116*38fd1498Szrj {
1117*38fd1498Szrj   switch (TREE_CODE (TREE_TYPE (expr)))
1118*38fd1498Szrj     {
1119*38fd1498Szrj     case INTEGER_TYPE:
1120*38fd1498Szrj     case VECTOR_TYPE:
1121*38fd1498Szrj       if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
1122*38fd1498Szrj 	{
1123*38fd1498Szrj 	  error ("can%'t convert a value of type %qT"
1124*38fd1498Szrj 		 " to vector type %qT which has different size",
1125*38fd1498Szrj 		 TREE_TYPE (expr), type);
1126*38fd1498Szrj 	  return error_mark_node;
1127*38fd1498Szrj 	}
1128*38fd1498Szrj       return build1 (VIEW_CONVERT_EXPR, type, expr);
1129*38fd1498Szrj 
1130*38fd1498Szrj     default:
1131*38fd1498Szrj       error ("can%'t convert value to a vector");
1132*38fd1498Szrj       return error_mark_node;
1133*38fd1498Szrj     }
1134*38fd1498Szrj }
1135*38fd1498Szrj 
1136*38fd1498Szrj /* Convert EXPR to some fixed-point type TYPE.
1137*38fd1498Szrj 
1138*38fd1498Szrj    EXPR must be fixed-point, float, integer, or enumeral;
1139*38fd1498Szrj    in other cases error is called.  */
1140*38fd1498Szrj 
1141*38fd1498Szrj tree
convert_to_fixed(tree type,tree expr)1142*38fd1498Szrj convert_to_fixed (tree type, tree expr)
1143*38fd1498Szrj {
1144*38fd1498Szrj   if (integer_zerop (expr))
1145*38fd1498Szrj     {
1146*38fd1498Szrj       tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1147*38fd1498Szrj       return fixed_zero_node;
1148*38fd1498Szrj     }
1149*38fd1498Szrj   else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1150*38fd1498Szrj     {
1151*38fd1498Szrj       tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1152*38fd1498Szrj       return fixed_one_node;
1153*38fd1498Szrj     }
1154*38fd1498Szrj 
1155*38fd1498Szrj   switch (TREE_CODE (TREE_TYPE (expr)))
1156*38fd1498Szrj     {
1157*38fd1498Szrj     case FIXED_POINT_TYPE:
1158*38fd1498Szrj     case INTEGER_TYPE:
1159*38fd1498Szrj     case ENUMERAL_TYPE:
1160*38fd1498Szrj     case BOOLEAN_TYPE:
1161*38fd1498Szrj     case REAL_TYPE:
1162*38fd1498Szrj       return build1 (FIXED_CONVERT_EXPR, type, expr);
1163*38fd1498Szrj 
1164*38fd1498Szrj     case COMPLEX_TYPE:
1165*38fd1498Szrj       return convert (type,
1166*38fd1498Szrj 		      fold_build1 (REALPART_EXPR,
1167*38fd1498Szrj 				   TREE_TYPE (TREE_TYPE (expr)), expr));
1168*38fd1498Szrj 
1169*38fd1498Szrj     default:
1170*38fd1498Szrj       error ("aggregate value used where a fixed-point was expected");
1171*38fd1498Szrj       return error_mark_node;
1172*38fd1498Szrj     }
1173*38fd1498Szrj }
1174