* g++.dg/template/using30.C: Move ...
[official-gcc.git] / gcc / convert.c
blob00907290c211479b2e4f492f3c19c36b7b4c0b06
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"
35 #include "builtins.h"
36 #include "ubsan.h"
38 /* Convert EXPR to some pointer or reference type TYPE.
39 EXPR must be pointer, reference, integer, enumeral, or literal zero;
40 in other cases error is called. */
42 tree
43 convert_to_pointer (tree type, tree expr)
45 location_t loc = EXPR_LOCATION (expr);
46 if (TREE_TYPE (expr) == type)
47 return expr;
49 switch (TREE_CODE (TREE_TYPE (expr)))
51 case POINTER_TYPE:
52 case REFERENCE_TYPE:
54 /* If the pointers point to different address spaces, conversion needs
55 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
56 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
57 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
59 if (to_as == from_as)
60 return fold_build1_loc (loc, NOP_EXPR, type, expr);
61 else
62 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
65 case INTEGER_TYPE:
66 case ENUMERAL_TYPE:
67 case BOOLEAN_TYPE:
69 /* If the input precision differs from the target pointer type
70 precision, first convert the input expression to an integer type of
71 the target precision. Some targets, e.g. VMS, need several pointer
72 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
73 unsigned int pprec = TYPE_PRECISION (type);
74 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
76 if (eprec != pprec)
77 expr = fold_build1_loc (loc, NOP_EXPR,
78 lang_hooks.types.type_for_size (pprec, 0),
79 expr);
82 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
84 default:
85 error ("cannot convert to a pointer type");
86 return convert_to_pointer (type, integer_zero_node);
91 /* Convert EXPR to some floating-point type TYPE.
93 EXPR must be float, fixed-point, integer, or enumeral;
94 in other cases error is called. */
96 tree
97 convert_to_real (tree type, tree expr)
99 enum built_in_function fcode = builtin_mathfn_code (expr);
100 tree itype = TREE_TYPE (expr);
102 if (TREE_CODE (expr) == COMPOUND_EXPR)
104 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
105 if (t == TREE_OPERAND (expr, 1))
106 return expr;
107 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
108 TREE_OPERAND (expr, 0), t);
111 /* Disable until we figure out how to decide whether the functions are
112 present in runtime. */
113 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
114 if (optimize
115 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
116 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
118 switch (fcode)
120 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
121 CASE_MATHFN (COSH)
122 CASE_MATHFN (EXP)
123 CASE_MATHFN (EXP10)
124 CASE_MATHFN (EXP2)
125 CASE_MATHFN (EXPM1)
126 CASE_MATHFN (GAMMA)
127 CASE_MATHFN (J0)
128 CASE_MATHFN (J1)
129 CASE_MATHFN (LGAMMA)
130 CASE_MATHFN (POW10)
131 CASE_MATHFN (SINH)
132 CASE_MATHFN (TGAMMA)
133 CASE_MATHFN (Y0)
134 CASE_MATHFN (Y1)
135 /* The above functions may set errno differently with float
136 input or output so this transformation is not safe with
137 -fmath-errno. */
138 if (flag_errno_math)
139 break;
140 CASE_MATHFN (ACOS)
141 CASE_MATHFN (ACOSH)
142 CASE_MATHFN (ASIN)
143 CASE_MATHFN (ASINH)
144 CASE_MATHFN (ATAN)
145 CASE_MATHFN (ATANH)
146 CASE_MATHFN (CBRT)
147 CASE_MATHFN (COS)
148 CASE_MATHFN (ERF)
149 CASE_MATHFN (ERFC)
150 CASE_MATHFN (LOG)
151 CASE_MATHFN (LOG10)
152 CASE_MATHFN (LOG2)
153 CASE_MATHFN (LOG1P)
154 CASE_MATHFN (SIN)
155 CASE_MATHFN (TAN)
156 CASE_MATHFN (TANH)
157 /* The above functions are not safe to do this conversion. */
158 if (!flag_unsafe_math_optimizations)
159 break;
160 CASE_MATHFN (SQRT)
161 CASE_MATHFN (FABS)
162 CASE_MATHFN (LOGB)
163 #undef CASE_MATHFN
165 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
166 tree newtype = type;
168 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
169 the both as the safe type for operation. */
170 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
171 newtype = TREE_TYPE (arg0);
173 /* We consider to convert
175 (T1) sqrtT2 ((T2) exprT3)
177 (T1) sqrtT4 ((T4) exprT3)
179 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
180 and T4 is NEWTYPE. All those types are of floating point types.
181 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
182 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
183 T2 and T4. See the following URL for a reference:
184 http://stackoverflow.com/questions/9235456/determining-
185 floating-point-square-root
187 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
188 && !flag_unsafe_math_optimizations)
190 /* The following conversion is unsafe even the precision condition
191 below is satisfied:
193 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
195 if (TYPE_MODE (type) != TYPE_MODE (newtype))
196 break;
198 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
199 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
200 if (p1 < p2 * 2 + 2)
201 break;
204 /* Be careful about integer to fp conversions.
205 These may overflow still. */
206 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
207 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
208 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
209 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
211 tree fn = mathfn_built_in (newtype, fcode);
213 if (fn)
215 tree arg = fold (convert_to_real (newtype, arg0));
216 expr = build_call_expr (fn, 1, arg);
217 if (newtype == type)
218 return expr;
222 default:
223 break;
226 if (optimize
227 && (((fcode == BUILT_IN_FLOORL
228 || fcode == BUILT_IN_CEILL
229 || fcode == BUILT_IN_ROUNDL
230 || fcode == BUILT_IN_RINTL
231 || fcode == BUILT_IN_TRUNCL
232 || fcode == BUILT_IN_NEARBYINTL)
233 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
234 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
235 || ((fcode == BUILT_IN_FLOOR
236 || fcode == BUILT_IN_CEIL
237 || fcode == BUILT_IN_ROUND
238 || fcode == BUILT_IN_RINT
239 || fcode == BUILT_IN_TRUNC
240 || fcode == BUILT_IN_NEARBYINT)
241 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
243 tree fn = mathfn_built_in (type, fcode);
245 if (fn)
247 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
249 /* Make sure (type)arg0 is an extension, otherwise we could end up
250 changing (float)floor(double d) into floorf((float)d), which is
251 incorrect because (float)d uses round-to-nearest and can round
252 up to the next integer. */
253 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
254 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
258 /* Propagate the cast into the operation. */
259 if (itype != type && FLOAT_TYPE_P (type))
260 switch (TREE_CODE (expr))
262 /* Convert (float)-x into -(float)x. This is safe for
263 round-to-nearest rounding mode when the inner type is float. */
264 case ABS_EXPR:
265 case NEGATE_EXPR:
266 if (!flag_rounding_math
267 && FLOAT_TYPE_P (itype)
268 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
269 return build1 (TREE_CODE (expr), type,
270 fold (convert_to_real (type,
271 TREE_OPERAND (expr, 0))));
272 break;
273 /* Convert (outertype)((innertype0)a+(innertype1)b)
274 into ((newtype)a+(newtype)b) where newtype
275 is the widest mode from all of these. */
276 case PLUS_EXPR:
277 case MINUS_EXPR:
278 case MULT_EXPR:
279 case RDIV_EXPR:
281 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
282 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
284 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
285 && FLOAT_TYPE_P (TREE_TYPE (arg1))
286 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
288 tree newtype = type;
290 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
291 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
292 || TYPE_MODE (type) == SDmode)
293 newtype = dfloat32_type_node;
294 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
295 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
296 || TYPE_MODE (type) == DDmode)
297 newtype = dfloat64_type_node;
298 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
299 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
300 || TYPE_MODE (type) == TDmode)
301 newtype = dfloat128_type_node;
302 if (newtype == dfloat32_type_node
303 || newtype == dfloat64_type_node
304 || newtype == dfloat128_type_node)
306 expr = build2 (TREE_CODE (expr), newtype,
307 fold (convert_to_real (newtype, arg0)),
308 fold (convert_to_real (newtype, arg1)));
309 if (newtype == type)
310 return expr;
311 break;
314 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
315 newtype = TREE_TYPE (arg0);
316 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
317 newtype = TREE_TYPE (arg1);
318 /* Sometimes this transformation is safe (cannot
319 change results through affecting double rounding
320 cases) and sometimes it is not. If NEWTYPE is
321 wider than TYPE, e.g. (float)((long double)double
322 + (long double)double) converted to
323 (float)(double + double), the transformation is
324 unsafe regardless of the details of the types
325 involved; double rounding can arise if the result
326 of NEWTYPE arithmetic is a NEWTYPE value half way
327 between two representable TYPE values but the
328 exact value is sufficiently different (in the
329 right direction) for this difference to be
330 visible in ITYPE arithmetic. If NEWTYPE is the
331 same as TYPE, however, the transformation may be
332 safe depending on the types involved: it is safe
333 if the ITYPE has strictly more than twice as many
334 mantissa bits as TYPE, can represent infinities
335 and NaNs if the TYPE can, and has sufficient
336 exponent range for the product or ratio of two
337 values representable in the TYPE to be within the
338 range of normal values of ITYPE. */
339 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
340 && (flag_unsafe_math_optimizations
341 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
342 && real_can_shorten_arithmetic (TYPE_MODE (itype),
343 TYPE_MODE (type))
344 && !excess_precision_type (newtype))))
346 expr = build2 (TREE_CODE (expr), newtype,
347 fold (convert_to_real (newtype, arg0)),
348 fold (convert_to_real (newtype, arg1)));
349 if (newtype == type)
350 return expr;
354 break;
355 default:
356 break;
359 switch (TREE_CODE (TREE_TYPE (expr)))
361 case REAL_TYPE:
362 /* Ignore the conversion if we don't need to store intermediate
363 results and neither type is a decimal float. */
364 return build1 ((flag_float_store
365 || DECIMAL_FLOAT_TYPE_P (type)
366 || DECIMAL_FLOAT_TYPE_P (itype))
367 ? CONVERT_EXPR : NOP_EXPR, type, expr);
369 case INTEGER_TYPE:
370 case ENUMERAL_TYPE:
371 case BOOLEAN_TYPE:
372 return build1 (FLOAT_EXPR, type, expr);
374 case FIXED_POINT_TYPE:
375 return build1 (FIXED_CONVERT_EXPR, type, expr);
377 case COMPLEX_TYPE:
378 return convert (type,
379 fold_build1 (REALPART_EXPR,
380 TREE_TYPE (TREE_TYPE (expr)), expr));
382 case POINTER_TYPE:
383 case REFERENCE_TYPE:
384 error ("pointer value used where a floating point value was expected");
385 return convert_to_real (type, integer_zero_node);
387 default:
388 error ("aggregate value used where a float was expected");
389 return convert_to_real (type, integer_zero_node);
393 /* Convert EXPR to some integer (or enum) type TYPE.
395 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
396 fixed-point or vector; in other cases error is called.
398 The result of this is always supposed to be a newly created tree node
399 not in use in any existing structure. */
401 tree
402 convert_to_integer (tree type, tree expr)
404 enum tree_code ex_form = TREE_CODE (expr);
405 tree intype = TREE_TYPE (expr);
406 unsigned int inprec = element_precision (intype);
407 unsigned int outprec = element_precision (type);
408 location_t loc = EXPR_LOCATION (expr);
410 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
411 be. Consider `enum E = { a, b = (enum E) 3 };'. */
412 if (!COMPLETE_TYPE_P (type))
414 error ("conversion to incomplete type");
415 return error_mark_node;
418 if (ex_form == COMPOUND_EXPR)
420 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
421 if (t == TREE_OPERAND (expr, 1))
422 return expr;
423 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
424 TREE_OPERAND (expr, 0), t);
427 /* Convert e.g. (long)round(d) -> lround(d). */
428 /* If we're converting to char, we may encounter differing behavior
429 between converting from double->char vs double->long->char.
430 We're in "undefined" territory but we prefer to be conservative,
431 so only proceed in "unsafe" math mode. */
432 if (optimize
433 && (flag_unsafe_math_optimizations
434 || (long_integer_type_node
435 && outprec >= TYPE_PRECISION (long_integer_type_node))))
437 tree s_expr = strip_float_extensions (expr);
438 tree s_intype = TREE_TYPE (s_expr);
439 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
440 tree fn = 0;
442 switch (fcode)
444 CASE_FLT_FN (BUILT_IN_CEIL):
445 /* Only convert in ISO C99 mode. */
446 if (!targetm.libc_has_function (function_c99_misc))
447 break;
448 if (outprec < TYPE_PRECISION (integer_type_node)
449 || (outprec == TYPE_PRECISION (integer_type_node)
450 && !TYPE_UNSIGNED (type)))
451 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
452 else if (outprec == TYPE_PRECISION (long_integer_type_node)
453 && !TYPE_UNSIGNED (type))
454 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
455 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
456 && !TYPE_UNSIGNED (type))
457 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
458 break;
460 CASE_FLT_FN (BUILT_IN_FLOOR):
461 /* Only convert in ISO C99 mode. */
462 if (!targetm.libc_has_function (function_c99_misc))
463 break;
464 if (outprec < TYPE_PRECISION (integer_type_node)
465 || (outprec == TYPE_PRECISION (integer_type_node)
466 && !TYPE_UNSIGNED (type)))
467 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
468 else if (outprec == TYPE_PRECISION (long_integer_type_node)
469 && !TYPE_UNSIGNED (type))
470 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
471 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
472 && !TYPE_UNSIGNED (type))
473 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
474 break;
476 CASE_FLT_FN (BUILT_IN_ROUND):
477 /* Only convert in ISO C99 mode and with -fno-math-errno. */
478 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
479 break;
480 if (outprec < TYPE_PRECISION (integer_type_node)
481 || (outprec == TYPE_PRECISION (integer_type_node)
482 && !TYPE_UNSIGNED (type)))
483 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
484 else if (outprec == TYPE_PRECISION (long_integer_type_node)
485 && !TYPE_UNSIGNED (type))
486 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
487 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
488 && !TYPE_UNSIGNED (type))
489 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
490 break;
492 CASE_FLT_FN (BUILT_IN_NEARBYINT):
493 /* Only convert nearbyint* if we can ignore math exceptions. */
494 if (flag_trapping_math)
495 break;
496 /* ... Fall through ... */
497 CASE_FLT_FN (BUILT_IN_RINT):
498 /* Only convert in ISO C99 mode and with -fno-math-errno. */
499 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
500 break;
501 if (outprec < TYPE_PRECISION (integer_type_node)
502 || (outprec == TYPE_PRECISION (integer_type_node)
503 && !TYPE_UNSIGNED (type)))
504 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
505 else if (outprec == TYPE_PRECISION (long_integer_type_node)
506 && !TYPE_UNSIGNED (type))
507 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
508 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
509 && !TYPE_UNSIGNED (type))
510 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
511 break;
513 CASE_FLT_FN (BUILT_IN_TRUNC):
514 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
516 default:
517 break;
520 if (fn)
522 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
523 return convert_to_integer (type, newexpr);
527 /* Convert (int)logb(d) -> ilogb(d). */
528 if (optimize
529 && flag_unsafe_math_optimizations
530 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
531 && integer_type_node
532 && (outprec > TYPE_PRECISION (integer_type_node)
533 || (outprec == TYPE_PRECISION (integer_type_node)
534 && !TYPE_UNSIGNED (type))))
536 tree s_expr = strip_float_extensions (expr);
537 tree s_intype = TREE_TYPE (s_expr);
538 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
539 tree fn = 0;
541 switch (fcode)
543 CASE_FLT_FN (BUILT_IN_LOGB):
544 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
545 break;
547 default:
548 break;
551 if (fn)
553 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
554 return convert_to_integer (type, newexpr);
558 switch (TREE_CODE (intype))
560 case POINTER_TYPE:
561 case REFERENCE_TYPE:
562 if (integer_zerop (expr))
563 return build_int_cst (type, 0);
565 /* Convert to an unsigned integer of the correct width first, and from
566 there widen/truncate to the required type. Some targets support the
567 coexistence of multiple valid pointer sizes, so fetch the one we need
568 from the type. */
569 expr = fold_build1 (CONVERT_EXPR,
570 lang_hooks.types.type_for_size
571 (TYPE_PRECISION (intype), 0),
572 expr);
573 return fold_convert (type, expr);
575 case INTEGER_TYPE:
576 case ENUMERAL_TYPE:
577 case BOOLEAN_TYPE:
578 case OFFSET_TYPE:
579 /* If this is a logical operation, which just returns 0 or 1, we can
580 change the type of the expression. */
582 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
584 expr = copy_node (expr);
585 TREE_TYPE (expr) = type;
586 return expr;
589 /* If we are widening the type, put in an explicit conversion.
590 Similarly if we are not changing the width. After this, we know
591 we are truncating EXPR. */
593 else if (outprec >= inprec)
595 enum tree_code code;
597 /* If the precision of the EXPR's type is K bits and the
598 destination mode has more bits, and the sign is changing,
599 it is not safe to use a NOP_EXPR. For example, suppose
600 that EXPR's type is a 3-bit unsigned integer type, the
601 TYPE is a 3-bit signed integer type, and the machine mode
602 for the types is 8-bit QImode. In that case, the
603 conversion necessitates an explicit sign-extension. In
604 the signed-to-unsigned case the high-order bits have to
605 be cleared. */
606 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
607 && (TYPE_PRECISION (TREE_TYPE (expr))
608 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
609 code = CONVERT_EXPR;
610 else
611 code = NOP_EXPR;
613 return fold_build1 (code, type, expr);
616 /* If TYPE is an enumeral type or a type with a precision less
617 than the number of bits in its mode, do the conversion to the
618 type corresponding to its mode, then do a nop conversion
619 to TYPE. */
620 else if (TREE_CODE (type) == ENUMERAL_TYPE
621 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
622 return build1 (NOP_EXPR, type,
623 convert (lang_hooks.types.type_for_mode
624 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
625 expr));
627 /* Here detect when we can distribute the truncation down past some
628 arithmetic. For example, if adding two longs and converting to an
629 int, we can equally well convert both to ints and then add.
630 For the operations handled here, such truncation distribution
631 is always safe.
632 It is desirable in these cases:
633 1) when truncating down to full-word from a larger size
634 2) when truncating takes no work.
635 3) when at least one operand of the arithmetic has been extended
636 (as by C's default conversions). In this case we need two conversions
637 if we do the arithmetic as already requested, so we might as well
638 truncate both and then combine. Perhaps that way we need only one.
640 Note that in general we cannot do the arithmetic in a type
641 shorter than the desired result of conversion, even if the operands
642 are both extended from a shorter type, because they might overflow
643 if combined in that type. The exceptions to this--the times when
644 two narrow values can be combined in their narrow type even to
645 make a wider result--are handled by "shorten" in build_binary_op. */
647 switch (ex_form)
649 case RSHIFT_EXPR:
650 /* We can pass truncation down through right shifting
651 when the shift count is a nonpositive constant. */
652 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
653 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
654 goto trunc1;
655 break;
657 case LSHIFT_EXPR:
658 /* We can pass truncation down through left shifting
659 when the shift count is a nonnegative constant and
660 the target type is unsigned. */
661 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
662 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
663 && TYPE_UNSIGNED (type)
664 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
666 /* If shift count is less than the width of the truncated type,
667 really shift. */
668 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
669 /* In this case, shifting is like multiplication. */
670 goto trunc1;
671 else
673 /* If it is >= that width, result is zero.
674 Handling this with trunc1 would give the wrong result:
675 (int) ((long long) a << 32) is well defined (as 0)
676 but (int) a << 32 is undefined and would get a
677 warning. */
679 tree t = build_int_cst (type, 0);
681 /* If the original expression had side-effects, we must
682 preserve it. */
683 if (TREE_SIDE_EFFECTS (expr))
684 return build2 (COMPOUND_EXPR, type, expr, t);
685 else
686 return t;
689 break;
691 case TRUNC_DIV_EXPR:
693 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
694 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
696 /* Don't distribute unless the output precision is at least as big
697 as the actual inputs and it has the same signedness. */
698 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
699 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
700 /* If signedness of arg0 and arg1 don't match,
701 we can't necessarily find a type to compare them in. */
702 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
703 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
704 /* Do not change the sign of the division. */
705 && (TYPE_UNSIGNED (TREE_TYPE (expr))
706 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
707 /* Either require unsigned division or a division by
708 a constant that is not -1. */
709 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
710 || (TREE_CODE (arg1) == INTEGER_CST
711 && !integer_all_onesp (arg1))))
712 goto trunc1;
713 break;
716 case MAX_EXPR:
717 case MIN_EXPR:
718 case MULT_EXPR:
720 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
721 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
723 /* Don't distribute unless the output precision is at least as big
724 as the actual inputs. Otherwise, the comparison of the
725 truncated values will be wrong. */
726 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
727 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
728 /* If signedness of arg0 and arg1 don't match,
729 we can't necessarily find a type to compare them in. */
730 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
731 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
732 goto trunc1;
733 break;
736 case PLUS_EXPR:
737 case MINUS_EXPR:
738 case BIT_AND_EXPR:
739 case BIT_IOR_EXPR:
740 case BIT_XOR_EXPR:
741 trunc1:
743 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
744 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
746 /* Do not try to narrow operands of pointer subtraction;
747 that will interfere with other folding. */
748 if (ex_form == MINUS_EXPR
749 && CONVERT_EXPR_P (arg0)
750 && CONVERT_EXPR_P (arg1)
751 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
752 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
753 break;
755 if (outprec >= BITS_PER_WORD
756 || TRULY_NOOP_TRUNCATION (outprec, inprec)
757 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
758 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
760 /* Do the arithmetic in type TYPEX,
761 then convert result to TYPE. */
762 tree typex = type;
764 /* Can't do arithmetic in enumeral types
765 so use an integer type that will hold the values. */
766 if (TREE_CODE (typex) == ENUMERAL_TYPE)
767 typex
768 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
769 TYPE_UNSIGNED (typex));
771 /* But now perhaps TYPEX is as wide as INPREC.
772 In that case, do nothing special here.
773 (Otherwise would recurse infinitely in convert. */
774 if (TYPE_PRECISION (typex) != inprec)
776 /* Don't do unsigned arithmetic where signed was wanted,
777 or vice versa.
778 Exception: if both of the original operands were
779 unsigned then we can safely do the work as unsigned.
780 Exception: shift operations take their type solely
781 from the first argument.
782 Exception: the LSHIFT_EXPR case above requires that
783 we perform this operation unsigned lest we produce
784 signed-overflow undefinedness.
785 And we may need to do it as unsigned
786 if we truncate to the original size. */
787 if (TYPE_UNSIGNED (TREE_TYPE (expr))
788 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
789 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
790 || ex_form == LSHIFT_EXPR
791 || ex_form == RSHIFT_EXPR
792 || ex_form == LROTATE_EXPR
793 || ex_form == RROTATE_EXPR))
794 || ex_form == LSHIFT_EXPR
795 /* If we have !flag_wrapv, and either ARG0 or
796 ARG1 is of a signed type, we have to do
797 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
798 type in case the operation in outprec precision
799 could overflow. Otherwise, we would introduce
800 signed-overflow undefinedness. */
801 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
802 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
803 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
804 > outprec)
805 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
806 > outprec))
807 && (ex_form == PLUS_EXPR
808 || ex_form == MINUS_EXPR
809 || ex_form == MULT_EXPR)))
811 if (!TYPE_UNSIGNED (typex))
812 typex = unsigned_type_for (typex);
814 else
816 if (TYPE_UNSIGNED (typex))
817 typex = signed_type_for (typex);
819 return convert (type,
820 fold_build2 (ex_form, typex,
821 convert (typex, arg0),
822 convert (typex, arg1)));
826 break;
828 case NEGATE_EXPR:
829 case BIT_NOT_EXPR:
830 /* This is not correct for ABS_EXPR,
831 since we must test the sign before truncation. */
833 /* Do the arithmetic in type TYPEX,
834 then convert result to TYPE. */
835 tree typex = type;
837 /* Can't do arithmetic in enumeral types
838 so use an integer type that will hold the values. */
839 if (TREE_CODE (typex) == ENUMERAL_TYPE)
840 typex
841 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
842 TYPE_UNSIGNED (typex));
844 if (!TYPE_UNSIGNED (typex))
845 typex = unsigned_type_for (typex);
846 return convert (type,
847 fold_build1 (ex_form, typex,
848 convert (typex,
849 TREE_OPERAND (expr, 0))));
852 CASE_CONVERT:
853 /* Don't introduce a
854 "can't convert between vector values of different size" error. */
855 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
856 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
857 != GET_MODE_SIZE (TYPE_MODE (type))))
858 break;
859 /* If truncating after truncating, might as well do all at once.
860 If truncating after extending, we may get rid of wasted work. */
861 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
863 case COND_EXPR:
864 /* It is sometimes worthwhile to push the narrowing down through
865 the conditional and never loses. A COND_EXPR may have a throw
866 as one operand, which then has void type. Just leave void
867 operands as they are. */
868 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
869 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
870 ? TREE_OPERAND (expr, 1)
871 : convert (type, TREE_OPERAND (expr, 1)),
872 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
873 ? TREE_OPERAND (expr, 2)
874 : convert (type, TREE_OPERAND (expr, 2)));
876 default:
877 break;
880 /* When parsing long initializers, we might end up with a lot of casts.
881 Shortcut this. */
882 if (TREE_CODE (expr) == INTEGER_CST)
883 return fold_convert (type, expr);
884 return build1 (CONVERT_EXPR, type, expr);
886 case REAL_TYPE:
887 if (flag_sanitize & SANITIZE_FLOAT_CAST
888 && current_function_decl != NULL_TREE
889 && !lookup_attribute ("no_sanitize_undefined",
890 DECL_ATTRIBUTES (current_function_decl)))
892 expr = save_expr (expr);
893 tree check = ubsan_instrument_float_cast (loc, type, 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 between vector values of different size");
914 return error_mark_node;
916 return build1 (VIEW_CONVERT_EXPR, type, expr);
918 default:
919 error ("aggregate value used where an integer was expected");
920 return convert (type, integer_zero_node);
924 /* Convert EXPR to the complex type TYPE in the usual ways. */
926 tree
927 convert_to_complex (tree type, tree expr)
929 tree subtype = TREE_TYPE (type);
931 switch (TREE_CODE (TREE_TYPE (expr)))
933 case REAL_TYPE:
934 case FIXED_POINT_TYPE:
935 case INTEGER_TYPE:
936 case ENUMERAL_TYPE:
937 case BOOLEAN_TYPE:
938 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
939 convert (subtype, integer_zero_node));
941 case COMPLEX_TYPE:
943 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
945 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
946 return expr;
947 else if (TREE_CODE (expr) == COMPOUND_EXPR)
949 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
950 if (t == TREE_OPERAND (expr, 1))
951 return expr;
952 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
953 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
955 else if (TREE_CODE (expr) == COMPLEX_EXPR)
956 return fold_build2 (COMPLEX_EXPR, type,
957 convert (subtype, TREE_OPERAND (expr, 0)),
958 convert (subtype, TREE_OPERAND (expr, 1)));
959 else
961 expr = save_expr (expr);
962 return
963 fold_build2 (COMPLEX_EXPR, type,
964 convert (subtype,
965 fold_build1 (REALPART_EXPR,
966 TREE_TYPE (TREE_TYPE (expr)),
967 expr)),
968 convert (subtype,
969 fold_build1 (IMAGPART_EXPR,
970 TREE_TYPE (TREE_TYPE (expr)),
971 expr)));
975 case POINTER_TYPE:
976 case REFERENCE_TYPE:
977 error ("pointer value used where a complex was expected");
978 return convert_to_complex (type, integer_zero_node);
980 default:
981 error ("aggregate value used where a complex was expected");
982 return convert_to_complex (type, integer_zero_node);
986 /* Convert EXPR to the vector type TYPE in the usual ways. */
988 tree
989 convert_to_vector (tree type, tree expr)
991 switch (TREE_CODE (TREE_TYPE (expr)))
993 case INTEGER_TYPE:
994 case VECTOR_TYPE:
995 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
997 error ("can%'t convert between vector values of different size");
998 return error_mark_node;
1000 return build1 (VIEW_CONVERT_EXPR, type, expr);
1002 default:
1003 error ("can%'t convert value to a vector");
1004 return error_mark_node;
1008 /* Convert EXPR to some fixed-point type TYPE.
1010 EXPR must be fixed-point, float, integer, or enumeral;
1011 in other cases error is called. */
1013 tree
1014 convert_to_fixed (tree type, tree expr)
1016 if (integer_zerop (expr))
1018 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1019 return fixed_zero_node;
1021 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1023 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1024 return fixed_one_node;
1027 switch (TREE_CODE (TREE_TYPE (expr)))
1029 case FIXED_POINT_TYPE:
1030 case INTEGER_TYPE:
1031 case ENUMERAL_TYPE:
1032 case BOOLEAN_TYPE:
1033 case REAL_TYPE:
1034 return build1 (FIXED_CONVERT_EXPR, type, expr);
1036 case COMPLEX_TYPE:
1037 return convert (type,
1038 fold_build1 (REALPART_EXPR,
1039 TREE_TYPE (TREE_TYPE (expr)), expr));
1041 default:
1042 error ("aggregate value used where a fixed-point was expected");
1043 return error_mark_node;