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