1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2016 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 DOFOLD, or if it is CONSTANT_CLASS_P. */
111 convert_to_pointer_maybe_fold (tree type
, tree expr
, bool dofold
)
113 return convert_to_pointer_1 (type
, expr
, dofold
|| 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
185 /* The above functions are not safe to do this conversion. */
186 if (!flag_unsafe_math_optimizations
)
194 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
197 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
198 the both as the safe type for operation. */
199 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
200 newtype
= TREE_TYPE (arg0
);
202 /* We consider to convert
204 (T1) sqrtT2 ((T2) exprT3)
206 (T1) sqrtT4 ((T4) exprT3)
208 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
209 and T4 is NEWTYPE. All those types are of floating point types.
210 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
211 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
212 T2 and T4. See the following URL for a reference:
213 http://stackoverflow.com/questions/9235456/determining-
214 floating-point-square-root
216 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
217 && !flag_unsafe_math_optimizations
)
219 /* The following conversion is unsafe even the precision condition
222 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
224 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
227 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
228 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
233 /* Be careful about integer to fp conversions.
234 These may overflow still. */
235 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
236 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
237 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
238 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
240 tree fn
= mathfn_built_in (newtype
, fcode
);
243 tree arg
= convert_to_real_1 (newtype
, arg0
, fold_p
);
244 expr
= build_call_expr (fn
, 1, arg
);
255 /* Propagate the cast into the operation. */
256 if (itype
!= type
&& FLOAT_TYPE_P (type
))
257 switch (TREE_CODE (expr
))
259 /* Convert (float)-x into -(float)x. This is safe for
260 round-to-nearest rounding mode when the inner type is float. */
263 if (!flag_rounding_math
264 && FLOAT_TYPE_P (itype
)
265 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
267 tree arg
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 0),
269 return build1 (TREE_CODE (expr
), type
, arg
);
272 /* Convert (outertype)((innertype0)a+(innertype1)b)
273 into ((newtype)a+(newtype)b) where newtype
274 is the widest mode from all of these. */
280 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
281 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
283 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
284 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
285 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
289 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
290 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
291 || TYPE_MODE (type
) == SDmode
)
292 newtype
= dfloat32_type_node
;
293 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
294 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
295 || TYPE_MODE (type
) == DDmode
)
296 newtype
= dfloat64_type_node
;
297 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
298 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
299 || TYPE_MODE (type
) == TDmode
)
300 newtype
= dfloat128_type_node
;
301 if (newtype
== dfloat32_type_node
302 || newtype
== dfloat64_type_node
303 || newtype
== dfloat128_type_node
)
305 expr
= build2 (TREE_CODE (expr
), newtype
,
306 convert_to_real_1 (newtype
, arg0
,
308 convert_to_real_1 (newtype
, arg1
,
315 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
316 newtype
= TREE_TYPE (arg0
);
317 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
318 newtype
= TREE_TYPE (arg1
);
319 /* Sometimes this transformation is safe (cannot
320 change results through affecting double rounding
321 cases) and sometimes it is not. If NEWTYPE is
322 wider than TYPE, e.g. (float)((long double)double
323 + (long double)double) converted to
324 (float)(double + double), the transformation is
325 unsafe regardless of the details of the types
326 involved; double rounding can arise if the result
327 of NEWTYPE arithmetic is a NEWTYPE value half way
328 between two representable TYPE values but the
329 exact value is sufficiently different (in the
330 right direction) for this difference to be
331 visible in ITYPE arithmetic. If NEWTYPE is the
332 same as TYPE, however, the transformation may be
333 safe depending on the types involved: it is safe
334 if the ITYPE has strictly more than twice as many
335 mantissa bits as TYPE, can represent infinities
336 and NaNs if the TYPE can, and has sufficient
337 exponent range for the product or ratio of two
338 values representable in the TYPE to be within the
339 range of normal values of ITYPE. */
340 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
341 && (flag_unsafe_math_optimizations
342 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
343 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
345 && !excess_precision_type (newtype
))))
347 expr
= build2 (TREE_CODE (expr
), newtype
,
348 convert_to_real_1 (newtype
, arg0
,
350 convert_to_real_1 (newtype
, arg1
,
362 switch (TREE_CODE (TREE_TYPE (expr
)))
365 /* Ignore the conversion if we don't need to store intermediate
366 results and neither type is a decimal float. */
367 return build1_loc (loc
,
369 || DECIMAL_FLOAT_TYPE_P (type
)
370 || DECIMAL_FLOAT_TYPE_P (itype
))
371 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
376 return build1 (FLOAT_EXPR
, type
, expr
);
378 case FIXED_POINT_TYPE
:
379 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
382 return convert (type
,
383 maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
384 TREE_TYPE (TREE_TYPE (expr
)),
389 error ("pointer value used where a floating point value was expected");
390 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
393 error ("aggregate value used where a float was expected");
394 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
398 /* A wrapper around convert_to_real_1 that always folds the
402 convert_to_real (tree type
, tree expr
)
404 return convert_to_real_1 (type
, expr
, true);
407 /* A wrapper around convert_to_real_1 that only folds the
408 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
411 convert_to_real_maybe_fold (tree type
, tree expr
, bool dofold
)
413 return convert_to_real_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
416 /* Convert EXPR to some integer (or enum) type TYPE.
418 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
419 fixed-point or vector; in other cases error is called.
421 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
423 The result of this is always supposed to be a newly created tree node
424 not in use in any existing structure. */
427 convert_to_integer_1 (tree type
, tree expr
, bool dofold
)
429 enum tree_code ex_form
= TREE_CODE (expr
);
430 tree intype
= TREE_TYPE (expr
);
431 unsigned int inprec
= element_precision (intype
);
432 unsigned int outprec
= element_precision (type
);
433 location_t loc
= EXPR_LOCATION (expr
);
435 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
436 be. Consider `enum E = { a, b = (enum E) 3 };'. */
437 if (!COMPLETE_TYPE_P (type
))
439 error ("conversion to incomplete type");
440 return error_mark_node
;
443 if (ex_form
== COMPOUND_EXPR
)
445 tree t
= convert_to_integer_1 (type
, TREE_OPERAND (expr
, 1), dofold
);
446 if (t
== TREE_OPERAND (expr
, 1))
448 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
449 TREE_OPERAND (expr
, 0), t
);
452 /* Convert e.g. (long)round(d) -> lround(d). */
453 /* If we're converting to char, we may encounter differing behavior
454 between converting from double->char vs double->long->char.
455 We're in "undefined" territory but we prefer to be conservative,
456 so only proceed in "unsafe" math mode. */
458 && (flag_unsafe_math_optimizations
459 || (long_integer_type_node
460 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
462 tree s_expr
= strip_float_extensions (expr
);
463 tree s_intype
= TREE_TYPE (s_expr
);
464 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
469 CASE_FLT_FN (BUILT_IN_CEIL
):
470 /* Only convert in ISO C99 mode. */
471 if (!targetm
.libc_has_function (function_c99_misc
))
473 if (outprec
< TYPE_PRECISION (integer_type_node
)
474 || (outprec
== TYPE_PRECISION (integer_type_node
)
475 && !TYPE_UNSIGNED (type
)))
476 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
477 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
478 && !TYPE_UNSIGNED (type
))
479 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
480 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
481 && !TYPE_UNSIGNED (type
))
482 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
485 CASE_FLT_FN (BUILT_IN_FLOOR
):
486 /* Only convert in ISO C99 mode. */
487 if (!targetm
.libc_has_function (function_c99_misc
))
489 if (outprec
< TYPE_PRECISION (integer_type_node
)
490 || (outprec
== TYPE_PRECISION (integer_type_node
)
491 && !TYPE_UNSIGNED (type
)))
492 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
493 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
494 && !TYPE_UNSIGNED (type
))
495 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
496 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
497 && !TYPE_UNSIGNED (type
))
498 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
501 CASE_FLT_FN (BUILT_IN_ROUND
):
502 /* Only convert in ISO C99 mode and with -fno-math-errno. */
503 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
505 if (outprec
< TYPE_PRECISION (integer_type_node
)
506 || (outprec
== TYPE_PRECISION (integer_type_node
)
507 && !TYPE_UNSIGNED (type
)))
508 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
509 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
510 && !TYPE_UNSIGNED (type
))
511 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
512 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
513 && !TYPE_UNSIGNED (type
))
514 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
517 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
518 /* Only convert nearbyint* if we can ignore math exceptions. */
519 if (flag_trapping_math
)
522 CASE_FLT_FN (BUILT_IN_RINT
):
523 /* Only convert in ISO C99 mode and with -fno-math-errno. */
524 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
526 if (outprec
< TYPE_PRECISION (integer_type_node
)
527 || (outprec
== TYPE_PRECISION (integer_type_node
)
528 && !TYPE_UNSIGNED (type
)))
529 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
530 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
531 && !TYPE_UNSIGNED (type
))
532 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
533 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
534 && !TYPE_UNSIGNED (type
))
535 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
538 CASE_FLT_FN (BUILT_IN_TRUNC
):
539 return convert_to_integer_1 (type
, CALL_EXPR_ARG (s_expr
, 0), dofold
);
547 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
548 return convert_to_integer_1 (type
, newexpr
, dofold
);
552 /* Convert (int)logb(d) -> ilogb(d). */
554 && flag_unsafe_math_optimizations
555 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
557 && (outprec
> TYPE_PRECISION (integer_type_node
)
558 || (outprec
== TYPE_PRECISION (integer_type_node
)
559 && !TYPE_UNSIGNED (type
))))
561 tree s_expr
= strip_float_extensions (expr
);
562 tree s_intype
= TREE_TYPE (s_expr
);
563 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
568 CASE_FLT_FN (BUILT_IN_LOGB
):
569 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
578 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
579 return convert_to_integer_1 (type
, newexpr
, dofold
);
583 switch (TREE_CODE (intype
))
587 if (integer_zerop (expr
))
588 return build_int_cst (type
, 0);
590 /* Convert to an unsigned integer of the correct width first, and from
591 there widen/truncate to the required type. Some targets support the
592 coexistence of multiple valid pointer sizes, so fetch the one we need
595 return build1 (CONVERT_EXPR
, type
, expr
);
596 expr
= fold_build1 (CONVERT_EXPR
,
597 lang_hooks
.types
.type_for_size
598 (TYPE_PRECISION (intype
), 0),
600 return fold_convert (type
, expr
);
606 /* If this is a logical operation, which just returns 0 or 1, we can
607 change the type of the expression. */
609 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
611 expr
= copy_node (expr
);
612 TREE_TYPE (expr
) = type
;
616 /* If we are widening the type, put in an explicit conversion.
617 Similarly if we are not changing the width. After this, we know
618 we are truncating EXPR. */
620 else if (outprec
>= inprec
)
624 /* If the precision of the EXPR's type is K bits and the
625 destination mode has more bits, and the sign is changing,
626 it is not safe to use a NOP_EXPR. For example, suppose
627 that EXPR's type is a 3-bit unsigned integer type, the
628 TYPE is a 3-bit signed integer type, and the machine mode
629 for the types is 8-bit QImode. In that case, the
630 conversion necessitates an explicit sign-extension. In
631 the signed-to-unsigned case the high-order bits have to
633 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
634 && (TYPE_PRECISION (TREE_TYPE (expr
))
635 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
640 return maybe_fold_build1_loc (dofold
, loc
, code
, type
, expr
);
643 /* If TYPE is an enumeral type or a type with a precision less
644 than the number of bits in its mode, do the conversion to the
645 type corresponding to its mode, then do a nop conversion
647 else if (TREE_CODE (type
) == ENUMERAL_TYPE
648 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
650 expr
= convert (lang_hooks
.types
.type_for_mode
651 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)), expr
);
652 return maybe_fold_build1_loc (dofold
, loc
, NOP_EXPR
, type
, expr
);
655 /* Here detect when we can distribute the truncation down past some
656 arithmetic. For example, if adding two longs and converting to an
657 int, we can equally well convert both to ints and then add.
658 For the operations handled here, such truncation distribution
660 It is desirable in these cases:
661 1) when truncating down to full-word from a larger size
662 2) when truncating takes no work.
663 3) when at least one operand of the arithmetic has been extended
664 (as by C's default conversions). In this case we need two conversions
665 if we do the arithmetic as already requested, so we might as well
666 truncate both and then combine. Perhaps that way we need only one.
668 Note that in general we cannot do the arithmetic in a type
669 shorter than the desired result of conversion, even if the operands
670 are both extended from a shorter type, because they might overflow
671 if combined in that type. The exceptions to this--the times when
672 two narrow values can be combined in their narrow type even to
673 make a wider result--are handled by "shorten" in build_binary_op. */
679 /* We can pass truncation down through right shifting
680 when the shift count is a nonpositive constant. */
681 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
682 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
687 /* We can pass truncation down through left shifting
688 when the shift count is a nonnegative constant and
689 the target type is unsigned. */
690 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
691 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
692 && TYPE_UNSIGNED (type
)
693 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
695 /* If shift count is less than the width of the truncated type,
697 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
698 /* In this case, shifting is like multiplication. */
702 /* If it is >= that width, result is zero.
703 Handling this with trunc1 would give the wrong result:
704 (int) ((long long) a << 32) is well defined (as 0)
705 but (int) a << 32 is undefined and would get a
708 tree t
= build_int_cst (type
, 0);
710 /* If the original expression had side-effects, we must
712 if (TREE_SIDE_EFFECTS (expr
))
713 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
722 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
723 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
725 /* Don't distribute unless the output precision is at least as
726 big as the actual inputs and it has the same signedness. */
727 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
728 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
729 /* If signedness of arg0 and arg1 don't match,
730 we can't necessarily find a type to compare them in. */
731 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
732 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
733 /* Do not change the sign of the division. */
734 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
735 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
736 /* Either require unsigned division or a division by
737 a constant that is not -1. */
738 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
739 || (TREE_CODE (arg1
) == INTEGER_CST
740 && !integer_all_onesp (arg1
))))
749 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
750 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
752 /* Don't distribute unless the output precision is at least as
753 big as the actual inputs. Otherwise, the comparison of the
754 truncated values will be wrong. */
755 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
756 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
757 /* If signedness of arg0 and arg1 don't match,
758 we can't necessarily find a type to compare them in. */
759 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
760 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
772 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
773 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
775 /* Do not try to narrow operands of pointer subtraction;
776 that will interfere with other folding. */
777 if (ex_form
== MINUS_EXPR
778 && CONVERT_EXPR_P (arg0
)
779 && CONVERT_EXPR_P (arg1
)
780 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
781 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
784 if (outprec
>= BITS_PER_WORD
785 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
786 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
787 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
789 /* Do the arithmetic in type TYPEX,
790 then convert result to TYPE. */
793 /* Can't do arithmetic in enumeral types
794 so use an integer type that will hold the values. */
795 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
797 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
798 TYPE_UNSIGNED (typex
));
800 /* But now perhaps TYPEX is as wide as INPREC.
801 In that case, do nothing special here.
802 (Otherwise would recurse infinitely in convert. */
803 if (TYPE_PRECISION (typex
) != inprec
)
805 /* Don't do unsigned arithmetic where signed was wanted,
807 Exception: if both of the original operands were
808 unsigned then we can safely do the work as unsigned.
809 Exception: shift operations take their type solely
810 from the first argument.
811 Exception: the LSHIFT_EXPR case above requires that
812 we perform this operation unsigned lest we produce
813 signed-overflow undefinedness.
814 And we may need to do it as unsigned
815 if we truncate to the original size. */
816 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
817 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
818 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
819 || ex_form
== LSHIFT_EXPR
820 || ex_form
== RSHIFT_EXPR
821 || ex_form
== LROTATE_EXPR
822 || ex_form
== RROTATE_EXPR
))
823 || ex_form
== LSHIFT_EXPR
824 /* If we have !flag_wrapv, and either ARG0 or
825 ARG1 is of a signed type, we have to do
826 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
827 type in case the operation in outprec precision
828 could overflow. Otherwise, we would introduce
829 signed-overflow undefinedness. */
830 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
831 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
832 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
834 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
836 && (ex_form
== PLUS_EXPR
837 || ex_form
== MINUS_EXPR
838 || ex_form
== MULT_EXPR
)))
840 if (!TYPE_UNSIGNED (typex
))
841 typex
= unsigned_type_for (typex
);
845 if (TYPE_UNSIGNED (typex
))
846 typex
= signed_type_for (typex
);
848 /* We should do away with all this once we have a proper
849 type promotion/demotion pass, see PR45397. */
850 expr
= maybe_fold_build2_loc (dofold
, loc
, ex_form
, typex
,
851 convert (typex
, arg0
),
852 convert (typex
, arg1
));
853 return convert (type
, expr
);
861 /* This is not correct for ABS_EXPR,
862 since we must test the sign before truncation. */
864 /* Do the arithmetic in type TYPEX,
865 then convert result to TYPE. */
868 /* Can't do arithmetic in enumeral types
869 so use an integer type that will hold the values. */
870 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
872 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
873 TYPE_UNSIGNED (typex
));
875 if (!TYPE_UNSIGNED (typex
))
876 typex
= unsigned_type_for (typex
);
877 return convert (type
,
878 fold_build1 (ex_form
, typex
,
880 TREE_OPERAND (expr
, 0))));
884 /* Don't introduce a "can't convert between vector values of
885 different size" error. */
886 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
887 && (GET_MODE_SIZE (TYPE_MODE
888 (TREE_TYPE (TREE_OPERAND (expr
, 0))))
889 != GET_MODE_SIZE (TYPE_MODE (type
))))
891 /* If truncating after truncating, might as well do all at once.
892 If truncating after extending, we may get rid of wasted work. */
893 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
896 /* It is sometimes worthwhile to push the narrowing down through
897 the conditional and never loses. A COND_EXPR may have a throw
898 as one operand, which then has void type. Just leave void
899 operands as they are. */
901 fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
902 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
903 ? TREE_OPERAND (expr
, 1)
904 : convert (type
, TREE_OPERAND (expr
, 1)),
905 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
906 ? TREE_OPERAND (expr
, 2)
907 : convert (type
, TREE_OPERAND (expr
, 2)));
913 /* When parsing long initializers, we might end up with a lot of casts.
915 if (TREE_CODE (expr
) == INTEGER_CST
)
916 return fold_convert (type
, expr
);
917 return build1 (CONVERT_EXPR
, type
, expr
);
920 if (flag_sanitize
& SANITIZE_FLOAT_CAST
921 && do_ubsan_in_current_function ())
923 expr
= save_expr (expr
);
924 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
);
925 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
926 if (check
== NULL_TREE
)
928 return maybe_fold_build2_loc (dofold
, loc
, COMPOUND_EXPR
,
929 TREE_TYPE (expr
), check
, expr
);
932 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
934 case FIXED_POINT_TYPE
:
935 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
938 expr
= maybe_fold_build1_loc (dofold
, loc
, REALPART_EXPR
,
939 TREE_TYPE (TREE_TYPE (expr
)), expr
);
940 return convert (type
, expr
);
943 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
945 error ("can%'t convert a vector of type %qT"
946 " to type %qT which has different size",
947 TREE_TYPE (expr
), type
);
948 return error_mark_node
;
950 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
953 error ("aggregate value used where an integer was expected");
954 return convert (type
, integer_zero_node
);
958 /* Convert EXPR to some integer (or enum) type TYPE.
960 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
961 fixed-point or vector; in other cases error is called.
963 The result of this is always supposed to be a newly created tree node
964 not in use in any existing structure. */
967 convert_to_integer (tree type
, tree expr
)
969 return convert_to_integer_1 (type
, expr
, true);
972 /* A wrapper around convert_to_complex_1 that only folds the
973 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
976 convert_to_integer_maybe_fold (tree type
, tree expr
, bool dofold
)
978 return convert_to_integer_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
981 /* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
982 true, try to fold the expression. */
985 convert_to_complex_1 (tree type
, tree expr
, bool fold_p
)
987 location_t loc
= EXPR_LOCATION (expr
);
988 tree subtype
= TREE_TYPE (type
);
990 switch (TREE_CODE (TREE_TYPE (expr
)))
993 case FIXED_POINT_TYPE
:
997 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
998 convert (subtype
, integer_zero_node
));
1002 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
1004 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
1006 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
1008 tree t
= convert_to_complex_1 (type
, TREE_OPERAND (expr
, 1),
1010 if (t
== TREE_OPERAND (expr
, 1))
1012 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
1013 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
1015 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
1016 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1018 TREE_OPERAND (expr
, 0)),
1020 TREE_OPERAND (expr
, 1)));
1023 expr
= save_expr (expr
);
1024 tree realp
= maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
1025 TREE_TYPE (TREE_TYPE (expr
)),
1027 tree imagp
= maybe_fold_build1_loc (fold_p
, loc
, IMAGPART_EXPR
,
1028 TREE_TYPE (TREE_TYPE (expr
)),
1030 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1031 convert (subtype
, realp
),
1032 convert (subtype
, imagp
));
1037 case REFERENCE_TYPE
:
1038 error ("pointer value used where a complex was expected");
1039 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1042 error ("aggregate value used where a complex was expected");
1043 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1047 /* A wrapper around convert_to_complex_1 that always folds the
1051 convert_to_complex (tree type
, tree expr
)
1053 return convert_to_complex_1 (type
, expr
, true);
1056 /* A wrapper around convert_to_complex_1 that only folds the
1057 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
1060 convert_to_complex_maybe_fold (tree type
, tree expr
, bool dofold
)
1062 return convert_to_complex_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
1065 /* Convert EXPR to the vector type TYPE in the usual ways. */
1068 convert_to_vector (tree type
, tree expr
)
1070 switch (TREE_CODE (TREE_TYPE (expr
)))
1074 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1076 error ("can%'t convert a value of type %qT"
1077 " to vector type %qT which has different size",
1078 TREE_TYPE (expr
), type
);
1079 return error_mark_node
;
1081 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1084 error ("can%'t convert value to a vector");
1085 return error_mark_node
;
1089 /* Convert EXPR to some fixed-point type TYPE.
1091 EXPR must be fixed-point, float, integer, or enumeral;
1092 in other cases error is called. */
1095 convert_to_fixed (tree type
, tree expr
)
1097 if (integer_zerop (expr
))
1099 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1100 return fixed_zero_node
;
1102 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1104 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1105 return fixed_one_node
;
1108 switch (TREE_CODE (TREE_TYPE (expr
)))
1110 case FIXED_POINT_TYPE
:
1115 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1118 return convert (type
,
1119 fold_build1 (REALPART_EXPR
,
1120 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1123 error ("aggregate value used where a fixed-point was expected");
1124 return error_mark_node
;