Correct typos.
[official-gcc.git] / gcc / convert.c
blob459d5337e35450bd24a6678dfc50ed9f632c7aee
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 /* Propagate overflow to the NULL pointer. */
48 if (integer_zerop (expr))
49 return force_fit_type_double (type, double_int_zero, 0,
50 TREE_OVERFLOW (expr));
52 switch (TREE_CODE (TREE_TYPE (expr)))
54 case POINTER_TYPE:
55 case REFERENCE_TYPE:
57 /* If the pointers point to different address spaces, conversion needs
58 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
59 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
60 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
62 if (to_as == from_as)
63 return fold_build1_loc (loc, NOP_EXPR, type, expr);
64 else
65 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
68 case INTEGER_TYPE:
69 case ENUMERAL_TYPE:
70 case BOOLEAN_TYPE:
72 /* If the input precision differs from the target pointer type
73 precision, first convert the input expression to an integer type of
74 the target precision. Some targets, e.g. VMS, need several pointer
75 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
76 unsigned int pprec = TYPE_PRECISION (type);
77 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
79 if (eprec != pprec)
80 expr = fold_build1_loc (loc, NOP_EXPR,
81 lang_hooks.types.type_for_size (pprec, 0),
82 expr);
85 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
87 default:
88 error ("cannot convert to a pointer type");
89 return convert_to_pointer (type, integer_zero_node);
93 /* Avoid any floating point extensions from EXP. */
94 tree
95 strip_float_extensions (tree exp)
97 tree sub, expt, subt;
99 /* For floating point constant look up the narrowest type that can hold
100 it properly and handle it like (type)(narrowest_type)constant.
101 This way we can optimize for instance a=a*2.0 where "a" is float
102 but 2.0 is double constant. */
103 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
105 REAL_VALUE_TYPE orig;
106 tree type = NULL;
108 orig = TREE_REAL_CST (exp);
109 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
110 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
111 type = float_type_node;
112 else if (TYPE_PRECISION (TREE_TYPE (exp))
113 > TYPE_PRECISION (double_type_node)
114 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
115 type = double_type_node;
116 if (type)
117 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
120 if (!CONVERT_EXPR_P (exp))
121 return exp;
123 sub = TREE_OPERAND (exp, 0);
124 subt = TREE_TYPE (sub);
125 expt = TREE_TYPE (exp);
127 if (!FLOAT_TYPE_P (subt))
128 return exp;
130 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
131 return exp;
133 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
134 return exp;
136 return strip_float_extensions (sub);
140 /* Convert EXPR to some floating-point type TYPE.
142 EXPR must be float, fixed-point, integer, or enumeral;
143 in other cases error is called. */
145 tree
146 convert_to_real (tree type, tree expr)
148 enum built_in_function fcode = builtin_mathfn_code (expr);
149 tree itype = TREE_TYPE (expr);
151 /* Disable until we figure out how to decide whether the functions are
152 present in runtime. */
153 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
154 if (optimize
155 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
156 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
158 switch (fcode)
160 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
161 CASE_MATHFN (COSH)
162 CASE_MATHFN (EXP)
163 CASE_MATHFN (EXP10)
164 CASE_MATHFN (EXP2)
165 CASE_MATHFN (EXPM1)
166 CASE_MATHFN (GAMMA)
167 CASE_MATHFN (J0)
168 CASE_MATHFN (J1)
169 CASE_MATHFN (LGAMMA)
170 CASE_MATHFN (POW10)
171 CASE_MATHFN (SINH)
172 CASE_MATHFN (TGAMMA)
173 CASE_MATHFN (Y0)
174 CASE_MATHFN (Y1)
175 /* The above functions may set errno differently with float
176 input or output so this transformation is not safe with
177 -fmath-errno. */
178 if (flag_errno_math)
179 break;
180 CASE_MATHFN (ACOS)
181 CASE_MATHFN (ACOSH)
182 CASE_MATHFN (ASIN)
183 CASE_MATHFN (ASINH)
184 CASE_MATHFN (ATAN)
185 CASE_MATHFN (ATANH)
186 CASE_MATHFN (CBRT)
187 CASE_MATHFN (COS)
188 CASE_MATHFN (ERF)
189 CASE_MATHFN (ERFC)
190 CASE_MATHFN (FABS)
191 CASE_MATHFN (LOG)
192 CASE_MATHFN (LOG10)
193 CASE_MATHFN (LOG2)
194 CASE_MATHFN (LOG1P)
195 CASE_MATHFN (LOGB)
196 CASE_MATHFN (SIN)
197 CASE_MATHFN (SQRT)
198 CASE_MATHFN (TAN)
199 CASE_MATHFN (TANH)
200 #undef CASE_MATHFN
202 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
203 tree newtype = type;
205 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
206 the both as the safe type for operation. */
207 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
208 newtype = TREE_TYPE (arg0);
210 /* Be careful about integer to fp conversions.
211 These may overflow still. */
212 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
213 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
214 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
215 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
217 tree fn = mathfn_built_in (newtype, fcode);
219 if (fn)
221 tree arg = fold (convert_to_real (newtype, arg0));
222 expr = build_call_expr (fn, 1, arg);
223 if (newtype == type)
224 return expr;
228 default:
229 break;
232 if (optimize
233 && (((fcode == BUILT_IN_FLOORL
234 || fcode == BUILT_IN_CEILL
235 || fcode == BUILT_IN_ROUNDL
236 || fcode == BUILT_IN_RINTL
237 || fcode == BUILT_IN_TRUNCL
238 || fcode == BUILT_IN_NEARBYINTL)
239 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
240 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
241 || ((fcode == BUILT_IN_FLOOR
242 || fcode == BUILT_IN_CEIL
243 || fcode == BUILT_IN_ROUND
244 || fcode == BUILT_IN_RINT
245 || fcode == BUILT_IN_TRUNC
246 || fcode == BUILT_IN_NEARBYINT)
247 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
249 tree fn = mathfn_built_in (type, fcode);
251 if (fn)
253 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
255 /* Make sure (type)arg0 is an extension, otherwise we could end up
256 changing (float)floor(double d) into floorf((float)d), which is
257 incorrect because (float)d uses round-to-nearest and can round
258 up to the next integer. */
259 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
260 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
264 /* Propagate the cast into the operation. */
265 if (itype != type && FLOAT_TYPE_P (type))
266 switch (TREE_CODE (expr))
268 /* Convert (float)-x into -(float)x. This is safe for
269 round-to-nearest rounding mode. */
270 case ABS_EXPR:
271 case NEGATE_EXPR:
272 if (!flag_rounding_math
273 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
274 return build1 (TREE_CODE (expr), type,
275 fold (convert_to_real (type,
276 TREE_OPERAND (expr, 0))));
277 break;
278 /* Convert (outertype)((innertype0)a+(innertype1)b)
279 into ((newtype)a+(newtype)b) where newtype
280 is the widest mode from all of these. */
281 case PLUS_EXPR:
282 case MINUS_EXPR:
283 case MULT_EXPR:
284 case RDIV_EXPR:
286 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
287 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
289 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
290 && FLOAT_TYPE_P (TREE_TYPE (arg1))
291 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
293 tree newtype = type;
295 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
296 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
297 || TYPE_MODE (type) == SDmode)
298 newtype = dfloat32_type_node;
299 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
300 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
301 || TYPE_MODE (type) == DDmode)
302 newtype = dfloat64_type_node;
303 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
304 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
305 || TYPE_MODE (type) == TDmode)
306 newtype = dfloat128_type_node;
307 if (newtype == dfloat32_type_node
308 || newtype == dfloat64_type_node
309 || newtype == dfloat128_type_node)
311 expr = build2 (TREE_CODE (expr), newtype,
312 fold (convert_to_real (newtype, arg0)),
313 fold (convert_to_real (newtype, arg1)));
314 if (newtype == type)
315 return expr;
316 break;
319 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
320 newtype = TREE_TYPE (arg0);
321 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
322 newtype = TREE_TYPE (arg1);
323 /* Sometimes this transformation is safe (cannot
324 change results through affecting double rounding
325 cases) and sometimes it is not. If NEWTYPE is
326 wider than TYPE, e.g. (float)((long double)double
327 + (long double)double) converted to
328 (float)(double + double), the transformation is
329 unsafe regardless of the details of the types
330 involved; double rounding can arise if the result
331 of NEWTYPE arithmetic is a NEWTYPE value half way
332 between two representable TYPE values but the
333 exact value is sufficiently different (in the
334 right direction) for this difference to be
335 visible in ITYPE arithmetic. If NEWTYPE is the
336 same as TYPE, however, the transformation may be
337 safe depending on the types involved: it is safe
338 if the ITYPE has strictly more than twice as many
339 mantissa bits as TYPE, can represent infinities
340 and NaNs if the TYPE can, and has sufficient
341 exponent range for the product or ratio of two
342 values representable in the TYPE to be within the
343 range of normal values of ITYPE. */
344 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
345 && (flag_unsafe_math_optimizations
346 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
347 && real_can_shorten_arithmetic (TYPE_MODE (itype),
348 TYPE_MODE (type))
349 && !excess_precision_type (newtype))))
351 expr = build2 (TREE_CODE (expr), newtype,
352 fold (convert_to_real (newtype, arg0)),
353 fold (convert_to_real (newtype, arg1)));
354 if (newtype == type)
355 return expr;
359 break;
360 default:
361 break;
364 switch (TREE_CODE (TREE_TYPE (expr)))
366 case REAL_TYPE:
367 /* Ignore the conversion if we don't need to store intermediate
368 results and neither type is a decimal float. */
369 return build1 ((flag_float_store
370 || DECIMAL_FLOAT_TYPE_P (type)
371 || DECIMAL_FLOAT_TYPE_P (itype))
372 ? CONVERT_EXPR : NOP_EXPR, type, expr);
374 case INTEGER_TYPE:
375 case ENUMERAL_TYPE:
376 case BOOLEAN_TYPE:
377 return build1 (FLOAT_EXPR, type, expr);
379 case FIXED_POINT_TYPE:
380 return build1 (FIXED_CONVERT_EXPR, type, expr);
382 case COMPLEX_TYPE:
383 return convert (type,
384 fold_build1 (REALPART_EXPR,
385 TREE_TYPE (TREE_TYPE (expr)), expr));
387 case POINTER_TYPE:
388 case REFERENCE_TYPE:
389 error ("pointer value used where a floating point value was expected");
390 return convert_to_real (type, integer_zero_node);
392 default:
393 error ("aggregate value used where a float was expected");
394 return convert_to_real (type, integer_zero_node);
398 /* Convert EXPR to some integer (or enum) type TYPE.
400 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
401 fixed-point or vector; in other cases error is called.
403 The result of this is always supposed to be a newly created tree node
404 not in use in any existing structure. */
406 tree
407 convert_to_integer (tree type, tree expr)
409 enum tree_code ex_form = TREE_CODE (expr);
410 tree intype = TREE_TYPE (expr);
411 unsigned int inprec = TYPE_PRECISION (intype);
412 unsigned int outprec = TYPE_PRECISION (type);
414 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
415 be. Consider `enum E = { a, b = (enum E) 3 };'. */
416 if (!COMPLETE_TYPE_P (type))
418 error ("conversion to incomplete type");
419 return error_mark_node;
422 /* Convert e.g. (long)round(d) -> lround(d). */
423 /* If we're converting to char, we may encounter differing behavior
424 between converting from double->char vs double->long->char.
425 We're in "undefined" territory but we prefer to be conservative,
426 so only proceed in "unsafe" math mode. */
427 if (optimize
428 && (flag_unsafe_math_optimizations
429 || (long_integer_type_node
430 && outprec >= TYPE_PRECISION (long_integer_type_node))))
432 tree s_expr = strip_float_extensions (expr);
433 tree s_intype = TREE_TYPE (s_expr);
434 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
435 tree fn = 0;
437 switch (fcode)
439 CASE_FLT_FN (BUILT_IN_CEIL):
440 /* Only convert in ISO C99 mode. */
441 if (!TARGET_C99_FUNCTIONS)
442 break;
443 if (outprec < TYPE_PRECISION (long_integer_type_node)
444 || (outprec == TYPE_PRECISION (long_integer_type_node)
445 && !TYPE_UNSIGNED (type)))
446 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
447 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
448 && !TYPE_UNSIGNED (type))
449 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
450 break;
452 CASE_FLT_FN (BUILT_IN_FLOOR):
453 /* Only convert in ISO C99 mode. */
454 if (!TARGET_C99_FUNCTIONS)
455 break;
456 if (outprec < TYPE_PRECISION (long_integer_type_node)
457 || (outprec == TYPE_PRECISION (long_integer_type_node)
458 && !TYPE_UNSIGNED (type)))
459 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
460 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
461 && !TYPE_UNSIGNED (type))
462 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
463 break;
465 CASE_FLT_FN (BUILT_IN_ROUND):
466 if (outprec < TYPE_PRECISION (long_integer_type_node)
467 || (outprec == TYPE_PRECISION (long_integer_type_node)
468 && !TYPE_UNSIGNED (type)))
469 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
470 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
471 && !TYPE_UNSIGNED (type))
472 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
473 break;
475 CASE_FLT_FN (BUILT_IN_NEARBYINT):
476 /* Only convert nearbyint* if we can ignore math exceptions. */
477 if (flag_trapping_math)
478 break;
479 /* ... Fall through ... */
480 CASE_FLT_FN (BUILT_IN_RINT):
481 if (outprec < TYPE_PRECISION (long_integer_type_node)
482 || (outprec == TYPE_PRECISION (long_integer_type_node)
483 && !TYPE_UNSIGNED (type)))
484 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
485 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
486 && !TYPE_UNSIGNED (type))
487 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
488 break;
490 CASE_FLT_FN (BUILT_IN_TRUNC):
491 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
493 default:
494 break;
497 if (fn)
499 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
500 return convert_to_integer (type, newexpr);
504 /* Convert (int)logb(d) -> ilogb(d). */
505 if (optimize
506 && flag_unsafe_math_optimizations
507 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
508 && integer_type_node
509 && (outprec > TYPE_PRECISION (integer_type_node)
510 || (outprec == TYPE_PRECISION (integer_type_node)
511 && !TYPE_UNSIGNED (type))))
513 tree s_expr = strip_float_extensions (expr);
514 tree s_intype = TREE_TYPE (s_expr);
515 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
516 tree fn = 0;
518 switch (fcode)
520 CASE_FLT_FN (BUILT_IN_LOGB):
521 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
522 break;
524 default:
525 break;
528 if (fn)
530 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
531 return convert_to_integer (type, newexpr);
535 switch (TREE_CODE (intype))
537 case POINTER_TYPE:
538 case REFERENCE_TYPE:
539 if (integer_zerop (expr))
540 return build_int_cst (type, 0);
542 /* Convert to an unsigned integer of the correct width first, and from
543 there widen/truncate to the required type. Some targets support the
544 coexistence of multiple valid pointer sizes, so fetch the one we need
545 from the type. */
546 expr = fold_build1 (CONVERT_EXPR,
547 lang_hooks.types.type_for_size
548 (TYPE_PRECISION (intype), 0),
549 expr);
550 return fold_convert (type, expr);
552 case INTEGER_TYPE:
553 case ENUMERAL_TYPE:
554 case BOOLEAN_TYPE:
555 case OFFSET_TYPE:
556 /* If this is a logical operation, which just returns 0 or 1, we can
557 change the type of the expression. */
559 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
561 expr = copy_node (expr);
562 TREE_TYPE (expr) = type;
563 return expr;
566 /* If we are widening the type, put in an explicit conversion.
567 Similarly if we are not changing the width. After this, we know
568 we are truncating EXPR. */
570 else if (outprec >= inprec)
572 enum tree_code code;
573 tree tem;
575 /* If the precision of the EXPR's type is K bits and the
576 destination mode has more bits, and the sign is changing,
577 it is not safe to use a NOP_EXPR. For example, suppose
578 that EXPR's type is a 3-bit unsigned integer type, the
579 TYPE is a 3-bit signed integer type, and the machine mode
580 for the types is 8-bit QImode. In that case, the
581 conversion necessitates an explicit sign-extension. In
582 the signed-to-unsigned case the high-order bits have to
583 be cleared. */
584 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
585 && (TYPE_PRECISION (TREE_TYPE (expr))
586 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
587 code = CONVERT_EXPR;
588 else
589 code = NOP_EXPR;
591 tem = fold_unary (code, type, expr);
592 if (tem)
593 return tem;
595 tem = build1 (code, type, expr);
596 TREE_NO_WARNING (tem) = 1;
597 return tem;
600 /* If TYPE is an enumeral type or a type with a precision less
601 than the number of bits in its mode, do the conversion to the
602 type corresponding to its mode, then do a nop conversion
603 to TYPE. */
604 else if (TREE_CODE (type) == ENUMERAL_TYPE
605 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
606 return build1 (NOP_EXPR, type,
607 convert (lang_hooks.types.type_for_mode
608 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
609 expr));
611 /* Here detect when we can distribute the truncation down past some
612 arithmetic. For example, if adding two longs and converting to an
613 int, we can equally well convert both to ints and then add.
614 For the operations handled here, such truncation distribution
615 is always safe.
616 It is desirable in these cases:
617 1) when truncating down to full-word from a larger size
618 2) when truncating takes no work.
619 3) when at least one operand of the arithmetic has been extended
620 (as by C's default conversions). In this case we need two conversions
621 if we do the arithmetic as already requested, so we might as well
622 truncate both and then combine. Perhaps that way we need only one.
624 Note that in general we cannot do the arithmetic in a type
625 shorter than the desired result of conversion, even if the operands
626 are both extended from a shorter type, because they might overflow
627 if combined in that type. The exceptions to this--the times when
628 two narrow values can be combined in their narrow type even to
629 make a wider result--are handled by "shorten" in build_binary_op. */
631 switch (ex_form)
633 case RSHIFT_EXPR:
634 /* We can pass truncation down through right shifting
635 when the shift count is a nonpositive constant. */
636 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
637 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
638 goto trunc1;
639 break;
641 case LSHIFT_EXPR:
642 /* We can pass truncation down through left shifting
643 when the shift count is a nonnegative constant and
644 the target type is unsigned. */
645 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
646 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
647 && TYPE_UNSIGNED (type)
648 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
650 /* If shift count is less than the width of the truncated type,
651 really shift. */
652 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
653 /* In this case, shifting is like multiplication. */
654 goto trunc1;
655 else
657 /* If it is >= that width, result is zero.
658 Handling this with trunc1 would give the wrong result:
659 (int) ((long long) a << 32) is well defined (as 0)
660 but (int) a << 32 is undefined and would get a
661 warning. */
663 tree t = build_int_cst (type, 0);
665 /* If the original expression had side-effects, we must
666 preserve it. */
667 if (TREE_SIDE_EFFECTS (expr))
668 return build2 (COMPOUND_EXPR, type, expr, t);
669 else
670 return t;
673 break;
675 case TRUNC_DIV_EXPR:
677 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
678 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
680 /* Don't distribute unless the output precision is at least as big
681 as the actual inputs and it has the same signedness. */
682 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
683 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
684 /* If signedness of arg0 and arg1 don't match,
685 we can't necessarily find a type to compare them in. */
686 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
687 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
688 /* Do not change the sign of the division. */
689 && (TYPE_UNSIGNED (TREE_TYPE (expr))
690 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
691 /* Either require unsigned division or a division by
692 a constant that is not -1. */
693 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
694 || (TREE_CODE (arg1) == INTEGER_CST
695 && !integer_all_onesp (arg1))))
696 goto trunc1;
697 break;
700 case MAX_EXPR:
701 case MIN_EXPR:
702 case MULT_EXPR:
704 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
705 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
707 /* Don't distribute unless the output precision is at least as big
708 as the actual inputs. Otherwise, the comparison of the
709 truncated values will be wrong. */
710 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
711 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
712 /* If signedness of arg0 and arg1 don't match,
713 we can't necessarily find a type to compare them in. */
714 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
715 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
716 goto trunc1;
717 break;
720 case PLUS_EXPR:
721 case MINUS_EXPR:
722 case BIT_AND_EXPR:
723 case BIT_IOR_EXPR:
724 case BIT_XOR_EXPR:
725 trunc1:
727 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
728 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
730 if (outprec >= BITS_PER_WORD
731 || TRULY_NOOP_TRUNCATION (outprec, inprec)
732 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
733 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
735 /* Do the arithmetic in type TYPEX,
736 then convert result to TYPE. */
737 tree typex = type;
739 /* Can't do arithmetic in enumeral types
740 so use an integer type that will hold the values. */
741 if (TREE_CODE (typex) == ENUMERAL_TYPE)
742 typex = lang_hooks.types.type_for_size
743 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
745 /* But now perhaps TYPEX is as wide as INPREC.
746 In that case, do nothing special here.
747 (Otherwise would recurse infinitely in convert. */
748 if (TYPE_PRECISION (typex) != inprec)
750 /* Don't do unsigned arithmetic where signed was wanted,
751 or vice versa.
752 Exception: if both of the original operands were
753 unsigned then we can safely do the work as unsigned.
754 Exception: shift operations take their type solely
755 from the first argument.
756 Exception: the LSHIFT_EXPR case above requires that
757 we perform this operation unsigned lest we produce
758 signed-overflow undefinedness.
759 And we may need to do it as unsigned
760 if we truncate to the original size. */
761 if (TYPE_UNSIGNED (TREE_TYPE (expr))
762 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
763 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
764 || ex_form == LSHIFT_EXPR
765 || ex_form == RSHIFT_EXPR
766 || ex_form == LROTATE_EXPR
767 || ex_form == RROTATE_EXPR))
768 || ex_form == LSHIFT_EXPR
769 /* If we have !flag_wrapv, and either ARG0 or
770 ARG1 is of a signed type, we have to do
771 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
772 type in case the operation in outprec precision
773 could overflow. Otherwise, we would introduce
774 signed-overflow undefinedness. */
775 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
776 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
777 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
778 > outprec)
779 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
780 > outprec))
781 && (ex_form == PLUS_EXPR
782 || ex_form == MINUS_EXPR
783 || ex_form == MULT_EXPR)))
784 typex = unsigned_type_for (typex);
785 else
786 typex = signed_type_for (typex);
787 return convert (type,
788 fold_build2 (ex_form, typex,
789 convert (typex, arg0),
790 convert (typex, arg1)));
794 break;
796 case NEGATE_EXPR:
797 case BIT_NOT_EXPR:
798 /* This is not correct for ABS_EXPR,
799 since we must test the sign before truncation. */
801 tree typex = unsigned_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;