gcc/
[official-gcc.git] / gcc / convert.c
blob436fb2a65879fe2ff573ed6b4f7d0b09f0ae0409
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 "toplev.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 /* Propagate overflow to the NULL pointer. */
48 if (integer_zerop (expr))
49 return force_fit_type_double (type, 0, 0, 0, TREE_OVERFLOW (expr));
51 switch (TREE_CODE (TREE_TYPE (expr)))
53 case POINTER_TYPE:
54 case REFERENCE_TYPE:
56 /* If the pointers point to different address spaces, conversion needs
57 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
58 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
59 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
61 if (to_as == from_as)
62 return fold_build1_loc (loc, NOP_EXPR, type, expr);
63 else
64 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
67 case INTEGER_TYPE:
68 case ENUMERAL_TYPE:
69 case BOOLEAN_TYPE:
71 /* If the input precision differs from the target pointer type
72 precision, first convert the input expression to an integer type of
73 the target precision. Some targets, e.g. VMS, need several pointer
74 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
75 unsigned int pprec = TYPE_PRECISION (type);
76 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
78 if (eprec != pprec)
79 expr = fold_build1_loc (loc, NOP_EXPR,
80 lang_hooks.types.type_for_size (pprec, 0),
81 expr);
84 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
86 default:
87 error ("cannot convert to a pointer type");
88 return convert_to_pointer (type, integer_zero_node);
92 /* Avoid any floating point extensions from EXP. */
93 tree
94 strip_float_extensions (tree exp)
96 tree sub, expt, subt;
98 /* For floating point constant look up the narrowest type that can hold
99 it properly and handle it like (type)(narrowest_type)constant.
100 This way we can optimize for instance a=a*2.0 where "a" is float
101 but 2.0 is double constant. */
102 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
104 REAL_VALUE_TYPE orig;
105 tree type = NULL;
107 orig = TREE_REAL_CST (exp);
108 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
109 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
110 type = float_type_node;
111 else if (TYPE_PRECISION (TREE_TYPE (exp))
112 > TYPE_PRECISION (double_type_node)
113 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
114 type = double_type_node;
115 if (type)
116 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
119 if (!CONVERT_EXPR_P (exp))
120 return exp;
122 sub = TREE_OPERAND (exp, 0);
123 subt = TREE_TYPE (sub);
124 expt = TREE_TYPE (exp);
126 if (!FLOAT_TYPE_P (subt))
127 return exp;
129 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
130 return exp;
132 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
133 return exp;
135 return strip_float_extensions (sub);
139 /* Convert EXPR to some floating-point type TYPE.
141 EXPR must be float, fixed-point, integer, or enumeral;
142 in other cases error is called. */
144 tree
145 convert_to_real (tree type, tree expr)
147 enum built_in_function fcode = builtin_mathfn_code (expr);
148 tree itype = TREE_TYPE (expr);
150 /* Disable until we figure out how to decide whether the functions are
151 present in runtime. */
152 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
153 if (optimize
154 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
155 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
157 switch (fcode)
159 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
160 CASE_MATHFN (COSH)
161 CASE_MATHFN (EXP)
162 CASE_MATHFN (EXP10)
163 CASE_MATHFN (EXP2)
164 CASE_MATHFN (EXPM1)
165 CASE_MATHFN (GAMMA)
166 CASE_MATHFN (J0)
167 CASE_MATHFN (J1)
168 CASE_MATHFN (LGAMMA)
169 CASE_MATHFN (POW10)
170 CASE_MATHFN (SINH)
171 CASE_MATHFN (TGAMMA)
172 CASE_MATHFN (Y0)
173 CASE_MATHFN (Y1)
174 /* The above functions may set errno differently with float
175 input or output so this transformation is not safe with
176 -fmath-errno. */
177 if (flag_errno_math)
178 break;
179 CASE_MATHFN (ACOS)
180 CASE_MATHFN (ACOSH)
181 CASE_MATHFN (ASIN)
182 CASE_MATHFN (ASINH)
183 CASE_MATHFN (ATAN)
184 CASE_MATHFN (ATANH)
185 CASE_MATHFN (CBRT)
186 CASE_MATHFN (COS)
187 CASE_MATHFN (ERF)
188 CASE_MATHFN (ERFC)
189 CASE_MATHFN (FABS)
190 CASE_MATHFN (LOG)
191 CASE_MATHFN (LOG10)
192 CASE_MATHFN (LOG2)
193 CASE_MATHFN (LOG1P)
194 CASE_MATHFN (LOGB)
195 CASE_MATHFN (SIN)
196 CASE_MATHFN (SQRT)
197 CASE_MATHFN (TAN)
198 CASE_MATHFN (TANH)
199 #undef CASE_MATHFN
201 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
202 tree newtype = type;
204 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
205 the both as the safe type for operation. */
206 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
207 newtype = TREE_TYPE (arg0);
209 /* Be careful about integer to fp conversions.
210 These may overflow still. */
211 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
212 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
213 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
214 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
216 tree fn = mathfn_built_in (newtype, fcode);
218 if (fn)
220 tree arg = fold (convert_to_real (newtype, arg0));
221 expr = build_call_expr (fn, 1, arg);
222 if (newtype == type)
223 return expr;
227 default:
228 break;
231 if (optimize
232 && (((fcode == BUILT_IN_FLOORL
233 || fcode == BUILT_IN_CEILL
234 || fcode == BUILT_IN_ROUNDL
235 || fcode == BUILT_IN_RINTL
236 || fcode == BUILT_IN_TRUNCL
237 || fcode == BUILT_IN_NEARBYINTL)
238 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
239 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
240 || ((fcode == BUILT_IN_FLOOR
241 || fcode == BUILT_IN_CEIL
242 || fcode == BUILT_IN_ROUND
243 || fcode == BUILT_IN_RINT
244 || fcode == BUILT_IN_TRUNC
245 || fcode == BUILT_IN_NEARBYINT)
246 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
248 tree fn = mathfn_built_in (type, fcode);
250 if (fn)
252 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
254 /* Make sure (type)arg0 is an extension, otherwise we could end up
255 changing (float)floor(double d) into floorf((float)d), which is
256 incorrect because (float)d uses round-to-nearest and can round
257 up to the next integer. */
258 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
259 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
263 /* Propagate the cast into the operation. */
264 if (itype != type && FLOAT_TYPE_P (type))
265 switch (TREE_CODE (expr))
267 /* Convert (float)-x into -(float)x. This is safe for
268 round-to-nearest rounding mode. */
269 case ABS_EXPR:
270 case NEGATE_EXPR:
271 if (!flag_rounding_math
272 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
273 return build1 (TREE_CODE (expr), type,
274 fold (convert_to_real (type,
275 TREE_OPERAND (expr, 0))));
276 break;
277 /* Convert (outertype)((innertype0)a+(innertype1)b)
278 into ((newtype)a+(newtype)b) where newtype
279 is the widest mode from all of these. */
280 case PLUS_EXPR:
281 case MINUS_EXPR:
282 case MULT_EXPR:
283 case RDIV_EXPR:
285 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
286 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
288 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
289 && FLOAT_TYPE_P (TREE_TYPE (arg1))
290 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
292 tree newtype = type;
294 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
295 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
296 || TYPE_MODE (type) == SDmode)
297 newtype = dfloat32_type_node;
298 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
299 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
300 || TYPE_MODE (type) == DDmode)
301 newtype = dfloat64_type_node;
302 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
303 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
304 || TYPE_MODE (type) == TDmode)
305 newtype = dfloat128_type_node;
306 if (newtype == dfloat32_type_node
307 || newtype == dfloat64_type_node
308 || newtype == dfloat128_type_node)
310 expr = build2 (TREE_CODE (expr), newtype,
311 fold (convert_to_real (newtype, arg0)),
312 fold (convert_to_real (newtype, arg1)));
313 if (newtype == type)
314 return expr;
315 break;
318 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
319 newtype = TREE_TYPE (arg0);
320 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
321 newtype = TREE_TYPE (arg1);
322 /* Sometimes this transformation is safe (cannot
323 change results through affecting double rounding
324 cases) and sometimes it is not. If NEWTYPE is
325 wider than TYPE, e.g. (float)((long double)double
326 + (long double)double) converted to
327 (float)(double + double), the transformation is
328 unsafe regardless of the details of the types
329 involved; double rounding can arise if the result
330 of NEWTYPE arithmetic is a NEWTYPE value half way
331 between two representable TYPE values but the
332 exact value is sufficiently different (in the
333 right direction) for this difference to be
334 visible in ITYPE arithmetic. If NEWTYPE is the
335 same as TYPE, however, the transformation may be
336 safe depending on the types involved: it is safe
337 if the ITYPE has strictly more than twice as many
338 mantissa bits as TYPE, can represent infinities
339 and NaNs if the TYPE can, and has sufficient
340 exponent range for the product or ratio of two
341 values representable in the TYPE to be within the
342 range of normal values of ITYPE. */
343 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
344 && (flag_unsafe_math_optimizations
345 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
346 && real_can_shorten_arithmetic (TYPE_MODE (itype),
347 TYPE_MODE (type))
348 && !excess_precision_type (newtype))))
350 expr = build2 (TREE_CODE (expr), newtype,
351 fold (convert_to_real (newtype, arg0)),
352 fold (convert_to_real (newtype, arg1)));
353 if (newtype == type)
354 return expr;
358 break;
359 default:
360 break;
363 switch (TREE_CODE (TREE_TYPE (expr)))
365 case REAL_TYPE:
366 /* Ignore the conversion if we don't need to store intermediate
367 results and neither type is a decimal float. */
368 return build1 ((flag_float_store
369 || DECIMAL_FLOAT_TYPE_P (type)
370 || DECIMAL_FLOAT_TYPE_P (itype))
371 ? CONVERT_EXPR : NOP_EXPR, type, expr);
373 case INTEGER_TYPE:
374 case ENUMERAL_TYPE:
375 case BOOLEAN_TYPE:
376 return build1 (FLOAT_EXPR, type, expr);
378 case FIXED_POINT_TYPE:
379 return build1 (FIXED_CONVERT_EXPR, type, expr);
381 case COMPLEX_TYPE:
382 return convert (type,
383 fold_build1 (REALPART_EXPR,
384 TREE_TYPE (TREE_TYPE (expr)), expr));
386 case POINTER_TYPE:
387 case REFERENCE_TYPE:
388 error ("pointer value used where a floating point value was expected");
389 return convert_to_real (type, integer_zero_node);
391 default:
392 error ("aggregate value used where a float was expected");
393 return convert_to_real (type, integer_zero_node);
397 /* Convert EXPR to some integer (or enum) type TYPE.
399 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
400 fixed-point or vector; in other cases error is called.
402 The result of this is always supposed to be a newly created tree node
403 not in use in any existing structure. */
405 tree
406 convert_to_integer (tree type, tree expr)
408 enum tree_code ex_form = TREE_CODE (expr);
409 tree intype = TREE_TYPE (expr);
410 unsigned int inprec = TYPE_PRECISION (intype);
411 unsigned int outprec = TYPE_PRECISION (type);
413 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
414 be. Consider `enum E = { a, b = (enum E) 3 };'. */
415 if (!COMPLETE_TYPE_P (type))
417 error ("conversion to incomplete type");
418 return error_mark_node;
421 /* Convert e.g. (long)round(d) -> lround(d). */
422 /* If we're converting to char, we may encounter differing behavior
423 between converting from double->char vs double->long->char.
424 We're in "undefined" territory but we prefer to be conservative,
425 so only proceed in "unsafe" math mode. */
426 if (optimize
427 && (flag_unsafe_math_optimizations
428 || (long_integer_type_node
429 && outprec >= TYPE_PRECISION (long_integer_type_node))))
431 tree s_expr = strip_float_extensions (expr);
432 tree s_intype = TREE_TYPE (s_expr);
433 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
434 tree fn = 0;
436 switch (fcode)
438 CASE_FLT_FN (BUILT_IN_CEIL):
439 /* Only convert in ISO C99 mode. */
440 if (!TARGET_C99_FUNCTIONS)
441 break;
442 if (outprec < TYPE_PRECISION (long_integer_type_node)
443 || (outprec == TYPE_PRECISION (long_integer_type_node)
444 && !TYPE_UNSIGNED (type)))
445 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
446 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
447 && !TYPE_UNSIGNED (type))
448 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
449 break;
451 CASE_FLT_FN (BUILT_IN_FLOOR):
452 /* Only convert in ISO C99 mode. */
453 if (!TARGET_C99_FUNCTIONS)
454 break;
455 if (outprec < TYPE_PRECISION (long_integer_type_node)
456 || (outprec == TYPE_PRECISION (long_integer_type_node)
457 && !TYPE_UNSIGNED (type)))
458 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
459 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
460 && !TYPE_UNSIGNED (type))
461 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
462 break;
464 CASE_FLT_FN (BUILT_IN_ROUND):
465 if (outprec < TYPE_PRECISION (long_integer_type_node)
466 || (outprec == TYPE_PRECISION (long_integer_type_node)
467 && !TYPE_UNSIGNED (type)))
468 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
469 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
470 && !TYPE_UNSIGNED (type))
471 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
472 break;
474 CASE_FLT_FN (BUILT_IN_NEARBYINT):
475 /* Only convert nearbyint* if we can ignore math exceptions. */
476 if (flag_trapping_math)
477 break;
478 /* ... Fall through ... */
479 CASE_FLT_FN (BUILT_IN_RINT):
480 if (outprec < TYPE_PRECISION (long_integer_type_node)
481 || (outprec == TYPE_PRECISION (long_integer_type_node)
482 && !TYPE_UNSIGNED (type)))
483 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
484 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
485 && !TYPE_UNSIGNED (type))
486 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
487 break;
489 CASE_FLT_FN (BUILT_IN_TRUNC):
490 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
492 default:
493 break;
496 if (fn)
498 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
499 return convert_to_integer (type, newexpr);
503 /* Convert (int)logb(d) -> ilogb(d). */
504 if (optimize
505 && flag_unsafe_math_optimizations
506 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
507 && integer_type_node
508 && (outprec > TYPE_PRECISION (integer_type_node)
509 || (outprec == TYPE_PRECISION (integer_type_node)
510 && !TYPE_UNSIGNED (type))))
512 tree s_expr = strip_float_extensions (expr);
513 tree s_intype = TREE_TYPE (s_expr);
514 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
515 tree fn = 0;
517 switch (fcode)
519 CASE_FLT_FN (BUILT_IN_LOGB):
520 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
521 break;
523 default:
524 break;
527 if (fn)
529 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
530 return convert_to_integer (type, newexpr);
534 switch (TREE_CODE (intype))
536 case POINTER_TYPE:
537 case REFERENCE_TYPE:
538 if (integer_zerop (expr))
539 return build_int_cst (type, 0);
541 /* Convert to an unsigned integer of the correct width first, and from
542 there widen/truncate to the required type. Some targets support the
543 coexistence of multiple valid pointer sizes, so fetch the one we need
544 from the type. */
545 expr = fold_build1 (CONVERT_EXPR,
546 lang_hooks.types.type_for_size
547 (TYPE_PRECISION (intype), 0),
548 expr);
549 return fold_convert (type, expr);
551 case INTEGER_TYPE:
552 case ENUMERAL_TYPE:
553 case BOOLEAN_TYPE:
554 case OFFSET_TYPE:
555 /* If this is a logical operation, which just returns 0 or 1, we can
556 change the type of the expression. */
558 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
560 expr = copy_node (expr);
561 TREE_TYPE (expr) = type;
562 return expr;
565 /* If we are widening the type, put in an explicit conversion.
566 Similarly if we are not changing the width. After this, we know
567 we are truncating EXPR. */
569 else if (outprec >= inprec)
571 enum tree_code code;
572 tree tem;
574 /* If the precision of the EXPR's type is K bits and the
575 destination mode has more bits, and the sign is changing,
576 it is not safe to use a NOP_EXPR. For example, suppose
577 that EXPR's type is a 3-bit unsigned integer type, the
578 TYPE is a 3-bit signed integer type, and the machine mode
579 for the types is 8-bit QImode. In that case, the
580 conversion necessitates an explicit sign-extension. In
581 the signed-to-unsigned case the high-order bits have to
582 be cleared. */
583 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
584 && (TYPE_PRECISION (TREE_TYPE (expr))
585 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
586 code = CONVERT_EXPR;
587 else
588 code = NOP_EXPR;
590 tem = fold_unary (code, type, expr);
591 if (tem)
592 return tem;
594 tem = build1 (code, type, expr);
595 TREE_NO_WARNING (tem) = 1;
596 return tem;
599 /* If TYPE is an enumeral type or a type with a precision less
600 than the number of bits in its mode, do the conversion to the
601 type corresponding to its mode, then do a nop conversion
602 to TYPE. */
603 else if (TREE_CODE (type) == ENUMERAL_TYPE
604 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
605 return build1 (NOP_EXPR, type,
606 convert (lang_hooks.types.type_for_mode
607 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
608 expr));
610 /* Here detect when we can distribute the truncation down past some
611 arithmetic. For example, if adding two longs and converting to an
612 int, we can equally well convert both to ints and then add.
613 For the operations handled here, such truncation distribution
614 is always safe.
615 It is desirable in these cases:
616 1) when truncating down to full-word from a larger size
617 2) when truncating takes no work.
618 3) when at least one operand of the arithmetic has been extended
619 (as by C's default conversions). In this case we need two conversions
620 if we do the arithmetic as already requested, so we might as well
621 truncate both and then combine. Perhaps that way we need only one.
623 Note that in general we cannot do the arithmetic in a type
624 shorter than the desired result of conversion, even if the operands
625 are both extended from a shorter type, because they might overflow
626 if combined in that type. The exceptions to this--the times when
627 two narrow values can be combined in their narrow type even to
628 make a wider result--are handled by "shorten" in build_binary_op. */
630 switch (ex_form)
632 case RSHIFT_EXPR:
633 /* We can pass truncation down through right shifting
634 when the shift count is a nonpositive constant. */
635 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
636 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
637 goto trunc1;
638 break;
640 case LSHIFT_EXPR:
641 /* We can pass truncation down through left shifting
642 when the shift count is a nonnegative constant and
643 the target type is unsigned. */
644 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
645 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
646 && TYPE_UNSIGNED (type)
647 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
649 /* If shift count is less than the width of the truncated type,
650 really shift. */
651 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
652 /* In this case, shifting is like multiplication. */
653 goto trunc1;
654 else
656 /* If it is >= that width, result is zero.
657 Handling this with trunc1 would give the wrong result:
658 (int) ((long long) a << 32) is well defined (as 0)
659 but (int) a << 32 is undefined and would get a
660 warning. */
662 tree t = build_int_cst (type, 0);
664 /* If the original expression had side-effects, we must
665 preserve it. */
666 if (TREE_SIDE_EFFECTS (expr))
667 return build2 (COMPOUND_EXPR, type, expr, t);
668 else
669 return t;
672 break;
674 case TRUNC_DIV_EXPR:
676 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
677 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
679 /* Don't distribute unless the output precision is at least as big
680 as the actual inputs and it has the same signedness. */
681 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
682 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
683 /* If signedness of arg0 and arg1 don't match,
684 we can't necessarily find a type to compare them in. */
685 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
686 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
687 /* Do not change the sign of the division. */
688 && (TYPE_UNSIGNED (TREE_TYPE (expr))
689 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
690 /* Either require unsigned division or a division by
691 a constant that is not -1. */
692 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
693 || (TREE_CODE (arg1) == INTEGER_CST
694 && !integer_all_onesp (arg1))))
695 goto trunc1;
696 break;
699 case MAX_EXPR:
700 case MIN_EXPR:
701 case MULT_EXPR:
703 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
704 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
706 /* Don't distribute unless the output precision is at least as big
707 as the actual inputs. Otherwise, the comparison of the
708 truncated values will be wrong. */
709 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
710 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
711 /* If signedness of arg0 and arg1 don't match,
712 we can't necessarily find a type to compare them in. */
713 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
714 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
715 goto trunc1;
716 break;
719 case PLUS_EXPR:
720 case MINUS_EXPR:
721 case BIT_AND_EXPR:
722 case BIT_IOR_EXPR:
723 case BIT_XOR_EXPR:
724 trunc1:
726 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
727 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
729 if (outprec >= BITS_PER_WORD
730 || TRULY_NOOP_TRUNCATION (outprec, inprec)
731 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
732 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
734 /* Do the arithmetic in type TYPEX,
735 then convert result to TYPE. */
736 tree typex = type;
738 /* Can't do arithmetic in enumeral types
739 so use an integer type that will hold the values. */
740 if (TREE_CODE (typex) == ENUMERAL_TYPE)
741 typex = lang_hooks.types.type_for_size
742 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
744 /* But now perhaps TYPEX is as wide as INPREC.
745 In that case, do nothing special here.
746 (Otherwise would recurse infinitely in convert. */
747 if (TYPE_PRECISION (typex) != inprec)
749 /* Don't do unsigned arithmetic where signed was wanted,
750 or vice versa.
751 Exception: if both of the original operands were
752 unsigned then we can safely do the work as unsigned.
753 Exception: shift operations take their type solely
754 from the first argument.
755 Exception: the LSHIFT_EXPR case above requires that
756 we perform this operation unsigned lest we produce
757 signed-overflow undefinedness.
758 And we may need to do it as unsigned
759 if we truncate to the original size. */
760 if (TYPE_UNSIGNED (TREE_TYPE (expr))
761 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
762 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
763 || ex_form == LSHIFT_EXPR
764 || ex_form == RSHIFT_EXPR
765 || ex_form == LROTATE_EXPR
766 || ex_form == RROTATE_EXPR))
767 || ex_form == LSHIFT_EXPR
768 /* If we have !flag_wrapv, and either ARG0 or
769 ARG1 is of a signed type, we have to do
770 PLUS_EXPR or MINUS_EXPR in an unsigned
771 type. Otherwise, we would introduce
772 signed-overflow undefinedness. */
773 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
774 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
775 && (ex_form == PLUS_EXPR
776 || ex_form == MINUS_EXPR)))
777 typex = unsigned_type_for (typex);
778 else
779 typex = signed_type_for (typex);
780 return convert (type,
781 fold_build2 (ex_form, typex,
782 convert (typex, arg0),
783 convert (typex, arg1)));
787 break;
789 case NEGATE_EXPR:
790 case BIT_NOT_EXPR:
791 /* This is not correct for ABS_EXPR,
792 since we must test the sign before truncation. */
794 tree typex;
796 /* Don't do unsigned arithmetic where signed was wanted,
797 or vice versa. */
798 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
799 typex = unsigned_type_for (type);
800 else
801 typex = signed_type_for (type);
802 return convert (type,
803 fold_build1 (ex_form, typex,
804 convert (typex,
805 TREE_OPERAND (expr, 0))));
808 case NOP_EXPR:
809 /* Don't introduce a
810 "can't convert between vector values of different size" error. */
811 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
812 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
813 != GET_MODE_SIZE (TYPE_MODE (type))))
814 break;
815 /* If truncating after truncating, might as well do all at once.
816 If truncating after extending, we may get rid of wasted work. */
817 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
819 case COND_EXPR:
820 /* It is sometimes worthwhile to push the narrowing down through
821 the conditional and never loses. A COND_EXPR may have a throw
822 as one operand, which then has void type. Just leave void
823 operands as they are. */
824 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
825 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
826 ? TREE_OPERAND (expr, 1)
827 : convert (type, TREE_OPERAND (expr, 1)),
828 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
829 ? TREE_OPERAND (expr, 2)
830 : convert (type, TREE_OPERAND (expr, 2)));
832 default:
833 break;
836 return build1 (CONVERT_EXPR, type, expr);
838 case REAL_TYPE:
839 return build1 (FIX_TRUNC_EXPR, type, expr);
841 case FIXED_POINT_TYPE:
842 return build1 (FIXED_CONVERT_EXPR, type, expr);
844 case COMPLEX_TYPE:
845 return convert (type,
846 fold_build1 (REALPART_EXPR,
847 TREE_TYPE (TREE_TYPE (expr)), expr));
849 case VECTOR_TYPE:
850 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
852 error ("can't convert between vector values of different size");
853 return error_mark_node;
855 return build1 (VIEW_CONVERT_EXPR, type, expr);
857 default:
858 error ("aggregate value used where an integer was expected");
859 return convert (type, integer_zero_node);
863 /* Convert EXPR to the complex type TYPE in the usual ways. */
865 tree
866 convert_to_complex (tree type, tree expr)
868 tree subtype = TREE_TYPE (type);
870 switch (TREE_CODE (TREE_TYPE (expr)))
872 case REAL_TYPE:
873 case FIXED_POINT_TYPE:
874 case INTEGER_TYPE:
875 case ENUMERAL_TYPE:
876 case BOOLEAN_TYPE:
877 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
878 convert (subtype, integer_zero_node));
880 case COMPLEX_TYPE:
882 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
884 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
885 return expr;
886 else if (TREE_CODE (expr) == COMPLEX_EXPR)
887 return fold_build2 (COMPLEX_EXPR, type,
888 convert (subtype, TREE_OPERAND (expr, 0)),
889 convert (subtype, TREE_OPERAND (expr, 1)));
890 else
892 expr = save_expr (expr);
893 return
894 fold_build2 (COMPLEX_EXPR, type,
895 convert (subtype,
896 fold_build1 (REALPART_EXPR,
897 TREE_TYPE (TREE_TYPE (expr)),
898 expr)),
899 convert (subtype,
900 fold_build1 (IMAGPART_EXPR,
901 TREE_TYPE (TREE_TYPE (expr)),
902 expr)));
906 case POINTER_TYPE:
907 case REFERENCE_TYPE:
908 error ("pointer value used where a complex was expected");
909 return convert_to_complex (type, integer_zero_node);
911 default:
912 error ("aggregate value used where a complex was expected");
913 return convert_to_complex (type, integer_zero_node);
917 /* Convert EXPR to the vector type TYPE in the usual ways. */
919 tree
920 convert_to_vector (tree type, tree expr)
922 switch (TREE_CODE (TREE_TYPE (expr)))
924 case INTEGER_TYPE:
925 case VECTOR_TYPE:
926 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
928 error ("can't convert between vector values of different size");
929 return error_mark_node;
931 return build1 (VIEW_CONVERT_EXPR, type, expr);
933 default:
934 error ("can't convert value to a vector");
935 return error_mark_node;
939 /* Convert EXPR to some fixed-point type TYPE.
941 EXPR must be fixed-point, float, integer, or enumeral;
942 in other cases error is called. */
944 tree
945 convert_to_fixed (tree type, tree expr)
947 if (integer_zerop (expr))
949 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
950 return fixed_zero_node;
952 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
954 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
955 return fixed_one_node;
958 switch (TREE_CODE (TREE_TYPE (expr)))
960 case FIXED_POINT_TYPE:
961 case INTEGER_TYPE:
962 case ENUMERAL_TYPE:
963 case BOOLEAN_TYPE:
964 case REAL_TYPE:
965 return build1 (FIXED_CONVERT_EXPR, type, expr);
967 case COMPLEX_TYPE:
968 return convert (type,
969 fold_build1 (REALPART_EXPR,
970 TREE_TYPE (TREE_TYPE (expr)), expr));
972 default:
973 error ("aggregate value used where a fixed-point was expected");
974 return error_mark_node;