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