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