1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* These routines are somewhat language-independent utility function
22 intended to be called by the language-specific convert () functions. */
26 #include "coretypes.h"
29 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
35 #include "langhooks.h"
39 /* Convert EXPR to some pointer or reference type TYPE.
40 EXPR must be pointer, reference, integer, enumeral, or literal zero;
41 in other cases error is called. */
44 convert_to_pointer (tree type
, tree expr
)
46 location_t loc
= EXPR_LOCATION (expr
);
47 if (TREE_TYPE (expr
) == type
)
50 switch (TREE_CODE (TREE_TYPE (expr
)))
55 /* If the pointers point to different address spaces, conversion needs
56 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
57 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
58 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
61 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
63 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
70 /* If the input precision differs from the target pointer type
71 precision, first convert the input expression to an integer type of
72 the target precision. Some targets, e.g. VMS, need several pointer
73 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
74 unsigned int pprec
= TYPE_PRECISION (type
);
75 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
78 expr
= fold_build1_loc (loc
, NOP_EXPR
,
79 lang_hooks
.types
.type_for_size (pprec
, 0),
83 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
86 error ("cannot convert to a pointer type");
87 return convert_to_pointer (type
, integer_zero_node
);
92 /* Convert EXPR to some floating-point type TYPE.
94 EXPR must be float, fixed-point, integer, or enumeral;
95 in other cases error is called. */
98 convert_to_real (tree type
, tree expr
)
100 enum built_in_function fcode
= builtin_mathfn_code (expr
);
101 tree itype
= TREE_TYPE (expr
);
103 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
105 tree t
= convert_to_real (type
, TREE_OPERAND (expr
, 1));
106 if (t
== TREE_OPERAND (expr
, 1))
108 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
109 TREE_OPERAND (expr
, 0), t
);
112 /* Disable until we figure out how to decide whether the functions are
113 present in runtime. */
114 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
116 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
117 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
121 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
136 /* The above functions may set errno differently with float
137 input or output so this transformation is not safe with
158 /* The above functions are not safe to do this conversion. */
159 if (!flag_unsafe_math_optimizations
)
166 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
169 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
170 the both as the safe type for operation. */
171 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
172 newtype
= TREE_TYPE (arg0
);
174 /* We consider to convert
176 (T1) sqrtT2 ((T2) exprT3)
178 (T1) sqrtT4 ((T4) exprT3)
180 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
181 and T4 is NEWTYPE. All those types are of floating point types.
182 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
183 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
184 T2 and T4. See the following URL for a reference:
185 http://stackoverflow.com/questions/9235456/determining-
186 floating-point-square-root
188 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
189 && !flag_unsafe_math_optimizations
)
191 /* The following conversion is unsafe even the precision condition
194 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
196 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
199 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
200 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
205 /* Be careful about integer to fp conversions.
206 These may overflow still. */
207 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
208 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
209 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
210 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
212 tree fn
= mathfn_built_in (newtype
, fcode
);
216 tree arg
= fold (convert_to_real (newtype
, arg0
));
217 expr
= build_call_expr (fn
, 1, arg
);
228 /* Propagate the cast into the operation. */
229 if (itype
!= type
&& FLOAT_TYPE_P (type
))
230 switch (TREE_CODE (expr
))
232 /* Convert (float)-x into -(float)x. This is safe for
233 round-to-nearest rounding mode when the inner type is float. */
236 if (!flag_rounding_math
237 && FLOAT_TYPE_P (itype
)
238 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
239 return build1 (TREE_CODE (expr
), type
,
240 fold (convert_to_real (type
,
241 TREE_OPERAND (expr
, 0))));
243 /* Convert (outertype)((innertype0)a+(innertype1)b)
244 into ((newtype)a+(newtype)b) where newtype
245 is the widest mode from all of these. */
251 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
252 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
254 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
255 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
256 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
260 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
261 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
262 || TYPE_MODE (type
) == SDmode
)
263 newtype
= dfloat32_type_node
;
264 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
265 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
266 || TYPE_MODE (type
) == DDmode
)
267 newtype
= dfloat64_type_node
;
268 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
269 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
270 || TYPE_MODE (type
) == TDmode
)
271 newtype
= dfloat128_type_node
;
272 if (newtype
== dfloat32_type_node
273 || newtype
== dfloat64_type_node
274 || newtype
== dfloat128_type_node
)
276 expr
= build2 (TREE_CODE (expr
), newtype
,
277 fold (convert_to_real (newtype
, arg0
)),
278 fold (convert_to_real (newtype
, arg1
)));
284 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
285 newtype
= TREE_TYPE (arg0
);
286 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
287 newtype
= TREE_TYPE (arg1
);
288 /* Sometimes this transformation is safe (cannot
289 change results through affecting double rounding
290 cases) and sometimes it is not. If NEWTYPE is
291 wider than TYPE, e.g. (float)((long double)double
292 + (long double)double) converted to
293 (float)(double + double), the transformation is
294 unsafe regardless of the details of the types
295 involved; double rounding can arise if the result
296 of NEWTYPE arithmetic is a NEWTYPE value half way
297 between two representable TYPE values but the
298 exact value is sufficiently different (in the
299 right direction) for this difference to be
300 visible in ITYPE arithmetic. If NEWTYPE is the
301 same as TYPE, however, the transformation may be
302 safe depending on the types involved: it is safe
303 if the ITYPE has strictly more than twice as many
304 mantissa bits as TYPE, can represent infinities
305 and NaNs if the TYPE can, and has sufficient
306 exponent range for the product or ratio of two
307 values representable in the TYPE to be within the
308 range of normal values of ITYPE. */
309 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
310 && (flag_unsafe_math_optimizations
311 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
312 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
314 && !excess_precision_type (newtype
))))
316 expr
= build2 (TREE_CODE (expr
), newtype
,
317 fold (convert_to_real (newtype
, arg0
)),
318 fold (convert_to_real (newtype
, arg1
)));
329 switch (TREE_CODE (TREE_TYPE (expr
)))
332 /* Ignore the conversion if we don't need to store intermediate
333 results and neither type is a decimal float. */
334 return build1 ((flag_float_store
335 || DECIMAL_FLOAT_TYPE_P (type
)
336 || DECIMAL_FLOAT_TYPE_P (itype
))
337 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
342 return build1 (FLOAT_EXPR
, type
, expr
);
344 case FIXED_POINT_TYPE
:
345 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
348 return convert (type
,
349 fold_build1 (REALPART_EXPR
,
350 TREE_TYPE (TREE_TYPE (expr
)), expr
));
354 error ("pointer value used where a floating point value was expected");
355 return convert_to_real (type
, integer_zero_node
);
358 error ("aggregate value used where a float was expected");
359 return convert_to_real (type
, integer_zero_node
);
363 /* Convert EXPR to some integer (or enum) type TYPE.
365 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
366 fixed-point or vector; in other cases error is called.
368 The result of this is always supposed to be a newly created tree node
369 not in use in any existing structure. */
372 convert_to_integer (tree type
, tree expr
)
374 enum tree_code ex_form
= TREE_CODE (expr
);
375 tree intype
= TREE_TYPE (expr
);
376 unsigned int inprec
= element_precision (intype
);
377 unsigned int outprec
= element_precision (type
);
378 location_t loc
= EXPR_LOCATION (expr
);
380 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
381 be. Consider `enum E = { a, b = (enum E) 3 };'. */
382 if (!COMPLETE_TYPE_P (type
))
384 error ("conversion to incomplete type");
385 return error_mark_node
;
388 if (ex_form
== COMPOUND_EXPR
)
390 tree t
= convert_to_integer (type
, TREE_OPERAND (expr
, 1));
391 if (t
== TREE_OPERAND (expr
, 1))
393 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
394 TREE_OPERAND (expr
, 0), t
);
397 /* Convert e.g. (long)round(d) -> lround(d). */
398 /* If we're converting to char, we may encounter differing behavior
399 between converting from double->char vs double->long->char.
400 We're in "undefined" territory but we prefer to be conservative,
401 so only proceed in "unsafe" math mode. */
403 && (flag_unsafe_math_optimizations
404 || (long_integer_type_node
405 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
407 tree s_expr
= strip_float_extensions (expr
);
408 tree s_intype
= TREE_TYPE (s_expr
);
409 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
414 CASE_FLT_FN (BUILT_IN_CEIL
):
415 /* Only convert in ISO C99 mode. */
416 if (!targetm
.libc_has_function (function_c99_misc
))
418 if (outprec
< TYPE_PRECISION (integer_type_node
)
419 || (outprec
== TYPE_PRECISION (integer_type_node
)
420 && !TYPE_UNSIGNED (type
)))
421 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
422 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
423 && !TYPE_UNSIGNED (type
))
424 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
425 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
426 && !TYPE_UNSIGNED (type
))
427 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
430 CASE_FLT_FN (BUILT_IN_FLOOR
):
431 /* Only convert in ISO C99 mode. */
432 if (!targetm
.libc_has_function (function_c99_misc
))
434 if (outprec
< TYPE_PRECISION (integer_type_node
)
435 || (outprec
== TYPE_PRECISION (integer_type_node
)
436 && !TYPE_UNSIGNED (type
)))
437 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
438 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
439 && !TYPE_UNSIGNED (type
))
440 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
441 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
442 && !TYPE_UNSIGNED (type
))
443 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
446 CASE_FLT_FN (BUILT_IN_ROUND
):
447 /* Only convert in ISO C99 mode and with -fno-math-errno. */
448 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
450 if (outprec
< TYPE_PRECISION (integer_type_node
)
451 || (outprec
== TYPE_PRECISION (integer_type_node
)
452 && !TYPE_UNSIGNED (type
)))
453 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
454 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
455 && !TYPE_UNSIGNED (type
))
456 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
457 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
458 && !TYPE_UNSIGNED (type
))
459 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
462 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
463 /* Only convert nearbyint* if we can ignore math exceptions. */
464 if (flag_trapping_math
)
466 /* ... Fall through ... */
467 CASE_FLT_FN (BUILT_IN_RINT
):
468 /* Only convert in ISO C99 mode and with -fno-math-errno. */
469 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
471 if (outprec
< TYPE_PRECISION (integer_type_node
)
472 || (outprec
== TYPE_PRECISION (integer_type_node
)
473 && !TYPE_UNSIGNED (type
)))
474 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
475 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
476 && !TYPE_UNSIGNED (type
))
477 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
478 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
479 && !TYPE_UNSIGNED (type
))
480 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
483 CASE_FLT_FN (BUILT_IN_TRUNC
):
484 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
492 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
493 return convert_to_integer (type
, newexpr
);
497 /* Convert (int)logb(d) -> ilogb(d). */
499 && flag_unsafe_math_optimizations
500 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
502 && (outprec
> TYPE_PRECISION (integer_type_node
)
503 || (outprec
== TYPE_PRECISION (integer_type_node
)
504 && !TYPE_UNSIGNED (type
))))
506 tree s_expr
= strip_float_extensions (expr
);
507 tree s_intype
= TREE_TYPE (s_expr
);
508 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
513 CASE_FLT_FN (BUILT_IN_LOGB
):
514 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
523 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
524 return convert_to_integer (type
, newexpr
);
528 switch (TREE_CODE (intype
))
532 if (integer_zerop (expr
))
533 return build_int_cst (type
, 0);
535 /* Convert to an unsigned integer of the correct width first, and from
536 there widen/truncate to the required type. Some targets support the
537 coexistence of multiple valid pointer sizes, so fetch the one we need
539 expr
= fold_build1 (CONVERT_EXPR
,
540 lang_hooks
.types
.type_for_size
541 (TYPE_PRECISION (intype
), 0),
543 return fold_convert (type
, expr
);
549 /* If this is a logical operation, which just returns 0 or 1, we can
550 change the type of the expression. */
552 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
554 expr
= copy_node (expr
);
555 TREE_TYPE (expr
) = type
;
559 /* If we are widening the type, put in an explicit conversion.
560 Similarly if we are not changing the width. After this, we know
561 we are truncating EXPR. */
563 else if (outprec
>= inprec
)
567 /* If the precision of the EXPR's type is K bits and the
568 destination mode has more bits, and the sign is changing,
569 it is not safe to use a NOP_EXPR. For example, suppose
570 that EXPR's type is a 3-bit unsigned integer type, the
571 TYPE is a 3-bit signed integer type, and the machine mode
572 for the types is 8-bit QImode. In that case, the
573 conversion necessitates an explicit sign-extension. In
574 the signed-to-unsigned case the high-order bits have to
576 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
577 && (TYPE_PRECISION (TREE_TYPE (expr
))
578 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
583 return fold_build1 (code
, type
, expr
);
586 /* If TYPE is an enumeral type or a type with a precision less
587 than the number of bits in its mode, do the conversion to the
588 type corresponding to its mode, then do a nop conversion
590 else if (TREE_CODE (type
) == ENUMERAL_TYPE
591 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
592 return build1 (NOP_EXPR
, type
,
593 convert (lang_hooks
.types
.type_for_mode
594 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
597 /* Here detect when we can distribute the truncation down past some
598 arithmetic. For example, if adding two longs and converting to an
599 int, we can equally well convert both to ints and then add.
600 For the operations handled here, such truncation distribution
602 It is desirable in these cases:
603 1) when truncating down to full-word from a larger size
604 2) when truncating takes no work.
605 3) when at least one operand of the arithmetic has been extended
606 (as by C's default conversions). In this case we need two conversions
607 if we do the arithmetic as already requested, so we might as well
608 truncate both and then combine. Perhaps that way we need only one.
610 Note that in general we cannot do the arithmetic in a type
611 shorter than the desired result of conversion, even if the operands
612 are both extended from a shorter type, because they might overflow
613 if combined in that type. The exceptions to this--the times when
614 two narrow values can be combined in their narrow type even to
615 make a wider result--are handled by "shorten" in build_binary_op. */
620 /* We can pass truncation down through right shifting
621 when the shift count is a nonpositive constant. */
622 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
623 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
628 /* We can pass truncation down through left shifting
629 when the shift count is a nonnegative constant and
630 the target type is unsigned. */
631 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
632 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
633 && TYPE_UNSIGNED (type
)
634 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
636 /* If shift count is less than the width of the truncated type,
638 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
639 /* In this case, shifting is like multiplication. */
643 /* If it is >= that width, result is zero.
644 Handling this with trunc1 would give the wrong result:
645 (int) ((long long) a << 32) is well defined (as 0)
646 but (int) a << 32 is undefined and would get a
649 tree t
= build_int_cst (type
, 0);
651 /* If the original expression had side-effects, we must
653 if (TREE_SIDE_EFFECTS (expr
))
654 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
663 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
664 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
666 /* Don't distribute unless the output precision is at least as big
667 as the actual inputs and it has the same signedness. */
668 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
669 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
670 /* If signedness of arg0 and arg1 don't match,
671 we can't necessarily find a type to compare them in. */
672 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
673 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
674 /* Do not change the sign of the division. */
675 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
676 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
677 /* Either require unsigned division or a division by
678 a constant that is not -1. */
679 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
680 || (TREE_CODE (arg1
) == INTEGER_CST
681 && !integer_all_onesp (arg1
))))
690 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
691 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
693 /* Don't distribute unless the output precision is at least as big
694 as the actual inputs. Otherwise, the comparison of the
695 truncated values will be wrong. */
696 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
697 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
698 /* If signedness of arg0 and arg1 don't match,
699 we can't necessarily find a type to compare them in. */
700 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
701 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
713 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
714 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
716 /* Do not try to narrow operands of pointer subtraction;
717 that will interfere with other folding. */
718 if (ex_form
== MINUS_EXPR
719 && CONVERT_EXPR_P (arg0
)
720 && CONVERT_EXPR_P (arg1
)
721 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
722 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
725 if (outprec
>= BITS_PER_WORD
726 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
727 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
728 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
730 /* Do the arithmetic in type TYPEX,
731 then convert result to TYPE. */
734 /* Can't do arithmetic in enumeral types
735 so use an integer type that will hold the values. */
736 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
738 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
739 TYPE_UNSIGNED (typex
));
741 /* But now perhaps TYPEX is as wide as INPREC.
742 In that case, do nothing special here.
743 (Otherwise would recurse infinitely in convert. */
744 if (TYPE_PRECISION (typex
) != inprec
)
746 /* Don't do unsigned arithmetic where signed was wanted,
748 Exception: if both of the original operands were
749 unsigned then we can safely do the work as unsigned.
750 Exception: shift operations take their type solely
751 from the first argument.
752 Exception: the LSHIFT_EXPR case above requires that
753 we perform this operation unsigned lest we produce
754 signed-overflow undefinedness.
755 And we may need to do it as unsigned
756 if we truncate to the original size. */
757 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
758 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
759 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
760 || ex_form
== LSHIFT_EXPR
761 || ex_form
== RSHIFT_EXPR
762 || ex_form
== LROTATE_EXPR
763 || ex_form
== RROTATE_EXPR
))
764 || ex_form
== LSHIFT_EXPR
765 /* If we have !flag_wrapv, and either ARG0 or
766 ARG1 is of a signed type, we have to do
767 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
768 type in case the operation in outprec precision
769 could overflow. Otherwise, we would introduce
770 signed-overflow undefinedness. */
771 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
772 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
773 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
775 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
777 && (ex_form
== PLUS_EXPR
778 || ex_form
== MINUS_EXPR
779 || ex_form
== MULT_EXPR
)))
781 if (!TYPE_UNSIGNED (typex
))
782 typex
= unsigned_type_for (typex
);
786 if (TYPE_UNSIGNED (typex
))
787 typex
= signed_type_for (typex
);
789 return convert (type
,
790 fold_build2 (ex_form
, typex
,
791 convert (typex
, arg0
),
792 convert (typex
, arg1
)));
800 /* This is not correct for ABS_EXPR,
801 since we must test the sign before truncation. */
803 /* Do the arithmetic in type TYPEX,
804 then convert result to TYPE. */
807 /* Can't do arithmetic in enumeral types
808 so use an integer type that will hold the values. */
809 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
811 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
812 TYPE_UNSIGNED (typex
));
814 if (!TYPE_UNSIGNED (typex
))
815 typex
= unsigned_type_for (typex
);
816 return convert (type
,
817 fold_build1 (ex_form
, typex
,
819 TREE_OPERAND (expr
, 0))));
824 "can't convert between vector values of different size" error. */
825 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
826 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
827 != GET_MODE_SIZE (TYPE_MODE (type
))))
829 /* If truncating after truncating, might as well do all at once.
830 If truncating after extending, we may get rid of wasted work. */
831 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
834 /* It is sometimes worthwhile to push the narrowing down through
835 the conditional and never loses. A COND_EXPR may have a throw
836 as one operand, which then has void type. Just leave void
837 operands as they are. */
838 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
839 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
840 ? TREE_OPERAND (expr
, 1)
841 : convert (type
, TREE_OPERAND (expr
, 1)),
842 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
843 ? TREE_OPERAND (expr
, 2)
844 : convert (type
, TREE_OPERAND (expr
, 2)));
850 /* When parsing long initializers, we might end up with a lot of casts.
852 if (TREE_CODE (expr
) == INTEGER_CST
)
853 return fold_convert (type
, expr
);
854 return build1 (CONVERT_EXPR
, type
, expr
);
857 if (flag_sanitize
& SANITIZE_FLOAT_CAST
858 && do_ubsan_in_current_function ())
860 expr
= save_expr (expr
);
861 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
, expr
);
862 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
865 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), check
, expr
);
868 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
870 case FIXED_POINT_TYPE
:
871 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
874 return convert (type
,
875 fold_build1 (REALPART_EXPR
,
876 TREE_TYPE (TREE_TYPE (expr
)), expr
));
879 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
881 error ("can%'t convert a vector of type %qT"
882 " to type %qT which has different size",
883 TREE_TYPE (expr
), type
);
884 return error_mark_node
;
886 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
889 error ("aggregate value used where an integer was expected");
890 return convert (type
, integer_zero_node
);
894 /* Convert EXPR to the complex type TYPE in the usual ways. */
897 convert_to_complex (tree type
, tree expr
)
899 tree subtype
= TREE_TYPE (type
);
901 switch (TREE_CODE (TREE_TYPE (expr
)))
904 case FIXED_POINT_TYPE
:
908 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
909 convert (subtype
, integer_zero_node
));
913 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
915 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
917 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
919 tree t
= convert_to_complex (type
, TREE_OPERAND (expr
, 1));
920 if (t
== TREE_OPERAND (expr
, 1))
922 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
923 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
925 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
926 return fold_build2 (COMPLEX_EXPR
, type
,
927 convert (subtype
, TREE_OPERAND (expr
, 0)),
928 convert (subtype
, TREE_OPERAND (expr
, 1)));
931 expr
= save_expr (expr
);
933 fold_build2 (COMPLEX_EXPR
, type
,
935 fold_build1 (REALPART_EXPR
,
936 TREE_TYPE (TREE_TYPE (expr
)),
939 fold_build1 (IMAGPART_EXPR
,
940 TREE_TYPE (TREE_TYPE (expr
)),
947 error ("pointer value used where a complex was expected");
948 return convert_to_complex (type
, integer_zero_node
);
951 error ("aggregate value used where a complex was expected");
952 return convert_to_complex (type
, integer_zero_node
);
956 /* Convert EXPR to the vector type TYPE in the usual ways. */
959 convert_to_vector (tree type
, tree expr
)
961 switch (TREE_CODE (TREE_TYPE (expr
)))
965 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
967 error ("can%'t convert a value of type %qT"
968 " to vector type %qT which has different size",
969 TREE_TYPE (expr
), type
);
970 return error_mark_node
;
972 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
975 error ("can%'t convert value to a vector");
976 return error_mark_node
;
980 /* Convert EXPR to some fixed-point type TYPE.
982 EXPR must be fixed-point, float, integer, or enumeral;
983 in other cases error is called. */
986 convert_to_fixed (tree type
, tree expr
)
988 if (integer_zerop (expr
))
990 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
991 return fixed_zero_node
;
993 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
995 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
996 return fixed_one_node
;
999 switch (TREE_CODE (TREE_TYPE (expr
)))
1001 case FIXED_POINT_TYPE
:
1006 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1009 return convert (type
,
1010 fold_build1 (REALPART_EXPR
,
1011 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1014 error ("aggregate value used where a fixed-point was expected");
1015 return error_mark_node
;