Daily bump.
[official-gcc.git] / gcc / convert.c
blobcb57b29e07e595aa4e1298ae2acd504490e3fe4b
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2014 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 "stor-layout.h"
30 #include "flags.h"
31 #include "convert.h"
32 #include "diagnostic-core.h"
33 #include "target.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. */
40 tree
41 convert_to_pointer (tree type, tree expr)
43 location_t loc = EXPR_LOCATION (expr);
44 if (TREE_TYPE (expr) == type)
45 return expr;
47 switch (TREE_CODE (TREE_TYPE (expr)))
49 case POINTER_TYPE:
50 case REFERENCE_TYPE:
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)));
57 if (to_as == from_as)
58 return fold_build1_loc (loc, NOP_EXPR, type, expr);
59 else
60 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
63 case INTEGER_TYPE:
64 case ENUMERAL_TYPE:
65 case BOOLEAN_TYPE:
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));
74 if (eprec != pprec)
75 expr = fold_build1_loc (loc, NOP_EXPR,
76 lang_hooks.types.type_for_size (pprec, 0),
77 expr);
80 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
82 default:
83 error ("cannot convert to a pointer type");
84 return convert_to_pointer (type, integer_zero_node);
89 /* Convert EXPR to some floating-point type TYPE.
91 EXPR must be float, fixed-point, integer, or enumeral;
92 in other cases error is called. */
94 tree
95 convert_to_real (tree type, tree expr)
97 enum built_in_function fcode = builtin_mathfn_code (expr);
98 tree itype = TREE_TYPE (expr);
100 if (TREE_CODE (expr) == COMPOUND_EXPR)
102 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
103 if (t == TREE_OPERAND (expr, 1))
104 return expr;
105 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
106 TREE_OPERAND (expr, 0), t);
109 /* Disable until we figure out how to decide whether the functions are
110 present in runtime. */
111 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
112 if (optimize
113 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
114 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
116 switch (fcode)
118 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
119 CASE_MATHFN (COSH)
120 CASE_MATHFN (EXP)
121 CASE_MATHFN (EXP10)
122 CASE_MATHFN (EXP2)
123 CASE_MATHFN (EXPM1)
124 CASE_MATHFN (GAMMA)
125 CASE_MATHFN (J0)
126 CASE_MATHFN (J1)
127 CASE_MATHFN (LGAMMA)
128 CASE_MATHFN (POW10)
129 CASE_MATHFN (SINH)
130 CASE_MATHFN (TGAMMA)
131 CASE_MATHFN (Y0)
132 CASE_MATHFN (Y1)
133 /* The above functions may set errno differently with float
134 input or output so this transformation is not safe with
135 -fmath-errno. */
136 if (flag_errno_math)
137 break;
138 CASE_MATHFN (ACOS)
139 CASE_MATHFN (ACOSH)
140 CASE_MATHFN (ASIN)
141 CASE_MATHFN (ASINH)
142 CASE_MATHFN (ATAN)
143 CASE_MATHFN (ATANH)
144 CASE_MATHFN (CBRT)
145 CASE_MATHFN (COS)
146 CASE_MATHFN (ERF)
147 CASE_MATHFN (ERFC)
148 CASE_MATHFN (LOG)
149 CASE_MATHFN (LOG10)
150 CASE_MATHFN (LOG2)
151 CASE_MATHFN (LOG1P)
152 CASE_MATHFN (SIN)
153 CASE_MATHFN (TAN)
154 CASE_MATHFN (TANH)
155 /* The above functions are not safe to do this conversion. */
156 if (!flag_unsafe_math_optimizations)
157 break;
158 CASE_MATHFN (SQRT)
159 CASE_MATHFN (FABS)
160 CASE_MATHFN (LOGB)
161 #undef CASE_MATHFN
163 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
164 tree newtype = type;
166 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
167 the both as the safe type for operation. */
168 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
169 newtype = TREE_TYPE (arg0);
171 /* We consider to convert
173 (T1) sqrtT2 ((T2) exprT3)
175 (T1) sqrtT4 ((T4) exprT3)
177 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
178 and T4 is NEWTYPE. All those types are of floating point types.
179 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
180 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
181 T2 and T4. See the following URL for a reference:
182 http://stackoverflow.com/questions/9235456/determining-
183 floating-point-square-root
185 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
186 && !flag_unsafe_math_optimizations)
188 /* The following conversion is unsafe even the precision condition
189 below is satisfied:
191 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
193 if (TYPE_MODE (type) != TYPE_MODE (newtype))
194 break;
196 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
197 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
198 if (p1 < p2 * 2 + 2)
199 break;
202 /* Be careful about integer to fp conversions.
203 These may overflow still. */
204 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
205 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
206 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
207 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
209 tree fn = mathfn_built_in (newtype, fcode);
211 if (fn)
213 tree arg = fold (convert_to_real (newtype, arg0));
214 expr = build_call_expr (fn, 1, arg);
215 if (newtype == type)
216 return expr;
220 default:
221 break;
224 if (optimize
225 && (((fcode == BUILT_IN_FLOORL
226 || fcode == BUILT_IN_CEILL
227 || fcode == BUILT_IN_ROUNDL
228 || fcode == BUILT_IN_RINTL
229 || fcode == BUILT_IN_TRUNCL
230 || fcode == BUILT_IN_NEARBYINTL)
231 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
232 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
233 || ((fcode == BUILT_IN_FLOOR
234 || fcode == BUILT_IN_CEIL
235 || fcode == BUILT_IN_ROUND
236 || fcode == BUILT_IN_RINT
237 || fcode == BUILT_IN_TRUNC
238 || fcode == BUILT_IN_NEARBYINT)
239 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
241 tree fn = mathfn_built_in (type, fcode);
243 if (fn)
245 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
247 /* Make sure (type)arg0 is an extension, otherwise we could end up
248 changing (float)floor(double d) into floorf((float)d), which is
249 incorrect because (float)d uses round-to-nearest and can round
250 up to the next integer. */
251 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
252 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
256 /* Propagate the cast into the operation. */
257 if (itype != type && FLOAT_TYPE_P (type))
258 switch (TREE_CODE (expr))
260 /* Convert (float)-x into -(float)x. This is safe for
261 round-to-nearest rounding mode when the inner type is float. */
262 case ABS_EXPR:
263 case NEGATE_EXPR:
264 if (!flag_rounding_math
265 && FLOAT_TYPE_P (itype)
266 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
267 return build1 (TREE_CODE (expr), type,
268 fold (convert_to_real (type,
269 TREE_OPERAND (expr, 0))));
270 break;
271 /* Convert (outertype)((innertype0)a+(innertype1)b)
272 into ((newtype)a+(newtype)b) where newtype
273 is the widest mode from all of these. */
274 case PLUS_EXPR:
275 case MINUS_EXPR:
276 case MULT_EXPR:
277 case RDIV_EXPR:
279 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
280 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
282 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
283 && FLOAT_TYPE_P (TREE_TYPE (arg1))
284 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
286 tree newtype = type;
288 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
289 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
290 || TYPE_MODE (type) == SDmode)
291 newtype = dfloat32_type_node;
292 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
293 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
294 || TYPE_MODE (type) == DDmode)
295 newtype = dfloat64_type_node;
296 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
297 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
298 || TYPE_MODE (type) == TDmode)
299 newtype = dfloat128_type_node;
300 if (newtype == dfloat32_type_node
301 || newtype == dfloat64_type_node
302 || newtype == dfloat128_type_node)
304 expr = build2 (TREE_CODE (expr), newtype,
305 fold (convert_to_real (newtype, arg0)),
306 fold (convert_to_real (newtype, arg1)));
307 if (newtype == type)
308 return expr;
309 break;
312 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
313 newtype = TREE_TYPE (arg0);
314 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
315 newtype = TREE_TYPE (arg1);
316 /* Sometimes this transformation is safe (cannot
317 change results through affecting double rounding
318 cases) and sometimes it is not. If NEWTYPE is
319 wider than TYPE, e.g. (float)((long double)double
320 + (long double)double) converted to
321 (float)(double + double), the transformation is
322 unsafe regardless of the details of the types
323 involved; double rounding can arise if the result
324 of NEWTYPE arithmetic is a NEWTYPE value half way
325 between two representable TYPE values but the
326 exact value is sufficiently different (in the
327 right direction) for this difference to be
328 visible in ITYPE arithmetic. If NEWTYPE is the
329 same as TYPE, however, the transformation may be
330 safe depending on the types involved: it is safe
331 if the ITYPE has strictly more than twice as many
332 mantissa bits as TYPE, can represent infinities
333 and NaNs if the TYPE can, and has sufficient
334 exponent range for the product or ratio of two
335 values representable in the TYPE to be within the
336 range of normal values of ITYPE. */
337 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
338 && (flag_unsafe_math_optimizations
339 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
340 && real_can_shorten_arithmetic (TYPE_MODE (itype),
341 TYPE_MODE (type))
342 && !excess_precision_type (newtype))))
344 expr = build2 (TREE_CODE (expr), newtype,
345 fold (convert_to_real (newtype, arg0)),
346 fold (convert_to_real (newtype, arg1)));
347 if (newtype == type)
348 return expr;
352 break;
353 default:
354 break;
357 switch (TREE_CODE (TREE_TYPE (expr)))
359 case REAL_TYPE:
360 /* Ignore the conversion if we don't need to store intermediate
361 results and neither type is a decimal float. */
362 return build1 ((flag_float_store
363 || DECIMAL_FLOAT_TYPE_P (type)
364 || DECIMAL_FLOAT_TYPE_P (itype))
365 ? CONVERT_EXPR : NOP_EXPR, type, expr);
367 case INTEGER_TYPE:
368 case ENUMERAL_TYPE:
369 case BOOLEAN_TYPE:
370 return build1 (FLOAT_EXPR, type, expr);
372 case FIXED_POINT_TYPE:
373 return build1 (FIXED_CONVERT_EXPR, type, expr);
375 case COMPLEX_TYPE:
376 return convert (type,
377 fold_build1 (REALPART_EXPR,
378 TREE_TYPE (TREE_TYPE (expr)), expr));
380 case POINTER_TYPE:
381 case REFERENCE_TYPE:
382 error ("pointer value used where a floating point value was expected");
383 return convert_to_real (type, integer_zero_node);
385 default:
386 error ("aggregate value used where a float was expected");
387 return convert_to_real (type, integer_zero_node);
391 /* Convert EXPR to some integer (or enum) type TYPE.
393 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
394 fixed-point or vector; in other cases error is called.
396 The result of this is always supposed to be a newly created tree node
397 not in use in any existing structure. */
399 tree
400 convert_to_integer (tree type, tree expr)
402 enum tree_code ex_form = TREE_CODE (expr);
403 tree intype = TREE_TYPE (expr);
404 unsigned int inprec = element_precision (intype);
405 unsigned int outprec = element_precision (type);
407 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
408 be. Consider `enum E = { a, b = (enum E) 3 };'. */
409 if (!COMPLETE_TYPE_P (type))
411 error ("conversion to incomplete type");
412 return error_mark_node;
415 if (ex_form == COMPOUND_EXPR)
417 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
418 if (t == TREE_OPERAND (expr, 1))
419 return expr;
420 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
421 TREE_OPERAND (expr, 0), t);
424 /* Convert e.g. (long)round(d) -> lround(d). */
425 /* If we're converting to char, we may encounter differing behavior
426 between converting from double->char vs double->long->char.
427 We're in "undefined" territory but we prefer to be conservative,
428 so only proceed in "unsafe" math mode. */
429 if (optimize
430 && (flag_unsafe_math_optimizations
431 || (long_integer_type_node
432 && outprec >= TYPE_PRECISION (long_integer_type_node))))
434 tree s_expr = strip_float_extensions (expr);
435 tree s_intype = TREE_TYPE (s_expr);
436 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
437 tree fn = 0;
439 switch (fcode)
441 CASE_FLT_FN (BUILT_IN_CEIL):
442 /* Only convert in ISO C99 mode. */
443 if (!targetm.libc_has_function (function_c99_misc))
444 break;
445 if (outprec < TYPE_PRECISION (integer_type_node)
446 || (outprec == TYPE_PRECISION (integer_type_node)
447 && !TYPE_UNSIGNED (type)))
448 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
449 else if (outprec == TYPE_PRECISION (long_integer_type_node)
450 && !TYPE_UNSIGNED (type))
451 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
452 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
453 && !TYPE_UNSIGNED (type))
454 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
455 break;
457 CASE_FLT_FN (BUILT_IN_FLOOR):
458 /* Only convert in ISO C99 mode. */
459 if (!targetm.libc_has_function (function_c99_misc))
460 break;
461 if (outprec < TYPE_PRECISION (integer_type_node)
462 || (outprec == TYPE_PRECISION (integer_type_node)
463 && !TYPE_UNSIGNED (type)))
464 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
465 else if (outprec == TYPE_PRECISION (long_integer_type_node)
466 && !TYPE_UNSIGNED (type))
467 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
468 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
469 && !TYPE_UNSIGNED (type))
470 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
471 break;
473 CASE_FLT_FN (BUILT_IN_ROUND):
474 /* Only convert in ISO C99 mode. */
475 if (!targetm.libc_has_function (function_c99_misc))
476 break;
477 if (outprec < TYPE_PRECISION (integer_type_node)
478 || (outprec == TYPE_PRECISION (integer_type_node)
479 && !TYPE_UNSIGNED (type)))
480 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
481 else if (outprec == TYPE_PRECISION (long_integer_type_node)
482 && !TYPE_UNSIGNED (type))
483 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
484 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
485 && !TYPE_UNSIGNED (type))
486 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
487 break;
489 CASE_FLT_FN (BUILT_IN_NEARBYINT):
490 /* Only convert nearbyint* if we can ignore math exceptions. */
491 if (flag_trapping_math)
492 break;
493 /* ... Fall through ... */
494 CASE_FLT_FN (BUILT_IN_RINT):
495 /* Only convert in ISO C99 mode. */
496 if (!targetm.libc_has_function (function_c99_misc))
497 break;
498 if (outprec < TYPE_PRECISION (integer_type_node)
499 || (outprec == TYPE_PRECISION (integer_type_node)
500 && !TYPE_UNSIGNED (type)))
501 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
502 else if (outprec == TYPE_PRECISION (long_integer_type_node)
503 && !TYPE_UNSIGNED (type))
504 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
505 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
506 && !TYPE_UNSIGNED (type))
507 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
508 break;
510 CASE_FLT_FN (BUILT_IN_TRUNC):
511 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
513 default:
514 break;
517 if (fn)
519 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
520 return convert_to_integer (type, newexpr);
524 /* Convert (int)logb(d) -> ilogb(d). */
525 if (optimize
526 && flag_unsafe_math_optimizations
527 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
528 && integer_type_node
529 && (outprec > TYPE_PRECISION (integer_type_node)
530 || (outprec == TYPE_PRECISION (integer_type_node)
531 && !TYPE_UNSIGNED (type))))
533 tree s_expr = strip_float_extensions (expr);
534 tree s_intype = TREE_TYPE (s_expr);
535 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
536 tree fn = 0;
538 switch (fcode)
540 CASE_FLT_FN (BUILT_IN_LOGB):
541 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
542 break;
544 default:
545 break;
548 if (fn)
550 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
551 return convert_to_integer (type, newexpr);
555 switch (TREE_CODE (intype))
557 case POINTER_TYPE:
558 case REFERENCE_TYPE:
559 if (integer_zerop (expr))
560 return build_int_cst (type, 0);
562 /* Convert to an unsigned integer of the correct width first, and from
563 there widen/truncate to the required type. Some targets support the
564 coexistence of multiple valid pointer sizes, so fetch the one we need
565 from the type. */
566 expr = fold_build1 (CONVERT_EXPR,
567 lang_hooks.types.type_for_size
568 (TYPE_PRECISION (intype), 0),
569 expr);
570 return fold_convert (type, expr);
572 case INTEGER_TYPE:
573 case ENUMERAL_TYPE:
574 case BOOLEAN_TYPE:
575 case OFFSET_TYPE:
576 /* If this is a logical operation, which just returns 0 or 1, we can
577 change the type of the expression. */
579 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
581 expr = copy_node (expr);
582 TREE_TYPE (expr) = type;
583 return expr;
586 /* If we are widening the type, put in an explicit conversion.
587 Similarly if we are not changing the width. After this, we know
588 we are truncating EXPR. */
590 else if (outprec >= inprec)
592 enum tree_code code;
594 /* If the precision of the EXPR's type is K bits and the
595 destination mode has more bits, and the sign is changing,
596 it is not safe to use a NOP_EXPR. For example, suppose
597 that EXPR's type is a 3-bit unsigned integer type, the
598 TYPE is a 3-bit signed integer type, and the machine mode
599 for the types is 8-bit QImode. In that case, the
600 conversion necessitates an explicit sign-extension. In
601 the signed-to-unsigned case the high-order bits have to
602 be cleared. */
603 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
604 && (TYPE_PRECISION (TREE_TYPE (expr))
605 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
606 code = CONVERT_EXPR;
607 else
608 code = NOP_EXPR;
610 return fold_build1 (code, type, expr);
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
616 to TYPE. */
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)),
622 expr));
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
628 is always safe.
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. */
644 switch (ex_form)
646 case RSHIFT_EXPR:
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)
651 goto trunc1;
652 break;
654 case LSHIFT_EXPR:
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,
664 really shift. */
665 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
666 /* In this case, shifting is like multiplication. */
667 goto trunc1;
668 else
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
674 warning. */
676 tree t = build_int_cst (type, 0);
678 /* If the original expression had side-effects, we must
679 preserve it. */
680 if (TREE_SIDE_EFFECTS (expr))
681 return build2 (COMPOUND_EXPR, type, expr, t);
682 else
683 return t;
686 break;
688 case TRUNC_DIV_EXPR:
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))))
709 goto trunc1;
710 break;
713 case MAX_EXPR:
714 case MIN_EXPR:
715 case MULT_EXPR:
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))))
729 goto trunc1;
730 break;
733 case PLUS_EXPR:
734 case MINUS_EXPR:
735 case BIT_AND_EXPR:
736 case BIT_IOR_EXPR:
737 case BIT_XOR_EXPR:
738 trunc1:
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))))
750 break;
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. */
759 tree typex = 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)
772 /* Don't do unsigned arithmetic where signed was wanted,
773 or vice versa.
774 Exception: if both of the original operands were
775 unsigned then we can safely do the work as unsigned.
776 Exception: shift operations take their type solely
777 from the first argument.
778 Exception: the LSHIFT_EXPR case above requires that
779 we perform this operation unsigned lest we produce
780 signed-overflow undefinedness.
781 And we may need to do it as unsigned
782 if we truncate to the original size. */
783 if (TYPE_UNSIGNED (TREE_TYPE (expr))
784 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
785 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
786 || ex_form == LSHIFT_EXPR
787 || ex_form == RSHIFT_EXPR
788 || ex_form == LROTATE_EXPR
789 || ex_form == RROTATE_EXPR))
790 || ex_form == LSHIFT_EXPR
791 /* If we have !flag_wrapv, and either ARG0 or
792 ARG1 is of a signed type, we have to do
793 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
794 type in case the operation in outprec precision
795 could overflow. Otherwise, we would introduce
796 signed-overflow undefinedness. */
797 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
798 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
799 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
800 > outprec)
801 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
802 > outprec))
803 && (ex_form == PLUS_EXPR
804 || ex_form == MINUS_EXPR
805 || ex_form == MULT_EXPR)))
806 typex = unsigned_type_for (typex);
807 else
808 typex = signed_type_for (typex);
809 return convert (type,
810 fold_build2 (ex_form, typex,
811 convert (typex, arg0),
812 convert (typex, arg1)));
816 break;
818 case NEGATE_EXPR:
819 case BIT_NOT_EXPR:
820 /* This is not correct for ABS_EXPR,
821 since we must test the sign before truncation. */
823 tree typex = unsigned_type_for (type);
824 return convert (type,
825 fold_build1 (ex_form, typex,
826 convert (typex,
827 TREE_OPERAND (expr, 0))));
830 case NOP_EXPR:
831 /* Don't introduce a
832 "can't convert between vector values of different size" error. */
833 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
834 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
835 != GET_MODE_SIZE (TYPE_MODE (type))))
836 break;
837 /* If truncating after truncating, might as well do all at once.
838 If truncating after extending, we may get rid of wasted work. */
839 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
841 case COND_EXPR:
842 /* It is sometimes worthwhile to push the narrowing down through
843 the conditional and never loses. A COND_EXPR may have a throw
844 as one operand, which then has void type. Just leave void
845 operands as they are. */
846 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
847 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
848 ? TREE_OPERAND (expr, 1)
849 : convert (type, TREE_OPERAND (expr, 1)),
850 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
851 ? TREE_OPERAND (expr, 2)
852 : convert (type, TREE_OPERAND (expr, 2)));
854 default:
855 break;
858 /* When parsing long initializers, we might end up with a lot of casts.
859 Shortcut this. */
860 if (TREE_CODE (expr) == INTEGER_CST)
861 return fold_convert (type, expr);
862 return build1 (CONVERT_EXPR, type, expr);
864 case REAL_TYPE:
865 return build1 (FIX_TRUNC_EXPR, type, expr);
867 case FIXED_POINT_TYPE:
868 return build1 (FIXED_CONVERT_EXPR, type, expr);
870 case COMPLEX_TYPE:
871 return convert (type,
872 fold_build1 (REALPART_EXPR,
873 TREE_TYPE (TREE_TYPE (expr)), expr));
875 case VECTOR_TYPE:
876 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
878 error ("can%'t convert between vector values of different size");
879 return error_mark_node;
881 return build1 (VIEW_CONVERT_EXPR, type, expr);
883 default:
884 error ("aggregate value used where an integer was expected");
885 return convert (type, integer_zero_node);
889 /* Convert EXPR to the complex type TYPE in the usual ways. */
891 tree
892 convert_to_complex (tree type, tree expr)
894 tree subtype = TREE_TYPE (type);
896 switch (TREE_CODE (TREE_TYPE (expr)))
898 case REAL_TYPE:
899 case FIXED_POINT_TYPE:
900 case INTEGER_TYPE:
901 case ENUMERAL_TYPE:
902 case BOOLEAN_TYPE:
903 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
904 convert (subtype, integer_zero_node));
906 case COMPLEX_TYPE:
908 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
910 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
911 return expr;
912 else if (TREE_CODE (expr) == COMPOUND_EXPR)
914 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
915 if (t == TREE_OPERAND (expr, 1))
916 return expr;
917 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
918 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
920 else if (TREE_CODE (expr) == COMPLEX_EXPR)
921 return fold_build2 (COMPLEX_EXPR, type,
922 convert (subtype, TREE_OPERAND (expr, 0)),
923 convert (subtype, TREE_OPERAND (expr, 1)));
924 else
926 expr = save_expr (expr);
927 return
928 fold_build2 (COMPLEX_EXPR, type,
929 convert (subtype,
930 fold_build1 (REALPART_EXPR,
931 TREE_TYPE (TREE_TYPE (expr)),
932 expr)),
933 convert (subtype,
934 fold_build1 (IMAGPART_EXPR,
935 TREE_TYPE (TREE_TYPE (expr)),
936 expr)));
940 case POINTER_TYPE:
941 case REFERENCE_TYPE:
942 error ("pointer value used where a complex was expected");
943 return convert_to_complex (type, integer_zero_node);
945 default:
946 error ("aggregate value used where a complex was expected");
947 return convert_to_complex (type, integer_zero_node);
951 /* Convert EXPR to the vector type TYPE in the usual ways. */
953 tree
954 convert_to_vector (tree type, tree expr)
956 switch (TREE_CODE (TREE_TYPE (expr)))
958 case INTEGER_TYPE:
959 case VECTOR_TYPE:
960 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
962 error ("can%'t convert between vector values of different size");
963 return error_mark_node;
965 return build1 (VIEW_CONVERT_EXPR, type, expr);
967 default:
968 error ("can%'t convert value to a vector");
969 return error_mark_node;
973 /* Convert EXPR to some fixed-point type TYPE.
975 EXPR must be fixed-point, float, integer, or enumeral;
976 in other cases error is called. */
978 tree
979 convert_to_fixed (tree type, tree expr)
981 if (integer_zerop (expr))
983 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
984 return fixed_zero_node;
986 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
988 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
989 return fixed_one_node;
992 switch (TREE_CODE (TREE_TYPE (expr)))
994 case FIXED_POINT_TYPE:
995 case INTEGER_TYPE:
996 case ENUMERAL_TYPE:
997 case BOOLEAN_TYPE:
998 case REAL_TYPE:
999 return build1 (FIXED_CONVERT_EXPR, type, expr);
1001 case COMPLEX_TYPE:
1002 return convert (type,
1003 fold_build1 (REALPART_EXPR,
1004 TREE_TYPE (TREE_TYPE (expr)), expr));
1006 default:
1007 error ("aggregate value used where a fixed-point was expected");
1008 return error_mark_node;