libgo: add misc/cgo files
[official-gcc.git] / gcc / convert.c
blob429f988cbde24f7ed8f3790967f6ee2f48b2afe1
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2017 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 "target.h"
28 #include "tree.h"
29 #include "diagnostic-core.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "convert.h"
33 #include "langhooks.h"
34 #include "builtins.h"
35 #include "ubsan.h"
36 #include "asan.h"
38 #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
39 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
40 : build1_loc (LOC, CODE, TYPE, EXPR))
41 #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
42 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
43 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
45 /* Convert EXPR to some pointer or reference type TYPE.
46 EXPR must be pointer, reference, integer, enumeral, or literal zero;
47 in other cases error is called. If FOLD_P is true, try to fold the
48 expression. */
50 static tree
51 convert_to_pointer_1 (tree type, tree expr, bool fold_p)
53 location_t loc = EXPR_LOCATION (expr);
54 if (TREE_TYPE (expr) == type)
55 return expr;
57 switch (TREE_CODE (TREE_TYPE (expr)))
59 case POINTER_TYPE:
60 case REFERENCE_TYPE:
62 /* If the pointers point to different address spaces, conversion needs
63 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
64 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
65 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
67 if (to_as == from_as)
68 return maybe_fold_build1_loc (fold_p, loc, NOP_EXPR, type, expr);
69 else
70 return maybe_fold_build1_loc (fold_p, loc, ADDR_SPACE_CONVERT_EXPR,
71 type, expr);
74 case INTEGER_TYPE:
75 case ENUMERAL_TYPE:
76 case BOOLEAN_TYPE:
78 /* If the input precision differs from the target pointer type
79 precision, first convert the input expression to an integer type of
80 the target precision. Some targets, e.g. VMS, need several pointer
81 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
82 unsigned int pprec = TYPE_PRECISION (type);
83 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
85 if (eprec != pprec)
86 expr
87 = maybe_fold_build1_loc (fold_p, loc, NOP_EXPR,
88 lang_hooks.types.type_for_size (pprec, 0),
89 expr);
91 return maybe_fold_build1_loc (fold_p, loc, CONVERT_EXPR, type, expr);
93 default:
94 error ("cannot convert to a pointer type");
95 return convert_to_pointer_1 (type, integer_zero_node, fold_p);
99 /* A wrapper around convert_to_pointer_1 that always folds the
100 expression. */
102 tree
103 convert_to_pointer (tree type, tree expr)
105 return convert_to_pointer_1 (type, expr, true);
108 /* A wrapper around convert_to_pointer_1 that only folds the
109 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
111 tree
112 convert_to_pointer_maybe_fold (tree type, tree expr, bool dofold)
114 return convert_to_pointer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
117 /* Convert EXPR to some floating-point type TYPE.
119 EXPR must be float, fixed-point, integer, or enumeral;
120 in other cases error is called. If FOLD_P is true, try to fold
121 the expression. */
123 static tree
124 convert_to_real_1 (tree type, tree expr, bool fold_p)
126 enum built_in_function fcode = builtin_mathfn_code (expr);
127 tree itype = TREE_TYPE (expr);
128 location_t loc = EXPR_LOCATION (expr);
130 if (TREE_CODE (expr) == COMPOUND_EXPR)
132 tree t = convert_to_real_1 (type, TREE_OPERAND (expr, 1), fold_p);
133 if (t == TREE_OPERAND (expr, 1))
134 return expr;
135 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
136 TREE_OPERAND (expr, 0), t);
139 /* Disable until we figure out how to decide whether the functions are
140 present in runtime. */
141 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
142 if (optimize
143 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
144 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
146 switch (fcode)
148 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
149 CASE_MATHFN (COSH)
150 CASE_MATHFN (EXP)
151 CASE_MATHFN (EXP10)
152 CASE_MATHFN (EXP2)
153 CASE_MATHFN (EXPM1)
154 CASE_MATHFN (GAMMA)
155 CASE_MATHFN (J0)
156 CASE_MATHFN (J1)
157 CASE_MATHFN (LGAMMA)
158 CASE_MATHFN (POW10)
159 CASE_MATHFN (SINH)
160 CASE_MATHFN (TGAMMA)
161 CASE_MATHFN (Y0)
162 CASE_MATHFN (Y1)
163 /* The above functions may set errno differently with float
164 input or output so this transformation is not safe with
165 -fmath-errno. */
166 if (flag_errno_math)
167 break;
168 gcc_fallthrough ();
169 CASE_MATHFN (ACOS)
170 CASE_MATHFN (ACOSH)
171 CASE_MATHFN (ASIN)
172 CASE_MATHFN (ASINH)
173 CASE_MATHFN (ATAN)
174 CASE_MATHFN (ATANH)
175 CASE_MATHFN (CBRT)
176 CASE_MATHFN (COS)
177 CASE_MATHFN (ERF)
178 CASE_MATHFN (ERFC)
179 CASE_MATHFN (LOG)
180 CASE_MATHFN (LOG10)
181 CASE_MATHFN (LOG2)
182 CASE_MATHFN (LOG1P)
183 CASE_MATHFN (SIN)
184 CASE_MATHFN (TAN)
185 CASE_MATHFN (TANH)
186 /* The above functions are not safe to do this conversion. */
187 if (!flag_unsafe_math_optimizations)
188 break;
189 gcc_fallthrough ();
190 CASE_MATHFN (SQRT)
191 CASE_MATHFN (FABS)
192 CASE_MATHFN (LOGB)
193 #undef CASE_MATHFN
195 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
196 tree newtype = type;
198 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
199 the both as the safe type for operation. */
200 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
201 newtype = TREE_TYPE (arg0);
203 /* We consider to convert
205 (T1) sqrtT2 ((T2) exprT3)
207 (T1) sqrtT4 ((T4) exprT3)
209 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
210 and T4 is NEWTYPE. All those types are of floating point types.
211 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
212 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
213 T2 and T4. See the following URL for a reference:
214 http://stackoverflow.com/questions/9235456/determining-
215 floating-point-square-root
217 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
218 && !flag_unsafe_math_optimizations)
220 /* The following conversion is unsafe even the precision condition
221 below is satisfied:
223 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
225 if (TYPE_MODE (type) != TYPE_MODE (newtype))
226 break;
228 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
229 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
230 if (p1 < p2 * 2 + 2)
231 break;
234 /* Be careful about integer to fp conversions.
235 These may overflow still. */
236 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
237 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
238 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
239 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
241 tree fn = mathfn_built_in (newtype, fcode);
242 if (fn)
244 tree arg = convert_to_real_1 (newtype, arg0, fold_p);
245 expr = build_call_expr (fn, 1, arg);
246 if (newtype == type)
247 return expr;
251 default:
252 break;
256 /* Propagate the cast into the operation. */
257 if (itype != type && FLOAT_TYPE_P (type))
258 switch (TREE_CODE (expr))
260 /* Convert (float)-x into -(float)x. This is safe for
261 round-to-nearest rounding mode when the inner type is float. */
262 case ABS_EXPR:
263 case NEGATE_EXPR:
264 if (!flag_rounding_math
265 && FLOAT_TYPE_P (itype)
266 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
268 tree arg = convert_to_real_1 (type, TREE_OPERAND (expr, 0),
269 fold_p);
270 return build1 (TREE_CODE (expr), type, arg);
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 convert_to_real_1 (newtype, arg0,
308 fold_p),
309 convert_to_real_1 (newtype, arg1,
310 fold_p));
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 convert_to_real_1 (newtype, arg0,
350 fold_p),
351 convert_to_real_1 (newtype, arg1,
352 fold_p));
353 if (newtype == type)
354 return expr;
358 break;
359 default:
360 break;
363 switch (TREE_CODE (TREE_TYPE (expr)))
365 case REAL_TYPE:
366 /* Ignore the conversion if we don't need to store intermediate
367 results and neither type is a decimal float. */
368 return build1_loc (loc,
369 (flag_float_store
370 || DECIMAL_FLOAT_TYPE_P (type)
371 || DECIMAL_FLOAT_TYPE_P (itype))
372 ? CONVERT_EXPR : NOP_EXPR, type, expr);
374 case INTEGER_TYPE:
375 case ENUMERAL_TYPE:
376 case BOOLEAN_TYPE:
377 return build1 (FLOAT_EXPR, type, expr);
379 case FIXED_POINT_TYPE:
380 return build1 (FIXED_CONVERT_EXPR, type, expr);
382 case COMPLEX_TYPE:
383 return convert (type,
384 maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
385 TREE_TYPE (TREE_TYPE (expr)),
386 expr));
388 case POINTER_TYPE:
389 case REFERENCE_TYPE:
390 error ("pointer value used where a floating point value was expected");
391 return convert_to_real_1 (type, integer_zero_node, fold_p);
393 default:
394 error ("aggregate value used where a float was expected");
395 return convert_to_real_1 (type, integer_zero_node, fold_p);
399 /* A wrapper around convert_to_real_1 that always folds the
400 expression. */
402 tree
403 convert_to_real (tree type, tree expr)
405 return convert_to_real_1 (type, expr, true);
408 /* A wrapper around convert_to_real_1 that only folds the
409 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
411 tree
412 convert_to_real_maybe_fold (tree type, tree expr, bool dofold)
414 return convert_to_real_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
417 /* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
418 result in TYPE. */
420 static tree
421 do_narrow (location_t loc,
422 enum tree_code ex_form, tree type, tree arg0, tree arg1,
423 tree expr, unsigned inprec, unsigned outprec, bool dofold)
425 /* Do the arithmetic in type TYPEX,
426 then convert result to TYPE. */
427 tree typex = type;
429 /* Can't do arithmetic in enumeral types
430 so use an integer type that will hold the values. */
431 if (TREE_CODE (typex) == ENUMERAL_TYPE)
432 typex = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
433 TYPE_UNSIGNED (typex));
435 /* But now perhaps TYPEX is as wide as INPREC.
436 In that case, do nothing special here.
437 (Otherwise would recurse infinitely in convert. */
438 if (TYPE_PRECISION (typex) != inprec)
440 /* Don't do unsigned arithmetic where signed was wanted,
441 or vice versa.
442 Exception: if both of the original operands were
443 unsigned then we can safely do the work as unsigned.
444 Exception: shift operations take their type solely
445 from the first argument.
446 Exception: the LSHIFT_EXPR case above requires that
447 we perform this operation unsigned lest we produce
448 signed-overflow undefinedness.
449 And we may need to do it as unsigned
450 if we truncate to the original size. */
451 if (TYPE_UNSIGNED (TREE_TYPE (expr))
452 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
453 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
454 || ex_form == LSHIFT_EXPR
455 || ex_form == RSHIFT_EXPR
456 || ex_form == LROTATE_EXPR
457 || ex_form == RROTATE_EXPR))
458 || ex_form == LSHIFT_EXPR
459 /* If we have !flag_wrapv, and either ARG0 or
460 ARG1 is of a signed type, we have to do
461 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
462 type in case the operation in outprec precision
463 could overflow. Otherwise, we would introduce
464 signed-overflow undefinedness. */
465 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
466 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
467 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
468 > outprec)
469 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
470 > outprec))
471 && (ex_form == PLUS_EXPR
472 || ex_form == MINUS_EXPR
473 || ex_form == MULT_EXPR)))
475 if (!TYPE_UNSIGNED (typex))
476 typex = unsigned_type_for (typex);
478 else
480 if (TYPE_UNSIGNED (typex))
481 typex = signed_type_for (typex);
483 /* We should do away with all this once we have a proper
484 type promotion/demotion pass, see PR45397. */
485 expr = maybe_fold_build2_loc (dofold, loc, ex_form, typex,
486 convert (typex, arg0),
487 convert (typex, arg1));
488 return convert (type, expr);
491 return NULL_TREE;
494 /* Convert EXPR to some integer (or enum) type TYPE.
496 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
497 fixed-point or vector; in other cases error is called.
499 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
501 The result of this is always supposed to be a newly created tree node
502 not in use in any existing structure. */
504 static tree
505 convert_to_integer_1 (tree type, tree expr, bool dofold)
507 enum tree_code ex_form = TREE_CODE (expr);
508 tree intype = TREE_TYPE (expr);
509 unsigned int inprec = element_precision (intype);
510 unsigned int outprec = element_precision (type);
511 location_t loc = EXPR_LOCATION (expr);
513 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
514 be. Consider `enum E = { a, b = (enum E) 3 };'. */
515 if (!COMPLETE_TYPE_P (type))
517 error ("conversion to incomplete type");
518 return error_mark_node;
521 if (ex_form == COMPOUND_EXPR)
523 tree t = convert_to_integer_1 (type, TREE_OPERAND (expr, 1), dofold);
524 if (t == TREE_OPERAND (expr, 1))
525 return expr;
526 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
527 TREE_OPERAND (expr, 0), t);
530 /* Convert e.g. (long)round(d) -> lround(d). */
531 /* If we're converting to char, we may encounter differing behavior
532 between converting from double->char vs double->long->char.
533 We're in "undefined" territory but we prefer to be conservative,
534 so only proceed in "unsafe" math mode. */
535 if (optimize
536 && (flag_unsafe_math_optimizations
537 || (long_integer_type_node
538 && outprec >= TYPE_PRECISION (long_integer_type_node))))
540 tree s_expr = strip_float_extensions (expr);
541 tree s_intype = TREE_TYPE (s_expr);
542 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
543 tree fn = 0;
545 switch (fcode)
547 CASE_FLT_FN (BUILT_IN_CEIL):
548 /* Only convert in ISO C99 mode. */
549 if (!targetm.libc_has_function (function_c99_misc))
550 break;
551 if (outprec < TYPE_PRECISION (integer_type_node)
552 || (outprec == TYPE_PRECISION (integer_type_node)
553 && !TYPE_UNSIGNED (type)))
554 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
555 else if (outprec == TYPE_PRECISION (long_integer_type_node)
556 && !TYPE_UNSIGNED (type))
557 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
558 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
559 && !TYPE_UNSIGNED (type))
560 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
561 break;
563 CASE_FLT_FN (BUILT_IN_FLOOR):
564 /* Only convert in ISO C99 mode. */
565 if (!targetm.libc_has_function (function_c99_misc))
566 break;
567 if (outprec < TYPE_PRECISION (integer_type_node)
568 || (outprec == TYPE_PRECISION (integer_type_node)
569 && !TYPE_UNSIGNED (type)))
570 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
571 else if (outprec == TYPE_PRECISION (long_integer_type_node)
572 && !TYPE_UNSIGNED (type))
573 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
574 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
575 && !TYPE_UNSIGNED (type))
576 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
577 break;
579 CASE_FLT_FN (BUILT_IN_ROUND):
580 /* Only convert in ISO C99 mode and with -fno-math-errno. */
581 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
582 break;
583 if (outprec < TYPE_PRECISION (integer_type_node)
584 || (outprec == TYPE_PRECISION (integer_type_node)
585 && !TYPE_UNSIGNED (type)))
586 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
587 else if (outprec == TYPE_PRECISION (long_integer_type_node)
588 && !TYPE_UNSIGNED (type))
589 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
590 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
591 && !TYPE_UNSIGNED (type))
592 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
593 break;
595 CASE_FLT_FN (BUILT_IN_NEARBYINT):
596 /* Only convert nearbyint* if we can ignore math exceptions. */
597 if (flag_trapping_math)
598 break;
599 gcc_fallthrough ();
600 CASE_FLT_FN (BUILT_IN_RINT):
601 /* Only convert in ISO C99 mode and with -fno-math-errno. */
602 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
603 break;
604 if (outprec < TYPE_PRECISION (integer_type_node)
605 || (outprec == TYPE_PRECISION (integer_type_node)
606 && !TYPE_UNSIGNED (type)))
607 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
608 else if (outprec == TYPE_PRECISION (long_integer_type_node)
609 && !TYPE_UNSIGNED (type))
610 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
611 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
612 && !TYPE_UNSIGNED (type))
613 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
614 break;
616 CASE_FLT_FN (BUILT_IN_TRUNC):
617 return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0), dofold);
619 default:
620 break;
623 if (fn)
625 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
626 return convert_to_integer_1 (type, newexpr, dofold);
630 /* Convert (int)logb(d) -> ilogb(d). */
631 if (optimize
632 && flag_unsafe_math_optimizations
633 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
634 && integer_type_node
635 && (outprec > TYPE_PRECISION (integer_type_node)
636 || (outprec == TYPE_PRECISION (integer_type_node)
637 && !TYPE_UNSIGNED (type))))
639 tree s_expr = strip_float_extensions (expr);
640 tree s_intype = TREE_TYPE (s_expr);
641 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
642 tree fn = 0;
644 switch (fcode)
646 CASE_FLT_FN (BUILT_IN_LOGB):
647 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
648 break;
650 default:
651 break;
654 if (fn)
656 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
657 return convert_to_integer_1 (type, newexpr, dofold);
661 switch (TREE_CODE (intype))
663 case POINTER_TYPE:
664 case REFERENCE_TYPE:
665 if (integer_zerop (expr))
666 return build_int_cst (type, 0);
668 /* Convert to an unsigned integer of the correct width first, and from
669 there widen/truncate to the required type. Some targets support the
670 coexistence of multiple valid pointer sizes, so fetch the one we need
671 from the type. */
672 if (!dofold)
673 return build1 (CONVERT_EXPR, type, expr);
674 expr = fold_build1 (CONVERT_EXPR,
675 lang_hooks.types.type_for_size
676 (TYPE_PRECISION (intype), 0),
677 expr);
678 return fold_convert (type, expr);
680 case INTEGER_TYPE:
681 case ENUMERAL_TYPE:
682 case BOOLEAN_TYPE:
683 case OFFSET_TYPE:
684 /* If this is a logical operation, which just returns 0 or 1, we can
685 change the type of the expression. */
687 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
689 expr = copy_node (expr);
690 TREE_TYPE (expr) = type;
691 return expr;
694 /* If we are widening the type, put in an explicit conversion.
695 Similarly if we are not changing the width. After this, we know
696 we are truncating EXPR. */
698 else if (outprec >= inprec)
700 enum tree_code code;
702 /* If the precision of the EXPR's type is K bits and the
703 destination mode has more bits, and the sign is changing,
704 it is not safe to use a NOP_EXPR. For example, suppose
705 that EXPR's type is a 3-bit unsigned integer type, the
706 TYPE is a 3-bit signed integer type, and the machine mode
707 for the types is 8-bit QImode. In that case, the
708 conversion necessitates an explicit sign-extension. In
709 the signed-to-unsigned case the high-order bits have to
710 be cleared. */
711 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
712 && (TYPE_PRECISION (TREE_TYPE (expr))
713 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
714 code = CONVERT_EXPR;
715 else
716 code = NOP_EXPR;
718 return maybe_fold_build1_loc (dofold, loc, code, type, expr);
721 /* If TYPE is an enumeral type or a type with a precision less
722 than the number of bits in its mode, do the conversion to the
723 type corresponding to its mode, then do a nop conversion
724 to TYPE. */
725 else if (TREE_CODE (type) == ENUMERAL_TYPE
726 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
728 expr = convert (lang_hooks.types.type_for_mode
729 (TYPE_MODE (type), TYPE_UNSIGNED (type)), expr);
730 return maybe_fold_build1_loc (dofold, loc, NOP_EXPR, type, expr);
733 /* Here detect when we can distribute the truncation down past some
734 arithmetic. For example, if adding two longs and converting to an
735 int, we can equally well convert both to ints and then add.
736 For the operations handled here, such truncation distribution
737 is always safe.
738 It is desirable in these cases:
739 1) when truncating down to full-word from a larger size
740 2) when truncating takes no work.
741 3) when at least one operand of the arithmetic has been extended
742 (as by C's default conversions). In this case we need two conversions
743 if we do the arithmetic as already requested, so we might as well
744 truncate both and then combine. Perhaps that way we need only one.
746 Note that in general we cannot do the arithmetic in a type
747 shorter than the desired result of conversion, even if the operands
748 are both extended from a shorter type, because they might overflow
749 if combined in that type. The exceptions to this--the times when
750 two narrow values can be combined in their narrow type even to
751 make a wider result--are handled by "shorten" in build_binary_op. */
753 if (dofold)
754 switch (ex_form)
756 case RSHIFT_EXPR:
757 /* We can pass truncation down through right shifting
758 when the shift count is a nonpositive constant. */
759 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
760 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
761 goto trunc1;
762 break;
764 case LSHIFT_EXPR:
765 /* We can pass truncation down through left shifting
766 when the shift count is a nonnegative constant and
767 the target type is unsigned. */
768 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
769 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
770 && TYPE_UNSIGNED (type)
771 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
773 /* If shift count is less than the width of the truncated type,
774 really shift. */
775 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
776 /* In this case, shifting is like multiplication. */
777 goto trunc1;
778 else
780 /* If it is >= that width, result is zero.
781 Handling this with trunc1 would give the wrong result:
782 (int) ((long long) a << 32) is well defined (as 0)
783 but (int) a << 32 is undefined and would get a
784 warning. */
786 tree t = build_int_cst (type, 0);
788 /* If the original expression had side-effects, we must
789 preserve it. */
790 if (TREE_SIDE_EFFECTS (expr))
791 return build2 (COMPOUND_EXPR, type, expr, t);
792 else
793 return t;
796 break;
798 case TRUNC_DIV_EXPR:
800 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), NULL_TREE);
801 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), NULL_TREE);
803 /* Don't distribute unless the output precision is at least as
804 big as the actual inputs and it has the same signedness. */
805 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
806 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
807 /* If signedness of arg0 and arg1 don't match,
808 we can't necessarily find a type to compare them in. */
809 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
810 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
811 /* Do not change the sign of the division. */
812 && (TYPE_UNSIGNED (TREE_TYPE (expr))
813 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
814 /* Either require unsigned division or a division by
815 a constant that is not -1. */
816 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
817 || (TREE_CODE (arg1) == INTEGER_CST
818 && !integer_all_onesp (arg1))))
820 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
821 expr, inprec, outprec, dofold);
822 if (tem)
823 return tem;
825 break;
828 case MAX_EXPR:
829 case MIN_EXPR:
830 case MULT_EXPR:
832 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
833 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
835 /* Don't distribute unless the output precision is at least as
836 big as the actual inputs. Otherwise, the comparison of the
837 truncated values will be wrong. */
838 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
839 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
840 /* If signedness of arg0 and arg1 don't match,
841 we can't necessarily find a type to compare them in. */
842 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
843 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
844 goto trunc1;
845 break;
848 case PLUS_EXPR:
849 case MINUS_EXPR:
850 case BIT_AND_EXPR:
851 case BIT_IOR_EXPR:
852 case BIT_XOR_EXPR:
853 trunc1:
855 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
856 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
858 /* Do not try to narrow operands of pointer subtraction;
859 that will interfere with other folding. */
860 if (ex_form == MINUS_EXPR
861 && CONVERT_EXPR_P (arg0)
862 && CONVERT_EXPR_P (arg1)
863 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
864 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
865 break;
867 if (outprec >= BITS_PER_WORD
868 || TRULY_NOOP_TRUNCATION (outprec, inprec)
869 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
870 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
872 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
873 expr, inprec, outprec, dofold);
874 if (tem)
875 return tem;
878 break;
880 case NEGATE_EXPR:
881 case BIT_NOT_EXPR:
882 /* This is not correct for ABS_EXPR,
883 since we must test the sign before truncation. */
885 /* Do the arithmetic in type TYPEX,
886 then convert result to TYPE. */
887 tree typex = type;
889 /* Can't do arithmetic in enumeral types
890 so use an integer type that will hold the values. */
891 if (TREE_CODE (typex) == ENUMERAL_TYPE)
892 typex
893 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
894 TYPE_UNSIGNED (typex));
896 if (!TYPE_UNSIGNED (typex))
897 typex = unsigned_type_for (typex);
898 return convert (type,
899 fold_build1 (ex_form, typex,
900 convert (typex,
901 TREE_OPERAND (expr, 0))));
904 CASE_CONVERT:
905 /* Don't introduce a "can't convert between vector values of
906 different size" error. */
907 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
908 && (GET_MODE_SIZE (TYPE_MODE
909 (TREE_TYPE (TREE_OPERAND (expr, 0))))
910 != GET_MODE_SIZE (TYPE_MODE (type))))
911 break;
912 /* If truncating after truncating, might as well do all at once.
913 If truncating after extending, we may get rid of wasted work. */
914 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
916 case COND_EXPR:
917 /* It is sometimes worthwhile to push the narrowing down through
918 the conditional and never loses. A COND_EXPR may have a throw
919 as one operand, which then has void type. Just leave void
920 operands as they are. */
921 return
922 fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
923 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
924 ? TREE_OPERAND (expr, 1)
925 : convert (type, TREE_OPERAND (expr, 1)),
926 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
927 ? TREE_OPERAND (expr, 2)
928 : convert (type, TREE_OPERAND (expr, 2)));
930 default:
931 break;
934 /* When parsing long initializers, we might end up with a lot of casts.
935 Shortcut this. */
936 if (TREE_CODE (expr) == INTEGER_CST)
937 return fold_convert (type, expr);
938 return build1 (CONVERT_EXPR, type, expr);
940 case REAL_TYPE:
941 if (sanitize_flags_p (SANITIZE_FLOAT_CAST))
943 expr = save_expr (expr);
944 tree check = ubsan_instrument_float_cast (loc, type, expr);
945 expr = build1 (FIX_TRUNC_EXPR, type, expr);
946 if (check == NULL_TREE)
947 return expr;
948 return maybe_fold_build2_loc (dofold, loc, COMPOUND_EXPR,
949 TREE_TYPE (expr), check, expr);
951 else
952 return build1 (FIX_TRUNC_EXPR, type, expr);
954 case FIXED_POINT_TYPE:
955 return build1 (FIXED_CONVERT_EXPR, type, expr);
957 case COMPLEX_TYPE:
958 expr = maybe_fold_build1_loc (dofold, loc, REALPART_EXPR,
959 TREE_TYPE (TREE_TYPE (expr)), expr);
960 return convert (type, expr);
962 case VECTOR_TYPE:
963 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
965 error ("can%'t convert a vector of type %qT"
966 " to type %qT which has different size",
967 TREE_TYPE (expr), type);
968 return error_mark_node;
970 return build1 (VIEW_CONVERT_EXPR, type, expr);
972 default:
973 error ("aggregate value used where an integer was expected");
974 return convert (type, integer_zero_node);
978 /* Convert EXPR to some integer (or enum) type TYPE.
980 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
981 fixed-point or vector; in other cases error is called.
983 The result of this is always supposed to be a newly created tree node
984 not in use in any existing structure. */
986 tree
987 convert_to_integer (tree type, tree expr)
989 return convert_to_integer_1 (type, expr, true);
992 /* A wrapper around convert_to_complex_1 that only folds the
993 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
995 tree
996 convert_to_integer_maybe_fold (tree type, tree expr, bool dofold)
998 return convert_to_integer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
1001 /* Convert EXPR to the complex type TYPE in the usual ways. If FOLD_P is
1002 true, try to fold the expression. */
1004 static tree
1005 convert_to_complex_1 (tree type, tree expr, bool fold_p)
1007 location_t loc = EXPR_LOCATION (expr);
1008 tree subtype = TREE_TYPE (type);
1010 switch (TREE_CODE (TREE_TYPE (expr)))
1012 case REAL_TYPE:
1013 case FIXED_POINT_TYPE:
1014 case INTEGER_TYPE:
1015 case ENUMERAL_TYPE:
1016 case BOOLEAN_TYPE:
1017 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
1018 convert (subtype, integer_zero_node));
1020 case COMPLEX_TYPE:
1022 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
1024 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
1025 return expr;
1026 else if (TREE_CODE (expr) == COMPOUND_EXPR)
1028 tree t = convert_to_complex_1 (type, TREE_OPERAND (expr, 1),
1029 fold_p);
1030 if (t == TREE_OPERAND (expr, 1))
1031 return expr;
1032 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
1033 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
1035 else if (TREE_CODE (expr) == COMPLEX_EXPR)
1036 return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1037 convert (subtype,
1038 TREE_OPERAND (expr, 0)),
1039 convert (subtype,
1040 TREE_OPERAND (expr, 1)));
1041 else
1043 expr = save_expr (expr);
1044 tree realp = maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
1045 TREE_TYPE (TREE_TYPE (expr)),
1046 expr);
1047 tree imagp = maybe_fold_build1_loc (fold_p, loc, IMAGPART_EXPR,
1048 TREE_TYPE (TREE_TYPE (expr)),
1049 expr);
1050 return maybe_fold_build2_loc (fold_p, loc, COMPLEX_EXPR, type,
1051 convert (subtype, realp),
1052 convert (subtype, imagp));
1056 case POINTER_TYPE:
1057 case REFERENCE_TYPE:
1058 error ("pointer value used where a complex was expected");
1059 return convert_to_complex_1 (type, integer_zero_node, fold_p);
1061 default:
1062 error ("aggregate value used where a complex was expected");
1063 return convert_to_complex_1 (type, integer_zero_node, fold_p);
1067 /* A wrapper around convert_to_complex_1 that always folds the
1068 expression. */
1070 tree
1071 convert_to_complex (tree type, tree expr)
1073 return convert_to_complex_1 (type, expr, true);
1076 /* A wrapper around convert_to_complex_1 that only folds the
1077 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
1079 tree
1080 convert_to_complex_maybe_fold (tree type, tree expr, bool dofold)
1082 return convert_to_complex_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
1085 /* Convert EXPR to the vector type TYPE in the usual ways. */
1087 tree
1088 convert_to_vector (tree type, tree expr)
1090 switch (TREE_CODE (TREE_TYPE (expr)))
1092 case INTEGER_TYPE:
1093 case VECTOR_TYPE:
1094 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
1096 error ("can%'t convert a value of type %qT"
1097 " to vector type %qT which has different size",
1098 TREE_TYPE (expr), type);
1099 return error_mark_node;
1101 return build1 (VIEW_CONVERT_EXPR, type, expr);
1103 default:
1104 error ("can%'t convert value to a vector");
1105 return error_mark_node;
1109 /* Convert EXPR to some fixed-point type TYPE.
1111 EXPR must be fixed-point, float, integer, or enumeral;
1112 in other cases error is called. */
1114 tree
1115 convert_to_fixed (tree type, tree expr)
1117 if (integer_zerop (expr))
1119 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
1120 return fixed_zero_node;
1122 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
1124 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
1125 return fixed_one_node;
1128 switch (TREE_CODE (TREE_TYPE (expr)))
1130 case FIXED_POINT_TYPE:
1131 case INTEGER_TYPE:
1132 case ENUMERAL_TYPE:
1133 case BOOLEAN_TYPE:
1134 case REAL_TYPE:
1135 return build1 (FIXED_CONVERT_EXPR, type, expr);
1137 case COMPLEX_TYPE:
1138 return convert (type,
1139 fold_build1 (REALPART_EXPR,
1140 TREE_TYPE (TREE_TYPE (expr)), expr));
1142 default:
1143 error ("aggregate value used where a fixed-point was expected");
1144 return error_mark_node;