1 /* Convert tree expression to rtl instructions, for GNU compiler. 2 Copyright (C) 1988-2024 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it under 7 the terms of the GNU General Public License as published by the Free 8 Software Foundation; either version 3, or (at your option) any later 9 version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 #include "config.h" 21 #include "system.h" 22 #include "coretypes.h" 23 #include "backend.h" 24 #include "target.h" 25 #include "rtl.h" 26 #include "tree.h" 27 #include "gimple.h" 28 #include "predict.h" 29 #include "memmodel.h" 30 #include "tm_p.h" 31 #include "ssa.h" 32 #include "optabs.h" 33 #include "expmed.h" 34 #include "regs.h" 35 #include "emit-rtl.h" 36 #include "recog.h" 37 #include "cgraph.h" 38 #include "diagnostic.h" 39 #include "alias.h" 40 #include "fold-const.h" 41 #include "stor-layout.h" 42 #include "attribs.h" 43 #include "varasm.h" 44 #include "except.h" 45 #include "insn-attr.h" 46 #include "dojump.h" 47 #include "explow.h" 48 #include "calls.h" 49 #include "stmt.h" 50 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */ 51 #include "expr.h" 52 #include "optabs-tree.h" 53 #include "libfuncs.h" 54 #include "reload.h" 55 #include "langhooks.h" 56 #include "common/common-target.h" 57 #include "tree-dfa.h" 58 #include "tree-ssa-live.h" 59 #include "tree-outof-ssa.h" 60 #include "tree-ssa-address.h" 61 #include "builtins.h" 62 #include "ccmp.h" 63 #include "gimple-iterator.h" 64 #include "gimple-fold.h" 65 #include "rtx-vector-builder.h" 66 #include "tree-pretty-print.h" 67 #include "flags.h" 68 69 70 /* If this is nonzero, we do not bother generating VOLATILE 71 around volatile memory references, and we are willing to 72 output indirect addresses. If cse is to follow, we reject 73 indirect addresses so a useful potential cse is generated; 74 if it is used only once, instruction combination will produce 75 the same indirect address eventually. */ 76 int cse_not_expected; 77 78 static bool block_move_libcall_safe_for_call_parm (void); 79 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned, 80 HOST_WIDE_INT, unsigned HOST_WIDE_INT, 81 unsigned HOST_WIDE_INT, 82 unsigned HOST_WIDE_INT, bool); 83 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned, int); 84 static void emit_block_move_via_sized_loop (rtx, rtx, rtx, unsigned, unsigned); 85 static void emit_block_move_via_oriented_loop (rtx, rtx, rtx, unsigned, unsigned); 86 static rtx emit_block_cmp_via_loop (rtx, rtx, rtx, tree, rtx, bool, 87 unsigned, unsigned); 88 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int); 89 static rtx_insn *compress_float_constant (rtx, rtx); 90 static rtx get_subtarget (rtx); 91 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64, 92 machine_mode, tree, alias_set_type, bool, bool); 93 94 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree); 95 96 static bool is_aligning_offset (const_tree, const_tree); 97 static rtx reduce_to_bit_field_precision (rtx, rtx, tree); 98 static rtx do_store_flag (sepops, rtx, machine_mode); 99 #ifdef PUSH_ROUNDING 100 static void emit_single_push_insn (machine_mode, rtx, tree); 101 #endif 102 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, 103 profile_probability); 104 static rtx const_vector_from_tree (tree); 105 static tree tree_expr_size (const_tree); 106 static void convert_mode_scalar (rtx, rtx, int); 107 108 109 /* This is run to set up which modes can be used 111 directly in memory and to initialize the block move optab. It is run 112 at the beginning of compilation and when the target is reinitialized. */ 113 114 void 115 init_expr_target (void) 116 { 117 rtx pat; 118 int num_clobbers; 119 rtx mem, mem1; 120 rtx reg; 121 122 /* Try indexing by frame ptr and try by stack ptr. 123 It is known that on the Convex the stack ptr isn't a valid index. 124 With luck, one or the other is valid on any machine. */ 125 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx); 126 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx); 127 128 /* A scratch register we can modify in-place below to avoid 129 useless RTL allocations. */ 130 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1); 131 132 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN)); 133 pat = gen_rtx_SET (NULL_RTX, NULL_RTX); 134 PATTERN (insn) = pat; 135 136 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES; 137 mode = (machine_mode) ((int) mode + 1)) 138 { 139 int regno; 140 141 direct_load[(int) mode] = direct_store[(int) mode] = 0; 142 PUT_MODE (mem, mode); 143 PUT_MODE (mem1, mode); 144 145 /* See if there is some register that can be used in this mode and 146 directly loaded or stored from memory. */ 147 148 if (mode != VOIDmode && mode != BLKmode) 149 for (regno = 0; regno < FIRST_PSEUDO_REGISTER 150 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0); 151 regno++) 152 { 153 if (!targetm.hard_regno_mode_ok (regno, mode)) 154 continue; 155 156 set_mode_and_regno (reg, mode, regno); 157 158 SET_SRC (pat) = mem; 159 SET_DEST (pat) = reg; 160 if (recog (pat, insn, &num_clobbers) >= 0) 161 direct_load[(int) mode] = 1; 162 163 SET_SRC (pat) = mem1; 164 SET_DEST (pat) = reg; 165 if (recog (pat, insn, &num_clobbers) >= 0) 166 direct_load[(int) mode] = 1; 167 168 SET_SRC (pat) = reg; 169 SET_DEST (pat) = mem; 170 if (recog (pat, insn, &num_clobbers) >= 0) 171 direct_store[(int) mode] = 1; 172 173 SET_SRC (pat) = reg; 174 SET_DEST (pat) = mem1; 175 if (recog (pat, insn, &num_clobbers) >= 0) 176 direct_store[(int) mode] = 1; 177 } 178 } 179 180 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1)); 181 182 opt_scalar_float_mode mode_iter; 183 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT) 184 { 185 scalar_float_mode mode = mode_iter.require (); 186 scalar_float_mode srcmode; 187 FOR_EACH_MODE_UNTIL (srcmode, mode) 188 { 189 enum insn_code ic; 190 191 ic = can_extend_p (mode, srcmode, 0); 192 if (ic == CODE_FOR_nothing) 193 continue; 194 195 PUT_MODE (mem, srcmode); 196 197 if (insn_operand_matches (ic, 1, mem)) 198 float_extend_from_mem[mode][srcmode] = true; 199 } 200 } 201 } 202 203 /* This is run at the start of compiling a function. */ 204 205 void 206 init_expr (void) 207 { 208 memset (&crtl->expr, 0, sizeof (crtl->expr)); 209 } 210 211 /* Copy data from FROM to TO, where the machine modes are not the same. 213 Both modes may be integer, or both may be floating, or both may be 214 fixed-point. 215 UNSIGNEDP should be nonzero if FROM is an unsigned type. 216 This causes zero-extension instead of sign-extension. */ 217 218 void 219 convert_move (rtx to, rtx from, int unsignedp) 220 { 221 machine_mode to_mode = GET_MODE (to); 222 machine_mode from_mode = GET_MODE (from); 223 224 gcc_assert (to_mode != BLKmode); 225 gcc_assert (from_mode != BLKmode); 226 227 /* If the source and destination are already the same, then there's 228 nothing to do. */ 229 if (to == from) 230 return; 231 232 /* If FROM is a SUBREG that indicates that we have already done at least 233 the required extension, strip it. We don't handle such SUBREGs as 234 TO here. */ 235 236 scalar_int_mode to_int_mode; 237 if (GET_CODE (from) == SUBREG 238 && SUBREG_PROMOTED_VAR_P (from) 239 && is_a <scalar_int_mode> (to_mode, &to_int_mode) 240 && (GET_MODE_PRECISION (subreg_promoted_mode (from)) 241 >= GET_MODE_PRECISION (to_int_mode)) 242 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp)) 243 { 244 scalar_int_mode int_orig_mode; 245 scalar_int_mode int_inner_mode; 246 machine_mode orig_mode = GET_MODE (from); 247 248 from = gen_lowpart (to_int_mode, SUBREG_REG (from)); 249 from_mode = to_int_mode; 250 251 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than 252 the original mode, but narrower than the inner mode. */ 253 if (GET_CODE (from) == SUBREG 254 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode) 255 && GET_MODE_PRECISION (to_int_mode) 256 > GET_MODE_PRECISION (int_orig_mode) 257 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)), 258 &int_inner_mode) 259 && GET_MODE_PRECISION (int_inner_mode) 260 > GET_MODE_PRECISION (to_int_mode)) 261 { 262 SUBREG_PROMOTED_VAR_P (from) = 1; 263 SUBREG_PROMOTED_SET (from, unsignedp); 264 } 265 } 266 267 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to)); 268 269 if (to_mode == from_mode 270 || (from_mode == VOIDmode && CONSTANT_P (from))) 271 { 272 emit_move_insn (to, from); 273 return; 274 } 275 276 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode)) 277 { 278 if (GET_MODE_UNIT_PRECISION (to_mode) 279 > GET_MODE_UNIT_PRECISION (from_mode)) 280 { 281 optab op = unsignedp ? zext_optab : sext_optab; 282 insn_code icode = convert_optab_handler (op, to_mode, from_mode); 283 if (icode != CODE_FOR_nothing) 284 { 285 emit_unop_insn (icode, to, from, 286 unsignedp ? ZERO_EXTEND : SIGN_EXTEND); 287 return; 288 } 289 } 290 291 if (GET_MODE_UNIT_PRECISION (to_mode) 292 < GET_MODE_UNIT_PRECISION (from_mode)) 293 { 294 insn_code icode = convert_optab_handler (trunc_optab, 295 to_mode, from_mode); 296 if (icode != CODE_FOR_nothing) 297 { 298 emit_unop_insn (icode, to, from, TRUNCATE); 299 return; 300 } 301 } 302 303 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode), 304 GET_MODE_BITSIZE (to_mode))); 305 306 if (VECTOR_MODE_P (to_mode)) 307 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0); 308 else 309 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0); 310 311 emit_move_insn (to, from); 312 return; 313 } 314 315 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT) 316 { 317 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp); 318 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp); 319 return; 320 } 321 322 convert_mode_scalar (to, from, unsignedp); 323 } 324 325 /* Like convert_move, but deals only with scalar modes. */ 326 327 static void 328 convert_mode_scalar (rtx to, rtx from, int unsignedp) 329 { 330 /* Both modes should be scalar types. */ 331 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from)); 332 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to)); 333 bool to_real = SCALAR_FLOAT_MODE_P (to_mode); 334 bool from_real = SCALAR_FLOAT_MODE_P (from_mode); 335 enum insn_code code; 336 rtx libcall; 337 338 gcc_assert (to_real == from_real); 339 340 /* rtx code for making an equivalent value. */ 341 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN 342 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND)); 343 344 if (to_real) 345 { 346 rtx value; 347 rtx_insn *insns; 348 convert_optab tab; 349 350 gcc_assert ((GET_MODE_PRECISION (from_mode) 351 != GET_MODE_PRECISION (to_mode)) 352 || (DECIMAL_FLOAT_MODE_P (from_mode) 353 != DECIMAL_FLOAT_MODE_P (to_mode)) 354 || (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format 355 && REAL_MODE_FORMAT (to_mode) == &ieee_half_format) 356 || (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format 357 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format)); 358 359 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode)) 360 { 361 if (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format 362 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format) 363 /* libgcc implements just __trunchfbf2, not __extendhfbf2. */ 364 tab = trunc_optab; 365 else 366 /* Conversion between decimal float and binary float, same 367 size. */ 368 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab; 369 } 370 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)) 371 tab = sext_optab; 372 else 373 tab = trunc_optab; 374 375 /* Try converting directly if the insn is supported. */ 376 377 code = convert_optab_handler (tab, to_mode, from_mode); 378 if (code != CODE_FOR_nothing) 379 { 380 emit_unop_insn (code, to, from, 381 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE); 382 return; 383 } 384 385 #ifdef HAVE_SFmode 386 if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format 387 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format) 388 { 389 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode)) 390 { 391 /* To cut down on libgcc size, implement 392 BFmode -> {DF,XF,TF}mode conversions by 393 BFmode -> SFmode -> {DF,XF,TF}mode conversions. */ 394 rtx temp = gen_reg_rtx (SFmode); 395 convert_mode_scalar (temp, from, unsignedp); 396 convert_mode_scalar (to, temp, unsignedp); 397 return; 398 } 399 if (REAL_MODE_FORMAT (to_mode) == &ieee_half_format) 400 { 401 /* Similarly, implement BFmode -> HFmode as 402 BFmode -> SFmode -> HFmode conversion where SFmode 403 has superset of BFmode values. We don't need 404 to handle sNaNs by raising exception and turning 405 it into qNaN though, as that can be done in the 406 SFmode -> HFmode conversion too. */ 407 rtx temp = gen_reg_rtx (SFmode); 408 int save_flag_finite_math_only = flag_finite_math_only; 409 flag_finite_math_only = true; 410 convert_mode_scalar (temp, from, unsignedp); 411 flag_finite_math_only = save_flag_finite_math_only; 412 convert_mode_scalar (to, temp, unsignedp); 413 return; 414 } 415 if (to_mode == SFmode 416 && !HONOR_NANS (from_mode) 417 && !HONOR_NANS (to_mode) 418 && optimize_insn_for_speed_p ()) 419 { 420 /* If we don't expect sNaNs, for BFmode -> SFmode we can just 421 shift the bits up. */ 422 machine_mode fromi_mode, toi_mode; 423 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode), 424 0).exists (&fromi_mode) 425 && int_mode_for_size (GET_MODE_BITSIZE (to_mode), 426 0).exists (&toi_mode)) 427 { 428 start_sequence (); 429 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode); 430 rtx tof = NULL_RTX; 431 if (fromi) 432 { 433 rtx toi; 434 if (GET_MODE (fromi) == VOIDmode) 435 toi = simplify_unary_operation (ZERO_EXTEND, toi_mode, 436 fromi, fromi_mode); 437 else 438 { 439 toi = gen_reg_rtx (toi_mode); 440 convert_mode_scalar (toi, fromi, 1); 441 } 442 toi 443 = maybe_expand_shift (LSHIFT_EXPR, toi_mode, toi, 444 GET_MODE_PRECISION (to_mode) 445 - GET_MODE_PRECISION (from_mode), 446 NULL_RTX, 1); 447 if (toi) 448 { 449 tof = lowpart_subreg (to_mode, toi, toi_mode); 450 if (tof) 451 emit_move_insn (to, tof); 452 } 453 } 454 insns = get_insns (); 455 end_sequence (); 456 if (tof) 457 { 458 emit_insn (insns); 459 return; 460 } 461 } 462 } 463 } 464 if (REAL_MODE_FORMAT (from_mode) == &ieee_single_format 465 && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format 466 && !HONOR_NANS (from_mode) 467 && !HONOR_NANS (to_mode) 468 && !flag_rounding_math 469 && optimize_insn_for_speed_p ()) 470 { 471 /* If we don't expect qNaNs nor sNaNs and can assume rounding 472 to nearest, we can expand the conversion inline as 473 (fromi + 0x7fff + ((fromi >> 16) & 1)) >> 16. */ 474 machine_mode fromi_mode, toi_mode; 475 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode), 476 0).exists (&fromi_mode) 477 && int_mode_for_size (GET_MODE_BITSIZE (to_mode), 478 0).exists (&toi_mode)) 479 { 480 start_sequence (); 481 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode); 482 rtx tof = NULL_RTX; 483 do 484 { 485 if (!fromi) 486 break; 487 int shift = (GET_MODE_PRECISION (from_mode) 488 - GET_MODE_PRECISION (to_mode)); 489 rtx temp1 490 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, fromi, 491 shift, NULL_RTX, 1); 492 if (!temp1) 493 break; 494 rtx temp2 495 = expand_binop (fromi_mode, and_optab, temp1, const1_rtx, 496 NULL_RTX, 1, OPTAB_DIRECT); 497 if (!temp2) 498 break; 499 rtx temp3 500 = expand_binop (fromi_mode, add_optab, fromi, 501 gen_int_mode ((HOST_WIDE_INT_1U 502 << (shift - 1)) - 1, 503 fromi_mode), NULL_RTX, 504 1, OPTAB_DIRECT); 505 if (!temp3) 506 break; 507 rtx temp4 508 = expand_binop (fromi_mode, add_optab, temp3, temp2, 509 NULL_RTX, 1, OPTAB_DIRECT); 510 if (!temp4) 511 break; 512 rtx temp5 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, 513 temp4, shift, NULL_RTX, 1); 514 if (!temp5) 515 break; 516 rtx temp6 = lowpart_subreg (toi_mode, temp5, fromi_mode); 517 if (!temp6) 518 break; 519 tof = lowpart_subreg (to_mode, force_reg (toi_mode, temp6), 520 toi_mode); 521 if (tof) 522 emit_move_insn (to, tof); 523 } 524 while (0); 525 insns = get_insns (); 526 end_sequence (); 527 if (tof) 528 { 529 emit_insn (insns); 530 return; 531 } 532 } 533 } 534 #endif 535 536 /* Otherwise use a libcall. */ 537 libcall = convert_optab_libfunc (tab, to_mode, from_mode); 538 539 /* Is this conversion implemented yet? */ 540 gcc_assert (libcall); 541 542 start_sequence (); 543 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode, 544 from, from_mode); 545 insns = get_insns (); 546 end_sequence (); 547 emit_libcall_block (insns, to, value, 548 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode, 549 from) 550 : gen_rtx_FLOAT_EXTEND (to_mode, from)); 551 return; 552 } 553 554 /* Handle pointer conversion. */ /* SPEE 900220. */ 555 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */ 556 { 557 convert_optab ctab; 558 559 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode)) 560 ctab = trunc_optab; 561 else if (unsignedp) 562 ctab = zext_optab; 563 else 564 ctab = sext_optab; 565 566 if (convert_optab_handler (ctab, to_mode, from_mode) 567 != CODE_FOR_nothing) 568 { 569 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode), 570 to, from, UNKNOWN); 571 return; 572 } 573 } 574 575 /* Targets are expected to provide conversion insns between PxImode and 576 xImode for all MODE_PARTIAL_INT modes they use, but no others. */ 577 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT) 578 { 579 scalar_int_mode full_mode 580 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode)); 581 582 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode) 583 != CODE_FOR_nothing); 584 585 if (full_mode != from_mode) 586 from = convert_to_mode (full_mode, from, unsignedp); 587 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode), 588 to, from, UNKNOWN); 589 return; 590 } 591 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT) 592 { 593 rtx new_from; 594 scalar_int_mode full_mode 595 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode)); 596 convert_optab ctab = unsignedp ? zext_optab : sext_optab; 597 enum insn_code icode; 598 599 icode = convert_optab_handler (ctab, full_mode, from_mode); 600 gcc_assert (icode != CODE_FOR_nothing); 601 602 if (to_mode == full_mode) 603 { 604 emit_unop_insn (icode, to, from, UNKNOWN); 605 return; 606 } 607 608 new_from = gen_reg_rtx (full_mode); 609 emit_unop_insn (icode, new_from, from, UNKNOWN); 610 611 /* else proceed to integer conversions below. */ 612 from_mode = full_mode; 613 from = new_from; 614 } 615 616 /* Make sure both are fixed-point modes or both are not. */ 617 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) == 618 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode)); 619 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode)) 620 { 621 /* If we widen from_mode to to_mode and they are in the same class, 622 we won't saturate the result. 623 Otherwise, always saturate the result to play safe. */ 624 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode) 625 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode)) 626 expand_fixed_convert (to, from, 0, 0); 627 else 628 expand_fixed_convert (to, from, 0, 1); 629 return; 630 } 631 632 /* Now both modes are integers. */ 633 634 /* Handle expanding beyond a word. */ 635 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode) 636 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD) 637 { 638 rtx_insn *insns; 639 rtx lowpart; 640 rtx fill_value; 641 rtx lowfrom; 642 int i; 643 scalar_mode lowpart_mode; 644 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD); 645 646 /* Try converting directly if the insn is supported. */ 647 if ((code = can_extend_p (to_mode, from_mode, unsignedp)) 648 != CODE_FOR_nothing) 649 { 650 /* If FROM is a SUBREG, put it into a register. Do this 651 so that we always generate the same set of insns for 652 better cse'ing; if an intermediate assignment occurred, 653 we won't be doing the operation directly on the SUBREG. */ 654 if (optimize > 0 && GET_CODE (from) == SUBREG) 655 from = force_reg (from_mode, from); 656 emit_unop_insn (code, to, from, equiv_code); 657 return; 658 } 659 /* Next, try converting via full word. */ 660 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD 661 && ((code = can_extend_p (to_mode, word_mode, unsignedp)) 662 != CODE_FOR_nothing)) 663 { 664 rtx word_to = gen_reg_rtx (word_mode); 665 if (REG_P (to)) 666 { 667 if (reg_overlap_mentioned_p (to, from)) 668 from = force_reg (from_mode, from); 669 emit_clobber (to); 670 } 671 convert_move (word_to, from, unsignedp); 672 emit_unop_insn (code, to, word_to, equiv_code); 673 return; 674 } 675 676 /* No special multiword conversion insn; do it by hand. */ 677 start_sequence (); 678 679 /* Since we will turn this into a no conflict block, we must ensure 680 the source does not overlap the target so force it into an isolated 681 register when maybe so. Likewise for any MEM input, since the 682 conversion sequence might require several references to it and we 683 must ensure we're getting the same value every time. */ 684 685 if (MEM_P (from) || reg_overlap_mentioned_p (to, from)) 686 from = force_reg (from_mode, from); 687 688 /* Get a copy of FROM widened to a word, if necessary. */ 689 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD) 690 lowpart_mode = word_mode; 691 else 692 lowpart_mode = from_mode; 693 694 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp); 695 696 lowpart = gen_lowpart (lowpart_mode, to); 697 emit_move_insn (lowpart, lowfrom); 698 699 /* Compute the value to put in each remaining word. */ 700 if (unsignedp) 701 fill_value = const0_rtx; 702 else 703 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode), 704 LT, lowfrom, const0_rtx, 705 lowpart_mode, 0, -1); 706 707 /* Fill the remaining words. */ 708 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++) 709 { 710 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i); 711 rtx subword = operand_subword (to, index, 1, to_mode); 712 713 gcc_assert (subword); 714 715 if (fill_value != subword) 716 emit_move_insn (subword, fill_value); 717 } 718 719 insns = get_insns (); 720 end_sequence (); 721 722 emit_insn (insns); 723 return; 724 } 725 726 /* Truncating multi-word to a word or less. */ 727 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD 728 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD) 729 { 730 if (!((MEM_P (from) 731 && ! MEM_VOLATILE_P (from) 732 && direct_load[(int) to_mode] 733 && ! mode_dependent_address_p (XEXP (from, 0), 734 MEM_ADDR_SPACE (from))) 735 || REG_P (from) 736 || GET_CODE (from) == SUBREG)) 737 from = force_reg (from_mode, from); 738 convert_move (to, gen_lowpart (word_mode, from), 0); 739 return; 740 } 741 742 /* Now follow all the conversions between integers 743 no more than a word long. */ 744 745 /* For truncation, usually we can just refer to FROM in a narrower mode. */ 746 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode) 747 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode)) 748 { 749 if (!((MEM_P (from) 750 && ! MEM_VOLATILE_P (from) 751 && direct_load[(int) to_mode] 752 && ! mode_dependent_address_p (XEXP (from, 0), 753 MEM_ADDR_SPACE (from))) 754 || REG_P (from) 755 || GET_CODE (from) == SUBREG)) 756 from = force_reg (from_mode, from); 757 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER 758 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode)) 759 from = copy_to_reg (from); 760 emit_move_insn (to, gen_lowpart (to_mode, from)); 761 return; 762 } 763 764 /* Handle extension. */ 765 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode)) 766 { 767 /* Convert directly if that works. */ 768 if ((code = can_extend_p (to_mode, from_mode, unsignedp)) 769 != CODE_FOR_nothing) 770 { 771 emit_unop_insn (code, to, from, equiv_code); 772 return; 773 } 774 else 775 { 776 rtx tmp; 777 int shift_amount; 778 779 /* Search for a mode to convert via. */ 780 opt_scalar_mode intermediate_iter; 781 FOR_EACH_MODE_FROM (intermediate_iter, from_mode) 782 { 783 scalar_mode intermediate = intermediate_iter.require (); 784 if (((can_extend_p (to_mode, intermediate, unsignedp) 785 != CODE_FOR_nothing) 786 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate) 787 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, 788 intermediate))) 789 && (can_extend_p (intermediate, from_mode, unsignedp) 790 != CODE_FOR_nothing)) 791 { 792 convert_move (to, convert_to_mode (intermediate, from, 793 unsignedp), unsignedp); 794 return; 795 } 796 } 797 798 /* No suitable intermediate mode. 799 Generate what we need with shifts. */ 800 shift_amount = (GET_MODE_PRECISION (to_mode) 801 - GET_MODE_PRECISION (from_mode)); 802 from = gen_lowpart (to_mode, force_reg (from_mode, from)); 803 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount, 804 to, unsignedp); 805 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount, 806 to, unsignedp); 807 if (tmp != to) 808 emit_move_insn (to, tmp); 809 return; 810 } 811 } 812 813 /* Support special truncate insns for certain modes. */ 814 if (convert_optab_handler (trunc_optab, to_mode, 815 from_mode) != CODE_FOR_nothing) 816 { 817 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode), 818 to, from, UNKNOWN); 819 return; 820 } 821 822 /* Handle truncation of volatile memrefs, and so on; 823 the things that couldn't be truncated directly, 824 and for which there was no special instruction. 825 826 ??? Code above formerly short-circuited this, for most integer 827 mode pairs, with a force_reg in from_mode followed by a recursive 828 call to this routine. Appears always to have been wrong. */ 829 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode)) 830 { 831 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from)); 832 emit_move_insn (to, temp); 833 return; 834 } 835 836 /* Mode combination is not recognized. */ 837 gcc_unreachable (); 838 } 839 840 /* Return an rtx for a value that would result 841 from converting X to mode MODE. 842 Both X and MODE may be floating, or both integer. 843 UNSIGNEDP is nonzero if X is an unsigned value. 844 This can be done by referring to a part of X in place 845 or by copying to a new temporary with conversion. */ 846 847 rtx 848 convert_to_mode (machine_mode mode, rtx x, int unsignedp) 849 { 850 return convert_modes (mode, VOIDmode, x, unsignedp); 851 } 852 853 /* Return an rtx for a value that would result 854 from converting X from mode OLDMODE to mode MODE. 855 Both modes may be floating, or both integer. 856 UNSIGNEDP is nonzero if X is an unsigned value. 857 858 This can be done by referring to a part of X in place 859 or by copying to a new temporary with conversion. 860 861 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */ 862 863 rtx 864 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp) 865 { 866 rtx temp; 867 scalar_int_mode int_mode; 868 869 /* If FROM is a SUBREG that indicates that we have already done at least 870 the required extension, strip it. */ 871 872 if (GET_CODE (x) == SUBREG 873 && SUBREG_PROMOTED_VAR_P (x) 874 && is_a <scalar_int_mode> (mode, &int_mode) 875 && (GET_MODE_PRECISION (subreg_promoted_mode (x)) 876 >= GET_MODE_PRECISION (int_mode)) 877 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp)) 878 { 879 scalar_int_mode int_orig_mode; 880 scalar_int_mode int_inner_mode; 881 machine_mode orig_mode = GET_MODE (x); 882 x = gen_lowpart (int_mode, SUBREG_REG (x)); 883 884 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than 885 the original mode, but narrower than the inner mode. */ 886 if (GET_CODE (x) == SUBREG 887 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode) 888 && GET_MODE_PRECISION (int_mode) 889 > GET_MODE_PRECISION (int_orig_mode) 890 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)), 891 &int_inner_mode) 892 && GET_MODE_PRECISION (int_inner_mode) 893 > GET_MODE_PRECISION (int_mode)) 894 { 895 SUBREG_PROMOTED_VAR_P (x) = 1; 896 SUBREG_PROMOTED_SET (x, unsignedp); 897 } 898 } 899 900 if (GET_MODE (x) != VOIDmode) 901 oldmode = GET_MODE (x); 902 903 if (mode == oldmode) 904 return x; 905 906 if (CONST_SCALAR_INT_P (x) 907 && is_a <scalar_int_mode> (mode, &int_mode)) 908 { 909 /* If the caller did not tell us the old mode, then there is not 910 much to do with respect to canonicalization. We have to 911 assume that all the bits are significant. */ 912 if (!is_a <scalar_int_mode> (oldmode)) 913 oldmode = MAX_MODE_INT; 914 wide_int w = wide_int::from (rtx_mode_t (x, oldmode), 915 GET_MODE_PRECISION (int_mode), 916 unsignedp ? UNSIGNED : SIGNED); 917 return immed_wide_int_const (w, int_mode); 918 } 919 920 /* We can do this with a gen_lowpart if both desired and current modes 921 are integer, and this is either a constant integer, a register, or a 922 non-volatile MEM. */ 923 scalar_int_mode int_oldmode; 924 if (is_int_mode (mode, &int_mode) 925 && is_int_mode (oldmode, &int_oldmode) 926 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode) 927 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode]) 928 || CONST_POLY_INT_P (x) 929 || (REG_P (x) 930 && (!HARD_REGISTER_P (x) 931 || targetm.hard_regno_mode_ok (REGNO (x), int_mode)) 932 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x))))) 933 return gen_lowpart (int_mode, x); 934 935 /* Converting from integer constant into mode is always equivalent to an 936 subreg operation. */ 937 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode) 938 { 939 gcc_assert (known_eq (GET_MODE_BITSIZE (mode), 940 GET_MODE_BITSIZE (oldmode))); 941 return simplify_gen_subreg (mode, x, oldmode, 0); 942 } 943 944 temp = gen_reg_rtx (mode); 945 convert_move (temp, x, unsignedp); 946 return temp; 947 } 948 949 /* Variant of convert_modes for ABI parameter passing/return. 950 Return an rtx for a value that would result from converting X from 951 a floating point mode FMODE to wider integer mode MODE. */ 952 953 rtx 954 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x) 955 { 956 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode)); 957 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require (); 958 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x)); 959 return convert_modes (mode, tmp_mode, tmp, 1); 960 } 961 962 /* Variant of convert_modes for ABI parameter passing/return. 963 Return an rtx for a value that would result from converting X from 964 an integer mode IMODE to a narrower floating point mode MODE. */ 965 966 rtx 967 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x) 968 { 969 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode)); 970 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require (); 971 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x)); 972 return gen_lowpart_SUBREG (mode, tmp); 973 } 974 975 /* Return the largest alignment we can use for doing a move (or store) 977 of MAX_PIECES. ALIGN is the largest alignment we could use. */ 978 979 static unsigned int 980 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align) 981 { 982 scalar_int_mode tmode 983 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require (); 984 985 if (align >= GET_MODE_ALIGNMENT (tmode)) 986 align = GET_MODE_ALIGNMENT (tmode); 987 else 988 { 989 scalar_int_mode xmode = NARROWEST_INT_MODE; 990 opt_scalar_int_mode mode_iter; 991 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT) 992 { 993 tmode = mode_iter.require (); 994 if (GET_MODE_SIZE (tmode) > max_pieces 995 || targetm.slow_unaligned_access (tmode, align)) 996 break; 997 xmode = tmode; 998 } 999 1000 align = MAX (align, GET_MODE_ALIGNMENT (xmode)); 1001 } 1002 1003 return align; 1004 } 1005 1006 /* Return true if we know how to implement OP using vectors of bytes. */ 1007 static bool 1008 can_use_qi_vectors (by_pieces_operation op) 1009 { 1010 return (op == COMPARE_BY_PIECES 1011 || op == SET_BY_PIECES 1012 || op == CLEAR_BY_PIECES); 1013 } 1014 1015 /* Return true if optabs exists for the mode and certain by pieces 1016 operations. */ 1017 static bool 1018 by_pieces_mode_supported_p (fixed_size_mode mode, by_pieces_operation op) 1019 { 1020 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing) 1021 return false; 1022 1023 if ((op == SET_BY_PIECES || op == CLEAR_BY_PIECES) 1024 && VECTOR_MODE_P (mode) 1025 && optab_handler (vec_duplicate_optab, mode) == CODE_FOR_nothing) 1026 return false; 1027 1028 if (op == COMPARE_BY_PIECES 1029 && !can_compare_p (EQ, mode, ccp_jump)) 1030 return false; 1031 1032 return true; 1033 } 1034 1035 /* Return the widest mode that can be used to perform part of an 1036 operation OP on SIZE bytes. Try to use QI vector modes where 1037 possible. */ 1038 static fixed_size_mode 1039 widest_fixed_size_mode_for_size (unsigned int size, by_pieces_operation op) 1040 { 1041 fixed_size_mode result = NARROWEST_INT_MODE; 1042 1043 gcc_checking_assert (size > 1); 1044 1045 /* Use QI vector only if size is wider than a WORD. */ 1046 if (can_use_qi_vectors (op) && size > UNITS_PER_WORD) 1047 { 1048 machine_mode mode; 1049 fixed_size_mode candidate; 1050 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT) 1051 if (is_a<fixed_size_mode> (mode, &candidate) 1052 && GET_MODE_INNER (candidate) == QImode) 1053 { 1054 if (GET_MODE_SIZE (candidate) >= size) 1055 break; 1056 if (by_pieces_mode_supported_p (candidate, op)) 1057 result = candidate; 1058 } 1059 1060 if (result != NARROWEST_INT_MODE) 1061 return result; 1062 } 1063 1064 opt_scalar_int_mode tmode; 1065 scalar_int_mode mode; 1066 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT) 1067 { 1068 mode = tmode.require (); 1069 if (GET_MODE_SIZE (mode) < size 1070 && by_pieces_mode_supported_p (mode, op)) 1071 result = mode; 1072 } 1073 1074 return result; 1075 } 1076 1077 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can 1078 and should be performed piecewise. */ 1079 1080 static bool 1081 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align, 1082 enum by_pieces_operation op) 1083 { 1084 return targetm.use_by_pieces_infrastructure_p (len, align, op, 1085 optimize_insn_for_speed_p ()); 1086 } 1087 1088 /* Determine whether the LEN bytes can be moved by using several move 1089 instructions. Return nonzero if a call to move_by_pieces should 1090 succeed. */ 1091 1092 bool 1093 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align) 1094 { 1095 return can_do_by_pieces (len, align, MOVE_BY_PIECES); 1096 } 1097 1098 /* Return number of insns required to perform operation OP by pieces 1099 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */ 1100 1101 unsigned HOST_WIDE_INT 1102 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align, 1103 unsigned int max_size, by_pieces_operation op) 1104 { 1105 unsigned HOST_WIDE_INT n_insns = 0; 1106 fixed_size_mode mode; 1107 1108 if (targetm.overlap_op_by_pieces_p ()) 1109 { 1110 /* NB: Round up L and ALIGN to the widest integer mode for 1111 MAX_SIZE. */ 1112 mode = widest_fixed_size_mode_for_size (max_size, op); 1113 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing); 1114 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode)); 1115 if (up > l) 1116 l = up; 1117 align = GET_MODE_ALIGNMENT (mode); 1118 } 1119 1120 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align); 1121 1122 while (max_size > 1 && l > 0) 1123 { 1124 mode = widest_fixed_size_mode_for_size (max_size, op); 1125 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing); 1126 1127 unsigned int modesize = GET_MODE_SIZE (mode); 1128 1129 if (align >= GET_MODE_ALIGNMENT (mode)) 1130 { 1131 unsigned HOST_WIDE_INT n_pieces = l / modesize; 1132 l %= modesize; 1133 switch (op) 1134 { 1135 default: 1136 n_insns += n_pieces; 1137 break; 1138 1139 case COMPARE_BY_PIECES: 1140 int batch = targetm.compare_by_pieces_branch_ratio (mode); 1141 int batch_ops = 4 * batch - 1; 1142 unsigned HOST_WIDE_INT full = n_pieces / batch; 1143 n_insns += full * batch_ops; 1144 if (n_pieces % batch != 0) 1145 n_insns++; 1146 break; 1147 1148 } 1149 } 1150 max_size = modesize; 1151 } 1152 1153 gcc_assert (!l); 1154 return n_insns; 1155 } 1156 1157 /* Used when performing piecewise block operations, holds information 1158 about one of the memory objects involved. The member functions 1159 can be used to generate code for loading from the object and 1160 updating the address when iterating. */ 1161 1162 class pieces_addr 1163 { 1164 /* The object being referenced, a MEM. Can be NULL_RTX to indicate 1165 stack pushes. */ 1166 rtx m_obj; 1167 /* The address of the object. Can differ from that seen in the 1168 MEM rtx if we copied the address to a register. */ 1169 rtx m_addr; 1170 /* Nonzero if the address on the object has an autoincrement already, 1171 signifies whether that was an increment or decrement. */ 1172 signed char m_addr_inc; 1173 /* Nonzero if we intend to use autoinc without the address already 1174 having autoinc form. We will insert add insns around each memory 1175 reference, expecting later passes to form autoinc addressing modes. 1176 The only supported options are predecrement and postincrement. */ 1177 signed char m_explicit_inc; 1178 /* True if we have either of the two possible cases of using 1179 autoincrement. */ 1180 bool m_auto; 1181 /* True if this is an address to be used for load operations rather 1182 than stores. */ 1183 bool m_is_load; 1184 1185 /* Optionally, a function to obtain constants for any given offset into 1186 the objects, and data associated with it. */ 1187 by_pieces_constfn m_constfn; 1188 void *m_cfndata; 1189 public: 1190 pieces_addr (rtx, bool, by_pieces_constfn, void *); 1191 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr); 1192 void increment_address (HOST_WIDE_INT); 1193 void maybe_predec (HOST_WIDE_INT); 1194 void maybe_postinc (HOST_WIDE_INT); 1195 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT); 1196 int get_addr_inc () 1197 { 1198 return m_addr_inc; 1199 } 1200 }; 1201 1202 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is 1203 true if the operation to be performed on this object is a load 1204 rather than a store. For stores, OBJ can be NULL, in which case we 1205 assume the operation is a stack push. For loads, the optional 1206 CONSTFN and its associated CFNDATA can be used in place of the 1207 memory load. */ 1208 1209 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn, 1210 void *cfndata) 1211 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata) 1212 { 1213 m_addr_inc = 0; 1214 m_auto = false; 1215 if (obj) 1216 { 1217 rtx addr = XEXP (obj, 0); 1218 rtx_code code = GET_CODE (addr); 1219 m_addr = addr; 1220 bool dec = code == PRE_DEC || code == POST_DEC; 1221 bool inc = code == PRE_INC || code == POST_INC; 1222 m_auto = inc || dec; 1223 if (m_auto) 1224 m_addr_inc = dec ? -1 : 1; 1225 1226 /* While we have always looked for these codes here, the code 1227 implementing the memory operation has never handled them. 1228 Support could be added later if necessary or beneficial. */ 1229 gcc_assert (code != PRE_INC && code != POST_DEC); 1230 } 1231 else 1232 { 1233 m_addr = NULL_RTX; 1234 if (!is_load) 1235 { 1236 m_auto = true; 1237 if (STACK_GROWS_DOWNWARD) 1238 m_addr_inc = -1; 1239 else 1240 m_addr_inc = 1; 1241 } 1242 else 1243 gcc_assert (constfn != NULL); 1244 } 1245 m_explicit_inc = 0; 1246 if (constfn) 1247 gcc_assert (is_load); 1248 } 1249 1250 /* Decide whether to use autoinc for an address involved in a memory op. 1251 MODE is the mode of the accesses, REVERSE is true if we've decided to 1252 perform the operation starting from the end, and LEN is the length of 1253 the operation. Don't override an earlier decision to set m_auto. */ 1254 1255 void 1256 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse, 1257 HOST_WIDE_INT len) 1258 { 1259 if (m_auto || m_obj == NULL_RTX) 1260 return; 1261 1262 bool use_predec = (m_is_load 1263 ? USE_LOAD_PRE_DECREMENT (mode) 1264 : USE_STORE_PRE_DECREMENT (mode)); 1265 bool use_postinc = (m_is_load 1266 ? USE_LOAD_POST_INCREMENT (mode) 1267 : USE_STORE_POST_INCREMENT (mode)); 1268 machine_mode addr_mode = get_address_mode (m_obj); 1269 1270 if (use_predec && reverse) 1271 { 1272 m_addr = copy_to_mode_reg (addr_mode, 1273 plus_constant (addr_mode, 1274 m_addr, len)); 1275 m_auto = true; 1276 m_explicit_inc = -1; 1277 } 1278 else if (use_postinc && !reverse) 1279 { 1280 m_addr = copy_to_mode_reg (addr_mode, m_addr); 1281 m_auto = true; 1282 m_explicit_inc = 1; 1283 } 1284 else if (CONSTANT_P (m_addr)) 1285 m_addr = copy_to_mode_reg (addr_mode, m_addr); 1286 } 1287 1288 /* Adjust the address to refer to the data at OFFSET in MODE. If we 1289 are using autoincrement for this address, we don't add the offset, 1290 but we still modify the MEM's properties. */ 1291 1292 rtx 1293 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset, 1294 by_pieces_prev *prev) 1295 { 1296 if (m_constfn) 1297 /* Pass the previous data to m_constfn. */ 1298 return m_constfn (m_cfndata, prev, offset, mode); 1299 if (m_obj == NULL_RTX) 1300 return NULL_RTX; 1301 if (m_auto) 1302 return adjust_automodify_address (m_obj, mode, m_addr, offset); 1303 else 1304 return adjust_address (m_obj, mode, offset); 1305 } 1306 1307 /* Emit an add instruction to increment the address by SIZE. */ 1308 1309 void 1310 pieces_addr::increment_address (HOST_WIDE_INT size) 1311 { 1312 rtx amount = gen_int_mode (size, GET_MODE (m_addr)); 1313 emit_insn (gen_add2_insn (m_addr, amount)); 1314 } 1315 1316 /* If we are supposed to decrement the address after each access, emit code 1317 to do so now. Increment by SIZE (which has should have the correct sign 1318 already). */ 1319 1320 void 1321 pieces_addr::maybe_predec (HOST_WIDE_INT size) 1322 { 1323 if (m_explicit_inc >= 0) 1324 return; 1325 gcc_assert (HAVE_PRE_DECREMENT); 1326 increment_address (size); 1327 } 1328 1329 /* If we are supposed to decrement the address after each access, emit code 1330 to do so now. Increment by SIZE. */ 1331 1332 void 1333 pieces_addr::maybe_postinc (HOST_WIDE_INT size) 1334 { 1335 if (m_explicit_inc <= 0) 1336 return; 1337 gcc_assert (HAVE_POST_INCREMENT); 1338 increment_address (size); 1339 } 1340 1341 /* This structure is used by do_op_by_pieces to describe the operation 1342 to be performed. */ 1343 1344 class op_by_pieces_d 1345 { 1346 private: 1347 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int); 1348 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int); 1349 1350 protected: 1351 pieces_addr m_to, m_from; 1352 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can 1353 use it to check the valid mode size. */ 1354 const unsigned HOST_WIDE_INT m_len; 1355 HOST_WIDE_INT m_offset; 1356 unsigned int m_align; 1357 unsigned int m_max_size; 1358 bool m_reverse; 1359 /* True if this is a stack push. */ 1360 bool m_push; 1361 /* True if targetm.overlap_op_by_pieces_p () returns true. */ 1362 bool m_overlap_op_by_pieces; 1363 /* The type of operation that we're performing. */ 1364 by_pieces_operation m_op; 1365 1366 /* Virtual functions, overriden by derived classes for the specific 1367 operation. */ 1368 virtual void generate (rtx, rtx, machine_mode) = 0; 1369 virtual bool prepare_mode (machine_mode, unsigned int) = 0; 1370 virtual void finish_mode (machine_mode) 1371 { 1372 } 1373 1374 public: 1375 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn, 1376 void *, unsigned HOST_WIDE_INT, unsigned int, bool, 1377 by_pieces_operation); 1378 void run (); 1379 }; 1380 1381 /* The constructor for an op_by_pieces_d structure. We require two 1382 objects named TO and FROM, which are identified as loads or stores 1383 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN 1384 and its associated FROM_CFN_DATA can be used to replace loads with 1385 constant values. MAX_PIECES describes the maximum number of bytes 1386 at a time which can be moved efficiently. LEN describes the length 1387 of the operation. */ 1388 1389 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to, 1390 bool to_load, rtx from, bool from_load, 1391 by_pieces_constfn from_cfn, 1392 void *from_cfn_data, 1393 unsigned HOST_WIDE_INT len, 1394 unsigned int align, bool push, 1395 by_pieces_operation op) 1396 : m_to (to, to_load, NULL, NULL), 1397 m_from (from, from_load, from_cfn, from_cfn_data), 1398 m_len (len), m_max_size (max_pieces + 1), 1399 m_push (push), m_op (op) 1400 { 1401 int toi = m_to.get_addr_inc (); 1402 int fromi = m_from.get_addr_inc (); 1403 if (toi >= 0 && fromi >= 0) 1404 m_reverse = false; 1405 else if (toi <= 0 && fromi <= 0) 1406 m_reverse = true; 1407 else 1408 gcc_unreachable (); 1409 1410 m_offset = m_reverse ? len : 0; 1411 align = MIN (to ? MEM_ALIGN (to) : align, 1412 from ? MEM_ALIGN (from) : align); 1413 1414 /* If copying requires more than two move insns, 1415 copy addresses to registers (to make displacements shorter) 1416 and use post-increment if available. */ 1417 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2) 1418 { 1419 /* Find the mode of the largest comparison. */ 1420 fixed_size_mode mode 1421 = widest_fixed_size_mode_for_size (m_max_size, m_op); 1422 1423 m_from.decide_autoinc (mode, m_reverse, len); 1424 m_to.decide_autoinc (mode, m_reverse, len); 1425 } 1426 1427 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align); 1428 m_align = align; 1429 1430 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p (); 1431 } 1432 1433 /* This function returns the largest usable integer mode for LEN bytes 1434 whose size is no bigger than size of MODE. */ 1435 1436 fixed_size_mode 1437 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len) 1438 { 1439 unsigned int size; 1440 do 1441 { 1442 size = GET_MODE_SIZE (mode); 1443 if (len >= size && prepare_mode (mode, m_align)) 1444 break; 1445 /* widest_fixed_size_mode_for_size checks SIZE > 1. */ 1446 mode = widest_fixed_size_mode_for_size (size, m_op); 1447 } 1448 while (1); 1449 return mode; 1450 } 1451 1452 /* Return the smallest integer or QI vector mode that is not narrower 1453 than SIZE bytes. */ 1454 1455 fixed_size_mode 1456 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size) 1457 { 1458 /* Use QI vector only for > size of WORD. */ 1459 if (can_use_qi_vectors (m_op) && size > UNITS_PER_WORD) 1460 { 1461 machine_mode mode; 1462 fixed_size_mode candidate; 1463 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT) 1464 if (is_a<fixed_size_mode> (mode, &candidate) 1465 && GET_MODE_INNER (candidate) == QImode) 1466 { 1467 /* Don't return a mode wider than M_LEN. */ 1468 if (GET_MODE_SIZE (candidate) > m_len) 1469 break; 1470 1471 if (GET_MODE_SIZE (candidate) >= size 1472 && by_pieces_mode_supported_p (candidate, m_op)) 1473 return candidate; 1474 } 1475 } 1476 1477 return smallest_int_mode_for_size (size * BITS_PER_UNIT); 1478 } 1479 1480 /* This function contains the main loop used for expanding a block 1481 operation. First move what we can in the largest integer mode, 1482 then go to successively smaller modes. For every access, call 1483 GENFUN with the two operands and the EXTRA_DATA. */ 1484 1485 void 1486 op_by_pieces_d::run () 1487 { 1488 if (m_len == 0) 1489 return; 1490 1491 unsigned HOST_WIDE_INT length = m_len; 1492 1493 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */ 1494 fixed_size_mode mode 1495 = widest_fixed_size_mode_for_size (m_max_size, m_op); 1496 mode = get_usable_mode (mode, length); 1497 1498 by_pieces_prev to_prev = { nullptr, mode }; 1499 by_pieces_prev from_prev = { nullptr, mode }; 1500 1501 do 1502 { 1503 unsigned int size = GET_MODE_SIZE (mode); 1504 rtx to1 = NULL_RTX, from1; 1505 1506 while (length >= size) 1507 { 1508 if (m_reverse) 1509 m_offset -= size; 1510 1511 to1 = m_to.adjust (mode, m_offset, &to_prev); 1512 to_prev.data = to1; 1513 to_prev.mode = mode; 1514 from1 = m_from.adjust (mode, m_offset, &from_prev); 1515 from_prev.data = from1; 1516 from_prev.mode = mode; 1517 1518 m_to.maybe_predec (-(HOST_WIDE_INT)size); 1519 m_from.maybe_predec (-(HOST_WIDE_INT)size); 1520 1521 generate (to1, from1, mode); 1522 1523 m_to.maybe_postinc (size); 1524 m_from.maybe_postinc (size); 1525 1526 if (!m_reverse) 1527 m_offset += size; 1528 1529 length -= size; 1530 } 1531 1532 finish_mode (mode); 1533 1534 if (length == 0) 1535 return; 1536 1537 if (!m_push && m_overlap_op_by_pieces) 1538 { 1539 /* NB: Generate overlapping operations if it is not a stack 1540 push since stack push must not overlap. Get the smallest 1541 fixed size mode for M_LEN bytes. */ 1542 mode = smallest_fixed_size_mode_for_size (length); 1543 mode = get_usable_mode (mode, GET_MODE_SIZE (mode)); 1544 int gap = GET_MODE_SIZE (mode) - length; 1545 if (gap > 0) 1546 { 1547 /* If size of MODE > M_LEN, generate the last operation 1548 in MODE for the remaining bytes with ovelapping memory 1549 from the previois operation. */ 1550 if (m_reverse) 1551 m_offset += gap; 1552 else 1553 m_offset -= gap; 1554 length += gap; 1555 } 1556 } 1557 else 1558 { 1559 /* widest_fixed_size_mode_for_size checks SIZE > 1. */ 1560 mode = widest_fixed_size_mode_for_size (size, m_op); 1561 mode = get_usable_mode (mode, length); 1562 } 1563 } 1564 while (1); 1565 } 1566 1567 /* Derived class from op_by_pieces_d, providing support for block move 1568 operations. */ 1569 1570 #ifdef PUSH_ROUNDING 1571 #define PUSHG_P(to) ((to) == nullptr) 1572 #else 1573 #define PUSHG_P(to) false 1574 #endif 1575 1576 class move_by_pieces_d : public op_by_pieces_d 1577 { 1578 insn_gen_fn m_gen_fun; 1579 void generate (rtx, rtx, machine_mode) final override; 1580 bool prepare_mode (machine_mode, unsigned int) final override; 1581 1582 public: 1583 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len, 1584 unsigned int align) 1585 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL, 1586 NULL, len, align, PUSHG_P (to), MOVE_BY_PIECES) 1587 { 1588 } 1589 rtx finish_retmode (memop_ret); 1590 }; 1591 1592 /* Return true if MODE can be used for a set of copies, given an 1593 alignment ALIGN. Prepare whatever data is necessary for later 1594 calls to generate. */ 1595 1596 bool 1597 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align) 1598 { 1599 insn_code icode = optab_handler (mov_optab, mode); 1600 m_gen_fun = GEN_FCN (icode); 1601 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode); 1602 } 1603 1604 /* A callback used when iterating for a compare_by_pieces_operation. 1605 OP0 and OP1 are the values that have been loaded and should be 1606 compared in MODE. If OP0 is NULL, this means we should generate a 1607 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn 1608 gen function that should be used to generate the mode. */ 1609 1610 void 1611 move_by_pieces_d::generate (rtx op0, rtx op1, 1612 machine_mode mode ATTRIBUTE_UNUSED) 1613 { 1614 #ifdef PUSH_ROUNDING 1615 if (op0 == NULL_RTX) 1616 { 1617 emit_single_push_insn (mode, op1, NULL); 1618 return; 1619 } 1620 #endif 1621 emit_insn (m_gen_fun (op0, op1)); 1622 } 1623 1624 /* Perform the final adjustment at the end of a string to obtain the 1625 correct return value for the block operation. 1626 Return value is based on RETMODE argument. */ 1627 1628 rtx 1629 move_by_pieces_d::finish_retmode (memop_ret retmode) 1630 { 1631 gcc_assert (!m_reverse); 1632 if (retmode == RETURN_END_MINUS_ONE) 1633 { 1634 m_to.maybe_postinc (-1); 1635 --m_offset; 1636 } 1637 return m_to.adjust (QImode, m_offset); 1638 } 1639 1640 /* Generate several move instructions to copy LEN bytes from block FROM to 1641 block TO. (These are MEM rtx's with BLKmode). 1642 1643 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is 1644 used to push FROM to the stack. 1645 1646 ALIGN is maximum stack alignment we can assume. 1647 1648 Return value is based on RETMODE argument. */ 1649 1650 rtx 1651 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len, 1652 unsigned int align, memop_ret retmode) 1653 { 1654 #ifndef PUSH_ROUNDING 1655 if (to == NULL) 1656 gcc_unreachable (); 1657 #endif 1658 1659 move_by_pieces_d data (to, from, len, align); 1660 1661 data.run (); 1662 1663 if (retmode != RETURN_BEGIN) 1664 return data.finish_retmode (retmode); 1665 else 1666 return to; 1667 } 1668 1669 /* Derived class from op_by_pieces_d, providing support for block move 1670 operations. */ 1671 1672 class store_by_pieces_d : public op_by_pieces_d 1673 { 1674 insn_gen_fn m_gen_fun; 1675 1676 void generate (rtx, rtx, machine_mode) final override; 1677 bool prepare_mode (machine_mode, unsigned int) final override; 1678 1679 public: 1680 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data, 1681 unsigned HOST_WIDE_INT len, unsigned int align, 1682 by_pieces_operation op) 1683 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn, 1684 cfn_data, len, align, false, op) 1685 { 1686 } 1687 rtx finish_retmode (memop_ret); 1688 }; 1689 1690 /* Return true if MODE can be used for a set of stores, given an 1691 alignment ALIGN. Prepare whatever data is necessary for later 1692 calls to generate. */ 1693 1694 bool 1695 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align) 1696 { 1697 insn_code icode = optab_handler (mov_optab, mode); 1698 m_gen_fun = GEN_FCN (icode); 1699 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode); 1700 } 1701 1702 /* A callback used when iterating for a store_by_pieces_operation. 1703 OP0 and OP1 are the values that have been loaded and should be 1704 compared in MODE. If OP0 is NULL, this means we should generate a 1705 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn 1706 gen function that should be used to generate the mode. */ 1707 1708 void 1709 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode) 1710 { 1711 emit_insn (m_gen_fun (op0, op1)); 1712 } 1713 1714 /* Perform the final adjustment at the end of a string to obtain the 1715 correct return value for the block operation. 1716 Return value is based on RETMODE argument. */ 1717 1718 rtx 1719 store_by_pieces_d::finish_retmode (memop_ret retmode) 1720 { 1721 gcc_assert (!m_reverse); 1722 if (retmode == RETURN_END_MINUS_ONE) 1723 { 1724 m_to.maybe_postinc (-1); 1725 --m_offset; 1726 } 1727 return m_to.adjust (QImode, m_offset); 1728 } 1729 1730 /* Determine whether the LEN bytes generated by CONSTFUN can be 1731 stored to memory using several move instructions. CONSTFUNDATA is 1732 a pointer which will be passed as argument in every CONSTFUN call. 1733 ALIGN is maximum alignment we can assume. MEMSETP is true if this is 1734 a memset operation and false if it's a copy of a constant string. 1735 Return true if a call to store_by_pieces should succeed. */ 1736 1737 bool 1738 can_store_by_pieces (unsigned HOST_WIDE_INT len, 1739 by_pieces_constfn constfun, 1740 void *constfundata, unsigned int align, bool memsetp) 1741 { 1742 unsigned HOST_WIDE_INT l; 1743 unsigned int max_size; 1744 HOST_WIDE_INT offset = 0; 1745 enum insn_code icode; 1746 int reverse; 1747 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */ 1748 rtx cst ATTRIBUTE_UNUSED; 1749 1750 if (len == 0) 1751 return true; 1752 1753 if (!targetm.use_by_pieces_infrastructure_p (len, align, 1754 memsetp 1755 ? SET_BY_PIECES 1756 : STORE_BY_PIECES, 1757 optimize_insn_for_speed_p ())) 1758 return false; 1759 1760 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align); 1761 1762 /* We would first store what we can in the largest integer mode, then go to 1763 successively smaller modes. */ 1764 1765 for (reverse = 0; 1766 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT); 1767 reverse++) 1768 { 1769 l = len; 1770 max_size = STORE_MAX_PIECES + 1; 1771 while (max_size > 1 && l > 0) 1772 { 1773 auto op = memsetp ? SET_BY_PIECES : STORE_BY_PIECES; 1774 auto mode = widest_fixed_size_mode_for_size (max_size, op); 1775 1776 icode = optab_handler (mov_optab, mode); 1777 if (icode != CODE_FOR_nothing 1778 && align >= GET_MODE_ALIGNMENT (mode)) 1779 { 1780 unsigned int size = GET_MODE_SIZE (mode); 1781 1782 while (l >= size) 1783 { 1784 if (reverse) 1785 offset -= size; 1786 1787 cst = (*constfun) (constfundata, nullptr, offset, mode); 1788 /* All CONST_VECTORs can be loaded for memset since 1789 vec_duplicate_optab is a precondition to pick a 1790 vector mode for the memset expander. */ 1791 if (!((memsetp && VECTOR_MODE_P (mode)) 1792 || targetm.legitimate_constant_p (mode, cst))) 1793 return false; 1794 1795 if (!reverse) 1796 offset += size; 1797 1798 l -= size; 1799 } 1800 } 1801 1802 max_size = GET_MODE_SIZE (mode); 1803 } 1804 1805 /* The code above should have handled everything. */ 1806 gcc_assert (!l); 1807 } 1808 1809 return true; 1810 } 1811 1812 /* Generate several move instructions to store LEN bytes generated by 1813 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a 1814 pointer which will be passed as argument in every CONSTFUN call. 1815 ALIGN is maximum alignment we can assume. MEMSETP is true if this is 1816 a memset operation and false if it's a copy of a constant string. 1817 Return value is based on RETMODE argument. */ 1818 1819 rtx 1820 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len, 1821 by_pieces_constfn constfun, 1822 void *constfundata, unsigned int align, bool memsetp, 1823 memop_ret retmode) 1824 { 1825 if (len == 0) 1826 { 1827 gcc_assert (retmode != RETURN_END_MINUS_ONE); 1828 return to; 1829 } 1830 1831 gcc_assert (targetm.use_by_pieces_infrastructure_p 1832 (len, align, 1833 memsetp ? SET_BY_PIECES : STORE_BY_PIECES, 1834 optimize_insn_for_speed_p ())); 1835 1836 store_by_pieces_d data (to, constfun, constfundata, len, align, 1837 memsetp ? SET_BY_PIECES : STORE_BY_PIECES); 1838 data.run (); 1839 1840 if (retmode != RETURN_BEGIN) 1841 return data.finish_retmode (retmode); 1842 else 1843 return to; 1844 } 1845 1846 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM 1847 rtx with BLKmode). ALIGN is maximum alignment we can assume. */ 1848 1849 static void 1850 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align) 1851 { 1852 if (len == 0) 1853 return; 1854 1855 /* Use builtin_memset_read_str to support vector mode broadcast. */ 1856 char c = 0; 1857 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align, 1858 CLEAR_BY_PIECES); 1859 data.run (); 1860 } 1861 1862 /* Context used by compare_by_pieces_genfn. It stores the fail label 1863 to jump to in case of miscomparison, and for branch ratios greater than 1, 1864 it stores an accumulator and the current and maximum counts before 1865 emitting another branch. */ 1866 1867 class compare_by_pieces_d : public op_by_pieces_d 1868 { 1869 rtx_code_label *m_fail_label; 1870 rtx m_accumulator; 1871 int m_count, m_batch; 1872 1873 void generate (rtx, rtx, machine_mode) final override; 1874 bool prepare_mode (machine_mode, unsigned int) final override; 1875 void finish_mode (machine_mode) final override; 1876 1877 public: 1878 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn, 1879 void *op1_cfn_data, HOST_WIDE_INT len, int align, 1880 rtx_code_label *fail_label) 1881 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn, 1882 op1_cfn_data, len, align, false, COMPARE_BY_PIECES) 1883 { 1884 m_fail_label = fail_label; 1885 } 1886 }; 1887 1888 /* A callback used when iterating for a compare_by_pieces_operation. 1889 OP0 and OP1 are the values that have been loaded and should be 1890 compared in MODE. DATA holds a pointer to the compare_by_pieces_data 1891 context structure. */ 1892 1893 void 1894 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode) 1895 { 1896 if (m_batch > 1) 1897 { 1898 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, 1899 true, OPTAB_LIB_WIDEN); 1900 if (m_count != 0) 1901 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp, 1902 true, OPTAB_LIB_WIDEN); 1903 m_accumulator = temp; 1904 1905 if (++m_count < m_batch) 1906 return; 1907 1908 m_count = 0; 1909 op0 = m_accumulator; 1910 op1 = const0_rtx; 1911 m_accumulator = NULL_RTX; 1912 } 1913 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL, 1914 m_fail_label, profile_probability::uninitialized ()); 1915 } 1916 1917 /* Return true if MODE can be used for a set of moves and comparisons, 1918 given an alignment ALIGN. Prepare whatever data is necessary for 1919 later calls to generate. */ 1920 1921 bool 1922 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align) 1923 { 1924 insn_code icode = optab_handler (mov_optab, mode); 1925 if (icode == CODE_FOR_nothing 1926 || align < GET_MODE_ALIGNMENT (mode) 1927 || !can_compare_p (EQ, mode, ccp_jump)) 1928 return false; 1929 m_batch = targetm.compare_by_pieces_branch_ratio (mode); 1930 if (m_batch < 0) 1931 return false; 1932 m_accumulator = NULL_RTX; 1933 m_count = 0; 1934 return true; 1935 } 1936 1937 /* Called after expanding a series of comparisons in MODE. If we have 1938 accumulated results for which we haven't emitted a branch yet, do 1939 so now. */ 1940 1941 void 1942 compare_by_pieces_d::finish_mode (machine_mode mode) 1943 { 1944 if (m_accumulator != NULL_RTX) 1945 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode, 1946 NULL_RTX, NULL, m_fail_label, 1947 profile_probability::uninitialized ()); 1948 } 1949 1950 /* Generate several move instructions to compare LEN bytes from blocks 1951 ARG0 and ARG1. (These are MEM rtx's with BLKmode). 1952 1953 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is 1954 used to push FROM to the stack. 1955 1956 ALIGN is maximum stack alignment we can assume. 1957 1958 Optionally, the caller can pass a constfn and associated data in A1_CFN 1959 and A1_CFN_DATA. describing that the second operand being compared is a 1960 known constant and how to obtain its data. */ 1961 1962 static rtx 1963 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len, 1964 rtx target, unsigned int align, 1965 by_pieces_constfn a1_cfn, void *a1_cfn_data) 1966 { 1967 rtx_code_label *fail_label = gen_label_rtx (); 1968 rtx_code_label *end_label = gen_label_rtx (); 1969 1970 if (target == NULL_RTX 1971 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER) 1972 target = gen_reg_rtx (TYPE_MODE (integer_type_node)); 1973 1974 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align, 1975 fail_label); 1976 1977 data.run (); 1978 1979 emit_move_insn (target, const0_rtx); 1980 emit_jump (end_label); 1981 emit_barrier (); 1982 emit_label (fail_label); 1983 emit_move_insn (target, const1_rtx); 1984 emit_label (end_label); 1985 1986 return target; 1987 } 1988 1989 /* Emit code to move a block Y to a block X. This may be done with 1991 string-move instructions, with multiple scalar move instructions, 1992 or with a library call. 1993 1994 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode. 1995 SIZE is an rtx that says how long they are. 1996 ALIGN is the maximum alignment we can assume they have. 1997 METHOD describes what kind of copy this is, and what mechanisms may be used. 1998 MIN_SIZE is the minimal size of block to move 1999 MAX_SIZE is the maximal size of block to move, if it cannot be represented 2000 in unsigned HOST_WIDE_INT, than it is mask of all ones. 2001 CTZ_SIZE is the trailing-zeros count of SIZE; even a nonconstant SIZE is 2002 known to be a multiple of 1<<CTZ_SIZE. 2003 2004 Return the address of the new block, if memcpy is called and returns it, 2005 0 otherwise. */ 2006 2007 rtx 2008 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method, 2009 unsigned int expected_align, HOST_WIDE_INT expected_size, 2010 unsigned HOST_WIDE_INT min_size, 2011 unsigned HOST_WIDE_INT max_size, 2012 unsigned HOST_WIDE_INT probable_max_size, 2013 bool bail_out_libcall, bool *is_move_done, 2014 bool might_overlap, unsigned ctz_size) 2015 { 2016 int may_use_call; 2017 rtx retval = 0; 2018 unsigned int align; 2019 2020 if (is_move_done) 2021 *is_move_done = true; 2022 2023 gcc_assert (size); 2024 if (CONST_INT_P (size) && INTVAL (size) == 0) 2025 return 0; 2026 2027 switch (method) 2028 { 2029 case BLOCK_OP_NORMAL: 2030 case BLOCK_OP_TAILCALL: 2031 may_use_call = 1; 2032 break; 2033 2034 case BLOCK_OP_CALL_PARM: 2035 may_use_call = block_move_libcall_safe_for_call_parm (); 2036 2037 /* Make inhibit_defer_pop nonzero around the library call 2038 to force it to pop the arguments right away. */ 2039 NO_DEFER_POP; 2040 break; 2041 2042 case BLOCK_OP_NO_LIBCALL: 2043 may_use_call = 0; 2044 break; 2045 2046 case BLOCK_OP_NO_LIBCALL_RET: 2047 may_use_call = -1; 2048 break; 2049 2050 default: 2051 gcc_unreachable (); 2052 } 2053 2054 gcc_assert (MEM_P (x) && MEM_P (y)); 2055 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y)); 2056 gcc_assert (align >= BITS_PER_UNIT); 2057 2058 /* Make sure we've got BLKmode addresses; store_one_arg can decide that 2059 block copy is more efficient for other large modes, e.g. DCmode. */ 2060 x = adjust_address (x, BLKmode, 0); 2061 y = adjust_address (y, BLKmode, 0); 2062 2063 /* If source and destination are the same, no need to copy anything. */ 2064 if (rtx_equal_p (x, y) 2065 && !MEM_VOLATILE_P (x) 2066 && !MEM_VOLATILE_P (y)) 2067 return 0; 2068 2069 /* Set MEM_SIZE as appropriate for this block copy. The main place this 2070 can be incorrect is coming from __builtin_memcpy. */ 2071 poly_int64 const_size; 2072 if (poly_int_rtx_p (size, &const_size)) 2073 { 2074 x = shallow_copy_rtx (x); 2075 y = shallow_copy_rtx (y); 2076 set_mem_size (x, const_size); 2077 set_mem_size (y, const_size); 2078 } 2079 2080 bool pieces_ok = CONST_INT_P (size) 2081 && can_move_by_pieces (INTVAL (size), align); 2082 bool pattern_ok = false; 2083 2084 if (!pieces_ok || might_overlap) 2085 { 2086 pattern_ok 2087 = emit_block_move_via_pattern (x, y, size, align, 2088 expected_align, expected_size, 2089 min_size, max_size, probable_max_size, 2090 might_overlap); 2091 if (!pattern_ok && might_overlap) 2092 { 2093 /* Do not try any of the other methods below as they are not safe 2094 for overlapping moves. */ 2095 *is_move_done = false; 2096 return retval; 2097 } 2098 } 2099 2100 bool dynamic_direction = false; 2101 if (!pattern_ok && !pieces_ok && may_use_call 2102 && (flag_inline_stringops & (might_overlap ? ILSOP_MEMMOVE : ILSOP_MEMCPY))) 2103 { 2104 may_use_call = 0; 2105 dynamic_direction = might_overlap; 2106 } 2107 2108 if (pattern_ok) 2109 ; 2110 else if (pieces_ok) 2111 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN); 2112 else if (may_use_call && !might_overlap 2113 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x)) 2114 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y))) 2115 { 2116 if (bail_out_libcall) 2117 { 2118 if (is_move_done) 2119 *is_move_done = false; 2120 return retval; 2121 } 2122 2123 if (may_use_call < 0) 2124 return pc_rtx; 2125 2126 retval = emit_block_copy_via_libcall (x, y, size, 2127 method == BLOCK_OP_TAILCALL); 2128 } 2129 else if (dynamic_direction) 2130 emit_block_move_via_oriented_loop (x, y, size, align, ctz_size); 2131 else if (might_overlap) 2132 *is_move_done = false; 2133 else 2134 emit_block_move_via_sized_loop (x, y, size, align, ctz_size); 2135 2136 if (method == BLOCK_OP_CALL_PARM) 2137 OK_DEFER_POP; 2138 2139 return retval; 2140 } 2141 2142 rtx 2143 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method, 2144 unsigned int ctz_size) 2145 { 2146 unsigned HOST_WIDE_INT max, min = 0; 2147 if (GET_CODE (size) == CONST_INT) 2148 min = max = UINTVAL (size); 2149 else 2150 max = GET_MODE_MASK (GET_MODE (size)); 2151 return emit_block_move_hints (x, y, size, method, 0, -1, 2152 min, max, max, 2153 false, NULL, false, ctz_size); 2154 } 2155 2156 /* A subroutine of emit_block_move. Returns true if calling the 2157 block move libcall will not clobber any parameters which may have 2158 already been placed on the stack. */ 2159 2160 static bool 2161 block_move_libcall_safe_for_call_parm (void) 2162 { 2163 tree fn; 2164 2165 /* If arguments are pushed on the stack, then they're safe. */ 2166 if (targetm.calls.push_argument (0)) 2167 return true; 2168 2169 /* If registers go on the stack anyway, any argument is sure to clobber 2170 an outgoing argument. */ 2171 #if defined (REG_PARM_STACK_SPACE) 2172 fn = builtin_decl_implicit (BUILT_IN_MEMCPY); 2173 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't 2174 depend on its argument. */ 2175 (void) fn; 2176 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn))) 2177 && REG_PARM_STACK_SPACE (fn) != 0) 2178 return false; 2179 #endif 2180 2181 /* If any argument goes in memory, then it might clobber an outgoing 2182 argument. */ 2183 { 2184 CUMULATIVE_ARGS args_so_far_v; 2185 cumulative_args_t args_so_far; 2186 tree arg; 2187 2188 fn = builtin_decl_implicit (BUILT_IN_MEMCPY); 2189 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3); 2190 args_so_far = pack_cumulative_args (&args_so_far_v); 2191 2192 arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); 2193 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg)) 2194 { 2195 machine_mode mode = TYPE_MODE (TREE_VALUE (arg)); 2196 function_arg_info arg_info (mode, /*named=*/true); 2197 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info); 2198 if (!tmp || !REG_P (tmp)) 2199 return false; 2200 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info)) 2201 return false; 2202 targetm.calls.function_arg_advance (args_so_far, arg_info); 2203 } 2204 } 2205 return true; 2206 } 2207 2208 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern; 2209 return true if successful. 2210 2211 X is the destination of the copy or move. 2212 Y is the source of the copy or move. 2213 SIZE is the size of the block to be moved. 2214 2215 MIGHT_OVERLAP indicates this originated with expansion of a 2216 builtin_memmove() and the source and destination blocks may 2217 overlap. 2218 */ 2219 2220 static bool 2221 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align, 2222 unsigned int expected_align, 2223 HOST_WIDE_INT expected_size, 2224 unsigned HOST_WIDE_INT min_size, 2225 unsigned HOST_WIDE_INT max_size, 2226 unsigned HOST_WIDE_INT probable_max_size, 2227 bool might_overlap) 2228 { 2229 if (expected_align < align) 2230 expected_align = align; 2231 if (expected_size != -1) 2232 { 2233 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size) 2234 expected_size = probable_max_size; 2235 if ((unsigned HOST_WIDE_INT)expected_size < min_size) 2236 expected_size = min_size; 2237 } 2238 2239 /* Since this is a move insn, we don't care about volatility. */ 2240 temporary_volatile_ok v (true); 2241 2242 /* Try the most limited insn first, because there's no point 2243 including more than one in the machine description unless 2244 the more limited one has some advantage. */ 2245 2246 opt_scalar_int_mode mode_iter; 2247 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT) 2248 { 2249 scalar_int_mode mode = mode_iter.require (); 2250 enum insn_code code; 2251 if (might_overlap) 2252 code = direct_optab_handler (movmem_optab, mode); 2253 else 2254 code = direct_optab_handler (cpymem_optab, mode); 2255 2256 if (code != CODE_FOR_nothing 2257 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT 2258 here because if SIZE is less than the mode mask, as it is 2259 returned by the macro, it will definitely be less than the 2260 actual mode mask. Since SIZE is within the Pmode address 2261 space, we limit MODE to Pmode. */ 2262 && ((CONST_INT_P (size) 2263 && ((unsigned HOST_WIDE_INT) INTVAL (size) 2264 <= (GET_MODE_MASK (mode) >> 1))) 2265 || max_size <= (GET_MODE_MASK (mode) >> 1) 2266 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode))) 2267 { 2268 class expand_operand ops[9]; 2269 unsigned int nops; 2270 2271 /* ??? When called via emit_block_move_for_call, it'd be 2272 nice if there were some way to inform the backend, so 2273 that it doesn't fail the expansion because it thinks 2274 emitting the libcall would be more efficient. */ 2275 nops = insn_data[(int) code].n_generator_args; 2276 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9); 2277 2278 create_fixed_operand (&ops[0], x); 2279 create_fixed_operand (&ops[1], y); 2280 /* The check above guarantees that this size conversion is valid. */ 2281 create_convert_operand_to (&ops[2], size, mode, true); 2282 create_integer_operand (&ops[3], align / BITS_PER_UNIT); 2283 if (nops >= 6) 2284 { 2285 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT); 2286 create_integer_operand (&ops[5], expected_size); 2287 } 2288 if (nops >= 8) 2289 { 2290 create_integer_operand (&ops[6], min_size); 2291 /* If we cannot represent the maximal size, 2292 make parameter NULL. */ 2293 if ((HOST_WIDE_INT) max_size != -1) 2294 create_integer_operand (&ops[7], max_size); 2295 else 2296 create_fixed_operand (&ops[7], NULL); 2297 } 2298 if (nops == 9) 2299 { 2300 /* If we cannot represent the maximal size, 2301 make parameter NULL. */ 2302 if ((HOST_WIDE_INT) probable_max_size != -1) 2303 create_integer_operand (&ops[8], probable_max_size); 2304 else 2305 create_fixed_operand (&ops[8], NULL); 2306 } 2307 if (maybe_expand_insn (code, nops, ops)) 2308 return true; 2309 } 2310 } 2311 2312 return false; 2313 } 2314 2315 /* Like emit_block_move_via_loop, but choose a suitable INCR based on 2316 ALIGN and CTZ_SIZE. */ 2317 2318 static void 2319 emit_block_move_via_sized_loop (rtx x, rtx y, rtx size, 2320 unsigned int align, 2321 unsigned int ctz_size) 2322 { 2323 int incr = align / BITS_PER_UNIT; 2324 2325 if (CONST_INT_P (size)) 2326 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size))); 2327 2328 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr) 2329 incr = HOST_WIDE_INT_1U << ctz_size; 2330 2331 while (incr > 1 && !can_move_by_pieces (incr, align)) 2332 incr >>= 1; 2333 2334 gcc_checking_assert (incr); 2335 2336 return emit_block_move_via_loop (x, y, size, align, incr); 2337 } 2338 2339 /* Like emit_block_move_via_sized_loop, but besides choosing INCR so 2340 as to ensure safe moves even in case of overlap, output dynamic 2341 tests to choose between two loops, one moving downwards, another 2342 moving upwards. */ 2343 2344 static void 2345 emit_block_move_via_oriented_loop (rtx x, rtx y, rtx size, 2346 unsigned int align, 2347 unsigned int ctz_size) 2348 { 2349 int incr = align / BITS_PER_UNIT; 2350 2351 if (CONST_INT_P (size)) 2352 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size))); 2353 2354 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr) 2355 incr = HOST_WIDE_INT_1U << ctz_size; 2356 2357 while (incr > 1 && !int_mode_for_size (incr, 0).exists ()) 2358 incr >>= 1; 2359 2360 gcc_checking_assert (incr); 2361 2362 rtx_code_label *upw_label, *end_label; 2363 upw_label = gen_label_rtx (); 2364 end_label = gen_label_rtx (); 2365 2366 rtx x_addr = force_operand (XEXP (x, 0), NULL_RTX); 2367 rtx y_addr = force_operand (XEXP (y, 0), NULL_RTX); 2368 do_pending_stack_adjust (); 2369 2370 machine_mode mode = GET_MODE (x_addr); 2371 if (mode != GET_MODE (y_addr)) 2372 { 2373 scalar_int_mode xmode 2374 = smallest_int_mode_for_size (GET_MODE_BITSIZE (mode)); 2375 scalar_int_mode ymode 2376 = smallest_int_mode_for_size (GET_MODE_BITSIZE 2377 (GET_MODE (y_addr))); 2378 if (GET_MODE_BITSIZE (xmode) < GET_MODE_BITSIZE (ymode)) 2379 mode = ymode; 2380 else 2381 mode = xmode; 2382 2383 #ifndef POINTERS_EXTEND_UNSIGNED 2384 const int POINTERS_EXTEND_UNSIGNED = 1; 2385 #endif 2386 x_addr = convert_modes (mode, GET_MODE (x_addr), x_addr, 2387 POINTERS_EXTEND_UNSIGNED); 2388 y_addr = convert_modes (mode, GET_MODE (y_addr), y_addr, 2389 POINTERS_EXTEND_UNSIGNED); 2390 } 2391 2392 /* Test for overlap: if (x >= y || x + size <= y) goto upw_label. */ 2393 emit_cmp_and_jump_insns (x_addr, y_addr, GEU, NULL_RTX, mode, 2394 true, upw_label, 2395 profile_probability::guessed_always () 2396 .apply_scale (5, 10)); 2397 rtx tmp = convert_modes (GET_MODE (x_addr), GET_MODE (size), size, true); 2398 tmp = simplify_gen_binary (PLUS, GET_MODE (x_addr), x_addr, tmp); 2399 2400 emit_cmp_and_jump_insns (tmp, y_addr, LEU, NULL_RTX, mode, 2401 true, upw_label, 2402 profile_probability::guessed_always () 2403 .apply_scale (8, 10)); 2404 2405 emit_block_move_via_loop (x, y, size, align, -incr); 2406 2407 emit_jump (end_label); 2408 emit_label (upw_label); 2409 2410 emit_block_move_via_loop (x, y, size, align, incr); 2411 2412 emit_label (end_label); 2413 } 2414 2415 /* A subroutine of emit_block_move. Copy the data via an explicit 2416 loop. This is used only when libcalls are forbidden, or when 2417 inlining is required. INCR is the block size to be copied in each 2418 loop iteration. If it is negative, the absolute value is used, and 2419 the block is copied backwards. INCR must be a power of two, an 2420 exact divisor for SIZE and ALIGN, and imply a mode that can be 2421 safely copied per iteration assuming no overlap. */ 2422 2423 static void 2424 emit_block_move_via_loop (rtx x, rtx y, rtx size, 2425 unsigned int align, int incr) 2426 { 2427 rtx_code_label *cmp_label, *top_label; 2428 rtx iter, x_addr, y_addr, tmp; 2429 machine_mode x_addr_mode = get_address_mode (x); 2430 machine_mode y_addr_mode = get_address_mode (y); 2431 machine_mode iter_mode; 2432 2433 iter_mode = GET_MODE (size); 2434 if (iter_mode == VOIDmode) 2435 iter_mode = word_mode; 2436 2437 top_label = gen_label_rtx (); 2438 cmp_label = gen_label_rtx (); 2439 iter = gen_reg_rtx (iter_mode); 2440 2441 bool downwards = incr < 0; 2442 rtx iter_init; 2443 rtx_code iter_cond; 2444 rtx iter_limit; 2445 rtx iter_incr; 2446 machine_mode move_mode; 2447 if (downwards) 2448 { 2449 incr = -incr; 2450 iter_init = size; 2451 iter_cond = GEU; 2452 iter_limit = const0_rtx; 2453 iter_incr = GEN_INT (incr); 2454 } 2455 else 2456 { 2457 iter_init = const0_rtx; 2458 iter_cond = LTU; 2459 iter_limit = size; 2460 iter_incr = GEN_INT (incr); 2461 } 2462 emit_move_insn (iter, iter_init); 2463 2464 opt_scalar_int_mode int_move_mode 2465 = int_mode_for_size (incr * BITS_PER_UNIT, 1); 2466 if (!int_move_mode.exists (&move_mode) 2467 || GET_MODE_BITSIZE (int_move_mode.require ()) != incr * BITS_PER_UNIT) 2468 { 2469 move_mode = BLKmode; 2470 gcc_checking_assert (can_move_by_pieces (incr, align)); 2471 } 2472 2473 x_addr = force_operand (XEXP (x, 0), NULL_RTX); 2474 y_addr = force_operand (XEXP (y, 0), NULL_RTX); 2475 do_pending_stack_adjust (); 2476 2477 emit_jump (cmp_label); 2478 emit_label (top_label); 2479 2480 tmp = convert_modes (x_addr_mode, iter_mode, iter, true); 2481 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp); 2482 2483 if (x_addr_mode != y_addr_mode) 2484 tmp = convert_modes (y_addr_mode, iter_mode, iter, true); 2485 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp); 2486 2487 x = change_address (x, move_mode, x_addr); 2488 y = change_address (y, move_mode, y_addr); 2489 2490 if (move_mode == BLKmode) 2491 { 2492 bool done; 2493 emit_block_move_hints (x, y, iter_incr, BLOCK_OP_NO_LIBCALL, 2494 align, incr, incr, incr, incr, 2495 false, &done, false); 2496 gcc_checking_assert (done); 2497 } 2498 else 2499 emit_move_insn (x, y); 2500 2501 if (downwards) 2502 emit_label (cmp_label); 2503 2504 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter, 2505 true, OPTAB_LIB_WIDEN); 2506 if (tmp != iter) 2507 emit_move_insn (iter, tmp); 2508 2509 if (!downwards) 2510 emit_label (cmp_label); 2511 2512 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode, 2513 true, top_label, 2514 profile_probability::guessed_always () 2515 .apply_scale (9, 10)); 2516 } 2517 2518 /* Expand a call to memcpy or memmove or memcmp, and return the result. 2520 TAILCALL is true if this is a tail call. */ 2521 2522 rtx 2523 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src, 2524 rtx size, bool tailcall) 2525 { 2526 rtx dst_addr, src_addr; 2527 tree call_expr, dst_tree, src_tree, size_tree; 2528 machine_mode size_mode; 2529 2530 /* Since dst and src are passed to a libcall, mark the corresponding 2531 tree EXPR as addressable. */ 2532 tree dst_expr = MEM_EXPR (dst); 2533 tree src_expr = MEM_EXPR (src); 2534 if (dst_expr) 2535 mark_addressable (dst_expr); 2536 if (src_expr) 2537 mark_addressable (src_expr); 2538 2539 dst_addr = copy_addr_to_reg (XEXP (dst, 0)); 2540 dst_addr = convert_memory_address (ptr_mode, dst_addr); 2541 dst_tree = make_tree (ptr_type_node, dst_addr); 2542 2543 src_addr = copy_addr_to_reg (XEXP (src, 0)); 2544 src_addr = convert_memory_address (ptr_mode, src_addr); 2545 src_tree = make_tree (ptr_type_node, src_addr); 2546 2547 size_mode = TYPE_MODE (sizetype); 2548 size = convert_to_mode (size_mode, size, 1); 2549 size = copy_to_mode_reg (size_mode, size); 2550 size_tree = make_tree (sizetype, size); 2551 2552 /* It is incorrect to use the libcall calling conventions for calls to 2553 memcpy/memmove/memcmp because they can be provided by the user. */ 2554 tree fn = builtin_decl_implicit (fncode); 2555 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree); 2556 CALL_EXPR_TAILCALL (call_expr) = tailcall; 2557 2558 return expand_call (call_expr, NULL_RTX, false); 2559 } 2560 2561 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands. 2562 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success, 2563 otherwise return null. */ 2564 2565 rtx 2566 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, 2567 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx, 2568 HOST_WIDE_INT align) 2569 { 2570 machine_mode insn_mode = insn_data[icode].operand[0].mode; 2571 2572 if (target && (!REG_P (target) || HARD_REGISTER_P (target))) 2573 target = NULL_RTX; 2574 2575 class expand_operand ops[5]; 2576 create_output_operand (&ops[0], target, insn_mode); 2577 create_fixed_operand (&ops[1], arg1_rtx); 2578 create_fixed_operand (&ops[2], arg2_rtx); 2579 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type), 2580 TYPE_UNSIGNED (arg3_type)); 2581 create_integer_operand (&ops[4], align); 2582 if (maybe_expand_insn (icode, 5, ops)) 2583 return ops[0].value; 2584 return NULL_RTX; 2585 } 2586 2587 /* Expand a block compare between X and Y with length LEN using the 2588 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type 2589 of the expression that was used to calculate the length. ALIGN 2590 gives the known minimum common alignment. */ 2591 2592 static rtx 2593 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target, 2594 unsigned align) 2595 { 2596 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for 2597 implementing memcmp because it will stop if it encounters two 2598 zero bytes. */ 2599 insn_code icode = direct_optab_handler (cmpmem_optab, SImode); 2600 2601 if (icode == CODE_FOR_nothing) 2602 return NULL_RTX; 2603 2604 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align); 2605 } 2606 2607 /* Emit code to compare a block Y to a block X. This may be done with 2608 string-compare instructions, with multiple scalar instructions, 2609 or with a library call. 2610 2611 Both X and Y must be MEM rtx's. LEN is an rtx that says how long 2612 they are. LEN_TYPE is the type of the expression that was used to 2613 calculate it, and CTZ_LEN is the known trailing-zeros count of LEN, 2614 so LEN must be a multiple of 1<<CTZ_LEN even if it's not constant. 2615 2616 If EQUALITY_ONLY is true, it means we don't have to return the tri-state 2617 value of a normal memcmp call, instead we can just compare for equality. 2618 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than 2619 returning NULL_RTX. 2620 2621 Optionally, the caller can pass a constfn and associated data in Y_CFN 2622 and Y_CFN_DATA. describing that the second operand being compared is a 2623 known constant and how to obtain its data. 2624 Return the result of the comparison, or NULL_RTX if we failed to 2625 perform the operation. */ 2626 2627 rtx 2628 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target, 2629 bool equality_only, by_pieces_constfn y_cfn, 2630 void *y_cfndata, unsigned ctz_len) 2631 { 2632 rtx result = 0; 2633 2634 if (CONST_INT_P (len) && INTVAL (len) == 0) 2635 return const0_rtx; 2636 2637 gcc_assert (MEM_P (x) && MEM_P (y)); 2638 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y)); 2639 gcc_assert (align >= BITS_PER_UNIT); 2640 2641 x = adjust_address (x, BLKmode, 0); 2642 y = adjust_address (y, BLKmode, 0); 2643 2644 if (equality_only 2645 && CONST_INT_P (len) 2646 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES)) 2647 result = compare_by_pieces (x, y, INTVAL (len), target, align, 2648 y_cfn, y_cfndata); 2649 else 2650 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align); 2651 2652 if (!result && (flag_inline_stringops & ILSOP_MEMCMP)) 2653 result = emit_block_cmp_via_loop (x, y, len, len_type, 2654 target, equality_only, 2655 align, ctz_len); 2656 2657 return result; 2658 } 2659 2660 /* Like emit_block_cmp_hints, but with known alignment and no support 2661 for constats. Always expand to a loop with iterations that compare 2662 blocks of the largest compare-by-pieces size that divides both len 2663 and align, and then, if !EQUALITY_ONLY, identify the word and then 2664 the unit that first differs to return the result. */ 2665 2666 rtx 2667 emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target, 2668 bool equality_only, unsigned align, unsigned ctz_len) 2669 { 2670 unsigned incr = align / BITS_PER_UNIT; 2671 2672 if (CONST_INT_P (len)) 2673 ctz_len = MAX (ctz_len, (unsigned) wi::ctz (UINTVAL (len))); 2674 2675 if (HOST_WIDE_INT_1U << ctz_len < (unsigned HOST_WIDE_INT) incr) 2676 incr = HOST_WIDE_INT_1U << ctz_len; 2677 2678 while (incr > 1 2679 && !can_do_by_pieces (incr, align, COMPARE_BY_PIECES)) 2680 incr >>= 1; 2681 2682 rtx_code_label *cmp_label, *top_label, *ne_label, *res_label; 2683 rtx iter, x_addr, y_addr, tmp; 2684 machine_mode x_addr_mode = get_address_mode (x); 2685 machine_mode y_addr_mode = get_address_mode (y); 2686 machine_mode iter_mode; 2687 2688 iter_mode = GET_MODE (len); 2689 if (iter_mode == VOIDmode) 2690 iter_mode = word_mode; 2691 2692 rtx iter_init = const0_rtx; 2693 rtx_code iter_cond = LTU; 2694 rtx_code entry_cond = GEU; 2695 rtx iter_limit = len; 2696 rtx iter_incr = GEN_INT (incr); 2697 machine_mode cmp_mode; 2698 2699 /* We can drop the loop back edge if we know there's exactly one 2700 iteration. */ 2701 top_label = (!rtx_equal_p (len, iter_incr) 2702 ? gen_label_rtx () 2703 : NULL); 2704 /* We need not test before entering the loop if len is known 2705 nonzero. ??? This could be even stricter, testing whether a 2706 nonconstant LEN could possibly be zero. */ 2707 cmp_label = (!CONSTANT_P (len) || rtx_equal_p (len, iter_init) 2708 ? gen_label_rtx () 2709 : NULL); 2710 ne_label = gen_label_rtx (); 2711 res_label = gen_label_rtx (); 2712 2713 iter = gen_reg_rtx (iter_mode); 2714 emit_move_insn (iter, iter_init); 2715 2716 opt_scalar_int_mode int_cmp_mode 2717 = int_mode_for_size (incr * BITS_PER_UNIT, 1); 2718 if (!int_cmp_mode.exists (&cmp_mode) 2719 || GET_MODE_BITSIZE (int_cmp_mode.require ()) != incr * BITS_PER_UNIT 2720 || !can_compare_p (NE, cmp_mode, ccp_jump)) 2721 { 2722 cmp_mode = BLKmode; 2723 gcc_checking_assert (incr != 1); 2724 } 2725 2726 /* Save the base addresses. */ 2727 x_addr = force_operand (XEXP (x, 0), NULL_RTX); 2728 y_addr = force_operand (XEXP (y, 0), NULL_RTX); 2729 do_pending_stack_adjust (); 2730 2731 if (cmp_label) 2732 { 2733 if (top_label) 2734 emit_jump (cmp_label); 2735 else 2736 emit_cmp_and_jump_insns (iter, iter_limit, entry_cond, 2737 NULL_RTX, iter_mode, 2738 true, cmp_label, 2739 profile_probability::guessed_always () 2740 .apply_scale (1, 10)); 2741 } 2742 if (top_label) 2743 emit_label (top_label); 2744 2745 /* Offset the base addresses by ITER. */ 2746 tmp = convert_modes (x_addr_mode, iter_mode, iter, true); 2747 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp); 2748 2749 if (x_addr_mode != y_addr_mode) 2750 tmp = convert_modes (y_addr_mode, iter_mode, iter, true); 2751 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp); 2752 2753 x = change_address (x, cmp_mode, x_addr); 2754 y = change_address (y, cmp_mode, y_addr); 2755 2756 /* Compare one block. */ 2757 rtx part_res; 2758 if (cmp_mode == BLKmode) 2759 part_res = compare_by_pieces (x, y, incr, target, align, 0, 0); 2760 else 2761 part_res = expand_binop (cmp_mode, sub_optab, x, y, NULL_RTX, 2762 true, OPTAB_LIB_WIDEN); 2763 2764 /* Stop if we found a difference. */ 2765 emit_cmp_and_jump_insns (part_res, GEN_INT (0), NE, NULL_RTX, 2766 GET_MODE (part_res), true, ne_label, 2767 profile_probability::guessed_always () 2768 .apply_scale (1, 10)); 2769 2770 /* Increment ITER. */ 2771 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter, 2772 true, OPTAB_LIB_WIDEN); 2773 if (tmp != iter) 2774 emit_move_insn (iter, tmp); 2775 2776 if (cmp_label) 2777 emit_label (cmp_label); 2778 /* Loop until we reach the limit. */ 2779 2780 if (top_label) 2781 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode, 2782 true, top_label, 2783 profile_probability::guessed_always () 2784 .apply_scale (9, 10)); 2785 2786 /* We got to the end without differences, so the result is zero. */ 2787 if (target == NULL_RTX 2788 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER) 2789 target = gen_reg_rtx (TYPE_MODE (integer_type_node)); 2790 2791 emit_move_insn (target, const0_rtx); 2792 emit_jump (res_label); 2793 2794 emit_label (ne_label); 2795 2796 /* Return nonzero, or pinpoint the difference to return the expected 2797 result for non-equality tests. */ 2798 if (equality_only) 2799 emit_move_insn (target, const1_rtx); 2800 else 2801 { 2802 if (incr > UNITS_PER_WORD) 2803 /* ??? Re-compare the block found to be different one word at a 2804 time. */ 2805 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type, 2806 target, equality_only, 2807 BITS_PER_WORD, 0); 2808 else if (incr > 1) 2809 /* ??? Re-compare the block found to be different one byte at a 2810 time. We could do better using part_res, and being careful 2811 about endianness. */ 2812 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type, 2813 target, equality_only, 2814 BITS_PER_UNIT, 0); 2815 else if (known_gt (GET_MODE_BITSIZE (GET_MODE (target)), 2816 GET_MODE_BITSIZE (cmp_mode))) 2817 part_res = expand_binop (GET_MODE (target), sub_optab, x, y, target, 2818 true, OPTAB_LIB_WIDEN); 2819 else 2820 { 2821 /* In the odd chance target is QImode, we can't count on 2822 widening subtract to capture the result of the unsigned 2823 compares. */ 2824 rtx_code_label *ltu_label; 2825 ltu_label = gen_label_rtx (); 2826 emit_cmp_and_jump_insns (x, y, LTU, NULL_RTX, 2827 cmp_mode, true, ltu_label, 2828 profile_probability::guessed_always () 2829 .apply_scale (5, 10)); 2830 2831 emit_move_insn (target, const1_rtx); 2832 emit_jump (res_label); 2833 2834 emit_label (ltu_label); 2835 emit_move_insn (target, constm1_rtx); 2836 part_res = target; 2837 } 2838 2839 if (target != part_res) 2840 convert_move (target, part_res, false); 2841 } 2842 2843 emit_label (res_label); 2844 2845 return target; 2846 } 2847 2848 2849 /* Copy all or part of a value X into registers starting at REGNO. 2851 The number of registers to be filled is NREGS. */ 2852 2853 void 2854 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode) 2855 { 2856 if (nregs == 0) 2857 return; 2858 2859 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x)) 2860 x = validize_mem (force_const_mem (mode, x)); 2861 2862 /* See if the machine can do this with a load multiple insn. */ 2863 if (targetm.have_load_multiple ()) 2864 { 2865 rtx_insn *last = get_last_insn (); 2866 rtx first = gen_rtx_REG (word_mode, regno); 2867 if (rtx_insn *pat = targetm.gen_load_multiple (first, x, 2868 GEN_INT (nregs))) 2869 { 2870 emit_insn (pat); 2871 return; 2872 } 2873 else 2874 delete_insns_since (last); 2875 } 2876 2877 for (int i = 0; i < nregs; i++) 2878 emit_move_insn (gen_rtx_REG (word_mode, regno + i), 2879 operand_subword_force (x, i, mode)); 2880 } 2881 2882 /* Copy all or part of a BLKmode value X out of registers starting at REGNO. 2883 The number of registers to be filled is NREGS. */ 2884 2885 void 2886 move_block_from_reg (int regno, rtx x, int nregs) 2887 { 2888 if (nregs == 0) 2889 return; 2890 2891 /* See if the machine can do this with a store multiple insn. */ 2892 if (targetm.have_store_multiple ()) 2893 { 2894 rtx_insn *last = get_last_insn (); 2895 rtx first = gen_rtx_REG (word_mode, regno); 2896 if (rtx_insn *pat = targetm.gen_store_multiple (x, first, 2897 GEN_INT (nregs))) 2898 { 2899 emit_insn (pat); 2900 return; 2901 } 2902 else 2903 delete_insns_since (last); 2904 } 2905 2906 for (int i = 0; i < nregs; i++) 2907 { 2908 rtx tem = operand_subword (x, i, 1, BLKmode); 2909 2910 gcc_assert (tem); 2911 2912 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i)); 2913 } 2914 } 2915 2916 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from 2917 ORIG, where ORIG is a non-consecutive group of registers represented by 2918 a PARALLEL. The clone is identical to the original except in that the 2919 original set of registers is replaced by a new set of pseudo registers. 2920 The new set has the same modes as the original set. */ 2921 2922 rtx 2923 gen_group_rtx (rtx orig) 2924 { 2925 int i, length; 2926 rtx *tmps; 2927 2928 gcc_assert (GET_CODE (orig) == PARALLEL); 2929 2930 length = XVECLEN (orig, 0); 2931 tmps = XALLOCAVEC (rtx, length); 2932 2933 /* Skip a NULL entry in first slot. */ 2934 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1; 2935 2936 if (i) 2937 tmps[0] = 0; 2938 2939 for (; i < length; i++) 2940 { 2941 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0)); 2942 rtx offset = XEXP (XVECEXP (orig, 0, i), 1); 2943 2944 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset); 2945 } 2946 2947 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps)); 2948 } 2949 2950 /* A subroutine of emit_group_load. Arguments as for emit_group_load, 2951 except that values are placed in TMPS[i], and must later be moved 2952 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */ 2953 2954 static void 2955 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, 2956 poly_int64 ssize) 2957 { 2958 rtx src; 2959 int start, i; 2960 machine_mode m = GET_MODE (orig_src); 2961 2962 gcc_assert (GET_CODE (dst) == PARALLEL); 2963 2964 if (m != VOIDmode 2965 && !SCALAR_INT_MODE_P (m) 2966 && !MEM_P (orig_src) 2967 && GET_CODE (orig_src) != CONCAT) 2968 { 2969 scalar_int_mode imode; 2970 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode)) 2971 { 2972 src = gen_reg_rtx (imode); 2973 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src); 2974 } 2975 else 2976 { 2977 src = assign_stack_temp (GET_MODE (orig_src), ssize); 2978 emit_move_insn (src, orig_src); 2979 } 2980 emit_group_load_1 (tmps, dst, src, type, ssize); 2981 return; 2982 } 2983 2984 /* Check for a NULL entry, used to indicate that the parameter goes 2985 both on the stack and in registers. */ 2986 if (XEXP (XVECEXP (dst, 0, 0), 0)) 2987 start = 0; 2988 else 2989 start = 1; 2990 2991 /* Process the pieces. */ 2992 for (i = start; i < XVECLEN (dst, 0); i++) 2993 { 2994 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0)); 2995 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1)); 2996 poly_int64 bytelen = GET_MODE_SIZE (mode); 2997 poly_int64 shift = 0; 2998 2999 /* Handle trailing fragments that run over the size of the struct. 3000 It's the target's responsibility to make sure that the fragment 3001 cannot be strictly smaller in some cases and strictly larger 3002 in others. */ 3003 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize)); 3004 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize)) 3005 { 3006 /* Arrange to shift the fragment to where it belongs. 3007 extract_bit_field loads to the lsb of the reg. */ 3008 if ( 3009 #ifdef BLOCK_REG_PADDING 3010 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start) 3011 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD) 3012 #else 3013 BYTES_BIG_ENDIAN 3014 #endif 3015 ) 3016 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT; 3017 bytelen = ssize - bytepos; 3018 gcc_assert (maybe_gt (bytelen, 0)); 3019 } 3020 3021 /* If we won't be loading directly from memory, protect the real source 3022 from strange tricks we might play; but make sure that the source can 3023 be loaded directly into the destination. */ 3024 src = orig_src; 3025 if (!MEM_P (orig_src) 3026 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src)) 3027 && !CONSTANT_P (orig_src)) 3028 { 3029 gcc_assert (GET_MODE (orig_src) != VOIDmode); 3030 src = force_reg (GET_MODE (orig_src), orig_src); 3031 } 3032 3033 /* Optimize the access just a bit. */ 3034 if (MEM_P (src) 3035 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src)) 3036 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode)) 3037 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode)) 3038 && known_eq (bytelen, GET_MODE_SIZE (mode))) 3039 { 3040 tmps[i] = gen_reg_rtx (mode); 3041 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos)); 3042 } 3043 else if (COMPLEX_MODE_P (mode) 3044 && GET_MODE (src) == mode 3045 && known_eq (bytelen, GET_MODE_SIZE (mode))) 3046 /* Let emit_move_complex do the bulk of the work. */ 3047 tmps[i] = src; 3048 else if (GET_CODE (src) == CONCAT) 3049 { 3050 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src)); 3051 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0))); 3052 unsigned int elt; 3053 poly_int64 subpos; 3054 3055 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos) 3056 && known_le (subpos + bytelen, slen0)) 3057 { 3058 /* The following assumes that the concatenated objects all 3059 have the same size. In this case, a simple calculation 3060 can be used to determine the object and the bit field 3061 to be extracted. */ 3062 tmps[i] = XEXP (src, elt); 3063 if (maybe_ne (subpos, 0) 3064 || maybe_ne (subpos + bytelen, slen0) 3065 || (!CONSTANT_P (tmps[i]) 3066 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))) 3067 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT, 3068 subpos * BITS_PER_UNIT, 3069 1, NULL_RTX, mode, mode, false, 3070 NULL); 3071 } 3072 else 3073 { 3074 rtx mem; 3075 3076 gcc_assert (known_eq (bytepos, 0)); 3077 mem = assign_stack_temp (GET_MODE (src), slen); 3078 emit_move_insn (mem, src); 3079 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT, 3080 0, 1, NULL_RTX, mode, mode, false, 3081 NULL); 3082 } 3083 } 3084 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode 3085 && XVECLEN (dst, 0) > 1) 3086 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos); 3087 else if (CONSTANT_P (src)) 3088 { 3089 if (known_eq (bytelen, ssize)) 3090 tmps[i] = src; 3091 else 3092 { 3093 rtx first, second; 3094 3095 /* TODO: const_wide_int can have sizes other than this... */ 3096 gcc_assert (known_eq (2 * bytelen, ssize)); 3097 split_double (src, &first, &second); 3098 if (i) 3099 tmps[i] = second; 3100 else 3101 tmps[i] = first; 3102 } 3103 } 3104 else if (REG_P (src) && GET_MODE (src) == mode) 3105 tmps[i] = src; 3106 else 3107 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT, 3108 bytepos * BITS_PER_UNIT, 1, NULL_RTX, 3109 mode, mode, false, NULL); 3110 3111 if (maybe_ne (shift, 0)) 3112 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i], 3113 shift, tmps[i], 0); 3114 } 3115 } 3116 3117 /* Emit code to move a block SRC of type TYPE to a block DST, 3118 where DST is non-consecutive registers represented by a PARALLEL. 3119 SSIZE represents the total size of block ORIG_SRC in bytes, or -1 3120 if not known. */ 3121 3122 void 3123 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize) 3124 { 3125 rtx *tmps; 3126 int i; 3127 3128 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0)); 3129 emit_group_load_1 (tmps, dst, src, type, ssize); 3130 3131 /* Copy the extracted pieces into the proper (probable) hard regs. */ 3132 for (i = 0; i < XVECLEN (dst, 0); i++) 3133 { 3134 rtx d = XEXP (XVECEXP (dst, 0, i), 0); 3135 if (d == NULL) 3136 continue; 3137 emit_move_insn (d, tmps[i]); 3138 } 3139 } 3140 3141 /* Similar, but load SRC into new pseudos in a format that looks like 3142 PARALLEL. This can later be fed to emit_group_move to get things 3143 in the right place. */ 3144 3145 rtx 3146 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize) 3147 { 3148 rtvec vec; 3149 int i; 3150 3151 vec = rtvec_alloc (XVECLEN (parallel, 0)); 3152 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize); 3153 3154 /* Convert the vector to look just like the original PARALLEL, except 3155 with the computed values. */ 3156 for (i = 0; i < XVECLEN (parallel, 0); i++) 3157 { 3158 rtx e = XVECEXP (parallel, 0, i); 3159 rtx d = XEXP (e, 0); 3160 3161 if (d) 3162 { 3163 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i)); 3164 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1)); 3165 } 3166 RTVEC_ELT (vec, i) = e; 3167 } 3168 3169 return gen_rtx_PARALLEL (GET_MODE (parallel), vec); 3170 } 3171 3172 /* Emit code to move a block SRC to block DST, where SRC and DST are 3173 non-consecutive groups of registers, each represented by a PARALLEL. */ 3174 3175 void 3176 emit_group_move (rtx dst, rtx src) 3177 { 3178 int i; 3179 3180 gcc_assert (GET_CODE (src) == PARALLEL 3181 && GET_CODE (dst) == PARALLEL 3182 && XVECLEN (src, 0) == XVECLEN (dst, 0)); 3183 3184 /* Skip first entry if NULL. */ 3185 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++) 3186 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0), 3187 XEXP (XVECEXP (src, 0, i), 0)); 3188 } 3189 3190 /* Move a group of registers represented by a PARALLEL into pseudos. */ 3191 3192 rtx 3193 emit_group_move_into_temps (rtx src) 3194 { 3195 rtvec vec = rtvec_alloc (XVECLEN (src, 0)); 3196 int i; 3197 3198 for (i = 0; i < XVECLEN (src, 0); i++) 3199 { 3200 rtx e = XVECEXP (src, 0, i); 3201 rtx d = XEXP (e, 0); 3202 3203 if (d) 3204 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1)); 3205 RTVEC_ELT (vec, i) = e; 3206 } 3207 3208 return gen_rtx_PARALLEL (GET_MODE (src), vec); 3209 } 3210 3211 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE, 3212 where SRC is non-consecutive registers represented by a PARALLEL. 3213 SSIZE represents the total size of block ORIG_DST, or -1 if not 3214 known. */ 3215 3216 void 3217 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, 3218 poly_int64 ssize) 3219 { 3220 rtx *tmps, dst; 3221 int start, finish, i; 3222 machine_mode m = GET_MODE (orig_dst); 3223 3224 gcc_assert (GET_CODE (src) == PARALLEL); 3225 3226 if (!SCALAR_INT_MODE_P (m) 3227 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT) 3228 { 3229 scalar_int_mode imode; 3230 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode)) 3231 { 3232 dst = gen_reg_rtx (imode); 3233 emit_group_store (dst, src, type, ssize); 3234 dst = gen_lowpart (GET_MODE (orig_dst), dst); 3235 } 3236 else 3237 { 3238 dst = assign_stack_temp (GET_MODE (orig_dst), ssize); 3239 emit_group_store (dst, src, type, ssize); 3240 } 3241 emit_move_insn (orig_dst, dst); 3242 return; 3243 } 3244 3245 /* Check for a NULL entry, used to indicate that the parameter goes 3246 both on the stack and in registers. */ 3247 if (XEXP (XVECEXP (src, 0, 0), 0)) 3248 start = 0; 3249 else 3250 start = 1; 3251 finish = XVECLEN (src, 0); 3252 3253 tmps = XALLOCAVEC (rtx, finish); 3254 3255 /* Copy the (probable) hard regs into pseudos. */ 3256 for (i = start; i < finish; i++) 3257 { 3258 rtx reg = XEXP (XVECEXP (src, 0, i), 0); 3259 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER) 3260 { 3261 tmps[i] = gen_reg_rtx (GET_MODE (reg)); 3262 emit_move_insn (tmps[i], reg); 3263 } 3264 else 3265 tmps[i] = reg; 3266 } 3267 3268 /* If we won't be storing directly into memory, protect the real destination 3269 from strange tricks we might play. */ 3270 dst = orig_dst; 3271 if (GET_CODE (dst) == PARALLEL) 3272 { 3273 rtx temp; 3274 3275 /* We can get a PARALLEL dst if there is a conditional expression in 3276 a return statement. In that case, the dst and src are the same, 3277 so no action is necessary. */ 3278 if (rtx_equal_p (dst, src)) 3279 return; 3280 3281 /* It is unclear if we can ever reach here, but we may as well handle 3282 it. Allocate a temporary, and split this into a store/load to/from 3283 the temporary. */ 3284 temp = assign_stack_temp (GET_MODE (dst), ssize); 3285 emit_group_store (temp, src, type, ssize); 3286 emit_group_load (dst, temp, type, ssize); 3287 return; 3288 } 3289 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT) 3290 { 3291 machine_mode outer = GET_MODE (dst); 3292 machine_mode inner; 3293 poly_int64 bytepos; 3294 bool done = false; 3295 rtx temp; 3296 3297 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER) 3298 dst = gen_reg_rtx (outer); 3299 3300 /* Make life a bit easier for combine: if the first element of the 3301 vector is the low part of the destination mode, use a paradoxical 3302 subreg to initialize the destination. */ 3303 if (start < finish) 3304 { 3305 inner = GET_MODE (tmps[start]); 3306 bytepos = subreg_lowpart_offset (inner, outer); 3307 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)), 3308 bytepos)) 3309 { 3310 temp = simplify_gen_subreg (outer, tmps[start], inner, 0); 3311 if (temp) 3312 { 3313 emit_move_insn (dst, temp); 3314 done = true; 3315 start++; 3316 } 3317 } 3318 } 3319 3320 /* If the first element wasn't the low part, try the last. */ 3321 if (!done 3322 && start < finish - 1) 3323 { 3324 inner = GET_MODE (tmps[finish - 1]); 3325 bytepos = subreg_lowpart_offset (inner, outer); 3326 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, 3327 finish - 1), 1)), 3328 bytepos)) 3329 { 3330 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0); 3331 if (temp) 3332 { 3333 emit_move_insn (dst, temp); 3334 done = true; 3335 finish--; 3336 } 3337 } 3338 } 3339 3340 /* Otherwise, simply initialize the result to zero. */ 3341 if (!done) 3342 emit_move_insn (dst, CONST0_RTX (outer)); 3343 } 3344 3345 /* Process the pieces. */ 3346 for (i = start; i < finish; i++) 3347 { 3348 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1)); 3349 machine_mode mode = GET_MODE (tmps[i]); 3350 poly_int64 bytelen = GET_MODE_SIZE (mode); 3351 poly_uint64 adj_bytelen; 3352 rtx dest = dst; 3353 3354 /* Handle trailing fragments that run over the size of the struct. 3355 It's the target's responsibility to make sure that the fragment 3356 cannot be strictly smaller in some cases and strictly larger 3357 in others. */ 3358 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize)); 3359 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize)) 3360 adj_bytelen = ssize - bytepos; 3361 else 3362 adj_bytelen = bytelen; 3363 3364 /* Deal with destination CONCATs by either storing into one of the parts 3365 or doing a copy after storing into a register or stack temporary. */ 3366 if (GET_CODE (dst) == CONCAT) 3367 { 3368 if (known_le (bytepos + adj_bytelen, 3369 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))) 3370 dest = XEXP (dst, 0); 3371 3372 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))) 3373 { 3374 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))); 3375 dest = XEXP (dst, 1); 3376 } 3377 3378 else 3379 { 3380 machine_mode dest_mode = GET_MODE (dest); 3381 machine_mode tmp_mode = GET_MODE (tmps[i]); 3382 scalar_int_mode dest_imode; 3383 3384 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0)); 3385 3386 /* If the source is a single scalar integer register, and the 3387 destination has a complex mode for which a same-sized integer 3388 mode exists, then we can take the left-justified part of the 3389 source in the complex mode. */ 3390 if (finish == start + 1 3391 && REG_P (tmps[i]) 3392 && SCALAR_INT_MODE_P (tmp_mode) 3393 && COMPLEX_MODE_P (dest_mode) 3394 && int_mode_for_mode (dest_mode).exists (&dest_imode)) 3395 { 3396 const scalar_int_mode tmp_imode 3397 = as_a <scalar_int_mode> (tmp_mode); 3398 3399 if (GET_MODE_BITSIZE (dest_imode) 3400 < GET_MODE_BITSIZE (tmp_imode)) 3401 { 3402 dest = gen_reg_rtx (dest_imode); 3403 if (BYTES_BIG_ENDIAN) 3404 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i], 3405 GET_MODE_BITSIZE (tmp_imode) 3406 - GET_MODE_BITSIZE (dest_imode), 3407 NULL_RTX, 1); 3408 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i])); 3409 dst = gen_lowpart (dest_mode, dest); 3410 } 3411 else 3412 dst = gen_lowpart (dest_mode, tmps[i]); 3413 } 3414 3415 /* Otherwise spill the source onto the stack using the more 3416 aligned of the two modes. */ 3417 else if (GET_MODE_ALIGNMENT (dest_mode) 3418 >= GET_MODE_ALIGNMENT (tmp_mode)) 3419 { 3420 dest = assign_stack_temp (dest_mode, 3421 GET_MODE_SIZE (dest_mode)); 3422 emit_move_insn (adjust_address (dest, tmp_mode, bytepos), 3423 tmps[i]); 3424 dst = dest; 3425 } 3426 3427 else 3428 { 3429 dest = assign_stack_temp (tmp_mode, 3430 GET_MODE_SIZE (tmp_mode)); 3431 emit_move_insn (dest, tmps[i]); 3432 dst = adjust_address (dest, dest_mode, bytepos); 3433 } 3434 3435 break; 3436 } 3437 } 3438 3439 /* Handle trailing fragments that run over the size of the struct. */ 3440 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize)) 3441 { 3442 /* store_bit_field always takes its value from the lsb. 3443 Move the fragment to the lsb if it's not already there. */ 3444 if ( 3445 #ifdef BLOCK_REG_PADDING 3446 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start) 3447 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD) 3448 #else 3449 BYTES_BIG_ENDIAN 3450 #endif 3451 ) 3452 { 3453 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT; 3454 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i], 3455 shift, tmps[i], 0); 3456 } 3457 3458 /* Make sure not to write past the end of the struct. */ 3459 store_bit_field (dest, 3460 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT, 3461 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1, 3462 VOIDmode, tmps[i], false, false); 3463 } 3464 3465 /* Optimize the access just a bit. */ 3466 else if (MEM_P (dest) 3467 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest)) 3468 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode)) 3469 && multiple_p (bytepos * BITS_PER_UNIT, 3470 GET_MODE_ALIGNMENT (mode)) 3471 && known_eq (bytelen, GET_MODE_SIZE (mode))) 3472 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]); 3473 3474 else 3475 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT, 3476 0, 0, mode, tmps[i], false, false); 3477 } 3478 3479 /* Copy from the pseudo into the (probable) hard reg. */ 3480 if (orig_dst != dst) 3481 emit_move_insn (orig_dst, dst); 3482 } 3483 3484 /* Return a form of X that does not use a PARALLEL. TYPE is the type 3485 of the value stored in X. */ 3486 3487 rtx 3488 maybe_emit_group_store (rtx x, tree type) 3489 { 3490 machine_mode mode = TYPE_MODE (type); 3491 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode); 3492 if (GET_CODE (x) == PARALLEL) 3493 { 3494 rtx result = gen_reg_rtx (mode); 3495 emit_group_store (result, x, type, int_size_in_bytes (type)); 3496 return result; 3497 } 3498 return x; 3499 } 3500 3501 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET. 3502 3503 This is used on targets that return BLKmode values in registers. */ 3504 3505 static void 3506 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type) 3507 { 3508 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type); 3509 rtx src = NULL, dst = NULL; 3510 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD); 3511 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0; 3512 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */ 3513 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg)); 3514 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target)); 3515 fixed_size_mode copy_mode; 3516 3517 /* BLKmode registers created in the back-end shouldn't have survived. */ 3518 gcc_assert (mode != BLKmode); 3519 3520 /* If the structure doesn't take up a whole number of words, see whether 3521 SRCREG is padded on the left or on the right. If it's on the left, 3522 set PADDING_CORRECTION to the number of bits to skip. 3523 3524 In most ABIs, the structure will be returned at the least end of 3525 the register, which translates to right padding on little-endian 3526 targets and left padding on big-endian targets. The opposite 3527 holds if the structure is returned at the most significant 3528 end of the register. */ 3529 if (bytes % UNITS_PER_WORD != 0 3530 && (targetm.calls.return_in_msb (type) 3531 ? !BYTES_BIG_ENDIAN 3532 : BYTES_BIG_ENDIAN)) 3533 padding_correction 3534 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT)); 3535 3536 /* We can use a single move if we have an exact mode for the size. */ 3537 else if (MEM_P (target) 3538 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target)) 3539 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode)) 3540 && bytes == GET_MODE_SIZE (mode)) 3541 { 3542 emit_move_insn (adjust_address (target, mode, 0), srcreg); 3543 return; 3544 } 3545 3546 /* And if we additionally have the same mode for a register. */ 3547 else if (REG_P (target) 3548 && GET_MODE (target) == mode 3549 && bytes == GET_MODE_SIZE (mode)) 3550 { 3551 emit_move_insn (target, srcreg); 3552 return; 3553 } 3554 3555 /* This code assumes srcreg is at least a full word. If it isn't, copy it 3556 into a new pseudo which is a full word. */ 3557 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD) 3558 { 3559 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type)); 3560 mode = word_mode; 3561 } 3562 3563 /* Copy the structure BITSIZE bits at a time. If the target lives in 3564 memory, take care of not reading/writing past its end by selecting 3565 a copy mode suited to BITSIZE. This should always be possible given 3566 how it is computed. 3567 3568 If the target lives in register, make sure not to select a copy mode 3569 larger than the mode of the register. 3570 3571 We could probably emit more efficient code for machines which do not use 3572 strict alignment, but it doesn't seem worth the effort at the current 3573 time. */ 3574 3575 copy_mode = word_mode; 3576 if (MEM_P (target)) 3577 { 3578 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1); 3579 if (mem_mode.exists ()) 3580 copy_mode = mem_mode.require (); 3581 } 3582 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD) 3583 copy_mode = tmode; 3584 3585 for (bitpos = 0, xbitpos = padding_correction; 3586 bitpos < bytes * BITS_PER_UNIT; 3587 bitpos += bitsize, xbitpos += bitsize) 3588 { 3589 /* We need a new source operand each time xbitpos is on a 3590 word boundary and when xbitpos == padding_correction 3591 (the first time through). */ 3592 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction) 3593 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode); 3594 3595 /* We need a new destination operand each time bitpos is on 3596 a word boundary. */ 3597 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD) 3598 dst = target; 3599 else if (bitpos % BITS_PER_WORD == 0) 3600 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode); 3601 3602 /* Use xbitpos for the source extraction (right justified) and 3603 bitpos for the destination store (left justified). */ 3604 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode, 3605 extract_bit_field (src, bitsize, 3606 xbitpos % BITS_PER_WORD, 1, 3607 NULL_RTX, copy_mode, copy_mode, 3608 false, NULL), 3609 false, false); 3610 } 3611 } 3612 3613 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the 3614 register if it contains any data, otherwise return null. 3615 3616 This is used on targets that return BLKmode values in registers. */ 3617 3618 rtx 3619 copy_blkmode_to_reg (machine_mode mode_in, tree src) 3620 { 3621 int i, n_regs; 3622 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes; 3623 unsigned int bitsize; 3624 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX; 3625 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */ 3626 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in); 3627 fixed_size_mode dst_mode; 3628 scalar_int_mode min_mode; 3629 3630 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode); 3631 3632 x = expand_normal (src); 3633 3634 bytes = arg_int_size_in_bytes (TREE_TYPE (src)); 3635 if (bytes == 0) 3636 return NULL_RTX; 3637 3638 /* If the structure doesn't take up a whole number of words, see 3639 whether the register value should be padded on the left or on 3640 the right. Set PADDING_CORRECTION to the number of padding 3641 bits needed on the left side. 3642 3643 In most ABIs, the structure will be returned at the least end of 3644 the register, which translates to right padding on little-endian 3645 targets and left padding on big-endian targets. The opposite 3646 holds if the structure is returned at the most significant 3647 end of the register. */ 3648 if (bytes % UNITS_PER_WORD != 0 3649 && (targetm.calls.return_in_msb (TREE_TYPE (src)) 3650 ? !BYTES_BIG_ENDIAN 3651 : BYTES_BIG_ENDIAN)) 3652 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) 3653 * BITS_PER_UNIT)); 3654 3655 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD; 3656 dst_words = XALLOCAVEC (rtx, n_regs); 3657 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD); 3658 min_mode = smallest_int_mode_for_size (bitsize); 3659 3660 /* Copy the structure BITSIZE bits at a time. */ 3661 for (bitpos = 0, xbitpos = padding_correction; 3662 bitpos < bytes * BITS_PER_UNIT; 3663 bitpos += bitsize, xbitpos += bitsize) 3664 { 3665 /* We need a new destination pseudo each time xbitpos is 3666 on a word boundary and when xbitpos == padding_correction 3667 (the first time through). */ 3668 if (xbitpos % BITS_PER_WORD == 0 3669 || xbitpos == padding_correction) 3670 { 3671 /* Generate an appropriate register. */ 3672 dst_word = gen_reg_rtx (word_mode); 3673 dst_words[xbitpos / BITS_PER_WORD] = dst_word; 3674 3675 /* Clear the destination before we move anything into it. */ 3676 emit_move_insn (dst_word, CONST0_RTX (word_mode)); 3677 } 3678 3679 /* Find the largest integer mode that can be used to copy all or as 3680 many bits as possible of the structure if the target supports larger 3681 copies. There are too many corner cases here w.r.t to alignments on 3682 the read/writes. So if there is any padding just use single byte 3683 operations. */ 3684 opt_scalar_int_mode mode_iter; 3685 if (padding_correction == 0 && !STRICT_ALIGNMENT) 3686 { 3687 FOR_EACH_MODE_FROM (mode_iter, min_mode) 3688 { 3689 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ()); 3690 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos) 3691 && msize <= BITS_PER_WORD) 3692 bitsize = msize; 3693 else 3694 break; 3695 } 3696 } 3697 3698 /* We need a new source operand each time bitpos is on a word 3699 boundary. */ 3700 if (bitpos % BITS_PER_WORD == 0) 3701 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode); 3702 3703 /* Use bitpos for the source extraction (left justified) and 3704 xbitpos for the destination store (right justified). */ 3705 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD, 3706 0, 0, word_mode, 3707 extract_bit_field (src_word, bitsize, 3708 bitpos % BITS_PER_WORD, 1, 3709 NULL_RTX, word_mode, word_mode, 3710 false, NULL), 3711 false, false); 3712 } 3713 3714 if (mode == BLKmode) 3715 { 3716 /* Find the smallest integer mode large enough to hold the 3717 entire structure. */ 3718 opt_scalar_int_mode mode_iter; 3719 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT) 3720 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes) 3721 break; 3722 3723 /* A suitable mode should have been found. */ 3724 mode = mode_iter.require (); 3725 } 3726 3727 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode)) 3728 dst_mode = word_mode; 3729 else 3730 dst_mode = mode; 3731 dst = gen_reg_rtx (dst_mode); 3732 3733 for (i = 0; i < n_regs; i++) 3734 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]); 3735 3736 if (mode != dst_mode) 3737 dst = gen_lowpart (mode, dst); 3738 3739 return dst; 3740 } 3741 3742 /* Add a USE expression for REG to the (possibly empty) list pointed 3743 to by CALL_FUSAGE. REG must denote a hard register. */ 3744 3745 void 3746 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode) 3747 { 3748 gcc_assert (REG_P (reg)); 3749 3750 if (!HARD_REGISTER_P (reg)) 3751 return; 3752 3753 *call_fusage 3754 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage); 3755 } 3756 3757 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed 3758 to by CALL_FUSAGE. REG must denote a hard register. */ 3759 3760 void 3761 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode) 3762 { 3763 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER); 3764 3765 *call_fusage 3766 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage); 3767 } 3768 3769 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs, 3770 starting at REGNO. All of these registers must be hard registers. */ 3771 3772 void 3773 use_regs (rtx *call_fusage, int regno, int nregs) 3774 { 3775 int i; 3776 3777 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER); 3778 3779 for (i = 0; i < nregs; i++) 3780 use_reg (call_fusage, regno_reg_rtx[regno + i]); 3781 } 3782 3783 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the 3784 PARALLEL REGS. This is for calls that pass values in multiple 3785 non-contiguous locations. The Irix 6 ABI has examples of this. */ 3786 3787 void 3788 use_group_regs (rtx *call_fusage, rtx regs) 3789 { 3790 int i; 3791 3792 for (i = 0; i < XVECLEN (regs, 0); i++) 3793 { 3794 rtx reg = XEXP (XVECEXP (regs, 0, i), 0); 3795 3796 /* A NULL entry means the parameter goes both on the stack and in 3797 registers. This can also be a MEM for targets that pass values 3798 partially on the stack and partially in registers. */ 3799 if (reg != 0 && REG_P (reg)) 3800 use_reg (call_fusage, reg); 3801 } 3802 } 3803 3804 /* Return the defining gimple statement for SSA_NAME NAME if it is an 3805 assigment and the code of the expresion on the RHS is CODE. Return 3806 NULL otherwise. */ 3807 3808 static gimple * 3809 get_def_for_expr (tree name, enum tree_code code) 3810 { 3811 gimple *def_stmt; 3812 3813 if (TREE_CODE (name) != SSA_NAME) 3814 return NULL; 3815 3816 def_stmt = get_gimple_for_ssa_name (name); 3817 if (!def_stmt 3818 || gimple_assign_rhs_code (def_stmt) != code) 3819 return NULL; 3820 3821 return def_stmt; 3822 } 3823 3824 /* Return the defining gimple statement for SSA_NAME NAME if it is an 3825 assigment and the class of the expresion on the RHS is CLASS. Return 3826 NULL otherwise. */ 3827 3828 static gimple * 3829 get_def_for_expr_class (tree name, enum tree_code_class tclass) 3830 { 3831 gimple *def_stmt; 3832 3833 if (TREE_CODE (name) != SSA_NAME) 3834 return NULL; 3835 3836 def_stmt = get_gimple_for_ssa_name (name); 3837 if (!def_stmt 3838 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass) 3839 return NULL; 3840 3841 return def_stmt; 3842 } 3843 3844 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is 3846 its length in bytes. */ 3847 3848 rtx 3849 clear_storage_hints (rtx object, rtx size, enum block_op_methods method, 3850 unsigned int expected_align, HOST_WIDE_INT expected_size, 3851 unsigned HOST_WIDE_INT min_size, 3852 unsigned HOST_WIDE_INT max_size, 3853 unsigned HOST_WIDE_INT probable_max_size, 3854 unsigned ctz_size) 3855 { 3856 machine_mode mode = GET_MODE (object); 3857 unsigned int align; 3858 3859 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL); 3860 3861 /* If OBJECT is not BLKmode and SIZE is the same size as its mode, 3862 just move a zero. Otherwise, do this a piece at a time. */ 3863 poly_int64 size_val; 3864 if (mode != BLKmode 3865 && poly_int_rtx_p (size, &size_val) 3866 && known_eq (size_val, GET_MODE_SIZE (mode))) 3867 { 3868 rtx zero = CONST0_RTX (mode); 3869 if (zero != NULL) 3870 { 3871 emit_move_insn (object, zero); 3872 return NULL; 3873 } 3874 3875 if (COMPLEX_MODE_P (mode)) 3876 { 3877 zero = CONST0_RTX (GET_MODE_INNER (mode)); 3878 if (zero != NULL) 3879 { 3880 write_complex_part (object, zero, 0, true); 3881 write_complex_part (object, zero, 1, false); 3882 return NULL; 3883 } 3884 } 3885 } 3886 3887 if (size == const0_rtx) 3888 return NULL; 3889 3890 align = MEM_ALIGN (object); 3891 3892 if (CONST_INT_P (size) 3893 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align, 3894 CLEAR_BY_PIECES, 3895 optimize_insn_for_speed_p ())) 3896 clear_by_pieces (object, INTVAL (size), align); 3897 else if (set_storage_via_setmem (object, size, const0_rtx, align, 3898 expected_align, expected_size, 3899 min_size, max_size, probable_max_size)) 3900 ; 3901 else if (try_store_by_multiple_pieces (object, size, ctz_size, 3902 min_size, max_size, 3903 NULL_RTX, 0, align)) 3904 ; 3905 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object))) 3906 return set_storage_via_libcall (object, size, const0_rtx, 3907 method == BLOCK_OP_TAILCALL); 3908 else 3909 gcc_unreachable (); 3910 3911 return NULL; 3912 } 3913 3914 rtx 3915 clear_storage (rtx object, rtx size, enum block_op_methods method) 3916 { 3917 unsigned HOST_WIDE_INT max, min = 0; 3918 if (GET_CODE (size) == CONST_INT) 3919 min = max = UINTVAL (size); 3920 else 3921 max = GET_MODE_MASK (GET_MODE (size)); 3922 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0); 3923 } 3924 3925 3926 /* A subroutine of clear_storage. Expand a call to memset. 3927 Return the return value of memset, 0 otherwise. */ 3928 3929 rtx 3930 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall) 3931 { 3932 tree call_expr, fn, object_tree, size_tree, val_tree; 3933 machine_mode size_mode; 3934 3935 object = copy_addr_to_reg (XEXP (object, 0)); 3936 object_tree = make_tree (ptr_type_node, object); 3937 3938 if (!CONST_INT_P (val)) 3939 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1); 3940 val_tree = make_tree (integer_type_node, val); 3941 3942 size_mode = TYPE_MODE (sizetype); 3943 size = convert_to_mode (size_mode, size, 1); 3944 size = copy_to_mode_reg (size_mode, size); 3945 size_tree = make_tree (sizetype, size); 3946 3947 /* It is incorrect to use the libcall calling conventions for calls to 3948 memset because it can be provided by the user. */ 3949 fn = builtin_decl_implicit (BUILT_IN_MEMSET); 3950 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree); 3951 CALL_EXPR_TAILCALL (call_expr) = tailcall; 3952 3953 return expand_call (call_expr, NULL_RTX, false); 3954 } 3955 3956 /* Expand a setmem pattern; return true if successful. */ 3958 3959 bool 3960 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align, 3961 unsigned int expected_align, HOST_WIDE_INT expected_size, 3962 unsigned HOST_WIDE_INT min_size, 3963 unsigned HOST_WIDE_INT max_size, 3964 unsigned HOST_WIDE_INT probable_max_size) 3965 { 3966 /* Try the most limited insn first, because there's no point 3967 including more than one in the machine description unless 3968 the more limited one has some advantage. */ 3969 3970 if (expected_align < align) 3971 expected_align = align; 3972 if (expected_size != -1) 3973 { 3974 if ((unsigned HOST_WIDE_INT)expected_size > max_size) 3975 expected_size = max_size; 3976 if ((unsigned HOST_WIDE_INT)expected_size < min_size) 3977 expected_size = min_size; 3978 } 3979 3980 opt_scalar_int_mode mode_iter; 3981 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT) 3982 { 3983 scalar_int_mode mode = mode_iter.require (); 3984 enum insn_code code = direct_optab_handler (setmem_optab, mode); 3985 3986 if (code != CODE_FOR_nothing 3987 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT 3988 here because if SIZE is less than the mode mask, as it is 3989 returned by the macro, it will definitely be less than the 3990 actual mode mask. Since SIZE is within the Pmode address 3991 space, we limit MODE to Pmode. */ 3992 && ((CONST_INT_P (size) 3993 && ((unsigned HOST_WIDE_INT) INTVAL (size) 3994 <= (GET_MODE_MASK (mode) >> 1))) 3995 || max_size <= (GET_MODE_MASK (mode) >> 1) 3996 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode))) 3997 { 3998 class expand_operand ops[9]; 3999 unsigned int nops; 4000 4001 nops = insn_data[(int) code].n_generator_args; 4002 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9); 4003 4004 create_fixed_operand (&ops[0], object); 4005 /* The check above guarantees that this size conversion is valid. */ 4006 create_convert_operand_to (&ops[1], size, mode, true); 4007 create_convert_operand_from (&ops[2], val, byte_mode, true); 4008 create_integer_operand (&ops[3], align / BITS_PER_UNIT); 4009 if (nops >= 6) 4010 { 4011 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT); 4012 create_integer_operand (&ops[5], expected_size); 4013 } 4014 if (nops >= 8) 4015 { 4016 create_integer_operand (&ops[6], min_size); 4017 /* If we cannot represent the maximal size, 4018 make parameter NULL. */ 4019 if ((HOST_WIDE_INT) max_size != -1) 4020 create_integer_operand (&ops[7], max_size); 4021 else 4022 create_fixed_operand (&ops[7], NULL); 4023 } 4024 if (nops == 9) 4025 { 4026 /* If we cannot represent the maximal size, 4027 make parameter NULL. */ 4028 if ((HOST_WIDE_INT) probable_max_size != -1) 4029 create_integer_operand (&ops[8], probable_max_size); 4030 else 4031 create_fixed_operand (&ops[8], NULL); 4032 } 4033 if (maybe_expand_insn (code, nops, ops)) 4034 return true; 4035 } 4036 } 4037 4038 return false; 4039 } 4040 4041 4042 /* Write to one of the components of the complex value CPLX. Write VAL to 4044 the real part if IMAG_P is false, and the imaginary part if its true. 4045 If UNDEFINED_P then the value in CPLX is currently undefined. */ 4046 4047 void 4048 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p) 4049 { 4050 machine_mode cmode; 4051 scalar_mode imode; 4052 unsigned ibitsize; 4053 4054 if (GET_CODE (cplx) == CONCAT) 4055 { 4056 emit_move_insn (XEXP (cplx, imag_p), val); 4057 return; 4058 } 4059 4060 cmode = GET_MODE (cplx); 4061 imode = GET_MODE_INNER (cmode); 4062 ibitsize = GET_MODE_BITSIZE (imode); 4063 4064 /* For MEMs simplify_gen_subreg may generate an invalid new address 4065 because, e.g., the original address is considered mode-dependent 4066 by the target, which restricts simplify_subreg from invoking 4067 adjust_address_nv. Instead of preparing fallback support for an 4068 invalid address, we call adjust_address_nv directly. */ 4069 if (MEM_P (cplx)) 4070 { 4071 emit_move_insn (adjust_address_nv (cplx, imode, 4072 imag_p ? GET_MODE_SIZE (imode) : 0), 4073 val); 4074 return; 4075 } 4076 4077 /* If the sub-object is at least word sized, then we know that subregging 4078 will work. This special case is important, since store_bit_field 4079 wants to operate on integer modes, and there's rarely an OImode to 4080 correspond to TCmode. */ 4081 if (ibitsize >= BITS_PER_WORD 4082 /* For hard regs we have exact predicates. Assume we can split 4083 the original object if it spans an even number of hard regs. 4084 This special case is important for SCmode on 64-bit platforms 4085 where the natural size of floating-point regs is 32-bit. */ 4086 || (REG_P (cplx) 4087 && REGNO (cplx) < FIRST_PSEUDO_REGISTER 4088 && REG_NREGS (cplx) % 2 == 0)) 4089 { 4090 rtx part = simplify_gen_subreg (imode, cplx, cmode, 4091 imag_p ? GET_MODE_SIZE (imode) : 0); 4092 if (part) 4093 { 4094 emit_move_insn (part, val); 4095 return; 4096 } 4097 else 4098 /* simplify_gen_subreg may fail for sub-word MEMs. */ 4099 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD); 4100 } 4101 4102 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val, 4103 false, undefined_p); 4104 } 4105 4106 /* Extract one of the components of the complex value CPLX. Extract the 4107 real part if IMAG_P is false, and the imaginary part if it's true. */ 4108 4109 rtx 4110 read_complex_part (rtx cplx, bool imag_p) 4111 { 4112 machine_mode cmode; 4113 scalar_mode imode; 4114 unsigned ibitsize; 4115 4116 if (GET_CODE (cplx) == CONCAT) 4117 return XEXP (cplx, imag_p); 4118 4119 cmode = GET_MODE (cplx); 4120 imode = GET_MODE_INNER (cmode); 4121 ibitsize = GET_MODE_BITSIZE (imode); 4122 4123 /* Special case reads from complex constants that got spilled to memory. */ 4124 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF) 4125 { 4126 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0)); 4127 if (decl && TREE_CODE (decl) == COMPLEX_CST) 4128 { 4129 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl); 4130 if (CONSTANT_CLASS_P (part)) 4131 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL); 4132 } 4133 } 4134 4135 /* For MEMs simplify_gen_subreg may generate an invalid new address 4136 because, e.g., the original address is considered mode-dependent 4137 by the target, which restricts simplify_subreg from invoking 4138 adjust_address_nv. Instead of preparing fallback support for an 4139 invalid address, we call adjust_address_nv directly. */ 4140 if (MEM_P (cplx)) 4141 return adjust_address_nv (cplx, imode, 4142 imag_p ? GET_MODE_SIZE (imode) : 0); 4143 4144 /* If the sub-object is at least word sized, then we know that subregging 4145 will work. This special case is important, since extract_bit_field 4146 wants to operate on integer modes, and there's rarely an OImode to 4147 correspond to TCmode. */ 4148 if (ibitsize >= BITS_PER_WORD 4149 /* For hard regs we have exact predicates. Assume we can split 4150 the original object if it spans an even number of hard regs. 4151 This special case is important for SCmode on 64-bit platforms 4152 where the natural size of floating-point regs is 32-bit. */ 4153 || (REG_P (cplx) 4154 && REGNO (cplx) < FIRST_PSEUDO_REGISTER 4155 && REG_NREGS (cplx) % 2 == 0)) 4156 { 4157 rtx ret = simplify_gen_subreg (imode, cplx, cmode, 4158 imag_p ? GET_MODE_SIZE (imode) : 0); 4159 if (ret) 4160 return ret; 4161 else 4162 /* simplify_gen_subreg may fail for sub-word MEMs. */ 4163 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD); 4164 } 4165 4166 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 4167 true, NULL_RTX, imode, imode, false, NULL); 4168 } 4169 4170 /* A subroutine of emit_move_insn_1. Yet another lowpart generator. 4172 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be 4173 represented in NEW_MODE. If FORCE is true, this will never happen, as 4174 we'll force-create a SUBREG if needed. */ 4175 4176 static rtx 4177 emit_move_change_mode (machine_mode new_mode, 4178 machine_mode old_mode, rtx x, bool force) 4179 { 4180 rtx ret; 4181 4182 if (push_operand (x, GET_MODE (x))) 4183 { 4184 ret = gen_rtx_MEM (new_mode, XEXP (x, 0)); 4185 MEM_COPY_ATTRIBUTES (ret, x); 4186 } 4187 else if (MEM_P (x)) 4188 { 4189 /* We don't have to worry about changing the address since the 4190 size in bytes is supposed to be the same. */ 4191 if (reload_in_progress) 4192 { 4193 /* Copy the MEM to change the mode and move any 4194 substitutions from the old MEM to the new one. */ 4195 ret = adjust_address_nv (x, new_mode, 0); 4196 copy_replacements (x, ret); 4197 } 4198 else 4199 ret = adjust_address (x, new_mode, 0); 4200 } 4201 else 4202 { 4203 /* Note that we do want simplify_subreg's behavior of validating 4204 that the new mode is ok for a hard register. If we were to use 4205 simplify_gen_subreg, we would create the subreg, but would 4206 probably run into the target not being able to implement it. */ 4207 /* Except, of course, when FORCE is true, when this is exactly what 4208 we want. Which is needed for CCmodes on some targets. */ 4209 if (force) 4210 ret = simplify_gen_subreg (new_mode, x, old_mode, 0); 4211 else 4212 ret = simplify_subreg (new_mode, x, old_mode, 0); 4213 } 4214 4215 return ret; 4216 } 4217 4218 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using 4219 an integer mode of the same size as MODE. Returns the instruction 4220 emitted, or NULL if such a move could not be generated. */ 4221 4222 static rtx_insn * 4223 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force) 4224 { 4225 scalar_int_mode imode; 4226 enum insn_code code; 4227 4228 /* There must exist a mode of the exact size we require. */ 4229 if (!int_mode_for_mode (mode).exists (&imode)) 4230 return NULL; 4231 4232 /* The target must support moves in this mode. */ 4233 code = optab_handler (mov_optab, imode); 4234 if (code == CODE_FOR_nothing) 4235 return NULL; 4236 4237 x = emit_move_change_mode (imode, mode, x, force); 4238 if (x == NULL_RTX) 4239 return NULL; 4240 y = emit_move_change_mode (imode, mode, y, force); 4241 if (y == NULL_RTX) 4242 return NULL; 4243 return emit_insn (GEN_FCN (code) (x, y)); 4244 } 4245 4246 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE. 4247 Return an equivalent MEM that does not use an auto-increment. */ 4248 4249 rtx 4250 emit_move_resolve_push (machine_mode mode, rtx x) 4251 { 4252 enum rtx_code code = GET_CODE (XEXP (x, 0)); 4253 rtx temp; 4254 4255 poly_int64 adjust = GET_MODE_SIZE (mode); 4256 #ifdef PUSH_ROUNDING 4257 adjust = PUSH_ROUNDING (adjust); 4258 #endif 4259 if (code == PRE_DEC || code == POST_DEC) 4260 adjust = -adjust; 4261 else if (code == PRE_MODIFY || code == POST_MODIFY) 4262 { 4263 rtx expr = XEXP (XEXP (x, 0), 1); 4264 4265 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS); 4266 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1)); 4267 if (GET_CODE (expr) == MINUS) 4268 val = -val; 4269 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val)); 4270 adjust = val; 4271 } 4272 4273 /* Do not use anti_adjust_stack, since we don't want to update 4274 stack_pointer_delta. */ 4275 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx, 4276 gen_int_mode (adjust, Pmode), stack_pointer_rtx, 4277 0, OPTAB_LIB_WIDEN); 4278 if (temp != stack_pointer_rtx) 4279 emit_move_insn (stack_pointer_rtx, temp); 4280 4281 switch (code) 4282 { 4283 case PRE_INC: 4284 case PRE_DEC: 4285 case PRE_MODIFY: 4286 temp = stack_pointer_rtx; 4287 break; 4288 case POST_INC: 4289 case POST_DEC: 4290 case POST_MODIFY: 4291 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust); 4292 break; 4293 default: 4294 gcc_unreachable (); 4295 } 4296 4297 return replace_equiv_address (x, temp); 4298 } 4299 4300 /* A subroutine of emit_move_complex. Generate a move from Y into X. 4301 X is known to satisfy push_operand, and MODE is known to be complex. 4302 Returns the last instruction emitted. */ 4303 4304 rtx_insn * 4305 emit_move_complex_push (machine_mode mode, rtx x, rtx y) 4306 { 4307 scalar_mode submode = GET_MODE_INNER (mode); 4308 bool imag_first; 4309 4310 #ifdef PUSH_ROUNDING 4311 poly_int64 submodesize = GET_MODE_SIZE (submode); 4312 4313 /* In case we output to the stack, but the size is smaller than the 4314 machine can push exactly, we need to use move instructions. */ 4315 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize)) 4316 { 4317 x = emit_move_resolve_push (mode, x); 4318 return emit_move_insn (x, y); 4319 } 4320 #endif 4321 4322 /* Note that the real part always precedes the imag part in memory 4323 regardless of machine's endianness. */ 4324 switch (GET_CODE (XEXP (x, 0))) 4325 { 4326 case PRE_DEC: 4327 case POST_DEC: 4328 imag_first = true; 4329 break; 4330 case PRE_INC: 4331 case POST_INC: 4332 imag_first = false; 4333 break; 4334 default: 4335 gcc_unreachable (); 4336 } 4337 4338 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)), 4339 read_complex_part (y, imag_first)); 4340 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)), 4341 read_complex_part (y, !imag_first)); 4342 } 4343 4344 /* A subroutine of emit_move_complex. Perform the move from Y to X 4345 via two moves of the parts. Returns the last instruction emitted. */ 4346 4347 rtx_insn * 4348 emit_move_complex_parts (rtx x, rtx y) 4349 { 4350 /* Show the output dies here. This is necessary for SUBREGs 4351 of pseudos since we cannot track their lifetimes correctly; 4352 hard regs shouldn't appear here except as return values. */ 4353 if (!reload_completed && !reload_in_progress 4354 && REG_P (x) && !reg_overlap_mentioned_p (x, y)) 4355 emit_clobber (x); 4356 4357 write_complex_part (x, read_complex_part (y, false), false, true); 4358 write_complex_part (x, read_complex_part (y, true), true, false); 4359 4360 return get_last_insn (); 4361 } 4362 4363 /* A subroutine of emit_move_insn_1. Generate a move from Y into X. 4364 MODE is known to be complex. Returns the last instruction emitted. */ 4365 4366 static rtx_insn * 4367 emit_move_complex (machine_mode mode, rtx x, rtx y) 4368 { 4369 bool try_int; 4370 4371 /* Need to take special care for pushes, to maintain proper ordering 4372 of the data, and possibly extra padding. */ 4373 if (push_operand (x, mode)) 4374 return emit_move_complex_push (mode, x, y); 4375 4376 /* See if we can coerce the target into moving both values at once, except 4377 for floating point where we favor moving as parts if this is easy. */ 4378 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT 4379 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing 4380 && !(REG_P (x) 4381 && HARD_REGISTER_P (x) 4382 && REG_NREGS (x) == 1) 4383 && !(REG_P (y) 4384 && HARD_REGISTER_P (y) 4385 && REG_NREGS (y) == 1)) 4386 try_int = false; 4387 /* Not possible if the values are inherently not adjacent. */ 4388 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT) 4389 try_int = false; 4390 /* Is possible if both are registers (or subregs of registers). */ 4391 else if (register_operand (x, mode) && register_operand (y, mode)) 4392 try_int = true; 4393 /* If one of the operands is a memory, and alignment constraints 4394 are friendly enough, we may be able to do combined memory operations. 4395 We do not attempt this if Y is a constant because that combination is 4396 usually better with the by-parts thing below. */ 4397 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y)) 4398 && (!STRICT_ALIGNMENT 4399 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT)) 4400 try_int = true; 4401 else 4402 try_int = false; 4403 4404 if (try_int) 4405 { 4406 rtx_insn *ret; 4407 4408 /* For memory to memory moves, optimal behavior can be had with the 4409 existing block move logic. But use normal expansion if optimizing 4410 for size. */ 4411 if (MEM_P (x) && MEM_P (y)) 4412 { 4413 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode), 4414 (optimize_insn_for_speed_p() 4415 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL)); 4416 return get_last_insn (); 4417 } 4418 4419 ret = emit_move_via_integer (mode, x, y, true); 4420 if (ret) 4421 return ret; 4422 } 4423 4424 return emit_move_complex_parts (x, y); 4425 } 4426 4427 /* A subroutine of emit_move_insn_1. Generate a move from Y into X. 4428 MODE is known to be MODE_CC. Returns the last instruction emitted. */ 4429 4430 static rtx_insn * 4431 emit_move_ccmode (machine_mode mode, rtx x, rtx y) 4432 { 4433 rtx_insn *ret; 4434 4435 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */ 4436 if (mode != CCmode) 4437 { 4438 enum insn_code code = optab_handler (mov_optab, CCmode); 4439 if (code != CODE_FOR_nothing) 4440 { 4441 x = emit_move_change_mode (CCmode, mode, x, true); 4442 y = emit_move_change_mode (CCmode, mode, y, true); 4443 return emit_insn (GEN_FCN (code) (x, y)); 4444 } 4445 } 4446 4447 /* Otherwise, find the MODE_INT mode of the same width. */ 4448 ret = emit_move_via_integer (mode, x, y, false); 4449 gcc_assert (ret != NULL); 4450 return ret; 4451 } 4452 4453 /* Return true if word I of OP lies entirely in the 4454 undefined bits of a paradoxical subreg. */ 4455 4456 static bool 4457 undefined_operand_subword_p (const_rtx op, int i) 4458 { 4459 if (GET_CODE (op) != SUBREG) 4460 return false; 4461 machine_mode innermostmode = GET_MODE (SUBREG_REG (op)); 4462 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op); 4463 return (known_ge (offset, GET_MODE_SIZE (innermostmode)) 4464 || known_le (offset, -UNITS_PER_WORD)); 4465 } 4466 4467 /* A subroutine of emit_move_insn_1. Generate a move from Y into X. 4468 MODE is any multi-word or full-word mode that lacks a move_insn 4469 pattern. Note that you will get better code if you define such 4470 patterns, even if they must turn into multiple assembler instructions. */ 4471 4472 static rtx_insn * 4473 emit_move_multi_word (machine_mode mode, rtx x, rtx y) 4474 { 4475 rtx_insn *last_insn = 0; 4476 rtx_insn *seq; 4477 rtx inner; 4478 bool need_clobber; 4479 int i, mode_size; 4480 4481 /* This function can only handle cases where the number of words is 4482 known at compile time. */ 4483 mode_size = GET_MODE_SIZE (mode).to_constant (); 4484 gcc_assert (mode_size >= UNITS_PER_WORD); 4485 4486 /* If X is a push on the stack, do the push now and replace 4487 X with a reference to the stack pointer. */ 4488 if (push_operand (x, mode)) 4489 x = emit_move_resolve_push (mode, x); 4490 4491 /* If we are in reload, see if either operand is a MEM whose address 4492 is scheduled for replacement. */ 4493 if (reload_in_progress && MEM_P (x) 4494 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0)) 4495 x = replace_equiv_address_nv (x, inner); 4496 if (reload_in_progress && MEM_P (y) 4497 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0)) 4498 y = replace_equiv_address_nv (y, inner); 4499 4500 start_sequence (); 4501 4502 need_clobber = false; 4503 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++) 4504 { 4505 /* Do not generate code for a move if it would go entirely 4506 to the non-existing bits of a paradoxical subreg. */ 4507 if (undefined_operand_subword_p (x, i)) 4508 continue; 4509 4510 rtx xpart = operand_subword (x, i, 1, mode); 4511 rtx ypart; 4512 4513 /* Do not generate code for a move if it would come entirely 4514 from the undefined bits of a paradoxical subreg. */ 4515 if (undefined_operand_subword_p (y, i)) 4516 continue; 4517 4518 ypart = operand_subword (y, i, 1, mode); 4519 4520 /* If we can't get a part of Y, put Y into memory if it is a 4521 constant. Otherwise, force it into a register. Then we must 4522 be able to get a part of Y. */ 4523 if (ypart == 0 && CONSTANT_P (y)) 4524 { 4525 y = use_anchored_address (force_const_mem (mode, y)); 4526 ypart = operand_subword (y, i, 1, mode); 4527 } 4528 else if (ypart == 0) 4529 ypart = operand_subword_force (y, i, mode); 4530 4531 gcc_assert (xpart && ypart); 4532 4533 need_clobber |= (GET_CODE (xpart) == SUBREG); 4534 4535 last_insn = emit_move_insn (xpart, ypart); 4536 } 4537 4538 seq = get_insns (); 4539 end_sequence (); 4540 4541 /* Show the output dies here. This is necessary for SUBREGs 4542 of pseudos since we cannot track their lifetimes correctly; 4543 hard regs shouldn't appear here except as return values. 4544 We never want to emit such a clobber after reload. */ 4545 if (x != y 4546 && ! (reload_in_progress || reload_completed) 4547 && need_clobber != 0) 4548 emit_clobber (x); 4549 4550 emit_insn (seq); 4551 4552 return last_insn; 4553 } 4554 4555 /* Low level part of emit_move_insn. 4556 Called just like emit_move_insn, but assumes X and Y 4557 are basically valid. */ 4558 4559 rtx_insn * 4560 emit_move_insn_1 (rtx x, rtx y) 4561 { 4562 machine_mode mode = GET_MODE (x); 4563 enum insn_code code; 4564 4565 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE); 4566 4567 code = optab_handler (mov_optab, mode); 4568 if (code != CODE_FOR_nothing) 4569 return emit_insn (GEN_FCN (code) (x, y)); 4570 4571 /* Expand complex moves by moving real part and imag part. */ 4572 if (COMPLEX_MODE_P (mode)) 4573 return emit_move_complex (mode, x, y); 4574 4575 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT 4576 || ALL_FIXED_POINT_MODE_P (mode)) 4577 { 4578 rtx_insn *result = emit_move_via_integer (mode, x, y, true); 4579 4580 /* If we can't find an integer mode, use multi words. */ 4581 if (result) 4582 return result; 4583 else 4584 return emit_move_multi_word (mode, x, y); 4585 } 4586 4587 if (GET_MODE_CLASS (mode) == MODE_CC) 4588 return emit_move_ccmode (mode, x, y); 4589 4590 /* Try using a move pattern for the corresponding integer mode. This is 4591 only safe when simplify_subreg can convert MODE constants into integer 4592 constants. At present, it can only do this reliably if the value 4593 fits within a HOST_WIDE_INT. */ 4594 if (!CONSTANT_P (y) 4595 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT)) 4596 { 4597 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress); 4598 4599 if (ret) 4600 { 4601 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0) 4602 return ret; 4603 } 4604 } 4605 4606 return emit_move_multi_word (mode, x, y); 4607 } 4608 4609 /* Generate code to copy Y into X. 4610 Both Y and X must have the same mode, except that 4611 Y can be a constant with VOIDmode. 4612 This mode cannot be BLKmode; use emit_block_move for that. 4613 4614 Return the last instruction emitted. */ 4615 4616 rtx_insn * 4617 emit_move_insn (rtx x, rtx y) 4618 { 4619 machine_mode mode = GET_MODE (x); 4620 rtx y_cst = NULL_RTX; 4621 rtx_insn *last_insn; 4622 rtx set; 4623 4624 gcc_assert (mode != BLKmode 4625 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode)); 4626 4627 /* If we have a copy that looks like one of the following patterns: 4628 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...))) 4629 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR)) 4630 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...))) 4631 (set (subreg:M1 (reg:M2 ...)) (constant C)) 4632 where mode M1 is equal in size to M2, try to detect whether the 4633 mode change involves an implicit round trip through memory. 4634 If so, see if we can avoid that by removing the subregs and 4635 doing the move in mode M2 instead. */ 4636 4637 rtx x_inner = NULL_RTX; 4638 rtx y_inner = NULL_RTX; 4639 4640 auto candidate_subreg_p = [&](rtx subreg) { 4641 return (REG_P (SUBREG_REG (subreg)) 4642 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))), 4643 GET_MODE_SIZE (GET_MODE (subreg))) 4644 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg))) 4645 != CODE_FOR_nothing); 4646 }; 4647 4648 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) { 4649 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS) 4650 && !push_operand (mem, GET_MODE (mem)) 4651 /* Not a candiate if innermode requires too much alignment. */ 4652 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode) 4653 || targetm.slow_unaligned_access (GET_MODE (mem), 4654 MEM_ALIGN (mem)) 4655 || !targetm.slow_unaligned_access (innermode, 4656 MEM_ALIGN (mem)))); 4657 }; 4658 4659 if (SUBREG_P (x) && candidate_subreg_p (x)) 4660 x_inner = SUBREG_REG (x); 4661 4662 if (SUBREG_P (y) && candidate_subreg_p (y)) 4663 y_inner = SUBREG_REG (y); 4664 4665 if (x_inner != NULL_RTX 4666 && y_inner != NULL_RTX 4667 && GET_MODE (x_inner) == GET_MODE (y_inner) 4668 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS)) 4669 { 4670 x = x_inner; 4671 y = y_inner; 4672 mode = GET_MODE (x_inner); 4673 } 4674 else if (x_inner != NULL_RTX 4675 && MEM_P (y) 4676 && candidate_mem_p (GET_MODE (x_inner), y)) 4677 { 4678 x = x_inner; 4679 y = adjust_address (y, GET_MODE (x_inner), 0); 4680 mode = GET_MODE (x_inner); 4681 } 4682 else if (y_inner != NULL_RTX 4683 && MEM_P (x) 4684 && candidate_mem_p (GET_MODE (y_inner), x)) 4685 { 4686 x = adjust_address (x, GET_MODE (y_inner), 0); 4687 y = y_inner; 4688 mode = GET_MODE (y_inner); 4689 } 4690 else if (x_inner != NULL_RTX 4691 && CONSTANT_P (y) 4692 && !targetm.can_change_mode_class (GET_MODE (x_inner), 4693 mode, ALL_REGS) 4694 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0))) 4695 { 4696 x = x_inner; 4697 y = y_inner; 4698 mode = GET_MODE (x_inner); 4699 } 4700 4701 if (CONSTANT_P (y)) 4702 { 4703 if (optimize 4704 && SCALAR_FLOAT_MODE_P (GET_MODE (x)) 4705 && (last_insn = compress_float_constant (x, y))) 4706 return last_insn; 4707 4708 y_cst = y; 4709 4710 if (!targetm.legitimate_constant_p (mode, y)) 4711 { 4712 y = force_const_mem (mode, y); 4713 4714 /* If the target's cannot_force_const_mem prevented the spill, 4715 assume that the target's move expanders will also take care 4716 of the non-legitimate constant. */ 4717 if (!y) 4718 y = y_cst; 4719 else 4720 y = use_anchored_address (y); 4721 } 4722 } 4723 4724 /* If X or Y are memory references, verify that their addresses are valid 4725 for the machine. */ 4726 if (MEM_P (x) 4727 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0), 4728 MEM_ADDR_SPACE (x)) 4729 && ! push_operand (x, GET_MODE (x)))) 4730 x = validize_mem (x); 4731 4732 if (MEM_P (y) 4733 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0), 4734 MEM_ADDR_SPACE (y))) 4735 y = validize_mem (y); 4736 4737 gcc_assert (mode != BLKmode); 4738 4739 last_insn = emit_move_insn_1 (x, y); 4740 4741 if (y_cst && REG_P (x) 4742 && (set = single_set (last_insn)) != NULL_RTX 4743 && SET_DEST (set) == x 4744 && ! rtx_equal_p (y_cst, SET_SRC (set))) 4745 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst)); 4746 4747 return last_insn; 4748 } 4749 4750 /* Generate the body of an instruction to copy Y into X. 4751 It may be a list of insns, if one insn isn't enough. */ 4752 4753 rtx_insn * 4754 gen_move_insn (rtx x, rtx y) 4755 { 4756 rtx_insn *seq; 4757 4758 start_sequence (); 4759 emit_move_insn_1 (x, y); 4760 seq = get_insns (); 4761 end_sequence (); 4762 return seq; 4763 } 4764 4765 /* If Y is representable exactly in a narrower mode, and the target can 4766 perform the extension directly from constant or memory, then emit the 4767 move as an extension. */ 4768 4769 static rtx_insn * 4770 compress_float_constant (rtx x, rtx y) 4771 { 4772 machine_mode dstmode = GET_MODE (x); 4773 machine_mode orig_srcmode = GET_MODE (y); 4774 machine_mode srcmode; 4775 const REAL_VALUE_TYPE *r; 4776 int oldcost, newcost; 4777 bool speed = optimize_insn_for_speed_p (); 4778 4779 r = CONST_DOUBLE_REAL_VALUE (y); 4780 4781 if (targetm.legitimate_constant_p (dstmode, y)) 4782 oldcost = set_src_cost (y, orig_srcmode, speed); 4783 else 4784 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed); 4785 4786 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode) 4787 { 4788 enum insn_code ic; 4789 rtx trunc_y; 4790 rtx_insn *last_insn; 4791 4792 /* Skip if the target can't extend this way. */ 4793 ic = can_extend_p (dstmode, srcmode, 0); 4794 if (ic == CODE_FOR_nothing) 4795 continue; 4796 4797 /* Skip if the narrowed value isn't exact. */ 4798 if (! exact_real_truncate (srcmode, r)) 4799 continue; 4800 4801 trunc_y = const_double_from_real_value (*r, srcmode); 4802 4803 if (targetm.legitimate_constant_p (srcmode, trunc_y)) 4804 { 4805 /* Skip if the target needs extra instructions to perform 4806 the extension. */ 4807 if (!insn_operand_matches (ic, 1, trunc_y)) 4808 continue; 4809 /* This is valid, but may not be cheaper than the original. */ 4810 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y), 4811 dstmode, speed); 4812 if (oldcost < newcost) 4813 continue; 4814 } 4815 else if (float_extend_from_mem[dstmode][srcmode]) 4816 { 4817 trunc_y = force_const_mem (srcmode, trunc_y); 4818 /* This is valid, but may not be cheaper than the original. */ 4819 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y), 4820 dstmode, speed); 4821 if (oldcost < newcost) 4822 continue; 4823 trunc_y = validize_mem (trunc_y); 4824 } 4825 else 4826 continue; 4827 4828 /* For CSE's benefit, force the compressed constant pool entry 4829 into a new pseudo. This constant may be used in different modes, 4830 and if not, combine will put things back together for us. */ 4831 trunc_y = force_reg (srcmode, trunc_y); 4832 4833 /* If x is a hard register, perform the extension into a pseudo, 4834 so that e.g. stack realignment code is aware of it. */ 4835 rtx target = x; 4836 if (REG_P (x) && HARD_REGISTER_P (x)) 4837 target = gen_reg_rtx (dstmode); 4838 4839 emit_unop_insn (ic, target, trunc_y, UNKNOWN); 4840 last_insn = get_last_insn (); 4841 4842 if (REG_P (target)) 4843 set_unique_reg_note (last_insn, REG_EQUAL, y); 4844 4845 if (target != x) 4846 return emit_move_insn (x, target); 4847 return last_insn; 4848 } 4849 4850 return NULL; 4851 } 4852 4853 /* Pushing data onto the stack. */ 4855 4856 /* Push a block of length SIZE (perhaps variable) 4857 and return an rtx to address the beginning of the block. 4858 The value may be virtual_outgoing_args_rtx. 4859 4860 EXTRA is the number of bytes of padding to push in addition to SIZE. 4861 BELOW nonzero means this padding comes at low addresses; 4862 otherwise, the padding comes at high addresses. */ 4863 4864 rtx 4865 push_block (rtx size, poly_int64 extra, int below) 4866 { 4867 rtx temp; 4868 4869 size = convert_modes (Pmode, ptr_mode, size, 1); 4870 if (CONSTANT_P (size)) 4871 anti_adjust_stack (plus_constant (Pmode, size, extra)); 4872 else if (REG_P (size) && known_eq (extra, 0)) 4873 anti_adjust_stack (size); 4874 else 4875 { 4876 temp = copy_to_mode_reg (Pmode, size); 4877 if (maybe_ne (extra, 0)) 4878 temp = expand_binop (Pmode, add_optab, temp, 4879 gen_int_mode (extra, Pmode), 4880 temp, 0, OPTAB_LIB_WIDEN); 4881 anti_adjust_stack (temp); 4882 } 4883 4884 if (STACK_GROWS_DOWNWARD) 4885 { 4886 temp = virtual_outgoing_args_rtx; 4887 if (maybe_ne (extra, 0) && below) 4888 temp = plus_constant (Pmode, temp, extra); 4889 } 4890 else 4891 { 4892 poly_int64 csize; 4893 if (poly_int_rtx_p (size, &csize)) 4894 temp = plus_constant (Pmode, virtual_outgoing_args_rtx, 4895 -csize - (below ? 0 : extra)); 4896 else if (maybe_ne (extra, 0) && !below) 4897 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx, 4898 negate_rtx (Pmode, plus_constant (Pmode, size, 4899 extra))); 4900 else 4901 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx, 4902 negate_rtx (Pmode, size)); 4903 } 4904 4905 return memory_address (NARROWEST_INT_MODE, temp); 4906 } 4907 4908 /* A utility routine that returns the base of an auto-inc memory, or NULL. */ 4909 4910 static rtx 4911 mem_autoinc_base (rtx mem) 4912 { 4913 if (MEM_P (mem)) 4914 { 4915 rtx addr = XEXP (mem, 0); 4916 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC) 4917 return XEXP (addr, 0); 4918 } 4919 return NULL; 4920 } 4921 4922 /* A utility routine used here, in reload, and in try_split. The insns 4923 after PREV up to and including LAST are known to adjust the stack, 4924 with a final value of END_ARGS_SIZE. Iterate backward from LAST 4925 placing notes as appropriate. PREV may be NULL, indicating the 4926 entire insn sequence prior to LAST should be scanned. 4927 4928 The set of allowed stack pointer modifications is small: 4929 (1) One or more auto-inc style memory references (aka pushes), 4930 (2) One or more addition/subtraction with the SP as destination, 4931 (3) A single move insn with the SP as destination, 4932 (4) A call_pop insn, 4933 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS. 4934 4935 Insns in the sequence that do not modify the SP are ignored, 4936 except for noreturn calls. 4937 4938 The return value is the amount of adjustment that can be trivially 4939 verified, via immediate operand or auto-inc. If the adjustment 4940 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */ 4941 4942 poly_int64 4943 find_args_size_adjust (rtx_insn *insn) 4944 { 4945 rtx dest, set, pat; 4946 int i; 4947 4948 pat = PATTERN (insn); 4949 set = NULL; 4950 4951 /* Look for a call_pop pattern. */ 4952 if (CALL_P (insn)) 4953 { 4954 /* We have to allow non-call_pop patterns for the case 4955 of emit_single_push_insn of a TLS address. */ 4956 if (GET_CODE (pat) != PARALLEL) 4957 return 0; 4958 4959 /* All call_pop have a stack pointer adjust in the parallel. 4960 The call itself is always first, and the stack adjust is 4961 usually last, so search from the end. */ 4962 for (i = XVECLEN (pat, 0) - 1; i > 0; --i) 4963 { 4964 set = XVECEXP (pat, 0, i); 4965 if (GET_CODE (set) != SET) 4966 continue; 4967 dest = SET_DEST (set); 4968 if (dest == stack_pointer_rtx) 4969 break; 4970 } 4971 /* We'd better have found the stack pointer adjust. */ 4972 if (i == 0) 4973 return 0; 4974 /* Fall through to process the extracted SET and DEST 4975 as if it was a standalone insn. */ 4976 } 4977 else if (GET_CODE (pat) == SET) 4978 set = pat; 4979 else if ((set = single_set (insn)) != NULL) 4980 ; 4981 else if (GET_CODE (pat) == PARALLEL) 4982 { 4983 /* ??? Some older ports use a parallel with a stack adjust 4984 and a store for a PUSH_ROUNDING pattern, rather than a 4985 PRE/POST_MODIFY rtx. Don't force them to update yet... */ 4986 /* ??? See h8300 and m68k, pushqi1. */ 4987 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i) 4988 { 4989 set = XVECEXP (pat, 0, i); 4990 if (GET_CODE (set) != SET) 4991 continue; 4992 dest = SET_DEST (set); 4993 if (dest == stack_pointer_rtx) 4994 break; 4995 4996 /* We do not expect an auto-inc of the sp in the parallel. */ 4997 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx); 4998 gcc_checking_assert (mem_autoinc_base (SET_SRC (set)) 4999 != stack_pointer_rtx); 5000 } 5001 if (i < 0) 5002 return 0; 5003 } 5004 else 5005 return 0; 5006 5007 dest = SET_DEST (set); 5008 5009 /* Look for direct modifications of the stack pointer. */ 5010 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM) 5011 { 5012 /* Look for a trivial adjustment, otherwise assume nothing. */ 5013 /* Note that the SPU restore_stack_block pattern refers to 5014 the stack pointer in V4SImode. Consider that non-trivial. */ 5015 poly_int64 offset; 5016 if (SCALAR_INT_MODE_P (GET_MODE (dest)) 5017 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx) 5018 return offset; 5019 /* ??? Reload can generate no-op moves, which will be cleaned 5020 up later. Recognize it and continue searching. */ 5021 else if (rtx_equal_p (dest, SET_SRC (set))) 5022 return 0; 5023 else 5024 return HOST_WIDE_INT_MIN; 5025 } 5026 else 5027 { 5028 rtx mem, addr; 5029 5030 /* Otherwise only think about autoinc patterns. */ 5031 if (mem_autoinc_base (dest) == stack_pointer_rtx) 5032 { 5033 mem = dest; 5034 gcc_checking_assert (mem_autoinc_base (SET_SRC (set)) 5035 != stack_pointer_rtx); 5036 } 5037 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx) 5038 mem = SET_SRC (set); 5039 else 5040 return 0; 5041 5042 addr = XEXP (mem, 0); 5043 switch (GET_CODE (addr)) 5044 { 5045 case PRE_INC: 5046 case POST_INC: 5047 return GET_MODE_SIZE (GET_MODE (mem)); 5048 case PRE_DEC: 5049 case POST_DEC: 5050 return -GET_MODE_SIZE (GET_MODE (mem)); 5051 case PRE_MODIFY: 5052 case POST_MODIFY: 5053 addr = XEXP (addr, 1); 5054 gcc_assert (GET_CODE (addr) == PLUS); 5055 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx); 5056 return rtx_to_poly_int64 (XEXP (addr, 1)); 5057 default: 5058 gcc_unreachable (); 5059 } 5060 } 5061 } 5062 5063 poly_int64 5064 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, 5065 poly_int64 end_args_size) 5066 { 5067 poly_int64 args_size = end_args_size; 5068 bool saw_unknown = false; 5069 rtx_insn *insn; 5070 5071 for (insn = last; insn != prev; insn = PREV_INSN (insn)) 5072 { 5073 if (!NONDEBUG_INSN_P (insn)) 5074 continue; 5075 5076 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing 5077 a call argument containing a TLS address that itself requires 5078 a call to __tls_get_addr. The handling of stack_pointer_delta 5079 in emit_single_push_insn is supposed to ensure that any such 5080 notes are already correct. */ 5081 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX); 5082 gcc_assert (!note || known_eq (args_size, get_args_size (note))); 5083 5084 poly_int64 this_delta = find_args_size_adjust (insn); 5085 if (known_eq (this_delta, 0)) 5086 { 5087 if (!CALL_P (insn) 5088 || ACCUMULATE_OUTGOING_ARGS 5089 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX) 5090 continue; 5091 } 5092 5093 gcc_assert (!saw_unknown); 5094 if (known_eq (this_delta, HOST_WIDE_INT_MIN)) 5095 saw_unknown = true; 5096 5097 if (!note) 5098 add_args_size_note (insn, args_size); 5099 if (STACK_GROWS_DOWNWARD) 5100 this_delta = -poly_uint64 (this_delta); 5101 5102 if (saw_unknown) 5103 args_size = HOST_WIDE_INT_MIN; 5104 else 5105 args_size -= this_delta; 5106 } 5107 5108 return args_size; 5109 } 5110 5111 #ifdef PUSH_ROUNDING 5112 /* Emit single push insn. */ 5113 5114 static void 5115 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type) 5116 { 5117 rtx dest_addr; 5118 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode)); 5119 rtx dest; 5120 enum insn_code icode; 5121 5122 /* If there is push pattern, use it. Otherwise try old way of throwing 5123 MEM representing push operation to move expander. */ 5124 icode = optab_handler (push_optab, mode); 5125 if (icode != CODE_FOR_nothing) 5126 { 5127 class expand_operand ops[1]; 5128 5129 create_input_operand (&ops[0], x, mode); 5130 if (maybe_expand_insn (icode, 1, ops)) 5131 return; 5132 } 5133 if (known_eq (GET_MODE_SIZE (mode), rounded_size)) 5134 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx); 5135 /* If we are to pad downward, adjust the stack pointer first and 5136 then store X into the stack location using an offset. This is 5137 because emit_move_insn does not know how to pad; it does not have 5138 access to type. */ 5139 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD) 5140 { 5141 emit_move_insn (stack_pointer_rtx, 5142 expand_binop (Pmode, 5143 STACK_GROWS_DOWNWARD ? sub_optab 5144 : add_optab, 5145 stack_pointer_rtx, 5146 gen_int_mode (rounded_size, Pmode), 5147 NULL_RTX, 0, OPTAB_LIB_WIDEN)); 5148 5149 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode); 5150 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC) 5151 /* We have already decremented the stack pointer, so get the 5152 previous value. */ 5153 offset += rounded_size; 5154 5155 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC) 5156 /* We have already incremented the stack pointer, so get the 5157 previous value. */ 5158 offset -= rounded_size; 5159 5160 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset); 5161 } 5162 else 5163 { 5164 if (STACK_GROWS_DOWNWARD) 5165 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */ 5166 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size); 5167 else 5168 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */ 5169 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size); 5170 5171 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr); 5172 } 5173 5174 dest = gen_rtx_MEM (mode, dest_addr); 5175 5176 if (type != 0) 5177 { 5178 set_mem_attributes (dest, type, 1); 5179 5180 if (cfun->tail_call_marked) 5181 /* Function incoming arguments may overlap with sibling call 5182 outgoing arguments and we cannot allow reordering of reads 5183 from function arguments with stores to outgoing arguments 5184 of sibling calls. */ 5185 set_mem_alias_set (dest, 0); 5186 } 5187 emit_move_insn (dest, x); 5188 } 5189 5190 /* Emit and annotate a single push insn. */ 5191 5192 static void 5193 emit_single_push_insn (machine_mode mode, rtx x, tree type) 5194 { 5195 poly_int64 delta, old_delta = stack_pointer_delta; 5196 rtx_insn *prev = get_last_insn (); 5197 rtx_insn *last; 5198 5199 emit_single_push_insn_1 (mode, x, type); 5200 5201 /* Adjust stack_pointer_delta to describe the situation after the push 5202 we just performed. Note that we must do this after the push rather 5203 than before the push in case calculating X needs pushes and pops of 5204 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes 5205 for such pushes and pops must not include the effect of the future 5206 push of X. */ 5207 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode)); 5208 5209 last = get_last_insn (); 5210 5211 /* Notice the common case where we emitted exactly one insn. */ 5212 if (PREV_INSN (last) == prev) 5213 { 5214 add_args_size_note (last, stack_pointer_delta); 5215 return; 5216 } 5217 5218 delta = fixup_args_size_notes (prev, last, stack_pointer_delta); 5219 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN) 5220 || known_eq (delta, old_delta)); 5221 } 5222 #endif 5223 5224 /* If reading SIZE bytes from X will end up reading from 5225 Y return the number of bytes that overlap. Return -1 5226 if there is no overlap or -2 if we can't determine 5227 (for example when X and Y have different base registers). */ 5228 5229 static int 5230 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size) 5231 { 5232 rtx tmp = plus_constant (Pmode, x, size); 5233 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y); 5234 5235 if (!CONST_INT_P (sub)) 5236 return -2; 5237 5238 HOST_WIDE_INT val = INTVAL (sub); 5239 5240 return IN_RANGE (val, 1, size) ? val : -1; 5241 } 5242 5243 /* Generate code to push X onto the stack, assuming it has mode MODE and 5244 type TYPE. 5245 MODE is redundant except when X is a CONST_INT (since they don't 5246 carry mode info). 5247 SIZE is an rtx for the size of data to be copied (in bytes), 5248 needed only if X is BLKmode. 5249 Return true if successful. May return false if asked to push a 5250 partial argument during a sibcall optimization (as specified by 5251 SIBCALL_P) and the incoming and outgoing pointers cannot be shown 5252 to not overlap. 5253 5254 ALIGN (in bits) is maximum alignment we can assume. 5255 5256 If PARTIAL and REG are both nonzero, then copy that many of the first 5257 bytes of X into registers starting with REG, and push the rest of X. 5258 The amount of space pushed is decreased by PARTIAL bytes. 5259 REG must be a hard register in this case. 5260 If REG is zero but PARTIAL is not, take any all others actions for an 5261 argument partially in registers, but do not actually load any 5262 registers. 5263 5264 EXTRA is the amount in bytes of extra space to leave next to this arg. 5265 This is ignored if an argument block has already been allocated. 5266 5267 On a machine that lacks real push insns, ARGS_ADDR is the address of 5268 the bottom of the argument block for this call. We use indexing off there 5269 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a 5270 argument block has not been preallocated. 5271 5272 ARGS_SO_FAR is the size of args previously pushed for this call. 5273 5274 REG_PARM_STACK_SPACE is nonzero if functions require stack space 5275 for arguments passed in registers. If nonzero, it will be the number 5276 of bytes required. */ 5277 5278 bool 5279 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size, 5280 unsigned int align, int partial, rtx reg, poly_int64 extra, 5281 rtx args_addr, rtx args_so_far, int reg_parm_stack_space, 5282 rtx alignment_pad, bool sibcall_p) 5283 { 5284 rtx xinner; 5285 pad_direction stack_direction 5286 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD; 5287 5288 /* Decide where to pad the argument: PAD_DOWNWARD for below, 5289 PAD_UPWARD for above, or PAD_NONE for don't pad it. 5290 Default is below for small data on big-endian machines; else above. */ 5291 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type); 5292 5293 /* Invert direction if stack is post-decrement. 5294 FIXME: why? */ 5295 if (STACK_PUSH_CODE == POST_DEC) 5296 if (where_pad != PAD_NONE) 5297 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD); 5298 5299 xinner = x; 5300 5301 int nregs = partial / UNITS_PER_WORD; 5302 rtx *tmp_regs = NULL; 5303 int overlapping = 0; 5304 5305 if (mode == BLKmode 5306 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode) 5307 && type != NULL_TREE)) 5308 { 5309 /* Copy a block into the stack, entirely or partially. */ 5310 5311 rtx temp; 5312 int used; 5313 int offset; 5314 int skip; 5315 5316 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT); 5317 used = partial - offset; 5318 5319 if (mode != BLKmode) 5320 { 5321 /* A value is to be stored in an insufficiently aligned 5322 stack slot; copy via a suitably aligned slot if 5323 necessary. */ 5324 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode); 5325 if (!MEM_P (xinner)) 5326 { 5327 temp = assign_temp (type, 1, 1); 5328 emit_move_insn (temp, xinner); 5329 xinner = temp; 5330 } 5331 } 5332 5333 gcc_assert (size); 5334 5335 /* USED is now the # of bytes we need not copy to the stack 5336 because registers will take care of them. */ 5337 5338 if (partial != 0) 5339 xinner = adjust_address (xinner, BLKmode, used); 5340 5341 /* If the partial register-part of the arg counts in its stack size, 5342 skip the part of stack space corresponding to the registers. 5343 Otherwise, start copying to the beginning of the stack space, 5344 by setting SKIP to 0. */ 5345 skip = (reg_parm_stack_space == 0) ? 0 : used; 5346 5347 #ifdef PUSH_ROUNDING 5348 /* NB: Let the backend known the number of bytes to push and 5349 decide if push insns should be generated. */ 5350 unsigned int push_size; 5351 if (CONST_INT_P (size)) 5352 push_size = INTVAL (size); 5353 else 5354 push_size = 0; 5355 5356 /* Do it with several push insns if that doesn't take lots of insns 5357 and if there is no difficulty with push insns that skip bytes 5358 on the stack for alignment purposes. */ 5359 if (args_addr == 0 5360 && targetm.calls.push_argument (push_size) 5361 && CONST_INT_P (size) 5362 && skip == 0 5363 && MEM_ALIGN (xinner) >= align 5364 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align) 5365 /* Here we avoid the case of a structure whose weak alignment 5366 forces many pushes of a small amount of data, 5367 and such small pushes do rounding that causes trouble. */ 5368 && ((!targetm.slow_unaligned_access (word_mode, align)) 5369 || align >= BIGGEST_ALIGNMENT 5370 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT), 5371 align / BITS_PER_UNIT)) 5372 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size))) 5373 { 5374 /* Push padding now if padding above and stack grows down, 5375 or if padding below and stack grows up. 5376 But if space already allocated, this has already been done. */ 5377 if (maybe_ne (extra, 0) 5378 && args_addr == 0 5379 && where_pad != PAD_NONE 5380 && where_pad != stack_direction) 5381 anti_adjust_stack (gen_int_mode (extra, Pmode)); 5382 5383 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 5384 RETURN_BEGIN); 5385 } 5386 else 5387 #endif /* PUSH_ROUNDING */ 5388 { 5389 rtx target; 5390 5391 /* Otherwise make space on the stack and copy the data 5392 to the address of that space. */ 5393 5394 /* Deduct words put into registers from the size we must copy. */ 5395 if (partial != 0) 5396 { 5397 if (CONST_INT_P (size)) 5398 size = GEN_INT (INTVAL (size) - used); 5399 else 5400 size = expand_binop (GET_MODE (size), sub_optab, size, 5401 gen_int_mode (used, GET_MODE (size)), 5402 NULL_RTX, 0, OPTAB_LIB_WIDEN); 5403 } 5404 5405 /* Get the address of the stack space. 5406 In this case, we do not deal with EXTRA separately. 5407 A single stack adjust will do. */ 5408 poly_int64 const_args_so_far; 5409 if (! args_addr) 5410 { 5411 temp = push_block (size, extra, where_pad == PAD_DOWNWARD); 5412 extra = 0; 5413 } 5414 else if (poly_int_rtx_p (args_so_far, &const_args_so_far)) 5415 temp = memory_address (BLKmode, 5416 plus_constant (Pmode, args_addr, 5417 skip + const_args_so_far)); 5418 else 5419 temp = memory_address (BLKmode, 5420 plus_constant (Pmode, 5421 gen_rtx_PLUS (Pmode, 5422 args_addr, 5423 args_so_far), 5424 skip)); 5425 5426 if (!ACCUMULATE_OUTGOING_ARGS) 5427 { 5428 /* If the source is referenced relative to the stack pointer, 5429 copy it to another register to stabilize it. We do not need 5430 to do this if we know that we won't be changing sp. */ 5431 5432 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp) 5433 || reg_mentioned_p (virtual_outgoing_args_rtx, temp)) 5434 temp = copy_to_reg (temp); 5435 } 5436 5437 target = gen_rtx_MEM (BLKmode, temp); 5438 5439 /* We do *not* set_mem_attributes here, because incoming arguments 5440 may overlap with sibling call outgoing arguments and we cannot 5441 allow reordering of reads from function arguments with stores 5442 to outgoing arguments of sibling calls. We do, however, want 5443 to record the alignment of the stack slot. */ 5444 /* ALIGN may well be better aligned than TYPE, e.g. due to 5445 PARM_BOUNDARY. Assume the caller isn't lying. */ 5446 set_mem_align (target, align); 5447 5448 /* If part should go in registers and pushing to that part would 5449 overwrite some of the values that need to go into regs, load the 5450 overlapping values into temporary pseudos to be moved into the hard 5451 regs at the end after the stack pushing has completed. 5452 We cannot load them directly into the hard regs here because 5453 they can be clobbered by the block move expansions. 5454 See PR 65358. */ 5455 5456 if (partial > 0 && reg != 0 && mode == BLKmode 5457 && GET_CODE (reg) != PARALLEL) 5458 { 5459 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial); 5460 if (overlapping > 0) 5461 { 5462 gcc_assert (overlapping % UNITS_PER_WORD == 0); 5463 overlapping /= UNITS_PER_WORD; 5464 5465 tmp_regs = XALLOCAVEC (rtx, overlapping); 5466 5467 for (int i = 0; i < overlapping; i++) 5468 tmp_regs[i] = gen_reg_rtx (word_mode); 5469 5470 for (int i = 0; i < overlapping; i++) 5471 emit_move_insn (tmp_regs[i], 5472 operand_subword_force (target, i, mode)); 5473 } 5474 else if (overlapping == -1) 5475 overlapping = 0; 5476 /* Could not determine whether there is overlap. 5477 Fail the sibcall. */ 5478 else 5479 { 5480 overlapping = 0; 5481 if (sibcall_p) 5482 return false; 5483 } 5484 } 5485 5486 /* If source is a constant VAR_DECL with a simple constructor, 5487 store the constructor to the stack instead of moving it. */ 5488 const_tree decl; 5489 HOST_WIDE_INT sz; 5490 if (partial == 0 5491 && MEM_P (xinner) 5492 && SYMBOL_REF_P (XEXP (xinner, 0)) 5493 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE 5494 && VAR_P (decl) 5495 && TREE_READONLY (decl) 5496 && !TREE_SIDE_EFFECTS (decl) 5497 && immediate_const_ctor_p (DECL_INITIAL (decl), 2) 5498 && (sz = int_expr_size (DECL_INITIAL (decl))) > 0 5499 && CONST_INT_P (size) 5500 && INTVAL (size) == sz) 5501 store_constructor (DECL_INITIAL (decl), target, 0, sz, false); 5502 else 5503 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM); 5504 } 5505 } 5506 else if (partial > 0) 5507 { 5508 /* Scalar partly in registers. This case is only supported 5509 for fixed-wdth modes. */ 5510 int num_words = GET_MODE_SIZE (mode).to_constant (); 5511 num_words /= UNITS_PER_WORD; 5512 int i; 5513 int not_stack; 5514 /* # bytes of start of argument 5515 that we must make space for but need not store. */ 5516 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT); 5517 int args_offset = INTVAL (args_so_far); 5518 int skip; 5519 5520 /* Push padding now if padding above and stack grows down, 5521 or if padding below and stack grows up. 5522 But if space already allocated, this has already been done. */ 5523 if (maybe_ne (extra, 0) 5524 && args_addr == 0 5525 && where_pad != PAD_NONE 5526 && where_pad != stack_direction) 5527 anti_adjust_stack (gen_int_mode (extra, Pmode)); 5528 5529 /* If we make space by pushing it, we might as well push 5530 the real data. Otherwise, we can leave OFFSET nonzero 5531 and leave the space uninitialized. */ 5532 if (args_addr == 0) 5533 offset = 0; 5534 5535 /* Now NOT_STACK gets the number of words that we don't need to 5536 allocate on the stack. Convert OFFSET to words too. */ 5537 not_stack = (partial - offset) / UNITS_PER_WORD; 5538 offset /= UNITS_PER_WORD; 5539 5540 /* If the partial register-part of the arg counts in its stack size, 5541 skip the part of stack space corresponding to the registers. 5542 Otherwise, start copying to the beginning of the stack space, 5543 by setting SKIP to 0. */ 5544 skip = (reg_parm_stack_space == 0) ? 0 : not_stack; 5545 5546 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x)) 5547 x = validize_mem (force_const_mem (mode, x)); 5548 5549 /* If X is a hard register in a non-integer mode, copy it into a pseudo; 5550 SUBREGs of such registers are not allowed. */ 5551 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER 5552 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT)) 5553 x = copy_to_reg (x); 5554 5555 /* Loop over all the words allocated on the stack for this arg. */ 5556 /* We can do it by words, because any scalar bigger than a word 5557 has a size a multiple of a word. */ 5558 tree word_mode_type = lang_hooks.types.type_for_mode (word_mode, 1); 5559 for (i = num_words - 1; i >= not_stack; i--) 5560 if (i >= not_stack + offset) 5561 if (!emit_push_insn (operand_subword_force (x, i, mode), 5562 word_mode, word_mode_type, NULL_RTX, align, 0, 5563 NULL_RTX, 0, args_addr, 5564 GEN_INT (args_offset + ((i - not_stack + skip) 5565 * UNITS_PER_WORD)), 5566 reg_parm_stack_space, alignment_pad, sibcall_p)) 5567 return false; 5568 } 5569 else 5570 { 5571 rtx addr; 5572 rtx dest; 5573 5574 /* Push padding now if padding above and stack grows down, 5575 or if padding below and stack grows up. 5576 But if space already allocated, this has already been done. */ 5577 if (maybe_ne (extra, 0) 5578 && args_addr == 0 5579 && where_pad != PAD_NONE 5580 && where_pad != stack_direction) 5581 anti_adjust_stack (gen_int_mode (extra, Pmode)); 5582 5583 #ifdef PUSH_ROUNDING 5584 if (args_addr == 0 && targetm.calls.push_argument (0)) 5585 emit_single_push_insn (mode, x, type); 5586 else 5587 #endif 5588 { 5589 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far); 5590 dest = gen_rtx_MEM (mode, memory_address (mode, addr)); 5591 5592 /* We do *not* set_mem_attributes here, because incoming arguments 5593 may overlap with sibling call outgoing arguments and we cannot 5594 allow reordering of reads from function arguments with stores 5595 to outgoing arguments of sibling calls. We do, however, want 5596 to record the alignment of the stack slot. */ 5597 /* ALIGN may well be better aligned than TYPE, e.g. due to 5598 PARM_BOUNDARY. Assume the caller isn't lying. */ 5599 set_mem_align (dest, align); 5600 5601 emit_move_insn (dest, x); 5602 } 5603 } 5604 5605 /* Move the partial arguments into the registers and any overlapping 5606 values that we moved into the pseudos in tmp_regs. */ 5607 if (partial > 0 && reg != 0) 5608 { 5609 /* Handle calls that pass values in multiple non-contiguous locations. 5610 The Irix 6 ABI has examples of this. */ 5611 if (GET_CODE (reg) == PARALLEL) 5612 emit_group_load (reg, x, type, -1); 5613 else 5614 { 5615 gcc_assert (partial % UNITS_PER_WORD == 0); 5616 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode); 5617 5618 for (int i = 0; i < overlapping; i++) 5619 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) 5620 + nregs - overlapping + i), 5621 tmp_regs[i]); 5622 5623 } 5624 } 5625 5626 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction) 5627 anti_adjust_stack (gen_int_mode (extra, Pmode)); 5628 5629 if (alignment_pad && args_addr == 0) 5630 anti_adjust_stack (alignment_pad); 5631 5632 return true; 5633 } 5634 5635 /* Return X if X can be used as a subtarget in a sequence of arithmetic 5637 operations. */ 5638 5639 static rtx 5640 get_subtarget (rtx x) 5641 { 5642 return (optimize 5643 || x == 0 5644 /* Only registers can be subtargets. */ 5645 || !REG_P (x) 5646 /* Don't use hard regs to avoid extending their life. */ 5647 || REGNO (x) < FIRST_PSEUDO_REGISTER 5648 ? 0 : x); 5649 } 5650 5651 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where 5652 FIELD is a bitfield. Returns true if the optimization was successful, 5653 and there's nothing else to do. */ 5654 5655 static bool 5656 optimize_bitfield_assignment_op (poly_uint64 pbitsize, 5657 poly_uint64 pbitpos, 5658 poly_uint64 pbitregion_start, 5659 poly_uint64 pbitregion_end, 5660 machine_mode mode1, rtx str_rtx, 5661 tree to, tree src, bool reverse) 5662 { 5663 /* str_mode is not guaranteed to be a scalar type. */ 5664 machine_mode str_mode = GET_MODE (str_rtx); 5665 unsigned int str_bitsize; 5666 tree op0, op1; 5667 rtx value, result; 5668 optab binop; 5669 gimple *srcstmt; 5670 enum tree_code code; 5671 5672 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end; 5673 if (mode1 != VOIDmode 5674 || !pbitsize.is_constant (&bitsize) 5675 || !pbitpos.is_constant (&bitpos) 5676 || !pbitregion_start.is_constant (&bitregion_start) 5677 || !pbitregion_end.is_constant (&bitregion_end) 5678 || bitsize >= BITS_PER_WORD 5679 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize) 5680 || str_bitsize > BITS_PER_WORD 5681 || TREE_SIDE_EFFECTS (to) 5682 || TREE_THIS_VOLATILE (to)) 5683 return false; 5684 5685 STRIP_NOPS (src); 5686 if (TREE_CODE (src) != SSA_NAME) 5687 return false; 5688 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE) 5689 return false; 5690 5691 srcstmt = get_gimple_for_ssa_name (src); 5692 if (!srcstmt 5693 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary) 5694 return false; 5695 5696 code = gimple_assign_rhs_code (srcstmt); 5697 5698 op0 = gimple_assign_rhs1 (srcstmt); 5699 5700 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain 5701 to find its initialization. Hopefully the initialization will 5702 be from a bitfield load. */ 5703 if (TREE_CODE (op0) == SSA_NAME) 5704 { 5705 gimple *op0stmt = get_gimple_for_ssa_name (op0); 5706 5707 /* We want to eventually have OP0 be the same as TO, which 5708 should be a bitfield. */ 5709 if (!op0stmt 5710 || !is_gimple_assign (op0stmt) 5711 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to)) 5712 return false; 5713 op0 = gimple_assign_rhs1 (op0stmt); 5714 } 5715 5716 op1 = gimple_assign_rhs2 (srcstmt); 5717 5718 if (!operand_equal_p (to, op0, 0)) 5719 return false; 5720 5721 if (MEM_P (str_rtx)) 5722 { 5723 unsigned HOST_WIDE_INT offset1; 5724 5725 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD) 5726 str_bitsize = BITS_PER_WORD; 5727 5728 scalar_int_mode best_mode; 5729 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end, 5730 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode)) 5731 return false; 5732 str_mode = best_mode; 5733 str_bitsize = GET_MODE_BITSIZE (best_mode); 5734 5735 offset1 = bitpos; 5736 bitpos %= str_bitsize; 5737 offset1 = (offset1 - bitpos) / BITS_PER_UNIT; 5738 str_rtx = adjust_address (str_rtx, str_mode, offset1); 5739 } 5740 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG) 5741 return false; 5742 5743 /* If the bit field covers the whole REG/MEM, store_field 5744 will likely generate better code. */ 5745 if (bitsize >= str_bitsize) 5746 return false; 5747 5748 /* We can't handle fields split across multiple entities. */ 5749 if (bitpos + bitsize > str_bitsize) 5750 return false; 5751 5752 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN) 5753 bitpos = str_bitsize - bitpos - bitsize; 5754 5755 switch (code) 5756 { 5757 case PLUS_EXPR: 5758 case MINUS_EXPR: 5759 /* For now, just optimize the case of the topmost bitfield 5760 where we don't need to do any masking and also 5761 1 bit bitfields where xor can be used. 5762 We might win by one instruction for the other bitfields 5763 too if insv/extv instructions aren't used, so that 5764 can be added later. */ 5765 if ((reverse || bitpos + bitsize != str_bitsize) 5766 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST)) 5767 break; 5768 5769 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL); 5770 value = convert_modes (str_mode, 5771 TYPE_MODE (TREE_TYPE (op1)), value, 5772 TYPE_UNSIGNED (TREE_TYPE (op1))); 5773 5774 /* We may be accessing data outside the field, which means 5775 we can alias adjacent data. */ 5776 if (MEM_P (str_rtx)) 5777 { 5778 str_rtx = shallow_copy_rtx (str_rtx); 5779 set_mem_alias_set (str_rtx, 0); 5780 set_mem_expr (str_rtx, 0); 5781 } 5782 5783 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize)) 5784 { 5785 value = expand_and (str_mode, value, const1_rtx, NULL); 5786 binop = xor_optab; 5787 } 5788 else 5789 binop = code == PLUS_EXPR ? add_optab : sub_optab; 5790 5791 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1); 5792 if (reverse) 5793 value = flip_storage_order (str_mode, value); 5794 result = expand_binop (str_mode, binop, str_rtx, 5795 value, str_rtx, 1, OPTAB_WIDEN); 5796 if (result != str_rtx) 5797 emit_move_insn (str_rtx, result); 5798 return true; 5799 5800 case BIT_IOR_EXPR: 5801 case BIT_XOR_EXPR: 5802 if (TREE_CODE (op1) != INTEGER_CST) 5803 break; 5804 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL); 5805 value = convert_modes (str_mode, 5806 TYPE_MODE (TREE_TYPE (op1)), value, 5807 TYPE_UNSIGNED (TREE_TYPE (op1))); 5808 5809 /* We may be accessing data outside the field, which means 5810 we can alias adjacent data. */ 5811 if (MEM_P (str_rtx)) 5812 { 5813 str_rtx = shallow_copy_rtx (str_rtx); 5814 set_mem_alias_set (str_rtx, 0); 5815 set_mem_expr (str_rtx, 0); 5816 } 5817 5818 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab; 5819 if (bitpos + bitsize != str_bitsize) 5820 { 5821 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1, 5822 str_mode); 5823 value = expand_and (str_mode, value, mask, NULL_RTX); 5824 } 5825 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1); 5826 if (reverse) 5827 value = flip_storage_order (str_mode, value); 5828 result = expand_binop (str_mode, binop, str_rtx, 5829 value, str_rtx, 1, OPTAB_WIDEN); 5830 if (result != str_rtx) 5831 emit_move_insn (str_rtx, result); 5832 return true; 5833 5834 default: 5835 break; 5836 } 5837 5838 return false; 5839 } 5840 5841 /* In the C++ memory model, consecutive bit fields in a structure are 5842 considered one memory location. 5843 5844 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function 5845 returns the bit range of consecutive bits in which this COMPONENT_REF 5846 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS 5847 and *OFFSET may be adjusted in the process. 5848 5849 If the access does not need to be restricted, 0 is returned in both 5850 *BITSTART and *BITEND. */ 5851 5852 void 5853 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp, 5854 poly_int64 *bitpos, tree *offset) 5855 { 5856 poly_int64 bitoffset; 5857 tree field, repr; 5858 5859 gcc_assert (TREE_CODE (exp) == COMPONENT_REF); 5860 5861 field = TREE_OPERAND (exp, 1); 5862 repr = DECL_BIT_FIELD_REPRESENTATIVE (field); 5863 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no 5864 need to limit the range we can access. */ 5865 if (!repr) 5866 { 5867 *bitstart = *bitend = 0; 5868 return; 5869 } 5870 5871 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is 5872 part of a larger bit field, then the representative does not serve any 5873 useful purpose. This can occur in Ada. */ 5874 if (handled_component_p (TREE_OPERAND (exp, 0))) 5875 { 5876 machine_mode rmode; 5877 poly_int64 rbitsize, rbitpos; 5878 tree roffset; 5879 int unsignedp, reversep, volatilep = 0; 5880 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos, 5881 &roffset, &rmode, &unsignedp, &reversep, 5882 &volatilep); 5883 if (!multiple_p (rbitpos, BITS_PER_UNIT)) 5884 { 5885 *bitstart = *bitend = 0; 5886 return; 5887 } 5888 } 5889 5890 /* Compute the adjustment to bitpos from the offset of the field 5891 relative to the representative. DECL_FIELD_OFFSET of field and 5892 repr are the same by construction if they are not constants, 5893 see finish_bitfield_layout. */ 5894 poly_uint64 field_offset, repr_offset; 5895 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset) 5896 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset)) 5897 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT; 5898 else 5899 bitoffset = 0; 5900 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)) 5901 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr))); 5902 5903 /* If the adjustment is larger than bitpos, we would have a negative bit 5904 position for the lower bound and this may wreak havoc later. Adjust 5905 offset and bitpos to make the lower bound non-negative in that case. */ 5906 if (maybe_gt (bitoffset, *bitpos)) 5907 { 5908 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos; 5909 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT); 5910 5911 *bitpos += adjust_bits; 5912 if (*offset == NULL_TREE) 5913 *offset = size_int (-adjust_bytes); 5914 else 5915 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes)); 5916 *bitstart = 0; 5917 } 5918 else 5919 *bitstart = *bitpos - bitoffset; 5920 5921 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1; 5922 } 5923 5924 /* Returns true if BASE is a DECL that does not reside in memory and 5925 has non-BLKmode. DECL_RTL must not be a MEM; if 5926 DECL_RTL was not set yet, return false. */ 5927 5928 bool 5929 non_mem_decl_p (tree base) 5930 { 5931 if (!DECL_P (base) 5932 || TREE_ADDRESSABLE (base) 5933 || DECL_MODE (base) == BLKmode) 5934 return false; 5935 5936 if (!DECL_RTL_SET_P (base)) 5937 return false; 5938 5939 return (!MEM_P (DECL_RTL (base))); 5940 } 5941 5942 /* Returns true if REF refers to an object that does not 5943 reside in memory and has non-BLKmode. */ 5944 5945 bool 5946 mem_ref_refers_to_non_mem_p (tree ref) 5947 { 5948 tree base; 5949 5950 if (TREE_CODE (ref) == MEM_REF 5951 || TREE_CODE (ref) == TARGET_MEM_REF) 5952 { 5953 tree addr = TREE_OPERAND (ref, 0); 5954 5955 if (TREE_CODE (addr) != ADDR_EXPR) 5956 return false; 5957 5958 base = TREE_OPERAND (addr, 0); 5959 } 5960 else 5961 base = ref; 5962 5963 return non_mem_decl_p (base); 5964 } 5965 5966 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL 5967 is true, try generating a nontemporal store. */ 5968 5969 void 5970 expand_assignment (tree to, tree from, bool nontemporal) 5971 { 5972 rtx to_rtx = 0; 5973 rtx result; 5974 machine_mode mode; 5975 unsigned int align; 5976 enum insn_code icode; 5977 5978 /* Don't crash if the lhs of the assignment was erroneous. */ 5979 if (TREE_CODE (to) == ERROR_MARK) 5980 { 5981 expand_normal (from); 5982 return; 5983 } 5984 5985 /* Optimize away no-op moves without side-effects. */ 5986 if (operand_equal_p (to, from, 0)) 5987 return; 5988 5989 /* Handle misaligned stores. */ 5990 mode = TYPE_MODE (TREE_TYPE (to)); 5991 if ((TREE_CODE (to) == MEM_REF 5992 || TREE_CODE (to) == TARGET_MEM_REF 5993 || DECL_P (to)) 5994 && mode != BLKmode 5995 && !mem_ref_refers_to_non_mem_p (to) 5996 && ((align = get_object_alignment (to)) 5997 < GET_MODE_ALIGNMENT (mode)) 5998 && (((icode = optab_handler (movmisalign_optab, mode)) 5999 != CODE_FOR_nothing) 6000 || targetm.slow_unaligned_access (mode, align))) 6001 { 6002 rtx reg, mem; 6003 6004 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL); 6005 /* Handle PARALLEL. */ 6006 reg = maybe_emit_group_store (reg, TREE_TYPE (from)); 6007 reg = force_not_mem (reg); 6008 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE); 6009 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to)) 6010 reg = flip_storage_order (mode, reg); 6011 6012 if (icode != CODE_FOR_nothing) 6013 { 6014 class expand_operand ops[2]; 6015 6016 create_fixed_operand (&ops[0], mem); 6017 create_input_operand (&ops[1], reg, mode); 6018 /* The movmisalign<mode> pattern cannot fail, else the assignment 6019 would silently be omitted. */ 6020 expand_insn (icode, 2, ops); 6021 } 6022 else 6023 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg, 6024 false, false); 6025 return; 6026 } 6027 6028 /* Assignment of a structure component needs special treatment 6029 if the structure component's rtx is not simply a MEM. 6030 Assignment of an array element at a constant index, and assignment of 6031 an array element in an unaligned packed structure field, has the same 6032 problem. Same for (partially) storing into a non-memory object. */ 6033 if (handled_component_p (to) 6034 || (TREE_CODE (to) == MEM_REF 6035 && (REF_REVERSE_STORAGE_ORDER (to) 6036 || mem_ref_refers_to_non_mem_p (to))) 6037 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE) 6038 { 6039 machine_mode mode1; 6040 poly_int64 bitsize, bitpos; 6041 poly_uint64 bitregion_start = 0; 6042 poly_uint64 bitregion_end = 0; 6043 tree offset; 6044 int unsignedp, reversep, volatilep = 0; 6045 tree tem; 6046 6047 push_temp_slots (); 6048 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1, 6049 &unsignedp, &reversep, &volatilep); 6050 6051 /* Make sure bitpos is not negative, it can wreak havoc later. */ 6052 if (maybe_lt (bitpos, 0)) 6053 { 6054 gcc_assert (offset == NULL_TREE); 6055 offset = size_int (bits_to_bytes_round_down (bitpos)); 6056 bitpos = num_trailing_bits (bitpos); 6057 } 6058 6059 if (TREE_CODE (to) == COMPONENT_REF 6060 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1))) 6061 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset); 6062 /* The C++ memory model naturally applies to byte-aligned fields. 6063 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or 6064 BITSIZE are not byte-aligned, there is no need to limit the range 6065 we can access. This can occur with packed structures in Ada. */ 6066 else if (maybe_gt (bitsize, 0) 6067 && multiple_p (bitsize, BITS_PER_UNIT) 6068 && multiple_p (bitpos, BITS_PER_UNIT)) 6069 { 6070 bitregion_start = bitpos; 6071 bitregion_end = bitpos + bitsize - 1; 6072 } 6073 6074 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE); 6075 6076 /* If the field has a mode, we want to access it in the 6077 field's mode, not the computed mode. 6078 If a MEM has VOIDmode (external with incomplete type), 6079 use BLKmode for it instead. */ 6080 if (MEM_P (to_rtx)) 6081 { 6082 if (mode1 != VOIDmode) 6083 to_rtx = adjust_address (to_rtx, mode1, 0); 6084 else if (GET_MODE (to_rtx) == VOIDmode) 6085 to_rtx = adjust_address (to_rtx, BLKmode, 0); 6086 } 6087 6088 rtx stemp = NULL_RTX, old_to_rtx = NULL_RTX; 6089 if (offset != 0) 6090 { 6091 machine_mode address_mode; 6092 rtx offset_rtx; 6093 6094 if (!MEM_P (to_rtx)) 6095 { 6096 /* We can get constant negative offsets into arrays with broken 6097 user code. Translate this to a trap instead of ICEing. */ 6098 if (TREE_CODE (offset) == INTEGER_CST) 6099 { 6100 expand_builtin_trap (); 6101 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx); 6102 } 6103 /* Else spill for variable offset to the destination. We expect 6104 to run into this only for hard registers. */ 6105 else 6106 { 6107 gcc_assert (VAR_P (tem) && DECL_HARD_REGISTER (tem)); 6108 stemp = assign_stack_temp (GET_MODE (to_rtx), 6109 GET_MODE_SIZE (GET_MODE (to_rtx))); 6110 emit_move_insn (stemp, to_rtx); 6111 old_to_rtx = to_rtx; 6112 to_rtx = stemp; 6113 } 6114 } 6115 6116 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM); 6117 address_mode = get_address_mode (to_rtx); 6118 if (GET_MODE (offset_rtx) != address_mode) 6119 { 6120 /* We cannot be sure that the RTL in offset_rtx is valid outside 6121 of a memory address context, so force it into a register 6122 before attempting to convert it to the desired mode. */ 6123 offset_rtx = force_operand (offset_rtx, NULL_RTX); 6124 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0); 6125 } 6126 6127 /* If we have an expression in OFFSET_RTX and a non-zero 6128 byte offset in BITPOS, adding the byte offset before the 6129 OFFSET_RTX results in better intermediate code, which makes 6130 later rtl optimization passes perform better. 6131 6132 We prefer intermediate code like this: 6133 6134 r124:DI=r123:DI+0x18 6135 [r124:DI]=r121:DI 6136 6137 ... instead of ... 6138 6139 r124:DI=r123:DI+0x10 6140 [r124:DI+0x8]=r121:DI 6141 6142 This is only done for aligned data values, as these can 6143 be expected to result in single move instructions. */ 6144 poly_int64 bytepos; 6145 if (mode1 != VOIDmode 6146 && maybe_ne (bitpos, 0) 6147 && maybe_gt (bitsize, 0) 6148 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos) 6149 && multiple_p (bitpos, bitsize) 6150 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1)) 6151 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1)) 6152 { 6153 to_rtx = adjust_address (to_rtx, mode1, bytepos); 6154 bitregion_start = 0; 6155 if (known_ge (bitregion_end, poly_uint64 (bitpos))) 6156 bitregion_end -= bitpos; 6157 bitpos = 0; 6158 } 6159 6160 to_rtx = offset_address (to_rtx, offset_rtx, 6161 highest_pow2_factor_for_target (to, 6162 offset)); 6163 } 6164 6165 /* No action is needed if the target is not a memory and the field 6166 lies completely outside that target. This can occur if the source 6167 code contains an out-of-bounds access to a small array. */ 6168 if (!MEM_P (to_rtx) 6169 && GET_MODE (to_rtx) != BLKmode 6170 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx)))) 6171 { 6172 expand_normal (from); 6173 result = NULL; 6174 } 6175 /* Handle expand_expr of a complex value returning a CONCAT. */ 6176 else if (GET_CODE (to_rtx) == CONCAT) 6177 { 6178 machine_mode to_mode = GET_MODE (to_rtx); 6179 gcc_checking_assert (COMPLEX_MODE_P (to_mode)); 6180 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode); 6181 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode); 6182 if (TYPE_MODE (TREE_TYPE (from)) == to_mode 6183 && known_eq (bitpos, 0) 6184 && known_eq (bitsize, mode_bitsize)) 6185 result = store_expr (from, to_rtx, false, nontemporal, reversep); 6186 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode) 6187 && known_eq (bitsize, inner_bitsize) 6188 && (known_eq (bitpos, 0) 6189 || known_eq (bitpos, inner_bitsize))) 6190 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)), 6191 false, nontemporal, reversep); 6192 else if (known_le (bitpos + bitsize, inner_bitsize)) 6193 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos, 6194 bitregion_start, bitregion_end, 6195 mode1, from, get_alias_set (to), 6196 nontemporal, reversep); 6197 else if (known_ge (bitpos, inner_bitsize)) 6198 result = store_field (XEXP (to_rtx, 1), bitsize, 6199 bitpos - inner_bitsize, 6200 bitregion_start, bitregion_end, 6201 mode1, from, get_alias_set (to), 6202 nontemporal, reversep); 6203 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize)) 6204 { 6205 result = expand_normal (from); 6206 if (GET_CODE (result) == CONCAT) 6207 { 6208 to_mode = GET_MODE_INNER (to_mode); 6209 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result)); 6210 rtx from_real 6211 = simplify_gen_subreg (to_mode, XEXP (result, 0), 6212 from_mode, 0); 6213 rtx from_imag 6214 = simplify_gen_subreg (to_mode, XEXP (result, 1), 6215 from_mode, 0); 6216 if (!from_real || !from_imag) 6217 goto concat_store_slow; 6218 emit_move_insn (XEXP (to_rtx, 0), from_real); 6219 emit_move_insn (XEXP (to_rtx, 1), from_imag); 6220 } 6221 else 6222 { 6223 machine_mode from_mode 6224 = GET_MODE (result) == VOIDmode 6225 ? TYPE_MODE (TREE_TYPE (from)) 6226 : GET_MODE (result); 6227 rtx from_rtx; 6228 if (MEM_P (result)) 6229 from_rtx = change_address (result, to_mode, NULL_RTX); 6230 else 6231 from_rtx 6232 = simplify_gen_subreg (to_mode, result, from_mode, 0); 6233 if (from_rtx) 6234 { 6235 emit_move_insn (XEXP (to_rtx, 0), 6236 read_complex_part (from_rtx, false)); 6237 emit_move_insn (XEXP (to_rtx, 1), 6238 read_complex_part (from_rtx, true)); 6239 } 6240 else 6241 { 6242 to_mode = GET_MODE_INNER (to_mode); 6243 rtx from_real 6244 = simplify_gen_subreg (to_mode, result, from_mode, 0); 6245 rtx from_imag 6246 = simplify_gen_subreg (to_mode, result, from_mode, 6247 GET_MODE_SIZE (to_mode)); 6248 if (!from_real || !from_imag) 6249 goto concat_store_slow; 6250 emit_move_insn (XEXP (to_rtx, 0), from_real); 6251 emit_move_insn (XEXP (to_rtx, 1), from_imag); 6252 } 6253 } 6254 } 6255 else 6256 { 6257 concat_store_slow:; 6258 rtx temp = assign_stack_temp (GET_MODE (to_rtx), 6259 GET_MODE_SIZE (GET_MODE (to_rtx))); 6260 write_complex_part (temp, XEXP (to_rtx, 0), false, true); 6261 write_complex_part (temp, XEXP (to_rtx, 1), true, false); 6262 result = store_field (temp, bitsize, bitpos, 6263 bitregion_start, bitregion_end, 6264 mode1, from, get_alias_set (to), 6265 nontemporal, reversep); 6266 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false)); 6267 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true)); 6268 } 6269 } 6270 /* For calls to functions returning variable length structures, if TO_RTX 6271 is not a MEM, go through a MEM because we must not create temporaries 6272 of the VLA type. */ 6273 else if (!MEM_P (to_rtx) 6274 && TREE_CODE (from) == CALL_EXPR 6275 && COMPLETE_TYPE_P (TREE_TYPE (from)) 6276 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST) 6277 { 6278 rtx temp = assign_stack_temp (GET_MODE (to_rtx), 6279 GET_MODE_SIZE (GET_MODE (to_rtx))); 6280 result = store_field (temp, bitsize, bitpos, bitregion_start, 6281 bitregion_end, mode1, from, get_alias_set (to), 6282 nontemporal, reversep); 6283 emit_move_insn (to_rtx, temp); 6284 } 6285 else 6286 { 6287 if (MEM_P (to_rtx)) 6288 { 6289 /* If the field is at offset zero, we could have been given the 6290 DECL_RTX of the parent struct. Don't munge it. */ 6291 to_rtx = shallow_copy_rtx (to_rtx); 6292 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos); 6293 if (volatilep) 6294 MEM_VOLATILE_P (to_rtx) = 1; 6295 } 6296 6297 gcc_checking_assert (known_ge (bitpos, 0)); 6298 if (optimize_bitfield_assignment_op (bitsize, bitpos, 6299 bitregion_start, bitregion_end, 6300 mode1, to_rtx, to, from, 6301 reversep)) 6302 result = NULL; 6303 else if (SUBREG_P (to_rtx) 6304 && SUBREG_PROMOTED_VAR_P (to_rtx)) 6305 { 6306 /* If to_rtx is a promoted subreg, we need to zero or sign 6307 extend the value afterwards. */ 6308 if (TREE_CODE (to) == MEM_REF 6309 && TYPE_MODE (TREE_TYPE (from)) != BLKmode 6310 && !REF_REVERSE_STORAGE_ORDER (to) 6311 && known_eq (bitpos, 0) 6312 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx)))) 6313 result = store_expr (from, to_rtx, 0, nontemporal, false); 6314 /* Check if the field overlaps the MSB, requiring extension. */ 6315 else if (maybe_eq (bitpos + bitsize, 6316 GET_MODE_BITSIZE (GET_MODE (to_rtx)))) 6317 { 6318 scalar_int_mode imode = subreg_unpromoted_mode (to_rtx); 6319 scalar_int_mode omode = subreg_promoted_mode (to_rtx); 6320 rtx to_rtx1 = lowpart_subreg (imode, SUBREG_REG (to_rtx), 6321 omode); 6322 result = store_field (to_rtx1, bitsize, bitpos, 6323 bitregion_start, bitregion_end, 6324 mode1, from, get_alias_set (to), 6325 nontemporal, reversep); 6326 /* If the target usually keeps IMODE appropriately 6327 extended in OMODE it's unsafe to refer to it using 6328 a SUBREG whilst this invariant doesn't hold. */ 6329 if (targetm.mode_rep_extended (imode, omode) != UNKNOWN) 6330 to_rtx1 = simplify_gen_unary (TRUNCATE, imode, 6331 SUBREG_REG (to_rtx), omode); 6332 convert_move (SUBREG_REG (to_rtx), to_rtx1, 6333 SUBREG_PROMOTED_SIGN (to_rtx)); 6334 } 6335 else 6336 result = store_field (to_rtx, bitsize, bitpos, 6337 bitregion_start, bitregion_end, 6338 mode1, from, get_alias_set (to), 6339 nontemporal, reversep); 6340 } 6341 else 6342 result = store_field (to_rtx, bitsize, bitpos, 6343 bitregion_start, bitregion_end, 6344 mode1, from, get_alias_set (to), 6345 nontemporal, reversep); 6346 /* Move the temporary storage back to the non-MEM_P. */ 6347 if (stemp) 6348 emit_move_insn (old_to_rtx, stemp); 6349 } 6350 6351 if (result) 6352 preserve_temp_slots (result); 6353 pop_temp_slots (); 6354 return; 6355 } 6356 6357 /* If the rhs is a function call and its value is not an aggregate, 6358 call the function before we start to compute the lhs. 6359 This is needed for correct code for cases such as 6360 val = setjmp (buf) on machines where reference to val 6361 requires loading up part of an address in a separate insn. 6362 6363 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG 6364 since it might be a promoted variable where the zero- or sign- extension 6365 needs to be done. Handling this in the normal way is safe because no 6366 computation is done before the call. The same is true for SSA names. */ 6367 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from) 6368 && COMPLETE_TYPE_P (TREE_TYPE (from)) 6369 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST 6370 && ! (((VAR_P (to) 6371 || TREE_CODE (to) == PARM_DECL 6372 || TREE_CODE (to) == RESULT_DECL) 6373 && REG_P (DECL_RTL (to))) 6374 || TREE_CODE (to) == SSA_NAME)) 6375 { 6376 rtx value; 6377 6378 push_temp_slots (); 6379 value = expand_normal (from); 6380 6381 if (to_rtx == 0) 6382 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE); 6383 6384 /* Handle calls that return values in multiple non-contiguous locations. 6385 The Irix 6 ABI has examples of this. */ 6386 if (GET_CODE (to_rtx) == PARALLEL) 6387 { 6388 if (GET_CODE (value) == PARALLEL) 6389 emit_group_move (to_rtx, value); 6390 else 6391 emit_group_load (to_rtx, value, TREE_TYPE (from), 6392 int_size_in_bytes (TREE_TYPE (from))); 6393 } 6394 else if (GET_CODE (value) == PARALLEL) 6395 emit_group_store (to_rtx, value, TREE_TYPE (from), 6396 int_size_in_bytes (TREE_TYPE (from))); 6397 else if (GET_MODE (to_rtx) == BLKmode) 6398 { 6399 /* Handle calls that return BLKmode values in registers. */ 6400 if (REG_P (value)) 6401 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from)); 6402 else 6403 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL); 6404 } 6405 else 6406 { 6407 if (POINTER_TYPE_P (TREE_TYPE (to))) 6408 value = convert_memory_address_addr_space 6409 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value, 6410 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to)))); 6411 6412 emit_move_insn (to_rtx, value); 6413 } 6414 6415 preserve_temp_slots (to_rtx); 6416 pop_temp_slots (); 6417 return; 6418 } 6419 6420 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */ 6421 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE); 6422 6423 /* Don't move directly into a return register. */ 6424 if (TREE_CODE (to) == RESULT_DECL 6425 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL)) 6426 { 6427 rtx temp; 6428 6429 push_temp_slots (); 6430 6431 /* If the source is itself a return value, it still is in a pseudo at 6432 this point so we can move it back to the return register directly. */ 6433 if (REG_P (to_rtx) 6434 && TYPE_MODE (TREE_TYPE (from)) == BLKmode 6435 && TREE_CODE (from) != CALL_EXPR) 6436 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from); 6437 else 6438 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL); 6439 6440 /* Handle calls that return values in multiple non-contiguous locations. 6441 The Irix 6 ABI has examples of this. */ 6442 if (GET_CODE (to_rtx) == PARALLEL) 6443 { 6444 if (GET_CODE (temp) == PARALLEL) 6445 emit_group_move (to_rtx, temp); 6446 else 6447 emit_group_load (to_rtx, temp, TREE_TYPE (from), 6448 int_size_in_bytes (TREE_TYPE (from))); 6449 } 6450 else if (temp) 6451 emit_move_insn (to_rtx, temp); 6452 6453 preserve_temp_slots (to_rtx); 6454 pop_temp_slots (); 6455 return; 6456 } 6457 6458 /* In case we are returning the contents of an object which overlaps 6459 the place the value is being stored, use a safe function when copying 6460 a value through a pointer into a structure value return block. */ 6461 if (TREE_CODE (to) == RESULT_DECL 6462 && TREE_CODE (from) == INDIRECT_REF 6463 && ADDR_SPACE_GENERIC_P 6464 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0))))) 6465 && refs_may_alias_p (to, from) 6466 && cfun->returns_struct 6467 && !cfun->returns_pcc_struct) 6468 { 6469 rtx from_rtx, size; 6470 6471 push_temp_slots (); 6472 size = expr_size (from); 6473 from_rtx = expand_normal (from); 6474 6475 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size); 6476 6477 preserve_temp_slots (to_rtx); 6478 pop_temp_slots (); 6479 return; 6480 } 6481 6482 /* Compute FROM and store the value in the rtx we got. */ 6483 6484 push_temp_slots (); 6485 result = store_expr (from, to_rtx, 0, nontemporal, false); 6486 preserve_temp_slots (result); 6487 pop_temp_slots (); 6488 return; 6489 } 6490 6491 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this 6492 succeeded, false otherwise. */ 6493 6494 bool 6495 emit_storent_insn (rtx to, rtx from) 6496 { 6497 class expand_operand ops[2]; 6498 machine_mode mode = GET_MODE (to); 6499 enum insn_code code = optab_handler (storent_optab, mode); 6500 6501 if (code == CODE_FOR_nothing) 6502 return false; 6503 6504 create_fixed_operand (&ops[0], to); 6505 create_input_operand (&ops[1], from, mode); 6506 return maybe_expand_insn (code, 2, ops); 6507 } 6508 6509 /* Helper function for store_expr storing of STRING_CST. */ 6510 6511 static rtx 6512 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset, 6513 fixed_size_mode mode) 6514 { 6515 tree str = (tree) data; 6516 6517 gcc_assert (offset >= 0); 6518 if (offset >= TREE_STRING_LENGTH (str)) 6519 return const0_rtx; 6520 6521 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode) 6522 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str)) 6523 { 6524 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode)); 6525 size_t l = TREE_STRING_LENGTH (str) - offset; 6526 memcpy (p, TREE_STRING_POINTER (str) + offset, l); 6527 memset (p + l, '\0', GET_MODE_SIZE (mode) - l); 6528 return c_readstr (p, mode, false); 6529 } 6530 6531 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false); 6532 } 6533 6534 /* Generate code for computing expression EXP, 6535 and storing the value into TARGET. 6536 6537 If the mode is BLKmode then we may return TARGET itself. 6538 It turns out that in BLKmode it doesn't cause a problem. 6539 because C has no operators that could combine two different 6540 assignments into the same BLKmode object with different values 6541 with no sequence point. Will other languages need this to 6542 be more thorough? 6543 6544 If CALL_PARAM_P is nonzero, this is a store into a call param on the 6545 stack, and block moves may need to be treated specially. 6546 6547 If NONTEMPORAL is true, try using a nontemporal store instruction. 6548 6549 If REVERSE is true, the store is to be done in reverse order. */ 6550 6551 rtx 6552 store_expr (tree exp, rtx target, int call_param_p, 6553 bool nontemporal, bool reverse) 6554 { 6555 rtx temp; 6556 rtx alt_rtl = NULL_RTX; 6557 location_t loc = curr_insn_location (); 6558 bool shortened_string_cst = false; 6559 6560 if (VOID_TYPE_P (TREE_TYPE (exp))) 6561 { 6562 /* C++ can generate ?: expressions with a throw expression in one 6563 branch and an rvalue in the other. Here, we resolve attempts to 6564 store the throw expression's nonexistent result. */ 6565 gcc_assert (!call_param_p); 6566 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL); 6567 return NULL_RTX; 6568 } 6569 if (TREE_CODE (exp) == COMPOUND_EXPR) 6570 { 6571 /* Perform first part of compound expression, then assign from second 6572 part. */ 6573 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode, 6574 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL); 6575 return store_expr (TREE_OPERAND (exp, 1), target, 6576 call_param_p, nontemporal, reverse); 6577 } 6578 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode) 6579 { 6580 /* For conditional expression, get safe form of the target. Then 6581 test the condition, doing the appropriate assignment on either 6582 side. This avoids the creation of unnecessary temporaries. 6583 For non-BLKmode, it is more efficient not to do this. */ 6584 6585 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx (); 6586 6587 do_pending_stack_adjust (); 6588 NO_DEFER_POP; 6589 jumpifnot (TREE_OPERAND (exp, 0), lab1, 6590 profile_probability::uninitialized ()); 6591 store_expr (TREE_OPERAND (exp, 1), target, call_param_p, 6592 nontemporal, reverse); 6593 emit_jump_insn (targetm.gen_jump (lab2)); 6594 emit_barrier (); 6595 emit_label (lab1); 6596 store_expr (TREE_OPERAND (exp, 2), target, call_param_p, 6597 nontemporal, reverse); 6598 emit_label (lab2); 6599 OK_DEFER_POP; 6600 6601 return NULL_RTX; 6602 } 6603 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target)) 6604 /* If this is a scalar in a register that is stored in a wider mode 6605 than the declared mode, compute the result into its declared mode 6606 and then convert to the wider mode. Our value is the computed 6607 expression. */ 6608 { 6609 rtx inner_target = 0; 6610 scalar_int_mode outer_mode = subreg_unpromoted_mode (target); 6611 scalar_int_mode inner_mode = subreg_promoted_mode (target); 6612 6613 /* We can do the conversion inside EXP, which will often result 6614 in some optimizations. Do the conversion in two steps: first 6615 change the signedness, if needed, then the extend. But don't 6616 do this if the type of EXP is a subtype of something else 6617 since then the conversion might involve more than just 6618 converting modes. */ 6619 if (INTEGRAL_TYPE_P (TREE_TYPE (exp)) 6620 && TREE_TYPE (TREE_TYPE (exp)) == 0 6621 && GET_MODE_PRECISION (outer_mode) 6622 == TYPE_PRECISION (TREE_TYPE (exp))) 6623 { 6624 if (!SUBREG_CHECK_PROMOTED_SIGN (target, 6625 TYPE_UNSIGNED (TREE_TYPE (exp)))) 6626 { 6627 /* Some types, e.g. Fortran's logical*4, won't have a signed 6628 version, so use the mode instead. */ 6629 tree ntype 6630 = (signed_or_unsigned_type_for 6631 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp))); 6632 if (ntype == NULL) 6633 ntype = lang_hooks.types.type_for_mode 6634 (TYPE_MODE (TREE_TYPE (exp)), 6635 SUBREG_PROMOTED_SIGN (target)); 6636 6637 exp = fold_convert_loc (loc, ntype, exp); 6638 } 6639 6640 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode 6641 (inner_mode, SUBREG_PROMOTED_SIGN (target)), 6642 exp); 6643 6644 inner_target = SUBREG_REG (target); 6645 } 6646 6647 temp = expand_expr (exp, inner_target, VOIDmode, 6648 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL); 6649 6650 6651 /* If TEMP is a VOIDmode constant, use convert_modes to make 6652 sure that we properly convert it. */ 6653 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode) 6654 { 6655 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)), 6656 temp, SUBREG_PROMOTED_SIGN (target)); 6657 temp = convert_modes (inner_mode, outer_mode, temp, 6658 SUBREG_PROMOTED_SIGN (target)); 6659 } 6660 else if (!SCALAR_INT_MODE_P (GET_MODE (temp))) 6661 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)), 6662 temp, SUBREG_PROMOTED_SIGN (target)); 6663 6664 convert_move (SUBREG_REG (target), temp, 6665 SUBREG_PROMOTED_SIGN (target)); 6666 6667 return NULL_RTX; 6668 } 6669 else if ((TREE_CODE (exp) == STRING_CST 6670 || (TREE_CODE (exp) == MEM_REF 6671 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR 6672 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) 6673 == STRING_CST 6674 && integer_zerop (TREE_OPERAND (exp, 1)))) 6675 && !nontemporal && !call_param_p 6676 && MEM_P (target)) 6677 { 6678 /* Optimize initialization of an array with a STRING_CST. */ 6679 HOST_WIDE_INT exp_len, str_copy_len; 6680 rtx dest_mem; 6681 tree str = TREE_CODE (exp) == STRING_CST 6682 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0); 6683 6684 exp_len = int_expr_size (exp); 6685 if (exp_len <= 0) 6686 goto normal_expr; 6687 6688 if (TREE_STRING_LENGTH (str) <= 0) 6689 goto normal_expr; 6690 6691 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str, 6692 MEM_ALIGN (target), false)) 6693 { 6694 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str, 6695 MEM_ALIGN (target), false, RETURN_BEGIN); 6696 return NULL_RTX; 6697 } 6698 6699 str_copy_len = TREE_STRING_LENGTH (str); 6700 6701 /* Trailing NUL bytes in EXP will be handled by the call to 6702 clear_storage, which is more efficient than copying them from 6703 the STRING_CST, so trim those from STR_COPY_LEN. */ 6704 while (str_copy_len) 6705 { 6706 if (TREE_STRING_POINTER (str)[str_copy_len - 1]) 6707 break; 6708 str_copy_len--; 6709 } 6710 6711 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0) 6712 { 6713 str_copy_len += STORE_MAX_PIECES - 1; 6714 str_copy_len &= ~(STORE_MAX_PIECES - 1); 6715 } 6716 if (str_copy_len >= exp_len) 6717 goto normal_expr; 6718 6719 if (!can_store_by_pieces (str_copy_len, string_cst_read_str, 6720 (void *) str, MEM_ALIGN (target), false)) 6721 goto normal_expr; 6722 6723 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str, 6724 (void *) str, MEM_ALIGN (target), false, 6725 RETURN_END); 6726 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0, 6727 exp_len - str_copy_len), 6728 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL); 6729 return NULL_RTX; 6730 } 6731 else 6732 { 6733 rtx tmp_target; 6734 6735 normal_expr: 6736 /* If we want to use a nontemporal or a reverse order store, force the 6737 value into a register first. */ 6738 tmp_target = nontemporal || reverse ? NULL_RTX : target; 6739 tree rexp = exp; 6740 if (TREE_CODE (exp) == STRING_CST 6741 && tmp_target == target 6742 && GET_MODE (target) == BLKmode 6743 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode) 6744 { 6745 rtx size = expr_size (exp); 6746 if (CONST_INT_P (size) 6747 && size != const0_rtx 6748 && (UINTVAL (size) 6749 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32))) 6750 { 6751 /* If the STRING_CST has much larger array type than 6752 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of 6753 it into the rodata section as the code later on will use 6754 memset zero for the remainder anyway. See PR95052. */ 6755 tmp_target = NULL_RTX; 6756 rexp = copy_node (exp); 6757 tree index 6758 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1)); 6759 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)), 6760 index); 6761 shortened_string_cst = true; 6762 } 6763 } 6764 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target), 6765 (call_param_p 6766 ? EXPAND_STACK_PARM : EXPAND_NORMAL), 6767 &alt_rtl, false); 6768 if (shortened_string_cst) 6769 { 6770 gcc_assert (MEM_P (temp)); 6771 temp = change_address (temp, BLKmode, NULL_RTX); 6772 } 6773 } 6774 6775 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not 6776 the same as that of TARGET, adjust the constant. This is needed, for 6777 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want 6778 only a word-sized value. */ 6779 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode 6780 && TREE_CODE (exp) != ERROR_MARK 6781 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp))) 6782 { 6783 gcc_assert (!shortened_string_cst); 6784 if (GET_MODE_CLASS (GET_MODE (target)) 6785 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp))) 6786 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)), 6787 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp))))) 6788 { 6789 rtx t = simplify_gen_subreg (GET_MODE (target), temp, 6790 TYPE_MODE (TREE_TYPE (exp)), 0); 6791 if (t) 6792 temp = t; 6793 } 6794 if (GET_MODE (temp) == VOIDmode) 6795 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)), 6796 temp, TYPE_UNSIGNED (TREE_TYPE (exp))); 6797 } 6798 6799 /* If value was not generated in the target, store it there. 6800 Convert the value to TARGET's type first if necessary and emit the 6801 pending incrementations that have been queued when expanding EXP. 6802 Note that we cannot emit the whole queue blindly because this will 6803 effectively disable the POST_INC optimization later. 6804 6805 If TEMP and TARGET compare equal according to rtx_equal_p, but 6806 one or both of them are volatile memory refs, we have to distinguish 6807 two cases: 6808 - expand_expr has used TARGET. In this case, we must not generate 6809 another copy. This can be detected by TARGET being equal according 6810 to == . 6811 - expand_expr has not used TARGET - that means that the source just 6812 happens to have the same RTX form. Since temp will have been created 6813 by expand_expr, it will compare unequal according to == . 6814 We must generate a copy in this case, to reach the correct number 6815 of volatile memory references. */ 6816 6817 if ((! rtx_equal_p (temp, target) 6818 || (temp != target && (side_effects_p (temp) 6819 || side_effects_p (target) 6820 || (MEM_P (temp) 6821 && !mems_same_for_tbaa_p (temp, target))))) 6822 && TREE_CODE (exp) != ERROR_MARK 6823 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET, 6824 but TARGET is not valid memory reference, TEMP will differ 6825 from TARGET although it is really the same location. */ 6826 && !(alt_rtl 6827 && rtx_equal_p (alt_rtl, target) 6828 && !side_effects_p (alt_rtl) 6829 && !side_effects_p (target)) 6830 /* If there's nothing to copy, don't bother. Don't call 6831 expr_size unless necessary, because some front-ends (C++) 6832 expr_size-hook must not be given objects that are not 6833 supposed to be bit-copied or bit-initialized. */ 6834 && expr_size (exp) != const0_rtx) 6835 { 6836 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode) 6837 { 6838 gcc_assert (!shortened_string_cst); 6839 if (GET_MODE (target) == BLKmode) 6840 { 6841 /* Handle calls that return BLKmode values in registers. */ 6842 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR) 6843 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp)); 6844 else 6845 store_bit_field (target, 6846 rtx_to_poly_int64 (expr_size (exp)) 6847 * BITS_PER_UNIT, 6848 0, 0, 0, GET_MODE (temp), temp, reverse, 6849 false); 6850 } 6851 else 6852 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp))); 6853 } 6854 6855 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST) 6856 { 6857 /* Handle copying a string constant into an array. The string 6858 constant may be shorter than the array. So copy just the string's 6859 actual length, and clear the rest. First get the size of the data 6860 type of the string, which is actually the size of the target. */ 6861 rtx size = expr_size (exp); 6862 6863 if (CONST_INT_P (size) 6864 && INTVAL (size) < TREE_STRING_LENGTH (exp)) 6865 emit_block_move (target, temp, size, 6866 (call_param_p 6867 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL)); 6868 else 6869 { 6870 machine_mode pointer_mode 6871 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target)); 6872 machine_mode address_mode = get_address_mode (target); 6873 6874 /* Compute the size of the data to copy from the string. */ 6875 tree copy_size 6876 = size_binop_loc (loc, MIN_EXPR, 6877 make_tree (sizetype, size), 6878 size_int (TREE_STRING_LENGTH (exp))); 6879 rtx copy_size_rtx 6880 = expand_expr (copy_size, NULL_RTX, VOIDmode, 6881 (call_param_p 6882 ? EXPAND_STACK_PARM : EXPAND_NORMAL)); 6883 rtx_code_label *label = 0; 6884 6885 /* Copy that much. */ 6886 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx, 6887 TYPE_UNSIGNED (sizetype)); 6888 emit_block_move (target, temp, copy_size_rtx, 6889 (call_param_p 6890 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL)); 6891 6892 /* Figure out how much is left in TARGET that we have to clear. 6893 Do all calculations in pointer_mode. */ 6894 poly_int64 const_copy_size; 6895 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size)) 6896 { 6897 size = plus_constant (address_mode, size, -const_copy_size); 6898 target = adjust_address (target, BLKmode, const_copy_size); 6899 } 6900 else 6901 { 6902 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size, 6903 copy_size_rtx, NULL_RTX, 0, 6904 OPTAB_LIB_WIDEN); 6905 6906 if (GET_MODE (copy_size_rtx) != address_mode) 6907 copy_size_rtx = convert_to_mode (address_mode, 6908 copy_size_rtx, 6909 TYPE_UNSIGNED (sizetype)); 6910 6911 target = offset_address (target, copy_size_rtx, 6912 highest_pow2_factor (copy_size)); 6913 label = gen_label_rtx (); 6914 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX, 6915 GET_MODE (size), 0, label); 6916 } 6917 6918 if (size != const0_rtx) 6919 clear_storage (target, size, BLOCK_OP_NORMAL); 6920 6921 if (label) 6922 emit_label (label); 6923 } 6924 } 6925 else if (shortened_string_cst) 6926 gcc_unreachable (); 6927 /* Handle calls that return values in multiple non-contiguous locations. 6928 The Irix 6 ABI has examples of this. */ 6929 else if (GET_CODE (target) == PARALLEL) 6930 { 6931 if (GET_CODE (temp) == PARALLEL) 6932 emit_group_move (target, temp); 6933 else 6934 emit_group_load (target, temp, TREE_TYPE (exp), 6935 int_size_in_bytes (TREE_TYPE (exp))); 6936 } 6937 else if (GET_CODE (temp) == PARALLEL) 6938 emit_group_store (target, temp, TREE_TYPE (exp), 6939 int_size_in_bytes (TREE_TYPE (exp))); 6940 else if (GET_MODE (temp) == BLKmode) 6941 emit_block_move (target, temp, expr_size (exp), 6942 (call_param_p 6943 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL)); 6944 /* If we emit a nontemporal store, there is nothing else to do. */ 6945 else if (nontemporal && emit_storent_insn (target, temp)) 6946 ; 6947 else 6948 { 6949 if (reverse) 6950 temp = flip_storage_order (GET_MODE (target), temp); 6951 temp = force_operand (temp, target); 6952 if (temp != target) 6953 emit_move_insn (target, temp); 6954 } 6955 } 6956 else 6957 gcc_assert (!shortened_string_cst); 6958 6959 return NULL_RTX; 6960 } 6961 6962 /* Return true if field F of structure TYPE is a flexible array. */ 6964 6965 static bool 6966 flexible_array_member_p (const_tree f, const_tree type) 6967 { 6968 const_tree tf; 6969 6970 tf = TREE_TYPE (f); 6971 return (DECL_CHAIN (f) == NULL 6972 && TREE_CODE (tf) == ARRAY_TYPE 6973 && TYPE_DOMAIN (tf) 6974 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf)) 6975 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf))) 6976 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf)) 6977 && int_size_in_bytes (type) >= 0); 6978 } 6979 6980 /* If FOR_CTOR_P, return the number of top-level elements that a constructor 6981 must have in order for it to completely initialize a value of type TYPE. 6982 Return -1 if the number isn't known. 6983 6984 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */ 6985 6986 static HOST_WIDE_INT 6987 count_type_elements (const_tree type, bool for_ctor_p) 6988 { 6989 switch (TREE_CODE (type)) 6990 { 6991 case ARRAY_TYPE: 6992 { 6993 tree nelts; 6994 6995 nelts = array_type_nelts (type); 6996 if (nelts && tree_fits_uhwi_p (nelts)) 6997 { 6998 unsigned HOST_WIDE_INT n; 6999 7000 n = tree_to_uhwi (nelts) + 1; 7001 if (n == 0 || for_ctor_p) 7002 return n; 7003 else 7004 return n * count_type_elements (TREE_TYPE (type), false); 7005 } 7006 return for_ctor_p ? -1 : 1; 7007 } 7008 7009 case RECORD_TYPE: 7010 { 7011 unsigned HOST_WIDE_INT n; 7012 tree f; 7013 7014 n = 0; 7015 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f)) 7016 if (TREE_CODE (f) == FIELD_DECL) 7017 { 7018 if (!for_ctor_p) 7019 n += count_type_elements (TREE_TYPE (f), false); 7020 else if (!flexible_array_member_p (f, type)) 7021 /* Don't count flexible arrays, which are not supposed 7022 to be initialized. */ 7023 n += 1; 7024 } 7025 7026 return n; 7027 } 7028 7029 case UNION_TYPE: 7030 case QUAL_UNION_TYPE: 7031 { 7032 tree f; 7033 HOST_WIDE_INT n, m; 7034 7035 gcc_assert (!for_ctor_p); 7036 /* Estimate the number of scalars in each field and pick the 7037 maximum. Other estimates would do instead; the idea is simply 7038 to make sure that the estimate is not sensitive to the ordering 7039 of the fields. */ 7040 n = 1; 7041 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f)) 7042 if (TREE_CODE (f) == FIELD_DECL) 7043 { 7044 m = count_type_elements (TREE_TYPE (f), false); 7045 /* If the field doesn't span the whole union, add an extra 7046 scalar for the rest. */ 7047 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)), 7048 TYPE_SIZE (type)) != 1) 7049 m++; 7050 if (n < m) 7051 n = m; 7052 } 7053 return n; 7054 } 7055 7056 case COMPLEX_TYPE: 7057 return 2; 7058 7059 case VECTOR_TYPE: 7060 { 7061 unsigned HOST_WIDE_INT nelts; 7062 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts)) 7063 return nelts; 7064 else 7065 return -1; 7066 } 7067 7068 case INTEGER_TYPE: 7069 case REAL_TYPE: 7070 case FIXED_POINT_TYPE: 7071 case ENUMERAL_TYPE: 7072 case BOOLEAN_TYPE: 7073 case POINTER_TYPE: 7074 case OFFSET_TYPE: 7075 case REFERENCE_TYPE: 7076 case NULLPTR_TYPE: 7077 case OPAQUE_TYPE: 7078 case BITINT_TYPE: 7079 return 1; 7080 7081 case ERROR_MARK: 7082 return 0; 7083 7084 case VOID_TYPE: 7085 case METHOD_TYPE: 7086 case FUNCTION_TYPE: 7087 case LANG_TYPE: 7088 default: 7089 gcc_unreachable (); 7090 } 7091 } 7092 7093 /* Helper for categorize_ctor_elements. Identical interface. */ 7094 7095 static bool 7096 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts, 7097 HOST_WIDE_INT *p_unique_nz_elts, 7098 HOST_WIDE_INT *p_init_elts, bool *p_complete) 7099 { 7100 unsigned HOST_WIDE_INT idx; 7101 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields; 7102 tree value, purpose, elt_type; 7103 7104 /* Whether CTOR is a valid constant initializer, in accordance with what 7105 initializer_constant_valid_p does. If inferred from the constructor 7106 elements, true until proven otherwise. */ 7107 bool const_from_elts_p = constructor_static_from_elts_p (ctor); 7108 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor); 7109 7110 nz_elts = 0; 7111 unique_nz_elts = 0; 7112 init_elts = 0; 7113 num_fields = 0; 7114 elt_type = NULL_TREE; 7115 7116 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value) 7117 { 7118 HOST_WIDE_INT mult = 1; 7119 7120 if (purpose && TREE_CODE (purpose) == RANGE_EXPR) 7121 { 7122 tree lo_index = TREE_OPERAND (purpose, 0); 7123 tree hi_index = TREE_OPERAND (purpose, 1); 7124 7125 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index)) 7126 mult = (tree_to_uhwi (hi_index) 7127 - tree_to_uhwi (lo_index) + 1); 7128 } 7129 num_fields += mult; 7130 elt_type = TREE_TYPE (value); 7131 7132 switch (TREE_CODE (value)) 7133 { 7134 case CONSTRUCTOR: 7135 { 7136 HOST_WIDE_INT nz = 0, unz = 0, ic = 0; 7137 7138 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz, 7139 &ic, p_complete); 7140 7141 nz_elts += mult * nz; 7142 unique_nz_elts += unz; 7143 init_elts += mult * ic; 7144 7145 if (const_from_elts_p && const_p) 7146 const_p = const_elt_p; 7147 } 7148 break; 7149 7150 case INTEGER_CST: 7151 case REAL_CST: 7152 case FIXED_CST: 7153 if (!initializer_zerop (value)) 7154 { 7155 nz_elts += mult; 7156 unique_nz_elts++; 7157 } 7158 init_elts += mult; 7159 break; 7160 7161 case STRING_CST: 7162 nz_elts += mult * TREE_STRING_LENGTH (value); 7163 unique_nz_elts += TREE_STRING_LENGTH (value); 7164 init_elts += mult * TREE_STRING_LENGTH (value); 7165 break; 7166 7167 case COMPLEX_CST: 7168 if (!initializer_zerop (TREE_REALPART (value))) 7169 { 7170 nz_elts += mult; 7171 unique_nz_elts++; 7172 } 7173 if (!initializer_zerop (TREE_IMAGPART (value))) 7174 { 7175 nz_elts += mult; 7176 unique_nz_elts++; 7177 } 7178 init_elts += 2 * mult; 7179 break; 7180 7181 case VECTOR_CST: 7182 { 7183 /* We can only construct constant-length vectors using 7184 CONSTRUCTOR. */ 7185 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant (); 7186 for (unsigned int i = 0; i < nunits; ++i) 7187 { 7188 tree v = VECTOR_CST_ELT (value, i); 7189 if (!initializer_zerop (v)) 7190 { 7191 nz_elts += mult; 7192 unique_nz_elts++; 7193 } 7194 init_elts += mult; 7195 } 7196 } 7197 break; 7198 7199 default: 7200 { 7201 HOST_WIDE_INT tc = count_type_elements (elt_type, false); 7202 nz_elts += mult * tc; 7203 unique_nz_elts += tc; 7204 init_elts += mult * tc; 7205 7206 if (const_from_elts_p && const_p) 7207 const_p 7208 = initializer_constant_valid_p (value, 7209 elt_type, 7210 TYPE_REVERSE_STORAGE_ORDER 7211 (TREE_TYPE (ctor))) 7212 != NULL_TREE; 7213 } 7214 break; 7215 } 7216 } 7217 7218 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor), 7219 num_fields, elt_type)) 7220 *p_complete = false; 7221 7222 *p_nz_elts += nz_elts; 7223 *p_unique_nz_elts += unique_nz_elts; 7224 *p_init_elts += init_elts; 7225 7226 return const_p; 7227 } 7228 7229 /* Examine CTOR to discover: 7230 * how many scalar fields are set to nonzero values, 7231 and place it in *P_NZ_ELTS; 7232 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of 7233 high - low + 1 (this can be useful for callers to determine ctors 7234 that could be cheaply initialized with - perhaps nested - loops 7235 compared to copied from huge read-only data), 7236 and place it in *P_UNIQUE_NZ_ELTS; 7237 * how many scalar fields in total are in CTOR, 7238 and place it in *P_ELT_COUNT. 7239 * whether the constructor is complete -- in the sense that every 7240 meaningful byte is explicitly given a value -- 7241 and place it in *P_COMPLETE. 7242 7243 Return whether or not CTOR is a valid static constant initializer, the same 7244 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */ 7245 7246 bool 7247 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts, 7248 HOST_WIDE_INT *p_unique_nz_elts, 7249 HOST_WIDE_INT *p_init_elts, bool *p_complete) 7250 { 7251 *p_nz_elts = 0; 7252 *p_unique_nz_elts = 0; 7253 *p_init_elts = 0; 7254 *p_complete = true; 7255 7256 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts, 7257 p_init_elts, p_complete); 7258 } 7259 7260 /* Return true if constructor CTOR is simple enough to be materialized 7261 in an integer mode register. Limit the size to WORDS words, which 7262 is 1 by default. */ 7263 7264 bool 7265 immediate_const_ctor_p (const_tree ctor, unsigned int words) 7266 { 7267 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */ 7268 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR) 7269 return false; 7270 7271 return TREE_CONSTANT (ctor) 7272 && !TREE_ADDRESSABLE (ctor) 7273 && CONSTRUCTOR_NELTS (ctor) 7274 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE 7275 && int_expr_size (ctor) <= words * UNITS_PER_WORD 7276 && initializer_constant_valid_for_bitfield_p (ctor); 7277 } 7278 7279 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last 7280 of which had type LAST_TYPE. Each element was itself a complete 7281 initializer, in the sense that every meaningful byte was explicitly 7282 given a value. Return true if the same is true for the constructor 7283 as a whole. */ 7284 7285 bool 7286 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts, 7287 const_tree last_type) 7288 { 7289 if (TREE_CODE (type) == UNION_TYPE 7290 || TREE_CODE (type) == QUAL_UNION_TYPE) 7291 { 7292 if (num_elts == 0) 7293 return false; 7294 7295 gcc_assert (num_elts == 1 && last_type); 7296 7297 /* ??? We could look at each element of the union, and find the 7298 largest element. Which would avoid comparing the size of the 7299 initialized element against any tail padding in the union. 7300 Doesn't seem worth the effort... */ 7301 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1; 7302 } 7303 7304 return count_type_elements (type, true) == num_elts; 7305 } 7306 7307 /* Return true if EXP contains mostly (3/4) zeros. */ 7308 7309 static bool 7310 mostly_zeros_p (const_tree exp) 7311 { 7312 if (TREE_CODE (exp) == CONSTRUCTOR) 7313 { 7314 HOST_WIDE_INT nz_elts, unz_elts, init_elts; 7315 bool complete_p; 7316 7317 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts, 7318 &complete_p); 7319 return !complete_p || nz_elts < init_elts / 4; 7320 } 7321 7322 return initializer_zerop (exp); 7323 } 7324 7325 /* Return true if EXP contains all zeros. */ 7326 7327 static bool 7328 all_zeros_p (const_tree exp) 7329 { 7330 if (TREE_CODE (exp) == CONSTRUCTOR) 7331 { 7332 HOST_WIDE_INT nz_elts, unz_elts, init_elts; 7333 bool complete_p; 7334 7335 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts, 7336 &complete_p); 7337 return nz_elts == 0; 7338 } 7339 7340 return initializer_zerop (exp); 7341 } 7342 7343 /* Helper function for store_constructor. 7345 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field. 7346 CLEARED is as for store_constructor. 7347 ALIAS_SET is the alias set to use for any stores. 7348 If REVERSE is true, the store is to be done in reverse order. 7349 7350 This provides a recursive shortcut back to store_constructor when it isn't 7351 necessary to go through store_field. This is so that we can pass through 7352 the cleared field to let store_constructor know that we may not have to 7353 clear a substructure if the outer structure has already been cleared. */ 7354 7355 static void 7356 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos, 7357 poly_uint64 bitregion_start, 7358 poly_uint64 bitregion_end, 7359 machine_mode mode, 7360 tree exp, int cleared, 7361 alias_set_type alias_set, bool reverse) 7362 { 7363 poly_int64 bytepos; 7364 poly_uint64 bytesize; 7365 if (TREE_CODE (exp) == CONSTRUCTOR 7366 /* We can only call store_constructor recursively if the size and 7367 bit position are on a byte boundary. */ 7368 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos) 7369 && maybe_ne (bitsize, 0U) 7370 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize) 7371 /* If we have a nonzero bitpos for a register target, then we just 7372 let store_field do the bitfield handling. This is unlikely to 7373 generate unnecessary clear instructions anyways. */ 7374 && (known_eq (bitpos, 0) || MEM_P (target))) 7375 { 7376 if (MEM_P (target)) 7377 { 7378 machine_mode target_mode = GET_MODE (target); 7379 if (target_mode != BLKmode 7380 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode))) 7381 target_mode = BLKmode; 7382 target = adjust_address (target, target_mode, bytepos); 7383 } 7384 7385 7386 /* Update the alias set, if required. */ 7387 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target) 7388 && MEM_ALIAS_SET (target) != 0) 7389 { 7390 target = copy_rtx (target); 7391 set_mem_alias_set (target, alias_set); 7392 } 7393 7394 store_constructor (exp, target, cleared, bytesize, reverse); 7395 } 7396 else 7397 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode, 7398 exp, alias_set, false, reverse); 7399 } 7400 7401 7402 /* Returns the number of FIELD_DECLs in TYPE. */ 7403 7404 static int 7405 fields_length (const_tree type) 7406 { 7407 tree t = TYPE_FIELDS (type); 7408 int count = 0; 7409 7410 for (; t; t = DECL_CHAIN (t)) 7411 if (TREE_CODE (t) == FIELD_DECL) 7412 ++count; 7413 7414 return count; 7415 } 7416 7417 7418 /* Store the value of constructor EXP into the rtx TARGET. 7419 TARGET is either a REG or a MEM; we know it cannot conflict, since 7420 safe_from_p has been called. 7421 CLEARED is true if TARGET is known to have been zero'd. 7422 SIZE is the number of bytes of TARGET we are allowed to modify: this 7423 may not be the same as the size of EXP if we are assigning to a field 7424 which has been packed to exclude padding bits. 7425 If REVERSE is true, the store is to be done in reverse order. */ 7426 7427 void 7428 store_constructor (tree exp, rtx target, int cleared, poly_int64 size, 7429 bool reverse) 7430 { 7431 tree type = TREE_TYPE (exp); 7432 HOST_WIDE_INT exp_size = int_size_in_bytes (type); 7433 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0; 7434 7435 switch (TREE_CODE (type)) 7436 { 7437 case RECORD_TYPE: 7438 case UNION_TYPE: 7439 case QUAL_UNION_TYPE: 7440 { 7441 unsigned HOST_WIDE_INT idx; 7442 tree field, value; 7443 7444 /* The storage order is specified for every aggregate type. */ 7445 reverse = TYPE_REVERSE_STORAGE_ORDER (type); 7446 7447 /* If size is zero or the target is already cleared, do nothing. */ 7448 if (known_eq (size, 0) || cleared) 7449 cleared = 1; 7450 /* We either clear the aggregate or indicate the value is dead. */ 7451 else if ((TREE_CODE (type) == UNION_TYPE 7452 || TREE_CODE (type) == QUAL_UNION_TYPE) 7453 && ! CONSTRUCTOR_ELTS (exp)) 7454 /* If the constructor is empty, clear the union. */ 7455 { 7456 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL); 7457 cleared = 1; 7458 } 7459 7460 /* If we are building a static constructor into a register, 7461 set the initial value as zero so we can fold the value into 7462 a constant. But if more than one register is involved, 7463 this probably loses. */ 7464 else if (REG_P (target) && TREE_STATIC (exp) 7465 && known_le (GET_MODE_SIZE (GET_MODE (target)), 7466 REGMODE_NATURAL_SIZE (GET_MODE (target)))) 7467 { 7468 emit_move_insn (target, CONST0_RTX (GET_MODE (target))); 7469 cleared = 1; 7470 } 7471 7472 /* If the constructor has fewer fields than the structure or 7473 if we are initializing the structure to mostly zeros, clear 7474 the whole structure first. Don't do this if TARGET is a 7475 register whose mode size isn't equal to SIZE since 7476 clear_storage can't handle this case. */ 7477 else if (known_size_p (size) 7478 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type)) 7479 || mostly_zeros_p (exp)) 7480 && (!REG_P (target) 7481 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size))) 7482 { 7483 clear_storage (target, gen_int_mode (size, Pmode), 7484 BLOCK_OP_NORMAL); 7485 cleared = 1; 7486 } 7487 7488 if (REG_P (target) && !cleared) 7489 emit_clobber (target); 7490 7491 /* Store each element of the constructor into the 7492 corresponding field of TARGET. */ 7493 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value) 7494 { 7495 machine_mode mode; 7496 HOST_WIDE_INT bitsize; 7497 HOST_WIDE_INT bitpos = 0; 7498 tree offset; 7499 rtx to_rtx = target; 7500 7501 /* Just ignore missing fields. We cleared the whole 7502 structure, above, if any fields are missing. */ 7503 if (field == 0) 7504 continue; 7505 7506 if (cleared && initializer_zerop (value)) 7507 continue; 7508 7509 if (tree_fits_uhwi_p (DECL_SIZE (field))) 7510 bitsize = tree_to_uhwi (DECL_SIZE (field)); 7511 else 7512 gcc_unreachable (); 7513 7514 mode = DECL_MODE (field); 7515 if (DECL_BIT_FIELD (field)) 7516 mode = VOIDmode; 7517 7518 offset = DECL_FIELD_OFFSET (field); 7519 if (tree_fits_shwi_p (offset) 7520 && tree_fits_shwi_p (bit_position (field))) 7521 { 7522 bitpos = int_bit_position (field); 7523 offset = NULL_TREE; 7524 } 7525 else 7526 gcc_unreachable (); 7527 7528 /* If this initializes a field that is smaller than a 7529 word, at the start of a word, try to widen it to a full 7530 word. This special case allows us to output C++ member 7531 function initializations in a form that the optimizers 7532 can understand. */ 7533 if (WORD_REGISTER_OPERATIONS 7534 && REG_P (target) 7535 && bitsize < BITS_PER_WORD 7536 && bitpos % BITS_PER_WORD == 0 7537 && GET_MODE_CLASS (mode) == MODE_INT 7538 && TREE_CODE (value) == INTEGER_CST 7539 && exp_size >= 0 7540 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT) 7541 { 7542 type = TREE_TYPE (value); 7543 7544 if (TYPE_PRECISION (type) < BITS_PER_WORD) 7545 { 7546 type = lang_hooks.types.type_for_mode 7547 (word_mode, TYPE_UNSIGNED (type)); 7548 value = fold_convert (type, value); 7549 /* Make sure the bits beyond the original bitsize are zero 7550 so that we can correctly avoid extra zeroing stores in 7551 later constructor elements. */ 7552 tree bitsize_mask 7553 = wide_int_to_tree (type, wi::mask (bitsize, false, 7554 BITS_PER_WORD)); 7555 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask); 7556 } 7557 7558 if (BYTES_BIG_ENDIAN) 7559 value 7560 = fold_build2 (LSHIFT_EXPR, type, value, 7561 build_int_cst (type, 7562 BITS_PER_WORD - bitsize)); 7563 bitsize = BITS_PER_WORD; 7564 mode = word_mode; 7565 } 7566 7567 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx) 7568 && DECL_NONADDRESSABLE_P (field)) 7569 { 7570 to_rtx = copy_rtx (to_rtx); 7571 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1; 7572 } 7573 7574 store_constructor_field (to_rtx, bitsize, bitpos, 7575 0, bitregion_end, mode, 7576 value, cleared, 7577 get_alias_set (TREE_TYPE (field)), 7578 reverse); 7579 } 7580 break; 7581 } 7582 case ARRAY_TYPE: 7583 { 7584 tree value, index; 7585 unsigned HOST_WIDE_INT i; 7586 bool need_to_clear; 7587 tree domain; 7588 tree elttype = TREE_TYPE (type); 7589 bool const_bounds_p; 7590 HOST_WIDE_INT minelt = 0; 7591 HOST_WIDE_INT maxelt = 0; 7592 7593 /* The storage order is specified for every aggregate type. */ 7594 reverse = TYPE_REVERSE_STORAGE_ORDER (type); 7595 7596 domain = TYPE_DOMAIN (type); 7597 const_bounds_p = (TYPE_MIN_VALUE (domain) 7598 && TYPE_MAX_VALUE (domain) 7599 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain)) 7600 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain))); 7601 7602 /* If we have constant bounds for the range of the type, get them. */ 7603 if (const_bounds_p) 7604 { 7605 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain)); 7606 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain)); 7607 } 7608 7609 /* If the constructor has fewer elements than the array, clear 7610 the whole array first. Similarly if this is static 7611 constructor of a non-BLKmode object. */ 7612 if (cleared) 7613 need_to_clear = false; 7614 else if (REG_P (target) && TREE_STATIC (exp)) 7615 need_to_clear = true; 7616 else 7617 { 7618 unsigned HOST_WIDE_INT idx; 7619 HOST_WIDE_INT count = 0, zero_count = 0; 7620 need_to_clear = ! const_bounds_p; 7621 7622 /* This loop is a more accurate version of the loop in 7623 mostly_zeros_p (it handles RANGE_EXPR in an index). It 7624 is also needed to check for missing elements. */ 7625 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value) 7626 { 7627 HOST_WIDE_INT this_node_count; 7628 7629 if (need_to_clear) 7630 break; 7631 7632 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR) 7633 { 7634 tree lo_index = TREE_OPERAND (index, 0); 7635 tree hi_index = TREE_OPERAND (index, 1); 7636 7637 if (! tree_fits_uhwi_p (lo_index) 7638 || ! tree_fits_uhwi_p (hi_index)) 7639 { 7640 need_to_clear = true; 7641 break; 7642 } 7643 7644 this_node_count = (tree_to_uhwi (hi_index) 7645 - tree_to_uhwi (lo_index) + 1); 7646 } 7647 else 7648 this_node_count = 1; 7649 7650 count += this_node_count; 7651 if (mostly_zeros_p (value)) 7652 zero_count += this_node_count; 7653 } 7654 7655 /* Clear the entire array first if there are any missing 7656 elements, or if the incidence of zero elements is >= 7657 75%. */ 7658 if (! need_to_clear 7659 && (count < maxelt - minelt + 1 7660 || 4 * zero_count >= 3 * count)) 7661 need_to_clear = true; 7662 } 7663 7664 if (need_to_clear && maybe_gt (size, 0)) 7665 { 7666 if (REG_P (target)) 7667 emit_move_insn (target, CONST0_RTX (GET_MODE (target))); 7668 else 7669 clear_storage (target, gen_int_mode (size, Pmode), 7670 BLOCK_OP_NORMAL); 7671 cleared = 1; 7672 } 7673 7674 if (!cleared && REG_P (target)) 7675 /* Inform later passes that the old value is dead. */ 7676 emit_clobber (target); 7677 7678 /* Store each element of the constructor into the 7679 corresponding element of TARGET, determined by counting the 7680 elements. */ 7681 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value) 7682 { 7683 machine_mode mode; 7684 poly_int64 bitsize; 7685 HOST_WIDE_INT bitpos; 7686 rtx xtarget = target; 7687 7688 if (cleared && initializer_zerop (value)) 7689 continue; 7690 7691 mode = TYPE_MODE (elttype); 7692 if (mode != BLKmode) 7693 bitsize = GET_MODE_BITSIZE (mode); 7694 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize)) 7695 bitsize = -1; 7696 7697 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR) 7698 { 7699 tree lo_index = TREE_OPERAND (index, 0); 7700 tree hi_index = TREE_OPERAND (index, 1); 7701 rtx index_r, pos_rtx; 7702 HOST_WIDE_INT lo, hi, count; 7703 tree position; 7704 7705 /* If the range is constant and "small", unroll the loop. */ 7706 if (const_bounds_p 7707 && tree_fits_shwi_p (lo_index) 7708 && tree_fits_shwi_p (hi_index) 7709 && (lo = tree_to_shwi (lo_index), 7710 hi = tree_to_shwi (hi_index), 7711 count = hi - lo + 1, 7712 (!MEM_P (target) 7713 || count <= 2 7714 || (tree_fits_uhwi_p (TYPE_SIZE (elttype)) 7715 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count 7716 <= 40 * 8))))) 7717 { 7718 lo -= minelt; hi -= minelt; 7719 for (; lo <= hi; lo++) 7720 { 7721 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype)); 7722 7723 if (MEM_P (target) 7724 && !MEM_KEEP_ALIAS_SET_P (target) 7725 && TREE_CODE (type) == ARRAY_TYPE 7726 && TYPE_NONALIASED_COMPONENT (type)) 7727 { 7728 target = copy_rtx (target); 7729 MEM_KEEP_ALIAS_SET_P (target) = 1; 7730 } 7731 7732 store_constructor_field 7733 (target, bitsize, bitpos, 0, bitregion_end, 7734 mode, value, cleared, 7735 get_alias_set (elttype), reverse); 7736 } 7737 } 7738 else 7739 { 7740 rtx_code_label *loop_start = gen_label_rtx (); 7741 rtx_code_label *loop_end = gen_label_rtx (); 7742 tree exit_cond; 7743 7744 expand_normal (hi_index); 7745 7746 index = build_decl (EXPR_LOCATION (exp), 7747 VAR_DECL, NULL_TREE, domain); 7748 index_r = gen_reg_rtx (promote_decl_mode (index, NULL)); 7749 SET_DECL_RTL (index, index_r); 7750 store_expr (lo_index, index_r, 0, false, reverse); 7751 7752 /* Build the head of the loop. */ 7753 do_pending_stack_adjust (); 7754 emit_label (loop_start); 7755 7756 /* Assign value to element index. */ 7757 position = 7758 fold_convert (ssizetype, 7759 fold_build2 (MINUS_EXPR, 7760 TREE_TYPE (index), 7761 index, 7762 TYPE_MIN_VALUE (domain))); 7763 7764 position = 7765 size_binop (MULT_EXPR, position, 7766 fold_convert (ssizetype, 7767 TYPE_SIZE_UNIT (elttype))); 7768 7769 pos_rtx = expand_normal (position); 7770 xtarget = offset_address (target, pos_rtx, 7771 highest_pow2_factor (position)); 7772 xtarget = adjust_address (xtarget, mode, 0); 7773 if (TREE_CODE (value) == CONSTRUCTOR) 7774 store_constructor (value, xtarget, cleared, 7775 exact_div (bitsize, BITS_PER_UNIT), 7776 reverse); 7777 else 7778 store_expr (value, xtarget, 0, false, reverse); 7779 7780 /* Generate a conditional jump to exit the loop. */ 7781 exit_cond = build2 (LT_EXPR, integer_type_node, 7782 index, hi_index); 7783 jumpif (exit_cond, loop_end, 7784 profile_probability::uninitialized ()); 7785 7786 /* Update the loop counter, and jump to the head of 7787 the loop. */ 7788 expand_assignment (index, 7789 build2 (PLUS_EXPR, TREE_TYPE (index), 7790 index, integer_one_node), 7791 false); 7792 7793 emit_jump (loop_start); 7794 7795 /* Build the end of the loop. */ 7796 emit_label (loop_end); 7797 } 7798 } 7799 else if ((index != 0 && ! tree_fits_shwi_p (index)) 7800 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype))) 7801 { 7802 tree position; 7803 7804 if (index == 0) 7805 index = ssize_int (1); 7806 7807 if (minelt) 7808 index = fold_convert (ssizetype, 7809 fold_build2 (MINUS_EXPR, 7810 TREE_TYPE (index), 7811 index, 7812 TYPE_MIN_VALUE (domain))); 7813 7814 position = 7815 size_binop (MULT_EXPR, index, 7816 fold_convert (ssizetype, 7817 TYPE_SIZE_UNIT (elttype))); 7818 xtarget = offset_address (target, 7819 expand_normal (position), 7820 highest_pow2_factor (position)); 7821 xtarget = adjust_address (xtarget, mode, 0); 7822 store_expr (value, xtarget, 0, false, reverse); 7823 } 7824 else 7825 { 7826 if (index != 0) 7827 bitpos = ((tree_to_shwi (index) - minelt) 7828 * tree_to_uhwi (TYPE_SIZE (elttype))); 7829 else 7830 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype))); 7831 7832 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target) 7833 && TREE_CODE (type) == ARRAY_TYPE 7834 && TYPE_NONALIASED_COMPONENT (type)) 7835 { 7836 target = copy_rtx (target); 7837 MEM_KEEP_ALIAS_SET_P (target) = 1; 7838 } 7839 store_constructor_field (target, bitsize, bitpos, 0, 7840 bitregion_end, mode, value, 7841 cleared, get_alias_set (elttype), 7842 reverse); 7843 } 7844 } 7845 break; 7846 } 7847 7848 case VECTOR_TYPE: 7849 { 7850 unsigned HOST_WIDE_INT idx; 7851 constructor_elt *ce; 7852 int i; 7853 bool need_to_clear; 7854 insn_code icode = CODE_FOR_nothing; 7855 tree elt; 7856 tree elttype = TREE_TYPE (type); 7857 int elt_size = vector_element_bits (type); 7858 machine_mode eltmode = TYPE_MODE (elttype); 7859 HOST_WIDE_INT bitsize; 7860 HOST_WIDE_INT bitpos; 7861 rtvec vector = NULL; 7862 poly_uint64 n_elts; 7863 unsigned HOST_WIDE_INT const_n_elts; 7864 alias_set_type alias; 7865 bool vec_vec_init_p = false; 7866 machine_mode mode = GET_MODE (target); 7867 7868 gcc_assert (eltmode != BLKmode); 7869 7870 /* Try using vec_duplicate_optab for uniform vectors. */ 7871 if (!TREE_SIDE_EFFECTS (exp) 7872 && VECTOR_MODE_P (mode) 7873 && eltmode == GET_MODE_INNER (mode) 7874 && ((icode = optab_handler (vec_duplicate_optab, mode)) 7875 != CODE_FOR_nothing) 7876 && (elt = uniform_vector_p (exp)) 7877 && !VECTOR_TYPE_P (TREE_TYPE (elt))) 7878 { 7879 class expand_operand ops[2]; 7880 create_output_operand (&ops[0], target, mode); 7881 create_input_operand (&ops[1], expand_normal (elt), eltmode); 7882 expand_insn (icode, 2, ops); 7883 if (!rtx_equal_p (target, ops[0].value)) 7884 emit_move_insn (target, ops[0].value); 7885 break; 7886 } 7887 /* Use sign-extension for uniform boolean vectors with 7888 integer modes and single-bit mask entries. 7889 Effectively "vec_duplicate" for bitmasks. */ 7890 if (elt_size == 1 7891 && !TREE_SIDE_EFFECTS (exp) 7892 && VECTOR_BOOLEAN_TYPE_P (type) 7893 && SCALAR_INT_MODE_P (TYPE_MODE (type)) 7894 && (elt = uniform_vector_p (exp)) 7895 && !VECTOR_TYPE_P (TREE_TYPE (elt))) 7896 { 7897 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)), 7898 expand_normal (elt)); 7899 rtx tmp = gen_reg_rtx (mode); 7900 convert_move (tmp, op0, 0); 7901 7902 /* Ensure no excess bits are set. 7903 GCN needs this for nunits < 64. 7904 x86 needs this for nunits < 8. */ 7905 auto nunits = TYPE_VECTOR_SUBPARTS (type).to_constant (); 7906 if (maybe_ne (GET_MODE_PRECISION (mode), nunits)) 7907 tmp = expand_binop (mode, and_optab, tmp, 7908 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1), 7909 target, true, OPTAB_WIDEN); 7910 if (tmp != target) 7911 emit_move_insn (target, tmp); 7912 break; 7913 } 7914 7915 n_elts = TYPE_VECTOR_SUBPARTS (type); 7916 if (REG_P (target) 7917 && VECTOR_MODE_P (mode) 7918 && n_elts.is_constant (&const_n_elts)) 7919 { 7920 machine_mode emode = eltmode; 7921 bool vector_typed_elts_p = false; 7922 7923 if (CONSTRUCTOR_NELTS (exp) 7924 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)) 7925 == VECTOR_TYPE)) 7926 { 7927 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value); 7928 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp) 7929 * TYPE_VECTOR_SUBPARTS (etype), 7930 n_elts)); 7931 emode = TYPE_MODE (etype); 7932 vector_typed_elts_p = true; 7933 } 7934 icode = convert_optab_handler (vec_init_optab, mode, emode); 7935 if (icode != CODE_FOR_nothing) 7936 { 7937 unsigned int n = const_n_elts; 7938 7939 if (vector_typed_elts_p) 7940 { 7941 n = CONSTRUCTOR_NELTS (exp); 7942 vec_vec_init_p = true; 7943 } 7944 vector = rtvec_alloc (n); 7945 for (unsigned int k = 0; k < n; k++) 7946 RTVEC_ELT (vector, k) = CONST0_RTX (emode); 7947 } 7948 } 7949 7950 /* Compute the size of the elements in the CTOR. It differs 7951 from the size of the vector type elements only when the 7952 CTOR elements are vectors themselves. */ 7953 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0 7954 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value) 7955 : elttype); 7956 if (VECTOR_TYPE_P (val_type)) 7957 bitsize = tree_to_uhwi (TYPE_SIZE (val_type)); 7958 else 7959 bitsize = elt_size; 7960 7961 /* If the constructor has fewer elements than the vector, 7962 clear the whole array first. Similarly if this is static 7963 constructor of a non-BLKmode object. */ 7964 if (cleared) 7965 need_to_clear = false; 7966 else if (REG_P (target) && TREE_STATIC (exp)) 7967 need_to_clear = true; 7968 else 7969 { 7970 unsigned HOST_WIDE_INT count = 0, zero_count = 0; 7971 tree value; 7972 7973 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value) 7974 { 7975 int n_elts_here = bitsize / elt_size; 7976 count += n_elts_here; 7977 if (mostly_zeros_p (value)) 7978 zero_count += n_elts_here; 7979 } 7980 7981 /* Clear the entire vector first if there are any missing elements, 7982 or if the incidence of zero elements is >= 75%. */ 7983 need_to_clear = (maybe_lt (count, n_elts) 7984 || 4 * zero_count >= 3 * count); 7985 } 7986 7987 if (need_to_clear && maybe_gt (size, 0) && !vector) 7988 { 7989 if (REG_P (target)) 7990 emit_move_insn (target, CONST0_RTX (mode)); 7991 else 7992 clear_storage (target, gen_int_mode (size, Pmode), 7993 BLOCK_OP_NORMAL); 7994 cleared = 1; 7995 } 7996 7997 /* Inform later passes that the old value is dead. */ 7998 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u)) 7999 { 8000 emit_move_insn (target, CONST0_RTX (mode)); 8001 cleared = 1; 8002 } 8003 8004 if (MEM_P (target)) 8005 alias = MEM_ALIAS_SET (target); 8006 else 8007 alias = get_alias_set (elttype); 8008 8009 /* Store each element of the constructor into the corresponding 8010 element of TARGET, determined by counting the elements. */ 8011 for (idx = 0, i = 0; 8012 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce); 8013 idx++, i += bitsize / elt_size) 8014 { 8015 HOST_WIDE_INT eltpos; 8016 tree value = ce->value; 8017 8018 if (cleared && initializer_zerop (value)) 8019 continue; 8020 8021 if (ce->index) 8022 eltpos = tree_to_uhwi (ce->index); 8023 else 8024 eltpos = i; 8025 8026 if (vector) 8027 { 8028 if (vec_vec_init_p) 8029 { 8030 gcc_assert (ce->index == NULL_TREE); 8031 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE); 8032 eltpos = idx; 8033 } 8034 else 8035 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE); 8036 RTVEC_ELT (vector, eltpos) = expand_normal (value); 8037 } 8038 else 8039 { 8040 machine_mode value_mode 8041 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE 8042 ? TYPE_MODE (TREE_TYPE (value)) : eltmode); 8043 bitpos = eltpos * elt_size; 8044 store_constructor_field (target, bitsize, bitpos, 0, 8045 bitregion_end, value_mode, 8046 value, cleared, alias, reverse); 8047 } 8048 } 8049 8050 if (vector) 8051 emit_insn (GEN_FCN (icode) (target, 8052 gen_rtx_PARALLEL (mode, vector))); 8053 break; 8054 } 8055 8056 default: 8057 gcc_unreachable (); 8058 } 8059 } 8060 8061 /* Store the value of EXP (an expression tree) 8062 into a subfield of TARGET which has mode MODE and occupies 8063 BITSIZE bits, starting BITPOS bits from the start of TARGET. 8064 If MODE is VOIDmode, it means that we are storing into a bit-field. 8065 8066 BITREGION_START is bitpos of the first bitfield in this region. 8067 BITREGION_END is the bitpos of the ending bitfield in this region. 8068 These two fields are 0, if the C++ memory model does not apply, 8069 or we are not interested in keeping track of bitfield regions. 8070 8071 Always return const0_rtx unless we have something particular to 8072 return. 8073 8074 ALIAS_SET is the alias set for the destination. This value will 8075 (in general) be different from that for TARGET, since TARGET is a 8076 reference to the containing structure. 8077 8078 If NONTEMPORAL is true, try generating a nontemporal store. 8079 8080 If REVERSE is true, the store is to be done in reverse order. */ 8081 8082 static rtx 8083 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos, 8084 poly_uint64 bitregion_start, poly_uint64 bitregion_end, 8085 machine_mode mode, tree exp, 8086 alias_set_type alias_set, bool nontemporal, bool reverse) 8087 { 8088 if (TREE_CODE (exp) == ERROR_MARK) 8089 return const0_rtx; 8090 8091 /* If we have nothing to store, do nothing unless the expression has 8092 side-effects. Don't do that for zero sized addressable lhs of 8093 calls. */ 8094 if (known_eq (bitsize, 0) 8095 && (!TREE_ADDRESSABLE (TREE_TYPE (exp)) 8096 || TREE_CODE (exp) != CALL_EXPR)) 8097 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL); 8098 8099 if (GET_CODE (target) == CONCAT) 8100 { 8101 /* We're storing into a struct containing a single __complex. */ 8102 8103 gcc_assert (known_eq (bitpos, 0)); 8104 return store_expr (exp, target, 0, nontemporal, reverse); 8105 } 8106 8107 /* If the structure is in a register or if the component 8108 is a bit field, we cannot use addressing to access it. 8109 Use bit-field techniques or SUBREG to store in it. */ 8110 8111 poly_int64 decl_bitsize; 8112 if (mode == VOIDmode 8113 || (mode != BLKmode && ! direct_store[(int) mode] 8114 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT 8115 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT) 8116 || REG_P (target) 8117 || GET_CODE (target) == SUBREG 8118 /* If the field isn't aligned enough to store as an ordinary memref, 8119 store it as a bit field. */ 8120 || (mode != BLKmode 8121 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode)) 8122 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode))) 8123 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target))) 8124 || !multiple_p (bitpos, BITS_PER_UNIT))) 8125 || (known_size_p (bitsize) 8126 && mode != BLKmode 8127 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize)) 8128 /* If the RHS and field are a constant size and the size of the 8129 RHS isn't the same size as the bitfield, we must use bitfield 8130 operations. */ 8131 || (known_size_p (bitsize) 8132 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp))) 8133 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))), 8134 bitsize) 8135 /* Except for initialization of full bytes from a CONSTRUCTOR, which 8136 we will handle specially below. */ 8137 && !(TREE_CODE (exp) == CONSTRUCTOR 8138 && multiple_p (bitsize, BITS_PER_UNIT)) 8139 /* And except for bitwise copying of TREE_ADDRESSABLE types, 8140 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp) 8141 includes some extra padding. store_expr / expand_expr will in 8142 that case call get_inner_reference that will have the bitsize 8143 we check here and thus the block move will not clobber the 8144 padding that shouldn't be clobbered. In the future we could 8145 replace the TREE_ADDRESSABLE check with a check that 8146 get_base_address needs to live in memory. */ 8147 && (!TREE_ADDRESSABLE (TREE_TYPE (exp)) 8148 || TREE_CODE (exp) != COMPONENT_REF 8149 || !multiple_p (bitsize, BITS_PER_UNIT) 8150 || !multiple_p (bitpos, BITS_PER_UNIT) 8151 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)), 8152 &decl_bitsize) 8153 || maybe_ne (decl_bitsize, bitsize)) 8154 /* A call with an addressable return type and return-slot 8155 optimization must not need bitfield operations but we must 8156 pass down the original target. */ 8157 && (TREE_CODE (exp) != CALL_EXPR 8158 || !TREE_ADDRESSABLE (TREE_TYPE (exp)) 8159 || !CALL_EXPR_RETURN_SLOT_OPT (exp))) 8160 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable 8161 decl we must use bitfield operations. */ 8162 || (known_size_p (bitsize) 8163 && TREE_CODE (exp) == MEM_REF 8164 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR 8165 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) 8166 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) 8167 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode)) 8168 { 8169 rtx temp; 8170 gimple *nop_def; 8171 8172 /* If EXP is a NOP_EXPR of precision less than its mode, then that 8173 implies a mask operation. If the precision is the same size as 8174 the field we're storing into, that mask is redundant. This is 8175 particularly common with bit field assignments generated by the 8176 C front end. */ 8177 nop_def = get_def_for_expr (exp, NOP_EXPR); 8178 if (nop_def) 8179 { 8180 tree type = TREE_TYPE (exp); 8181 if (INTEGRAL_TYPE_P (type) 8182 && maybe_ne (TYPE_PRECISION (type), 8183 GET_MODE_BITSIZE (TYPE_MODE (type))) 8184 && known_eq (bitsize, TYPE_PRECISION (type))) 8185 { 8186 tree op = gimple_assign_rhs1 (nop_def); 8187 type = TREE_TYPE (op); 8188 if (INTEGRAL_TYPE_P (type) 8189 && known_ge (TYPE_PRECISION (type), bitsize)) 8190 exp = op; 8191 } 8192 } 8193 8194 temp = expand_normal (exp); 8195 8196 /* We don't support variable-sized BLKmode bitfields, since our 8197 handling of BLKmode is bound up with the ability to break 8198 things into words. */ 8199 gcc_assert (mode != BLKmode || bitsize.is_constant ()); 8200 8201 /* Handle calls that return values in multiple non-contiguous locations. 8202 The Irix 6 ABI has examples of this. */ 8203 if (GET_CODE (temp) == PARALLEL) 8204 { 8205 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp)); 8206 machine_mode temp_mode = GET_MODE (temp); 8207 if (temp_mode == BLKmode || temp_mode == VOIDmode) 8208 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT); 8209 rtx temp_target = gen_reg_rtx (temp_mode); 8210 emit_group_store (temp_target, temp, TREE_TYPE (exp), size); 8211 temp = temp_target; 8212 } 8213 8214 /* Handle calls that return BLKmode values in registers. */ 8215 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR) 8216 { 8217 rtx temp_target = gen_reg_rtx (GET_MODE (temp)); 8218 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp)); 8219 temp = temp_target; 8220 } 8221 8222 /* If the value has aggregate type and an integral mode then, if BITSIZE 8223 is narrower than this mode and this is for big-endian data, we first 8224 need to put the value into the low-order bits for store_bit_field, 8225 except when MODE is BLKmode and BITSIZE larger than the word size 8226 (see the handling of fields larger than a word in store_bit_field). 8227 Moreover, the field may be not aligned on a byte boundary; in this 8228 case, if it has reverse storage order, it needs to be accessed as a 8229 scalar field with reverse storage order and we must first put the 8230 value into target order. */ 8231 scalar_int_mode temp_mode; 8232 if (AGGREGATE_TYPE_P (TREE_TYPE (exp)) 8233 && is_int_mode (GET_MODE (temp), &temp_mode)) 8234 { 8235 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode); 8236 8237 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp)); 8238 8239 if (reverse) 8240 temp = flip_storage_order (temp_mode, temp); 8241 8242 gcc_checking_assert (known_le (bitsize, size)); 8243 if (maybe_lt (bitsize, size) 8244 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN 8245 /* Use of to_constant for BLKmode was checked above. */ 8246 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD)) 8247 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp, 8248 size - bitsize, NULL_RTX, 1); 8249 } 8250 8251 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */ 8252 if (mode != VOIDmode && mode != BLKmode 8253 && mode != TYPE_MODE (TREE_TYPE (exp))) 8254 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1); 8255 8256 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory 8257 and BITPOS must be aligned on a byte boundary. If so, we simply do 8258 a block copy. Likewise for a BLKmode-like TARGET. */ 8259 if (GET_MODE (temp) == BLKmode 8260 && (GET_MODE (target) == BLKmode 8261 || (MEM_P (target) 8262 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT 8263 && multiple_p (bitpos, BITS_PER_UNIT) 8264 && multiple_p (bitsize, BITS_PER_UNIT)))) 8265 { 8266 gcc_assert (MEM_P (target) && MEM_P (temp)); 8267 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT); 8268 poly_int64 bytesize = bits_to_bytes_round_up (bitsize); 8269 8270 target = adjust_address (target, VOIDmode, bytepos); 8271 emit_block_move (target, temp, 8272 gen_int_mode (bytesize, Pmode), 8273 BLOCK_OP_NORMAL); 8274 8275 return const0_rtx; 8276 } 8277 8278 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the 8279 word size, we need to load the value (see again store_bit_field). */ 8280 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD)) 8281 { 8282 temp_mode = smallest_int_mode_for_size (bitsize); 8283 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode, 8284 temp_mode, false, NULL); 8285 } 8286 8287 /* Store the value in the bitfield. */ 8288 gcc_checking_assert (known_ge (bitpos, 0)); 8289 store_bit_field (target, bitsize, bitpos, 8290 bitregion_start, bitregion_end, 8291 mode, temp, reverse, false); 8292 8293 return const0_rtx; 8294 } 8295 else 8296 { 8297 /* Now build a reference to just the desired component. */ 8298 rtx to_rtx = adjust_address (target, mode, 8299 exact_div (bitpos, BITS_PER_UNIT)); 8300 8301 if (to_rtx == target) 8302 to_rtx = copy_rtx (to_rtx); 8303 8304 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0) 8305 set_mem_alias_set (to_rtx, alias_set); 8306 8307 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR 8308 into a target smaller than its type; handle that case now. */ 8309 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize)) 8310 { 8311 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT); 8312 store_constructor (exp, to_rtx, 0, bytesize, reverse); 8313 return to_rtx; 8314 } 8315 8316 return store_expr (exp, to_rtx, 0, nontemporal, reverse); 8317 } 8318 } 8319 8320 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF, 8322 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these 8323 codes and find the ultimate containing object, which we return. 8324 8325 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the 8326 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the 8327 storage order of the field. 8328 If the position of the field is variable, we store a tree 8329 giving the variable offset (in units) in *POFFSET. 8330 This offset is in addition to the bit position. 8331 If the position is not variable, we store 0 in *POFFSET. 8332 8333 If any of the extraction expressions is volatile, 8334 we store 1 in *PVOLATILEP. Otherwise we don't change that. 8335 8336 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode. 8337 Otherwise, it is a mode that can be used to access the field. 8338 8339 If the field describes a variable-sized object, *PMODE is set to 8340 BLKmode and *PBITSIZE is set to -1. An access cannot be made in 8341 this case, but the address of the object can be found. */ 8342 8343 tree 8344 get_inner_reference (tree exp, poly_int64 *pbitsize, 8345 poly_int64 *pbitpos, tree *poffset, 8346 machine_mode *pmode, int *punsignedp, 8347 int *preversep, int *pvolatilep) 8348 { 8349 tree size_tree = 0; 8350 machine_mode mode = VOIDmode; 8351 bool blkmode_bitfield = false; 8352 tree offset = size_zero_node; 8353 poly_offset_int bit_offset = 0; 8354 8355 /* First get the mode, signedness, storage order and size. We do this from 8356 just the outermost expression. */ 8357 *pbitsize = -1; 8358 if (TREE_CODE (exp) == COMPONENT_REF) 8359 { 8360 tree field = TREE_OPERAND (exp, 1); 8361 size_tree = DECL_SIZE (field); 8362 if (flag_strict_volatile_bitfields > 0 8363 && TREE_THIS_VOLATILE (exp) 8364 && DECL_BIT_FIELD_TYPE (field) 8365 && DECL_MODE (field) != BLKmode) 8366 /* Volatile bitfields should be accessed in the mode of the 8367 field's type, not the mode computed based on the bit 8368 size. */ 8369 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field)); 8370 else if (!DECL_BIT_FIELD (field)) 8371 { 8372 mode = DECL_MODE (field); 8373 /* For vector fields re-check the target flags, as DECL_MODE 8374 could have been set with different target flags than 8375 the current function has. */ 8376 if (VECTOR_TYPE_P (TREE_TYPE (field)) 8377 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field)))) 8378 mode = TYPE_MODE (TREE_TYPE (field)); 8379 } 8380 else if (DECL_MODE (field) == BLKmode) 8381 blkmode_bitfield = true; 8382 8383 *punsignedp = DECL_UNSIGNED (field); 8384 } 8385 else if (TREE_CODE (exp) == BIT_FIELD_REF) 8386 { 8387 size_tree = TREE_OPERAND (exp, 1); 8388 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp)) 8389 || TYPE_UNSIGNED (TREE_TYPE (exp))); 8390 8391 /* For vector element types with the correct size of access or for 8392 vector typed accesses use the mode of the access type. */ 8393 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE 8394 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))) 8395 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp)))) 8396 || VECTOR_TYPE_P (TREE_TYPE (exp))) 8397 mode = TYPE_MODE (TREE_TYPE (exp)); 8398 } 8399 else 8400 { 8401 mode = TYPE_MODE (TREE_TYPE (exp)); 8402 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp)); 8403 8404 if (mode == BLKmode) 8405 size_tree = TYPE_SIZE (TREE_TYPE (exp)); 8406 else 8407 *pbitsize = GET_MODE_BITSIZE (mode); 8408 } 8409 8410 if (size_tree != 0) 8411 { 8412 if (! tree_fits_uhwi_p (size_tree)) 8413 mode = BLKmode, *pbitsize = -1; 8414 else 8415 *pbitsize = tree_to_uhwi (size_tree); 8416 } 8417 8418 *preversep = reverse_storage_order_for_component_p (exp); 8419 8420 /* Compute cumulative bit-offset for nested component-refs and array-refs, 8421 and find the ultimate containing object. */ 8422 while (1) 8423 { 8424 switch (TREE_CODE (exp)) 8425 { 8426 case BIT_FIELD_REF: 8427 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2)); 8428 break; 8429 8430 case COMPONENT_REF: 8431 { 8432 tree field = TREE_OPERAND (exp, 1); 8433 tree this_offset = component_ref_field_offset (exp); 8434 8435 /* If this field hasn't been filled in yet, don't go past it. 8436 This should only happen when folding expressions made during 8437 type construction. */ 8438 if (this_offset == 0) 8439 break; 8440 8441 offset = size_binop (PLUS_EXPR, offset, this_offset); 8442 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field)); 8443 8444 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */ 8445 } 8446 break; 8447 8448 case ARRAY_REF: 8449 case ARRAY_RANGE_REF: 8450 { 8451 tree index = TREE_OPERAND (exp, 1); 8452 tree low_bound = array_ref_low_bound (exp); 8453 tree unit_size = array_ref_element_size (exp); 8454 8455 /* We assume all arrays have sizes that are a multiple of a byte. 8456 First subtract the lower bound, if any, in the type of the 8457 index, then convert to sizetype and multiply by the size of 8458 the array element. */ 8459 if (! integer_zerop (low_bound)) 8460 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index), 8461 index, low_bound); 8462 8463 offset = size_binop (PLUS_EXPR, offset, 8464 size_binop (MULT_EXPR, 8465 fold_convert (sizetype, index), 8466 unit_size)); 8467 } 8468 break; 8469 8470 case REALPART_EXPR: 8471 break; 8472 8473 case IMAGPART_EXPR: 8474 bit_offset += *pbitsize; 8475 break; 8476 8477 case VIEW_CONVERT_EXPR: 8478 break; 8479 8480 case MEM_REF: 8481 /* Hand back the decl for MEM[&decl, off]. */ 8482 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR) 8483 { 8484 tree off = TREE_OPERAND (exp, 1); 8485 if (!integer_zerop (off)) 8486 { 8487 poly_offset_int boff = mem_ref_offset (exp); 8488 boff <<= LOG2_BITS_PER_UNIT; 8489 bit_offset += boff; 8490 } 8491 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); 8492 } 8493 goto done; 8494 8495 default: 8496 goto done; 8497 } 8498 8499 /* If any reference in the chain is volatile, the effect is volatile. */ 8500 if (TREE_THIS_VOLATILE (exp)) 8501 *pvolatilep = 1; 8502 8503 exp = TREE_OPERAND (exp, 0); 8504 } 8505 done: 8506 8507 /* If OFFSET is constant, see if we can return the whole thing as a 8508 constant bit position. Make sure to handle overflow during 8509 this conversion. */ 8510 if (poly_int_tree_p (offset)) 8511 { 8512 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset), 8513 TYPE_PRECISION (sizetype)); 8514 tem <<= LOG2_BITS_PER_UNIT; 8515 tem += bit_offset; 8516 if (tem.to_shwi (pbitpos)) 8517 *poffset = offset = NULL_TREE; 8518 } 8519 8520 /* Otherwise, split it up. */ 8521 if (offset) 8522 { 8523 /* Avoid returning a negative bitpos as this may wreak havoc later. */ 8524 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0)) 8525 { 8526 *pbitpos = num_trailing_bits (bit_offset.force_shwi ()); 8527 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset); 8528 offset = size_binop (PLUS_EXPR, offset, 8529 build_int_cst (sizetype, bytes.force_shwi ())); 8530 } 8531 8532 *poffset = offset; 8533 } 8534 8535 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */ 8536 if (mode == VOIDmode 8537 && blkmode_bitfield 8538 && multiple_p (*pbitpos, BITS_PER_UNIT) 8539 && multiple_p (*pbitsize, BITS_PER_UNIT)) 8540 *pmode = BLKmode; 8541 else 8542 *pmode = mode; 8543 8544 return exp; 8545 } 8546 8547 /* Alignment in bits the TARGET of an assignment may be assumed to have. */ 8548 8549 static unsigned HOST_WIDE_INT 8550 target_align (const_tree target) 8551 { 8552 /* We might have a chain of nested references with intermediate misaligning 8553 bitfields components, so need to recurse to find out. */ 8554 8555 unsigned HOST_WIDE_INT this_align, outer_align; 8556 8557 switch (TREE_CODE (target)) 8558 { 8559 case BIT_FIELD_REF: 8560 return 1; 8561 8562 case COMPONENT_REF: 8563 this_align = DECL_ALIGN (TREE_OPERAND (target, 1)); 8564 outer_align = target_align (TREE_OPERAND (target, 0)); 8565 return MIN (this_align, outer_align); 8566 8567 case ARRAY_REF: 8568 case ARRAY_RANGE_REF: 8569 this_align = TYPE_ALIGN (TREE_TYPE (target)); 8570 outer_align = target_align (TREE_OPERAND (target, 0)); 8571 return MIN (this_align, outer_align); 8572 8573 CASE_CONVERT: 8574 case NON_LVALUE_EXPR: 8575 case VIEW_CONVERT_EXPR: 8576 this_align = TYPE_ALIGN (TREE_TYPE (target)); 8577 outer_align = target_align (TREE_OPERAND (target, 0)); 8578 return MAX (this_align, outer_align); 8579 8580 default: 8581 return TYPE_ALIGN (TREE_TYPE (target)); 8582 } 8583 } 8584 8585 8586 /* Given an rtx VALUE that may contain additions and multiplications, return 8588 an equivalent value that just refers to a register, memory, or constant. 8589 This is done by generating instructions to perform the arithmetic and 8590 returning a pseudo-register containing the value. 8591 8592 The returned value may be a REG, SUBREG, MEM or constant. */ 8593 8594 rtx 8595 force_operand (rtx value, rtx target) 8596 { 8597 rtx op1, op2; 8598 /* Use subtarget as the target for operand 0 of a binary operation. */ 8599 rtx subtarget = get_subtarget (target); 8600 enum rtx_code code = GET_CODE (value); 8601 8602 /* Check for subreg applied to an expression produced by loop optimizer. */ 8603 if (code == SUBREG 8604 && !REG_P (SUBREG_REG (value)) 8605 && !MEM_P (SUBREG_REG (value))) 8606 { 8607 value 8608 = simplify_gen_subreg (GET_MODE (value), 8609 force_reg (GET_MODE (SUBREG_REG (value)), 8610 force_operand (SUBREG_REG (value), 8611 NULL_RTX)), 8612 GET_MODE (SUBREG_REG (value)), 8613 SUBREG_BYTE (value)); 8614 code = GET_CODE (value); 8615 } 8616 8617 /* Check for a PIC address load. */ 8618 if ((code == PLUS || code == MINUS) 8619 && XEXP (value, 0) == pic_offset_table_rtx 8620 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF 8621 || GET_CODE (XEXP (value, 1)) == LABEL_REF 8622 || GET_CODE (XEXP (value, 1)) == CONST)) 8623 { 8624 if (!subtarget) 8625 subtarget = gen_reg_rtx (GET_MODE (value)); 8626 emit_move_insn (subtarget, value); 8627 return subtarget; 8628 } 8629 8630 if (ARITHMETIC_P (value)) 8631 { 8632 op2 = XEXP (value, 1); 8633 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget)) 8634 subtarget = 0; 8635 if (code == MINUS && CONST_INT_P (op2)) 8636 { 8637 code = PLUS; 8638 op2 = negate_rtx (GET_MODE (value), op2); 8639 } 8640 8641 /* Check for an addition with OP2 a constant integer and our first 8642 operand a PLUS of a virtual register and something else. In that 8643 case, we want to emit the sum of the virtual register and the 8644 constant first and then add the other value. This allows virtual 8645 register instantiation to simply modify the constant rather than 8646 creating another one around this addition. */ 8647 if (code == PLUS && CONST_INT_P (op2) 8648 && GET_CODE (XEXP (value, 0)) == PLUS 8649 && REG_P (XEXP (XEXP (value, 0), 0)) 8650 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0))) 8651 { 8652 rtx temp = expand_simple_binop (GET_MODE (value), code, 8653 XEXP (XEXP (value, 0), 0), op2, 8654 subtarget, 0, OPTAB_LIB_WIDEN); 8655 return expand_simple_binop (GET_MODE (value), code, temp, 8656 force_operand (XEXP (XEXP (value, 8657 0), 1), 0), 8658 target, 0, OPTAB_LIB_WIDEN); 8659 } 8660 8661 op1 = force_operand (XEXP (value, 0), subtarget); 8662 op2 = force_operand (op2, NULL_RTX); 8663 switch (code) 8664 { 8665 case MULT: 8666 return expand_mult (GET_MODE (value), op1, op2, target, 1); 8667 case DIV: 8668 if (!INTEGRAL_MODE_P (GET_MODE (value))) 8669 return expand_simple_binop (GET_MODE (value), code, op1, op2, 8670 target, 1, OPTAB_LIB_WIDEN); 8671 else 8672 return expand_divmod (0, 8673 FLOAT_MODE_P (GET_MODE (value)) 8674 ? RDIV_EXPR : TRUNC_DIV_EXPR, 8675 GET_MODE (value), op1, op2, target, 0); 8676 case MOD: 8677 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2, 8678 target, 0); 8679 case UDIV: 8680 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2, 8681 target, 1); 8682 case UMOD: 8683 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2, 8684 target, 1); 8685 case ASHIFTRT: 8686 return expand_simple_binop (GET_MODE (value), code, op1, op2, 8687 target, 0, OPTAB_LIB_WIDEN); 8688 default: 8689 return expand_simple_binop (GET_MODE (value), code, op1, op2, 8690 target, 1, OPTAB_LIB_WIDEN); 8691 } 8692 } 8693 if (UNARY_P (value)) 8694 { 8695 if (!target) 8696 target = gen_reg_rtx (GET_MODE (value)); 8697 op1 = force_operand (XEXP (value, 0), NULL_RTX); 8698 switch (code) 8699 { 8700 case ZERO_EXTEND: 8701 case SIGN_EXTEND: 8702 case TRUNCATE: 8703 case FLOAT_EXTEND: 8704 case FLOAT_TRUNCATE: 8705 convert_move (target, op1, code == ZERO_EXTEND); 8706 return target; 8707 8708 case FIX: 8709 case UNSIGNED_FIX: 8710 expand_fix (target, op1, code == UNSIGNED_FIX); 8711 return target; 8712 8713 case FLOAT: 8714 case UNSIGNED_FLOAT: 8715 expand_float (target, op1, code == UNSIGNED_FLOAT); 8716 return target; 8717 8718 default: 8719 return expand_simple_unop (GET_MODE (value), code, op1, target, 0); 8720 } 8721 } 8722 8723 #ifdef INSN_SCHEDULING 8724 /* On machines that have insn scheduling, we want all memory reference to be 8725 explicit, so we need to deal with such paradoxical SUBREGs. */ 8726 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value))) 8727 value 8728 = simplify_gen_subreg (GET_MODE (value), 8729 force_reg (GET_MODE (SUBREG_REG (value)), 8730 force_operand (SUBREG_REG (value), 8731 NULL_RTX)), 8732 GET_MODE (SUBREG_REG (value)), 8733 SUBREG_BYTE (value)); 8734 #endif 8735 8736 return value; 8737 } 8738 8739 /* Subroutine of expand_expr: return true iff there is no way that 8741 EXP can reference X, which is being modified. TOP_P is nonzero if this 8742 call is going to be used to determine whether we need a temporary 8743 for EXP, as opposed to a recursive call to this function. 8744 8745 It is always safe for this routine to return false since it merely 8746 searches for optimization opportunities. */ 8747 8748 bool 8749 safe_from_p (const_rtx x, tree exp, int top_p) 8750 { 8751 rtx exp_rtl = 0; 8752 int i, nops; 8753 8754 if (x == 0 8755 /* If EXP has varying size, we MUST use a target since we currently 8756 have no way of allocating temporaries of variable size 8757 (except for arrays that have TYPE_ARRAY_MAX_SIZE set). 8758 So we assume here that something at a higher level has prevented a 8759 clash. This is somewhat bogus, but the best we can do. Only 8760 do this when X is BLKmode and when we are at the top level. */ 8761 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp)) 8762 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST 8763 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE 8764 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE 8765 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp))) 8766 != INTEGER_CST) 8767 && GET_MODE (x) == BLKmode) 8768 /* If X is in the outgoing argument area, it is always safe. */ 8769 || (MEM_P (x) 8770 && (XEXP (x, 0) == virtual_outgoing_args_rtx 8771 || (GET_CODE (XEXP (x, 0)) == PLUS 8772 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx)))) 8773 return true; 8774 8775 /* If this is a subreg of a hard register, declare it unsafe, otherwise, 8776 find the underlying pseudo. */ 8777 if (GET_CODE (x) == SUBREG) 8778 { 8779 x = SUBREG_REG (x); 8780 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER) 8781 return false; 8782 } 8783 8784 /* Now look at our tree code and possibly recurse. */ 8785 switch (TREE_CODE_CLASS (TREE_CODE (exp))) 8786 { 8787 case tcc_declaration: 8788 exp_rtl = DECL_RTL_IF_SET (exp); 8789 break; 8790 8791 case tcc_constant: 8792 return true; 8793 8794 case tcc_exceptional: 8795 if (TREE_CODE (exp) == TREE_LIST) 8796 { 8797 while (1) 8798 { 8799 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0)) 8800 return false; 8801 exp = TREE_CHAIN (exp); 8802 if (!exp) 8803 return true; 8804 if (TREE_CODE (exp) != TREE_LIST) 8805 return safe_from_p (x, exp, 0); 8806 } 8807 } 8808 else if (TREE_CODE (exp) == CONSTRUCTOR) 8809 { 8810 constructor_elt *ce; 8811 unsigned HOST_WIDE_INT idx; 8812 8813 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce) 8814 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0)) 8815 || !safe_from_p (x, ce->value, 0)) 8816 return false; 8817 return true; 8818 } 8819 else if (TREE_CODE (exp) == ERROR_MARK) 8820 return true; /* An already-visited SAVE_EXPR? */ 8821 else 8822 return false; 8823 8824 case tcc_statement: 8825 /* The only case we look at here is the DECL_INITIAL inside a 8826 DECL_EXPR. */ 8827 return (TREE_CODE (exp) != DECL_EXPR 8828 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL 8829 || !DECL_INITIAL (DECL_EXPR_DECL (exp)) 8830 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0)); 8831 8832 case tcc_binary: 8833 case tcc_comparison: 8834 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0)) 8835 return false; 8836 /* Fall through. */ 8837 8838 case tcc_unary: 8839 return safe_from_p (x, TREE_OPERAND (exp, 0), 0); 8840 8841 case tcc_expression: 8842 case tcc_reference: 8843 case tcc_vl_exp: 8844 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in 8845 the expression. If it is set, we conflict iff we are that rtx or 8846 both are in memory. Otherwise, we check all operands of the 8847 expression recursively. */ 8848 8849 switch (TREE_CODE (exp)) 8850 { 8851 case ADDR_EXPR: 8852 /* If the operand is static or we are static, we can't conflict. 8853 Likewise if we don't conflict with the operand at all. */ 8854 if (staticp (TREE_OPERAND (exp, 0)) 8855 || TREE_STATIC (exp) 8856 || safe_from_p (x, TREE_OPERAND (exp, 0), 0)) 8857 return true; 8858 8859 /* Otherwise, the only way this can conflict is if we are taking 8860 the address of a DECL a that address if part of X, which is 8861 very rare. */ 8862 exp = TREE_OPERAND (exp, 0); 8863 if (DECL_P (exp)) 8864 { 8865 if (!DECL_RTL_SET_P (exp) 8866 || !MEM_P (DECL_RTL (exp))) 8867 return false; 8868 else 8869 exp_rtl = XEXP (DECL_RTL (exp), 0); 8870 } 8871 break; 8872 8873 case MEM_REF: 8874 if (MEM_P (x) 8875 && alias_sets_conflict_p (MEM_ALIAS_SET (x), 8876 get_alias_set (exp))) 8877 return false; 8878 break; 8879 8880 case CALL_EXPR: 8881 /* Assume that the call will clobber all hard registers and 8882 all of memory. */ 8883 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER) 8884 || MEM_P (x)) 8885 return false; 8886 break; 8887 8888 case WITH_CLEANUP_EXPR: 8889 case CLEANUP_POINT_EXPR: 8890 /* Lowered by gimplify.cc. */ 8891 gcc_unreachable (); 8892 8893 case SAVE_EXPR: 8894 return safe_from_p (x, TREE_OPERAND (exp, 0), 0); 8895 8896 default: 8897 break; 8898 } 8899 8900 /* If we have an rtx, we do not need to scan our operands. */ 8901 if (exp_rtl) 8902 break; 8903 8904 nops = TREE_OPERAND_LENGTH (exp); 8905 for (i = 0; i < nops; i++) 8906 if (TREE_OPERAND (exp, i) != 0 8907 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0)) 8908 return false; 8909 8910 break; 8911 8912 case tcc_type: 8913 /* Should never get a type here. */ 8914 gcc_unreachable (); 8915 } 8916 8917 /* If we have an rtl, find any enclosed object. Then see if we conflict 8918 with it. */ 8919 if (exp_rtl) 8920 { 8921 if (GET_CODE (exp_rtl) == SUBREG) 8922 { 8923 exp_rtl = SUBREG_REG (exp_rtl); 8924 if (REG_P (exp_rtl) 8925 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER) 8926 return false; 8927 } 8928 8929 /* If the rtl is X, then it is not safe. Otherwise, it is unless both 8930 are memory and they conflict. */ 8931 return ! (rtx_equal_p (x, exp_rtl) 8932 || (MEM_P (x) && MEM_P (exp_rtl) 8933 && true_dependence (exp_rtl, VOIDmode, x))); 8934 } 8935 8936 /* If we reach here, it is safe. */ 8937 return true; 8938 } 8939 8940 8941 /* Return the highest power of two that EXP is known to be a multiple of. 8943 This is used in updating alignment of MEMs in array references. */ 8944 8945 unsigned HOST_WIDE_INT 8946 highest_pow2_factor (const_tree exp) 8947 { 8948 unsigned HOST_WIDE_INT ret; 8949 int trailing_zeros = tree_ctz (exp); 8950 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT) 8951 return BIGGEST_ALIGNMENT; 8952 ret = HOST_WIDE_INT_1U << trailing_zeros; 8953 if (ret > BIGGEST_ALIGNMENT) 8954 return BIGGEST_ALIGNMENT; 8955 return ret; 8956 } 8957 8958 /* Similar, except that the alignment requirements of TARGET are 8959 taken into account. Assume it is at least as aligned as its 8960 type, unless it is a COMPONENT_REF in which case the layout of 8961 the structure gives the alignment. */ 8962 8963 static unsigned HOST_WIDE_INT 8964 highest_pow2_factor_for_target (const_tree target, const_tree exp) 8965 { 8966 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT; 8967 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp); 8968 8969 return MAX (factor, talign); 8970 } 8971 8972 /* Convert the tree comparison code TCODE to the rtl one where the 8974 signedness is UNSIGNEDP. */ 8975 8976 static enum rtx_code 8977 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp) 8978 { 8979 enum rtx_code code; 8980 switch (tcode) 8981 { 8982 case EQ_EXPR: 8983 code = EQ; 8984 break; 8985 case NE_EXPR: 8986 code = NE; 8987 break; 8988 case LT_EXPR: 8989 code = unsignedp ? LTU : LT; 8990 break; 8991 case LE_EXPR: 8992 code = unsignedp ? LEU : LE; 8993 break; 8994 case GT_EXPR: 8995 code = unsignedp ? GTU : GT; 8996 break; 8997 case GE_EXPR: 8998 code = unsignedp ? GEU : GE; 8999 break; 9000 case UNORDERED_EXPR: 9001 code = UNORDERED; 9002 break; 9003 case ORDERED_EXPR: 9004 code = ORDERED; 9005 break; 9006 case UNLT_EXPR: 9007 code = UNLT; 9008 break; 9009 case UNLE_EXPR: 9010 code = UNLE; 9011 break; 9012 case UNGT_EXPR: 9013 code = UNGT; 9014 break; 9015 case UNGE_EXPR: 9016 code = UNGE; 9017 break; 9018 case UNEQ_EXPR: 9019 code = UNEQ; 9020 break; 9021 case LTGT_EXPR: 9022 code = LTGT; 9023 break; 9024 9025 default: 9026 gcc_unreachable (); 9027 } 9028 return code; 9029 } 9030 9031 /* Subroutine of expand_expr. Expand the two operands of a binary 9032 expression EXP0 and EXP1 placing the results in OP0 and OP1. 9033 The value may be stored in TARGET if TARGET is nonzero. The 9034 MODIFIER argument is as documented by expand_expr. */ 9035 9036 void 9037 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1, 9038 enum expand_modifier modifier) 9039 { 9040 if (! safe_from_p (target, exp1, 1)) 9041 target = 0; 9042 if (operand_equal_p (exp0, exp1, 0)) 9043 { 9044 *op0 = expand_expr (exp0, target, VOIDmode, modifier); 9045 *op1 = copy_rtx (*op0); 9046 } 9047 else 9048 { 9049 *op0 = expand_expr (exp0, target, VOIDmode, modifier); 9050 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier); 9051 } 9052 } 9053 9054 9055 /* Return a MEM that contains constant EXP. DEFER is as for 9057 output_constant_def and MODIFIER is as for expand_expr. */ 9058 9059 static rtx 9060 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier) 9061 { 9062 rtx mem; 9063 9064 mem = output_constant_def (exp, defer); 9065 if (modifier != EXPAND_INITIALIZER) 9066 mem = use_anchored_address (mem); 9067 return mem; 9068 } 9069 9070 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP. 9071 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */ 9072 9073 static rtx 9074 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode, 9075 enum expand_modifier modifier, addr_space_t as) 9076 { 9077 rtx result, subtarget; 9078 tree inner, offset; 9079 poly_int64 bitsize, bitpos; 9080 int unsignedp, reversep, volatilep = 0; 9081 machine_mode mode1; 9082 9083 /* If we are taking the address of a constant and are at the top level, 9084 we have to use output_constant_def since we can't call force_const_mem 9085 at top level. */ 9086 /* ??? This should be considered a front-end bug. We should not be 9087 generating ADDR_EXPR of something that isn't an LVALUE. The only 9088 exception here is STRING_CST. */ 9089 if (CONSTANT_CLASS_P (exp)) 9090 { 9091 result = XEXP (expand_expr_constant (exp, 0, modifier), 0); 9092 if (modifier < EXPAND_SUM) 9093 result = force_operand (result, target); 9094 return result; 9095 } 9096 9097 /* Everything must be something allowed by is_gimple_addressable. */ 9098 switch (TREE_CODE (exp)) 9099 { 9100 case INDIRECT_REF: 9101 /* This case will happen via recursion for &a->b. */ 9102 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier); 9103 9104 case MEM_REF: 9105 { 9106 tree tem = TREE_OPERAND (exp, 0); 9107 if (!integer_zerop (TREE_OPERAND (exp, 1))) 9108 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1)); 9109 return expand_expr (tem, target, tmode, modifier); 9110 } 9111 9112 case TARGET_MEM_REF: 9113 return addr_for_mem_ref (exp, as, true); 9114 9115 case CONST_DECL: 9116 /* Expand the initializer like constants above. */ 9117 result = XEXP (expand_expr_constant (DECL_INITIAL (exp), 9118 0, modifier), 0); 9119 if (modifier < EXPAND_SUM) 9120 result = force_operand (result, target); 9121 return result; 9122 9123 case REALPART_EXPR: 9124 /* The real part of the complex number is always first, therefore 9125 the address is the same as the address of the parent object. */ 9126 offset = 0; 9127 bitpos = 0; 9128 inner = TREE_OPERAND (exp, 0); 9129 break; 9130 9131 case IMAGPART_EXPR: 9132 /* The imaginary part of the complex number is always second. 9133 The expression is therefore always offset by the size of the 9134 scalar type. */ 9135 offset = 0; 9136 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp))); 9137 inner = TREE_OPERAND (exp, 0); 9138 break; 9139 9140 case COMPOUND_LITERAL_EXPR: 9141 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from 9142 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL 9143 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static 9144 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL; 9145 the initializers aren't gimplified. */ 9146 if (COMPOUND_LITERAL_EXPR_DECL (exp) 9147 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp))) 9148 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp), 9149 target, tmode, modifier, as); 9150 /* FALLTHRU */ 9151 default: 9152 /* If the object is a DECL, then expand it for its rtl. Don't bypass 9153 expand_expr, as that can have various side effects; LABEL_DECLs for 9154 example, may not have their DECL_RTL set yet. Expand the rtl of 9155 CONSTRUCTORs too, which should yield a memory reference for the 9156 constructor's contents. Assume language specific tree nodes can 9157 be expanded in some interesting way. */ 9158 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE); 9159 if (DECL_P (exp) 9160 || TREE_CODE (exp) == CONSTRUCTOR 9161 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR) 9162 { 9163 result = expand_expr (exp, target, tmode, 9164 modifier == EXPAND_INITIALIZER 9165 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS); 9166 9167 /* If the DECL isn't in memory, then the DECL wasn't properly 9168 marked TREE_ADDRESSABLE, which will be either a front-end 9169 or a tree optimizer bug. */ 9170 9171 gcc_assert (MEM_P (result)); 9172 result = XEXP (result, 0); 9173 9174 /* ??? Is this needed anymore? */ 9175 if (DECL_P (exp)) 9176 TREE_USED (exp) = 1; 9177 9178 if (modifier != EXPAND_INITIALIZER 9179 && modifier != EXPAND_CONST_ADDRESS 9180 && modifier != EXPAND_SUM) 9181 result = force_operand (result, target); 9182 return result; 9183 } 9184 9185 /* Pass FALSE as the last argument to get_inner_reference although 9186 we are expanding to RTL. The rationale is that we know how to 9187 handle "aligning nodes" here: we can just bypass them because 9188 they won't change the final object whose address will be returned 9189 (they actually exist only for that purpose). */ 9190 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1, 9191 &unsignedp, &reversep, &volatilep); 9192 break; 9193 } 9194 9195 /* We must have made progress. */ 9196 gcc_assert (inner != exp); 9197 9198 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target; 9199 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than 9200 inner alignment, force the inner to be sufficiently aligned. */ 9201 if (CONSTANT_CLASS_P (inner) 9202 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp))) 9203 { 9204 inner = copy_node (inner); 9205 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner)); 9206 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp))); 9207 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1; 9208 } 9209 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as); 9210 9211 if (offset) 9212 { 9213 rtx tmp; 9214 9215 if (modifier != EXPAND_NORMAL) 9216 result = force_operand (result, NULL); 9217 tmp = expand_expr (offset, NULL_RTX, tmode, 9218 modifier == EXPAND_INITIALIZER 9219 ? EXPAND_INITIALIZER : EXPAND_NORMAL); 9220 9221 /* expand_expr is allowed to return an object in a mode other 9222 than TMODE. If it did, we need to convert. */ 9223 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp)) 9224 tmp = convert_modes (tmode, GET_MODE (tmp), 9225 tmp, TYPE_UNSIGNED (TREE_TYPE (offset))); 9226 result = convert_memory_address_addr_space (tmode, result, as); 9227 tmp = convert_memory_address_addr_space (tmode, tmp, as); 9228 9229 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER) 9230 result = simplify_gen_binary (PLUS, tmode, result, tmp); 9231 else 9232 { 9233 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target; 9234 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget, 9235 1, OPTAB_LIB_WIDEN); 9236 } 9237 } 9238 9239 if (maybe_ne (bitpos, 0)) 9240 { 9241 /* Someone beforehand should have rejected taking the address 9242 of an object that isn't byte-aligned. */ 9243 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT); 9244 result = convert_memory_address_addr_space (tmode, result, as); 9245 result = plus_constant (tmode, result, bytepos); 9246 if (modifier < EXPAND_SUM) 9247 result = force_operand (result, target); 9248 } 9249 9250 return result; 9251 } 9252 9253 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR. 9254 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */ 9255 9256 static rtx 9257 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode, 9258 enum expand_modifier modifier) 9259 { 9260 addr_space_t as = ADDR_SPACE_GENERIC; 9261 scalar_int_mode address_mode = Pmode; 9262 scalar_int_mode pointer_mode = ptr_mode; 9263 machine_mode rmode; 9264 rtx result; 9265 9266 /* Target mode of VOIDmode says "whatever's natural". */ 9267 if (tmode == VOIDmode) 9268 tmode = TYPE_MODE (TREE_TYPE (exp)); 9269 9270 if (POINTER_TYPE_P (TREE_TYPE (exp))) 9271 { 9272 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp))); 9273 address_mode = targetm.addr_space.address_mode (as); 9274 pointer_mode = targetm.addr_space.pointer_mode (as); 9275 } 9276 9277 /* We can get called with some Weird Things if the user does silliness 9278 like "(short) &a". In that case, convert_memory_address won't do 9279 the right thing, so ignore the given target mode. */ 9280 scalar_int_mode new_tmode = (tmode == pointer_mode 9281 ? pointer_mode 9282 : address_mode); 9283 9284 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target, 9285 new_tmode, modifier, as); 9286 9287 /* Despite expand_expr claims concerning ignoring TMODE when not 9288 strictly convenient, stuff breaks if we don't honor it. Note 9289 that combined with the above, we only do this for pointer modes. */ 9290 rmode = GET_MODE (result); 9291 if (rmode == VOIDmode) 9292 rmode = new_tmode; 9293 if (rmode != new_tmode) 9294 result = convert_memory_address_addr_space (new_tmode, result, as); 9295 9296 return result; 9297 } 9298 9299 /* Generate code for computing CONSTRUCTOR EXP. 9300 An rtx for the computed value is returned. If AVOID_TEMP_MEM 9301 is TRUE, instead of creating a temporary variable in memory 9302 NULL is returned and the caller needs to handle it differently. */ 9303 9304 static rtx 9305 expand_constructor (tree exp, rtx target, enum expand_modifier modifier, 9306 bool avoid_temp_mem) 9307 { 9308 tree type = TREE_TYPE (exp); 9309 machine_mode mode = TYPE_MODE (type); 9310 9311 /* Try to avoid creating a temporary at all. This is possible 9312 if all of the initializer is zero. 9313 FIXME: try to handle all [0..255] initializers we can handle 9314 with memset. */ 9315 if (TREE_STATIC (exp) 9316 && !TREE_ADDRESSABLE (exp) 9317 && target != 0 && mode == BLKmode 9318 && all_zeros_p (exp)) 9319 { 9320 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL); 9321 return target; 9322 } 9323 9324 /* All elts simple constants => refer to a constant in memory. But 9325 if this is a non-BLKmode mode, let it store a field at a time 9326 since that should make a CONST_INT, CONST_WIDE_INT or 9327 CONST_DOUBLE when we fold. Likewise, if we have a target we can 9328 use, it is best to store directly into the target unless the type 9329 is large enough that memcpy will be used. If we are making an 9330 initializer and all operands are constant, put it in memory as 9331 well. 9332 9333 FIXME: Avoid trying to fill vector constructors piece-meal. 9334 Output them with output_constant_def below unless we're sure 9335 they're zeros. This should go away when vector initializers 9336 are treated like VECTOR_CST instead of arrays. */ 9337 if ((TREE_STATIC (exp) 9338 && ((mode == BLKmode 9339 && ! (target != 0 && safe_from_p (target, exp, 1))) 9340 || TREE_ADDRESSABLE (exp) 9341 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) 9342 && (! can_move_by_pieces 9343 (tree_to_uhwi (TYPE_SIZE_UNIT (type)), 9344 TYPE_ALIGN (type))) 9345 && ! mostly_zeros_p (exp)))) 9346 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS) 9347 && TREE_CONSTANT (exp))) 9348 { 9349 rtx constructor; 9350 9351 if (avoid_temp_mem) 9352 return NULL_RTX; 9353 9354 constructor = expand_expr_constant (exp, 1, modifier); 9355 9356 if (modifier != EXPAND_CONST_ADDRESS 9357 && modifier != EXPAND_INITIALIZER 9358 && modifier != EXPAND_SUM) 9359 constructor = validize_mem (constructor); 9360 9361 return constructor; 9362 } 9363 9364 /* If the CTOR is available in static storage and not mostly 9365 zeros and we can move it by pieces prefer to do so since 9366 that's usually more efficient than performing a series of 9367 stores from immediates. */ 9368 if (avoid_temp_mem 9369 && TREE_STATIC (exp) 9370 && TREE_CONSTANT (exp) 9371 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) 9372 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)), 9373 TYPE_ALIGN (type)) 9374 && ! mostly_zeros_p (exp)) 9375 return NULL_RTX; 9376 9377 /* Handle calls that pass values in multiple non-contiguous 9378 locations. The Irix 6 ABI has examples of this. */ 9379 if (target == 0 || ! safe_from_p (target, exp, 1) 9380 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM 9381 /* Also make a temporary if the store is to volatile memory, to 9382 avoid individual accesses to aggregate members. */ 9383 || (GET_CODE (target) == MEM 9384 && MEM_VOLATILE_P (target) 9385 && !TREE_ADDRESSABLE (TREE_TYPE (exp)))) 9386 { 9387 if (avoid_temp_mem) 9388 return NULL_RTX; 9389 9390 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1); 9391 } 9392 9393 store_constructor (exp, target, 0, int_expr_size (exp), false); 9394 return target; 9395 } 9396 9397 9398 /* expand_expr: generate code for computing expression EXP. 9399 An rtx for the computed value is returned. The value is never null. 9400 In the case of a void EXP, const0_rtx is returned. 9401 9402 The value may be stored in TARGET if TARGET is nonzero. 9403 TARGET is just a suggestion; callers must assume that 9404 the rtx returned may not be the same as TARGET. 9405 9406 If TARGET is CONST0_RTX, it means that the value will be ignored. 9407 9408 If TMODE is not VOIDmode, it suggests generating the 9409 result in mode TMODE. But this is done only when convenient. 9410 Otherwise, TMODE is ignored and the value generated in its natural mode. 9411 TMODE is just a suggestion; callers must assume that 9412 the rtx returned may not have mode TMODE. 9413 9414 Note that TARGET may have neither TMODE nor MODE. In that case, it 9415 probably will not be used. 9416 9417 If MODIFIER is EXPAND_SUM then when EXP is an addition 9418 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...)) 9419 or a nest of (PLUS ...) and (MINUS ...) where the terms are 9420 products as above, or REG or MEM, or constant. 9421 Ordinarily in such cases we would output mul or add instructions 9422 and then return a pseudo reg containing the sum. 9423 9424 EXPAND_INITIALIZER is much like EXPAND_SUM except that 9425 it also marks a label as absolutely required (it can't be dead). 9426 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns. 9427 This is used for outputting expressions used in initializers. 9428 9429 EXPAND_CONST_ADDRESS says that it is okay to return a MEM 9430 with a constant address even if that address is not normally legitimate. 9431 EXPAND_INITIALIZER and EXPAND_SUM also have this effect. 9432 9433 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for 9434 a call parameter. Such targets require special care as we haven't yet 9435 marked TARGET so that it's safe from being trashed by libcalls. We 9436 don't want to use TARGET for anything but the final result; 9437 Intermediate values must go elsewhere. Additionally, calls to 9438 emit_block_move will be flagged with BLOCK_OP_CALL_PARM. 9439 9440 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid 9441 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the 9442 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a 9443 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on 9444 recursively. 9445 If the result can be stored at TARGET, and ALT_RTL is non-NULL, 9446 then *ALT_RTL is set to TARGET (before legitimziation). 9447 9448 If INNER_REFERENCE_P is true, we are expanding an inner reference. 9449 In this case, we don't adjust a returned MEM rtx that wouldn't be 9450 sufficiently aligned for its mode; instead, it's up to the caller 9451 to deal with it afterwards. This is used to make sure that unaligned 9452 base objects for which out-of-bounds accesses are supported, for 9453 example record types with trailing arrays, aren't realigned behind 9454 the back of the caller. 9455 The normal operating mode is to pass FALSE for this parameter. */ 9456 9457 rtx 9458 expand_expr_real (tree exp, rtx target, machine_mode tmode, 9459 enum expand_modifier modifier, rtx *alt_rtl, 9460 bool inner_reference_p) 9461 { 9462 rtx ret; 9463 9464 /* Handle ERROR_MARK before anybody tries to access its type. */ 9465 if (TREE_CODE (exp) == ERROR_MARK 9466 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK)) 9467 { 9468 ret = CONST0_RTX (tmode); 9469 return ret ? ret : const0_rtx; 9470 } 9471 9472 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl, 9473 inner_reference_p); 9474 return ret; 9475 } 9476 9477 /* Try to expand the conditional expression which is represented by 9478 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds 9479 return the rtl reg which represents the result. Otherwise return 9480 NULL_RTX. */ 9481 9482 static rtx 9483 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED, 9484 tree treeop1 ATTRIBUTE_UNUSED, 9485 tree treeop2 ATTRIBUTE_UNUSED) 9486 { 9487 rtx insn; 9488 rtx op00, op01, op1, op2; 9489 enum rtx_code comparison_code; 9490 machine_mode comparison_mode; 9491 gimple *srcstmt; 9492 rtx temp; 9493 tree type = TREE_TYPE (treeop1); 9494 int unsignedp = TYPE_UNSIGNED (type); 9495 machine_mode mode = TYPE_MODE (type); 9496 machine_mode orig_mode = mode; 9497 static bool expanding_cond_expr_using_cmove = false; 9498 9499 /* Conditional move expansion can end up TERing two operands which, 9500 when recursively hitting conditional expressions can result in 9501 exponential behavior if the cmove expansion ultimatively fails. 9502 It's hardly profitable to TER a cmove into a cmove so avoid doing 9503 that by failing early if we end up recursing. */ 9504 if (expanding_cond_expr_using_cmove) 9505 return NULL_RTX; 9506 9507 /* If we cannot do a conditional move on the mode, try doing it 9508 with the promoted mode. */ 9509 if (!can_conditionally_move_p (mode)) 9510 { 9511 mode = promote_mode (type, mode, &unsignedp); 9512 if (!can_conditionally_move_p (mode)) 9513 return NULL_RTX; 9514 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */ 9515 } 9516 else 9517 temp = assign_temp (type, 0, 1); 9518 9519 expanding_cond_expr_using_cmove = true; 9520 start_sequence (); 9521 expand_operands (treeop1, treeop2, 9522 mode == orig_mode ? temp : NULL_RTX, &op1, &op2, 9523 EXPAND_NORMAL); 9524 9525 if (TREE_CODE (treeop0) == SSA_NAME 9526 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison))) 9527 { 9528 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt)); 9529 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt); 9530 op00 = expand_normal (gimple_assign_rhs1 (srcstmt)); 9531 op01 = expand_normal (gimple_assign_rhs2 (srcstmt)); 9532 comparison_mode = TYPE_MODE (type); 9533 unsignedp = TYPE_UNSIGNED (type); 9534 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp); 9535 } 9536 else if (COMPARISON_CLASS_P (treeop0)) 9537 { 9538 type = TREE_TYPE (TREE_OPERAND (treeop0, 0)); 9539 enum tree_code cmpcode = TREE_CODE (treeop0); 9540 op00 = expand_normal (TREE_OPERAND (treeop0, 0)); 9541 op01 = expand_normal (TREE_OPERAND (treeop0, 1)); 9542 unsignedp = TYPE_UNSIGNED (type); 9543 comparison_mode = TYPE_MODE (type); 9544 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp); 9545 } 9546 else 9547 { 9548 op00 = expand_normal (treeop0); 9549 op01 = const0_rtx; 9550 comparison_code = NE; 9551 comparison_mode = GET_MODE (op00); 9552 if (comparison_mode == VOIDmode) 9553 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0)); 9554 } 9555 expanding_cond_expr_using_cmove = false; 9556 9557 if (GET_MODE (op1) != mode) 9558 op1 = gen_lowpart (mode, op1); 9559 9560 if (GET_MODE (op2) != mode) 9561 op2 = gen_lowpart (mode, op2); 9562 9563 /* Try to emit the conditional move. */ 9564 insn = emit_conditional_move (temp, 9565 { comparison_code, op00, op01, 9566 comparison_mode }, 9567 op1, op2, mode, 9568 unsignedp); 9569 9570 /* If we could do the conditional move, emit the sequence, 9571 and return. */ 9572 if (insn) 9573 { 9574 rtx_insn *seq = get_insns (); 9575 end_sequence (); 9576 emit_insn (seq); 9577 return convert_modes (orig_mode, mode, temp, 0); 9578 } 9579 9580 /* Otherwise discard the sequence and fall back to code with 9581 branches. */ 9582 end_sequence (); 9583 return NULL_RTX; 9584 } 9585 9586 /* A helper function for expand_expr_real_2 to be used with a 9587 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP 9588 is nonzero, with alignment ALIGN in bits. 9589 Store the value at TARGET if possible (if TARGET is nonzero). 9590 Regardless of TARGET, we return the rtx for where the value is placed. 9591 If the result can be stored at TARGET, and ALT_RTL is non-NULL, 9592 then *ALT_RTL is set to TARGET (before legitimziation). */ 9593 9594 static rtx 9595 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp, 9596 unsigned int align, rtx target, rtx *alt_rtl) 9597 { 9598 enum insn_code icode; 9599 9600 if ((icode = optab_handler (movmisalign_optab, mode)) 9601 != CODE_FOR_nothing) 9602 { 9603 class expand_operand ops[2]; 9604 9605 /* We've already validated the memory, and we're creating a 9606 new pseudo destination. The predicates really can't fail, 9607 nor can the generator. */ 9608 create_output_operand (&ops[0], NULL_RTX, mode); 9609 create_fixed_operand (&ops[1], temp); 9610 expand_insn (icode, 2, ops); 9611 temp = ops[0].value; 9612 } 9613 else if (targetm.slow_unaligned_access (mode, align)) 9614 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode), 9615 0, unsignedp, target, 9616 mode, mode, false, alt_rtl); 9617 return temp; 9618 } 9619 9620 /* Helper function of expand_expr_2, expand a division or modulo. 9621 op0 and op1 should be already expanded treeop0 and treeop1, using 9622 expand_operands. */ 9623 9624 static rtx 9625 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0, 9626 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp) 9627 { 9628 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR 9629 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR); 9630 if (SCALAR_INT_MODE_P (mode) 9631 && optimize >= 2 9632 && get_range_pos_neg (treeop0) == 1 9633 && get_range_pos_neg (treeop1) == 1) 9634 { 9635 /* If both arguments are known to be positive when interpreted 9636 as signed, we can expand it as both signed and unsigned 9637 division or modulo. Choose the cheaper sequence in that case. */ 9638 bool speed_p = optimize_insn_for_speed_p (); 9639 do_pending_stack_adjust (); 9640 start_sequence (); 9641 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1); 9642 rtx_insn *uns_insns = get_insns (); 9643 end_sequence (); 9644 start_sequence (); 9645 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0); 9646 rtx_insn *sgn_insns = get_insns (); 9647 end_sequence (); 9648 unsigned uns_cost = seq_cost (uns_insns, speed_p); 9649 unsigned sgn_cost = seq_cost (sgn_insns, speed_p); 9650 9651 /* If costs are the same then use as tie breaker the other other 9652 factor. */ 9653 if (uns_cost == sgn_cost) 9654 { 9655 uns_cost = seq_cost (uns_insns, !speed_p); 9656 sgn_cost = seq_cost (sgn_insns, !speed_p); 9657 } 9658 9659 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp)) 9660 { 9661 emit_insn (uns_insns); 9662 return uns_ret; 9663 } 9664 emit_insn (sgn_insns); 9665 return sgn_ret; 9666 } 9667 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp); 9668 } 9669 9670 rtx 9671 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode, 9672 enum expand_modifier modifier) 9673 { 9674 rtx op0, op1, op2, temp; 9675 rtx_code_label *lab; 9676 tree type; 9677 int unsignedp; 9678 machine_mode mode; 9679 scalar_int_mode int_mode; 9680 enum tree_code code = ops->code; 9681 optab this_optab; 9682 rtx subtarget, original_target; 9683 int ignore; 9684 bool reduce_bit_field; 9685 location_t loc = ops->location; 9686 tree treeop0, treeop1, treeop2; 9687 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \ 9688 ? reduce_to_bit_field_precision ((expr), \ 9689 target, \ 9690 type) \ 9691 : (expr)) 9692 9693 type = ops->type; 9694 mode = TYPE_MODE (type); 9695 unsignedp = TYPE_UNSIGNED (type); 9696 9697 treeop0 = ops->op0; 9698 treeop1 = ops->op1; 9699 treeop2 = ops->op2; 9700 9701 /* We should be called only on simple (binary or unary) expressions, 9702 exactly those that are valid in gimple expressions that aren't 9703 GIMPLE_SINGLE_RHS (or invalid). */ 9704 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS 9705 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS 9706 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS); 9707 9708 ignore = (target == const0_rtx 9709 || ((CONVERT_EXPR_CODE_P (code) 9710 || code == COND_EXPR || code == VIEW_CONVERT_EXPR) 9711 && TREE_CODE (type) == VOID_TYPE)); 9712 9713 /* We should be called only if we need the result. */ 9714 gcc_assert (!ignore); 9715 9716 /* An operation in what may be a bit-field type needs the 9717 result to be reduced to the precision of the bit-field type, 9718 which is narrower than that of the type's mode. */ 9719 reduce_bit_field = (INTEGRAL_TYPE_P (type) 9720 && !type_has_mode_precision_p (type)); 9721 9722 if (reduce_bit_field 9723 && (modifier == EXPAND_STACK_PARM 9724 || (target && GET_MODE (target) != mode))) 9725 target = 0; 9726 9727 /* Use subtarget as the target for operand 0 of a binary operation. */ 9728 subtarget = get_subtarget (target); 9729 original_target = target; 9730 9731 switch (code) 9732 { 9733 case NON_LVALUE_EXPR: 9734 case PAREN_EXPR: 9735 CASE_CONVERT: 9736 if (treeop0 == error_mark_node) 9737 return const0_rtx; 9738 9739 if (TREE_CODE (type) == UNION_TYPE) 9740 { 9741 tree valtype = TREE_TYPE (treeop0); 9742 9743 /* If both input and output are BLKmode, this conversion isn't doing 9744 anything except possibly changing memory attribute. */ 9745 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode) 9746 { 9747 rtx result = expand_expr (treeop0, target, tmode, 9748 modifier); 9749 9750 result = copy_rtx (result); 9751 set_mem_attributes (result, type, 0); 9752 return result; 9753 } 9754 9755 if (target == 0) 9756 { 9757 if (TYPE_MODE (type) != BLKmode) 9758 target = gen_reg_rtx (TYPE_MODE (type)); 9759 else 9760 target = assign_temp (type, 1, 1); 9761 } 9762 9763 if (MEM_P (target)) 9764 /* Store data into beginning of memory target. */ 9765 store_expr (treeop0, 9766 adjust_address (target, TYPE_MODE (valtype), 0), 9767 modifier == EXPAND_STACK_PARM, 9768 false, TYPE_REVERSE_STORAGE_ORDER (type)); 9769 9770 else 9771 { 9772 gcc_assert (REG_P (target) 9773 && !TYPE_REVERSE_STORAGE_ORDER (type)); 9774 9775 /* Store this field into a union of the proper type. */ 9776 poly_uint64 op0_size 9777 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0))); 9778 poly_uint64 union_size = GET_MODE_BITSIZE (mode); 9779 store_field (target, 9780 /* The conversion must be constructed so that 9781 we know at compile time how many bits 9782 to preserve. */ 9783 ordered_min (op0_size, union_size), 9784 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, 9785 false, false); 9786 } 9787 9788 /* Return the entire union. */ 9789 return target; 9790 } 9791 9792 if (mode == TYPE_MODE (TREE_TYPE (treeop0))) 9793 { 9794 op0 = expand_expr (treeop0, target, VOIDmode, 9795 modifier); 9796 9797 return REDUCE_BIT_FIELD (op0); 9798 } 9799 9800 op0 = expand_expr (treeop0, NULL_RTX, mode, 9801 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier); 9802 if (GET_MODE (op0) == mode) 9803 ; 9804 9805 /* If OP0 is a constant, just convert it into the proper mode. */ 9806 else if (CONSTANT_P (op0)) 9807 { 9808 tree inner_type = TREE_TYPE (treeop0); 9809 machine_mode inner_mode = GET_MODE (op0); 9810 9811 if (inner_mode == VOIDmode) 9812 inner_mode = TYPE_MODE (inner_type); 9813 9814 if (modifier == EXPAND_INITIALIZER) 9815 op0 = lowpart_subreg (mode, op0, inner_mode); 9816 else 9817 op0= convert_modes (mode, inner_mode, op0, 9818 TYPE_UNSIGNED (inner_type)); 9819 } 9820 9821 else if (modifier == EXPAND_INITIALIZER) 9822 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0)) 9823 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0); 9824 9825 else if (target == 0) 9826 op0 = convert_to_mode (mode, op0, 9827 TYPE_UNSIGNED (TREE_TYPE 9828 (treeop0))); 9829 else 9830 { 9831 convert_move (target, op0, 9832 TYPE_UNSIGNED (TREE_TYPE (treeop0))); 9833 op0 = target; 9834 } 9835 9836 return REDUCE_BIT_FIELD (op0); 9837 9838 case ADDR_SPACE_CONVERT_EXPR: 9839 { 9840 tree treeop0_type = TREE_TYPE (treeop0); 9841 9842 gcc_assert (POINTER_TYPE_P (type)); 9843 gcc_assert (POINTER_TYPE_P (treeop0_type)); 9844 9845 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type)); 9846 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type)); 9847 9848 /* Conversions between pointers to the same address space should 9849 have been implemented via CONVERT_EXPR / NOP_EXPR. */ 9850 gcc_assert (as_to != as_from); 9851 9852 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier); 9853 9854 /* Ask target code to handle conversion between pointers 9855 to overlapping address spaces. */ 9856 if (targetm.addr_space.subset_p (as_to, as_from) 9857 || targetm.addr_space.subset_p (as_from, as_to)) 9858 { 9859 op0 = targetm.addr_space.convert (op0, treeop0_type, type); 9860 } 9861 else 9862 { 9863 /* For disjoint address spaces, converting anything but a null 9864 pointer invokes undefined behavior. We truncate or extend the 9865 value as if we'd converted via integers, which handles 0 as 9866 required, and all others as the programmer likely expects. */ 9867 #ifndef POINTERS_EXTEND_UNSIGNED 9868 const int POINTERS_EXTEND_UNSIGNED = 1; 9869 #endif 9870 op0 = convert_modes (mode, TYPE_MODE (treeop0_type), 9871 op0, POINTERS_EXTEND_UNSIGNED); 9872 } 9873 gcc_assert (op0); 9874 return op0; 9875 } 9876 9877 case POINTER_PLUS_EXPR: 9878 /* Even though the sizetype mode and the pointer's mode can be different 9879 expand is able to handle this correctly and get the correct result out 9880 of the PLUS_EXPR code. */ 9881 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR 9882 if sizetype precision is smaller than pointer precision. */ 9883 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type)) 9884 treeop1 = fold_convert_loc (loc, type, 9885 fold_convert_loc (loc, ssizetype, 9886 treeop1)); 9887 /* If sizetype precision is larger than pointer precision, truncate the 9888 offset to have matching modes. */ 9889 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type)) 9890 treeop1 = fold_convert_loc (loc, type, treeop1); 9891 /* FALLTHRU */ 9892 9893 case PLUS_EXPR: 9894 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and 9895 something else, make sure we add the register to the constant and 9896 then to the other thing. This case can occur during strength 9897 reduction and doing it this way will produce better code if the 9898 frame pointer or argument pointer is eliminated. 9899 9900 fold-const.cc will ensure that the constant is always in the inner 9901 PLUS_EXPR, so the only case we need to do anything about is if 9902 sp, ap, or fp is our second argument, in which case we must swap 9903 the innermost first argument and our second argument. */ 9904 9905 if (TREE_CODE (treeop0) == PLUS_EXPR 9906 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST 9907 && VAR_P (treeop1) 9908 && (DECL_RTL (treeop1) == frame_pointer_rtx 9909 || DECL_RTL (treeop1) == stack_pointer_rtx 9910 || DECL_RTL (treeop1) == arg_pointer_rtx)) 9911 { 9912 gcc_unreachable (); 9913 } 9914 9915 /* If the result is to be ptr_mode and we are adding an integer to 9916 something, we might be forming a constant. So try to use 9917 plus_constant. If it produces a sum and we can't accept it, 9918 use force_operand. This allows P = &ARR[const] to generate 9919 efficient code on machines where a SYMBOL_REF is not a valid 9920 address. 9921 9922 If this is an EXPAND_SUM call, always return the sum. */ 9923 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER 9924 || (mode == ptr_mode && (unsignedp || ! flag_trapv))) 9925 { 9926 if (modifier == EXPAND_STACK_PARM) 9927 target = 0; 9928 if (TREE_CODE (treeop0) == INTEGER_CST 9929 && HWI_COMPUTABLE_MODE_P (mode) 9930 && TREE_CONSTANT (treeop1)) 9931 { 9932 rtx constant_part; 9933 HOST_WIDE_INT wc; 9934 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1)); 9935 9936 op1 = expand_expr (treeop1, subtarget, VOIDmode, 9937 EXPAND_SUM); 9938 /* Use wi::shwi to ensure that the constant is 9939 truncated according to the mode of OP1, then sign extended 9940 to a HOST_WIDE_INT. Using the constant directly can result 9941 in non-canonical RTL in a 64x32 cross compile. */ 9942 wc = TREE_INT_CST_LOW (treeop0); 9943 constant_part = 9944 immed_wide_int_const (wi::shwi (wc, wmode), wmode); 9945 op1 = plus_constant (mode, op1, INTVAL (constant_part)); 9946 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER) 9947 op1 = force_operand (op1, target); 9948 return REDUCE_BIT_FIELD (op1); 9949 } 9950 9951 else if (TREE_CODE (treeop1) == INTEGER_CST 9952 && HWI_COMPUTABLE_MODE_P (mode) 9953 && TREE_CONSTANT (treeop0)) 9954 { 9955 rtx constant_part; 9956 HOST_WIDE_INT wc; 9957 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0)); 9958 9959 op0 = expand_expr (treeop0, subtarget, VOIDmode, 9960 (modifier == EXPAND_INITIALIZER 9961 ? EXPAND_INITIALIZER : EXPAND_SUM)); 9962 if (! CONSTANT_P (op0)) 9963 { 9964 op1 = expand_expr (treeop1, NULL_RTX, 9965 VOIDmode, modifier); 9966 /* Return a PLUS if modifier says it's OK. */ 9967 if (modifier == EXPAND_SUM 9968 || modifier == EXPAND_INITIALIZER) 9969 return simplify_gen_binary (PLUS, mode, op0, op1); 9970 goto binop2; 9971 } 9972 /* Use wi::shwi to ensure that the constant is 9973 truncated according to the mode of OP1, then sign extended 9974 to a HOST_WIDE_INT. Using the constant directly can result 9975 in non-canonical RTL in a 64x32 cross compile. */ 9976 wc = TREE_INT_CST_LOW (treeop1); 9977 constant_part 9978 = immed_wide_int_const (wi::shwi (wc, wmode), wmode); 9979 op0 = plus_constant (mode, op0, INTVAL (constant_part)); 9980 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER) 9981 op0 = force_operand (op0, target); 9982 return REDUCE_BIT_FIELD (op0); 9983 } 9984 } 9985 9986 /* Use TER to expand pointer addition of a negated value 9987 as pointer subtraction. */ 9988 if ((POINTER_TYPE_P (TREE_TYPE (treeop0)) 9989 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE 9990 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0))))) 9991 && TREE_CODE (treeop1) == SSA_NAME 9992 && TYPE_MODE (TREE_TYPE (treeop0)) 9993 == TYPE_MODE (TREE_TYPE (treeop1))) 9994 { 9995 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR); 9996 if (def) 9997 { 9998 treeop1 = gimple_assign_rhs1 (def); 9999 code = MINUS_EXPR; 10000 goto do_minus; 10001 } 10002 } 10003 10004 /* No sense saving up arithmetic to be done 10005 if it's all in the wrong mode to form part of an address. 10006 And force_operand won't know whether to sign-extend or 10007 zero-extend. */ 10008 if (modifier != EXPAND_INITIALIZER 10009 && (modifier != EXPAND_SUM || mode != ptr_mode)) 10010 { 10011 expand_operands (treeop0, treeop1, 10012 subtarget, &op0, &op1, modifier); 10013 if (op0 == const0_rtx) 10014 return op1; 10015 if (op1 == const0_rtx) 10016 return op0; 10017 goto binop2; 10018 } 10019 10020 expand_operands (treeop0, treeop1, 10021 subtarget, &op0, &op1, modifier); 10022 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1)); 10023 10024 case MINUS_EXPR: 10025 case POINTER_DIFF_EXPR: 10026 do_minus: 10027 /* For initializers, we are allowed to return a MINUS of two 10028 symbolic constants. Here we handle all cases when both operands 10029 are constant. */ 10030 /* Handle difference of two symbolic constants, 10031 for the sake of an initializer. */ 10032 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER) 10033 && really_constant_p (treeop0) 10034 && really_constant_p (treeop1)) 10035 { 10036 expand_operands (treeop0, treeop1, 10037 NULL_RTX, &op0, &op1, modifier); 10038 return simplify_gen_binary (MINUS, mode, op0, op1); 10039 } 10040 10041 /* No sense saving up arithmetic to be done 10042 if it's all in the wrong mode to form part of an address. 10043 And force_operand won't know whether to sign-extend or 10044 zero-extend. */ 10045 if (modifier != EXPAND_INITIALIZER 10046 && (modifier != EXPAND_SUM || mode != ptr_mode)) 10047 goto binop; 10048 10049 expand_operands (treeop0, treeop1, 10050 subtarget, &op0, &op1, modifier); 10051 10052 /* Convert A - const to A + (-const). */ 10053 if (CONST_INT_P (op1)) 10054 { 10055 op1 = negate_rtx (mode, op1); 10056 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1)); 10057 } 10058 10059 goto binop2; 10060 10061 case WIDEN_MULT_PLUS_EXPR: 10062 case WIDEN_MULT_MINUS_EXPR: 10063 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10064 op2 = expand_normal (treeop2); 10065 target = expand_widen_pattern_expr (ops, op0, op1, op2, 10066 target, unsignedp); 10067 return target; 10068 10069 case WIDEN_MULT_EXPR: 10070 /* If first operand is constant, swap them. 10071 Thus the following special case checks need only 10072 check the second operand. */ 10073 if (TREE_CODE (treeop0) == INTEGER_CST) 10074 std::swap (treeop0, treeop1); 10075 10076 /* First, check if we have a multiplication of one signed and one 10077 unsigned operand. */ 10078 if (TREE_CODE (treeop1) != INTEGER_CST 10079 && (TYPE_UNSIGNED (TREE_TYPE (treeop0)) 10080 != TYPE_UNSIGNED (TREE_TYPE (treeop1)))) 10081 { 10082 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0)); 10083 this_optab = usmul_widen_optab; 10084 if (find_widening_optab_handler (this_optab, mode, innermode) 10085 != CODE_FOR_nothing) 10086 { 10087 if (TYPE_UNSIGNED (TREE_TYPE (treeop0))) 10088 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, 10089 EXPAND_NORMAL); 10090 else 10091 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0, 10092 EXPAND_NORMAL); 10093 /* op0 and op1 might still be constant, despite the above 10094 != INTEGER_CST check. Handle it. */ 10095 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode) 10096 { 10097 op0 = convert_modes (mode, innermode, op0, true); 10098 op1 = convert_modes (mode, innermode, op1, false); 10099 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, 10100 target, unsignedp)); 10101 } 10102 goto binop3; 10103 } 10104 } 10105 /* Check for a multiplication with matching signedness. */ 10106 else if ((TREE_CODE (treeop1) == INTEGER_CST 10107 && int_fits_type_p (treeop1, TREE_TYPE (treeop0))) 10108 || (TYPE_UNSIGNED (TREE_TYPE (treeop1)) 10109 == TYPE_UNSIGNED (TREE_TYPE (treeop0)))) 10110 { 10111 tree op0type = TREE_TYPE (treeop0); 10112 machine_mode innermode = TYPE_MODE (op0type); 10113 bool zextend_p = TYPE_UNSIGNED (op0type); 10114 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab; 10115 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab; 10116 10117 if (TREE_CODE (treeop0) != INTEGER_CST) 10118 { 10119 if (find_widening_optab_handler (this_optab, mode, innermode) 10120 != CODE_FOR_nothing) 10121 { 10122 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, 10123 EXPAND_NORMAL); 10124 /* op0 and op1 might still be constant, despite the above 10125 != INTEGER_CST check. Handle it. */ 10126 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode) 10127 { 10128 widen_mult_const: 10129 op0 = convert_modes (mode, innermode, op0, zextend_p); 10130 op1 10131 = convert_modes (mode, innermode, op1, 10132 TYPE_UNSIGNED (TREE_TYPE (treeop1))); 10133 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, 10134 target, 10135 unsignedp)); 10136 } 10137 temp = expand_widening_mult (mode, op0, op1, target, 10138 unsignedp, this_optab); 10139 return REDUCE_BIT_FIELD (temp); 10140 } 10141 if (find_widening_optab_handler (other_optab, mode, innermode) 10142 != CODE_FOR_nothing 10143 && innermode == word_mode) 10144 { 10145 rtx htem, hipart; 10146 op0 = expand_normal (treeop0); 10147 op1 = expand_normal (treeop1); 10148 /* op0 and op1 might be constants, despite the above 10149 != INTEGER_CST check. Handle it. */ 10150 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode) 10151 goto widen_mult_const; 10152 temp = expand_binop (mode, other_optab, op0, op1, target, 10153 unsignedp, OPTAB_LIB_WIDEN); 10154 hipart = gen_highpart (word_mode, temp); 10155 htem = expand_mult_highpart_adjust (word_mode, hipart, 10156 op0, op1, hipart, 10157 zextend_p); 10158 if (htem != hipart) 10159 emit_move_insn (hipart, htem); 10160 return REDUCE_BIT_FIELD (temp); 10161 } 10162 } 10163 } 10164 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0); 10165 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1); 10166 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL); 10167 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp)); 10168 10169 case MULT_EXPR: 10170 /* If this is a fixed-point operation, then we cannot use the code 10171 below because "expand_mult" doesn't support sat/no-sat fixed-point 10172 multiplications. */ 10173 if (ALL_FIXED_POINT_MODE_P (mode)) 10174 goto binop; 10175 10176 /* If first operand is constant, swap them. 10177 Thus the following special case checks need only 10178 check the second operand. */ 10179 if (TREE_CODE (treeop0) == INTEGER_CST) 10180 std::swap (treeop0, treeop1); 10181 10182 /* Attempt to return something suitable for generating an 10183 indexed address, for machines that support that. */ 10184 10185 if (modifier == EXPAND_SUM && mode == ptr_mode 10186 && tree_fits_shwi_p (treeop1)) 10187 { 10188 tree exp1 = treeop1; 10189 10190 op0 = expand_expr (treeop0, subtarget, VOIDmode, 10191 EXPAND_SUM); 10192 10193 if (!REG_P (op0)) 10194 op0 = force_operand (op0, NULL_RTX); 10195 if (!REG_P (op0)) 10196 op0 = copy_to_mode_reg (mode, op0); 10197 10198 op1 = gen_int_mode (tree_to_shwi (exp1), 10199 TYPE_MODE (TREE_TYPE (exp1))); 10200 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1)); 10201 } 10202 10203 if (modifier == EXPAND_STACK_PARM) 10204 target = 0; 10205 10206 if (SCALAR_INT_MODE_P (mode) && optimize >= 2) 10207 { 10208 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR); 10209 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR); 10210 if (def_stmt0 10211 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0)) 10212 def_stmt0 = NULL; 10213 if (def_stmt1 10214 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0)) 10215 def_stmt1 = NULL; 10216 10217 if (def_stmt0 || def_stmt1) 10218 { 10219 /* X / Y * Y can be expanded as X - X % Y too. 10220 Choose the cheaper sequence of those two. */ 10221 if (def_stmt0) 10222 treeop0 = gimple_assign_rhs1 (def_stmt0); 10223 else 10224 { 10225 treeop1 = treeop0; 10226 treeop0 = gimple_assign_rhs1 (def_stmt1); 10227 } 10228 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, 10229 EXPAND_NORMAL); 10230 bool speed_p = optimize_insn_for_speed_p (); 10231 do_pending_stack_adjust (); 10232 start_sequence (); 10233 rtx divmul_ret 10234 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1, 10235 op0, op1, NULL_RTX, unsignedp); 10236 divmul_ret = expand_mult (mode, divmul_ret, op1, target, 10237 unsignedp); 10238 rtx_insn *divmul_insns = get_insns (); 10239 end_sequence (); 10240 start_sequence (); 10241 rtx modsub_ret 10242 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1, 10243 op0, op1, NULL_RTX, unsignedp); 10244 this_optab = optab_for_tree_code (MINUS_EXPR, type, 10245 optab_default); 10246 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret, 10247 target, unsignedp, OPTAB_LIB_WIDEN); 10248 rtx_insn *modsub_insns = get_insns (); 10249 end_sequence (); 10250 unsigned divmul_cost = seq_cost (divmul_insns, speed_p); 10251 unsigned modsub_cost = seq_cost (modsub_insns, speed_p); 10252 /* If costs are the same then use as tie breaker the other other 10253 factor. */ 10254 if (divmul_cost == modsub_cost) 10255 { 10256 divmul_cost = seq_cost (divmul_insns, !speed_p); 10257 modsub_cost = seq_cost (modsub_insns, !speed_p); 10258 } 10259 10260 if (divmul_cost <= modsub_cost) 10261 { 10262 emit_insn (divmul_insns); 10263 return REDUCE_BIT_FIELD (divmul_ret); 10264 } 10265 emit_insn (modsub_insns); 10266 return REDUCE_BIT_FIELD (modsub_ret); 10267 } 10268 } 10269 10270 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL); 10271 10272 /* Expand X*Y as X&-Y when Y must be zero or one. */ 10273 if (SCALAR_INT_MODE_P (mode)) 10274 { 10275 bool gimple_zero_one_valued_p (tree, tree (*)(tree)); 10276 bool bit0_p = gimple_zero_one_valued_p (treeop0, nullptr); 10277 bool bit1_p = gimple_zero_one_valued_p (treeop1, nullptr); 10278 10279 /* Expand X*Y as X&Y when both X and Y must be zero or one. */ 10280 if (bit0_p && bit1_p) 10281 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target)); 10282 10283 if (bit0_p || bit1_p) 10284 { 10285 bool speed = optimize_insn_for_speed_p (); 10286 int cost = add_cost (speed, mode) + neg_cost (speed, mode); 10287 struct algorithm algorithm; 10288 enum mult_variant variant; 10289 if (CONST_INT_P (op1) 10290 ? !choose_mult_variant (mode, INTVAL (op1), 10291 &algorithm, &variant, cost) 10292 : cost < mul_cost (speed, mode)) 10293 { 10294 temp = bit0_p ? expand_and (mode, negate_rtx (mode, op0), 10295 op1, target) 10296 : expand_and (mode, op0, 10297 negate_rtx (mode, op1), 10298 target); 10299 return REDUCE_BIT_FIELD (temp); 10300 } 10301 } 10302 } 10303 10304 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp)); 10305 10306 case TRUNC_MOD_EXPR: 10307 case FLOOR_MOD_EXPR: 10308 case CEIL_MOD_EXPR: 10309 case ROUND_MOD_EXPR: 10310 10311 case TRUNC_DIV_EXPR: 10312 case FLOOR_DIV_EXPR: 10313 case CEIL_DIV_EXPR: 10314 case ROUND_DIV_EXPR: 10315 case EXACT_DIV_EXPR: 10316 /* If this is a fixed-point operation, then we cannot use the code 10317 below because "expand_divmod" doesn't support sat/no-sat fixed-point 10318 divisions. */ 10319 if (ALL_FIXED_POINT_MODE_P (mode)) 10320 goto binop; 10321 10322 if (modifier == EXPAND_STACK_PARM) 10323 target = 0; 10324 /* Possible optimization: compute the dividend with EXPAND_SUM 10325 then if the divisor is constant can optimize the case 10326 where some terms of the dividend have coeffs divisible by it. */ 10327 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL); 10328 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1, 10329 target, unsignedp); 10330 10331 case RDIV_EXPR: 10332 goto binop; 10333 10334 case MULT_HIGHPART_EXPR: 10335 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL); 10336 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp); 10337 gcc_assert (temp); 10338 return temp; 10339 10340 case FIXED_CONVERT_EXPR: 10341 op0 = expand_normal (treeop0); 10342 if (target == 0 || modifier == EXPAND_STACK_PARM) 10343 target = gen_reg_rtx (mode); 10344 10345 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE 10346 && TYPE_UNSIGNED (TREE_TYPE (treeop0))) 10347 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))) 10348 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type)); 10349 else 10350 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type)); 10351 return target; 10352 10353 case FIX_TRUNC_EXPR: 10354 op0 = expand_normal (treeop0); 10355 if (target == 0 || modifier == EXPAND_STACK_PARM) 10356 target = gen_reg_rtx (mode); 10357 expand_fix (target, op0, unsignedp); 10358 return target; 10359 10360 case FLOAT_EXPR: 10361 op0 = expand_normal (treeop0); 10362 if (target == 0 || modifier == EXPAND_STACK_PARM) 10363 target = gen_reg_rtx (mode); 10364 /* expand_float can't figure out what to do if FROM has VOIDmode. 10365 So give it the correct mode. With -O, cse will optimize this. */ 10366 if (GET_MODE (op0) == VOIDmode) 10367 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)), 10368 op0); 10369 expand_float (target, op0, 10370 TYPE_UNSIGNED (TREE_TYPE (treeop0))); 10371 return target; 10372 10373 case NEGATE_EXPR: 10374 op0 = expand_expr (treeop0, subtarget, 10375 VOIDmode, EXPAND_NORMAL); 10376 if (modifier == EXPAND_STACK_PARM) 10377 target = 0; 10378 temp = expand_unop (mode, 10379 optab_for_tree_code (NEGATE_EXPR, type, 10380 optab_default), 10381 op0, target, 0); 10382 gcc_assert (temp); 10383 return REDUCE_BIT_FIELD (temp); 10384 10385 case ABS_EXPR: 10386 case ABSU_EXPR: 10387 op0 = expand_expr (treeop0, subtarget, 10388 VOIDmode, EXPAND_NORMAL); 10389 if (modifier == EXPAND_STACK_PARM) 10390 target = 0; 10391 10392 /* ABS_EXPR is not valid for complex arguments. */ 10393 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT 10394 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT); 10395 10396 /* Unsigned abs is simply the operand. Testing here means we don't 10397 risk generating incorrect code below. */ 10398 if (TYPE_UNSIGNED (TREE_TYPE (treeop0))) 10399 return op0; 10400 10401 return expand_abs (mode, op0, target, unsignedp, 10402 safe_from_p (target, treeop0, 1)); 10403 10404 case MAX_EXPR: 10405 case MIN_EXPR: 10406 target = original_target; 10407 if (target == 0 10408 || modifier == EXPAND_STACK_PARM 10409 || (MEM_P (target) && MEM_VOLATILE_P (target)) 10410 || GET_MODE (target) != mode 10411 || (REG_P (target) 10412 && REGNO (target) < FIRST_PSEUDO_REGISTER)) 10413 target = gen_reg_rtx (mode); 10414 expand_operands (treeop0, treeop1, 10415 target, &op0, &op1, EXPAND_NORMAL); 10416 10417 /* First try to do it with a special MIN or MAX instruction. 10418 If that does not win, use a conditional jump to select the proper 10419 value. */ 10420 this_optab = optab_for_tree_code (code, type, optab_default); 10421 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp, 10422 OPTAB_WIDEN); 10423 if (temp != 0) 10424 return temp; 10425 10426 if (VECTOR_TYPE_P (type)) 10427 gcc_unreachable (); 10428 10429 /* At this point, a MEM target is no longer useful; we will get better 10430 code without it. */ 10431 10432 if (! REG_P (target)) 10433 target = gen_reg_rtx (mode); 10434 10435 /* If op1 was placed in target, swap op0 and op1. */ 10436 if (target != op0 && target == op1) 10437 std::swap (op0, op1); 10438 10439 /* We generate better code and avoid problems with op1 mentioning 10440 target by forcing op1 into a pseudo if it isn't a constant. */ 10441 if (! CONSTANT_P (op1)) 10442 op1 = force_reg (mode, op1); 10443 10444 { 10445 enum rtx_code comparison_code; 10446 rtx cmpop1 = op1; 10447 10448 if (code == MAX_EXPR) 10449 comparison_code = unsignedp ? GEU : GE; 10450 else 10451 comparison_code = unsignedp ? LEU : LE; 10452 10453 /* Canonicalize to comparisons against 0. */ 10454 if (op1 == const1_rtx) 10455 { 10456 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1) 10457 or (a != 0 ? a : 1) for unsigned. 10458 For MIN we are safe converting (a <= 1 ? a : 1) 10459 into (a <= 0 ? a : 1) */ 10460 cmpop1 = const0_rtx; 10461 if (code == MAX_EXPR) 10462 comparison_code = unsignedp ? NE : GT; 10463 } 10464 if (op1 == constm1_rtx && !unsignedp) 10465 { 10466 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1) 10467 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */ 10468 cmpop1 = const0_rtx; 10469 if (code == MIN_EXPR) 10470 comparison_code = LT; 10471 } 10472 10473 /* Use a conditional move if possible. */ 10474 if (can_conditionally_move_p (mode)) 10475 { 10476 rtx insn; 10477 10478 start_sequence (); 10479 10480 /* Try to emit the conditional move. */ 10481 insn = emit_conditional_move (target, 10482 { comparison_code, 10483 op0, cmpop1, mode }, 10484 op0, op1, mode, 10485 unsignedp); 10486 10487 /* If we could do the conditional move, emit the sequence, 10488 and return. */ 10489 if (insn) 10490 { 10491 rtx_insn *seq = get_insns (); 10492 end_sequence (); 10493 emit_insn (seq); 10494 return target; 10495 } 10496 10497 /* Otherwise discard the sequence and fall back to code with 10498 branches. */ 10499 end_sequence (); 10500 } 10501 10502 if (target != op0) 10503 emit_move_insn (target, op0); 10504 10505 lab = gen_label_rtx (); 10506 do_compare_rtx_and_jump (target, cmpop1, comparison_code, 10507 unsignedp, mode, NULL_RTX, NULL, lab, 10508 profile_probability::uninitialized ()); 10509 } 10510 emit_move_insn (target, op1); 10511 emit_label (lab); 10512 return target; 10513 10514 case BIT_NOT_EXPR: 10515 op0 = expand_expr (treeop0, subtarget, 10516 VOIDmode, EXPAND_NORMAL); 10517 if (modifier == EXPAND_STACK_PARM) 10518 target = 0; 10519 /* In case we have to reduce the result to bitfield precision 10520 for unsigned bitfield expand this as XOR with a proper constant 10521 instead. */ 10522 if (reduce_bit_field && TYPE_UNSIGNED (type)) 10523 { 10524 int_mode = SCALAR_INT_TYPE_MODE (type); 10525 wide_int mask = wi::mask (TYPE_PRECISION (type), 10526 false, GET_MODE_PRECISION (int_mode)); 10527 10528 temp = expand_binop (int_mode, xor_optab, op0, 10529 immed_wide_int_const (mask, int_mode), 10530 target, 1, OPTAB_LIB_WIDEN); 10531 } 10532 else 10533 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1); 10534 gcc_assert (temp); 10535 return temp; 10536 10537 /* ??? Can optimize bitwise operations with one arg constant. 10538 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b) 10539 and (a bitwise1 b) bitwise2 b (etc) 10540 but that is probably not worth while. */ 10541 10542 case BIT_AND_EXPR: 10543 case BIT_IOR_EXPR: 10544 case BIT_XOR_EXPR: 10545 goto binop; 10546 10547 case LROTATE_EXPR: 10548 case RROTATE_EXPR: 10549 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type)) 10550 || type_has_mode_precision_p (type)); 10551 /* fall through */ 10552 10553 case LSHIFT_EXPR: 10554 case RSHIFT_EXPR: 10555 { 10556 /* If this is a fixed-point operation, then we cannot use the code 10557 below because "expand_shift" doesn't support sat/no-sat fixed-point 10558 shifts. */ 10559 if (ALL_FIXED_POINT_MODE_P (mode)) 10560 goto binop; 10561 10562 if (! safe_from_p (subtarget, treeop1, 1)) 10563 subtarget = 0; 10564 if (modifier == EXPAND_STACK_PARM) 10565 target = 0; 10566 op0 = expand_expr (treeop0, subtarget, 10567 VOIDmode, EXPAND_NORMAL); 10568 10569 /* Left shift optimization when shifting across word_size boundary. 10570 10571 If mode == GET_MODE_WIDER_MODE (word_mode), then normally 10572 there isn't native instruction to support this wide mode 10573 left shift. Given below scenario: 10574 10575 Type A = (Type) B << C 10576 10577 |< T >| 10578 | dest_high | dest_low | 10579 10580 | word_size | 10581 10582 If the shift amount C caused we shift B to across the word 10583 size boundary, i.e part of B shifted into high half of 10584 destination register, and part of B remains in the low 10585 half, then GCC will use the following left shift expand 10586 logic: 10587 10588 1. Initialize dest_low to B. 10589 2. Initialize every bit of dest_high to the sign bit of B. 10590 3. Logic left shift dest_low by C bit to finalize dest_low. 10591 The value of dest_low before this shift is kept in a temp D. 10592 4. Logic left shift dest_high by C. 10593 5. Logic right shift D by (word_size - C). 10594 6. Or the result of 4 and 5 to finalize dest_high. 10595 10596 While, by checking gimple statements, if operand B is 10597 coming from signed extension, then we can simplify above 10598 expand logic into: 10599 10600 1. dest_high = src_low >> (word_size - C). 10601 2. dest_low = src_low << C. 10602 10603 We can use one arithmetic right shift to finish all the 10604 purpose of steps 2, 4, 5, 6, thus we reduce the steps 10605 needed from 6 into 2. 10606 10607 The case is similar for zero extension, except that we 10608 initialize dest_high to zero rather than copies of the sign 10609 bit from B. Furthermore, we need to use a logical right shift 10610 in this case. 10611 10612 The choice of sign-extension versus zero-extension is 10613 determined entirely by whether or not B is signed and is 10614 independent of the current setting of unsignedp. */ 10615 10616 temp = NULL_RTX; 10617 if (code == LSHIFT_EXPR 10618 && target 10619 && REG_P (target) 10620 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode) 10621 && mode == int_mode 10622 && TREE_CONSTANT (treeop1) 10623 && TREE_CODE (treeop0) == SSA_NAME) 10624 { 10625 gimple *def = SSA_NAME_DEF_STMT (treeop0); 10626 if (is_gimple_assign (def) 10627 && gimple_assign_rhs_code (def) == NOP_EXPR) 10628 { 10629 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE 10630 (TREE_TYPE (gimple_assign_rhs1 (def))); 10631 10632 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode) 10633 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode) 10634 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode)) 10635 >= GET_MODE_BITSIZE (word_mode))) 10636 { 10637 rtx_insn *seq, *seq_old; 10638 poly_uint64 high_off = subreg_highpart_offset (word_mode, 10639 int_mode); 10640 bool extend_unsigned 10641 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def))); 10642 rtx low = lowpart_subreg (word_mode, op0, int_mode); 10643 rtx dest_low = lowpart_subreg (word_mode, target, int_mode); 10644 rtx dest_high = simplify_gen_subreg (word_mode, target, 10645 int_mode, high_off); 10646 HOST_WIDE_INT ramount = (BITS_PER_WORD 10647 - TREE_INT_CST_LOW (treeop1)); 10648 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount); 10649 10650 start_sequence (); 10651 /* dest_high = src_low >> (word_size - C). */ 10652 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low, 10653 rshift, dest_high, 10654 extend_unsigned); 10655 if (temp != dest_high) 10656 emit_move_insn (dest_high, temp); 10657 10658 /* dest_low = src_low << C. */ 10659 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low, 10660 treeop1, dest_low, unsignedp); 10661 if (temp != dest_low) 10662 emit_move_insn (dest_low, temp); 10663 10664 seq = get_insns (); 10665 end_sequence (); 10666 temp = target ; 10667 10668 if (have_insn_for (ASHIFT, int_mode)) 10669 { 10670 bool speed_p = optimize_insn_for_speed_p (); 10671 start_sequence (); 10672 rtx ret_old = expand_variable_shift (code, int_mode, 10673 op0, treeop1, 10674 target, 10675 unsignedp); 10676 10677 seq_old = get_insns (); 10678 end_sequence (); 10679 if (seq_cost (seq, speed_p) 10680 >= seq_cost (seq_old, speed_p)) 10681 { 10682 seq = seq_old; 10683 temp = ret_old; 10684 } 10685 } 10686 emit_insn (seq); 10687 } 10688 } 10689 } 10690 10691 if (temp == NULL_RTX) 10692 temp = expand_variable_shift (code, mode, op0, treeop1, target, 10693 unsignedp); 10694 if (code == LSHIFT_EXPR) 10695 temp = REDUCE_BIT_FIELD (temp); 10696 return temp; 10697 } 10698 10699 /* Could determine the answer when only additive constants differ. Also, 10700 the addition of one can be handled by changing the condition. */ 10701 case LT_EXPR: 10702 case LE_EXPR: 10703 case GT_EXPR: 10704 case GE_EXPR: 10705 case EQ_EXPR: 10706 case NE_EXPR: 10707 case UNORDERED_EXPR: 10708 case ORDERED_EXPR: 10709 case UNLT_EXPR: 10710 case UNLE_EXPR: 10711 case UNGT_EXPR: 10712 case UNGE_EXPR: 10713 case UNEQ_EXPR: 10714 case LTGT_EXPR: 10715 { 10716 temp = do_store_flag (ops, 10717 modifier != EXPAND_STACK_PARM ? target : NULL_RTX, 10718 tmode != VOIDmode ? tmode : mode); 10719 if (temp) 10720 return temp; 10721 10722 /* Use a compare and a jump for BLKmode comparisons, or for function 10723 type comparisons is have_canonicalize_funcptr_for_compare. */ 10724 10725 if ((target == 0 10726 || modifier == EXPAND_STACK_PARM 10727 || ! safe_from_p (target, treeop0, 1) 10728 || ! safe_from_p (target, treeop1, 1) 10729 /* Make sure we don't have a hard reg (such as function's return 10730 value) live across basic blocks, if not optimizing. */ 10731 || (!optimize && REG_P (target) 10732 && REGNO (target) < FIRST_PSEUDO_REGISTER))) 10733 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode); 10734 10735 emit_move_insn (target, const0_rtx); 10736 10737 rtx_code_label *lab1 = gen_label_rtx (); 10738 jumpifnot_1 (code, treeop0, treeop1, lab1, 10739 profile_probability::uninitialized ()); 10740 10741 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) 10742 emit_move_insn (target, constm1_rtx); 10743 else 10744 emit_move_insn (target, const1_rtx); 10745 10746 emit_label (lab1); 10747 return target; 10748 } 10749 case COMPLEX_EXPR: 10750 /* Get the rtx code of the operands. */ 10751 op0 = expand_normal (treeop0); 10752 op1 = expand_normal (treeop1); 10753 10754 if (!target) 10755 target = gen_reg_rtx (TYPE_MODE (type)); 10756 else 10757 /* If target overlaps with op1, then either we need to force 10758 op1 into a pseudo (if target also overlaps with op0), 10759 or write the complex parts in reverse order. */ 10760 switch (GET_CODE (target)) 10761 { 10762 case CONCAT: 10763 if (reg_overlap_mentioned_p (XEXP (target, 0), op1)) 10764 { 10765 if (reg_overlap_mentioned_p (XEXP (target, 1), op0)) 10766 { 10767 complex_expr_force_op1: 10768 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target))); 10769 emit_move_insn (temp, op1); 10770 op1 = temp; 10771 break; 10772 } 10773 complex_expr_swap_order: 10774 /* Move the imaginary (op1) and real (op0) parts to their 10775 location. */ 10776 write_complex_part (target, op1, true, true); 10777 write_complex_part (target, op0, false, false); 10778 10779 return target; 10780 } 10781 break; 10782 case MEM: 10783 temp = adjust_address_nv (target, 10784 GET_MODE_INNER (GET_MODE (target)), 0); 10785 if (reg_overlap_mentioned_p (temp, op1)) 10786 { 10787 scalar_mode imode = GET_MODE_INNER (GET_MODE (target)); 10788 temp = adjust_address_nv (target, imode, 10789 GET_MODE_SIZE (imode)); 10790 if (reg_overlap_mentioned_p (temp, op0)) 10791 goto complex_expr_force_op1; 10792 goto complex_expr_swap_order; 10793 } 10794 break; 10795 default: 10796 if (reg_overlap_mentioned_p (target, op1)) 10797 { 10798 if (reg_overlap_mentioned_p (target, op0)) 10799 goto complex_expr_force_op1; 10800 goto complex_expr_swap_order; 10801 } 10802 break; 10803 } 10804 10805 /* Move the real (op0) and imaginary (op1) parts to their location. */ 10806 write_complex_part (target, op0, false, true); 10807 write_complex_part (target, op1, true, false); 10808 10809 return target; 10810 10811 case WIDEN_SUM_EXPR: 10812 { 10813 tree oprnd0 = treeop0; 10814 tree oprnd1 = treeop1; 10815 10816 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10817 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1, 10818 target, unsignedp); 10819 return target; 10820 } 10821 10822 case VEC_UNPACK_HI_EXPR: 10823 case VEC_UNPACK_LO_EXPR: 10824 case VEC_UNPACK_FIX_TRUNC_HI_EXPR: 10825 case VEC_UNPACK_FIX_TRUNC_LO_EXPR: 10826 { 10827 op0 = expand_normal (treeop0); 10828 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX, 10829 target, unsignedp); 10830 gcc_assert (temp); 10831 return temp; 10832 } 10833 10834 case VEC_UNPACK_FLOAT_HI_EXPR: 10835 case VEC_UNPACK_FLOAT_LO_EXPR: 10836 { 10837 op0 = expand_normal (treeop0); 10838 /* The signedness is determined from input operand. */ 10839 temp = expand_widen_pattern_expr 10840 (ops, op0, NULL_RTX, NULL_RTX, 10841 target, TYPE_UNSIGNED (TREE_TYPE (treeop0))); 10842 10843 gcc_assert (temp); 10844 return temp; 10845 } 10846 10847 case VEC_WIDEN_MULT_HI_EXPR: 10848 case VEC_WIDEN_MULT_LO_EXPR: 10849 case VEC_WIDEN_MULT_EVEN_EXPR: 10850 case VEC_WIDEN_MULT_ODD_EXPR: 10851 case VEC_WIDEN_LSHIFT_HI_EXPR: 10852 case VEC_WIDEN_LSHIFT_LO_EXPR: 10853 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10854 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX, 10855 target, unsignedp); 10856 gcc_assert (target); 10857 return target; 10858 10859 case VEC_PACK_SAT_EXPR: 10860 case VEC_PACK_FIX_TRUNC_EXPR: 10861 mode = TYPE_MODE (TREE_TYPE (treeop0)); 10862 subtarget = NULL_RTX; 10863 goto binop; 10864 10865 case VEC_PACK_TRUNC_EXPR: 10866 if (VECTOR_BOOLEAN_TYPE_P (type) 10867 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0)) 10868 && mode == TYPE_MODE (TREE_TYPE (treeop0)) 10869 && SCALAR_INT_MODE_P (mode)) 10870 { 10871 class expand_operand eops[4]; 10872 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0)); 10873 expand_operands (treeop0, treeop1, 10874 subtarget, &op0, &op1, EXPAND_NORMAL); 10875 this_optab = vec_pack_sbool_trunc_optab; 10876 enum insn_code icode = optab_handler (this_optab, imode); 10877 create_output_operand (&eops[0], target, mode); 10878 create_convert_operand_from (&eops[1], op0, imode, false); 10879 create_convert_operand_from (&eops[2], op1, imode, false); 10880 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ()); 10881 create_input_operand (&eops[3], temp, imode); 10882 expand_insn (icode, 4, eops); 10883 return eops[0].value; 10884 } 10885 mode = TYPE_MODE (TREE_TYPE (treeop0)); 10886 subtarget = NULL_RTX; 10887 goto binop; 10888 10889 case VEC_PACK_FLOAT_EXPR: 10890 mode = TYPE_MODE (TREE_TYPE (treeop0)); 10891 expand_operands (treeop0, treeop1, 10892 subtarget, &op0, &op1, EXPAND_NORMAL); 10893 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0), 10894 optab_default); 10895 target = expand_binop (mode, this_optab, op0, op1, target, 10896 TYPE_UNSIGNED (TREE_TYPE (treeop0)), 10897 OPTAB_LIB_WIDEN); 10898 gcc_assert (target); 10899 return target; 10900 10901 case VEC_PERM_EXPR: 10902 { 10903 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL); 10904 vec_perm_builder sel; 10905 if (TREE_CODE (treeop2) == VECTOR_CST 10906 && tree_to_vec_perm_builder (&sel, treeop2)) 10907 { 10908 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2)); 10909 temp = expand_vec_perm_const (mode, op0, op1, sel, 10910 sel_mode, target); 10911 } 10912 else 10913 { 10914 op2 = expand_normal (treeop2); 10915 temp = expand_vec_perm_var (mode, op0, op1, op2, target); 10916 } 10917 gcc_assert (temp); 10918 return temp; 10919 } 10920 10921 case DOT_PROD_EXPR: 10922 { 10923 tree oprnd0 = treeop0; 10924 tree oprnd1 = treeop1; 10925 tree oprnd2 = treeop2; 10926 10927 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10928 op2 = expand_normal (oprnd2); 10929 target = expand_widen_pattern_expr (ops, op0, op1, op2, 10930 target, unsignedp); 10931 return target; 10932 } 10933 10934 case SAD_EXPR: 10935 { 10936 tree oprnd0 = treeop0; 10937 tree oprnd1 = treeop1; 10938 tree oprnd2 = treeop2; 10939 10940 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10941 op2 = expand_normal (oprnd2); 10942 target = expand_widen_pattern_expr (ops, op0, op1, op2, 10943 target, unsignedp); 10944 return target; 10945 } 10946 10947 case REALIGN_LOAD_EXPR: 10948 { 10949 tree oprnd0 = treeop0; 10950 tree oprnd1 = treeop1; 10951 tree oprnd2 = treeop2; 10952 10953 this_optab = optab_for_tree_code (code, type, optab_default); 10954 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL); 10955 op2 = expand_normal (oprnd2); 10956 temp = expand_ternary_op (mode, this_optab, op0, op1, op2, 10957 target, unsignedp); 10958 gcc_assert (temp); 10959 return temp; 10960 } 10961 10962 case COND_EXPR: 10963 { 10964 /* A COND_EXPR with its type being VOID_TYPE represents a 10965 conditional jump and is handled in 10966 expand_gimple_cond_expr. */ 10967 gcc_assert (!VOID_TYPE_P (type)); 10968 10969 /* Note that COND_EXPRs whose type is a structure or union 10970 are required to be constructed to contain assignments of 10971 a temporary variable, so that we can evaluate them here 10972 for side effect only. If type is void, we must do likewise. */ 10973 10974 gcc_assert (!TREE_ADDRESSABLE (type) 10975 && !ignore 10976 && TREE_TYPE (treeop1) != void_type_node 10977 && TREE_TYPE (treeop2) != void_type_node); 10978 10979 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2); 10980 if (temp) 10981 return temp; 10982 10983 /* If we are not to produce a result, we have no target. Otherwise, 10984 if a target was specified use it; it will not be used as an 10985 intermediate target unless it is safe. If no target, use a 10986 temporary. */ 10987 10988 if (modifier != EXPAND_STACK_PARM 10989 && original_target 10990 && safe_from_p (original_target, treeop0, 1) 10991 && GET_MODE (original_target) == mode 10992 && !MEM_P (original_target)) 10993 temp = original_target; 10994 else 10995 temp = assign_temp (type, 0, 1); 10996 10997 do_pending_stack_adjust (); 10998 NO_DEFER_POP; 10999 rtx_code_label *lab0 = gen_label_rtx (); 11000 rtx_code_label *lab1 = gen_label_rtx (); 11001 jumpifnot (treeop0, lab0, 11002 profile_probability::uninitialized ()); 11003 store_expr (treeop1, temp, 11004 modifier == EXPAND_STACK_PARM, 11005 false, false); 11006 11007 emit_jump_insn (targetm.gen_jump (lab1)); 11008 emit_barrier (); 11009 emit_label (lab0); 11010 store_expr (treeop2, temp, 11011 modifier == EXPAND_STACK_PARM, 11012 false, false); 11013 11014 emit_label (lab1); 11015 OK_DEFER_POP; 11016 return temp; 11017 } 11018 11019 case VEC_DUPLICATE_EXPR: 11020 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier); 11021 target = expand_vector_broadcast (mode, op0); 11022 gcc_assert (target); 11023 return target; 11024 11025 case VEC_SERIES_EXPR: 11026 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier); 11027 return expand_vec_series_expr (mode, op0, op1, target); 11028 11029 case BIT_INSERT_EXPR: 11030 { 11031 unsigned bitpos = tree_to_uhwi (treeop2); 11032 unsigned bitsize; 11033 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1))) 11034 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1)); 11035 else 11036 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1))); 11037 op0 = expand_normal (treeop0); 11038 op1 = expand_normal (treeop1); 11039 rtx dst = gen_reg_rtx (mode); 11040 emit_move_insn (dst, op0); 11041 store_bit_field (dst, bitsize, bitpos, 0, 0, 11042 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false); 11043 return dst; 11044 } 11045 11046 default: 11047 gcc_unreachable (); 11048 } 11049 11050 /* Here to do an ordinary binary operator. */ 11051 binop: 11052 expand_operands (treeop0, treeop1, 11053 subtarget, &op0, &op1, EXPAND_NORMAL); 11054 binop2: 11055 this_optab = optab_for_tree_code (code, type, optab_default); 11056 binop3: 11057 if (modifier == EXPAND_STACK_PARM) 11058 target = 0; 11059 temp = expand_binop (mode, this_optab, op0, op1, target, 11060 unsignedp, OPTAB_LIB_WIDEN); 11061 gcc_assert (temp); 11062 /* Bitwise operations do not need bitfield reduction as we expect their 11063 operands being properly truncated. */ 11064 if (code == BIT_XOR_EXPR 11065 || code == BIT_AND_EXPR 11066 || code == BIT_IOR_EXPR) 11067 return temp; 11068 return REDUCE_BIT_FIELD (temp); 11069 } 11070 #undef REDUCE_BIT_FIELD 11071 11072 11073 /* Return TRUE if expression STMT is suitable for replacement. 11074 Never consider memory loads as replaceable, because those don't ever lead 11075 into constant expressions. */ 11076 11077 static bool 11078 stmt_is_replaceable_p (gimple *stmt) 11079 { 11080 if (ssa_is_replaceable_p (stmt)) 11081 { 11082 /* Don't move around loads. */ 11083 if (!gimple_assign_single_p (stmt) 11084 || is_gimple_val (gimple_assign_rhs1 (stmt))) 11085 return true; 11086 } 11087 return false; 11088 } 11089 11090 /* A subroutine of expand_expr_real_1. Expand gimple assignment G, 11091 which is known to set an SSA_NAME result. The other arguments are 11092 as for expand_expr_real_1. */ 11093 11094 rtx 11095 expand_expr_real_gassign (gassign *g, rtx target, machine_mode tmode, 11096 enum expand_modifier modifier, rtx *alt_rtl, 11097 bool inner_reference_p) 11098 { 11099 separate_ops ops; 11100 rtx r; 11101 location_t saved_loc = curr_insn_location (); 11102 auto loc = gimple_location (g); 11103 if (loc != UNKNOWN_LOCATION) 11104 set_curr_insn_location (loc); 11105 tree lhs = gimple_assign_lhs (g); 11106 ops.code = gimple_assign_rhs_code (g); 11107 ops.type = TREE_TYPE (lhs); 11108 switch (get_gimple_rhs_class (ops.code)) 11109 { 11110 case GIMPLE_TERNARY_RHS: 11111 ops.op2 = gimple_assign_rhs3 (g); 11112 /* Fallthru */ 11113 case GIMPLE_BINARY_RHS: 11114 ops.op1 = gimple_assign_rhs2 (g); 11115 11116 /* Try to expand conditonal compare. */ 11117 if (targetm.gen_ccmp_first) 11118 { 11119 gcc_checking_assert (targetm.gen_ccmp_next != NULL); 11120 r = expand_ccmp_expr (g, TYPE_MODE (ops.type)); 11121 if (r) 11122 break; 11123 } 11124 /* Fallthru */ 11125 case GIMPLE_UNARY_RHS: 11126 ops.op0 = gimple_assign_rhs1 (g); 11127 ops.location = loc; 11128 r = expand_expr_real_2 (&ops, target, tmode, modifier); 11129 break; 11130 case GIMPLE_SINGLE_RHS: 11131 { 11132 r = expand_expr_real (gimple_assign_rhs1 (g), target, 11133 tmode, modifier, alt_rtl, 11134 inner_reference_p); 11135 break; 11136 } 11137 default: 11138 gcc_unreachable (); 11139 } 11140 set_curr_insn_location (saved_loc); 11141 if (REG_P (r) && !REG_EXPR (r)) 11142 set_reg_attrs_for_decl_rtl (lhs, r); 11143 return r; 11144 } 11145 11146 rtx 11147 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode, 11148 enum expand_modifier modifier, rtx *alt_rtl, 11149 bool inner_reference_p) 11150 { 11151 rtx op0, op1, temp, decl_rtl; 11152 tree type; 11153 int unsignedp; 11154 machine_mode mode, dmode; 11155 enum tree_code code = TREE_CODE (exp); 11156 rtx subtarget, original_target; 11157 int ignore; 11158 bool reduce_bit_field; 11159 location_t loc = EXPR_LOCATION (exp); 11160 struct separate_ops ops; 11161 tree treeop0, treeop1, treeop2; 11162 tree ssa_name = NULL_TREE; 11163 gimple *g; 11164 11165 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL 11166 expansion sign/zero extends integral types with less than mode precision 11167 when reading from bit-fields and after arithmetic operations (see 11168 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies 11169 on those extensions to have been already performed, but because of the 11170 above for _BitInt they need to be sign/zero extended when reading from 11171 locations that could be exposed to ABI boundaries (when loading from 11172 objects in memory, or function arguments, return value). Because we 11173 internally extend after arithmetic operations, we can avoid doing that 11174 when reading from SSA_NAMEs of vars. */ 11175 #define EXTEND_BITINT(expr) \ 11176 ((TREE_CODE (type) == BITINT_TYPE \ 11177 && reduce_bit_field \ 11178 && mode != BLKmode \ 11179 && modifier != EXPAND_MEMORY \ 11180 && modifier != EXPAND_WRITE \ 11181 && modifier != EXPAND_INITIALIZER \ 11182 && modifier != EXPAND_CONST_ADDRESS) \ 11183 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr)) 11184 11185 type = TREE_TYPE (exp); 11186 mode = TYPE_MODE (type); 11187 unsignedp = TYPE_UNSIGNED (type); 11188 11189 treeop0 = treeop1 = treeop2 = NULL_TREE; 11190 if (!VL_EXP_CLASS_P (exp)) 11191 switch (TREE_CODE_LENGTH (code)) 11192 { 11193 default: 11194 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */ 11195 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */ 11196 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */ 11197 case 0: break; 11198 } 11199 ops.code = code; 11200 ops.type = type; 11201 ops.op0 = treeop0; 11202 ops.op1 = treeop1; 11203 ops.op2 = treeop2; 11204 ops.location = loc; 11205 11206 ignore = (target == const0_rtx 11207 || ((CONVERT_EXPR_CODE_P (code) 11208 || code == COND_EXPR || code == VIEW_CONVERT_EXPR) 11209 && TREE_CODE (type) == VOID_TYPE)); 11210 11211 /* An operation in what may be a bit-field type needs the 11212 result to be reduced to the precision of the bit-field type, 11213 which is narrower than that of the type's mode. */ 11214 reduce_bit_field = (!ignore 11215 && INTEGRAL_TYPE_P (type) 11216 && !type_has_mode_precision_p (type)); 11217 11218 /* If we are going to ignore this result, we need only do something 11219 if there is a side-effect somewhere in the expression. If there 11220 is, short-circuit the most common cases here. Note that we must 11221 not call expand_expr with anything but const0_rtx in case this 11222 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */ 11223 11224 if (ignore) 11225 { 11226 if (! TREE_SIDE_EFFECTS (exp)) 11227 return const0_rtx; 11228 11229 /* Ensure we reference a volatile object even if value is ignored, but 11230 don't do this if all we are doing is taking its address. */ 11231 if (TREE_THIS_VOLATILE (exp) 11232 && TREE_CODE (exp) != FUNCTION_DECL 11233 && mode != VOIDmode && mode != BLKmode 11234 && modifier != EXPAND_CONST_ADDRESS) 11235 { 11236 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier); 11237 if (MEM_P (temp)) 11238 copy_to_reg (temp); 11239 return const0_rtx; 11240 } 11241 11242 if (TREE_CODE_CLASS (code) == tcc_unary 11243 || code == BIT_FIELD_REF 11244 || code == COMPONENT_REF 11245 || code == INDIRECT_REF) 11246 return expand_expr (treeop0, const0_rtx, VOIDmode, 11247 modifier); 11248 11249 else if (TREE_CODE_CLASS (code) == tcc_binary 11250 || TREE_CODE_CLASS (code) == tcc_comparison 11251 || code == ARRAY_REF || code == ARRAY_RANGE_REF) 11252 { 11253 expand_expr (treeop0, const0_rtx, VOIDmode, modifier); 11254 expand_expr (treeop1, const0_rtx, VOIDmode, modifier); 11255 return const0_rtx; 11256 } 11257 11258 target = 0; 11259 } 11260 11261 if (reduce_bit_field && modifier == EXPAND_STACK_PARM) 11262 target = 0; 11263 11264 /* Use subtarget as the target for operand 0 of a binary operation. */ 11265 subtarget = get_subtarget (target); 11266 original_target = target; 11267 11268 switch (code) 11269 { 11270 case LABEL_DECL: 11271 { 11272 tree function = decl_function_context (exp); 11273 11274 temp = label_rtx (exp); 11275 temp = gen_rtx_LABEL_REF (Pmode, temp); 11276 11277 if (function != current_function_decl 11278 && function != 0) 11279 LABEL_REF_NONLOCAL_P (temp) = 1; 11280 11281 temp = gen_rtx_MEM (FUNCTION_MODE, temp); 11282 return temp; 11283 } 11284 11285 case SSA_NAME: 11286 /* ??? ivopts calls expander, without any preparation from 11287 out-of-ssa. So fake instructions as if this was an access to the 11288 base variable. This unnecessarily allocates a pseudo, see how we can 11289 reuse it, if partition base vars have it set already. */ 11290 if (!currently_expanding_to_rtl) 11291 { 11292 tree var = SSA_NAME_VAR (exp); 11293 if (var && DECL_RTL_SET_P (var)) 11294 return DECL_RTL (var); 11295 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)), 11296 LAST_VIRTUAL_REGISTER + 1); 11297 } 11298 11299 g = get_gimple_for_ssa_name (exp); 11300 /* For EXPAND_INITIALIZER try harder to get something simpler. */ 11301 if (g == NULL 11302 && modifier == EXPAND_INITIALIZER 11303 && !SSA_NAME_IS_DEFAULT_DEF (exp) 11304 && (optimize || !SSA_NAME_VAR (exp) 11305 || DECL_IGNORED_P (SSA_NAME_VAR (exp))) 11306 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp))) 11307 g = SSA_NAME_DEF_STMT (exp); 11308 if (g) 11309 return expand_expr_real_gassign (as_a<gassign *> (g), target, tmode, 11310 modifier, alt_rtl, inner_reference_p); 11311 11312 ssa_name = exp; 11313 decl_rtl = get_rtx_for_ssa_name (ssa_name); 11314 exp = SSA_NAME_VAR (ssa_name); 11315 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an 11316 SSA_NAME computed within the current function. In such case the 11317 value have been already extended before. While if it is a function 11318 parameter, result or some memory location, we need to be prepared 11319 for some other compiler leaving the bits uninitialized. */ 11320 if (!exp || VAR_P (exp)) 11321 reduce_bit_field = false; 11322 goto expand_decl_rtl; 11323 11324 case VAR_DECL: 11325 /* Allow accel compiler to handle variables that require special 11326 treatment, e.g. if they have been modified in some way earlier in 11327 compilation by the adjust_private_decl OpenACC hook. */ 11328 if (flag_openacc && targetm.goacc.expand_var_decl) 11329 { 11330 temp = targetm.goacc.expand_var_decl (exp); 11331 if (temp) 11332 return temp; 11333 } 11334 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that 11335 have scalar integer modes to a reg via store_constructor. */ 11336 if (TREE_READONLY (exp) 11337 && !TREE_SIDE_EFFECTS (exp) 11338 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM) 11339 && immediate_const_ctor_p (DECL_INITIAL (exp)) 11340 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp))) 11341 && crtl->emit.regno_pointer_align_length 11342 && !target) 11343 { 11344 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp))); 11345 store_constructor (DECL_INITIAL (exp), target, 0, 11346 int_expr_size (DECL_INITIAL (exp)), false); 11347 return target; 11348 } 11349 /* ... fall through ... */ 11350 11351 case PARM_DECL: 11352 /* If a static var's type was incomplete when the decl was written, 11353 but the type is complete now, lay out the decl now. */ 11354 if (DECL_SIZE (exp) == 0 11355 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp)) 11356 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp))) 11357 layout_decl (exp, 0); 11358 11359 /* fall through */ 11360 11361 case FUNCTION_DECL: 11362 case RESULT_DECL: 11363 decl_rtl = DECL_RTL (exp); 11364 expand_decl_rtl: 11365 gcc_assert (decl_rtl); 11366 11367 /* DECL_MODE might change when TYPE_MODE depends on attribute target 11368 settings for VECTOR_TYPE_P that might switch for the function. */ 11369 if (currently_expanding_to_rtl 11370 && code == VAR_DECL && MEM_P (decl_rtl) 11371 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode) 11372 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0); 11373 else 11374 decl_rtl = copy_rtx (decl_rtl); 11375 11376 /* Record writes to register variables. */ 11377 if (modifier == EXPAND_WRITE 11378 && REG_P (decl_rtl) 11379 && HARD_REGISTER_P (decl_rtl)) 11380 add_to_hard_reg_set (&crtl->asm_clobbers, 11381 GET_MODE (decl_rtl), REGNO (decl_rtl)); 11382 11383 /* Ensure variable marked as used even if it doesn't go through 11384 a parser. If it hasn't be used yet, write out an external 11385 definition. */ 11386 if (exp) 11387 TREE_USED (exp) = 1; 11388 11389 /* Show we haven't gotten RTL for this yet. */ 11390 temp = 0; 11391 11392 /* Variables inherited from containing functions should have 11393 been lowered by this point. */ 11394 if (exp) 11395 { 11396 tree context = decl_function_context (exp); 11397 gcc_assert (SCOPE_FILE_SCOPE_P (context) 11398 || context == current_function_decl 11399 || TREE_STATIC (exp) 11400 || DECL_EXTERNAL (exp) 11401 /* ??? C++ creates functions that are not 11402 TREE_STATIC. */ 11403 || TREE_CODE (exp) == FUNCTION_DECL); 11404 } 11405 11406 /* This is the case of an array whose size is to be determined 11407 from its initializer, while the initializer is still being parsed. 11408 ??? We aren't parsing while expanding anymore. */ 11409 11410 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0))) 11411 temp = validize_mem (decl_rtl); 11412 11413 /* If DECL_RTL is memory, we are in the normal case and the 11414 address is not valid, get the address into a register. */ 11415 11416 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER) 11417 { 11418 if (alt_rtl) 11419 *alt_rtl = decl_rtl; 11420 decl_rtl = use_anchored_address (decl_rtl); 11421 if (modifier != EXPAND_CONST_ADDRESS 11422 && modifier != EXPAND_SUM 11423 && !memory_address_addr_space_p (exp ? DECL_MODE (exp) 11424 : GET_MODE (decl_rtl), 11425 XEXP (decl_rtl, 0), 11426 MEM_ADDR_SPACE (decl_rtl))) 11427 temp = replace_equiv_address (decl_rtl, 11428 copy_rtx (XEXP (decl_rtl, 0))); 11429 } 11430 11431 /* If we got something, return it. But first, set the alignment 11432 if the address is a register. */ 11433 if (temp != 0) 11434 { 11435 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0))) 11436 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp)); 11437 } 11438 else if (MEM_P (decl_rtl)) 11439 temp = decl_rtl; 11440 11441 if (temp != 0) 11442 { 11443 if (MEM_P (temp) 11444 && modifier != EXPAND_WRITE 11445 && modifier != EXPAND_MEMORY 11446 && modifier != EXPAND_INITIALIZER 11447 && modifier != EXPAND_CONST_ADDRESS 11448 && modifier != EXPAND_SUM 11449 && !inner_reference_p 11450 && mode != BLKmode 11451 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode)) 11452 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, 11453 MEM_ALIGN (temp), NULL_RTX, NULL); 11454 11455 return EXTEND_BITINT (temp); 11456 } 11457 11458 if (exp) 11459 dmode = DECL_MODE (exp); 11460 else 11461 dmode = TYPE_MODE (TREE_TYPE (ssa_name)); 11462 11463 /* If the mode of DECL_RTL does not match that of the decl, 11464 there are two cases: we are dealing with a BLKmode value 11465 that is returned in a register, or we are dealing with 11466 a promoted value. In the latter case, return a SUBREG 11467 of the wanted mode, but mark it so that we know that it 11468 was already extended. */ 11469 if (REG_P (decl_rtl) 11470 && dmode != BLKmode 11471 && GET_MODE (decl_rtl) != dmode) 11472 { 11473 machine_mode pmode; 11474 11475 /* Get the signedness to be used for this variable. Ensure we get 11476 the same mode we got when the variable was declared. */ 11477 if (code != SSA_NAME) 11478 pmode = promote_decl_mode (exp, &unsignedp); 11479 else if ((g = SSA_NAME_DEF_STMT (ssa_name)) 11480 && gimple_code (g) == GIMPLE_CALL 11481 && !gimple_call_internal_p (g)) 11482 pmode = promote_function_mode (type, mode, &unsignedp, 11483 gimple_call_fntype (g), 11484 2); 11485 else 11486 pmode = promote_ssa_mode (ssa_name, &unsignedp); 11487 gcc_assert (GET_MODE (decl_rtl) == pmode); 11488 11489 /* Some ABIs require scalar floating point modes to be passed 11490 in a wider scalar integer mode. We need to explicitly 11491 truncate to an integer mode of the correct precision before 11492 using a SUBREG to reinterpret as a floating point value. */ 11493 if (SCALAR_FLOAT_MODE_P (mode) 11494 && SCALAR_INT_MODE_P (pmode) 11495 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode))) 11496 return convert_wider_int_to_float (mode, pmode, decl_rtl); 11497 11498 temp = gen_lowpart_SUBREG (mode, decl_rtl); 11499 SUBREG_PROMOTED_VAR_P (temp) = 1; 11500 SUBREG_PROMOTED_SET (temp, unsignedp); 11501 return EXTEND_BITINT (temp); 11502 } 11503 11504 return EXTEND_BITINT (decl_rtl); 11505 11506 case INTEGER_CST: 11507 { 11508 if (TREE_CODE (type) == BITINT_TYPE) 11509 { 11510 unsigned int prec = TYPE_PRECISION (type); 11511 struct bitint_info info; 11512 bool ok = targetm.c.bitint_type_info (prec, &info); 11513 gcc_assert (ok); 11514 scalar_int_mode limb_mode 11515 = as_a <scalar_int_mode> (info.limb_mode); 11516 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode); 11517 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE) 11518 { 11519 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */ 11520 exp = tree_output_constant_def (exp); 11521 return expand_expr (exp, target, VOIDmode, modifier); 11522 } 11523 } 11524 11525 /* Given that TYPE_PRECISION (type) is not always equal to 11526 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from 11527 the former to the latter according to the signedness of the 11528 type. */ 11529 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type); 11530 temp = immed_wide_int_const 11531 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode); 11532 return temp; 11533 } 11534 11535 case VECTOR_CST: 11536 { 11537 tree tmp = NULL_TREE; 11538 if (VECTOR_MODE_P (mode)) 11539 return const_vector_from_tree (exp); 11540 scalar_int_mode int_mode; 11541 if (is_int_mode (mode, &int_mode)) 11542 { 11543 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1); 11544 if (type_for_mode) 11545 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, 11546 type_for_mode, exp); 11547 } 11548 if (!tmp) 11549 { 11550 vec<constructor_elt, va_gc> *v; 11551 /* Constructors need to be fixed-length. FIXME. */ 11552 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant (); 11553 vec_alloc (v, nunits); 11554 for (unsigned int i = 0; i < nunits; ++i) 11555 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i)); 11556 tmp = build_constructor (type, v); 11557 } 11558 return expand_expr (tmp, ignore ? const0_rtx : target, 11559 tmode, modifier); 11560 } 11561 11562 case CONST_DECL: 11563 if (modifier == EXPAND_WRITE) 11564 { 11565 /* Writing into CONST_DECL is always invalid, but handle it 11566 gracefully. */ 11567 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp)); 11568 scalar_int_mode address_mode = targetm.addr_space.address_mode (as); 11569 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode, 11570 EXPAND_NORMAL, as); 11571 op0 = memory_address_addr_space (mode, op0, as); 11572 temp = gen_rtx_MEM (mode, op0); 11573 set_mem_addr_space (temp, as); 11574 return temp; 11575 } 11576 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier); 11577 11578 case REAL_CST: 11579 /* If optimized, generate immediate CONST_DOUBLE 11580 which will be turned into memory by reload if necessary. 11581 11582 We used to force a register so that loop.c could see it. But 11583 this does not allow gen_* patterns to perform optimizations with 11584 the constants. It also produces two insns in cases like "x = 1.0;". 11585 On most machines, floating-point constants are not permitted in 11586 many insns, so we'd end up copying it to a register in any case. 11587 11588 Now, we do the copying in expand_binop, if appropriate. */ 11589 return const_double_from_real_value (TREE_REAL_CST (exp), 11590 TYPE_MODE (TREE_TYPE (exp))); 11591 11592 case FIXED_CST: 11593 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp), 11594 TYPE_MODE (TREE_TYPE (exp))); 11595 11596 case COMPLEX_CST: 11597 /* Handle evaluating a complex constant in a CONCAT target. */ 11598 if (original_target && GET_CODE (original_target) == CONCAT) 11599 { 11600 rtx rtarg, itarg; 11601 11602 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp))); 11603 rtarg = XEXP (original_target, 0); 11604 itarg = XEXP (original_target, 1); 11605 11606 /* Move the real and imaginary parts separately. */ 11607 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL); 11608 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL); 11609 11610 if (op0 != rtarg) 11611 emit_move_insn (rtarg, op0); 11612 if (op1 != itarg) 11613 emit_move_insn (itarg, op1); 11614 11615 return original_target; 11616 } 11617 11618 /* fall through */ 11619 11620 case STRING_CST: 11621 temp = expand_expr_constant (exp, 1, modifier); 11622 11623 /* temp contains a constant address. 11624 On RISC machines where a constant address isn't valid, 11625 make some insns to get that address into a register. */ 11626 if (modifier != EXPAND_CONST_ADDRESS 11627 && modifier != EXPAND_INITIALIZER 11628 && modifier != EXPAND_SUM 11629 && ! memory_address_addr_space_p (mode, XEXP (temp, 0), 11630 MEM_ADDR_SPACE (temp))) 11631 return replace_equiv_address (temp, 11632 copy_rtx (XEXP (temp, 0))); 11633 return temp; 11634 11635 case POLY_INT_CST: 11636 return immed_wide_int_const (poly_int_cst_value (exp), mode); 11637 11638 case SAVE_EXPR: 11639 { 11640 tree val = treeop0; 11641 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl, 11642 inner_reference_p); 11643 11644 if (!SAVE_EXPR_RESOLVED_P (exp)) 11645 { 11646 /* We can indeed still hit this case, typically via builtin 11647 expanders calling save_expr immediately before expanding 11648 something. Assume this means that we only have to deal 11649 with non-BLKmode values. */ 11650 gcc_assert (GET_MODE (ret) != BLKmode); 11651 11652 val = build_decl (curr_insn_location (), 11653 VAR_DECL, NULL, TREE_TYPE (exp)); 11654 DECL_ARTIFICIAL (val) = 1; 11655 DECL_IGNORED_P (val) = 1; 11656 treeop0 = val; 11657 TREE_OPERAND (exp, 0) = treeop0; 11658 SAVE_EXPR_RESOLVED_P (exp) = 1; 11659 11660 if (!CONSTANT_P (ret)) 11661 ret = copy_to_reg (ret); 11662 SET_DECL_RTL (val, ret); 11663 } 11664 11665 return ret; 11666 } 11667 11668 11669 case CONSTRUCTOR: 11670 /* If we don't need the result, just ensure we evaluate any 11671 subexpressions. */ 11672 if (ignore) 11673 { 11674 unsigned HOST_WIDE_INT idx; 11675 tree value; 11676 11677 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value) 11678 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL); 11679 11680 return const0_rtx; 11681 } 11682 11683 return expand_constructor (exp, target, modifier, false); 11684 11685 case TARGET_MEM_REF: 11686 { 11687 addr_space_t as 11688 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))); 11689 unsigned int align; 11690 11691 op0 = addr_for_mem_ref (exp, as, true); 11692 op0 = memory_address_addr_space (mode, op0, as); 11693 temp = gen_rtx_MEM (mode, op0); 11694 set_mem_attributes (temp, exp, 0); 11695 set_mem_addr_space (temp, as); 11696 align = get_object_alignment (exp); 11697 if (modifier != EXPAND_WRITE 11698 && modifier != EXPAND_MEMORY 11699 && mode != BLKmode 11700 && align < GET_MODE_ALIGNMENT (mode)) 11701 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, 11702 align, NULL_RTX, NULL); 11703 return EXTEND_BITINT (temp); 11704 } 11705 11706 case MEM_REF: 11707 { 11708 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp); 11709 addr_space_t as 11710 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))); 11711 machine_mode address_mode; 11712 tree base = TREE_OPERAND (exp, 0); 11713 gimple *def_stmt; 11714 unsigned align; 11715 /* Handle expansion of non-aliased memory with non-BLKmode. That 11716 might end up in a register. */ 11717 if (mem_ref_refers_to_non_mem_p (exp)) 11718 { 11719 poly_int64 offset = mem_ref_offset (exp).force_shwi (); 11720 base = TREE_OPERAND (base, 0); 11721 poly_uint64 type_size; 11722 if (known_eq (offset, 0) 11723 && !reverse 11724 && poly_int_tree_p (TYPE_SIZE (type), &type_size) 11725 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size)) 11726 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base), 11727 target, tmode, modifier); 11728 if (TYPE_MODE (type) == BLKmode) 11729 { 11730 temp = assign_stack_temp (DECL_MODE (base), 11731 GET_MODE_SIZE (DECL_MODE (base))); 11732 store_expr (base, temp, 0, false, false); 11733 temp = adjust_address (temp, BLKmode, offset); 11734 set_mem_size (temp, int_size_in_bytes (type)); 11735 return temp; 11736 } 11737 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type), 11738 bitsize_int (offset * BITS_PER_UNIT)); 11739 REF_REVERSE_STORAGE_ORDER (exp) = reverse; 11740 return expand_expr (exp, target, tmode, modifier); 11741 } 11742 address_mode = targetm.addr_space.address_mode (as); 11743 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR))) 11744 { 11745 tree mask = gimple_assign_rhs2 (def_stmt); 11746 base = build2 (BIT_AND_EXPR, TREE_TYPE (base), 11747 gimple_assign_rhs1 (def_stmt), mask); 11748 TREE_OPERAND (exp, 0) = base; 11749 } 11750 align = get_object_alignment (exp); 11751 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM); 11752 op0 = memory_address_addr_space (mode, op0, as); 11753 if (!integer_zerop (TREE_OPERAND (exp, 1))) 11754 { 11755 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode); 11756 op0 = simplify_gen_binary (PLUS, address_mode, op0, off); 11757 op0 = memory_address_addr_space (mode, op0, as); 11758 } 11759 temp = gen_rtx_MEM (mode, op0); 11760 set_mem_attributes (temp, exp, 0); 11761 set_mem_addr_space (temp, as); 11762 if (TREE_THIS_VOLATILE (exp)) 11763 MEM_VOLATILE_P (temp) = 1; 11764 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY) 11765 return temp; 11766 if (!inner_reference_p 11767 && mode != BLKmode 11768 && align < GET_MODE_ALIGNMENT (mode)) 11769 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align, 11770 modifier == EXPAND_STACK_PARM 11771 ? NULL_RTX : target, alt_rtl); 11772 if (reverse) 11773 temp = flip_storage_order (mode, temp); 11774 return EXTEND_BITINT (temp); 11775 } 11776 11777 case ARRAY_REF: 11778 11779 { 11780 tree array = treeop0; 11781 tree index = treeop1; 11782 tree init; 11783 11784 /* Fold an expression like: "foo"[2]. 11785 This is not done in fold so it won't happen inside &. 11786 Don't fold if this is for wide characters since it's too 11787 difficult to do correctly and this is a very rare case. */ 11788 11789 if (modifier != EXPAND_CONST_ADDRESS 11790 && modifier != EXPAND_INITIALIZER 11791 && modifier != EXPAND_MEMORY) 11792 { 11793 tree t = fold_read_from_constant_string (exp); 11794 11795 if (t) 11796 return expand_expr (t, target, tmode, modifier); 11797 } 11798 11799 /* If this is a constant index into a constant array, 11800 just get the value from the array. Handle both the cases when 11801 we have an explicit constructor and when our operand is a variable 11802 that was declared const. */ 11803 11804 if (modifier != EXPAND_CONST_ADDRESS 11805 && modifier != EXPAND_INITIALIZER 11806 && modifier != EXPAND_MEMORY 11807 && TREE_CODE (array) == CONSTRUCTOR 11808 && ! TREE_SIDE_EFFECTS (array) 11809 && TREE_CODE (index) == INTEGER_CST) 11810 { 11811 unsigned HOST_WIDE_INT ix; 11812 tree field, value; 11813 11814 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix, 11815 field, value) 11816 if (tree_int_cst_equal (field, index)) 11817 { 11818 if (!TREE_SIDE_EFFECTS (value)) 11819 return expand_expr (fold (value), target, tmode, modifier); 11820 break; 11821 } 11822 } 11823 11824 else if (optimize >= 1 11825 && modifier != EXPAND_CONST_ADDRESS 11826 && modifier != EXPAND_INITIALIZER 11827 && modifier != EXPAND_MEMORY 11828 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array) 11829 && TREE_CODE (index) == INTEGER_CST 11830 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL) 11831 && (init = ctor_for_folding (array)) != error_mark_node) 11832 { 11833 if (init == NULL_TREE) 11834 { 11835 tree value = build_zero_cst (type); 11836 if (TREE_CODE (value) == CONSTRUCTOR) 11837 { 11838 /* If VALUE is a CONSTRUCTOR, this optimization is only 11839 useful if this doesn't store the CONSTRUCTOR into 11840 memory. If it does, it is more efficient to just 11841 load the data from the array directly. */ 11842 rtx ret = expand_constructor (value, target, 11843 modifier, true); 11844 if (ret == NULL_RTX) 11845 value = NULL_TREE; 11846 } 11847 11848 if (value) 11849 return expand_expr (value, target, tmode, modifier); 11850 } 11851 else if (TREE_CODE (init) == CONSTRUCTOR) 11852 { 11853 unsigned HOST_WIDE_INT ix; 11854 tree field, value; 11855 11856 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, 11857 field, value) 11858 if (tree_int_cst_equal (field, index)) 11859 { 11860 if (TREE_SIDE_EFFECTS (value)) 11861 break; 11862 11863 if (TREE_CODE (value) == CONSTRUCTOR) 11864 { 11865 /* If VALUE is a CONSTRUCTOR, this 11866 optimization is only useful if 11867 this doesn't store the CONSTRUCTOR 11868 into memory. If it does, it is more 11869 efficient to just load the data from 11870 the array directly. */ 11871 rtx ret = expand_constructor (value, target, 11872 modifier, true); 11873 if (ret == NULL_RTX) 11874 break; 11875 } 11876 11877 return 11878 expand_expr (fold (value), target, tmode, modifier); 11879 } 11880 } 11881 else if (TREE_CODE (init) == STRING_CST) 11882 { 11883 tree low_bound = array_ref_low_bound (exp); 11884 tree index1 = fold_convert_loc (loc, sizetype, treeop1); 11885 11886 /* Optimize the special case of a zero lower bound. 11887 11888 We convert the lower bound to sizetype to avoid problems 11889 with constant folding. E.g. suppose the lower bound is 11890 1 and its mode is QI. Without the conversion 11891 (ARRAY + (INDEX - (unsigned char)1)) 11892 becomes 11893 (ARRAY + (-(unsigned char)1) + INDEX) 11894 which becomes 11895 (ARRAY + 255 + INDEX). Oops! */ 11896 if (!integer_zerop (low_bound)) 11897 index1 = size_diffop_loc (loc, index1, 11898 fold_convert_loc (loc, sizetype, 11899 low_bound)); 11900 11901 if (tree_fits_uhwi_p (index1) 11902 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0) 11903 { 11904 tree char_type = TREE_TYPE (TREE_TYPE (init)); 11905 scalar_int_mode char_mode; 11906 11907 if (is_int_mode (TYPE_MODE (char_type), &char_mode) 11908 && GET_MODE_SIZE (char_mode) == 1) 11909 return gen_int_mode (TREE_STRING_POINTER (init) 11910 [TREE_INT_CST_LOW (index1)], 11911 char_mode); 11912 } 11913 } 11914 } 11915 } 11916 goto normal_inner_ref; 11917 11918 case COMPONENT_REF: 11919 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR); 11920 /* Fall through. */ 11921 case BIT_FIELD_REF: 11922 case ARRAY_RANGE_REF: 11923 normal_inner_ref: 11924 { 11925 machine_mode mode1, mode2; 11926 poly_int64 bitsize, bitpos, bytepos; 11927 tree offset; 11928 int reversep, volatilep = 0; 11929 tree tem 11930 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1, 11931 &unsignedp, &reversep, &volatilep); 11932 rtx orig_op0, memloc; 11933 bool clear_mem_expr = false; 11934 bool must_force_mem; 11935 11936 /* If we got back the original object, something is wrong. Perhaps 11937 we are evaluating an expression too early. In any event, don't 11938 infinitely recurse. */ 11939 gcc_assert (tem != exp); 11940 11941 /* Make sure bitpos is not negative, this can wreak havoc later. */ 11942 if (maybe_lt (bitpos, 0)) 11943 { 11944 gcc_checking_assert (offset == NULL_TREE); 11945 offset = size_int (bits_to_bytes_round_down (bitpos)); 11946 bitpos = num_trailing_bits (bitpos); 11947 } 11948 11949 /* If we have either an offset, a BLKmode result, or a reference 11950 outside the underlying object, we must force it to memory. 11951 Such a case can occur in Ada if we have unchecked conversion 11952 of an expression from a scalar type to an aggregate type or 11953 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were 11954 passed a partially uninitialized object or a view-conversion 11955 to a larger size. */ 11956 must_force_mem = offset != NULL_TREE 11957 || mode1 == BLKmode 11958 || (mode == BLKmode 11959 && !int_mode_for_size (bitsize, 1).exists ()); 11960 11961 const enum expand_modifier tem_modifier 11962 = must_force_mem 11963 ? EXPAND_MEMORY 11964 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier; 11965 11966 /* If TEM's type is a union of variable size, pass TARGET to the inner 11967 computation, since it will need a temporary and TARGET is known 11968 to have to do. This occurs in unchecked conversion in Ada. */ 11969 const rtx tem_target 11970 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE 11971 && COMPLETE_TYPE_P (TREE_TYPE (tem)) 11972 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST 11973 && modifier != EXPAND_STACK_PARM 11974 ? target 11975 : NULL_RTX; 11976 11977 orig_op0 = op0 11978 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL, 11979 true); 11980 11981 /* If the field has a mode, we want to access it in the 11982 field's mode, not the computed mode. 11983 If a MEM has VOIDmode (external with incomplete type), 11984 use BLKmode for it instead. */ 11985 if (MEM_P (op0)) 11986 { 11987 if (mode1 != VOIDmode) 11988 op0 = adjust_address (op0, mode1, 0); 11989 else if (GET_MODE (op0) == VOIDmode) 11990 op0 = adjust_address (op0, BLKmode, 0); 11991 } 11992 11993 mode2 11994 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0); 11995 11996 /* See above for the rationale. */ 11997 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2))) 11998 must_force_mem = true; 11999 12000 /* Handle CONCAT first. */ 12001 if (GET_CODE (op0) == CONCAT && !must_force_mem) 12002 { 12003 if (known_eq (bitpos, 0) 12004 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0))) 12005 && COMPLEX_MODE_P (mode1) 12006 && COMPLEX_MODE_P (GET_MODE (op0)) 12007 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1)) 12008 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0))))) 12009 { 12010 if (reversep) 12011 op0 = flip_storage_order (GET_MODE (op0), op0); 12012 if (mode1 != GET_MODE (op0)) 12013 { 12014 rtx parts[2]; 12015 for (int i = 0; i < 2; i++) 12016 { 12017 rtx op = read_complex_part (op0, i != 0); 12018 if (GET_CODE (op) == SUBREG) 12019 op = force_reg (GET_MODE (op), op); 12020 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op); 12021 if (temp) 12022 op = temp; 12023 else 12024 { 12025 if (!REG_P (op) && !MEM_P (op)) 12026 op = force_reg (GET_MODE (op), op); 12027 op = gen_lowpart (GET_MODE_INNER (mode1), op); 12028 } 12029 parts[i] = op; 12030 } 12031 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]); 12032 } 12033 return op0; 12034 } 12035 if (known_eq (bitpos, 0) 12036 && known_eq (bitsize, 12037 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))) 12038 && maybe_ne (bitsize, 0)) 12039 { 12040 op0 = XEXP (op0, 0); 12041 mode2 = GET_MODE (op0); 12042 } 12043 else if (known_eq (bitpos, 12044 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))) 12045 && known_eq (bitsize, 12046 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))) 12047 && maybe_ne (bitpos, 0) 12048 && maybe_ne (bitsize, 0)) 12049 { 12050 op0 = XEXP (op0, 1); 12051 bitpos = 0; 12052 mode2 = GET_MODE (op0); 12053 } 12054 else 12055 /* Otherwise force into memory. */ 12056 must_force_mem = true; 12057 } 12058 12059 /* If this is a constant, put it in a register if it is a legitimate 12060 constant and we don't need a memory reference. */ 12061 if (CONSTANT_P (op0) 12062 && mode2 != BLKmode 12063 && targetm.legitimate_constant_p (mode2, op0) 12064 && !must_force_mem) 12065 op0 = force_reg (mode2, op0); 12066 12067 /* Otherwise, if this is a constant, try to force it to the constant 12068 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it 12069 is a legitimate constant. */ 12070 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0))) 12071 op0 = validize_mem (memloc); 12072 12073 /* Otherwise, if this is a constant or the object is not in memory 12074 and need be, put it there. */ 12075 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem)) 12076 { 12077 memloc = assign_temp (TREE_TYPE (tem), 1, 1); 12078 emit_move_insn (memloc, op0); 12079 op0 = memloc; 12080 clear_mem_expr = true; 12081 } 12082 12083 if (offset) 12084 { 12085 machine_mode address_mode; 12086 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, 12087 EXPAND_SUM); 12088 12089 gcc_assert (MEM_P (op0)); 12090 12091 address_mode = get_address_mode (op0); 12092 if (GET_MODE (offset_rtx) != address_mode) 12093 { 12094 /* We cannot be sure that the RTL in offset_rtx is valid outside 12095 of a memory address context, so force it into a register 12096 before attempting to convert it to the desired mode. */ 12097 offset_rtx = force_operand (offset_rtx, NULL_RTX); 12098 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0); 12099 } 12100 12101 /* See the comment in expand_assignment for the rationale. */ 12102 if (mode1 != VOIDmode 12103 && maybe_ne (bitpos, 0) 12104 && maybe_gt (bitsize, 0) 12105 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos) 12106 && multiple_p (bitpos, bitsize) 12107 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1)) 12108 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1)) 12109 { 12110 op0 = adjust_address (op0, mode1, bytepos); 12111 bitpos = 0; 12112 } 12113 12114 op0 = offset_address (op0, offset_rtx, 12115 highest_pow2_factor (offset)); 12116 } 12117 12118 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT, 12119 record its alignment as BIGGEST_ALIGNMENT. */ 12120 if (MEM_P (op0) 12121 && known_eq (bitpos, 0) 12122 && offset != 0 12123 && is_aligning_offset (offset, tem)) 12124 set_mem_align (op0, BIGGEST_ALIGNMENT); 12125 12126 /* Don't forget about volatility even if this is a bitfield. */ 12127 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0)) 12128 { 12129 if (op0 == orig_op0) 12130 op0 = copy_rtx (op0); 12131 12132 MEM_VOLATILE_P (op0) = 1; 12133 } 12134 12135 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL) 12136 { 12137 if (op0 == orig_op0) 12138 op0 = copy_rtx (op0); 12139 12140 set_mem_align (op0, BITS_PER_UNIT); 12141 } 12142 12143 /* In cases where an aligned union has an unaligned object 12144 as a field, we might be extracting a BLKmode value from 12145 an integer-mode (e.g., SImode) object. Handle this case 12146 by doing the extract into an object as wide as the field 12147 (which we know to be the width of a basic mode), then 12148 storing into memory, and changing the mode to BLKmode. */ 12149 if (mode1 == VOIDmode 12150 || REG_P (op0) || GET_CODE (op0) == SUBREG 12151 || (mode1 != BLKmode && ! direct_load[(int) mode1] 12152 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT 12153 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT 12154 && modifier != EXPAND_CONST_ADDRESS 12155 && modifier != EXPAND_INITIALIZER 12156 && modifier != EXPAND_MEMORY) 12157 /* If the bitfield is volatile and the bitsize 12158 is narrower than the access size of the bitfield, 12159 we need to extract bitfields from the access. */ 12160 || (volatilep && TREE_CODE (exp) == COMPONENT_REF 12161 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)) 12162 && mode1 != BLKmode 12163 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT)) 12164 /* If the field isn't aligned enough to fetch as a memref, 12165 fetch it as a bit field. */ 12166 || (mode1 != BLKmode 12167 && (((MEM_P (op0) 12168 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1) 12169 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1)) 12170 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode) 12171 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode))) 12172 && modifier != EXPAND_MEMORY 12173 && ((modifier == EXPAND_CONST_ADDRESS 12174 || modifier == EXPAND_INITIALIZER) 12175 ? STRICT_ALIGNMENT 12176 : targetm.slow_unaligned_access (mode1, 12177 MEM_ALIGN (op0)))) 12178 || !multiple_p (bitpos, BITS_PER_UNIT))) 12179 /* If the type and the field are a constant size and the 12180 size of the type isn't the same size as the bitfield, 12181 we must use bitfield operations. */ 12182 || (known_size_p (bitsize) 12183 && TYPE_SIZE (TREE_TYPE (exp)) 12184 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp))) 12185 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))), 12186 bitsize))) 12187 { 12188 machine_mode ext_mode = mode; 12189 12190 if (ext_mode == BLKmode 12191 && ! (target != 0 && MEM_P (op0) 12192 && MEM_P (target) 12193 && multiple_p (bitpos, BITS_PER_UNIT))) 12194 ext_mode = int_mode_for_size (bitsize, 1).else_blk (); 12195 12196 if (ext_mode == BLKmode) 12197 { 12198 if (target == 0) 12199 target = assign_temp (type, 1, 1); 12200 12201 /* ??? Unlike the similar test a few lines below, this one is 12202 very likely obsolete. */ 12203 if (known_eq (bitsize, 0)) 12204 return target; 12205 12206 /* In this case, BITPOS must start at a byte boundary and 12207 TARGET, if specified, must be a MEM. */ 12208 gcc_assert (MEM_P (op0) 12209 && (!target || MEM_P (target))); 12210 12211 bytepos = exact_div (bitpos, BITS_PER_UNIT); 12212 poly_int64 bytesize = bits_to_bytes_round_up (bitsize); 12213 emit_block_move (target, 12214 adjust_address (op0, VOIDmode, bytepos), 12215 gen_int_mode (bytesize, Pmode), 12216 (modifier == EXPAND_STACK_PARM 12217 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL)); 12218 12219 return target; 12220 } 12221 12222 /* If we have nothing to extract, the result will be 0 for targets 12223 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always 12224 return 0 for the sake of consistency, as reading a zero-sized 12225 bitfield is valid in Ada and the value is fully specified. */ 12226 if (known_eq (bitsize, 0)) 12227 return const0_rtx; 12228 12229 op0 = validize_mem (op0); 12230 12231 if (MEM_P (op0) && REG_P (XEXP (op0, 0))) 12232 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0)); 12233 12234 /* If the result has aggregate type and the extraction is done in 12235 an integral mode, then the field may be not aligned on a byte 12236 boundary; in this case, if it has reverse storage order, it 12237 needs to be extracted as a scalar field with reverse storage 12238 order and put back into memory order afterwards. */ 12239 if (AGGREGATE_TYPE_P (type) 12240 && GET_MODE_CLASS (ext_mode) == MODE_INT) 12241 reversep = TYPE_REVERSE_STORAGE_ORDER (type); 12242 12243 gcc_checking_assert (known_ge (bitpos, 0)); 12244 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, 12245 (modifier == EXPAND_STACK_PARM 12246 ? NULL_RTX : target), 12247 ext_mode, ext_mode, reversep, alt_rtl); 12248 12249 /* If the result has aggregate type and the mode of OP0 is an 12250 integral mode then, if BITSIZE is narrower than this mode 12251 and this is for big-endian data, we must put the field 12252 into the high-order bits. And we must also put it back 12253 into memory order if it has been previously reversed. */ 12254 scalar_int_mode op0_mode; 12255 if (AGGREGATE_TYPE_P (type) 12256 && is_int_mode (GET_MODE (op0), &op0_mode)) 12257 { 12258 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode); 12259 12260 gcc_checking_assert (known_le (bitsize, size)); 12261 if (maybe_lt (bitsize, size) 12262 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN) 12263 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0, 12264 size - bitsize, op0, 1); 12265 12266 if (reversep) 12267 op0 = flip_storage_order (op0_mode, op0); 12268 } 12269 12270 /* If the result type is BLKmode, store the data into a temporary 12271 of the appropriate type, but with the mode corresponding to the 12272 mode for the data we have (op0's mode). */ 12273 if (mode == BLKmode) 12274 { 12275 rtx new_rtx 12276 = assign_stack_temp_for_type (ext_mode, 12277 GET_MODE_BITSIZE (ext_mode), 12278 type); 12279 emit_move_insn (new_rtx, op0); 12280 op0 = copy_rtx (new_rtx); 12281 PUT_MODE (op0, BLKmode); 12282 } 12283 12284 return op0; 12285 } 12286 12287 /* If the result is BLKmode, use that to access the object 12288 now as well. */ 12289 if (mode == BLKmode) 12290 mode1 = BLKmode; 12291 12292 /* Get a reference to just this component. */ 12293 bytepos = bits_to_bytes_round_down (bitpos); 12294 if (modifier == EXPAND_CONST_ADDRESS 12295 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER) 12296 op0 = adjust_address_nv (op0, mode1, bytepos); 12297 else 12298 op0 = adjust_address (op0, mode1, bytepos); 12299 12300 if (op0 == orig_op0) 12301 op0 = copy_rtx (op0); 12302 12303 /* Don't set memory attributes if the base expression is 12304 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case, 12305 we should just honor its original memory attributes. */ 12306 if (!(TREE_CODE (tem) == SSA_NAME 12307 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0)))) 12308 set_mem_attributes (op0, exp, 0); 12309 12310 if (REG_P (XEXP (op0, 0))) 12311 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0)); 12312 12313 /* If op0 is a temporary because the original expressions was forced 12314 to memory, clear MEM_EXPR so that the original expression cannot 12315 be marked as addressable through MEM_EXPR of the temporary. */ 12316 if (clear_mem_expr) 12317 set_mem_expr (op0, NULL_TREE); 12318 12319 MEM_VOLATILE_P (op0) |= volatilep; 12320 12321 if (reversep 12322 && modifier != EXPAND_MEMORY 12323 && modifier != EXPAND_WRITE) 12324 op0 = flip_storage_order (mode1, op0); 12325 12326 op0 = EXTEND_BITINT (op0); 12327 12328 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode 12329 || modifier == EXPAND_CONST_ADDRESS 12330 || modifier == EXPAND_INITIALIZER) 12331 return op0; 12332 12333 if (target == 0) 12334 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode); 12335 12336 convert_move (target, op0, unsignedp); 12337 return target; 12338 } 12339 12340 case OBJ_TYPE_REF: 12341 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier); 12342 12343 case CALL_EXPR: 12344 /* All valid uses of __builtin_va_arg_pack () are removed during 12345 inlining. */ 12346 if (CALL_EXPR_VA_ARG_PACK (exp)) 12347 error ("invalid use of %<__builtin_va_arg_pack ()%>"); 12348 { 12349 tree fndecl = get_callee_fndecl (exp), attr; 12350 12351 if (fndecl 12352 /* Don't diagnose the error attribute in thunks, those are 12353 artificially created. */ 12354 && !CALL_FROM_THUNK_P (exp) 12355 && (attr = lookup_attribute ("error", 12356 DECL_ATTRIBUTES (fndecl))) != NULL) 12357 { 12358 const char *ident = lang_hooks.decl_printable_name (fndecl, 1); 12359 error ("call to %qs declared with attribute error: %s", 12360 identifier_to_locale (ident), 12361 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)))); 12362 } 12363 if (fndecl 12364 /* Don't diagnose the warning attribute in thunks, those are 12365 artificially created. */ 12366 && !CALL_FROM_THUNK_P (exp) 12367 && (attr = lookup_attribute ("warning", 12368 DECL_ATTRIBUTES (fndecl))) != NULL) 12369 { 12370 const char *ident = lang_hooks.decl_printable_name (fndecl, 1); 12371 warning_at (EXPR_LOCATION (exp), 12372 OPT_Wattribute_warning, 12373 "call to %qs declared with attribute warning: %s", 12374 identifier_to_locale (ident), 12375 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)))); 12376 } 12377 12378 /* Check for a built-in function. */ 12379 if (fndecl && fndecl_built_in_p (fndecl)) 12380 { 12381 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND); 12382 return expand_builtin (exp, target, subtarget, tmode, ignore); 12383 } 12384 } 12385 temp = expand_call (exp, target, ignore); 12386 return EXTEND_BITINT (temp); 12387 12388 case VIEW_CONVERT_EXPR: 12389 op0 = NULL_RTX; 12390 12391 /* If we are converting to BLKmode, try to avoid an intermediate 12392 temporary by fetching an inner memory reference. */ 12393 if (mode == BLKmode 12394 && poly_int_tree_p (TYPE_SIZE (type)) 12395 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode 12396 && handled_component_p (treeop0)) 12397 { 12398 machine_mode mode1; 12399 poly_int64 bitsize, bitpos, bytepos; 12400 tree offset; 12401 int reversep, volatilep = 0; 12402 tree tem 12403 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1, 12404 &unsignedp, &reversep, &volatilep); 12405 12406 /* ??? We should work harder and deal with non-zero offsets. */ 12407 if (!offset 12408 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos) 12409 && !reversep 12410 && known_size_p (bitsize) 12411 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize)) 12412 { 12413 /* See the normal_inner_ref case for the rationale. */ 12414 rtx orig_op0 12415 = expand_expr_real (tem, 12416 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE 12417 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) 12418 != INTEGER_CST) 12419 && modifier != EXPAND_STACK_PARM 12420 ? target : NULL_RTX), 12421 VOIDmode, 12422 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier, 12423 NULL, true); 12424 12425 if (MEM_P (orig_op0)) 12426 { 12427 op0 = orig_op0; 12428 12429 /* Get a reference to just this component. */ 12430 if (modifier == EXPAND_CONST_ADDRESS 12431 || modifier == EXPAND_SUM 12432 || modifier == EXPAND_INITIALIZER) 12433 op0 = adjust_address_nv (op0, mode, bytepos); 12434 else 12435 op0 = adjust_address (op0, mode, bytepos); 12436 12437 if (op0 == orig_op0) 12438 op0 = copy_rtx (op0); 12439 12440 set_mem_attributes (op0, treeop0, 0); 12441 if (REG_P (XEXP (op0, 0))) 12442 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0)); 12443 12444 MEM_VOLATILE_P (op0) |= volatilep; 12445 } 12446 } 12447 } 12448 12449 if (!op0) 12450 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier, 12451 NULL, inner_reference_p || mode == BLKmode); 12452 12453 /* If the input and output modes are both the same, we are done. */ 12454 if (mode == GET_MODE (op0)) 12455 ; 12456 /* Similarly if the output mode is BLKmode and input is a MEM, 12457 adjust_address done below is all we need. */ 12458 else if (mode == BLKmode && MEM_P (op0)) 12459 ; 12460 /* If neither mode is BLKmode, and both modes are the same size 12461 then we can use gen_lowpart. */ 12462 else if (mode != BLKmode 12463 && GET_MODE (op0) != BLKmode 12464 && known_eq (GET_MODE_PRECISION (mode), 12465 GET_MODE_PRECISION (GET_MODE (op0))) 12466 && !COMPLEX_MODE_P (GET_MODE (op0))) 12467 { 12468 if (GET_CODE (op0) == SUBREG) 12469 op0 = force_reg (GET_MODE (op0), op0); 12470 temp = gen_lowpart_common (mode, op0); 12471 if (temp) 12472 op0 = temp; 12473 else 12474 { 12475 if (!REG_P (op0) && !MEM_P (op0)) 12476 op0 = force_reg (GET_MODE (op0), op0); 12477 op0 = gen_lowpart (mode, op0); 12478 } 12479 } 12480 /* If both types are integral, convert from one mode to the other. */ 12481 else if (INTEGRAL_TYPE_P (type) 12482 && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)) 12483 && mode != BLKmode 12484 && GET_MODE (op0) != BLKmode) 12485 op0 = convert_modes (mode, GET_MODE (op0), op0, 12486 TYPE_UNSIGNED (TREE_TYPE (treeop0))); 12487 /* If the output type is a bit-field type, do an extraction. */ 12488 else if (reduce_bit_field 12489 && mode != BLKmode 12490 && (MEM_P (op0) || !COMPLEX_MODE_P (GET_MODE (op0)))) 12491 return extract_bit_field (op0, TYPE_PRECISION (type), 0, 12492 TYPE_UNSIGNED (type), NULL_RTX, 12493 mode, mode, false, NULL); 12494 /* As a last resort, spill op0 to memory, and reload it in a 12495 different mode. */ 12496 else if (!MEM_P (op0)) 12497 { 12498 /* If the operand is not a MEM, force it into memory. Since we 12499 are going to be changing the mode of the MEM, don't call 12500 force_const_mem for constants because we don't allow pool 12501 constants to change mode. */ 12502 tree inner_type = TREE_TYPE (treeop0); 12503 12504 gcc_assert (!TREE_ADDRESSABLE (exp)); 12505 12506 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type)) 12507 target 12508 = assign_stack_temp_for_type 12509 (TYPE_MODE (inner_type), 12510 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type); 12511 12512 emit_move_insn (target, op0); 12513 op0 = target; 12514 12515 if (reduce_bit_field && mode != BLKmode) 12516 return extract_bit_field (op0, TYPE_PRECISION (type), 0, 12517 TYPE_UNSIGNED (type), NULL_RTX, 12518 mode, mode, false, NULL); 12519 } 12520 12521 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the 12522 output type is such that the operand is known to be aligned, indicate 12523 that it is. Otherwise, we need only be concerned about alignment for 12524 non-BLKmode results. */ 12525 if (MEM_P (op0)) 12526 { 12527 enum insn_code icode; 12528 12529 if (modifier != EXPAND_WRITE 12530 && modifier != EXPAND_MEMORY 12531 && !inner_reference_p 12532 && mode != BLKmode 12533 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)) 12534 { 12535 /* If the target does have special handling for unaligned 12536 loads of mode then use them. */ 12537 if ((icode = optab_handler (movmisalign_optab, mode)) 12538 != CODE_FOR_nothing) 12539 { 12540 rtx reg; 12541 12542 op0 = adjust_address (op0, mode, 0); 12543 /* We've already validated the memory, and we're creating a 12544 new pseudo destination. The predicates really can't 12545 fail. */ 12546 reg = gen_reg_rtx (mode); 12547 12548 /* Nor can the insn generator. */ 12549 rtx_insn *insn = GEN_FCN (icode) (reg, op0); 12550 emit_insn (insn); 12551 return reg; 12552 } 12553 else if (STRICT_ALIGNMENT) 12554 { 12555 poly_uint64 mode_size = GET_MODE_SIZE (mode); 12556 poly_uint64 temp_size = mode_size; 12557 if (GET_MODE (op0) != BLKmode) 12558 temp_size = upper_bound (temp_size, 12559 GET_MODE_SIZE (GET_MODE (op0))); 12560 rtx new_rtx 12561 = assign_stack_temp_for_type (mode, temp_size, type); 12562 rtx new_with_op0_mode 12563 = adjust_address (new_rtx, GET_MODE (op0), 0); 12564 12565 gcc_assert (!TREE_ADDRESSABLE (exp)); 12566 12567 if (GET_MODE (op0) == BLKmode) 12568 { 12569 rtx size_rtx = gen_int_mode (mode_size, Pmode); 12570 emit_block_move (new_with_op0_mode, op0, size_rtx, 12571 (modifier == EXPAND_STACK_PARM 12572 ? BLOCK_OP_CALL_PARM 12573 : BLOCK_OP_NORMAL)); 12574 } 12575 else 12576 emit_move_insn (new_with_op0_mode, op0); 12577 12578 op0 = new_rtx; 12579 } 12580 } 12581 12582 op0 = adjust_address (op0, mode, 0); 12583 } 12584 12585 return op0; 12586 12587 case MODIFY_EXPR: 12588 { 12589 tree lhs = treeop0; 12590 tree rhs = treeop1; 12591 gcc_assert (ignore); 12592 12593 /* Check for |= or &= of a bitfield of size one into another bitfield 12594 of size 1. In this case, (unless we need the result of the 12595 assignment) we can do this more efficiently with a 12596 test followed by an assignment, if necessary. 12597 12598 ??? At this point, we can't get a BIT_FIELD_REF here. But if 12599 things change so we do, this code should be enhanced to 12600 support it. */ 12601 if (TREE_CODE (lhs) == COMPONENT_REF 12602 && (TREE_CODE (rhs) == BIT_IOR_EXPR 12603 || TREE_CODE (rhs) == BIT_AND_EXPR) 12604 && TREE_OPERAND (rhs, 0) == lhs 12605 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF 12606 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1))) 12607 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1)))) 12608 { 12609 rtx_code_label *label = gen_label_rtx (); 12610 int value = TREE_CODE (rhs) == BIT_IOR_EXPR; 12611 profile_probability prob = profile_probability::uninitialized (); 12612 if (value) 12613 jumpifnot (TREE_OPERAND (rhs, 1), label, prob); 12614 else 12615 jumpif (TREE_OPERAND (rhs, 1), label, prob); 12616 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value), 12617 false); 12618 do_pending_stack_adjust (); 12619 emit_label (label); 12620 return const0_rtx; 12621 } 12622 12623 expand_assignment (lhs, rhs, false); 12624 return const0_rtx; 12625 } 12626 12627 case ADDR_EXPR: 12628 return expand_expr_addr_expr (exp, target, tmode, modifier); 12629 12630 case REALPART_EXPR: 12631 op0 = expand_normal (treeop0); 12632 return read_complex_part (op0, false); 12633 12634 case IMAGPART_EXPR: 12635 op0 = expand_normal (treeop0); 12636 return read_complex_part (op0, true); 12637 12638 case RETURN_EXPR: 12639 case LABEL_EXPR: 12640 case GOTO_EXPR: 12641 case SWITCH_EXPR: 12642 case ASM_EXPR: 12643 /* Expanded in cfgexpand.cc. */ 12644 gcc_unreachable (); 12645 12646 case TRY_CATCH_EXPR: 12647 case CATCH_EXPR: 12648 case EH_FILTER_EXPR: 12649 case TRY_FINALLY_EXPR: 12650 case EH_ELSE_EXPR: 12651 /* Lowered by tree-eh.cc. */ 12652 gcc_unreachable (); 12653 12654 case WITH_CLEANUP_EXPR: 12655 case CLEANUP_POINT_EXPR: 12656 case TARGET_EXPR: 12657 case CASE_LABEL_EXPR: 12658 case VA_ARG_EXPR: 12659 case BIND_EXPR: 12660 case INIT_EXPR: 12661 case CONJ_EXPR: 12662 case COMPOUND_EXPR: 12663 case PREINCREMENT_EXPR: 12664 case PREDECREMENT_EXPR: 12665 case POSTINCREMENT_EXPR: 12666 case POSTDECREMENT_EXPR: 12667 case LOOP_EXPR: 12668 case EXIT_EXPR: 12669 case COMPOUND_LITERAL_EXPR: 12670 /* Lowered by gimplify.cc. */ 12671 gcc_unreachable (); 12672 12673 case FDESC_EXPR: 12674 /* Function descriptors are not valid except for as 12675 initialization constants, and should not be expanded. */ 12676 gcc_unreachable (); 12677 12678 case WITH_SIZE_EXPR: 12679 /* WITH_SIZE_EXPR expands to its first argument. The caller should 12680 have pulled out the size to use in whatever context it needed. */ 12681 return expand_expr_real (treeop0, original_target, tmode, 12682 modifier, alt_rtl, inner_reference_p); 12683 12684 default: 12685 return expand_expr_real_2 (&ops, target, tmode, modifier); 12686 } 12687 } 12688 #undef EXTEND_BITINT 12689 12690 /* Subroutine of above: reduce EXP to the precision of TYPE (in the 12692 signedness of TYPE), possibly returning the result in TARGET. 12693 TYPE is known to be a partial integer type. */ 12694 static rtx 12695 reduce_to_bit_field_precision (rtx exp, rtx target, tree type) 12696 { 12697 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type); 12698 HOST_WIDE_INT prec = TYPE_PRECISION (type); 12699 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode) 12700 && (!target || GET_MODE (target) == mode)); 12701 12702 /* For constant values, reduce using wide_int_to_tree. */ 12703 if (poly_int_rtx_p (exp)) 12704 { 12705 auto value = wi::to_poly_wide (exp, mode); 12706 tree t = wide_int_to_tree (type, value); 12707 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL); 12708 } 12709 else if (TYPE_UNSIGNED (type)) 12710 { 12711 rtx mask = immed_wide_int_const 12712 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode); 12713 return expand_and (mode, exp, mask, target); 12714 } 12715 else 12716 { 12717 int count = GET_MODE_PRECISION (mode) - prec; 12718 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0); 12719 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0); 12720 } 12721 } 12722 12723 /* Subroutine of above: returns true if OFFSET corresponds to an offset that 12725 when applied to the address of EXP produces an address known to be 12726 aligned more than BIGGEST_ALIGNMENT. */ 12727 12728 static bool 12729 is_aligning_offset (const_tree offset, const_tree exp) 12730 { 12731 /* Strip off any conversions. */ 12732 while (CONVERT_EXPR_P (offset)) 12733 offset = TREE_OPERAND (offset, 0); 12734 12735 /* We must now have a BIT_AND_EXPR with a constant that is one less than 12736 power of 2 and which is larger than BIGGEST_ALIGNMENT. */ 12737 if (TREE_CODE (offset) != BIT_AND_EXPR 12738 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1)) 12739 || compare_tree_int (TREE_OPERAND (offset, 1), 12740 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0 12741 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1)) 12742 return false; 12743 12744 /* Look at the first operand of BIT_AND_EXPR and strip any conversion. 12745 It must be NEGATE_EXPR. Then strip any more conversions. */ 12746 offset = TREE_OPERAND (offset, 0); 12747 while (CONVERT_EXPR_P (offset)) 12748 offset = TREE_OPERAND (offset, 0); 12749 12750 if (TREE_CODE (offset) != NEGATE_EXPR) 12751 return false; 12752 12753 offset = TREE_OPERAND (offset, 0); 12754 while (CONVERT_EXPR_P (offset)) 12755 offset = TREE_OPERAND (offset, 0); 12756 12757 /* This must now be the address of EXP. */ 12758 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp; 12759 } 12760 12761 /* Return a STRING_CST corresponding to ARG's constant initializer either 12762 if it's a string constant, or, when VALREP is set, any other constant, 12763 or null otherwise. 12764 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset 12765 within the byte string that ARG is references. If nonnull set *MEM_SIZE 12766 to the size of the byte string. If nonnull, set *DECL to the constant 12767 declaration ARG refers to. */ 12768 12769 static tree 12770 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl, 12771 bool valrep = false) 12772 { 12773 tree dummy = NULL_TREE; 12774 if (!mem_size) 12775 mem_size = &dummy; 12776 12777 /* Store the type of the original expression before conversions 12778 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been 12779 removed. */ 12780 tree argtype = TREE_TYPE (arg); 12781 12782 tree array; 12783 STRIP_NOPS (arg); 12784 12785 /* Non-constant index into the character array in an ARRAY_REF 12786 expression or null. */ 12787 tree varidx = NULL_TREE; 12788 12789 poly_int64 base_off = 0; 12790 12791 if (TREE_CODE (arg) == ADDR_EXPR) 12792 { 12793 arg = TREE_OPERAND (arg, 0); 12794 tree ref = arg; 12795 if (TREE_CODE (arg) == ARRAY_REF) 12796 { 12797 tree idx = TREE_OPERAND (arg, 1); 12798 if (TREE_CODE (idx) != INTEGER_CST) 12799 { 12800 /* From a pointer (but not array) argument extract the variable 12801 index to prevent get_addr_base_and_unit_offset() from failing 12802 due to it. Use it later to compute the non-constant offset 12803 into the string and return it to the caller. */ 12804 varidx = idx; 12805 ref = TREE_OPERAND (arg, 0); 12806 12807 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE) 12808 return NULL_TREE; 12809 12810 if (!integer_zerop (array_ref_low_bound (arg))) 12811 return NULL_TREE; 12812 12813 if (!integer_onep (array_ref_element_size (arg))) 12814 return NULL_TREE; 12815 } 12816 } 12817 array = get_addr_base_and_unit_offset (ref, &base_off); 12818 if (!array 12819 || (TREE_CODE (array) != VAR_DECL 12820 && TREE_CODE (array) != CONST_DECL 12821 && TREE_CODE (array) != STRING_CST)) 12822 return NULL_TREE; 12823 } 12824 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR) 12825 { 12826 tree arg0 = TREE_OPERAND (arg, 0); 12827 tree arg1 = TREE_OPERAND (arg, 1); 12828 12829 tree offset; 12830 tree str = string_constant (arg0, &offset, mem_size, decl); 12831 if (!str) 12832 { 12833 str = string_constant (arg1, &offset, mem_size, decl); 12834 arg1 = arg0; 12835 } 12836 12837 if (str) 12838 { 12839 /* Avoid pointers to arrays (see bug 86622). */ 12840 if (POINTER_TYPE_P (TREE_TYPE (arg)) 12841 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE 12842 && !(decl && !*decl) 12843 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl)) 12844 && tree_fits_uhwi_p (*mem_size) 12845 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl)))) 12846 return NULL_TREE; 12847 12848 tree type = TREE_TYPE (offset); 12849 arg1 = fold_convert (type, arg1); 12850 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1); 12851 return str; 12852 } 12853 return NULL_TREE; 12854 } 12855 else if (TREE_CODE (arg) == SSA_NAME) 12856 { 12857 gimple *stmt = SSA_NAME_DEF_STMT (arg); 12858 if (!is_gimple_assign (stmt)) 12859 return NULL_TREE; 12860 12861 tree rhs1 = gimple_assign_rhs1 (stmt); 12862 tree_code code = gimple_assign_rhs_code (stmt); 12863 if (code == ADDR_EXPR) 12864 return string_constant (rhs1, ptr_offset, mem_size, decl); 12865 else if (code != POINTER_PLUS_EXPR) 12866 return NULL_TREE; 12867 12868 tree offset; 12869 if (tree str = string_constant (rhs1, &offset, mem_size, decl)) 12870 { 12871 /* Avoid pointers to arrays (see bug 86622). */ 12872 if (POINTER_TYPE_P (TREE_TYPE (rhs1)) 12873 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE 12874 && !(decl && !*decl) 12875 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl)) 12876 && tree_fits_uhwi_p (*mem_size) 12877 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl)))) 12878 return NULL_TREE; 12879 12880 tree rhs2 = gimple_assign_rhs2 (stmt); 12881 tree type = TREE_TYPE (offset); 12882 rhs2 = fold_convert (type, rhs2); 12883 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2); 12884 return str; 12885 } 12886 return NULL_TREE; 12887 } 12888 else if (DECL_P (arg)) 12889 array = arg; 12890 else 12891 return NULL_TREE; 12892 12893 tree offset = wide_int_to_tree (sizetype, base_off); 12894 if (varidx) 12895 { 12896 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE) 12897 return NULL_TREE; 12898 12899 gcc_assert (TREE_CODE (arg) == ARRAY_REF); 12900 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0))); 12901 if (TREE_CODE (chartype) != INTEGER_TYPE) 12902 return NULL; 12903 12904 offset = fold_convert (sizetype, varidx); 12905 } 12906 12907 if (TREE_CODE (array) == STRING_CST) 12908 { 12909 *ptr_offset = fold_convert (sizetype, offset); 12910 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array)); 12911 if (decl) 12912 *decl = NULL_TREE; 12913 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array))) 12914 >= TREE_STRING_LENGTH (array)); 12915 return array; 12916 } 12917 12918 tree init = ctor_for_folding (array); 12919 if (!init || init == error_mark_node) 12920 return NULL_TREE; 12921 12922 if (valrep) 12923 { 12924 HOST_WIDE_INT cstoff; 12925 if (!base_off.is_constant (&cstoff)) 12926 return NULL_TREE; 12927 12928 /* Check that the host and target are sane. */ 12929 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) 12930 return NULL_TREE; 12931 12932 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init)); 12933 if (typesz <= 0 || (int) typesz != typesz) 12934 return NULL_TREE; 12935 12936 HOST_WIDE_INT size = typesz; 12937 if (VAR_P (array) 12938 && DECL_SIZE_UNIT (array) 12939 && tree_fits_shwi_p (DECL_SIZE_UNIT (array))) 12940 { 12941 size = tree_to_shwi (DECL_SIZE_UNIT (array)); 12942 gcc_checking_assert (size >= typesz); 12943 } 12944 12945 /* If value representation was requested convert the initializer 12946 for the whole array or object into a string of bytes forming 12947 its value representation and return it. */ 12948 unsigned char *bytes = XNEWVEC (unsigned char, size); 12949 int r = native_encode_initializer (init, bytes, size); 12950 if (r < typesz) 12951 { 12952 XDELETEVEC (bytes); 12953 return NULL_TREE; 12954 } 12955 12956 if (r < size) 12957 memset (bytes + r, '\0', size - r); 12958 12959 const char *p = reinterpret_cast<const char *>(bytes); 12960 init = build_string_literal (size, p, char_type_node); 12961 init = TREE_OPERAND (init, 0); 12962 init = TREE_OPERAND (init, 0); 12963 XDELETE (bytes); 12964 12965 *mem_size = size_int (TREE_STRING_LENGTH (init)); 12966 *ptr_offset = wide_int_to_tree (ssizetype, base_off); 12967 12968 if (decl) 12969 *decl = array; 12970 12971 return init; 12972 } 12973 12974 if (TREE_CODE (init) == CONSTRUCTOR) 12975 { 12976 /* Convert the 64-bit constant offset to a wider type to avoid 12977 overflow and use it to obtain the initializer for the subobject 12978 it points into. */ 12979 offset_int wioff; 12980 if (!base_off.is_constant (&wioff)) 12981 return NULL_TREE; 12982 12983 wioff *= BITS_PER_UNIT; 12984 if (!wi::fits_uhwi_p (wioff)) 12985 return NULL_TREE; 12986 12987 base_off = wioff.to_uhwi (); 12988 unsigned HOST_WIDE_INT fieldoff = 0; 12989 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array, 12990 &fieldoff); 12991 if (!init || init == error_mark_node) 12992 return NULL_TREE; 12993 12994 HOST_WIDE_INT cstoff; 12995 if (!base_off.is_constant (&cstoff)) 12996 return NULL_TREE; 12997 12998 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT; 12999 tree off = build_int_cst (sizetype, cstoff); 13000 if (varidx) 13001 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off); 13002 else 13003 offset = off; 13004 } 13005 13006 *ptr_offset = offset; 13007 13008 tree inittype = TREE_TYPE (init); 13009 13010 if (TREE_CODE (init) == INTEGER_CST 13011 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE 13012 || TYPE_MAIN_VARIANT (inittype) == char_type_node)) 13013 { 13014 /* Check that the host and target are sane. */ 13015 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) 13016 return NULL_TREE; 13017 13018 /* For a reference to (address of) a single constant character, 13019 store the native representation of the character in CHARBUF. 13020 If the reference is to an element of an array or a member 13021 of a struct, only consider narrow characters until ctors 13022 for wide character arrays are transformed to STRING_CSTs 13023 like those for narrow arrays. */ 13024 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; 13025 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0); 13026 if (len > 0) 13027 { 13028 /* Construct a string literal with elements of INITTYPE and 13029 the representation above. Then strip 13030 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */ 13031 init = build_string_literal (len, (char *)charbuf, inittype); 13032 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0); 13033 } 13034 } 13035 13036 tree initsize = TYPE_SIZE_UNIT (inittype); 13037 13038 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init)) 13039 { 13040 /* Fold an empty/zero constructor for an implicitly initialized 13041 object or subobject into the empty string. */ 13042 13043 /* Determine the character type from that of the original 13044 expression. */ 13045 tree chartype = argtype; 13046 if (POINTER_TYPE_P (chartype)) 13047 chartype = TREE_TYPE (chartype); 13048 while (TREE_CODE (chartype) == ARRAY_TYPE) 13049 chartype = TREE_TYPE (chartype); 13050 13051 if (INTEGRAL_TYPE_P (chartype) 13052 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node)) 13053 { 13054 /* Convert a char array to an empty STRING_CST having an array 13055 of the expected type and size. */ 13056 if (!initsize) 13057 initsize = integer_zero_node; 13058 13059 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize); 13060 if (size > (unsigned HOST_WIDE_INT) INT_MAX) 13061 return NULL_TREE; 13062 13063 init = build_string_literal (size, NULL, chartype, size); 13064 init = TREE_OPERAND (init, 0); 13065 init = TREE_OPERAND (init, 0); 13066 13067 *ptr_offset = integer_zero_node; 13068 } 13069 } 13070 13071 if (decl) 13072 *decl = array; 13073 13074 if (TREE_CODE (init) != STRING_CST) 13075 return NULL_TREE; 13076 13077 *mem_size = initsize; 13078 13079 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init)); 13080 13081 return init; 13082 } 13083 13084 /* Return STRING_CST if an ARG corresponds to a string constant or zero 13085 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly 13086 non-constant) offset in bytes within the string that ARG is accessing. 13087 If MEM_SIZE is non-zero the storage size of the memory is returned. 13088 If DECL is non-zero the constant declaration is returned if available. */ 13089 13090 tree 13091 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl) 13092 { 13093 return constant_byte_string (arg, ptr_offset, mem_size, decl, false); 13094 } 13095 13096 /* Similar to string_constant, return a STRING_CST corresponding 13097 to the value representation of the first argument if it's 13098 a constant. */ 13099 13100 tree 13101 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl) 13102 { 13103 return constant_byte_string (arg, ptr_offset, mem_size, decl, true); 13104 } 13105 13106 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2 13107 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)): 13108 for C2 > 0 to x & C3 == C2 13109 for C2 < 0 to x & C3 == (C2 & C3). */ 13110 enum tree_code 13111 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1) 13112 { 13113 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR); 13114 tree treeop0 = gimple_assign_rhs1 (stmt); 13115 tree treeop1 = gimple_assign_rhs2 (stmt); 13116 tree type = TREE_TYPE (*arg0); 13117 scalar_int_mode mode; 13118 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode)) 13119 return code; 13120 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type) 13121 || TYPE_PRECISION (type) <= 1 13122 || TYPE_UNSIGNED (type) 13123 /* Signed x % c == 0 should have been optimized into unsigned modulo 13124 earlier. */ 13125 || integer_zerop (*arg1) 13126 /* If c is known to be non-negative, modulo will be expanded as unsigned 13127 modulo. */ 13128 || get_range_pos_neg (treeop0) == 1) 13129 return code; 13130 13131 /* x % c == d where d < 0 && d <= -c should be always false. */ 13132 if (tree_int_cst_sgn (*arg1) == -1 13133 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1)) 13134 return code; 13135 13136 int prec = TYPE_PRECISION (type); 13137 wide_int w = wi::to_wide (treeop1) - 1; 13138 w |= wi::shifted_mask (0, prec - 1, true, prec); 13139 tree c3 = wide_int_to_tree (type, w); 13140 tree c4 = *arg1; 13141 if (tree_int_cst_sgn (*arg1) == -1) 13142 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1)); 13143 13144 rtx op0 = expand_normal (treeop0); 13145 treeop0 = make_tree (TREE_TYPE (treeop0), op0); 13146 13147 bool speed_p = optimize_insn_for_speed_p (); 13148 13149 do_pending_stack_adjust (); 13150 13151 location_t loc = gimple_location (stmt); 13152 struct separate_ops ops; 13153 ops.code = TRUNC_MOD_EXPR; 13154 ops.location = loc; 13155 ops.type = TREE_TYPE (treeop0); 13156 ops.op0 = treeop0; 13157 ops.op1 = treeop1; 13158 ops.op2 = NULL_TREE; 13159 start_sequence (); 13160 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type), 13161 EXPAND_NORMAL); 13162 rtx_insn *moinsns = get_insns (); 13163 end_sequence (); 13164 13165 unsigned mocost = seq_cost (moinsns, speed_p); 13166 mocost += rtx_cost (mor, mode, EQ, 0, speed_p); 13167 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p); 13168 13169 ops.code = BIT_AND_EXPR; 13170 ops.location = loc; 13171 ops.type = TREE_TYPE (treeop0); 13172 ops.op0 = treeop0; 13173 ops.op1 = c3; 13174 ops.op2 = NULL_TREE; 13175 start_sequence (); 13176 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type), 13177 EXPAND_NORMAL); 13178 rtx_insn *muinsns = get_insns (); 13179 end_sequence (); 13180 13181 unsigned mucost = seq_cost (muinsns, speed_p); 13182 mucost += rtx_cost (mur, mode, EQ, 0, speed_p); 13183 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p); 13184 13185 if (mocost <= mucost) 13186 { 13187 emit_insn (moinsns); 13188 *arg0 = make_tree (TREE_TYPE (*arg0), mor); 13189 return code; 13190 } 13191 13192 emit_insn (muinsns); 13193 *arg0 = make_tree (TREE_TYPE (*arg0), mur); 13194 *arg1 = c4; 13195 return code; 13196 } 13197 13198 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2). 13199 If C1 is odd to: 13200 (X - C2) * C3 <= C4 (or >), where 13201 C3 is modular multiplicative inverse of C1 and 1<<prec and 13202 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter 13203 if C2 > ((1<<prec) - 1) % C1). 13204 If C1 is even, S = ctz (C1) and C2 is 0, use 13205 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative 13206 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S. 13207 13208 For signed (X % C1) == 0 if C1 is odd to (all operations in it 13209 unsigned): 13210 (X * C3) + C4 <= 2 * C4, where 13211 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and 13212 C4 is ((1<<(prec - 1) - 1) / C1). 13213 If C1 is even, S = ctz(C1), use 13214 ((X * C3) + C4) r>> S <= (C4 >> (S - 1)) 13215 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec 13216 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S). 13217 13218 See the Hacker's Delight book, section 10-17. */ 13219 enum tree_code 13220 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1) 13221 { 13222 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR); 13223 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST); 13224 13225 if (optimize < 2) 13226 return code; 13227 13228 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR); 13229 if (stmt == NULL) 13230 return code; 13231 13232 tree treeop0 = gimple_assign_rhs1 (stmt); 13233 tree treeop1 = gimple_assign_rhs2 (stmt); 13234 if (TREE_CODE (treeop0) != SSA_NAME 13235 || TREE_CODE (treeop1) != INTEGER_CST 13236 /* Don't optimize the undefined behavior case x % 0; 13237 x % 1 should have been optimized into zero, punt if 13238 it makes it here for whatever reason; 13239 x % -c should have been optimized into x % c. */ 13240 || compare_tree_int (treeop1, 2) <= 0 13241 /* Likewise x % c == d where d >= c should be always false. */ 13242 || tree_int_cst_le (treeop1, *arg1)) 13243 return code; 13244 13245 /* Unsigned x % pow2 is handled right already, for signed 13246 modulo handle it in maybe_optimize_pow2p_mod_cmp. */ 13247 if (integer_pow2p (treeop1)) 13248 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1); 13249 13250 tree type = TREE_TYPE (*arg0); 13251 scalar_int_mode mode; 13252 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode)) 13253 return code; 13254 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type) 13255 || TYPE_PRECISION (type) <= 1) 13256 return code; 13257 13258 signop sgn = UNSIGNED; 13259 /* If both operands are known to have the sign bit clear, handle 13260 even the signed modulo case as unsigned. treeop1 is always 13261 positive >= 2, checked above. */ 13262 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1) 13263 sgn = SIGNED; 13264 13265 if (!TYPE_UNSIGNED (type)) 13266 { 13267 if (tree_int_cst_sgn (*arg1) == -1) 13268 return code; 13269 type = unsigned_type_for (type); 13270 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0))) 13271 return code; 13272 } 13273 13274 int prec = TYPE_PRECISION (type); 13275 wide_int w = wi::to_wide (treeop1); 13276 int shift = wi::ctz (w); 13277 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if 13278 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0. 13279 If C1 is odd, we can handle all cases by subtracting 13280 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases 13281 e.g. by testing for overflow on the subtraction, punt on that for now 13282 though. */ 13283 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1)) 13284 { 13285 if (sgn == SIGNED) 13286 return code; 13287 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w); 13288 if (wi::gtu_p (wi::to_wide (*arg1), x)) 13289 return code; 13290 } 13291 13292 imm_use_iterator imm_iter; 13293 use_operand_p use_p; 13294 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0) 13295 { 13296 gimple *use_stmt = USE_STMT (use_p); 13297 /* Punt if treeop0 is used in the same bb in a division 13298 or another modulo with the same divisor. We should expect 13299 the division and modulo combined together. */ 13300 if (use_stmt == stmt 13301 || gimple_bb (use_stmt) != gimple_bb (stmt)) 13302 continue; 13303 if (!is_gimple_assign (use_stmt) 13304 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR 13305 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR)) 13306 continue; 13307 if (gimple_assign_rhs1 (use_stmt) != treeop0 13308 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0)) 13309 continue; 13310 return code; 13311 } 13312 13313 w = wi::lrshift (w, shift); 13314 wide_int a = wide_int::from (w, prec + 1, UNSIGNED); 13315 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1); 13316 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED); 13317 tree c3 = wide_int_to_tree (type, m); 13318 tree c5 = NULL_TREE; 13319 wide_int d, e; 13320 if (sgn == UNSIGNED) 13321 { 13322 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e); 13323 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1, 13324 otherwise use < or subtract one from C4. E.g. for 13325 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but 13326 x % 3U == 1 already needs to be 13327 (x - 1) * 0xaaaaaaabU <= 0x55555554. */ 13328 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e)) 13329 d -= 1; 13330 if (shift) 13331 d = wi::lrshift (d, shift); 13332 } 13333 else 13334 { 13335 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w); 13336 if (!shift) 13337 d = wi::lshift (e, 1); 13338 else 13339 { 13340 e = wi::bit_and (e, wi::mask (shift, true, prec)); 13341 d = wi::lrshift (e, shift - 1); 13342 } 13343 c5 = wide_int_to_tree (type, e); 13344 } 13345 tree c4 = wide_int_to_tree (type, d); 13346 13347 rtx op0 = expand_normal (treeop0); 13348 treeop0 = make_tree (TREE_TYPE (treeop0), op0); 13349 13350 bool speed_p = optimize_insn_for_speed_p (); 13351 13352 do_pending_stack_adjust (); 13353 13354 location_t loc = gimple_location (stmt); 13355 struct separate_ops ops; 13356 ops.code = TRUNC_MOD_EXPR; 13357 ops.location = loc; 13358 ops.type = TREE_TYPE (treeop0); 13359 ops.op0 = treeop0; 13360 ops.op1 = treeop1; 13361 ops.op2 = NULL_TREE; 13362 start_sequence (); 13363 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type), 13364 EXPAND_NORMAL); 13365 rtx_insn *moinsns = get_insns (); 13366 end_sequence (); 13367 13368 unsigned mocost = seq_cost (moinsns, speed_p); 13369 mocost += rtx_cost (mor, mode, EQ, 0, speed_p); 13370 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p); 13371 13372 tree t = fold_convert_loc (loc, type, treeop0); 13373 if (!integer_zerop (*arg1)) 13374 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1)); 13375 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3); 13376 if (sgn == SIGNED) 13377 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5); 13378 if (shift) 13379 { 13380 tree s = build_int_cst (NULL_TREE, shift); 13381 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s); 13382 } 13383 13384 start_sequence (); 13385 rtx mur = expand_normal (t); 13386 rtx_insn *muinsns = get_insns (); 13387 end_sequence (); 13388 13389 unsigned mucost = seq_cost (muinsns, speed_p); 13390 mucost += rtx_cost (mur, mode, LE, 0, speed_p); 13391 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p); 13392 13393 if (mocost <= mucost) 13394 { 13395 emit_insn (moinsns); 13396 *arg0 = make_tree (TREE_TYPE (*arg0), mor); 13397 return code; 13398 } 13399 13400 emit_insn (muinsns); 13401 *arg0 = make_tree (type, mur); 13402 *arg1 = c4; 13403 return code == EQ_EXPR ? LE_EXPR : GT_EXPR; 13404 } 13405 13406 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */ 13407 13408 void 13409 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1) 13410 { 13411 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR 13412 || code == LT_EXPR || code == LE_EXPR); 13413 gcc_checking_assert (integer_zerop (*arg1)); 13414 13415 if (!optimize) 13416 return; 13417 13418 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR); 13419 if (stmt == NULL) 13420 return; 13421 13422 tree treeop0 = gimple_assign_rhs1 (stmt); 13423 tree treeop1 = gimple_assign_rhs2 (stmt); 13424 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0))) 13425 return; 13426 13427 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON)) 13428 warning_at (gimple_location (stmt), OPT_Wstrict_overflow, 13429 "assuming signed overflow does not occur when " 13430 "simplifying %<X - Y %s 0%> to %<X %s Y%>", 13431 op_symbol_code (code), op_symbol_code (code)); 13432 13433 *arg0 = treeop0; 13434 *arg1 = treeop1; 13435 } 13436 13437 13439 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents 13440 a single bit equality/inequality test, returns where the result is located. */ 13441 13442 static rtx 13443 expand_single_bit_test (location_t loc, enum tree_code code, 13444 tree inner, int bitnum, 13445 tree result_type, rtx target, 13446 machine_mode mode) 13447 { 13448 gcc_assert (code == NE_EXPR || code == EQ_EXPR); 13449 13450 tree type = TREE_TYPE (inner); 13451 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type); 13452 int ops_unsigned; 13453 tree signed_type, unsigned_type, intermediate_type; 13454 gimple *inner_def; 13455 13456 /* First, see if we can fold the single bit test into a sign-bit 13457 test. */ 13458 if (bitnum == TYPE_PRECISION (type) - 1 13459 && type_has_mode_precision_p (type)) 13460 { 13461 tree stype = signed_type_for (type); 13462 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, 13463 result_type, 13464 fold_convert_loc (loc, stype, inner), 13465 build_int_cst (stype, 0)); 13466 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL); 13467 } 13468 13469 /* Otherwise we have (A & C) != 0 where C is a single bit, 13470 convert that into ((A >> C2) & 1). Where C2 = log2(C). 13471 Similarly for (A & C) == 0. */ 13472 13473 /* If INNER is a right shift of a constant and it plus BITNUM does 13474 not overflow, adjust BITNUM and INNER. */ 13475 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR)) 13476 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST 13477 && bitnum < TYPE_PRECISION (type) 13478 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)), 13479 TYPE_PRECISION (type) - bitnum)) 13480 { 13481 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def)); 13482 inner = gimple_assign_rhs1 (inner_def); 13483 } 13484 13485 /* If we are going to be able to omit the AND below, we must do our 13486 operations as unsigned. If we must use the AND, we have a choice. 13487 Normally unsigned is faster, but for some machines signed is. */ 13488 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND 13489 && !flag_syntax_only) ? 0 : 1; 13490 13491 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0); 13492 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1); 13493 intermediate_type = ops_unsigned ? unsigned_type : signed_type; 13494 inner = fold_convert_loc (loc, intermediate_type, inner); 13495 13496 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL); 13497 13498 if (CONST_SCALAR_INT_P (inner0)) 13499 { 13500 wide_int t = rtx_mode_t (inner0, operand_mode); 13501 bool setp = (wi::lrshift (t, bitnum) & 1) != 0; 13502 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx; 13503 } 13504 int bitpos = bitnum; 13505 13506 if (BYTES_BIG_ENDIAN) 13507 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos; 13508 13509 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target, 13510 operand_mode, mode, 0, NULL); 13511 13512 if (code == EQ_EXPR) 13513 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx, 13514 NULL_RTX, 1, OPTAB_LIB_WIDEN); 13515 if (GET_MODE (inner0) != mode) 13516 { 13517 rtx t = gen_reg_rtx (mode); 13518 convert_move (t, inner0, 0); 13519 return t; 13520 } 13521 return inner0; 13522 } 13523 13524 /* Generate code to calculate OPS, and exploded expression 13525 using a store-flag instruction and return an rtx for the result. 13526 OPS reflects a comparison. 13527 13528 If TARGET is nonzero, store the result there if convenient. 13529 13530 Return zero if there is no suitable set-flag instruction 13531 available on this machine. 13532 13533 Once expand_expr has been called on the arguments of the comparison, 13534 we are committed to doing the store flag, since it is not safe to 13535 re-evaluate the expression. We emit the store-flag insn by calling 13536 emit_store_flag, but only expand the arguments if we have a reason 13537 to believe that emit_store_flag will be successful. If we think that 13538 it will, but it isn't, we have to simulate the store-flag with a 13539 set/jump/set sequence. */ 13540 13541 static rtx 13542 do_store_flag (sepops ops, rtx target, machine_mode mode) 13543 { 13544 enum rtx_code code; 13545 tree arg0, arg1, type; 13546 machine_mode operand_mode; 13547 int unsignedp; 13548 rtx op0, op1; 13549 rtx subtarget = target; 13550 location_t loc = ops->location; 13551 unsigned HOST_WIDE_INT nunits; 13552 13553 arg0 = ops->op0; 13554 arg1 = ops->op1; 13555 13556 /* Don't crash if the comparison was erroneous. */ 13557 if (arg0 == error_mark_node || arg1 == error_mark_node) 13558 return const0_rtx; 13559 13560 type = TREE_TYPE (arg0); 13561 operand_mode = TYPE_MODE (type); 13562 unsignedp = TYPE_UNSIGNED (type); 13563 13564 /* We won't bother with BLKmode store-flag operations because it would mean 13565 passing a lot of information to emit_store_flag. */ 13566 if (operand_mode == BLKmode) 13567 return 0; 13568 13569 /* We won't bother with store-flag operations involving function pointers 13570 when function pointers must be canonicalized before comparisons. */ 13571 if (targetm.have_canonicalize_funcptr_for_compare () 13572 && ((POINTER_TYPE_P (TREE_TYPE (arg0)) 13573 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))) 13574 || (POINTER_TYPE_P (TREE_TYPE (arg1)) 13575 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))))) 13576 return 0; 13577 13578 STRIP_NOPS (arg0); 13579 STRIP_NOPS (arg1); 13580 13581 /* For vector typed comparisons emit code to generate the desired 13582 all-ones or all-zeros mask. */ 13583 if (VECTOR_TYPE_P (ops->type)) 13584 { 13585 tree ifexp = build2 (ops->code, ops->type, arg0, arg1); 13586 if (VECTOR_BOOLEAN_TYPE_P (ops->type) 13587 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code)) 13588 return expand_vec_cmp_expr (ops->type, ifexp, target); 13589 else 13590 gcc_unreachable (); 13591 } 13592 13593 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial 13594 into (x - C2) * C3 < C4. */ 13595 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR) 13596 && TREE_CODE (arg0) == SSA_NAME 13597 && TREE_CODE (arg1) == INTEGER_CST) 13598 { 13599 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code, 13600 &arg0, &arg1); 13601 if (new_code != ops->code) 13602 { 13603 struct separate_ops nops = *ops; 13604 nops.code = ops->code = new_code; 13605 nops.op0 = arg0; 13606 nops.op1 = arg1; 13607 nops.type = TREE_TYPE (arg0); 13608 return do_store_flag (&nops, target, mode); 13609 } 13610 } 13611 13612 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */ 13613 if (!unsignedp 13614 && (ops->code == LT_EXPR || ops->code == LE_EXPR 13615 || ops->code == GT_EXPR || ops->code == GE_EXPR) 13616 && integer_zerop (arg1) 13617 && TREE_CODE (arg0) == SSA_NAME) 13618 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1); 13619 13620 /* Get the rtx comparison code to use. We know that EXP is a comparison 13621 operation of some type. Some comparisons against 1 and -1 can be 13622 converted to comparisons with zero. Do so here so that the tests 13623 below will be aware that we have a comparison with zero. These 13624 tests will not catch constants in the first operand, but constants 13625 are rarely passed as the first operand. */ 13626 13627 switch (ops->code) 13628 { 13629 case EQ_EXPR: 13630 code = EQ; 13631 break; 13632 case NE_EXPR: 13633 code = NE; 13634 break; 13635 case LT_EXPR: 13636 if (integer_onep (arg1)) 13637 arg1 = integer_zero_node, code = unsignedp ? LEU : LE; 13638 else 13639 code = unsignedp ? LTU : LT; 13640 break; 13641 case LE_EXPR: 13642 if (! unsignedp && integer_all_onesp (arg1)) 13643 arg1 = integer_zero_node, code = LT; 13644 else 13645 code = unsignedp ? LEU : LE; 13646 break; 13647 case GT_EXPR: 13648 if (! unsignedp && integer_all_onesp (arg1)) 13649 arg1 = integer_zero_node, code = GE; 13650 else 13651 code = unsignedp ? GTU : GT; 13652 break; 13653 case GE_EXPR: 13654 if (integer_onep (arg1)) 13655 arg1 = integer_zero_node, code = unsignedp ? GTU : GT; 13656 else 13657 code = unsignedp ? GEU : GE; 13658 break; 13659 13660 case UNORDERED_EXPR: 13661 code = UNORDERED; 13662 break; 13663 case ORDERED_EXPR: 13664 code = ORDERED; 13665 break; 13666 case UNLT_EXPR: 13667 code = UNLT; 13668 break; 13669 case UNLE_EXPR: 13670 code = UNLE; 13671 break; 13672 case UNGT_EXPR: 13673 code = UNGT; 13674 break; 13675 case UNGE_EXPR: 13676 code = UNGE; 13677 break; 13678 case UNEQ_EXPR: 13679 code = UNEQ; 13680 break; 13681 case LTGT_EXPR: 13682 code = LTGT; 13683 break; 13684 13685 default: 13686 gcc_unreachable (); 13687 } 13688 13689 /* Put a constant second. */ 13690 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST 13691 || TREE_CODE (arg0) == FIXED_CST) 13692 { 13693 std::swap (arg0, arg1); 13694 code = swap_condition (code); 13695 } 13696 13697 /* If this is an equality or inequality test of a single bit, we can 13698 do this by shifting the bit being tested to the low-order bit and 13699 masking the result with the constant 1. If the condition was EQ, 13700 we xor it with 1. This does not require an scc insn and is faster 13701 than an scc insn even if we have it. */ 13702 13703 if ((code == NE || code == EQ) 13704 && (integer_zerop (arg1) 13705 || integer_pow2p (arg1)) 13706 /* vector types are not handled here. */ 13707 && TREE_CODE (TREE_TYPE (arg1)) != VECTOR_TYPE 13708 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type))) 13709 { 13710 tree narg0 = arg0; 13711 wide_int nz = tree_nonzero_bits (narg0); 13712 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR); 13713 /* If the defining statement was (x & POW2), then use that instead of 13714 the non-zero bits. */ 13715 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt))) 13716 { 13717 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt)); 13718 narg0 = gimple_assign_rhs1 (srcstmt); 13719 } 13720 13721 if (wi::popcount (nz) == 1 13722 && (integer_zerop (arg1) 13723 || wi::to_wide (arg1) == nz)) 13724 { 13725 int bitnum = wi::exact_log2 (nz); 13726 enum tree_code tcode = EQ_EXPR; 13727 if ((code == NE) ^ !integer_zerop (arg1)) 13728 tcode = NE_EXPR; 13729 13730 type = lang_hooks.types.type_for_mode (mode, unsignedp); 13731 return expand_single_bit_test (loc, tcode, 13732 narg0, 13733 bitnum, type, target, mode); 13734 } 13735 } 13736 13737 13738 if (! get_subtarget (target) 13739 || GET_MODE (subtarget) != operand_mode) 13740 subtarget = 0; 13741 13742 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL); 13743 13744 /* For boolean vectors with less than mode precision 13745 make sure to fill padding with consistent values. */ 13746 if (VECTOR_BOOLEAN_TYPE_P (type) 13747 && SCALAR_INT_MODE_P (operand_mode) 13748 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nunits) 13749 && maybe_ne (GET_MODE_PRECISION (operand_mode), nunits)) 13750 { 13751 gcc_assert (code == EQ || code == NE); 13752 op0 = expand_binop (mode, and_optab, op0, 13753 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1), 13754 NULL_RTX, true, OPTAB_WIDEN); 13755 op1 = expand_binop (mode, and_optab, op1, 13756 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1), 13757 NULL_RTX, true, OPTAB_WIDEN); 13758 } 13759 13760 if (target == 0) 13761 target = gen_reg_rtx (mode); 13762 13763 /* Try a cstore if possible. */ 13764 return emit_store_flag_force (target, code, op0, op1, 13765 operand_mode, unsignedp, 13766 (TYPE_PRECISION (ops->type) == 1 13767 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1); 13768 } 13769 13770 /* Attempt to generate a casesi instruction. Returns true if successful, 13772 false otherwise (i.e. if there is no casesi instruction). 13773 13774 DEFAULT_PROBABILITY is the probability of jumping to the default 13775 label. */ 13776 bool 13777 try_casesi (tree index_type, tree index_expr, tree minval, tree range, 13778 rtx table_label, rtx default_label, rtx fallback_label, 13779 profile_probability default_probability) 13780 { 13781 class expand_operand ops[5]; 13782 scalar_int_mode index_mode = SImode; 13783 rtx op1, op2, index; 13784 13785 if (! targetm.have_casesi ()) 13786 return false; 13787 13788 /* The index must be some form of integer. Convert it to SImode. */ 13789 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type); 13790 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode)) 13791 { 13792 rtx rangertx = expand_normal (range); 13793 13794 /* We must handle the endpoints in the original mode. */ 13795 index_expr = build2 (MINUS_EXPR, index_type, 13796 index_expr, minval); 13797 minval = integer_zero_node; 13798 index = expand_normal (index_expr); 13799 if (default_label) 13800 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX, 13801 omode, 1, default_label, 13802 default_probability); 13803 /* Now we can safely truncate. */ 13804 index = convert_to_mode (index_mode, index, 0); 13805 } 13806 else 13807 { 13808 if (omode != index_mode) 13809 { 13810 index_type = lang_hooks.types.type_for_mode (index_mode, 0); 13811 index_expr = fold_convert (index_type, index_expr); 13812 } 13813 13814 index = expand_normal (index_expr); 13815 } 13816 13817 do_pending_stack_adjust (); 13818 13819 op1 = expand_normal (minval); 13820 op2 = expand_normal (range); 13821 13822 create_input_operand (&ops[0], index, index_mode); 13823 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval)); 13824 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range)); 13825 create_fixed_operand (&ops[3], table_label); 13826 create_fixed_operand (&ops[4], (default_label 13827 ? default_label 13828 : fallback_label)); 13829 expand_jump_insn (targetm.code_for_casesi, 5, ops); 13830 return true; 13831 } 13832 13833 /* Attempt to generate a tablejump instruction; same concept. */ 13834 /* Subroutine of the next function. 13835 13836 INDEX is the value being switched on, with the lowest value 13837 in the table already subtracted. 13838 MODE is its expected mode (needed if INDEX is constant). 13839 RANGE is the length of the jump table. 13840 TABLE_LABEL is a CODE_LABEL rtx for the table itself. 13841 13842 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the 13843 index value is out of range. 13844 DEFAULT_PROBABILITY is the probability of jumping to 13845 the default label. */ 13846 13847 static void 13848 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label, 13849 rtx default_label, profile_probability default_probability) 13850 { 13851 rtx temp, vector; 13852 13853 if (INTVAL (range) > cfun->cfg->max_jumptable_ents) 13854 cfun->cfg->max_jumptable_ents = INTVAL (range); 13855 13856 /* Do an unsigned comparison (in the proper mode) between the index 13857 expression and the value which represents the length of the range. 13858 Since we just finished subtracting the lower bound of the range 13859 from the index expression, this comparison allows us to simultaneously 13860 check that the original index expression value is both greater than 13861 or equal to the minimum value of the range and less than or equal to 13862 the maximum value of the range. */ 13863 13864 if (default_label) 13865 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1, 13866 default_label, default_probability); 13867 13868 /* If index is in range, it must fit in Pmode. 13869 Convert to Pmode so we can index with it. */ 13870 if (mode != Pmode) 13871 { 13872 unsigned int width; 13873 13874 /* We know the value of INDEX is between 0 and RANGE. If we have a 13875 sign-extended subreg, and RANGE does not have the sign bit set, then 13876 we have a value that is valid for both sign and zero extension. In 13877 this case, we get better code if we sign extend. */ 13878 if (GET_CODE (index) == SUBREG 13879 && SUBREG_PROMOTED_VAR_P (index) 13880 && SUBREG_PROMOTED_SIGNED_P (index) 13881 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode))) 13882 <= HOST_BITS_PER_WIDE_INT) 13883 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1)))) 13884 index = convert_to_mode (Pmode, index, 0); 13885 else 13886 index = convert_to_mode (Pmode, index, 1); 13887 } 13888 13889 /* Don't let a MEM slip through, because then INDEX that comes 13890 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address, 13891 and break_out_memory_refs will go to work on it and mess it up. */ 13892 #ifdef PIC_CASE_VECTOR_ADDRESS 13893 if (flag_pic && !REG_P (index)) 13894 index = copy_to_mode_reg (Pmode, index); 13895 #endif 13896 13897 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the 13898 GET_MODE_SIZE, because this indicates how large insns are. The other 13899 uses should all be Pmode, because they are addresses. This code 13900 could fail if addresses and insns are not the same size. */ 13901 index = simplify_gen_binary (MULT, Pmode, index, 13902 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE), 13903 Pmode)); 13904 index = simplify_gen_binary (PLUS, Pmode, index, 13905 gen_rtx_LABEL_REF (Pmode, table_label)); 13906 13907 #ifdef PIC_CASE_VECTOR_ADDRESS 13908 if (flag_pic) 13909 index = PIC_CASE_VECTOR_ADDRESS (index); 13910 else 13911 #endif 13912 index = memory_address (CASE_VECTOR_MODE, index); 13913 temp = gen_reg_rtx (CASE_VECTOR_MODE); 13914 vector = gen_const_mem (CASE_VECTOR_MODE, index); 13915 convert_move (temp, vector, 0); 13916 13917 emit_jump_insn (targetm.gen_tablejump (temp, table_label)); 13918 13919 /* If we are generating PIC code or if the table is PC-relative, the 13920 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */ 13921 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic) 13922 emit_barrier (); 13923 } 13924 13925 bool 13926 try_tablejump (tree index_type, tree index_expr, tree minval, tree range, 13927 rtx table_label, rtx default_label, 13928 profile_probability default_probability) 13929 { 13930 rtx index; 13931 13932 if (! targetm.have_tablejump ()) 13933 return false; 13934 13935 index_expr = fold_build2 (MINUS_EXPR, index_type, 13936 fold_convert (index_type, index_expr), 13937 fold_convert (index_type, minval)); 13938 index = expand_normal (index_expr); 13939 do_pending_stack_adjust (); 13940 13941 do_tablejump (index, TYPE_MODE (index_type), 13942 convert_modes (TYPE_MODE (index_type), 13943 TYPE_MODE (TREE_TYPE (range)), 13944 expand_normal (range), 13945 TYPE_UNSIGNED (TREE_TYPE (range))), 13946 table_label, default_label, default_probability); 13947 return true; 13948 } 13949 13950 /* Return a CONST_VECTOR rtx representing vector mask for 13951 a VECTOR_CST of booleans. */ 13952 static rtx 13953 const_vector_mask_from_tree (tree exp) 13954 { 13955 machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); 13956 machine_mode inner = GET_MODE_INNER (mode); 13957 13958 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp), 13959 VECTOR_CST_NELTS_PER_PATTERN (exp)); 13960 unsigned int count = builder.encoded_nelts (); 13961 for (unsigned int i = 0; i < count; ++i) 13962 { 13963 tree elt = VECTOR_CST_ELT (exp, i); 13964 gcc_assert (TREE_CODE (elt) == INTEGER_CST); 13965 if (integer_zerop (elt)) 13966 builder.quick_push (CONST0_RTX (inner)); 13967 else if (integer_onep (elt) 13968 || integer_minus_onep (elt)) 13969 builder.quick_push (CONSTM1_RTX (inner)); 13970 else 13971 gcc_unreachable (); 13972 } 13973 return builder.build (); 13974 } 13975 13976 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */ 13977 static rtx 13978 const_vector_from_tree (tree exp) 13979 { 13980 machine_mode mode = TYPE_MODE (TREE_TYPE (exp)); 13981 13982 if (initializer_zerop (exp)) 13983 return CONST0_RTX (mode); 13984 13985 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp))) 13986 return const_vector_mask_from_tree (exp); 13987 13988 machine_mode inner = GET_MODE_INNER (mode); 13989 13990 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp), 13991 VECTOR_CST_NELTS_PER_PATTERN (exp)); 13992 unsigned int count = builder.encoded_nelts (); 13993 for (unsigned int i = 0; i < count; ++i) 13994 { 13995 tree elt = VECTOR_CST_ELT (exp, i); 13996 if (TREE_CODE (elt) == REAL_CST) 13997 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt), 13998 inner)); 13999 else if (TREE_CODE (elt) == FIXED_CST) 14000 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt), 14001 inner)); 14002 else 14003 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt), 14004 inner)); 14005 } 14006 return builder.build (); 14007 } 14008 14009 /* Build a decl for a personality function given a language prefix. */ 14010 14011 tree 14012 build_personality_function (const char *lang) 14013 { 14014 const char *unwind_and_version; 14015 tree decl, type; 14016 char *name; 14017 14018 switch (targetm_common.except_unwind_info (&global_options)) 14019 { 14020 case UI_NONE: 14021 return NULL; 14022 case UI_SJLJ: 14023 unwind_and_version = "_sj0"; 14024 break; 14025 case UI_DWARF2: 14026 case UI_TARGET: 14027 unwind_and_version = "_v0"; 14028 break; 14029 case UI_SEH: 14030 unwind_and_version = "_seh0"; 14031 break; 14032 default: 14033 gcc_unreachable (); 14034 } 14035 14036 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL)); 14037 14038 type = build_function_type_list (unsigned_type_node, 14039 integer_type_node, integer_type_node, 14040 long_long_unsigned_type_node, 14041 ptr_type_node, ptr_type_node, NULL_TREE); 14042 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, 14043 get_identifier (name), type); 14044 DECL_ARTIFICIAL (decl) = 1; 14045 DECL_EXTERNAL (decl) = 1; 14046 TREE_PUBLIC (decl) = 1; 14047 14048 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with 14049 are the flags assigned by targetm.encode_section_info. */ 14050 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL); 14051 14052 return decl; 14053 } 14054 14055 /* Extracts the personality function of DECL and returns the corresponding 14056 libfunc. */ 14057 14058 rtx 14059 get_personality_function (tree decl) 14060 { 14061 tree personality = DECL_FUNCTION_PERSONALITY (decl); 14062 enum eh_personality_kind pk; 14063 14064 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl)); 14065 if (pk == eh_personality_none) 14066 return NULL; 14067 14068 if (!personality 14069 && pk == eh_personality_any) 14070 personality = lang_hooks.eh_personality (); 14071 14072 if (pk == eh_personality_lang) 14073 gcc_assert (personality != NULL_TREE); 14074 14075 return XEXP (DECL_RTL (personality), 0); 14076 } 14077 14078 /* Returns a tree for the size of EXP in bytes. */ 14079 14080 static tree 14081 tree_expr_size (const_tree exp) 14082 { 14083 if (DECL_P (exp) 14084 && DECL_SIZE_UNIT (exp) != 0) 14085 return DECL_SIZE_UNIT (exp); 14086 else 14087 return size_in_bytes (TREE_TYPE (exp)); 14088 } 14089 14090 /* Return an rtx for the size in bytes of the value of EXP. */ 14091 14092 rtx 14093 expr_size (tree exp) 14094 { 14095 tree size; 14096 14097 if (TREE_CODE (exp) == WITH_SIZE_EXPR) 14098 size = TREE_OPERAND (exp, 1); 14099 else 14100 { 14101 size = tree_expr_size (exp); 14102 gcc_assert (size); 14103 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp)); 14104 } 14105 14106 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL); 14107 } 14108 14109 /* Return a wide integer for the size in bytes of the value of EXP, or -1 14110 if the size can vary or is larger than an integer. */ 14111 14112 HOST_WIDE_INT 14113 int_expr_size (const_tree exp) 14114 { 14115 tree size; 14116 14117 if (TREE_CODE (exp) == WITH_SIZE_EXPR) 14118 size = TREE_OPERAND (exp, 1); 14119 else 14120 { 14121 size = tree_expr_size (exp); 14122 gcc_assert (size); 14123 } 14124 14125 if (size == 0 || !tree_fits_shwi_p (size)) 14126 return -1; 14127 14128 return tree_to_shwi (size); 14129 } 14130