* c-family/array-notation-common.c (length_mismatch_in_expr_p): Delete
[official-gcc.git] / gcc / convert.c
blob62ff224b5663707166901341cedb942d69761e52
1 /* Utility routines for data type conversion for GCC.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* These routines are somewhat language-independent utility function
22 intended to be called by the language-specific convert () functions. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "convert.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
34 /* Convert EXPR to some pointer or reference type TYPE.
35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
36 in other cases error is called. */
38 tree
39 convert_to_pointer (tree type, tree expr)
41 location_t loc = EXPR_LOCATION (expr);
42 if (TREE_TYPE (expr) == type)
43 return expr;
45 switch (TREE_CODE (TREE_TYPE (expr)))
47 case POINTER_TYPE:
48 case REFERENCE_TYPE:
50 /* If the pointers point to different address spaces, conversion needs
51 to be done via a ADDR_SPACE_CONVERT_EXPR instead of a NOP_EXPR. */
52 addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (type));
53 addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (expr)));
55 if (to_as == from_as)
56 return fold_build1_loc (loc, NOP_EXPR, type, expr);
57 else
58 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, expr);
61 case INTEGER_TYPE:
62 case ENUMERAL_TYPE:
63 case BOOLEAN_TYPE:
65 /* If the input precision differs from the target pointer type
66 precision, first convert the input expression to an integer type of
67 the target precision. Some targets, e.g. VMS, need several pointer
68 sizes to coexist so the latter isn't necessarily POINTER_SIZE. */
69 unsigned int pprec = TYPE_PRECISION (type);
70 unsigned int eprec = TYPE_PRECISION (TREE_TYPE (expr));
72 if (eprec != pprec)
73 expr = fold_build1_loc (loc, NOP_EXPR,
74 lang_hooks.types.type_for_size (pprec, 0),
75 expr);
78 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
80 default:
81 error ("cannot convert to a pointer type");
82 return convert_to_pointer (type, integer_zero_node);
87 /* Convert EXPR to some floating-point type TYPE.
89 EXPR must be float, fixed-point, integer, or enumeral;
90 in other cases error is called. */
92 tree
93 convert_to_real (tree type, tree expr)
95 enum built_in_function fcode = builtin_mathfn_code (expr);
96 tree itype = TREE_TYPE (expr);
98 /* Disable until we figure out how to decide whether the functions are
99 present in runtime. */
100 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
101 if (optimize
102 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
103 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
105 switch (fcode)
107 #define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
108 CASE_MATHFN (COSH)
109 CASE_MATHFN (EXP)
110 CASE_MATHFN (EXP10)
111 CASE_MATHFN (EXP2)
112 CASE_MATHFN (EXPM1)
113 CASE_MATHFN (GAMMA)
114 CASE_MATHFN (J0)
115 CASE_MATHFN (J1)
116 CASE_MATHFN (LGAMMA)
117 CASE_MATHFN (POW10)
118 CASE_MATHFN (SINH)
119 CASE_MATHFN (TGAMMA)
120 CASE_MATHFN (Y0)
121 CASE_MATHFN (Y1)
122 /* The above functions may set errno differently with float
123 input or output so this transformation is not safe with
124 -fmath-errno. */
125 if (flag_errno_math)
126 break;
127 CASE_MATHFN (ACOS)
128 CASE_MATHFN (ACOSH)
129 CASE_MATHFN (ASIN)
130 CASE_MATHFN (ASINH)
131 CASE_MATHFN (ATAN)
132 CASE_MATHFN (ATANH)
133 CASE_MATHFN (CBRT)
134 CASE_MATHFN (COS)
135 CASE_MATHFN (ERF)
136 CASE_MATHFN (ERFC)
137 CASE_MATHFN (FABS)
138 CASE_MATHFN (LOG)
139 CASE_MATHFN (LOG10)
140 CASE_MATHFN (LOG2)
141 CASE_MATHFN (LOG1P)
142 CASE_MATHFN (LOGB)
143 CASE_MATHFN (SIN)
144 CASE_MATHFN (SQRT)
145 CASE_MATHFN (TAN)
146 CASE_MATHFN (TANH)
147 #undef CASE_MATHFN
149 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
150 tree newtype = type;
152 /* We have (outertype)sqrt((innertype)x). Choose the wider mode from
153 the both as the safe type for operation. */
154 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (type))
155 newtype = TREE_TYPE (arg0);
157 /* Be careful about integer to fp conversions.
158 These may overflow still. */
159 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
160 && TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
161 && (TYPE_MODE (newtype) == TYPE_MODE (double_type_node)
162 || TYPE_MODE (newtype) == TYPE_MODE (float_type_node)))
164 tree fn = mathfn_built_in (newtype, fcode);
166 if (fn)
168 tree arg = fold (convert_to_real (newtype, arg0));
169 expr = build_call_expr (fn, 1, arg);
170 if (newtype == type)
171 return expr;
175 default:
176 break;
179 if (optimize
180 && (((fcode == BUILT_IN_FLOORL
181 || fcode == BUILT_IN_CEILL
182 || fcode == BUILT_IN_ROUNDL
183 || fcode == BUILT_IN_RINTL
184 || fcode == BUILT_IN_TRUNCL
185 || fcode == BUILT_IN_NEARBYINTL)
186 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
187 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
188 || ((fcode == BUILT_IN_FLOOR
189 || fcode == BUILT_IN_CEIL
190 || fcode == BUILT_IN_ROUND
191 || fcode == BUILT_IN_RINT
192 || fcode == BUILT_IN_TRUNC
193 || fcode == BUILT_IN_NEARBYINT)
194 && (TYPE_MODE (type) == TYPE_MODE (float_type_node)))))
196 tree fn = mathfn_built_in (type, fcode);
198 if (fn)
200 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
202 /* Make sure (type)arg0 is an extension, otherwise we could end up
203 changing (float)floor(double d) into floorf((float)d), which is
204 incorrect because (float)d uses round-to-nearest and can round
205 up to the next integer. */
206 if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (arg)))
207 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
211 /* Propagate the cast into the operation. */
212 if (itype != type && FLOAT_TYPE_P (type))
213 switch (TREE_CODE (expr))
215 /* Convert (float)-x into -(float)x. This is safe for
216 round-to-nearest rounding mode. */
217 case ABS_EXPR:
218 case NEGATE_EXPR:
219 if (!flag_rounding_math
220 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
221 return build1 (TREE_CODE (expr), type,
222 fold (convert_to_real (type,
223 TREE_OPERAND (expr, 0))));
224 break;
225 /* Convert (outertype)((innertype0)a+(innertype1)b)
226 into ((newtype)a+(newtype)b) where newtype
227 is the widest mode from all of these. */
228 case PLUS_EXPR:
229 case MINUS_EXPR:
230 case MULT_EXPR:
231 case RDIV_EXPR:
233 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
234 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
236 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
237 && FLOAT_TYPE_P (TREE_TYPE (arg1))
238 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
240 tree newtype = type;
242 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
243 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
244 || TYPE_MODE (type) == SDmode)
245 newtype = dfloat32_type_node;
246 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
247 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
248 || TYPE_MODE (type) == DDmode)
249 newtype = dfloat64_type_node;
250 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
251 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
252 || TYPE_MODE (type) == TDmode)
253 newtype = dfloat128_type_node;
254 if (newtype == dfloat32_type_node
255 || newtype == dfloat64_type_node
256 || newtype == dfloat128_type_node)
258 expr = build2 (TREE_CODE (expr), newtype,
259 fold (convert_to_real (newtype, arg0)),
260 fold (convert_to_real (newtype, arg1)));
261 if (newtype == type)
262 return expr;
263 break;
266 if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
267 newtype = TREE_TYPE (arg0);
268 if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
269 newtype = TREE_TYPE (arg1);
270 /* Sometimes this transformation is safe (cannot
271 change results through affecting double rounding
272 cases) and sometimes it is not. If NEWTYPE is
273 wider than TYPE, e.g. (float)((long double)double
274 + (long double)double) converted to
275 (float)(double + double), the transformation is
276 unsafe regardless of the details of the types
277 involved; double rounding can arise if the result
278 of NEWTYPE arithmetic is a NEWTYPE value half way
279 between two representable TYPE values but the
280 exact value is sufficiently different (in the
281 right direction) for this difference to be
282 visible in ITYPE arithmetic. If NEWTYPE is the
283 same as TYPE, however, the transformation may be
284 safe depending on the types involved: it is safe
285 if the ITYPE has strictly more than twice as many
286 mantissa bits as TYPE, can represent infinities
287 and NaNs if the TYPE can, and has sufficient
288 exponent range for the product or ratio of two
289 values representable in the TYPE to be within the
290 range of normal values of ITYPE. */
291 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
292 && (flag_unsafe_math_optimizations
293 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
294 && real_can_shorten_arithmetic (TYPE_MODE (itype),
295 TYPE_MODE (type))
296 && !excess_precision_type (newtype))))
298 expr = build2 (TREE_CODE (expr), newtype,
299 fold (convert_to_real (newtype, arg0)),
300 fold (convert_to_real (newtype, arg1)));
301 if (newtype == type)
302 return expr;
306 break;
307 default:
308 break;
311 switch (TREE_CODE (TREE_TYPE (expr)))
313 case REAL_TYPE:
314 /* Ignore the conversion if we don't need to store intermediate
315 results and neither type is a decimal float. */
316 return build1 ((flag_float_store
317 || DECIMAL_FLOAT_TYPE_P (type)
318 || DECIMAL_FLOAT_TYPE_P (itype))
319 ? CONVERT_EXPR : NOP_EXPR, type, expr);
321 case INTEGER_TYPE:
322 case ENUMERAL_TYPE:
323 case BOOLEAN_TYPE:
324 return build1 (FLOAT_EXPR, type, expr);
326 case FIXED_POINT_TYPE:
327 return build1 (FIXED_CONVERT_EXPR, type, expr);
329 case COMPLEX_TYPE:
330 return convert (type,
331 fold_build1 (REALPART_EXPR,
332 TREE_TYPE (TREE_TYPE (expr)), expr));
334 case POINTER_TYPE:
335 case REFERENCE_TYPE:
336 error ("pointer value used where a floating point value was expected");
337 return convert_to_real (type, integer_zero_node);
339 default:
340 error ("aggregate value used where a float was expected");
341 return convert_to_real (type, integer_zero_node);
345 /* Convert EXPR to some integer (or enum) type TYPE.
347 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
348 fixed-point or vector; in other cases error is called.
350 The result of this is always supposed to be a newly created tree node
351 not in use in any existing structure. */
353 tree
354 convert_to_integer (tree type, tree expr)
356 enum tree_code ex_form = TREE_CODE (expr);
357 tree intype = TREE_TYPE (expr);
358 unsigned int inprec = element_precision (intype);
359 unsigned int outprec = element_precision (type);
361 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
362 be. Consider `enum E = { a, b = (enum E) 3 };'. */
363 if (!COMPLETE_TYPE_P (type))
365 error ("conversion to incomplete type");
366 return error_mark_node;
369 /* Convert e.g. (long)round(d) -> lround(d). */
370 /* If we're converting to char, we may encounter differing behavior
371 between converting from double->char vs double->long->char.
372 We're in "undefined" territory but we prefer to be conservative,
373 so only proceed in "unsafe" math mode. */
374 if (optimize
375 && (flag_unsafe_math_optimizations
376 || (long_integer_type_node
377 && outprec >= TYPE_PRECISION (long_integer_type_node))))
379 tree s_expr = strip_float_extensions (expr);
380 tree s_intype = TREE_TYPE (s_expr);
381 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
382 tree fn = 0;
384 switch (fcode)
386 CASE_FLT_FN (BUILT_IN_CEIL):
387 /* Only convert in ISO C99 mode. */
388 if (!TARGET_C99_FUNCTIONS)
389 break;
390 if (outprec < TYPE_PRECISION (integer_type_node)
391 || (outprec == TYPE_PRECISION (integer_type_node)
392 && !TYPE_UNSIGNED (type)))
393 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
394 else if (outprec == TYPE_PRECISION (long_integer_type_node)
395 && !TYPE_UNSIGNED (type))
396 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
397 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
398 && !TYPE_UNSIGNED (type))
399 fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
400 break;
402 CASE_FLT_FN (BUILT_IN_FLOOR):
403 /* Only convert in ISO C99 mode. */
404 if (!TARGET_C99_FUNCTIONS)
405 break;
406 if (outprec < TYPE_PRECISION (integer_type_node)
407 || (outprec == TYPE_PRECISION (integer_type_node)
408 && !TYPE_UNSIGNED (type)))
409 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
410 else if (outprec == TYPE_PRECISION (long_integer_type_node)
411 && !TYPE_UNSIGNED (type))
412 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
413 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
414 && !TYPE_UNSIGNED (type))
415 fn = mathfn_built_in (s_intype, BUILT_IN_LLFLOOR);
416 break;
418 CASE_FLT_FN (BUILT_IN_ROUND):
419 /* Only convert in ISO C99 mode. */
420 if (!TARGET_C99_FUNCTIONS)
421 break;
422 if (outprec < TYPE_PRECISION (integer_type_node)
423 || (outprec == TYPE_PRECISION (integer_type_node)
424 && !TYPE_UNSIGNED (type)))
425 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
426 else if (outprec == TYPE_PRECISION (long_integer_type_node)
427 && !TYPE_UNSIGNED (type))
428 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
429 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
430 && !TYPE_UNSIGNED (type))
431 fn = mathfn_built_in (s_intype, BUILT_IN_LLROUND);
432 break;
434 CASE_FLT_FN (BUILT_IN_NEARBYINT):
435 /* Only convert nearbyint* if we can ignore math exceptions. */
436 if (flag_trapping_math)
437 break;
438 /* ... Fall through ... */
439 CASE_FLT_FN (BUILT_IN_RINT):
440 /* Only convert in ISO C99 mode. */
441 if (!TARGET_C99_FUNCTIONS)
442 break;
443 if (outprec < TYPE_PRECISION (integer_type_node)
444 || (outprec == TYPE_PRECISION (integer_type_node)
445 && !TYPE_UNSIGNED (type)))
446 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
447 else if (outprec == TYPE_PRECISION (long_integer_type_node)
448 && !TYPE_UNSIGNED (type))
449 fn = mathfn_built_in (s_intype, BUILT_IN_LRINT);
450 else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
451 && !TYPE_UNSIGNED (type))
452 fn = mathfn_built_in (s_intype, BUILT_IN_LLRINT);
453 break;
455 CASE_FLT_FN (BUILT_IN_TRUNC):
456 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
458 default:
459 break;
462 if (fn)
464 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
465 return convert_to_integer (type, newexpr);
469 /* Convert (int)logb(d) -> ilogb(d). */
470 if (optimize
471 && flag_unsafe_math_optimizations
472 && !flag_trapping_math && !flag_errno_math && flag_finite_math_only
473 && integer_type_node
474 && (outprec > TYPE_PRECISION (integer_type_node)
475 || (outprec == TYPE_PRECISION (integer_type_node)
476 && !TYPE_UNSIGNED (type))))
478 tree s_expr = strip_float_extensions (expr);
479 tree s_intype = TREE_TYPE (s_expr);
480 const enum built_in_function fcode = builtin_mathfn_code (s_expr);
481 tree fn = 0;
483 switch (fcode)
485 CASE_FLT_FN (BUILT_IN_LOGB):
486 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
487 break;
489 default:
490 break;
493 if (fn)
495 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
496 return convert_to_integer (type, newexpr);
500 switch (TREE_CODE (intype))
502 case POINTER_TYPE:
503 case REFERENCE_TYPE:
504 if (integer_zerop (expr))
505 return build_int_cst (type, 0);
507 /* Convert to an unsigned integer of the correct width first, and from
508 there widen/truncate to the required type. Some targets support the
509 coexistence of multiple valid pointer sizes, so fetch the one we need
510 from the type. */
511 expr = fold_build1 (CONVERT_EXPR,
512 lang_hooks.types.type_for_size
513 (TYPE_PRECISION (intype), 0),
514 expr);
515 return fold_convert (type, expr);
517 case INTEGER_TYPE:
518 case ENUMERAL_TYPE:
519 case BOOLEAN_TYPE:
520 case OFFSET_TYPE:
521 /* If this is a logical operation, which just returns 0 or 1, we can
522 change the type of the expression. */
524 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
526 expr = copy_node (expr);
527 TREE_TYPE (expr) = type;
528 return expr;
531 /* If we are widening the type, put in an explicit conversion.
532 Similarly if we are not changing the width. After this, we know
533 we are truncating EXPR. */
535 else if (outprec >= inprec)
537 enum tree_code code;
539 /* If the precision of the EXPR's type is K bits and the
540 destination mode has more bits, and the sign is changing,
541 it is not safe to use a NOP_EXPR. For example, suppose
542 that EXPR's type is a 3-bit unsigned integer type, the
543 TYPE is a 3-bit signed integer type, and the machine mode
544 for the types is 8-bit QImode. In that case, the
545 conversion necessitates an explicit sign-extension. In
546 the signed-to-unsigned case the high-order bits have to
547 be cleared. */
548 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
549 && (TYPE_PRECISION (TREE_TYPE (expr))
550 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
551 code = CONVERT_EXPR;
552 else
553 code = NOP_EXPR;
555 return fold_build1 (code, type, expr);
558 /* If TYPE is an enumeral type or a type with a precision less
559 than the number of bits in its mode, do the conversion to the
560 type corresponding to its mode, then do a nop conversion
561 to TYPE. */
562 else if (TREE_CODE (type) == ENUMERAL_TYPE
563 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
564 return build1 (NOP_EXPR, type,
565 convert (lang_hooks.types.type_for_mode
566 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
567 expr));
569 /* Here detect when we can distribute the truncation down past some
570 arithmetic. For example, if adding two longs and converting to an
571 int, we can equally well convert both to ints and then add.
572 For the operations handled here, such truncation distribution
573 is always safe.
574 It is desirable in these cases:
575 1) when truncating down to full-word from a larger size
576 2) when truncating takes no work.
577 3) when at least one operand of the arithmetic has been extended
578 (as by C's default conversions). In this case we need two conversions
579 if we do the arithmetic as already requested, so we might as well
580 truncate both and then combine. Perhaps that way we need only one.
582 Note that in general we cannot do the arithmetic in a type
583 shorter than the desired result of conversion, even if the operands
584 are both extended from a shorter type, because they might overflow
585 if combined in that type. The exceptions to this--the times when
586 two narrow values can be combined in their narrow type even to
587 make a wider result--are handled by "shorten" in build_binary_op. */
589 switch (ex_form)
591 case RSHIFT_EXPR:
592 /* We can pass truncation down through right shifting
593 when the shift count is a nonpositive constant. */
594 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
595 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
596 goto trunc1;
597 break;
599 case LSHIFT_EXPR:
600 /* We can pass truncation down through left shifting
601 when the shift count is a nonnegative constant and
602 the target type is unsigned. */
603 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
604 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
605 && TYPE_UNSIGNED (type)
606 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
608 /* If shift count is less than the width of the truncated type,
609 really shift. */
610 if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
611 /* In this case, shifting is like multiplication. */
612 goto trunc1;
613 else
615 /* If it is >= that width, result is zero.
616 Handling this with trunc1 would give the wrong result:
617 (int) ((long long) a << 32) is well defined (as 0)
618 but (int) a << 32 is undefined and would get a
619 warning. */
621 tree t = build_int_cst (type, 0);
623 /* If the original expression had side-effects, we must
624 preserve it. */
625 if (TREE_SIDE_EFFECTS (expr))
626 return build2 (COMPOUND_EXPR, type, expr, t);
627 else
628 return t;
631 break;
633 case TRUNC_DIV_EXPR:
635 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
636 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
638 /* Don't distribute unless the output precision is at least as big
639 as the actual inputs and it has the same signedness. */
640 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
641 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
642 /* If signedness of arg0 and arg1 don't match,
643 we can't necessarily find a type to compare them in. */
644 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
645 == TYPE_UNSIGNED (TREE_TYPE (arg1)))
646 /* Do not change the sign of the division. */
647 && (TYPE_UNSIGNED (TREE_TYPE (expr))
648 == TYPE_UNSIGNED (TREE_TYPE (arg0)))
649 /* Either require unsigned division or a division by
650 a constant that is not -1. */
651 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
652 || (TREE_CODE (arg1) == INTEGER_CST
653 && !integer_all_onesp (arg1))))
654 goto trunc1;
655 break;
658 case MAX_EXPR:
659 case MIN_EXPR:
660 case MULT_EXPR:
662 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
663 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
665 /* Don't distribute unless the output precision is at least as big
666 as the actual inputs. Otherwise, the comparison of the
667 truncated values will be wrong. */
668 if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
669 && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
670 /* If signedness of arg0 and arg1 don't match,
671 we can't necessarily find a type to compare them in. */
672 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
673 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
674 goto trunc1;
675 break;
678 case PLUS_EXPR:
679 case MINUS_EXPR:
680 case BIT_AND_EXPR:
681 case BIT_IOR_EXPR:
682 case BIT_XOR_EXPR:
683 trunc1:
685 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
686 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
688 /* Do not try to narrow operands of pointer subtraction;
689 that will interfere with other folding. */
690 if (ex_form == MINUS_EXPR
691 && CONVERT_EXPR_P (arg0)
692 && CONVERT_EXPR_P (arg1)
693 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
694 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
695 break;
697 if (outprec >= BITS_PER_WORD
698 || TRULY_NOOP_TRUNCATION (outprec, inprec)
699 || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
700 || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
702 /* Do the arithmetic in type TYPEX,
703 then convert result to TYPE. */
704 tree typex = type;
706 /* Can't do arithmetic in enumeral types
707 so use an integer type that will hold the values. */
708 if (TREE_CODE (typex) == ENUMERAL_TYPE)
709 typex = lang_hooks.types.type_for_size
710 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
712 /* But now perhaps TYPEX is as wide as INPREC.
713 In that case, do nothing special here.
714 (Otherwise would recurse infinitely in convert. */
715 if (TYPE_PRECISION (typex) != inprec)
717 /* Don't do unsigned arithmetic where signed was wanted,
718 or vice versa.
719 Exception: if both of the original operands were
720 unsigned then we can safely do the work as unsigned.
721 Exception: shift operations take their type solely
722 from the first argument.
723 Exception: the LSHIFT_EXPR case above requires that
724 we perform this operation unsigned lest we produce
725 signed-overflow undefinedness.
726 And we may need to do it as unsigned
727 if we truncate to the original size. */
728 if (TYPE_UNSIGNED (TREE_TYPE (expr))
729 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
730 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
731 || ex_form == LSHIFT_EXPR
732 || ex_form == RSHIFT_EXPR
733 || ex_form == LROTATE_EXPR
734 || ex_form == RROTATE_EXPR))
735 || ex_form == LSHIFT_EXPR
736 /* If we have !flag_wrapv, and either ARG0 or
737 ARG1 is of a signed type, we have to do
738 PLUS_EXPR, MINUS_EXPR or MULT_EXPR in an unsigned
739 type in case the operation in outprec precision
740 could overflow. Otherwise, we would introduce
741 signed-overflow undefinedness. */
742 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
743 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
744 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
745 > outprec)
746 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
747 > outprec))
748 && (ex_form == PLUS_EXPR
749 || ex_form == MINUS_EXPR
750 || ex_form == MULT_EXPR)))
751 typex = unsigned_type_for (typex);
752 else
753 typex = signed_type_for (typex);
754 return convert (type,
755 fold_build2 (ex_form, typex,
756 convert (typex, arg0),
757 convert (typex, arg1)));
761 break;
763 case NEGATE_EXPR:
764 case BIT_NOT_EXPR:
765 /* This is not correct for ABS_EXPR,
766 since we must test the sign before truncation. */
768 tree typex = unsigned_type_for (type);
769 return convert (type,
770 fold_build1 (ex_form, typex,
771 convert (typex,
772 TREE_OPERAND (expr, 0))));
775 case NOP_EXPR:
776 /* Don't introduce a
777 "can't convert between vector values of different size" error. */
778 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == VECTOR_TYPE
779 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0))))
780 != GET_MODE_SIZE (TYPE_MODE (type))))
781 break;
782 /* If truncating after truncating, might as well do all at once.
783 If truncating after extending, we may get rid of wasted work. */
784 return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
786 case COND_EXPR:
787 /* It is sometimes worthwhile to push the narrowing down through
788 the conditional and never loses. A COND_EXPR may have a throw
789 as one operand, which then has void type. Just leave void
790 operands as they are. */
791 return fold_build3 (COND_EXPR, type, TREE_OPERAND (expr, 0),
792 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1)))
793 ? TREE_OPERAND (expr, 1)
794 : convert (type, TREE_OPERAND (expr, 1)),
795 VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 2)))
796 ? TREE_OPERAND (expr, 2)
797 : convert (type, TREE_OPERAND (expr, 2)));
799 default:
800 break;
803 /* When parsing long initializers, we might end up with a lot of casts.
804 Shortcut this. */
805 if (TREE_CODE (expr) == INTEGER_CST)
806 return fold_convert (type, expr);
807 return build1 (CONVERT_EXPR, type, expr);
809 case REAL_TYPE:
810 return build1 (FIX_TRUNC_EXPR, type, expr);
812 case FIXED_POINT_TYPE:
813 return build1 (FIXED_CONVERT_EXPR, type, expr);
815 case COMPLEX_TYPE:
816 return convert (type,
817 fold_build1 (REALPART_EXPR,
818 TREE_TYPE (TREE_TYPE (expr)), expr));
820 case VECTOR_TYPE:
821 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
823 error ("can%'t convert between vector values of different size");
824 return error_mark_node;
826 return build1 (VIEW_CONVERT_EXPR, type, expr);
828 default:
829 error ("aggregate value used where an integer was expected");
830 return convert (type, integer_zero_node);
834 /* Convert EXPR to the complex type TYPE in the usual ways. */
836 tree
837 convert_to_complex (tree type, tree expr)
839 tree subtype = TREE_TYPE (type);
841 switch (TREE_CODE (TREE_TYPE (expr)))
843 case REAL_TYPE:
844 case FIXED_POINT_TYPE:
845 case INTEGER_TYPE:
846 case ENUMERAL_TYPE:
847 case BOOLEAN_TYPE:
848 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
849 convert (subtype, integer_zero_node));
851 case COMPLEX_TYPE:
853 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
855 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
856 return expr;
857 else if (TREE_CODE (expr) == COMPLEX_EXPR)
858 return fold_build2 (COMPLEX_EXPR, type,
859 convert (subtype, TREE_OPERAND (expr, 0)),
860 convert (subtype, TREE_OPERAND (expr, 1)));
861 else
863 expr = save_expr (expr);
864 return
865 fold_build2 (COMPLEX_EXPR, type,
866 convert (subtype,
867 fold_build1 (REALPART_EXPR,
868 TREE_TYPE (TREE_TYPE (expr)),
869 expr)),
870 convert (subtype,
871 fold_build1 (IMAGPART_EXPR,
872 TREE_TYPE (TREE_TYPE (expr)),
873 expr)));
877 case POINTER_TYPE:
878 case REFERENCE_TYPE:
879 error ("pointer value used where a complex was expected");
880 return convert_to_complex (type, integer_zero_node);
882 default:
883 error ("aggregate value used where a complex was expected");
884 return convert_to_complex (type, integer_zero_node);
888 /* Convert EXPR to the vector type TYPE in the usual ways. */
890 tree
891 convert_to_vector (tree type, tree expr)
893 switch (TREE_CODE (TREE_TYPE (expr)))
895 case INTEGER_TYPE:
896 case VECTOR_TYPE:
897 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
899 error ("can%'t convert between vector values of different size");
900 return error_mark_node;
902 return build1 (VIEW_CONVERT_EXPR, type, expr);
904 default:
905 error ("can%'t convert value to a vector");
906 return error_mark_node;
910 /* Convert EXPR to some fixed-point type TYPE.
912 EXPR must be fixed-point, float, integer, or enumeral;
913 in other cases error is called. */
915 tree
916 convert_to_fixed (tree type, tree expr)
918 if (integer_zerop (expr))
920 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
921 return fixed_zero_node;
923 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
925 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
926 return fixed_one_node;
929 switch (TREE_CODE (TREE_TYPE (expr)))
931 case FIXED_POINT_TYPE:
932 case INTEGER_TYPE:
933 case ENUMERAL_TYPE:
934 case BOOLEAN_TYPE:
935 case REAL_TYPE:
936 return build1 (FIXED_CONVERT_EXPR, type, expr);
938 case COMPLEX_TYPE:
939 return convert (type,
940 fold_build1 (REALPART_EXPR,
941 TREE_TYPE (TREE_TYPE (expr)), expr));
943 default:
944 error ("aggregate value used where a fixed-point was expected");
945 return error_mark_node;