1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2023 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"
36 #include "stringpool.h"
41 #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
42 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
43 : build1_loc (LOC, CODE, TYPE, EXPR))
44 #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
45 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
46 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
48 /* Convert EXPR to some pointer or reference type TYPE.
49 EXPR must be pointer, reference, integer, enumeral, or literal zero;
50 in other cases error is called. If FOLD_P is true, try to fold the
54 convert_to_pointer_1 (tree type
, tree expr
, bool fold_p
)
56 location_t loc
= EXPR_LOCATION (expr
);
57 if (TREE_TYPE (expr
) == type
)
60 switch (TREE_CODE (TREE_TYPE (expr
)))
65 /* If the pointers point to different address spaces, conversion needs
66 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
67 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
68 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
71 return maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
, type
, expr
);
73 return maybe_fold_build1_loc (fold_p
, loc
, ADDR_SPACE_CONVERT_EXPR
,
81 /* If the input precision differs from the target pointer type
82 precision, first convert the input expression to an integer type of
83 the target precision. Some targets, e.g. VMS, need several pointer
84 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
85 unsigned int pprec
= TYPE_PRECISION (type
);
86 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
90 = maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
,
91 lang_hooks
.types
.type_for_size (pprec
, 0),
94 return maybe_fold_build1_loc (fold_p
, loc
, CONVERT_EXPR
, type
, expr
);
97 error ("cannot convert to a pointer type");
98 return convert_to_pointer_1 (type
, integer_zero_node
, fold_p
);
102 /* Subroutine of the various convert_to_*_maybe_fold routines.
104 If a location wrapper has been folded to a constant (presumably of
105 a different type), re-wrap the new constant with a location wrapper. */
108 preserve_any_location_wrapper (tree result
, tree orig_expr
)
110 if (CONSTANT_CLASS_P (result
) && location_wrapper_p (orig_expr
))
112 if (result
== TREE_OPERAND (orig_expr
, 0))
115 return maybe_wrap_with_location (result
, EXPR_LOCATION (orig_expr
));
121 /* A wrapper around convert_to_pointer_1 that always folds the
125 convert_to_pointer (tree type
, tree expr
)
127 return convert_to_pointer_1 (type
, expr
, true);
130 /* A wrapper around convert_to_pointer_1 that only folds the
131 expression if DOFOLD, or if it is CONSTANT_CLASS_OR_WRAPPER_P. */
134 convert_to_pointer_maybe_fold (tree type
, tree expr
, bool dofold
)
137 = convert_to_pointer_1 (type
, expr
,
138 dofold
|| CONSTANT_CLASS_OR_WRAPPER_P (expr
));
139 return preserve_any_location_wrapper (result
, expr
);
142 /* Convert EXPR to some floating-point type TYPE.
144 EXPR must be float, fixed-point, integer, or enumeral;
145 in other cases error is called. If FOLD_P is true, try to fold
149 convert_to_real_1 (tree type
, tree expr
, bool fold_p
)
151 enum built_in_function fcode
= builtin_mathfn_code (expr
);
152 tree itype
= TREE_TYPE (expr
);
153 location_t loc
= EXPR_LOCATION (expr
);
155 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
157 tree t
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 1), fold_p
);
158 if (t
== TREE_OPERAND (expr
, 1))
160 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
161 TREE_OPERAND (expr
, 0), t
);
164 /* Disable until we figure out how to decide whether the functions are
165 present in runtime. */
166 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
168 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
169 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
173 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
188 /* The above functions may set errno differently with float
189 input or output so this transformation is not safe with
211 /* The above functions are not safe to do this conversion. */
212 if (!flag_unsafe_math_optimizations
)
219 if (call_expr_nargs (expr
) != 1
220 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (expr
, 0))))
223 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
226 /* We have (outertype)sqrt((innertype)x). Choose the wider mode
227 from the both as the safe type for operation. */
228 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
229 newtype
= TREE_TYPE (arg0
);
231 /* We consider to convert
233 (T1) sqrtT2 ((T2) exprT3)
235 (T1) sqrtT4 ((T4) exprT3)
237 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
238 and T4 is NEWTYPE. All those types are of floating-point types.
239 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
240 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
241 T2 and T4. See the following URL for a reference:
242 http://stackoverflow.com/questions/9235456/determining-
243 floating-point-square-root
245 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
246 && !flag_unsafe_math_optimizations
)
248 /* The following conversion is unsafe even the precision condition
251 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
253 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
256 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
257 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
262 /* Be careful about integer to fp conversions.
263 These may overflow still. */
264 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
265 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
266 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
267 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
269 tree fn
= mathfn_built_in (newtype
, fcode
);
272 tree arg
= convert_to_real_1 (newtype
, arg0
, fold_p
);
273 expr
= build_call_expr (fn
, 1, arg
);
284 /* Propagate the cast into the operation. */
285 if (itype
!= type
&& FLOAT_TYPE_P (type
))
286 switch (TREE_CODE (expr
))
288 /* Convert (float)-x into -(float)x. This is safe for
289 round-to-nearest rounding mode when the inner type is float. */
292 if (!flag_rounding_math
293 && FLOAT_TYPE_P (itype
)
294 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
296 tree arg
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 0),
298 return build1 (TREE_CODE (expr
), type
, arg
);
305 switch (TREE_CODE (TREE_TYPE (expr
)))
308 /* Ignore the conversion if we don't need to store intermediate
309 results and neither type is a decimal float. */
310 return build1_loc (loc
,
312 || DECIMAL_FLOAT_TYPE_P (type
)
313 || DECIMAL_FLOAT_TYPE_P (itype
))
314 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
319 return build1 (FLOAT_EXPR
, type
, expr
);
321 case FIXED_POINT_TYPE
:
322 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
325 return convert (type
,
326 maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
327 TREE_TYPE (TREE_TYPE (expr
)),
332 error ("pointer value used where a floating-point was expected");
333 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
336 error ("aggregate value used where a floating-point was expected");
337 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
341 /* A wrapper around convert_to_real_1 that always folds the
345 convert_to_real (tree type
, tree expr
)
347 return convert_to_real_1 (type
, expr
, true);
350 /* A wrapper around convert_to_real_1 that only folds the
351 expression if DOFOLD, or if it is CONSTANT_CLASS_OR_WRAPPER_P. */
354 convert_to_real_maybe_fold (tree type
, tree expr
, bool dofold
)
357 = convert_to_real_1 (type
, expr
,
358 dofold
|| CONSTANT_CLASS_OR_WRAPPER_P (expr
));
359 return preserve_any_location_wrapper (result
, expr
);
362 /* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
366 do_narrow (location_t loc
,
367 enum tree_code ex_form
, tree type
, tree arg0
, tree arg1
,
368 tree expr
, unsigned inprec
, unsigned outprec
, bool dofold
)
370 /* Do the arithmetic in type TYPEX,
371 then convert result to TYPE. */
374 /* Can't do arithmetic in enumeral types
375 so use an integer type that will hold the values. */
376 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
377 typex
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
378 TYPE_UNSIGNED (typex
));
380 /* The type demotion below might cause doing unsigned arithmetic
381 instead of signed, and thus hide overflow bugs. */
382 if ((ex_form
== PLUS_EXPR
|| ex_form
== MINUS_EXPR
)
383 && !TYPE_UNSIGNED (typex
)
384 && sanitize_flags_p (SANITIZE_SI_OVERFLOW
))
387 /* Similarly for multiplication, but in that case it can be
388 problematic even if typex is unsigned type - 0xffff * 0xffff
390 if (ex_form
== MULT_EXPR
391 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
392 && sanitize_flags_p (SANITIZE_SI_OVERFLOW
))
395 /* But now perhaps TYPEX is as wide as INPREC.
396 In that case, do nothing special here.
397 (Otherwise would recurse infinitely in convert. */
398 if (TYPE_PRECISION (typex
) != inprec
)
400 /* Don't do unsigned arithmetic where signed was wanted,
402 Exception: if both of the original operands were
403 unsigned then we can safely do the work as unsigned.
404 Exception: shift operations take their type solely
405 from the first argument.
406 Exception: the LSHIFT_EXPR case above requires that
407 we perform this operation unsigned lest we produce
408 signed-overflow undefinedness.
409 And we may need to do it as unsigned
410 if we truncate to the original size. */
411 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
412 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
413 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
414 || ex_form
== LSHIFT_EXPR
415 || ex_form
== RSHIFT_EXPR
416 || ex_form
== LROTATE_EXPR
417 || ex_form
== RROTATE_EXPR
))
418 || ex_form
== LSHIFT_EXPR
419 /* If we have !flag_wrapv, and either ARG0 or
420 ARG1 is of a signed type, we have to do
421 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
422 type in case the operation in outprec precision
423 could overflow. Otherwise, we would introduce
424 signed-overflow undefinedness. */
425 || ((!(INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
426 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
427 || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
428 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
))))
429 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
431 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
433 && (ex_form
== PLUS_EXPR
434 || ex_form
== MINUS_EXPR
435 || ex_form
== MULT_EXPR
)))
437 if (!TYPE_UNSIGNED (typex
))
438 typex
= unsigned_type_for (typex
);
442 if (TYPE_UNSIGNED (typex
))
443 typex
= signed_type_for (typex
);
445 /* We should do away with all this once we have a proper
446 type promotion/demotion pass, see PR45397. */
447 expr
= maybe_fold_build2_loc (dofold
, loc
, ex_form
, typex
,
448 convert (typex
, arg0
),
449 convert (typex
, arg1
));
450 return convert (type
, expr
);
456 /* Convert EXPR to some integer (or enum) type TYPE.
458 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
459 fixed-point or vector; in other cases error is called.
461 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
463 The result of this is always supposed to be a newly created tree node
464 not in use in any existing structure. */
467 convert_to_integer_1 (tree type
, tree expr
, bool dofold
)
469 enum tree_code ex_form
= TREE_CODE (expr
);
470 tree intype
= TREE_TYPE (expr
);
471 unsigned int inprec
= element_precision (intype
);
472 unsigned int outprec
= element_precision (type
);
473 location_t loc
= EXPR_LOCATION (expr
);
475 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
476 be. Consider `enum E = { a, b = (enum E) 3 };'. */
477 if (!COMPLETE_TYPE_P (type
))
479 error ("conversion to incomplete type");
480 return error_mark_node
;
483 if (ex_form
== COMPOUND_EXPR
)
485 tree t
= convert_to_integer_1 (type
, TREE_OPERAND (expr
, 1), dofold
);
486 if (t
== TREE_OPERAND (expr
, 1))
488 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
489 TREE_OPERAND (expr
, 0), t
);
492 /* Convert e.g. (long)round(d) -> lround(d). */
493 /* If we're converting to char, we may encounter differing behavior
494 between converting from double->char vs double->long->char.
495 We're in "undefined" territory but we prefer to be conservative,
496 so only proceed in "unsafe" math mode. */
498 && (flag_unsafe_math_optimizations
499 || (long_integer_type_node
500 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
502 tree s_expr
= strip_float_extensions (expr
);
503 tree s_intype
= TREE_TYPE (s_expr
);
504 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
509 CASE_FLT_FN (BUILT_IN_CEIL
):
510 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
511 /* Only convert in ISO C99 mode. */
512 if (!targetm
.libc_has_function (function_c99_misc
, intype
))
514 if (outprec
< TYPE_PRECISION (integer_type_node
)
515 || (outprec
== TYPE_PRECISION (integer_type_node
)
516 && !TYPE_UNSIGNED (type
)))
517 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
518 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
519 && !TYPE_UNSIGNED (type
))
520 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
521 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
522 && !TYPE_UNSIGNED (type
))
523 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
526 CASE_FLT_FN (BUILT_IN_FLOOR
):
527 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
528 /* Only convert in ISO C99 mode. */
529 if (!targetm
.libc_has_function (function_c99_misc
, intype
))
531 if (outprec
< TYPE_PRECISION (integer_type_node
)
532 || (outprec
== TYPE_PRECISION (integer_type_node
)
533 && !TYPE_UNSIGNED (type
)))
534 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
535 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
536 && !TYPE_UNSIGNED (type
))
537 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
538 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
539 && !TYPE_UNSIGNED (type
))
540 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
543 CASE_FLT_FN (BUILT_IN_ROUND
):
544 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
545 /* Only convert in ISO C99 mode and with -fno-math-errno. */
546 if (!targetm
.libc_has_function (function_c99_misc
, intype
)
549 if (outprec
< TYPE_PRECISION (integer_type_node
)
550 || (outprec
== TYPE_PRECISION (integer_type_node
)
551 && !TYPE_UNSIGNED (type
)))
552 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
553 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
554 && !TYPE_UNSIGNED (type
))
555 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
556 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
557 && !TYPE_UNSIGNED (type
))
558 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
561 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
562 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
563 /* Only convert nearbyint* if we can ignore math exceptions. */
564 if (flag_trapping_math
)
567 CASE_FLT_FN (BUILT_IN_RINT
):
568 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
569 /* Only convert in ISO C99 mode and with -fno-math-errno. */
570 if (!targetm
.libc_has_function (function_c99_misc
, intype
)
573 if (outprec
< TYPE_PRECISION (integer_type_node
)
574 || (outprec
== TYPE_PRECISION (integer_type_node
)
575 && !TYPE_UNSIGNED (type
)))
576 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
577 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
578 && !TYPE_UNSIGNED (type
))
579 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
580 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
581 && !TYPE_UNSIGNED (type
))
582 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
585 CASE_FLT_FN (BUILT_IN_TRUNC
):
586 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
587 if (call_expr_nargs (s_expr
) != 1
588 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr
, 0))))
590 return convert_to_integer_1 (type
, CALL_EXPR_ARG (s_expr
, 0),
598 && call_expr_nargs (s_expr
) == 1
599 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr
, 0))))
601 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
602 return convert_to_integer_1 (type
, newexpr
, dofold
);
606 /* Convert (int)logb(d) -> ilogb(d). */
608 && flag_unsafe_math_optimizations
609 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
611 && (outprec
> TYPE_PRECISION (integer_type_node
)
612 || (outprec
== TYPE_PRECISION (integer_type_node
)
613 && !TYPE_UNSIGNED (type
))))
615 tree s_expr
= strip_float_extensions (expr
);
616 tree s_intype
= TREE_TYPE (s_expr
);
617 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
622 CASE_FLT_FN (BUILT_IN_LOGB
):
623 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
631 && call_expr_nargs (s_expr
) == 1
632 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr
, 0))))
634 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
635 return convert_to_integer_1 (type
, newexpr
, dofold
);
639 switch (TREE_CODE (intype
))
643 if (integer_zerop (expr
)
644 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (expr
)))
645 return build_int_cst (type
, 0);
647 /* Convert to an unsigned integer of the correct width first, and from
648 there widen/truncate to the required type. Some targets support the
649 coexistence of multiple valid pointer sizes, so fetch the one we need
652 return build1 (CONVERT_EXPR
, type
, expr
);
653 expr
= fold_build1 (CONVERT_EXPR
,
654 lang_hooks
.types
.type_for_size
655 (TYPE_PRECISION (intype
), 0),
657 return fold_convert (type
, expr
);
663 /* If this is a logical operation, which just returns 0 or 1, we can
664 change the type of the expression. */
666 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
668 expr
= copy_node (expr
);
669 TREE_TYPE (expr
) = type
;
673 /* If we are widening the type, put in an explicit conversion.
674 Similarly if we are not changing the width. After this, we know
675 we are truncating EXPR. */
677 else if (outprec
>= inprec
)
681 /* If the precision of the EXPR's type is K bits and the
682 destination mode has more bits, and the sign is changing,
683 it is not safe to use a NOP_EXPR. For example, suppose
684 that EXPR's type is a 3-bit unsigned integer type, the
685 TYPE is a 3-bit signed integer type, and the machine mode
686 for the types is 8-bit QImode. In that case, the
687 conversion necessitates an explicit sign-extension. In
688 the signed-to-unsigned case the high-order bits have to
690 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
691 && !type_has_mode_precision_p (TREE_TYPE (expr
)))
696 return maybe_fold_build1_loc (dofold
, loc
, code
, type
, expr
);
699 /* If TYPE is an enumeral type or a type with a precision less
700 than the number of bits in its mode, do the conversion to the
701 type corresponding to its mode, then do a nop conversion
703 else if (TREE_CODE (type
) == ENUMERAL_TYPE
704 || maybe_ne (outprec
, GET_MODE_PRECISION (TYPE_MODE (type
))))
707 = convert_to_integer_1 (lang_hooks
.types
.type_for_mode
708 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
710 return maybe_fold_build1_loc (dofold
, loc
, NOP_EXPR
, type
, expr
);
713 /* Here detect when we can distribute the truncation down past some
714 arithmetic. For example, if adding two longs and converting to an
715 int, we can equally well convert both to ints and then add.
716 For the operations handled here, such truncation distribution
718 It is desirable in these cases:
719 1) when truncating down to full-word from a larger size
720 2) when truncating takes no work.
721 3) when at least one operand of the arithmetic has been extended
722 (as by C's default conversions). In this case we need two conversions
723 if we do the arithmetic as already requested, so we might as well
724 truncate both and then combine. Perhaps that way we need only one.
726 Note that in general we cannot do the arithmetic in a type
727 shorter than the desired result of conversion, even if the operands
728 are both extended from a shorter type, because they might overflow
729 if combined in that type. The exceptions to this--the times when
730 two narrow values can be combined in their narrow type even to
731 make a wider result--are handled by "shorten" in build_binary_op. */
737 /* We can pass truncation down through right shifting
738 when the shift count is a nonpositive constant. */
739 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
740 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
745 /* We can pass truncation down through left shifting
746 when the shift count is a nonnegative constant and
747 the target type is unsigned. */
748 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
749 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
750 && TYPE_UNSIGNED (type
)
751 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
753 /* If shift count is less than the width of the truncated type,
755 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
756 /* In this case, shifting is like multiplication. */
760 /* If it is >= that width, result is zero.
761 Handling this with trunc1 would give the wrong result:
762 (int) ((long long) a << 32) is well defined (as 0)
763 but (int) a << 32 is undefined and would get a
766 tree t
= build_int_cst (type
, 0);
768 /* If the original expression had side-effects, we must
770 if (TREE_SIDE_EFFECTS (expr
))
771 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
780 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), NULL_TREE
);
781 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), NULL_TREE
);
783 /* Don't distribute unless the output precision is at least as
784 big as the actual inputs and it has the same signedness. */
785 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
786 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
787 /* If signedness of arg0 and arg1 don't match,
788 we can't necessarily find a type to compare them in. */
789 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
790 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
791 /* Do not change the sign of the division. */
792 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
793 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
794 /* Either require unsigned division or a division by
795 a constant that is not -1. */
796 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
797 || (TREE_CODE (arg1
) == INTEGER_CST
798 && !integer_all_onesp (arg1
))))
800 tree tem
= do_narrow (loc
, ex_form
, type
, arg0
, arg1
,
801 expr
, inprec
, outprec
, dofold
);
812 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
813 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
815 /* Don't distribute unless the output precision is at least as
816 big as the actual inputs. Otherwise, the comparison of the
817 truncated values will be wrong. */
818 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
819 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
820 /* If signedness of arg0 and arg1 don't match,
821 we can't necessarily find a type to compare them in. */
822 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
823 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
835 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
836 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
838 /* Do not try to narrow operands of pointer subtraction;
839 that will interfere with other folding. */
840 if (ex_form
== MINUS_EXPR
841 && CONVERT_EXPR_P (arg0
)
842 && CONVERT_EXPR_P (arg1
)
843 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
844 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
847 tree tem
= do_narrow (loc
, ex_form
, type
, arg0
, arg1
,
848 expr
, inprec
, outprec
, dofold
);
855 /* Using unsigned arithmetic for signed types may hide overflow
857 if (!TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (expr
, 0)))
858 && sanitize_flags_p (SANITIZE_SI_OVERFLOW
))
862 /* This is not correct for ABS_EXPR,
863 since we must test the sign before truncation. */
865 /* Do the arithmetic in type TYPEX,
866 then convert result to TYPE. */
869 /* Can't do arithmetic in enumeral types
870 so use an integer type that will hold the values. */
871 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
873 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
874 TYPE_UNSIGNED (typex
));
876 if (!TYPE_UNSIGNED (typex
))
877 typex
= unsigned_type_for (typex
);
878 return convert (type
,
879 fold_build1 (ex_form
, typex
,
881 TREE_OPERAND (expr
, 0))));
886 tree argtype
= TREE_TYPE (TREE_OPERAND (expr
, 0));
887 /* Don't introduce a "can't convert between vector values
888 of different size" error. */
889 if (TREE_CODE (argtype
) == VECTOR_TYPE
890 && maybe_ne (GET_MODE_SIZE (TYPE_MODE (argtype
)),
891 GET_MODE_SIZE (TYPE_MODE (type
))))
894 /* If truncating after truncating, might as well do all at once.
895 If truncating after extending, we may get rid of wasted work. */
896 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
899 /* It is sometimes worthwhile to push the narrowing down through
900 the conditional and never loses. A COND_EXPR may have a throw
901 as one operand, which then has void type. Just leave void
902 operands as they are. */
904 fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
905 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
906 ? TREE_OPERAND (expr
, 1)
907 : convert (type
, TREE_OPERAND (expr
, 1)),
908 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
909 ? TREE_OPERAND (expr
, 2)
910 : convert (type
, TREE_OPERAND (expr
, 2)));
916 /* When parsing long initializers, we might end up with a lot of casts.
918 if (TREE_CODE (tree_strip_any_location_wrapper (expr
)) == INTEGER_CST
)
919 return fold_convert (type
, expr
);
920 return build1 (CONVERT_EXPR
, type
, expr
);
923 if (sanitize_flags_p (SANITIZE_FLOAT_CAST
)
924 && current_function_decl
!= NULL_TREE
)
926 expr
= save_expr (expr
);
927 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
);
928 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
929 if (check
== NULL_TREE
)
931 return maybe_fold_build2_loc (dofold
, loc
, COMPOUND_EXPR
,
932 TREE_TYPE (expr
), check
, expr
);
935 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
937 case FIXED_POINT_TYPE
:
938 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
941 expr
= maybe_fold_build1_loc (dofold
, loc
, REALPART_EXPR
,
942 TREE_TYPE (TREE_TYPE (expr
)), expr
);
943 return convert (type
, expr
);
946 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
948 error ("cannot convert a vector of type %qT"
949 " to type %qT which has different size",
950 TREE_TYPE (expr
), type
);
951 return error_mark_node
;
953 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
956 error ("aggregate value used where an integer was expected");
957 return convert (type
, integer_zero_node
);
961 /* Convert EXPR to some integer (or enum) type TYPE.
963 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
964 fixed-point or vector; in other cases error is called.
966 The result of this is always supposed to be a newly created tree node
967 not in use in any existing structure. */
970 convert_to_integer (tree type
, tree expr
)
972 return convert_to_integer_1 (type
, expr
, true);
975 /* A wrapper around convert_to_complex_1 that only folds the
976 expression if DOFOLD, or if it is CONSTANT_CLASS_OR_WRAPPER_P. */
979 convert_to_integer_maybe_fold (tree type
, tree expr
, bool dofold
)
982 = convert_to_integer_1 (type
, expr
,
983 dofold
|| CONSTANT_CLASS_OR_WRAPPER_P (expr
));
984 return preserve_any_location_wrapper (result
, expr
);
987 /* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
988 true, try to fold the expression. */
991 convert_to_complex_1 (tree type
, tree expr
, bool fold_p
)
993 location_t loc
= EXPR_LOCATION (expr
);
994 tree subtype
= TREE_TYPE (type
);
996 switch (TREE_CODE (TREE_TYPE (expr
)))
999 case FIXED_POINT_TYPE
:
1003 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
1004 convert (subtype
, integer_zero_node
));
1008 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
1010 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
1012 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
1014 tree t
= convert_to_complex_1 (type
, TREE_OPERAND (expr
, 1),
1016 if (t
== TREE_OPERAND (expr
, 1))
1018 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
1019 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
1021 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
1022 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1024 TREE_OPERAND (expr
, 0)),
1026 TREE_OPERAND (expr
, 1)));
1029 expr
= save_expr (expr
);
1030 tree realp
= maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
1031 TREE_TYPE (TREE_TYPE (expr
)),
1033 tree imagp
= maybe_fold_build1_loc (fold_p
, loc
, IMAGPART_EXPR
,
1034 TREE_TYPE (TREE_TYPE (expr
)),
1036 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1037 convert (subtype
, realp
),
1038 convert (subtype
, imagp
));
1043 case REFERENCE_TYPE
:
1044 error ("pointer value used where a complex was expected");
1045 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1048 error ("aggregate value used where a complex was expected");
1049 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1053 /* A wrapper around convert_to_complex_1 that always folds the
1057 convert_to_complex (tree type
, tree expr
)
1059 return convert_to_complex_1 (type
, expr
, true);
1062 /* A wrapper around convert_to_complex_1 that only folds the
1063 expression if DOFOLD, or if it is CONSTANT_CLASS_OR_WRAPPER_P. */
1066 convert_to_complex_maybe_fold (tree type
, tree expr
, bool dofold
)
1069 = convert_to_complex_1 (type
, expr
,
1070 dofold
|| CONSTANT_CLASS_OR_WRAPPER_P (expr
));
1071 return preserve_any_location_wrapper (result
, expr
);
1074 /* Convert EXPR to the vector type TYPE in the usual ways. */
1077 convert_to_vector (tree type
, tree expr
)
1079 switch (TREE_CODE (TREE_TYPE (expr
)))
1083 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1085 error ("cannot convert a value of type %qT"
1086 " to vector type %qT which has different size",
1087 TREE_TYPE (expr
), type
);
1088 return error_mark_node
;
1090 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1093 error ("cannot convert value to a vector");
1094 return error_mark_node
;
1098 /* Convert EXPR to some fixed-point type TYPE.
1100 EXPR must be fixed-point, float, integer, or enumeral;
1101 in other cases error is called. */
1104 convert_to_fixed (tree type
, tree expr
)
1106 if (integer_zerop (expr
))
1108 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1109 return fixed_zero_node
;
1111 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1113 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1114 return fixed_one_node
;
1117 switch (TREE_CODE (TREE_TYPE (expr
)))
1119 case FIXED_POINT_TYPE
:
1124 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1127 return convert (type
,
1128 fold_build1 (REALPART_EXPR
,
1129 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1132 error ("aggregate value used where a fixed-point was expected");
1133 return error_mark_node
;
1139 namespace selftest
{
1141 /* Selftests for conversions. */
1144 test_convert_to_integer_maybe_fold (tree orig_type
, tree new_type
)
1146 /* Calling convert_to_integer_maybe_fold on an INTEGER_CST. */
1148 tree orig_cst
= build_int_cst (orig_type
, 42);
1150 /* Verify that convert_to_integer_maybe_fold on a constant returns a new
1151 constant of the new type, unless the types are the same, in which
1152 case verify it's a no-op. */
1154 tree result
= convert_to_integer_maybe_fold (new_type
,
1156 if (orig_type
!= new_type
)
1158 ASSERT_EQ (TREE_TYPE (result
), new_type
);
1159 ASSERT_EQ (TREE_CODE (result
), INTEGER_CST
);
1162 ASSERT_EQ (result
, orig_cst
);
1165 /* Calling convert_to_integer_maybe_fold on a location wrapper around
1168 Verify that convert_to_integer_maybe_fold on a location wrapper
1169 around a constant returns a new location wrapper around an equivalent
1170 constant, both of the new type, unless the types are the same,
1171 in which case the original wrapper should be returned. */
1173 const location_t loc
= BUILTINS_LOCATION
;
1174 tree wrapped_orig_cst
= maybe_wrap_with_location (orig_cst
, loc
);
1176 = convert_to_integer_maybe_fold (new_type
, wrapped_orig_cst
, false);
1177 ASSERT_EQ (TREE_TYPE (result
), new_type
);
1178 ASSERT_EQ (EXPR_LOCATION (result
), loc
);
1179 ASSERT_TRUE (location_wrapper_p (result
));
1180 ASSERT_EQ (TREE_TYPE (TREE_OPERAND (result
, 0)), new_type
);
1181 ASSERT_EQ (TREE_CODE (TREE_OPERAND (result
, 0)), INTEGER_CST
);
1183 if (orig_type
== new_type
)
1184 ASSERT_EQ (result
, wrapped_orig_cst
);
1188 /* Verify that convert_to_integer_maybe_fold preserves locations. */
1191 test_convert_to_integer_maybe_fold ()
1194 test_convert_to_integer_maybe_fold (char_type_node
, long_integer_type_node
);
1197 test_convert_to_integer_maybe_fold (char_type_node
, char_type_node
);
1200 test_convert_to_integer_maybe_fold (char_type_node
, long_integer_type_node
);
1203 test_convert_to_integer_maybe_fold (long_integer_type_node
,
1204 long_integer_type_node
);
1207 /* Run all of the selftests within this file. */
1212 test_convert_to_integer_maybe_fold ();
1215 } // namespace selftest
1217 #endif /* CHECKING_P */