Released GUPC 4.9.0.1 based on version 203902.
[official-gcc.git] / gcc / convert.c
blobfa75e359c25cd6e81e59c9803e5ade5ec1e2d0f3
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
9 version.
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
14 for more details.
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. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "diagnostic-core.h"
32 #include "target.h"
33 #include "langhooks.h"
35 /* Convert EXPR to some pointer or reference type TYPE.
36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
37 in other cases error is called. */
39 tree
40 convert_to_pointer (tree type, tree expr)
42 location_t loc = EXPR_LOCATION (expr);
43 if (TREE_TYPE (expr) == type)
44 return expr;
46 if (integer_zerop (expr) && POINTER_TYPE_P (type)
47 && upc_shared_type_p (TREE_TYPE (type)))
49 expr = copy_node (upc_null_pts_node);
50 TREE_TYPE (expr) = build_upc_unshared_type (type);
51 return expr;
54 switch (TREE_CODE (TREE_TYPE (expr)))
56 case POINTER_TYPE:
57 case REFERENCE_TYPE:
59 /* If the pointers point to different address spaces, conversion needs
60 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
61 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
62 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
64 if (to_as == from_as)
65 return fold_build1_loc (loc, NOP_EXPR, type, expr);
66 else
67 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
70 case INTEGER_TYPE:
71 case ENUMERAL_TYPE:
72 case BOOLEAN_TYPE:
74 /* If the input precision differs from the target pointer type
75 precision, first convert the input expression to an integer type of
76 the target precision. Some targets, e.g. VMS, need several pointer
77 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
78 unsigned int pprec = TYPE_PRECISION (type);
79 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
81 if (eprec != pprec)
82 expr = fold_build1_loc (loc, NOP_EXPR,
83 lang_hooks.types.type_for_size (pprec, 0),
84 expr);
87 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
89 default:
90 error ("cannot convert to a pointer type");
91 return convert_to_pointer (type, integer_zero_node);
96 /* Convert EXPR to some floating-point type TYPE.
98 EXPR must be float, fixed-point, integer, or enumeral;
99 in other cases error is called. */
101 tree
102 convert_to_real (tree type, tree expr)
104 enum built_in_function fcode = builtin_mathfn_code (expr);
105 tree itype = TREE_TYPE (expr);
107 /* Disable until we figure out how to decide whether the functions are
108 present in runtime. */
109 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
110 if (optimize
111 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
112 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
114 switch (fcode)
116 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
117 CASE_MATHFN (COSH)
118 CASE_MATHFN (EXP)
119 CASE_MATHFN (EXP10)
120 CASE_MATHFN (EXP2)
121 CASE_MATHFN (EXPM1)
122 CASE_MATHFN (GAMMA)
123 CASE_MATHFN (J0)
124 CASE_MATHFN (J1)
125 CASE_MATHFN (LGAMMA)
126 CASE_MATHFN (POW10)
127 CASE_MATHFN (SINH)
128 CASE_MATHFN (TGAMMA)
129 CASE_MATHFN (Y0)
130 CASE_MATHFN (Y1)
131 /* The above functions may set errno differently with float
132 input or output so this transformation is not safe with
133 -fmath-errno. */
134 if (flag_errno_math)
135 break;
136 CASE_MATHFN (ACOS)
137 CASE_MATHFN (ACOSH)
138 CASE_MATHFN (ASIN)
139 CASE_MATHFN (ASINH)
140 CASE_MATHFN (ATAN)
141 CASE_MATHFN (ATANH)
142 CASE_MATHFN (CBRT)
143 CASE_MATHFN (COS)
144 CASE_MATHFN (ERF)
145 CASE_MATHFN (ERFC)
146 CASE_MATHFN (LOG)
147 CASE_MATHFN (LOG10)
148 CASE_MATHFN (LOG2)
149 CASE_MATHFN (LOG1P)
150 CASE_MATHFN (SIN)
151 CASE_MATHFN (TAN)
152 CASE_MATHFN (TANH)
153 /* The above functions are not safe to do this conversion. */
154 if (!flag_unsafe_math_optimizations)
155 break;
156 CASE_MATHFN (SQRT)
157 CASE_MATHFN (FABS)
158 CASE_MATHFN (LOGB)
159 #undef CASE_MATHFN
161 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
162 tree newtype = type;
164 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
165 the both as the safe type for operation. */
166 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
167 newtype = TREE_TYPE (arg0);
169 /* We consider to convert
171 (T1) sqrtT2 ((T2) exprT3)
173 (T1) sqrtT4 ((T4) exprT3)
175 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
176 and T4 is NEWTYPE. All those types are of floating point types.
177 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
178 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
179 T2 and T4. See the following URL for a reference:
180 http://stackoverflow.com/questions/9235456/determining-
181 floating-point-square-root
183 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
184 && !flag_unsafe_math_optimizations)
186 /* The following conversion is unsafe even the precision condition
187 below is satisfied:
189 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
191 if (TYPE_MODE (type) != TYPE_MODE (newtype))
192 break;
194 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
195 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
196 if (p1 < p2 * 2 + 2)
197 break;
200 /* Be careful about integer to fp conversions.
201 These may overflow still. */
202 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
203 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
204 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
205 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
207 tree fn = mathfn_built_in (newtype, fcode);
209 if (fn)
211 tree arg = fold (convert_to_real (newtype, arg0));
212 expr = build_call_expr (fn, 1, arg);
213 if (newtype == type)
214 return expr;
218 default:
219 break;
222 if (optimize
223 && (((fcode == BUILT_IN_FLOORL
224 || fcode == BUILT_IN_CEILL
225 || fcode == BUILT_IN_ROUNDL
226 || fcode == BUILT_IN_RINTL
227 || fcode == BUILT_IN_TRUNCL
228 || fcode == BUILT_IN_NEARBYINTL)
229 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
230 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
231 || ((fcode == BUILT_IN_FLOOR
232 || fcode == BUILT_IN_CEIL
233 || fcode == BUILT_IN_ROUND
234 || fcode == BUILT_IN_RINT
235 || fcode == BUILT_IN_TRUNC
236 || fcode == BUILT_IN_NEARBYINT)
237 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
239 tree fn = mathfn_built_in (type, fcode);
241 if (fn)
243 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
245 /* Make sure (type)arg0 is an extension, otherwise we could end up
246 changing (float)floor(double d) into floorf((float)d), which is
247 incorrect because (float)d uses round-to-nearest and can round
248 up to the next integer. */
249 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
250 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
254 /* Propagate the cast into the operation. */
255 if (itype != type && FLOAT_TYPE_P (type))
256 switch (TREE_CODE (expr))
258 /* Convert (float)-x into -(float)x. This is safe for
259 round-to-nearest rounding mode when the inner type is float. */
260 case ABS_EXPR:
261 case NEGATE_EXPR:
262 if (!flag_rounding_math
263 && FLOAT_TYPE_P (itype)
264 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
265 return build1 (TREE_CODE (expr), type,
266 fold (convert_to_real (type,
267 TREE_OPERAND (expr, 0))));
268 break;
269 /* Convert (outertype)((innertype0)a+(innertype1)b)
270 into ((newtype)a+(newtype)b) where newtype
271 is the widest mode from all of these. */
272 case PLUS_EXPR:
273 case MINUS_EXPR:
274 case MULT_EXPR:
275 case RDIV_EXPR:
277 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
278 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
280 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
281 && FLOAT_TYPE_P (TREE_TYPE (arg1))
282 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
284 tree newtype = type;
286 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
287 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
288 || TYPE_MODE (type) == SDmode)
289 newtype = dfloat32_type_node;
290 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
291 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
292 || TYPE_MODE (type) == DDmode)
293 newtype = dfloat64_type_node;
294 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
295 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
296 || TYPE_MODE (type) == TDmode)
297 newtype = dfloat128_type_node;
298 if (newtype == dfloat32_type_node
299 || newtype == dfloat64_type_node
300 || newtype == dfloat128_type_node)
302 expr = build2 (TREE_CODE (expr), newtype,
303 fold (convert_to_real (newtype, arg0)),
304 fold (convert_to_real (newtype, arg1)));
305 if (newtype == type)
306 return expr;
307 break;
310 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
311 newtype = TREE_TYPE (arg0);
312 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
313 newtype = TREE_TYPE (arg1);
314 /* Sometimes this transformation is safe (cannot
315 change results through affecting double rounding
316 cases) and sometimes it is not. If NEWTYPE is
317 wider than TYPE, e.g. (float)((long double)double
318 + (long double)double) converted to
319 (float)(double + double), the transformation is
320 unsafe regardless of the details of the types
321 involved; double rounding can arise if the result
322 of NEWTYPE arithmetic is a NEWTYPE value half way
323 between two representable TYPE values but the
324 exact value is sufficiently different (in the
325 right direction) for this difference to be
326 visible in ITYPE arithmetic. If NEWTYPE is the
327 same as TYPE, however, the transformation may be
328 safe depending on the types involved: it is safe
329 if the ITYPE has strictly more than twice as many
330 mantissa bits as TYPE, can represent infinities
331 and NaNs if the TYPE can, and has sufficient
332 exponent range for the product or ratio of two
333 values representable in the TYPE to be within the
334 range of normal values of ITYPE. */
335 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
336 && (flag_unsafe_math_optimizations
337 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
338 && real_can_shorten_arithmetic (TYPE_MODE (itype),
339 TYPE_MODE (type))
340 && !excess_precision_type (newtype))))
342 expr = build2 (TREE_CODE (expr), newtype,
343 fold (convert_to_real (newtype, arg0)),
344 fold (convert_to_real (newtype, arg1)));
345 if (newtype == type)
346 return expr;
350 break;
351 default:
352 break;
355 switch (TREE_CODE (TREE_TYPE (expr)))
357 case REAL_TYPE:
358 /* Ignore the conversion if we don't need to store intermediate
359 results and neither type is a decimal float. */
360 return build1 ((flag_float_store
361 || DECIMAL_FLOAT_TYPE_P (type)
362 || DECIMAL_FLOAT_TYPE_P (itype))
363 ? CONVERT_EXPR : NOP_EXPR, type, expr);
365 case INTEGER_TYPE:
366 case ENUMERAL_TYPE:
367 case BOOLEAN_TYPE:
368 return build1 (FLOAT_EXPR, type, expr);
370 case FIXED_POINT_TYPE:
371 return build1 (FIXED_CONVERT_EXPR, type, expr);
373 case COMPLEX_TYPE:
374 return convert (type,
375 fold_build1 (REALPART_EXPR,
376 TREE_TYPE (TREE_TYPE (expr)), expr));
378 case POINTER_TYPE:
379 case REFERENCE_TYPE:
380 error ("pointer value used where a floating point value was expected");
381 return convert_to_real (type, integer_zero_node);
383 default:
384 error ("aggregate value used where a float was expected");
385 return convert_to_real (type, integer_zero_node);
389 /* Convert EXPR to some integer (or enum) type TYPE.
391 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
392 fixed-point or vector; in other cases error is called.
394 The result of this is always supposed to be a newly created tree node
395 not in use in any existing structure. */
397 tree
398 convert_to_integer (tree type, tree expr)
400 enum tree_code ex_form = TREE_CODE (expr);
401 tree intype = TREE_TYPE (expr);
402 unsigned int inprec = element_precision (intype);
403 unsigned int outprec = element_precision (type);
405 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
406 be. Consider `enum E = { a, b = (enum E) 3 };'. */
407 if (!COMPLETE_TYPE_P (type))
409 error ("conversion to incomplete type");
410 return error_mark_node;
413 /* Can't optimize the conversion of UPC shared pointer difference. */
414 if (ex_form == MINUS_EXPR
415 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
416 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
417 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (TREE_OPERAND (expr, 0))))
418 && upc_shared_type_p (TREE_TYPE (TREE_TYPE (TREE_OPERAND (expr, 1)))))
420 return build1 (CONVERT_EXPR, type, expr);
423 /* Convert e.g. (long)round(d) -> lround(d). */
424 /* If we're converting to char, we may encounter differing behavior
425 between converting from double->char vs double->long->char.
426 We're in "undefined" territory but we prefer to be conservative,
427 so only proceed in "unsafe" math mode. */
428 if (optimize
429 && (flag_unsafe_math_optimizations
430 || (long_integer_type_node
431 && outprec >= TYPE_PRECISION (long_integer_type_node))))
433 tree s_expr = strip_float_extensions (expr);
434 tree s_intype = TREE_TYPE (s_expr);
435 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
436 tree fn = 0;
438 switch (fcode)
440 CASE_FLT_FN (BUILT_IN_CEIL):
441 /* Only convert in ISO C99 mode. */
442 if (!targetm.libc_has_function (function_c99_misc))
443 break;
444 if (outprec < TYPE_PRECISION (integer_type_node)
445 || (outprec == TYPE_PRECISION (integer_type_node)
446 && !TYPE_UNSIGNED (type)))
447 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
448 else if (outprec == TYPE_PRECISION (long_integer_type_node)
449 && !TYPE_UNSIGNED (type))
450 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
451 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
452 && !TYPE_UNSIGNED (type))
453 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
454 break;
456 CASE_FLT_FN (BUILT_IN_FLOOR):
457 /* Only convert in ISO C99 mode. */
458 if (!targetm.libc_has_function (function_c99_misc))
459 break;
460 if (outprec < TYPE_PRECISION (integer_type_node)
461 || (outprec == TYPE_PRECISION (integer_type_node)
462 && !TYPE_UNSIGNED (type)))
463 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
464 else if (outprec == TYPE_PRECISION (long_integer_type_node)
465 && !TYPE_UNSIGNED (type))
466 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
467 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
468 && !TYPE_UNSIGNED (type))
469 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
470 break;
472 CASE_FLT_FN (BUILT_IN_ROUND):
473 /* Only convert in ISO C99 mode. */
474 if (!targetm.libc_has_function (function_c99_misc))
475 break;
476 if (outprec < TYPE_PRECISION (integer_type_node)
477 || (outprec == TYPE_PRECISION (integer_type_node)
478 && !TYPE_UNSIGNED (type)))
479 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
480 else if (outprec == TYPE_PRECISION (long_integer_type_node)
481 && !TYPE_UNSIGNED (type))
482 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
483 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
484 && !TYPE_UNSIGNED (type))
485 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
486 break;
488 CASE_FLT_FN (BUILT_IN_NEARBYINT):
489 /* Only convert nearbyint* if we can ignore math exceptions. */
490 if (flag_trapping_math)
491 break;
492 /* ... Fall through ... */
493 CASE_FLT_FN (BUILT_IN_RINT):
494 /* Only convert in ISO C99 mode. */
495 if (!targetm.libc_has_function (function_c99_misc))
496 break;
497 if (outprec < TYPE_PRECISION (integer_type_node)
498 || (outprec == TYPE_PRECISION (integer_type_node)
499 && !TYPE_UNSIGNED (type)))
500 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
501 else if (outprec == TYPE_PRECISION (long_integer_type_node)
502 && !TYPE_UNSIGNED (type))
503 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
504 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
505 && !TYPE_UNSIGNED (type))
506 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
507 break;
509 CASE_FLT_FN (BUILT_IN_TRUNC):
510 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
512 default:
513 break;
516 if (fn)
518 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
519 return convert_to_integer (type, newexpr);
523 /* Convert (int)logb(d) -> ilogb(d). */
524 if (optimize
525 && flag_unsafe_math_optimizations
526 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
527 && integer_type_node
528 && (outprec > TYPE_PRECISION (integer_type_node)
529 || (outprec == TYPE_PRECISION (integer_type_node)
530 && !TYPE_UNSIGNED (type))))
532 tree s_expr = strip_float_extensions (expr);
533 tree s_intype = TREE_TYPE (s_expr);
534 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
535 tree fn = 0;
537 switch (fcode)
539 CASE_FLT_FN (BUILT_IN_LOGB):
540 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
541 break;
543 default:
544 break;
547 if (fn)
549 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
550 return convert_to_integer (type, newexpr);
554 switch (TREE_CODE (intype))
556 case POINTER_TYPE:
557 case REFERENCE_TYPE:
558 if (upc_shared_type_p (TREE_TYPE (intype)))
560 error ("invalid conversion from a UPC pointer-to-shared to an integer");
561 expr = integer_zero_node;
563 if (integer_zerop (expr))
564 return build_int_cst (type, 0);
566 /* Convert to an unsigned integer of the correct width first, and from
567 there widen/truncate to the required type. Some targets support the
568 coexistence of multiple valid pointer sizes, so fetch the one we need
569 from the type. */
570 expr = fold_build1 (CONVERT_EXPR,
571 lang_hooks.types.type_for_size
572 (TYPE_PRECISION (intype), 0),
573 expr);
574 return fold_convert (type, expr);
576 case INTEGER_TYPE:
577 case ENUMERAL_TYPE:
578 case BOOLEAN_TYPE:
579 case OFFSET_TYPE:
580 /* If this is a logical operation, which just returns 0 or 1, we can
581 change the type of the expression. */
583 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
585 expr = copy_node (expr);
586 TREE_TYPE (expr) = type;
587 return expr;
590 /* If we are widening the type, put in an explicit conversion.
591 Similarly if we are not changing the width. After this, we know
592 we are truncating EXPR. */
594 else if (outprec >= inprec)
596 enum tree_code code;
598 /* If the precision of the EXPR's type is K bits and the
599 destination mode has more bits, and the sign is changing,
600 it is not safe to use a NOP_EXPR. For example, suppose
601 that EXPR's type is a 3-bit unsigned integer type, the
602 TYPE is a 3-bit signed integer type, and the machine mode
603 for the types is 8-bit QImode. In that case, the
604 conversion necessitates an explicit sign-extension. In
605 the signed-to-unsigned case the high-order bits have to
606 be cleared. */
607 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
608 && (TYPE_PRECISION (TREE_TYPE (expr))
609 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
610 code = CONVERT_EXPR;
611 else
612 code = NOP_EXPR;
614 return fold_build1 (code, type, expr);
617 /* If TYPE is an enumeral type or a type with a precision less
618 than the number of bits in its mode, do the conversion to the
619 type corresponding to its mode, then do a nop conversion
620 to TYPE. */
621 else if (TREE_CODE (type) == ENUMERAL_TYPE
622 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
623 return build1 (NOP_EXPR, type,
624 convert (lang_hooks.types.type_for_mode
625 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
626 expr));
628 /* Here detect when we can distribute the truncation down past some
629 arithmetic. For example, if adding two longs and converting to an
630 int, we can equally well convert both to ints and then add.
631 For the operations handled here, such truncation distribution
632 is always safe.
633 It is desirable in these cases:
634 1) when truncating down to full-word from a larger size
635 2) when truncating takes no work.
636 3) when at least one operand of the arithmetic has been extended
637 (as by C's default conversions). In this case we need two conversions
638 if we do the arithmetic as already requested, so we might as well
639 truncate both and then combine. Perhaps that way we need only one.
641 Note that in general we cannot do the arithmetic in a type
642 shorter than the desired result of conversion, even if the operands
643 are both extended from a shorter type, because they might overflow
644 if combined in that type. The exceptions to this--the times when
645 two narrow values can be combined in their narrow type even to
646 make a wider result--are handled by "shorten" in build_binary_op. */
648 switch (ex_form)
650 case RSHIFT_EXPR:
651 /* We can pass truncation down through right shifting
652 when the shift count is a nonpositive constant. */
653 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
654 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
655 goto trunc1;
656 break;
658 case LSHIFT_EXPR:
659 /* We can pass truncation down through left shifting
660 when the shift count is a nonnegative constant and
661 the target type is unsigned. */
662 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
663 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
664 && TYPE_UNSIGNED (type)
665 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
667 /* If shift count is less than the width of the truncated type,
668 really shift. */
669 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
670 /* In this case, shifting is like multiplication. */
671 goto trunc1;
672 else
674 /* If it is >= that width, result is zero.
675 Handling this with trunc1 would give the wrong result:
676 (int) ((long long) a << 32) is well defined (as 0)
677 but (int) a << 32 is undefined and would get a
678 warning. */
680 tree t = build_int_cst (type, 0);
682 /* If the original expression had side-effects, we must
683 preserve it. */
684 if (TREE_SIDE_EFFECTS (expr))
685 return build2 (COMPOUND_EXPR, type, expr, t);
686 else
687 return t;
690 break;
692 case TRUNC_DIV_EXPR:
694 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
695 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
697 /* Don't distribute unless the output precision is at least as big
698 as the actual inputs and it has the same signedness. */
699 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
700 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
701 /* If signedness of arg0 and arg1 don't match,
702 we can't necessarily find a type to compare them in. */
703 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
704 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
705 /* Do not change the sign of the division. */
706 && (TYPE_UNSIGNED (TREE_TYPE (expr))
707 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
708 /* Either require unsigned division or a division by
709 a constant that is not -1. */
710 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
711 || (TREE_CODE (arg1) == INTEGER_CST
712 && !integer_all_onesp (arg1))))
713 goto trunc1;
714 break;
717 case MAX_EXPR:
718 case MIN_EXPR:
719 case MULT_EXPR:
721 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
722 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
724 /* Don't distribute unless the output precision is at least as big
725 as the actual inputs. Otherwise, the comparison of the
726 truncated values will be wrong. */
727 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
728 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
729 /* If signedness of arg0 and arg1 don't match,
730 we can't necessarily find a type to compare them in. */
731 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
732 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
733 goto trunc1;
734 break;
737 case PLUS_EXPR:
738 case MINUS_EXPR:
739 case BIT_AND_EXPR:
740 case BIT_IOR_EXPR:
741 case BIT_XOR_EXPR:
742 trunc1:
744 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
745 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
747 /* Do not try to narrow operands of pointer subtraction;
748 that will interfere with other folding. */
749 if (ex_form == MINUS_EXPR
750 && CONVERT_EXPR_P (arg0)
751 && CONVERT_EXPR_P (arg1)
752 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
753 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
754 break;
756 if (outprec >= BITS_PER_WORD
757 || TRULY_NOOP_TRUNCATION (outprec, inprec)
758 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
759 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
761 /* Do the arithmetic in type TYPEX,
762 then convert result to TYPE. */
763 tree typex = type;
765 /* Can't do arithmetic in enumeral types
766 so use an integer type that will hold the values. */
767 if (TREE_CODE (typex) == ENUMERAL_TYPE)
768 typex = lang_hooks.types.type_for_size
769 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
771 /* But now perhaps TYPEX is as wide as INPREC.
772 In that case, do nothing special here.
773 (Otherwise would recurse infinitely in convert. */
774 if (TYPE_PRECISION (typex) != inprec)
776 /* Don't do unsigned arithmetic where signed was wanted,
777 or vice versa.
778 Exception: if both of the original operands were
779 unsigned then we can safely do the work as unsigned.
780 Exception: shift operations take their type solely
781 from the first argument.
782 Exception: the LSHIFT_EXPR case above requires that
783 we perform this operation unsigned lest we produce
784 signed-overflow undefinedness.
785 And we may need to do it as unsigned
786 if we truncate to the original size. */
787 if (TYPE_UNSIGNED (TREE_TYPE (expr))
788 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
789 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
790 || ex_form == LSHIFT_EXPR
791 || ex_form == RSHIFT_EXPR
792 || ex_form == LROTATE_EXPR
793 || ex_form == RROTATE_EXPR))
794 || ex_form == LSHIFT_EXPR
795 /* If we have !flag_wrapv, and either ARG0 or
796 ARG1 is of a signed type, we have to do
797 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
798 type in case the operation in outprec precision
799 could overflow. Otherwise, we would introduce
800 signed-overflow undefinedness. */
801 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
802 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
803 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
804 > outprec)
805 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
806 > outprec))
807 && (ex_form == PLUS_EXPR
808 || ex_form == MINUS_EXPR
809 || ex_form == MULT_EXPR)))
810 typex = unsigned_type_for (typex);
811 else
812 typex = signed_type_for (typex);
813 return convert (type,
814 fold_build2 (ex_form, typex,
815 convert (typex, arg0),
816 convert (typex, arg1)));
820 break;
822 case NEGATE_EXPR:
823 case BIT_NOT_EXPR:
824 /* This is not correct for ABS_EXPR,
825 since we must test the sign before truncation. */
827 tree typex = unsigned_type_for (type);
828 return convert (type,
829 fold_build1 (ex_form, typex,
830 convert (typex,
831 TREE_OPERAND (expr, 0))));
834 case NOP_EXPR:
835 /* Don't introduce a
836 "can't convert between vector values of different size" error. */
837 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
838 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
839 != GET_MODE_SIZE (TYPE_MODE (type))))
840 break;
841 /* If truncating after truncating, might as well do all at once.
842 If truncating after extending, we may get rid of wasted work. */
843 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
845 case COND_EXPR:
846 /* It is sometimes worthwhile to push the narrowing down through
847 the conditional and never loses. A COND_EXPR may have a throw
848 as one operand, which then has void type. Just leave void
849 operands as they are. */
850 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
851 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
852 ? TREE_OPERAND (expr, 1)
853 : convert (type, TREE_OPERAND (expr, 1)),
854 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
855 ? TREE_OPERAND (expr, 2)
856 : convert (type, TREE_OPERAND (expr, 2)));
858 default:
859 break;
862 /* When parsing long initializers, we might end up with a lot of casts.
863 Shortcut this. */
864 if (TREE_CODE (expr) == INTEGER_CST)
865 return fold_convert (type, expr);
866 return build1 (CONVERT_EXPR, type, expr);
868 case REAL_TYPE:
869 return build1 (FIX_TRUNC_EXPR, type, expr);
871 case FIXED_POINT_TYPE:
872 return build1 (FIXED_CONVERT_EXPR, type, expr);
874 case COMPLEX_TYPE:
875 return convert (type,
876 fold_build1 (REALPART_EXPR,
877 TREE_TYPE (TREE_TYPE (expr)), expr));
879 case VECTOR_TYPE:
880 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
882 error ("can%'t convert between vector values of different size");
883 return error_mark_node;
885 return build1 (VIEW_CONVERT_EXPR, type, expr);
887 default:
888 error ("aggregate value used where an integer was expected");
889 return convert (type, integer_zero_node);
893 /* Convert EXPR to the complex type TYPE in the usual ways. */
895 tree
896 convert_to_complex (tree type, tree expr)
898 tree subtype = TREE_TYPE (type);
900 switch (TREE_CODE (TREE_TYPE (expr)))
902 case REAL_TYPE:
903 case FIXED_POINT_TYPE:
904 case INTEGER_TYPE:
905 case ENUMERAL_TYPE:
906 case BOOLEAN_TYPE:
907 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
908 convert (subtype, integer_zero_node));
910 case COMPLEX_TYPE:
912 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
914 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
915 return expr;
916 else if (TREE_CODE (expr) == COMPLEX_EXPR)
917 return fold_build2 (COMPLEX_EXPR, type,
918 convert (subtype, TREE_OPERAND (expr, 0)),
919 convert (subtype, TREE_OPERAND (expr, 1)));
920 else
922 expr = save_expr (expr);
923 return
924 fold_build2 (COMPLEX_EXPR, type,
925 convert (subtype,
926 fold_build1 (REALPART_EXPR,
927 TREE_TYPE (TREE_TYPE (expr)),
928 expr)),
929 convert (subtype,
930 fold_build1 (IMAGPART_EXPR,
931 TREE_TYPE (TREE_TYPE (expr)),
932 expr)));
936 case POINTER_TYPE:
937 case REFERENCE_TYPE:
938 error ("pointer value used where a complex was expected");
939 return convert_to_complex (type, integer_zero_node);
941 default:
942 error ("aggregate value used where a complex was expected");
943 return convert_to_complex (type, integer_zero_node);
947 /* Convert EXPR to the vector type TYPE in the usual ways. */
949 tree
950 convert_to_vector (tree type, tree expr)
952 switch (TREE_CODE (TREE_TYPE (expr)))
954 case INTEGER_TYPE:
955 case VECTOR_TYPE:
956 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
958 error ("can%'t convert between vector values of different size");
959 return error_mark_node;
961 return build1 (VIEW_CONVERT_EXPR, type, expr);
963 default:
964 error ("can%'t convert value to a vector");
965 return error_mark_node;
969 /* Convert EXPR to some fixed-point type TYPE.
971 EXPR must be fixed-point, float, integer, or enumeral;
972 in other cases error is called. */
974 tree
975 convert_to_fixed (tree type, tree expr)
977 if (integer_zerop (expr))
979 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
980 return fixed_zero_node;
982 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
984 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
985 return fixed_one_node;
988 switch (TREE_CODE (TREE_TYPE (expr)))
990 case FIXED_POINT_TYPE:
991 case INTEGER_TYPE:
992 case ENUMERAL_TYPE:
993 case BOOLEAN_TYPE:
994 case REAL_TYPE:
995 return build1 (FIXED_CONVERT_EXPR, type, expr);
997 case COMPLEX_TYPE:
998 return convert (type,
999 fold_build1 (REALPART_EXPR,
1000 TREE_TYPE (TREE_TYPE (expr)), expr));
1002 default:
1003 error ("aggregate value used where a fixed-point was expected");
1004 return error_mark_node;