Mark ChangeLog
[official-gcc.git] / gcc / convert.c
blob2b93437e5ea6f157ec2e4113d32eb0bc28a10945
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 "langhooks.h"
34 /* Convert EXPR to some pointer or reference type TYPE.
35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
36 in other cases error is called. */
38 tree
39 convert_to_pointer (tree type, tree expr)
41 location_t loc = EXPR_LOCATION (expr);
42 if (TREE_TYPE (expr) == type)
43 return expr;
45 switch (TREE_CODE (TREE_TYPE (expr)))
47 case POINTER_TYPE:
48 case REFERENCE_TYPE:
50 /* If the pointers point to different address spaces, conversion needs
51 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
52 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
53 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
55 if (to_as == from_as)
56 return fold_build1_loc (loc, NOP_EXPR, type, expr);
57 else
58 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
61 case INTEGER_TYPE:
62 case ENUMERAL_TYPE:
63 case BOOLEAN_TYPE:
65 /* If the input precision differs from the target pointer type
66 precision, first convert the input expression to an integer type of
67 the target precision. Some targets, e.g. VMS, need several pointer
68 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
69 unsigned int pprec = TYPE_PRECISION (type);
70 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
72 if (eprec != pprec)
73 expr = fold_build1_loc (loc, NOP_EXPR,
74 lang_hooks.types.type_for_size (pprec, 0),
75 expr);
78 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
80 default:
81 error ("cannot convert to a pointer type");
82 return convert_to_pointer (type, integer_zero_node);
87 /* Convert EXPR to some floating-point type TYPE.
89 EXPR must be float, fixed-point, integer, or enumeral;
90 in other cases error is called. */
92 tree
93 convert_to_real (tree type, tree expr)
95 enum built_in_function fcode = builtin_mathfn_code (expr);
96 tree itype = TREE_TYPE (expr);
98 if (TREE_CODE (expr) == COMPOUND_EXPR)
100 tree t = convert_to_real (type, TREE_OPERAND (expr, 1));
101 if (t == TREE_OPERAND (expr, 1))
102 return expr;
103 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
104 TREE_OPERAND (expr, 0), t);
107 /* Disable until we figure out how to decide whether the functions are
108 present in runtime. */
109 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
110 if (optimize
111 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
112 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
114 switch (fcode)
116 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
117 CASE_MATHFN (COSH)
118 CASE_MATHFN (EXP)
119 CASE_MATHFN (EXP10)
120 CASE_MATHFN (EXP2)
121 CASE_MATHFN (EXPM1)
122 CASE_MATHFN (GAMMA)
123 CASE_MATHFN (J0)
124 CASE_MATHFN (J1)
125 CASE_MATHFN (LGAMMA)
126 CASE_MATHFN (POW10)
127 CASE_MATHFN (SINH)
128 CASE_MATHFN (TGAMMA)
129 CASE_MATHFN (Y0)
130 CASE_MATHFN (Y1)
131 /* The above functions may set errno differently with float
132 input or output so this transformation is not safe with
133 -fmath-errno. */
134 if (flag_errno_math)
135 break;
136 CASE_MATHFN (ACOS)
137 CASE_MATHFN (ACOSH)
138 CASE_MATHFN (ASIN)
139 CASE_MATHFN (ASINH)
140 CASE_MATHFN (ATAN)
141 CASE_MATHFN (ATANH)
142 CASE_MATHFN (CBRT)
143 CASE_MATHFN (COS)
144 CASE_MATHFN (ERF)
145 CASE_MATHFN (ERFC)
146 CASE_MATHFN (FABS)
147 CASE_MATHFN (LOG)
148 CASE_MATHFN (LOG10)
149 CASE_MATHFN (LOG2)
150 CASE_MATHFN (LOG1P)
151 CASE_MATHFN (LOGB)
152 CASE_MATHFN (SIN)
153 CASE_MATHFN (SQRT)
154 CASE_MATHFN (TAN)
155 CASE_MATHFN (TANH)
156 #undef CASE_MATHFN
158 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
159 tree newtype = type;
161 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
162 the both as the safe type for operation. */
163 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
164 newtype = TREE_TYPE (arg0);
166 /* Be careful about integer to fp conversions.
167 These may overflow still. */
168 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
169 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
170 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
171 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
173 tree fn = mathfn_built_in (newtype, fcode);
175 if (fn)
177 tree arg = fold (convert_to_real (newtype, arg0));
178 expr = build_call_expr (fn, 1, arg);
179 if (newtype == type)
180 return expr;
184 default:
185 break;
188 if (optimize
189 && (((fcode == BUILT_IN_FLOORL
190 || fcode == BUILT_IN_CEILL
191 || fcode == BUILT_IN_ROUNDL
192 || fcode == BUILT_IN_RINTL
193 || fcode == BUILT_IN_TRUNCL
194 || fcode == BUILT_IN_NEARBYINTL)
195 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
196 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
197 || ((fcode == BUILT_IN_FLOOR
198 || fcode == BUILT_IN_CEIL
199 || fcode == BUILT_IN_ROUND
200 || fcode == BUILT_IN_RINT
201 || fcode == BUILT_IN_TRUNC
202 || fcode == BUILT_IN_NEARBYINT)
203 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
205 tree fn = mathfn_built_in (type, fcode);
207 if (fn)
209 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
211 /* Make sure (type)arg0 is an extension, otherwise we could end up
212 changing (float)floor(double d) into floorf((float)d), which is
213 incorrect because (float)d uses round-to-nearest and can round
214 up to the next integer. */
215 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
216 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
220 /* Propagate the cast into the operation. */
221 if (itype != type && FLOAT_TYPE_P (type))
222 switch (TREE_CODE (expr))
224 /* Convert (float)-x into -(float)x. This is safe for
225 round-to-nearest rounding mode. */
226 case ABS_EXPR:
227 case NEGATE_EXPR:
228 if (!flag_rounding_math
229 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
230 return build1 (TREE_CODE (expr), type,
231 fold (convert_to_real (type,
232 TREE_OPERAND (expr, 0))));
233 break;
234 /* Convert (outertype)((innertype0)a+(innertype1)b)
235 into ((newtype)a+(newtype)b) where newtype
236 is the widest mode from all of these. */
237 case PLUS_EXPR:
238 case MINUS_EXPR:
239 case MULT_EXPR:
240 case RDIV_EXPR:
242 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
243 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
245 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
246 && FLOAT_TYPE_P (TREE_TYPE (arg1))
247 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
249 tree newtype = type;
251 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
252 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
253 || TYPE_MODE (type) == SDmode)
254 newtype = dfloat32_type_node;
255 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
256 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
257 || TYPE_MODE (type) == DDmode)
258 newtype = dfloat64_type_node;
259 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
260 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
261 || TYPE_MODE (type) == TDmode)
262 newtype = dfloat128_type_node;
263 if (newtype == dfloat32_type_node
264 || newtype == dfloat64_type_node
265 || newtype == dfloat128_type_node)
267 expr = build2 (TREE_CODE (expr), newtype,
268 fold (convert_to_real (newtype, arg0)),
269 fold (convert_to_real (newtype, arg1)));
270 if (newtype == type)
271 return expr;
272 break;
275 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
276 newtype = TREE_TYPE (arg0);
277 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
278 newtype = TREE_TYPE (arg1);
279 /* Sometimes this transformation is safe (cannot
280 change results through affecting double rounding
281 cases) and sometimes it is not. If NEWTYPE is
282 wider than TYPE, e.g. (float)((long double)double
283 + (long double)double) converted to
284 (float)(double + double), the transformation is
285 unsafe regardless of the details of the types
286 involved; double rounding can arise if the result
287 of NEWTYPE arithmetic is a NEWTYPE value half way
288 between two representable TYPE values but the
289 exact value is sufficiently different (in the
290 right direction) for this difference to be
291 visible in ITYPE arithmetic. If NEWTYPE is the
292 same as TYPE, however, the transformation may be
293 safe depending on the types involved: it is safe
294 if the ITYPE has strictly more than twice as many
295 mantissa bits as TYPE, can represent infinities
296 and NaNs if the TYPE can, and has sufficient
297 exponent range for the product or ratio of two
298 values representable in the TYPE to be within the
299 range of normal values of ITYPE. */
300 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
301 && (flag_unsafe_math_optimizations
302 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
303 && real_can_shorten_arithmetic (TYPE_MODE (itype),
304 TYPE_MODE (type))
305 && !excess_precision_type (newtype))))
307 expr = build2 (TREE_CODE (expr), newtype,
308 fold (convert_to_real (newtype, arg0)),
309 fold (convert_to_real (newtype, arg1)));
310 if (newtype == type)
311 return expr;
315 break;
316 default:
317 break;
320 switch (TREE_CODE (TREE_TYPE (expr)))
322 case REAL_TYPE:
323 /* Ignore the conversion if we don't need to store intermediate
324 results and neither type is a decimal float. */
325 return build1 ((flag_float_store
326 || DECIMAL_FLOAT_TYPE_P (type)
327 || DECIMAL_FLOAT_TYPE_P (itype))
328 ? CONVERT_EXPR : NOP_EXPR, type, expr);
330 case INTEGER_TYPE:
331 case ENUMERAL_TYPE:
332 case BOOLEAN_TYPE:
333 return build1 (FLOAT_EXPR, type, expr);
335 case FIXED_POINT_TYPE:
336 return build1 (FIXED_CONVERT_EXPR, type, expr);
338 case COMPLEX_TYPE:
339 return convert (type,
340 fold_build1 (REALPART_EXPR,
341 TREE_TYPE (TREE_TYPE (expr)), expr));
343 case POINTER_TYPE:
344 case REFERENCE_TYPE:
345 error ("pointer value used where a floating point value was expected");
346 return convert_to_real (type, integer_zero_node);
348 default:
349 error ("aggregate value used where a float was expected");
350 return convert_to_real (type, integer_zero_node);
354 /* Convert EXPR to some integer (or enum) type TYPE.
356 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
357 fixed-point or vector; in other cases error is called.
359 The result of this is always supposed to be a newly created tree node
360 not in use in any existing structure. */
362 tree
363 convert_to_integer (tree type, tree expr)
365 enum tree_code ex_form = TREE_CODE (expr);
366 tree intype = TREE_TYPE (expr);
367 unsigned int inprec = TYPE_PRECISION (intype);
368 unsigned int outprec = TYPE_PRECISION (type);
370 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
371 be. Consider `enum E = { a, b = (enum E) 3 };'. */
372 if (!COMPLETE_TYPE_P (type))
374 error ("conversion to incomplete type");
375 return error_mark_node;
378 if (ex_form == COMPOUND_EXPR)
380 tree t = convert_to_integer (type, TREE_OPERAND (expr, 1));
381 if (t == TREE_OPERAND (expr, 1))
382 return expr;
383 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (t),
384 TREE_OPERAND (expr, 0), t);
387 /* Convert e.g. (long)round(d) -> lround(d). */
388 /* If we're converting to char, we may encounter differing behavior
389 between converting from double->char vs double->long->char.
390 We're in "undefined" territory but we prefer to be conservative,
391 so only proceed in "unsafe" math mode. */
392 if (optimize
393 && (flag_unsafe_math_optimizations
394 || (long_integer_type_node
395 && outprec >= TYPE_PRECISION (long_integer_type_node))))
397 tree s_expr = strip_float_extensions (expr);
398 tree s_intype = TREE_TYPE (s_expr);
399 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
400 tree fn = 0;
402 switch (fcode)
404 CASE_FLT_FN (BUILT_IN_CEIL):
405 /* Only convert in ISO C99 mode. */
406 if (!TARGET_C99_FUNCTIONS)
407 break;
408 if (outprec < TYPE_PRECISION (integer_type_node)
409 || (outprec == TYPE_PRECISION (integer_type_node)
410 && !TYPE_UNSIGNED (type)))
411 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
412 else if (outprec == TYPE_PRECISION (long_integer_type_node)
413 && !TYPE_UNSIGNED (type))
414 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
415 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
416 && !TYPE_UNSIGNED (type))
417 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
418 break;
420 CASE_FLT_FN (BUILT_IN_FLOOR):
421 /* Only convert in ISO C99 mode. */
422 if (!TARGET_C99_FUNCTIONS)
423 break;
424 if (outprec < TYPE_PRECISION (integer_type_node)
425 || (outprec == TYPE_PRECISION (integer_type_node)
426 && !TYPE_UNSIGNED (type)))
427 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
428 else if (outprec == TYPE_PRECISION (long_integer_type_node)
429 && !TYPE_UNSIGNED (type))
430 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
431 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
432 && !TYPE_UNSIGNED (type))
433 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
434 break;
436 CASE_FLT_FN (BUILT_IN_ROUND):
437 /* Only convert in ISO C99 mode. */
438 if (!TARGET_C99_FUNCTIONS)
439 break;
440 if (outprec < TYPE_PRECISION (integer_type_node)
441 || (outprec == TYPE_PRECISION (integer_type_node)
442 && !TYPE_UNSIGNED (type)))
443 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
444 else if (outprec == TYPE_PRECISION (long_integer_type_node)
445 && !TYPE_UNSIGNED (type))
446 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
447 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
448 && !TYPE_UNSIGNED (type))
449 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
450 break;
452 CASE_FLT_FN (BUILT_IN_NEARBYINT):
453 /* Only convert nearbyint* if we can ignore math exceptions. */
454 if (flag_trapping_math)
455 break;
456 /* ... Fall through ... */
457 CASE_FLT_FN (BUILT_IN_RINT):
458 /* Only convert in ISO C99 mode. */
459 if (!TARGET_C99_FUNCTIONS)
460 break;
461 if (outprec < TYPE_PRECISION (integer_type_node)
462 || (outprec == TYPE_PRECISION (integer_type_node)
463 && !TYPE_UNSIGNED (type)))
464 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
465 else if (outprec == TYPE_PRECISION (long_integer_type_node)
466 && !TYPE_UNSIGNED (type))
467 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
468 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
469 && !TYPE_UNSIGNED (type))
470 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
471 break;
473 CASE_FLT_FN (BUILT_IN_TRUNC):
474 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
476 default:
477 break;
480 if (fn)
482 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
483 return convert_to_integer (type, newexpr);
487 /* Convert (int)logb(d) -> ilogb(d). */
488 if (optimize
489 && flag_unsafe_math_optimizations
490 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
491 && integer_type_node
492 && (outprec > TYPE_PRECISION (integer_type_node)
493 || (outprec == TYPE_PRECISION (integer_type_node)
494 && !TYPE_UNSIGNED (type))))
496 tree s_expr = strip_float_extensions (expr);
497 tree s_intype = TREE_TYPE (s_expr);
498 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
499 tree fn = 0;
501 switch (fcode)
503 CASE_FLT_FN (BUILT_IN_LOGB):
504 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
505 break;
507 default:
508 break;
511 if (fn)
513 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
514 return convert_to_integer (type, newexpr);
518 switch (TREE_CODE (intype))
520 case POINTER_TYPE:
521 case REFERENCE_TYPE:
522 if (integer_zerop (expr))
523 return build_int_cst (type, 0);
525 /* Convert to an unsigned integer of the correct width first, and from
526 there widen/truncate to the required type. Some targets support the
527 coexistence of multiple valid pointer sizes, so fetch the one we need
528 from the type. */
529 expr = fold_build1 (CONVERT_EXPR,
530 lang_hooks.types.type_for_size
531 (TYPE_PRECISION (intype), 0),
532 expr);
533 return fold_convert (type, expr);
535 case INTEGER_TYPE:
536 case ENUMERAL_TYPE:
537 case BOOLEAN_TYPE:
538 case OFFSET_TYPE:
539 /* If this is a logical operation, which just returns 0 or 1, we can
540 change the type of the expression. */
542 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
544 expr = copy_node (expr);
545 TREE_TYPE (expr) = type;
546 return expr;
549 /* If we are widening the type, put in an explicit conversion.
550 Similarly if we are not changing the width. After this, we know
551 we are truncating EXPR. */
553 else if (outprec >= inprec)
555 enum tree_code code;
557 /* If the precision of the EXPR's type is K bits and the
558 destination mode has more bits, and the sign is changing,
559 it is not safe to use a NOP_EXPR. For example, suppose
560 that EXPR's type is a 3-bit unsigned integer type, the
561 TYPE is a 3-bit signed integer type, and the machine mode
562 for the types is 8-bit QImode. In that case, the
563 conversion necessitates an explicit sign-extension. In
564 the signed-to-unsigned case the high-order bits have to
565 be cleared. */
566 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
567 && (TYPE_PRECISION (TREE_TYPE (expr))
568 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
569 code = CONVERT_EXPR;
570 else
571 code = NOP_EXPR;
573 return fold_build1 (code, type, expr);
576 /* If TYPE is an enumeral type or a type with a precision less
577 than the number of bits in its mode, do the conversion to the
578 type corresponding to its mode, then do a nop conversion
579 to TYPE. */
580 else if (TREE_CODE (type) == ENUMERAL_TYPE
581 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
582 return build1 (NOP_EXPR, type,
583 convert (lang_hooks.types.type_for_mode
584 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
585 expr));
587 /* Here detect when we can distribute the truncation down past some
588 arithmetic. For example, if adding two longs and converting to an
589 int, we can equally well convert both to ints and then add.
590 For the operations handled here, such truncation distribution
591 is always safe.
592 It is desirable in these cases:
593 1) when truncating down to full-word from a larger size
594 2) when truncating takes no work.
595 3) when at least one operand of the arithmetic has been extended
596 (as by C's default conversions). In this case we need two conversions
597 if we do the arithmetic as already requested, so we might as well
598 truncate both and then combine. Perhaps that way we need only one.
600 Note that in general we cannot do the arithmetic in a type
601 shorter than the desired result of conversion, even if the operands
602 are both extended from a shorter type, because they might overflow
603 if combined in that type. The exceptions to this--the times when
604 two narrow values can be combined in their narrow type even to
605 make a wider result--are handled by "shorten" in build_binary_op. */
607 switch (ex_form)
609 case RSHIFT_EXPR:
610 /* We can pass truncation down through right shifting
611 when the shift count is a nonpositive constant. */
612 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
613 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
614 goto trunc1;
615 break;
617 case LSHIFT_EXPR:
618 /* We can pass truncation down through left shifting
619 when the shift count is a nonnegative constant and
620 the target type is unsigned. */
621 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
622 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
623 && TYPE_UNSIGNED (type)
624 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
626 /* If shift count is less than the width of the truncated type,
627 really shift. */
628 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
629 /* In this case, shifting is like multiplication. */
630 goto trunc1;
631 else
633 /* If it is >= that width, result is zero.
634 Handling this with trunc1 would give the wrong result:
635 (int) ((long long) a << 32) is well defined (as 0)
636 but (int) a << 32 is undefined and would get a
637 warning. */
639 tree t = build_int_cst (type, 0);
641 /* If the original expression had side-effects, we must
642 preserve it. */
643 if (TREE_SIDE_EFFECTS (expr))
644 return build2 (COMPOUND_EXPR, type, expr, t);
645 else
646 return t;
649 break;
651 case TRUNC_DIV_EXPR:
653 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
654 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
656 /* Don't distribute unless the output precision is at least as big
657 as the actual inputs and it has the same signedness. */
658 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
659 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
660 /* If signedness of arg0 and arg1 don't match,
661 we can't necessarily find a type to compare them in. */
662 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
663 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
664 /* Do not change the sign of the division. */
665 && (TYPE_UNSIGNED (TREE_TYPE (expr))
666 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
667 /* Either require unsigned division or a division by
668 a constant that is not -1. */
669 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
670 || (TREE_CODE (arg1) == INTEGER_CST
671 && !integer_all_onesp (arg1))))
672 goto trunc1;
673 break;
676 case MAX_EXPR:
677 case MIN_EXPR:
678 case MULT_EXPR:
680 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
681 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
683 /* Don't distribute unless the output precision is at least as big
684 as the actual inputs. Otherwise, the comparison of the
685 truncated values will be wrong. */
686 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
687 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
688 /* If signedness of arg0 and arg1 don't match,
689 we can't necessarily find a type to compare them in. */
690 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
691 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
692 goto trunc1;
693 break;
696 case PLUS_EXPR:
697 case MINUS_EXPR:
698 case BIT_AND_EXPR:
699 case BIT_IOR_EXPR:
700 case BIT_XOR_EXPR:
701 trunc1:
703 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
704 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
706 /* Do not try to narrow operands of pointer subtraction;
707 that will interfere with other folding. */
708 if (ex_form == MINUS_EXPR
709 && CONVERT_EXPR_P (arg0)
710 && CONVERT_EXPR_P (arg1)
711 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
712 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
713 break;
715 if (outprec >= BITS_PER_WORD
716 || TRULY_NOOP_TRUNCATION (outprec, inprec)
717 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
718 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
720 /* Do the arithmetic in type TYPEX,
721 then convert result to TYPE. */
722 tree typex = type;
724 /* Can't do arithmetic in enumeral types
725 so use an integer type that will hold the values. */
726 if (TREE_CODE (typex) == ENUMERAL_TYPE)
727 typex = lang_hooks.types.type_for_size
728 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
730 /* But now perhaps TYPEX is as wide as INPREC.
731 In that case, do nothing special here.
732 (Otherwise would recurse infinitely in convert. */
733 if (TYPE_PRECISION (typex) != inprec)
735 /* Don't do unsigned arithmetic where signed was wanted,
736 or vice versa.
737 Exception: if both of the original operands were
738 unsigned then we can safely do the work as unsigned.
739 Exception: shift operations take their type solely
740 from the first argument.
741 Exception: the LSHIFT_EXPR case above requires that
742 we perform this operation unsigned lest we produce
743 signed-overflow undefinedness.
744 And we may need to do it as unsigned
745 if we truncate to the original size. */
746 if (TYPE_UNSIGNED (TREE_TYPE (expr))
747 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
748 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
749 || ex_form == LSHIFT_EXPR
750 || ex_form == RSHIFT_EXPR
751 || ex_form == LROTATE_EXPR
752 || ex_form == RROTATE_EXPR))
753 || ex_form == LSHIFT_EXPR
754 /* If we have !flag_wrapv, and either ARG0 or
755 ARG1 is of a signed type, we have to do
756 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
757 type in case the operation in outprec precision
758 could overflow. Otherwise, we would introduce
759 signed-overflow undefinedness. */
760 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
761 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
762 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
763 > outprec)
764 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
765 > outprec))
766 && (ex_form == PLUS_EXPR
767 || ex_form == MINUS_EXPR
768 || ex_form == MULT_EXPR)))
769 typex = unsigned_type_for (typex);
770 else
771 typex = signed_type_for (typex);
772 return convert (type,
773 fold_build2 (ex_form, typex,
774 convert (typex, arg0),
775 convert (typex, arg1)));
779 break;
781 case NEGATE_EXPR:
782 case BIT_NOT_EXPR:
783 /* This is not correct for ABS_EXPR,
784 since we must test the sign before truncation. */
786 tree typex = unsigned_type_for (type);
787 return convert (type,
788 fold_build1 (ex_form, typex,
789 convert (typex,
790 TREE_OPERAND (expr, 0))));
793 case NOP_EXPR:
794 /* Don't introduce a
795 "can't convert between vector values of different size" error. */
796 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
797 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
798 != GET_MODE_SIZE (TYPE_MODE (type))))
799 break;
800 /* If truncating after truncating, might as well do all at once.
801 If truncating after extending, we may get rid of wasted work. */
802 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
804 case COND_EXPR:
805 /* It is sometimes worthwhile to push the narrowing down through
806 the conditional and never loses. A COND_EXPR may have a throw
807 as one operand, which then has void type. Just leave void
808 operands as they are. */
809 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
810 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
811 ? TREE_OPERAND (expr, 1)
812 : convert (type, TREE_OPERAND (expr, 1)),
813 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
814 ? TREE_OPERAND (expr, 2)
815 : convert (type, TREE_OPERAND (expr, 2)));
817 default:
818 break;
821 /* When parsing long initializers, we might end up with a lot of casts.
822 Shortcut this. */
823 if (TREE_CODE (expr) == INTEGER_CST)
824 return fold_convert (type, expr);
825 return build1 (CONVERT_EXPR, type, expr);
827 case REAL_TYPE:
828 return build1 (FIX_TRUNC_EXPR, type, expr);
830 case FIXED_POINT_TYPE:
831 return build1 (FIXED_CONVERT_EXPR, type, expr);
833 case COMPLEX_TYPE:
834 return convert (type,
835 fold_build1 (REALPART_EXPR,
836 TREE_TYPE (TREE_TYPE (expr)), expr));
838 case VECTOR_TYPE:
839 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
841 error ("can%'t convert between vector values of different size");
842 return error_mark_node;
844 return build1 (VIEW_CONVERT_EXPR, type, expr);
846 default:
847 error ("aggregate value used where an integer was expected");
848 return convert (type, integer_zero_node);
852 /* Convert EXPR to the complex type TYPE in the usual ways. */
854 tree
855 convert_to_complex (tree type, tree expr)
857 tree subtype = TREE_TYPE (type);
859 switch (TREE_CODE (TREE_TYPE (expr)))
861 case REAL_TYPE:
862 case FIXED_POINT_TYPE:
863 case INTEGER_TYPE:
864 case ENUMERAL_TYPE:
865 case BOOLEAN_TYPE:
866 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
867 convert (subtype, integer_zero_node));
869 case COMPLEX_TYPE:
871 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
873 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
874 return expr;
875 else if (TREE_CODE (expr) == COMPOUND_EXPR)
877 tree t = convert_to_complex (type, TREE_OPERAND (expr, 1));
878 if (t == TREE_OPERAND (expr, 1))
879 return expr;
880 return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR,
881 TREE_TYPE (t), TREE_OPERAND (expr, 0), t);
883 else if (TREE_CODE (expr) == COMPLEX_EXPR)
884 return fold_build2 (COMPLEX_EXPR, type,
885 convert (subtype, TREE_OPERAND (expr, 0)),
886 convert (subtype, TREE_OPERAND (expr, 1)));
887 else
889 expr = save_expr (expr);
890 return
891 fold_build2 (COMPLEX_EXPR, type,
892 convert (subtype,
893 fold_build1 (REALPART_EXPR,
894 TREE_TYPE (TREE_TYPE (expr)),
895 expr)),
896 convert (subtype,
897 fold_build1 (IMAGPART_EXPR,
898 TREE_TYPE (TREE_TYPE (expr)),
899 expr)));
903 case POINTER_TYPE:
904 case REFERENCE_TYPE:
905 error ("pointer value used where a complex was expected");
906 return convert_to_complex (type, integer_zero_node);
908 default:
909 error ("aggregate value used where a complex was expected");
910 return convert_to_complex (type, integer_zero_node);
914 /* Convert EXPR to the vector type TYPE in the usual ways. */
916 tree
917 convert_to_vector (tree type, tree expr)
919 switch (TREE_CODE (TREE_TYPE (expr)))
921 case INTEGER_TYPE:
922 case VECTOR_TYPE:
923 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
925 error ("can%'t convert between vector values of different size");
926 return error_mark_node;
928 return build1 (VIEW_CONVERT_EXPR, type, expr);
930 default:
931 error ("can%'t convert value to a vector");
932 return error_mark_node;
936 /* Convert EXPR to some fixed-point type TYPE.
938 EXPR must be fixed-point, float, integer, or enumeral;
939 in other cases error is called. */
941 tree
942 convert_to_fixed (tree type, tree expr)
944 if (integer_zerop (expr))
946 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
947 return fixed_zero_node;
949 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
951 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
952 return fixed_one_node;
955 switch (TREE_CODE (TREE_TYPE (expr)))
957 case FIXED_POINT_TYPE:
958 case INTEGER_TYPE:
959 case ENUMERAL_TYPE:
960 case BOOLEAN_TYPE:
961 case REAL_TYPE:
962 return build1 (FIXED_CONVERT_EXPR, type, expr);
964 case COMPLEX_TYPE:
965 return convert (type,
966 fold_build1 (REALPART_EXPR,
967 TREE_TYPE (TREE_TYPE (expr)), expr));
969 default:
970 error ("aggregate value used where a fixed-point was expected");
971 return error_mark_node;