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
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
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. */
26 #include "coretypes.h"
29 #include "diagnostic-core.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
33 #include "langhooks.h"
37 #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
38 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
39 : build1_loc (LOC, CODE, TYPE, EXPR))
40 #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
41 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
42 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
44 /* Convert EXPR to some pointer or reference type TYPE.
45 EXPR must be pointer, reference, integer, enumeral, or literal zero;
46 in other cases error is called. If FOLD_P is true, try to fold the
50 convert_to_pointer_1 (tree type
, tree expr
, bool fold_p
)
52 location_t loc
= EXPR_LOCATION (expr
);
53 if (TREE_TYPE (expr
) == type
)
56 switch (TREE_CODE (TREE_TYPE (expr
)))
61 /* If the pointers point to different address spaces, conversion needs
62 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
63 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
64 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
67 return maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
, type
, expr
);
69 return maybe_fold_build1_loc (fold_p
, loc
, ADDR_SPACE_CONVERT_EXPR
,
77 /* If the input precision differs from the target pointer type
78 precision, first convert the input expression to an integer type of
79 the target precision. Some targets, e.g. VMS, need several pointer
80 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
81 unsigned int pprec
= TYPE_PRECISION (type
);
82 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
86 = maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
,
87 lang_hooks
.types
.type_for_size (pprec
, 0),
90 return maybe_fold_build1_loc (fold_p
, loc
, CONVERT_EXPR
, type
, expr
);
93 error ("cannot convert to a pointer type");
94 return convert_to_pointer_1 (type
, integer_zero_node
, fold_p
);
98 /* A wrapper around convert_to_pointer_1 that always folds the
102 convert_to_pointer (tree type
, tree expr
)
104 return convert_to_pointer_1 (type
, expr
, true);
107 /* A wrapper around convert_to_pointer_1 that only folds the
108 expression if it is CONSTANT_CLASS_P. */
111 convert_to_pointer_nofold (tree type
, tree expr
)
113 return convert_to_pointer_1 (type
, expr
, CONSTANT_CLASS_P (expr
));
116 /* Convert EXPR to some floating-point type TYPE.
118 EXPR must be float, fixed-point, integer, or enumeral;
119 in other cases error is called. If FOLD_P is true, try to fold
123 convert_to_real_1 (tree type
, tree expr
, bool fold_p
)
125 enum built_in_function fcode
= builtin_mathfn_code (expr
);
126 tree itype
= TREE_TYPE (expr
);
127 location_t loc
= EXPR_LOCATION (expr
);
129 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
131 tree t
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 1), fold_p
);
132 if (t
== TREE_OPERAND (expr
, 1))
134 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
135 TREE_OPERAND (expr
, 0), t
);
138 /* Disable until we figure out how to decide whether the functions are
139 present in runtime. */
140 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
142 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
143 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
147 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
162 /* The above functions may set errno differently with float
163 input or output so this transformation is not safe with
184 /* The above functions are not safe to do this conversion. */
185 if (!flag_unsafe_math_optimizations
)
192 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
195 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
196 the both as the safe type for operation. */
197 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
198 newtype
= TREE_TYPE (arg0
);
200 /* We consider to convert
202 (T1) sqrtT2 ((T2) exprT3)
204 (T1) sqrtT4 ((T4) exprT3)
206 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
207 and T4 is NEWTYPE. All those types are of floating point types.
208 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
209 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
210 T2 and T4. See the following URL for a reference:
211 http://stackoverflow.com/questions/9235456/determining-
212 floating-point-square-root
214 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
215 && !flag_unsafe_math_optimizations
)
217 /* The following conversion is unsafe even the precision condition
220 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
222 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
225 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
226 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
231 /* Be careful about integer to fp conversions.
232 These may overflow still. */
233 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
234 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
235 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
236 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
238 tree fn
= mathfn_built_in (newtype
, fcode
);
241 tree arg
= convert_to_real_1 (newtype
, arg0
, fold_p
);
242 expr
= build_call_expr (fn
, 1, arg
);
253 /* Propagate the cast into the operation. */
254 if (itype
!= type
&& FLOAT_TYPE_P (type
))
255 switch (TREE_CODE (expr
))
257 /* Convert (float)-x into -(float)x. This is safe for
258 round-to-nearest rounding mode when the inner type is float. */
261 if (!flag_rounding_math
262 && FLOAT_TYPE_P (itype
)
263 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
265 tree arg
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 0),
267 return build1 (TREE_CODE (expr
), type
, arg
);
270 /* Convert (outertype)((innertype0)a+(innertype1)b)
271 into ((newtype)a+(newtype)b) where newtype
272 is the widest mode from all of these. */
278 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
279 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
281 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
282 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
283 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
287 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
288 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
289 || TYPE_MODE (type
) == SDmode
)
290 newtype
= dfloat32_type_node
;
291 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
292 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
293 || TYPE_MODE (type
) == DDmode
)
294 newtype
= dfloat64_type_node
;
295 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
296 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
297 || TYPE_MODE (type
) == TDmode
)
298 newtype
= dfloat128_type_node
;
299 if (newtype
== dfloat32_type_node
300 || newtype
== dfloat64_type_node
301 || newtype
== dfloat128_type_node
)
303 expr
= build2 (TREE_CODE (expr
), newtype
,
304 convert_to_real_1 (newtype
, arg0
,
306 convert_to_real_1 (newtype
, arg1
,
313 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
314 newtype
= TREE_TYPE (arg0
);
315 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
316 newtype
= TREE_TYPE (arg1
);
317 /* Sometimes this transformation is safe (cannot
318 change results through affecting double rounding
319 cases) and sometimes it is not. If NEWTYPE is
320 wider than TYPE, e.g. (float)((long double)double
321 + (long double)double) converted to
322 (float)(double + double), the transformation is
323 unsafe regardless of the details of the types
324 involved; double rounding can arise if the result
325 of NEWTYPE arithmetic is a NEWTYPE value half way
326 between two representable TYPE values but the
327 exact value is sufficiently different (in the
328 right direction) for this difference to be
329 visible in ITYPE arithmetic. If NEWTYPE is the
330 same as TYPE, however, the transformation may be
331 safe depending on the types involved: it is safe
332 if the ITYPE has strictly more than twice as many
333 mantissa bits as TYPE, can represent infinities
334 and NaNs if the TYPE can, and has sufficient
335 exponent range for the product or ratio of two
336 values representable in the TYPE to be within the
337 range of normal values of ITYPE. */
338 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
339 && (flag_unsafe_math_optimizations
340 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
341 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
343 && !excess_precision_type (newtype
))))
345 expr
= build2 (TREE_CODE (expr
), newtype
,
346 convert_to_real_1 (newtype
, arg0
,
348 convert_to_real_1 (newtype
, arg1
,
360 switch (TREE_CODE (TREE_TYPE (expr
)))
363 /* Ignore the conversion if we don't need to store intermediate
364 results and neither type is a decimal float. */
365 return build1 ((flag_float_store
366 || DECIMAL_FLOAT_TYPE_P (type
)
367 || DECIMAL_FLOAT_TYPE_P (itype
))
368 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
373 return build1 (FLOAT_EXPR
, type
, expr
);
375 case FIXED_POINT_TYPE
:
376 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
379 return convert (type
,
380 maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
381 TREE_TYPE (TREE_TYPE (expr
)),
386 error ("pointer value used where a floating point value was expected");
387 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
390 error ("aggregate value used where a float was expected");
391 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
395 /* A wrapper around convert_to_real_1 that always folds the
399 convert_to_real (tree type
, tree expr
)
401 return convert_to_real_1 (type
, expr
, true);
404 /* A wrapper around convert_to_real_1 that only folds the
405 expression if it is CONSTANT_CLASS_P. */
408 convert_to_real_nofold (tree type
, tree expr
)
410 return convert_to_real_1 (type
, expr
, CONSTANT_CLASS_P (expr
));
413 /* Convert EXPR to some integer (or enum) type TYPE.
415 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
416 fixed-point or vector; in other cases error is called.
418 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
420 The result of this is always supposed to be a newly created tree node
421 not in use in any existing structure. */
424 convert_to_integer_1 (tree type
, tree expr
, bool dofold
)
426 enum tree_code ex_form
= TREE_CODE (expr
);
427 tree intype
= TREE_TYPE (expr
);
428 unsigned int inprec
= element_precision (intype
);
429 unsigned int outprec
= element_precision (type
);
430 location_t loc
= EXPR_LOCATION (expr
);
432 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
433 be. Consider `enum E = { a, b = (enum E) 3 };'. */
434 if (!COMPLETE_TYPE_P (type
))
436 error ("conversion to incomplete type");
437 return error_mark_node
;
440 if (ex_form
== COMPOUND_EXPR
)
442 tree t
= convert_to_integer_1 (type
, TREE_OPERAND (expr
, 1), dofold
);
443 if (t
== TREE_OPERAND (expr
, 1))
445 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
446 TREE_OPERAND (expr
, 0), t
);
449 /* Convert e.g. (long)round(d) -> lround(d). */
450 /* If we're converting to char, we may encounter differing behavior
451 between converting from double->char vs double->long->char.
452 We're in "undefined" territory but we prefer to be conservative,
453 so only proceed in "unsafe" math mode. */
455 && (flag_unsafe_math_optimizations
456 || (long_integer_type_node
457 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
459 tree s_expr
= strip_float_extensions (expr
);
460 tree s_intype
= TREE_TYPE (s_expr
);
461 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
466 CASE_FLT_FN (BUILT_IN_CEIL
):
467 /* Only convert in ISO C99 mode. */
468 if (!targetm
.libc_has_function (function_c99_misc
))
470 if (outprec
< TYPE_PRECISION (integer_type_node
)
471 || (outprec
== TYPE_PRECISION (integer_type_node
)
472 && !TYPE_UNSIGNED (type
)))
473 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
474 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
475 && !TYPE_UNSIGNED (type
))
476 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
477 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
478 && !TYPE_UNSIGNED (type
))
479 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
482 CASE_FLT_FN (BUILT_IN_FLOOR
):
483 /* Only convert in ISO C99 mode. */
484 if (!targetm
.libc_has_function (function_c99_misc
))
486 if (outprec
< TYPE_PRECISION (integer_type_node
)
487 || (outprec
== TYPE_PRECISION (integer_type_node
)
488 && !TYPE_UNSIGNED (type
)))
489 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
490 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
491 && !TYPE_UNSIGNED (type
))
492 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
493 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
494 && !TYPE_UNSIGNED (type
))
495 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
498 CASE_FLT_FN (BUILT_IN_ROUND
):
499 /* Only convert in ISO C99 mode and with -fno-math-errno. */
500 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
502 if (outprec
< TYPE_PRECISION (integer_type_node
)
503 || (outprec
== TYPE_PRECISION (integer_type_node
)
504 && !TYPE_UNSIGNED (type
)))
505 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
506 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
507 && !TYPE_UNSIGNED (type
))
508 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
509 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
510 && !TYPE_UNSIGNED (type
))
511 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
514 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
515 /* Only convert nearbyint* if we can ignore math exceptions. */
516 if (flag_trapping_math
)
518 /* ... Fall through ... */
519 CASE_FLT_FN (BUILT_IN_RINT
):
520 /* Only convert in ISO C99 mode and with -fno-math-errno. */
521 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
523 if (outprec
< TYPE_PRECISION (integer_type_node
)
524 || (outprec
== TYPE_PRECISION (integer_type_node
)
525 && !TYPE_UNSIGNED (type
)))
526 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
527 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
528 && !TYPE_UNSIGNED (type
))
529 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
530 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
531 && !TYPE_UNSIGNED (type
))
532 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
535 CASE_FLT_FN (BUILT_IN_TRUNC
):
536 return convert_to_integer_1 (type
, CALL_EXPR_ARG (s_expr
, 0), dofold
);
544 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
545 return convert_to_integer_1 (type
, newexpr
, dofold
);
549 /* Convert (int)logb(d) -> ilogb(d). */
551 && flag_unsafe_math_optimizations
552 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
554 && (outprec
> TYPE_PRECISION (integer_type_node
)
555 || (outprec
== TYPE_PRECISION (integer_type_node
)
556 && !TYPE_UNSIGNED (type
))))
558 tree s_expr
= strip_float_extensions (expr
);
559 tree s_intype
= TREE_TYPE (s_expr
);
560 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
565 CASE_FLT_FN (BUILT_IN_LOGB
):
566 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
575 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
576 return convert_to_integer_1 (type
, newexpr
, dofold
);
580 switch (TREE_CODE (intype
))
584 if (integer_zerop (expr
))
585 return build_int_cst (type
, 0);
587 /* Convert to an unsigned integer of the correct width first, and from
588 there widen/truncate to the required type. Some targets support the
589 coexistence of multiple valid pointer sizes, so fetch the one we need
592 return build1 (CONVERT_EXPR
, type
, expr
);
593 expr
= fold_build1 (CONVERT_EXPR
,
594 lang_hooks
.types
.type_for_size
595 (TYPE_PRECISION (intype
), 0),
597 return fold_convert (type
, expr
);
603 /* If this is a logical operation, which just returns 0 or 1, we can
604 change the type of the expression. */
606 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
608 expr
= copy_node (expr
);
609 TREE_TYPE (expr
) = type
;
613 /* If we are widening the type, put in an explicit conversion.
614 Similarly if we are not changing the width. After this, we know
615 we are truncating EXPR. */
617 else if (outprec
>= inprec
)
621 /* If the precision of the EXPR's type is K bits and the
622 destination mode has more bits, and the sign is changing,
623 it is not safe to use a NOP_EXPR. For example, suppose
624 that EXPR's type is a 3-bit unsigned integer type, the
625 TYPE is a 3-bit signed integer type, and the machine mode
626 for the types is 8-bit QImode. In that case, the
627 conversion necessitates an explicit sign-extension. In
628 the signed-to-unsigned case the high-order bits have to
630 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
631 && (TYPE_PRECISION (TREE_TYPE (expr
))
632 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
637 return maybe_fold_build1_loc (dofold
, loc
, code
, type
, expr
);
640 /* If TYPE is an enumeral type or a type with a precision less
641 than the number of bits in its mode, do the conversion to the
642 type corresponding to its mode, then do a nop conversion
644 else if (TREE_CODE (type
) == ENUMERAL_TYPE
645 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
646 return build1 (NOP_EXPR
, type
,
647 convert (lang_hooks
.types
.type_for_mode
648 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
651 /* Here detect when we can distribute the truncation down past some
652 arithmetic. For example, if adding two longs and converting to an
653 int, we can equally well convert both to ints and then add.
654 For the operations handled here, such truncation distribution
656 It is desirable in these cases:
657 1) when truncating down to full-word from a larger size
658 2) when truncating takes no work.
659 3) when at least one operand of the arithmetic has been extended
660 (as by C's default conversions). In this case we need two conversions
661 if we do the arithmetic as already requested, so we might as well
662 truncate both and then combine. Perhaps that way we need only one.
664 Note that in general we cannot do the arithmetic in a type
665 shorter than the desired result of conversion, even if the operands
666 are both extended from a shorter type, because they might overflow
667 if combined in that type. The exceptions to this--the times when
668 two narrow values can be combined in their narrow type even to
669 make a wider result--are handled by "shorten" in build_binary_op. */
674 /* We can pass truncation down through right shifting
675 when the shift count is a nonpositive constant. */
676 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
677 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
682 /* We can pass truncation down through left shifting
683 when the shift count is a nonnegative constant and
684 the target type is unsigned. */
685 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
686 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
687 && TYPE_UNSIGNED (type
)
688 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
690 /* If shift count is less than the width of the truncated type,
692 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
693 /* In this case, shifting is like multiplication. */
697 /* If it is >= that width, result is zero.
698 Handling this with trunc1 would give the wrong result:
699 (int) ((long long) a << 32) is well defined (as 0)
700 but (int) a << 32 is undefined and would get a
703 tree t
= build_int_cst (type
, 0);
705 /* If the original expression had side-effects, we must
707 if (TREE_SIDE_EFFECTS (expr
))
708 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
717 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
718 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
720 /* Don't distribute unless the output precision is at least as big
721 as the actual inputs and it has the same signedness. */
722 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
723 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
724 /* If signedness of arg0 and arg1 don't match,
725 we can't necessarily find a type to compare them in. */
726 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
727 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
728 /* Do not change the sign of the division. */
729 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
730 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
731 /* Either require unsigned division or a division by
732 a constant that is not -1. */
733 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
734 || (TREE_CODE (arg1
) == INTEGER_CST
735 && !integer_all_onesp (arg1
))))
744 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
745 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
747 /* Don't distribute unless the output precision is at least as big
748 as the actual inputs. Otherwise, the comparison of the
749 truncated values will be wrong. */
750 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
751 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
752 /* If signedness of arg0 and arg1 don't match,
753 we can't necessarily find a type to compare them in. */
754 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
755 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
767 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
768 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
770 /* Do not try to narrow operands of pointer subtraction;
771 that will interfere with other folding. */
772 if (ex_form
== MINUS_EXPR
773 && CONVERT_EXPR_P (arg0
)
774 && CONVERT_EXPR_P (arg1
)
775 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
776 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
779 if (outprec
>= BITS_PER_WORD
780 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
781 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
782 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
784 /* Do the arithmetic in type TYPEX,
785 then convert result to TYPE. */
788 /* Can't do arithmetic in enumeral types
789 so use an integer type that will hold the values. */
790 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
792 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
793 TYPE_UNSIGNED (typex
));
795 /* But now perhaps TYPEX is as wide as INPREC.
796 In that case, do nothing special here.
797 (Otherwise would recurse infinitely in convert. */
798 if (TYPE_PRECISION (typex
) != inprec
)
800 /* Don't do unsigned arithmetic where signed was wanted,
802 Exception: if both of the original operands were
803 unsigned then we can safely do the work as unsigned.
804 Exception: shift operations take their type solely
805 from the first argument.
806 Exception: the LSHIFT_EXPR case above requires that
807 we perform this operation unsigned lest we produce
808 signed-overflow undefinedness.
809 And we may need to do it as unsigned
810 if we truncate to the original size. */
811 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
812 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
813 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
814 || ex_form
== LSHIFT_EXPR
815 || ex_form
== RSHIFT_EXPR
816 || ex_form
== LROTATE_EXPR
817 || ex_form
== RROTATE_EXPR
))
818 || ex_form
== LSHIFT_EXPR
819 /* If we have !flag_wrapv, and either ARG0 or
820 ARG1 is of a signed type, we have to do
821 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
822 type in case the operation in outprec precision
823 could overflow. Otherwise, we would introduce
824 signed-overflow undefinedness. */
825 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
826 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
827 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
829 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
831 && (ex_form
== PLUS_EXPR
832 || ex_form
== MINUS_EXPR
833 || ex_form
== MULT_EXPR
)))
835 if (!TYPE_UNSIGNED (typex
))
836 typex
= unsigned_type_for (typex
);
840 if (TYPE_UNSIGNED (typex
))
841 typex
= signed_type_for (typex
);
843 /* We should do away with all this once we have a proper
844 type promotion/demotion pass, see PR45397. */
845 expr
= maybe_fold_build2_loc (dofold
, loc
, ex_form
, typex
,
846 convert (typex
, arg0
),
847 convert (typex
, arg1
));
848 return convert (type
, expr
);
856 /* This is not correct for ABS_EXPR,
857 since we must test the sign before truncation. */
862 /* Do the arithmetic in type TYPEX,
863 then convert result to TYPE. */
866 /* Can't do arithmetic in enumeral types
867 so use an integer type that will hold the values. */
868 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
870 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
871 TYPE_UNSIGNED (typex
));
873 if (!TYPE_UNSIGNED (typex
))
874 typex
= unsigned_type_for (typex
);
875 return convert (type
,
876 fold_build1 (ex_form
, typex
,
878 TREE_OPERAND (expr
, 0))));
883 "can't convert between vector values of different size" error. */
884 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
885 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
886 != GET_MODE_SIZE (TYPE_MODE (type
))))
888 /* If truncating after truncating, might as well do all at once.
889 If truncating after extending, we may get rid of wasted work. */
890 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
893 /* It is sometimes worthwhile to push the narrowing down through
894 the conditional and never loses. A COND_EXPR may have a throw
895 as one operand, which then has void type. Just leave void
896 operands as they are. */
899 fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
900 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
901 ? TREE_OPERAND (expr
, 1)
902 : convert (type
, TREE_OPERAND (expr
, 1)),
903 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
904 ? TREE_OPERAND (expr
, 2)
905 : convert (type
, TREE_OPERAND (expr
, 2)));
911 /* When parsing long initializers, we might end up with a lot of casts.
913 if (TREE_CODE (expr
) == INTEGER_CST
)
914 return fold_convert (type
, expr
);
915 return build1 (CONVERT_EXPR
, type
, expr
);
918 if (flag_sanitize
& SANITIZE_FLOAT_CAST
919 && do_ubsan_in_current_function ())
921 expr
= save_expr (expr
);
922 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
, expr
);
923 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
926 return maybe_fold_build2_loc (dofold
, loc
, COMPOUND_EXPR
,
927 TREE_TYPE (expr
), check
, expr
);
930 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
932 case FIXED_POINT_TYPE
:
933 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
936 expr
= maybe_fold_build1_loc (dofold
, loc
, REALPART_EXPR
,
937 TREE_TYPE (TREE_TYPE (expr
)), expr
);
938 return convert (type
, expr
);
941 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
943 error ("can%'t convert a vector of type %qT"
944 " to type %qT which has different size",
945 TREE_TYPE (expr
), type
);
946 return error_mark_node
;
948 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
951 error ("aggregate value used where an integer was expected");
952 return convert (type
, integer_zero_node
);
956 /* Convert EXPR to some integer (or enum) type TYPE.
958 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
959 fixed-point or vector; in other cases error is called.
961 The result of this is always supposed to be a newly created tree node
962 not in use in any existing structure. */
965 convert_to_integer (tree type
, tree expr
)
967 return convert_to_integer_1 (type
, expr
, true);
970 /* Convert EXPR to some integer (or enum) type TYPE.
972 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
973 fixed-point or vector; in other cases error is called.
975 The result of this is always supposed to be a newly created tree node
976 not in use in any existing structure. The tree node isn't folded,
977 beside EXPR is of constant class. */
980 convert_to_integer_nofold (tree type
, tree expr
)
982 return convert_to_integer_1 (type
, expr
, CONSTANT_CLASS_P (expr
));
985 /* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
986 true, try to fold the expression. */
989 convert_to_complex_1 (tree type
, tree expr
, bool fold_p
)
991 location_t loc
= EXPR_LOCATION (expr
);
992 tree subtype
= TREE_TYPE (type
);
994 switch (TREE_CODE (TREE_TYPE (expr
)))
997 case FIXED_POINT_TYPE
:
1001 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
1002 convert (subtype
, integer_zero_node
));
1006 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
1008 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
1010 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
1012 tree t
= convert_to_complex_1 (type
, TREE_OPERAND (expr
, 1),
1014 if (t
== TREE_OPERAND (expr
, 1))
1016 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
1017 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
1019 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
1020 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1022 TREE_OPERAND (expr
, 0)),
1024 TREE_OPERAND (expr
, 1)));
1027 expr
= save_expr (expr
);
1028 tree realp
= maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
1029 TREE_TYPE (TREE_TYPE (expr
)),
1031 tree imagp
= maybe_fold_build1_loc (fold_p
, loc
, IMAGPART_EXPR
,
1032 TREE_TYPE (TREE_TYPE (expr
)),
1034 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1035 convert (subtype
, realp
),
1036 convert (subtype
, imagp
));
1041 case REFERENCE_TYPE
:
1042 error ("pointer value used where a complex was expected");
1043 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1046 error ("aggregate value used where a complex was expected");
1047 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1051 /* A wrapper around convert_to_complex_1 that always folds the
1055 convert_to_complex (tree type
, tree expr
)
1057 return convert_to_complex_1 (type
, expr
, true);
1060 /* A wrapper around convert_to_complex_1 that only folds the
1061 expression if it is CONSTANT_CLASS_P. */
1064 convert_to_complex_nofold (tree type
, tree expr
)
1066 return convert_to_complex_1 (type
, expr
, CONSTANT_CLASS_P (expr
));
1069 /* Convert EXPR to the vector type TYPE in the usual ways. */
1072 convert_to_vector (tree type
, tree expr
)
1074 switch (TREE_CODE (TREE_TYPE (expr
)))
1078 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1080 error ("can%'t convert a value of type %qT"
1081 " to vector type %qT which has different size",
1082 TREE_TYPE (expr
), type
);
1083 return error_mark_node
;
1085 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1088 error ("can%'t convert value to a vector");
1089 return error_mark_node
;
1093 /* Convert EXPR to some fixed-point type TYPE.
1095 EXPR must be fixed-point, float, integer, or enumeral;
1096 in other cases error is called. */
1099 convert_to_fixed (tree type
, tree expr
)
1101 if (integer_zerop (expr
))
1103 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1104 return fixed_zero_node
;
1106 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1108 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1109 return fixed_one_node
;
1112 switch (TREE_CODE (TREE_TYPE (expr
)))
1114 case FIXED_POINT_TYPE
:
1119 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1122 return convert (type
,
1123 fold_build1 (REALPART_EXPR
,
1124 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1127 error ("aggregate value used where a fixed-point was expected");
1128 return error_mark_node
;