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