re PR target/35659 (Miscompiled code with -O2 (but not with -O2 -funroll-loops) on...
[official-gcc.git] / gcc / convert.c
blob77907bafcb5e7e1fc799085179d1f3a236c47ca6
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
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"
35 #include "real.h"
36 #include "fixed-value.h"
38 /* Convert EXPR to some pointer or reference type TYPE.
39 EXPR must be pointer, reference, integer, enumeral, or literal zero;
40 in other cases error is called. */
42 tree
43 convert_to_pointer (tree type, tree expr)
45 if (TREE_TYPE (expr) == type)
46 return expr;
48 /* Propagate overflow to the NULL pointer. */
49 if (integer_zerop (expr))
50 return force_fit_type_double (type, 0, 0, 0, TREE_OVERFLOW (expr));
52 switch (TREE_CODE (TREE_TYPE (expr)))
54 case POINTER_TYPE:
55 case REFERENCE_TYPE:
56 return fold_build1 (NOP_EXPR, type, expr);
58 case INTEGER_TYPE:
59 case ENUMERAL_TYPE:
60 case BOOLEAN_TYPE:
61 if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
62 expr = fold_build1 (NOP_EXPR,
63 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
64 expr);
65 return fold_build1 (CONVERT_EXPR, type, expr);
68 default:
69 error ("cannot convert to a pointer type");
70 return convert_to_pointer (type, integer_zero_node);
74 /* Avoid any floating point extensions from EXP. */
75 tree
76 strip_float_extensions (tree exp)
78 tree sub, expt, subt;
80 /* For floating point constant look up the narrowest type that can hold
81 it properly and handle it like (type)(narrowest_type)constant.
82 This way we can optimize for instance a=a*2.0 where "a" is float
83 but 2.0 is double constant. */
84 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
86 REAL_VALUE_TYPE orig;
87 tree type = NULL;
89 orig = TREE_REAL_CST (exp);
90 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
91 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
92 type = float_type_node;
93 else if (TYPE_PRECISION (TREE_TYPE (exp))
94 > TYPE_PRECISION (double_type_node)
95 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
96 type = double_type_node;
97 if (type)
98 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
101 if (!CONVERT_EXPR_P (exp))
102 return exp;
104 sub = TREE_OPERAND (exp, 0);
105 subt = TREE_TYPE (sub);
106 expt = TREE_TYPE (exp);
108 if (!FLOAT_TYPE_P (subt))
109 return exp;
111 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
112 return exp;
114 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
115 return exp;
117 return strip_float_extensions (sub);
121 /* Convert EXPR to some floating-point type TYPE.
123 EXPR must be float, fixed-point, integer, or enumeral;
124 in other cases error is called. */
126 tree
127 convert_to_real (tree type, tree expr)
129 enum built_in_function fcode = builtin_mathfn_code (expr);
130 tree itype = TREE_TYPE (expr);
132 /* Disable until we figure out how to decide whether the functions are
133 present in runtime. */
134 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
135 if (optimize
136 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
137 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
139 switch (fcode)
141 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
142 CASE_MATHFN (ACOS)
143 CASE_MATHFN (ACOSH)
144 CASE_MATHFN (ASIN)
145 CASE_MATHFN (ASINH)
146 CASE_MATHFN (ATAN)
147 CASE_MATHFN (ATANH)
148 CASE_MATHFN (CBRT)
149 CASE_MATHFN (COS)
150 CASE_MATHFN (COSH)
151 CASE_MATHFN (ERF)
152 CASE_MATHFN (ERFC)
153 CASE_MATHFN (EXP)
154 CASE_MATHFN (EXP10)
155 CASE_MATHFN (EXP2)
156 CASE_MATHFN (EXPM1)
157 CASE_MATHFN (FABS)
158 CASE_MATHFN (GAMMA)
159 CASE_MATHFN (J0)
160 CASE_MATHFN (J1)
161 CASE_MATHFN (LGAMMA)
162 CASE_MATHFN (LOG)
163 CASE_MATHFN (LOG10)
164 CASE_MATHFN (LOG1P)
165 CASE_MATHFN (LOG2)
166 CASE_MATHFN (LOGB)
167 CASE_MATHFN (POW10)
168 CASE_MATHFN (SIN)
169 CASE_MATHFN (SINH)
170 CASE_MATHFN (SQRT)
171 CASE_MATHFN (TAN)
172 CASE_MATHFN (TANH)
173 CASE_MATHFN (TGAMMA)
174 CASE_MATHFN (Y0)
175 CASE_MATHFN (Y1)
176 #undef CASE_MATHFN
178 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
179 tree newtype = type;
181 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
182 the both as the safe type for operation. */
183 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
184 newtype = TREE_TYPE (arg0);
186 /* Be careful about integer to fp conversions.
187 These may overflow still. */
188 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
189 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
190 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
191 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
193 tree fn = mathfn_built_in (newtype, fcode);
195 if (fn)
197 tree arg = fold (convert_to_real (newtype, arg0));
198 expr = build_call_expr (fn, 1, arg);
199 if (newtype == type)
200 return expr;
204 default:
205 break;
208 if (optimize
209 && (((fcode == BUILT_IN_FLOORL
210 || fcode == BUILT_IN_CEILL
211 || fcode == BUILT_IN_ROUNDL
212 || fcode == BUILT_IN_RINTL
213 || fcode == BUILT_IN_TRUNCL
214 || fcode == BUILT_IN_NEARBYINTL)
215 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
216 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
217 || ((fcode == BUILT_IN_FLOOR
218 || fcode == BUILT_IN_CEIL
219 || fcode == BUILT_IN_ROUND
220 || fcode == BUILT_IN_RINT
221 || fcode == BUILT_IN_TRUNC
222 || fcode == BUILT_IN_NEARBYINT)
223 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
225 tree fn = mathfn_built_in (type, fcode);
227 if (fn)
229 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
231 /* Make sure (type)arg0 is an extension, otherwise we could end up
232 changing (float)floor(double d) into floorf((float)d), which is
233 incorrect because (float)d uses round-to-nearest and can round
234 up to the next integer. */
235 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
236 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
240 /* Propagate the cast into the operation. */
241 if (itype != type && FLOAT_TYPE_P (type))
242 switch (TREE_CODE (expr))
244 /* Convert (float)-x into -(float)x. This is safe for
245 round-to-nearest rounding mode. */
246 case ABS_EXPR:
247 case NEGATE_EXPR:
248 if (!flag_rounding_math
249 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
250 return build1 (TREE_CODE (expr), type,
251 fold (convert_to_real (type,
252 TREE_OPERAND (expr, 0))));
253 break;
254 /* Convert (outertype)((innertype0)a+(innertype1)b)
255 into ((newtype)a+(newtype)b) where newtype
256 is the widest mode from all of these. */
257 case PLUS_EXPR:
258 case MINUS_EXPR:
259 case MULT_EXPR:
260 case RDIV_EXPR:
262 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
263 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
265 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
266 && FLOAT_TYPE_P (TREE_TYPE (arg1))
267 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
269 tree newtype = type;
271 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
272 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
273 || TYPE_MODE (type) == SDmode)
274 newtype = dfloat32_type_node;
275 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
276 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
277 || TYPE_MODE (type) == DDmode)
278 newtype = dfloat64_type_node;
279 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
280 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
281 || TYPE_MODE (type) == TDmode)
282 newtype = dfloat128_type_node;
283 if (newtype == dfloat32_type_node
284 || newtype == dfloat64_type_node
285 || newtype == dfloat128_type_node)
287 expr = build2 (TREE_CODE (expr), newtype,
288 fold (convert_to_real (newtype, arg0)),
289 fold (convert_to_real (newtype, arg1)));
290 if (newtype == type)
291 return expr;
292 break;
295 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
296 newtype = TREE_TYPE (arg0);
297 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
298 newtype = TREE_TYPE (arg1);
299 /* Sometimes this transformation is safe (cannot
300 change results through affecting double rounding
301 cases) and sometimes it is not. If NEWTYPE is
302 wider than TYPE, e.g. (float)((long double)double
303 + (long double)double) converted to
304 (float)(double + double), the transformation is
305 unsafe regardless of the details of the types
306 involved; double rounding can arise if the result
307 of NEWTYPE arithmetic is a NEWTYPE value half way
308 between two representable TYPE values but the
309 exact value is sufficiently different (in the
310 right direction) for this difference to be
311 visible in ITYPE arithmetic. If NEWTYPE is the
312 same as TYPE, however, the transformation may be
313 safe depending on the types involved: it is safe
314 if the ITYPE has strictly more than twice as many
315 mantissa bits as TYPE, can represent infinities
316 and NaNs if the TYPE can, and has sufficient
317 exponent range for the product or ratio of two
318 values representable in the TYPE to be within the
319 range of normal values of ITYPE. */
320 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
321 && (flag_unsafe_math_optimizations
322 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
323 && real_can_shorten_arithmetic (TYPE_MODE (itype),
324 TYPE_MODE (type)))))
326 expr = build2 (TREE_CODE (expr), newtype,
327 fold (convert_to_real (newtype, arg0)),
328 fold (convert_to_real (newtype, arg1)));
329 if (newtype == type)
330 return expr;
334 break;
335 default:
336 break;
339 switch (TREE_CODE (TREE_TYPE (expr)))
341 case REAL_TYPE:
342 /* Ignore the conversion if we don't need to store intermediate
343 results and neither type is a decimal float. */
344 return build1 ((flag_float_store
345 || DECIMAL_FLOAT_TYPE_P (type)
346 || DECIMAL_FLOAT_TYPE_P (itype))
347 ? CONVERT_EXPR : NOP_EXPR, type, expr);
349 case INTEGER_TYPE:
350 case ENUMERAL_TYPE:
351 case BOOLEAN_TYPE:
352 return build1 (FLOAT_EXPR, type, expr);
354 case FIXED_POINT_TYPE:
355 return build1 (FIXED_CONVERT_EXPR, type, expr);
357 case COMPLEX_TYPE:
358 return convert (type,
359 fold_build1 (REALPART_EXPR,
360 TREE_TYPE (TREE_TYPE (expr)), expr));
362 case POINTER_TYPE:
363 case REFERENCE_TYPE:
364 error ("pointer value used where a floating point value was expected");
365 return convert_to_real (type, integer_zero_node);
367 default:
368 error ("aggregate value used where a float was expected");
369 return convert_to_real (type, integer_zero_node);
373 /* Convert EXPR to some integer (or enum) type TYPE.
375 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
376 fixed-point or vector; in other cases error is called.
378 The result of this is always supposed to be a newly created tree node
379 not in use in any existing structure. */
381 tree
382 convert_to_integer (tree type, tree expr)
384 enum tree_code ex_form = TREE_CODE (expr);
385 tree intype = TREE_TYPE (expr);
386 unsigned int inprec = TYPE_PRECISION (intype);
387 unsigned int outprec = TYPE_PRECISION (type);
389 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
390 be. Consider `enum E = { a, b = (enum E) 3 };'. */
391 if (!COMPLETE_TYPE_P (type))
393 error ("conversion to incomplete type");
394 return error_mark_node;
397 /* Convert e.g. (long)round(d) -> lround(d). */
398 /* If we're converting to char, we may encounter differing behavior
399 between converting from double->char vs double->long->char.
400 We're in "undefined" territory but we prefer to be conservative,
401 so only proceed in "unsafe" math mode. */
402 if (optimize
403 && (flag_unsafe_math_optimizations
404 || (long_integer_type_node
405 && outprec >= TYPE_PRECISION (long_integer_type_node))))
407 tree s_expr = strip_float_extensions (expr);
408 tree s_intype = TREE_TYPE (s_expr);
409 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
410 tree fn = 0;
412 switch (fcode)
414 CASE_FLT_FN (BUILT_IN_CEIL):
415 /* Only convert in ISO C99 mode. */
416 if (!TARGET_C99_FUNCTIONS)
417 break;
418 if (outprec < TYPE_PRECISION (long_integer_type_node)
419 || (outprec == TYPE_PRECISION (long_integer_type_node)
420 && !TYPE_UNSIGNED (type)))
421 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
422 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
423 && !TYPE_UNSIGNED (type))
424 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
425 break;
427 CASE_FLT_FN (BUILT_IN_FLOOR):
428 /* Only convert in ISO C99 mode. */
429 if (!TARGET_C99_FUNCTIONS)
430 break;
431 if (outprec < TYPE_PRECISION (long_integer_type_node)
432 || (outprec == TYPE_PRECISION (long_integer_type_node)
433 && !TYPE_UNSIGNED (type)))
434 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
435 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
436 && !TYPE_UNSIGNED (type))
437 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
438 break;
440 CASE_FLT_FN (BUILT_IN_ROUND):
441 if (outprec < TYPE_PRECISION (long_integer_type_node)
442 || (outprec == TYPE_PRECISION (long_integer_type_node)
443 && !TYPE_UNSIGNED (type)))
444 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
445 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
446 && !TYPE_UNSIGNED (type))
447 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
448 break;
450 CASE_FLT_FN (BUILT_IN_NEARBYINT):
451 /* Only convert nearbyint* if we can ignore math exceptions. */
452 if (flag_trapping_math)
453 break;
454 /* ... Fall through ... */
455 CASE_FLT_FN (BUILT_IN_RINT):
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_LRINT);
460 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
461 && !TYPE_UNSIGNED (type))
462 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
463 break;
465 CASE_FLT_FN (BUILT_IN_TRUNC):
466 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
468 default:
469 break;
472 if (fn)
474 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
475 return convert_to_integer (type, newexpr);
479 switch (TREE_CODE (intype))
481 case POINTER_TYPE:
482 case REFERENCE_TYPE:
483 if (integer_zerop (expr))
484 return build_int_cst (type, 0);
486 /* Convert to an unsigned integer of the correct width first,
487 and from there widen/truncate to the required type. */
488 expr = fold_build1 (CONVERT_EXPR,
489 lang_hooks.types.type_for_size (POINTER_SIZE, 0),
490 expr);
491 return fold_convert (type, expr);
493 case INTEGER_TYPE:
494 case ENUMERAL_TYPE:
495 case BOOLEAN_TYPE:
496 case OFFSET_TYPE:
497 /* If this is a logical operation, which just returns 0 or 1, we can
498 change the type of the expression. */
500 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
502 expr = copy_node (expr);
503 TREE_TYPE (expr) = type;
504 return expr;
507 /* If we are widening the type, put in an explicit conversion.
508 Similarly if we are not changing the width. After this, we know
509 we are truncating EXPR. */
511 else if (outprec >= inprec)
513 enum tree_code code;
514 tree tem;
516 /* If the precision of the EXPR's type is K bits and the
517 destination mode has more bits, and the sign is changing,
518 it is not safe to use a NOP_EXPR. For example, suppose
519 that EXPR's type is a 3-bit unsigned integer type, the
520 TYPE is a 3-bit signed integer type, and the machine mode
521 for the types is 8-bit QImode. In that case, the
522 conversion necessitates an explicit sign-extension. In
523 the signed-to-unsigned case the high-order bits have to
524 be cleared. */
525 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
526 && (TYPE_PRECISION (TREE_TYPE (expr))
527 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)))))
528 code = CONVERT_EXPR;
529 else
530 code = NOP_EXPR;
532 tem = fold_unary (code, type, expr);
533 if (tem)
534 return tem;
536 tem = build1 (code, type, expr);
537 TREE_NO_WARNING (tem) = 1;
538 return tem;
541 /* If TYPE is an enumeral type or a type with a precision less
542 than the number of bits in its mode, do the conversion to the
543 type corresponding to its mode, then do a nop conversion
544 to TYPE. */
545 else if (TREE_CODE (type) == ENUMERAL_TYPE
546 || outprec != GET_MODE_BITSIZE (TYPE_MODE (type)))
547 return build1 (NOP_EXPR, type,
548 convert (lang_hooks.types.type_for_mode
549 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
550 expr));
552 /* Here detect when we can distribute the truncation down past some
553 arithmetic. For example, if adding two longs and converting to an
554 int, we can equally well convert both to ints and then add.
555 For the operations handled here, such truncation distribution
556 is always safe.
557 It is desirable in these cases:
558 1) when truncating down to full-word from a larger size
559 2) when truncating takes no work.
560 3) when at least one operand of the arithmetic has been extended
561 (as by C's default conversions). In this case we need two conversions
562 if we do the arithmetic as already requested, so we might as well
563 truncate both and then combine. Perhaps that way we need only one.
565 Note that in general we cannot do the arithmetic in a type
566 shorter than the desired result of conversion, even if the operands
567 are both extended from a shorter type, because they might overflow
568 if combined in that type. The exceptions to this--the times when
569 two narrow values can be combined in their narrow type even to
570 make a wider result--are handled by "shorten" in build_binary_op. */
572 switch (ex_form)
574 case RSHIFT_EXPR:
575 /* We can pass truncation down through right shifting
576 when the shift count is a nonpositive constant. */
577 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
578 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
579 goto trunc1;
580 break;
582 case LSHIFT_EXPR:
583 /* We can pass truncation down through left shifting
584 when the shift count is a nonnegative constant and
585 the target type is unsigned. */
586 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
587 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
588 && TYPE_UNSIGNED (type)
589 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
591 /* If shift count is less than the width of the truncated type,
592 really shift. */
593 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
594 /* In this case, shifting is like multiplication. */
595 goto trunc1;
596 else
598 /* If it is >= that width, result is zero.
599 Handling this with trunc1 would give the wrong result:
600 (int) ((long long) a << 32) is well defined (as 0)
601 but (int) a << 32 is undefined and would get a
602 warning. */
604 tree t = build_int_cst (type, 0);
606 /* If the original expression had side-effects, we must
607 preserve it. */
608 if (TREE_SIDE_EFFECTS (expr))
609 return build2 (COMPOUND_EXPR, type, expr, t);
610 else
611 return t;
614 break;
616 case MAX_EXPR:
617 case MIN_EXPR:
618 case MULT_EXPR:
620 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
621 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
623 /* Don't distribute unless the output precision is at least as big
624 as the actual inputs. Otherwise, the comparison of the
625 truncated values will be wrong. */
626 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
627 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
628 /* If signedness of arg0 and arg1 don't match,
629 we can't necessarily find a type to compare them in. */
630 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
631 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
632 goto trunc1;
633 break;
636 case PLUS_EXPR:
637 case MINUS_EXPR:
638 case BIT_AND_EXPR:
639 case BIT_IOR_EXPR:
640 case BIT_XOR_EXPR:
641 trunc1:
643 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
644 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
646 if (outprec >= BITS_PER_WORD
647 || TRULY_NOOP_TRUNCATION (outprec, inprec)
648 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
649 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
651 /* Do the arithmetic in type TYPEX,
652 then convert result to TYPE. */
653 tree typex = type;
655 /* Can't do arithmetic in enumeral types
656 so use an integer type that will hold the values. */
657 if (TREE_CODE (typex) == ENUMERAL_TYPE)
658 typex = lang_hooks.types.type_for_size
659 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
661 /* But now perhaps TYPEX is as wide as INPREC.
662 In that case, do nothing special here.
663 (Otherwise would recurse infinitely in convert. */
664 if (TYPE_PRECISION (typex) != inprec)
666 /* Don't do unsigned arithmetic where signed was wanted,
667 or vice versa.
668 Exception: if both of the original operands were
669 unsigned then we can safely do the work as unsigned.
670 Exception: shift operations take their type solely
671 from the first argument.
672 Exception: the LSHIFT_EXPR case above requires that
673 we perform this operation unsigned lest we produce
674 signed-overflow undefinedness.
675 And we may need to do it as unsigned
676 if we truncate to the original size. */
677 if (TYPE_UNSIGNED (TREE_TYPE (expr))
678 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
679 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
680 || ex_form == LSHIFT_EXPR
681 || ex_form == RSHIFT_EXPR
682 || ex_form == LROTATE_EXPR
683 || ex_form == RROTATE_EXPR))
684 || ex_form == LSHIFT_EXPR
685 /* If we have !flag_wrapv, and either ARG0 or
686 ARG1 is of a signed type, we have to do
687 PLUS_EXPR or MINUS_EXPR in an unsigned
688 type. Otherwise, we would introduce
689 signed-overflow undefinedness. */
690 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
691 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
692 && (ex_form == PLUS_EXPR
693 || ex_form == MINUS_EXPR)))
694 typex = unsigned_type_for (typex);
695 else
696 typex = signed_type_for (typex);
697 return convert (type,
698 fold_build2 (ex_form, typex,
699 convert (typex, arg0),
700 convert (typex, arg1)));
704 break;
706 case NEGATE_EXPR:
707 case BIT_NOT_EXPR:
708 /* This is not correct for ABS_EXPR,
709 since we must test the sign before truncation. */
711 tree typex;
713 /* Don't do unsigned arithmetic where signed was wanted,
714 or vice versa. */
715 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
716 typex = unsigned_type_for (type);
717 else
718 typex = signed_type_for (type);
719 return convert (type,
720 fold_build1 (ex_form, typex,
721 convert (typex,
722 TREE_OPERAND (expr, 0))));
725 case NOP_EXPR:
726 /* Don't introduce a
727 "can't convert between vector values of different size" error. */
728 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
729 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
730 != GET_MODE_SIZE (TYPE_MODE (type))))
731 break;
732 /* If truncating after truncating, might as well do all at once.
733 If truncating after extending, we may get rid of wasted work. */
734 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
736 case COND_EXPR:
737 /* It is sometimes worthwhile to push the narrowing down through
738 the conditional and never loses. */
739 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
740 convert (type, TREE_OPERAND (expr, 1)),
741 convert (type, TREE_OPERAND (expr, 2)));
743 default:
744 break;
747 return build1 (CONVERT_EXPR, type, expr);
749 case REAL_TYPE:
750 return build1 (FIX_TRUNC_EXPR, type, expr);
752 case FIXED_POINT_TYPE:
753 return build1 (FIXED_CONVERT_EXPR, type, expr);
755 case COMPLEX_TYPE:
756 return convert (type,
757 fold_build1 (REALPART_EXPR,
758 TREE_TYPE (TREE_TYPE (expr)), expr));
760 case VECTOR_TYPE:
761 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
763 error ("can't convert between vector values of different size");
764 return error_mark_node;
766 return build1 (VIEW_CONVERT_EXPR, type, expr);
768 default:
769 error ("aggregate value used where an integer was expected");
770 return convert (type, integer_zero_node);
774 /* Convert EXPR to the complex type TYPE in the usual ways. */
776 tree
777 convert_to_complex (tree type, tree expr)
779 tree subtype = TREE_TYPE (type);
781 switch (TREE_CODE (TREE_TYPE (expr)))
783 case REAL_TYPE:
784 case FIXED_POINT_TYPE:
785 case INTEGER_TYPE:
786 case ENUMERAL_TYPE:
787 case BOOLEAN_TYPE:
788 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
789 convert (subtype, integer_zero_node));
791 case COMPLEX_TYPE:
793 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
795 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
796 return expr;
797 else if (TREE_CODE (expr) == COMPLEX_EXPR)
798 return fold_build2 (COMPLEX_EXPR, type,
799 convert (subtype, TREE_OPERAND (expr, 0)),
800 convert (subtype, TREE_OPERAND (expr, 1)));
801 else
803 expr = save_expr (expr);
804 return
805 fold_build2 (COMPLEX_EXPR, type,
806 convert (subtype,
807 fold_build1 (REALPART_EXPR,
808 TREE_TYPE (TREE_TYPE (expr)),
809 expr)),
810 convert (subtype,
811 fold_build1 (IMAGPART_EXPR,
812 TREE_TYPE (TREE_TYPE (expr)),
813 expr)));
817 case POINTER_TYPE:
818 case REFERENCE_TYPE:
819 error ("pointer value used where a complex was expected");
820 return convert_to_complex (type, integer_zero_node);
822 default:
823 error ("aggregate value used where a complex was expected");
824 return convert_to_complex (type, integer_zero_node);
828 /* Convert EXPR to the vector type TYPE in the usual ways. */
830 tree
831 convert_to_vector (tree type, tree expr)
833 switch (TREE_CODE (TREE_TYPE (expr)))
835 case INTEGER_TYPE:
836 case VECTOR_TYPE:
837 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
839 error ("can't convert between vector values of different size");
840 return error_mark_node;
842 return build1 (VIEW_CONVERT_EXPR, type, expr);
844 default:
845 error ("can't convert value to a vector");
846 return error_mark_node;
850 /* Convert EXPR to some fixed-point type TYPE.
852 EXPR must be fixed-point, float, integer, or enumeral;
853 in other cases error is called. */
855 tree
856 convert_to_fixed (tree type, tree expr)
858 if (integer_zerop (expr))
860 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
861 return fixed_zero_node;
863 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
865 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
866 return fixed_one_node;
869 switch (TREE_CODE (TREE_TYPE (expr)))
871 case FIXED_POINT_TYPE:
872 case INTEGER_TYPE:
873 case ENUMERAL_TYPE:
874 case BOOLEAN_TYPE:
875 case REAL_TYPE:
876 return build1 (FIXED_CONVERT_EXPR, type, expr);
878 case COMPLEX_TYPE:
879 return convert (type,
880 fold_build1 (REALPART_EXPR,
881 TREE_TYPE (TREE_TYPE (expr)), expr));
883 default:
884 error ("aggregate value used where a fixed-point was expected");
885 return error_mark_node;