* config/rs6000/rs6000.c (rs6000_xcoff_asm_named_section): Place
[official-gcc.git] / gcc / convert.c
blobbff29784e1809e1272a9ace18dfaa6089db71e71
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2015 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 "alias.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "flags.h"
33 #include "convert.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "langhooks.h"
37 #include "builtins.h"
38 #include "ubsan.h"
40 /* Convert EXPR to some pointer or reference type TYPE.
41 EXPR must be pointer, reference, integer, enumeral, or literal zero;
42 in other cases error is called. */
44 tree
45 convert_to_pointer (tree type, tree expr)
47 location_t loc = EXPR_LOCATION (expr);
48 if (TREE_TYPE (expr) == type)
49 return expr;
51 switch (TREE_CODE (TREE_TYPE (expr)))
53 case POINTER_TYPE:
54 case REFERENCE_TYPE:
56 /* If the pointers point to different address spaces, conversion needs
57 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
58 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
59 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
61 if (to_as == from_as)
62 return fold_build1_loc (loc, NOP_EXPR, type, expr);
63 else
64 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
67 case INTEGER_TYPE:
68 case ENUMERAL_TYPE:
69 case BOOLEAN_TYPE:
71 /* If the input precision differs from the target pointer type
72 precision, first convert the input expression to an integer type of
73 the target precision. Some targets, e.g. VMS, need several pointer
74 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
75 unsigned int pprec = TYPE_PRECISION (type);
76 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
78 if (eprec != pprec)
79 expr = fold_build1_loc (loc, NOP_EXPR,
80 lang_hooks.types.type_for_size (pprec, 0),
81 expr);
84 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
86 default:
87 error ("cannot convert to a pointer type");
88 return convert_to_pointer (type, integer_zero_node);
93 /* Convert EXPR to some floating-point type TYPE.
95 EXPR must be float, fixed-point, integer, or enumeral;
96 in other cases error is called. */
98 tree
99 convert_to_real (tree type, tree expr)
101 enum built_in_function fcode = builtin_mathfn_code (expr);
102 tree itype = TREE_TYPE (expr);
104 if (TREE_CODE (expr) == COMPOUND_EXPR)
106 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
107 if (t == TREE_OPERAND (expr, 1))
108 return expr;
109 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
110 TREE_OPERAND (expr, 0), t);
113 /* Disable until we figure out how to decide whether the functions are
114 present in runtime. */
115 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
116 if (optimize
117 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
118 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
120 switch (fcode)
122 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
123 CASE_MATHFN (COSH)
124 CASE_MATHFN (EXP)
125 CASE_MATHFN (EXP10)
126 CASE_MATHFN (EXP2)
127 CASE_MATHFN (EXPM1)
128 CASE_MATHFN (GAMMA)
129 CASE_MATHFN (J0)
130 CASE_MATHFN (J1)
131 CASE_MATHFN (LGAMMA)
132 CASE_MATHFN (POW10)
133 CASE_MATHFN (SINH)
134 CASE_MATHFN (TGAMMA)
135 CASE_MATHFN (Y0)
136 CASE_MATHFN (Y1)
137 /* The above functions may set errno differently with float
138 input or output so this transformation is not safe with
139 -fmath-errno. */
140 if (flag_errno_math)
141 break;
142 CASE_MATHFN (ACOS)
143 CASE_MATHFN (ACOSH)
144 CASE_MATHFN (ASIN)
145 CASE_MATHFN (ASINH)
146 CASE_MATHFN (ATAN)
147 CASE_MATHFN (ATANH)
148 CASE_MATHFN (CBRT)
149 CASE_MATHFN (COS)
150 CASE_MATHFN (ERF)
151 CASE_MATHFN (ERFC)
152 CASE_MATHFN (LOG)
153 CASE_MATHFN (LOG10)
154 CASE_MATHFN (LOG2)
155 CASE_MATHFN (LOG1P)
156 CASE_MATHFN (SIN)
157 CASE_MATHFN (TAN)
158 CASE_MATHFN (TANH)
159 /* The above functions are not safe to do this conversion. */
160 if (!flag_unsafe_math_optimizations)
161 break;
162 CASE_MATHFN (SQRT)
163 CASE_MATHFN (FABS)
164 CASE_MATHFN (LOGB)
165 #undef CASE_MATHFN
167 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
168 tree newtype = type;
170 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
171 the both as the safe type for operation. */
172 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
173 newtype = TREE_TYPE (arg0);
175 /* We consider to convert
177 (T1) sqrtT2 ((T2) exprT3)
179 (T1) sqrtT4 ((T4) exprT3)
181 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
182 and T4 is NEWTYPE. All those types are of floating point types.
183 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
184 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
185 T2 and T4. See the following URL for a reference:
186 http://stackoverflow.com/questions/9235456/determining-
187 floating-point-square-root
189 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
190 && !flag_unsafe_math_optimizations)
192 /* The following conversion is unsafe even the precision condition
193 below is satisfied:
195 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
197 if (TYPE_MODE (type) != TYPE_MODE (newtype))
198 break;
200 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
201 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
202 if (p1 < p2 * 2 + 2)
203 break;
206 /* Be careful about integer to fp conversions.
207 These may overflow still. */
208 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
209 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
210 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
211 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
213 tree fn = mathfn_built_in (newtype, fcode);
215 if (fn)
217 tree arg = fold (convert_to_real (newtype, arg0));
218 expr = build_call_expr (fn, 1, arg);
219 if (newtype == type)
220 return expr;
224 default:
225 break;
228 if (optimize
229 && (((fcode == BUILT_IN_FLOORL
230 || fcode == BUILT_IN_CEILL
231 || fcode == BUILT_IN_ROUNDL
232 || fcode == BUILT_IN_RINTL
233 || fcode == BUILT_IN_TRUNCL
234 || fcode == BUILT_IN_NEARBYINTL)
235 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
236 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
237 || ((fcode == BUILT_IN_FLOOR
238 || fcode == BUILT_IN_CEIL
239 || fcode == BUILT_IN_ROUND
240 || fcode == BUILT_IN_RINT
241 || fcode == BUILT_IN_TRUNC
242 || fcode == BUILT_IN_NEARBYINT)
243 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
245 tree fn = mathfn_built_in (type, fcode);
247 if (fn)
249 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
251 /* Make sure (type)arg0 is an extension, otherwise we could end up
252 changing (float)floor(double d) into floorf((float)d), which is
253 incorrect because (float)d uses round-to-nearest and can round
254 up to the next integer. */
255 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
256 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
260 /* Propagate the cast into the operation. */
261 if (itype != type && FLOAT_TYPE_P (type))
262 switch (TREE_CODE (expr))
264 /* Convert (float)-x into -(float)x. This is safe for
265 round-to-nearest rounding mode when the inner type is float. */
266 case ABS_EXPR:
267 case NEGATE_EXPR:
268 if (!flag_rounding_math
269 && FLOAT_TYPE_P (itype)
270 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
271 return build1 (TREE_CODE (expr), type,
272 fold (convert_to_real (type,
273 TREE_OPERAND (expr, 0))));
274 break;
275 /* Convert (outertype)((innertype0)a+(innertype1)b)
276 into ((newtype)a+(newtype)b) where newtype
277 is the widest mode from all of these. */
278 case PLUS_EXPR:
279 case MINUS_EXPR:
280 case MULT_EXPR:
281 case RDIV_EXPR:
283 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
284 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
286 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
287 && FLOAT_TYPE_P (TREE_TYPE (arg1))
288 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
290 tree newtype = type;
292 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
293 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
294 || TYPE_MODE (type) == SDmode)
295 newtype = dfloat32_type_node;
296 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
297 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
298 || TYPE_MODE (type) == DDmode)
299 newtype = dfloat64_type_node;
300 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
301 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
302 || TYPE_MODE (type) == TDmode)
303 newtype = dfloat128_type_node;
304 if (newtype == dfloat32_type_node
305 || newtype == dfloat64_type_node
306 || newtype == dfloat128_type_node)
308 expr = build2 (TREE_CODE (expr), newtype,
309 fold (convert_to_real (newtype, arg0)),
310 fold (convert_to_real (newtype, arg1)));
311 if (newtype == type)
312 return expr;
313 break;
316 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
317 newtype = TREE_TYPE (arg0);
318 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
319 newtype = TREE_TYPE (arg1);
320 /* Sometimes this transformation is safe (cannot
321 change results through affecting double rounding
322 cases) and sometimes it is not. If NEWTYPE is
323 wider than TYPE, e.g. (float)((long double)double
324 + (long double)double) converted to
325 (float)(double + double), the transformation is
326 unsafe regardless of the details of the types
327 involved; double rounding can arise if the result
328 of NEWTYPE arithmetic is a NEWTYPE value half way
329 between two representable TYPE values but the
330 exact value is sufficiently different (in the
331 right direction) for this difference to be
332 visible in ITYPE arithmetic. If NEWTYPE is the
333 same as TYPE, however, the transformation may be
334 safe depending on the types involved: it is safe
335 if the ITYPE has strictly more than twice as many
336 mantissa bits as TYPE, can represent infinities
337 and NaNs if the TYPE can, and has sufficient
338 exponent range for the product or ratio of two
339 values representable in the TYPE to be within the
340 range of normal values of ITYPE. */
341 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
342 && (flag_unsafe_math_optimizations
343 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
344 && real_can_shorten_arithmetic (TYPE_MODE (itype),
345 TYPE_MODE (type))
346 && !excess_precision_type (newtype))))
348 expr = build2 (TREE_CODE (expr), newtype,
349 fold (convert_to_real (newtype, arg0)),
350 fold (convert_to_real (newtype, arg1)));
351 if (newtype == type)
352 return expr;
356 break;
357 default:
358 break;
361 switch (TREE_CODE (TREE_TYPE (expr)))
363 case REAL_TYPE:
364 /* Ignore the conversion if we don't need to store intermediate
365 results and neither type is a decimal float. */
366 return build1 ((flag_float_store
367 || DECIMAL_FLOAT_TYPE_P (type)
368 || DECIMAL_FLOAT_TYPE_P (itype))
369 ? CONVERT_EXPR : NOP_EXPR, type, expr);
371 case INTEGER_TYPE:
372 case ENUMERAL_TYPE:
373 case BOOLEAN_TYPE:
374 return build1 (FLOAT_EXPR, type, expr);
376 case FIXED_POINT_TYPE:
377 return build1 (FIXED_CONVERT_EXPR, type, expr);
379 case COMPLEX_TYPE:
380 return convert (type,
381 fold_build1 (REALPART_EXPR,
382 TREE_TYPE (TREE_TYPE (expr)), expr));
384 case POINTER_TYPE:
385 case REFERENCE_TYPE:
386 error ("pointer value used where a floating point value was expected");
387 return convert_to_real (type, integer_zero_node);
389 default:
390 error ("aggregate value used where a float was expected");
391 return convert_to_real (type, integer_zero_node);
395 /* Convert EXPR to some integer (or enum) type TYPE.
397 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
398 fixed-point or vector; in other cases error is called.
400 The result of this is always supposed to be a newly created tree node
401 not in use in any existing structure. */
403 tree
404 convert_to_integer (tree type, tree expr)
406 enum tree_code ex_form = TREE_CODE (expr);
407 tree intype = TREE_TYPE (expr);
408 unsigned int inprec = element_precision (intype);
409 unsigned int outprec = element_precision (type);
410 location_t loc = EXPR_LOCATION (expr);
412 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
413 be. Consider `enum E = { a, b = (enum E) 3 };'. */
414 if (!COMPLETE_TYPE_P (type))
416 error ("conversion to incomplete type");
417 return error_mark_node;
420 if (ex_form == COMPOUND_EXPR)
422 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
423 if (t == TREE_OPERAND (expr, 1))
424 return expr;
425 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
426 TREE_OPERAND (expr, 0), t);
429 /* Convert e.g. (long)round(d) -> lround(d). */
430 /* If we're converting to char, we may encounter differing behavior
431 between converting from double->char vs double->long->char.
432 We're in "undefined" territory but we prefer to be conservative,
433 so only proceed in "unsafe" math mode. */
434 if (optimize
435 && (flag_unsafe_math_optimizations
436 || (long_integer_type_node
437 && outprec >= TYPE_PRECISION (long_integer_type_node))))
439 tree s_expr = strip_float_extensions (expr);
440 tree s_intype = TREE_TYPE (s_expr);
441 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
442 tree fn = 0;
444 switch (fcode)
446 CASE_FLT_FN (BUILT_IN_CEIL):
447 /* Only convert in ISO C99 mode. */
448 if (!targetm.libc_has_function (function_c99_misc))
449 break;
450 if (outprec < TYPE_PRECISION (integer_type_node)
451 || (outprec == TYPE_PRECISION (integer_type_node)
452 && !TYPE_UNSIGNED (type)))
453 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
454 else if (outprec == TYPE_PRECISION (long_integer_type_node)
455 && !TYPE_UNSIGNED (type))
456 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
457 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
458 && !TYPE_UNSIGNED (type))
459 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
460 break;
462 CASE_FLT_FN (BUILT_IN_FLOOR):
463 /* Only convert in ISO C99 mode. */
464 if (!targetm.libc_has_function (function_c99_misc))
465 break;
466 if (outprec < TYPE_PRECISION (integer_type_node)
467 || (outprec == TYPE_PRECISION (integer_type_node)
468 && !TYPE_UNSIGNED (type)))
469 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
470 else if (outprec == TYPE_PRECISION (long_integer_type_node)
471 && !TYPE_UNSIGNED (type))
472 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
473 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
474 && !TYPE_UNSIGNED (type))
475 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
476 break;
478 CASE_FLT_FN (BUILT_IN_ROUND):
479 /* Only convert in ISO C99 mode and with -fno-math-errno. */
480 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
481 break;
482 if (outprec < TYPE_PRECISION (integer_type_node)
483 || (outprec == TYPE_PRECISION (integer_type_node)
484 && !TYPE_UNSIGNED (type)))
485 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
486 else if (outprec == TYPE_PRECISION (long_integer_type_node)
487 && !TYPE_UNSIGNED (type))
488 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
489 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
490 && !TYPE_UNSIGNED (type))
491 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
492 break;
494 CASE_FLT_FN (BUILT_IN_NEARBYINT):
495 /* Only convert nearbyint* if we can ignore math exceptions. */
496 if (flag_trapping_math)
497 break;
498 /* ... Fall through ... */
499 CASE_FLT_FN (BUILT_IN_RINT):
500 /* Only convert in ISO C99 mode and with -fno-math-errno. */
501 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
502 break;
503 if (outprec < TYPE_PRECISION (integer_type_node)
504 || (outprec == TYPE_PRECISION (integer_type_node)
505 && !TYPE_UNSIGNED (type)))
506 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
507 else if (outprec == TYPE_PRECISION (long_integer_type_node)
508 && !TYPE_UNSIGNED (type))
509 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
510 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
511 && !TYPE_UNSIGNED (type))
512 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
513 break;
515 CASE_FLT_FN (BUILT_IN_TRUNC):
516 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
518 default:
519 break;
522 if (fn)
524 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
525 return convert_to_integer (type, newexpr);
529 /* Convert (int)logb(d) -> ilogb(d). */
530 if (optimize
531 && flag_unsafe_math_optimizations
532 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
533 && integer_type_node
534 && (outprec > TYPE_PRECISION (integer_type_node)
535 || (outprec == TYPE_PRECISION (integer_type_node)
536 && !TYPE_UNSIGNED (type))))
538 tree s_expr = strip_float_extensions (expr);
539 tree s_intype = TREE_TYPE (s_expr);
540 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
541 tree fn = 0;
543 switch (fcode)
545 CASE_FLT_FN (BUILT_IN_LOGB):
546 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
547 break;
549 default:
550 break;
553 if (fn)
555 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
556 return convert_to_integer (type, newexpr);
560 switch (TREE_CODE (intype))
562 case POINTER_TYPE:
563 case REFERENCE_TYPE:
564 if (integer_zerop (expr))
565 return build_int_cst (type, 0);
567 /* Convert to an unsigned integer of the correct width first, and from
568 there widen/truncate to the required type. Some targets support the
569 coexistence of multiple valid pointer sizes, so fetch the one we need
570 from the type. */
571 expr = fold_build1 (CONVERT_EXPR,
572 lang_hooks.types.type_for_size
573 (TYPE_PRECISION (intype), 0),
574 expr);
575 return fold_convert (type, expr);
577 case INTEGER_TYPE:
578 case ENUMERAL_TYPE:
579 case BOOLEAN_TYPE:
580 case OFFSET_TYPE:
581 /* If this is a logical operation, which just returns 0 or 1, we can
582 change the type of the expression. */
584 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
586 expr = copy_node (expr);
587 TREE_TYPE (expr) = type;
588 return expr;
591 /* If we are widening the type, put in an explicit conversion.
592 Similarly if we are not changing the width. After this, we know
593 we are truncating EXPR. */
595 else if (outprec >= inprec)
597 enum tree_code code;
599 /* If the precision of the EXPR's type is K bits and the
600 destination mode has more bits, and the sign is changing,
601 it is not safe to use a NOP_EXPR. For example, suppose
602 that EXPR's type is a 3-bit unsigned integer type, the
603 TYPE is a 3-bit signed integer type, and the machine mode
604 for the types is 8-bit QImode. In that case, the
605 conversion necessitates an explicit sign-extension. In
606 the signed-to-unsigned case the high-order bits have to
607 be cleared. */
608 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
609 && (TYPE_PRECISION (TREE_TYPE (expr))
610 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
611 code = CONVERT_EXPR;
612 else
613 code = NOP_EXPR;
615 return fold_build1 (code, type, expr);
618 /* If TYPE is an enumeral type or a type with a precision less
619 than the number of bits in its mode, do the conversion to the
620 type corresponding to its mode, then do a nop conversion
621 to TYPE. */
622 else if (TREE_CODE (type) == ENUMERAL_TYPE
623 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
624 return build1 (NOP_EXPR, type,
625 convert (lang_hooks.types.type_for_mode
626 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
627 expr));
629 /* Here detect when we can distribute the truncation down past some
630 arithmetic. For example, if adding two longs and converting to an
631 int, we can equally well convert both to ints and then add.
632 For the operations handled here, such truncation distribution
633 is always safe.
634 It is desirable in these cases:
635 1) when truncating down to full-word from a larger size
636 2) when truncating takes no work.
637 3) when at least one operand of the arithmetic has been extended
638 (as by C's default conversions). In this case we need two conversions
639 if we do the arithmetic as already requested, so we might as well
640 truncate both and then combine. Perhaps that way we need only one.
642 Note that in general we cannot do the arithmetic in a type
643 shorter than the desired result of conversion, even if the operands
644 are both extended from a shorter type, because they might overflow
645 if combined in that type. The exceptions to this--the times when
646 two narrow values can be combined in their narrow type even to
647 make a wider result--are handled by "shorten" in build_binary_op. */
649 switch (ex_form)
651 case RSHIFT_EXPR:
652 /* We can pass truncation down through right shifting
653 when the shift count is a nonpositive constant. */
654 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
655 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
656 goto trunc1;
657 break;
659 case LSHIFT_EXPR:
660 /* We can pass truncation down through left shifting
661 when the shift count is a nonnegative constant and
662 the target type is unsigned. */
663 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
664 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
665 && TYPE_UNSIGNED (type)
666 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
668 /* If shift count is less than the width of the truncated type,
669 really shift. */
670 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
671 /* In this case, shifting is like multiplication. */
672 goto trunc1;
673 else
675 /* If it is >= that width, result is zero.
676 Handling this with trunc1 would give the wrong result:
677 (int) ((long long) a << 32) is well defined (as 0)
678 but (int) a << 32 is undefined and would get a
679 warning. */
681 tree t = build_int_cst (type, 0);
683 /* If the original expression had side-effects, we must
684 preserve it. */
685 if (TREE_SIDE_EFFECTS (expr))
686 return build2 (COMPOUND_EXPR, type, expr, t);
687 else
688 return t;
691 break;
693 case TRUNC_DIV_EXPR:
695 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
696 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
698 /* Don't distribute unless the output precision is at least as big
699 as the actual inputs and it has the same signedness. */
700 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
701 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
702 /* If signedness of arg0 and arg1 don't match,
703 we can't necessarily find a type to compare them in. */
704 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
705 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
706 /* Do not change the sign of the division. */
707 && (TYPE_UNSIGNED (TREE_TYPE (expr))
708 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
709 /* Either require unsigned division or a division by
710 a constant that is not -1. */
711 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
712 || (TREE_CODE (arg1) == INTEGER_CST
713 && !integer_all_onesp (arg1))))
714 goto trunc1;
715 break;
718 case MAX_EXPR:
719 case MIN_EXPR:
720 case MULT_EXPR:
722 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
723 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
725 /* Don't distribute unless the output precision is at least as big
726 as the actual inputs. Otherwise, the comparison of the
727 truncated values will be wrong. */
728 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
729 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
730 /* If signedness of arg0 and arg1 don't match,
731 we can't necessarily find a type to compare them in. */
732 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
733 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
734 goto trunc1;
735 break;
738 case PLUS_EXPR:
739 case MINUS_EXPR:
740 case BIT_AND_EXPR:
741 case BIT_IOR_EXPR:
742 case BIT_XOR_EXPR:
743 trunc1:
745 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
746 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
748 /* Do not try to narrow operands of pointer subtraction;
749 that will interfere with other folding. */
750 if (ex_form == MINUS_EXPR
751 && CONVERT_EXPR_P (arg0)
752 && CONVERT_EXPR_P (arg1)
753 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
754 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
755 break;
757 if (outprec >= BITS_PER_WORD
758 || TRULY_NOOP_TRUNCATION (outprec, inprec)
759 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
760 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
762 /* Do the arithmetic in type TYPEX,
763 then convert result to TYPE. */
764 tree typex = type;
766 /* Can't do arithmetic in enumeral types
767 so use an integer type that will hold the values. */
768 if (TREE_CODE (typex) == ENUMERAL_TYPE)
769 typex
770 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
771 TYPE_UNSIGNED (typex));
773 /* But now perhaps TYPEX is as wide as INPREC.
774 In that case, do nothing special here.
775 (Otherwise would recurse infinitely in convert. */
776 if (TYPE_PRECISION (typex) != inprec)
778 /* Don't do unsigned arithmetic where signed was wanted,
779 or vice versa.
780 Exception: if both of the original operands were
781 unsigned then we can safely do the work as unsigned.
782 Exception: shift operations take their type solely
783 from the first argument.
784 Exception: the LSHIFT_EXPR case above requires that
785 we perform this operation unsigned lest we produce
786 signed-overflow undefinedness.
787 And we may need to do it as unsigned
788 if we truncate to the original size. */
789 if (TYPE_UNSIGNED (TREE_TYPE (expr))
790 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
791 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
792 || ex_form == LSHIFT_EXPR
793 || ex_form == RSHIFT_EXPR
794 || ex_form == LROTATE_EXPR
795 || ex_form == RROTATE_EXPR))
796 || ex_form == LSHIFT_EXPR
797 /* If we have !flag_wrapv, and either ARG0 or
798 ARG1 is of a signed type, we have to do
799 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
800 type in case the operation in outprec precision
801 could overflow. Otherwise, we would introduce
802 signed-overflow undefinedness. */
803 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
804 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
805 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
806 > outprec)
807 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
808 > outprec))
809 && (ex_form == PLUS_EXPR
810 || ex_form == MINUS_EXPR
811 || ex_form == MULT_EXPR)))
813 if (!TYPE_UNSIGNED (typex))
814 typex = unsigned_type_for (typex);
816 else
818 if (TYPE_UNSIGNED (typex))
819 typex = signed_type_for (typex);
821 return convert (type,
822 fold_build2 (ex_form, typex,
823 convert (typex, arg0),
824 convert (typex, arg1)));
828 break;
830 case NEGATE_EXPR:
831 case BIT_NOT_EXPR:
832 /* This is not correct for ABS_EXPR,
833 since we must test the sign before truncation. */
835 /* Do the arithmetic in type TYPEX,
836 then convert result to TYPE. */
837 tree typex = type;
839 /* Can't do arithmetic in enumeral types
840 so use an integer type that will hold the values. */
841 if (TREE_CODE (typex) == ENUMERAL_TYPE)
842 typex
843 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
844 TYPE_UNSIGNED (typex));
846 if (!TYPE_UNSIGNED (typex))
847 typex = unsigned_type_for (typex);
848 return convert (type,
849 fold_build1 (ex_form, typex,
850 convert (typex,
851 TREE_OPERAND (expr, 0))));
854 CASE_CONVERT:
855 /* Don't introduce a
856 "can't convert between vector values of different size" error. */
857 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
858 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
859 != GET_MODE_SIZE (TYPE_MODE (type))))
860 break;
861 /* If truncating after truncating, might as well do all at once.
862 If truncating after extending, we may get rid of wasted work. */
863 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
865 case COND_EXPR:
866 /* It is sometimes worthwhile to push the narrowing down through
867 the conditional and never loses. A COND_EXPR may have a throw
868 as one operand, which then has void type. Just leave void
869 operands as they are. */
870 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
871 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
872 ? TREE_OPERAND (expr, 1)
873 : convert (type, TREE_OPERAND (expr, 1)),
874 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
875 ? TREE_OPERAND (expr, 2)
876 : convert (type, TREE_OPERAND (expr, 2)));
878 default:
879 break;
882 /* When parsing long initializers, we might end up with a lot of casts.
883 Shortcut this. */
884 if (TREE_CODE (expr) == INTEGER_CST)
885 return fold_convert (type, expr);
886 return build1 (CONVERT_EXPR, type, expr);
888 case REAL_TYPE:
889 if (flag_sanitize & SANITIZE_FLOAT_CAST
890 && do_ubsan_in_current_function ())
892 expr = save_expr (expr);
893 tree check = ubsan_instrument_float_cast (loc, type, expr, expr);
894 expr = build1 (FIX_TRUNC_EXPR, type, expr);
895 if (check == NULL)
896 return expr;
897 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
899 else
900 return build1 (FIX_TRUNC_EXPR, type, expr);
902 case FIXED_POINT_TYPE:
903 return build1 (FIXED_CONVERT_EXPR, type, expr);
905 case COMPLEX_TYPE:
906 return convert (type,
907 fold_build1 (REALPART_EXPR,
908 TREE_TYPE (TREE_TYPE (expr)), expr));
910 case VECTOR_TYPE:
911 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
913 error ("can%'t convert a vector of type %qT"
914 " to type %qT which has different size",
915 TREE_TYPE (expr), type);
916 return error_mark_node;
918 return build1 (VIEW_CONVERT_EXPR, type, expr);
920 default:
921 error ("aggregate value used where an integer was expected");
922 return convert (type, integer_zero_node);
926 /* Convert EXPR to the complex type TYPE in the usual ways. */
928 tree
929 convert_to_complex (tree type, tree expr)
931 tree subtype = TREE_TYPE (type);
933 switch (TREE_CODE (TREE_TYPE (expr)))
935 case REAL_TYPE:
936 case FIXED_POINT_TYPE:
937 case INTEGER_TYPE:
938 case ENUMERAL_TYPE:
939 case BOOLEAN_TYPE:
940 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
941 convert (subtype, integer_zero_node));
943 case COMPLEX_TYPE:
945 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
947 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
948 return expr;
949 else if (TREE_CODE (expr) == COMPOUND_EXPR)
951 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
952 if (t == TREE_OPERAND (expr, 1))
953 return expr;
954 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
955 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
957 else if (TREE_CODE (expr) == COMPLEX_EXPR)
958 return fold_build2 (COMPLEX_EXPR, type,
959 convert (subtype, TREE_OPERAND (expr, 0)),
960 convert (subtype, TREE_OPERAND (expr, 1)));
961 else
963 expr = save_expr (expr);
964 return
965 fold_build2 (COMPLEX_EXPR, type,
966 convert (subtype,
967 fold_build1 (REALPART_EXPR,
968 TREE_TYPE (TREE_TYPE (expr)),
969 expr)),
970 convert (subtype,
971 fold_build1 (IMAGPART_EXPR,
972 TREE_TYPE (TREE_TYPE (expr)),
973 expr)));
977 case POINTER_TYPE:
978 case REFERENCE_TYPE:
979 error ("pointer value used where a complex was expected");
980 return convert_to_complex (type, integer_zero_node);
982 default:
983 error ("aggregate value used where a complex was expected");
984 return convert_to_complex (type, integer_zero_node);
988 /* Convert EXPR to the vector type TYPE in the usual ways. */
990 tree
991 convert_to_vector (tree type, tree expr)
993 switch (TREE_CODE (TREE_TYPE (expr)))
995 case INTEGER_TYPE:
996 case VECTOR_TYPE:
997 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
999 error ("can%'t convert a value of type %qT"
1000 " to vector type %qT which has different size",
1001 TREE_TYPE (expr), type);
1002 return error_mark_node;
1004 return build1 (VIEW_CONVERT_EXPR, type, expr);
1006 default:
1007 error ("can%'t convert value to a vector");
1008 return error_mark_node;
1012 /* Convert EXPR to some fixed-point type TYPE.
1014 EXPR must be fixed-point, float, integer, or enumeral;
1015 in other cases error is called. */
1017 tree
1018 convert_to_fixed (tree type, tree expr)
1020 if (integer_zerop (expr))
1022 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1023 return fixed_zero_node;
1025 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1027 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1028 return fixed_one_node;
1031 switch (TREE_CODE (TREE_TYPE (expr)))
1033 case FIXED_POINT_TYPE:
1034 case INTEGER_TYPE:
1035 case ENUMERAL_TYPE:
1036 case BOOLEAN_TYPE:
1037 case REAL_TYPE:
1038 return build1 (FIXED_CONVERT_EXPR, type, expr);
1040 case COMPLEX_TYPE:
1041 return convert (type,
1042 fold_build1 (REALPART_EXPR,
1043 TREE_TYPE (TREE_TYPE (expr)), expr));
1045 default:
1046 error ("aggregate value used where a fixed-point was expected");
1047 return error_mark_node;