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 "stor-layout.h"
32 #include "diagnostic-core.h"
34 #include "langhooks.h"
38 /* Convert EXPR to some pointer or reference type TYPE.
39 EXPR must be pointer, reference, integer, enumeral, or literal zero;
40 in other cases error is called. */
43 convert_to_pointer (tree type
, tree expr
)
45 location_t loc
= EXPR_LOCATION (expr
);
46 if (TREE_TYPE (expr
) == type
)
49 switch (TREE_CODE (TREE_TYPE (expr
)))
54 /* If the pointers point to different address spaces, conversion needs
55 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
56 addr_space_t to_as
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
57 addr_space_t from_as
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr
)));
60 return fold_build1_loc (loc
, NOP_EXPR
, type
, expr
);
62 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, expr
);
69 /* If the input precision differs from the target pointer type
70 precision, first convert the input expression to an integer type of
71 the target precision. Some targets, e.g. VMS, need several pointer
72 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
73 unsigned int pprec
= TYPE_PRECISION (type
);
74 unsigned int eprec
= TYPE_PRECISION (TREE_TYPE (expr
));
77 expr
= fold_build1_loc (loc
, NOP_EXPR
,
78 lang_hooks
.types
.type_for_size (pprec
, 0),
82 return fold_build1_loc (loc
, CONVERT_EXPR
, type
, expr
);
85 error ("cannot convert to a pointer type");
86 return convert_to_pointer (type
, integer_zero_node
);
91 /* Convert EXPR to some floating-point type TYPE.
93 EXPR must be float, fixed-point, integer, or enumeral;
94 in other cases error is called. */
97 convert_to_real (tree type
, tree expr
)
99 enum built_in_function fcode
= builtin_mathfn_code (expr
);
100 tree itype
= TREE_TYPE (expr
);
102 if (TREE_CODE (expr
) == COMPOUND_EXPR
)
104 tree t
= convert_to_real (type
, TREE_OPERAND (expr
, 1));
105 if (t
== TREE_OPERAND (expr
, 1))
107 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
108 TREE_OPERAND (expr
, 0), t
);
111 /* Disable until we figure out how to decide whether the functions are
112 present in runtime. */
113 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
115 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
116 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
120 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
135 /* The above functions may set errno differently with float
136 input or output so this transformation is not safe with
157 /* The above functions are not safe to do this conversion. */
158 if (!flag_unsafe_math_optimizations
)
165 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
168 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
169 the both as the safe type for operation. */
170 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
171 newtype
= TREE_TYPE (arg0
);
173 /* We consider to convert
175 (T1) sqrtT2 ((T2) exprT3)
177 (T1) sqrtT4 ((T4) exprT3)
179 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
180 and T4 is NEWTYPE. All those types are of floating point types.
181 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
182 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
183 T2 and T4. See the following URL for a reference:
184 http://stackoverflow.com/questions/9235456/determining-
185 floating-point-square-root
187 if ((fcode
== BUILT_IN_SQRT
|| fcode
== BUILT_IN_SQRTL
)
188 && !flag_unsafe_math_optimizations
)
190 /* The following conversion is unsafe even the precision condition
193 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
195 if (TYPE_MODE (type
) != TYPE_MODE (newtype
))
198 int p1
= REAL_MODE_FORMAT (TYPE_MODE (itype
))->p
;
199 int p2
= REAL_MODE_FORMAT (TYPE_MODE (newtype
))->p
;
204 /* Be careful about integer to fp conversions.
205 These may overflow still. */
206 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
207 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
208 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
209 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
211 tree fn
= mathfn_built_in (newtype
, fcode
);
215 tree arg
= fold (convert_to_real (newtype
, arg0
));
216 expr
= build_call_expr (fn
, 1, arg
);
227 && (((fcode
== BUILT_IN_FLOORL
228 || fcode
== BUILT_IN_CEILL
229 || fcode
== BUILT_IN_ROUNDL
230 || fcode
== BUILT_IN_RINTL
231 || fcode
== BUILT_IN_TRUNCL
232 || fcode
== BUILT_IN_NEARBYINTL
)
233 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
234 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
235 || ((fcode
== BUILT_IN_FLOOR
236 || fcode
== BUILT_IN_CEIL
237 || fcode
== BUILT_IN_ROUND
238 || fcode
== BUILT_IN_RINT
239 || fcode
== BUILT_IN_TRUNC
240 || fcode
== BUILT_IN_NEARBYINT
)
241 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
243 tree fn
= mathfn_built_in (type
, fcode
);
247 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
249 /* Make sure (type)arg0 is an extension, otherwise we could end up
250 changing (float)floor(double d) into floorf((float)d), which is
251 incorrect because (float)d uses round-to-nearest and can round
252 up to the next integer. */
253 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
254 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
258 /* Propagate the cast into the operation. */
259 if (itype
!= type
&& FLOAT_TYPE_P (type
))
260 switch (TREE_CODE (expr
))
262 /* Convert (float)-x into -(float)x. This is safe for
263 round-to-nearest rounding mode when the inner type is float. */
266 if (!flag_rounding_math
267 && FLOAT_TYPE_P (itype
)
268 && TYPE_PRECISION (type
) < TYPE_PRECISION (itype
))
269 return build1 (TREE_CODE (expr
), type
,
270 fold (convert_to_real (type
,
271 TREE_OPERAND (expr
, 0))));
273 /* Convert (outertype)((innertype0)a+(innertype1)b)
274 into ((newtype)a+(newtype)b) where newtype
275 is the widest mode from all of these. */
281 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
282 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
284 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
285 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
286 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
290 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
291 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
292 || TYPE_MODE (type
) == SDmode
)
293 newtype
= dfloat32_type_node
;
294 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
295 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
296 || TYPE_MODE (type
) == DDmode
)
297 newtype
= dfloat64_type_node
;
298 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
299 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
300 || TYPE_MODE (type
) == TDmode
)
301 newtype
= dfloat128_type_node
;
302 if (newtype
== dfloat32_type_node
303 || newtype
== dfloat64_type_node
304 || newtype
== dfloat128_type_node
)
306 expr
= build2 (TREE_CODE (expr
), newtype
,
307 fold (convert_to_real (newtype
, arg0
)),
308 fold (convert_to_real (newtype
, arg1
)));
314 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
315 newtype
= TREE_TYPE (arg0
);
316 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
317 newtype
= TREE_TYPE (arg1
);
318 /* Sometimes this transformation is safe (cannot
319 change results through affecting double rounding
320 cases) and sometimes it is not. If NEWTYPE is
321 wider than TYPE, e.g. (float)((long double)double
322 + (long double)double) converted to
323 (float)(double + double), the transformation is
324 unsafe regardless of the details of the types
325 involved; double rounding can arise if the result
326 of NEWTYPE arithmetic is a NEWTYPE value half way
327 between two representable TYPE values but the
328 exact value is sufficiently different (in the
329 right direction) for this difference to be
330 visible in ITYPE arithmetic. If NEWTYPE is the
331 same as TYPE, however, the transformation may be
332 safe depending on the types involved: it is safe
333 if the ITYPE has strictly more than twice as many
334 mantissa bits as TYPE, can represent infinities
335 and NaNs if the TYPE can, and has sufficient
336 exponent range for the product or ratio of two
337 values representable in the TYPE to be within the
338 range of normal values of ITYPE. */
339 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
340 && (flag_unsafe_math_optimizations
341 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
342 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
344 && !excess_precision_type (newtype
))))
346 expr
= build2 (TREE_CODE (expr
), newtype
,
347 fold (convert_to_real (newtype
, arg0
)),
348 fold (convert_to_real (newtype
, arg1
)));
359 switch (TREE_CODE (TREE_TYPE (expr
)))
362 /* Ignore the conversion if we don't need to store intermediate
363 results and neither type is a decimal float. */
364 return build1 ((flag_float_store
365 || DECIMAL_FLOAT_TYPE_P (type
)
366 || DECIMAL_FLOAT_TYPE_P (itype
))
367 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
372 return build1 (FLOAT_EXPR
, type
, expr
);
374 case FIXED_POINT_TYPE
:
375 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
378 return convert (type
,
379 fold_build1 (REALPART_EXPR
,
380 TREE_TYPE (TREE_TYPE (expr
)), expr
));
384 error ("pointer value used where a floating point value was expected");
385 return convert_to_real (type
, integer_zero_node
);
388 error ("aggregate value used where a float was expected");
389 return convert_to_real (type
, integer_zero_node
);
393 /* Convert EXPR to some integer (or enum) type TYPE.
395 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
396 fixed-point or vector; in other cases error is called.
398 The result of this is always supposed to be a newly created tree node
399 not in use in any existing structure. */
402 convert_to_integer (tree type
, tree expr
)
404 enum tree_code ex_form
= TREE_CODE (expr
);
405 tree intype
= TREE_TYPE (expr
);
406 unsigned int inprec
= element_precision (intype
);
407 unsigned int outprec
= element_precision (type
);
408 location_t loc
= EXPR_LOCATION (expr
);
410 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
411 be. Consider `enum E = { a, b = (enum E) 3 };'. */
412 if (!COMPLETE_TYPE_P (type
))
414 error ("conversion to incomplete type");
415 return error_mark_node
;
418 if (ex_form
== COMPOUND_EXPR
)
420 tree t
= convert_to_integer (type
, TREE_OPERAND (expr
, 1));
421 if (t
== TREE_OPERAND (expr
, 1))
423 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
, TREE_TYPE (t
),
424 TREE_OPERAND (expr
, 0), t
);
427 /* Convert e.g. (long)round(d) -> lround(d). */
428 /* If we're converting to char, we may encounter differing behavior
429 between converting from double->char vs double->long->char.
430 We're in "undefined" territory but we prefer to be conservative,
431 so only proceed in "unsafe" math mode. */
433 && (flag_unsafe_math_optimizations
434 || (long_integer_type_node
435 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
437 tree s_expr
= strip_float_extensions (expr
);
438 tree s_intype
= TREE_TYPE (s_expr
);
439 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
444 CASE_FLT_FN (BUILT_IN_CEIL
):
445 /* Only convert in ISO C99 mode. */
446 if (!targetm
.libc_has_function (function_c99_misc
))
448 if (outprec
< TYPE_PRECISION (integer_type_node
)
449 || (outprec
== TYPE_PRECISION (integer_type_node
)
450 && !TYPE_UNSIGNED (type
)))
451 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
452 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
453 && !TYPE_UNSIGNED (type
))
454 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
455 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
456 && !TYPE_UNSIGNED (type
))
457 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
460 CASE_FLT_FN (BUILT_IN_FLOOR
):
461 /* Only convert in ISO C99 mode. */
462 if (!targetm
.libc_has_function (function_c99_misc
))
464 if (outprec
< TYPE_PRECISION (integer_type_node
)
465 || (outprec
== TYPE_PRECISION (integer_type_node
)
466 && !TYPE_UNSIGNED (type
)))
467 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
468 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
469 && !TYPE_UNSIGNED (type
))
470 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
471 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
472 && !TYPE_UNSIGNED (type
))
473 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
476 CASE_FLT_FN (BUILT_IN_ROUND
):
477 /* Only convert in ISO C99 mode and with -fno-math-errno. */
478 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
480 if (outprec
< TYPE_PRECISION (integer_type_node
)
481 || (outprec
== TYPE_PRECISION (integer_type_node
)
482 && !TYPE_UNSIGNED (type
)))
483 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
484 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
485 && !TYPE_UNSIGNED (type
))
486 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
487 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
488 && !TYPE_UNSIGNED (type
))
489 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
492 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
493 /* Only convert nearbyint* if we can ignore math exceptions. */
494 if (flag_trapping_math
)
496 /* ... Fall through ... */
497 CASE_FLT_FN (BUILT_IN_RINT
):
498 /* Only convert in ISO C99 mode and with -fno-math-errno. */
499 if (!targetm
.libc_has_function (function_c99_misc
) || flag_errno_math
)
501 if (outprec
< TYPE_PRECISION (integer_type_node
)
502 || (outprec
== TYPE_PRECISION (integer_type_node
)
503 && !TYPE_UNSIGNED (type
)))
504 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
505 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
506 && !TYPE_UNSIGNED (type
))
507 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
508 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
509 && !TYPE_UNSIGNED (type
))
510 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
513 CASE_FLT_FN (BUILT_IN_TRUNC
):
514 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
522 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
523 return convert_to_integer (type
, newexpr
);
527 /* Convert (int)logb(d) -> ilogb(d). */
529 && flag_unsafe_math_optimizations
530 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
532 && (outprec
> TYPE_PRECISION (integer_type_node
)
533 || (outprec
== TYPE_PRECISION (integer_type_node
)
534 && !TYPE_UNSIGNED (type
))))
536 tree s_expr
= strip_float_extensions (expr
);
537 tree s_intype
= TREE_TYPE (s_expr
);
538 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
543 CASE_FLT_FN (BUILT_IN_LOGB
):
544 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
553 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
554 return convert_to_integer (type
, newexpr
);
558 switch (TREE_CODE (intype
))
562 if (integer_zerop (expr
))
563 return build_int_cst (type
, 0);
565 /* Convert to an unsigned integer of the correct width first, and from
566 there widen/truncate to the required type. Some targets support the
567 coexistence of multiple valid pointer sizes, so fetch the one we need
569 expr
= fold_build1 (CONVERT_EXPR
,
570 lang_hooks
.types
.type_for_size
571 (TYPE_PRECISION (intype
), 0),
573 return fold_convert (type
, expr
);
579 /* If this is a logical operation, which just returns 0 or 1, we can
580 change the type of the expression. */
582 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
584 expr
= copy_node (expr
);
585 TREE_TYPE (expr
) = type
;
589 /* If we are widening the type, put in an explicit conversion.
590 Similarly if we are not changing the width. After this, we know
591 we are truncating EXPR. */
593 else if (outprec
>= inprec
)
597 /* If the precision of the EXPR's type is K bits and the
598 destination mode has more bits, and the sign is changing,
599 it is not safe to use a NOP_EXPR. For example, suppose
600 that EXPR's type is a 3-bit unsigned integer type, the
601 TYPE is a 3-bit signed integer type, and the machine mode
602 for the types is 8-bit QImode. In that case, the
603 conversion necessitates an explicit sign-extension. In
604 the signed-to-unsigned case the high-order bits have to
606 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
607 && (TYPE_PRECISION (TREE_TYPE (expr
))
608 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
613 return fold_build1 (code
, type
, expr
);
616 /* If TYPE is an enumeral type or a type with a precision less
617 than the number of bits in its mode, do the conversion to the
618 type corresponding to its mode, then do a nop conversion
620 else if (TREE_CODE (type
) == ENUMERAL_TYPE
621 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
622 return build1 (NOP_EXPR
, type
,
623 convert (lang_hooks
.types
.type_for_mode
624 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
627 /* Here detect when we can distribute the truncation down past some
628 arithmetic. For example, if adding two longs and converting to an
629 int, we can equally well convert both to ints and then add.
630 For the operations handled here, such truncation distribution
632 It is desirable in these cases:
633 1) when truncating down to full-word from a larger size
634 2) when truncating takes no work.
635 3) when at least one operand of the arithmetic has been extended
636 (as by C's default conversions). In this case we need two conversions
637 if we do the arithmetic as already requested, so we might as well
638 truncate both and then combine. Perhaps that way we need only one.
640 Note that in general we cannot do the arithmetic in a type
641 shorter than the desired result of conversion, even if the operands
642 are both extended from a shorter type, because they might overflow
643 if combined in that type. The exceptions to this--the times when
644 two narrow values can be combined in their narrow type even to
645 make a wider result--are handled by "shorten" in build_binary_op. */
650 /* We can pass truncation down through right shifting
651 when the shift count is a nonpositive constant. */
652 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
653 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
658 /* We can pass truncation down through left shifting
659 when the shift count is a nonnegative constant and
660 the target type is unsigned. */
661 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
662 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
663 && TYPE_UNSIGNED (type
)
664 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
666 /* If shift count is less than the width of the truncated type,
668 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
669 /* In this case, shifting is like multiplication. */
673 /* If it is >= that width, result is zero.
674 Handling this with trunc1 would give the wrong result:
675 (int) ((long long) a << 32) is well defined (as 0)
676 but (int) a << 32 is undefined and would get a
679 tree t
= build_int_cst (type
, 0);
681 /* If the original expression had side-effects, we must
683 if (TREE_SIDE_EFFECTS (expr
))
684 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
693 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
694 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
696 /* Don't distribute unless the output precision is at least as big
697 as the actual inputs and it has the same signedness. */
698 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
699 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
700 /* If signedness of arg0 and arg1 don't match,
701 we can't necessarily find a type to compare them in. */
702 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
703 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
704 /* Do not change the sign of the division. */
705 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
706 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
707 /* Either require unsigned division or a division by
708 a constant that is not -1. */
709 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
710 || (TREE_CODE (arg1
) == INTEGER_CST
711 && !integer_all_onesp (arg1
))))
720 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
721 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
723 /* Don't distribute unless the output precision is at least as big
724 as the actual inputs. Otherwise, the comparison of the
725 truncated values will be wrong. */
726 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
727 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
728 /* If signedness of arg0 and arg1 don't match,
729 we can't necessarily find a type to compare them in. */
730 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
731 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
743 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
744 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
746 /* Do not try to narrow operands of pointer subtraction;
747 that will interfere with other folding. */
748 if (ex_form
== MINUS_EXPR
749 && CONVERT_EXPR_P (arg0
)
750 && CONVERT_EXPR_P (arg1
)
751 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
752 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
755 if (outprec
>= BITS_PER_WORD
756 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
757 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
758 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
760 /* Do the arithmetic in type TYPEX,
761 then convert result to TYPE. */
764 /* Can't do arithmetic in enumeral types
765 so use an integer type that will hold the values. */
766 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
768 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
769 TYPE_UNSIGNED (typex
));
771 /* But now perhaps TYPEX is as wide as INPREC.
772 In that case, do nothing special here.
773 (Otherwise would recurse infinitely in convert. */
774 if (TYPE_PRECISION (typex
) != inprec
)
776 /* Don't do unsigned arithmetic where signed was wanted,
778 Exception: if both of the original operands were
779 unsigned then we can safely do the work as unsigned.
780 Exception: shift operations take their type solely
781 from the first argument.
782 Exception: the LSHIFT_EXPR case above requires that
783 we perform this operation unsigned lest we produce
784 signed-overflow undefinedness.
785 And we may need to do it as unsigned
786 if we truncate to the original size. */
787 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
788 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
789 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
790 || ex_form
== LSHIFT_EXPR
791 || ex_form
== RSHIFT_EXPR
792 || ex_form
== LROTATE_EXPR
793 || ex_form
== RROTATE_EXPR
))
794 || ex_form
== LSHIFT_EXPR
795 /* If we have !flag_wrapv, and either ARG0 or
796 ARG1 is of a signed type, we have to do
797 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
798 type in case the operation in outprec precision
799 could overflow. Otherwise, we would introduce
800 signed-overflow undefinedness. */
801 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
802 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
803 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
805 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
807 && (ex_form
== PLUS_EXPR
808 || ex_form
== MINUS_EXPR
809 || ex_form
== MULT_EXPR
)))
811 if (!TYPE_UNSIGNED (typex
))
812 typex
= unsigned_type_for (typex
);
816 if (TYPE_UNSIGNED (typex
))
817 typex
= signed_type_for (typex
);
819 return convert (type
,
820 fold_build2 (ex_form
, typex
,
821 convert (typex
, arg0
),
822 convert (typex
, arg1
)));
830 /* This is not correct for ABS_EXPR,
831 since we must test the sign before truncation. */
833 /* Do the arithmetic in type TYPEX,
834 then convert result to TYPE. */
837 /* Can't do arithmetic in enumeral types
838 so use an integer type that will hold the values. */
839 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
841 = lang_hooks
.types
.type_for_size (TYPE_PRECISION (typex
),
842 TYPE_UNSIGNED (typex
));
844 if (!TYPE_UNSIGNED (typex
))
845 typex
= unsigned_type_for (typex
);
846 return convert (type
,
847 fold_build1 (ex_form
, typex
,
849 TREE_OPERAND (expr
, 0))));
854 "can't convert between vector values of different size" error. */
855 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
856 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
857 != GET_MODE_SIZE (TYPE_MODE (type
))))
859 /* If truncating after truncating, might as well do all at once.
860 If truncating after extending, we may get rid of wasted work. */
861 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
864 /* It is sometimes worthwhile to push the narrowing down through
865 the conditional and never loses. A COND_EXPR may have a throw
866 as one operand, which then has void type. Just leave void
867 operands as they are. */
868 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
869 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
870 ? TREE_OPERAND (expr
, 1)
871 : convert (type
, TREE_OPERAND (expr
, 1)),
872 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
873 ? TREE_OPERAND (expr
, 2)
874 : convert (type
, TREE_OPERAND (expr
, 2)));
880 /* When parsing long initializers, we might end up with a lot of casts.
882 if (TREE_CODE (expr
) == INTEGER_CST
)
883 return fold_convert (type
, expr
);
884 return build1 (CONVERT_EXPR
, type
, expr
);
887 if (flag_sanitize
& SANITIZE_FLOAT_CAST
888 && current_function_decl
!= NULL_TREE
889 && !lookup_attribute ("no_sanitize_undefined",
890 DECL_ATTRIBUTES (current_function_decl
)))
892 expr
= save_expr (expr
);
893 tree check
= ubsan_instrument_float_cast (loc
, type
, expr
);
894 expr
= build1 (FIX_TRUNC_EXPR
, type
, expr
);
897 return fold_build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), check
, expr
);
900 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
902 case FIXED_POINT_TYPE
:
903 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
906 return convert (type
,
907 fold_build1 (REALPART_EXPR
,
908 TREE_TYPE (TREE_TYPE (expr
)), expr
));
911 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
913 error ("can%'t convert between vector values of different size");
914 return error_mark_node
;
916 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
919 error ("aggregate value used where an integer was expected");
920 return convert (type
, integer_zero_node
);
924 /* Convert EXPR to the complex type TYPE in the usual ways. */
927 convert_to_complex (tree type
, tree expr
)
929 tree subtype
= TREE_TYPE (type
);
931 switch (TREE_CODE (TREE_TYPE (expr
)))
934 case FIXED_POINT_TYPE
:
938 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
939 convert (subtype
, integer_zero_node
));
943 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
945 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
947 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
949 tree t
= convert_to_complex (type
, TREE_OPERAND (expr
, 1));
950 if (t
== TREE_OPERAND (expr
, 1))
952 return build2_loc (EXPR_LOCATION (expr
), COMPOUND_EXPR
,
953 TREE_TYPE (t
), TREE_OPERAND (expr
, 0), t
);
955 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
956 return fold_build2 (COMPLEX_EXPR
, type
,
957 convert (subtype
, TREE_OPERAND (expr
, 0)),
958 convert (subtype
, TREE_OPERAND (expr
, 1)));
961 expr
= save_expr (expr
);
963 fold_build2 (COMPLEX_EXPR
, type
,
965 fold_build1 (REALPART_EXPR
,
966 TREE_TYPE (TREE_TYPE (expr
)),
969 fold_build1 (IMAGPART_EXPR
,
970 TREE_TYPE (TREE_TYPE (expr
)),
977 error ("pointer value used where a complex was expected");
978 return convert_to_complex (type
, integer_zero_node
);
981 error ("aggregate value used where a complex was expected");
982 return convert_to_complex (type
, integer_zero_node
);
986 /* Convert EXPR to the vector type TYPE in the usual ways. */
989 convert_to_vector (tree type
, tree expr
)
991 switch (TREE_CODE (TREE_TYPE (expr
)))
995 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
997 error ("can%'t convert between vector values of different size");
998 return error_mark_node
;
1000 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
1003 error ("can%'t convert value to a vector");
1004 return error_mark_node
;
1008 /* Convert EXPR to some fixed-point type TYPE.
1010 EXPR must be fixed-point, float, integer, or enumeral;
1011 in other cases error is called. */
1014 convert_to_fixed (tree type
, tree expr
)
1016 if (integer_zerop (expr
))
1018 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
1019 return fixed_zero_node
;
1021 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
1023 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
1024 return fixed_one_node
;
1027 switch (TREE_CODE (TREE_TYPE (expr
)))
1029 case FIXED_POINT_TYPE
:
1034 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
1037 return convert (type
,
1038 fold_build1 (REALPART_EXPR
,
1039 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1042 error ("aggregate value used where a fixed-point was expected");
1043 return error_mark_node
;