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"
32 #include "fold-const.h"
33 #include "stor-layout.h"
36 #include "diagnostic-core.h"
38 #include "langhooks.h"
42 /* Convert EXPR to some pointer or reference type TYPE.
43 EXPR must be pointer, reference, integer, enumeral, or literal zero;
44 in other cases error is called. */
47 convert_to_pointer (tree type
, tree expr
)
49 location_t loc
= EXPR_LOCATION (expr
);
50 if (TREE_TYPE (expr
) == type
)
53 switch (TREE_CODE (TREE_TYPE (expr
)))
58 /* If the pointers point to different address spaces, conversion needs
59 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
60 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
61 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
64 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
66 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
73 /* If the input precision differs from the target pointer type
74 precision, first convert the input expression to an integer type of
75 the target precision. Some targets, e.g. VMS, need several pointer
76 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
77 unsigned int pprec
= TYPE_PRECISION (type
);
78 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
81 expr
= fold_build1_loc (loc
, NOP_EXPR
,
82 lang_hooks
.types
.type_for_size (pprec
, 0),
86 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
89 error ("cannot convert to a pointer type");
90 return convert_to_pointer (type
, integer_zero_node
);
95 /* Convert EXPR to some floating-point type TYPE.
97 EXPR must be float, fixed-point, integer, or enumeral;
98 in other cases error is called. */
101 convert_to_real (tree type
, tree expr
)
103 enum built_in_function fcode
= builtin_mathfn_code (expr
);
104 tree itype
= TREE_TYPE (expr
);
106 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
108 tree t
= convert_to_real (type
, TREE_OPERAND (expr
, 1));
109 if (t
== TREE_OPERAND (expr
, 1))
111 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
112 TREE_OPERAND (expr
, 0), t
);
115 /* Disable until we figure out how to decide whether the functions are
116 present in runtime. */
117 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
119 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
120 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
124 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
139 /* The above functions may set errno differently with float
140 input or output so this transformation is not safe with
161 /* The above functions are not safe to do this conversion. */
162 if (!flag_unsafe_math_optimizations
)
169 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
172 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
173 the both as the safe type for operation. */
174 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
175 newtype
= TREE_TYPE (arg0
);
177 /* We consider to convert
179 (T1) sqrtT2 ((T2) exprT3)
181 (T1) sqrtT4 ((T4) exprT3)
183 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
184 and T4 is NEWTYPE. All those types are of floating point types.
185 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
186 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
187 T2 and T4. See the following URL for a reference:
188 http://stackoverflow.com/questions/9235456/determining-
189 floating-point-square-root
191 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
192 && !flag_unsafe_math_optimizations
)
194 /* The following conversion is unsafe even the precision condition
197 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
199 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
202 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
203 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
208 /* Be careful about integer to fp conversions.
209 These may overflow still. */
210 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
211 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
212 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
213 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
215 tree fn
= mathfn_built_in (newtype
, fcode
);
219 tree arg
= fold (convert_to_real (newtype
, arg0
));
220 expr
= build_call_expr (fn
, 1, arg
);
231 && (((fcode
== BUILT_IN_FLOORL
232 || fcode
== BUILT_IN_CEILL
233 || fcode
== BUILT_IN_ROUNDL
234 || fcode
== BUILT_IN_RINTL
235 || fcode
== BUILT_IN_TRUNCL
236 || fcode
== BUILT_IN_NEARBYINTL
)
237 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
238 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
239 || ((fcode
== BUILT_IN_FLOOR
240 || fcode
== BUILT_IN_CEIL
241 || fcode
== BUILT_IN_ROUND
242 || fcode
== BUILT_IN_RINT
243 || fcode
== BUILT_IN_TRUNC
244 || fcode
== BUILT_IN_NEARBYINT
)
245 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
247 tree fn
= mathfn_built_in (type
, fcode
);
251 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
253 /* Make sure (type)arg0 is an extension, otherwise we could end up
254 changing (float)floor(double d) into floorf((float)d), which is
255 incorrect because (float)d uses round-to-nearest and can round
256 up to the next integer. */
257 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
258 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
262 /* Propagate the cast into the operation. */
263 if (itype
!= type
&& FLOAT_TYPE_P (type
))
264 switch (TREE_CODE (expr
))
266 /* Convert (float)-x into -(float)x. This is safe for
267 round-to-nearest rounding mode when the inner type is float. */
270 if (!flag_rounding_math
271 && FLOAT_TYPE_P (itype
)
272 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
273 return build1 (TREE_CODE (expr
), type
,
274 fold (convert_to_real (type
,
275 TREE_OPERAND (expr
, 0))));
277 /* Convert (outertype)((innertype0)a+(innertype1)b)
278 into ((newtype)a+(newtype)b) where newtype
279 is the widest mode from all of these. */
285 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
286 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
288 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
289 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
290 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
294 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
295 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
296 || TYPE_MODE (type
) == SDmode
)
297 newtype
= dfloat32_type_node
;
298 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
299 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
300 || TYPE_MODE (type
) == DDmode
)
301 newtype
= dfloat64_type_node
;
302 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
303 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
304 || TYPE_MODE (type
) == TDmode
)
305 newtype
= dfloat128_type_node
;
306 if (newtype
== dfloat32_type_node
307 || newtype
== dfloat64_type_node
308 || newtype
== dfloat128_type_node
)
310 expr
= build2 (TREE_CODE (expr
), newtype
,
311 fold (convert_to_real (newtype
, arg0
)),
312 fold (convert_to_real (newtype
, arg1
)));
318 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
319 newtype
= TREE_TYPE (arg0
);
320 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
321 newtype
= TREE_TYPE (arg1
);
322 /* Sometimes this transformation is safe (cannot
323 change results through affecting double rounding
324 cases) and sometimes it is not. If NEWTYPE is
325 wider than TYPE, e.g. (float)((long double)double
326 + (long double)double) converted to
327 (float)(double + double), the transformation is
328 unsafe regardless of the details of the types
329 involved; double rounding can arise if the result
330 of NEWTYPE arithmetic is a NEWTYPE value half way
331 between two representable TYPE values but the
332 exact value is sufficiently different (in the
333 right direction) for this difference to be
334 visible in ITYPE arithmetic. If NEWTYPE is the
335 same as TYPE, however, the transformation may be
336 safe depending on the types involved: it is safe
337 if the ITYPE has strictly more than twice as many
338 mantissa bits as TYPE, can represent infinities
339 and NaNs if the TYPE can, and has sufficient
340 exponent range for the product or ratio of two
341 values representable in the TYPE to be within the
342 range of normal values of ITYPE. */
343 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
344 && (flag_unsafe_math_optimizations
345 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
346 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
348 && !excess_precision_type (newtype
))))
350 expr
= build2 (TREE_CODE (expr
), newtype
,
351 fold (convert_to_real (newtype
, arg0
)),
352 fold (convert_to_real (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 ((flag_float_store
369 || DECIMAL_FLOAT_TYPE_P (type
)
370 || DECIMAL_FLOAT_TYPE_P (itype
))
371 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
376 return build1 (FLOAT_EXPR
, type
, expr
);
378 case FIXED_POINT_TYPE
:
379 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
382 return convert (type
,
383 fold_build1 (REALPART_EXPR
,
384 TREE_TYPE (TREE_TYPE (expr
)), expr
));
388 error ("pointer value used where a floating point value was expected");
389 return convert_to_real (type
, integer_zero_node
);
392 error ("aggregate value used where a float was expected");
393 return convert_to_real (type
, integer_zero_node
);
397 /* Convert EXPR to some integer (or enum) type TYPE.
399 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
400 fixed-point or vector; in other cases error is called.
402 The result of this is always supposed to be a newly created tree node
403 not in use in any existing structure. */
406 convert_to_integer (tree type
, tree expr
)
408 enum tree_code ex_form
= TREE_CODE (expr
);
409 tree intype
= TREE_TYPE (expr
);
410 unsigned int inprec
= element_precision (intype
);
411 unsigned int outprec
= element_precision (type
);
412 location_t loc
= EXPR_LOCATION (expr
);
414 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
415 be. Consider `enum E = { a, b = (enum E) 3 };'. */
416 if (!COMPLETE_TYPE_P (type
))
418 error ("conversion to incomplete type");
419 return error_mark_node
;
422 if (ex_form
== COMPOUND_EXPR
)
424 tree t
= convert_to_integer (type
, TREE_OPERAND (expr
, 1));
425 if (t
== TREE_OPERAND (expr
, 1))
427 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
428 TREE_OPERAND (expr
, 0), t
);
431 /* Convert e.g. (long)round(d) -> lround(d). */
432 /* If we're converting to char, we may encounter differing behavior
433 between converting from double->char vs double->long->char.
434 We're in "undefined" territory but we prefer to be conservative,
435 so only proceed in "unsafe" math mode. */
437 && (flag_unsafe_math_optimizations
438 || (long_integer_type_node
439 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
441 tree s_expr
= strip_float_extensions (expr
);
442 tree s_intype
= TREE_TYPE (s_expr
);
443 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
448 CASE_FLT_FN (BUILT_IN_CEIL
):
449 /* Only convert in ISO C99 mode. */
450 if (!targetm
.libc_has_function (function_c99_misc
))
452 if (outprec
< TYPE_PRECISION (integer_type_node
)
453 || (outprec
== TYPE_PRECISION (integer_type_node
)
454 && !TYPE_UNSIGNED (type
)))
455 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
456 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
457 && !TYPE_UNSIGNED (type
))
458 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
459 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
460 && !TYPE_UNSIGNED (type
))
461 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
464 CASE_FLT_FN (BUILT_IN_FLOOR
):
465 /* Only convert in ISO C99 mode. */
466 if (!targetm
.libc_has_function (function_c99_misc
))
468 if (outprec
< TYPE_PRECISION (integer_type_node
)
469 || (outprec
== TYPE_PRECISION (integer_type_node
)
470 && !TYPE_UNSIGNED (type
)))
471 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
472 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
473 && !TYPE_UNSIGNED (type
))
474 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
475 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
476 && !TYPE_UNSIGNED (type
))
477 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
480 CASE_FLT_FN (BUILT_IN_ROUND
):
481 /* Only convert in ISO C99 mode and with -fno-math-errno. */
482 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
484 if (outprec
< TYPE_PRECISION (integer_type_node
)
485 || (outprec
== TYPE_PRECISION (integer_type_node
)
486 && !TYPE_UNSIGNED (type
)))
487 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
488 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
489 && !TYPE_UNSIGNED (type
))
490 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
491 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
492 && !TYPE_UNSIGNED (type
))
493 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
496 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
497 /* Only convert nearbyint* if we can ignore math exceptions. */
498 if (flag_trapping_math
)
500 /* ... Fall through ... */
501 CASE_FLT_FN (BUILT_IN_RINT
):
502 /* Only convert in ISO C99 mode and with -fno-math-errno. */
503 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
505 if (outprec
< TYPE_PRECISION (integer_type_node
)
506 || (outprec
== TYPE_PRECISION (integer_type_node
)
507 && !TYPE_UNSIGNED (type
)))
508 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
509 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
510 && !TYPE_UNSIGNED (type
))
511 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
512 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
513 && !TYPE_UNSIGNED (type
))
514 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
517 CASE_FLT_FN (BUILT_IN_TRUNC
):
518 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
526 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
527 return convert_to_integer (type
, newexpr
);
531 /* Convert (int)logb(d) -> ilogb(d). */
533 && flag_unsafe_math_optimizations
534 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
536 && (outprec
> TYPE_PRECISION (integer_type_node
)
537 || (outprec
== TYPE_PRECISION (integer_type_node
)
538 && !TYPE_UNSIGNED (type
))))
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_LOGB
):
548 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
557 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
558 return convert_to_integer (type
, newexpr
);
562 switch (TREE_CODE (intype
))
566 if (integer_zerop (expr
))
567 return build_int_cst (type
, 0);
569 /* Convert to an unsigned integer of the correct width first, and from
570 there widen/truncate to the required type. Some targets support the
571 coexistence of multiple valid pointer sizes, so fetch the one we need
573 expr
= fold_build1 (CONVERT_EXPR
,
574 lang_hooks
.types
.type_for_size
575 (TYPE_PRECISION (intype
), 0),
577 return fold_convert (type
, expr
);
583 /* If this is a logical operation, which just returns 0 or 1, we can
584 change the type of the expression. */
586 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
588 expr
= copy_node (expr
);
589 TREE_TYPE (expr
) = type
;
593 /* If we are widening the type, put in an explicit conversion.
594 Similarly if we are not changing the width. After this, we know
595 we are truncating EXPR. */
597 else if (outprec
>= inprec
)
601 /* If the precision of the EXPR's type is K bits and the
602 destination mode has more bits, and the sign is changing,
603 it is not safe to use a NOP_EXPR. For example, suppose
604 that EXPR's type is a 3-bit unsigned integer type, the
605 TYPE is a 3-bit signed integer type, and the machine mode
606 for the types is 8-bit QImode. In that case, the
607 conversion necessitates an explicit sign-extension. In
608 the signed-to-unsigned case the high-order bits have to
610 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
611 && (TYPE_PRECISION (TREE_TYPE (expr
))
612 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
617 return fold_build1 (code
, type
, expr
);
620 /* If TYPE is an enumeral type or a type with a precision less
621 than the number of bits in its mode, do the conversion to the
622 type corresponding to its mode, then do a nop conversion
624 else if (TREE_CODE (type
) == ENUMERAL_TYPE
625 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
626 return build1 (NOP_EXPR
, type
,
627 convert (lang_hooks
.types
.type_for_mode
628 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
631 /* Here detect when we can distribute the truncation down past some
632 arithmetic. For example, if adding two longs and converting to an
633 int, we can equally well convert both to ints and then add.
634 For the operations handled here, such truncation distribution
636 It is desirable in these cases:
637 1) when truncating down to full-word from a larger size
638 2) when truncating takes no work.
639 3) when at least one operand of the arithmetic has been extended
640 (as by C's default conversions). In this case we need two conversions
641 if we do the arithmetic as already requested, so we might as well
642 truncate both and then combine. Perhaps that way we need only one.
644 Note that in general we cannot do the arithmetic in a type
645 shorter than the desired result of conversion, even if the operands
646 are both extended from a shorter type, because they might overflow
647 if combined in that type. The exceptions to this--the times when
648 two narrow values can be combined in their narrow type even to
649 make a wider result--are handled by "shorten" in build_binary_op. */
654 /* We can pass truncation down through right shifting
655 when the shift count is a nonpositive constant. */
656 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
657 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
662 /* We can pass truncation down through left shifting
663 when the shift count is a nonnegative constant and
664 the target type is unsigned. */
665 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
666 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
667 && TYPE_UNSIGNED (type
)
668 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
670 /* If shift count is less than the width of the truncated type,
672 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
673 /* In this case, shifting is like multiplication. */
677 /* If it is >= that width, result is zero.
678 Handling this with trunc1 would give the wrong result:
679 (int) ((long long) a << 32) is well defined (as 0)
680 but (int) a << 32 is undefined and would get a
683 tree t
= build_int_cst (type
, 0);
685 /* If the original expression had side-effects, we must
687 if (TREE_SIDE_EFFECTS (expr
))
688 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
697 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
698 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
700 /* Don't distribute unless the output precision is at least as big
701 as the actual inputs and it has the same signedness. */
702 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
703 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
704 /* If signedness of arg0 and arg1 don't match,
705 we can't necessarily find a type to compare them in. */
706 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
707 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
708 /* Do not change the sign of the division. */
709 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
710 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
711 /* Either require unsigned division or a division by
712 a constant that is not -1. */
713 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
714 || (TREE_CODE (arg1
) == INTEGER_CST
715 && !integer_all_onesp (arg1
))))
724 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
725 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
727 /* Don't distribute unless the output precision is at least as big
728 as the actual inputs. Otherwise, the comparison of the
729 truncated values will be wrong. */
730 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
731 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
732 /* If signedness of arg0 and arg1 don't match,
733 we can't necessarily find a type to compare them in. */
734 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
735 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
747 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
748 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
750 /* Do not try to narrow operands of pointer subtraction;
751 that will interfere with other folding. */
752 if (ex_form
== MINUS_EXPR
753 && CONVERT_EXPR_P (arg0
)
754 && CONVERT_EXPR_P (arg1
)
755 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
756 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
759 if (outprec
>= BITS_PER_WORD
760 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
761 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
762 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
764 /* Do the arithmetic in type TYPEX,
765 then convert result to TYPE. */
768 /* Can't do arithmetic in enumeral types
769 so use an integer type that will hold the values. */
770 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
772 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
773 TYPE_UNSIGNED (typex
));
775 /* But now perhaps TYPEX is as wide as INPREC.
776 In that case, do nothing special here.
777 (Otherwise would recurse infinitely in convert. */
778 if (TYPE_PRECISION (typex
) != inprec
)
780 /* Don't do unsigned arithmetic where signed was wanted,
782 Exception: if both of the original operands were
783 unsigned then we can safely do the work as unsigned.
784 Exception: shift operations take their type solely
785 from the first argument.
786 Exception: the LSHIFT_EXPR case above requires that
787 we perform this operation unsigned lest we produce
788 signed-overflow undefinedness.
789 And we may need to do it as unsigned
790 if we truncate to the original size. */
791 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
792 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
793 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
794 || ex_form
== LSHIFT_EXPR
795 || ex_form
== RSHIFT_EXPR
796 || ex_form
== LROTATE_EXPR
797 || ex_form
== RROTATE_EXPR
))
798 || ex_form
== LSHIFT_EXPR
799 /* If we have !flag_wrapv, and either ARG0 or
800 ARG1 is of a signed type, we have to do
801 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
802 type in case the operation in outprec precision
803 could overflow. Otherwise, we would introduce
804 signed-overflow undefinedness. */
805 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
806 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
807 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
809 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
811 && (ex_form
== PLUS_EXPR
812 || ex_form
== MINUS_EXPR
813 || ex_form
== MULT_EXPR
)))
815 if (!TYPE_UNSIGNED (typex
))
816 typex
= unsigned_type_for (typex
);
820 if (TYPE_UNSIGNED (typex
))
821 typex
= signed_type_for (typex
);
823 return convert (type
,
824 fold_build2 (ex_form
, typex
,
825 convert (typex
, arg0
),
826 convert (typex
, arg1
)));
834 /* This is not correct for ABS_EXPR,
835 since we must test the sign before truncation. */
837 /* Do the arithmetic in type TYPEX,
838 then convert result to TYPE. */
841 /* Can't do arithmetic in enumeral types
842 so use an integer type that will hold the values. */
843 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
845 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
846 TYPE_UNSIGNED (typex
));
848 if (!TYPE_UNSIGNED (typex
))
849 typex
= unsigned_type_for (typex
);
850 return convert (type
,
851 fold_build1 (ex_form
, typex
,
853 TREE_OPERAND (expr
, 0))));
858 "can't convert between vector values of different size" error. */
859 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
860 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
861 != GET_MODE_SIZE (TYPE_MODE (type
))))
863 /* If truncating after truncating, might as well do all at once.
864 If truncating after extending, we may get rid of wasted work. */
865 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
868 /* It is sometimes worthwhile to push the narrowing down through
869 the conditional and never loses. A COND_EXPR may have a throw
870 as one operand, which then has void type. Just leave void
871 operands as they are. */
872 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
873 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
874 ? TREE_OPERAND (expr
, 1)
875 : convert (type
, TREE_OPERAND (expr
, 1)),
876 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
877 ? TREE_OPERAND (expr
, 2)
878 : convert (type
, TREE_OPERAND (expr
, 2)));
884 /* When parsing long initializers, we might end up with a lot of casts.
886 if (TREE_CODE (expr
) == INTEGER_CST
)
887 return fold_convert (type
, expr
);
888 return build1 (CONVERT_EXPR
, type
, expr
);
891 if (flag_sanitize
& SANITIZE_FLOAT_CAST
892 && do_ubsan_in_current_function ())
894 expr
= save_expr (expr
);
895 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
, expr
);
896 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
899 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), check
, expr
);
902 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
904 case FIXED_POINT_TYPE
:
905 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
908 return convert (type
,
909 fold_build1 (REALPART_EXPR
,
910 TREE_TYPE (TREE_TYPE (expr
)), expr
));
913 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
915 error ("can%'t convert a vector of type %qT"
916 " to type %qT which has different size",
917 TREE_TYPE (expr
), type
);
918 return error_mark_node
;
920 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
923 error ("aggregate value used where an integer was expected");
924 return convert (type
, integer_zero_node
);
928 /* Convert EXPR to the complex type TYPE in the usual ways. */
931 convert_to_complex (tree type
, tree expr
)
933 tree subtype
= TREE_TYPE (type
);
935 switch (TREE_CODE (TREE_TYPE (expr
)))
938 case FIXED_POINT_TYPE
:
942 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
943 convert (subtype
, integer_zero_node
));
947 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
949 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
951 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
953 tree t
= convert_to_complex (type
, TREE_OPERAND (expr
, 1));
954 if (t
== TREE_OPERAND (expr
, 1))
956 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
957 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
959 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
960 return fold_build2 (COMPLEX_EXPR
, type
,
961 convert (subtype
, TREE_OPERAND (expr
, 0)),
962 convert (subtype
, TREE_OPERAND (expr
, 1)));
965 expr
= save_expr (expr
);
967 fold_build2 (COMPLEX_EXPR
, type
,
969 fold_build1 (REALPART_EXPR
,
970 TREE_TYPE (TREE_TYPE (expr
)),
973 fold_build1 (IMAGPART_EXPR
,
974 TREE_TYPE (TREE_TYPE (expr
)),
981 error ("pointer value used where a complex was expected");
982 return convert_to_complex (type
, integer_zero_node
);
985 error ("aggregate value used where a complex was expected");
986 return convert_to_complex (type
, integer_zero_node
);
990 /* Convert EXPR to the vector type TYPE in the usual ways. */
993 convert_to_vector (tree type
, tree expr
)
995 switch (TREE_CODE (TREE_TYPE (expr
)))
999 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1001 error ("can%'t convert a value of type %qT"
1002 " to vector type %qT which has different size",
1003 TREE_TYPE (expr
), type
);
1004 return error_mark_node
;
1006 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1009 error ("can%'t convert value to a vector");
1010 return error_mark_node
;
1014 /* Convert EXPR to some fixed-point type TYPE.
1016 EXPR must be fixed-point, float, integer, or enumeral;
1017 in other cases error is called. */
1020 convert_to_fixed (tree type
, tree expr
)
1022 if (integer_zerop (expr
))
1024 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1025 return fixed_zero_node
;
1027 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1029 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1030 return fixed_one_node
;
1033 switch (TREE_CODE (TREE_TYPE (expr
)))
1035 case FIXED_POINT_TYPE
:
1040 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1043 return convert (type
,
1044 fold_build1 (REALPART_EXPR
,
1045 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1048 error ("aggregate value used where a fixed-point was expected");
1049 return error_mark_node
;