* gcc.dg/pthread-init-2.c (dg-options): Define _XOPEN_SOURCE=500
[official-gcc.git] / gcc / convert.c
blob5e6b09e06545805374c87b117b9a2f1057d4e392
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* These routines are somewhat language-independent utility function
24 intended to be called by the language-specific convert () functions. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "convert.h"
33 #include "diagnostic-core.h"
34 #include "langhooks.h"
36 /* Convert EXPR to some pointer or reference type TYPE.
37 EXPR must be pointer, reference, integer, enumeral, or literal zero;
38 in other cases error is called. */
40 tree
41 convert_to_pointer (tree type, tree expr)
43 location_t loc = EXPR_LOCATION (expr);
44 if (TREE_TYPE (expr) == type)
45 return expr;
47 switch (TREE_CODE (TREE_TYPE (expr)))
49 case POINTER_TYPE:
50 case REFERENCE_TYPE:
52 /* If the pointers point to different address spaces, conversion needs
53 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
54 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
55 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
57 if (to_as == from_as)
58 return fold_build1_loc (loc, NOP_EXPR, type, expr);
59 else
60 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
63 case INTEGER_TYPE:
64 case ENUMERAL_TYPE:
65 case BOOLEAN_TYPE:
67 /* If the input precision differs from the target pointer type
68 precision, first convert the input expression to an integer type of
69 the target precision. Some targets, e.g. VMS, need several pointer
70 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
71 unsigned int pprec = TYPE_PRECISION (type);
72 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
74 if (eprec != pprec)
75 expr = fold_build1_loc (loc, NOP_EXPR,
76 lang_hooks.types.type_for_size (pprec, 0),
77 expr);
80 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
82 default:
83 error ("cannot convert to a pointer type");
84 return convert_to_pointer (type, integer_zero_node);
89 /* Convert EXPR to some floating-point type TYPE.
91 EXPR must be float, fixed-point, integer, or enumeral;
92 in other cases error is called. */
94 tree
95 convert_to_real (tree type, tree expr)
97 enum built_in_function fcode = builtin_mathfn_code (expr);
98 tree itype = TREE_TYPE (expr);
100 /* Disable until we figure out how to decide whether the functions are
101 present in runtime. */
102 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
103 if (optimize
104 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
105 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
107 switch (fcode)
109 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
110 CASE_MATHFN (COSH)
111 CASE_MATHFN (EXP)
112 CASE_MATHFN (EXP10)
113 CASE_MATHFN (EXP2)
114 CASE_MATHFN (EXPM1)
115 CASE_MATHFN (GAMMA)
116 CASE_MATHFN (J0)
117 CASE_MATHFN (J1)
118 CASE_MATHFN (LGAMMA)
119 CASE_MATHFN (POW10)
120 CASE_MATHFN (SINH)
121 CASE_MATHFN (TGAMMA)
122 CASE_MATHFN (Y0)
123 CASE_MATHFN (Y1)
124 /* The above functions may set errno differently with float
125 input or output so this transformation is not safe with
126 -fmath-errno. */
127 if (flag_errno_math)
128 break;
129 CASE_MATHFN (ACOS)
130 CASE_MATHFN (ACOSH)
131 CASE_MATHFN (ASIN)
132 CASE_MATHFN (ASINH)
133 CASE_MATHFN (ATAN)
134 CASE_MATHFN (ATANH)
135 CASE_MATHFN (CBRT)
136 CASE_MATHFN (COS)
137 CASE_MATHFN (ERF)
138 CASE_MATHFN (ERFC)
139 CASE_MATHFN (FABS)
140 CASE_MATHFN (LOG)
141 CASE_MATHFN (LOG10)
142 CASE_MATHFN (LOG2)
143 CASE_MATHFN (LOG1P)
144 CASE_MATHFN (LOGB)
145 CASE_MATHFN (SIN)
146 CASE_MATHFN (SQRT)
147 CASE_MATHFN (TAN)
148 CASE_MATHFN (TANH)
149 #undef CASE_MATHFN
151 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
152 tree newtype = type;
154 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
155 the both as the safe type for operation. */
156 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
157 newtype = TREE_TYPE (arg0);
159 /* Be careful about integer to fp conversions.
160 These may overflow still. */
161 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
162 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
163 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
164 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
166 tree fn = mathfn_built_in (newtype, fcode);
168 if (fn)
170 tree arg = fold (convert_to_real (newtype, arg0));
171 expr = build_call_expr (fn, 1, arg);
172 if (newtype == type)
173 return expr;
177 default:
178 break;
181 if (optimize
182 && (((fcode == BUILT_IN_FLOORL
183 || fcode == BUILT_IN_CEILL
184 || fcode == BUILT_IN_ROUNDL
185 || fcode == BUILT_IN_RINTL
186 || fcode == BUILT_IN_TRUNCL
187 || fcode == BUILT_IN_NEARBYINTL)
188 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
189 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
190 || ((fcode == BUILT_IN_FLOOR
191 || fcode == BUILT_IN_CEIL
192 || fcode == BUILT_IN_ROUND
193 || fcode == BUILT_IN_RINT
194 || fcode == BUILT_IN_TRUNC
195 || fcode == BUILT_IN_NEARBYINT)
196 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
198 tree fn = mathfn_built_in (type, fcode);
200 if (fn)
202 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
204 /* Make sure (type)arg0 is an extension, otherwise we could end up
205 changing (float)floor(double d) into floorf((float)d), which is
206 incorrect because (float)d uses round-to-nearest and can round
207 up to the next integer. */
208 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
209 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
213 /* Propagate the cast into the operation. */
214 if (itype != type && FLOAT_TYPE_P (type))
215 switch (TREE_CODE (expr))
217 /* Convert (float)-x into -(float)x. This is safe for
218 round-to-nearest rounding mode. */
219 case ABS_EXPR:
220 case NEGATE_EXPR:
221 if (!flag_rounding_math
222 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
223 return build1 (TREE_CODE (expr), type,
224 fold (convert_to_real (type,
225 TREE_OPERAND (expr, 0))));
226 break;
227 /* Convert (outertype)((innertype0)a+(innertype1)b)
228 into ((newtype)a+(newtype)b) where newtype
229 is the widest mode from all of these. */
230 case PLUS_EXPR:
231 case MINUS_EXPR:
232 case MULT_EXPR:
233 case RDIV_EXPR:
235 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
236 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
238 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
239 && FLOAT_TYPE_P (TREE_TYPE (arg1))
240 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
242 tree newtype = type;
244 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
245 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
246 || TYPE_MODE (type) == SDmode)
247 newtype = dfloat32_type_node;
248 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
249 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
250 || TYPE_MODE (type) == DDmode)
251 newtype = dfloat64_type_node;
252 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
253 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
254 || TYPE_MODE (type) == TDmode)
255 newtype = dfloat128_type_node;
256 if (newtype == dfloat32_type_node
257 || newtype == dfloat64_type_node
258 || newtype == dfloat128_type_node)
260 expr = build2 (TREE_CODE (expr), newtype,
261 fold (convert_to_real (newtype, arg0)),
262 fold (convert_to_real (newtype, arg1)));
263 if (newtype == type)
264 return expr;
265 break;
268 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
269 newtype = TREE_TYPE (arg0);
270 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
271 newtype = TREE_TYPE (arg1);
272 /* Sometimes this transformation is safe (cannot
273 change results through affecting double rounding
274 cases) and sometimes it is not. If NEWTYPE is
275 wider than TYPE, e.g. (float)((long double)double
276 + (long double)double) converted to
277 (float)(double + double), the transformation is
278 unsafe regardless of the details of the types
279 involved; double rounding can arise if the result
280 of NEWTYPE arithmetic is a NEWTYPE value half way
281 between two representable TYPE values but the
282 exact value is sufficiently different (in the
283 right direction) for this difference to be
284 visible in ITYPE arithmetic. If NEWTYPE is the
285 same as TYPE, however, the transformation may be
286 safe depending on the types involved: it is safe
287 if the ITYPE has strictly more than twice as many
288 mantissa bits as TYPE, can represent infinities
289 and NaNs if the TYPE can, and has sufficient
290 exponent range for the product or ratio of two
291 values representable in the TYPE to be within the
292 range of normal values of ITYPE. */
293 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
294 && (flag_unsafe_math_optimizations
295 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
296 && real_can_shorten_arithmetic (TYPE_MODE (itype),
297 TYPE_MODE (type))
298 && !excess_precision_type (newtype))))
300 expr = build2 (TREE_CODE (expr), newtype,
301 fold (convert_to_real (newtype, arg0)),
302 fold (convert_to_real (newtype, arg1)));
303 if (newtype == type)
304 return expr;
308 break;
309 default:
310 break;
313 switch (TREE_CODE (TREE_TYPE (expr)))
315 case REAL_TYPE:
316 /* Ignore the conversion if we don't need to store intermediate
317 results and neither type is a decimal float. */
318 return build1 ((flag_float_store
319 || DECIMAL_FLOAT_TYPE_P (type)
320 || DECIMAL_FLOAT_TYPE_P (itype))
321 ? CONVERT_EXPR : NOP_EXPR, type, expr);
323 case INTEGER_TYPE:
324 case ENUMERAL_TYPE:
325 case BOOLEAN_TYPE:
326 return build1 (FLOAT_EXPR, type, expr);
328 case FIXED_POINT_TYPE:
329 return build1 (FIXED_CONVERT_EXPR, type, expr);
331 case COMPLEX_TYPE:
332 return convert (type,
333 fold_build1 (REALPART_EXPR,
334 TREE_TYPE (TREE_TYPE (expr)), expr));
336 case POINTER_TYPE:
337 case REFERENCE_TYPE:
338 error ("pointer value used where a floating point value was expected");
339 return convert_to_real (type, integer_zero_node);
341 default:
342 error ("aggregate value used where a float was expected");
343 return convert_to_real (type, integer_zero_node);
347 /* Convert EXPR to some integer (or enum) type TYPE.
349 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
350 fixed-point or vector; in other cases error is called.
352 The result of this is always supposed to be a newly created tree node
353 not in use in any existing structure. */
355 tree
356 convert_to_integer (tree type, tree expr)
358 enum tree_code ex_form = TREE_CODE (expr);
359 tree intype = TREE_TYPE (expr);
360 unsigned int inprec = TYPE_PRECISION (intype);
361 unsigned int outprec = TYPE_PRECISION (type);
363 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
364 be. Consider `enum E = { a, b = (enum E) 3 };'. */
365 if (!COMPLETE_TYPE_P (type))
367 error ("conversion to incomplete type");
368 return error_mark_node;
371 /* Convert e.g. (long)round(d) -> lround(d). */
372 /* If we're converting to char, we may encounter differing behavior
373 between converting from double->char vs double->long->char.
374 We're in "undefined" territory but we prefer to be conservative,
375 so only proceed in "unsafe" math mode. */
376 if (optimize
377 && (flag_unsafe_math_optimizations
378 || (long_integer_type_node
379 && outprec >= TYPE_PRECISION (long_integer_type_node))))
381 tree s_expr = strip_float_extensions (expr);
382 tree s_intype = TREE_TYPE (s_expr);
383 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
384 tree fn = 0;
386 switch (fcode)
388 CASE_FLT_FN (BUILT_IN_CEIL):
389 /* Only convert in ISO C99 mode. */
390 if (!TARGET_C99_FUNCTIONS)
391 break;
392 if (outprec < TYPE_PRECISION (integer_type_node)
393 || (outprec == TYPE_PRECISION (integer_type_node)
394 && !TYPE_UNSIGNED (type)))
395 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
396 else if (outprec == TYPE_PRECISION (long_integer_type_node)
397 && !TYPE_UNSIGNED (type))
398 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
399 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
400 && !TYPE_UNSIGNED (type))
401 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
402 break;
404 CASE_FLT_FN (BUILT_IN_FLOOR):
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_IFLOOR);
412 else if (outprec == TYPE_PRECISION (long_integer_type_node)
413 && !TYPE_UNSIGNED (type))
414 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
415 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
416 && !TYPE_UNSIGNED (type))
417 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
418 break;
420 CASE_FLT_FN (BUILT_IN_ROUND):
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_IROUND);
428 else if (outprec == TYPE_PRECISION (long_integer_type_node)
429 && !TYPE_UNSIGNED (type))
430 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
431 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
432 && !TYPE_UNSIGNED (type))
433 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
434 break;
436 CASE_FLT_FN (BUILT_IN_NEARBYINT):
437 /* Only convert nearbyint* if we can ignore math exceptions. */
438 if (flag_trapping_math)
439 break;
440 /* ... Fall through ... */
441 CASE_FLT_FN (BUILT_IN_RINT):
442 /* Only convert in ISO C99 mode. */
443 if (!TARGET_C99_FUNCTIONS)
444 break;
445 if (outprec < TYPE_PRECISION (integer_type_node)
446 || (outprec == TYPE_PRECISION (integer_type_node)
447 && !TYPE_UNSIGNED (type)))
448 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
449 else if (outprec == TYPE_PRECISION (long_integer_type_node)
450 && !TYPE_UNSIGNED (type))
451 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
452 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
453 && !TYPE_UNSIGNED (type))
454 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
455 break;
457 CASE_FLT_FN (BUILT_IN_TRUNC):
458 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
460 default:
461 break;
464 if (fn)
466 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
467 return convert_to_integer (type, newexpr);
471 /* Convert (int)logb(d) -> ilogb(d). */
472 if (optimize
473 && flag_unsafe_math_optimizations
474 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
475 && integer_type_node
476 && (outprec > TYPE_PRECISION (integer_type_node)
477 || (outprec == TYPE_PRECISION (integer_type_node)
478 && !TYPE_UNSIGNED (type))))
480 tree s_expr = strip_float_extensions (expr);
481 tree s_intype = TREE_TYPE (s_expr);
482 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
483 tree fn = 0;
485 switch (fcode)
487 CASE_FLT_FN (BUILT_IN_LOGB):
488 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
489 break;
491 default:
492 break;
495 if (fn)
497 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
498 return convert_to_integer (type, newexpr);
502 switch (TREE_CODE (intype))
504 case POINTER_TYPE:
505 case REFERENCE_TYPE:
506 if (integer_zerop (expr))
507 return build_int_cst (type, 0);
509 /* Convert to an unsigned integer of the correct width first, and from
510 there widen/truncate to the required type. Some targets support the
511 coexistence of multiple valid pointer sizes, so fetch the one we need
512 from the type. */
513 expr = fold_build1 (CONVERT_EXPR,
514 lang_hooks.types.type_for_size
515 (TYPE_PRECISION (intype), 0),
516 expr);
517 return fold_convert (type, expr);
519 case INTEGER_TYPE:
520 case ENUMERAL_TYPE:
521 case BOOLEAN_TYPE:
522 case OFFSET_TYPE:
523 /* If this is a logical operation, which just returns 0 or 1, we can
524 change the type of the expression. */
526 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
528 expr = copy_node (expr);
529 TREE_TYPE (expr) = type;
530 return expr;
533 /* If we are widening the type, put in an explicit conversion.
534 Similarly if we are not changing the width. After this, we know
535 we are truncating EXPR. */
537 else if (outprec >= inprec)
539 enum tree_code code;
541 /* If the precision of the EXPR's type is K bits and the
542 destination mode has more bits, and the sign is changing,
543 it is not safe to use a NOP_EXPR. For example, suppose
544 that EXPR's type is a 3-bit unsigned integer type, the
545 TYPE is a 3-bit signed integer type, and the machine mode
546 for the types is 8-bit QImode. In that case, the
547 conversion necessitates an explicit sign-extension. In
548 the signed-to-unsigned case the high-order bits have to
549 be cleared. */
550 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
551 && (TYPE_PRECISION (TREE_TYPE (expr))
552 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
553 code = CONVERT_EXPR;
554 else
555 code = NOP_EXPR;
557 return fold_build1 (code, type, expr);
560 /* If TYPE is an enumeral type or a type with a precision less
561 than the number of bits in its mode, do the conversion to the
562 type corresponding to its mode, then do a nop conversion
563 to TYPE. */
564 else if (TREE_CODE (type) == ENUMERAL_TYPE
565 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
566 return build1 (NOP_EXPR, type,
567 convert (lang_hooks.types.type_for_mode
568 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
569 expr));
571 /* Here detect when we can distribute the truncation down past some
572 arithmetic. For example, if adding two longs and converting to an
573 int, we can equally well convert both to ints and then add.
574 For the operations handled here, such truncation distribution
575 is always safe.
576 It is desirable in these cases:
577 1) when truncating down to full-word from a larger size
578 2) when truncating takes no work.
579 3) when at least one operand of the arithmetic has been extended
580 (as by C's default conversions). In this case we need two conversions
581 if we do the arithmetic as already requested, so we might as well
582 truncate both and then combine. Perhaps that way we need only one.
584 Note that in general we cannot do the arithmetic in a type
585 shorter than the desired result of conversion, even if the operands
586 are both extended from a shorter type, because they might overflow
587 if combined in that type. The exceptions to this--the times when
588 two narrow values can be combined in their narrow type even to
589 make a wider result--are handled by "shorten" in build_binary_op. */
591 switch (ex_form)
593 case RSHIFT_EXPR:
594 /* We can pass truncation down through right shifting
595 when the shift count is a nonpositive constant. */
596 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
597 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
598 goto trunc1;
599 break;
601 case LSHIFT_EXPR:
602 /* We can pass truncation down through left shifting
603 when the shift count is a nonnegative constant and
604 the target type is unsigned. */
605 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
606 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
607 && TYPE_UNSIGNED (type)
608 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
610 /* If shift count is less than the width of the truncated type,
611 really shift. */
612 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
613 /* In this case, shifting is like multiplication. */
614 goto trunc1;
615 else
617 /* If it is >= that width, result is zero.
618 Handling this with trunc1 would give the wrong result:
619 (int) ((long long) a << 32) is well defined (as 0)
620 but (int) a << 32 is undefined and would get a
621 warning. */
623 tree t = build_int_cst (type, 0);
625 /* If the original expression had side-effects, we must
626 preserve it. */
627 if (TREE_SIDE_EFFECTS (expr))
628 return build2 (COMPOUND_EXPR, type, expr, t);
629 else
630 return t;
633 break;
635 case TRUNC_DIV_EXPR:
637 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
638 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
640 /* Don't distribute unless the output precision is at least as big
641 as the actual inputs and it has the same signedness. */
642 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
643 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
644 /* If signedness of arg0 and arg1 don't match,
645 we can't necessarily find a type to compare them in. */
646 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
647 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
648 /* Do not change the sign of the division. */
649 && (TYPE_UNSIGNED (TREE_TYPE (expr))
650 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
651 /* Either require unsigned division or a division by
652 a constant that is not -1. */
653 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
654 || (TREE_CODE (arg1) == INTEGER_CST
655 && !integer_all_onesp (arg1))))
656 goto trunc1;
657 break;
660 case MAX_EXPR:
661 case MIN_EXPR:
662 case MULT_EXPR:
664 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
665 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
667 /* Don't distribute unless the output precision is at least as big
668 as the actual inputs. Otherwise, the comparison of the
669 truncated values will be wrong. */
670 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
671 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
672 /* If signedness of arg0 and arg1 don't match,
673 we can't necessarily find a type to compare them in. */
674 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
675 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
676 goto trunc1;
677 break;
680 case PLUS_EXPR:
681 case MINUS_EXPR:
682 case BIT_AND_EXPR:
683 case BIT_IOR_EXPR:
684 case BIT_XOR_EXPR:
685 trunc1:
687 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
688 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
690 /* Do not try to narrow operands of pointer subtraction;
691 that will interfere with other folding. */
692 if (ex_form == MINUS_EXPR
693 && CONVERT_EXPR_P (arg0)
694 && CONVERT_EXPR_P (arg1)
695 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
696 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
697 break;
699 if (outprec >= BITS_PER_WORD
700 || TRULY_NOOP_TRUNCATION (outprec, inprec)
701 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
702 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
704 /* Do the arithmetic in type TYPEX,
705 then convert result to TYPE. */
706 tree typex = type;
708 /* Can't do arithmetic in enumeral types
709 so use an integer type that will hold the values. */
710 if (TREE_CODE (typex) == ENUMERAL_TYPE)
711 typex = lang_hooks.types.type_for_size
712 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
714 /* But now perhaps TYPEX is as wide as INPREC.
715 In that case, do nothing special here.
716 (Otherwise would recurse infinitely in convert. */
717 if (TYPE_PRECISION (typex) != inprec)
719 /* Don't do unsigned arithmetic where signed was wanted,
720 or vice versa.
721 Exception: if both of the original operands were
722 unsigned then we can safely do the work as unsigned.
723 Exception: shift operations take their type solely
724 from the first argument.
725 Exception: the LSHIFT_EXPR case above requires that
726 we perform this operation unsigned lest we produce
727 signed-overflow undefinedness.
728 And we may need to do it as unsigned
729 if we truncate to the original size. */
730 if (TYPE_UNSIGNED (TREE_TYPE (expr))
731 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
732 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
733 || ex_form == LSHIFT_EXPR
734 || ex_form == RSHIFT_EXPR
735 || ex_form == LROTATE_EXPR
736 || ex_form == RROTATE_EXPR))
737 || ex_form == LSHIFT_EXPR
738 /* If we have !flag_wrapv, and either ARG0 or
739 ARG1 is of a signed type, we have to do
740 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
741 type in case the operation in outprec precision
742 could overflow. Otherwise, we would introduce
743 signed-overflow undefinedness. */
744 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
745 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
746 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
747 > outprec)
748 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
749 > outprec))
750 && (ex_form == PLUS_EXPR
751 || ex_form == MINUS_EXPR
752 || ex_form == MULT_EXPR)))
753 typex = unsigned_type_for (typex);
754 else
755 typex = signed_type_for (typex);
756 return convert (type,
757 fold_build2 (ex_form, typex,
758 convert (typex, arg0),
759 convert (typex, arg1)));
763 break;
765 case NEGATE_EXPR:
766 case BIT_NOT_EXPR:
767 /* This is not correct for ABS_EXPR,
768 since we must test the sign before truncation. */
770 tree typex = unsigned_type_for (type);
771 return convert (type,
772 fold_build1 (ex_form, typex,
773 convert (typex,
774 TREE_OPERAND (expr, 0))));
777 case NOP_EXPR:
778 /* Don't introduce a
779 "can't convert between vector values of different size" error. */
780 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
781 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
782 != GET_MODE_SIZE (TYPE_MODE (type))))
783 break;
784 /* If truncating after truncating, might as well do all at once.
785 If truncating after extending, we may get rid of wasted work. */
786 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
788 case COND_EXPR:
789 /* It is sometimes worthwhile to push the narrowing down through
790 the conditional and never loses. A COND_EXPR may have a throw
791 as one operand, which then has void type. Just leave void
792 operands as they are. */
793 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
794 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
795 ? TREE_OPERAND (expr, 1)
796 : convert (type, TREE_OPERAND (expr, 1)),
797 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
798 ? TREE_OPERAND (expr, 2)
799 : convert (type, TREE_OPERAND (expr, 2)));
801 default:
802 break;
805 /* When parsing long initializers, we might end up with a lot of casts.
806 Shortcut this. */
807 if (TREE_CODE (expr) == INTEGER_CST)
808 return fold_convert (type, expr);
809 return build1 (CONVERT_EXPR, type, expr);
811 case REAL_TYPE:
812 return build1 (FIX_TRUNC_EXPR, type, expr);
814 case FIXED_POINT_TYPE:
815 return build1 (FIXED_CONVERT_EXPR, type, expr);
817 case COMPLEX_TYPE:
818 return convert (type,
819 fold_build1 (REALPART_EXPR,
820 TREE_TYPE (TREE_TYPE (expr)), expr));
822 case VECTOR_TYPE:
823 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
825 error ("can%'t convert between vector values of different size");
826 return error_mark_node;
828 return build1 (VIEW_CONVERT_EXPR, type, expr);
830 default:
831 error ("aggregate value used where an integer was expected");
832 return convert (type, integer_zero_node);
836 /* Convert EXPR to the complex type TYPE in the usual ways. */
838 tree
839 convert_to_complex (tree type, tree expr)
841 tree subtype = TREE_TYPE (type);
843 switch (TREE_CODE (TREE_TYPE (expr)))
845 case REAL_TYPE:
846 case FIXED_POINT_TYPE:
847 case INTEGER_TYPE:
848 case ENUMERAL_TYPE:
849 case BOOLEAN_TYPE:
850 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
851 convert (subtype, integer_zero_node));
853 case COMPLEX_TYPE:
855 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
857 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
858 return expr;
859 else if (TREE_CODE (expr) == COMPLEX_EXPR)
860 return fold_build2 (COMPLEX_EXPR, type,
861 convert (subtype, TREE_OPERAND (expr, 0)),
862 convert (subtype, TREE_OPERAND (expr, 1)));
863 else
865 expr = save_expr (expr);
866 return
867 fold_build2 (COMPLEX_EXPR, type,
868 convert (subtype,
869 fold_build1 (REALPART_EXPR,
870 TREE_TYPE (TREE_TYPE (expr)),
871 expr)),
872 convert (subtype,
873 fold_build1 (IMAGPART_EXPR,
874 TREE_TYPE (TREE_TYPE (expr)),
875 expr)));
879 case POINTER_TYPE:
880 case REFERENCE_TYPE:
881 error ("pointer value used where a complex was expected");
882 return convert_to_complex (type, integer_zero_node);
884 default:
885 error ("aggregate value used where a complex was expected");
886 return convert_to_complex (type, integer_zero_node);
890 /* Convert EXPR to the vector type TYPE in the usual ways. */
892 tree
893 convert_to_vector (tree type, tree expr)
895 switch (TREE_CODE (TREE_TYPE (expr)))
897 case INTEGER_TYPE:
898 case VECTOR_TYPE:
899 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
901 error ("can%'t convert between vector values of different size");
902 return error_mark_node;
904 return build1 (VIEW_CONVERT_EXPR, type, expr);
906 default:
907 error ("can%'t convert value to a vector");
908 return error_mark_node;
912 /* Convert EXPR to some fixed-point type TYPE.
914 EXPR must be fixed-point, float, integer, or enumeral;
915 in other cases error is called. */
917 tree
918 convert_to_fixed (tree type, tree expr)
920 if (integer_zerop (expr))
922 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
923 return fixed_zero_node;
925 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
927 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
928 return fixed_one_node;
931 switch (TREE_CODE (TREE_TYPE (expr)))
933 case FIXED_POINT_TYPE:
934 case INTEGER_TYPE:
935 case ENUMERAL_TYPE:
936 case BOOLEAN_TYPE:
937 case REAL_TYPE:
938 return build1 (FIXED_CONVERT_EXPR, type, expr);
940 case COMPLEX_TYPE:
941 return convert (type,
942 fold_build1 (REALPART_EXPR,
943 TREE_TYPE (TREE_TYPE (expr)), expr));
945 default:
946 error ("aggregate value used where a fixed-point was expected");
947 return error_mark_node;