]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/convert.c
gcc/
[thirdparty/gcc.git] / gcc / convert.c
CommitLineData
5e6908ea 1/* Utility routines for data type conversion for GCC.
d1e082c2 2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
76e616db 3
1322177d 4This file is part of GCC.
76e616db 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
76e616db 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
76e616db
BK
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
76e616db
BK
19
20
21/* These routines are somewhat language-independent utility function
0f41302f 22 intended to be called by the language-specific convert () functions. */
76e616db
BK
23
24#include "config.h"
c5c76735 25#include "system.h"
4977bab6
ZW
26#include "coretypes.h"
27#include "tm.h"
76e616db
BK
28#include "tree.h"
29#include "flags.h"
30#include "convert.h"
718f9c0f 31#include "diagnostic-core.h"
b0c48229 32#include "langhooks.h"
76e616db 33
0a931ce5 34/* Convert EXPR to some pointer or reference type TYPE.
98c76e3c 35 EXPR must be pointer, reference, integer, enumeral, or literal zero;
0f41302f 36 in other cases error is called. */
76e616db
BK
37
38tree
159b3be1 39convert_to_pointer (tree type, tree expr)
76e616db 40{
db3927fb 41 location_t loc = EXPR_LOCATION (expr);
0a931ce5
RS
42 if (TREE_TYPE (expr) == type)
43 return expr;
44
f5963e61 45 switch (TREE_CODE (TREE_TYPE (expr)))
76e616db 46 {
f5963e61
JL
47 case POINTER_TYPE:
48 case REFERENCE_TYPE:
09e881c9
BE
49 {
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)));
54
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);
59 }
f5963e61
JL
60
61 case INTEGER_TYPE:
62 case ENUMERAL_TYPE:
63 case BOOLEAN_TYPE:
cf157324
OH
64 {
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));
71
72 if (eprec != pprec)
73 expr = fold_build1_loc (loc, NOP_EXPR,
74 lang_hooks.types.type_for_size (pprec, 0),
75 expr);
76 }
76e616db 77
cf157324 78 return fold_build1_loc (loc, CONVERT_EXPR, type, expr);
76e616db 79
f5963e61
JL
80 default:
81 error ("cannot convert to a pointer type");
82 return convert_to_pointer (type, integer_zero_node);
83 }
76e616db
BK
84}
85
4977bab6 86
76e616db
BK
87/* Convert EXPR to some floating-point type TYPE.
88
0f996086 89 EXPR must be float, fixed-point, integer, or enumeral;
0f41302f 90 in other cases error is called. */
76e616db
BK
91
92tree
159b3be1 93convert_to_real (tree type, tree expr)
76e616db 94{
27a6aa72 95 enum built_in_function fcode = builtin_mathfn_code (expr);
4977bab6
ZW
96 tree itype = TREE_TYPE (expr);
97
4b207444
JH
98 /* Disable until we figure out how to decide whether the functions are
99 present in runtime. */
4977bab6 100 /* Convert (float)sqrt((double)x) where x is float into sqrtf(x) */
78bd5210 101 if (optimize
4977bab6
ZW
102 && (TYPE_MODE (type) == TYPE_MODE (double_type_node)
103 || TYPE_MODE (type) == TYPE_MODE (float_type_node)))
104 {
b3810360
KG
105 switch (fcode)
106 {
107#define CASE_MATHFN(FN) case BUILT_IN_##FN: case BUILT_IN_##FN##L:
1fb7e3af 108 CASE_MATHFN (COSH)
b3810360 109 CASE_MATHFN (EXP)
1fb7e3af
KG
110 CASE_MATHFN (EXP10)
111 CASE_MATHFN (EXP2)
f060a261 112 CASE_MATHFN (EXPM1)
1fb7e3af
KG
113 CASE_MATHFN (GAMMA)
114 CASE_MATHFN (J0)
115 CASE_MATHFN (J1)
116 CASE_MATHFN (LGAMMA)
1fb7e3af 117 CASE_MATHFN (POW10)
1fb7e3af 118 CASE_MATHFN (SINH)
1fb7e3af
KG
119 CASE_MATHFN (TGAMMA)
120 CASE_MATHFN (Y0)
121 CASE_MATHFN (Y1)
f060a261
RG
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)
b3810360 147#undef CASE_MATHFN
4977bab6 148 {
5039610b 149 tree arg0 = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
b3810360
KG
150 tree newtype = type;
151
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);
156
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)))
163 {
b3810360
KG
164 tree fn = mathfn_built_in (newtype, fcode);
165
166 if (fn)
167 {
5039610b
SL
168 tree arg = fold (convert_to_real (newtype, arg0));
169 expr = build_call_expr (fn, 1, arg);
b3810360
KG
170 if (newtype == type)
171 return expr;
172 }
173 }
4977bab6 174 }
b3810360
KG
175 default:
176 break;
4977bab6
ZW
177 }
178 }
5e8b5b08
EB
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)))))
195 {
196 tree fn = mathfn_built_in (type, fcode);
197
198 if (fn)
199 {
5039610b 200 tree arg = strip_float_extensions (CALL_EXPR_ARG (expr, 0));
5e8b5b08
EB
201
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)))
5039610b 207 return build_call_expr (fn, 1, fold (convert_to_real (type, arg)));
5e8b5b08
EB
208 }
209 }
4977bab6
ZW
210
211 /* Propagate the cast into the operation. */
212 if (itype != type && FLOAT_TYPE_P (type))
213 switch (TREE_CODE (expr))
214 {
4f76e46b
RG
215 /* Convert (float)-x into -(float)x. This is safe for
216 round-to-nearest rounding mode. */
4977bab6
ZW
217 case ABS_EXPR:
218 case NEGATE_EXPR:
4f76e46b
RG
219 if (!flag_rounding_math
220 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (expr)))
b1a6f8db
JH
221 return build1 (TREE_CODE (expr), type,
222 fold (convert_to_real (type,
223 TREE_OPERAND (expr, 0))));
224 break;
beb235f8 225 /* Convert (outertype)((innertype0)a+(innertype1)b)
4977bab6
ZW
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:
232 {
233 tree arg0 = strip_float_extensions (TREE_OPERAND (expr, 0));
234 tree arg1 = strip_float_extensions (TREE_OPERAND (expr, 1));
235
236 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
20ded7a6
JM
237 && FLOAT_TYPE_P (TREE_TYPE (arg1))
238 && DECIMAL_FLOAT_TYPE_P (itype) == DECIMAL_FLOAT_TYPE_P (type))
4977bab6
ZW
239 {
240 tree newtype = type;
15ed7b52
JG
241
242 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
20ded7a6
JM
243 || TYPE_MODE (TREE_TYPE (arg1)) == SDmode
244 || TYPE_MODE (type) == SDmode)
15ed7b52
JG
245 newtype = dfloat32_type_node;
246 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
20ded7a6
JM
247 || TYPE_MODE (TREE_TYPE (arg1)) == DDmode
248 || TYPE_MODE (type) == DDmode)
15ed7b52
JG
249 newtype = dfloat64_type_node;
250 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
20ded7a6
JM
251 || TYPE_MODE (TREE_TYPE (arg1)) == TDmode
252 || TYPE_MODE (type) == TDmode)
15ed7b52
JG
253 newtype = dfloat128_type_node;
254 if (newtype == dfloat32_type_node
255 || newtype == dfloat64_type_node
256 || newtype == dfloat128_type_node)
257 {
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;
264 }
265
4977bab6
ZW
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);
20ded7a6
JM
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),
8ce94e44
JM
295 TYPE_MODE (type))
296 && !excess_precision_type (newtype))))
4977bab6 297 {
3244e67d
RS
298 expr = build2 (TREE_CODE (expr), newtype,
299 fold (convert_to_real (newtype, arg0)),
300 fold (convert_to_real (newtype, arg1)));
4977bab6
ZW
301 if (newtype == type)
302 return expr;
303 }
304 }
305 }
306 break;
307 default:
308 break;
309 }
310
f5963e61
JL
311 switch (TREE_CODE (TREE_TYPE (expr)))
312 {
313 case REAL_TYPE:
5fc89bfd
JJ
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);
f5963e61
JL
320
321 case INTEGER_TYPE:
322 case ENUMERAL_TYPE:
323 case BOOLEAN_TYPE:
f5963e61
JL
324 return build1 (FLOAT_EXPR, type, expr);
325
0f996086
CF
326 case FIXED_POINT_TYPE:
327 return build1 (FIXED_CONVERT_EXPR, type, expr);
328
f5963e61
JL
329 case COMPLEX_TYPE:
330 return convert (type,
987b67bc
KH
331 fold_build1 (REALPART_EXPR,
332 TREE_TYPE (TREE_TYPE (expr)), expr));
f5963e61
JL
333
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);
338
339 default:
340 error ("aggregate value used where a float was expected");
341 return convert_to_real (type, integer_zero_node);
342 }
76e616db
BK
343}
344
345/* Convert EXPR to some integer (or enum) type TYPE.
346
0f996086
CF
347 EXPR must be pointer, integer, discrete (enum, char, or bool), float,
348 fixed-point or vector; in other cases error is called.
76e616db
BK
349
350 The result of this is always supposed to be a newly created tree node
351 not in use in any existing structure. */
352
353tree
159b3be1 354convert_to_integer (tree type, tree expr)
76e616db 355{
f5963e61
JL
356 enum tree_code ex_form = TREE_CODE (expr);
357 tree intype = TREE_TYPE (expr);
770ae6cc
RK
358 unsigned int inprec = TYPE_PRECISION (intype);
359 unsigned int outprec = TYPE_PRECISION (type);
76e616db 360
9c4cb3a3
MM
361 /* An INTEGER_TYPE cannot be incomplete, but an ENUMERAL_TYPE can
362 be. Consider `enum E = { a, b = (enum E) 3 };'. */
d0f062fb 363 if (!COMPLETE_TYPE_P (type))
9c4cb3a3
MM
364 {
365 error ("conversion to incomplete type");
366 return error_mark_node;
367 }
368
332d782c
KG
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
d2be4368
KG
376 || (long_integer_type_node
377 && outprec >= TYPE_PRECISION (long_integer_type_node))))
332d782c
KG
378 {
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;
b8698a0f 383
332d782c
KG
384 switch (fcode)
385 {
ea6a6627 386 CASE_FLT_FN (BUILT_IN_CEIL):
1c432a0c
UB
387 /* Only convert in ISO C99 mode. */
388 if (!TARGET_C99_FUNCTIONS)
389 break;
6c32ee74
UB
390 if (outprec < TYPE_PRECISION (integer_type_node)
391 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 392 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
393 fn = mathfn_built_in (s_intype, BUILT_IN_ICEIL);
394 else if (outprec == TYPE_PRECISION (long_integer_type_node)
395 && !TYPE_UNSIGNED (type))
f94b1661 396 fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
738764ef
RS
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);
f94b1661
UB
400 break;
401
ea6a6627 402 CASE_FLT_FN (BUILT_IN_FLOOR):
1c432a0c
UB
403 /* Only convert in ISO C99 mode. */
404 if (!TARGET_C99_FUNCTIONS)
405 break;
6c32ee74
UB
406 if (outprec < TYPE_PRECISION (integer_type_node)
407 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 408 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
409 fn = mathfn_built_in (s_intype, BUILT_IN_IFLOOR);
410 else if (outprec == TYPE_PRECISION (long_integer_type_node)
411 && !TYPE_UNSIGNED (type))
d8b42d06 412 fn = mathfn_built_in (s_intype, BUILT_IN_LFLOOR);
738764ef
RS
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);
d8b42d06
UB
416 break;
417
ea6a6627 418 CASE_FLT_FN (BUILT_IN_ROUND):
44782c0c
UB
419 /* Only convert in ISO C99 mode. */
420 if (!TARGET_C99_FUNCTIONS)
421 break;
6c32ee74
UB
422 if (outprec < TYPE_PRECISION (integer_type_node)
423 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 424 && !TYPE_UNSIGNED (type)))
6c32ee74
UB
425 fn = mathfn_built_in (s_intype, BUILT_IN_IROUND);
426 else if (outprec == TYPE_PRECISION (long_integer_type_node)
427 && !TYPE_UNSIGNED (type))
332d782c 428 fn = mathfn_built_in (s_intype, BUILT_IN_LROUND);
738764ef
RS
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);
332d782c
KG
432 break;
433
65bda21f
KG
434 CASE_FLT_FN (BUILT_IN_NEARBYINT):
435 /* Only convert nearbyint* if we can ignore math exceptions. */
332d782c
KG
436 if (flag_trapping_math)
437 break;
438 /* ... Fall through ... */
65bda21f 439 CASE_FLT_FN (BUILT_IN_RINT):
44782c0c
UB
440 /* Only convert in ISO C99 mode. */
441 if (!TARGET_C99_FUNCTIONS)
442 break;
6c32ee74
UB
443 if (outprec < TYPE_PRECISION (integer_type_node)
444 || (outprec == TYPE_PRECISION (integer_type_node)
738764ef 445 && !TYPE_UNSIGNED (type)))
6c32ee74 446 fn = mathfn_built_in (s_intype, BUILT_IN_IRINT);
44782c0c 447 else if (outprec == TYPE_PRECISION (long_integer_type_node)
6c32ee74 448 && !TYPE_UNSIGNED (type))
738764ef
RS
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);
332d782c 453 break;
2ec76fdb 454
ea6a6627 455 CASE_FLT_FN (BUILT_IN_TRUNC):
5039610b 456 return convert_to_integer (type, CALL_EXPR_ARG (s_expr, 0));
2ec76fdb 457
332d782c
KG
458 default:
459 break;
460 }
b8698a0f 461
332d782c
KG
462 if (fn)
463 {
5039610b 464 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
332d782c
KG
465 return convert_to_integer (type, newexpr);
466 }
467 }
468
2c2f70e1
UB
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))))
477 {
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;
b8698a0f 482
2c2f70e1
UB
483 switch (fcode)
484 {
485 CASE_FLT_FN (BUILT_IN_LOGB):
486 fn = mathfn_built_in (s_intype, BUILT_IN_ILOGB);
487 break;
488
489 default:
490 break;
491 }
492
493 if (fn)
494 {
495 tree newexpr = build_call_expr (fn, 1, CALL_EXPR_ARG (s_expr, 0));
496 return convert_to_integer (type, newexpr);
497 }
498 }
499
f5963e61 500 switch (TREE_CODE (intype))
76e616db 501 {
f5963e61
JL
502 case POINTER_TYPE:
503 case REFERENCE_TYPE:
76e616db 504 if (integer_zerop (expr))
97471d8f
RS
505 return build_int_cst (type, 0);
506
c767899e
OH
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. */
97471d8f 511 expr = fold_build1 (CONVERT_EXPR,
c767899e
OH
512 lang_hooks.types.type_for_size
513 (TYPE_PRECISION (intype), 0),
97471d8f 514 expr);
e7a6c127 515 return fold_convert (type, expr);
76e616db 516
f5963e61
JL
517 case INTEGER_TYPE:
518 case ENUMERAL_TYPE:
519 case BOOLEAN_TYPE:
6175f578 520 case OFFSET_TYPE:
f5963e61 521 /* If this is a logical operation, which just returns 0 or 1, we can
a338ab5a 522 change the type of the expression. */
76e616db 523
6615c446 524 if (TREE_CODE_CLASS (ex_form) == tcc_comparison)
76e616db 525 {
5dfa45d0 526 expr = copy_node (expr);
76e616db
BK
527 TREE_TYPE (expr) = type;
528 return expr;
529 }
f5963e61 530
f5963e61
JL
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. */
534
76e616db 535 else if (outprec >= inprec)
4b0d3cbe
MM
536 {
537 enum tree_code code;
538
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. */
8df83eae 548 if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (TREE_TYPE (expr))
4b0d3cbe 549 && (TYPE_PRECISION (TREE_TYPE (expr))
69660a70 550 != GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (expr)))))
4b0d3cbe
MM
551 code = CONVERT_EXPR;
552 else
553 code = NOP_EXPR;
554
007a787d 555 return fold_build1 (code, type, expr);
4b0d3cbe 556 }
76e616db 557
1c013b45
RK
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
69660a70 563 || outprec != GET_MODE_PRECISION (TYPE_MODE (type)))
1c013b45 564 return build1 (NOP_EXPR, type,
ae2bcd98 565 convert (lang_hooks.types.type_for_mode
8df83eae 566 (TYPE_MODE (type), TYPE_UNSIGNED (type)),
1c013b45
RK
567 expr));
568
ab29fdfc
RK
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.
581
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. */
76e616db
BK
588
589 switch (ex_form)
590 {
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
da6d971d 595 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
76e616db
BK
596 goto trunc1;
597 break;
598
599 case LSHIFT_EXPR:
600 /* We can pass truncation down through left shifting
43e4a9d8
EB
601 when the shift count is a nonnegative constant and
602 the target type is unsigned. */
76e616db 603 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
ab29fdfc 604 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) >= 0
8df83eae 605 && TYPE_UNSIGNED (type)
76e616db
BK
606 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
607 {
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
d9a9c5a7
RK
614 {
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. */
620
e7a6c127 621 tree t = build_int_cst (type, 0);
d9a9c5a7
RK
622
623 /* If the original expression had side-effects, we must
624 preserve it. */
625 if (TREE_SIDE_EFFECTS (expr))
3244e67d 626 return build2 (COMPOUND_EXPR, type, expr, t);
d9a9c5a7
RK
627 else
628 return t;
629 }
76e616db
BK
630 }
631 break;
632
d977cb9c
RG
633 case TRUNC_DIV_EXPR:
634 {
635 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
636 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
637
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;
656 }
657
76e616db
BK
658 case MAX_EXPR:
659 case MIN_EXPR:
660 case MULT_EXPR:
661 {
662 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
663 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
664
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. */
8df83eae
RK
672 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
673 == TYPE_UNSIGNED (TREE_TYPE (arg1))))
76e616db
BK
674 goto trunc1;
675 break;
676 }
677
678 case PLUS_EXPR:
679 case MINUS_EXPR:
680 case BIT_AND_EXPR:
681 case BIT_IOR_EXPR:
682 case BIT_XOR_EXPR:
76e616db
BK
683 trunc1:
684 {
685 tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
686 tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
687
a2d5091a
JM
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;
696
76e616db
BK
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)))
701 {
702 /* Do the arithmetic in type TYPEX,
703 then convert result to TYPE. */
b3694847 704 tree typex = type;
76e616db
BK
705
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)
ae2bcd98 709 typex = lang_hooks.types.type_for_size
8df83eae 710 (TYPE_PRECISION (typex), TYPE_UNSIGNED (typex));
76e616db
BK
711
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)
716 {
717 /* Don't do unsigned arithmetic where signed was wanted,
718 or vice versa.
3cc247a8 719 Exception: if both of the original operands were
159b3be1 720 unsigned then we can safely do the work as unsigned.
43e4a9d8
EB
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.
76e616db
BK
726 And we may need to do it as unsigned
727 if we truncate to the original size. */
8df83eae
RK
728 if (TYPE_UNSIGNED (TREE_TYPE (expr))
729 || (TYPE_UNSIGNED (TREE_TYPE (arg0))
730 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
43e4a9d8
EB
731 || ex_form == LSHIFT_EXPR
732 || ex_form == RSHIFT_EXPR
733 || ex_form == LROTATE_EXPR
734 || ex_form == RROTATE_EXPR))
4a2ab192
KH
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
dfb88126
RG
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
4a2ab192 741 signed-overflow undefinedness. */
eeef0e45
ILT
742 || ((!TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
743 || !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
dfb88126
RG
744 && ((TYPE_PRECISION (TREE_TYPE (arg0)) * 2u
745 > outprec)
746 || (TYPE_PRECISION (TREE_TYPE (arg1)) * 2u
747 > outprec))
4a2ab192 748 && (ex_form == PLUS_EXPR
dfb88126
RG
749 || ex_form == MINUS_EXPR
750 || ex_form == MULT_EXPR)))
ca5ba2a3 751 typex = unsigned_type_for (typex);
ceef8ce4 752 else
12753674 753 typex = signed_type_for (typex);
76e616db 754 return convert (type,
987b67bc
KH
755 fold_build2 (ex_form, typex,
756 convert (typex, arg0),
757 convert (typex, arg1)));
76e616db
BK
758 }
759 }
760 }
761 break;
762
763 case NEGATE_EXPR:
764 case BIT_NOT_EXPR:
d283912a
RS
765 /* This is not correct for ABS_EXPR,
766 since we must test the sign before truncation. */
76e616db 767 {
6befd6b0 768 tree typex = unsigned_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
c53153e7
JH
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);
0b87eff5 807 return build1 (CONVERT_EXPR, type, expr);
76e616db 808
f5963e61
JL
809 case REAL_TYPE:
810 return build1 (FIX_TRUNC_EXPR, type, expr);
76e616db 811
0f996086
CF
812 case FIXED_POINT_TYPE:
813 return build1 (FIXED_CONVERT_EXPR, type, expr);
814
f5963e61
JL
815 case COMPLEX_TYPE:
816 return convert (type,
987b67bc
KH
817 fold_build1 (REALPART_EXPR,
818 TREE_TYPE (TREE_TYPE (expr)), expr));
0b127821 819
0b4565c9 820 case VECTOR_TYPE:
3a021db2 821 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
0b4565c9 822 {
d8a07487 823 error ("can%'t convert between vector values of different size");
0b4565c9
BS
824 return error_mark_node;
825 }
4d3c798d 826 return build1 (VIEW_CONVERT_EXPR, type, expr);
0b4565c9 827
f5963e61
JL
828 default:
829 error ("aggregate value used where an integer was expected");
830 return convert (type, integer_zero_node);
831 }
76e616db 832}
0b127821
RS
833
834/* Convert EXPR to the complex type TYPE in the usual ways. */
835
836tree
159b3be1 837convert_to_complex (tree type, tree expr)
0b127821 838{
0b127821 839 tree subtype = TREE_TYPE (type);
159b3be1 840
f5963e61 841 switch (TREE_CODE (TREE_TYPE (expr)))
0b127821 842 {
f5963e61 843 case REAL_TYPE:
0f996086 844 case FIXED_POINT_TYPE:
f5963e61
JL
845 case INTEGER_TYPE:
846 case ENUMERAL_TYPE:
847 case BOOLEAN_TYPE:
3244e67d
RS
848 return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
849 convert (subtype, integer_zero_node));
0b127821 850
f5963e61
JL
851 case COMPLEX_TYPE:
852 {
853 tree elt_type = TREE_TYPE (TREE_TYPE (expr));
854
855 if (TYPE_MAIN_VARIANT (elt_type) == TYPE_MAIN_VARIANT (subtype))
856 return expr;
857 else if (TREE_CODE (expr) == COMPLEX_EXPR)
987b67bc
KH
858 return fold_build2 (COMPLEX_EXPR, type,
859 convert (subtype, TREE_OPERAND (expr, 0)),
860 convert (subtype, TREE_OPERAND (expr, 1)));
f5963e61
JL
861 else
862 {
863 expr = save_expr (expr);
864 return
987b67bc
KH
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)));
f5963e61
JL
874 }
875 }
0b127821 876
f5963e61
JL
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);
881
882 default:
883 error ("aggregate value used where a complex was expected");
884 return convert_to_complex (type, integer_zero_node);
885 }
0b127821 886}
0b4565c9
BS
887
888/* Convert EXPR to the vector type TYPE in the usual ways. */
889
890tree
159b3be1 891convert_to_vector (tree type, tree expr)
0b4565c9 892{
0b4565c9
BS
893 switch (TREE_CODE (TREE_TYPE (expr)))
894 {
895 case INTEGER_TYPE:
896 case VECTOR_TYPE:
3a021db2 897 if (!tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (expr))))
0b4565c9 898 {
d8a07487 899 error ("can%'t convert between vector values of different size");
0b4565c9
BS
900 return error_mark_node;
901 }
4d3c798d 902 return build1 (VIEW_CONVERT_EXPR, type, expr);
0b4565c9
BS
903
904 default:
d8a07487 905 error ("can%'t convert value to a vector");
273d67e7 906 return error_mark_node;
0b4565c9
BS
907 }
908}
0f996086
CF
909
910/* Convert EXPR to some fixed-point type TYPE.
911
912 EXPR must be fixed-point, float, integer, or enumeral;
913 in other cases error is called. */
914
915tree
916convert_to_fixed (tree type, tree expr)
917{
918 if (integer_zerop (expr))
919 {
920 tree fixed_zero_node = build_fixed (type, FCONST0 (TYPE_MODE (type)));
921 return fixed_zero_node;
922 }
923 else if (integer_onep (expr) && ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)))
924 {
925 tree fixed_one_node = build_fixed (type, FCONST1 (TYPE_MODE (type)));
926 return fixed_one_node;
927 }
928
929 switch (TREE_CODE (TREE_TYPE (expr)))
930 {
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);
937
938 case COMPLEX_TYPE:
939 return convert (type,
940 fold_build1 (REALPART_EXPR,
941 TREE_TYPE (TREE_TYPE (expr)), expr));
942
943 default:
944 error ("aggregate value used where a fixed-point was expected");
945 return error_mark_node;
946 }
947}