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