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 /* Propagate overflow to the NULL pointer. */
48 if (integer_zerop (expr))
49 return force_fit_type_double (type, double_int_zero, 0,
50 TREE_OVERFLOW (expr));
52 switch (TREE_CODE (TREE_TYPE (expr)))
57 /* If the pointers point to different address spaces, conversion needs
58 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
59 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
60 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
63 return fold_build1_loc (loc, NOP_EXPR, type, expr);
65 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
72 /* If the input precision differs from the target pointer type
73 precision, first convert the input expression to an integer type of
74 the target precision. Some targets, e.g. VMS, need several pointer
75 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
76 unsigned int pprec = TYPE_PRECISION (type);
77 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
80 expr = fold_build1_loc (loc, NOP_EXPR,
81 lang_hooks.types.type_for_size (pprec, 0),
85 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
88 error ("cannot convert to a pointer type");
89 return convert_to_pointer (type, integer_zero_node);
93 /* Avoid any floating point extensions from EXP. */
95 strip_float_extensions (tree exp)
99 /* For floating point constant look up the narrowest type that can hold
100 it properly and handle it like (type)(narrowest_type)constant.
101 This way we can optimize for instance a=a*2.0 where "a" is float
102 but 2.0 is double constant. */
103 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
105 REAL_VALUE_TYPE orig;
108 orig = TREE_REAL_CST (exp);
109 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
110 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
111 type = float_type_node;
112 else if (TYPE_PRECISION (TREE_TYPE (exp))
113 > TYPE_PRECISION (double_type_node)
114 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
115 type = double_type_node;
117 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
120 if (!CONVERT_EXPR_P (exp))
123 sub = TREE_OPERAND (exp, 0);
124 subt = TREE_TYPE (sub);
125 expt = TREE_TYPE (exp);
127 if (!FLOAT_TYPE_P (subt))
130 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
133 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
136 return strip_float_extensions (sub);
140 /* Convert EXPR to some floating-point type TYPE.
142 EXPR must be float, fixed-point, integer, or enumeral;
143 in other cases error is called. */
146 convert_to_real (tree type, tree expr)
148 enum built_in_function fcode = builtin_mathfn_code (expr);
149 tree itype = TREE_TYPE (expr);
151 /* Disable until we figure out how to decide whether the functions are
152 present in runtime. */
153 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
155 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
156 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
160 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
175 /* The above functions may set errno differently with float
176 input or output so this transformation is not safe with
202 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
205 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
206 the both as the safe type for operation. */
207 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
208 newtype = TREE_TYPE (arg0);
210 /* Be careful about integer to fp conversions.
211 These may overflow still. */
212 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
213 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
214 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
215 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
217 tree fn = mathfn_built_in (newtype, fcode);
221 tree arg = fold (convert_to_real (newtype, arg0));
222 expr = build_call_expr (fn, 1, arg);
233 && (((fcode == BUILT_IN_FLOORL
234 || fcode == BUILT_IN_CEILL
235 || fcode == BUILT_IN_ROUNDL
236 || fcode == BUILT_IN_RINTL
237 || fcode == BUILT_IN_TRUNCL
238 || fcode == BUILT_IN_NEARBYINTL)
239 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
240 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
241 || ((fcode == BUILT_IN_FLOOR
242 || fcode == BUILT_IN_CEIL
243 || fcode == BUILT_IN_ROUND
244 || fcode == BUILT_IN_RINT
245 || fcode == BUILT_IN_TRUNC
246 || fcode == BUILT_IN_NEARBYINT)
247 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
249 tree fn = mathfn_built_in (type, fcode);
253 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
255 /* Make sure (type)arg0 is an extension, otherwise we could end up
256 changing (float)floor(double d) into floorf((float)d), which is
257 incorrect because (float)d uses round-to-nearest and can round
258 up to the next integer. */
259 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
260 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
264 /* Propagate the cast into the operation. */
265 if (itype != type && FLOAT_TYPE_P (type))
266 switch (TREE_CODE (expr))
268 /* Convert (float)-x into -(float)x. This is safe for
269 round-to-nearest rounding mode. */
272 if (!flag_rounding_math
273 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
274 return build1 (TREE_CODE (expr), type,
275 fold (convert_to_real (type,
276 TREE_OPERAND (expr, 0))));
278 /* Convert (outertype)((innertype0)a+(innertype1)b)
279 into ((newtype)a+(newtype)b) where newtype
280 is the widest mode from all of these. */
286 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
287 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
289 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
290 && FLOAT_TYPE_P (TREE_TYPE (arg1))
291 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
295 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
296 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
297 || TYPE_MODE (type) == SDmode)
298 newtype = dfloat32_type_node;
299 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
300 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
301 || TYPE_MODE (type) == DDmode)
302 newtype = dfloat64_type_node;
303 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
304 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
305 || TYPE_MODE (type) == TDmode)
306 newtype = dfloat128_type_node;
307 if (newtype == dfloat32_type_node
308 || newtype == dfloat64_type_node
309 || newtype == dfloat128_type_node)
311 expr = build2 (TREE_CODE (expr), newtype,
312 fold (convert_to_real (newtype, arg0)),
313 fold (convert_to_real (newtype, arg1)));
319 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
320 newtype = TREE_TYPE (arg0);
321 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
322 newtype = TREE_TYPE (arg1);
323 /* Sometimes this transformation is safe (cannot
324 change results through affecting double rounding
325 cases) and sometimes it is not. If NEWTYPE is
326 wider than TYPE, e.g. (float)((long double)double
327 + (long double)double) converted to
328 (float)(double + double), the transformation is
329 unsafe regardless of the details of the types
330 involved; double rounding can arise if the result
331 of NEWTYPE arithmetic is a NEWTYPE value half way
332 between two representable TYPE values but the
333 exact value is sufficiently different (in the
334 right direction) for this difference to be
335 visible in ITYPE arithmetic. If NEWTYPE is the
336 same as TYPE, however, the transformation may be
337 safe depending on the types involved: it is safe
338 if the ITYPE has strictly more than twice as many
339 mantissa bits as TYPE, can represent infinities
340 and NaNs if the TYPE can, and has sufficient
341 exponent range for the product or ratio of two
342 values representable in the TYPE to be within the
343 range of normal values of ITYPE. */
344 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
345 && (flag_unsafe_math_optimizations
346 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
347 && real_can_shorten_arithmetic (TYPE_MODE (itype),
349 && !excess_precision_type (newtype))))
351 expr = build2 (TREE_CODE (expr), newtype,
352 fold (convert_to_real (newtype, arg0)),
353 fold (convert_to_real (newtype, arg1)));
364 switch (TREE_CODE (TREE_TYPE (expr)))
367 /* Ignore the conversion if we don't need to store intermediate
368 results and neither type is a decimal float. */
369 return build1 ((flag_float_store
370 || DECIMAL_FLOAT_TYPE_P (type)
371 || DECIMAL_FLOAT_TYPE_P (itype))
372 ? CONVERT_EXPR : NOP_EXPR, type, expr);
377 return build1 (FLOAT_EXPR, type, expr);
379 case FIXED_POINT_TYPE:
380 return build1 (FIXED_CONVERT_EXPR, type, expr);
383 return convert (type,
384 fold_build1 (REALPART_EXPR,
385 TREE_TYPE (TREE_TYPE (expr)), expr));
389 error ("pointer value used where a floating point value was expected");
390 return convert_to_real (type, integer_zero_node);
393 error ("aggregate value used where a float was expected");
394 return convert_to_real (type, integer_zero_node);
398 /* Convert EXPR to some integer (or enum) type TYPE.
400 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
401 fixed-point or vector; in other cases error is called.
403 The result of this is always supposed to be a newly created tree node
404 not in use in any existing structure. */
407 convert_to_integer (tree type, tree expr)
409 enum tree_code ex_form = TREE_CODE (expr);
410 tree intype = TREE_TYPE (expr);
411 unsigned int inprec = TYPE_PRECISION (intype);
412 unsigned int outprec = TYPE_PRECISION (type);
414 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
415 be. Consider `enum E = { a, b = (enum E) 3 };'. */
416 if (!COMPLETE_TYPE_P (type))
418 error ("conversion to incomplete type");
419 return error_mark_node;
422 /* Convert e.g. (long)round(d) -> lround(d). */
423 /* If we're converting to char, we may encounter differing behavior
424 between converting from double->char vs double->long->char.
425 We're in "undefined" territory but we prefer to be conservative,
426 so only proceed in "unsafe" math mode. */
428 && (flag_unsafe_math_optimizations
429 || (long_integer_type_node
430 && outprec >= TYPE_PRECISION (long_integer_type_node))))
432 tree s_expr = strip_float_extensions (expr);
433 tree s_intype = TREE_TYPE (s_expr);
434 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
439 CASE_FLT_FN (BUILT_IN_CEIL):
440 /* Only convert in ISO C99 mode. */
441 if (!TARGET_C99_FUNCTIONS)
443 if (outprec < TYPE_PRECISION (integer_type_node)
444 || (outprec == TYPE_PRECISION (integer_type_node)
445 && !TYPE_UNSIGNED (type)))
446 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
447 else if (outprec == TYPE_PRECISION (long_integer_type_node)
448 && !TYPE_UNSIGNED (type))
449 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
450 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
451 && !TYPE_UNSIGNED (type))
452 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
455 CASE_FLT_FN (BUILT_IN_FLOOR):
456 /* Only convert in ISO C99 mode. */
457 if (!TARGET_C99_FUNCTIONS)
459 if (outprec < TYPE_PRECISION (integer_type_node)
460 || (outprec == TYPE_PRECISION (integer_type_node)
461 && !TYPE_UNSIGNED (type)))
462 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
463 else if (outprec == TYPE_PRECISION (long_integer_type_node)
464 && !TYPE_UNSIGNED (type))
465 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
466 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
467 && !TYPE_UNSIGNED (type))
468 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
471 CASE_FLT_FN (BUILT_IN_ROUND):
472 if (outprec < TYPE_PRECISION (integer_type_node)
473 || (outprec == TYPE_PRECISION (integer_type_node)
474 && !TYPE_UNSIGNED (type)))
475 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
476 else if (outprec == TYPE_PRECISION (long_integer_type_node)
477 && !TYPE_UNSIGNED (type))
478 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
479 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
480 && !TYPE_UNSIGNED (type))
481 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
484 CASE_FLT_FN (BUILT_IN_NEARBYINT):
485 /* Only convert nearbyint* if we can ignore math exceptions. */
486 if (flag_trapping_math)
488 /* ... Fall through ... */
489 CASE_FLT_FN (BUILT_IN_RINT):
490 if (outprec < TYPE_PRECISION (integer_type_node)
491 || (outprec == TYPE_PRECISION (integer_type_node)
492 && !TYPE_UNSIGNED (type)))
493 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
494 else if (outprec < TYPE_PRECISION (long_integer_type_node)
495 && !TYPE_UNSIGNED (type))
496 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
497 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
498 && !TYPE_UNSIGNED (type))
499 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
502 CASE_FLT_FN (BUILT_IN_TRUNC):
503 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
511 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
512 return convert_to_integer (type, newexpr);
516 /* Convert (int)logb(d) -> ilogb(d). */
518 && flag_unsafe_math_optimizations
519 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
521 && (outprec > TYPE_PRECISION (integer_type_node)
522 || (outprec == TYPE_PRECISION (integer_type_node)
523 && !TYPE_UNSIGNED (type))))
525 tree s_expr = strip_float_extensions (expr);
526 tree s_intype = TREE_TYPE (s_expr);
527 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
532 CASE_FLT_FN (BUILT_IN_LOGB):
533 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
542 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
543 return convert_to_integer (type, newexpr);
547 switch (TREE_CODE (intype))
551 if (integer_zerop (expr))
552 return build_int_cst (type, 0);
554 /* Convert to an unsigned integer of the correct width first, and from
555 there widen/truncate to the required type. Some targets support the
556 coexistence of multiple valid pointer sizes, so fetch the one we need
558 expr = fold_build1 (CONVERT_EXPR,
559 lang_hooks.types.type_for_size
560 (TYPE_PRECISION (intype), 0),
562 return fold_convert (type, expr);
568 /* If this is a logical operation, which just returns 0 or 1, we can
569 change the type of the expression. */
571 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
573 expr = copy_node (expr);
574 TREE_TYPE (expr) = type;
578 /* If we are widening the type, put in an explicit conversion.
579 Similarly if we are not changing the width. After this, we know
580 we are truncating EXPR. */
582 else if (outprec >= inprec)
587 /* If the precision of the EXPR's type is K bits and the
588 destination mode has more bits, and the sign is changing,
589 it is not safe to use a NOP_EXPR. For example, suppose
590 that EXPR's type is a 3-bit unsigned integer type, the
591 TYPE is a 3-bit signed integer type, and the machine mode
592 for the types is 8-bit QImode. In that case, the
593 conversion necessitates an explicit sign-extension. In
594 the signed-to-unsigned case the high-order bits have to
596 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
597 && (TYPE_PRECISION (TREE_TYPE (expr))
598 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
603 tem = fold_unary (code, type, expr);
607 tem = build1 (code, type, expr);
608 TREE_NO_WARNING (tem) = 1;
612 /* If TYPE is an enumeral type or a type with a precision less
613 than the number of bits in its mode, do the conversion to the
614 type corresponding to its mode, then do a nop conversion
616 else if (TREE_CODE (type) == ENUMERAL_TYPE
617 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
618 return build1 (NOP_EXPR, type,
619 convert (lang_hooks.types.type_for_mode
620 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
623 /* Here detect when we can distribute the truncation down past some
624 arithmetic. For example, if adding two longs and converting to an
625 int, we can equally well convert both to ints and then add.
626 For the operations handled here, such truncation distribution
628 It is desirable in these cases:
629 1) when truncating down to full-word from a larger size
630 2) when truncating takes no work.
631 3) when at least one operand of the arithmetic has been extended
632 (as by C's default conversions). In this case we need two conversions
633 if we do the arithmetic as already requested, so we might as well
634 truncate both and then combine. Perhaps that way we need only one.
636 Note that in general we cannot do the arithmetic in a type
637 shorter than the desired result of conversion, even if the operands
638 are both extended from a shorter type, because they might overflow
639 if combined in that type. The exceptions to this--the times when
640 two narrow values can be combined in their narrow type even to
641 make a wider result--are handled by "shorten" in build_binary_op. */
646 /* We can pass truncation down through right shifting
647 when the shift count is a nonpositive constant. */
648 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
649 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
654 /* We can pass truncation down through left shifting
655 when the shift count is a nonnegative constant and
656 the target type is unsigned. */
657 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
658 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
659 && TYPE_UNSIGNED (type)
660 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
662 /* If shift count is less than the width of the truncated type,
664 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
665 /* In this case, shifting is like multiplication. */
669 /* If it is >= that width, result is zero.
670 Handling this with trunc1 would give the wrong result:
671 (int) ((long long) a << 32) is well defined (as 0)
672 but (int) a << 32 is undefined and would get a
675 tree t = build_int_cst (type, 0);
677 /* If the original expression had side-effects, we must
679 if (TREE_SIDE_EFFECTS (expr))
680 return build2 (COMPOUND_EXPR, type, expr, t);
689 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
690 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
692 /* Don't distribute unless the output precision is at least as big
693 as the actual inputs and it has the same signedness. */
694 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
695 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
696 /* If signedness of arg0 and arg1 don't match,
697 we can't necessarily find a type to compare them in. */
698 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
699 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
700 /* Do not change the sign of the division. */
701 && (TYPE_UNSIGNED (TREE_TYPE (expr))
702 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
703 /* Either require unsigned division or a division by
704 a constant that is not -1. */
705 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
706 || (TREE_CODE (arg1) == INTEGER_CST
707 && !integer_all_onesp (arg1))))
716 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
717 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
719 /* Don't distribute unless the output precision is at least as big
720 as the actual inputs. Otherwise, the comparison of the
721 truncated values will be wrong. */
722 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
723 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
724 /* If signedness of arg0 and arg1 don't match,
725 we can't necessarily find a type to compare them in. */
726 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
727 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
739 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
740 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
742 if (outprec >= BITS_PER_WORD
743 || TRULY_NOOP_TRUNCATION (outprec, inprec)
744 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
745 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
747 /* Do the arithmetic in type TYPEX,
748 then convert result to TYPE. */
751 /* Can't do arithmetic in enumeral types
752 so use an integer type that will hold the values. */
753 if (TREE_CODE (typex) == ENUMERAL_TYPE)
754 typex = lang_hooks.types.type_for_size
755 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
757 /* But now perhaps TYPEX is as wide as INPREC.
758 In that case, do nothing special here.
759 (Otherwise would recurse infinitely in convert. */
760 if (TYPE_PRECISION (typex) != inprec)
762 /* Don't do unsigned arithmetic where signed was wanted,
764 Exception: if both of the original operands were
765 unsigned then we can safely do the work as unsigned.
766 Exception: shift operations take their type solely
767 from the first argument.
768 Exception: the LSHIFT_EXPR case above requires that
769 we perform this operation unsigned lest we produce
770 signed-overflow undefinedness.
771 And we may need to do it as unsigned
772 if we truncate to the original size. */
773 if (TYPE_UNSIGNED (TREE_TYPE (expr))
774 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
775 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
776 || ex_form == LSHIFT_EXPR
777 || ex_form == RSHIFT_EXPR
778 || ex_form == LROTATE_EXPR
779 || ex_form == RROTATE_EXPR))
780 || ex_form == LSHIFT_EXPR
781 /* If we have !flag_wrapv, and either ARG0 or
782 ARG1 is of a signed type, we have to do
783 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
784 type in case the operation in outprec precision
785 could overflow. Otherwise, we would introduce
786 signed-overflow undefinedness. */
787 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
788 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
789 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
791 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
793 && (ex_form == PLUS_EXPR
794 || ex_form == MINUS_EXPR
795 || ex_form == MULT_EXPR)))
796 typex = unsigned_type_for (typex);
798 typex = signed_type_for (typex);
799 return convert (type,
800 fold_build2 (ex_form, typex,
801 convert (typex, arg0),
802 convert (typex, arg1)));
810 /* This is not correct for ABS_EXPR,
811 since we must test the sign before truncation. */
813 tree typex = unsigned_type_for (type);
814 return convert (type,
815 fold_build1 (ex_form, typex,
817 TREE_OPERAND (expr, 0))));
822 "can't convert between vector values of different size" error. */
823 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
824 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
825 != GET_MODE_SIZE (TYPE_MODE (type))))
827 /* If truncating after truncating, might as well do all at once.
828 If truncating after extending, we may get rid of wasted work. */
829 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
832 /* It is sometimes worthwhile to push the narrowing down through
833 the conditional and never loses. A COND_EXPR may have a throw
834 as one operand, which then has void type. Just leave void
835 operands as they are. */
836 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
837 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
838 ? TREE_OPERAND (expr, 1)
839 : convert (type, TREE_OPERAND (expr, 1)),
840 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
841 ? TREE_OPERAND (expr, 2)
842 : convert (type, TREE_OPERAND (expr, 2)));
848 return build1 (CONVERT_EXPR, type, expr);
851 return build1 (FIX_TRUNC_EXPR, type, expr);
853 case FIXED_POINT_TYPE:
854 return build1 (FIXED_CONVERT_EXPR, type, expr);
857 return convert (type,
858 fold_build1 (REALPART_EXPR,
859 TREE_TYPE (TREE_TYPE (expr)), expr));
862 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
864 error ("can%'t convert between vector values of different size");
865 return error_mark_node;
867 return build1 (VIEW_CONVERT_EXPR, type, expr);
870 error ("aggregate value used where an integer was expected");
871 return convert (type, integer_zero_node);
875 /* Convert EXPR to the complex type TYPE in the usual ways. */
878 convert_to_complex (tree type, tree expr)
880 tree subtype = TREE_TYPE (type);
882 switch (TREE_CODE (TREE_TYPE (expr)))
885 case FIXED_POINT_TYPE:
889 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
890 convert (subtype, integer_zero_node));
894 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
896 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
898 else if (TREE_CODE (expr) == COMPLEX_EXPR)
899 return fold_build2 (COMPLEX_EXPR, type,
900 convert (subtype, TREE_OPERAND (expr, 0)),
901 convert (subtype, TREE_OPERAND (expr, 1)));
904 expr = save_expr (expr);
906 fold_build2 (COMPLEX_EXPR, type,
908 fold_build1 (REALPART_EXPR,
909 TREE_TYPE (TREE_TYPE (expr)),
912 fold_build1 (IMAGPART_EXPR,
913 TREE_TYPE (TREE_TYPE (expr)),
920 error ("pointer value used where a complex was expected");
921 return convert_to_complex (type, integer_zero_node);
924 error ("aggregate value used where a complex was expected");
925 return convert_to_complex (type, integer_zero_node);
929 /* Convert EXPR to the vector type TYPE in the usual ways. */
932 convert_to_vector (tree type, tree expr)
934 switch (TREE_CODE (TREE_TYPE (expr)))
938 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
940 error ("can%'t convert between vector values of different size");
941 return error_mark_node;
943 return build1 (VIEW_CONVERT_EXPR, type, expr);
946 error ("can%'t convert value to a vector");
947 return error_mark_node;
951 /* Convert EXPR to some fixed-point type TYPE.
953 EXPR must be fixed-point, float, integer, or enumeral;
954 in other cases error is called. */
957 convert_to_fixed (tree type, tree expr)
959 if (integer_zerop (expr))
961 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
962 return fixed_zero_node;
964 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
966 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
967 return fixed_one_node;
970 switch (TREE_CODE (TREE_TYPE (expr)))
972 case FIXED_POINT_TYPE:
977 return build1 (FIXED_CONVERT_EXPR, type, expr);
980 return convert (type,
981 fold_build1 (REALPART_EXPR,
982 TREE_TYPE (TREE_TYPE (expr)), expr));
985 error ("aggregate value used where a fixed-point was expected");
986 return error_mark_node;