gcc/
[official-gcc.git] / gcc / convert.c
blobc3cb0ae29aaa8e64a7f1c42d4f837fe90d1a8ec6
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* These routines are somewhat language-independent utility function
22 intended to be called by the language-specific convert () functions. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "flags.h"
34 #include "convert.h"
35 #include "diagnostic-core.h"
36 #include "target.h"
37 #include "langhooks.h"
38 #include "builtins.h"
39 #include "ubsan.h"
41 /* Convert EXPR to some pointer or reference type TYPE.
42 EXPR must be pointer, reference, integer, enumeral, or literal zero;
43 in other cases error is called. */
45 tree
46 convert_to_pointer (tree type, tree expr)
48 location_t loc = EXPR_LOCATION (expr);
49 if (TREE_TYPE (expr) == type)
50 return expr;
52 switch (TREE_CODE (TREE_TYPE (expr)))
54 case POINTER_TYPE:
55 case REFERENCE_TYPE:
57 /* If the pointers point to different address spaces, conversion needs
58 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
59 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
60 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
62 if (to_as == from_as)
63 return fold_build1_loc (loc, NOP_EXPR, type, expr);
64 else
65 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
68 case INTEGER_TYPE:
69 case ENUMERAL_TYPE:
70 case BOOLEAN_TYPE:
72 /* If the input precision differs from the target pointer type
73 precision, first convert the input expression to an integer type of
74 the target precision. Some targets, e.g. VMS, need several pointer
75 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
76 unsigned int pprec = TYPE_PRECISION (type);
77 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
79 if (eprec != pprec)
80 expr = fold_build1_loc (loc, NOP_EXPR,
81 lang_hooks.types.type_for_size (pprec, 0),
82 expr);
85 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
87 default:
88 error ("cannot convert to a pointer type");
89 return convert_to_pointer (type, integer_zero_node);
94 /* Convert EXPR to some floating-point type TYPE.
96 EXPR must be float, fixed-point, integer, or enumeral;
97 in other cases error is called. */
99 tree
100 convert_to_real (tree type, tree expr)
102 enum built_in_function fcode = builtin_mathfn_code (expr);
103 tree itype = TREE_TYPE (expr);
105 if (TREE_CODE (expr) == COMPOUND_EXPR)
107 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
108 if (t == TREE_OPERAND (expr, 1))
109 return expr;
110 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
111 TREE_OPERAND (expr, 0), t);
114 /* Disable until we figure out how to decide whether the functions are
115 present in runtime. */
116 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
117 if (optimize
118 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
119 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
121 switch (fcode)
123 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
124 CASE_MATHFN (COSH)
125 CASE_MATHFN (EXP)
126 CASE_MATHFN (EXP10)
127 CASE_MATHFN (EXP2)
128 CASE_MATHFN (EXPM1)
129 CASE_MATHFN (GAMMA)
130 CASE_MATHFN (J0)
131 CASE_MATHFN (J1)
132 CASE_MATHFN (LGAMMA)
133 CASE_MATHFN (POW10)
134 CASE_MATHFN (SINH)
135 CASE_MATHFN (TGAMMA)
136 CASE_MATHFN (Y0)
137 CASE_MATHFN (Y1)
138 /* The above functions may set errno differently with float
139 input or output so this transformation is not safe with
140 -fmath-errno. */
141 if (flag_errno_math)
142 break;
143 CASE_MATHFN (ACOS)
144 CASE_MATHFN (ACOSH)
145 CASE_MATHFN (ASIN)
146 CASE_MATHFN (ASINH)
147 CASE_MATHFN (ATAN)
148 CASE_MATHFN (ATANH)
149 CASE_MATHFN (CBRT)
150 CASE_MATHFN (COS)
151 CASE_MATHFN (ERF)
152 CASE_MATHFN (ERFC)
153 CASE_MATHFN (LOG)
154 CASE_MATHFN (LOG10)
155 CASE_MATHFN (LOG2)
156 CASE_MATHFN (LOG1P)
157 CASE_MATHFN (SIN)
158 CASE_MATHFN (TAN)
159 CASE_MATHFN (TANH)
160 /* The above functions are not safe to do this conversion. */
161 if (!flag_unsafe_math_optimizations)
162 break;
163 CASE_MATHFN (SQRT)
164 CASE_MATHFN (FABS)
165 CASE_MATHFN (LOGB)
166 #undef CASE_MATHFN
168 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
169 tree newtype = type;
171 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
172 the both as the safe type for operation. */
173 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
174 newtype = TREE_TYPE (arg0);
176 /* We consider to convert
178 (T1) sqrtT2 ((T2) exprT3)
180 (T1) sqrtT4 ((T4) exprT3)
182 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
183 and T4 is NEWTYPE. All those types are of floating point types.
184 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
185 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
186 T2 and T4. See the following URL for a reference:
187 http://stackoverflow.com/questions/9235456/determining-
188 floating-point-square-root
190 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
191 && !flag_unsafe_math_optimizations)
193 /* The following conversion is unsafe even the precision condition
194 below is satisfied:
196 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
198 if (TYPE_MODE (type) != TYPE_MODE (newtype))
199 break;
201 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
202 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
203 if (p1 < p2 * 2 + 2)
204 break;
207 /* Be careful about integer to fp conversions.
208 These may overflow still. */
209 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
210 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
211 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
212 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
214 tree fn = mathfn_built_in (newtype, fcode);
216 if (fn)
218 tree arg = fold (convert_to_real (newtype, arg0));
219 expr = build_call_expr (fn, 1, arg);
220 if (newtype == type)
221 return expr;
225 default:
226 break;
229 if (optimize
230 && (((fcode == BUILT_IN_FLOORL
231 || fcode == BUILT_IN_CEILL
232 || fcode == BUILT_IN_ROUNDL
233 || fcode == BUILT_IN_RINTL
234 || fcode == BUILT_IN_TRUNCL
235 || fcode == BUILT_IN_NEARBYINTL)
236 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
237 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
238 || ((fcode == BUILT_IN_FLOOR
239 || fcode == BUILT_IN_CEIL
240 || fcode == BUILT_IN_ROUND
241 || fcode == BUILT_IN_RINT
242 || fcode == BUILT_IN_TRUNC
243 || fcode == BUILT_IN_NEARBYINT)
244 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
246 tree fn = mathfn_built_in (type, fcode);
248 if (fn)
250 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
252 /* Make sure (type)arg0 is an extension, otherwise we could end up
253 changing (float)floor(double d) into floorf((float)d), which is
254 incorrect because (float)d uses round-to-nearest and can round
255 up to the next integer. */
256 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
257 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
261 /* Propagate the cast into the operation. */
262 if (itype != type && FLOAT_TYPE_P (type))
263 switch (TREE_CODE (expr))
265 /* Convert (float)-x into -(float)x. This is safe for
266 round-to-nearest rounding mode when the inner type is float. */
267 case ABS_EXPR:
268 case NEGATE_EXPR:
269 if (!flag_rounding_math
270 && FLOAT_TYPE_P (itype)
271 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
272 return build1 (TREE_CODE (expr), type,
273 fold (convert_to_real (type,
274 TREE_OPERAND (expr, 0))));
275 break;
276 /* Convert (outertype)((innertype0)a+(innertype1)b)
277 into ((newtype)a+(newtype)b) where newtype
278 is the widest mode from all of these. */
279 case PLUS_EXPR:
280 case MINUS_EXPR:
281 case MULT_EXPR:
282 case RDIV_EXPR:
284 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
285 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
287 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
288 && FLOAT_TYPE_P (TREE_TYPE (arg1))
289 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
291 tree newtype = type;
293 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
294 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
295 || TYPE_MODE (type) == SDmode)
296 newtype = dfloat32_type_node;
297 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
298 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
299 || TYPE_MODE (type) == DDmode)
300 newtype = dfloat64_type_node;
301 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
302 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
303 || TYPE_MODE (type) == TDmode)
304 newtype = dfloat128_type_node;
305 if (newtype == dfloat32_type_node
306 || newtype == dfloat64_type_node
307 || newtype == dfloat128_type_node)
309 expr = build2 (TREE_CODE (expr), newtype,
310 fold (convert_to_real (newtype, arg0)),
311 fold (convert_to_real (newtype, arg1)));
312 if (newtype == type)
313 return expr;
314 break;
317 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
318 newtype = TREE_TYPE (arg0);
319 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
320 newtype = TREE_TYPE (arg1);
321 /* Sometimes this transformation is safe (cannot
322 change results through affecting double rounding
323 cases) and sometimes it is not. If NEWTYPE is
324 wider than TYPE, e.g. (float)((long double)double
325 + (long double)double) converted to
326 (float)(double + double), the transformation is
327 unsafe regardless of the details of the types
328 involved; double rounding can arise if the result
329 of NEWTYPE arithmetic is a NEWTYPE value half way
330 between two representable TYPE values but the
331 exact value is sufficiently different (in the
332 right direction) for this difference to be
333 visible in ITYPE arithmetic. If NEWTYPE is the
334 same as TYPE, however, the transformation may be
335 safe depending on the types involved: it is safe
336 if the ITYPE has strictly more than twice as many
337 mantissa bits as TYPE, can represent infinities
338 and NaNs if the TYPE can, and has sufficient
339 exponent range for the product or ratio of two
340 values representable in the TYPE to be within the
341 range of normal values of ITYPE. */
342 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
343 && (flag_unsafe_math_optimizations
344 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
345 && real_can_shorten_arithmetic (TYPE_MODE (itype),
346 TYPE_MODE (type))
347 && !excess_precision_type (newtype))))
349 expr = build2 (TREE_CODE (expr), newtype,
350 fold (convert_to_real (newtype, arg0)),
351 fold (convert_to_real (newtype, arg1)));
352 if (newtype == type)
353 return expr;
357 break;
358 default:
359 break;
362 switch (TREE_CODE (TREE_TYPE (expr)))
364 case REAL_TYPE:
365 /* Ignore the conversion if we don't need to store intermediate
366 results and neither type is a decimal float. */
367 return build1 ((flag_float_store
368 || DECIMAL_FLOAT_TYPE_P (type)
369 || DECIMAL_FLOAT_TYPE_P (itype))
370 ? CONVERT_EXPR : NOP_EXPR, type, expr);
372 case INTEGER_TYPE:
373 case ENUMERAL_TYPE:
374 case BOOLEAN_TYPE:
375 return build1 (FLOAT_EXPR, type, expr);
377 case FIXED_POINT_TYPE:
378 return build1 (FIXED_CONVERT_EXPR, type, expr);
380 case COMPLEX_TYPE:
381 return convert (type,
382 fold_build1 (REALPART_EXPR,
383 TREE_TYPE (TREE_TYPE (expr)), expr));
385 case POINTER_TYPE:
386 case REFERENCE_TYPE:
387 error ("pointer value used where a floating point value was expected");
388 return convert_to_real (type, integer_zero_node);
390 default:
391 error ("aggregate value used where a float was expected");
392 return convert_to_real (type, integer_zero_node);
396 /* Convert EXPR to some integer (or enum) type TYPE.
398 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
399 fixed-point or vector; in other cases error is called.
401 The result of this is always supposed to be a newly created tree node
402 not in use in any existing structure. */
404 tree
405 convert_to_integer (tree type, tree expr)
407 enum tree_code ex_form = TREE_CODE (expr);
408 tree intype = TREE_TYPE (expr);
409 unsigned int inprec = element_precision (intype);
410 unsigned int outprec = element_precision (type);
411 location_t loc = EXPR_LOCATION (expr);
413 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
414 be. Consider `enum E = { a, b = (enum E) 3 };'. */
415 if (!COMPLETE_TYPE_P (type))
417 error ("conversion to incomplete type");
418 return error_mark_node;
421 if (ex_form == COMPOUND_EXPR)
423 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
424 if (t == TREE_OPERAND (expr, 1))
425 return expr;
426 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
427 TREE_OPERAND (expr, 0), t);
430 /* Convert e.g. (long)round(d) -> lround(d). */
431 /* If we're converting to char, we may encounter differing behavior
432 between converting from double->char vs double->long->char.
433 We're in "undefined" territory but we prefer to be conservative,
434 so only proceed in "unsafe" math mode. */
435 if (optimize
436 && (flag_unsafe_math_optimizations
437 || (long_integer_type_node
438 && outprec >= TYPE_PRECISION (long_integer_type_node))))
440 tree s_expr = strip_float_extensions (expr);
441 tree s_intype = TREE_TYPE (s_expr);
442 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
443 tree fn = 0;
445 switch (fcode)
447 CASE_FLT_FN (BUILT_IN_CEIL):
448 /* Only convert in ISO C99 mode. */
449 if (!targetm.libc_has_function (function_c99_misc))
450 break;
451 if (outprec < TYPE_PRECISION (integer_type_node)
452 || (outprec == TYPE_PRECISION (integer_type_node)
453 && !TYPE_UNSIGNED (type)))
454 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
455 else if (outprec == TYPE_PRECISION (long_integer_type_node)
456 && !TYPE_UNSIGNED (type))
457 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
458 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
459 && !TYPE_UNSIGNED (type))
460 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
461 break;
463 CASE_FLT_FN (BUILT_IN_FLOOR):
464 /* Only convert in ISO C99 mode. */
465 if (!targetm.libc_has_function (function_c99_misc))
466 break;
467 if (outprec < TYPE_PRECISION (integer_type_node)
468 || (outprec == TYPE_PRECISION (integer_type_node)
469 && !TYPE_UNSIGNED (type)))
470 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
471 else if (outprec == TYPE_PRECISION (long_integer_type_node)
472 && !TYPE_UNSIGNED (type))
473 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
474 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
475 && !TYPE_UNSIGNED (type))
476 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
477 break;
479 CASE_FLT_FN (BUILT_IN_ROUND):
480 /* Only convert in ISO C99 mode and with -fno-math-errno. */
481 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
482 break;
483 if (outprec < TYPE_PRECISION (integer_type_node)
484 || (outprec == TYPE_PRECISION (integer_type_node)
485 && !TYPE_UNSIGNED (type)))
486 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
487 else if (outprec == TYPE_PRECISION (long_integer_type_node)
488 && !TYPE_UNSIGNED (type))
489 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
490 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
491 && !TYPE_UNSIGNED (type))
492 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
493 break;
495 CASE_FLT_FN (BUILT_IN_NEARBYINT):
496 /* Only convert nearbyint* if we can ignore math exceptions. */
497 if (flag_trapping_math)
498 break;
499 /* ... Fall through ... */
500 CASE_FLT_FN (BUILT_IN_RINT):
501 /* Only convert in ISO C99 mode and with -fno-math-errno. */
502 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
503 break;
504 if (outprec < TYPE_PRECISION (integer_type_node)
505 || (outprec == TYPE_PRECISION (integer_type_node)
506 && !TYPE_UNSIGNED (type)))
507 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
508 else if (outprec == TYPE_PRECISION (long_integer_type_node)
509 && !TYPE_UNSIGNED (type))
510 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
511 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
512 && !TYPE_UNSIGNED (type))
513 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
514 break;
516 CASE_FLT_FN (BUILT_IN_TRUNC):
517 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
519 default:
520 break;
523 if (fn)
525 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
526 return convert_to_integer (type, newexpr);
530 /* Convert (int)logb(d) -> ilogb(d). */
531 if (optimize
532 && flag_unsafe_math_optimizations
533 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
534 && integer_type_node
535 && (outprec > TYPE_PRECISION (integer_type_node)
536 || (outprec == TYPE_PRECISION (integer_type_node)
537 && !TYPE_UNSIGNED (type))))
539 tree s_expr = strip_float_extensions (expr);
540 tree s_intype = TREE_TYPE (s_expr);
541 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
542 tree fn = 0;
544 switch (fcode)
546 CASE_FLT_FN (BUILT_IN_LOGB):
547 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
548 break;
550 default:
551 break;
554 if (fn)
556 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
557 return convert_to_integer (type, newexpr);
561 switch (TREE_CODE (intype))
563 case POINTER_TYPE:
564 case REFERENCE_TYPE:
565 if (integer_zerop (expr))
566 return build_int_cst (type, 0);
568 /* Convert to an unsigned integer of the correct width first, and from
569 there widen/truncate to the required type. Some targets support the
570 coexistence of multiple valid pointer sizes, so fetch the one we need
571 from the type. */
572 expr = fold_build1 (CONVERT_EXPR,
573 lang_hooks.types.type_for_size
574 (TYPE_PRECISION (intype), 0),
575 expr);
576 return fold_convert (type, expr);
578 case INTEGER_TYPE:
579 case ENUMERAL_TYPE:
580 case BOOLEAN_TYPE:
581 case OFFSET_TYPE:
582 /* If this is a logical operation, which just returns 0 or 1, we can
583 change the type of the expression. */
585 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
587 expr = copy_node (expr);
588 TREE_TYPE (expr) = type;
589 return expr;
592 /* If we are widening the type, put in an explicit conversion.
593 Similarly if we are not changing the width. After this, we know
594 we are truncating EXPR. */
596 else if (outprec >= inprec)
598 enum tree_code code;
600 /* If the precision of the EXPR's type is K bits and the
601 destination mode has more bits, and the sign is changing,
602 it is not safe to use a NOP_EXPR. For example, suppose
603 that EXPR's type is a 3-bit unsigned integer type, the
604 TYPE is a 3-bit signed integer type, and the machine mode
605 for the types is 8-bit QImode. In that case, the
606 conversion necessitates an explicit sign-extension. In
607 the signed-to-unsigned case the high-order bits have to
608 be cleared. */
609 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
610 && (TYPE_PRECISION (TREE_TYPE (expr))
611 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
612 code = CONVERT_EXPR;
613 else
614 code = NOP_EXPR;
616 return fold_build1 (code, type, expr);
619 /* If TYPE is an enumeral type or a type with a precision less
620 than the number of bits in its mode, do the conversion to the
621 type corresponding to its mode, then do a nop conversion
622 to TYPE. */
623 else if (TREE_CODE (type) == ENUMERAL_TYPE
624 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
625 return build1 (NOP_EXPR, type,
626 convert (lang_hooks.types.type_for_mode
627 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
628 expr));
630 /* Here detect when we can distribute the truncation down past some
631 arithmetic. For example, if adding two longs and converting to an
632 int, we can equally well convert both to ints and then add.
633 For the operations handled here, such truncation distribution
634 is always safe.
635 It is desirable in these cases:
636 1) when truncating down to full-word from a larger size
637 2) when truncating takes no work.
638 3) when at least one operand of the arithmetic has been extended
639 (as by C's default conversions). In this case we need two conversions
640 if we do the arithmetic as already requested, so we might as well
641 truncate both and then combine. Perhaps that way we need only one.
643 Note that in general we cannot do the arithmetic in a type
644 shorter than the desired result of conversion, even if the operands
645 are both extended from a shorter type, because they might overflow
646 if combined in that type. The exceptions to this--the times when
647 two narrow values can be combined in their narrow type even to
648 make a wider result--are handled by "shorten" in build_binary_op. */
650 switch (ex_form)
652 case RSHIFT_EXPR:
653 /* We can pass truncation down through right shifting
654 when the shift count is a nonpositive constant. */
655 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
656 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
657 goto trunc1;
658 break;
660 case LSHIFT_EXPR:
661 /* We can pass truncation down through left shifting
662 when the shift count is a nonnegative constant and
663 the target type is unsigned. */
664 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
665 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
666 && TYPE_UNSIGNED (type)
667 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
669 /* If shift count is less than the width of the truncated type,
670 really shift. */
671 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
672 /* In this case, shifting is like multiplication. */
673 goto trunc1;
674 else
676 /* If it is >= that width, result is zero.
677 Handling this with trunc1 would give the wrong result:
678 (int) ((long long) a << 32) is well defined (as 0)
679 but (int) a << 32 is undefined and would get a
680 warning. */
682 tree t = build_int_cst (type, 0);
684 /* If the original expression had side-effects, we must
685 preserve it. */
686 if (TREE_SIDE_EFFECTS (expr))
687 return build2 (COMPOUND_EXPR, type, expr, t);
688 else
689 return t;
692 break;
694 case TRUNC_DIV_EXPR:
696 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
697 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
699 /* Don't distribute unless the output precision is at least as big
700 as the actual inputs and it has the same signedness. */
701 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
702 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
703 /* If signedness of arg0 and arg1 don't match,
704 we can't necessarily find a type to compare them in. */
705 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
706 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
707 /* Do not change the sign of the division. */
708 && (TYPE_UNSIGNED (TREE_TYPE (expr))
709 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
710 /* Either require unsigned division or a division by
711 a constant that is not -1. */
712 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
713 || (TREE_CODE (arg1) == INTEGER_CST
714 && !integer_all_onesp (arg1))))
715 goto trunc1;
716 break;
719 case MAX_EXPR:
720 case MIN_EXPR:
721 case MULT_EXPR:
723 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
724 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
726 /* Don't distribute unless the output precision is at least as big
727 as the actual inputs. Otherwise, the comparison of the
728 truncated values will be wrong. */
729 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
730 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
731 /* If signedness of arg0 and arg1 don't match,
732 we can't necessarily find a type to compare them in. */
733 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
734 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
735 goto trunc1;
736 break;
739 case PLUS_EXPR:
740 case MINUS_EXPR:
741 case BIT_AND_EXPR:
742 case BIT_IOR_EXPR:
743 case BIT_XOR_EXPR:
744 trunc1:
746 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
747 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
749 /* Do not try to narrow operands of pointer subtraction;
750 that will interfere with other folding. */
751 if (ex_form == MINUS_EXPR
752 && CONVERT_EXPR_P (arg0)
753 && CONVERT_EXPR_P (arg1)
754 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
755 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
756 break;
758 if (outprec >= BITS_PER_WORD
759 || TRULY_NOOP_TRUNCATION (outprec, inprec)
760 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
761 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
763 /* Do the arithmetic in type TYPEX,
764 then convert result to TYPE. */
765 tree typex = type;
767 /* Can't do arithmetic in enumeral types
768 so use an integer type that will hold the values. */
769 if (TREE_CODE (typex) == ENUMERAL_TYPE)
770 typex
771 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
772 TYPE_UNSIGNED (typex));
774 /* But now perhaps TYPEX is as wide as INPREC.
775 In that case, do nothing special here.
776 (Otherwise would recurse infinitely in convert. */
777 if (TYPE_PRECISION (typex) != inprec)
779 /* Don't do unsigned arithmetic where signed was wanted,
780 or vice versa.
781 Exception: if both of the original operands were
782 unsigned then we can safely do the work as unsigned.
783 Exception: shift operations take their type solely
784 from the first argument.
785 Exception: the LSHIFT_EXPR case above requires that
786 we perform this operation unsigned lest we produce
787 signed-overflow undefinedness.
788 And we may need to do it as unsigned
789 if we truncate to the original size. */
790 if (TYPE_UNSIGNED (TREE_TYPE (expr))
791 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
792 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
793 || ex_form == LSHIFT_EXPR
794 || ex_form == RSHIFT_EXPR
795 || ex_form == LROTATE_EXPR
796 || ex_form == RROTATE_EXPR))
797 || ex_form == LSHIFT_EXPR
798 /* If we have !flag_wrapv, and either ARG0 or
799 ARG1 is of a signed type, we have to do
800 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
801 type in case the operation in outprec precision
802 could overflow. Otherwise, we would introduce
803 signed-overflow undefinedness. */
804 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
805 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
806 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
807 > outprec)
808 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
809 > outprec))
810 && (ex_form == PLUS_EXPR
811 || ex_form == MINUS_EXPR
812 || ex_form == MULT_EXPR)))
814 if (!TYPE_UNSIGNED (typex))
815 typex = unsigned_type_for (typex);
817 else
819 if (TYPE_UNSIGNED (typex))
820 typex = signed_type_for (typex);
822 return convert (type,
823 fold_build2 (ex_form, typex,
824 convert (typex, arg0),
825 convert (typex, arg1)));
829 break;
831 case NEGATE_EXPR:
832 case BIT_NOT_EXPR:
833 /* This is not correct for ABS_EXPR,
834 since we must test the sign before truncation. */
836 /* Do the arithmetic in type TYPEX,
837 then convert result to TYPE. */
838 tree typex = type;
840 /* Can't do arithmetic in enumeral types
841 so use an integer type that will hold the values. */
842 if (TREE_CODE (typex) == ENUMERAL_TYPE)
843 typex
844 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
845 TYPE_UNSIGNED (typex));
847 if (!TYPE_UNSIGNED (typex))
848 typex = unsigned_type_for (typex);
849 return convert (type,
850 fold_build1 (ex_form, typex,
851 convert (typex,
852 TREE_OPERAND (expr, 0))));
855 CASE_CONVERT:
856 /* Don't introduce a
857 "can't convert between vector values of different size" error. */
858 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
859 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
860 != GET_MODE_SIZE (TYPE_MODE (type))))
861 break;
862 /* If truncating after truncating, might as well do all at once.
863 If truncating after extending, we may get rid of wasted work. */
864 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
866 case COND_EXPR:
867 /* It is sometimes worthwhile to push the narrowing down through
868 the conditional and never loses. A COND_EXPR may have a throw
869 as one operand, which then has void type. Just leave void
870 operands as they are. */
871 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
872 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
873 ? TREE_OPERAND (expr, 1)
874 : convert (type, TREE_OPERAND (expr, 1)),
875 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
876 ? TREE_OPERAND (expr, 2)
877 : convert (type, TREE_OPERAND (expr, 2)));
879 default:
880 break;
883 /* When parsing long initializers, we might end up with a lot of casts.
884 Shortcut this. */
885 if (TREE_CODE (expr) == INTEGER_CST)
886 return fold_convert (type, expr);
887 return build1 (CONVERT_EXPR, type, expr);
889 case REAL_TYPE:
890 if (flag_sanitize & SANITIZE_FLOAT_CAST
891 && do_ubsan_in_current_function ())
893 expr = save_expr (expr);
894 tree check = ubsan_instrument_float_cast (loc, type, expr, expr);
895 expr = build1 (FIX_TRUNC_EXPR, type, expr);
896 if (check == NULL)
897 return expr;
898 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
900 else
901 return build1 (FIX_TRUNC_EXPR, type, expr);
903 case FIXED_POINT_TYPE:
904 return build1 (FIXED_CONVERT_EXPR, type, expr);
906 case COMPLEX_TYPE:
907 return convert (type,
908 fold_build1 (REALPART_EXPR,
909 TREE_TYPE (TREE_TYPE (expr)), expr));
911 case VECTOR_TYPE:
912 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
914 error ("can%'t convert a vector of type %qT"
915 " to type %qT which has different size",
916 TREE_TYPE (expr), type);
917 return error_mark_node;
919 return build1 (VIEW_CONVERT_EXPR, type, expr);
921 default:
922 error ("aggregate value used where an integer was expected");
923 return convert (type, integer_zero_node);
927 /* Convert EXPR to the complex type TYPE in the usual ways. */
929 tree
930 convert_to_complex (tree type, tree expr)
932 tree subtype = TREE_TYPE (type);
934 switch (TREE_CODE (TREE_TYPE (expr)))
936 case REAL_TYPE:
937 case FIXED_POINT_TYPE:
938 case INTEGER_TYPE:
939 case ENUMERAL_TYPE:
940 case BOOLEAN_TYPE:
941 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
942 convert (subtype, integer_zero_node));
944 case COMPLEX_TYPE:
946 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
948 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
949 return expr;
950 else if (TREE_CODE (expr) == COMPOUND_EXPR)
952 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
953 if (t == TREE_OPERAND (expr, 1))
954 return expr;
955 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
956 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
958 else if (TREE_CODE (expr) == COMPLEX_EXPR)
959 return fold_build2 (COMPLEX_EXPR, type,
960 convert (subtype, TREE_OPERAND (expr, 0)),
961 convert (subtype, TREE_OPERAND (expr, 1)));
962 else
964 expr = save_expr (expr);
965 return
966 fold_build2 (COMPLEX_EXPR, type,
967 convert (subtype,
968 fold_build1 (REALPART_EXPR,
969 TREE_TYPE (TREE_TYPE (expr)),
970 expr)),
971 convert (subtype,
972 fold_build1 (IMAGPART_EXPR,
973 TREE_TYPE (TREE_TYPE (expr)),
974 expr)));
978 case POINTER_TYPE:
979 case REFERENCE_TYPE:
980 error ("pointer value used where a complex was expected");
981 return convert_to_complex (type, integer_zero_node);
983 default:
984 error ("aggregate value used where a complex was expected");
985 return convert_to_complex (type, integer_zero_node);
989 /* Convert EXPR to the vector type TYPE in the usual ways. */
991 tree
992 convert_to_vector (tree type, tree expr)
994 switch (TREE_CODE (TREE_TYPE (expr)))
996 case INTEGER_TYPE:
997 case VECTOR_TYPE:
998 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
1000 error ("can%'t convert a value of type %qT"
1001 " to vector type %qT which has different size",
1002 TREE_TYPE (expr), type);
1003 return error_mark_node;
1005 return build1 (VIEW_CONVERT_EXPR, type, expr);
1007 default:
1008 error ("can%'t convert value to a vector");
1009 return error_mark_node;
1013 /* Convert EXPR to some fixed-point type TYPE.
1015 EXPR must be fixed-point, float, integer, or enumeral;
1016 in other cases error is called. */
1018 tree
1019 convert_to_fixed (tree type, tree expr)
1021 if (integer_zerop (expr))
1023 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1024 return fixed_zero_node;
1026 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1028 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1029 return fixed_one_node;
1032 switch (TREE_CODE (TREE_TYPE (expr)))
1034 case FIXED_POINT_TYPE:
1035 case INTEGER_TYPE:
1036 case ENUMERAL_TYPE:
1037 case BOOLEAN_TYPE:
1038 case REAL_TYPE:
1039 return build1 (FIXED_CONVERT_EXPR, type, expr);
1041 case COMPLEX_TYPE:
1042 return convert (type,
1043 fold_build1 (REALPART_EXPR,
1044 TREE_TYPE (TREE_TYPE (expr)), expr));
1046 default:
1047 error ("aggregate value used where a fixed-point was expected");
1048 return error_mark_node;