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 (integer_type_node
)
439 || (outprec
== TYPE_PRECISION (integer_type_node
)
440 && !TYPE_UNSIGNED (type
)))
441 fn
= mathfn_built_in (s_intype
, BUILT_IN_ICEIL
);
442 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
443 && !TYPE_UNSIGNED (type
))
444 fn
= mathfn_built_in (s_intype
, BUILT_IN_LCEIL
);
445 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
446 && !TYPE_UNSIGNED (type
))
447 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLCEIL
);
450 CASE_FLT_FN (BUILT_IN_FLOOR
):
451 /* Only convert in ISO C99 mode. */
452 if (!TARGET_C99_FUNCTIONS
)
454 if (outprec
< TYPE_PRECISION (integer_type_node
)
455 || (outprec
== TYPE_PRECISION (integer_type_node
)
456 && !TYPE_UNSIGNED (type
)))
457 fn
= mathfn_built_in (s_intype
, BUILT_IN_IFLOOR
);
458 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
459 && !TYPE_UNSIGNED (type
))
460 fn
= mathfn_built_in (s_intype
, BUILT_IN_LFLOOR
);
461 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
462 && !TYPE_UNSIGNED (type
))
463 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLFLOOR
);
466 CASE_FLT_FN (BUILT_IN_ROUND
):
467 /* Only convert in ISO C99 mode. */
468 if (!TARGET_C99_FUNCTIONS
)
470 if (outprec
< TYPE_PRECISION (integer_type_node
)
471 || (outprec
== TYPE_PRECISION (integer_type_node
)
472 && !TYPE_UNSIGNED (type
)))
473 fn
= mathfn_built_in (s_intype
, BUILT_IN_IROUND
);
474 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
475 && !TYPE_UNSIGNED (type
))
476 fn
= mathfn_built_in (s_intype
, BUILT_IN_LROUND
);
477 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
478 && !TYPE_UNSIGNED (type
))
479 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLROUND
);
482 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
483 /* Only convert nearbyint* if we can ignore math exceptions. */
484 if (flag_trapping_math
)
486 /* ... Fall through ... */
487 CASE_FLT_FN (BUILT_IN_RINT
):
488 /* Only convert in ISO C99 mode. */
489 if (!TARGET_C99_FUNCTIONS
)
491 if (outprec
< TYPE_PRECISION (integer_type_node
)
492 || (outprec
== TYPE_PRECISION (integer_type_node
)
493 && !TYPE_UNSIGNED (type
)))
494 fn
= mathfn_built_in (s_intype
, BUILT_IN_IRINT
);
495 else if (outprec
== TYPE_PRECISION (long_integer_type_node
)
496 && !TYPE_UNSIGNED (type
))
497 fn
= mathfn_built_in (s_intype
, BUILT_IN_LRINT
);
498 else if (outprec
== TYPE_PRECISION (long_long_integer_type_node
)
499 && !TYPE_UNSIGNED (type
))
500 fn
= mathfn_built_in (s_intype
, BUILT_IN_LLRINT
);
503 CASE_FLT_FN (BUILT_IN_TRUNC
):
504 return convert_to_integer (type
, CALL_EXPR_ARG (s_expr
, 0));
512 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
513 return convert_to_integer (type
, newexpr
);
517 /* Convert (int)logb(d) -> ilogb(d). */
519 && flag_unsafe_math_optimizations
520 && !flag_trapping_math
&& !flag_errno_math
&& flag_finite_math_only
522 && (outprec
> TYPE_PRECISION (integer_type_node
)
523 || (outprec
== TYPE_PRECISION (integer_type_node
)
524 && !TYPE_UNSIGNED (type
))))
526 tree s_expr
= strip_float_extensions (expr
);
527 tree s_intype
= TREE_TYPE (s_expr
);
528 const enum built_in_function fcode
= builtin_mathfn_code (s_expr
);
533 CASE_FLT_FN (BUILT_IN_LOGB
):
534 fn
= mathfn_built_in (s_intype
, BUILT_IN_ILOGB
);
543 tree newexpr
= build_call_expr (fn
, 1, CALL_EXPR_ARG (s_expr
, 0));
544 return convert_to_integer (type
, newexpr
);
548 switch (TREE_CODE (intype
))
552 if (integer_zerop (expr
))
553 return build_int_cst (type
, 0);
555 /* Convert to an unsigned integer of the correct width first, and from
556 there widen/truncate to the required type. Some targets support the
557 coexistence of multiple valid pointer sizes, so fetch the one we need
559 expr
= fold_build1 (CONVERT_EXPR
,
560 lang_hooks
.types
.type_for_size
561 (TYPE_PRECISION (intype
), 0),
563 return fold_convert (type
, expr
);
569 /* If this is a logical operation, which just returns 0 or 1, we can
570 change the type of the expression. */
572 if (TREE_CODE_CLASS (ex_form
) == tcc_comparison
)
574 expr
= copy_node (expr
);
575 TREE_TYPE (expr
) = type
;
579 /* If we are widening the type, put in an explicit conversion.
580 Similarly if we are not changing the width. After this, we know
581 we are truncating EXPR. */
583 else if (outprec
>= inprec
)
588 /* If the precision of the EXPR's type is K bits and the
589 destination mode has more bits, and the sign is changing,
590 it is not safe to use a NOP_EXPR. For example, suppose
591 that EXPR's type is a 3-bit unsigned integer type, the
592 TYPE is a 3-bit signed integer type, and the machine mode
593 for the types is 8-bit QImode. In that case, the
594 conversion necessitates an explicit sign-extension. In
595 the signed-to-unsigned case the high-order bits have to
597 if (TYPE_UNSIGNED (type
) != TYPE_UNSIGNED (TREE_TYPE (expr
))
598 && (TYPE_PRECISION (TREE_TYPE (expr
))
599 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr
)))))
604 tem
= fold_unary (code
, type
, expr
);
608 tem
= build1 (code
, type
, expr
);
609 TREE_NO_WARNING (tem
) = 1;
613 /* If TYPE is an enumeral type or a type with a precision less
614 than the number of bits in its mode, do the conversion to the
615 type corresponding to its mode, then do a nop conversion
617 else if (TREE_CODE (type
) == ENUMERAL_TYPE
618 || outprec
!= GET_MODE_PRECISION (TYPE_MODE (type
)))
619 return build1 (NOP_EXPR
, type
,
620 convert (lang_hooks
.types
.type_for_mode
621 (TYPE_MODE (type
), TYPE_UNSIGNED (type
)),
624 /* Here detect when we can distribute the truncation down past some
625 arithmetic. For example, if adding two longs and converting to an
626 int, we can equally well convert both to ints and then add.
627 For the operations handled here, such truncation distribution
629 It is desirable in these cases:
630 1) when truncating down to full-word from a larger size
631 2) when truncating takes no work.
632 3) when at least one operand of the arithmetic has been extended
633 (as by C's default conversions). In this case we need two conversions
634 if we do the arithmetic as already requested, so we might as well
635 truncate both and then combine. Perhaps that way we need only one.
637 Note that in general we cannot do the arithmetic in a type
638 shorter than the desired result of conversion, even if the operands
639 are both extended from a shorter type, because they might overflow
640 if combined in that type. The exceptions to this--the times when
641 two narrow values can be combined in their narrow type even to
642 make a wider result--are handled by "shorten" in build_binary_op. */
647 /* We can pass truncation down through right shifting
648 when the shift count is a nonpositive constant. */
649 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
650 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) <= 0)
655 /* We can pass truncation down through left shifting
656 when the shift count is a nonnegative constant and
657 the target type is unsigned. */
658 if (TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
659 && tree_int_cst_sgn (TREE_OPERAND (expr
, 1)) >= 0
660 && TYPE_UNSIGNED (type
)
661 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
663 /* If shift count is less than the width of the truncated type,
665 if (tree_int_cst_lt (TREE_OPERAND (expr
, 1), TYPE_SIZE (type
)))
666 /* In this case, shifting is like multiplication. */
670 /* If it is >= that width, result is zero.
671 Handling this with trunc1 would give the wrong result:
672 (int) ((long long) a << 32) is well defined (as 0)
673 but (int) a << 32 is undefined and would get a
676 tree t
= build_int_cst (type
, 0);
678 /* If the original expression had side-effects, we must
680 if (TREE_SIDE_EFFECTS (expr
))
681 return build2 (COMPOUND_EXPR
, type
, expr
, t
);
690 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
691 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
693 /* Don't distribute unless the output precision is at least as big
694 as the actual inputs and it has the same signedness. */
695 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
696 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
697 /* If signedness of arg0 and arg1 don't match,
698 we can't necessarily find a type to compare them in. */
699 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
700 == TYPE_UNSIGNED (TREE_TYPE (arg1
)))
701 /* Do not change the sign of the division. */
702 && (TYPE_UNSIGNED (TREE_TYPE (expr
))
703 == TYPE_UNSIGNED (TREE_TYPE (arg0
)))
704 /* Either require unsigned division or a division by
705 a constant that is not -1. */
706 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
707 || (TREE_CODE (arg1
) == INTEGER_CST
708 && !integer_all_onesp (arg1
))))
717 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
718 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
720 /* Don't distribute unless the output precision is at least as big
721 as the actual inputs. Otherwise, the comparison of the
722 truncated values will be wrong. */
723 if (outprec
>= TYPE_PRECISION (TREE_TYPE (arg0
))
724 && outprec
>= TYPE_PRECISION (TREE_TYPE (arg1
))
725 /* If signedness of arg0 and arg1 don't match,
726 we can't necessarily find a type to compare them in. */
727 && (TYPE_UNSIGNED (TREE_TYPE (arg0
))
728 == TYPE_UNSIGNED (TREE_TYPE (arg1
))))
740 tree arg0
= get_unwidened (TREE_OPERAND (expr
, 0), type
);
741 tree arg1
= get_unwidened (TREE_OPERAND (expr
, 1), type
);
743 /* Do not try to narrow operands of pointer subtraction;
744 that will interfere with other folding. */
745 if (ex_form
== MINUS_EXPR
746 && CONVERT_EXPR_P (arg0
)
747 && CONVERT_EXPR_P (arg1
)
748 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
749 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
752 if (outprec
>= BITS_PER_WORD
753 || TRULY_NOOP_TRUNCATION (outprec
, inprec
)
754 || inprec
> TYPE_PRECISION (TREE_TYPE (arg0
))
755 || inprec
> TYPE_PRECISION (TREE_TYPE (arg1
)))
757 /* Do the arithmetic in type TYPEX,
758 then convert result to TYPE. */
761 /* Can't do arithmetic in enumeral types
762 so use an integer type that will hold the values. */
763 if (TREE_CODE (typex
) == ENUMERAL_TYPE
)
764 typex
= lang_hooks
.types
.type_for_size
765 (TYPE_PRECISION (typex
), TYPE_UNSIGNED (typex
));
767 /* But now perhaps TYPEX is as wide as INPREC.
768 In that case, do nothing special here.
769 (Otherwise would recurse infinitely in convert. */
770 if (TYPE_PRECISION (typex
) != inprec
)
773 /* Don't do unsigned arithmetic where signed was wanted,
775 Exception: if both of the original operands were
776 unsigned then we can safely do the work as unsigned.
777 Exception: shift operations take their type solely
778 from the first argument.
779 Exception: the LSHIFT_EXPR case above requires that
780 we perform this operation unsigned lest we produce
781 signed-overflow undefinedness.
782 And we may need to do it as unsigned
783 if we truncate to the original size. */
784 if (TYPE_UNSIGNED (TREE_TYPE (expr
))
785 || (TYPE_UNSIGNED (TREE_TYPE (arg0
))
786 && (TYPE_UNSIGNED (TREE_TYPE (arg1
))
787 || ex_form
== LSHIFT_EXPR
788 || ex_form
== RSHIFT_EXPR
789 || ex_form
== LROTATE_EXPR
790 || ex_form
== RROTATE_EXPR
))
791 || ex_form
== LSHIFT_EXPR
792 /* If we have !flag_wrapv, and either ARG0 or
793 ARG1 is of a signed type, we have to do
794 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
795 type in case the operation in outprec precision
796 could overflow. Otherwise, we would introduce
797 signed-overflow undefinedness. */
798 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
799 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
800 && ((TYPE_PRECISION (TREE_TYPE (arg0
)) * 2u
802 || (TYPE_PRECISION (TREE_TYPE (arg1
)) * 2u
804 && (ex_form
== PLUS_EXPR
805 || ex_form
== MINUS_EXPR
806 || ex_form
== MULT_EXPR
)))
807 typex
= unsigned_type_for (typex
);
809 typex
= signed_type_for (typex
);
811 if (TYPE_PRECISION (otypex
) == TYPE_PRECISION (typex
))
812 return convert (type
,
813 fold_build2 (ex_form
, typex
,
814 convert (typex
, arg0
),
815 convert (typex
, arg1
)));
823 /* This is not correct for ABS_EXPR,
824 since we must test the sign before truncation. */
826 tree typex
= unsigned_type_for (type
);
827 return convert (type
,
828 fold_build1 (ex_form
, typex
,
830 TREE_OPERAND (expr
, 0))));
835 "can't convert between vector values of different size" error. */
836 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == VECTOR_TYPE
837 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr
, 0))))
838 != GET_MODE_SIZE (TYPE_MODE (type
))))
840 /* If truncating after truncating, might as well do all at once.
841 If truncating after extending, we may get rid of wasted work. */
842 return convert (type
, get_unwidened (TREE_OPERAND (expr
, 0), type
));
845 /* It is sometimes worthwhile to push the narrowing down through
846 the conditional and never loses. A COND_EXPR may have a throw
847 as one operand, which then has void type. Just leave void
848 operands as they are. */
849 return fold_build3 (COND_EXPR
, type
, TREE_OPERAND (expr
, 0),
850 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 1)))
851 ? TREE_OPERAND (expr
, 1)
852 : convert (type
, TREE_OPERAND (expr
, 1)),
853 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 2)))
854 ? TREE_OPERAND (expr
, 2)
855 : convert (type
, TREE_OPERAND (expr
, 2)));
861 /* When parsing long initializers, we might end up with a lot of casts.
863 if (TREE_CODE (expr
) == INTEGER_CST
)
864 return fold_convert (type
, expr
);
865 return build1 (CONVERT_EXPR
, type
, expr
);
868 return build1 (FIX_TRUNC_EXPR
, type
, expr
);
870 case FIXED_POINT_TYPE
:
871 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
874 return convert (type
,
875 fold_build1 (REALPART_EXPR
,
876 TREE_TYPE (TREE_TYPE (expr
)), expr
));
879 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
881 error ("can%'t convert between vector values of different size");
882 return error_mark_node
;
884 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
887 error ("aggregate value used where an integer was expected");
888 return convert (type
, integer_zero_node
);
892 /* Convert EXPR to the complex type TYPE in the usual ways. */
895 convert_to_complex (tree type
, tree expr
)
897 tree subtype
= TREE_TYPE (type
);
899 switch (TREE_CODE (TREE_TYPE (expr
)))
902 case FIXED_POINT_TYPE
:
906 return build2 (COMPLEX_EXPR
, type
, convert (subtype
, expr
),
907 convert (subtype
, integer_zero_node
));
911 tree elt_type
= TREE_TYPE (TREE_TYPE (expr
));
913 if (TYPE_MAIN_VARIANT (elt_type
) == TYPE_MAIN_VARIANT (subtype
))
915 else if (TREE_CODE (expr
) == COMPLEX_EXPR
)
916 return fold_build2 (COMPLEX_EXPR
, type
,
917 convert (subtype
, TREE_OPERAND (expr
, 0)),
918 convert (subtype
, TREE_OPERAND (expr
, 1)));
921 expr
= save_expr (expr
);
923 fold_build2 (COMPLEX_EXPR
, type
,
925 fold_build1 (REALPART_EXPR
,
926 TREE_TYPE (TREE_TYPE (expr
)),
929 fold_build1 (IMAGPART_EXPR
,
930 TREE_TYPE (TREE_TYPE (expr
)),
937 error ("pointer value used where a complex was expected");
938 return convert_to_complex (type
, integer_zero_node
);
941 error ("aggregate value used where a complex was expected");
942 return convert_to_complex (type
, integer_zero_node
);
946 /* Convert EXPR to the vector type TYPE in the usual ways. */
949 convert_to_vector (tree type
, tree expr
)
951 switch (TREE_CODE (TREE_TYPE (expr
)))
955 if (!tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (TREE_TYPE (expr
))))
957 error ("can%'t convert between vector values of different size");
958 return error_mark_node
;
960 return build1 (VIEW_CONVERT_EXPR
, type
, expr
);
963 error ("can%'t convert value to a vector");
964 return error_mark_node
;
968 /* Convert EXPR to some fixed-point type TYPE.
970 EXPR must be fixed-point, float, integer, or enumeral;
971 in other cases error is called. */
974 convert_to_fixed (tree type
, tree expr
)
976 if (integer_zerop (expr
))
978 tree fixed_zero_node
= build_fixed (type
, FCONST0 (TYPE_MODE (type
)));
979 return fixed_zero_node
;
981 else if (integer_onep (expr
) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type
)))
983 tree fixed_one_node
= build_fixed (type
, FCONST1 (TYPE_MODE (type
)));
984 return fixed_one_node
;
987 switch (TREE_CODE (TREE_TYPE (expr
)))
989 case FIXED_POINT_TYPE
:
994 return build1 (FIXED_CONVERT_EXPR
, type
, expr
);
997 return convert (type
,
998 fold_build1 (REALPART_EXPR
,
999 TREE_TYPE (TREE_TYPE (expr
)), expr
));
1002 error ("aggregate value used where a fixed-point was expected");
1003 return error_mark_node
;