1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
28 #include "coretypes.h"
33 #include "diagnostic-core.h"
34 #include "langhooks.h"
36 /* Convert EXPR to some pointer or reference type TYPE.
37 EXPR must be pointer, reference, integer, enumeral, or literal zero;
38 in other cases error is called. */
41 convert_to_pointer (tree type, tree expr)
43 location_t loc = EXPR_LOCATION (expr);
44 if (TREE_TYPE (expr) == type)
47 switch (TREE_CODE (TREE_TYPE (expr)))
52 /* If the pointers point to different address spaces, conversion needs
53 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
54 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
55 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
58 return fold_build1_loc (loc, NOP_EXPR, type, expr);
60 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
67 /* If the input precision differs from the target pointer type
68 precision, first convert the input expression to an integer type of
69 the target precision. Some targets, e.g. VMS, need several pointer
70 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
71 unsigned int pprec = TYPE_PRECISION (type);
72 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
75 expr = fold_build1_loc (loc, NOP_EXPR,
76 lang_hooks.types.type_for_size (pprec, 0),
80 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
83 error ("cannot convert to a pointer type");
84 return convert_to_pointer (type, integer_zero_node);
88 /* Avoid any floating point extensions from EXP. */
90 strip_float_extensions (tree exp)
94 /* For floating point constant look up the narrowest type that can hold
95 it properly and handle it like (type)(narrowest_type)constant.
96 This way we can optimize for instance a=a*2.0 where "a" is float
97 but 2.0 is double constant. */
98 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
100 REAL_VALUE_TYPE orig;
103 orig = TREE_REAL_CST (exp);
104 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
105 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
106 type = float_type_node;
107 else if (TYPE_PRECISION (TREE_TYPE (exp))
108 > TYPE_PRECISION (double_type_node)
109 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
110 type = double_type_node;
112 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
115 if (!CONVERT_EXPR_P (exp))
118 sub = TREE_OPERAND (exp, 0);
119 subt = TREE_TYPE (sub);
120 expt = TREE_TYPE (exp);
122 if (!FLOAT_TYPE_P (subt))
125 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
128 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
131 return strip_float_extensions (sub);
135 /* Convert EXPR to some floating-point type TYPE.
137 EXPR must be float, fixed-point, integer, or enumeral;
138 in other cases error is called. */
141 convert_to_real (tree type, tree expr)
143 enum built_in_function fcode = builtin_mathfn_code (expr);
144 tree itype = TREE_TYPE (expr);
146 /* Disable until we figure out how to decide whether the functions are
147 present in runtime. */
148 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
150 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
151 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
155 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
170 /* The above functions may set errno differently with float
171 input or output so this transformation is not safe with
197 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
200 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
201 the both as the safe type for operation. */
202 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
203 newtype = TREE_TYPE (arg0);
205 /* Be careful about integer to fp conversions.
206 These may overflow still. */
207 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
208 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
209 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
210 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
212 tree fn = mathfn_built_in (newtype, fcode);
216 tree arg = fold (convert_to_real (newtype, arg0));
217 expr = build_call_expr (fn, 1, arg);
228 && (((fcode == BUILT_IN_FLOORL
229 || fcode == BUILT_IN_CEILL
230 || fcode == BUILT_IN_ROUNDL
231 || fcode == BUILT_IN_RINTL
232 || fcode == BUILT_IN_TRUNCL
233 || fcode == BUILT_IN_NEARBYINTL)
234 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
235 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
236 || ((fcode == BUILT_IN_FLOOR
237 || fcode == BUILT_IN_CEIL
238 || fcode == BUILT_IN_ROUND
239 || fcode == BUILT_IN_RINT
240 || fcode == BUILT_IN_TRUNC
241 || fcode == BUILT_IN_NEARBYINT)
242 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
244 tree fn = mathfn_built_in (type, fcode);
248 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
250 /* Make sure (type)arg0 is an extension, otherwise we could end up
251 changing (float)floor(double d) into floorf((float)d), which is
252 incorrect because (float)d uses round-to-nearest and can round
253 up to the next integer. */
254 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
255 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
259 /* Propagate the cast into the operation. */
260 if (itype != type && FLOAT_TYPE_P (type))
261 switch (TREE_CODE (expr))
263 /* Convert (float)-x into -(float)x. This is safe for
264 round-to-nearest rounding mode. */
267 if (!flag_rounding_math
268 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
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 = TYPE_PRECISION (intype);
407 unsigned int outprec = TYPE_PRECISION (type);
409 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
410 be. Consider `enum E = { a, b = (enum E) 3 };'. */
411 if (!COMPLETE_TYPE_P (type))
413 error ("conversion to incomplete type");
414 return error_mark_node;
417 /* Convert e.g. (long)round(d) -> lround(d). */
418 /* If we're converting to char, we may encounter differing behavior
419 between converting from double->char vs double->long->char.
420 We're in "undefined" territory but we prefer to be conservative,
421 so only proceed in "unsafe" math mode. */
423 && (flag_unsafe_math_optimizations
424 || (long_integer_type_node
425 && outprec >= TYPE_PRECISION (long_integer_type_node))))
427 tree s_expr = strip_float_extensions (expr);
428 tree s_intype = TREE_TYPE (s_expr);
429 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
434 CASE_FLT_FN (BUILT_IN_CEIL):
435 /* Only convert in ISO C99 mode. */
436 if (!TARGET_C99_FUNCTIONS)
438 if (outprec < TYPE_PRECISION (long_integer_type_node)
439 || (outprec == TYPE_PRECISION (long_integer_type_node)
440 && !TYPE_UNSIGNED (type)))
441 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
442 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
443 && !TYPE_UNSIGNED (type))
444 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
447 CASE_FLT_FN (BUILT_IN_FLOOR):
448 /* Only convert in ISO C99 mode. */
449 if (!TARGET_C99_FUNCTIONS)
451 if (outprec < TYPE_PRECISION (long_integer_type_node)
452 || (outprec == TYPE_PRECISION (long_integer_type_node)
453 && !TYPE_UNSIGNED (type)))
454 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
455 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
456 && !TYPE_UNSIGNED (type))
457 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
460 CASE_FLT_FN (BUILT_IN_ROUND):
461 if (outprec < TYPE_PRECISION (long_integer_type_node)
462 || (outprec == TYPE_PRECISION (long_integer_type_node)
463 && !TYPE_UNSIGNED (type)))
464 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
465 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
466 && !TYPE_UNSIGNED (type))
467 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
470 CASE_FLT_FN (BUILT_IN_NEARBYINT):
471 /* Only convert nearbyint* if we can ignore math exceptions. */
472 if (flag_trapping_math)
474 /* ... Fall through ... */
475 CASE_FLT_FN (BUILT_IN_RINT):
476 if (outprec < TYPE_PRECISION (long_integer_type_node)
477 || (outprec == TYPE_PRECISION (long_integer_type_node)
478 && !TYPE_UNSIGNED (type)))
479 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
480 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
481 && !TYPE_UNSIGNED (type))
482 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
485 CASE_FLT_FN (BUILT_IN_TRUNC):
486 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
494 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
495 return convert_to_integer (type, newexpr);
499 /* Convert (int)logb(d) -> ilogb(d). */
501 && flag_unsafe_math_optimizations
502 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
504 && (outprec > TYPE_PRECISION (integer_type_node)
505 || (outprec == TYPE_PRECISION (integer_type_node)
506 && !TYPE_UNSIGNED (type))))
508 tree s_expr = strip_float_extensions (expr);
509 tree s_intype = TREE_TYPE (s_expr);
510 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
515 CASE_FLT_FN (BUILT_IN_LOGB):
516 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
525 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
526 return convert_to_integer (type, newexpr);
530 switch (TREE_CODE (intype))
534 if (integer_zerop (expr))
535 return build_int_cst (type, 0);
537 /* Convert to an unsigned integer of the correct width first, and from
538 there widen/truncate to the required type. Some targets support the
539 coexistence of multiple valid pointer sizes, so fetch the one we need
541 expr = fold_build1 (CONVERT_EXPR,
542 lang_hooks.types.type_for_size
543 (TYPE_PRECISION (intype), 0),
545 return fold_convert (type, expr);
551 /* If this is a logical operation, which just returns 0 or 1, we can
552 change the type of the expression. */
554 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
556 expr = copy_node (expr);
557 TREE_TYPE (expr) = type;
561 /* If we are widening the type, put in an explicit conversion.
562 Similarly if we are not changing the width. After this, we know
563 we are truncating EXPR. */
565 else if (outprec >= inprec)
570 /* If the precision of the EXPR's type is K bits and the
571 destination mode has more bits, and the sign is changing,
572 it is not safe to use a NOP_EXPR. For example, suppose
573 that EXPR's type is a 3-bit unsigned integer type, the
574 TYPE is a 3-bit signed integer type, and the machine mode
575 for the types is 8-bit QImode. In that case, the
576 conversion necessitates an explicit sign-extension. In
577 the signed-to-unsigned case the high-order bits have to
579 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
580 && (TYPE_PRECISION (TREE_TYPE (expr))
581 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
586 tem = fold_unary (code, type, expr);
590 tem = build1 (code, type, expr);
591 TREE_NO_WARNING (tem) = 1;
595 /* If TYPE is an enumeral type or a type with a precision less
596 than the number of bits in its mode, do the conversion to the
597 type corresponding to its mode, then do a nop conversion
599 else if (TREE_CODE (type) == ENUMERAL_TYPE
600 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
601 return build1 (NOP_EXPR, type,
602 convert (lang_hooks.types.type_for_mode
603 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
606 /* Here detect when we can distribute the truncation down past some
607 arithmetic. For example, if adding two longs and converting to an
608 int, we can equally well convert both to ints and then add.
609 For the operations handled here, such truncation distribution
611 It is desirable in these cases:
612 1) when truncating down to full-word from a larger size
613 2) when truncating takes no work.
614 3) when at least one operand of the arithmetic has been extended
615 (as by C's default conversions). In this case we need two conversions
616 if we do the arithmetic as already requested, so we might as well
617 truncate both and then combine. Perhaps that way we need only one.
619 Note that in general we cannot do the arithmetic in a type
620 shorter than the desired result of conversion, even if the operands
621 are both extended from a shorter type, because they might overflow
622 if combined in that type. The exceptions to this--the times when
623 two narrow values can be combined in their narrow type even to
624 make a wider result--are handled by "shorten" in build_binary_op. */
629 /* We can pass truncation down through right shifting
630 when the shift count is a nonpositive constant. */
631 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
632 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
637 /* We can pass truncation down through left shifting
638 when the shift count is a nonnegative constant and
639 the target type is unsigned. */
640 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
641 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
642 && TYPE_UNSIGNED (type)
643 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
645 /* If shift count is less than the width of the truncated type,
647 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
648 /* In this case, shifting is like multiplication. */
652 /* If it is >= that width, result is zero.
653 Handling this with trunc1 would give the wrong result:
654 (int) ((long long) a << 32) is well defined (as 0)
655 but (int) a << 32 is undefined and would get a
658 tree t = build_int_cst (type, 0);
660 /* If the original expression had side-effects, we must
662 if (TREE_SIDE_EFFECTS (expr))
663 return build2 (COMPOUND_EXPR, type, expr, t);
672 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
673 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
675 /* Don't distribute unless the output precision is at least as big
676 as the actual inputs and it has the same signedness. */
677 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
678 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
679 /* If signedness of arg0 and arg1 don't match,
680 we can't necessarily find a type to compare them in. */
681 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
682 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
683 /* Do not change the sign of the division. */
684 && (TYPE_UNSIGNED (TREE_TYPE (expr))
685 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
686 /* Either require unsigned division or a division by
687 a constant that is not -1. */
688 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
689 || (TREE_CODE (arg1) == INTEGER_CST
690 && !integer_all_onesp (arg1))))
699 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
700 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
702 /* Don't distribute unless the output precision is at least as big
703 as the actual inputs. Otherwise, the comparison of the
704 truncated values will be wrong. */
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))))
722 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
723 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
725 /* Do not try to narrow operands of pointer subtraction;
726 that will interfere with other folding. */
727 if (ex_form == MINUS_EXPR
728 && CONVERT_EXPR_P (arg0)
729 && CONVERT_EXPR_P (arg1)
730 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
731 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
734 if (outprec >= BITS_PER_WORD
735 || TRULY_NOOP_TRUNCATION (outprec, inprec)
736 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
737 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
739 /* Do the arithmetic in type TYPEX,
740 then convert result to TYPE. */
743 /* Can't do arithmetic in enumeral types
744 so use an integer type that will hold the values. */
745 if (TREE_CODE (typex) == ENUMERAL_TYPE)
746 typex = lang_hooks.types.type_for_size
747 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
749 /* But now perhaps TYPEX is as wide as INPREC.
750 In that case, do nothing special here.
751 (Otherwise would recurse infinitely in convert. */
752 if (TYPE_PRECISION (typex) != inprec)
754 /* Don't do unsigned arithmetic where signed was wanted,
756 Exception: if both of the original operands were
757 unsigned then we can safely do the work as unsigned.
758 Exception: shift operations take their type solely
759 from the first argument.
760 Exception: the LSHIFT_EXPR case above requires that
761 we perform this operation unsigned lest we produce
762 signed-overflow undefinedness.
763 And we may need to do it as unsigned
764 if we truncate to the original size. */
765 if (TYPE_UNSIGNED (TREE_TYPE (expr))
766 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
767 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
768 || ex_form == LSHIFT_EXPR
769 || ex_form == RSHIFT_EXPR
770 || ex_form == LROTATE_EXPR
771 || ex_form == RROTATE_EXPR))
772 || ex_form == LSHIFT_EXPR
773 /* If we have !flag_wrapv, and either ARG0 or
774 ARG1 is of a signed type, we have to do
775 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
776 type in case the operation in outprec precision
777 could overflow. Otherwise, we would introduce
778 signed-overflow undefinedness. */
779 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
780 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
781 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
783 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
785 && (ex_form == PLUS_EXPR
786 || ex_form == MINUS_EXPR
787 || ex_form == MULT_EXPR)))
788 typex = unsigned_type_for (typex);
790 typex = signed_type_for (typex);
791 return convert (type,
792 fold_build2 (ex_form, typex,
793 convert (typex, arg0),
794 convert (typex, arg1)));
802 /* This is not correct for ABS_EXPR,
803 since we must test the sign before truncation. */
805 tree typex = unsigned_type_for (type);
806 return convert (type,
807 fold_build1 (ex_form, typex,
809 TREE_OPERAND (expr, 0))));
814 "can't convert between vector values of different size" error. */
815 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
816 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
817 != GET_MODE_SIZE (TYPE_MODE (type))))
819 /* If truncating after truncating, might as well do all at once.
820 If truncating after extending, we may get rid of wasted work. */
821 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
824 /* It is sometimes worthwhile to push the narrowing down through
825 the conditional and never loses. A COND_EXPR may have a throw
826 as one operand, which then has void type. Just leave void
827 operands as they are. */
828 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
829 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
830 ? TREE_OPERAND (expr, 1)
831 : convert (type, TREE_OPERAND (expr, 1)),
832 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
833 ? TREE_OPERAND (expr, 2)
834 : convert (type, TREE_OPERAND (expr, 2)));
840 return build1 (CONVERT_EXPR, type, expr);
843 return build1 (FIX_TRUNC_EXPR, type, expr);
845 case FIXED_POINT_TYPE:
846 return build1 (FIXED_CONVERT_EXPR, type, expr);
849 return convert (type,
850 fold_build1 (REALPART_EXPR,
851 TREE_TYPE (TREE_TYPE (expr)), expr));
854 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
856 error ("can%'t convert between vector values of different size");
857 return error_mark_node;
859 return build1 (VIEW_CONVERT_EXPR, type, expr);
862 error ("aggregate value used where an integer was expected");
863 return convert (type, integer_zero_node);
867 /* Convert EXPR to the complex type TYPE in the usual ways. */
870 convert_to_complex (tree type, tree expr)
872 tree subtype = TREE_TYPE (type);
874 switch (TREE_CODE (TREE_TYPE (expr)))
877 case FIXED_POINT_TYPE:
881 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
882 convert (subtype, integer_zero_node));
886 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
888 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
890 else if (TREE_CODE (expr) == COMPLEX_EXPR)
891 return fold_build2 (COMPLEX_EXPR, type,
892 convert (subtype, TREE_OPERAND (expr, 0)),
893 convert (subtype, TREE_OPERAND (expr, 1)));
896 expr = save_expr (expr);
898 fold_build2 (COMPLEX_EXPR, type,
900 fold_build1 (REALPART_EXPR,
901 TREE_TYPE (TREE_TYPE (expr)),
904 fold_build1 (IMAGPART_EXPR,
905 TREE_TYPE (TREE_TYPE (expr)),
912 error ("pointer value used where a complex was expected");
913 return convert_to_complex (type, integer_zero_node);
916 error ("aggregate value used where a complex was expected");
917 return convert_to_complex (type, integer_zero_node);
921 /* Convert EXPR to the vector type TYPE in the usual ways. */
924 convert_to_vector (tree type, tree expr)
926 switch (TREE_CODE (TREE_TYPE (expr)))
930 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
932 error ("can%'t convert between vector values of different size");
933 return error_mark_node;
935 return build1 (VIEW_CONVERT_EXPR, type, expr);
938 error ("can%'t convert value to a vector");
939 return error_mark_node;
943 /* Convert EXPR to some fixed-point type TYPE.
945 EXPR must be fixed-point, float, integer, or enumeral;
946 in other cases error is called. */
949 convert_to_fixed (tree type, tree expr)
951 if (integer_zerop (expr))
953 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
954 return fixed_zero_node;
956 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
958 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
959 return fixed_one_node;
962 switch (TREE_CODE (TREE_TYPE (expr)))
964 case FIXED_POINT_TYPE:
969 return build1 (FIXED_CONVERT_EXPR, type, expr);
972 return convert (type,
973 fold_build1 (REALPART_EXPR,
974 TREE_TYPE (TREE_TYPE (expr)), expr));
977 error ("aggregate value used where a fixed-point was expected");
978 return error_mark_node;