1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2013 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"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
34 /* Convert EXPR to some pointer or reference type TYPE.
35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
36 in other cases error is called. */
39 convert_to_pointer (tree type
, tree expr
)
41 location_t loc
= EXPR_LOCATION (expr
);
42 if (TREE_TYPE (expr
) == type
)
45 if (integer_zerop (expr
) && POINTER_TYPE_P (type
)
46 && upc_shared_type_p (TREE_TYPE (type
)))
48 expr
= copy_node (upc_null_pts_node
);
49 TREE_TYPE (expr
) = build_upc_unshared_type (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 /* Disable until we figure out how to decide whether the functions are
107 present in runtime. */
108 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
110 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
111 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
115 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
130 /* The above functions may set errno differently with float
131 input or output so this transformation is not safe with
157 tree arg0
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
160 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
161 the both as the safe type for operation. */
162 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (type
))
163 newtype
= TREE_TYPE (arg0
);
165 /* Be careful about integer to fp conversions.
166 These may overflow still. */
167 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
168 && TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
169 && (TYPE_MODE (newtype
) == TYPE_MODE (double_type_node
)
170 || TYPE_MODE (newtype
) == TYPE_MODE (float_type_node
)))
172 tree fn
= mathfn_built_in (newtype
, fcode
);
176 tree arg
= fold (convert_to_real (newtype
, arg0
));
177 expr
= build_call_expr (fn
, 1, arg
);
188 && (((fcode
== BUILT_IN_FLOORL
189 || fcode
== BUILT_IN_CEILL
190 || fcode
== BUILT_IN_ROUNDL
191 || fcode
== BUILT_IN_RINTL
192 || fcode
== BUILT_IN_TRUNCL
193 || fcode
== BUILT_IN_NEARBYINTL
)
194 && (TYPE_MODE (type
) == TYPE_MODE (double_type_node
)
195 || TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))
196 || ((fcode
== BUILT_IN_FLOOR
197 || fcode
== BUILT_IN_CEIL
198 || fcode
== BUILT_IN_ROUND
199 || fcode
== BUILT_IN_RINT
200 || fcode
== BUILT_IN_TRUNC
201 || fcode
== BUILT_IN_NEARBYINT
)
202 && (TYPE_MODE (type
) == TYPE_MODE (float_type_node
)))))
204 tree fn
= mathfn_built_in (type
, fcode
);
208 tree arg
= strip_float_extensions (CALL_EXPR_ARG (expr
, 0));
210 /* Make sure (type)arg0 is an extension, otherwise we could end up
211 changing (float)floor(double d) into floorf((float)d), which is
212 incorrect because (float)d uses round-to-nearest and can round
213 up to the next integer. */
214 if (TYPE_PRECISION (type
) >= TYPE_PRECISION (TREE_TYPE (arg
)))
215 return build_call_expr (fn
, 1, fold (convert_to_real (type
, arg
)));
219 /* Propagate the cast into the operation. */
220 if (itype
!= type
&& FLOAT_TYPE_P (type
))
221 switch (TREE_CODE (expr
))
223 /* Convert (float)-x into -(float)x. This is safe for
224 round-to-nearest rounding mode. */
227 if (!flag_rounding_math
228 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (expr
)))
229 return build1 (TREE_CODE (expr
), type
,
230 fold (convert_to_real (type
,
231 TREE_OPERAND (expr
, 0))));
233 /* Convert (outertype)((innertype0)a+(innertype1)b)
234 into ((newtype)a+(newtype)b) where newtype
235 is the widest mode from all of these. */
241 tree arg0
= strip_float_extensions (TREE_OPERAND (expr
, 0));
242 tree arg1
= strip_float_extensions (TREE_OPERAND (expr
, 1));
244 if (FLOAT_TYPE_P (TREE_TYPE (arg0
))
245 && FLOAT_TYPE_P (TREE_TYPE (arg1
))
246 && DECIMAL_FLOAT_TYPE_P (itype
) == DECIMAL_FLOAT_TYPE_P (type
))
250 if (TYPE_MODE (TREE_TYPE (arg0
)) == SDmode
251 || TYPE_MODE (TREE_TYPE (arg1
)) == SDmode
252 || TYPE_MODE (type
) == SDmode
)
253 newtype
= dfloat32_type_node
;
254 if (TYPE_MODE (TREE_TYPE (arg0
)) == DDmode
255 || TYPE_MODE (TREE_TYPE (arg1
)) == DDmode
256 || TYPE_MODE (type
) == DDmode
)
257 newtype
= dfloat64_type_node
;
258 if (TYPE_MODE (TREE_TYPE (arg0
)) == TDmode
259 || TYPE_MODE (TREE_TYPE (arg1
)) == TDmode
260 || TYPE_MODE (type
) == TDmode
)
261 newtype
= dfloat128_type_node
;
262 if (newtype
== dfloat32_type_node
263 || newtype
== dfloat64_type_node
264 || newtype
== dfloat128_type_node
)
266 expr
= build2 (TREE_CODE (expr
), newtype
,
267 fold (convert_to_real (newtype
, arg0
)),
268 fold (convert_to_real (newtype
, arg1
)));
274 if (TYPE_PRECISION (TREE_TYPE (arg0
)) > TYPE_PRECISION (newtype
))
275 newtype
= TREE_TYPE (arg0
);
276 if (TYPE_PRECISION (TREE_TYPE (arg1
)) > TYPE_PRECISION (newtype
))
277 newtype
= TREE_TYPE (arg1
);
278 /* Sometimes this transformation is safe (cannot
279 change results through affecting double rounding
280 cases) and sometimes it is not. If NEWTYPE is
281 wider than TYPE, e.g. (float)((long double)double
282 + (long double)double) converted to
283 (float)(double + double), the transformation is
284 unsafe regardless of the details of the types
285 involved; double rounding can arise if the result
286 of NEWTYPE arithmetic is a NEWTYPE value half way
287 between two representable TYPE values but the
288 exact value is sufficiently different (in the
289 right direction) for this difference to be
290 visible in ITYPE arithmetic. If NEWTYPE is the
291 same as TYPE, however, the transformation may be
292 safe depending on the types involved: it is safe
293 if the ITYPE has strictly more than twice as many
294 mantissa bits as TYPE, can represent infinities
295 and NaNs if the TYPE can, and has sufficient
296 exponent range for the product or ratio of two
297 values representable in the TYPE to be within the
298 range of normal values of ITYPE. */
299 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (itype
)
300 && (flag_unsafe_math_optimizations
301 || (TYPE_PRECISION (newtype
) == TYPE_PRECISION (type
)
302 && real_can_shorten_arithmetic (TYPE_MODE (itype
),
304 && !excess_precision_type (newtype
))))
306 expr
= build2 (TREE_CODE (expr
), newtype
,
307 fold (convert_to_real (newtype
, arg0
)),
308 fold (convert_to_real (newtype
, arg1
)));
319 switch (TREE_CODE (TREE_TYPE (expr
)))
322 /* Ignore the conversion if we don't need to store intermediate
323 results and neither type is a decimal float. */
324 return build1 ((flag_float_store
325 || DECIMAL_FLOAT_TYPE_P (type
)
326 || DECIMAL_FLOAT_TYPE_P (itype
))
327 ? CONVERT_EXPR
: NOP_EXPR
, type
, expr
);
332 return build1 (FLOAT_EXPR
, type
, expr
);
334 case FIXED_POINT_TYPE
:
335 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
338 return convert (type
,
339 fold_build1 (REALPART_EXPR
,
340 TREE_TYPE (TREE_TYPE (expr
)), expr
));
344 error ("pointer value used where a floating point value was expected");
345 return convert_to_real (type
, integer_zero_node
);
348 error ("aggregate value used where a float was expected");
349 return convert_to_real (type
, integer_zero_node
);
353 /* Convert EXPR to some integer (or enum) type TYPE.
355 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
356 fixed-point or vector; in other cases error is called.
358 The result of this is always supposed to be a newly created tree node
359 not in use in any existing structure. */
362 convert_to_integer (tree type
, tree expr
)
364 enum tree_code ex_form
= TREE_CODE (expr
);
365 tree intype
= TREE_TYPE (expr
);
366 unsigned int inprec
= TYPE_PRECISION (intype
);
367 unsigned int outprec
= TYPE_PRECISION (type
);
369 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
370 be. Consider `enum E = { a, b = (enum E) 3 };'. */
371 if (!COMPLETE_TYPE_P (type
))
373 error ("conversion to incomplete type");
374 return error_mark_node
;
377 /* Can't optimize the conversion of UPC shared pointer difference. */
378 if (ex_form
== MINUS_EXPR
379 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)))
380 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
381 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
382 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (TREE_OPERAND (expr
, 1)))))
384 return build1 (CONVERT_EXPR
, type
, expr
);
387 /* Convert e.g. (long)round(d) -> lround(d). */
388 /* If we're converting to char, we may encounter differing behavior
389 between converting from double->char vs double->long->char.
390 We're in "undefined" territory but we prefer to be conservative,
391 so only proceed in "unsafe" math mode. */
393 && (flag_unsafe_math_optimizations
394 || (long_integer_type_node
395 && outprec
>= TYPE_PRECISION (long_integer_type_node
))))
397 tree s_expr
= strip_float_extensions (expr
);
398 tree s_intype
= TREE_TYPE (s_expr
);
399 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
404 CASE_FLT_FN (BUILT_IN_CEIL
):
405 /* Only convert in ISO C99 mode. */
406 if (!TARGET_C99_FUNCTIONS
)
408 if (outprec
< TYPE_PRECISION (integer_type_node
)
409 || (outprec
== TYPE_PRECISION (integer_type_node
)
410 && !TYPE_UNSIGNED (type
)))
411 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
412 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
413 && !TYPE_UNSIGNED (type
))
414 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
415 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
416 && !TYPE_UNSIGNED (type
))
417 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
420 CASE_FLT_FN (BUILT_IN_FLOOR
):
421 /* Only convert in ISO C99 mode. */
422 if (!TARGET_C99_FUNCTIONS
)
424 if (outprec
< TYPE_PRECISION (integer_type_node
)
425 || (outprec
== TYPE_PRECISION (integer_type_node
)
426 && !TYPE_UNSIGNED (type
)))
427 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
428 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
429 && !TYPE_UNSIGNED (type
))
430 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
431 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
432 && !TYPE_UNSIGNED (type
))
433 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
436 CASE_FLT_FN (BUILT_IN_ROUND
):
437 /* Only convert in ISO C99 mode. */
438 if (!TARGET_C99_FUNCTIONS
)
440 if (outprec
< TYPE_PRECISION (integer_type_node
)
441 || (outprec
== TYPE_PRECISION (integer_type_node
)
442 && !TYPE_UNSIGNED (type
)))
443 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
444 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
445 && !TYPE_UNSIGNED (type
))
446 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
447 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
448 && !TYPE_UNSIGNED (type
))
449 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
452 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
453 /* Only convert nearbyint* if we can ignore math exceptions. */
454 if (flag_trapping_math
)
456 /* ... Fall through ... */
457 CASE_FLT_FN (BUILT_IN_RINT
):
458 /* Only convert in ISO C99 mode. */
459 if (!TARGET_C99_FUNCTIONS
)
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_IRINT
);
465 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
466 && !TYPE_UNSIGNED (type
))
467 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
468 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
469 && !TYPE_UNSIGNED (type
))
470 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
473 CASE_FLT_FN (BUILT_IN_TRUNC
):
474 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
482 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
483 return convert_to_integer (type
, newexpr
);
487 /* Convert (int)logb(d) -> ilogb(d). */
489 && flag_unsafe_math_optimizations
490 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
492 && (outprec
> TYPE_PRECISION (integer_type_node
)
493 || (outprec
== TYPE_PRECISION (integer_type_node
)
494 && !TYPE_UNSIGNED (type
))))
496 tree s_expr
= strip_float_extensions (expr
);
497 tree s_intype
= TREE_TYPE (s_expr
);
498 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
503 CASE_FLT_FN (BUILT_IN_LOGB
):
504 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
513 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
514 return convert_to_integer (type
, newexpr
);
518 switch (TREE_CODE (intype
))
522 if (upc_shared_type_p (TREE_TYPE (intype
)))
524 error ("invalid conversion from a UPC pointer-to-shared to an integer");
525 expr
= integer_zero_node
;
527 if (integer_zerop (expr
))
528 return build_int_cst (type
, 0);
530 /* Convert to an unsigned integer of the correct width first, and from
531 there widen/truncate to the required type. Some targets support the
532 coexistence of multiple valid pointer sizes, so fetch the one we need
534 expr
= fold_build1 (CONVERT_EXPR
,
535 lang_hooks
.types
.type_for_size
536 (TYPE_PRECISION (intype
), 0),
538 return fold_convert (type
, expr
);
544 /* If this is a logical operation, which just returns 0 or 1, we can
545 change the type of the expression. */
547 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
549 expr
= copy_node (expr
);
550 TREE_TYPE (expr
) = type
;
554 /* If we are widening the type, put in an explicit conversion.
555 Similarly if we are not changing the width. After this, we know
556 we are truncating EXPR. */
558 else if (outprec
>= inprec
)
562 /* If the precision of the EXPR's type is K bits and the
563 destination mode has more bits, and the sign is changing,
564 it is not safe to use a NOP_EXPR. For example, suppose
565 that EXPR's type is a 3-bit unsigned integer type, the
566 TYPE is a 3-bit signed integer type, and the machine mode
567 for the types is 8-bit QImode. In that case, the
568 conversion necessitates an explicit sign-extension. In
569 the signed-to-unsigned case the high-order bits have to
571 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
572 && (TYPE_PRECISION (TREE_TYPE (expr
))
573 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
578 return fold_build1 (code
, type
, expr
);
581 /* If TYPE is an enumeral type or a type with a precision less
582 than the number of bits in its mode, do the conversion to the
583 type corresponding to its mode, then do a nop conversion
585 else if (TREE_CODE (type
) == ENUMERAL_TYPE
586 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
587 return build1 (NOP_EXPR
, type
,
588 convert (lang_hooks
.types
.type_for_mode
589 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
592 /* Here detect when we can distribute the truncation down past some
593 arithmetic. For example, if adding two longs and converting to an
594 int, we can equally well convert both to ints and then add.
595 For the operations handled here, such truncation distribution
597 It is desirable in these cases:
598 1) when truncating down to full-word from a larger size
599 2) when truncating takes no work.
600 3) when at least one operand of the arithmetic has been extended
601 (as by C's default conversions). In this case we need two conversions
602 if we do the arithmetic as already requested, so we might as well
603 truncate both and then combine. Perhaps that way we need only one.
605 Note that in general we cannot do the arithmetic in a type
606 shorter than the desired result of conversion, even if the operands
607 are both extended from a shorter type, because they might overflow
608 if combined in that type. The exceptions to this--the times when
609 two narrow values can be combined in their narrow type even to
610 make a wider result--are handled by "shorten" in build_binary_op. */
615 /* We can pass truncation down through right shifting
616 when the shift count is a nonpositive constant. */
617 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
618 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
623 /* We can pass truncation down through left shifting
624 when the shift count is a nonnegative constant and
625 the target type is unsigned. */
626 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
627 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
628 && TYPE_UNSIGNED (type
)
629 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
631 /* If shift count is less than the width of the truncated type,
633 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
634 /* In this case, shifting is like multiplication. */
638 /* If it is >= that width, result is zero.
639 Handling this with trunc1 would give the wrong result:
640 (int) ((long long) a << 32) is well defined (as 0)
641 but (int) a << 32 is undefined and would get a
644 tree t
= build_int_cst (type
, 0);
646 /* If the original expression had side-effects, we must
648 if (TREE_SIDE_EFFECTS (expr
))
649 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
658 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
659 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
661 /* Don't distribute unless the output precision is at least as big
662 as the actual inputs and it has the same signedness. */
663 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
664 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
665 /* If signedness of arg0 and arg1 don't match,
666 we can't necessarily find a type to compare them in. */
667 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
668 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
669 /* Do not change the sign of the division. */
670 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
671 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
672 /* Either require unsigned division or a division by
673 a constant that is not -1. */
674 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
675 || (TREE_CODE (arg1
) == INTEGER_CST
676 && !integer_all_onesp (arg1
))))
685 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
686 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
688 /* Don't distribute unless the output precision is at least as big
689 as the actual inputs. Otherwise, the comparison of the
690 truncated values will be wrong. */
691 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
692 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
693 /* If signedness of arg0 and arg1 don't match,
694 we can't necessarily find a type to compare them in. */
695 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
696 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
708 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
709 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
711 /* Do not try to narrow operands of pointer subtraction;
712 that will interfere with other folding. */
713 if (ex_form
== MINUS_EXPR
714 && CONVERT_EXPR_P (arg0
)
715 && CONVERT_EXPR_P (arg1
)
716 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
717 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
720 if (outprec
>= BITS_PER_WORD
721 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
722 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
723 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
725 /* Do the arithmetic in type TYPEX,
726 then convert result to TYPE. */
729 /* Can't do arithmetic in enumeral types
730 so use an integer type that will hold the values. */
731 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
732 typex
= lang_hooks
.types
.type_for_size
733 (TYPE_PRECISION (typex
), TYPE_UNSIGNED (typex
));
735 /* But now perhaps TYPEX is as wide as INPREC.
736 In that case, do nothing special here.
737 (Otherwise would recurse infinitely in convert. */
738 if (TYPE_PRECISION (typex
) != inprec
)
740 /* Don't do unsigned arithmetic where signed was wanted,
742 Exception: if both of the original operands were
743 unsigned then we can safely do the work as unsigned.
744 Exception: shift operations take their type solely
745 from the first argument.
746 Exception: the LSHIFT_EXPR case above requires that
747 we perform this operation unsigned lest we produce
748 signed-overflow undefinedness.
749 And we may need to do it as unsigned
750 if we truncate to the original size. */
751 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
752 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
753 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
754 || ex_form
== LSHIFT_EXPR
755 || ex_form
== RSHIFT_EXPR
756 || ex_form
== LROTATE_EXPR
757 || ex_form
== RROTATE_EXPR
))
758 || ex_form
== LSHIFT_EXPR
759 /* If we have !flag_wrapv, and either ARG0 or
760 ARG1 is of a signed type, we have to do
761 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
762 type in case the operation in outprec precision
763 could overflow. Otherwise, we would introduce
764 signed-overflow undefinedness. */
765 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
766 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
767 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
769 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
771 && (ex_form
== PLUS_EXPR
772 || ex_form
== MINUS_EXPR
773 || ex_form
== MULT_EXPR
)))
774 typex
= unsigned_type_for (typex
);
776 typex
= signed_type_for (typex
);
777 return convert (type
,
778 fold_build2 (ex_form
, typex
,
779 convert (typex
, arg0
),
780 convert (typex
, arg1
)));
788 /* This is not correct for ABS_EXPR,
789 since we must test the sign before truncation. */
791 tree typex
= unsigned_type_for (type
);
792 return convert (type
,
793 fold_build1 (ex_form
, typex
,
795 TREE_OPERAND (expr
, 0))));
800 "can't convert between vector values of different size" error. */
801 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
802 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
803 != GET_MODE_SIZE (TYPE_MODE (type
))))
805 /* If truncating after truncating, might as well do all at once.
806 If truncating after extending, we may get rid of wasted work. */
807 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
810 /* It is sometimes worthwhile to push the narrowing down through
811 the conditional and never loses. A COND_EXPR may have a throw
812 as one operand, which then has void type. Just leave void
813 operands as they are. */
814 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
815 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
816 ? TREE_OPERAND (expr
, 1)
817 : convert (type
, TREE_OPERAND (expr
, 1)),
818 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
819 ? TREE_OPERAND (expr
, 2)
820 : convert (type
, TREE_OPERAND (expr
, 2)));
826 /* When parsing long initializers, we might end up with a lot of casts.
828 if (TREE_CODE (expr
) == INTEGER_CST
)
829 return fold_convert (type
, expr
);
830 return build1 (CONVERT_EXPR
, type
, expr
);
833 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
835 case FIXED_POINT_TYPE
:
836 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
839 return convert (type
,
840 fold_build1 (REALPART_EXPR
,
841 TREE_TYPE (TREE_TYPE (expr
)), expr
));
844 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
846 error ("can%'t convert between vector values of different size");
847 return error_mark_node
;
849 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
852 error ("aggregate value used where an integer was expected");
853 return convert (type
, integer_zero_node
);
857 /* Convert EXPR to the complex type TYPE in the usual ways. */
860 convert_to_complex (tree type
, tree expr
)
862 tree subtype
= TREE_TYPE (type
);
864 switch (TREE_CODE (TREE_TYPE (expr
)))
867 case FIXED_POINT_TYPE
:
871 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
872 convert (subtype
, integer_zero_node
));
876 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
878 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
880 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
881 return fold_build2 (COMPLEX_EXPR
, type
,
882 convert (subtype
, TREE_OPERAND (expr
, 0)),
883 convert (subtype
, TREE_OPERAND (expr
, 1)));
886 expr
= save_expr (expr
);
888 fold_build2 (COMPLEX_EXPR
, type
,
890 fold_build1 (REALPART_EXPR
,
891 TREE_TYPE (TREE_TYPE (expr
)),
894 fold_build1 (IMAGPART_EXPR
,
895 TREE_TYPE (TREE_TYPE (expr
)),
902 error ("pointer value used where a complex was expected");
903 return convert_to_complex (type
, integer_zero_node
);
906 error ("aggregate value used where a complex was expected");
907 return convert_to_complex (type
, integer_zero_node
);
911 /* Convert EXPR to the vector type TYPE in the usual ways. */
914 convert_to_vector (tree type
, tree expr
)
916 switch (TREE_CODE (TREE_TYPE (expr
)))
920 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
922 error ("can%'t convert between vector values of different size");
923 return error_mark_node
;
925 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
928 error ("can%'t convert value to a vector");
929 return error_mark_node
;
933 /* Convert EXPR to some fixed-point type TYPE.
935 EXPR must be fixed-point, float, integer, or enumeral;
936 in other cases error is called. */
939 convert_to_fixed (tree type
, tree expr
)
941 if (integer_zerop (expr
))
943 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
944 return fixed_zero_node
;
946 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
948 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
949 return fixed_one_node
;
952 switch (TREE_CODE (TREE_TYPE (expr
)))
954 case FIXED_POINT_TYPE
:
959 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
962 return convert (type
,
963 fold_build1 (REALPART_EXPR
,
964 TREE_TYPE (TREE_TYPE (expr
)), expr
));
967 error ("aggregate value used where a fixed-point was expected");
968 return error_mark_node
;