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