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"
35 #include "fold-const.h"
36 #include "stor-layout.h"
39 #include "diagnostic-core.h"
41 #include "langhooks.h"
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. */
50 convert_to_pointer (tree type
, tree expr
)
52 location_t loc
= EXPR_LOCATION (expr
);
53 if (TREE_TYPE (expr
) == type
)
56 switch (TREE_CODE (TREE_TYPE (expr
)))
61 /* If the pointers point to different address spaces, conversion needs
62 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
63 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
64 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
67 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
69 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
76 /* If the input precision differs from the target pointer type
77 precision, first convert the input expression to an integer type of
78 the target precision. Some targets, e.g. VMS, need several pointer
79 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
80 unsigned int pprec
= TYPE_PRECISION (type
);
81 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
84 expr
= fold_build1_loc (loc
, NOP_EXPR
,
85 lang_hooks
.types
.type_for_size (pprec
, 0),
89 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
92 error ("cannot convert to a pointer type");
93 return convert_to_pointer (type
, integer_zero_node
);
98 /* Convert EXPR to some floating-point type TYPE.
100 EXPR must be float, fixed-point, integer, or enumeral;
101 in other cases error is called. */
104 convert_to_real (tree type
, tree expr
)
106 enum built_in_function fcode
= builtin_mathfn_code (expr
);
107 tree itype
= TREE_TYPE (expr
);
109 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
111 tree t
= convert_to_real (type
, TREE_OPERAND (expr
, 1));
112 if (t
== TREE_OPERAND (expr
, 1))
114 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
115 TREE_OPERAND (expr
, 0), t
);
118 /* Disable until we figure out how to decide whether the functions are
119 present in runtime. */
120 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
122 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
123 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
127 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
142 /* The above functions may set errno differently with float
143 input or output so this transformation is not safe with
164 /* The above functions are not safe to do this conversion. */
165 if (!flag_unsafe_math_optimizations
)
172 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
175 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
176 the both as the safe type for operation. */
177 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
178 newtype
= TREE_TYPE (arg0
);
180 /* We consider to convert
182 (T1) sqrtT2 ((T2) exprT3)
184 (T1) sqrtT4 ((T4) exprT3)
186 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
187 and T4 is NEWTYPE. All those types are of floating point types.
188 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
189 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
190 T2 and T4. See the following URL for a reference:
191 http://stackoverflow.com/questions/9235456/determining-
192 floating-point-square-root
194 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
195 && !flag_unsafe_math_optimizations
)
197 /* The following conversion is unsafe even the precision condition
200 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
202 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
205 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
206 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
211 /* Be careful about integer to fp conversions.
212 These may overflow still. */
213 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
214 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
215 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
216 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
218 tree fn
= mathfn_built_in (newtype
, fcode
);
222 tree arg
= fold (convert_to_real (newtype
, arg0
));
223 expr
= build_call_expr (fn
, 1, arg
);
234 && (((fcode
== BUILT_IN_FLOORL
235 || fcode
== BUILT_IN_CEILL
236 || fcode
== BUILT_IN_ROUNDL
237 || fcode
== BUILT_IN_RINTL
238 || fcode
== BUILT_IN_TRUNCL
239 || fcode
== BUILT_IN_NEARBYINTL
)
240 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
241 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
242 || ((fcode
== BUILT_IN_FLOOR
243 || fcode
== BUILT_IN_CEIL
244 || fcode
== BUILT_IN_ROUND
245 || fcode
== BUILT_IN_RINT
246 || fcode
== BUILT_IN_TRUNC
247 || fcode
== BUILT_IN_NEARBYINT
)
248 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
250 tree fn
= mathfn_built_in (type
, fcode
);
254 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
256 /* Make sure (type)arg0 is an extension, otherwise we could end up
257 changing (float)floor(double d) into floorf((float)d), which is
258 incorrect because (float)d uses round-to-nearest and can round
259 up to the next integer. */
260 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
261 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
265 /* Propagate the cast into the operation. */
266 if (itype
!= type
&& FLOAT_TYPE_P (type
))
267 switch (TREE_CODE (expr
))
269 /* Convert (float)-x into -(float)x. This is safe for
270 round-to-nearest rounding mode when the inner type is float. */
273 if (!flag_rounding_math
274 && FLOAT_TYPE_P (itype
)
275 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
276 return build1 (TREE_CODE (expr
), type
,
277 fold (convert_to_real (type
,
278 TREE_OPERAND (expr
, 0))));
280 /* Convert (outertype)((innertype0)a+(innertype1)b)
281 into ((newtype)a+(newtype)b) where newtype
282 is the widest mode from all of these. */
288 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
289 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
291 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
292 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
293 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
297 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
298 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
299 || TYPE_MODE (type
) == SDmode
)
300 newtype
= dfloat32_type_node
;
301 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
302 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
303 || TYPE_MODE (type
) == DDmode
)
304 newtype
= dfloat64_type_node
;
305 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
306 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
307 || TYPE_MODE (type
) == TDmode
)
308 newtype
= dfloat128_type_node
;
309 if (newtype
== dfloat32_type_node
310 || newtype
== dfloat64_type_node
311 || newtype
== dfloat128_type_node
)
313 expr
= build2 (TREE_CODE (expr
), newtype
,
314 fold (convert_to_real (newtype
, arg0
)),
315 fold (convert_to_real (newtype
, arg1
)));
321 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
322 newtype
= TREE_TYPE (arg0
);
323 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
324 newtype
= TREE_TYPE (arg1
);
325 /* Sometimes this transformation is safe (cannot
326 change results through affecting double rounding
327 cases) and sometimes it is not. If NEWTYPE is
328 wider than TYPE, e.g. (float)((long double)double
329 + (long double)double) converted to
330 (float)(double + double), the transformation is
331 unsafe regardless of the details of the types
332 involved; double rounding can arise if the result
333 of NEWTYPE arithmetic is a NEWTYPE value half way
334 between two representable TYPE values but the
335 exact value is sufficiently different (in the
336 right direction) for this difference to be
337 visible in ITYPE arithmetic. If NEWTYPE is the
338 same as TYPE, however, the transformation may be
339 safe depending on the types involved: it is safe
340 if the ITYPE has strictly more than twice as many
341 mantissa bits as TYPE, can represent infinities
342 and NaNs if the TYPE can, and has sufficient
343 exponent range for the product or ratio of two
344 values representable in the TYPE to be within the
345 range of normal values of ITYPE. */
346 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
347 && (flag_unsafe_math_optimizations
348 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
349 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
351 && !excess_precision_type (newtype
))))
353 expr
= build2 (TREE_CODE (expr
), newtype
,
354 fold (convert_to_real (newtype
, arg0
)),
355 fold (convert_to_real (newtype
, arg1
)));
366 switch (TREE_CODE (TREE_TYPE (expr
)))
369 /* Ignore the conversion if we don't need to store intermediate
370 results and neither type is a decimal float. */
371 return build1 ((flag_float_store
372 || DECIMAL_FLOAT_TYPE_P (type
)
373 || DECIMAL_FLOAT_TYPE_P (itype
))
374 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
379 return build1 (FLOAT_EXPR
, type
, expr
);
381 case FIXED_POINT_TYPE
:
382 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
385 return convert (type
,
386 fold_build1 (REALPART_EXPR
,
387 TREE_TYPE (TREE_TYPE (expr
)), expr
));
391 error ("pointer value used where a floating point value was expected");
392 return convert_to_real (type
, integer_zero_node
);
395 error ("aggregate value used where a float was expected");
396 return convert_to_real (type
, integer_zero_node
);
400 /* Convert EXPR to some integer (or enum) type TYPE.
402 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
403 fixed-point or vector; in other cases error is called.
405 The result of this is always supposed to be a newly created tree node
406 not in use in any existing structure. */
409 convert_to_integer (tree type
, tree expr
)
411 enum tree_code ex_form
= TREE_CODE (expr
);
412 tree intype
= TREE_TYPE (expr
);
413 unsigned int inprec
= element_precision (intype
);
414 unsigned int outprec
= element_precision (type
);
415 location_t loc
= EXPR_LOCATION (expr
);
417 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
418 be. Consider `enum E = { a, b = (enum E) 3 };'. */
419 if (!COMPLETE_TYPE_P (type
))
421 error ("conversion to incomplete type");
422 return error_mark_node
;
425 if (ex_form
== COMPOUND_EXPR
)
427 tree t
= convert_to_integer (type
, TREE_OPERAND (expr
, 1));
428 if (t
== TREE_OPERAND (expr
, 1))
430 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
431 TREE_OPERAND (expr
, 0), t
);
434 /* Convert e.g. (long)round(d) -> lround(d). */
435 /* If we're converting to char, we may encounter differing behavior
436 between converting from double->char vs double->long->char.
437 We're in "undefined" territory but we prefer to be conservative,
438 so only proceed in "unsafe" math mode. */
440 && (flag_unsafe_math_optimizations
441 || (long_integer_type_node
442 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
444 tree s_expr
= strip_float_extensions (expr
);
445 tree s_intype
= TREE_TYPE (s_expr
);
446 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
451 CASE_FLT_FN (BUILT_IN_CEIL
):
452 /* Only convert in ISO C99 mode. */
453 if (!targetm
.libc_has_function (function_c99_misc
))
455 if (outprec
< TYPE_PRECISION (integer_type_node
)
456 || (outprec
== TYPE_PRECISION (integer_type_node
)
457 && !TYPE_UNSIGNED (type
)))
458 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
459 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
460 && !TYPE_UNSIGNED (type
))
461 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
462 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
463 && !TYPE_UNSIGNED (type
))
464 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
467 CASE_FLT_FN (BUILT_IN_FLOOR
):
468 /* Only convert in ISO C99 mode. */
469 if (!targetm
.libc_has_function (function_c99_misc
))
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_IFLOOR
);
475 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
476 && !TYPE_UNSIGNED (type
))
477 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
478 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
479 && !TYPE_UNSIGNED (type
))
480 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
483 CASE_FLT_FN (BUILT_IN_ROUND
):
484 /* Only convert in ISO C99 mode and with -fno-math-errno. */
485 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
487 if (outprec
< TYPE_PRECISION (integer_type_node
)
488 || (outprec
== TYPE_PRECISION (integer_type_node
)
489 && !TYPE_UNSIGNED (type
)))
490 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
491 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
492 && !TYPE_UNSIGNED (type
))
493 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
494 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
495 && !TYPE_UNSIGNED (type
))
496 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
499 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
500 /* Only convert nearbyint* if we can ignore math exceptions. */
501 if (flag_trapping_math
)
503 /* ... Fall through ... */
504 CASE_FLT_FN (BUILT_IN_RINT
):
505 /* Only convert in ISO C99 mode and with -fno-math-errno. */
506 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
508 if (outprec
< TYPE_PRECISION (integer_type_node
)
509 || (outprec
== TYPE_PRECISION (integer_type_node
)
510 && !TYPE_UNSIGNED (type
)))
511 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
512 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
513 && !TYPE_UNSIGNED (type
))
514 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
515 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
516 && !TYPE_UNSIGNED (type
))
517 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
520 CASE_FLT_FN (BUILT_IN_TRUNC
):
521 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
529 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
530 return convert_to_integer (type
, newexpr
);
534 /* Convert (int)logb(d) -> ilogb(d). */
536 && flag_unsafe_math_optimizations
537 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
539 && (outprec
> TYPE_PRECISION (integer_type_node
)
540 || (outprec
== TYPE_PRECISION (integer_type_node
)
541 && !TYPE_UNSIGNED (type
))))
543 tree s_expr
= strip_float_extensions (expr
);
544 tree s_intype
= TREE_TYPE (s_expr
);
545 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
550 CASE_FLT_FN (BUILT_IN_LOGB
):
551 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
560 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
561 return convert_to_integer (type
, newexpr
);
565 switch (TREE_CODE (intype
))
569 if (integer_zerop (expr
))
570 return build_int_cst (type
, 0);
572 /* Convert to an unsigned integer of the correct width first, and from
573 there widen/truncate to the required type. Some targets support the
574 coexistence of multiple valid pointer sizes, so fetch the one we need
576 expr
= fold_build1 (CONVERT_EXPR
,
577 lang_hooks
.types
.type_for_size
578 (TYPE_PRECISION (intype
), 0),
580 return fold_convert (type
, expr
);
586 /* If this is a logical operation, which just returns 0 or 1, we can
587 change the type of the expression. */
589 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
591 expr
= copy_node (expr
);
592 TREE_TYPE (expr
) = type
;
596 /* If we are widening the type, put in an explicit conversion.
597 Similarly if we are not changing the width. After this, we know
598 we are truncating EXPR. */
600 else if (outprec
>= inprec
)
604 /* If the precision of the EXPR's type is K bits and the
605 destination mode has more bits, and the sign is changing,
606 it is not safe to use a NOP_EXPR. For example, suppose
607 that EXPR's type is a 3-bit unsigned integer type, the
608 TYPE is a 3-bit signed integer type, and the machine mode
609 for the types is 8-bit QImode. In that case, the
610 conversion necessitates an explicit sign-extension. In
611 the signed-to-unsigned case the high-order bits have to
613 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
614 && (TYPE_PRECISION (TREE_TYPE (expr
))
615 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
620 return fold_build1 (code
, type
, expr
);
623 /* If TYPE is an enumeral type or a type with a precision less
624 than the number of bits in its mode, do the conversion to the
625 type corresponding to its mode, then do a nop conversion
627 else if (TREE_CODE (type
) == ENUMERAL_TYPE
628 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
629 return build1 (NOP_EXPR
, type
,
630 convert (lang_hooks
.types
.type_for_mode
631 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
634 /* Here detect when we can distribute the truncation down past some
635 arithmetic. For example, if adding two longs and converting to an
636 int, we can equally well convert both to ints and then add.
637 For the operations handled here, such truncation distribution
639 It is desirable in these cases:
640 1) when truncating down to full-word from a larger size
641 2) when truncating takes no work.
642 3) when at least one operand of the arithmetic has been extended
643 (as by C's default conversions). In this case we need two conversions
644 if we do the arithmetic as already requested, so we might as well
645 truncate both and then combine. Perhaps that way we need only one.
647 Note that in general we cannot do the arithmetic in a type
648 shorter than the desired result of conversion, even if the operands
649 are both extended from a shorter type, because they might overflow
650 if combined in that type. The exceptions to this--the times when
651 two narrow values can be combined in their narrow type even to
652 make a wider result--are handled by "shorten" in build_binary_op. */
657 /* We can pass truncation down through right shifting
658 when the shift count is a nonpositive constant. */
659 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
660 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
665 /* We can pass truncation down through left shifting
666 when the shift count is a nonnegative constant and
667 the target type is unsigned. */
668 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
669 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
670 && TYPE_UNSIGNED (type
)
671 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
673 /* If shift count is less than the width of the truncated type,
675 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
676 /* In this case, shifting is like multiplication. */
680 /* If it is >= that width, result is zero.
681 Handling this with trunc1 would give the wrong result:
682 (int) ((long long) a << 32) is well defined (as 0)
683 but (int) a << 32 is undefined and would get a
686 tree t
= build_int_cst (type
, 0);
688 /* If the original expression had side-effects, we must
690 if (TREE_SIDE_EFFECTS (expr
))
691 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
700 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
701 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
703 /* Don't distribute unless the output precision is at least as big
704 as the actual inputs and it has the same signedness. */
705 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
706 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
707 /* If signedness of arg0 and arg1 don't match,
708 we can't necessarily find a type to compare them in. */
709 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
710 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
711 /* Do not change the sign of the division. */
712 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
713 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
714 /* Either require unsigned division or a division by
715 a constant that is not -1. */
716 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
717 || (TREE_CODE (arg1
) == INTEGER_CST
718 && !integer_all_onesp (arg1
))))
727 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
728 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
730 /* Don't distribute unless the output precision is at least as big
731 as the actual inputs. Otherwise, the comparison of the
732 truncated values will be wrong. */
733 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
734 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
735 /* If signedness of arg0 and arg1 don't match,
736 we can't necessarily find a type to compare them in. */
737 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
738 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
750 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
751 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
753 /* Do not try to narrow operands of pointer subtraction;
754 that will interfere with other folding. */
755 if (ex_form
== MINUS_EXPR
756 && CONVERT_EXPR_P (arg0
)
757 && CONVERT_EXPR_P (arg1
)
758 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
759 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
762 if (outprec
>= BITS_PER_WORD
763 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
764 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
765 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
767 /* Do the arithmetic in type TYPEX,
768 then convert result to TYPE. */
771 /* Can't do arithmetic in enumeral types
772 so use an integer type that will hold the values. */
773 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
775 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
776 TYPE_UNSIGNED (typex
));
778 /* But now perhaps TYPEX is as wide as INPREC.
779 In that case, do nothing special here.
780 (Otherwise would recurse infinitely in convert. */
781 if (TYPE_PRECISION (typex
) != inprec
)
783 /* Don't do unsigned arithmetic where signed was wanted,
785 Exception: if both of the original operands were
786 unsigned then we can safely do the work as unsigned.
787 Exception: shift operations take their type solely
788 from the first argument.
789 Exception: the LSHIFT_EXPR case above requires that
790 we perform this operation unsigned lest we produce
791 signed-overflow undefinedness.
792 And we may need to do it as unsigned
793 if we truncate to the original size. */
794 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
795 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
796 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
797 || ex_form
== LSHIFT_EXPR
798 || ex_form
== RSHIFT_EXPR
799 || ex_form
== LROTATE_EXPR
800 || ex_form
== RROTATE_EXPR
))
801 || ex_form
== LSHIFT_EXPR
802 /* If we have !flag_wrapv, and either ARG0 or
803 ARG1 is of a signed type, we have to do
804 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
805 type in case the operation in outprec precision
806 could overflow. Otherwise, we would introduce
807 signed-overflow undefinedness. */
808 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
809 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
810 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
812 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
814 && (ex_form
== PLUS_EXPR
815 || ex_form
== MINUS_EXPR
816 || ex_form
== MULT_EXPR
)))
818 if (!TYPE_UNSIGNED (typex
))
819 typex
= unsigned_type_for (typex
);
823 if (TYPE_UNSIGNED (typex
))
824 typex
= signed_type_for (typex
);
826 return convert (type
,
827 fold_build2 (ex_form
, typex
,
828 convert (typex
, arg0
),
829 convert (typex
, arg1
)));
837 /* This is not correct for ABS_EXPR,
838 since we must test the sign before truncation. */
840 /* Do the arithmetic in type TYPEX,
841 then convert result to TYPE. */
844 /* Can't do arithmetic in enumeral types
845 so use an integer type that will hold the values. */
846 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
848 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
849 TYPE_UNSIGNED (typex
));
851 if (!TYPE_UNSIGNED (typex
))
852 typex
= unsigned_type_for (typex
);
853 return convert (type
,
854 fold_build1 (ex_form
, typex
,
856 TREE_OPERAND (expr
, 0))));
861 "can't convert between vector values of different size" error. */
862 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
863 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
864 != GET_MODE_SIZE (TYPE_MODE (type
))))
866 /* If truncating after truncating, might as well do all at once.
867 If truncating after extending, we may get rid of wasted work. */
868 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
871 /* It is sometimes worthwhile to push the narrowing down through
872 the conditional and never loses. A COND_EXPR may have a throw
873 as one operand, which then has void type. Just leave void
874 operands as they are. */
875 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
876 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
877 ? TREE_OPERAND (expr
, 1)
878 : convert (type
, TREE_OPERAND (expr
, 1)),
879 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
880 ? TREE_OPERAND (expr
, 2)
881 : convert (type
, TREE_OPERAND (expr
, 2)));
887 /* When parsing long initializers, we might end up with a lot of casts.
889 if (TREE_CODE (expr
) == INTEGER_CST
)
890 return fold_convert (type
, expr
);
891 return build1 (CONVERT_EXPR
, type
, expr
);
894 if (flag_sanitize
& SANITIZE_FLOAT_CAST
895 && do_ubsan_in_current_function ())
897 expr
= save_expr (expr
);
898 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
, expr
);
899 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
902 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), check
, expr
);
905 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
907 case FIXED_POINT_TYPE
:
908 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
911 return convert (type
,
912 fold_build1 (REALPART_EXPR
,
913 TREE_TYPE (TREE_TYPE (expr
)), expr
));
916 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
918 error ("can%'t convert a vector of type %qT"
919 " to type %qT which has different size",
920 TREE_TYPE (expr
), type
);
921 return error_mark_node
;
923 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
926 error ("aggregate value used where an integer was expected");
927 return convert (type
, integer_zero_node
);
931 /* Convert EXPR to the complex type TYPE in the usual ways. */
934 convert_to_complex (tree type
, tree expr
)
936 tree subtype
= TREE_TYPE (type
);
938 switch (TREE_CODE (TREE_TYPE (expr
)))
941 case FIXED_POINT_TYPE
:
945 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
946 convert (subtype
, integer_zero_node
));
950 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
952 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
954 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
956 tree t
= convert_to_complex (type
, TREE_OPERAND (expr
, 1));
957 if (t
== TREE_OPERAND (expr
, 1))
959 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
960 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
962 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
963 return fold_build2 (COMPLEX_EXPR
, type
,
964 convert (subtype
, TREE_OPERAND (expr
, 0)),
965 convert (subtype
, TREE_OPERAND (expr
, 1)));
968 expr
= save_expr (expr
);
970 fold_build2 (COMPLEX_EXPR
, type
,
972 fold_build1 (REALPART_EXPR
,
973 TREE_TYPE (TREE_TYPE (expr
)),
976 fold_build1 (IMAGPART_EXPR
,
977 TREE_TYPE (TREE_TYPE (expr
)),
984 error ("pointer value used where a complex was expected");
985 return convert_to_complex (type
, integer_zero_node
);
988 error ("aggregate value used where a complex was expected");
989 return convert_to_complex (type
, integer_zero_node
);
993 /* Convert EXPR to the vector type TYPE in the usual ways. */
996 convert_to_vector (tree type
, tree expr
)
998 switch (TREE_CODE (TREE_TYPE (expr
)))
1002 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
1004 error ("can%'t convert a value of type %qT"
1005 " to vector type %qT which has different size",
1006 TREE_TYPE (expr
), type
);
1007 return error_mark_node
;
1009 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1012 error ("can%'t convert value to a vector");
1013 return error_mark_node
;
1017 /* Convert EXPR to some fixed-point type TYPE.
1019 EXPR must be fixed-point, float, integer, or enumeral;
1020 in other cases error is called. */
1023 convert_to_fixed (tree type
, tree expr
)
1025 if (integer_zerop (expr
))
1027 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1028 return fixed_zero_node
;
1030 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1032 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1033 return fixed_one_node
;
1036 switch (TREE_CODE (TREE_TYPE (expr
)))
1038 case FIXED_POINT_TYPE
:
1043 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1046 return convert (type
,
1047 fold_build1 (REALPART_EXPR
,
1048 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1051 error ("aggregate value used where a fixed-point was expected");
1052 return error_mark_node
;