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