comparison gcc/convert.c @ 111:04ced10e8804

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