[ARM] Add source mode to coprocessor pattern SETs
[official-gcc.git] / gcc / convert.c
blobaf8dfda0eb44769885d2f06b92aac8b36c665ca5
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"
37 #define maybe_fold_build1_loc(FOLD_P, LOC, CODE, TYPE, EXPR) \
38 ((FOLD_P) ? fold_build1_loc (LOC, CODE, TYPE, EXPR) \
39 : build1_loc (LOC, CODE, TYPE, EXPR))
40 #define maybe_fold_build2_loc(FOLD_P, LOC, CODE, TYPE, EXPR1, EXPR2) \
41 ((FOLD_P) ? fold_build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2) \
42 : build2_loc (LOC, CODE, TYPE, EXPR1, EXPR2))
44 /* Convert EXPR to some pointer or reference type TYPE.
45 EXPR must be pointer, reference, integer, enumeral, or literal zero;
46 in other cases error is called. If FOLD_P is true, try to fold the
47 expression. */
49 static tree
50 convert_to_pointer_1 (tree type, tree expr, bool fold_p)
52 location_t loc = EXPR_LOCATION (expr);
53 if (TREE_TYPE (expr) == type)
54 return expr;
56 switch (TREE_CODE (TREE_TYPE (expr)))
58 case POINTER_TYPE:
59 case REFERENCE_TYPE:
61 /* If the pointers point to different address spaces, conversion needs
62 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
63 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
64 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
66 if (to_as == from_as)
67 return maybe_fold_build1_loc (fold_p, loc, NOP_EXPR, type, expr);
68 else
69 return maybe_fold_build1_loc (fold_p, loc, ADDR_SPACE_CONVERT_EXPR,
70 type, expr);
73 case INTEGER_TYPE:
74 case ENUMERAL_TYPE:
75 case BOOLEAN_TYPE:
77 /* If the input precision differs from the target pointer type
78 precision, first convert the input expression to an integer type of
79 the target precision. Some targets, e.g. VMS, need several pointer
80 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
81 unsigned int pprec = TYPE_PRECISION (type);
82 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
84 if (eprec != pprec)
85 expr
86 = maybe_fold_build1_loc (fold_p, loc, NOP_EXPR,
87 lang_hooks.types.type_for_size (pprec, 0),
88 expr);
90 return maybe_fold_build1_loc (fold_p, loc, CONVERT_EXPR, type, expr);
92 default:
93 error ("cannot convert to a pointer type");
94 return convert_to_pointer_1 (type, integer_zero_node, fold_p);
98 /* A wrapper around convert_to_pointer_1 that always folds the
99 expression. */
101 tree
102 convert_to_pointer (tree type, tree expr)
104 return convert_to_pointer_1 (type, expr, true);
107 /* A wrapper around convert_to_pointer_1 that only folds the
108 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
110 tree
111 convert_to_pointer_maybe_fold (tree type, tree expr, bool dofold)
113 return convert_to_pointer_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
116 /* Convert EXPR to some floating-point type TYPE.
118 EXPR must be float, fixed-point, integer, or enumeral;
119 in other cases error is called. If FOLD_P is true, try to fold
120 the expression. */
122 static tree
123 convert_to_real_1 (tree type, tree expr, bool fold_p)
125 enum built_in_function fcode = builtin_mathfn_code (expr);
126 tree itype = TREE_TYPE (expr);
127 location_t loc = EXPR_LOCATION (expr);
129 if (TREE_CODE (expr) == COMPOUND_EXPR)
131 tree t = convert_to_real_1 (type, TREE_OPERAND (expr, 1), fold_p);
132 if (t == TREE_OPERAND (expr, 1))
133 return expr;
134 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
135 TREE_OPERAND (expr, 0), t);
138 /* Disable until we figure out how to decide whether the functions are
139 present in runtime. */
140 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
141 if (optimize
142 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
143 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
145 switch (fcode)
147 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
148 CASE_MATHFN (COSH)
149 CASE_MATHFN (EXP)
150 CASE_MATHFN (EXP10)
151 CASE_MATHFN (EXP2)
152 CASE_MATHFN (EXPM1)
153 CASE_MATHFN (GAMMA)
154 CASE_MATHFN (J0)
155 CASE_MATHFN (J1)
156 CASE_MATHFN (LGAMMA)
157 CASE_MATHFN (POW10)
158 CASE_MATHFN (SINH)
159 CASE_MATHFN (TGAMMA)
160 CASE_MATHFN (Y0)
161 CASE_MATHFN (Y1)
162 /* The above functions may set errno differently with float
163 input or output so this transformation is not safe with
164 -fmath-errno. */
165 if (flag_errno_math)
166 break;
167 gcc_fallthrough ();
168 CASE_MATHFN (ACOS)
169 CASE_MATHFN (ACOSH)
170 CASE_MATHFN (ASIN)
171 CASE_MATHFN (ASINH)
172 CASE_MATHFN (ATAN)
173 CASE_MATHFN (ATANH)
174 CASE_MATHFN (CBRT)
175 CASE_MATHFN (COS)
176 CASE_MATHFN (ERF)
177 CASE_MATHFN (ERFC)
178 CASE_MATHFN (LOG)
179 CASE_MATHFN (LOG10)
180 CASE_MATHFN (LOG2)
181 CASE_MATHFN (LOG1P)
182 CASE_MATHFN (SIN)
183 CASE_MATHFN (TAN)
184 CASE_MATHFN (TANH)
185 /* The above functions are not safe to do this conversion. */
186 if (!flag_unsafe_math_optimizations)
187 break;
188 gcc_fallthrough ();
189 CASE_MATHFN (SQRT)
190 CASE_MATHFN (FABS)
191 CASE_MATHFN (LOGB)
192 #undef CASE_MATHFN
194 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
195 tree newtype = type;
197 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
198 the both as the safe type for operation. */
199 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
200 newtype = TREE_TYPE (arg0);
202 /* We consider to convert
204 (T1) sqrtT2 ((T2) exprT3)
206 (T1) sqrtT4 ((T4) exprT3)
208 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
209 and T4 is NEWTYPE. All those types are of floating point types.
210 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
211 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
212 T2 and T4. See the following URL for a reference:
213 http://stackoverflow.com/questions/9235456/determining-
214 floating-point-square-root
216 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
217 && !flag_unsafe_math_optimizations)
219 /* The following conversion is unsafe even the precision condition
220 below is satisfied:
222 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
224 if (TYPE_MODE (type) != TYPE_MODE (newtype))
225 break;
227 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
228 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
229 if (p1 < p2 * 2 + 2)
230 break;
233 /* Be careful about integer to fp conversions.
234 These may overflow still. */
235 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
236 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
237 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
238 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
240 tree fn = mathfn_built_in (newtype, fcode);
241 if (fn)
243 tree arg = convert_to_real_1 (newtype, arg0, fold_p);
244 expr = build_call_expr (fn, 1, arg);
245 if (newtype == type)
246 return expr;
250 default:
251 break;
255 /* Propagate the cast into the operation. */
256 if (itype != type && FLOAT_TYPE_P (type))
257 switch (TREE_CODE (expr))
259 /* Convert (float)-x into -(float)x. This is safe for
260 round-to-nearest rounding mode when the inner type is float. */
261 case ABS_EXPR:
262 case NEGATE_EXPR:
263 if (!flag_rounding_math
264 && FLOAT_TYPE_P (itype)
265 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
267 tree arg = convert_to_real_1 (type, TREE_OPERAND (expr, 0),
268 fold_p);
269 return build1 (TREE_CODE (expr), type, arg);
271 break;
272 /* Convert (outertype)((innertype0)a+(innertype1)b)
273 into ((newtype)a+(newtype)b) where newtype
274 is the widest mode from all of these. */
275 case PLUS_EXPR:
276 case MINUS_EXPR:
277 case MULT_EXPR:
278 case RDIV_EXPR:
280 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
281 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
283 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
284 && FLOAT_TYPE_P (TREE_TYPE (arg1))
285 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
287 tree newtype = type;
289 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
290 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
291 || TYPE_MODE (type) == SDmode)
292 newtype = dfloat32_type_node;
293 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
294 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
295 || TYPE_MODE (type) == DDmode)
296 newtype = dfloat64_type_node;
297 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
298 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
299 || TYPE_MODE (type) == TDmode)
300 newtype = dfloat128_type_node;
301 if (newtype == dfloat32_type_node
302 || newtype == dfloat64_type_node
303 || newtype == dfloat128_type_node)
305 expr = build2 (TREE_CODE (expr), newtype,
306 convert_to_real_1 (newtype, arg0,
307 fold_p),
308 convert_to_real_1 (newtype, arg1,
309 fold_p));
310 if (newtype == type)
311 return expr;
312 break;
315 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
316 newtype = TREE_TYPE (arg0);
317 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
318 newtype = TREE_TYPE (arg1);
319 /* Sometimes this transformation is safe (cannot
320 change results through affecting double rounding
321 cases) and sometimes it is not. If NEWTYPE is
322 wider than TYPE, e.g. (float)((long double)double
323 + (long double)double) converted to
324 (float)(double + double), the transformation is
325 unsafe regardless of the details of the types
326 involved; double rounding can arise if the result
327 of NEWTYPE arithmetic is a NEWTYPE value half way
328 between two representable TYPE values but the
329 exact value is sufficiently different (in the
330 right direction) for this difference to be
331 visible in ITYPE arithmetic. If NEWTYPE is the
332 same as TYPE, however, the transformation may be
333 safe depending on the types involved: it is safe
334 if the ITYPE has strictly more than twice as many
335 mantissa bits as TYPE, can represent infinities
336 and NaNs if the TYPE can, and has sufficient
337 exponent range for the product or ratio of two
338 values representable in the TYPE to be within the
339 range of normal values of ITYPE. */
340 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
341 && (flag_unsafe_math_optimizations
342 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
343 && real_can_shorten_arithmetic (TYPE_MODE (itype),
344 TYPE_MODE (type))
345 && !excess_precision_type (newtype))))
347 expr = build2 (TREE_CODE (expr), newtype,
348 convert_to_real_1 (newtype, arg0,
349 fold_p),
350 convert_to_real_1 (newtype, arg1,
351 fold_p));
352 if (newtype == type)
353 return expr;
357 break;
358 default:
359 break;
362 switch (TREE_CODE (TREE_TYPE (expr)))
364 case REAL_TYPE:
365 /* Ignore the conversion if we don't need to store intermediate
366 results and neither type is a decimal float. */
367 return build1_loc (loc,
368 (flag_float_store
369 || DECIMAL_FLOAT_TYPE_P (type)
370 || DECIMAL_FLOAT_TYPE_P (itype))
371 ? CONVERT_EXPR : NOP_EXPR, type, expr);
373 case INTEGER_TYPE:
374 case ENUMERAL_TYPE:
375 case BOOLEAN_TYPE:
376 return build1 (FLOAT_EXPR, type, expr);
378 case FIXED_POINT_TYPE:
379 return build1 (FIXED_CONVERT_EXPR, type, expr);
381 case COMPLEX_TYPE:
382 return convert (type,
383 maybe_fold_build1_loc (fold_p, loc, REALPART_EXPR,
384 TREE_TYPE (TREE_TYPE (expr)),
385 expr));
387 case POINTER_TYPE:
388 case REFERENCE_TYPE:
389 error ("pointer value used where a floating point value was expected");
390 return convert_to_real_1 (type, integer_zero_node, fold_p);
392 default:
393 error ("aggregate value used where a float was expected");
394 return convert_to_real_1 (type, integer_zero_node, fold_p);
398 /* A wrapper around convert_to_real_1 that always folds the
399 expression. */
401 tree
402 convert_to_real (tree type, tree expr)
404 return convert_to_real_1 (type, expr, true);
407 /* A wrapper around convert_to_real_1 that only folds the
408 expression if DOFOLD, or if it is CONSTANT_CLASS_P. */
410 tree
411 convert_to_real_maybe_fold (tree type, tree expr, bool dofold)
413 return convert_to_real_1 (type, expr, dofold || CONSTANT_CLASS_P (expr));
416 /* Try to narrow EX_FORM ARG0 ARG1 in narrowed arg types producing a
417 result in TYPE. */
419 static tree
420 do_narrow (location_t loc,
421 enum tree_code ex_form, tree type, tree arg0, tree arg1,
422 tree expr, unsigned inprec, unsigned outprec, bool dofold)
424 /* Do the arithmetic in type TYPEX,
425 then convert result to TYPE. */
426 tree typex = type;
428 /* Can't do arithmetic in enumeral types
429 so use an integer type that will hold the values. */
430 if (TREE_CODE (typex) == ENUMERAL_TYPE)
431 typex = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
432 TYPE_UNSIGNED (typex));
434 /* But now perhaps TYPEX is as wide as INPREC.
435 In that case, do nothing special here.
436 (Otherwise would recurse infinitely in convert. */
437 if (TYPE_PRECISION (typex) != inprec)
439 /* Don't do unsigned arithmetic where signed was wanted,
440 or vice versa.
441 Exception: if both of the original operands were
442 unsigned then we can safely do the work as unsigned.
443 Exception: shift operations take their type solely
444 from the first argument.
445 Exception: the LSHIFT_EXPR case above requires that
446 we perform this operation unsigned lest we produce
447 signed-overflow undefinedness.
448 And we may need to do it as unsigned
449 if we truncate to the original size. */
450 if (TYPE_UNSIGNED (TREE_TYPE (expr))
451 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
452 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
453 || ex_form == LSHIFT_EXPR
454 || ex_form == RSHIFT_EXPR
455 || ex_form == LROTATE_EXPR
456 || ex_form == RROTATE_EXPR))
457 || ex_form == LSHIFT_EXPR
458 /* If we have !flag_wrapv, and either ARG0 or
459 ARG1 is of a signed type, we have to do
460 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
461 type in case the operation in outprec precision
462 could overflow. Otherwise, we would introduce
463 signed-overflow undefinedness. */
464 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
465 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
466 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
467 > outprec)
468 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
469 > outprec))
470 && (ex_form == PLUS_EXPR
471 || ex_form == MINUS_EXPR
472 || ex_form == MULT_EXPR)))
474 if (!TYPE_UNSIGNED (typex))
475 typex = unsigned_type_for (typex);
477 else
479 if (TYPE_UNSIGNED (typex))
480 typex = signed_type_for (typex);
482 /* We should do away with all this once we have a proper
483 type promotion/demotion pass, see PR45397. */
484 expr = maybe_fold_build2_loc (dofold, loc, ex_form, typex,
485 convert (typex, arg0),
486 convert (typex, arg1));
487 return convert (type, expr);
490 return NULL_TREE;
493 /* Convert EXPR to some integer (or enum) type TYPE.
495 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
496 fixed-point or vector; in other cases error is called.
498 If DOFOLD is TRUE, we try to simplify newly-created patterns by folding.
500 The result of this is always supposed to be a newly created tree node
501 not in use in any existing structure. */
503 static tree
504 convert_to_integer_1 (tree type, tree expr, bool dofold)
506 enum tree_code ex_form = TREE_CODE (expr);
507 tree intype = TREE_TYPE (expr);
508 unsigned int inprec = element_precision (intype);
509 unsigned int outprec = element_precision (type);
510 location_t loc = EXPR_LOCATION (expr);
512 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
513 be. Consider `enum E = { a, b = (enum E) 3 };'. */
514 if (!COMPLETE_TYPE_P (type))
516 error ("conversion to incomplete type");
517 return error_mark_node;
520 if (ex_form == COMPOUND_EXPR)
522 tree t = convert_to_integer_1 (type, TREE_OPERAND (expr, 1), dofold);
523 if (t == TREE_OPERAND (expr, 1))
524 return expr;
525 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
526 TREE_OPERAND (expr, 0), t);
529 /* Convert e.g. (long)round(d) -> lround(d). */
530 /* If we're converting to char, we may encounter differing behavior
531 between converting from double->char vs double->long->char.
532 We're in "undefined" territory but we prefer to be conservative,
533 so only proceed in "unsafe" math mode. */
534 if (optimize
535 && (flag_unsafe_math_optimizations
536 || (long_integer_type_node
537 && outprec >= TYPE_PRECISION (long_integer_type_node))))
539 tree s_expr = strip_float_extensions (expr);
540 tree s_intype = TREE_TYPE (s_expr);
541 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
542 tree fn = 0;
544 switch (fcode)
546 CASE_FLT_FN (BUILT_IN_CEIL):
547 /* Only convert in ISO C99 mode. */
548 if (!targetm.libc_has_function (function_c99_misc))
549 break;
550 if (outprec < TYPE_PRECISION (integer_type_node)
551 || (outprec == TYPE_PRECISION (integer_type_node)
552 && !TYPE_UNSIGNED (type)))
553 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
554 else if (outprec == TYPE_PRECISION (long_integer_type_node)
555 && !TYPE_UNSIGNED (type))
556 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
557 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
558 && !TYPE_UNSIGNED (type))
559 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
560 break;
562 CASE_FLT_FN (BUILT_IN_FLOOR):
563 /* Only convert in ISO C99 mode. */
564 if (!targetm.libc_has_function (function_c99_misc))
565 break;
566 if (outprec < TYPE_PRECISION (integer_type_node)
567 || (outprec == TYPE_PRECISION (integer_type_node)
568 && !TYPE_UNSIGNED (type)))
569 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
570 else if (outprec == TYPE_PRECISION (long_integer_type_node)
571 && !TYPE_UNSIGNED (type))
572 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
573 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
574 && !TYPE_UNSIGNED (type))
575 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
576 break;
578 CASE_FLT_FN (BUILT_IN_ROUND):
579 /* Only convert in ISO C99 mode and with -fno-math-errno. */
580 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
581 break;
582 if (outprec < TYPE_PRECISION (integer_type_node)
583 || (outprec == TYPE_PRECISION (integer_type_node)
584 && !TYPE_UNSIGNED (type)))
585 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
586 else if (outprec == TYPE_PRECISION (long_integer_type_node)
587 && !TYPE_UNSIGNED (type))
588 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
589 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
590 && !TYPE_UNSIGNED (type))
591 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
592 break;
594 CASE_FLT_FN (BUILT_IN_NEARBYINT):
595 /* Only convert nearbyint* if we can ignore math exceptions. */
596 if (flag_trapping_math)
597 break;
598 gcc_fallthrough ();
599 CASE_FLT_FN (BUILT_IN_RINT):
600 /* Only convert in ISO C99 mode and with -fno-math-errno. */
601 if (!targetm.libc_has_function (function_c99_misc) || flag_errno_math)
602 break;
603 if (outprec < TYPE_PRECISION (integer_type_node)
604 || (outprec == TYPE_PRECISION (integer_type_node)
605 && !TYPE_UNSIGNED (type)))
606 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
607 else if (outprec == TYPE_PRECISION (long_integer_type_node)
608 && !TYPE_UNSIGNED (type))
609 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
610 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
611 && !TYPE_UNSIGNED (type))
612 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
613 break;
615 CASE_FLT_FN (BUILT_IN_TRUNC):
616 return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0), dofold);
618 default:
619 break;
622 if (fn)
624 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
625 return convert_to_integer_1 (type, newexpr, dofold);
629 /* Convert (int)logb(d) -> ilogb(d). */
630 if (optimize
631 && flag_unsafe_math_optimizations
632 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
633 && integer_type_node
634 && (outprec > TYPE_PRECISION (integer_type_node)
635 || (outprec == TYPE_PRECISION (integer_type_node)
636 && !TYPE_UNSIGNED (type))))
638 tree s_expr = strip_float_extensions (expr);
639 tree s_intype = TREE_TYPE (s_expr);
640 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
641 tree fn = 0;
643 switch (fcode)
645 CASE_FLT_FN (BUILT_IN_LOGB):
646 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
647 break;
649 default:
650 break;
653 if (fn)
655 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
656 return convert_to_integer_1 (type, newexpr, dofold);
660 switch (TREE_CODE (intype))
662 case POINTER_TYPE:
663 case REFERENCE_TYPE:
664 if (integer_zerop (expr))
665 return build_int_cst (type, 0);
667 /* Convert to an unsigned integer of the correct width first, and from
668 there widen/truncate to the required type. Some targets support the
669 coexistence of multiple valid pointer sizes, so fetch the one we need
670 from the type. */
671 if (!dofold)
672 return build1 (CONVERT_EXPR, type, expr);
673 expr = fold_build1 (CONVERT_EXPR,
674 lang_hooks.types.type_for_size
675 (TYPE_PRECISION (intype), 0),
676 expr);
677 return fold_convert (type, expr);
679 case INTEGER_TYPE:
680 case ENUMERAL_TYPE:
681 case BOOLEAN_TYPE:
682 case OFFSET_TYPE:
683 /* If this is a logical operation, which just returns 0 or 1, we can
684 change the type of the expression. */
686 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
688 expr = copy_node (expr);
689 TREE_TYPE (expr) = type;
690 return expr;
693 /* If we are widening the type, put in an explicit conversion.
694 Similarly if we are not changing the width. After this, we know
695 we are truncating EXPR. */
697 else if (outprec >= inprec)
699 enum tree_code code;
701 /* If the precision of the EXPR's type is K bits and the
702 destination mode has more bits, and the sign is changing,
703 it is not safe to use a NOP_EXPR. For example, suppose
704 that EXPR's type is a 3-bit unsigned integer type, the
705 TYPE is a 3-bit signed integer type, and the machine mode
706 for the types is 8-bit QImode. In that case, the
707 conversion necessitates an explicit sign-extension. In
708 the signed-to-unsigned case the high-order bits have to
709 be cleared. */
710 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
711 && (TYPE_PRECISION (TREE_TYPE (expr))
712 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
713 code = CONVERT_EXPR;
714 else
715 code = NOP_EXPR;
717 return maybe_fold_build1_loc (dofold, loc, code, type, expr);
720 /* If TYPE is an enumeral type or a type with a precision less
721 than the number of bits in its mode, do the conversion to the
722 type corresponding to its mode, then do a nop conversion
723 to TYPE. */
724 else if (TREE_CODE (type) == ENUMERAL_TYPE
725 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
727 expr = convert (lang_hooks.types.type_for_mode
728 (TYPE_MODE (type), TYPE_UNSIGNED (type)), expr);
729 return maybe_fold_build1_loc (dofold, loc, NOP_EXPR, type, expr);
732 /* Here detect when we can distribute the truncation down past some
733 arithmetic. For example, if adding two longs and converting to an
734 int, we can equally well convert both to ints and then add.
735 For the operations handled here, such truncation distribution
736 is always safe.
737 It is desirable in these cases:
738 1) when truncating down to full-word from a larger size
739 2) when truncating takes no work.
740 3) when at least one operand of the arithmetic has been extended
741 (as by C's default conversions). In this case we need two conversions
742 if we do the arithmetic as already requested, so we might as well
743 truncate both and then combine. Perhaps that way we need only one.
745 Note that in general we cannot do the arithmetic in a type
746 shorter than the desired result of conversion, even if the operands
747 are both extended from a shorter type, because they might overflow
748 if combined in that type. The exceptions to this--the times when
749 two narrow values can be combined in their narrow type even to
750 make a wider result--are handled by "shorten" in build_binary_op. */
752 if (dofold)
753 switch (ex_form)
755 case RSHIFT_EXPR:
756 /* We can pass truncation down through right shifting
757 when the shift count is a nonpositive constant. */
758 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
759 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
760 goto trunc1;
761 break;
763 case LSHIFT_EXPR:
764 /* We can pass truncation down through left shifting
765 when the shift count is a nonnegative constant and
766 the target type is unsigned. */
767 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
768 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
769 && TYPE_UNSIGNED (type)
770 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
772 /* If shift count is less than the width of the truncated type,
773 really shift. */
774 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
775 /* In this case, shifting is like multiplication. */
776 goto trunc1;
777 else
779 /* If it is >= that width, result is zero.
780 Handling this with trunc1 would give the wrong result:
781 (int) ((long long) a << 32) is well defined (as 0)
782 but (int) a << 32 is undefined and would get a
783 warning. */
785 tree t = build_int_cst (type, 0);
787 /* If the original expression had side-effects, we must
788 preserve it. */
789 if (TREE_SIDE_EFFECTS (expr))
790 return build2 (COMPOUND_EXPR, type, expr, t);
791 else
792 return t;
795 break;
797 case TRUNC_DIV_EXPR:
799 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), NULL_TREE);
800 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), NULL_TREE);
802 /* Don't distribute unless the output precision is at least as
803 big as the actual inputs and it has the same signedness. */
804 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
805 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
806 /* If signedness of arg0 and arg1 don't match,
807 we can't necessarily find a type to compare them in. */
808 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
809 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
810 /* Do not change the sign of the division. */
811 && (TYPE_UNSIGNED (TREE_TYPE (expr))
812 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
813 /* Either require unsigned division or a division by
814 a constant that is not -1. */
815 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
816 || (TREE_CODE (arg1) == INTEGER_CST
817 && !integer_all_onesp (arg1))))
819 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
820 expr, inprec, outprec, dofold);
821 if (tem)
822 return tem;
824 break;
827 case MAX_EXPR:
828 case MIN_EXPR:
829 case MULT_EXPR:
831 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
832 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
834 /* Don't distribute unless the output precision is at least as
835 big as the actual inputs. Otherwise, the comparison of the
836 truncated values will be wrong. */
837 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
838 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
839 /* If signedness of arg0 and arg1 don't match,
840 we can't necessarily find a type to compare them in. */
841 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
842 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
843 goto trunc1;
844 break;
847 case PLUS_EXPR:
848 case MINUS_EXPR:
849 case BIT_AND_EXPR:
850 case BIT_IOR_EXPR:
851 case BIT_XOR_EXPR:
852 trunc1:
854 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
855 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
857 /* Do not try to narrow operands of pointer subtraction;
858 that will interfere with other folding. */
859 if (ex_form == MINUS_EXPR
860 && CONVERT_EXPR_P (arg0)
861 && CONVERT_EXPR_P (arg1)
862 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
863 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
864 break;
866 if (outprec >= BITS_PER_WORD
867 || TRULY_NOOP_TRUNCATION (outprec, inprec)
868 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
869 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
871 tree tem = do_narrow (loc, ex_form, type, arg0, arg1,
872 expr, inprec, outprec, dofold);
873 if (tem)
874 return tem;
877 break;
879 case NEGATE_EXPR:
880 case BIT_NOT_EXPR:
881 /* This is not correct for ABS_EXPR,
882 since we must test the sign before truncation. */
884 /* Do the arithmetic in type TYPEX,
885 then convert result to TYPE. */
886 tree typex = type;
888 /* Can't do arithmetic in enumeral types
889 so use an integer type that will hold the values. */
890 if (TREE_CODE (typex) == ENUMERAL_TYPE)
891 typex
892 = lang_hooks.types.type_for_size (TYPE_PRECISION (typex),
893 TYPE_UNSIGNED (typex));
895 if (!TYPE_UNSIGNED (typex))
896 typex = unsigned_type_for (typex);
897 return convert (type,
898 fold_build1 (ex_form, typex,
899 convert (typex,
900 TREE_OPERAND (expr, 0))));
903 CASE_CONVERT:
904 /* Don't introduce a "can't convert between vector values of
905 different size" error. */
906 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
907 && (GET_MODE_SIZE (TYPE_MODE
908 (TREE_TYPE (TREE_OPERAND (expr, 0))))
909 != GET_MODE_SIZE (TYPE_MODE (type))))
910 break;
911 /* If truncating after truncating, might as well do all at once.
912 If truncating after extending, we may get rid of wasted work. */
913 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
915 case COND_EXPR:
916 /* It is sometimes worthwhile to push the narrowing down through
917 the conditional and never loses. A COND_EXPR may have a throw
918 as one operand, which then has void type. Just leave void
919 operands as they are. */
920 return
921 fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
922 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
923 ? TREE_OPERAND (expr, 1)
924 : convert (type, TREE_OPERAND (expr, 1)),
925 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
926 ? TREE_OPERAND (expr, 2)
927 : convert (type, TREE_OPERAND (expr, 2)));
929 default:
930 break;
933 /* When parsing long initializers, we might end up with a lot of casts.
934 Shortcut this. */
935 if (TREE_CODE (expr) == INTEGER_CST)
936 return fold_convert (type, expr);
937 return build1 (CONVERT_EXPR, type, expr);
939 case REAL_TYPE:
940 if (flag_sanitize & SANITIZE_FLOAT_CAST
941 && do_ubsan_in_current_function ())
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;