1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2017 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"
38 #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
39 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
40 : build1_loc (LOC, CODE, TYPE, EXPR))
41 #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
42 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
43 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
45 /* Convert EXPR to some pointer or reference type TYPE.
46 EXPR must be pointer, reference, integer, enumeral, or literal zero;
47 in other cases error is called. If FOLD_P is true, try to fold the
51 convert_to_pointer_1 (tree type
, tree expr
, bool fold_p
)
53 location_t loc
= EXPR_LOCATION (expr
);
54 if (TREE_TYPE (expr
) == type
)
57 switch (TREE_CODE (TREE_TYPE (expr
)))
62 /* If the pointers point to different address spaces, conversion needs
63 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
64 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
65 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
68 return maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
, type
, expr
);
70 return maybe_fold_build1_loc (fold_p
, loc
, ADDR_SPACE_CONVERT_EXPR
,
78 /* If the input precision differs from the target pointer type
79 precision, first convert the input expression to an integer type of
80 the target precision. Some targets, e.g. VMS, need several pointer
81 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
82 unsigned int pprec
= TYPE_PRECISION (type
);
83 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
87 = maybe_fold_build1_loc (fold_p
, loc
, NOP_EXPR
,
88 lang_hooks
.types
.type_for_size (pprec
, 0),
91 return maybe_fold_build1_loc (fold_p
, loc
, CONVERT_EXPR
, type
, expr
);
94 error ("cannot convert to a pointer type");
95 return convert_to_pointer_1 (type
, integer_zero_node
, fold_p
);
99 /* A wrapper around convert_to_pointer_1 that always folds the
103 convert_to_pointer (tree type
, tree expr
)
105 return convert_to_pointer_1 (type
, expr
, true);
108 /* A wrapper around convert_to_pointer_1 that only folds the
109 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
112 convert_to_pointer_maybe_fold (tree type
, tree expr
, bool dofold
)
114 return convert_to_pointer_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
117 /* Convert EXPR to some floating-point type TYPE.
119 EXPR must be float, fixed-point, integer, or enumeral;
120 in other cases error is called. If FOLD_P is true, try to fold
124 convert_to_real_1 (tree type
, tree expr
, bool fold_p
)
126 enum built_in_function fcode
= builtin_mathfn_code (expr
);
127 tree itype
= TREE_TYPE (expr
);
128 location_t loc
= EXPR_LOCATION (expr
);
130 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
132 tree t
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 1), fold_p
);
133 if (t
== TREE_OPERAND (expr
, 1))
135 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
136 TREE_OPERAND (expr
, 0), t
);
139 /* Disable until we figure out how to decide whether the functions are
140 present in runtime. */
141 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
143 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
144 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
148 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
163 /* The above functions may set errno differently with float
164 input or output so this transformation is not safe with
186 /* The above functions are not safe to do this conversion. */
187 if (!flag_unsafe_math_optimizations
)
195 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
198 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
199 the both as the safe type for operation. */
200 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
201 newtype
= TREE_TYPE (arg0
);
203 /* We consider to convert
205 (T1) sqrtT2 ((T2) exprT3)
207 (T1) sqrtT4 ((T4) exprT3)
209 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
210 and T4 is NEWTYPE. All those types are of floating point types.
211 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
212 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
213 T2 and T4. See the following URL for a reference:
214 http://stackoverflow.com/questions/9235456/determining-
215 floating-point-square-root
217 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
218 && !flag_unsafe_math_optimizations
)
220 /* The following conversion is unsafe even the precision condition
223 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
225 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
228 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
229 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
234 /* Be careful about integer to fp conversions.
235 These may overflow still. */
236 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
237 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
238 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
239 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
241 tree fn
= mathfn_built_in (newtype
, fcode
);
244 tree arg
= convert_to_real_1 (newtype
, arg0
, fold_p
);
245 expr
= build_call_expr (fn
, 1, arg
);
256 /* Propagate the cast into the operation. */
257 if (itype
!= type
&& FLOAT_TYPE_P (type
))
258 switch (TREE_CODE (expr
))
260 /* Convert (float)-x into -(float)x. This is safe for
261 round-to-nearest rounding mode when the inner type is float. */
264 if (!flag_rounding_math
265 && FLOAT_TYPE_P (itype
)
266 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
268 tree arg
= convert_to_real_1 (type
, TREE_OPERAND (expr
, 0),
270 return build1 (TREE_CODE (expr
), type
, arg
);
273 /* Convert (outertype)((innertype0)a+(innertype1)b)
274 into ((newtype)a+(newtype)b) where newtype
275 is the widest mode from all of these. */
281 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
282 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
284 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
285 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
286 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
290 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
291 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
292 || TYPE_MODE (type
) == SDmode
)
293 newtype
= dfloat32_type_node
;
294 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
295 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
296 || TYPE_MODE (type
) == DDmode
)
297 newtype
= dfloat64_type_node
;
298 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
299 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
300 || TYPE_MODE (type
) == TDmode
)
301 newtype
= dfloat128_type_node
;
302 if (newtype
== dfloat32_type_node
303 || newtype
== dfloat64_type_node
304 || newtype
== dfloat128_type_node
)
306 expr
= build2 (TREE_CODE (expr
), newtype
,
307 convert_to_real_1 (newtype
, arg0
,
309 convert_to_real_1 (newtype
, arg1
,
316 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
317 newtype
= TREE_TYPE (arg0
);
318 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
319 newtype
= TREE_TYPE (arg1
);
320 /* Sometimes this transformation is safe (cannot
321 change results through affecting double rounding
322 cases) and sometimes it is not. If NEWTYPE is
323 wider than TYPE, e.g. (float)((long double)double
324 + (long double)double) converted to
325 (float)(double + double), the transformation is
326 unsafe regardless of the details of the types
327 involved; double rounding can arise if the result
328 of NEWTYPE arithmetic is a NEWTYPE value half way
329 between two representable TYPE values but the
330 exact value is sufficiently different (in the
331 right direction) for this difference to be
332 visible in ITYPE arithmetic. If NEWTYPE is the
333 same as TYPE, however, the transformation may be
334 safe depending on the types involved: it is safe
335 if the ITYPE has strictly more than twice as many
336 mantissa bits as TYPE, can represent infinities
337 and NaNs if the TYPE can, and has sufficient
338 exponent range for the product or ratio of two
339 values representable in the TYPE to be within the
340 range of normal values of ITYPE. */
341 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
342 && (flag_unsafe_math_optimizations
343 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
344 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
346 && !excess_precision_type (newtype
))))
348 expr
= build2 (TREE_CODE (expr
), newtype
,
349 convert_to_real_1 (newtype
, arg0
,
351 convert_to_real_1 (newtype
, arg1
,
363 switch (TREE_CODE (TREE_TYPE (expr
)))
366 /* Ignore the conversion if we don't need to store intermediate
367 results and neither type is a decimal float. */
368 return build1_loc (loc
,
370 || DECIMAL_FLOAT_TYPE_P (type
)
371 || DECIMAL_FLOAT_TYPE_P (itype
))
372 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
377 return build1 (FLOAT_EXPR
, type
, expr
);
379 case FIXED_POINT_TYPE
:
380 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
383 return convert (type
,
384 maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
385 TREE_TYPE (TREE_TYPE (expr
)),
390 error ("pointer value used where a floating point value was expected");
391 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
394 error ("aggregate value used where a float was expected");
395 return convert_to_real_1 (type
, integer_zero_node
, fold_p
);
399 /* A wrapper around convert_to_real_1 that always folds the
403 convert_to_real (tree type
, tree expr
)
405 return convert_to_real_1 (type
, expr
, true);
408 /* A wrapper around convert_to_real_1 that only folds the
409 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
412 convert_to_real_maybe_fold (tree type
, tree expr
, bool dofold
)
414 return convert_to_real_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
417 /* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
421 do_narrow (location_t loc
,
422 enum tree_code ex_form
, tree type
, tree arg0
, tree arg1
,
423 tree expr
, unsigned inprec
, unsigned outprec
, bool dofold
)
425 /* Do the arithmetic in type TYPEX,
426 then convert result to TYPE. */
429 /* Can't do arithmetic in enumeral types
430 so use an integer type that will hold the values. */
431 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
432 typex
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
433 TYPE_UNSIGNED (typex
));
435 /* But now perhaps TYPEX is as wide as INPREC.
436 In that case, do nothing special here.
437 (Otherwise would recurse infinitely in convert. */
438 if (TYPE_PRECISION (typex
) != inprec
)
440 /* Don't do unsigned arithmetic where signed was wanted,
442 Exception: if both of the original operands were
443 unsigned then we can safely do the work as unsigned.
444 Exception: shift operations take their type solely
445 from the first argument.
446 Exception: the LSHIFT_EXPR case above requires that
447 we perform this operation unsigned lest we produce
448 signed-overflow undefinedness.
449 And we may need to do it as unsigned
450 if we truncate to the original size. */
451 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
452 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
453 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
454 || ex_form
== LSHIFT_EXPR
455 || ex_form
== RSHIFT_EXPR
456 || ex_form
== LROTATE_EXPR
457 || ex_form
== RROTATE_EXPR
))
458 || ex_form
== LSHIFT_EXPR
459 /* If we have !flag_wrapv, and either ARG0 or
460 ARG1 is of a signed type, we have to do
461 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
462 type in case the operation in outprec precision
463 could overflow. Otherwise, we would introduce
464 signed-overflow undefinedness. */
465 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
466 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
467 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
469 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
471 && (ex_form
== PLUS_EXPR
472 || ex_form
== MINUS_EXPR
473 || ex_form
== MULT_EXPR
)))
475 if (!TYPE_UNSIGNED (typex
))
476 typex
= unsigned_type_for (typex
);
480 if (TYPE_UNSIGNED (typex
))
481 typex
= signed_type_for (typex
);
483 /* We should do away with all this once we have a proper
484 type promotion/demotion pass, see PR45397. */
485 expr
= maybe_fold_build2_loc (dofold
, loc
, ex_form
, typex
,
486 convert (typex
, arg0
),
487 convert (typex
, arg1
));
488 return convert (type
, expr
);
494 /* Convert EXPR to some integer (or enum) type TYPE.
496 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
497 fixed-point or vector; in other cases error is called.
499 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
501 The result of this is always supposed to be a newly created tree node
502 not in use in any existing structure. */
505 convert_to_integer_1 (tree type
, tree expr
, bool dofold
)
507 enum tree_code ex_form
= TREE_CODE (expr
);
508 tree intype
= TREE_TYPE (expr
);
509 unsigned int inprec
= element_precision (intype
);
510 unsigned int outprec
= element_precision (type
);
511 location_t loc
= EXPR_LOCATION (expr
);
513 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
514 be. Consider `enum E = { a, b = (enum E) 3 };'. */
515 if (!COMPLETE_TYPE_P (type
))
517 error ("conversion to incomplete type");
518 return error_mark_node
;
521 if (ex_form
== COMPOUND_EXPR
)
523 tree t
= convert_to_integer_1 (type
, TREE_OPERAND (expr
, 1), dofold
);
524 if (t
== TREE_OPERAND (expr
, 1))
526 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
527 TREE_OPERAND (expr
, 0), t
);
530 /* Convert e.g. (long)round(d) -> lround(d). */
531 /* If we're converting to char, we may encounter differing behavior
532 between converting from double->char vs double->long->char.
533 We're in "undefined" territory but we prefer to be conservative,
534 so only proceed in "unsafe" math mode. */
536 && (flag_unsafe_math_optimizations
537 || (long_integer_type_node
538 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
540 tree s_expr
= strip_float_extensions (expr
);
541 tree s_intype
= TREE_TYPE (s_expr
);
542 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
547 CASE_FLT_FN (BUILT_IN_CEIL
):
548 /* Only convert in ISO C99 mode. */
549 if (!targetm
.libc_has_function (function_c99_misc
))
551 if (outprec
< TYPE_PRECISION (integer_type_node
)
552 || (outprec
== TYPE_PRECISION (integer_type_node
)
553 && !TYPE_UNSIGNED (type
)))
554 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
555 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
556 && !TYPE_UNSIGNED (type
))
557 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
558 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
559 && !TYPE_UNSIGNED (type
))
560 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
563 CASE_FLT_FN (BUILT_IN_FLOOR
):
564 /* Only convert in ISO C99 mode. */
565 if (!targetm
.libc_has_function (function_c99_misc
))
567 if (outprec
< TYPE_PRECISION (integer_type_node
)
568 || (outprec
== TYPE_PRECISION (integer_type_node
)
569 && !TYPE_UNSIGNED (type
)))
570 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
571 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
572 && !TYPE_UNSIGNED (type
))
573 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
574 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
575 && !TYPE_UNSIGNED (type
))
576 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
579 CASE_FLT_FN (BUILT_IN_ROUND
):
580 /* Only convert in ISO C99 mode and with -fno-math-errno. */
581 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
583 if (outprec
< TYPE_PRECISION (integer_type_node
)
584 || (outprec
== TYPE_PRECISION (integer_type_node
)
585 && !TYPE_UNSIGNED (type
)))
586 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
587 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
588 && !TYPE_UNSIGNED (type
))
589 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
590 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
591 && !TYPE_UNSIGNED (type
))
592 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
595 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
596 /* Only convert nearbyint* if we can ignore math exceptions. */
597 if (flag_trapping_math
)
600 CASE_FLT_FN (BUILT_IN_RINT
):
601 /* Only convert in ISO C99 mode and with -fno-math-errno. */
602 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
604 if (outprec
< TYPE_PRECISION (integer_type_node
)
605 || (outprec
== TYPE_PRECISION (integer_type_node
)
606 && !TYPE_UNSIGNED (type
)))
607 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
608 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
609 && !TYPE_UNSIGNED (type
))
610 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
611 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
612 && !TYPE_UNSIGNED (type
))
613 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
616 CASE_FLT_FN (BUILT_IN_TRUNC
):
617 return convert_to_integer_1 (type
, CALL_EXPR_ARG (s_expr
, 0), dofold
);
625 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
626 return convert_to_integer_1 (type
, newexpr
, dofold
);
630 /* Convert (int)logb(d) -> ilogb(d). */
632 && flag_unsafe_math_optimizations
633 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
635 && (outprec
> TYPE_PRECISION (integer_type_node
)
636 || (outprec
== TYPE_PRECISION (integer_type_node
)
637 && !TYPE_UNSIGNED (type
))))
639 tree s_expr
= strip_float_extensions (expr
);
640 tree s_intype
= TREE_TYPE (s_expr
);
641 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
646 CASE_FLT_FN (BUILT_IN_LOGB
):
647 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
656 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
657 return convert_to_integer_1 (type
, newexpr
, dofold
);
661 switch (TREE_CODE (intype
))
665 if (integer_zerop (expr
))
666 return build_int_cst (type
, 0);
668 /* Convert to an unsigned integer of the correct width first, and from
669 there widen/truncate to the required type. Some targets support the
670 coexistence of multiple valid pointer sizes, so fetch the one we need
673 return build1 (CONVERT_EXPR
, type
, expr
);
674 expr
= fold_build1 (CONVERT_EXPR
,
675 lang_hooks
.types
.type_for_size
676 (TYPE_PRECISION (intype
), 0),
678 return fold_convert (type
, expr
);
684 /* If this is a logical operation, which just returns 0 or 1, we can
685 change the type of the expression. */
687 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
689 expr
= copy_node (expr
);
690 TREE_TYPE (expr
) = type
;
694 /* If we are widening the type, put in an explicit conversion.
695 Similarly if we are not changing the width. After this, we know
696 we are truncating EXPR. */
698 else if (outprec
>= inprec
)
702 /* If the precision of the EXPR's type is K bits and the
703 destination mode has more bits, and the sign is changing,
704 it is not safe to use a NOP_EXPR. For example, suppose
705 that EXPR's type is a 3-bit unsigned integer type, the
706 TYPE is a 3-bit signed integer type, and the machine mode
707 for the types is 8-bit QImode. In that case, the
708 conversion necessitates an explicit sign-extension. In
709 the signed-to-unsigned case the high-order bits have to
711 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
712 && (TYPE_PRECISION (TREE_TYPE (expr
))
713 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
718 return maybe_fold_build1_loc (dofold
, loc
, code
, type
, expr
);
721 /* If TYPE is an enumeral type or a type with a precision less
722 than the number of bits in its mode, do the conversion to the
723 type corresponding to its mode, then do a nop conversion
725 else if (TREE_CODE (type
) == ENUMERAL_TYPE
726 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
728 expr
= convert (lang_hooks
.types
.type_for_mode
729 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)), expr
);
730 return maybe_fold_build1_loc (dofold
, loc
, NOP_EXPR
, type
, expr
);
733 /* Here detect when we can distribute the truncation down past some
734 arithmetic. For example, if adding two longs and converting to an
735 int, we can equally well convert both to ints and then add.
736 For the operations handled here, such truncation distribution
738 It is desirable in these cases:
739 1) when truncating down to full-word from a larger size
740 2) when truncating takes no work.
741 3) when at least one operand of the arithmetic has been extended
742 (as by C's default conversions). In this case we need two conversions
743 if we do the arithmetic as already requested, so we might as well
744 truncate both and then combine. Perhaps that way we need only one.
746 Note that in general we cannot do the arithmetic in a type
747 shorter than the desired result of conversion, even if the operands
748 are both extended from a shorter type, because they might overflow
749 if combined in that type. The exceptions to this--the times when
750 two narrow values can be combined in their narrow type even to
751 make a wider result--are handled by "shorten" in build_binary_op. */
757 /* We can pass truncation down through right shifting
758 when the shift count is a nonpositive constant. */
759 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
760 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
765 /* We can pass truncation down through left shifting
766 when the shift count is a nonnegative constant and
767 the target type is unsigned. */
768 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
769 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
770 && TYPE_UNSIGNED (type
)
771 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
773 /* If shift count is less than the width of the truncated type,
775 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
776 /* In this case, shifting is like multiplication. */
780 /* If it is >= that width, result is zero.
781 Handling this with trunc1 would give the wrong result:
782 (int) ((long long) a << 32) is well defined (as 0)
783 but (int) a << 32 is undefined and would get a
786 tree t
= build_int_cst (type
, 0);
788 /* If the original expression had side-effects, we must
790 if (TREE_SIDE_EFFECTS (expr
))
791 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
800 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), NULL_TREE
);
801 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), NULL_TREE
);
803 /* Don't distribute unless the output precision is at least as
804 big as the actual inputs and it has the same signedness. */
805 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
806 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
807 /* If signedness of arg0 and arg1 don't match,
808 we can't necessarily find a type to compare them in. */
809 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
810 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
811 /* Do not change the sign of the division. */
812 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
813 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
814 /* Either require unsigned division or a division by
815 a constant that is not -1. */
816 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
817 || (TREE_CODE (arg1
) == INTEGER_CST
818 && !integer_all_onesp (arg1
))))
820 tree tem
= do_narrow (loc
, ex_form
, type
, arg0
, arg1
,
821 expr
, inprec
, outprec
, dofold
);
832 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
833 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
835 /* Don't distribute unless the output precision is at least as
836 big as the actual inputs. Otherwise, the comparison of the
837 truncated values will be wrong. */
838 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
839 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
840 /* If signedness of arg0 and arg1 don't match,
841 we can't necessarily find a type to compare them in. */
842 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
843 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
855 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
856 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
858 /* Do not try to narrow operands of pointer subtraction;
859 that will interfere with other folding. */
860 if (ex_form
== MINUS_EXPR
861 && CONVERT_EXPR_P (arg0
)
862 && CONVERT_EXPR_P (arg1
)
863 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
864 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
867 if (outprec
>= BITS_PER_WORD
868 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
869 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
870 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
872 tree tem
= do_narrow (loc
, ex_form
, type
, arg0
, arg1
,
873 expr
, inprec
, outprec
, dofold
);
882 /* This is not correct for ABS_EXPR,
883 since we must test the sign before truncation. */
885 /* Do the arithmetic in type TYPEX,
886 then convert result to TYPE. */
889 /* Can't do arithmetic in enumeral types
890 so use an integer type that will hold the values. */
891 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
893 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
894 TYPE_UNSIGNED (typex
));
896 if (!TYPE_UNSIGNED (typex
))
897 typex
= unsigned_type_for (typex
);
898 return convert (type
,
899 fold_build1 (ex_form
, typex
,
901 TREE_OPERAND (expr
, 0))));
905 /* Don't introduce a "can't convert between vector values of
906 different size" error. */
907 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
908 && (GET_MODE_SIZE (TYPE_MODE
909 (TREE_TYPE (TREE_OPERAND (expr
, 0))))
910 != GET_MODE_SIZE (TYPE_MODE (type
))))
912 /* If truncating after truncating, might as well do all at once.
913 If truncating after extending, we may get rid of wasted work. */
914 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
917 /* It is sometimes worthwhile to push the narrowing down through
918 the conditional and never loses. A COND_EXPR may have a throw
919 as one operand, which then has void type. Just leave void
920 operands as they are. */
922 fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
923 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
924 ? TREE_OPERAND (expr
, 1)
925 : convert (type
, TREE_OPERAND (expr
, 1)),
926 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
927 ? TREE_OPERAND (expr
, 2)
928 : convert (type
, TREE_OPERAND (expr
, 2)));
934 /* When parsing long initializers, we might end up with a lot of casts.
936 if (TREE_CODE (expr
) == INTEGER_CST
)
937 return fold_convert (type
, expr
);
938 return build1 (CONVERT_EXPR
, type
, expr
);
941 if (sanitize_flags_p (SANITIZE_FLOAT_CAST
)
942 && current_function_decl
!= NULL_TREE
)
944 expr
= save_expr (expr
);
945 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
);
946 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
947 if (check
== NULL_TREE
)
949 return maybe_fold_build2_loc (dofold
, loc
, COMPOUND_EXPR
,
950 TREE_TYPE (expr
), check
, expr
);
953 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
955 case FIXED_POINT_TYPE
:
956 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
959 expr
= maybe_fold_build1_loc (dofold
, loc
, REALPART_EXPR
,
960 TREE_TYPE (TREE_TYPE (expr
)), expr
);
961 return convert (type
, expr
);
964 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
966 error ("can%'t convert a vector of type %qT"
967 " to type %qT which has different size",
968 TREE_TYPE (expr
), type
);
969 return error_mark_node
;
971 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
974 error ("aggregate value used where an integer was expected");
975 return convert (type
, integer_zero_node
);
979 /* Convert EXPR to some integer (or enum) type TYPE.
981 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
982 fixed-point or vector; in other cases error is called.
984 The result of this is always supposed to be a newly created tree node
985 not in use in any existing structure. */
988 convert_to_integer (tree type
, tree expr
)
990 return convert_to_integer_1 (type
, expr
, true);
993 /* A wrapper around convert_to_complex_1 that only folds the
994 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
997 convert_to_integer_maybe_fold (tree type
, tree expr
, bool dofold
)
999 return convert_to_integer_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
1002 /* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
1003 true, try to fold the expression. */
1006 convert_to_complex_1 (tree type
, tree expr
, bool fold_p
)
1008 location_t loc
= EXPR_LOCATION (expr
);
1009 tree subtype
= TREE_TYPE (type
);
1011 switch (TREE_CODE (TREE_TYPE (expr
)))
1014 case FIXED_POINT_TYPE
:
1018 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
1019 convert (subtype
, integer_zero_node
));
1023 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
1025 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
1027 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
1029 tree t
= convert_to_complex_1 (type
, TREE_OPERAND (expr
, 1),
1031 if (t
== TREE_OPERAND (expr
, 1))
1033 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
1034 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
1036 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
1037 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1039 TREE_OPERAND (expr
, 0)),
1041 TREE_OPERAND (expr
, 1)));
1044 expr
= save_expr (expr
);
1045 tree realp
= maybe_fold_build1_loc (fold_p
, loc
, REALPART_EXPR
,
1046 TREE_TYPE (TREE_TYPE (expr
)),
1048 tree imagp
= maybe_fold_build1_loc (fold_p
, loc
, IMAGPART_EXPR
,
1049 TREE_TYPE (TREE_TYPE (expr
)),
1051 return maybe_fold_build2_loc (fold_p
, loc
, COMPLEX_EXPR
, type
,
1052 convert (subtype
, realp
),
1053 convert (subtype
, imagp
));
1058 case REFERENCE_TYPE
:
1059 error ("pointer value used where a complex was expected");
1060 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1063 error ("aggregate value used where a complex was expected");
1064 return convert_to_complex_1 (type
, integer_zero_node
, fold_p
);
1068 /* A wrapper around convert_to_complex_1 that always folds the
1072 convert_to_complex (tree type
, tree expr
)
1074 return convert_to_complex_1 (type
, expr
, true);
1077 /* A wrapper around convert_to_complex_1 that only folds the
1078 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
1081 convert_to_complex_maybe_fold (tree type
, tree expr
, bool dofold
)
1083 return convert_to_complex_1 (type
, expr
, dofold
|| CONSTANT_CLASS_P (expr
));
1086 /* Convert EXPR to the vector type TYPE in the usual ways. */
1089 convert_to_vector (tree type
, tree expr
)
1091 switch (TREE_CODE (TREE_TYPE (expr
)))
1095 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1097 error ("can%'t convert a value of type %qT"
1098 " to vector type %qT which has different size",
1099 TREE_TYPE (expr
), type
);
1100 return error_mark_node
;
1102 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1105 error ("can%'t convert value to a vector");
1106 return error_mark_node
;
1110 /* Convert EXPR to some fixed-point type TYPE.
1112 EXPR must be fixed-point, float, integer, or enumeral;
1113 in other cases error is called. */
1116 convert_to_fixed (tree type
, tree expr
)
1118 if (integer_zerop (expr
))
1120 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1121 return fixed_zero_node
;
1123 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1125 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1126 return fixed_one_node
;
1129 switch (TREE_CODE (TREE_TYPE (expr
)))
1131 case FIXED_POINT_TYPE
:
1136 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1139 return convert (type
,
1140 fold_build1 (REALPART_EXPR
,
1141 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1144 error ("aggregate value used where a fixed-point was expected");
1145 return error_mark_node
;