/cp
[official-gcc.git] / gcc / convert.c
blob142e82a3c8f484888708d4ca527bb08bf13d9c92
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 "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "real.h"
38 #include "fixed-value.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "flags.h"
43 #include "convert.h"
44 #include "diagnostic-core.h"
45 #include "target.h"
46 #include "langhooks.h"
47 #include "builtins.h"
48 #include "ubsan.h"
50 /* Convert EXPR to some pointer or reference type TYPE.
51 EXPR must be pointer, reference, integer, enumeral, or literal zero;
52 in other cases error is called. */
54 tree
55 convert_to_pointer (tree type, tree expr)
57 location_t loc = EXPR_LOCATION (expr);
58 if (TREE_TYPE (expr) == type)
59 return expr;
61 switch (TREE_CODE (TREE_TYPE (expr)))
63 case POINTER_TYPE:
64 case REFERENCE_TYPE:
66 /* If the pointers point to different address spaces, conversion needs
67 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
68 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
69 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
71 if (to_as == from_as)
72 return fold_build1_loc (loc, NOP_EXPR, type, expr);
73 else
74 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
77 case INTEGER_TYPE:
78 case ENUMERAL_TYPE:
79 case BOOLEAN_TYPE:
81 /* If the input precision differs from the target pointer type
82 precision, first convert the input expression to an integer type of
83 the target precision. Some targets, e.g. VMS, need several pointer
84 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
85 unsigned int pprec = TYPE_PRECISION (type);
86 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
88 if (eprec != pprec)
89 expr = fold_build1_loc (loc, NOP_EXPR,
90 lang_hooks.types.type_for_size (pprec, 0),
91 expr);
94 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
96 default:
97 error ("cannot convert to a pointer type");
98 return convert_to_pointer (type, integer_zero_node);
103 /* Convert EXPR to some floating-point type TYPE.
105 EXPR must be float, fixed-point, integer, or enumeral;
106 in other cases error is called. */
108 tree
109 convert_to_real (tree type, tree expr)
111 enum built_in_function fcode = builtin_mathfn_code (expr);
112 tree itype = TREE_TYPE (expr);
114 if (TREE_CODE (expr) == COMPOUND_EXPR)
116 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
117 if (t == TREE_OPERAND (expr, 1))
118 return expr;
119 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
120 TREE_OPERAND (expr, 0), t);
123 /* Disable until we figure out how to decide whether the functions are
124 present in runtime. */
125 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
126 if (optimize
127 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
128 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
130 switch (fcode)
132 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
133 CASE_MATHFN (COSH)
134 CASE_MATHFN (EXP)
135 CASE_MATHFN (EXP10)
136 CASE_MATHFN (EXP2)
137 CASE_MATHFN (EXPM1)
138 CASE_MATHFN (GAMMA)
139 CASE_MATHFN (J0)
140 CASE_MATHFN (J1)
141 CASE_MATHFN (LGAMMA)
142 CASE_MATHFN (POW10)
143 CASE_MATHFN (SINH)
144 CASE_MATHFN (TGAMMA)
145 CASE_MATHFN (Y0)
146 CASE_MATHFN (Y1)
147 /* The above functions may set errno differently with float
148 input or output so this transformation is not safe with
149 -fmath-errno. */
150 if (flag_errno_math)
151 break;
152 CASE_MATHFN (ACOS)
153 CASE_MATHFN (ACOSH)
154 CASE_MATHFN (ASIN)
155 CASE_MATHFN (ASINH)
156 CASE_MATHFN (ATAN)
157 CASE_MATHFN (ATANH)
158 CASE_MATHFN (CBRT)
159 CASE_MATHFN (COS)
160 CASE_MATHFN (ERF)
161 CASE_MATHFN (ERFC)
162 CASE_MATHFN (LOG)
163 CASE_MATHFN (LOG10)
164 CASE_MATHFN (LOG2)
165 CASE_MATHFN (LOG1P)
166 CASE_MATHFN (SIN)
167 CASE_MATHFN (TAN)
168 CASE_MATHFN (TANH)
169 /* The above functions are not safe to do this conversion. */
170 if (!flag_unsafe_math_optimizations)
171 break;
172 CASE_MATHFN (SQRT)
173 CASE_MATHFN (FABS)
174 CASE_MATHFN (LOGB)
175 #undef CASE_MATHFN
177 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
178 tree newtype = type;
180 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
181 the both as the safe type for operation. */
182 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
183 newtype = TREE_TYPE (arg0);
185 /* We consider to convert
187 (T1) sqrtT2 ((T2) exprT3)
189 (T1) sqrtT4 ((T4) exprT3)
191 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
192 and T4 is NEWTYPE. All those types are of floating point types.
193 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
194 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
195 T2 and T4. See the following URL for a reference:
196 http://stackoverflow.com/questions/9235456/determining-
197 floating-point-square-root
199 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
200 && !flag_unsafe_math_optimizations)
202 /* The following conversion is unsafe even the precision condition
203 below is satisfied:
205 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
207 if (TYPE_MODE (type) != TYPE_MODE (newtype))
208 break;
210 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
211 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
212 if (p1 < p2 * 2 + 2)
213 break;
216 /* Be careful about integer to fp conversions.
217 These may overflow still. */
218 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
219 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
220 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
221 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
223 tree fn = mathfn_built_in (newtype, fcode);
225 if (fn)
227 tree arg = fold (convert_to_real (newtype, arg0));
228 expr = build_call_expr (fn, 1, arg);
229 if (newtype == type)
230 return expr;
234 default:
235 break;
238 if (optimize
239 && (((fcode == BUILT_IN_FLOORL
240 || fcode == BUILT_IN_CEILL
241 || fcode == BUILT_IN_ROUNDL
242 || fcode == BUILT_IN_RINTL
243 || fcode == BUILT_IN_TRUNCL
244 || fcode == BUILT_IN_NEARBYINTL)
245 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
246 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
247 || ((fcode == BUILT_IN_FLOOR
248 || fcode == BUILT_IN_CEIL
249 || fcode == BUILT_IN_ROUND
250 || fcode == BUILT_IN_RINT
251 || fcode == BUILT_IN_TRUNC
252 || fcode == BUILT_IN_NEARBYINT)
253 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
255 tree fn = mathfn_built_in (type, fcode);
257 if (fn)
259 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
261 /* Make sure (type)arg0 is an extension, otherwise we could end up
262 changing (float)floor(double d) into floorf((float)d), which is
263 incorrect because (float)d uses round-to-nearest and can round
264 up to the next integer. */
265 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
266 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
270 /* Propagate the cast into the operation. */
271 if (itype != type && FLOAT_TYPE_P (type))
272 switch (TREE_CODE (expr))
274 /* Convert (float)-x into -(float)x. This is safe for
275 round-to-nearest rounding mode when the inner type is float. */
276 case ABS_EXPR:
277 case NEGATE_EXPR:
278 if (!flag_rounding_math
279 && FLOAT_TYPE_P (itype)
280 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
281 return build1 (TREE_CODE (expr), type,
282 fold (convert_to_real (type,
283 TREE_OPERAND (expr, 0))));
284 break;
285 /* Convert (outertype)((innertype0)a+(innertype1)b)
286 into ((newtype)a+(newtype)b) where newtype
287 is the widest mode from all of these. */
288 case PLUS_EXPR:
289 case MINUS_EXPR:
290 case MULT_EXPR:
291 case RDIV_EXPR:
293 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
294 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
296 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
297 && FLOAT_TYPE_P (TREE_TYPE (arg1))
298 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
300 tree newtype = type;
302 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
303 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
304 || TYPE_MODE (type) == SDmode)
305 newtype = dfloat32_type_node;
306 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
307 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
308 || TYPE_MODE (type) == DDmode)
309 newtype = dfloat64_type_node;
310 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
311 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
312 || TYPE_MODE (type) == TDmode)
313 newtype = dfloat128_type_node;
314 if (newtype == dfloat32_type_node
315 || newtype == dfloat64_type_node
316 || newtype == dfloat128_type_node)
318 expr = build2 (TREE_CODE (expr), newtype,
319 fold (convert_to_real (newtype, arg0)),
320 fold (convert_to_real (newtype, arg1)));
321 if (newtype == type)
322 return expr;
323 break;
326 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
327 newtype = TREE_TYPE (arg0);
328 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
329 newtype = TREE_TYPE (arg1);
330 /* Sometimes this transformation is safe (cannot
331 change results through affecting double rounding
332 cases) and sometimes it is not. If NEWTYPE is
333 wider than TYPE, e.g. (float)((long double)double
334 + (long double)double) converted to
335 (float)(double + double), the transformation is
336 unsafe regardless of the details of the types
337 involved; double rounding can arise if the result
338 of NEWTYPE arithmetic is a NEWTYPE value half way
339 between two representable TYPE values but the
340 exact value is sufficiently different (in the
341 right direction) for this difference to be
342 visible in ITYPE arithmetic. If NEWTYPE is the
343 same as TYPE, however, the transformation may be
344 safe depending on the types involved: it is safe
345 if the ITYPE has strictly more than twice as many
346 mantissa bits as TYPE, can represent infinities
347 and NaNs if the TYPE can, and has sufficient
348 exponent range for the product or ratio of two
349 values representable in the TYPE to be within the
350 range of normal values of ITYPE. */
351 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
352 && (flag_unsafe_math_optimizations
353 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
354 && real_can_shorten_arithmetic (TYPE_MODE (itype),
355 TYPE_MODE (type))
356 && !excess_precision_type (newtype))))
358 expr = build2 (TREE_CODE (expr), newtype,
359 fold (convert_to_real (newtype, arg0)),
360 fold (convert_to_real (newtype, arg1)));
361 if (newtype == type)
362 return expr;
366 break;
367 default:
368 break;
371 switch (TREE_CODE (TREE_TYPE (expr)))
373 case REAL_TYPE:
374 /* Ignore the conversion if we don't need to store intermediate
375 results and neither type is a decimal float. */
376 return build1 ((flag_float_store
377 || DECIMAL_FLOAT_TYPE_P (type)
378 || DECIMAL_FLOAT_TYPE_P (itype))
379 ? CONVERT_EXPR : NOP_EXPR, type, expr);
381 case INTEGER_TYPE:
382 case ENUMERAL_TYPE:
383 case BOOLEAN_TYPE:
384 return build1 (FLOAT_EXPR, type, expr);
386 case FIXED_POINT_TYPE:
387 return build1 (FIXED_CONVERT_EXPR, type, expr);
389 case COMPLEX_TYPE:
390 return convert (type,
391 fold_build1 (REALPART_EXPR,
392 TREE_TYPE (TREE_TYPE (expr)), expr));
394 case POINTER_TYPE:
395 case REFERENCE_TYPE:
396 error ("pointer value used where a floating point value was expected");
397 return convert_to_real (type, integer_zero_node);
399 default:
400 error ("aggregate value used where a float was expected");
401 return convert_to_real (type, integer_zero_node);
405 /* Convert EXPR to some integer (or enum) type TYPE.
407 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
408 fixed-point or vector; in other cases error is called.
410 The result of this is always supposed to be a newly created tree node
411 not in use in any existing structure. */
413 tree
414 convert_to_integer (tree type, tree expr)
416 enum tree_code ex_form = TREE_CODE (expr);
417 tree intype = TREE_TYPE (expr);
418 unsigned int inprec = element_precision (intype);
419 unsigned int outprec = element_precision (type);
420 location_t loc = EXPR_LOCATION (expr);
422 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
423 be. Consider `enum E = { a, b = (enum E) 3 };'. */
424 if (!COMPLETE_TYPE_P (type))
426 error ("conversion to incomplete type");
427 return error_mark_node;
430 if (ex_form == COMPOUND_EXPR)
432 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
433 if (t == TREE_OPERAND (expr, 1))
434 return expr;
435 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
436 TREE_OPERAND (expr, 0), t);
439 /* Convert e.g. (long)round(d) -> lround(d). */
440 /* If we're converting to char, we may encounter differing behavior
441 between converting from double->char vs double->long->char.
442 We're in "undefined" territory but we prefer to be conservative,
443 so only proceed in "unsafe" math mode. */
444 if (optimize
445 && (flag_unsafe_math_optimizations
446 || (long_integer_type_node
447 && outprec >= TYPE_PRECISION (long_integer_type_node))))
449 tree s_expr = strip_float_extensions (expr);
450 tree s_intype = TREE_TYPE (s_expr);
451 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
452 tree fn = 0;
454 switch (fcode)
456 CASE_FLT_FN (BUILT_IN_CEIL):
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_ICEIL);
464 else if (outprec == TYPE_PRECISION (long_integer_type_node)
465 && !TYPE_UNSIGNED (type))
466 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
467 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
468 && !TYPE_UNSIGNED (type))
469 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
470 break;
472 CASE_FLT_FN (BUILT_IN_FLOOR):
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_IFLOOR);
480 else if (outprec == TYPE_PRECISION (long_integer_type_node)
481 && !TYPE_UNSIGNED (type))
482 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
483 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
484 && !TYPE_UNSIGNED (type))
485 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
486 break;
488 CASE_FLT_FN (BUILT_IN_ROUND):
489 /* Only convert in ISO C99 mode and with -fno-math-errno. */
490 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
491 break;
492 if (outprec < TYPE_PRECISION (integer_type_node)
493 || (outprec == TYPE_PRECISION (integer_type_node)
494 && !TYPE_UNSIGNED (type)))
495 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
496 else if (outprec == TYPE_PRECISION (long_integer_type_node)
497 && !TYPE_UNSIGNED (type))
498 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
499 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
500 && !TYPE_UNSIGNED (type))
501 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
502 break;
504 CASE_FLT_FN (BUILT_IN_NEARBYINT):
505 /* Only convert nearbyint* if we can ignore math exceptions. */
506 if (flag_trapping_math)
507 break;
508 /* ... Fall through ... */
509 CASE_FLT_FN (BUILT_IN_RINT):
510 /* Only convert in ISO C99 mode and with -fno-math-errno. */
511 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
512 break;
513 if (outprec < TYPE_PRECISION (integer_type_node)
514 || (outprec == TYPE_PRECISION (integer_type_node)
515 && !TYPE_UNSIGNED (type)))
516 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
517 else if (outprec == TYPE_PRECISION (long_integer_type_node)
518 && !TYPE_UNSIGNED (type))
519 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
520 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
521 && !TYPE_UNSIGNED (type))
522 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
523 break;
525 CASE_FLT_FN (BUILT_IN_TRUNC):
526 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
528 default:
529 break;
532 if (fn)
534 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
535 return convert_to_integer (type, newexpr);
539 /* Convert (int)logb(d) -> ilogb(d). */
540 if (optimize
541 && flag_unsafe_math_optimizations
542 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
543 && integer_type_node
544 && (outprec > TYPE_PRECISION (integer_type_node)
545 || (outprec == TYPE_PRECISION (integer_type_node)
546 && !TYPE_UNSIGNED (type))))
548 tree s_expr = strip_float_extensions (expr);
549 tree s_intype = TREE_TYPE (s_expr);
550 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
551 tree fn = 0;
553 switch (fcode)
555 CASE_FLT_FN (BUILT_IN_LOGB):
556 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
557 break;
559 default:
560 break;
563 if (fn)
565 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
566 return convert_to_integer (type, newexpr);
570 switch (TREE_CODE (intype))
572 case POINTER_TYPE:
573 case REFERENCE_TYPE:
574 if (integer_zerop (expr))
575 return build_int_cst (type, 0);
577 /* Convert to an unsigned integer of the correct width first, and from
578 there widen/truncate to the required type. Some targets support the
579 coexistence of multiple valid pointer sizes, so fetch the one we need
580 from the type. */
581 expr = fold_build1 (CONVERT_EXPR,
582 lang_hooks.types.type_for_size
583 (TYPE_PRECISION (intype), 0),
584 expr);
585 return fold_convert (type, expr);
587 case INTEGER_TYPE:
588 case ENUMERAL_TYPE:
589 case BOOLEAN_TYPE:
590 case OFFSET_TYPE:
591 /* If this is a logical operation, which just returns 0 or 1, we can
592 change the type of the expression. */
594 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
596 expr = copy_node (expr);
597 TREE_TYPE (expr) = type;
598 return expr;
601 /* If we are widening the type, put in an explicit conversion.
602 Similarly if we are not changing the width. After this, we know
603 we are truncating EXPR. */
605 else if (outprec >= inprec)
607 enum tree_code code;
609 /* If the precision of the EXPR's type is K bits and the
610 destination mode has more bits, and the sign is changing,
611 it is not safe to use a NOP_EXPR. For example, suppose
612 that EXPR's type is a 3-bit unsigned integer type, the
613 TYPE is a 3-bit signed integer type, and the machine mode
614 for the types is 8-bit QImode. In that case, the
615 conversion necessitates an explicit sign-extension. In
616 the signed-to-unsigned case the high-order bits have to
617 be cleared. */
618 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
619 && (TYPE_PRECISION (TREE_TYPE (expr))
620 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
621 code = CONVERT_EXPR;
622 else
623 code = NOP_EXPR;
625 return fold_build1 (code, type, expr);
628 /* If TYPE is an enumeral type or a type with a precision less
629 than the number of bits in its mode, do the conversion to the
630 type corresponding to its mode, then do a nop conversion
631 to TYPE. */
632 else if (TREE_CODE (type) == ENUMERAL_TYPE
633 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
634 return build1 (NOP_EXPR, type,
635 convert (lang_hooks.types.type_for_mode
636 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
637 expr));
639 /* Here detect when we can distribute the truncation down past some
640 arithmetic. For example, if adding two longs and converting to an
641 int, we can equally well convert both to ints and then add.
642 For the operations handled here, such truncation distribution
643 is always safe.
644 It is desirable in these cases:
645 1) when truncating down to full-word from a larger size
646 2) when truncating takes no work.
647 3) when at least one operand of the arithmetic has been extended
648 (as by C's default conversions). In this case we need two conversions
649 if we do the arithmetic as already requested, so we might as well
650 truncate both and then combine. Perhaps that way we need only one.
652 Note that in general we cannot do the arithmetic in a type
653 shorter than the desired result of conversion, even if the operands
654 are both extended from a shorter type, because they might overflow
655 if combined in that type. The exceptions to this--the times when
656 two narrow values can be combined in their narrow type even to
657 make a wider result--are handled by "shorten" in build_binary_op. */
659 switch (ex_form)
661 case RSHIFT_EXPR:
662 /* We can pass truncation down through right shifting
663 when the shift count is a nonpositive constant. */
664 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
665 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
666 goto trunc1;
667 break;
669 case LSHIFT_EXPR:
670 /* We can pass truncation down through left shifting
671 when the shift count is a nonnegative constant and
672 the target type is unsigned. */
673 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
674 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
675 && TYPE_UNSIGNED (type)
676 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
678 /* If shift count is less than the width of the truncated type,
679 really shift. */
680 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
681 /* In this case, shifting is like multiplication. */
682 goto trunc1;
683 else
685 /* If it is >= that width, result is zero.
686 Handling this with trunc1 would give the wrong result:
687 (int) ((long long) a << 32) is well defined (as 0)
688 but (int) a << 32 is undefined and would get a
689 warning. */
691 tree t = build_int_cst (type, 0);
693 /* If the original expression had side-effects, we must
694 preserve it. */
695 if (TREE_SIDE_EFFECTS (expr))
696 return build2 (COMPOUND_EXPR, type, expr, t);
697 else
698 return t;
701 break;
703 case TRUNC_DIV_EXPR:
705 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
706 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
708 /* Don't distribute unless the output precision is at least as big
709 as the actual inputs and it has the same signedness. */
710 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
711 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
712 /* If signedness of arg0 and arg1 don't match,
713 we can't necessarily find a type to compare them in. */
714 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
715 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
716 /* Do not change the sign of the division. */
717 && (TYPE_UNSIGNED (TREE_TYPE (expr))
718 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
719 /* Either require unsigned division or a division by
720 a constant that is not -1. */
721 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
722 || (TREE_CODE (arg1) == INTEGER_CST
723 && !integer_all_onesp (arg1))))
724 goto trunc1;
725 break;
728 case MAX_EXPR:
729 case MIN_EXPR:
730 case MULT_EXPR:
732 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
733 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
735 /* Don't distribute unless the output precision is at least as big
736 as the actual inputs. Otherwise, the comparison of the
737 truncated values will be wrong. */
738 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
739 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
740 /* If signedness of arg0 and arg1 don't match,
741 we can't necessarily find a type to compare them in. */
742 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
743 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
744 goto trunc1;
745 break;
748 case PLUS_EXPR:
749 case MINUS_EXPR:
750 case BIT_AND_EXPR:
751 case BIT_IOR_EXPR:
752 case BIT_XOR_EXPR:
753 trunc1:
755 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
756 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
758 /* Do not try to narrow operands of pointer subtraction;
759 that will interfere with other folding. */
760 if (ex_form == MINUS_EXPR
761 && CONVERT_EXPR_P (arg0)
762 && CONVERT_EXPR_P (arg1)
763 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
764 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
765 break;
767 if (outprec >= BITS_PER_WORD
768 || TRULY_NOOP_TRUNCATION (outprec, inprec)
769 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
770 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
772 /* Do the arithmetic in type TYPEX,
773 then convert result to TYPE. */
774 tree typex = type;
776 /* Can't do arithmetic in enumeral types
777 so use an integer type that will hold the values. */
778 if (TREE_CODE (typex) == ENUMERAL_TYPE)
779 typex
780 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
781 TYPE_UNSIGNED (typex));
783 /* But now perhaps TYPEX is as wide as INPREC.
784 In that case, do nothing special here.
785 (Otherwise would recurse infinitely in convert. */
786 if (TYPE_PRECISION (typex) != inprec)
788 /* Don't do unsigned arithmetic where signed was wanted,
789 or vice versa.
790 Exception: if both of the original operands were
791 unsigned then we can safely do the work as unsigned.
792 Exception: shift operations take their type solely
793 from the first argument.
794 Exception: the LSHIFT_EXPR case above requires that
795 we perform this operation unsigned lest we produce
796 signed-overflow undefinedness.
797 And we may need to do it as unsigned
798 if we truncate to the original size. */
799 if (TYPE_UNSIGNED (TREE_TYPE (expr))
800 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
801 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
802 || ex_form == LSHIFT_EXPR
803 || ex_form == RSHIFT_EXPR
804 || ex_form == LROTATE_EXPR
805 || ex_form == RROTATE_EXPR))
806 || ex_form == LSHIFT_EXPR
807 /* If we have !flag_wrapv, and either ARG0 or
808 ARG1 is of a signed type, we have to do
809 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
810 type in case the operation in outprec precision
811 could overflow. Otherwise, we would introduce
812 signed-overflow undefinedness. */
813 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
814 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
815 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
816 > outprec)
817 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
818 > outprec))
819 && (ex_form == PLUS_EXPR
820 || ex_form == MINUS_EXPR
821 || ex_form == MULT_EXPR)))
823 if (!TYPE_UNSIGNED (typex))
824 typex = unsigned_type_for (typex);
826 else
828 if (TYPE_UNSIGNED (typex))
829 typex = signed_type_for (typex);
831 return convert (type,
832 fold_build2 (ex_form, typex,
833 convert (typex, arg0),
834 convert (typex, arg1)));
838 break;
840 case NEGATE_EXPR:
841 case BIT_NOT_EXPR:
842 /* This is not correct for ABS_EXPR,
843 since we must test the sign before truncation. */
845 /* Do the arithmetic in type TYPEX,
846 then convert result to TYPE. */
847 tree typex = type;
849 /* Can't do arithmetic in enumeral types
850 so use an integer type that will hold the values. */
851 if (TREE_CODE (typex) == ENUMERAL_TYPE)
852 typex
853 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
854 TYPE_UNSIGNED (typex));
856 if (!TYPE_UNSIGNED (typex))
857 typex = unsigned_type_for (typex);
858 return convert (type,
859 fold_build1 (ex_form, typex,
860 convert (typex,
861 TREE_OPERAND (expr, 0))));
864 CASE_CONVERT:
865 /* Don't introduce a
866 "can't convert between vector values of different size" error. */
867 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
868 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
869 != GET_MODE_SIZE (TYPE_MODE (type))))
870 break;
871 /* If truncating after truncating, might as well do all at once.
872 If truncating after extending, we may get rid of wasted work. */
873 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
875 case COND_EXPR:
876 /* It is sometimes worthwhile to push the narrowing down through
877 the conditional and never loses. A COND_EXPR may have a throw
878 as one operand, which then has void type. Just leave void
879 operands as they are. */
880 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
881 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
882 ? TREE_OPERAND (expr, 1)
883 : convert (type, TREE_OPERAND (expr, 1)),
884 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
885 ? TREE_OPERAND (expr, 2)
886 : convert (type, TREE_OPERAND (expr, 2)));
888 default:
889 break;
892 /* When parsing long initializers, we might end up with a lot of casts.
893 Shortcut this. */
894 if (TREE_CODE (expr) == INTEGER_CST)
895 return fold_convert (type, expr);
896 return build1 (CONVERT_EXPR, type, expr);
898 case REAL_TYPE:
899 if (flag_sanitize & SANITIZE_FLOAT_CAST
900 && do_ubsan_in_current_function ())
902 expr = save_expr (expr);
903 tree check = ubsan_instrument_float_cast (loc, type, expr, expr);
904 expr = build1 (FIX_TRUNC_EXPR, type, expr);
905 if (check == NULL)
906 return expr;
907 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
909 else
910 return build1 (FIX_TRUNC_EXPR, type, expr);
912 case FIXED_POINT_TYPE:
913 return build1 (FIXED_CONVERT_EXPR, type, expr);
915 case COMPLEX_TYPE:
916 return convert (type,
917 fold_build1 (REALPART_EXPR,
918 TREE_TYPE (TREE_TYPE (expr)), expr));
920 case VECTOR_TYPE:
921 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
923 error ("can%'t convert between vector values of different size");
924 return error_mark_node;
926 return build1 (VIEW_CONVERT_EXPR, type, expr);
928 default:
929 error ("aggregate value used where an integer was expected");
930 return convert (type, integer_zero_node);
934 /* Convert EXPR to the complex type TYPE in the usual ways. */
936 tree
937 convert_to_complex (tree type, tree expr)
939 tree subtype = TREE_TYPE (type);
941 switch (TREE_CODE (TREE_TYPE (expr)))
943 case REAL_TYPE:
944 case FIXED_POINT_TYPE:
945 case INTEGER_TYPE:
946 case ENUMERAL_TYPE:
947 case BOOLEAN_TYPE:
948 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
949 convert (subtype, integer_zero_node));
951 case COMPLEX_TYPE:
953 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
955 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
956 return expr;
957 else if (TREE_CODE (expr) == COMPOUND_EXPR)
959 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
960 if (t == TREE_OPERAND (expr, 1))
961 return expr;
962 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
963 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
965 else if (TREE_CODE (expr) == COMPLEX_EXPR)
966 return fold_build2 (COMPLEX_EXPR, type,
967 convert (subtype, TREE_OPERAND (expr, 0)),
968 convert (subtype, TREE_OPERAND (expr, 1)));
969 else
971 expr = save_expr (expr);
972 return
973 fold_build2 (COMPLEX_EXPR, type,
974 convert (subtype,
975 fold_build1 (REALPART_EXPR,
976 TREE_TYPE (TREE_TYPE (expr)),
977 expr)),
978 convert (subtype,
979 fold_build1 (IMAGPART_EXPR,
980 TREE_TYPE (TREE_TYPE (expr)),
981 expr)));
985 case POINTER_TYPE:
986 case REFERENCE_TYPE:
987 error ("pointer value used where a complex was expected");
988 return convert_to_complex (type, integer_zero_node);
990 default:
991 error ("aggregate value used where a complex was expected");
992 return convert_to_complex (type, integer_zero_node);
996 /* Convert EXPR to the vector type TYPE in the usual ways. */
998 tree
999 convert_to_vector (tree type, tree expr)
1001 switch (TREE_CODE (TREE_TYPE (expr)))
1003 case INTEGER_TYPE:
1004 case VECTOR_TYPE:
1005 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
1007 error ("can%'t convert between vector values of different size");
1008 return error_mark_node;
1010 return build1 (VIEW_CONVERT_EXPR, type, expr);
1012 default:
1013 error ("can%'t convert value to a vector");
1014 return error_mark_node;
1018 /* Convert EXPR to some fixed-point type TYPE.
1020 EXPR must be fixed-point, float, integer, or enumeral;
1021 in other cases error is called. */
1023 tree
1024 convert_to_fixed (tree type, tree expr)
1026 if (integer_zerop (expr))
1028 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1029 return fixed_zero_node;
1031 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1033 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1034 return fixed_one_node;
1037 switch (TREE_CODE (TREE_TYPE (expr)))
1039 case FIXED_POINT_TYPE:
1040 case INTEGER_TYPE:
1041 case ENUMERAL_TYPE:
1042 case BOOLEAN_TYPE:
1043 case REAL_TYPE:
1044 return build1 (FIXED_CONVERT_EXPR, type, expr);
1046 case COMPLEX_TYPE:
1047 return convert (type,
1048 fold_build1 (REALPART_EXPR,
1049 TREE_TYPE (TREE_TYPE (expr)), expr));
1051 default:
1052 error ("aggregate value used where a fixed-point was expected");
1053 return error_mark_node;