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