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