gcc-defs.exp (dg-additional-files-options): Extend regsub for dg-additional-files...
[official-gcc.git] / gcc / convert.c
bloba2f2a334dbf85ed0335f834c76ae1990ac3df740
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2013 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 "flags.h"
30 #include "convert.h"
31 #include "diagnostic-core.h"
32 #include "target.h"
33 #include "langhooks.h"
35 /* Convert EXPR to some pointer or reference type TYPE.
36 EXPR must be pointer, reference, integer, enumeral, or literal zero;
37 in other cases error is called. */
39 tree
40 convert_to_pointer (tree type, tree expr)
42 location_t loc = EXPR_LOCATION (expr);
43 if (TREE_TYPE (expr) == type)
44 return expr;
46 switch (TREE_CODE (TREE_TYPE (expr)))
48 case POINTER_TYPE:
49 case REFERENCE_TYPE:
51 /* If the pointers point to different address spaces, conversion needs
52 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
53 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
54 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
56 if (to_as == from_as)
57 return fold_build1_loc (loc, NOP_EXPR, type, expr);
58 else
59 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
62 case INTEGER_TYPE:
63 case ENUMERAL_TYPE:
64 case BOOLEAN_TYPE:
66 /* If the input precision differs from the target pointer type
67 precision, first convert the input expression to an integer type of
68 the target precision. Some targets, e.g. VMS, need several pointer
69 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
70 unsigned int pprec = TYPE_PRECISION (type);
71 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
73 if (eprec != pprec)
74 expr = fold_build1_loc (loc, NOP_EXPR,
75 lang_hooks.types.type_for_size (pprec, 0),
76 expr);
79 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
81 default:
82 error ("cannot convert to a pointer type");
83 return convert_to_pointer (type, integer_zero_node);
88 /* Convert EXPR to some floating-point type TYPE.
90 EXPR must be float, fixed-point, integer, or enumeral;
91 in other cases error is called. */
93 tree
94 convert_to_real (tree type, tree expr)
96 enum built_in_function fcode = builtin_mathfn_code (expr);
97 tree itype = TREE_TYPE (expr);
99 /* Disable until we figure out how to decide whether the functions are
100 present in runtime. */
101 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
102 if (optimize
103 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
104 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
106 switch (fcode)
108 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
109 CASE_MATHFN (COSH)
110 CASE_MATHFN (EXP)
111 CASE_MATHFN (EXP10)
112 CASE_MATHFN (EXP2)
113 CASE_MATHFN (EXPM1)
114 CASE_MATHFN (GAMMA)
115 CASE_MATHFN (J0)
116 CASE_MATHFN (J1)
117 CASE_MATHFN (LGAMMA)
118 CASE_MATHFN (POW10)
119 CASE_MATHFN (SINH)
120 CASE_MATHFN (TGAMMA)
121 CASE_MATHFN (Y0)
122 CASE_MATHFN (Y1)
123 /* The above functions may set errno differently with float
124 input or output so this transformation is not safe with
125 -fmath-errno. */
126 if (flag_errno_math)
127 break;
128 CASE_MATHFN (ACOS)
129 CASE_MATHFN (ACOSH)
130 CASE_MATHFN (ASIN)
131 CASE_MATHFN (ASINH)
132 CASE_MATHFN (ATAN)
133 CASE_MATHFN (ATANH)
134 CASE_MATHFN (CBRT)
135 CASE_MATHFN (COS)
136 CASE_MATHFN (ERF)
137 CASE_MATHFN (ERFC)
138 CASE_MATHFN (LOG)
139 CASE_MATHFN (LOG10)
140 CASE_MATHFN (LOG2)
141 CASE_MATHFN (LOG1P)
142 CASE_MATHFN (SIN)
143 CASE_MATHFN (TAN)
144 CASE_MATHFN (TANH)
145 /* The above functions are not safe to do this conversion. */
146 if (!flag_unsafe_math_optimizations)
147 break;
148 CASE_MATHFN (SQRT)
149 CASE_MATHFN (FABS)
150 CASE_MATHFN (LOGB)
151 #undef CASE_MATHFN
153 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
154 tree newtype = type;
156 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
157 the both as the safe type for operation. */
158 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
159 newtype = TREE_TYPE (arg0);
161 /* We consider to convert
163 (T1) sqrtT2 ((T2) exprT3)
165 (T1) sqrtT4 ((T4) exprT3)
167 , where T1 is TYPE, T2 is ITYPE, T3 is TREE_TYPE (ARG0),
168 and T4 is NEWTYPE. All those types are of floating point types.
169 T4 (NEWTYPE) should be narrower than T2 (ITYPE). This conversion
170 is safe only if P1 >= P2*2+2, where P1 and P2 are precisions of
171 T2 and T4. See the following URL for a reference:
172 http://stackoverflow.com/questions/9235456/determining-
173 floating-point-square-root
175 if ((fcode == BUILT_IN_SQRT || fcode == BUILT_IN_SQRTL)
176 && !flag_unsafe_math_optimizations)
178 /* The following conversion is unsafe even the precision condition
179 below is satisfied:
181 (float) sqrtl ((long double) double_val) -> (float) sqrt (double_val)
183 if (TYPE_MODE (type) != TYPE_MODE (newtype))
184 break;
186 int p1 = REAL_MODE_FORMAT (TYPE_MODE (itype))->p;
187 int p2 = REAL_MODE_FORMAT (TYPE_MODE (newtype))->p;
188 if (p1 < p2 * 2 + 2)
189 break;
192 /* Be careful about integer to fp conversions.
193 These may overflow still. */
194 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
195 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
196 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
197 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
199 tree fn = mathfn_built_in (newtype, fcode);
201 if (fn)
203 tree arg = fold (convert_to_real (newtype, arg0));
204 expr = build_call_expr (fn, 1, arg);
205 if (newtype == type)
206 return expr;
210 default:
211 break;
214 if (optimize
215 && (((fcode == BUILT_IN_FLOORL
216 || fcode == BUILT_IN_CEILL
217 || fcode == BUILT_IN_ROUNDL
218 || fcode == BUILT_IN_RINTL
219 || fcode == BUILT_IN_TRUNCL
220 || fcode == BUILT_IN_NEARBYINTL)
221 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
222 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
223 || ((fcode == BUILT_IN_FLOOR
224 || fcode == BUILT_IN_CEIL
225 || fcode == BUILT_IN_ROUND
226 || fcode == BUILT_IN_RINT
227 || fcode == BUILT_IN_TRUNC
228 || fcode == BUILT_IN_NEARBYINT)
229 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
231 tree fn = mathfn_built_in (type, fcode);
233 if (fn)
235 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
237 /* Make sure (type)arg0 is an extension, otherwise we could end up
238 changing (float)floor(double d) into floorf((float)d), which is
239 incorrect because (float)d uses round-to-nearest and can round
240 up to the next integer. */
241 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
242 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
246 /* Propagate the cast into the operation. */
247 if (itype != type && FLOAT_TYPE_P (type))
248 switch (TREE_CODE (expr))
250 /* Convert (float)-x into -(float)x. This is safe for
251 round-to-nearest rounding mode when the inner type is float. */
252 case ABS_EXPR:
253 case NEGATE_EXPR:
254 if (!flag_rounding_math
255 && FLOAT_TYPE_P (itype)
256 && TYPE_PRECISION (type) < TYPE_PRECISION (itype))
257 return build1 (TREE_CODE (expr), type,
258 fold (convert_to_real (type,
259 TREE_OPERAND (expr, 0))));
260 break;
261 /* Convert (outertype)((innertype0)a+(innertype1)b)
262 into ((newtype)a+(newtype)b) where newtype
263 is the widest mode from all of these. */
264 case PLUS_EXPR:
265 case MINUS_EXPR:
266 case MULT_EXPR:
267 case RDIV_EXPR:
269 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
270 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
272 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
273 && FLOAT_TYPE_P (TREE_TYPE (arg1))
274 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
276 tree newtype = type;
278 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
279 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
280 || TYPE_MODE (type) == SDmode)
281 newtype = dfloat32_type_node;
282 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
283 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
284 || TYPE_MODE (type) == DDmode)
285 newtype = dfloat64_type_node;
286 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
287 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
288 || TYPE_MODE (type) == TDmode)
289 newtype = dfloat128_type_node;
290 if (newtype == dfloat32_type_node
291 || newtype == dfloat64_type_node
292 || newtype == dfloat128_type_node)
294 expr = build2 (TREE_CODE (expr), newtype,
295 fold (convert_to_real (newtype, arg0)),
296 fold (convert_to_real (newtype, arg1)));
297 if (newtype == type)
298 return expr;
299 break;
302 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
303 newtype = TREE_TYPE (arg0);
304 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
305 newtype = TREE_TYPE (arg1);
306 /* Sometimes this transformation is safe (cannot
307 change results through affecting double rounding
308 cases) and sometimes it is not. If NEWTYPE is
309 wider than TYPE, e.g. (float)((long double)double
310 + (long double)double) converted to
311 (float)(double + double), the transformation is
312 unsafe regardless of the details of the types
313 involved; double rounding can arise if the result
314 of NEWTYPE arithmetic is a NEWTYPE value half way
315 between two representable TYPE values but the
316 exact value is sufficiently different (in the
317 right direction) for this difference to be
318 visible in ITYPE arithmetic. If NEWTYPE is the
319 same as TYPE, however, the transformation may be
320 safe depending on the types involved: it is safe
321 if the ITYPE has strictly more than twice as many
322 mantissa bits as TYPE, can represent infinities
323 and NaNs if the TYPE can, and has sufficient
324 exponent range for the product or ratio of two
325 values representable in the TYPE to be within the
326 range of normal values of ITYPE. */
327 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
328 && (flag_unsafe_math_optimizations
329 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
330 && real_can_shorten_arithmetic (TYPE_MODE (itype),
331 TYPE_MODE (type))
332 && !excess_precision_type (newtype))))
334 expr = build2 (TREE_CODE (expr), newtype,
335 fold (convert_to_real (newtype, arg0)),
336 fold (convert_to_real (newtype, arg1)));
337 if (newtype == type)
338 return expr;
342 break;
343 default:
344 break;
347 switch (TREE_CODE (TREE_TYPE (expr)))
349 case REAL_TYPE:
350 /* Ignore the conversion if we don't need to store intermediate
351 results and neither type is a decimal float. */
352 return build1 ((flag_float_store
353 || DECIMAL_FLOAT_TYPE_P (type)
354 || DECIMAL_FLOAT_TYPE_P (itype))
355 ? CONVERT_EXPR : NOP_EXPR, type, expr);
357 case INTEGER_TYPE:
358 case ENUMERAL_TYPE:
359 case BOOLEAN_TYPE:
360 return build1 (FLOAT_EXPR, type, expr);
362 case FIXED_POINT_TYPE:
363 return build1 (FIXED_CONVERT_EXPR, type, expr);
365 case COMPLEX_TYPE:
366 return convert (type,
367 fold_build1 (REALPART_EXPR,
368 TREE_TYPE (TREE_TYPE (expr)), expr));
370 case POINTER_TYPE:
371 case REFERENCE_TYPE:
372 error ("pointer value used where a floating point value was expected");
373 return convert_to_real (type, integer_zero_node);
375 default:
376 error ("aggregate value used where a float was expected");
377 return convert_to_real (type, integer_zero_node);
381 /* Convert EXPR to some integer (or enum) type TYPE.
383 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
384 fixed-point or vector; in other cases error is called.
386 The result of this is always supposed to be a newly created tree node
387 not in use in any existing structure. */
389 tree
390 convert_to_integer (tree type, tree expr)
392 enum tree_code ex_form = TREE_CODE (expr);
393 tree intype = TREE_TYPE (expr);
394 unsigned int inprec = element_precision (intype);
395 unsigned int outprec = element_precision (type);
397 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
398 be. Consider `enum E = { a, b = (enum E) 3 };'. */
399 if (!COMPLETE_TYPE_P (type))
401 error ("conversion to incomplete type");
402 return error_mark_node;
405 /* Convert e.g. (long)round(d) -> lround(d). */
406 /* If we're converting to char, we may encounter differing behavior
407 between converting from double->char vs double->long->char.
408 We're in "undefined" territory but we prefer to be conservative,
409 so only proceed in "unsafe" math mode. */
410 if (optimize
411 && (flag_unsafe_math_optimizations
412 || (long_integer_type_node
413 && outprec >= TYPE_PRECISION (long_integer_type_node))))
415 tree s_expr = strip_float_extensions (expr);
416 tree s_intype = TREE_TYPE (s_expr);
417 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
418 tree fn = 0;
420 switch (fcode)
422 CASE_FLT_FN (BUILT_IN_CEIL):
423 /* Only convert in ISO C99 mode. */
424 if (!targetm.libc_has_function (function_c99_misc))
425 break;
426 if (outprec < TYPE_PRECISION (integer_type_node)
427 || (outprec == TYPE_PRECISION (integer_type_node)
428 && !TYPE_UNSIGNED (type)))
429 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
430 else if (outprec == TYPE_PRECISION (long_integer_type_node)
431 && !TYPE_UNSIGNED (type))
432 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
433 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
434 && !TYPE_UNSIGNED (type))
435 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
436 break;
438 CASE_FLT_FN (BUILT_IN_FLOOR):
439 /* Only convert in ISO C99 mode. */
440 if (!targetm.libc_has_function (function_c99_misc))
441 break;
442 if (outprec < TYPE_PRECISION (integer_type_node)
443 || (outprec == TYPE_PRECISION (integer_type_node)
444 && !TYPE_UNSIGNED (type)))
445 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
446 else if (outprec == TYPE_PRECISION (long_integer_type_node)
447 && !TYPE_UNSIGNED (type))
448 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
449 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
450 && !TYPE_UNSIGNED (type))
451 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
452 break;
454 CASE_FLT_FN (BUILT_IN_ROUND):
455 /* Only convert in ISO C99 mode. */
456 if (!targetm.libc_has_function (function_c99_misc))
457 break;
458 if (outprec < TYPE_PRECISION (integer_type_node)
459 || (outprec == TYPE_PRECISION (integer_type_node)
460 && !TYPE_UNSIGNED (type)))
461 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
462 else if (outprec == TYPE_PRECISION (long_integer_type_node)
463 && !TYPE_UNSIGNED (type))
464 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
465 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
466 && !TYPE_UNSIGNED (type))
467 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
468 break;
470 CASE_FLT_FN (BUILT_IN_NEARBYINT):
471 /* Only convert nearbyint* if we can ignore math exceptions. */
472 if (flag_trapping_math)
473 break;
474 /* ... Fall through ... */
475 CASE_FLT_FN (BUILT_IN_RINT):
476 /* Only convert in ISO C99 mode. */
477 if (!targetm.libc_has_function (function_c99_misc))
478 break;
479 if (outprec < TYPE_PRECISION (integer_type_node)
480 || (outprec == TYPE_PRECISION (integer_type_node)
481 && !TYPE_UNSIGNED (type)))
482 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
483 else if (outprec == TYPE_PRECISION (long_integer_type_node)
484 && !TYPE_UNSIGNED (type))
485 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
486 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
487 && !TYPE_UNSIGNED (type))
488 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
489 break;
491 CASE_FLT_FN (BUILT_IN_TRUNC):
492 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
494 default:
495 break;
498 if (fn)
500 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
501 return convert_to_integer (type, newexpr);
505 /* Convert (int)logb(d) -> ilogb(d). */
506 if (optimize
507 && flag_unsafe_math_optimizations
508 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
509 && integer_type_node
510 && (outprec > TYPE_PRECISION (integer_type_node)
511 || (outprec == TYPE_PRECISION (integer_type_node)
512 && !TYPE_UNSIGNED (type))))
514 tree s_expr = strip_float_extensions (expr);
515 tree s_intype = TREE_TYPE (s_expr);
516 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
517 tree fn = 0;
519 switch (fcode)
521 CASE_FLT_FN (BUILT_IN_LOGB):
522 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
523 break;
525 default:
526 break;
529 if (fn)
531 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
532 return convert_to_integer (type, newexpr);
536 switch (TREE_CODE (intype))
538 case POINTER_TYPE:
539 case REFERENCE_TYPE:
540 if (integer_zerop (expr))
541 return build_int_cst (type, 0);
543 /* Convert to an unsigned integer of the correct width first, and from
544 there widen/truncate to the required type. Some targets support the
545 coexistence of multiple valid pointer sizes, so fetch the one we need
546 from the type. */
547 expr = fold_build1 (CONVERT_EXPR,
548 lang_hooks.types.type_for_size
549 (TYPE_PRECISION (intype), 0),
550 expr);
551 return fold_convert (type, expr);
553 case INTEGER_TYPE:
554 case ENUMERAL_TYPE:
555 case BOOLEAN_TYPE:
556 case OFFSET_TYPE:
557 /* If this is a logical operation, which just returns 0 or 1, we can
558 change the type of the expression. */
560 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
562 expr = copy_node (expr);
563 TREE_TYPE (expr) = type;
564 return expr;
567 /* If we are widening the type, put in an explicit conversion.
568 Similarly if we are not changing the width. After this, we know
569 we are truncating EXPR. */
571 else if (outprec >= inprec)
573 enum tree_code code;
575 /* If the precision of the EXPR's type is K bits and the
576 destination mode has more bits, and the sign is changing,
577 it is not safe to use a NOP_EXPR. For example, suppose
578 that EXPR's type is a 3-bit unsigned integer type, the
579 TYPE is a 3-bit signed integer type, and the machine mode
580 for the types is 8-bit QImode. In that case, the
581 conversion necessitates an explicit sign-extension. In
582 the signed-to-unsigned case the high-order bits have to
583 be cleared. */
584 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
585 && (TYPE_PRECISION (TREE_TYPE (expr))
586 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
587 code = CONVERT_EXPR;
588 else
589 code = NOP_EXPR;
591 return fold_build1 (code, type, expr);
594 /* If TYPE is an enumeral type or a type with a precision less
595 than the number of bits in its mode, do the conversion to the
596 type corresponding to its mode, then do a nop conversion
597 to TYPE. */
598 else if (TREE_CODE (type) == ENUMERAL_TYPE
599 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
600 return build1 (NOP_EXPR, type,
601 convert (lang_hooks.types.type_for_mode
602 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
603 expr));
605 /* Here detect when we can distribute the truncation down past some
606 arithmetic. For example, if adding two longs and converting to an
607 int, we can equally well convert both to ints and then add.
608 For the operations handled here, such truncation distribution
609 is always safe.
610 It is desirable in these cases:
611 1) when truncating down to full-word from a larger size
612 2) when truncating takes no work.
613 3) when at least one operand of the arithmetic has been extended
614 (as by C's default conversions). In this case we need two conversions
615 if we do the arithmetic as already requested, so we might as well
616 truncate both and then combine. Perhaps that way we need only one.
618 Note that in general we cannot do the arithmetic in a type
619 shorter than the desired result of conversion, even if the operands
620 are both extended from a shorter type, because they might overflow
621 if combined in that type. The exceptions to this--the times when
622 two narrow values can be combined in their narrow type even to
623 make a wider result--are handled by "shorten" in build_binary_op. */
625 switch (ex_form)
627 case RSHIFT_EXPR:
628 /* We can pass truncation down through right shifting
629 when the shift count is a nonpositive constant. */
630 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
631 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
632 goto trunc1;
633 break;
635 case LSHIFT_EXPR:
636 /* We can pass truncation down through left shifting
637 when the shift count is a nonnegative constant and
638 the target type is unsigned. */
639 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
640 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
641 && TYPE_UNSIGNED (type)
642 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
644 /* If shift count is less than the width of the truncated type,
645 really shift. */
646 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
647 /* In this case, shifting is like multiplication. */
648 goto trunc1;
649 else
651 /* If it is >= that width, result is zero.
652 Handling this with trunc1 would give the wrong result:
653 (int) ((long long) a << 32) is well defined (as 0)
654 but (int) a << 32 is undefined and would get a
655 warning. */
657 tree t = build_int_cst (type, 0);
659 /* If the original expression had side-effects, we must
660 preserve it. */
661 if (TREE_SIDE_EFFECTS (expr))
662 return build2 (COMPOUND_EXPR, type, expr, t);
663 else
664 return t;
667 break;
669 case TRUNC_DIV_EXPR:
671 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
672 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
674 /* Don't distribute unless the output precision is at least as big
675 as the actual inputs and it has the same signedness. */
676 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
677 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
678 /* If signedness of arg0 and arg1 don't match,
679 we can't necessarily find a type to compare them in. */
680 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
681 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
682 /* Do not change the sign of the division. */
683 && (TYPE_UNSIGNED (TREE_TYPE (expr))
684 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
685 /* Either require unsigned division or a division by
686 a constant that is not -1. */
687 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
688 || (TREE_CODE (arg1) == INTEGER_CST
689 && !integer_all_onesp (arg1))))
690 goto trunc1;
691 break;
694 case MAX_EXPR:
695 case MIN_EXPR:
696 case MULT_EXPR:
698 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
699 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
701 /* Don't distribute unless the output precision is at least as big
702 as the actual inputs. Otherwise, the comparison of the
703 truncated values will be wrong. */
704 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
705 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
706 /* If signedness of arg0 and arg1 don't match,
707 we can't necessarily find a type to compare them in. */
708 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
709 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
710 goto trunc1;
711 break;
714 case PLUS_EXPR:
715 case MINUS_EXPR:
716 case BIT_AND_EXPR:
717 case BIT_IOR_EXPR:
718 case BIT_XOR_EXPR:
719 trunc1:
721 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
722 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
724 /* Do not try to narrow operands of pointer subtraction;
725 that will interfere with other folding. */
726 if (ex_form == MINUS_EXPR
727 && CONVERT_EXPR_P (arg0)
728 && CONVERT_EXPR_P (arg1)
729 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
730 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
731 break;
733 if (outprec >= BITS_PER_WORD
734 || TRULY_NOOP_TRUNCATION (outprec, inprec)
735 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
736 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
738 /* Do the arithmetic in type TYPEX,
739 then convert result to TYPE. */
740 tree typex = type;
742 /* Can't do arithmetic in enumeral types
743 so use an integer type that will hold the values. */
744 if (TREE_CODE (typex) == ENUMERAL_TYPE)
745 typex = lang_hooks.types.type_for_size
746 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
748 /* But now perhaps TYPEX is as wide as INPREC.
749 In that case, do nothing special here.
750 (Otherwise would recurse infinitely in convert. */
751 if (TYPE_PRECISION (typex) != inprec)
753 /* Don't do unsigned arithmetic where signed was wanted,
754 or vice versa.
755 Exception: if both of the original operands were
756 unsigned then we can safely do the work as unsigned.
757 Exception: shift operations take their type solely
758 from the first argument.
759 Exception: the LSHIFT_EXPR case above requires that
760 we perform this operation unsigned lest we produce
761 signed-overflow undefinedness.
762 And we may need to do it as unsigned
763 if we truncate to the original size. */
764 if (TYPE_UNSIGNED (TREE_TYPE (expr))
765 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
766 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
767 || ex_form == LSHIFT_EXPR
768 || ex_form == RSHIFT_EXPR
769 || ex_form == LROTATE_EXPR
770 || ex_form == RROTATE_EXPR))
771 || ex_form == LSHIFT_EXPR
772 /* If we have !flag_wrapv, and either ARG0 or
773 ARG1 is of a signed type, we have to do
774 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
775 type in case the operation in outprec precision
776 could overflow. Otherwise, we would introduce
777 signed-overflow undefinedness. */
778 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
779 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
780 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
781 > outprec)
782 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
783 > outprec))
784 && (ex_form == PLUS_EXPR
785 || ex_form == MINUS_EXPR
786 || ex_form == MULT_EXPR)))
787 typex = unsigned_type_for (typex);
788 else
789 typex = signed_type_for (typex);
790 return convert (type,
791 fold_build2 (ex_form, typex,
792 convert (typex, arg0),
793 convert (typex, arg1)));
797 break;
799 case NEGATE_EXPR:
800 case BIT_NOT_EXPR:
801 /* This is not correct for ABS_EXPR,
802 since we must test the sign before truncation. */
804 tree typex = unsigned_type_for (type);
805 return convert (type,
806 fold_build1 (ex_form, typex,
807 convert (typex,
808 TREE_OPERAND (expr, 0))));
811 case NOP_EXPR:
812 /* Don't introduce a
813 "can't convert between vector values of different size" error. */
814 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
815 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
816 != GET_MODE_SIZE (TYPE_MODE (type))))
817 break;
818 /* If truncating after truncating, might as well do all at once.
819 If truncating after extending, we may get rid of wasted work. */
820 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
822 case COND_EXPR:
823 /* It is sometimes worthwhile to push the narrowing down through
824 the conditional and never loses. A COND_EXPR may have a throw
825 as one operand, which then has void type. Just leave void
826 operands as they are. */
827 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
828 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
829 ? TREE_OPERAND (expr, 1)
830 : convert (type, TREE_OPERAND (expr, 1)),
831 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
832 ? TREE_OPERAND (expr, 2)
833 : convert (type, TREE_OPERAND (expr, 2)));
835 default:
836 break;
839 /* When parsing long initializers, we might end up with a lot of casts.
840 Shortcut this. */
841 if (TREE_CODE (expr) == INTEGER_CST)
842 return fold_convert (type, expr);
843 return build1 (CONVERT_EXPR, type, expr);
845 case REAL_TYPE:
846 return build1 (FIX_TRUNC_EXPR, type, expr);
848 case FIXED_POINT_TYPE:
849 return build1 (FIXED_CONVERT_EXPR, type, expr);
851 case COMPLEX_TYPE:
852 return convert (type,
853 fold_build1 (REALPART_EXPR,
854 TREE_TYPE (TREE_TYPE (expr)), expr));
856 case VECTOR_TYPE:
857 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
859 error ("can%'t convert between vector values of different size");
860 return error_mark_node;
862 return build1 (VIEW_CONVERT_EXPR, type, expr);
864 default:
865 error ("aggregate value used where an integer was expected");
866 return convert (type, integer_zero_node);
870 /* Convert EXPR to the complex type TYPE in the usual ways. */
872 tree
873 convert_to_complex (tree type, tree expr)
875 tree subtype = TREE_TYPE (type);
877 switch (TREE_CODE (TREE_TYPE (expr)))
879 case REAL_TYPE:
880 case FIXED_POINT_TYPE:
881 case INTEGER_TYPE:
882 case ENUMERAL_TYPE:
883 case BOOLEAN_TYPE:
884 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
885 convert (subtype, integer_zero_node));
887 case COMPLEX_TYPE:
889 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
891 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
892 return expr;
893 else if (TREE_CODE (expr) == COMPLEX_EXPR)
894 return fold_build2 (COMPLEX_EXPR, type,
895 convert (subtype, TREE_OPERAND (expr, 0)),
896 convert (subtype, TREE_OPERAND (expr, 1)));
897 else
899 expr = save_expr (expr);
900 return
901 fold_build2 (COMPLEX_EXPR, type,
902 convert (subtype,
903 fold_build1 (REALPART_EXPR,
904 TREE_TYPE (TREE_TYPE (expr)),
905 expr)),
906 convert (subtype,
907 fold_build1 (IMAGPART_EXPR,
908 TREE_TYPE (TREE_TYPE (expr)),
909 expr)));
913 case POINTER_TYPE:
914 case REFERENCE_TYPE:
915 error ("pointer value used where a complex was expected");
916 return convert_to_complex (type, integer_zero_node);
918 default:
919 error ("aggregate value used where a complex was expected");
920 return convert_to_complex (type, integer_zero_node);
924 /* Convert EXPR to the vector type TYPE in the usual ways. */
926 tree
927 convert_to_vector (tree type, tree expr)
929 switch (TREE_CODE (TREE_TYPE (expr)))
931 case INTEGER_TYPE:
932 case VECTOR_TYPE:
933 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
935 error ("can%'t convert between vector values of different size");
936 return error_mark_node;
938 return build1 (VIEW_CONVERT_EXPR, type, expr);
940 default:
941 error ("can%'t convert value to a vector");
942 return error_mark_node;
946 /* Convert EXPR to some fixed-point type TYPE.
948 EXPR must be fixed-point, float, integer, or enumeral;
949 in other cases error is called. */
951 tree
952 convert_to_fixed (tree type, tree expr)
954 if (integer_zerop (expr))
956 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
957 return fixed_zero_node;
959 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
961 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
962 return fixed_one_node;
965 switch (TREE_CODE (TREE_TYPE (expr)))
967 case FIXED_POINT_TYPE:
968 case INTEGER_TYPE:
969 case ENUMERAL_TYPE:
970 case BOOLEAN_TYPE:
971 case REAL_TYPE:
972 return build1 (FIXED_CONVERT_EXPR, type, expr);
974 case COMPLEX_TYPE:
975 return convert (type,
976 fold_build1 (REALPART_EXPR,
977 TREE_TYPE (TREE_TYPE (expr)), expr));
979 default:
980 error ("aggregate value used where a fixed-point was expected");
981 return error_mark_node;