1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2014 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 "stor-layout.h"
32 #include "diagnostic-core.h"
34 #include "langhooks.h"
37 /* Convert EXPR to some pointer or reference type TYPE.
38 EXPR must be pointer, reference, integer, enumeral, or literal zero;
39 in other cases error is called. */
42 convert_to_pointer (tree type
, tree expr
)
44 location_t loc
= EXPR_LOCATION (expr
);
45 if (TREE_TYPE (expr
) == type
)
48 switch (TREE_CODE (TREE_TYPE (expr
)))
53 /* If the pointers point to different address spaces, conversion needs
54 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
55 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
56 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
59 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
61 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
68 /* If the input precision differs from the target pointer type
69 precision, first convert the input expression to an integer type of
70 the target precision. Some targets, e.g. VMS, need several pointer
71 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
72 unsigned int pprec
= TYPE_PRECISION (type
);
73 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
76 expr
= fold_build1_loc (loc
, NOP_EXPR
,
77 lang_hooks
.types
.type_for_size (pprec
, 0),
81 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
84 error ("cannot convert to a pointer type");
85 return convert_to_pointer (type
, integer_zero_node
);
90 /* Convert EXPR to some floating-point type TYPE.
92 EXPR must be float, fixed-point, integer, or enumeral;
93 in other cases error is called. */
96 convert_to_real (tree type
, tree expr
)
98 enum built_in_function fcode
= builtin_mathfn_code (expr
);
99 tree itype
= TREE_TYPE (expr
);
101 /* Disable until we figure out how to decide whether the functions are
102 present in runtime. */
103 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
105 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
106 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
110 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
125 /* The above functions may set errno differently with float
126 input or output so this transformation is not safe with
147 /* The above functions are not safe to do this conversion. */
148 if (!flag_unsafe_math_optimizations
)
155 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
158 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
159 the both as the safe type for operation. */
160 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
161 newtype
= TREE_TYPE (arg0
);
163 /* We consider to convert
165 (T1) sqrtT2 ((T2) exprT3)
167 (T1) sqrtT4 ((T4) exprT3)
169 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
170 and T4 is NEWTYPE. All those types are of floating point types.
171 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
172 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
173 T2 and T4. See the following URL for a reference:
174 http://stackoverflow.com/questions/9235456/determining-
175 floating-point-square-root
177 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
178 && !flag_unsafe_math_optimizations
)
180 /* The following conversion is unsafe even the precision condition
183 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
185 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
188 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
189 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
194 /* Be careful about integer to fp conversions.
195 These may overflow still. */
196 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
197 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
198 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
199 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
201 tree fn
= mathfn_built_in (newtype
, fcode
);
205 tree arg
= fold (convert_to_real (newtype
, arg0
));
206 expr
= build_call_expr (fn
, 1, arg
);
217 && (((fcode
== BUILT_IN_FLOORL
218 || fcode
== BUILT_IN_CEILL
219 || fcode
== BUILT_IN_ROUNDL
220 || fcode
== BUILT_IN_RINTL
221 || fcode
== BUILT_IN_TRUNCL
222 || fcode
== BUILT_IN_NEARBYINTL
)
223 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
224 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
225 || ((fcode
== BUILT_IN_FLOOR
226 || fcode
== BUILT_IN_CEIL
227 || fcode
== BUILT_IN_ROUND
228 || fcode
== BUILT_IN_RINT
229 || fcode
== BUILT_IN_TRUNC
230 || fcode
== BUILT_IN_NEARBYINT
)
231 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
233 tree fn
= mathfn_built_in (type
, fcode
);
237 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
239 /* Make sure (type)arg0 is an extension, otherwise we could end up
240 changing (float)floor(double d) into floorf((float)d), which is
241 incorrect because (float)d uses round-to-nearest and can round
242 up to the next integer. */
243 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
244 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
248 /* Propagate the cast into the operation. */
249 if (itype
!= type
&& FLOAT_TYPE_P (type
))
250 switch (TREE_CODE (expr
))
252 /* Convert (float)-x into -(float)x. This is safe for
253 round-to-nearest rounding mode when the inner type is float. */
256 if (!flag_rounding_math
257 && FLOAT_TYPE_P (itype
)
258 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
259 return build1 (TREE_CODE (expr
), type
,
260 fold (convert_to_real (type
,
261 TREE_OPERAND (expr
, 0))));
263 /* Convert (outertype)((innertype0)a+(innertype1)b)
264 into ((newtype)a+(newtype)b) where newtype
265 is the widest mode from all of these. */
271 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
272 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
274 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
275 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
276 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
280 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
281 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
282 || TYPE_MODE (type
) == SDmode
)
283 newtype
= dfloat32_type_node
;
284 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
285 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
286 || TYPE_MODE (type
) == DDmode
)
287 newtype
= dfloat64_type_node
;
288 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
289 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
290 || TYPE_MODE (type
) == TDmode
)
291 newtype
= dfloat128_type_node
;
292 if (newtype
== dfloat32_type_node
293 || newtype
== dfloat64_type_node
294 || newtype
== dfloat128_type_node
)
296 expr
= build2 (TREE_CODE (expr
), newtype
,
297 fold (convert_to_real (newtype
, arg0
)),
298 fold (convert_to_real (newtype
, arg1
)));
304 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
305 newtype
= TREE_TYPE (arg0
);
306 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
307 newtype
= TREE_TYPE (arg1
);
308 /* Sometimes this transformation is safe (cannot
309 change results through affecting double rounding
310 cases) and sometimes it is not. If NEWTYPE is
311 wider than TYPE, e.g. (float)((long double)double
312 + (long double)double) converted to
313 (float)(double + double), the transformation is
314 unsafe regardless of the details of the types
315 involved; double rounding can arise if the result
316 of NEWTYPE arithmetic is a NEWTYPE value half way
317 between two representable TYPE values but the
318 exact value is sufficiently different (in the
319 right direction) for this difference to be
320 visible in ITYPE arithmetic. If NEWTYPE is the
321 same as TYPE, however, the transformation may be
322 safe depending on the types involved: it is safe
323 if the ITYPE has strictly more than twice as many
324 mantissa bits as TYPE, can represent infinities
325 and NaNs if the TYPE can, and has sufficient
326 exponent range for the product or ratio of two
327 values representable in the TYPE to be within the
328 range of normal values of ITYPE. */
329 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
330 && (flag_unsafe_math_optimizations
331 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
332 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
334 && !excess_precision_type (newtype
))))
336 expr
= build2 (TREE_CODE (expr
), newtype
,
337 fold (convert_to_real (newtype
, arg0
)),
338 fold (convert_to_real (newtype
, arg1
)));
349 switch (TREE_CODE (TREE_TYPE (expr
)))
352 /* Ignore the conversion if we don't need to store intermediate
353 results and neither type is a decimal float. */
354 return build1 ((flag_float_store
355 || DECIMAL_FLOAT_TYPE_P (type
)
356 || DECIMAL_FLOAT_TYPE_P (itype
))
357 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
362 return build1 (FLOAT_EXPR
, type
, expr
);
364 case FIXED_POINT_TYPE
:
365 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
368 return convert (type
,
369 fold_build1 (REALPART_EXPR
,
370 TREE_TYPE (TREE_TYPE (expr
)), expr
));
374 error ("pointer value used where a floating point value was expected");
375 return convert_to_real (type
, integer_zero_node
);
378 error ("aggregate value used where a float was expected");
379 return convert_to_real (type
, integer_zero_node
);
383 /* Convert EXPR to some integer (or enum) type TYPE.
385 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
386 fixed-point or vector; in other cases error is called.
388 The result of this is always supposed to be a newly created tree node
389 not in use in any existing structure. */
392 convert_to_integer (tree type
, tree expr
)
394 enum tree_code ex_form
= TREE_CODE (expr
);
395 tree intype
= TREE_TYPE (expr
);
396 unsigned int inprec
= element_precision (intype
);
397 unsigned int outprec
= element_precision (type
);
398 location_t loc
= EXPR_LOCATION (expr
);
400 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
401 be. Consider `enum E = { a, b = (enum E) 3 };'. */
402 if (!COMPLETE_TYPE_P (type
))
404 error ("conversion to incomplete type");
405 return error_mark_node
;
408 /* Convert e.g. (long)round(d) -> lround(d). */
409 /* If we're converting to char, we may encounter differing behavior
410 between converting from double->char vs double->long->char.
411 We're in "undefined" territory but we prefer to be conservative,
412 so only proceed in "unsafe" math mode. */
414 && (flag_unsafe_math_optimizations
415 || (long_integer_type_node
416 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
418 tree s_expr
= strip_float_extensions (expr
);
419 tree s_intype
= TREE_TYPE (s_expr
);
420 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
425 CASE_FLT_FN (BUILT_IN_CEIL
):
426 /* Only convert in ISO C99 mode. */
427 if (!targetm
.libc_has_function (function_c99_misc
))
429 if (outprec
< TYPE_PRECISION (integer_type_node
)
430 || (outprec
== TYPE_PRECISION (integer_type_node
)
431 && !TYPE_UNSIGNED (type
)))
432 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
433 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
434 && !TYPE_UNSIGNED (type
))
435 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
436 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
437 && !TYPE_UNSIGNED (type
))
438 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
441 CASE_FLT_FN (BUILT_IN_FLOOR
):
442 /* Only convert in ISO C99 mode. */
443 if (!targetm
.libc_has_function (function_c99_misc
))
445 if (outprec
< TYPE_PRECISION (integer_type_node
)
446 || (outprec
== TYPE_PRECISION (integer_type_node
)
447 && !TYPE_UNSIGNED (type
)))
448 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
449 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
450 && !TYPE_UNSIGNED (type
))
451 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
452 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
453 && !TYPE_UNSIGNED (type
))
454 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
457 CASE_FLT_FN (BUILT_IN_ROUND
):
458 /* Only convert in ISO C99 mode. */
459 if (!targetm
.libc_has_function (function_c99_misc
))
461 if (outprec
< TYPE_PRECISION (integer_type_node
)
462 || (outprec
== TYPE_PRECISION (integer_type_node
)
463 && !TYPE_UNSIGNED (type
)))
464 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
465 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
466 && !TYPE_UNSIGNED (type
))
467 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
468 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
469 && !TYPE_UNSIGNED (type
))
470 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
473 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
474 /* Only convert nearbyint* if we can ignore math exceptions. */
475 if (flag_trapping_math
)
477 /* ... Fall through ... */
478 CASE_FLT_FN (BUILT_IN_RINT
):
479 /* Only convert in ISO C99 mode. */
480 if (!targetm
.libc_has_function (function_c99_misc
))
482 if (outprec
< TYPE_PRECISION (integer_type_node
)
483 || (outprec
== TYPE_PRECISION (integer_type_node
)
484 && !TYPE_UNSIGNED (type
)))
485 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
486 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
487 && !TYPE_UNSIGNED (type
))
488 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
489 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
490 && !TYPE_UNSIGNED (type
))
491 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
494 CASE_FLT_FN (BUILT_IN_TRUNC
):
495 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
503 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
504 return convert_to_integer (type
, newexpr
);
508 /* Convert (int)logb(d) -> ilogb(d). */
510 && flag_unsafe_math_optimizations
511 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
513 && (outprec
> TYPE_PRECISION (integer_type_node
)
514 || (outprec
== TYPE_PRECISION (integer_type_node
)
515 && !TYPE_UNSIGNED (type
))))
517 tree s_expr
= strip_float_extensions (expr
);
518 tree s_intype
= TREE_TYPE (s_expr
);
519 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
524 CASE_FLT_FN (BUILT_IN_LOGB
):
525 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
534 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
535 return convert_to_integer (type
, newexpr
);
539 switch (TREE_CODE (intype
))
543 if (integer_zerop (expr
))
544 return build_int_cst (type
, 0);
546 /* Convert to an unsigned integer of the correct width first, and from
547 there widen/truncate to the required type. Some targets support the
548 coexistence of multiple valid pointer sizes, so fetch the one we need
550 expr
= fold_build1 (CONVERT_EXPR
,
551 lang_hooks
.types
.type_for_size
552 (TYPE_PRECISION (intype
), 0),
554 return fold_convert (type
, expr
);
560 /* If this is a logical operation, which just returns 0 or 1, we can
561 change the type of the expression. */
563 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
565 expr
= copy_node (expr
);
566 TREE_TYPE (expr
) = type
;
570 /* If we are widening the type, put in an explicit conversion.
571 Similarly if we are not changing the width. After this, we know
572 we are truncating EXPR. */
574 else if (outprec
>= inprec
)
578 /* If the precision of the EXPR's type is K bits and the
579 destination mode has more bits, and the sign is changing,
580 it is not safe to use a NOP_EXPR. For example, suppose
581 that EXPR's type is a 3-bit unsigned integer type, the
582 TYPE is a 3-bit signed integer type, and the machine mode
583 for the types is 8-bit QImode. In that case, the
584 conversion necessitates an explicit sign-extension. In
585 the signed-to-unsigned case the high-order bits have to
587 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
588 && (TYPE_PRECISION (TREE_TYPE (expr
))
589 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
594 return fold_build1 (code
, type
, expr
);
597 /* If TYPE is an enumeral type or a type with a precision less
598 than the number of bits in its mode, do the conversion to the
599 type corresponding to its mode, then do a nop conversion
601 else if (TREE_CODE (type
) == ENUMERAL_TYPE
602 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
603 return build1 (NOP_EXPR
, type
,
604 convert (lang_hooks
.types
.type_for_mode
605 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
608 /* Here detect when we can distribute the truncation down past some
609 arithmetic. For example, if adding two longs and converting to an
610 int, we can equally well convert both to ints and then add.
611 For the operations handled here, such truncation distribution
613 It is desirable in these cases:
614 1) when truncating down to full-word from a larger size
615 2) when truncating takes no work.
616 3) when at least one operand of the arithmetic has been extended
617 (as by C's default conversions). In this case we need two conversions
618 if we do the arithmetic as already requested, so we might as well
619 truncate both and then combine. Perhaps that way we need only one.
621 Note that in general we cannot do the arithmetic in a type
622 shorter than the desired result of conversion, even if the operands
623 are both extended from a shorter type, because they might overflow
624 if combined in that type. The exceptions to this--the times when
625 two narrow values can be combined in their narrow type even to
626 make a wider result--are handled by "shorten" in build_binary_op. */
631 /* We can pass truncation down through right shifting
632 when the shift count is a nonpositive constant. */
633 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
634 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
639 /* We can pass truncation down through left shifting
640 when the shift count is a nonnegative constant and
641 the target type is unsigned. */
642 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
643 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
644 && TYPE_UNSIGNED (type
)
645 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
647 /* If shift count is less than the width of the truncated type,
649 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
650 /* In this case, shifting is like multiplication. */
654 /* If it is >= that width, result is zero.
655 Handling this with trunc1 would give the wrong result:
656 (int) ((long long) a << 32) is well defined (as 0)
657 but (int) a << 32 is undefined and would get a
660 tree t
= build_int_cst (type
, 0);
662 /* If the original expression had side-effects, we must
664 if (TREE_SIDE_EFFECTS (expr
))
665 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
674 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
675 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
677 /* Don't distribute unless the output precision is at least as big
678 as the actual inputs and it has the same signedness. */
679 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
680 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
681 /* If signedness of arg0 and arg1 don't match,
682 we can't necessarily find a type to compare them in. */
683 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
684 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
685 /* Do not change the sign of the division. */
686 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
687 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
688 /* Either require unsigned division or a division by
689 a constant that is not -1. */
690 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
691 || (TREE_CODE (arg1
) == INTEGER_CST
692 && !integer_all_onesp (arg1
))))
701 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
702 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
704 /* Don't distribute unless the output precision is at least as big
705 as the actual inputs. Otherwise, the comparison of the
706 truncated values will be wrong. */
707 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
708 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
709 /* If signedness of arg0 and arg1 don't match,
710 we can't necessarily find a type to compare them in. */
711 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
712 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
724 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
725 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
727 /* Do not try to narrow operands of pointer subtraction;
728 that will interfere with other folding. */
729 if (ex_form
== MINUS_EXPR
730 && CONVERT_EXPR_P (arg0
)
731 && CONVERT_EXPR_P (arg1
)
732 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
733 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
736 if (outprec
>= BITS_PER_WORD
737 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
738 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
739 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
741 /* Do the arithmetic in type TYPEX,
742 then convert result to TYPE. */
745 /* Can't do arithmetic in enumeral types
746 so use an integer type that will hold the values. */
747 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
748 typex
= lang_hooks
.types
.type_for_size
749 (TYPE_PRECISION (typex
), TYPE_UNSIGNED (typex
));
751 /* But now perhaps TYPEX is as wide as INPREC.
752 In that case, do nothing special here.
753 (Otherwise would recurse infinitely in convert. */
754 if (TYPE_PRECISION (typex
) != inprec
)
756 /* Don't do unsigned arithmetic where signed was wanted,
758 Exception: if both of the original operands were
759 unsigned then we can safely do the work as unsigned.
760 Exception: shift operations take their type solely
761 from the first argument.
762 Exception: the LSHIFT_EXPR case above requires that
763 we perform this operation unsigned lest we produce
764 signed-overflow undefinedness.
765 And we may need to do it as unsigned
766 if we truncate to the original size. */
767 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
768 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
769 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
770 || ex_form
== LSHIFT_EXPR
771 || ex_form
== RSHIFT_EXPR
772 || ex_form
== LROTATE_EXPR
773 || ex_form
== RROTATE_EXPR
))
774 || ex_form
== LSHIFT_EXPR
775 /* If we have !flag_wrapv, and either ARG0 or
776 ARG1 is of a signed type, we have to do
777 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
778 type in case the operation in outprec precision
779 could overflow. Otherwise, we would introduce
780 signed-overflow undefinedness. */
781 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
782 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
783 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
785 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
787 && (ex_form
== PLUS_EXPR
788 || ex_form
== MINUS_EXPR
789 || ex_form
== MULT_EXPR
)))
790 typex
= unsigned_type_for (typex
);
792 typex
= signed_type_for (typex
);
793 return convert (type
,
794 fold_build2 (ex_form
, typex
,
795 convert (typex
, arg0
),
796 convert (typex
, arg1
)));
804 /* This is not correct for ABS_EXPR,
805 since we must test the sign before truncation. */
807 tree typex
= unsigned_type_for (type
);
808 return convert (type
,
809 fold_build1 (ex_form
, typex
,
811 TREE_OPERAND (expr
, 0))));
816 "can't convert between vector values of different size" error. */
817 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
818 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
819 != GET_MODE_SIZE (TYPE_MODE (type
))))
821 /* If truncating after truncating, might as well do all at once.
822 If truncating after extending, we may get rid of wasted work. */
823 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
826 /* It is sometimes worthwhile to push the narrowing down through
827 the conditional and never loses. A COND_EXPR may have a throw
828 as one operand, which then has void type. Just leave void
829 operands as they are. */
830 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
831 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
832 ? TREE_OPERAND (expr
, 1)
833 : convert (type
, TREE_OPERAND (expr
, 1)),
834 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
835 ? TREE_OPERAND (expr
, 2)
836 : convert (type
, TREE_OPERAND (expr
, 2)));
842 /* When parsing long initializers, we might end up with a lot of casts.
844 if (TREE_CODE (expr
) == INTEGER_CST
)
845 return fold_convert (type
, expr
);
846 return build1 (CONVERT_EXPR
, type
, expr
);
849 if (flag_sanitize
& SANITIZE_FLOAT_CAST
)
851 expr
= save_expr (expr
);
852 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
);
853 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
856 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), check
, expr
);
859 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
861 case FIXED_POINT_TYPE
:
862 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
865 return convert (type
,
866 fold_build1 (REALPART_EXPR
,
867 TREE_TYPE (TREE_TYPE (expr
)), expr
));
870 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
872 error ("can%'t convert between vector values of different size");
873 return error_mark_node
;
875 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
878 error ("aggregate value used where an integer was expected");
879 return convert (type
, integer_zero_node
);
883 /* Convert EXPR to the complex type TYPE in the usual ways. */
886 convert_to_complex (tree type
, tree expr
)
888 tree subtype
= TREE_TYPE (type
);
890 switch (TREE_CODE (TREE_TYPE (expr
)))
893 case FIXED_POINT_TYPE
:
897 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
898 convert (subtype
, integer_zero_node
));
902 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
904 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
906 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
907 return fold_build2 (COMPLEX_EXPR
, type
,
908 convert (subtype
, TREE_OPERAND (expr
, 0)),
909 convert (subtype
, TREE_OPERAND (expr
, 1)));
912 expr
= save_expr (expr
);
914 fold_build2 (COMPLEX_EXPR
, type
,
916 fold_build1 (REALPART_EXPR
,
917 TREE_TYPE (TREE_TYPE (expr
)),
920 fold_build1 (IMAGPART_EXPR
,
921 TREE_TYPE (TREE_TYPE (expr
)),
928 error ("pointer value used where a complex was expected");
929 return convert_to_complex (type
, integer_zero_node
);
932 error ("aggregate value used where a complex was expected");
933 return convert_to_complex (type
, integer_zero_node
);
937 /* Convert EXPR to the vector type TYPE in the usual ways. */
940 convert_to_vector (tree type
, tree expr
)
942 switch (TREE_CODE (TREE_TYPE (expr
)))
946 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
948 error ("can%'t convert between vector values of different size");
949 return error_mark_node
;
951 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
954 error ("can%'t convert value to a vector");
955 return error_mark_node
;
959 /* Convert EXPR to some fixed-point type TYPE.
961 EXPR must be fixed-point, float, integer, or enumeral;
962 in other cases error is called. */
965 convert_to_fixed (tree type
, tree expr
)
967 if (integer_zerop (expr
))
969 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
970 return fixed_zero_node
;
972 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
974 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
975 return fixed_one_node
;
978 switch (TREE_CODE (TREE_TYPE (expr
)))
980 case FIXED_POINT_TYPE
:
985 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
988 return convert (type
,
989 fold_build1 (REALPART_EXPR
,
990 TREE_TYPE (TREE_TYPE (expr
)), expr
));
993 error ("aggregate value used where a fixed-point was expected");
994 return error_mark_node
;