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