Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / expr.cc
blobfff09dc995107db448370331e8b06d562a4b3542
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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"
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;
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);
84 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
85 static rtx_insn *compress_float_constant (rtx, rtx);
86 static rtx get_subtarget (rtx);
87 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
88 machine_mode, tree, alias_set_type, bool, bool);
90 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
92 static bool is_aligning_offset (const_tree, const_tree);
93 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
94 static rtx do_store_flag (sepops, rtx, machine_mode);
95 #ifdef PUSH_ROUNDING
96 static void emit_single_push_insn (machine_mode, rtx, tree);
97 #endif
98 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
99 profile_probability);
100 static rtx const_vector_from_tree (tree);
101 static tree tree_expr_size (const_tree);
102 static void convert_mode_scalar (rtx, rtx, int);
105 /* This is run to set up which modes can be used
106 directly in memory and to initialize the block move optab. It is run
107 at the beginning of compilation and when the target is reinitialized. */
109 void
110 init_expr_target (void)
112 rtx pat;
113 int num_clobbers;
114 rtx mem, mem1;
115 rtx reg;
117 /* Try indexing by frame ptr and try by stack ptr.
118 It is known that on the Convex the stack ptr isn't a valid index.
119 With luck, one or the other is valid on any machine. */
120 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
121 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
123 /* A scratch register we can modify in-place below to avoid
124 useless RTL allocations. */
125 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
127 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
128 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
129 PATTERN (insn) = pat;
131 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
132 mode = (machine_mode) ((int) mode + 1))
134 int regno;
136 direct_load[(int) mode] = direct_store[(int) mode] = 0;
137 PUT_MODE (mem, mode);
138 PUT_MODE (mem1, mode);
140 /* See if there is some register that can be used in this mode and
141 directly loaded or stored from memory. */
143 if (mode != VOIDmode && mode != BLKmode)
144 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
145 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
146 regno++)
148 if (!targetm.hard_regno_mode_ok (regno, mode))
149 continue;
151 set_mode_and_regno (reg, mode, regno);
153 SET_SRC (pat) = mem;
154 SET_DEST (pat) = reg;
155 if (recog (pat, insn, &num_clobbers) >= 0)
156 direct_load[(int) mode] = 1;
158 SET_SRC (pat) = mem1;
159 SET_DEST (pat) = reg;
160 if (recog (pat, insn, &num_clobbers) >= 0)
161 direct_load[(int) mode] = 1;
163 SET_SRC (pat) = reg;
164 SET_DEST (pat) = mem;
165 if (recog (pat, insn, &num_clobbers) >= 0)
166 direct_store[(int) mode] = 1;
168 SET_SRC (pat) = reg;
169 SET_DEST (pat) = mem1;
170 if (recog (pat, insn, &num_clobbers) >= 0)
171 direct_store[(int) mode] = 1;
175 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
177 opt_scalar_float_mode mode_iter;
178 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
180 scalar_float_mode mode = mode_iter.require ();
181 scalar_float_mode srcmode;
182 FOR_EACH_MODE_UNTIL (srcmode, mode)
184 enum insn_code ic;
186 ic = can_extend_p (mode, srcmode, 0);
187 if (ic == CODE_FOR_nothing)
188 continue;
190 PUT_MODE (mem, srcmode);
192 if (insn_operand_matches (ic, 1, mem))
193 float_extend_from_mem[mode][srcmode] = true;
198 /* This is run at the start of compiling a function. */
200 void
201 init_expr (void)
203 memset (&crtl->expr, 0, sizeof (crtl->expr));
206 /* Copy data from FROM to TO, where the machine modes are not the same.
207 Both modes may be integer, or both may be floating, or both may be
208 fixed-point.
209 UNSIGNEDP should be nonzero if FROM is an unsigned type.
210 This causes zero-extension instead of sign-extension. */
212 void
213 convert_move (rtx to, rtx from, int unsignedp)
215 machine_mode to_mode = GET_MODE (to);
216 machine_mode from_mode = GET_MODE (from);
218 gcc_assert (to_mode != BLKmode);
219 gcc_assert (from_mode != BLKmode);
221 /* If the source and destination are already the same, then there's
222 nothing to do. */
223 if (to == from)
224 return;
226 /* If FROM is a SUBREG that indicates that we have already done at least
227 the required extension, strip it. We don't handle such SUBREGs as
228 TO here. */
230 scalar_int_mode to_int_mode;
231 if (GET_CODE (from) == SUBREG
232 && SUBREG_PROMOTED_VAR_P (from)
233 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
234 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
235 >= GET_MODE_PRECISION (to_int_mode))
236 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
238 scalar_int_mode int_orig_mode;
239 scalar_int_mode int_inner_mode;
240 machine_mode orig_mode = GET_MODE (from);
242 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
243 from_mode = to_int_mode;
245 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
246 the original mode, but narrower than the inner mode. */
247 if (GET_CODE (from) == SUBREG
248 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
249 && GET_MODE_PRECISION (to_int_mode)
250 > GET_MODE_PRECISION (int_orig_mode)
251 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)),
252 &int_inner_mode)
253 && GET_MODE_PRECISION (int_inner_mode)
254 > GET_MODE_PRECISION (to_int_mode))
256 SUBREG_PROMOTED_VAR_P (from) = 1;
257 SUBREG_PROMOTED_SET (from, unsignedp);
261 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
263 if (to_mode == from_mode
264 || (from_mode == VOIDmode && CONSTANT_P (from)))
266 emit_move_insn (to, from);
267 return;
270 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
272 if (GET_MODE_UNIT_PRECISION (to_mode)
273 > GET_MODE_UNIT_PRECISION (from_mode))
275 optab op = unsignedp ? zext_optab : sext_optab;
276 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
277 if (icode != CODE_FOR_nothing)
279 emit_unop_insn (icode, to, from,
280 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
281 return;
285 if (GET_MODE_UNIT_PRECISION (to_mode)
286 < GET_MODE_UNIT_PRECISION (from_mode))
288 insn_code icode = convert_optab_handler (trunc_optab,
289 to_mode, from_mode);
290 if (icode != CODE_FOR_nothing)
292 emit_unop_insn (icode, to, from, TRUNCATE);
293 return;
297 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
298 GET_MODE_BITSIZE (to_mode)));
300 if (VECTOR_MODE_P (to_mode))
301 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
302 else
303 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
305 emit_move_insn (to, from);
306 return;
309 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
311 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
312 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
313 return;
316 convert_mode_scalar (to, from, unsignedp);
319 /* Like convert_move, but deals only with scalar modes. */
321 static void
322 convert_mode_scalar (rtx to, rtx from, int unsignedp)
324 /* Both modes should be scalar types. */
325 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
326 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
327 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
328 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
329 enum insn_code code;
330 rtx libcall;
332 gcc_assert (to_real == from_real);
334 /* rtx code for making an equivalent value. */
335 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
336 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
338 if (to_real)
340 rtx value;
341 rtx_insn *insns;
342 convert_optab tab;
344 gcc_assert ((GET_MODE_PRECISION (from_mode)
345 != GET_MODE_PRECISION (to_mode))
346 || (DECIMAL_FLOAT_MODE_P (from_mode)
347 != DECIMAL_FLOAT_MODE_P (to_mode))
348 || (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
349 && REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
350 || (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
351 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format));
353 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
354 /* Conversion between decimal float and binary float, same size. */
355 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
356 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
357 tab = sext_optab;
358 else
359 tab = trunc_optab;
361 /* Try converting directly if the insn is supported. */
363 code = convert_optab_handler (tab, to_mode, from_mode);
364 if (code != CODE_FOR_nothing)
366 emit_unop_insn (code, to, from,
367 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
368 return;
371 #ifdef HAVE_SFmode
372 if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
373 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
375 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode))
377 /* To cut down on libgcc size, implement
378 BFmode -> {DF,XF,TF}mode conversions by
379 BFmode -> SFmode -> {DF,XF,TF}mode conversions. */
380 rtx temp = gen_reg_rtx (SFmode);
381 convert_mode_scalar (temp, from, unsignedp);
382 convert_mode_scalar (to, temp, unsignedp);
383 return;
385 if (REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
387 /* Similarly, implement BFmode -> HFmode as
388 BFmode -> SFmode -> HFmode conversion where SFmode
389 has superset of BFmode values. We don't need
390 to handle sNaNs by raising exception and turning
391 into into qNaN though, as that can be done in the
392 SFmode -> HFmode conversion too. */
393 rtx temp = gen_reg_rtx (SFmode);
394 int save_flag_finite_math_only = flag_finite_math_only;
395 flag_finite_math_only = true;
396 convert_mode_scalar (temp, from, unsignedp);
397 flag_finite_math_only = save_flag_finite_math_only;
398 convert_mode_scalar (to, temp, unsignedp);
399 return;
401 if (to_mode == SFmode
402 && !HONOR_NANS (from_mode)
403 && !HONOR_NANS (to_mode)
404 && optimize_insn_for_speed_p ())
406 /* If we don't expect sNaNs, for BFmode -> SFmode we can just
407 shift the bits up. */
408 machine_mode fromi_mode, toi_mode;
409 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
410 0).exists (&fromi_mode)
411 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
412 0).exists (&toi_mode))
414 start_sequence ();
415 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
416 rtx tof = NULL_RTX;
417 if (fromi)
419 rtx toi;
420 if (GET_MODE (fromi) == VOIDmode)
421 toi = simplify_unary_operation (ZERO_EXTEND, toi_mode,
422 fromi, fromi_mode);
423 else
425 toi = gen_reg_rtx (toi_mode);
426 convert_mode_scalar (toi, fromi, 1);
429 = maybe_expand_shift (LSHIFT_EXPR, toi_mode, toi,
430 GET_MODE_PRECISION (to_mode)
431 - GET_MODE_PRECISION (from_mode),
432 NULL_RTX, 1);
433 if (toi)
435 tof = lowpart_subreg (to_mode, toi, toi_mode);
436 if (tof)
437 emit_move_insn (to, tof);
440 insns = get_insns ();
441 end_sequence ();
442 if (tof)
444 emit_insn (insns);
445 return;
450 if (REAL_MODE_FORMAT (from_mode) == &ieee_single_format
451 && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
452 && !HONOR_NANS (from_mode)
453 && !HONOR_NANS (to_mode)
454 && !flag_rounding_math
455 && optimize_insn_for_speed_p ())
457 /* If we don't expect qNaNs nor sNaNs and can assume rounding
458 to nearest, we can expand the conversion inline as
459 (fromi + 0x7fff + ((fromi >> 16) & 1)) >> 16. */
460 machine_mode fromi_mode, toi_mode;
461 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
462 0).exists (&fromi_mode)
463 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
464 0).exists (&toi_mode))
466 start_sequence ();
467 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
468 rtx tof = NULL_RTX;
471 if (!fromi)
472 break;
473 int shift = (GET_MODE_PRECISION (from_mode)
474 - GET_MODE_PRECISION (to_mode));
475 rtx temp1
476 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, fromi,
477 shift, NULL_RTX, 1);
478 if (!temp1)
479 break;
480 rtx temp2
481 = expand_binop (fromi_mode, and_optab, temp1, const1_rtx,
482 NULL_RTX, 1, OPTAB_DIRECT);
483 if (!temp2)
484 break;
485 rtx temp3
486 = expand_binop (fromi_mode, add_optab, fromi,
487 gen_int_mode ((HOST_WIDE_INT_1U
488 << (shift - 1)) - 1,
489 fromi_mode), NULL_RTX,
490 1, OPTAB_DIRECT);
491 if (!temp3)
492 break;
493 rtx temp4
494 = expand_binop (fromi_mode, add_optab, temp3, temp2,
495 NULL_RTX, 1, OPTAB_DIRECT);
496 if (!temp4)
497 break;
498 rtx temp5 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode,
499 temp4, shift, NULL_RTX, 1);
500 if (!temp5)
501 break;
502 rtx temp6 = lowpart_subreg (toi_mode, temp5, fromi_mode);
503 if (!temp6)
504 break;
505 tof = lowpart_subreg (to_mode, force_reg (toi_mode, temp6),
506 toi_mode);
507 if (tof)
508 emit_move_insn (to, tof);
510 while (0);
511 insns = get_insns ();
512 end_sequence ();
513 if (tof)
515 emit_insn (insns);
516 return;
520 #endif
522 /* Otherwise use a libcall. */
523 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
525 /* Is this conversion implemented yet? */
526 gcc_assert (libcall);
528 start_sequence ();
529 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
530 from, from_mode);
531 insns = get_insns ();
532 end_sequence ();
533 emit_libcall_block (insns, to, value,
534 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
535 from)
536 : gen_rtx_FLOAT_EXTEND (to_mode, from));
537 return;
540 /* Handle pointer conversion. */ /* SPEE 900220. */
541 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
543 convert_optab ctab;
545 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
546 ctab = trunc_optab;
547 else if (unsignedp)
548 ctab = zext_optab;
549 else
550 ctab = sext_optab;
552 if (convert_optab_handler (ctab, to_mode, from_mode)
553 != CODE_FOR_nothing)
555 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
556 to, from, UNKNOWN);
557 return;
561 /* Targets are expected to provide conversion insns between PxImode and
562 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
563 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
565 scalar_int_mode full_mode
566 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
568 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
569 != CODE_FOR_nothing);
571 if (full_mode != from_mode)
572 from = convert_to_mode (full_mode, from, unsignedp);
573 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
574 to, from, UNKNOWN);
575 return;
577 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
579 rtx new_from;
580 scalar_int_mode full_mode
581 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
582 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
583 enum insn_code icode;
585 icode = convert_optab_handler (ctab, full_mode, from_mode);
586 gcc_assert (icode != CODE_FOR_nothing);
588 if (to_mode == full_mode)
590 emit_unop_insn (icode, to, from, UNKNOWN);
591 return;
594 new_from = gen_reg_rtx (full_mode);
595 emit_unop_insn (icode, new_from, from, UNKNOWN);
597 /* else proceed to integer conversions below. */
598 from_mode = full_mode;
599 from = new_from;
602 /* Make sure both are fixed-point modes or both are not. */
603 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
604 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
605 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
607 /* If we widen from_mode to to_mode and they are in the same class,
608 we won't saturate the result.
609 Otherwise, always saturate the result to play safe. */
610 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
611 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
612 expand_fixed_convert (to, from, 0, 0);
613 else
614 expand_fixed_convert (to, from, 0, 1);
615 return;
618 /* Now both modes are integers. */
620 /* Handle expanding beyond a word. */
621 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
622 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
624 rtx_insn *insns;
625 rtx lowpart;
626 rtx fill_value;
627 rtx lowfrom;
628 int i;
629 scalar_mode lowpart_mode;
630 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
632 /* Try converting directly if the insn is supported. */
633 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
634 != CODE_FOR_nothing)
636 /* If FROM is a SUBREG, put it into a register. Do this
637 so that we always generate the same set of insns for
638 better cse'ing; if an intermediate assignment occurred,
639 we won't be doing the operation directly on the SUBREG. */
640 if (optimize > 0 && GET_CODE (from) == SUBREG)
641 from = force_reg (from_mode, from);
642 emit_unop_insn (code, to, from, equiv_code);
643 return;
645 /* Next, try converting via full word. */
646 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
647 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
648 != CODE_FOR_nothing))
650 rtx word_to = gen_reg_rtx (word_mode);
651 if (REG_P (to))
653 if (reg_overlap_mentioned_p (to, from))
654 from = force_reg (from_mode, from);
655 emit_clobber (to);
657 convert_move (word_to, from, unsignedp);
658 emit_unop_insn (code, to, word_to, equiv_code);
659 return;
662 /* No special multiword conversion insn; do it by hand. */
663 start_sequence ();
665 /* Since we will turn this into a no conflict block, we must ensure
666 the source does not overlap the target so force it into an isolated
667 register when maybe so. Likewise for any MEM input, since the
668 conversion sequence might require several references to it and we
669 must ensure we're getting the same value every time. */
671 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
672 from = force_reg (from_mode, from);
674 /* Get a copy of FROM widened to a word, if necessary. */
675 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
676 lowpart_mode = word_mode;
677 else
678 lowpart_mode = from_mode;
680 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
682 lowpart = gen_lowpart (lowpart_mode, to);
683 emit_move_insn (lowpart, lowfrom);
685 /* Compute the value to put in each remaining word. */
686 if (unsignedp)
687 fill_value = const0_rtx;
688 else
689 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
690 LT, lowfrom, const0_rtx,
691 lowpart_mode, 0, -1);
693 /* Fill the remaining words. */
694 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
696 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
697 rtx subword = operand_subword (to, index, 1, to_mode);
699 gcc_assert (subword);
701 if (fill_value != subword)
702 emit_move_insn (subword, fill_value);
705 insns = get_insns ();
706 end_sequence ();
708 emit_insn (insns);
709 return;
712 /* Truncating multi-word to a word or less. */
713 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
714 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
716 if (!((MEM_P (from)
717 && ! MEM_VOLATILE_P (from)
718 && direct_load[(int) to_mode]
719 && ! mode_dependent_address_p (XEXP (from, 0),
720 MEM_ADDR_SPACE (from)))
721 || REG_P (from)
722 || GET_CODE (from) == SUBREG))
723 from = force_reg (from_mode, from);
724 convert_move (to, gen_lowpart (word_mode, from), 0);
725 return;
728 /* Now follow all the conversions between integers
729 no more than a word long. */
731 /* For truncation, usually we can just refer to FROM in a narrower mode. */
732 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
733 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
735 if (!((MEM_P (from)
736 && ! MEM_VOLATILE_P (from)
737 && direct_load[(int) to_mode]
738 && ! mode_dependent_address_p (XEXP (from, 0),
739 MEM_ADDR_SPACE (from)))
740 || REG_P (from)
741 || GET_CODE (from) == SUBREG))
742 from = force_reg (from_mode, from);
743 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
744 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
745 from = copy_to_reg (from);
746 emit_move_insn (to, gen_lowpart (to_mode, from));
747 return;
750 /* Handle extension. */
751 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
753 /* Convert directly if that works. */
754 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
755 != CODE_FOR_nothing)
757 emit_unop_insn (code, to, from, equiv_code);
758 return;
760 else
762 rtx tmp;
763 int shift_amount;
765 /* Search for a mode to convert via. */
766 opt_scalar_mode intermediate_iter;
767 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
769 scalar_mode intermediate = intermediate_iter.require ();
770 if (((can_extend_p (to_mode, intermediate, unsignedp)
771 != CODE_FOR_nothing)
772 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
773 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
774 intermediate)))
775 && (can_extend_p (intermediate, from_mode, unsignedp)
776 != CODE_FOR_nothing))
778 convert_move (to, convert_to_mode (intermediate, from,
779 unsignedp), unsignedp);
780 return;
784 /* No suitable intermediate mode.
785 Generate what we need with shifts. */
786 shift_amount = (GET_MODE_PRECISION (to_mode)
787 - GET_MODE_PRECISION (from_mode));
788 from = gen_lowpart (to_mode, force_reg (from_mode, from));
789 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
790 to, unsignedp);
791 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
792 to, unsignedp);
793 if (tmp != to)
794 emit_move_insn (to, tmp);
795 return;
799 /* Support special truncate insns for certain modes. */
800 if (convert_optab_handler (trunc_optab, to_mode,
801 from_mode) != CODE_FOR_nothing)
803 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
804 to, from, UNKNOWN);
805 return;
808 /* Handle truncation of volatile memrefs, and so on;
809 the things that couldn't be truncated directly,
810 and for which there was no special instruction.
812 ??? Code above formerly short-circuited this, for most integer
813 mode pairs, with a force_reg in from_mode followed by a recursive
814 call to this routine. Appears always to have been wrong. */
815 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
817 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
818 emit_move_insn (to, temp);
819 return;
822 /* Mode combination is not recognized. */
823 gcc_unreachable ();
826 /* Return an rtx for a value that would result
827 from converting X to mode MODE.
828 Both X and MODE may be floating, or both integer.
829 UNSIGNEDP is nonzero if X is an unsigned value.
830 This can be done by referring to a part of X in place
831 or by copying to a new temporary with conversion. */
834 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
836 return convert_modes (mode, VOIDmode, x, unsignedp);
839 /* Return an rtx for a value that would result
840 from converting X from mode OLDMODE to mode MODE.
841 Both modes may be floating, or both integer.
842 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.
847 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
850 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
852 rtx temp;
853 scalar_int_mode int_mode;
855 /* If FROM is a SUBREG that indicates that we have already done at least
856 the required extension, strip it. */
858 if (GET_CODE (x) == SUBREG
859 && SUBREG_PROMOTED_VAR_P (x)
860 && is_a <scalar_int_mode> (mode, &int_mode)
861 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
862 >= GET_MODE_PRECISION (int_mode))
863 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
865 scalar_int_mode int_orig_mode;
866 scalar_int_mode int_inner_mode;
867 machine_mode orig_mode = GET_MODE (x);
868 x = gen_lowpart (int_mode, SUBREG_REG (x));
870 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
871 the original mode, but narrower than the inner mode. */
872 if (GET_CODE (x) == SUBREG
873 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
874 && GET_MODE_PRECISION (int_mode)
875 > GET_MODE_PRECISION (int_orig_mode)
876 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
877 &int_inner_mode)
878 && GET_MODE_PRECISION (int_inner_mode)
879 > GET_MODE_PRECISION (int_mode))
881 SUBREG_PROMOTED_VAR_P (x) = 1;
882 SUBREG_PROMOTED_SET (x, unsignedp);
886 if (GET_MODE (x) != VOIDmode)
887 oldmode = GET_MODE (x);
889 if (mode == oldmode)
890 return x;
892 if (CONST_SCALAR_INT_P (x)
893 && is_a <scalar_int_mode> (mode, &int_mode))
895 /* If the caller did not tell us the old mode, then there is not
896 much to do with respect to canonicalization. We have to
897 assume that all the bits are significant. */
898 if (!is_a <scalar_int_mode> (oldmode))
899 oldmode = MAX_MODE_INT;
900 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
901 GET_MODE_PRECISION (int_mode),
902 unsignedp ? UNSIGNED : SIGNED);
903 return immed_wide_int_const (w, int_mode);
906 /* We can do this with a gen_lowpart if both desired and current modes
907 are integer, and this is either a constant integer, a register, or a
908 non-volatile MEM. */
909 scalar_int_mode int_oldmode;
910 if (is_int_mode (mode, &int_mode)
911 && is_int_mode (oldmode, &int_oldmode)
912 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
913 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
914 || CONST_POLY_INT_P (x)
915 || (REG_P (x)
916 && (!HARD_REGISTER_P (x)
917 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
918 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
919 return gen_lowpart (int_mode, x);
921 /* Converting from integer constant into mode is always equivalent to an
922 subreg operation. */
923 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
925 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
926 GET_MODE_BITSIZE (oldmode)));
927 return simplify_gen_subreg (mode, x, oldmode, 0);
930 temp = gen_reg_rtx (mode);
931 convert_move (temp, x, unsignedp);
932 return temp;
935 /* Variant of convert_modes for ABI parameter passing/return.
936 Return an rtx for a value that would result from converting X from
937 a floating point mode FMODE to wider integer mode MODE. */
940 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x)
942 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode));
943 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require ();
944 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
945 return convert_modes (mode, tmp_mode, tmp, 1);
948 /* Variant of convert_modes for ABI parameter passing/return.
949 Return an rtx for a value that would result from converting X from
950 an integer mode IMODE to a narrower floating point mode MODE. */
953 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x)
955 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode));
956 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require ();
957 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
958 return gen_lowpart_SUBREG (mode, tmp);
961 /* Return the largest alignment we can use for doing a move (or store)
962 of MAX_PIECES. ALIGN is the largest alignment we could use. */
964 static unsigned int
965 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
967 scalar_int_mode tmode
968 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
970 if (align >= GET_MODE_ALIGNMENT (tmode))
971 align = GET_MODE_ALIGNMENT (tmode);
972 else
974 scalar_int_mode xmode = NARROWEST_INT_MODE;
975 opt_scalar_int_mode mode_iter;
976 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
978 tmode = mode_iter.require ();
979 if (GET_MODE_SIZE (tmode) > max_pieces
980 || targetm.slow_unaligned_access (tmode, align))
981 break;
982 xmode = tmode;
985 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
988 return align;
991 /* Return the widest QI vector, if QI_MODE is true, or integer mode
992 that is narrower than SIZE bytes. */
994 static fixed_size_mode
995 widest_fixed_size_mode_for_size (unsigned int size, bool qi_vector)
997 fixed_size_mode result = NARROWEST_INT_MODE;
999 gcc_checking_assert (size > 1);
1001 /* Use QI vector only if size is wider than a WORD. */
1002 if (qi_vector && size > UNITS_PER_WORD)
1004 machine_mode mode;
1005 fixed_size_mode candidate;
1006 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1007 if (is_a<fixed_size_mode> (mode, &candidate)
1008 && GET_MODE_INNER (candidate) == QImode)
1010 if (GET_MODE_SIZE (candidate) >= size)
1011 break;
1012 if (optab_handler (vec_duplicate_optab, candidate)
1013 != CODE_FOR_nothing)
1014 result = candidate;
1017 if (result != NARROWEST_INT_MODE)
1018 return result;
1021 opt_scalar_int_mode tmode;
1022 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
1023 if (GET_MODE_SIZE (tmode.require ()) < size)
1024 result = tmode.require ();
1026 return result;
1029 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
1030 and should be performed piecewise. */
1032 static bool
1033 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
1034 enum by_pieces_operation op)
1036 return targetm.use_by_pieces_infrastructure_p (len, align, op,
1037 optimize_insn_for_speed_p ());
1040 /* Determine whether the LEN bytes can be moved by using several move
1041 instructions. Return nonzero if a call to move_by_pieces should
1042 succeed. */
1044 bool
1045 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
1047 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
1050 /* Return number of insns required to perform operation OP by pieces
1051 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
1053 unsigned HOST_WIDE_INT
1054 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1055 unsigned int max_size, by_pieces_operation op)
1057 unsigned HOST_WIDE_INT n_insns = 0;
1058 fixed_size_mode mode;
1060 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
1062 /* NB: Round up L and ALIGN to the widest integer mode for
1063 MAX_SIZE. */
1064 mode = widest_fixed_size_mode_for_size (max_size,
1065 op == SET_BY_PIECES);
1066 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
1068 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1069 if (up > l)
1070 l = up;
1071 align = GET_MODE_ALIGNMENT (mode);
1075 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1077 while (max_size > 1 && l > 0)
1079 mode = widest_fixed_size_mode_for_size (max_size,
1080 op == SET_BY_PIECES);
1081 enum insn_code icode;
1083 unsigned int modesize = GET_MODE_SIZE (mode);
1085 icode = optab_handler (mov_optab, mode);
1086 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1088 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1089 l %= modesize;
1090 switch (op)
1092 default:
1093 n_insns += n_pieces;
1094 break;
1096 case COMPARE_BY_PIECES:
1097 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1098 int batch_ops = 4 * batch - 1;
1099 unsigned HOST_WIDE_INT full = n_pieces / batch;
1100 n_insns += full * batch_ops;
1101 if (n_pieces % batch != 0)
1102 n_insns++;
1103 break;
1107 max_size = modesize;
1110 gcc_assert (!l);
1111 return n_insns;
1114 /* Used when performing piecewise block operations, holds information
1115 about one of the memory objects involved. The member functions
1116 can be used to generate code for loading from the object and
1117 updating the address when iterating. */
1119 class pieces_addr
1121 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1122 stack pushes. */
1123 rtx m_obj;
1124 /* The address of the object. Can differ from that seen in the
1125 MEM rtx if we copied the address to a register. */
1126 rtx m_addr;
1127 /* Nonzero if the address on the object has an autoincrement already,
1128 signifies whether that was an increment or decrement. */
1129 signed char m_addr_inc;
1130 /* Nonzero if we intend to use autoinc without the address already
1131 having autoinc form. We will insert add insns around each memory
1132 reference, expecting later passes to form autoinc addressing modes.
1133 The only supported options are predecrement and postincrement. */
1134 signed char m_explicit_inc;
1135 /* True if we have either of the two possible cases of using
1136 autoincrement. */
1137 bool m_auto;
1138 /* True if this is an address to be used for load operations rather
1139 than stores. */
1140 bool m_is_load;
1142 /* Optionally, a function to obtain constants for any given offset into
1143 the objects, and data associated with it. */
1144 by_pieces_constfn m_constfn;
1145 void *m_cfndata;
1146 public:
1147 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1148 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1149 void increment_address (HOST_WIDE_INT);
1150 void maybe_predec (HOST_WIDE_INT);
1151 void maybe_postinc (HOST_WIDE_INT);
1152 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1153 int get_addr_inc ()
1155 return m_addr_inc;
1159 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1160 true if the operation to be performed on this object is a load
1161 rather than a store. For stores, OBJ can be NULL, in which case we
1162 assume the operation is a stack push. For loads, the optional
1163 CONSTFN and its associated CFNDATA can be used in place of the
1164 memory load. */
1166 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1167 void *cfndata)
1168 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1170 m_addr_inc = 0;
1171 m_auto = false;
1172 if (obj)
1174 rtx addr = XEXP (obj, 0);
1175 rtx_code code = GET_CODE (addr);
1176 m_addr = addr;
1177 bool dec = code == PRE_DEC || code == POST_DEC;
1178 bool inc = code == PRE_INC || code == POST_INC;
1179 m_auto = inc || dec;
1180 if (m_auto)
1181 m_addr_inc = dec ? -1 : 1;
1183 /* While we have always looked for these codes here, the code
1184 implementing the memory operation has never handled them.
1185 Support could be added later if necessary or beneficial. */
1186 gcc_assert (code != PRE_INC && code != POST_DEC);
1188 else
1190 m_addr = NULL_RTX;
1191 if (!is_load)
1193 m_auto = true;
1194 if (STACK_GROWS_DOWNWARD)
1195 m_addr_inc = -1;
1196 else
1197 m_addr_inc = 1;
1199 else
1200 gcc_assert (constfn != NULL);
1202 m_explicit_inc = 0;
1203 if (constfn)
1204 gcc_assert (is_load);
1207 /* Decide whether to use autoinc for an address involved in a memory op.
1208 MODE is the mode of the accesses, REVERSE is true if we've decided to
1209 perform the operation starting from the end, and LEN is the length of
1210 the operation. Don't override an earlier decision to set m_auto. */
1212 void
1213 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1214 HOST_WIDE_INT len)
1216 if (m_auto || m_obj == NULL_RTX)
1217 return;
1219 bool use_predec = (m_is_load
1220 ? USE_LOAD_PRE_DECREMENT (mode)
1221 : USE_STORE_PRE_DECREMENT (mode));
1222 bool use_postinc = (m_is_load
1223 ? USE_LOAD_POST_INCREMENT (mode)
1224 : USE_STORE_POST_INCREMENT (mode));
1225 machine_mode addr_mode = get_address_mode (m_obj);
1227 if (use_predec && reverse)
1229 m_addr = copy_to_mode_reg (addr_mode,
1230 plus_constant (addr_mode,
1231 m_addr, len));
1232 m_auto = true;
1233 m_explicit_inc = -1;
1235 else if (use_postinc && !reverse)
1237 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1238 m_auto = true;
1239 m_explicit_inc = 1;
1241 else if (CONSTANT_P (m_addr))
1242 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1245 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1246 are using autoincrement for this address, we don't add the offset,
1247 but we still modify the MEM's properties. */
1250 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1251 by_pieces_prev *prev)
1253 if (m_constfn)
1254 /* Pass the previous data to m_constfn. */
1255 return m_constfn (m_cfndata, prev, offset, mode);
1256 if (m_obj == NULL_RTX)
1257 return NULL_RTX;
1258 if (m_auto)
1259 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1260 else
1261 return adjust_address (m_obj, mode, offset);
1264 /* Emit an add instruction to increment the address by SIZE. */
1266 void
1267 pieces_addr::increment_address (HOST_WIDE_INT size)
1269 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1270 emit_insn (gen_add2_insn (m_addr, amount));
1273 /* If we are supposed to decrement the address after each access, emit code
1274 to do so now. Increment by SIZE (which has should have the correct sign
1275 already). */
1277 void
1278 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1280 if (m_explicit_inc >= 0)
1281 return;
1282 gcc_assert (HAVE_PRE_DECREMENT);
1283 increment_address (size);
1286 /* If we are supposed to decrement the address after each access, emit code
1287 to do so now. Increment by SIZE. */
1289 void
1290 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1292 if (m_explicit_inc <= 0)
1293 return;
1294 gcc_assert (HAVE_POST_INCREMENT);
1295 increment_address (size);
1298 /* This structure is used by do_op_by_pieces to describe the operation
1299 to be performed. */
1301 class op_by_pieces_d
1303 private:
1304 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1305 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1307 protected:
1308 pieces_addr m_to, m_from;
1309 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1310 use it to check the valid mode size. */
1311 const unsigned HOST_WIDE_INT m_len;
1312 HOST_WIDE_INT m_offset;
1313 unsigned int m_align;
1314 unsigned int m_max_size;
1315 bool m_reverse;
1316 /* True if this is a stack push. */
1317 bool m_push;
1318 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1319 bool m_overlap_op_by_pieces;
1320 /* True if QI vector mode can be used. */
1321 bool m_qi_vector_mode;
1323 /* Virtual functions, overriden by derived classes for the specific
1324 operation. */
1325 virtual void generate (rtx, rtx, machine_mode) = 0;
1326 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1327 virtual void finish_mode (machine_mode)
1331 public:
1332 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1333 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1334 bool = false);
1335 void run ();
1338 /* The constructor for an op_by_pieces_d structure. We require two
1339 objects named TO and FROM, which are identified as loads or stores
1340 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1341 and its associated FROM_CFN_DATA can be used to replace loads with
1342 constant values. MAX_PIECES describes the maximum number of bytes
1343 at a time which can be moved efficiently. LEN describes the length
1344 of the operation. */
1346 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1347 bool to_load, rtx from, bool from_load,
1348 by_pieces_constfn from_cfn,
1349 void *from_cfn_data,
1350 unsigned HOST_WIDE_INT len,
1351 unsigned int align, bool push,
1352 bool qi_vector_mode)
1353 : m_to (to, to_load, NULL, NULL),
1354 m_from (from, from_load, from_cfn, from_cfn_data),
1355 m_len (len), m_max_size (max_pieces + 1),
1356 m_push (push), m_qi_vector_mode (qi_vector_mode)
1358 int toi = m_to.get_addr_inc ();
1359 int fromi = m_from.get_addr_inc ();
1360 if (toi >= 0 && fromi >= 0)
1361 m_reverse = false;
1362 else if (toi <= 0 && fromi <= 0)
1363 m_reverse = true;
1364 else
1365 gcc_unreachable ();
1367 m_offset = m_reverse ? len : 0;
1368 align = MIN (to ? MEM_ALIGN (to) : align,
1369 from ? MEM_ALIGN (from) : align);
1371 /* If copying requires more than two move insns,
1372 copy addresses to registers (to make displacements shorter)
1373 and use post-increment if available. */
1374 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1376 /* Find the mode of the largest comparison. */
1377 fixed_size_mode mode
1378 = widest_fixed_size_mode_for_size (m_max_size,
1379 m_qi_vector_mode);
1381 m_from.decide_autoinc (mode, m_reverse, len);
1382 m_to.decide_autoinc (mode, m_reverse, len);
1385 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1386 m_align = align;
1388 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1391 /* This function returns the largest usable integer mode for LEN bytes
1392 whose size is no bigger than size of MODE. */
1394 fixed_size_mode
1395 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1397 unsigned int size;
1400 size = GET_MODE_SIZE (mode);
1401 if (len >= size && prepare_mode (mode, m_align))
1402 break;
1403 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1404 mode = widest_fixed_size_mode_for_size (size, m_qi_vector_mode);
1406 while (1);
1407 return mode;
1410 /* Return the smallest integer or QI vector mode that is not narrower
1411 than SIZE bytes. */
1413 fixed_size_mode
1414 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1416 /* Use QI vector only for > size of WORD. */
1417 if (m_qi_vector_mode && size > UNITS_PER_WORD)
1419 machine_mode mode;
1420 fixed_size_mode candidate;
1421 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1422 if (is_a<fixed_size_mode> (mode, &candidate)
1423 && GET_MODE_INNER (candidate) == QImode)
1425 /* Don't return a mode wider than M_LEN. */
1426 if (GET_MODE_SIZE (candidate) > m_len)
1427 break;
1429 if (GET_MODE_SIZE (candidate) >= size
1430 && (optab_handler (vec_duplicate_optab, candidate)
1431 != CODE_FOR_nothing))
1432 return candidate;
1436 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1439 /* This function contains the main loop used for expanding a block
1440 operation. First move what we can in the largest integer mode,
1441 then go to successively smaller modes. For every access, call
1442 GENFUN with the two operands and the EXTRA_DATA. */
1444 void
1445 op_by_pieces_d::run ()
1447 if (m_len == 0)
1448 return;
1450 unsigned HOST_WIDE_INT length = m_len;
1452 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1453 fixed_size_mode mode
1454 = widest_fixed_size_mode_for_size (m_max_size, m_qi_vector_mode);
1455 mode = get_usable_mode (mode, length);
1457 by_pieces_prev to_prev = { nullptr, mode };
1458 by_pieces_prev from_prev = { nullptr, mode };
1462 unsigned int size = GET_MODE_SIZE (mode);
1463 rtx to1 = NULL_RTX, from1;
1465 while (length >= size)
1467 if (m_reverse)
1468 m_offset -= size;
1470 to1 = m_to.adjust (mode, m_offset, &to_prev);
1471 to_prev.data = to1;
1472 to_prev.mode = mode;
1473 from1 = m_from.adjust (mode, m_offset, &from_prev);
1474 from_prev.data = from1;
1475 from_prev.mode = mode;
1477 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1478 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1480 generate (to1, from1, mode);
1482 m_to.maybe_postinc (size);
1483 m_from.maybe_postinc (size);
1485 if (!m_reverse)
1486 m_offset += size;
1488 length -= size;
1491 finish_mode (mode);
1493 if (length == 0)
1494 return;
1496 if (!m_push && m_overlap_op_by_pieces)
1498 /* NB: Generate overlapping operations if it is not a stack
1499 push since stack push must not overlap. Get the smallest
1500 fixed size mode for M_LEN bytes. */
1501 mode = smallest_fixed_size_mode_for_size (length);
1502 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1503 int gap = GET_MODE_SIZE (mode) - length;
1504 if (gap > 0)
1506 /* If size of MODE > M_LEN, generate the last operation
1507 in MODE for the remaining bytes with ovelapping memory
1508 from the previois operation. */
1509 if (m_reverse)
1510 m_offset += gap;
1511 else
1512 m_offset -= gap;
1513 length += gap;
1516 else
1518 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1519 mode = widest_fixed_size_mode_for_size (size,
1520 m_qi_vector_mode);
1521 mode = get_usable_mode (mode, length);
1524 while (1);
1527 /* Derived class from op_by_pieces_d, providing support for block move
1528 operations. */
1530 #ifdef PUSH_ROUNDING
1531 #define PUSHG_P(to) ((to) == nullptr)
1532 #else
1533 #define PUSHG_P(to) false
1534 #endif
1536 class move_by_pieces_d : public op_by_pieces_d
1538 insn_gen_fn m_gen_fun;
1539 void generate (rtx, rtx, machine_mode) final override;
1540 bool prepare_mode (machine_mode, unsigned int) final override;
1542 public:
1543 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1544 unsigned int align)
1545 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1546 NULL, len, align, PUSHG_P (to))
1549 rtx finish_retmode (memop_ret);
1552 /* Return true if MODE can be used for a set of copies, given an
1553 alignment ALIGN. Prepare whatever data is necessary for later
1554 calls to generate. */
1556 bool
1557 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1559 insn_code icode = optab_handler (mov_optab, mode);
1560 m_gen_fun = GEN_FCN (icode);
1561 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1564 /* A callback used when iterating for a compare_by_pieces_operation.
1565 OP0 and OP1 are the values that have been loaded and should be
1566 compared in MODE. If OP0 is NULL, this means we should generate a
1567 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1568 gen function that should be used to generate the mode. */
1570 void
1571 move_by_pieces_d::generate (rtx op0, rtx op1,
1572 machine_mode mode ATTRIBUTE_UNUSED)
1574 #ifdef PUSH_ROUNDING
1575 if (op0 == NULL_RTX)
1577 emit_single_push_insn (mode, op1, NULL);
1578 return;
1580 #endif
1581 emit_insn (m_gen_fun (op0, op1));
1584 /* Perform the final adjustment at the end of a string to obtain the
1585 correct return value for the block operation.
1586 Return value is based on RETMODE argument. */
1589 move_by_pieces_d::finish_retmode (memop_ret retmode)
1591 gcc_assert (!m_reverse);
1592 if (retmode == RETURN_END_MINUS_ONE)
1594 m_to.maybe_postinc (-1);
1595 --m_offset;
1597 return m_to.adjust (QImode, m_offset);
1600 /* Generate several move instructions to copy LEN bytes from block FROM to
1601 block TO. (These are MEM rtx's with BLKmode).
1603 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1604 used to push FROM to the stack.
1606 ALIGN is maximum stack alignment we can assume.
1608 Return value is based on RETMODE argument. */
1611 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1612 unsigned int align, memop_ret retmode)
1614 #ifndef PUSH_ROUNDING
1615 if (to == NULL)
1616 gcc_unreachable ();
1617 #endif
1619 move_by_pieces_d data (to, from, len, align);
1621 data.run ();
1623 if (retmode != RETURN_BEGIN)
1624 return data.finish_retmode (retmode);
1625 else
1626 return to;
1629 /* Derived class from op_by_pieces_d, providing support for block move
1630 operations. */
1632 class store_by_pieces_d : public op_by_pieces_d
1634 insn_gen_fn m_gen_fun;
1635 void generate (rtx, rtx, machine_mode) final override;
1636 bool prepare_mode (machine_mode, unsigned int) final override;
1638 public:
1639 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1640 unsigned HOST_WIDE_INT len, unsigned int align,
1641 bool qi_vector_mode)
1642 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1643 cfn_data, len, align, false, qi_vector_mode)
1646 rtx finish_retmode (memop_ret);
1649 /* Return true if MODE can be used for a set of stores, given an
1650 alignment ALIGN. Prepare whatever data is necessary for later
1651 calls to generate. */
1653 bool
1654 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1656 insn_code icode = optab_handler (mov_optab, mode);
1657 m_gen_fun = GEN_FCN (icode);
1658 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1661 /* A callback used when iterating for a store_by_pieces_operation.
1662 OP0 and OP1 are the values that have been loaded and should be
1663 compared in MODE. If OP0 is NULL, this means we should generate a
1664 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1665 gen function that should be used to generate the mode. */
1667 void
1668 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1670 emit_insn (m_gen_fun (op0, op1));
1673 /* Perform the final adjustment at the end of a string to obtain the
1674 correct return value for the block operation.
1675 Return value is based on RETMODE argument. */
1678 store_by_pieces_d::finish_retmode (memop_ret retmode)
1680 gcc_assert (!m_reverse);
1681 if (retmode == RETURN_END_MINUS_ONE)
1683 m_to.maybe_postinc (-1);
1684 --m_offset;
1686 return m_to.adjust (QImode, m_offset);
1689 /* Determine whether the LEN bytes generated by CONSTFUN can be
1690 stored to memory using several move instructions. CONSTFUNDATA is
1691 a pointer which will be passed as argument in every CONSTFUN call.
1692 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1693 a memset operation and false if it's a copy of a constant string.
1694 Return true if a call to store_by_pieces should succeed. */
1696 bool
1697 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1698 by_pieces_constfn constfun,
1699 void *constfundata, unsigned int align, bool memsetp)
1701 unsigned HOST_WIDE_INT l;
1702 unsigned int max_size;
1703 HOST_WIDE_INT offset = 0;
1704 enum insn_code icode;
1705 int reverse;
1706 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1707 rtx cst ATTRIBUTE_UNUSED;
1709 if (len == 0)
1710 return true;
1712 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1713 memsetp
1714 ? SET_BY_PIECES
1715 : STORE_BY_PIECES,
1716 optimize_insn_for_speed_p ()))
1717 return false;
1719 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1721 /* We would first store what we can in the largest integer mode, then go to
1722 successively smaller modes. */
1724 for (reverse = 0;
1725 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1726 reverse++)
1728 l = len;
1729 max_size = STORE_MAX_PIECES + 1;
1730 while (max_size > 1 && l > 0)
1732 fixed_size_mode mode
1733 = widest_fixed_size_mode_for_size (max_size, memsetp);
1735 icode = optab_handler (mov_optab, mode);
1736 if (icode != CODE_FOR_nothing
1737 && align >= GET_MODE_ALIGNMENT (mode))
1739 unsigned int size = GET_MODE_SIZE (mode);
1741 while (l >= size)
1743 if (reverse)
1744 offset -= size;
1746 cst = (*constfun) (constfundata, nullptr, offset, mode);
1747 /* All CONST_VECTORs can be loaded for memset since
1748 vec_duplicate_optab is a precondition to pick a
1749 vector mode for the memset expander. */
1750 if (!((memsetp && VECTOR_MODE_P (mode))
1751 || targetm.legitimate_constant_p (mode, cst)))
1752 return false;
1754 if (!reverse)
1755 offset += size;
1757 l -= size;
1761 max_size = GET_MODE_SIZE (mode);
1764 /* The code above should have handled everything. */
1765 gcc_assert (!l);
1768 return true;
1771 /* Generate several move instructions to store LEN bytes generated by
1772 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1773 pointer which will be passed as argument in every CONSTFUN call.
1774 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1775 a memset operation and false if it's a copy of a constant string.
1776 Return value is based on RETMODE argument. */
1779 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1780 by_pieces_constfn constfun,
1781 void *constfundata, unsigned int align, bool memsetp,
1782 memop_ret retmode)
1784 if (len == 0)
1786 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1787 return to;
1790 gcc_assert (targetm.use_by_pieces_infrastructure_p
1791 (len, align,
1792 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1793 optimize_insn_for_speed_p ()));
1795 store_by_pieces_d data (to, constfun, constfundata, len, align,
1796 memsetp);
1797 data.run ();
1799 if (retmode != RETURN_BEGIN)
1800 return data.finish_retmode (retmode);
1801 else
1802 return to;
1805 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1806 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1808 static void
1809 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1811 if (len == 0)
1812 return;
1814 /* Use builtin_memset_read_str to support vector mode broadcast. */
1815 char c = 0;
1816 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1817 true);
1818 data.run ();
1821 /* Context used by compare_by_pieces_genfn. It stores the fail label
1822 to jump to in case of miscomparison, and for branch ratios greater than 1,
1823 it stores an accumulator and the current and maximum counts before
1824 emitting another branch. */
1826 class compare_by_pieces_d : public op_by_pieces_d
1828 rtx_code_label *m_fail_label;
1829 rtx m_accumulator;
1830 int m_count, m_batch;
1832 void generate (rtx, rtx, machine_mode) final override;
1833 bool prepare_mode (machine_mode, unsigned int) final override;
1834 void finish_mode (machine_mode) final override;
1835 public:
1836 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1837 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1838 rtx_code_label *fail_label)
1839 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1840 op1_cfn_data, len, align, false)
1842 m_fail_label = fail_label;
1846 /* A callback used when iterating for a compare_by_pieces_operation.
1847 OP0 and OP1 are the values that have been loaded and should be
1848 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1849 context structure. */
1851 void
1852 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1854 if (m_batch > 1)
1856 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1857 true, OPTAB_LIB_WIDEN);
1858 if (m_count != 0)
1859 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1860 true, OPTAB_LIB_WIDEN);
1861 m_accumulator = temp;
1863 if (++m_count < m_batch)
1864 return;
1866 m_count = 0;
1867 op0 = m_accumulator;
1868 op1 = const0_rtx;
1869 m_accumulator = NULL_RTX;
1871 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1872 m_fail_label, profile_probability::uninitialized ());
1875 /* Return true if MODE can be used for a set of moves and comparisons,
1876 given an alignment ALIGN. Prepare whatever data is necessary for
1877 later calls to generate. */
1879 bool
1880 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1882 insn_code icode = optab_handler (mov_optab, mode);
1883 if (icode == CODE_FOR_nothing
1884 || align < GET_MODE_ALIGNMENT (mode)
1885 || !can_compare_p (EQ, mode, ccp_jump))
1886 return false;
1887 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1888 if (m_batch < 0)
1889 return false;
1890 m_accumulator = NULL_RTX;
1891 m_count = 0;
1892 return true;
1895 /* Called after expanding a series of comparisons in MODE. If we have
1896 accumulated results for which we haven't emitted a branch yet, do
1897 so now. */
1899 void
1900 compare_by_pieces_d::finish_mode (machine_mode mode)
1902 if (m_accumulator != NULL_RTX)
1903 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1904 NULL_RTX, NULL, m_fail_label,
1905 profile_probability::uninitialized ());
1908 /* Generate several move instructions to compare LEN bytes from blocks
1909 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1911 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1912 used to push FROM to the stack.
1914 ALIGN is maximum stack alignment we can assume.
1916 Optionally, the caller can pass a constfn and associated data in A1_CFN
1917 and A1_CFN_DATA. describing that the second operand being compared is a
1918 known constant and how to obtain its data. */
1920 static rtx
1921 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1922 rtx target, unsigned int align,
1923 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1925 rtx_code_label *fail_label = gen_label_rtx ();
1926 rtx_code_label *end_label = gen_label_rtx ();
1928 if (target == NULL_RTX
1929 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1930 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1932 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1933 fail_label);
1935 data.run ();
1937 emit_move_insn (target, const0_rtx);
1938 emit_jump (end_label);
1939 emit_barrier ();
1940 emit_label (fail_label);
1941 emit_move_insn (target, const1_rtx);
1942 emit_label (end_label);
1944 return target;
1947 /* Emit code to move a block Y to a block X. This may be done with
1948 string-move instructions, with multiple scalar move instructions,
1949 or with a library call.
1951 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1952 SIZE is an rtx that says how long they are.
1953 ALIGN is the maximum alignment we can assume they have.
1954 METHOD describes what kind of copy this is, and what mechanisms may be used.
1955 MIN_SIZE is the minimal size of block to move
1956 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1957 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1959 Return the address of the new block, if memcpy is called and returns it,
1960 0 otherwise. */
1963 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1964 unsigned int expected_align, HOST_WIDE_INT expected_size,
1965 unsigned HOST_WIDE_INT min_size,
1966 unsigned HOST_WIDE_INT max_size,
1967 unsigned HOST_WIDE_INT probable_max_size,
1968 bool bail_out_libcall, bool *is_move_done,
1969 bool might_overlap)
1971 int may_use_call;
1972 rtx retval = 0;
1973 unsigned int align;
1975 if (is_move_done)
1976 *is_move_done = true;
1978 gcc_assert (size);
1979 if (CONST_INT_P (size) && INTVAL (size) == 0)
1980 return 0;
1982 switch (method)
1984 case BLOCK_OP_NORMAL:
1985 case BLOCK_OP_TAILCALL:
1986 may_use_call = 1;
1987 break;
1989 case BLOCK_OP_CALL_PARM:
1990 may_use_call = block_move_libcall_safe_for_call_parm ();
1992 /* Make inhibit_defer_pop nonzero around the library call
1993 to force it to pop the arguments right away. */
1994 NO_DEFER_POP;
1995 break;
1997 case BLOCK_OP_NO_LIBCALL:
1998 may_use_call = 0;
1999 break;
2001 case BLOCK_OP_NO_LIBCALL_RET:
2002 may_use_call = -1;
2003 break;
2005 default:
2006 gcc_unreachable ();
2009 gcc_assert (MEM_P (x) && MEM_P (y));
2010 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2011 gcc_assert (align >= BITS_PER_UNIT);
2013 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2014 block copy is more efficient for other large modes, e.g. DCmode. */
2015 x = adjust_address (x, BLKmode, 0);
2016 y = adjust_address (y, BLKmode, 0);
2018 /* If source and destination are the same, no need to copy anything. */
2019 if (rtx_equal_p (x, y)
2020 && !MEM_VOLATILE_P (x)
2021 && !MEM_VOLATILE_P (y))
2022 return 0;
2024 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2025 can be incorrect is coming from __builtin_memcpy. */
2026 poly_int64 const_size;
2027 if (poly_int_rtx_p (size, &const_size))
2029 x = shallow_copy_rtx (x);
2030 y = shallow_copy_rtx (y);
2031 set_mem_size (x, const_size);
2032 set_mem_size (y, const_size);
2035 bool pieces_ok = CONST_INT_P (size)
2036 && can_move_by_pieces (INTVAL (size), align);
2037 bool pattern_ok = false;
2039 if (!pieces_ok || might_overlap)
2041 pattern_ok
2042 = emit_block_move_via_pattern (x, y, size, align,
2043 expected_align, expected_size,
2044 min_size, max_size, probable_max_size,
2045 might_overlap);
2046 if (!pattern_ok && might_overlap)
2048 /* Do not try any of the other methods below as they are not safe
2049 for overlapping moves. */
2050 *is_move_done = false;
2051 return retval;
2055 if (pattern_ok)
2057 else if (pieces_ok)
2058 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2059 else if (may_use_call && !might_overlap
2060 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2061 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2063 if (bail_out_libcall)
2065 if (is_move_done)
2066 *is_move_done = false;
2067 return retval;
2070 if (may_use_call < 0)
2071 return pc_rtx;
2073 retval = emit_block_copy_via_libcall (x, y, size,
2074 method == BLOCK_OP_TAILCALL);
2076 else if (might_overlap)
2077 *is_move_done = false;
2078 else
2079 emit_block_move_via_loop (x, y, size, align);
2081 if (method == BLOCK_OP_CALL_PARM)
2082 OK_DEFER_POP;
2084 return retval;
2088 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
2090 unsigned HOST_WIDE_INT max, min = 0;
2091 if (GET_CODE (size) == CONST_INT)
2092 min = max = UINTVAL (size);
2093 else
2094 max = GET_MODE_MASK (GET_MODE (size));
2095 return emit_block_move_hints (x, y, size, method, 0, -1,
2096 min, max, max);
2099 /* A subroutine of emit_block_move. Returns true if calling the
2100 block move libcall will not clobber any parameters which may have
2101 already been placed on the stack. */
2103 static bool
2104 block_move_libcall_safe_for_call_parm (void)
2106 tree fn;
2108 /* If arguments are pushed on the stack, then they're safe. */
2109 if (targetm.calls.push_argument (0))
2110 return true;
2112 /* If registers go on the stack anyway, any argument is sure to clobber
2113 an outgoing argument. */
2114 #if defined (REG_PARM_STACK_SPACE)
2115 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2116 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2117 depend on its argument. */
2118 (void) fn;
2119 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2120 && REG_PARM_STACK_SPACE (fn) != 0)
2121 return false;
2122 #endif
2124 /* If any argument goes in memory, then it might clobber an outgoing
2125 argument. */
2127 CUMULATIVE_ARGS args_so_far_v;
2128 cumulative_args_t args_so_far;
2129 tree arg;
2131 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2132 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2133 args_so_far = pack_cumulative_args (&args_so_far_v);
2135 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2136 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2138 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2139 function_arg_info arg_info (mode, /*named=*/true);
2140 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2141 if (!tmp || !REG_P (tmp))
2142 return false;
2143 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2144 return false;
2145 targetm.calls.function_arg_advance (args_so_far, arg_info);
2148 return true;
2151 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2152 return true if successful.
2154 X is the destination of the copy or move.
2155 Y is the source of the copy or move.
2156 SIZE is the size of the block to be moved.
2158 MIGHT_OVERLAP indicates this originated with expansion of a
2159 builtin_memmove() and the source and destination blocks may
2160 overlap.
2163 static bool
2164 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2165 unsigned int expected_align,
2166 HOST_WIDE_INT expected_size,
2167 unsigned HOST_WIDE_INT min_size,
2168 unsigned HOST_WIDE_INT max_size,
2169 unsigned HOST_WIDE_INT probable_max_size,
2170 bool might_overlap)
2172 if (expected_align < align)
2173 expected_align = align;
2174 if (expected_size != -1)
2176 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2177 expected_size = probable_max_size;
2178 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2179 expected_size = min_size;
2182 /* Since this is a move insn, we don't care about volatility. */
2183 temporary_volatile_ok v (true);
2185 /* Try the most limited insn first, because there's no point
2186 including more than one in the machine description unless
2187 the more limited one has some advantage. */
2189 opt_scalar_int_mode mode_iter;
2190 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2192 scalar_int_mode mode = mode_iter.require ();
2193 enum insn_code code;
2194 if (might_overlap)
2195 code = direct_optab_handler (movmem_optab, mode);
2196 else
2197 code = direct_optab_handler (cpymem_optab, mode);
2199 if (code != CODE_FOR_nothing
2200 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2201 here because if SIZE is less than the mode mask, as it is
2202 returned by the macro, it will definitely be less than the
2203 actual mode mask. Since SIZE is within the Pmode address
2204 space, we limit MODE to Pmode. */
2205 && ((CONST_INT_P (size)
2206 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2207 <= (GET_MODE_MASK (mode) >> 1)))
2208 || max_size <= (GET_MODE_MASK (mode) >> 1)
2209 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2211 class expand_operand ops[9];
2212 unsigned int nops;
2214 /* ??? When called via emit_block_move_for_call, it'd be
2215 nice if there were some way to inform the backend, so
2216 that it doesn't fail the expansion because it thinks
2217 emitting the libcall would be more efficient. */
2218 nops = insn_data[(int) code].n_generator_args;
2219 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2221 create_fixed_operand (&ops[0], x);
2222 create_fixed_operand (&ops[1], y);
2223 /* The check above guarantees that this size conversion is valid. */
2224 create_convert_operand_to (&ops[2], size, mode, true);
2225 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2226 if (nops >= 6)
2228 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2229 create_integer_operand (&ops[5], expected_size);
2231 if (nops >= 8)
2233 create_integer_operand (&ops[6], min_size);
2234 /* If we cannot represent the maximal size,
2235 make parameter NULL. */
2236 if ((HOST_WIDE_INT) max_size != -1)
2237 create_integer_operand (&ops[7], max_size);
2238 else
2239 create_fixed_operand (&ops[7], NULL);
2241 if (nops == 9)
2243 /* If we cannot represent the maximal size,
2244 make parameter NULL. */
2245 if ((HOST_WIDE_INT) probable_max_size != -1)
2246 create_integer_operand (&ops[8], probable_max_size);
2247 else
2248 create_fixed_operand (&ops[8], NULL);
2250 if (maybe_expand_insn (code, nops, ops))
2251 return true;
2255 return false;
2258 /* A subroutine of emit_block_move. Copy the data via an explicit
2259 loop. This is used only when libcalls are forbidden. */
2260 /* ??? It'd be nice to copy in hunks larger than QImode. */
2262 static void
2263 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2264 unsigned int align ATTRIBUTE_UNUSED)
2266 rtx_code_label *cmp_label, *top_label;
2267 rtx iter, x_addr, y_addr, tmp;
2268 machine_mode x_addr_mode = get_address_mode (x);
2269 machine_mode y_addr_mode = get_address_mode (y);
2270 machine_mode iter_mode;
2272 iter_mode = GET_MODE (size);
2273 if (iter_mode == VOIDmode)
2274 iter_mode = word_mode;
2276 top_label = gen_label_rtx ();
2277 cmp_label = gen_label_rtx ();
2278 iter = gen_reg_rtx (iter_mode);
2280 emit_move_insn (iter, const0_rtx);
2282 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2283 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2284 do_pending_stack_adjust ();
2286 emit_jump (cmp_label);
2287 emit_label (top_label);
2289 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2290 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2292 if (x_addr_mode != y_addr_mode)
2293 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2294 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2296 x = change_address (x, QImode, x_addr);
2297 y = change_address (y, QImode, y_addr);
2299 emit_move_insn (x, y);
2301 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2302 true, OPTAB_LIB_WIDEN);
2303 if (tmp != iter)
2304 emit_move_insn (iter, tmp);
2306 emit_label (cmp_label);
2308 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2309 true, top_label,
2310 profile_probability::guessed_always ()
2311 .apply_scale (9, 10));
2314 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2315 TAILCALL is true if this is a tail call. */
2318 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2319 rtx size, bool tailcall)
2321 rtx dst_addr, src_addr;
2322 tree call_expr, dst_tree, src_tree, size_tree;
2323 machine_mode size_mode;
2325 /* Since dst and src are passed to a libcall, mark the corresponding
2326 tree EXPR as addressable. */
2327 tree dst_expr = MEM_EXPR (dst);
2328 tree src_expr = MEM_EXPR (src);
2329 if (dst_expr)
2330 mark_addressable (dst_expr);
2331 if (src_expr)
2332 mark_addressable (src_expr);
2334 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2335 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2336 dst_tree = make_tree (ptr_type_node, dst_addr);
2338 src_addr = copy_addr_to_reg (XEXP (src, 0));
2339 src_addr = convert_memory_address (ptr_mode, src_addr);
2340 src_tree = make_tree (ptr_type_node, src_addr);
2342 size_mode = TYPE_MODE (sizetype);
2343 size = convert_to_mode (size_mode, size, 1);
2344 size = copy_to_mode_reg (size_mode, size);
2345 size_tree = make_tree (sizetype, size);
2347 /* It is incorrect to use the libcall calling conventions for calls to
2348 memcpy/memmove/memcmp because they can be provided by the user. */
2349 tree fn = builtin_decl_implicit (fncode);
2350 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2351 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2353 return expand_call (call_expr, NULL_RTX, false);
2356 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2357 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2358 otherwise return null. */
2361 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2362 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2363 HOST_WIDE_INT align)
2365 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2367 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2368 target = NULL_RTX;
2370 class expand_operand ops[5];
2371 create_output_operand (&ops[0], target, insn_mode);
2372 create_fixed_operand (&ops[1], arg1_rtx);
2373 create_fixed_operand (&ops[2], arg2_rtx);
2374 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2375 TYPE_UNSIGNED (arg3_type));
2376 create_integer_operand (&ops[4], align);
2377 if (maybe_expand_insn (icode, 5, ops))
2378 return ops[0].value;
2379 return NULL_RTX;
2382 /* Expand a block compare between X and Y with length LEN using the
2383 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2384 of the expression that was used to calculate the length. ALIGN
2385 gives the known minimum common alignment. */
2387 static rtx
2388 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2389 unsigned align)
2391 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2392 implementing memcmp because it will stop if it encounters two
2393 zero bytes. */
2394 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2396 if (icode == CODE_FOR_nothing)
2397 return NULL_RTX;
2399 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2402 /* Emit code to compare a block Y to a block X. This may be done with
2403 string-compare instructions, with multiple scalar instructions,
2404 or with a library call.
2406 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2407 they are. LEN_TYPE is the type of the expression that was used to
2408 calculate it.
2410 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2411 value of a normal memcmp call, instead we can just compare for equality.
2412 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2413 returning NULL_RTX.
2415 Optionally, the caller can pass a constfn and associated data in Y_CFN
2416 and Y_CFN_DATA. describing that the second operand being compared is a
2417 known constant and how to obtain its data.
2418 Return the result of the comparison, or NULL_RTX if we failed to
2419 perform the operation. */
2422 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2423 bool equality_only, by_pieces_constfn y_cfn,
2424 void *y_cfndata)
2426 rtx result = 0;
2428 if (CONST_INT_P (len) && INTVAL (len) == 0)
2429 return const0_rtx;
2431 gcc_assert (MEM_P (x) && MEM_P (y));
2432 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2433 gcc_assert (align >= BITS_PER_UNIT);
2435 x = adjust_address (x, BLKmode, 0);
2436 y = adjust_address (y, BLKmode, 0);
2438 if (equality_only
2439 && CONST_INT_P (len)
2440 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2441 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2442 y_cfn, y_cfndata);
2443 else
2444 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2446 return result;
2449 /* Copy all or part of a value X into registers starting at REGNO.
2450 The number of registers to be filled is NREGS. */
2452 void
2453 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2455 if (nregs == 0)
2456 return;
2458 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2459 x = validize_mem (force_const_mem (mode, x));
2461 /* See if the machine can do this with a load multiple insn. */
2462 if (targetm.have_load_multiple ())
2464 rtx_insn *last = get_last_insn ();
2465 rtx first = gen_rtx_REG (word_mode, regno);
2466 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2467 GEN_INT (nregs)))
2469 emit_insn (pat);
2470 return;
2472 else
2473 delete_insns_since (last);
2476 for (int i = 0; i < nregs; i++)
2477 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2478 operand_subword_force (x, i, mode));
2481 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2482 The number of registers to be filled is NREGS. */
2484 void
2485 move_block_from_reg (int regno, rtx x, int nregs)
2487 if (nregs == 0)
2488 return;
2490 /* See if the machine can do this with a store multiple insn. */
2491 if (targetm.have_store_multiple ())
2493 rtx_insn *last = get_last_insn ();
2494 rtx first = gen_rtx_REG (word_mode, regno);
2495 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2496 GEN_INT (nregs)))
2498 emit_insn (pat);
2499 return;
2501 else
2502 delete_insns_since (last);
2505 for (int i = 0; i < nregs; i++)
2507 rtx tem = operand_subword (x, i, 1, BLKmode);
2509 gcc_assert (tem);
2511 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2515 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2516 ORIG, where ORIG is a non-consecutive group of registers represented by
2517 a PARALLEL. The clone is identical to the original except in that the
2518 original set of registers is replaced by a new set of pseudo registers.
2519 The new set has the same modes as the original set. */
2522 gen_group_rtx (rtx orig)
2524 int i, length;
2525 rtx *tmps;
2527 gcc_assert (GET_CODE (orig) == PARALLEL);
2529 length = XVECLEN (orig, 0);
2530 tmps = XALLOCAVEC (rtx, length);
2532 /* Skip a NULL entry in first slot. */
2533 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2535 if (i)
2536 tmps[0] = 0;
2538 for (; i < length; i++)
2540 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2541 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2543 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2546 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2549 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2550 except that values are placed in TMPS[i], and must later be moved
2551 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2553 static void
2554 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2555 poly_int64 ssize)
2557 rtx src;
2558 int start, i;
2559 machine_mode m = GET_MODE (orig_src);
2561 gcc_assert (GET_CODE (dst) == PARALLEL);
2563 if (m != VOIDmode
2564 && !SCALAR_INT_MODE_P (m)
2565 && !MEM_P (orig_src)
2566 && GET_CODE (orig_src) != CONCAT)
2568 scalar_int_mode imode;
2569 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2571 src = gen_reg_rtx (imode);
2572 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2574 else
2576 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2577 emit_move_insn (src, orig_src);
2579 emit_group_load_1 (tmps, dst, src, type, ssize);
2580 return;
2583 /* Check for a NULL entry, used to indicate that the parameter goes
2584 both on the stack and in registers. */
2585 if (XEXP (XVECEXP (dst, 0, 0), 0))
2586 start = 0;
2587 else
2588 start = 1;
2590 /* Process the pieces. */
2591 for (i = start; i < XVECLEN (dst, 0); i++)
2593 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2594 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2595 poly_int64 bytelen = GET_MODE_SIZE (mode);
2596 poly_int64 shift = 0;
2598 /* Handle trailing fragments that run over the size of the struct.
2599 It's the target's responsibility to make sure that the fragment
2600 cannot be strictly smaller in some cases and strictly larger
2601 in others. */
2602 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2603 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2605 /* Arrange to shift the fragment to where it belongs.
2606 extract_bit_field loads to the lsb of the reg. */
2607 if (
2608 #ifdef BLOCK_REG_PADDING
2609 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2610 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2611 #else
2612 BYTES_BIG_ENDIAN
2613 #endif
2615 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2616 bytelen = ssize - bytepos;
2617 gcc_assert (maybe_gt (bytelen, 0));
2620 /* If we won't be loading directly from memory, protect the real source
2621 from strange tricks we might play; but make sure that the source can
2622 be loaded directly into the destination. */
2623 src = orig_src;
2624 if (!MEM_P (orig_src)
2625 && (!CONSTANT_P (orig_src)
2626 || (GET_MODE (orig_src) != mode
2627 && GET_MODE (orig_src) != VOIDmode)))
2629 if (GET_MODE (orig_src) == VOIDmode)
2630 src = gen_reg_rtx (mode);
2631 else
2632 src = gen_reg_rtx (GET_MODE (orig_src));
2634 emit_move_insn (src, orig_src);
2637 /* Optimize the access just a bit. */
2638 if (MEM_P (src)
2639 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2640 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2641 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2642 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2644 tmps[i] = gen_reg_rtx (mode);
2645 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2647 else if (COMPLEX_MODE_P (mode)
2648 && GET_MODE (src) == mode
2649 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2650 /* Let emit_move_complex do the bulk of the work. */
2651 tmps[i] = src;
2652 else if (GET_CODE (src) == CONCAT)
2654 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2655 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2656 unsigned int elt;
2657 poly_int64 subpos;
2659 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2660 && known_le (subpos + bytelen, slen0))
2662 /* The following assumes that the concatenated objects all
2663 have the same size. In this case, a simple calculation
2664 can be used to determine the object and the bit field
2665 to be extracted. */
2666 tmps[i] = XEXP (src, elt);
2667 if (maybe_ne (subpos, 0)
2668 || maybe_ne (subpos + bytelen, slen0)
2669 || (!CONSTANT_P (tmps[i])
2670 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2671 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2672 subpos * BITS_PER_UNIT,
2673 1, NULL_RTX, mode, mode, false,
2674 NULL);
2676 else
2678 rtx mem;
2680 gcc_assert (known_eq (bytepos, 0));
2681 mem = assign_stack_temp (GET_MODE (src), slen);
2682 emit_move_insn (mem, src);
2683 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2684 0, 1, NULL_RTX, mode, mode, false,
2685 NULL);
2688 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2689 && XVECLEN (dst, 0) > 1)
2690 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2691 else if (CONSTANT_P (src))
2693 if (known_eq (bytelen, ssize))
2694 tmps[i] = src;
2695 else
2697 rtx first, second;
2699 /* TODO: const_wide_int can have sizes other than this... */
2700 gcc_assert (known_eq (2 * bytelen, ssize));
2701 split_double (src, &first, &second);
2702 if (i)
2703 tmps[i] = second;
2704 else
2705 tmps[i] = first;
2708 else if (REG_P (src) && GET_MODE (src) == mode)
2709 tmps[i] = src;
2710 else
2711 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2712 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2713 mode, mode, false, NULL);
2715 if (maybe_ne (shift, 0))
2716 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2717 shift, tmps[i], 0);
2721 /* Emit code to move a block SRC of type TYPE to a block DST,
2722 where DST is non-consecutive registers represented by a PARALLEL.
2723 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2724 if not known. */
2726 void
2727 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2729 rtx *tmps;
2730 int i;
2732 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2733 emit_group_load_1 (tmps, dst, src, type, ssize);
2735 /* Copy the extracted pieces into the proper (probable) hard regs. */
2736 for (i = 0; i < XVECLEN (dst, 0); i++)
2738 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2739 if (d == NULL)
2740 continue;
2741 emit_move_insn (d, tmps[i]);
2745 /* Similar, but load SRC into new pseudos in a format that looks like
2746 PARALLEL. This can later be fed to emit_group_move to get things
2747 in the right place. */
2750 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2752 rtvec vec;
2753 int i;
2755 vec = rtvec_alloc (XVECLEN (parallel, 0));
2756 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2758 /* Convert the vector to look just like the original PARALLEL, except
2759 with the computed values. */
2760 for (i = 0; i < XVECLEN (parallel, 0); i++)
2762 rtx e = XVECEXP (parallel, 0, i);
2763 rtx d = XEXP (e, 0);
2765 if (d)
2767 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2768 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2770 RTVEC_ELT (vec, i) = e;
2773 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2776 /* Emit code to move a block SRC to block DST, where SRC and DST are
2777 non-consecutive groups of registers, each represented by a PARALLEL. */
2779 void
2780 emit_group_move (rtx dst, rtx src)
2782 int i;
2784 gcc_assert (GET_CODE (src) == PARALLEL
2785 && GET_CODE (dst) == PARALLEL
2786 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2788 /* Skip first entry if NULL. */
2789 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2790 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2791 XEXP (XVECEXP (src, 0, i), 0));
2794 /* Move a group of registers represented by a PARALLEL into pseudos. */
2797 emit_group_move_into_temps (rtx src)
2799 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2800 int i;
2802 for (i = 0; i < XVECLEN (src, 0); i++)
2804 rtx e = XVECEXP (src, 0, i);
2805 rtx d = XEXP (e, 0);
2807 if (d)
2808 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2809 RTVEC_ELT (vec, i) = e;
2812 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2815 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2816 where SRC is non-consecutive registers represented by a PARALLEL.
2817 SSIZE represents the total size of block ORIG_DST, or -1 if not
2818 known. */
2820 void
2821 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2822 poly_int64 ssize)
2824 rtx *tmps, dst;
2825 int start, finish, i;
2826 machine_mode m = GET_MODE (orig_dst);
2828 gcc_assert (GET_CODE (src) == PARALLEL);
2830 if (!SCALAR_INT_MODE_P (m)
2831 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2833 scalar_int_mode imode;
2834 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2836 dst = gen_reg_rtx (imode);
2837 emit_group_store (dst, src, type, ssize);
2838 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2840 else
2842 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2843 emit_group_store (dst, src, type, ssize);
2845 emit_move_insn (orig_dst, dst);
2846 return;
2849 /* Check for a NULL entry, used to indicate that the parameter goes
2850 both on the stack and in registers. */
2851 if (XEXP (XVECEXP (src, 0, 0), 0))
2852 start = 0;
2853 else
2854 start = 1;
2855 finish = XVECLEN (src, 0);
2857 tmps = XALLOCAVEC (rtx, finish);
2859 /* Copy the (probable) hard regs into pseudos. */
2860 for (i = start; i < finish; i++)
2862 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2863 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2865 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2866 emit_move_insn (tmps[i], reg);
2868 else
2869 tmps[i] = reg;
2872 /* If we won't be storing directly into memory, protect the real destination
2873 from strange tricks we might play. */
2874 dst = orig_dst;
2875 if (GET_CODE (dst) == PARALLEL)
2877 rtx temp;
2879 /* We can get a PARALLEL dst if there is a conditional expression in
2880 a return statement. In that case, the dst and src are the same,
2881 so no action is necessary. */
2882 if (rtx_equal_p (dst, src))
2883 return;
2885 /* It is unclear if we can ever reach here, but we may as well handle
2886 it. Allocate a temporary, and split this into a store/load to/from
2887 the temporary. */
2888 temp = assign_stack_temp (GET_MODE (dst), ssize);
2889 emit_group_store (temp, src, type, ssize);
2890 emit_group_load (dst, temp, type, ssize);
2891 return;
2893 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2895 machine_mode outer = GET_MODE (dst);
2896 machine_mode inner;
2897 poly_int64 bytepos;
2898 bool done = false;
2899 rtx temp;
2901 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2902 dst = gen_reg_rtx (outer);
2904 /* Make life a bit easier for combine: if the first element of the
2905 vector is the low part of the destination mode, use a paradoxical
2906 subreg to initialize the destination. */
2907 if (start < finish)
2909 inner = GET_MODE (tmps[start]);
2910 bytepos = subreg_lowpart_offset (inner, outer);
2911 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2912 bytepos))
2914 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
2915 if (temp)
2917 emit_move_insn (dst, temp);
2918 done = true;
2919 start++;
2924 /* If the first element wasn't the low part, try the last. */
2925 if (!done
2926 && start < finish - 1)
2928 inner = GET_MODE (tmps[finish - 1]);
2929 bytepos = subreg_lowpart_offset (inner, outer);
2930 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2931 finish - 1), 1)),
2932 bytepos))
2934 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
2935 if (temp)
2937 emit_move_insn (dst, temp);
2938 done = true;
2939 finish--;
2944 /* Otherwise, simply initialize the result to zero. */
2945 if (!done)
2946 emit_move_insn (dst, CONST0_RTX (outer));
2949 /* Process the pieces. */
2950 for (i = start; i < finish; i++)
2952 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2953 machine_mode mode = GET_MODE (tmps[i]);
2954 poly_int64 bytelen = GET_MODE_SIZE (mode);
2955 poly_uint64 adj_bytelen;
2956 rtx dest = dst;
2958 /* Handle trailing fragments that run over the size of the struct.
2959 It's the target's responsibility to make sure that the fragment
2960 cannot be strictly smaller in some cases and strictly larger
2961 in others. */
2962 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2963 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2964 adj_bytelen = ssize - bytepos;
2965 else
2966 adj_bytelen = bytelen;
2968 /* Deal with destination CONCATs by either storing into one of the parts
2969 or doing a copy after storing into a register or stack temporary. */
2970 if (GET_CODE (dst) == CONCAT)
2972 if (known_le (bytepos + adj_bytelen,
2973 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2974 dest = XEXP (dst, 0);
2976 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2978 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2979 dest = XEXP (dst, 1);
2982 else
2984 machine_mode dest_mode = GET_MODE (dest);
2985 machine_mode tmp_mode = GET_MODE (tmps[i]);
2986 scalar_int_mode dest_imode;
2988 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2990 /* If the source is a single scalar integer register, and the
2991 destination has a complex mode for which a same-sized integer
2992 mode exists, then we can take the left-justified part of the
2993 source in the complex mode. */
2994 if (finish == start + 1
2995 && REG_P (tmps[i])
2996 && SCALAR_INT_MODE_P (tmp_mode)
2997 && COMPLEX_MODE_P (dest_mode)
2998 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3000 const scalar_int_mode tmp_imode
3001 = as_a <scalar_int_mode> (tmp_mode);
3003 if (GET_MODE_BITSIZE (dest_imode)
3004 < GET_MODE_BITSIZE (tmp_imode))
3006 dest = gen_reg_rtx (dest_imode);
3007 if (BYTES_BIG_ENDIAN)
3008 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3009 GET_MODE_BITSIZE (tmp_imode)
3010 - GET_MODE_BITSIZE (dest_imode),
3011 NULL_RTX, 1);
3012 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3013 dst = gen_lowpart (dest_mode, dest);
3015 else
3016 dst = gen_lowpart (dest_mode, tmps[i]);
3019 /* Otherwise spill the source onto the stack using the more
3020 aligned of the two modes. */
3021 else if (GET_MODE_ALIGNMENT (dest_mode)
3022 >= GET_MODE_ALIGNMENT (tmp_mode))
3024 dest = assign_stack_temp (dest_mode,
3025 GET_MODE_SIZE (dest_mode));
3026 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3027 tmps[i]);
3028 dst = dest;
3031 else
3033 dest = assign_stack_temp (tmp_mode,
3034 GET_MODE_SIZE (tmp_mode));
3035 emit_move_insn (dest, tmps[i]);
3036 dst = adjust_address (dest, dest_mode, bytepos);
3039 break;
3043 /* Handle trailing fragments that run over the size of the struct. */
3044 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3046 /* store_bit_field always takes its value from the lsb.
3047 Move the fragment to the lsb if it's not already there. */
3048 if (
3049 #ifdef BLOCK_REG_PADDING
3050 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3051 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3052 #else
3053 BYTES_BIG_ENDIAN
3054 #endif
3057 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3058 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3059 shift, tmps[i], 0);
3062 /* Make sure not to write past the end of the struct. */
3063 store_bit_field (dest,
3064 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3065 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3066 VOIDmode, tmps[i], false, false);
3069 /* Optimize the access just a bit. */
3070 else if (MEM_P (dest)
3071 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3072 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3073 && multiple_p (bytepos * BITS_PER_UNIT,
3074 GET_MODE_ALIGNMENT (mode))
3075 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3076 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3078 else
3079 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3080 0, 0, mode, tmps[i], false, false);
3083 /* Copy from the pseudo into the (probable) hard reg. */
3084 if (orig_dst != dst)
3085 emit_move_insn (orig_dst, dst);
3088 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3089 of the value stored in X. */
3092 maybe_emit_group_store (rtx x, tree type)
3094 machine_mode mode = TYPE_MODE (type);
3095 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3096 if (GET_CODE (x) == PARALLEL)
3098 rtx result = gen_reg_rtx (mode);
3099 emit_group_store (result, x, type, int_size_in_bytes (type));
3100 return result;
3102 return x;
3105 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3107 This is used on targets that return BLKmode values in registers. */
3109 static void
3110 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3112 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3113 rtx src = NULL, dst = NULL;
3114 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3115 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3116 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3117 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3118 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3119 fixed_size_mode copy_mode;
3121 /* BLKmode registers created in the back-end shouldn't have survived. */
3122 gcc_assert (mode != BLKmode);
3124 /* If the structure doesn't take up a whole number of words, see whether
3125 SRCREG is padded on the left or on the right. If it's on the left,
3126 set PADDING_CORRECTION to the number of bits to skip.
3128 In most ABIs, the structure will be returned at the least end of
3129 the register, which translates to right padding on little-endian
3130 targets and left padding on big-endian targets. The opposite
3131 holds if the structure is returned at the most significant
3132 end of the register. */
3133 if (bytes % UNITS_PER_WORD != 0
3134 && (targetm.calls.return_in_msb (type)
3135 ? !BYTES_BIG_ENDIAN
3136 : BYTES_BIG_ENDIAN))
3137 padding_correction
3138 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3140 /* We can use a single move if we have an exact mode for the size. */
3141 else if (MEM_P (target)
3142 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3143 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3144 && bytes == GET_MODE_SIZE (mode))
3146 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3147 return;
3150 /* And if we additionally have the same mode for a register. */
3151 else if (REG_P (target)
3152 && GET_MODE (target) == mode
3153 && bytes == GET_MODE_SIZE (mode))
3155 emit_move_insn (target, srcreg);
3156 return;
3159 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3160 into a new pseudo which is a full word. */
3161 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3163 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3164 mode = word_mode;
3167 /* Copy the structure BITSIZE bits at a time. If the target lives in
3168 memory, take care of not reading/writing past its end by selecting
3169 a copy mode suited to BITSIZE. This should always be possible given
3170 how it is computed.
3172 If the target lives in register, make sure not to select a copy mode
3173 larger than the mode of the register.
3175 We could probably emit more efficient code for machines which do not use
3176 strict alignment, but it doesn't seem worth the effort at the current
3177 time. */
3179 copy_mode = word_mode;
3180 if (MEM_P (target))
3182 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3183 if (mem_mode.exists ())
3184 copy_mode = mem_mode.require ();
3186 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3187 copy_mode = tmode;
3189 for (bitpos = 0, xbitpos = padding_correction;
3190 bitpos < bytes * BITS_PER_UNIT;
3191 bitpos += bitsize, xbitpos += bitsize)
3193 /* We need a new source operand each time xbitpos is on a
3194 word boundary and when xbitpos == padding_correction
3195 (the first time through). */
3196 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3197 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3199 /* We need a new destination operand each time bitpos is on
3200 a word boundary. */
3201 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3202 dst = target;
3203 else if (bitpos % BITS_PER_WORD == 0)
3204 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3206 /* Use xbitpos for the source extraction (right justified) and
3207 bitpos for the destination store (left justified). */
3208 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3209 extract_bit_field (src, bitsize,
3210 xbitpos % BITS_PER_WORD, 1,
3211 NULL_RTX, copy_mode, copy_mode,
3212 false, NULL),
3213 false, false);
3217 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3218 register if it contains any data, otherwise return null.
3220 This is used on targets that return BLKmode values in registers. */
3223 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3225 int i, n_regs;
3226 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3227 unsigned int bitsize;
3228 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3229 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3230 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3231 fixed_size_mode dst_mode;
3232 scalar_int_mode min_mode;
3234 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3236 x = expand_normal (src);
3238 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3239 if (bytes == 0)
3240 return NULL_RTX;
3242 /* If the structure doesn't take up a whole number of words, see
3243 whether the register value should be padded on the left or on
3244 the right. Set PADDING_CORRECTION to the number of padding
3245 bits needed on the left side.
3247 In most ABIs, the structure will be returned at the least end of
3248 the register, which translates to right padding on little-endian
3249 targets and left padding on big-endian targets. The opposite
3250 holds if the structure is returned at the most significant
3251 end of the register. */
3252 if (bytes % UNITS_PER_WORD != 0
3253 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3254 ? !BYTES_BIG_ENDIAN
3255 : BYTES_BIG_ENDIAN))
3256 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3257 * BITS_PER_UNIT));
3259 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3260 dst_words = XALLOCAVEC (rtx, n_regs);
3261 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3262 min_mode = smallest_int_mode_for_size (bitsize);
3264 /* Copy the structure BITSIZE bits at a time. */
3265 for (bitpos = 0, xbitpos = padding_correction;
3266 bitpos < bytes * BITS_PER_UNIT;
3267 bitpos += bitsize, xbitpos += bitsize)
3269 /* We need a new destination pseudo each time xbitpos is
3270 on a word boundary and when xbitpos == padding_correction
3271 (the first time through). */
3272 if (xbitpos % BITS_PER_WORD == 0
3273 || xbitpos == padding_correction)
3275 /* Generate an appropriate register. */
3276 dst_word = gen_reg_rtx (word_mode);
3277 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3279 /* Clear the destination before we move anything into it. */
3280 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3283 /* Find the largest integer mode that can be used to copy all or as
3284 many bits as possible of the structure if the target supports larger
3285 copies. There are too many corner cases here w.r.t to alignments on
3286 the read/writes. So if there is any padding just use single byte
3287 operations. */
3288 opt_scalar_int_mode mode_iter;
3289 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3291 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3293 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3294 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3295 && msize <= BITS_PER_WORD)
3296 bitsize = msize;
3297 else
3298 break;
3302 /* We need a new source operand each time bitpos is on a word
3303 boundary. */
3304 if (bitpos % BITS_PER_WORD == 0)
3305 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3307 /* Use bitpos for the source extraction (left justified) and
3308 xbitpos for the destination store (right justified). */
3309 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3310 0, 0, word_mode,
3311 extract_bit_field (src_word, bitsize,
3312 bitpos % BITS_PER_WORD, 1,
3313 NULL_RTX, word_mode, word_mode,
3314 false, NULL),
3315 false, false);
3318 if (mode == BLKmode)
3320 /* Find the smallest integer mode large enough to hold the
3321 entire structure. */
3322 opt_scalar_int_mode mode_iter;
3323 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3324 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3325 break;
3327 /* A suitable mode should have been found. */
3328 mode = mode_iter.require ();
3331 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3332 dst_mode = word_mode;
3333 else
3334 dst_mode = mode;
3335 dst = gen_reg_rtx (dst_mode);
3337 for (i = 0; i < n_regs; i++)
3338 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3340 if (mode != dst_mode)
3341 dst = gen_lowpart (mode, dst);
3343 return dst;
3346 /* Add a USE expression for REG to the (possibly empty) list pointed
3347 to by CALL_FUSAGE. REG must denote a hard register. */
3349 void
3350 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3352 gcc_assert (REG_P (reg));
3354 if (!HARD_REGISTER_P (reg))
3355 return;
3357 *call_fusage
3358 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3361 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3362 to by CALL_FUSAGE. REG must denote a hard register. */
3364 void
3365 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3367 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3369 *call_fusage
3370 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3373 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3374 starting at REGNO. All of these registers must be hard registers. */
3376 void
3377 use_regs (rtx *call_fusage, int regno, int nregs)
3379 int i;
3381 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3383 for (i = 0; i < nregs; i++)
3384 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3387 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3388 PARALLEL REGS. This is for calls that pass values in multiple
3389 non-contiguous locations. The Irix 6 ABI has examples of this. */
3391 void
3392 use_group_regs (rtx *call_fusage, rtx regs)
3394 int i;
3396 for (i = 0; i < XVECLEN (regs, 0); i++)
3398 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3400 /* A NULL entry means the parameter goes both on the stack and in
3401 registers. This can also be a MEM for targets that pass values
3402 partially on the stack and partially in registers. */
3403 if (reg != 0 && REG_P (reg))
3404 use_reg (call_fusage, reg);
3408 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3409 assigment and the code of the expresion on the RHS is CODE. Return
3410 NULL otherwise. */
3412 static gimple *
3413 get_def_for_expr (tree name, enum tree_code code)
3415 gimple *def_stmt;
3417 if (TREE_CODE (name) != SSA_NAME)
3418 return NULL;
3420 def_stmt = get_gimple_for_ssa_name (name);
3421 if (!def_stmt
3422 || gimple_assign_rhs_code (def_stmt) != code)
3423 return NULL;
3425 return def_stmt;
3428 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3429 assigment and the class of the expresion on the RHS is CLASS. Return
3430 NULL otherwise. */
3432 static gimple *
3433 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3435 gimple *def_stmt;
3437 if (TREE_CODE (name) != SSA_NAME)
3438 return NULL;
3440 def_stmt = get_gimple_for_ssa_name (name);
3441 if (!def_stmt
3442 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3443 return NULL;
3445 return def_stmt;
3448 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3449 its length in bytes. */
3452 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3453 unsigned int expected_align, HOST_WIDE_INT expected_size,
3454 unsigned HOST_WIDE_INT min_size,
3455 unsigned HOST_WIDE_INT max_size,
3456 unsigned HOST_WIDE_INT probable_max_size,
3457 unsigned ctz_size)
3459 machine_mode mode = GET_MODE (object);
3460 unsigned int align;
3462 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3464 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3465 just move a zero. Otherwise, do this a piece at a time. */
3466 poly_int64 size_val;
3467 if (mode != BLKmode
3468 && poly_int_rtx_p (size, &size_val)
3469 && known_eq (size_val, GET_MODE_SIZE (mode)))
3471 rtx zero = CONST0_RTX (mode);
3472 if (zero != NULL)
3474 emit_move_insn (object, zero);
3475 return NULL;
3478 if (COMPLEX_MODE_P (mode))
3480 zero = CONST0_RTX (GET_MODE_INNER (mode));
3481 if (zero != NULL)
3483 write_complex_part (object, zero, 0, true);
3484 write_complex_part (object, zero, 1, false);
3485 return NULL;
3490 if (size == const0_rtx)
3491 return NULL;
3493 align = MEM_ALIGN (object);
3495 if (CONST_INT_P (size)
3496 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3497 CLEAR_BY_PIECES,
3498 optimize_insn_for_speed_p ()))
3499 clear_by_pieces (object, INTVAL (size), align);
3500 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3501 expected_align, expected_size,
3502 min_size, max_size, probable_max_size))
3504 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3505 min_size, max_size,
3506 NULL_RTX, 0, align))
3508 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3509 return set_storage_via_libcall (object, size, const0_rtx,
3510 method == BLOCK_OP_TAILCALL);
3511 else
3512 gcc_unreachable ();
3514 return NULL;
3518 clear_storage (rtx object, rtx size, enum block_op_methods method)
3520 unsigned HOST_WIDE_INT max, min = 0;
3521 if (GET_CODE (size) == CONST_INT)
3522 min = max = UINTVAL (size);
3523 else
3524 max = GET_MODE_MASK (GET_MODE (size));
3525 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3529 /* A subroutine of clear_storage. Expand a call to memset.
3530 Return the return value of memset, 0 otherwise. */
3533 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3535 tree call_expr, fn, object_tree, size_tree, val_tree;
3536 machine_mode size_mode;
3538 object = copy_addr_to_reg (XEXP (object, 0));
3539 object_tree = make_tree (ptr_type_node, object);
3541 if (!CONST_INT_P (val))
3542 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3543 val_tree = make_tree (integer_type_node, val);
3545 size_mode = TYPE_MODE (sizetype);
3546 size = convert_to_mode (size_mode, size, 1);
3547 size = copy_to_mode_reg (size_mode, size);
3548 size_tree = make_tree (sizetype, size);
3550 /* It is incorrect to use the libcall calling conventions for calls to
3551 memset because it can be provided by the user. */
3552 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3553 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3554 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3556 return expand_call (call_expr, NULL_RTX, false);
3559 /* Expand a setmem pattern; return true if successful. */
3561 bool
3562 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3563 unsigned int expected_align, HOST_WIDE_INT expected_size,
3564 unsigned HOST_WIDE_INT min_size,
3565 unsigned HOST_WIDE_INT max_size,
3566 unsigned HOST_WIDE_INT probable_max_size)
3568 /* Try the most limited insn first, because there's no point
3569 including more than one in the machine description unless
3570 the more limited one has some advantage. */
3572 if (expected_align < align)
3573 expected_align = align;
3574 if (expected_size != -1)
3576 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3577 expected_size = max_size;
3578 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3579 expected_size = min_size;
3582 opt_scalar_int_mode mode_iter;
3583 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3585 scalar_int_mode mode = mode_iter.require ();
3586 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3588 if (code != CODE_FOR_nothing
3589 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3590 here because if SIZE is less than the mode mask, as it is
3591 returned by the macro, it will definitely be less than the
3592 actual mode mask. Since SIZE is within the Pmode address
3593 space, we limit MODE to Pmode. */
3594 && ((CONST_INT_P (size)
3595 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3596 <= (GET_MODE_MASK (mode) >> 1)))
3597 || max_size <= (GET_MODE_MASK (mode) >> 1)
3598 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3600 class expand_operand ops[9];
3601 unsigned int nops;
3603 nops = insn_data[(int) code].n_generator_args;
3604 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3606 create_fixed_operand (&ops[0], object);
3607 /* The check above guarantees that this size conversion is valid. */
3608 create_convert_operand_to (&ops[1], size, mode, true);
3609 create_convert_operand_from (&ops[2], val, byte_mode, true);
3610 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3611 if (nops >= 6)
3613 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3614 create_integer_operand (&ops[5], expected_size);
3616 if (nops >= 8)
3618 create_integer_operand (&ops[6], min_size);
3619 /* If we cannot represent the maximal size,
3620 make parameter NULL. */
3621 if ((HOST_WIDE_INT) max_size != -1)
3622 create_integer_operand (&ops[7], max_size);
3623 else
3624 create_fixed_operand (&ops[7], NULL);
3626 if (nops == 9)
3628 /* If we cannot represent the maximal size,
3629 make parameter NULL. */
3630 if ((HOST_WIDE_INT) probable_max_size != -1)
3631 create_integer_operand (&ops[8], probable_max_size);
3632 else
3633 create_fixed_operand (&ops[8], NULL);
3635 if (maybe_expand_insn (code, nops, ops))
3636 return true;
3640 return false;
3644 /* Write to one of the components of the complex value CPLX. Write VAL to
3645 the real part if IMAG_P is false, and the imaginary part if its true.
3646 If UNDEFINED_P then the value in CPLX is currently undefined. */
3648 void
3649 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
3651 machine_mode cmode;
3652 scalar_mode imode;
3653 unsigned ibitsize;
3655 if (GET_CODE (cplx) == CONCAT)
3657 emit_move_insn (XEXP (cplx, imag_p), val);
3658 return;
3661 cmode = GET_MODE (cplx);
3662 imode = GET_MODE_INNER (cmode);
3663 ibitsize = GET_MODE_BITSIZE (imode);
3665 /* For MEMs simplify_gen_subreg may generate an invalid new address
3666 because, e.g., the original address is considered mode-dependent
3667 by the target, which restricts simplify_subreg from invoking
3668 adjust_address_nv. Instead of preparing fallback support for an
3669 invalid address, we call adjust_address_nv directly. */
3670 if (MEM_P (cplx))
3672 emit_move_insn (adjust_address_nv (cplx, imode,
3673 imag_p ? GET_MODE_SIZE (imode) : 0),
3674 val);
3675 return;
3678 /* If the sub-object is at least word sized, then we know that subregging
3679 will work. This special case is important, since store_bit_field
3680 wants to operate on integer modes, and there's rarely an OImode to
3681 correspond to TCmode. */
3682 if (ibitsize >= BITS_PER_WORD
3683 /* For hard regs we have exact predicates. Assume we can split
3684 the original object if it spans an even number of hard regs.
3685 This special case is important for SCmode on 64-bit platforms
3686 where the natural size of floating-point regs is 32-bit. */
3687 || (REG_P (cplx)
3688 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3689 && REG_NREGS (cplx) % 2 == 0))
3691 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3692 imag_p ? GET_MODE_SIZE (imode) : 0);
3693 if (part)
3695 emit_move_insn (part, val);
3696 return;
3698 else
3699 /* simplify_gen_subreg may fail for sub-word MEMs. */
3700 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3703 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3704 false, undefined_p);
3707 /* Extract one of the components of the complex value CPLX. Extract the
3708 real part if IMAG_P is false, and the imaginary part if it's true. */
3711 read_complex_part (rtx cplx, bool imag_p)
3713 machine_mode cmode;
3714 scalar_mode imode;
3715 unsigned ibitsize;
3717 if (GET_CODE (cplx) == CONCAT)
3718 return XEXP (cplx, imag_p);
3720 cmode = GET_MODE (cplx);
3721 imode = GET_MODE_INNER (cmode);
3722 ibitsize = GET_MODE_BITSIZE (imode);
3724 /* Special case reads from complex constants that got spilled to memory. */
3725 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3727 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3728 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3730 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3731 if (CONSTANT_CLASS_P (part))
3732 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3736 /* For MEMs simplify_gen_subreg may generate an invalid new address
3737 because, e.g., the original address is considered mode-dependent
3738 by the target, which restricts simplify_subreg from invoking
3739 adjust_address_nv. Instead of preparing fallback support for an
3740 invalid address, we call adjust_address_nv directly. */
3741 if (MEM_P (cplx))
3742 return adjust_address_nv (cplx, imode,
3743 imag_p ? GET_MODE_SIZE (imode) : 0);
3745 /* If the sub-object is at least word sized, then we know that subregging
3746 will work. This special case is important, since extract_bit_field
3747 wants to operate on integer modes, and there's rarely an OImode to
3748 correspond to TCmode. */
3749 if (ibitsize >= BITS_PER_WORD
3750 /* For hard regs we have exact predicates. Assume we can split
3751 the original object if it spans an even number of hard regs.
3752 This special case is important for SCmode on 64-bit platforms
3753 where the natural size of floating-point regs is 32-bit. */
3754 || (REG_P (cplx)
3755 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3756 && REG_NREGS (cplx) % 2 == 0))
3758 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3759 imag_p ? GET_MODE_SIZE (imode) : 0);
3760 if (ret)
3761 return ret;
3762 else
3763 /* simplify_gen_subreg may fail for sub-word MEMs. */
3764 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3767 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3768 true, NULL_RTX, imode, imode, false, NULL);
3771 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3772 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3773 represented in NEW_MODE. If FORCE is true, this will never happen, as
3774 we'll force-create a SUBREG if needed. */
3776 static rtx
3777 emit_move_change_mode (machine_mode new_mode,
3778 machine_mode old_mode, rtx x, bool force)
3780 rtx ret;
3782 if (push_operand (x, GET_MODE (x)))
3784 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3785 MEM_COPY_ATTRIBUTES (ret, x);
3787 else if (MEM_P (x))
3789 /* We don't have to worry about changing the address since the
3790 size in bytes is supposed to be the same. */
3791 if (reload_in_progress)
3793 /* Copy the MEM to change the mode and move any
3794 substitutions from the old MEM to the new one. */
3795 ret = adjust_address_nv (x, new_mode, 0);
3796 copy_replacements (x, ret);
3798 else
3799 ret = adjust_address (x, new_mode, 0);
3801 else
3803 /* Note that we do want simplify_subreg's behavior of validating
3804 that the new mode is ok for a hard register. If we were to use
3805 simplify_gen_subreg, we would create the subreg, but would
3806 probably run into the target not being able to implement it. */
3807 /* Except, of course, when FORCE is true, when this is exactly what
3808 we want. Which is needed for CCmodes on some targets. */
3809 if (force)
3810 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3811 else
3812 ret = simplify_subreg (new_mode, x, old_mode, 0);
3815 return ret;
3818 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3819 an integer mode of the same size as MODE. Returns the instruction
3820 emitted, or NULL if such a move could not be generated. */
3822 static rtx_insn *
3823 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3825 scalar_int_mode imode;
3826 enum insn_code code;
3828 /* There must exist a mode of the exact size we require. */
3829 if (!int_mode_for_mode (mode).exists (&imode))
3830 return NULL;
3832 /* The target must support moves in this mode. */
3833 code = optab_handler (mov_optab, imode);
3834 if (code == CODE_FOR_nothing)
3835 return NULL;
3837 x = emit_move_change_mode (imode, mode, x, force);
3838 if (x == NULL_RTX)
3839 return NULL;
3840 y = emit_move_change_mode (imode, mode, y, force);
3841 if (y == NULL_RTX)
3842 return NULL;
3843 return emit_insn (GEN_FCN (code) (x, y));
3846 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3847 Return an equivalent MEM that does not use an auto-increment. */
3850 emit_move_resolve_push (machine_mode mode, rtx x)
3852 enum rtx_code code = GET_CODE (XEXP (x, 0));
3853 rtx temp;
3855 poly_int64 adjust = GET_MODE_SIZE (mode);
3856 #ifdef PUSH_ROUNDING
3857 adjust = PUSH_ROUNDING (adjust);
3858 #endif
3859 if (code == PRE_DEC || code == POST_DEC)
3860 adjust = -adjust;
3861 else if (code == PRE_MODIFY || code == POST_MODIFY)
3863 rtx expr = XEXP (XEXP (x, 0), 1);
3865 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3866 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3867 if (GET_CODE (expr) == MINUS)
3868 val = -val;
3869 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3870 adjust = val;
3873 /* Do not use anti_adjust_stack, since we don't want to update
3874 stack_pointer_delta. */
3875 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3876 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3877 0, OPTAB_LIB_WIDEN);
3878 if (temp != stack_pointer_rtx)
3879 emit_move_insn (stack_pointer_rtx, temp);
3881 switch (code)
3883 case PRE_INC:
3884 case PRE_DEC:
3885 case PRE_MODIFY:
3886 temp = stack_pointer_rtx;
3887 break;
3888 case POST_INC:
3889 case POST_DEC:
3890 case POST_MODIFY:
3891 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3892 break;
3893 default:
3894 gcc_unreachable ();
3897 return replace_equiv_address (x, temp);
3900 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3901 X is known to satisfy push_operand, and MODE is known to be complex.
3902 Returns the last instruction emitted. */
3904 rtx_insn *
3905 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3907 scalar_mode submode = GET_MODE_INNER (mode);
3908 bool imag_first;
3910 #ifdef PUSH_ROUNDING
3911 poly_int64 submodesize = GET_MODE_SIZE (submode);
3913 /* In case we output to the stack, but the size is smaller than the
3914 machine can push exactly, we need to use move instructions. */
3915 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3917 x = emit_move_resolve_push (mode, x);
3918 return emit_move_insn (x, y);
3920 #endif
3922 /* Note that the real part always precedes the imag part in memory
3923 regardless of machine's endianness. */
3924 switch (GET_CODE (XEXP (x, 0)))
3926 case PRE_DEC:
3927 case POST_DEC:
3928 imag_first = true;
3929 break;
3930 case PRE_INC:
3931 case POST_INC:
3932 imag_first = false;
3933 break;
3934 default:
3935 gcc_unreachable ();
3938 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3939 read_complex_part (y, imag_first));
3940 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3941 read_complex_part (y, !imag_first));
3944 /* A subroutine of emit_move_complex. Perform the move from Y to X
3945 via two moves of the parts. Returns the last instruction emitted. */
3947 rtx_insn *
3948 emit_move_complex_parts (rtx x, rtx y)
3950 /* Show the output dies here. This is necessary for SUBREGs
3951 of pseudos since we cannot track their lifetimes correctly;
3952 hard regs shouldn't appear here except as return values. */
3953 if (!reload_completed && !reload_in_progress
3954 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3955 emit_clobber (x);
3957 write_complex_part (x, read_complex_part (y, false), false, true);
3958 write_complex_part (x, read_complex_part (y, true), true, false);
3960 return get_last_insn ();
3963 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3964 MODE is known to be complex. Returns the last instruction emitted. */
3966 static rtx_insn *
3967 emit_move_complex (machine_mode mode, rtx x, rtx y)
3969 bool try_int;
3971 /* Need to take special care for pushes, to maintain proper ordering
3972 of the data, and possibly extra padding. */
3973 if (push_operand (x, mode))
3974 return emit_move_complex_push (mode, x, y);
3976 /* See if we can coerce the target into moving both values at once, except
3977 for floating point where we favor moving as parts if this is easy. */
3978 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3979 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3980 && !(REG_P (x)
3981 && HARD_REGISTER_P (x)
3982 && REG_NREGS (x) == 1)
3983 && !(REG_P (y)
3984 && HARD_REGISTER_P (y)
3985 && REG_NREGS (y) == 1))
3986 try_int = false;
3987 /* Not possible if the values are inherently not adjacent. */
3988 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3989 try_int = false;
3990 /* Is possible if both are registers (or subregs of registers). */
3991 else if (register_operand (x, mode) && register_operand (y, mode))
3992 try_int = true;
3993 /* If one of the operands is a memory, and alignment constraints
3994 are friendly enough, we may be able to do combined memory operations.
3995 We do not attempt this if Y is a constant because that combination is
3996 usually better with the by-parts thing below. */
3997 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3998 && (!STRICT_ALIGNMENT
3999 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4000 try_int = true;
4001 else
4002 try_int = false;
4004 if (try_int)
4006 rtx_insn *ret;
4008 /* For memory to memory moves, optimal behavior can be had with the
4009 existing block move logic. But use normal expansion if optimizing
4010 for size. */
4011 if (MEM_P (x) && MEM_P (y))
4013 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4014 (optimize_insn_for_speed_p()
4015 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4016 return get_last_insn ();
4019 ret = emit_move_via_integer (mode, x, y, true);
4020 if (ret)
4021 return ret;
4024 return emit_move_complex_parts (x, y);
4027 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4028 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4030 static rtx_insn *
4031 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4033 rtx_insn *ret;
4035 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4036 if (mode != CCmode)
4038 enum insn_code code = optab_handler (mov_optab, CCmode);
4039 if (code != CODE_FOR_nothing)
4041 x = emit_move_change_mode (CCmode, mode, x, true);
4042 y = emit_move_change_mode (CCmode, mode, y, true);
4043 return emit_insn (GEN_FCN (code) (x, y));
4047 /* Otherwise, find the MODE_INT mode of the same width. */
4048 ret = emit_move_via_integer (mode, x, y, false);
4049 gcc_assert (ret != NULL);
4050 return ret;
4053 /* Return true if word I of OP lies entirely in the
4054 undefined bits of a paradoxical subreg. */
4056 static bool
4057 undefined_operand_subword_p (const_rtx op, int i)
4059 if (GET_CODE (op) != SUBREG)
4060 return false;
4061 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4062 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4063 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4064 || known_le (offset, -UNITS_PER_WORD));
4067 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4068 MODE is any multi-word or full-word mode that lacks a move_insn
4069 pattern. Note that you will get better code if you define such
4070 patterns, even if they must turn into multiple assembler instructions. */
4072 static rtx_insn *
4073 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4075 rtx_insn *last_insn = 0;
4076 rtx_insn *seq;
4077 rtx inner;
4078 bool need_clobber;
4079 int i, mode_size;
4081 /* This function can only handle cases where the number of words is
4082 known at compile time. */
4083 mode_size = GET_MODE_SIZE (mode).to_constant ();
4084 gcc_assert (mode_size >= UNITS_PER_WORD);
4086 /* If X is a push on the stack, do the push now and replace
4087 X with a reference to the stack pointer. */
4088 if (push_operand (x, mode))
4089 x = emit_move_resolve_push (mode, x);
4091 /* If we are in reload, see if either operand is a MEM whose address
4092 is scheduled for replacement. */
4093 if (reload_in_progress && MEM_P (x)
4094 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4095 x = replace_equiv_address_nv (x, inner);
4096 if (reload_in_progress && MEM_P (y)
4097 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4098 y = replace_equiv_address_nv (y, inner);
4100 start_sequence ();
4102 need_clobber = false;
4103 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4105 /* Do not generate code for a move if it would go entirely
4106 to the non-existing bits of a paradoxical subreg. */
4107 if (undefined_operand_subword_p (x, i))
4108 continue;
4110 rtx xpart = operand_subword (x, i, 1, mode);
4111 rtx ypart;
4113 /* Do not generate code for a move if it would come entirely
4114 from the undefined bits of a paradoxical subreg. */
4115 if (undefined_operand_subword_p (y, i))
4116 continue;
4118 ypart = operand_subword (y, i, 1, mode);
4120 /* If we can't get a part of Y, put Y into memory if it is a
4121 constant. Otherwise, force it into a register. Then we must
4122 be able to get a part of Y. */
4123 if (ypart == 0 && CONSTANT_P (y))
4125 y = use_anchored_address (force_const_mem (mode, y));
4126 ypart = operand_subword (y, i, 1, mode);
4128 else if (ypart == 0)
4129 ypart = operand_subword_force (y, i, mode);
4131 gcc_assert (xpart && ypart);
4133 need_clobber |= (GET_CODE (xpart) == SUBREG);
4135 last_insn = emit_move_insn (xpart, ypart);
4138 seq = get_insns ();
4139 end_sequence ();
4141 /* Show the output dies here. This is necessary for SUBREGs
4142 of pseudos since we cannot track their lifetimes correctly;
4143 hard regs shouldn't appear here except as return values.
4144 We never want to emit such a clobber after reload. */
4145 if (x != y
4146 && ! (reload_in_progress || reload_completed)
4147 && need_clobber != 0)
4148 emit_clobber (x);
4150 emit_insn (seq);
4152 return last_insn;
4155 /* Low level part of emit_move_insn.
4156 Called just like emit_move_insn, but assumes X and Y
4157 are basically valid. */
4159 rtx_insn *
4160 emit_move_insn_1 (rtx x, rtx y)
4162 machine_mode mode = GET_MODE (x);
4163 enum insn_code code;
4165 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4167 code = optab_handler (mov_optab, mode);
4168 if (code != CODE_FOR_nothing)
4169 return emit_insn (GEN_FCN (code) (x, y));
4171 /* Expand complex moves by moving real part and imag part. */
4172 if (COMPLEX_MODE_P (mode))
4173 return emit_move_complex (mode, x, y);
4175 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4176 || ALL_FIXED_POINT_MODE_P (mode))
4178 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4180 /* If we can't find an integer mode, use multi words. */
4181 if (result)
4182 return result;
4183 else
4184 return emit_move_multi_word (mode, x, y);
4187 if (GET_MODE_CLASS (mode) == MODE_CC)
4188 return emit_move_ccmode (mode, x, y);
4190 /* Try using a move pattern for the corresponding integer mode. This is
4191 only safe when simplify_subreg can convert MODE constants into integer
4192 constants. At present, it can only do this reliably if the value
4193 fits within a HOST_WIDE_INT. */
4194 if (!CONSTANT_P (y)
4195 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4197 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4199 if (ret)
4201 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4202 return ret;
4206 return emit_move_multi_word (mode, x, y);
4209 /* Generate code to copy Y into X.
4210 Both Y and X must have the same mode, except that
4211 Y can be a constant with VOIDmode.
4212 This mode cannot be BLKmode; use emit_block_move for that.
4214 Return the last instruction emitted. */
4216 rtx_insn *
4217 emit_move_insn (rtx x, rtx y)
4219 machine_mode mode = GET_MODE (x);
4220 rtx y_cst = NULL_RTX;
4221 rtx_insn *last_insn;
4222 rtx set;
4224 gcc_assert (mode != BLKmode
4225 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4227 /* If we have a copy that looks like one of the following patterns:
4228 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4229 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4230 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4231 (set (subreg:M1 (reg:M2 ...)) (constant C))
4232 where mode M1 is equal in size to M2, try to detect whether the
4233 mode change involves an implicit round trip through memory.
4234 If so, see if we can avoid that by removing the subregs and
4235 doing the move in mode M2 instead. */
4237 rtx x_inner = NULL_RTX;
4238 rtx y_inner = NULL_RTX;
4240 auto candidate_subreg_p = [&](rtx subreg) {
4241 return (REG_P (SUBREG_REG (subreg))
4242 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4243 GET_MODE_SIZE (GET_MODE (subreg)))
4244 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4245 != CODE_FOR_nothing);
4248 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4249 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4250 && !push_operand (mem, GET_MODE (mem))
4251 /* Not a candiate if innermode requires too much alignment. */
4252 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4253 || targetm.slow_unaligned_access (GET_MODE (mem),
4254 MEM_ALIGN (mem))
4255 || !targetm.slow_unaligned_access (innermode,
4256 MEM_ALIGN (mem))));
4259 if (SUBREG_P (x) && candidate_subreg_p (x))
4260 x_inner = SUBREG_REG (x);
4262 if (SUBREG_P (y) && candidate_subreg_p (y))
4263 y_inner = SUBREG_REG (y);
4265 if (x_inner != NULL_RTX
4266 && y_inner != NULL_RTX
4267 && GET_MODE (x_inner) == GET_MODE (y_inner)
4268 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4270 x = x_inner;
4271 y = y_inner;
4272 mode = GET_MODE (x_inner);
4274 else if (x_inner != NULL_RTX
4275 && MEM_P (y)
4276 && candidate_mem_p (GET_MODE (x_inner), y))
4278 x = x_inner;
4279 y = adjust_address (y, GET_MODE (x_inner), 0);
4280 mode = GET_MODE (x_inner);
4282 else if (y_inner != NULL_RTX
4283 && MEM_P (x)
4284 && candidate_mem_p (GET_MODE (y_inner), x))
4286 x = adjust_address (x, GET_MODE (y_inner), 0);
4287 y = y_inner;
4288 mode = GET_MODE (y_inner);
4290 else if (x_inner != NULL_RTX
4291 && CONSTANT_P (y)
4292 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4293 mode, ALL_REGS)
4294 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4296 x = x_inner;
4297 y = y_inner;
4298 mode = GET_MODE (x_inner);
4301 if (CONSTANT_P (y))
4303 if (optimize
4304 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4305 && (last_insn = compress_float_constant (x, y)))
4306 return last_insn;
4308 y_cst = y;
4310 if (!targetm.legitimate_constant_p (mode, y))
4312 y = force_const_mem (mode, y);
4314 /* If the target's cannot_force_const_mem prevented the spill,
4315 assume that the target's move expanders will also take care
4316 of the non-legitimate constant. */
4317 if (!y)
4318 y = y_cst;
4319 else
4320 y = use_anchored_address (y);
4324 /* If X or Y are memory references, verify that their addresses are valid
4325 for the machine. */
4326 if (MEM_P (x)
4327 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4328 MEM_ADDR_SPACE (x))
4329 && ! push_operand (x, GET_MODE (x))))
4330 x = validize_mem (x);
4332 if (MEM_P (y)
4333 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4334 MEM_ADDR_SPACE (y)))
4335 y = validize_mem (y);
4337 gcc_assert (mode != BLKmode);
4339 last_insn = emit_move_insn_1 (x, y);
4341 if (y_cst && REG_P (x)
4342 && (set = single_set (last_insn)) != NULL_RTX
4343 && SET_DEST (set) == x
4344 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4345 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4347 return last_insn;
4350 /* Generate the body of an instruction to copy Y into X.
4351 It may be a list of insns, if one insn isn't enough. */
4353 rtx_insn *
4354 gen_move_insn (rtx x, rtx y)
4356 rtx_insn *seq;
4358 start_sequence ();
4359 emit_move_insn_1 (x, y);
4360 seq = get_insns ();
4361 end_sequence ();
4362 return seq;
4365 /* If Y is representable exactly in a narrower mode, and the target can
4366 perform the extension directly from constant or memory, then emit the
4367 move as an extension. */
4369 static rtx_insn *
4370 compress_float_constant (rtx x, rtx y)
4372 machine_mode dstmode = GET_MODE (x);
4373 machine_mode orig_srcmode = GET_MODE (y);
4374 machine_mode srcmode;
4375 const REAL_VALUE_TYPE *r;
4376 int oldcost, newcost;
4377 bool speed = optimize_insn_for_speed_p ();
4379 r = CONST_DOUBLE_REAL_VALUE (y);
4381 if (targetm.legitimate_constant_p (dstmode, y))
4382 oldcost = set_src_cost (y, orig_srcmode, speed);
4383 else
4384 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4386 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4388 enum insn_code ic;
4389 rtx trunc_y;
4390 rtx_insn *last_insn;
4392 /* Skip if the target can't extend this way. */
4393 ic = can_extend_p (dstmode, srcmode, 0);
4394 if (ic == CODE_FOR_nothing)
4395 continue;
4397 /* Skip if the narrowed value isn't exact. */
4398 if (! exact_real_truncate (srcmode, r))
4399 continue;
4401 trunc_y = const_double_from_real_value (*r, srcmode);
4403 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4405 /* Skip if the target needs extra instructions to perform
4406 the extension. */
4407 if (!insn_operand_matches (ic, 1, trunc_y))
4408 continue;
4409 /* This is valid, but may not be cheaper than the original. */
4410 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4411 dstmode, speed);
4412 if (oldcost < newcost)
4413 continue;
4415 else if (float_extend_from_mem[dstmode][srcmode])
4417 trunc_y = force_const_mem (srcmode, trunc_y);
4418 /* This is valid, but may not be cheaper than the original. */
4419 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4420 dstmode, speed);
4421 if (oldcost < newcost)
4422 continue;
4423 trunc_y = validize_mem (trunc_y);
4425 else
4426 continue;
4428 /* For CSE's benefit, force the compressed constant pool entry
4429 into a new pseudo. This constant may be used in different modes,
4430 and if not, combine will put things back together for us. */
4431 trunc_y = force_reg (srcmode, trunc_y);
4433 /* If x is a hard register, perform the extension into a pseudo,
4434 so that e.g. stack realignment code is aware of it. */
4435 rtx target = x;
4436 if (REG_P (x) && HARD_REGISTER_P (x))
4437 target = gen_reg_rtx (dstmode);
4439 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4440 last_insn = get_last_insn ();
4442 if (REG_P (target))
4443 set_unique_reg_note (last_insn, REG_EQUAL, y);
4445 if (target != x)
4446 return emit_move_insn (x, target);
4447 return last_insn;
4450 return NULL;
4453 /* Pushing data onto the stack. */
4455 /* Push a block of length SIZE (perhaps variable)
4456 and return an rtx to address the beginning of the block.
4457 The value may be virtual_outgoing_args_rtx.
4459 EXTRA is the number of bytes of padding to push in addition to SIZE.
4460 BELOW nonzero means this padding comes at low addresses;
4461 otherwise, the padding comes at high addresses. */
4464 push_block (rtx size, poly_int64 extra, int below)
4466 rtx temp;
4468 size = convert_modes (Pmode, ptr_mode, size, 1);
4469 if (CONSTANT_P (size))
4470 anti_adjust_stack (plus_constant (Pmode, size, extra));
4471 else if (REG_P (size) && known_eq (extra, 0))
4472 anti_adjust_stack (size);
4473 else
4475 temp = copy_to_mode_reg (Pmode, size);
4476 if (maybe_ne (extra, 0))
4477 temp = expand_binop (Pmode, add_optab, temp,
4478 gen_int_mode (extra, Pmode),
4479 temp, 0, OPTAB_LIB_WIDEN);
4480 anti_adjust_stack (temp);
4483 if (STACK_GROWS_DOWNWARD)
4485 temp = virtual_outgoing_args_rtx;
4486 if (maybe_ne (extra, 0) && below)
4487 temp = plus_constant (Pmode, temp, extra);
4489 else
4491 poly_int64 csize;
4492 if (poly_int_rtx_p (size, &csize))
4493 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4494 -csize - (below ? 0 : extra));
4495 else if (maybe_ne (extra, 0) && !below)
4496 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4497 negate_rtx (Pmode, plus_constant (Pmode, size,
4498 extra)));
4499 else
4500 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4501 negate_rtx (Pmode, size));
4504 return memory_address (NARROWEST_INT_MODE, temp);
4507 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4509 static rtx
4510 mem_autoinc_base (rtx mem)
4512 if (MEM_P (mem))
4514 rtx addr = XEXP (mem, 0);
4515 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4516 return XEXP (addr, 0);
4518 return NULL;
4521 /* A utility routine used here, in reload, and in try_split. The insns
4522 after PREV up to and including LAST are known to adjust the stack,
4523 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4524 placing notes as appropriate. PREV may be NULL, indicating the
4525 entire insn sequence prior to LAST should be scanned.
4527 The set of allowed stack pointer modifications is small:
4528 (1) One or more auto-inc style memory references (aka pushes),
4529 (2) One or more addition/subtraction with the SP as destination,
4530 (3) A single move insn with the SP as destination,
4531 (4) A call_pop insn,
4532 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4534 Insns in the sequence that do not modify the SP are ignored,
4535 except for noreturn calls.
4537 The return value is the amount of adjustment that can be trivially
4538 verified, via immediate operand or auto-inc. If the adjustment
4539 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4541 poly_int64
4542 find_args_size_adjust (rtx_insn *insn)
4544 rtx dest, set, pat;
4545 int i;
4547 pat = PATTERN (insn);
4548 set = NULL;
4550 /* Look for a call_pop pattern. */
4551 if (CALL_P (insn))
4553 /* We have to allow non-call_pop patterns for the case
4554 of emit_single_push_insn of a TLS address. */
4555 if (GET_CODE (pat) != PARALLEL)
4556 return 0;
4558 /* All call_pop have a stack pointer adjust in the parallel.
4559 The call itself is always first, and the stack adjust is
4560 usually last, so search from the end. */
4561 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4563 set = XVECEXP (pat, 0, i);
4564 if (GET_CODE (set) != SET)
4565 continue;
4566 dest = SET_DEST (set);
4567 if (dest == stack_pointer_rtx)
4568 break;
4570 /* We'd better have found the stack pointer adjust. */
4571 if (i == 0)
4572 return 0;
4573 /* Fall through to process the extracted SET and DEST
4574 as if it was a standalone insn. */
4576 else if (GET_CODE (pat) == SET)
4577 set = pat;
4578 else if ((set = single_set (insn)) != NULL)
4580 else if (GET_CODE (pat) == PARALLEL)
4582 /* ??? Some older ports use a parallel with a stack adjust
4583 and a store for a PUSH_ROUNDING pattern, rather than a
4584 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4585 /* ??? See h8300 and m68k, pushqi1. */
4586 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4588 set = XVECEXP (pat, 0, i);
4589 if (GET_CODE (set) != SET)
4590 continue;
4591 dest = SET_DEST (set);
4592 if (dest == stack_pointer_rtx)
4593 break;
4595 /* We do not expect an auto-inc of the sp in the parallel. */
4596 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4597 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4598 != stack_pointer_rtx);
4600 if (i < 0)
4601 return 0;
4603 else
4604 return 0;
4606 dest = SET_DEST (set);
4608 /* Look for direct modifications of the stack pointer. */
4609 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4611 /* Look for a trivial adjustment, otherwise assume nothing. */
4612 /* Note that the SPU restore_stack_block pattern refers to
4613 the stack pointer in V4SImode. Consider that non-trivial. */
4614 poly_int64 offset;
4615 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4616 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4617 return offset;
4618 /* ??? Reload can generate no-op moves, which will be cleaned
4619 up later. Recognize it and continue searching. */
4620 else if (rtx_equal_p (dest, SET_SRC (set)))
4621 return 0;
4622 else
4623 return HOST_WIDE_INT_MIN;
4625 else
4627 rtx mem, addr;
4629 /* Otherwise only think about autoinc patterns. */
4630 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4632 mem = dest;
4633 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4634 != stack_pointer_rtx);
4636 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4637 mem = SET_SRC (set);
4638 else
4639 return 0;
4641 addr = XEXP (mem, 0);
4642 switch (GET_CODE (addr))
4644 case PRE_INC:
4645 case POST_INC:
4646 return GET_MODE_SIZE (GET_MODE (mem));
4647 case PRE_DEC:
4648 case POST_DEC:
4649 return -GET_MODE_SIZE (GET_MODE (mem));
4650 case PRE_MODIFY:
4651 case POST_MODIFY:
4652 addr = XEXP (addr, 1);
4653 gcc_assert (GET_CODE (addr) == PLUS);
4654 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4655 return rtx_to_poly_int64 (XEXP (addr, 1));
4656 default:
4657 gcc_unreachable ();
4662 poly_int64
4663 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4664 poly_int64 end_args_size)
4666 poly_int64 args_size = end_args_size;
4667 bool saw_unknown = false;
4668 rtx_insn *insn;
4670 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4672 if (!NONDEBUG_INSN_P (insn))
4673 continue;
4675 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4676 a call argument containing a TLS address that itself requires
4677 a call to __tls_get_addr. The handling of stack_pointer_delta
4678 in emit_single_push_insn is supposed to ensure that any such
4679 notes are already correct. */
4680 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4681 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4683 poly_int64 this_delta = find_args_size_adjust (insn);
4684 if (known_eq (this_delta, 0))
4686 if (!CALL_P (insn)
4687 || ACCUMULATE_OUTGOING_ARGS
4688 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4689 continue;
4692 gcc_assert (!saw_unknown);
4693 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4694 saw_unknown = true;
4696 if (!note)
4697 add_args_size_note (insn, args_size);
4698 if (STACK_GROWS_DOWNWARD)
4699 this_delta = -poly_uint64 (this_delta);
4701 if (saw_unknown)
4702 args_size = HOST_WIDE_INT_MIN;
4703 else
4704 args_size -= this_delta;
4707 return args_size;
4710 #ifdef PUSH_ROUNDING
4711 /* Emit single push insn. */
4713 static void
4714 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4716 rtx dest_addr;
4717 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4718 rtx dest;
4719 enum insn_code icode;
4721 /* If there is push pattern, use it. Otherwise try old way of throwing
4722 MEM representing push operation to move expander. */
4723 icode = optab_handler (push_optab, mode);
4724 if (icode != CODE_FOR_nothing)
4726 class expand_operand ops[1];
4728 create_input_operand (&ops[0], x, mode);
4729 if (maybe_expand_insn (icode, 1, ops))
4730 return;
4732 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4733 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4734 /* If we are to pad downward, adjust the stack pointer first and
4735 then store X into the stack location using an offset. This is
4736 because emit_move_insn does not know how to pad; it does not have
4737 access to type. */
4738 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4740 emit_move_insn (stack_pointer_rtx,
4741 expand_binop (Pmode,
4742 STACK_GROWS_DOWNWARD ? sub_optab
4743 : add_optab,
4744 stack_pointer_rtx,
4745 gen_int_mode (rounded_size, Pmode),
4746 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4748 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4749 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4750 /* We have already decremented the stack pointer, so get the
4751 previous value. */
4752 offset += rounded_size;
4754 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4755 /* We have already incremented the stack pointer, so get the
4756 previous value. */
4757 offset -= rounded_size;
4759 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4761 else
4763 if (STACK_GROWS_DOWNWARD)
4764 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4765 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4766 else
4767 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4768 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4770 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4773 dest = gen_rtx_MEM (mode, dest_addr);
4775 if (type != 0)
4777 set_mem_attributes (dest, type, 1);
4779 if (cfun->tail_call_marked)
4780 /* Function incoming arguments may overlap with sibling call
4781 outgoing arguments and we cannot allow reordering of reads
4782 from function arguments with stores to outgoing arguments
4783 of sibling calls. */
4784 set_mem_alias_set (dest, 0);
4786 emit_move_insn (dest, x);
4789 /* Emit and annotate a single push insn. */
4791 static void
4792 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4794 poly_int64 delta, old_delta = stack_pointer_delta;
4795 rtx_insn *prev = get_last_insn ();
4796 rtx_insn *last;
4798 emit_single_push_insn_1 (mode, x, type);
4800 /* Adjust stack_pointer_delta to describe the situation after the push
4801 we just performed. Note that we must do this after the push rather
4802 than before the push in case calculating X needs pushes and pops of
4803 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4804 for such pushes and pops must not include the effect of the future
4805 push of X. */
4806 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4808 last = get_last_insn ();
4810 /* Notice the common case where we emitted exactly one insn. */
4811 if (PREV_INSN (last) == prev)
4813 add_args_size_note (last, stack_pointer_delta);
4814 return;
4817 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4818 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4819 || known_eq (delta, old_delta));
4821 #endif
4823 /* If reading SIZE bytes from X will end up reading from
4824 Y return the number of bytes that overlap. Return -1
4825 if there is no overlap or -2 if we can't determine
4826 (for example when X and Y have different base registers). */
4828 static int
4829 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4831 rtx tmp = plus_constant (Pmode, x, size);
4832 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4834 if (!CONST_INT_P (sub))
4835 return -2;
4837 HOST_WIDE_INT val = INTVAL (sub);
4839 return IN_RANGE (val, 1, size) ? val : -1;
4842 /* Generate code to push X onto the stack, assuming it has mode MODE and
4843 type TYPE.
4844 MODE is redundant except when X is a CONST_INT (since they don't
4845 carry mode info).
4846 SIZE is an rtx for the size of data to be copied (in bytes),
4847 needed only if X is BLKmode.
4848 Return true if successful. May return false if asked to push a
4849 partial argument during a sibcall optimization (as specified by
4850 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4851 to not overlap.
4853 ALIGN (in bits) is maximum alignment we can assume.
4855 If PARTIAL and REG are both nonzero, then copy that many of the first
4856 bytes of X into registers starting with REG, and push the rest of X.
4857 The amount of space pushed is decreased by PARTIAL bytes.
4858 REG must be a hard register in this case.
4859 If REG is zero but PARTIAL is not, take any all others actions for an
4860 argument partially in registers, but do not actually load any
4861 registers.
4863 EXTRA is the amount in bytes of extra space to leave next to this arg.
4864 This is ignored if an argument block has already been allocated.
4866 On a machine that lacks real push insns, ARGS_ADDR is the address of
4867 the bottom of the argument block for this call. We use indexing off there
4868 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4869 argument block has not been preallocated.
4871 ARGS_SO_FAR is the size of args previously pushed for this call.
4873 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4874 for arguments passed in registers. If nonzero, it will be the number
4875 of bytes required. */
4877 bool
4878 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4879 unsigned int align, int partial, rtx reg, poly_int64 extra,
4880 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4881 rtx alignment_pad, bool sibcall_p)
4883 rtx xinner;
4884 pad_direction stack_direction
4885 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4887 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4888 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4889 Default is below for small data on big-endian machines; else above. */
4890 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4892 /* Invert direction if stack is post-decrement.
4893 FIXME: why? */
4894 if (STACK_PUSH_CODE == POST_DEC)
4895 if (where_pad != PAD_NONE)
4896 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4898 xinner = x;
4900 int nregs = partial / UNITS_PER_WORD;
4901 rtx *tmp_regs = NULL;
4902 int overlapping = 0;
4904 if (mode == BLKmode
4905 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4907 /* Copy a block into the stack, entirely or partially. */
4909 rtx temp;
4910 int used;
4911 int offset;
4912 int skip;
4914 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4915 used = partial - offset;
4917 if (mode != BLKmode)
4919 /* A value is to be stored in an insufficiently aligned
4920 stack slot; copy via a suitably aligned slot if
4921 necessary. */
4922 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4923 if (!MEM_P (xinner))
4925 temp = assign_temp (type, 1, 1);
4926 emit_move_insn (temp, xinner);
4927 xinner = temp;
4931 gcc_assert (size);
4933 /* USED is now the # of bytes we need not copy to the stack
4934 because registers will take care of them. */
4936 if (partial != 0)
4937 xinner = adjust_address (xinner, BLKmode, used);
4939 /* If the partial register-part of the arg counts in its stack size,
4940 skip the part of stack space corresponding to the registers.
4941 Otherwise, start copying to the beginning of the stack space,
4942 by setting SKIP to 0. */
4943 skip = (reg_parm_stack_space == 0) ? 0 : used;
4945 #ifdef PUSH_ROUNDING
4946 /* NB: Let the backend known the number of bytes to push and
4947 decide if push insns should be generated. */
4948 unsigned int push_size;
4949 if (CONST_INT_P (size))
4950 push_size = INTVAL (size);
4951 else
4952 push_size = 0;
4954 /* Do it with several push insns if that doesn't take lots of insns
4955 and if there is no difficulty with push insns that skip bytes
4956 on the stack for alignment purposes. */
4957 if (args_addr == 0
4958 && targetm.calls.push_argument (push_size)
4959 && CONST_INT_P (size)
4960 && skip == 0
4961 && MEM_ALIGN (xinner) >= align
4962 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4963 /* Here we avoid the case of a structure whose weak alignment
4964 forces many pushes of a small amount of data,
4965 and such small pushes do rounding that causes trouble. */
4966 && ((!targetm.slow_unaligned_access (word_mode, align))
4967 || align >= BIGGEST_ALIGNMENT
4968 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4969 align / BITS_PER_UNIT))
4970 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4972 /* Push padding now if padding above and stack grows down,
4973 or if padding below and stack grows up.
4974 But if space already allocated, this has already been done. */
4975 if (maybe_ne (extra, 0)
4976 && args_addr == 0
4977 && where_pad != PAD_NONE
4978 && where_pad != stack_direction)
4979 anti_adjust_stack (gen_int_mode (extra, Pmode));
4981 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4982 RETURN_BEGIN);
4984 else
4985 #endif /* PUSH_ROUNDING */
4987 rtx target;
4989 /* Otherwise make space on the stack and copy the data
4990 to the address of that space. */
4992 /* Deduct words put into registers from the size we must copy. */
4993 if (partial != 0)
4995 if (CONST_INT_P (size))
4996 size = GEN_INT (INTVAL (size) - used);
4997 else
4998 size = expand_binop (GET_MODE (size), sub_optab, size,
4999 gen_int_mode (used, GET_MODE (size)),
5000 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5003 /* Get the address of the stack space.
5004 In this case, we do not deal with EXTRA separately.
5005 A single stack adjust will do. */
5006 poly_int64 const_args_so_far;
5007 if (! args_addr)
5009 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5010 extra = 0;
5012 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5013 temp = memory_address (BLKmode,
5014 plus_constant (Pmode, args_addr,
5015 skip + const_args_so_far));
5016 else
5017 temp = memory_address (BLKmode,
5018 plus_constant (Pmode,
5019 gen_rtx_PLUS (Pmode,
5020 args_addr,
5021 args_so_far),
5022 skip));
5024 if (!ACCUMULATE_OUTGOING_ARGS)
5026 /* If the source is referenced relative to the stack pointer,
5027 copy it to another register to stabilize it. We do not need
5028 to do this if we know that we won't be changing sp. */
5030 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5031 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5032 temp = copy_to_reg (temp);
5035 target = gen_rtx_MEM (BLKmode, temp);
5037 /* We do *not* set_mem_attributes here, because incoming arguments
5038 may overlap with sibling call outgoing arguments and we cannot
5039 allow reordering of reads from function arguments with stores
5040 to outgoing arguments of sibling calls. We do, however, want
5041 to record the alignment of the stack slot. */
5042 /* ALIGN may well be better aligned than TYPE, e.g. due to
5043 PARM_BOUNDARY. Assume the caller isn't lying. */
5044 set_mem_align (target, align);
5046 /* If part should go in registers and pushing to that part would
5047 overwrite some of the values that need to go into regs, load the
5048 overlapping values into temporary pseudos to be moved into the hard
5049 regs at the end after the stack pushing has completed.
5050 We cannot load them directly into the hard regs here because
5051 they can be clobbered by the block move expansions.
5052 See PR 65358. */
5054 if (partial > 0 && reg != 0 && mode == BLKmode
5055 && GET_CODE (reg) != PARALLEL)
5057 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5058 if (overlapping > 0)
5060 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5061 overlapping /= UNITS_PER_WORD;
5063 tmp_regs = XALLOCAVEC (rtx, overlapping);
5065 for (int i = 0; i < overlapping; i++)
5066 tmp_regs[i] = gen_reg_rtx (word_mode);
5068 for (int i = 0; i < overlapping; i++)
5069 emit_move_insn (tmp_regs[i],
5070 operand_subword_force (target, i, mode));
5072 else if (overlapping == -1)
5073 overlapping = 0;
5074 /* Could not determine whether there is overlap.
5075 Fail the sibcall. */
5076 else
5078 overlapping = 0;
5079 if (sibcall_p)
5080 return false;
5084 /* If source is a constant VAR_DECL with a simple constructor,
5085 store the constructor to the stack instead of moving it. */
5086 const_tree decl;
5087 if (partial == 0
5088 && MEM_P (xinner)
5089 && SYMBOL_REF_P (XEXP (xinner, 0))
5090 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5091 && VAR_P (decl)
5092 && TREE_READONLY (decl)
5093 && !TREE_SIDE_EFFECTS (decl)
5094 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5095 store_constructor (DECL_INITIAL (decl), target, 0,
5096 int_expr_size (DECL_INITIAL (decl)), false);
5097 else
5098 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5101 else if (partial > 0)
5103 /* Scalar partly in registers. This case is only supported
5104 for fixed-wdth modes. */
5105 int num_words = GET_MODE_SIZE (mode).to_constant ();
5106 num_words /= UNITS_PER_WORD;
5107 int i;
5108 int not_stack;
5109 /* # bytes of start of argument
5110 that we must make space for but need not store. */
5111 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5112 int args_offset = INTVAL (args_so_far);
5113 int skip;
5115 /* Push padding now if padding above and stack grows down,
5116 or if padding below and stack grows up.
5117 But if space already allocated, this has already been done. */
5118 if (maybe_ne (extra, 0)
5119 && args_addr == 0
5120 && where_pad != PAD_NONE
5121 && where_pad != stack_direction)
5122 anti_adjust_stack (gen_int_mode (extra, Pmode));
5124 /* If we make space by pushing it, we might as well push
5125 the real data. Otherwise, we can leave OFFSET nonzero
5126 and leave the space uninitialized. */
5127 if (args_addr == 0)
5128 offset = 0;
5130 /* Now NOT_STACK gets the number of words that we don't need to
5131 allocate on the stack. Convert OFFSET to words too. */
5132 not_stack = (partial - offset) / UNITS_PER_WORD;
5133 offset /= UNITS_PER_WORD;
5135 /* If the partial register-part of the arg counts in its stack size,
5136 skip the part of stack space corresponding to the registers.
5137 Otherwise, start copying to the beginning of the stack space,
5138 by setting SKIP to 0. */
5139 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5141 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5142 x = validize_mem (force_const_mem (mode, x));
5144 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5145 SUBREGs of such registers are not allowed. */
5146 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5147 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5148 x = copy_to_reg (x);
5150 /* Loop over all the words allocated on the stack for this arg. */
5151 /* We can do it by words, because any scalar bigger than a word
5152 has a size a multiple of a word. */
5153 for (i = num_words - 1; i >= not_stack; i--)
5154 if (i >= not_stack + offset)
5155 if (!emit_push_insn (operand_subword_force (x, i, mode),
5156 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
5157 0, args_addr,
5158 GEN_INT (args_offset + ((i - not_stack + skip)
5159 * UNITS_PER_WORD)),
5160 reg_parm_stack_space, alignment_pad, sibcall_p))
5161 return false;
5163 else
5165 rtx addr;
5166 rtx dest;
5168 /* Push padding now if padding above and stack grows down,
5169 or if padding below and stack grows up.
5170 But if space already allocated, this has already been done. */
5171 if (maybe_ne (extra, 0)
5172 && args_addr == 0
5173 && where_pad != PAD_NONE
5174 && where_pad != stack_direction)
5175 anti_adjust_stack (gen_int_mode (extra, Pmode));
5177 #ifdef PUSH_ROUNDING
5178 if (args_addr == 0 && targetm.calls.push_argument (0))
5179 emit_single_push_insn (mode, x, type);
5180 else
5181 #endif
5183 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5184 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5186 /* We do *not* set_mem_attributes here, because incoming arguments
5187 may overlap with sibling call outgoing arguments and we cannot
5188 allow reordering of reads from function arguments with stores
5189 to outgoing arguments of sibling calls. We do, however, want
5190 to record the alignment of the stack slot. */
5191 /* ALIGN may well be better aligned than TYPE, e.g. due to
5192 PARM_BOUNDARY. Assume the caller isn't lying. */
5193 set_mem_align (dest, align);
5195 emit_move_insn (dest, x);
5199 /* Move the partial arguments into the registers and any overlapping
5200 values that we moved into the pseudos in tmp_regs. */
5201 if (partial > 0 && reg != 0)
5203 /* Handle calls that pass values in multiple non-contiguous locations.
5204 The Irix 6 ABI has examples of this. */
5205 if (GET_CODE (reg) == PARALLEL)
5206 emit_group_load (reg, x, type, -1);
5207 else
5209 gcc_assert (partial % UNITS_PER_WORD == 0);
5210 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5212 for (int i = 0; i < overlapping; i++)
5213 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5214 + nregs - overlapping + i),
5215 tmp_regs[i]);
5220 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5221 anti_adjust_stack (gen_int_mode (extra, Pmode));
5223 if (alignment_pad && args_addr == 0)
5224 anti_adjust_stack (alignment_pad);
5226 return true;
5229 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5230 operations. */
5232 static rtx
5233 get_subtarget (rtx x)
5235 return (optimize
5236 || x == 0
5237 /* Only registers can be subtargets. */
5238 || !REG_P (x)
5239 /* Don't use hard regs to avoid extending their life. */
5240 || REGNO (x) < FIRST_PSEUDO_REGISTER
5241 ? 0 : x);
5244 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5245 FIELD is a bitfield. Returns true if the optimization was successful,
5246 and there's nothing else to do. */
5248 static bool
5249 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5250 poly_uint64 pbitpos,
5251 poly_uint64 pbitregion_start,
5252 poly_uint64 pbitregion_end,
5253 machine_mode mode1, rtx str_rtx,
5254 tree to, tree src, bool reverse)
5256 /* str_mode is not guaranteed to be a scalar type. */
5257 machine_mode str_mode = GET_MODE (str_rtx);
5258 unsigned int str_bitsize;
5259 tree op0, op1;
5260 rtx value, result;
5261 optab binop;
5262 gimple *srcstmt;
5263 enum tree_code code;
5265 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5266 if (mode1 != VOIDmode
5267 || !pbitsize.is_constant (&bitsize)
5268 || !pbitpos.is_constant (&bitpos)
5269 || !pbitregion_start.is_constant (&bitregion_start)
5270 || !pbitregion_end.is_constant (&bitregion_end)
5271 || bitsize >= BITS_PER_WORD
5272 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5273 || str_bitsize > BITS_PER_WORD
5274 || TREE_SIDE_EFFECTS (to)
5275 || TREE_THIS_VOLATILE (to))
5276 return false;
5278 STRIP_NOPS (src);
5279 if (TREE_CODE (src) != SSA_NAME)
5280 return false;
5281 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5282 return false;
5284 srcstmt = get_gimple_for_ssa_name (src);
5285 if (!srcstmt
5286 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5287 return false;
5289 code = gimple_assign_rhs_code (srcstmt);
5291 op0 = gimple_assign_rhs1 (srcstmt);
5293 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5294 to find its initialization. Hopefully the initialization will
5295 be from a bitfield load. */
5296 if (TREE_CODE (op0) == SSA_NAME)
5298 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5300 /* We want to eventually have OP0 be the same as TO, which
5301 should be a bitfield. */
5302 if (!op0stmt
5303 || !is_gimple_assign (op0stmt)
5304 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5305 return false;
5306 op0 = gimple_assign_rhs1 (op0stmt);
5309 op1 = gimple_assign_rhs2 (srcstmt);
5311 if (!operand_equal_p (to, op0, 0))
5312 return false;
5314 if (MEM_P (str_rtx))
5316 unsigned HOST_WIDE_INT offset1;
5318 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5319 str_bitsize = BITS_PER_WORD;
5321 scalar_int_mode best_mode;
5322 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5323 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5324 return false;
5325 str_mode = best_mode;
5326 str_bitsize = GET_MODE_BITSIZE (best_mode);
5328 offset1 = bitpos;
5329 bitpos %= str_bitsize;
5330 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5331 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5333 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5334 return false;
5336 /* If the bit field covers the whole REG/MEM, store_field
5337 will likely generate better code. */
5338 if (bitsize >= str_bitsize)
5339 return false;
5341 /* We can't handle fields split across multiple entities. */
5342 if (bitpos + bitsize > str_bitsize)
5343 return false;
5345 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5346 bitpos = str_bitsize - bitpos - bitsize;
5348 switch (code)
5350 case PLUS_EXPR:
5351 case MINUS_EXPR:
5352 /* For now, just optimize the case of the topmost bitfield
5353 where we don't need to do any masking and also
5354 1 bit bitfields where xor can be used.
5355 We might win by one instruction for the other bitfields
5356 too if insv/extv instructions aren't used, so that
5357 can be added later. */
5358 if ((reverse || bitpos + bitsize != str_bitsize)
5359 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5360 break;
5362 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5363 value = convert_modes (str_mode,
5364 TYPE_MODE (TREE_TYPE (op1)), value,
5365 TYPE_UNSIGNED (TREE_TYPE (op1)));
5367 /* We may be accessing data outside the field, which means
5368 we can alias adjacent data. */
5369 if (MEM_P (str_rtx))
5371 str_rtx = shallow_copy_rtx (str_rtx);
5372 set_mem_alias_set (str_rtx, 0);
5373 set_mem_expr (str_rtx, 0);
5376 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5378 value = expand_and (str_mode, value, const1_rtx, NULL);
5379 binop = xor_optab;
5381 else
5382 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5384 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5385 if (reverse)
5386 value = flip_storage_order (str_mode, value);
5387 result = expand_binop (str_mode, binop, str_rtx,
5388 value, str_rtx, 1, OPTAB_WIDEN);
5389 if (result != str_rtx)
5390 emit_move_insn (str_rtx, result);
5391 return true;
5393 case BIT_IOR_EXPR:
5394 case BIT_XOR_EXPR:
5395 if (TREE_CODE (op1) != INTEGER_CST)
5396 break;
5397 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5398 value = convert_modes (str_mode,
5399 TYPE_MODE (TREE_TYPE (op1)), value,
5400 TYPE_UNSIGNED (TREE_TYPE (op1)));
5402 /* We may be accessing data outside the field, which means
5403 we can alias adjacent data. */
5404 if (MEM_P (str_rtx))
5406 str_rtx = shallow_copy_rtx (str_rtx);
5407 set_mem_alias_set (str_rtx, 0);
5408 set_mem_expr (str_rtx, 0);
5411 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5412 if (bitpos + bitsize != str_bitsize)
5414 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5415 str_mode);
5416 value = expand_and (str_mode, value, mask, NULL_RTX);
5418 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5419 if (reverse)
5420 value = flip_storage_order (str_mode, value);
5421 result = expand_binop (str_mode, binop, str_rtx,
5422 value, str_rtx, 1, OPTAB_WIDEN);
5423 if (result != str_rtx)
5424 emit_move_insn (str_rtx, result);
5425 return true;
5427 default:
5428 break;
5431 return false;
5434 /* In the C++ memory model, consecutive bit fields in a structure are
5435 considered one memory location.
5437 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5438 returns the bit range of consecutive bits in which this COMPONENT_REF
5439 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5440 and *OFFSET may be adjusted in the process.
5442 If the access does not need to be restricted, 0 is returned in both
5443 *BITSTART and *BITEND. */
5445 void
5446 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5447 poly_int64_pod *bitpos, tree *offset)
5449 poly_int64 bitoffset;
5450 tree field, repr;
5452 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5454 field = TREE_OPERAND (exp, 1);
5455 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5456 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5457 need to limit the range we can access. */
5458 if (!repr)
5460 *bitstart = *bitend = 0;
5461 return;
5464 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5465 part of a larger bit field, then the representative does not serve any
5466 useful purpose. This can occur in Ada. */
5467 if (handled_component_p (TREE_OPERAND (exp, 0)))
5469 machine_mode rmode;
5470 poly_int64 rbitsize, rbitpos;
5471 tree roffset;
5472 int unsignedp, reversep, volatilep = 0;
5473 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5474 &roffset, &rmode, &unsignedp, &reversep,
5475 &volatilep);
5476 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5478 *bitstart = *bitend = 0;
5479 return;
5483 /* Compute the adjustment to bitpos from the offset of the field
5484 relative to the representative. DECL_FIELD_OFFSET of field and
5485 repr are the same by construction if they are not constants,
5486 see finish_bitfield_layout. */
5487 poly_uint64 field_offset, repr_offset;
5488 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5489 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5490 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5491 else
5492 bitoffset = 0;
5493 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5494 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5496 /* If the adjustment is larger than bitpos, we would have a negative bit
5497 position for the lower bound and this may wreak havoc later. Adjust
5498 offset and bitpos to make the lower bound non-negative in that case. */
5499 if (maybe_gt (bitoffset, *bitpos))
5501 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5502 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5504 *bitpos += adjust_bits;
5505 if (*offset == NULL_TREE)
5506 *offset = size_int (-adjust_bytes);
5507 else
5508 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5509 *bitstart = 0;
5511 else
5512 *bitstart = *bitpos - bitoffset;
5514 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5517 /* Returns true if BASE is a DECL that does not reside in memory and
5518 has non-BLKmode. DECL_RTL must not be a MEM; if
5519 DECL_RTL was not set yet, return false. */
5521 bool
5522 non_mem_decl_p (tree base)
5524 if (!DECL_P (base)
5525 || TREE_ADDRESSABLE (base)
5526 || DECL_MODE (base) == BLKmode)
5527 return false;
5529 if (!DECL_RTL_SET_P (base))
5530 return false;
5532 return (!MEM_P (DECL_RTL (base)));
5535 /* Returns true if REF refers to an object that does not
5536 reside in memory and has non-BLKmode. */
5538 bool
5539 mem_ref_refers_to_non_mem_p (tree ref)
5541 tree base;
5543 if (TREE_CODE (ref) == MEM_REF
5544 || TREE_CODE (ref) == TARGET_MEM_REF)
5546 tree addr = TREE_OPERAND (ref, 0);
5548 if (TREE_CODE (addr) != ADDR_EXPR)
5549 return false;
5551 base = TREE_OPERAND (addr, 0);
5553 else
5554 base = ref;
5556 return non_mem_decl_p (base);
5559 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5560 is true, try generating a nontemporal store. */
5562 void
5563 expand_assignment (tree to, tree from, bool nontemporal)
5565 rtx to_rtx = 0;
5566 rtx result;
5567 machine_mode mode;
5568 unsigned int align;
5569 enum insn_code icode;
5571 /* Don't crash if the lhs of the assignment was erroneous. */
5572 if (TREE_CODE (to) == ERROR_MARK)
5574 expand_normal (from);
5575 return;
5578 /* Optimize away no-op moves without side-effects. */
5579 if (operand_equal_p (to, from, 0))
5580 return;
5582 /* Handle misaligned stores. */
5583 mode = TYPE_MODE (TREE_TYPE (to));
5584 if ((TREE_CODE (to) == MEM_REF
5585 || TREE_CODE (to) == TARGET_MEM_REF
5586 || DECL_P (to))
5587 && mode != BLKmode
5588 && !mem_ref_refers_to_non_mem_p (to)
5589 && ((align = get_object_alignment (to))
5590 < GET_MODE_ALIGNMENT (mode))
5591 && (((icode = optab_handler (movmisalign_optab, mode))
5592 != CODE_FOR_nothing)
5593 || targetm.slow_unaligned_access (mode, align)))
5595 rtx reg, mem;
5597 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5598 /* Handle PARALLEL. */
5599 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5600 reg = force_not_mem (reg);
5601 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5602 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5603 reg = flip_storage_order (mode, reg);
5605 if (icode != CODE_FOR_nothing)
5607 class expand_operand ops[2];
5609 create_fixed_operand (&ops[0], mem);
5610 create_input_operand (&ops[1], reg, mode);
5611 /* The movmisalign<mode> pattern cannot fail, else the assignment
5612 would silently be omitted. */
5613 expand_insn (icode, 2, ops);
5615 else
5616 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5617 false, false);
5618 return;
5621 /* Assignment of a structure component needs special treatment
5622 if the structure component's rtx is not simply a MEM.
5623 Assignment of an array element at a constant index, and assignment of
5624 an array element in an unaligned packed structure field, has the same
5625 problem. Same for (partially) storing into a non-memory object. */
5626 if (handled_component_p (to)
5627 || (TREE_CODE (to) == MEM_REF
5628 && (REF_REVERSE_STORAGE_ORDER (to)
5629 || mem_ref_refers_to_non_mem_p (to)))
5630 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5632 machine_mode mode1;
5633 poly_int64 bitsize, bitpos;
5634 poly_uint64 bitregion_start = 0;
5635 poly_uint64 bitregion_end = 0;
5636 tree offset;
5637 int unsignedp, reversep, volatilep = 0;
5638 tree tem;
5640 push_temp_slots ();
5641 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5642 &unsignedp, &reversep, &volatilep);
5644 /* Make sure bitpos is not negative, it can wreak havoc later. */
5645 if (maybe_lt (bitpos, 0))
5647 gcc_assert (offset == NULL_TREE);
5648 offset = size_int (bits_to_bytes_round_down (bitpos));
5649 bitpos = num_trailing_bits (bitpos);
5652 if (TREE_CODE (to) == COMPONENT_REF
5653 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5654 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5655 /* The C++ memory model naturally applies to byte-aligned fields.
5656 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5657 BITSIZE are not byte-aligned, there is no need to limit the range
5658 we can access. This can occur with packed structures in Ada. */
5659 else if (maybe_gt (bitsize, 0)
5660 && multiple_p (bitsize, BITS_PER_UNIT)
5661 && multiple_p (bitpos, BITS_PER_UNIT))
5663 bitregion_start = bitpos;
5664 bitregion_end = bitpos + bitsize - 1;
5667 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5669 /* If the field has a mode, we want to access it in the
5670 field's mode, not the computed mode.
5671 If a MEM has VOIDmode (external with incomplete type),
5672 use BLKmode for it instead. */
5673 if (MEM_P (to_rtx))
5675 if (mode1 != VOIDmode)
5676 to_rtx = adjust_address (to_rtx, mode1, 0);
5677 else if (GET_MODE (to_rtx) == VOIDmode)
5678 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5681 if (offset != 0)
5683 machine_mode address_mode;
5684 rtx offset_rtx;
5686 if (!MEM_P (to_rtx))
5688 /* We can get constant negative offsets into arrays with broken
5689 user code. Translate this to a trap instead of ICEing. */
5690 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5691 expand_builtin_trap ();
5692 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5695 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5696 address_mode = get_address_mode (to_rtx);
5697 if (GET_MODE (offset_rtx) != address_mode)
5699 /* We cannot be sure that the RTL in offset_rtx is valid outside
5700 of a memory address context, so force it into a register
5701 before attempting to convert it to the desired mode. */
5702 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5703 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5706 /* If we have an expression in OFFSET_RTX and a non-zero
5707 byte offset in BITPOS, adding the byte offset before the
5708 OFFSET_RTX results in better intermediate code, which makes
5709 later rtl optimization passes perform better.
5711 We prefer intermediate code like this:
5713 r124:DI=r123:DI+0x18
5714 [r124:DI]=r121:DI
5716 ... instead of ...
5718 r124:DI=r123:DI+0x10
5719 [r124:DI+0x8]=r121:DI
5721 This is only done for aligned data values, as these can
5722 be expected to result in single move instructions. */
5723 poly_int64 bytepos;
5724 if (mode1 != VOIDmode
5725 && maybe_ne (bitpos, 0)
5726 && maybe_gt (bitsize, 0)
5727 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5728 && multiple_p (bitpos, bitsize)
5729 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5730 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5732 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5733 bitregion_start = 0;
5734 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5735 bitregion_end -= bitpos;
5736 bitpos = 0;
5739 to_rtx = offset_address (to_rtx, offset_rtx,
5740 highest_pow2_factor_for_target (to,
5741 offset));
5744 /* No action is needed if the target is not a memory and the field
5745 lies completely outside that target. This can occur if the source
5746 code contains an out-of-bounds access to a small array. */
5747 if (!MEM_P (to_rtx)
5748 && GET_MODE (to_rtx) != BLKmode
5749 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5751 expand_normal (from);
5752 result = NULL;
5754 /* Handle expand_expr of a complex value returning a CONCAT. */
5755 else if (GET_CODE (to_rtx) == CONCAT)
5757 machine_mode to_mode = GET_MODE (to_rtx);
5758 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5759 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5760 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5761 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5762 && known_eq (bitpos, 0)
5763 && known_eq (bitsize, mode_bitsize))
5764 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5765 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5766 && known_eq (bitsize, inner_bitsize)
5767 && (known_eq (bitpos, 0)
5768 || known_eq (bitpos, inner_bitsize)))
5769 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5770 false, nontemporal, reversep);
5771 else if (known_le (bitpos + bitsize, inner_bitsize))
5772 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5773 bitregion_start, bitregion_end,
5774 mode1, from, get_alias_set (to),
5775 nontemporal, reversep);
5776 else if (known_ge (bitpos, inner_bitsize))
5777 result = store_field (XEXP (to_rtx, 1), bitsize,
5778 bitpos - inner_bitsize,
5779 bitregion_start, bitregion_end,
5780 mode1, from, get_alias_set (to),
5781 nontemporal, reversep);
5782 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5784 result = expand_normal (from);
5785 if (GET_CODE (result) == CONCAT)
5787 to_mode = GET_MODE_INNER (to_mode);
5788 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5789 rtx from_real
5790 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5791 from_mode, 0);
5792 rtx from_imag
5793 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5794 from_mode, 0);
5795 if (!from_real || !from_imag)
5796 goto concat_store_slow;
5797 emit_move_insn (XEXP (to_rtx, 0), from_real);
5798 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5800 else
5802 machine_mode from_mode
5803 = GET_MODE (result) == VOIDmode
5804 ? TYPE_MODE (TREE_TYPE (from))
5805 : GET_MODE (result);
5806 rtx from_rtx;
5807 if (MEM_P (result))
5808 from_rtx = change_address (result, to_mode, NULL_RTX);
5809 else
5810 from_rtx
5811 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5812 if (from_rtx)
5814 emit_move_insn (XEXP (to_rtx, 0),
5815 read_complex_part (from_rtx, false));
5816 emit_move_insn (XEXP (to_rtx, 1),
5817 read_complex_part (from_rtx, true));
5819 else
5821 to_mode = GET_MODE_INNER (to_mode);
5822 rtx from_real
5823 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5824 rtx from_imag
5825 = simplify_gen_subreg (to_mode, result, from_mode,
5826 GET_MODE_SIZE (to_mode));
5827 if (!from_real || !from_imag)
5828 goto concat_store_slow;
5829 emit_move_insn (XEXP (to_rtx, 0), from_real);
5830 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5834 else
5836 concat_store_slow:;
5837 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5838 GET_MODE_SIZE (GET_MODE (to_rtx)));
5839 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
5840 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
5841 result = store_field (temp, bitsize, bitpos,
5842 bitregion_start, bitregion_end,
5843 mode1, from, get_alias_set (to),
5844 nontemporal, reversep);
5845 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5846 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5849 /* For calls to functions returning variable length structures, if TO_RTX
5850 is not a MEM, go through a MEM because we must not create temporaries
5851 of the VLA type. */
5852 else if (!MEM_P (to_rtx)
5853 && TREE_CODE (from) == CALL_EXPR
5854 && COMPLETE_TYPE_P (TREE_TYPE (from))
5855 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5857 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5858 GET_MODE_SIZE (GET_MODE (to_rtx)));
5859 result = store_field (temp, bitsize, bitpos, bitregion_start,
5860 bitregion_end, mode1, from, get_alias_set (to),
5861 nontemporal, reversep);
5862 emit_move_insn (to_rtx, temp);
5864 else
5866 if (MEM_P (to_rtx))
5868 /* If the field is at offset zero, we could have been given the
5869 DECL_RTX of the parent struct. Don't munge it. */
5870 to_rtx = shallow_copy_rtx (to_rtx);
5871 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5872 if (volatilep)
5873 MEM_VOLATILE_P (to_rtx) = 1;
5876 gcc_checking_assert (known_ge (bitpos, 0));
5877 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5878 bitregion_start, bitregion_end,
5879 mode1, to_rtx, to, from,
5880 reversep))
5881 result = NULL;
5882 else if (SUBREG_P (to_rtx)
5883 && SUBREG_PROMOTED_VAR_P (to_rtx))
5885 /* If to_rtx is a promoted subreg, we need to zero or sign
5886 extend the value afterwards. */
5887 if (TREE_CODE (to) == MEM_REF
5888 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5889 && !REF_REVERSE_STORAGE_ORDER (to)
5890 && known_eq (bitpos, 0)
5891 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5892 result = store_expr (from, to_rtx, 0, nontemporal, false);
5893 else
5895 rtx to_rtx1
5896 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5897 SUBREG_REG (to_rtx),
5898 subreg_promoted_mode (to_rtx));
5899 result = store_field (to_rtx1, bitsize, bitpos,
5900 bitregion_start, bitregion_end,
5901 mode1, from, get_alias_set (to),
5902 nontemporal, reversep);
5903 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5904 SUBREG_PROMOTED_SIGN (to_rtx));
5907 else
5908 result = store_field (to_rtx, bitsize, bitpos,
5909 bitregion_start, bitregion_end,
5910 mode1, from, get_alias_set (to),
5911 nontemporal, reversep);
5914 if (result)
5915 preserve_temp_slots (result);
5916 pop_temp_slots ();
5917 return;
5920 /* If the rhs is a function call and its value is not an aggregate,
5921 call the function before we start to compute the lhs.
5922 This is needed for correct code for cases such as
5923 val = setjmp (buf) on machines where reference to val
5924 requires loading up part of an address in a separate insn.
5926 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5927 since it might be a promoted variable where the zero- or sign- extension
5928 needs to be done. Handling this in the normal way is safe because no
5929 computation is done before the call. The same is true for SSA names. */
5930 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5931 && COMPLETE_TYPE_P (TREE_TYPE (from))
5932 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5933 && ! (((VAR_P (to)
5934 || TREE_CODE (to) == PARM_DECL
5935 || TREE_CODE (to) == RESULT_DECL)
5936 && REG_P (DECL_RTL (to)))
5937 || TREE_CODE (to) == SSA_NAME))
5939 rtx value;
5941 push_temp_slots ();
5942 value = expand_normal (from);
5944 if (to_rtx == 0)
5945 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5947 /* Handle calls that return values in multiple non-contiguous locations.
5948 The Irix 6 ABI has examples of this. */
5949 if (GET_CODE (to_rtx) == PARALLEL)
5951 if (GET_CODE (value) == PARALLEL)
5952 emit_group_move (to_rtx, value);
5953 else
5954 emit_group_load (to_rtx, value, TREE_TYPE (from),
5955 int_size_in_bytes (TREE_TYPE (from)));
5957 else if (GET_CODE (value) == PARALLEL)
5958 emit_group_store (to_rtx, value, TREE_TYPE (from),
5959 int_size_in_bytes (TREE_TYPE (from)));
5960 else if (GET_MODE (to_rtx) == BLKmode)
5962 /* Handle calls that return BLKmode values in registers. */
5963 if (REG_P (value))
5964 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5965 else
5966 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5968 else
5970 if (POINTER_TYPE_P (TREE_TYPE (to)))
5971 value = convert_memory_address_addr_space
5972 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5973 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5975 emit_move_insn (to_rtx, value);
5978 preserve_temp_slots (to_rtx);
5979 pop_temp_slots ();
5980 return;
5983 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5984 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5986 /* Don't move directly into a return register. */
5987 if (TREE_CODE (to) == RESULT_DECL
5988 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5990 rtx temp;
5992 push_temp_slots ();
5994 /* If the source is itself a return value, it still is in a pseudo at
5995 this point so we can move it back to the return register directly. */
5996 if (REG_P (to_rtx)
5997 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5998 && TREE_CODE (from) != CALL_EXPR)
5999 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6000 else
6001 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6003 /* Handle calls that return values in multiple non-contiguous locations.
6004 The Irix 6 ABI has examples of this. */
6005 if (GET_CODE (to_rtx) == PARALLEL)
6007 if (GET_CODE (temp) == PARALLEL)
6008 emit_group_move (to_rtx, temp);
6009 else
6010 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6011 int_size_in_bytes (TREE_TYPE (from)));
6013 else if (temp)
6014 emit_move_insn (to_rtx, temp);
6016 preserve_temp_slots (to_rtx);
6017 pop_temp_slots ();
6018 return;
6021 /* In case we are returning the contents of an object which overlaps
6022 the place the value is being stored, use a safe function when copying
6023 a value through a pointer into a structure value return block. */
6024 if (TREE_CODE (to) == RESULT_DECL
6025 && TREE_CODE (from) == INDIRECT_REF
6026 && ADDR_SPACE_GENERIC_P
6027 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6028 && refs_may_alias_p (to, from)
6029 && cfun->returns_struct
6030 && !cfun->returns_pcc_struct)
6032 rtx from_rtx, size;
6034 push_temp_slots ();
6035 size = expr_size (from);
6036 from_rtx = expand_normal (from);
6038 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6040 preserve_temp_slots (to_rtx);
6041 pop_temp_slots ();
6042 return;
6045 /* Compute FROM and store the value in the rtx we got. */
6047 push_temp_slots ();
6048 result = store_expr (from, to_rtx, 0, nontemporal, false);
6049 preserve_temp_slots (result);
6050 pop_temp_slots ();
6051 return;
6054 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6055 succeeded, false otherwise. */
6057 bool
6058 emit_storent_insn (rtx to, rtx from)
6060 class expand_operand ops[2];
6061 machine_mode mode = GET_MODE (to);
6062 enum insn_code code = optab_handler (storent_optab, mode);
6064 if (code == CODE_FOR_nothing)
6065 return false;
6067 create_fixed_operand (&ops[0], to);
6068 create_input_operand (&ops[1], from, mode);
6069 return maybe_expand_insn (code, 2, ops);
6072 /* Helper function for store_expr storing of STRING_CST. */
6074 static rtx
6075 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6076 fixed_size_mode mode)
6078 tree str = (tree) data;
6080 gcc_assert (offset >= 0);
6081 if (offset >= TREE_STRING_LENGTH (str))
6082 return const0_rtx;
6084 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6085 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6087 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6088 size_t l = TREE_STRING_LENGTH (str) - offset;
6089 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6090 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6091 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
6094 /* The by-pieces infrastructure does not try to pick a vector mode
6095 for storing STRING_CST. */
6096 return c_readstr (TREE_STRING_POINTER (str) + offset,
6097 as_a <scalar_int_mode> (mode), false);
6100 /* Generate code for computing expression EXP,
6101 and storing the value into TARGET.
6103 If the mode is BLKmode then we may return TARGET itself.
6104 It turns out that in BLKmode it doesn't cause a problem.
6105 because C has no operators that could combine two different
6106 assignments into the same BLKmode object with different values
6107 with no sequence point. Will other languages need this to
6108 be more thorough?
6110 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6111 stack, and block moves may need to be treated specially.
6113 If NONTEMPORAL is true, try using a nontemporal store instruction.
6115 If REVERSE is true, the store is to be done in reverse order. */
6118 store_expr (tree exp, rtx target, int call_param_p,
6119 bool nontemporal, bool reverse)
6121 rtx temp;
6122 rtx alt_rtl = NULL_RTX;
6123 location_t loc = curr_insn_location ();
6124 bool shortened_string_cst = false;
6126 if (VOID_TYPE_P (TREE_TYPE (exp)))
6128 /* C++ can generate ?: expressions with a throw expression in one
6129 branch and an rvalue in the other. Here, we resolve attempts to
6130 store the throw expression's nonexistent result. */
6131 gcc_assert (!call_param_p);
6132 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6133 return NULL_RTX;
6135 if (TREE_CODE (exp) == COMPOUND_EXPR)
6137 /* Perform first part of compound expression, then assign from second
6138 part. */
6139 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6140 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6141 return store_expr (TREE_OPERAND (exp, 1), target,
6142 call_param_p, nontemporal, reverse);
6144 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6146 /* For conditional expression, get safe form of the target. Then
6147 test the condition, doing the appropriate assignment on either
6148 side. This avoids the creation of unnecessary temporaries.
6149 For non-BLKmode, it is more efficient not to do this. */
6151 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6153 do_pending_stack_adjust ();
6154 NO_DEFER_POP;
6155 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6156 profile_probability::uninitialized ());
6157 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6158 nontemporal, reverse);
6159 emit_jump_insn (targetm.gen_jump (lab2));
6160 emit_barrier ();
6161 emit_label (lab1);
6162 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6163 nontemporal, reverse);
6164 emit_label (lab2);
6165 OK_DEFER_POP;
6167 return NULL_RTX;
6169 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6170 /* If this is a scalar in a register that is stored in a wider mode
6171 than the declared mode, compute the result into its declared mode
6172 and then convert to the wider mode. Our value is the computed
6173 expression. */
6175 rtx inner_target = 0;
6176 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6177 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6179 /* We can do the conversion inside EXP, which will often result
6180 in some optimizations. Do the conversion in two steps: first
6181 change the signedness, if needed, then the extend. But don't
6182 do this if the type of EXP is a subtype of something else
6183 since then the conversion might involve more than just
6184 converting modes. */
6185 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6186 && TREE_TYPE (TREE_TYPE (exp)) == 0
6187 && GET_MODE_PRECISION (outer_mode)
6188 == TYPE_PRECISION (TREE_TYPE (exp)))
6190 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6191 TYPE_UNSIGNED (TREE_TYPE (exp))))
6193 /* Some types, e.g. Fortran's logical*4, won't have a signed
6194 version, so use the mode instead. */
6195 tree ntype
6196 = (signed_or_unsigned_type_for
6197 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6198 if (ntype == NULL)
6199 ntype = lang_hooks.types.type_for_mode
6200 (TYPE_MODE (TREE_TYPE (exp)),
6201 SUBREG_PROMOTED_SIGN (target));
6203 exp = fold_convert_loc (loc, ntype, exp);
6206 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6207 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6208 exp);
6210 inner_target = SUBREG_REG (target);
6213 temp = expand_expr (exp, inner_target, VOIDmode,
6214 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6217 /* If TEMP is a VOIDmode constant, use convert_modes to make
6218 sure that we properly convert it. */
6219 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6221 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6222 temp, SUBREG_PROMOTED_SIGN (target));
6223 temp = convert_modes (inner_mode, outer_mode, temp,
6224 SUBREG_PROMOTED_SIGN (target));
6226 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6227 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6228 temp, SUBREG_PROMOTED_SIGN (target));
6230 convert_move (SUBREG_REG (target), temp,
6231 SUBREG_PROMOTED_SIGN (target));
6233 return NULL_RTX;
6235 else if ((TREE_CODE (exp) == STRING_CST
6236 || (TREE_CODE (exp) == MEM_REF
6237 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6238 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6239 == STRING_CST
6240 && integer_zerop (TREE_OPERAND (exp, 1))))
6241 && !nontemporal && !call_param_p
6242 && MEM_P (target))
6244 /* Optimize initialization of an array with a STRING_CST. */
6245 HOST_WIDE_INT exp_len, str_copy_len;
6246 rtx dest_mem;
6247 tree str = TREE_CODE (exp) == STRING_CST
6248 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6250 exp_len = int_expr_size (exp);
6251 if (exp_len <= 0)
6252 goto normal_expr;
6254 if (TREE_STRING_LENGTH (str) <= 0)
6255 goto normal_expr;
6257 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6258 MEM_ALIGN (target), false))
6260 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6261 MEM_ALIGN (target), false, RETURN_BEGIN);
6262 return NULL_RTX;
6265 str_copy_len = TREE_STRING_LENGTH (str);
6267 /* Trailing NUL bytes in EXP will be handled by the call to
6268 clear_storage, which is more efficient than copying them from
6269 the STRING_CST, so trim those from STR_COPY_LEN. */
6270 while (str_copy_len)
6272 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6273 break;
6274 str_copy_len--;
6277 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6279 str_copy_len += STORE_MAX_PIECES - 1;
6280 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6282 if (str_copy_len >= exp_len)
6283 goto normal_expr;
6285 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6286 (void *) str, MEM_ALIGN (target), false))
6287 goto normal_expr;
6289 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6290 (void *) str, MEM_ALIGN (target), false,
6291 RETURN_END);
6292 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6293 exp_len - str_copy_len),
6294 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6295 return NULL_RTX;
6297 else
6299 rtx tmp_target;
6301 normal_expr:
6302 /* If we want to use a nontemporal or a reverse order store, force the
6303 value into a register first. */
6304 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6305 tree rexp = exp;
6306 if (TREE_CODE (exp) == STRING_CST
6307 && tmp_target == target
6308 && GET_MODE (target) == BLKmode
6309 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6311 rtx size = expr_size (exp);
6312 if (CONST_INT_P (size)
6313 && size != const0_rtx
6314 && (UINTVAL (size)
6315 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6317 /* If the STRING_CST has much larger array type than
6318 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6319 it into the rodata section as the code later on will use
6320 memset zero for the remainder anyway. See PR95052. */
6321 tmp_target = NULL_RTX;
6322 rexp = copy_node (exp);
6323 tree index
6324 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6325 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6326 index);
6327 shortened_string_cst = true;
6330 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6331 (call_param_p
6332 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6333 &alt_rtl, false);
6334 if (shortened_string_cst)
6336 gcc_assert (MEM_P (temp));
6337 temp = change_address (temp, BLKmode, NULL_RTX);
6341 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6342 the same as that of TARGET, adjust the constant. This is needed, for
6343 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6344 only a word-sized value. */
6345 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6346 && TREE_CODE (exp) != ERROR_MARK
6347 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6349 gcc_assert (!shortened_string_cst);
6350 if (GET_MODE_CLASS (GET_MODE (target))
6351 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6352 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6353 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6355 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6356 TYPE_MODE (TREE_TYPE (exp)), 0);
6357 if (t)
6358 temp = t;
6360 if (GET_MODE (temp) == VOIDmode)
6361 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6362 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6365 /* If value was not generated in the target, store it there.
6366 Convert the value to TARGET's type first if necessary and emit the
6367 pending incrementations that have been queued when expanding EXP.
6368 Note that we cannot emit the whole queue blindly because this will
6369 effectively disable the POST_INC optimization later.
6371 If TEMP and TARGET compare equal according to rtx_equal_p, but
6372 one or both of them are volatile memory refs, we have to distinguish
6373 two cases:
6374 - expand_expr has used TARGET. In this case, we must not generate
6375 another copy. This can be detected by TARGET being equal according
6376 to == .
6377 - expand_expr has not used TARGET - that means that the source just
6378 happens to have the same RTX form. Since temp will have been created
6379 by expand_expr, it will compare unequal according to == .
6380 We must generate a copy in this case, to reach the correct number
6381 of volatile memory references. */
6383 if ((! rtx_equal_p (temp, target)
6384 || (temp != target && (side_effects_p (temp)
6385 || side_effects_p (target)
6386 || (MEM_P (temp)
6387 && !mems_same_for_tbaa_p (temp, target)))))
6388 && TREE_CODE (exp) != ERROR_MARK
6389 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6390 but TARGET is not valid memory reference, TEMP will differ
6391 from TARGET although it is really the same location. */
6392 && !(alt_rtl
6393 && rtx_equal_p (alt_rtl, target)
6394 && !side_effects_p (alt_rtl)
6395 && !side_effects_p (target))
6396 /* If there's nothing to copy, don't bother. Don't call
6397 expr_size unless necessary, because some front-ends (C++)
6398 expr_size-hook must not be given objects that are not
6399 supposed to be bit-copied or bit-initialized. */
6400 && expr_size (exp) != const0_rtx)
6402 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6404 gcc_assert (!shortened_string_cst);
6405 if (GET_MODE (target) == BLKmode)
6407 /* Handle calls that return BLKmode values in registers. */
6408 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6409 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6410 else
6411 store_bit_field (target,
6412 rtx_to_poly_int64 (expr_size (exp))
6413 * BITS_PER_UNIT,
6414 0, 0, 0, GET_MODE (temp), temp, reverse,
6415 false);
6417 else
6418 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6421 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6423 /* Handle copying a string constant into an array. The string
6424 constant may be shorter than the array. So copy just the string's
6425 actual length, and clear the rest. First get the size of the data
6426 type of the string, which is actually the size of the target. */
6427 rtx size = expr_size (exp);
6429 if (CONST_INT_P (size)
6430 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6431 emit_block_move (target, temp, size,
6432 (call_param_p
6433 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6434 else
6436 machine_mode pointer_mode
6437 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6438 machine_mode address_mode = get_address_mode (target);
6440 /* Compute the size of the data to copy from the string. */
6441 tree copy_size
6442 = size_binop_loc (loc, MIN_EXPR,
6443 make_tree (sizetype, size),
6444 size_int (TREE_STRING_LENGTH (exp)));
6445 rtx copy_size_rtx
6446 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6447 (call_param_p
6448 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6449 rtx_code_label *label = 0;
6451 /* Copy that much. */
6452 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6453 TYPE_UNSIGNED (sizetype));
6454 emit_block_move (target, temp, copy_size_rtx,
6455 (call_param_p
6456 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6458 /* Figure out how much is left in TARGET that we have to clear.
6459 Do all calculations in pointer_mode. */
6460 poly_int64 const_copy_size;
6461 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6463 size = plus_constant (address_mode, size, -const_copy_size);
6464 target = adjust_address (target, BLKmode, const_copy_size);
6466 else
6468 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6469 copy_size_rtx, NULL_RTX, 0,
6470 OPTAB_LIB_WIDEN);
6472 if (GET_MODE (copy_size_rtx) != address_mode)
6473 copy_size_rtx = convert_to_mode (address_mode,
6474 copy_size_rtx,
6475 TYPE_UNSIGNED (sizetype));
6477 target = offset_address (target, copy_size_rtx,
6478 highest_pow2_factor (copy_size));
6479 label = gen_label_rtx ();
6480 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6481 GET_MODE (size), 0, label);
6484 if (size != const0_rtx)
6485 clear_storage (target, size, BLOCK_OP_NORMAL);
6487 if (label)
6488 emit_label (label);
6491 else if (shortened_string_cst)
6492 gcc_unreachable ();
6493 /* Handle calls that return values in multiple non-contiguous locations.
6494 The Irix 6 ABI has examples of this. */
6495 else if (GET_CODE (target) == PARALLEL)
6497 if (GET_CODE (temp) == PARALLEL)
6498 emit_group_move (target, temp);
6499 else
6500 emit_group_load (target, temp, TREE_TYPE (exp),
6501 int_size_in_bytes (TREE_TYPE (exp)));
6503 else if (GET_CODE (temp) == PARALLEL)
6504 emit_group_store (target, temp, TREE_TYPE (exp),
6505 int_size_in_bytes (TREE_TYPE (exp)));
6506 else if (GET_MODE (temp) == BLKmode)
6507 emit_block_move (target, temp, expr_size (exp),
6508 (call_param_p
6509 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6510 /* If we emit a nontemporal store, there is nothing else to do. */
6511 else if (nontemporal && emit_storent_insn (target, temp))
6513 else
6515 if (reverse)
6516 temp = flip_storage_order (GET_MODE (target), temp);
6517 temp = force_operand (temp, target);
6518 if (temp != target)
6519 emit_move_insn (target, temp);
6522 else
6523 gcc_assert (!shortened_string_cst);
6525 return NULL_RTX;
6528 /* Return true if field F of structure TYPE is a flexible array. */
6530 static bool
6531 flexible_array_member_p (const_tree f, const_tree type)
6533 const_tree tf;
6535 tf = TREE_TYPE (f);
6536 return (DECL_CHAIN (f) == NULL
6537 && TREE_CODE (tf) == ARRAY_TYPE
6538 && TYPE_DOMAIN (tf)
6539 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6540 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6541 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6542 && int_size_in_bytes (type) >= 0);
6545 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6546 must have in order for it to completely initialize a value of type TYPE.
6547 Return -1 if the number isn't known.
6549 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6551 static HOST_WIDE_INT
6552 count_type_elements (const_tree type, bool for_ctor_p)
6554 switch (TREE_CODE (type))
6556 case ARRAY_TYPE:
6558 tree nelts;
6560 nelts = array_type_nelts (type);
6561 if (nelts && tree_fits_uhwi_p (nelts))
6563 unsigned HOST_WIDE_INT n;
6565 n = tree_to_uhwi (nelts) + 1;
6566 if (n == 0 || for_ctor_p)
6567 return n;
6568 else
6569 return n * count_type_elements (TREE_TYPE (type), false);
6571 return for_ctor_p ? -1 : 1;
6574 case RECORD_TYPE:
6576 unsigned HOST_WIDE_INT n;
6577 tree f;
6579 n = 0;
6580 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6581 if (TREE_CODE (f) == FIELD_DECL)
6583 if (!for_ctor_p)
6584 n += count_type_elements (TREE_TYPE (f), false);
6585 else if (!flexible_array_member_p (f, type))
6586 /* Don't count flexible arrays, which are not supposed
6587 to be initialized. */
6588 n += 1;
6591 return n;
6594 case UNION_TYPE:
6595 case QUAL_UNION_TYPE:
6597 tree f;
6598 HOST_WIDE_INT n, m;
6600 gcc_assert (!for_ctor_p);
6601 /* Estimate the number of scalars in each field and pick the
6602 maximum. Other estimates would do instead; the idea is simply
6603 to make sure that the estimate is not sensitive to the ordering
6604 of the fields. */
6605 n = 1;
6606 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6607 if (TREE_CODE (f) == FIELD_DECL)
6609 m = count_type_elements (TREE_TYPE (f), false);
6610 /* If the field doesn't span the whole union, add an extra
6611 scalar for the rest. */
6612 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6613 TYPE_SIZE (type)) != 1)
6614 m++;
6615 if (n < m)
6616 n = m;
6618 return n;
6621 case COMPLEX_TYPE:
6622 return 2;
6624 case VECTOR_TYPE:
6626 unsigned HOST_WIDE_INT nelts;
6627 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6628 return nelts;
6629 else
6630 return -1;
6633 case INTEGER_TYPE:
6634 case REAL_TYPE:
6635 case FIXED_POINT_TYPE:
6636 case ENUMERAL_TYPE:
6637 case BOOLEAN_TYPE:
6638 case POINTER_TYPE:
6639 case OFFSET_TYPE:
6640 case REFERENCE_TYPE:
6641 case NULLPTR_TYPE:
6642 case OPAQUE_TYPE:
6643 return 1;
6645 case ERROR_MARK:
6646 return 0;
6648 case VOID_TYPE:
6649 case METHOD_TYPE:
6650 case FUNCTION_TYPE:
6651 case LANG_TYPE:
6652 default:
6653 gcc_unreachable ();
6657 /* Helper for categorize_ctor_elements. Identical interface. */
6659 static bool
6660 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6661 HOST_WIDE_INT *p_unique_nz_elts,
6662 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6664 unsigned HOST_WIDE_INT idx;
6665 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6666 tree value, purpose, elt_type;
6668 /* Whether CTOR is a valid constant initializer, in accordance with what
6669 initializer_constant_valid_p does. If inferred from the constructor
6670 elements, true until proven otherwise. */
6671 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6672 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6674 nz_elts = 0;
6675 unique_nz_elts = 0;
6676 init_elts = 0;
6677 num_fields = 0;
6678 elt_type = NULL_TREE;
6680 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6682 HOST_WIDE_INT mult = 1;
6684 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6686 tree lo_index = TREE_OPERAND (purpose, 0);
6687 tree hi_index = TREE_OPERAND (purpose, 1);
6689 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6690 mult = (tree_to_uhwi (hi_index)
6691 - tree_to_uhwi (lo_index) + 1);
6693 num_fields += mult;
6694 elt_type = TREE_TYPE (value);
6696 switch (TREE_CODE (value))
6698 case CONSTRUCTOR:
6700 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6702 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6703 &ic, p_complete);
6705 nz_elts += mult * nz;
6706 unique_nz_elts += unz;
6707 init_elts += mult * ic;
6709 if (const_from_elts_p && const_p)
6710 const_p = const_elt_p;
6712 break;
6714 case INTEGER_CST:
6715 case REAL_CST:
6716 case FIXED_CST:
6717 if (!initializer_zerop (value))
6719 nz_elts += mult;
6720 unique_nz_elts++;
6722 init_elts += mult;
6723 break;
6725 case STRING_CST:
6726 nz_elts += mult * TREE_STRING_LENGTH (value);
6727 unique_nz_elts += TREE_STRING_LENGTH (value);
6728 init_elts += mult * TREE_STRING_LENGTH (value);
6729 break;
6731 case COMPLEX_CST:
6732 if (!initializer_zerop (TREE_REALPART (value)))
6734 nz_elts += mult;
6735 unique_nz_elts++;
6737 if (!initializer_zerop (TREE_IMAGPART (value)))
6739 nz_elts += mult;
6740 unique_nz_elts++;
6742 init_elts += 2 * mult;
6743 break;
6745 case VECTOR_CST:
6747 /* We can only construct constant-length vectors using
6748 CONSTRUCTOR. */
6749 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6750 for (unsigned int i = 0; i < nunits; ++i)
6752 tree v = VECTOR_CST_ELT (value, i);
6753 if (!initializer_zerop (v))
6755 nz_elts += mult;
6756 unique_nz_elts++;
6758 init_elts += mult;
6761 break;
6763 default:
6765 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6766 nz_elts += mult * tc;
6767 unique_nz_elts += tc;
6768 init_elts += mult * tc;
6770 if (const_from_elts_p && const_p)
6771 const_p
6772 = initializer_constant_valid_p (value,
6773 elt_type,
6774 TYPE_REVERSE_STORAGE_ORDER
6775 (TREE_TYPE (ctor)))
6776 != NULL_TREE;
6778 break;
6782 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6783 num_fields, elt_type))
6784 *p_complete = false;
6786 *p_nz_elts += nz_elts;
6787 *p_unique_nz_elts += unique_nz_elts;
6788 *p_init_elts += init_elts;
6790 return const_p;
6793 /* Examine CTOR to discover:
6794 * how many scalar fields are set to nonzero values,
6795 and place it in *P_NZ_ELTS;
6796 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6797 high - low + 1 (this can be useful for callers to determine ctors
6798 that could be cheaply initialized with - perhaps nested - loops
6799 compared to copied from huge read-only data),
6800 and place it in *P_UNIQUE_NZ_ELTS;
6801 * how many scalar fields in total are in CTOR,
6802 and place it in *P_ELT_COUNT.
6803 * whether the constructor is complete -- in the sense that every
6804 meaningful byte is explicitly given a value --
6805 and place it in *P_COMPLETE.
6807 Return whether or not CTOR is a valid static constant initializer, the same
6808 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6810 bool
6811 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6812 HOST_WIDE_INT *p_unique_nz_elts,
6813 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6815 *p_nz_elts = 0;
6816 *p_unique_nz_elts = 0;
6817 *p_init_elts = 0;
6818 *p_complete = true;
6820 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6821 p_init_elts, p_complete);
6824 /* Return true if constructor CTOR is simple enough to be materialized
6825 in an integer mode register. Limit the size to WORDS words, which
6826 is 1 by default. */
6828 bool
6829 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6831 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6832 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6833 return false;
6835 return TREE_CONSTANT (ctor)
6836 && !TREE_ADDRESSABLE (ctor)
6837 && CONSTRUCTOR_NELTS (ctor)
6838 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6839 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6840 && initializer_constant_valid_for_bitfield_p (ctor);
6843 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6844 of which had type LAST_TYPE. Each element was itself a complete
6845 initializer, in the sense that every meaningful byte was explicitly
6846 given a value. Return true if the same is true for the constructor
6847 as a whole. */
6849 bool
6850 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6851 const_tree last_type)
6853 if (TREE_CODE (type) == UNION_TYPE
6854 || TREE_CODE (type) == QUAL_UNION_TYPE)
6856 if (num_elts == 0)
6857 return false;
6859 gcc_assert (num_elts == 1 && last_type);
6861 /* ??? We could look at each element of the union, and find the
6862 largest element. Which would avoid comparing the size of the
6863 initialized element against any tail padding in the union.
6864 Doesn't seem worth the effort... */
6865 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6868 return count_type_elements (type, true) == num_elts;
6871 /* Return true if EXP contains mostly (3/4) zeros. */
6873 static bool
6874 mostly_zeros_p (const_tree exp)
6876 if (TREE_CODE (exp) == CONSTRUCTOR)
6878 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6879 bool complete_p;
6881 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6882 &complete_p);
6883 return !complete_p || nz_elts < init_elts / 4;
6886 return initializer_zerop (exp);
6889 /* Return true if EXP contains all zeros. */
6891 static bool
6892 all_zeros_p (const_tree exp)
6894 if (TREE_CODE (exp) == CONSTRUCTOR)
6896 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6897 bool complete_p;
6899 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6900 &complete_p);
6901 return nz_elts == 0;
6904 return initializer_zerop (exp);
6907 /* Helper function for store_constructor.
6908 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6909 CLEARED is as for store_constructor.
6910 ALIAS_SET is the alias set to use for any stores.
6911 If REVERSE is true, the store is to be done in reverse order.
6913 This provides a recursive shortcut back to store_constructor when it isn't
6914 necessary to go through store_field. This is so that we can pass through
6915 the cleared field to let store_constructor know that we may not have to
6916 clear a substructure if the outer structure has already been cleared. */
6918 static void
6919 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6920 poly_uint64 bitregion_start,
6921 poly_uint64 bitregion_end,
6922 machine_mode mode,
6923 tree exp, int cleared,
6924 alias_set_type alias_set, bool reverse)
6926 poly_int64 bytepos;
6927 poly_uint64 bytesize;
6928 if (TREE_CODE (exp) == CONSTRUCTOR
6929 /* We can only call store_constructor recursively if the size and
6930 bit position are on a byte boundary. */
6931 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6932 && maybe_ne (bitsize, 0U)
6933 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6934 /* If we have a nonzero bitpos for a register target, then we just
6935 let store_field do the bitfield handling. This is unlikely to
6936 generate unnecessary clear instructions anyways. */
6937 && (known_eq (bitpos, 0) || MEM_P (target)))
6939 if (MEM_P (target))
6941 machine_mode target_mode = GET_MODE (target);
6942 if (target_mode != BLKmode
6943 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6944 target_mode = BLKmode;
6945 target = adjust_address (target, target_mode, bytepos);
6949 /* Update the alias set, if required. */
6950 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6951 && MEM_ALIAS_SET (target) != 0)
6953 target = copy_rtx (target);
6954 set_mem_alias_set (target, alias_set);
6957 store_constructor (exp, target, cleared, bytesize, reverse);
6959 else
6960 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6961 exp, alias_set, false, reverse);
6965 /* Returns the number of FIELD_DECLs in TYPE. */
6967 static int
6968 fields_length (const_tree type)
6970 tree t = TYPE_FIELDS (type);
6971 int count = 0;
6973 for (; t; t = DECL_CHAIN (t))
6974 if (TREE_CODE (t) == FIELD_DECL)
6975 ++count;
6977 return count;
6981 /* Store the value of constructor EXP into the rtx TARGET.
6982 TARGET is either a REG or a MEM; we know it cannot conflict, since
6983 safe_from_p has been called.
6984 CLEARED is true if TARGET is known to have been zero'd.
6985 SIZE is the number of bytes of TARGET we are allowed to modify: this
6986 may not be the same as the size of EXP if we are assigning to a field
6987 which has been packed to exclude padding bits.
6988 If REVERSE is true, the store is to be done in reverse order. */
6990 void
6991 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6992 bool reverse)
6994 tree type = TREE_TYPE (exp);
6995 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6996 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6998 switch (TREE_CODE (type))
7000 case RECORD_TYPE:
7001 case UNION_TYPE:
7002 case QUAL_UNION_TYPE:
7004 unsigned HOST_WIDE_INT idx;
7005 tree field, value;
7007 /* The storage order is specified for every aggregate type. */
7008 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7010 /* If size is zero or the target is already cleared, do nothing. */
7011 if (known_eq (size, 0) || cleared)
7012 cleared = 1;
7013 /* We either clear the aggregate or indicate the value is dead. */
7014 else if ((TREE_CODE (type) == UNION_TYPE
7015 || TREE_CODE (type) == QUAL_UNION_TYPE)
7016 && ! CONSTRUCTOR_ELTS (exp))
7017 /* If the constructor is empty, clear the union. */
7019 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7020 cleared = 1;
7023 /* If we are building a static constructor into a register,
7024 set the initial value as zero so we can fold the value into
7025 a constant. But if more than one register is involved,
7026 this probably loses. */
7027 else if (REG_P (target) && TREE_STATIC (exp)
7028 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7029 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7031 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7032 cleared = 1;
7035 /* If the constructor has fewer fields than the structure or
7036 if we are initializing the structure to mostly zeros, clear
7037 the whole structure first. Don't do this if TARGET is a
7038 register whose mode size isn't equal to SIZE since
7039 clear_storage can't handle this case. */
7040 else if (known_size_p (size)
7041 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7042 || mostly_zeros_p (exp))
7043 && (!REG_P (target)
7044 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7046 clear_storage (target, gen_int_mode (size, Pmode),
7047 BLOCK_OP_NORMAL);
7048 cleared = 1;
7051 if (REG_P (target) && !cleared)
7052 emit_clobber (target);
7054 /* Store each element of the constructor into the
7055 corresponding field of TARGET. */
7056 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7058 machine_mode mode;
7059 HOST_WIDE_INT bitsize;
7060 HOST_WIDE_INT bitpos = 0;
7061 tree offset;
7062 rtx to_rtx = target;
7064 /* Just ignore missing fields. We cleared the whole
7065 structure, above, if any fields are missing. */
7066 if (field == 0)
7067 continue;
7069 if (cleared && initializer_zerop (value))
7070 continue;
7072 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7073 bitsize = tree_to_uhwi (DECL_SIZE (field));
7074 else
7075 gcc_unreachable ();
7077 mode = DECL_MODE (field);
7078 if (DECL_BIT_FIELD (field))
7079 mode = VOIDmode;
7081 offset = DECL_FIELD_OFFSET (field);
7082 if (tree_fits_shwi_p (offset)
7083 && tree_fits_shwi_p (bit_position (field)))
7085 bitpos = int_bit_position (field);
7086 offset = NULL_TREE;
7088 else
7089 gcc_unreachable ();
7091 /* If this initializes a field that is smaller than a
7092 word, at the start of a word, try to widen it to a full
7093 word. This special case allows us to output C++ member
7094 function initializations in a form that the optimizers
7095 can understand. */
7096 if (WORD_REGISTER_OPERATIONS
7097 && REG_P (target)
7098 && bitsize < BITS_PER_WORD
7099 && bitpos % BITS_PER_WORD == 0
7100 && GET_MODE_CLASS (mode) == MODE_INT
7101 && TREE_CODE (value) == INTEGER_CST
7102 && exp_size >= 0
7103 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7105 type = TREE_TYPE (value);
7107 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7109 type = lang_hooks.types.type_for_mode
7110 (word_mode, TYPE_UNSIGNED (type));
7111 value = fold_convert (type, value);
7112 /* Make sure the bits beyond the original bitsize are zero
7113 so that we can correctly avoid extra zeroing stores in
7114 later constructor elements. */
7115 tree bitsize_mask
7116 = wide_int_to_tree (type, wi::mask (bitsize, false,
7117 BITS_PER_WORD));
7118 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7121 if (BYTES_BIG_ENDIAN)
7122 value
7123 = fold_build2 (LSHIFT_EXPR, type, value,
7124 build_int_cst (type,
7125 BITS_PER_WORD - bitsize));
7126 bitsize = BITS_PER_WORD;
7127 mode = word_mode;
7130 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7131 && DECL_NONADDRESSABLE_P (field))
7133 to_rtx = copy_rtx (to_rtx);
7134 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7137 store_constructor_field (to_rtx, bitsize, bitpos,
7138 0, bitregion_end, mode,
7139 value, cleared,
7140 get_alias_set (TREE_TYPE (field)),
7141 reverse);
7143 break;
7145 case ARRAY_TYPE:
7147 tree value, index;
7148 unsigned HOST_WIDE_INT i;
7149 bool need_to_clear;
7150 tree domain;
7151 tree elttype = TREE_TYPE (type);
7152 bool const_bounds_p;
7153 HOST_WIDE_INT minelt = 0;
7154 HOST_WIDE_INT maxelt = 0;
7156 /* The storage order is specified for every aggregate type. */
7157 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7159 domain = TYPE_DOMAIN (type);
7160 const_bounds_p = (TYPE_MIN_VALUE (domain)
7161 && TYPE_MAX_VALUE (domain)
7162 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7163 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7165 /* If we have constant bounds for the range of the type, get them. */
7166 if (const_bounds_p)
7168 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7169 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7172 /* If the constructor has fewer elements than the array, clear
7173 the whole array first. Similarly if this is static
7174 constructor of a non-BLKmode object. */
7175 if (cleared)
7176 need_to_clear = false;
7177 else if (REG_P (target) && TREE_STATIC (exp))
7178 need_to_clear = true;
7179 else
7181 unsigned HOST_WIDE_INT idx;
7182 HOST_WIDE_INT count = 0, zero_count = 0;
7183 need_to_clear = ! const_bounds_p;
7185 /* This loop is a more accurate version of the loop in
7186 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7187 is also needed to check for missing elements. */
7188 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7190 HOST_WIDE_INT this_node_count;
7192 if (need_to_clear)
7193 break;
7195 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7197 tree lo_index = TREE_OPERAND (index, 0);
7198 tree hi_index = TREE_OPERAND (index, 1);
7200 if (! tree_fits_uhwi_p (lo_index)
7201 || ! tree_fits_uhwi_p (hi_index))
7203 need_to_clear = true;
7204 break;
7207 this_node_count = (tree_to_uhwi (hi_index)
7208 - tree_to_uhwi (lo_index) + 1);
7210 else
7211 this_node_count = 1;
7213 count += this_node_count;
7214 if (mostly_zeros_p (value))
7215 zero_count += this_node_count;
7218 /* Clear the entire array first if there are any missing
7219 elements, or if the incidence of zero elements is >=
7220 75%. */
7221 if (! need_to_clear
7222 && (count < maxelt - minelt + 1
7223 || 4 * zero_count >= 3 * count))
7224 need_to_clear = true;
7227 if (need_to_clear && maybe_gt (size, 0))
7229 if (REG_P (target))
7230 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7231 else
7232 clear_storage (target, gen_int_mode (size, Pmode),
7233 BLOCK_OP_NORMAL);
7234 cleared = 1;
7237 if (!cleared && REG_P (target))
7238 /* Inform later passes that the old value is dead. */
7239 emit_clobber (target);
7241 /* Store each element of the constructor into the
7242 corresponding element of TARGET, determined by counting the
7243 elements. */
7244 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7246 machine_mode mode;
7247 poly_int64 bitsize;
7248 HOST_WIDE_INT bitpos;
7249 rtx xtarget = target;
7251 if (cleared && initializer_zerop (value))
7252 continue;
7254 mode = TYPE_MODE (elttype);
7255 if (mode != BLKmode)
7256 bitsize = GET_MODE_BITSIZE (mode);
7257 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7258 bitsize = -1;
7260 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7262 tree lo_index = TREE_OPERAND (index, 0);
7263 tree hi_index = TREE_OPERAND (index, 1);
7264 rtx index_r, pos_rtx;
7265 HOST_WIDE_INT lo, hi, count;
7266 tree position;
7268 /* If the range is constant and "small", unroll the loop. */
7269 if (const_bounds_p
7270 && tree_fits_shwi_p (lo_index)
7271 && tree_fits_shwi_p (hi_index)
7272 && (lo = tree_to_shwi (lo_index),
7273 hi = tree_to_shwi (hi_index),
7274 count = hi - lo + 1,
7275 (!MEM_P (target)
7276 || count <= 2
7277 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7278 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7279 <= 40 * 8)))))
7281 lo -= minelt; hi -= minelt;
7282 for (; lo <= hi; lo++)
7284 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7286 if (MEM_P (target)
7287 && !MEM_KEEP_ALIAS_SET_P (target)
7288 && TREE_CODE (type) == ARRAY_TYPE
7289 && TYPE_NONALIASED_COMPONENT (type))
7291 target = copy_rtx (target);
7292 MEM_KEEP_ALIAS_SET_P (target) = 1;
7295 store_constructor_field
7296 (target, bitsize, bitpos, 0, bitregion_end,
7297 mode, value, cleared,
7298 get_alias_set (elttype), reverse);
7301 else
7303 rtx_code_label *loop_start = gen_label_rtx ();
7304 rtx_code_label *loop_end = gen_label_rtx ();
7305 tree exit_cond;
7307 expand_normal (hi_index);
7309 index = build_decl (EXPR_LOCATION (exp),
7310 VAR_DECL, NULL_TREE, domain);
7311 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7312 SET_DECL_RTL (index, index_r);
7313 store_expr (lo_index, index_r, 0, false, reverse);
7315 /* Build the head of the loop. */
7316 do_pending_stack_adjust ();
7317 emit_label (loop_start);
7319 /* Assign value to element index. */
7320 position =
7321 fold_convert (ssizetype,
7322 fold_build2 (MINUS_EXPR,
7323 TREE_TYPE (index),
7324 index,
7325 TYPE_MIN_VALUE (domain)));
7327 position =
7328 size_binop (MULT_EXPR, position,
7329 fold_convert (ssizetype,
7330 TYPE_SIZE_UNIT (elttype)));
7332 pos_rtx = expand_normal (position);
7333 xtarget = offset_address (target, pos_rtx,
7334 highest_pow2_factor (position));
7335 xtarget = adjust_address (xtarget, mode, 0);
7336 if (TREE_CODE (value) == CONSTRUCTOR)
7337 store_constructor (value, xtarget, cleared,
7338 exact_div (bitsize, BITS_PER_UNIT),
7339 reverse);
7340 else
7341 store_expr (value, xtarget, 0, false, reverse);
7343 /* Generate a conditional jump to exit the loop. */
7344 exit_cond = build2 (LT_EXPR, integer_type_node,
7345 index, hi_index);
7346 jumpif (exit_cond, loop_end,
7347 profile_probability::uninitialized ());
7349 /* Update the loop counter, and jump to the head of
7350 the loop. */
7351 expand_assignment (index,
7352 build2 (PLUS_EXPR, TREE_TYPE (index),
7353 index, integer_one_node),
7354 false);
7356 emit_jump (loop_start);
7358 /* Build the end of the loop. */
7359 emit_label (loop_end);
7362 else if ((index != 0 && ! tree_fits_shwi_p (index))
7363 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7365 tree position;
7367 if (index == 0)
7368 index = ssize_int (1);
7370 if (minelt)
7371 index = fold_convert (ssizetype,
7372 fold_build2 (MINUS_EXPR,
7373 TREE_TYPE (index),
7374 index,
7375 TYPE_MIN_VALUE (domain)));
7377 position =
7378 size_binop (MULT_EXPR, index,
7379 fold_convert (ssizetype,
7380 TYPE_SIZE_UNIT (elttype)));
7381 xtarget = offset_address (target,
7382 expand_normal (position),
7383 highest_pow2_factor (position));
7384 xtarget = adjust_address (xtarget, mode, 0);
7385 store_expr (value, xtarget, 0, false, reverse);
7387 else
7389 if (index != 0)
7390 bitpos = ((tree_to_shwi (index) - minelt)
7391 * tree_to_uhwi (TYPE_SIZE (elttype)));
7392 else
7393 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7395 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7396 && TREE_CODE (type) == ARRAY_TYPE
7397 && TYPE_NONALIASED_COMPONENT (type))
7399 target = copy_rtx (target);
7400 MEM_KEEP_ALIAS_SET_P (target) = 1;
7402 store_constructor_field (target, bitsize, bitpos, 0,
7403 bitregion_end, mode, value,
7404 cleared, get_alias_set (elttype),
7405 reverse);
7408 break;
7411 case VECTOR_TYPE:
7413 unsigned HOST_WIDE_INT idx;
7414 constructor_elt *ce;
7415 int i;
7416 bool need_to_clear;
7417 insn_code icode = CODE_FOR_nothing;
7418 tree elt;
7419 tree elttype = TREE_TYPE (type);
7420 int elt_size = vector_element_bits (type);
7421 machine_mode eltmode = TYPE_MODE (elttype);
7422 HOST_WIDE_INT bitsize;
7423 HOST_WIDE_INT bitpos;
7424 rtvec vector = NULL;
7425 poly_uint64 n_elts;
7426 unsigned HOST_WIDE_INT const_n_elts;
7427 alias_set_type alias;
7428 bool vec_vec_init_p = false;
7429 machine_mode mode = GET_MODE (target);
7431 gcc_assert (eltmode != BLKmode);
7433 /* Try using vec_duplicate_optab for uniform vectors. */
7434 if (!TREE_SIDE_EFFECTS (exp)
7435 && VECTOR_MODE_P (mode)
7436 && eltmode == GET_MODE_INNER (mode)
7437 && ((icode = optab_handler (vec_duplicate_optab, mode))
7438 != CODE_FOR_nothing)
7439 && (elt = uniform_vector_p (exp))
7440 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7442 class expand_operand ops[2];
7443 create_output_operand (&ops[0], target, mode);
7444 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7445 expand_insn (icode, 2, ops);
7446 if (!rtx_equal_p (target, ops[0].value))
7447 emit_move_insn (target, ops[0].value);
7448 break;
7450 /* Use sign-extension for uniform boolean vectors with
7451 integer modes. */
7452 if (!TREE_SIDE_EFFECTS (exp)
7453 && VECTOR_BOOLEAN_TYPE_P (type)
7454 && SCALAR_INT_MODE_P (mode)
7455 && (elt = uniform_vector_p (exp))
7456 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7458 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
7459 expand_normal (elt));
7460 convert_move (target, op0, 0);
7461 break;
7464 n_elts = TYPE_VECTOR_SUBPARTS (type);
7465 if (REG_P (target)
7466 && VECTOR_MODE_P (mode)
7467 && n_elts.is_constant (&const_n_elts))
7469 machine_mode emode = eltmode;
7470 bool vector_typed_elts_p = false;
7472 if (CONSTRUCTOR_NELTS (exp)
7473 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7474 == VECTOR_TYPE))
7476 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7477 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7478 * TYPE_VECTOR_SUBPARTS (etype),
7479 n_elts));
7480 emode = TYPE_MODE (etype);
7481 vector_typed_elts_p = true;
7483 icode = convert_optab_handler (vec_init_optab, mode, emode);
7484 if (icode != CODE_FOR_nothing)
7486 unsigned int n = const_n_elts;
7488 if (vector_typed_elts_p)
7490 n = CONSTRUCTOR_NELTS (exp);
7491 vec_vec_init_p = true;
7493 vector = rtvec_alloc (n);
7494 for (unsigned int k = 0; k < n; k++)
7495 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7499 /* Compute the size of the elements in the CTOR. It differs
7500 from the size of the vector type elements only when the
7501 CTOR elements are vectors themselves. */
7502 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7503 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7504 : elttype);
7505 if (VECTOR_TYPE_P (val_type))
7506 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7507 else
7508 bitsize = elt_size;
7510 /* If the constructor has fewer elements than the vector,
7511 clear the whole array first. Similarly if this is static
7512 constructor of a non-BLKmode object. */
7513 if (cleared)
7514 need_to_clear = false;
7515 else if (REG_P (target) && TREE_STATIC (exp))
7516 need_to_clear = true;
7517 else
7519 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7520 tree value;
7522 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7524 int n_elts_here = bitsize / elt_size;
7525 count += n_elts_here;
7526 if (mostly_zeros_p (value))
7527 zero_count += n_elts_here;
7530 /* Clear the entire vector first if there are any missing elements,
7531 or if the incidence of zero elements is >= 75%. */
7532 need_to_clear = (maybe_lt (count, n_elts)
7533 || 4 * zero_count >= 3 * count);
7536 if (need_to_clear && maybe_gt (size, 0) && !vector)
7538 if (REG_P (target))
7539 emit_move_insn (target, CONST0_RTX (mode));
7540 else
7541 clear_storage (target, gen_int_mode (size, Pmode),
7542 BLOCK_OP_NORMAL);
7543 cleared = 1;
7546 /* Inform later passes that the old value is dead. */
7547 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
7549 emit_move_insn (target, CONST0_RTX (mode));
7550 cleared = 1;
7553 if (MEM_P (target))
7554 alias = MEM_ALIAS_SET (target);
7555 else
7556 alias = get_alias_set (elttype);
7558 /* Store each element of the constructor into the corresponding
7559 element of TARGET, determined by counting the elements. */
7560 for (idx = 0, i = 0;
7561 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7562 idx++, i += bitsize / elt_size)
7564 HOST_WIDE_INT eltpos;
7565 tree value = ce->value;
7567 if (cleared && initializer_zerop (value))
7568 continue;
7570 if (ce->index)
7571 eltpos = tree_to_uhwi (ce->index);
7572 else
7573 eltpos = i;
7575 if (vector)
7577 if (vec_vec_init_p)
7579 gcc_assert (ce->index == NULL_TREE);
7580 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7581 eltpos = idx;
7583 else
7584 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7585 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7587 else
7589 machine_mode value_mode
7590 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7591 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7592 bitpos = eltpos * elt_size;
7593 store_constructor_field (target, bitsize, bitpos, 0,
7594 bitregion_end, value_mode,
7595 value, cleared, alias, reverse);
7599 if (vector)
7600 emit_insn (GEN_FCN (icode) (target,
7601 gen_rtx_PARALLEL (mode, vector)));
7602 break;
7605 default:
7606 gcc_unreachable ();
7610 /* Store the value of EXP (an expression tree)
7611 into a subfield of TARGET which has mode MODE and occupies
7612 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7613 If MODE is VOIDmode, it means that we are storing into a bit-field.
7615 BITREGION_START is bitpos of the first bitfield in this region.
7616 BITREGION_END is the bitpos of the ending bitfield in this region.
7617 These two fields are 0, if the C++ memory model does not apply,
7618 or we are not interested in keeping track of bitfield regions.
7620 Always return const0_rtx unless we have something particular to
7621 return.
7623 ALIAS_SET is the alias set for the destination. This value will
7624 (in general) be different from that for TARGET, since TARGET is a
7625 reference to the containing structure.
7627 If NONTEMPORAL is true, try generating a nontemporal store.
7629 If REVERSE is true, the store is to be done in reverse order. */
7631 static rtx
7632 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7633 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7634 machine_mode mode, tree exp,
7635 alias_set_type alias_set, bool nontemporal, bool reverse)
7637 if (TREE_CODE (exp) == ERROR_MARK)
7638 return const0_rtx;
7640 /* If we have nothing to store, do nothing unless the expression has
7641 side-effects. Don't do that for zero sized addressable lhs of
7642 calls. */
7643 if (known_eq (bitsize, 0)
7644 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7645 || TREE_CODE (exp) != CALL_EXPR))
7646 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7648 if (GET_CODE (target) == CONCAT)
7650 /* We're storing into a struct containing a single __complex. */
7652 gcc_assert (known_eq (bitpos, 0));
7653 return store_expr (exp, target, 0, nontemporal, reverse);
7656 /* If the structure is in a register or if the component
7657 is a bit field, we cannot use addressing to access it.
7658 Use bit-field techniques or SUBREG to store in it. */
7660 poly_int64 decl_bitsize;
7661 if (mode == VOIDmode
7662 || (mode != BLKmode && ! direct_store[(int) mode]
7663 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7664 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7665 || REG_P (target)
7666 || GET_CODE (target) == SUBREG
7667 /* If the field isn't aligned enough to store as an ordinary memref,
7668 store it as a bit field. */
7669 || (mode != BLKmode
7670 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7671 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7672 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7673 || !multiple_p (bitpos, BITS_PER_UNIT)))
7674 || (known_size_p (bitsize)
7675 && mode != BLKmode
7676 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7677 /* If the RHS and field are a constant size and the size of the
7678 RHS isn't the same size as the bitfield, we must use bitfield
7679 operations. */
7680 || (known_size_p (bitsize)
7681 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7682 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7683 bitsize)
7684 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7685 we will handle specially below. */
7686 && !(TREE_CODE (exp) == CONSTRUCTOR
7687 && multiple_p (bitsize, BITS_PER_UNIT))
7688 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7689 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7690 includes some extra padding. store_expr / expand_expr will in
7691 that case call get_inner_reference that will have the bitsize
7692 we check here and thus the block move will not clobber the
7693 padding that shouldn't be clobbered. In the future we could
7694 replace the TREE_ADDRESSABLE check with a check that
7695 get_base_address needs to live in memory. */
7696 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7697 || TREE_CODE (exp) != COMPONENT_REF
7698 || !multiple_p (bitsize, BITS_PER_UNIT)
7699 || !multiple_p (bitpos, BITS_PER_UNIT)
7700 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7701 &decl_bitsize)
7702 || maybe_ne (decl_bitsize, bitsize))
7703 /* A call with an addressable return type and return-slot
7704 optimization must not need bitfield operations but we must
7705 pass down the original target. */
7706 && (TREE_CODE (exp) != CALL_EXPR
7707 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7708 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7709 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7710 decl we must use bitfield operations. */
7711 || (known_size_p (bitsize)
7712 && TREE_CODE (exp) == MEM_REF
7713 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7714 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7715 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7716 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7718 rtx temp;
7719 gimple *nop_def;
7721 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7722 implies a mask operation. If the precision is the same size as
7723 the field we're storing into, that mask is redundant. This is
7724 particularly common with bit field assignments generated by the
7725 C front end. */
7726 nop_def = get_def_for_expr (exp, NOP_EXPR);
7727 if (nop_def)
7729 tree type = TREE_TYPE (exp);
7730 if (INTEGRAL_TYPE_P (type)
7731 && maybe_ne (TYPE_PRECISION (type),
7732 GET_MODE_BITSIZE (TYPE_MODE (type)))
7733 && known_eq (bitsize, TYPE_PRECISION (type)))
7735 tree op = gimple_assign_rhs1 (nop_def);
7736 type = TREE_TYPE (op);
7737 if (INTEGRAL_TYPE_P (type)
7738 && known_ge (TYPE_PRECISION (type), bitsize))
7739 exp = op;
7743 temp = expand_normal (exp);
7745 /* We don't support variable-sized BLKmode bitfields, since our
7746 handling of BLKmode is bound up with the ability to break
7747 things into words. */
7748 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7750 /* Handle calls that return values in multiple non-contiguous locations.
7751 The Irix 6 ABI has examples of this. */
7752 if (GET_CODE (temp) == PARALLEL)
7754 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7755 machine_mode temp_mode = GET_MODE (temp);
7756 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7757 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7758 rtx temp_target = gen_reg_rtx (temp_mode);
7759 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7760 temp = temp_target;
7763 /* Handle calls that return BLKmode values in registers. */
7764 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7766 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7767 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7768 temp = temp_target;
7771 /* If the value has aggregate type and an integral mode then, if BITSIZE
7772 is narrower than this mode and this is for big-endian data, we first
7773 need to put the value into the low-order bits for store_bit_field,
7774 except when MODE is BLKmode and BITSIZE larger than the word size
7775 (see the handling of fields larger than a word in store_bit_field).
7776 Moreover, the field may be not aligned on a byte boundary; in this
7777 case, if it has reverse storage order, it needs to be accessed as a
7778 scalar field with reverse storage order and we must first put the
7779 value into target order. */
7780 scalar_int_mode temp_mode;
7781 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7782 && is_int_mode (GET_MODE (temp), &temp_mode))
7784 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7786 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7788 if (reverse)
7789 temp = flip_storage_order (temp_mode, temp);
7791 gcc_checking_assert (known_le (bitsize, size));
7792 if (maybe_lt (bitsize, size)
7793 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7794 /* Use of to_constant for BLKmode was checked above. */
7795 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7796 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7797 size - bitsize, NULL_RTX, 1);
7800 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7801 if (mode != VOIDmode && mode != BLKmode
7802 && mode != TYPE_MODE (TREE_TYPE (exp)))
7803 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7805 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7806 and BITPOS must be aligned on a byte boundary. If so, we simply do
7807 a block copy. Likewise for a BLKmode-like TARGET. */
7808 if (GET_MODE (temp) == BLKmode
7809 && (GET_MODE (target) == BLKmode
7810 || (MEM_P (target)
7811 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7812 && multiple_p (bitpos, BITS_PER_UNIT)
7813 && multiple_p (bitsize, BITS_PER_UNIT))))
7815 gcc_assert (MEM_P (target) && MEM_P (temp));
7816 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7817 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7819 target = adjust_address (target, VOIDmode, bytepos);
7820 emit_block_move (target, temp,
7821 gen_int_mode (bytesize, Pmode),
7822 BLOCK_OP_NORMAL);
7824 return const0_rtx;
7827 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7828 word size, we need to load the value (see again store_bit_field). */
7829 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7831 temp_mode = smallest_int_mode_for_size (bitsize);
7832 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7833 temp_mode, false, NULL);
7836 /* Store the value in the bitfield. */
7837 gcc_checking_assert (known_ge (bitpos, 0));
7838 store_bit_field (target, bitsize, bitpos,
7839 bitregion_start, bitregion_end,
7840 mode, temp, reverse, false);
7842 return const0_rtx;
7844 else
7846 /* Now build a reference to just the desired component. */
7847 rtx to_rtx = adjust_address (target, mode,
7848 exact_div (bitpos, BITS_PER_UNIT));
7850 if (to_rtx == target)
7851 to_rtx = copy_rtx (to_rtx);
7853 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7854 set_mem_alias_set (to_rtx, alias_set);
7856 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7857 into a target smaller than its type; handle that case now. */
7858 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7860 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7861 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7862 return to_rtx;
7865 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7869 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7870 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7871 codes and find the ultimate containing object, which we return.
7873 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7874 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7875 storage order of the field.
7876 If the position of the field is variable, we store a tree
7877 giving the variable offset (in units) in *POFFSET.
7878 This offset is in addition to the bit position.
7879 If the position is not variable, we store 0 in *POFFSET.
7881 If any of the extraction expressions is volatile,
7882 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7884 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7885 Otherwise, it is a mode that can be used to access the field.
7887 If the field describes a variable-sized object, *PMODE is set to
7888 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7889 this case, but the address of the object can be found. */
7891 tree
7892 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7893 poly_int64_pod *pbitpos, tree *poffset,
7894 machine_mode *pmode, int *punsignedp,
7895 int *preversep, int *pvolatilep)
7897 tree size_tree = 0;
7898 machine_mode mode = VOIDmode;
7899 bool blkmode_bitfield = false;
7900 tree offset = size_zero_node;
7901 poly_offset_int bit_offset = 0;
7903 /* First get the mode, signedness, storage order and size. We do this from
7904 just the outermost expression. */
7905 *pbitsize = -1;
7906 if (TREE_CODE (exp) == COMPONENT_REF)
7908 tree field = TREE_OPERAND (exp, 1);
7909 size_tree = DECL_SIZE (field);
7910 if (flag_strict_volatile_bitfields > 0
7911 && TREE_THIS_VOLATILE (exp)
7912 && DECL_BIT_FIELD_TYPE (field)
7913 && DECL_MODE (field) != BLKmode)
7914 /* Volatile bitfields should be accessed in the mode of the
7915 field's type, not the mode computed based on the bit
7916 size. */
7917 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7918 else if (!DECL_BIT_FIELD (field))
7920 mode = DECL_MODE (field);
7921 /* For vector fields re-check the target flags, as DECL_MODE
7922 could have been set with different target flags than
7923 the current function has. */
7924 if (VECTOR_TYPE_P (TREE_TYPE (field))
7925 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7926 mode = TYPE_MODE (TREE_TYPE (field));
7928 else if (DECL_MODE (field) == BLKmode)
7929 blkmode_bitfield = true;
7931 *punsignedp = DECL_UNSIGNED (field);
7933 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7935 size_tree = TREE_OPERAND (exp, 1);
7936 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7937 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7939 /* For vector element types with the correct size of access or for
7940 vector typed accesses use the mode of the access type. */
7941 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7942 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7943 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7944 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7945 mode = TYPE_MODE (TREE_TYPE (exp));
7947 else
7949 mode = TYPE_MODE (TREE_TYPE (exp));
7950 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7952 if (mode == BLKmode)
7953 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7954 else
7955 *pbitsize = GET_MODE_BITSIZE (mode);
7958 if (size_tree != 0)
7960 if (! tree_fits_uhwi_p (size_tree))
7961 mode = BLKmode, *pbitsize = -1;
7962 else
7963 *pbitsize = tree_to_uhwi (size_tree);
7966 *preversep = reverse_storage_order_for_component_p (exp);
7968 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7969 and find the ultimate containing object. */
7970 while (1)
7972 switch (TREE_CODE (exp))
7974 case BIT_FIELD_REF:
7975 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7976 break;
7978 case COMPONENT_REF:
7980 tree field = TREE_OPERAND (exp, 1);
7981 tree this_offset = component_ref_field_offset (exp);
7983 /* If this field hasn't been filled in yet, don't go past it.
7984 This should only happen when folding expressions made during
7985 type construction. */
7986 if (this_offset == 0)
7987 break;
7989 offset = size_binop (PLUS_EXPR, offset, this_offset);
7990 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7992 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7994 break;
7996 case ARRAY_REF:
7997 case ARRAY_RANGE_REF:
7999 tree index = TREE_OPERAND (exp, 1);
8000 tree low_bound = array_ref_low_bound (exp);
8001 tree unit_size = array_ref_element_size (exp);
8003 /* We assume all arrays have sizes that are a multiple of a byte.
8004 First subtract the lower bound, if any, in the type of the
8005 index, then convert to sizetype and multiply by the size of
8006 the array element. */
8007 if (! integer_zerop (low_bound))
8008 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8009 index, low_bound);
8011 offset = size_binop (PLUS_EXPR, offset,
8012 size_binop (MULT_EXPR,
8013 fold_convert (sizetype, index),
8014 unit_size));
8016 break;
8018 case REALPART_EXPR:
8019 break;
8021 case IMAGPART_EXPR:
8022 bit_offset += *pbitsize;
8023 break;
8025 case VIEW_CONVERT_EXPR:
8026 break;
8028 case MEM_REF:
8029 /* Hand back the decl for MEM[&decl, off]. */
8030 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8032 tree off = TREE_OPERAND (exp, 1);
8033 if (!integer_zerop (off))
8035 poly_offset_int boff = mem_ref_offset (exp);
8036 boff <<= LOG2_BITS_PER_UNIT;
8037 bit_offset += boff;
8039 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8041 goto done;
8043 default:
8044 goto done;
8047 /* If any reference in the chain is volatile, the effect is volatile. */
8048 if (TREE_THIS_VOLATILE (exp))
8049 *pvolatilep = 1;
8051 exp = TREE_OPERAND (exp, 0);
8053 done:
8055 /* If OFFSET is constant, see if we can return the whole thing as a
8056 constant bit position. Make sure to handle overflow during
8057 this conversion. */
8058 if (poly_int_tree_p (offset))
8060 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8061 TYPE_PRECISION (sizetype));
8062 tem <<= LOG2_BITS_PER_UNIT;
8063 tem += bit_offset;
8064 if (tem.to_shwi (pbitpos))
8065 *poffset = offset = NULL_TREE;
8068 /* Otherwise, split it up. */
8069 if (offset)
8071 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8072 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8074 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8075 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8076 offset = size_binop (PLUS_EXPR, offset,
8077 build_int_cst (sizetype, bytes.force_shwi ()));
8080 *poffset = offset;
8083 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8084 if (mode == VOIDmode
8085 && blkmode_bitfield
8086 && multiple_p (*pbitpos, BITS_PER_UNIT)
8087 && multiple_p (*pbitsize, BITS_PER_UNIT))
8088 *pmode = BLKmode;
8089 else
8090 *pmode = mode;
8092 return exp;
8095 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8097 static unsigned HOST_WIDE_INT
8098 target_align (const_tree target)
8100 /* We might have a chain of nested references with intermediate misaligning
8101 bitfields components, so need to recurse to find out. */
8103 unsigned HOST_WIDE_INT this_align, outer_align;
8105 switch (TREE_CODE (target))
8107 case BIT_FIELD_REF:
8108 return 1;
8110 case COMPONENT_REF:
8111 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8112 outer_align = target_align (TREE_OPERAND (target, 0));
8113 return MIN (this_align, outer_align);
8115 case ARRAY_REF:
8116 case ARRAY_RANGE_REF:
8117 this_align = TYPE_ALIGN (TREE_TYPE (target));
8118 outer_align = target_align (TREE_OPERAND (target, 0));
8119 return MIN (this_align, outer_align);
8121 CASE_CONVERT:
8122 case NON_LVALUE_EXPR:
8123 case VIEW_CONVERT_EXPR:
8124 this_align = TYPE_ALIGN (TREE_TYPE (target));
8125 outer_align = target_align (TREE_OPERAND (target, 0));
8126 return MAX (this_align, outer_align);
8128 default:
8129 return TYPE_ALIGN (TREE_TYPE (target));
8134 /* Given an rtx VALUE that may contain additions and multiplications, return
8135 an equivalent value that just refers to a register, memory, or constant.
8136 This is done by generating instructions to perform the arithmetic and
8137 returning a pseudo-register containing the value.
8139 The returned value may be a REG, SUBREG, MEM or constant. */
8142 force_operand (rtx value, rtx target)
8144 rtx op1, op2;
8145 /* Use subtarget as the target for operand 0 of a binary operation. */
8146 rtx subtarget = get_subtarget (target);
8147 enum rtx_code code = GET_CODE (value);
8149 /* Check for subreg applied to an expression produced by loop optimizer. */
8150 if (code == SUBREG
8151 && !REG_P (SUBREG_REG (value))
8152 && !MEM_P (SUBREG_REG (value)))
8154 value
8155 = simplify_gen_subreg (GET_MODE (value),
8156 force_reg (GET_MODE (SUBREG_REG (value)),
8157 force_operand (SUBREG_REG (value),
8158 NULL_RTX)),
8159 GET_MODE (SUBREG_REG (value)),
8160 SUBREG_BYTE (value));
8161 code = GET_CODE (value);
8164 /* Check for a PIC address load. */
8165 if ((code == PLUS || code == MINUS)
8166 && XEXP (value, 0) == pic_offset_table_rtx
8167 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8168 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8169 || GET_CODE (XEXP (value, 1)) == CONST))
8171 if (!subtarget)
8172 subtarget = gen_reg_rtx (GET_MODE (value));
8173 emit_move_insn (subtarget, value);
8174 return subtarget;
8177 if (ARITHMETIC_P (value))
8179 op2 = XEXP (value, 1);
8180 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8181 subtarget = 0;
8182 if (code == MINUS && CONST_INT_P (op2))
8184 code = PLUS;
8185 op2 = negate_rtx (GET_MODE (value), op2);
8188 /* Check for an addition with OP2 a constant integer and our first
8189 operand a PLUS of a virtual register and something else. In that
8190 case, we want to emit the sum of the virtual register and the
8191 constant first and then add the other value. This allows virtual
8192 register instantiation to simply modify the constant rather than
8193 creating another one around this addition. */
8194 if (code == PLUS && CONST_INT_P (op2)
8195 && GET_CODE (XEXP (value, 0)) == PLUS
8196 && REG_P (XEXP (XEXP (value, 0), 0))
8197 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8199 rtx temp = expand_simple_binop (GET_MODE (value), code,
8200 XEXP (XEXP (value, 0), 0), op2,
8201 subtarget, 0, OPTAB_LIB_WIDEN);
8202 return expand_simple_binop (GET_MODE (value), code, temp,
8203 force_operand (XEXP (XEXP (value,
8204 0), 1), 0),
8205 target, 0, OPTAB_LIB_WIDEN);
8208 op1 = force_operand (XEXP (value, 0), subtarget);
8209 op2 = force_operand (op2, NULL_RTX);
8210 switch (code)
8212 case MULT:
8213 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8214 case DIV:
8215 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8216 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8217 target, 1, OPTAB_LIB_WIDEN);
8218 else
8219 return expand_divmod (0,
8220 FLOAT_MODE_P (GET_MODE (value))
8221 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8222 GET_MODE (value), op1, op2, target, 0);
8223 case MOD:
8224 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8225 target, 0);
8226 case UDIV:
8227 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8228 target, 1);
8229 case UMOD:
8230 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8231 target, 1);
8232 case ASHIFTRT:
8233 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8234 target, 0, OPTAB_LIB_WIDEN);
8235 default:
8236 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8237 target, 1, OPTAB_LIB_WIDEN);
8240 if (UNARY_P (value))
8242 if (!target)
8243 target = gen_reg_rtx (GET_MODE (value));
8244 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8245 switch (code)
8247 case ZERO_EXTEND:
8248 case SIGN_EXTEND:
8249 case TRUNCATE:
8250 case FLOAT_EXTEND:
8251 case FLOAT_TRUNCATE:
8252 convert_move (target, op1, code == ZERO_EXTEND);
8253 return target;
8255 case FIX:
8256 case UNSIGNED_FIX:
8257 expand_fix (target, op1, code == UNSIGNED_FIX);
8258 return target;
8260 case FLOAT:
8261 case UNSIGNED_FLOAT:
8262 expand_float (target, op1, code == UNSIGNED_FLOAT);
8263 return target;
8265 default:
8266 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8270 #ifdef INSN_SCHEDULING
8271 /* On machines that have insn scheduling, we want all memory reference to be
8272 explicit, so we need to deal with such paradoxical SUBREGs. */
8273 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8274 value
8275 = simplify_gen_subreg (GET_MODE (value),
8276 force_reg (GET_MODE (SUBREG_REG (value)),
8277 force_operand (SUBREG_REG (value),
8278 NULL_RTX)),
8279 GET_MODE (SUBREG_REG (value)),
8280 SUBREG_BYTE (value));
8281 #endif
8283 return value;
8286 /* Subroutine of expand_expr: return true iff there is no way that
8287 EXP can reference X, which is being modified. TOP_P is nonzero if this
8288 call is going to be used to determine whether we need a temporary
8289 for EXP, as opposed to a recursive call to this function.
8291 It is always safe for this routine to return false since it merely
8292 searches for optimization opportunities. */
8294 bool
8295 safe_from_p (const_rtx x, tree exp, int top_p)
8297 rtx exp_rtl = 0;
8298 int i, nops;
8300 if (x == 0
8301 /* If EXP has varying size, we MUST use a target since we currently
8302 have no way of allocating temporaries of variable size
8303 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8304 So we assume here that something at a higher level has prevented a
8305 clash. This is somewhat bogus, but the best we can do. Only
8306 do this when X is BLKmode and when we are at the top level. */
8307 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8308 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8309 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8310 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8311 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8312 != INTEGER_CST)
8313 && GET_MODE (x) == BLKmode)
8314 /* If X is in the outgoing argument area, it is always safe. */
8315 || (MEM_P (x)
8316 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8317 || (GET_CODE (XEXP (x, 0)) == PLUS
8318 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8319 return true;
8321 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8322 find the underlying pseudo. */
8323 if (GET_CODE (x) == SUBREG)
8325 x = SUBREG_REG (x);
8326 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8327 return false;
8330 /* Now look at our tree code and possibly recurse. */
8331 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8333 case tcc_declaration:
8334 exp_rtl = DECL_RTL_IF_SET (exp);
8335 break;
8337 case tcc_constant:
8338 return true;
8340 case tcc_exceptional:
8341 if (TREE_CODE (exp) == TREE_LIST)
8343 while (1)
8345 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8346 return false;
8347 exp = TREE_CHAIN (exp);
8348 if (!exp)
8349 return true;
8350 if (TREE_CODE (exp) != TREE_LIST)
8351 return safe_from_p (x, exp, 0);
8354 else if (TREE_CODE (exp) == CONSTRUCTOR)
8356 constructor_elt *ce;
8357 unsigned HOST_WIDE_INT idx;
8359 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8360 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8361 || !safe_from_p (x, ce->value, 0))
8362 return false;
8363 return true;
8365 else if (TREE_CODE (exp) == ERROR_MARK)
8366 return true; /* An already-visited SAVE_EXPR? */
8367 else
8368 return false;
8370 case tcc_statement:
8371 /* The only case we look at here is the DECL_INITIAL inside a
8372 DECL_EXPR. */
8373 return (TREE_CODE (exp) != DECL_EXPR
8374 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8375 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8376 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8378 case tcc_binary:
8379 case tcc_comparison:
8380 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8381 return false;
8382 /* Fall through. */
8384 case tcc_unary:
8385 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8387 case tcc_expression:
8388 case tcc_reference:
8389 case tcc_vl_exp:
8390 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8391 the expression. If it is set, we conflict iff we are that rtx or
8392 both are in memory. Otherwise, we check all operands of the
8393 expression recursively. */
8395 switch (TREE_CODE (exp))
8397 case ADDR_EXPR:
8398 /* If the operand is static or we are static, we can't conflict.
8399 Likewise if we don't conflict with the operand at all. */
8400 if (staticp (TREE_OPERAND (exp, 0))
8401 || TREE_STATIC (exp)
8402 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8403 return true;
8405 /* Otherwise, the only way this can conflict is if we are taking
8406 the address of a DECL a that address if part of X, which is
8407 very rare. */
8408 exp = TREE_OPERAND (exp, 0);
8409 if (DECL_P (exp))
8411 if (!DECL_RTL_SET_P (exp)
8412 || !MEM_P (DECL_RTL (exp)))
8413 return false;
8414 else
8415 exp_rtl = XEXP (DECL_RTL (exp), 0);
8417 break;
8419 case MEM_REF:
8420 if (MEM_P (x)
8421 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8422 get_alias_set (exp)))
8423 return false;
8424 break;
8426 case CALL_EXPR:
8427 /* Assume that the call will clobber all hard registers and
8428 all of memory. */
8429 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8430 || MEM_P (x))
8431 return false;
8432 break;
8434 case WITH_CLEANUP_EXPR:
8435 case CLEANUP_POINT_EXPR:
8436 /* Lowered by gimplify.cc. */
8437 gcc_unreachable ();
8439 case SAVE_EXPR:
8440 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8442 default:
8443 break;
8446 /* If we have an rtx, we do not need to scan our operands. */
8447 if (exp_rtl)
8448 break;
8450 nops = TREE_OPERAND_LENGTH (exp);
8451 for (i = 0; i < nops; i++)
8452 if (TREE_OPERAND (exp, i) != 0
8453 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8454 return false;
8456 break;
8458 case tcc_type:
8459 /* Should never get a type here. */
8460 gcc_unreachable ();
8463 /* If we have an rtl, find any enclosed object. Then see if we conflict
8464 with it. */
8465 if (exp_rtl)
8467 if (GET_CODE (exp_rtl) == SUBREG)
8469 exp_rtl = SUBREG_REG (exp_rtl);
8470 if (REG_P (exp_rtl)
8471 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8472 return false;
8475 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8476 are memory and they conflict. */
8477 return ! (rtx_equal_p (x, exp_rtl)
8478 || (MEM_P (x) && MEM_P (exp_rtl)
8479 && true_dependence (exp_rtl, VOIDmode, x)));
8482 /* If we reach here, it is safe. */
8483 return true;
8487 /* Return the highest power of two that EXP is known to be a multiple of.
8488 This is used in updating alignment of MEMs in array references. */
8490 unsigned HOST_WIDE_INT
8491 highest_pow2_factor (const_tree exp)
8493 unsigned HOST_WIDE_INT ret;
8494 int trailing_zeros = tree_ctz (exp);
8495 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8496 return BIGGEST_ALIGNMENT;
8497 ret = HOST_WIDE_INT_1U << trailing_zeros;
8498 if (ret > BIGGEST_ALIGNMENT)
8499 return BIGGEST_ALIGNMENT;
8500 return ret;
8503 /* Similar, except that the alignment requirements of TARGET are
8504 taken into account. Assume it is at least as aligned as its
8505 type, unless it is a COMPONENT_REF in which case the layout of
8506 the structure gives the alignment. */
8508 static unsigned HOST_WIDE_INT
8509 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8511 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8512 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8514 return MAX (factor, talign);
8517 /* Convert the tree comparison code TCODE to the rtl one where the
8518 signedness is UNSIGNEDP. */
8520 static enum rtx_code
8521 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8523 enum rtx_code code;
8524 switch (tcode)
8526 case EQ_EXPR:
8527 code = EQ;
8528 break;
8529 case NE_EXPR:
8530 code = NE;
8531 break;
8532 case LT_EXPR:
8533 code = unsignedp ? LTU : LT;
8534 break;
8535 case LE_EXPR:
8536 code = unsignedp ? LEU : LE;
8537 break;
8538 case GT_EXPR:
8539 code = unsignedp ? GTU : GT;
8540 break;
8541 case GE_EXPR:
8542 code = unsignedp ? GEU : GE;
8543 break;
8544 case UNORDERED_EXPR:
8545 code = UNORDERED;
8546 break;
8547 case ORDERED_EXPR:
8548 code = ORDERED;
8549 break;
8550 case UNLT_EXPR:
8551 code = UNLT;
8552 break;
8553 case UNLE_EXPR:
8554 code = UNLE;
8555 break;
8556 case UNGT_EXPR:
8557 code = UNGT;
8558 break;
8559 case UNGE_EXPR:
8560 code = UNGE;
8561 break;
8562 case UNEQ_EXPR:
8563 code = UNEQ;
8564 break;
8565 case LTGT_EXPR:
8566 code = LTGT;
8567 break;
8569 default:
8570 gcc_unreachable ();
8572 return code;
8575 /* Subroutine of expand_expr. Expand the two operands of a binary
8576 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8577 The value may be stored in TARGET if TARGET is nonzero. The
8578 MODIFIER argument is as documented by expand_expr. */
8580 void
8581 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8582 enum expand_modifier modifier)
8584 if (! safe_from_p (target, exp1, 1))
8585 target = 0;
8586 if (operand_equal_p (exp0, exp1, 0))
8588 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8589 *op1 = copy_rtx (*op0);
8591 else
8593 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8594 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8599 /* Return a MEM that contains constant EXP. DEFER is as for
8600 output_constant_def and MODIFIER is as for expand_expr. */
8602 static rtx
8603 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8605 rtx mem;
8607 mem = output_constant_def (exp, defer);
8608 if (modifier != EXPAND_INITIALIZER)
8609 mem = use_anchored_address (mem);
8610 return mem;
8613 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8614 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8616 static rtx
8617 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8618 enum expand_modifier modifier, addr_space_t as)
8620 rtx result, subtarget;
8621 tree inner, offset;
8622 poly_int64 bitsize, bitpos;
8623 int unsignedp, reversep, volatilep = 0;
8624 machine_mode mode1;
8626 /* If we are taking the address of a constant and are at the top level,
8627 we have to use output_constant_def since we can't call force_const_mem
8628 at top level. */
8629 /* ??? This should be considered a front-end bug. We should not be
8630 generating ADDR_EXPR of something that isn't an LVALUE. The only
8631 exception here is STRING_CST. */
8632 if (CONSTANT_CLASS_P (exp))
8634 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8635 if (modifier < EXPAND_SUM)
8636 result = force_operand (result, target);
8637 return result;
8640 /* Everything must be something allowed by is_gimple_addressable. */
8641 switch (TREE_CODE (exp))
8643 case INDIRECT_REF:
8644 /* This case will happen via recursion for &a->b. */
8645 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8647 case MEM_REF:
8649 tree tem = TREE_OPERAND (exp, 0);
8650 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8651 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8652 return expand_expr (tem, target, tmode, modifier);
8655 case TARGET_MEM_REF:
8656 return addr_for_mem_ref (exp, as, true);
8658 case CONST_DECL:
8659 /* Expand the initializer like constants above. */
8660 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8661 0, modifier), 0);
8662 if (modifier < EXPAND_SUM)
8663 result = force_operand (result, target);
8664 return result;
8666 case REALPART_EXPR:
8667 /* The real part of the complex number is always first, therefore
8668 the address is the same as the address of the parent object. */
8669 offset = 0;
8670 bitpos = 0;
8671 inner = TREE_OPERAND (exp, 0);
8672 break;
8674 case IMAGPART_EXPR:
8675 /* The imaginary part of the complex number is always second.
8676 The expression is therefore always offset by the size of the
8677 scalar type. */
8678 offset = 0;
8679 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8680 inner = TREE_OPERAND (exp, 0);
8681 break;
8683 case COMPOUND_LITERAL_EXPR:
8684 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8685 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8686 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8687 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8688 the initializers aren't gimplified. */
8689 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8690 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8691 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8692 target, tmode, modifier, as);
8693 /* FALLTHRU */
8694 default:
8695 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8696 expand_expr, as that can have various side effects; LABEL_DECLs for
8697 example, may not have their DECL_RTL set yet. Expand the rtl of
8698 CONSTRUCTORs too, which should yield a memory reference for the
8699 constructor's contents. Assume language specific tree nodes can
8700 be expanded in some interesting way. */
8701 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8702 if (DECL_P (exp)
8703 || TREE_CODE (exp) == CONSTRUCTOR
8704 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8706 result = expand_expr (exp, target, tmode,
8707 modifier == EXPAND_INITIALIZER
8708 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8710 /* If the DECL isn't in memory, then the DECL wasn't properly
8711 marked TREE_ADDRESSABLE, which will be either a front-end
8712 or a tree optimizer bug. */
8714 gcc_assert (MEM_P (result));
8715 result = XEXP (result, 0);
8717 /* ??? Is this needed anymore? */
8718 if (DECL_P (exp))
8719 TREE_USED (exp) = 1;
8721 if (modifier != EXPAND_INITIALIZER
8722 && modifier != EXPAND_CONST_ADDRESS
8723 && modifier != EXPAND_SUM)
8724 result = force_operand (result, target);
8725 return result;
8728 /* Pass FALSE as the last argument to get_inner_reference although
8729 we are expanding to RTL. The rationale is that we know how to
8730 handle "aligning nodes" here: we can just bypass them because
8731 they won't change the final object whose address will be returned
8732 (they actually exist only for that purpose). */
8733 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8734 &unsignedp, &reversep, &volatilep);
8735 break;
8738 /* We must have made progress. */
8739 gcc_assert (inner != exp);
8741 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8742 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8743 inner alignment, force the inner to be sufficiently aligned. */
8744 if (CONSTANT_CLASS_P (inner)
8745 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8747 inner = copy_node (inner);
8748 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8749 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8750 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8752 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8754 if (offset)
8756 rtx tmp;
8758 if (modifier != EXPAND_NORMAL)
8759 result = force_operand (result, NULL);
8760 tmp = expand_expr (offset, NULL_RTX, tmode,
8761 modifier == EXPAND_INITIALIZER
8762 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8764 /* expand_expr is allowed to return an object in a mode other
8765 than TMODE. If it did, we need to convert. */
8766 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8767 tmp = convert_modes (tmode, GET_MODE (tmp),
8768 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8769 result = convert_memory_address_addr_space (tmode, result, as);
8770 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8772 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8773 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8774 else
8776 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8777 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8778 1, OPTAB_LIB_WIDEN);
8782 if (maybe_ne (bitpos, 0))
8784 /* Someone beforehand should have rejected taking the address
8785 of an object that isn't byte-aligned. */
8786 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8787 result = convert_memory_address_addr_space (tmode, result, as);
8788 result = plus_constant (tmode, result, bytepos);
8789 if (modifier < EXPAND_SUM)
8790 result = force_operand (result, target);
8793 return result;
8796 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8797 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8799 static rtx
8800 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8801 enum expand_modifier modifier)
8803 addr_space_t as = ADDR_SPACE_GENERIC;
8804 scalar_int_mode address_mode = Pmode;
8805 scalar_int_mode pointer_mode = ptr_mode;
8806 machine_mode rmode;
8807 rtx result;
8809 /* Target mode of VOIDmode says "whatever's natural". */
8810 if (tmode == VOIDmode)
8811 tmode = TYPE_MODE (TREE_TYPE (exp));
8813 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8815 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8816 address_mode = targetm.addr_space.address_mode (as);
8817 pointer_mode = targetm.addr_space.pointer_mode (as);
8820 /* We can get called with some Weird Things if the user does silliness
8821 like "(short) &a". In that case, convert_memory_address won't do
8822 the right thing, so ignore the given target mode. */
8823 scalar_int_mode new_tmode = (tmode == pointer_mode
8824 ? pointer_mode
8825 : address_mode);
8827 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8828 new_tmode, modifier, as);
8830 /* Despite expand_expr claims concerning ignoring TMODE when not
8831 strictly convenient, stuff breaks if we don't honor it. Note
8832 that combined with the above, we only do this for pointer modes. */
8833 rmode = GET_MODE (result);
8834 if (rmode == VOIDmode)
8835 rmode = new_tmode;
8836 if (rmode != new_tmode)
8837 result = convert_memory_address_addr_space (new_tmode, result, as);
8839 return result;
8842 /* Generate code for computing CONSTRUCTOR EXP.
8843 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8844 is TRUE, instead of creating a temporary variable in memory
8845 NULL is returned and the caller needs to handle it differently. */
8847 static rtx
8848 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8849 bool avoid_temp_mem)
8851 tree type = TREE_TYPE (exp);
8852 machine_mode mode = TYPE_MODE (type);
8854 /* Try to avoid creating a temporary at all. This is possible
8855 if all of the initializer is zero.
8856 FIXME: try to handle all [0..255] initializers we can handle
8857 with memset. */
8858 if (TREE_STATIC (exp)
8859 && !TREE_ADDRESSABLE (exp)
8860 && target != 0 && mode == BLKmode
8861 && all_zeros_p (exp))
8863 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8864 return target;
8867 /* All elts simple constants => refer to a constant in memory. But
8868 if this is a non-BLKmode mode, let it store a field at a time
8869 since that should make a CONST_INT, CONST_WIDE_INT or
8870 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8871 use, it is best to store directly into the target unless the type
8872 is large enough that memcpy will be used. If we are making an
8873 initializer and all operands are constant, put it in memory as
8874 well.
8876 FIXME: Avoid trying to fill vector constructors piece-meal.
8877 Output them with output_constant_def below unless we're sure
8878 they're zeros. This should go away when vector initializers
8879 are treated like VECTOR_CST instead of arrays. */
8880 if ((TREE_STATIC (exp)
8881 && ((mode == BLKmode
8882 && ! (target != 0 && safe_from_p (target, exp, 1)))
8883 || TREE_ADDRESSABLE (exp)
8884 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8885 && (! can_move_by_pieces
8886 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8887 TYPE_ALIGN (type)))
8888 && ! mostly_zeros_p (exp))))
8889 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8890 && TREE_CONSTANT (exp)))
8892 rtx constructor;
8894 if (avoid_temp_mem)
8895 return NULL_RTX;
8897 constructor = expand_expr_constant (exp, 1, modifier);
8899 if (modifier != EXPAND_CONST_ADDRESS
8900 && modifier != EXPAND_INITIALIZER
8901 && modifier != EXPAND_SUM)
8902 constructor = validize_mem (constructor);
8904 return constructor;
8907 /* If the CTOR is available in static storage and not mostly
8908 zeros and we can move it by pieces prefer to do so since
8909 that's usually more efficient than performing a series of
8910 stores from immediates. */
8911 if (avoid_temp_mem
8912 && TREE_STATIC (exp)
8913 && TREE_CONSTANT (exp)
8914 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8915 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8916 TYPE_ALIGN (type))
8917 && ! mostly_zeros_p (exp))
8918 return NULL_RTX;
8920 /* Handle calls that pass values in multiple non-contiguous
8921 locations. The Irix 6 ABI has examples of this. */
8922 if (target == 0 || ! safe_from_p (target, exp, 1)
8923 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8924 /* Also make a temporary if the store is to volatile memory, to
8925 avoid individual accesses to aggregate members. */
8926 || (GET_CODE (target) == MEM
8927 && MEM_VOLATILE_P (target)
8928 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8930 if (avoid_temp_mem)
8931 return NULL_RTX;
8933 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8936 store_constructor (exp, target, 0, int_expr_size (exp), false);
8937 return target;
8941 /* expand_expr: generate code for computing expression EXP.
8942 An rtx for the computed value is returned. The value is never null.
8943 In the case of a void EXP, const0_rtx is returned.
8945 The value may be stored in TARGET if TARGET is nonzero.
8946 TARGET is just a suggestion; callers must assume that
8947 the rtx returned may not be the same as TARGET.
8949 If TARGET is CONST0_RTX, it means that the value will be ignored.
8951 If TMODE is not VOIDmode, it suggests generating the
8952 result in mode TMODE. But this is done only when convenient.
8953 Otherwise, TMODE is ignored and the value generated in its natural mode.
8954 TMODE is just a suggestion; callers must assume that
8955 the rtx returned may not have mode TMODE.
8957 Note that TARGET may have neither TMODE nor MODE. In that case, it
8958 probably will not be used.
8960 If MODIFIER is EXPAND_SUM then when EXP is an addition
8961 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8962 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8963 products as above, or REG or MEM, or constant.
8964 Ordinarily in such cases we would output mul or add instructions
8965 and then return a pseudo reg containing the sum.
8967 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8968 it also marks a label as absolutely required (it can't be dead).
8969 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8970 This is used for outputting expressions used in initializers.
8972 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8973 with a constant address even if that address is not normally legitimate.
8974 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8976 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8977 a call parameter. Such targets require special care as we haven't yet
8978 marked TARGET so that it's safe from being trashed by libcalls. We
8979 don't want to use TARGET for anything but the final result;
8980 Intermediate values must go elsewhere. Additionally, calls to
8981 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8983 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8984 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8985 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8986 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8987 recursively.
8988 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8989 then *ALT_RTL is set to TARGET (before legitimziation).
8991 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8992 In this case, we don't adjust a returned MEM rtx that wouldn't be
8993 sufficiently aligned for its mode; instead, it's up to the caller
8994 to deal with it afterwards. This is used to make sure that unaligned
8995 base objects for which out-of-bounds accesses are supported, for
8996 example record types with trailing arrays, aren't realigned behind
8997 the back of the caller.
8998 The normal operating mode is to pass FALSE for this parameter. */
9001 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9002 enum expand_modifier modifier, rtx *alt_rtl,
9003 bool inner_reference_p)
9005 rtx ret;
9007 /* Handle ERROR_MARK before anybody tries to access its type. */
9008 if (TREE_CODE (exp) == ERROR_MARK
9009 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9011 ret = CONST0_RTX (tmode);
9012 return ret ? ret : const0_rtx;
9015 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9016 inner_reference_p);
9017 return ret;
9020 /* Try to expand the conditional expression which is represented by
9021 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9022 return the rtl reg which represents the result. Otherwise return
9023 NULL_RTX. */
9025 static rtx
9026 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9027 tree treeop1 ATTRIBUTE_UNUSED,
9028 tree treeop2 ATTRIBUTE_UNUSED)
9030 rtx insn;
9031 rtx op00, op01, op1, op2;
9032 enum rtx_code comparison_code;
9033 machine_mode comparison_mode;
9034 gimple *srcstmt;
9035 rtx temp;
9036 tree type = TREE_TYPE (treeop1);
9037 int unsignedp = TYPE_UNSIGNED (type);
9038 machine_mode mode = TYPE_MODE (type);
9039 machine_mode orig_mode = mode;
9040 static bool expanding_cond_expr_using_cmove = false;
9042 /* Conditional move expansion can end up TERing two operands which,
9043 when recursively hitting conditional expressions can result in
9044 exponential behavior if the cmove expansion ultimatively fails.
9045 It's hardly profitable to TER a cmove into a cmove so avoid doing
9046 that by failing early if we end up recursing. */
9047 if (expanding_cond_expr_using_cmove)
9048 return NULL_RTX;
9050 /* If we cannot do a conditional move on the mode, try doing it
9051 with the promoted mode. */
9052 if (!can_conditionally_move_p (mode))
9054 mode = promote_mode (type, mode, &unsignedp);
9055 if (!can_conditionally_move_p (mode))
9056 return NULL_RTX;
9057 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9059 else
9060 temp = assign_temp (type, 0, 1);
9062 expanding_cond_expr_using_cmove = true;
9063 start_sequence ();
9064 expand_operands (treeop1, treeop2,
9065 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9066 EXPAND_NORMAL);
9068 if (TREE_CODE (treeop0) == SSA_NAME
9069 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9071 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9072 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9073 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9074 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9075 comparison_mode = TYPE_MODE (type);
9076 unsignedp = TYPE_UNSIGNED (type);
9077 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9079 else if (COMPARISON_CLASS_P (treeop0))
9081 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9082 enum tree_code cmpcode = TREE_CODE (treeop0);
9083 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9084 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9085 unsignedp = TYPE_UNSIGNED (type);
9086 comparison_mode = TYPE_MODE (type);
9087 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9089 else
9091 op00 = expand_normal (treeop0);
9092 op01 = const0_rtx;
9093 comparison_code = NE;
9094 comparison_mode = GET_MODE (op00);
9095 if (comparison_mode == VOIDmode)
9096 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9098 expanding_cond_expr_using_cmove = false;
9100 if (GET_MODE (op1) != mode)
9101 op1 = gen_lowpart (mode, op1);
9103 if (GET_MODE (op2) != mode)
9104 op2 = gen_lowpart (mode, op2);
9106 /* Try to emit the conditional move. */
9107 insn = emit_conditional_move (temp,
9108 { comparison_code, op00, op01,
9109 comparison_mode },
9110 op1, op2, mode,
9111 unsignedp);
9113 /* If we could do the conditional move, emit the sequence,
9114 and return. */
9115 if (insn)
9117 rtx_insn *seq = get_insns ();
9118 end_sequence ();
9119 emit_insn (seq);
9120 return convert_modes (orig_mode, mode, temp, 0);
9123 /* Otherwise discard the sequence and fall back to code with
9124 branches. */
9125 end_sequence ();
9126 return NULL_RTX;
9129 /* A helper function for expand_expr_real_2 to be used with a
9130 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9131 is nonzero, with alignment ALIGN in bits.
9132 Store the value at TARGET if possible (if TARGET is nonzero).
9133 Regardless of TARGET, we return the rtx for where the value is placed.
9134 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9135 then *ALT_RTL is set to TARGET (before legitimziation). */
9137 static rtx
9138 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9139 unsigned int align, rtx target, rtx *alt_rtl)
9141 enum insn_code icode;
9143 if ((icode = optab_handler (movmisalign_optab, mode))
9144 != CODE_FOR_nothing)
9146 class expand_operand ops[2];
9148 /* We've already validated the memory, and we're creating a
9149 new pseudo destination. The predicates really can't fail,
9150 nor can the generator. */
9151 create_output_operand (&ops[0], NULL_RTX, mode);
9152 create_fixed_operand (&ops[1], temp);
9153 expand_insn (icode, 2, ops);
9154 temp = ops[0].value;
9156 else if (targetm.slow_unaligned_access (mode, align))
9157 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9158 0, unsignedp, target,
9159 mode, mode, false, alt_rtl);
9160 return temp;
9163 /* Helper function of expand_expr_2, expand a division or modulo.
9164 op0 and op1 should be already expanded treeop0 and treeop1, using
9165 expand_operands. */
9167 static rtx
9168 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9169 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9171 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9172 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9173 if (SCALAR_INT_MODE_P (mode)
9174 && optimize >= 2
9175 && get_range_pos_neg (treeop0) == 1
9176 && get_range_pos_neg (treeop1) == 1)
9178 /* If both arguments are known to be positive when interpreted
9179 as signed, we can expand it as both signed and unsigned
9180 division or modulo. Choose the cheaper sequence in that case. */
9181 bool speed_p = optimize_insn_for_speed_p ();
9182 do_pending_stack_adjust ();
9183 start_sequence ();
9184 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9185 rtx_insn *uns_insns = get_insns ();
9186 end_sequence ();
9187 start_sequence ();
9188 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9189 rtx_insn *sgn_insns = get_insns ();
9190 end_sequence ();
9191 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9192 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9194 /* If costs are the same then use as tie breaker the other other
9195 factor. */
9196 if (uns_cost == sgn_cost)
9198 uns_cost = seq_cost (uns_insns, !speed_p);
9199 sgn_cost = seq_cost (sgn_insns, !speed_p);
9202 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9204 emit_insn (uns_insns);
9205 return uns_ret;
9207 emit_insn (sgn_insns);
9208 return sgn_ret;
9210 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9214 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9215 enum expand_modifier modifier)
9217 rtx op0, op1, op2, temp;
9218 rtx_code_label *lab;
9219 tree type;
9220 int unsignedp;
9221 machine_mode mode;
9222 scalar_int_mode int_mode;
9223 enum tree_code code = ops->code;
9224 optab this_optab;
9225 rtx subtarget, original_target;
9226 int ignore;
9227 bool reduce_bit_field;
9228 location_t loc = ops->location;
9229 tree treeop0, treeop1, treeop2;
9230 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9231 ? reduce_to_bit_field_precision ((expr), \
9232 target, \
9233 type) \
9234 : (expr))
9236 type = ops->type;
9237 mode = TYPE_MODE (type);
9238 unsignedp = TYPE_UNSIGNED (type);
9240 treeop0 = ops->op0;
9241 treeop1 = ops->op1;
9242 treeop2 = ops->op2;
9244 /* We should be called only on simple (binary or unary) expressions,
9245 exactly those that are valid in gimple expressions that aren't
9246 GIMPLE_SINGLE_RHS (or invalid). */
9247 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9248 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9249 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9251 ignore = (target == const0_rtx
9252 || ((CONVERT_EXPR_CODE_P (code)
9253 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9254 && TREE_CODE (type) == VOID_TYPE));
9256 /* We should be called only if we need the result. */
9257 gcc_assert (!ignore);
9259 /* An operation in what may be a bit-field type needs the
9260 result to be reduced to the precision of the bit-field type,
9261 which is narrower than that of the type's mode. */
9262 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9263 && !type_has_mode_precision_p (type));
9265 if (reduce_bit_field
9266 && (modifier == EXPAND_STACK_PARM
9267 || (target && GET_MODE (target) != mode)))
9268 target = 0;
9270 /* Use subtarget as the target for operand 0 of a binary operation. */
9271 subtarget = get_subtarget (target);
9272 original_target = target;
9274 switch (code)
9276 case NON_LVALUE_EXPR:
9277 case PAREN_EXPR:
9278 CASE_CONVERT:
9279 if (treeop0 == error_mark_node)
9280 return const0_rtx;
9282 if (TREE_CODE (type) == UNION_TYPE)
9284 tree valtype = TREE_TYPE (treeop0);
9286 /* If both input and output are BLKmode, this conversion isn't doing
9287 anything except possibly changing memory attribute. */
9288 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9290 rtx result = expand_expr (treeop0, target, tmode,
9291 modifier);
9293 result = copy_rtx (result);
9294 set_mem_attributes (result, type, 0);
9295 return result;
9298 if (target == 0)
9300 if (TYPE_MODE (type) != BLKmode)
9301 target = gen_reg_rtx (TYPE_MODE (type));
9302 else
9303 target = assign_temp (type, 1, 1);
9306 if (MEM_P (target))
9307 /* Store data into beginning of memory target. */
9308 store_expr (treeop0,
9309 adjust_address (target, TYPE_MODE (valtype), 0),
9310 modifier == EXPAND_STACK_PARM,
9311 false, TYPE_REVERSE_STORAGE_ORDER (type));
9313 else
9315 gcc_assert (REG_P (target)
9316 && !TYPE_REVERSE_STORAGE_ORDER (type));
9318 /* Store this field into a union of the proper type. */
9319 poly_uint64 op0_size
9320 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9321 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9322 store_field (target,
9323 /* The conversion must be constructed so that
9324 we know at compile time how many bits
9325 to preserve. */
9326 ordered_min (op0_size, union_size),
9327 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9328 false, false);
9331 /* Return the entire union. */
9332 return target;
9335 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9337 op0 = expand_expr (treeop0, target, VOIDmode,
9338 modifier);
9340 /* If the signedness of the conversion differs and OP0 is
9341 a promoted SUBREG, clear that indication since we now
9342 have to do the proper extension. */
9343 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9344 && GET_CODE (op0) == SUBREG)
9345 SUBREG_PROMOTED_VAR_P (op0) = 0;
9347 return REDUCE_BIT_FIELD (op0);
9350 op0 = expand_expr (treeop0, NULL_RTX, mode,
9351 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9352 if (GET_MODE (op0) == mode)
9355 /* If OP0 is a constant, just convert it into the proper mode. */
9356 else if (CONSTANT_P (op0))
9358 tree inner_type = TREE_TYPE (treeop0);
9359 machine_mode inner_mode = GET_MODE (op0);
9361 if (inner_mode == VOIDmode)
9362 inner_mode = TYPE_MODE (inner_type);
9364 if (modifier == EXPAND_INITIALIZER)
9365 op0 = lowpart_subreg (mode, op0, inner_mode);
9366 else
9367 op0= convert_modes (mode, inner_mode, op0,
9368 TYPE_UNSIGNED (inner_type));
9371 else if (modifier == EXPAND_INITIALIZER)
9372 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9373 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9375 else if (target == 0)
9376 op0 = convert_to_mode (mode, op0,
9377 TYPE_UNSIGNED (TREE_TYPE
9378 (treeop0)));
9379 else
9381 convert_move (target, op0,
9382 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9383 op0 = target;
9386 return REDUCE_BIT_FIELD (op0);
9388 case ADDR_SPACE_CONVERT_EXPR:
9390 tree treeop0_type = TREE_TYPE (treeop0);
9392 gcc_assert (POINTER_TYPE_P (type));
9393 gcc_assert (POINTER_TYPE_P (treeop0_type));
9395 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9396 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9398 /* Conversions between pointers to the same address space should
9399 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9400 gcc_assert (as_to != as_from);
9402 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9404 /* Ask target code to handle conversion between pointers
9405 to overlapping address spaces. */
9406 if (targetm.addr_space.subset_p (as_to, as_from)
9407 || targetm.addr_space.subset_p (as_from, as_to))
9409 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9411 else
9413 /* For disjoint address spaces, converting anything but a null
9414 pointer invokes undefined behavior. We truncate or extend the
9415 value as if we'd converted via integers, which handles 0 as
9416 required, and all others as the programmer likely expects. */
9417 #ifndef POINTERS_EXTEND_UNSIGNED
9418 const int POINTERS_EXTEND_UNSIGNED = 1;
9419 #endif
9420 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9421 op0, POINTERS_EXTEND_UNSIGNED);
9423 gcc_assert (op0);
9424 return op0;
9427 case POINTER_PLUS_EXPR:
9428 /* Even though the sizetype mode and the pointer's mode can be different
9429 expand is able to handle this correctly and get the correct result out
9430 of the PLUS_EXPR code. */
9431 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9432 if sizetype precision is smaller than pointer precision. */
9433 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9434 treeop1 = fold_convert_loc (loc, type,
9435 fold_convert_loc (loc, ssizetype,
9436 treeop1));
9437 /* If sizetype precision is larger than pointer precision, truncate the
9438 offset to have matching modes. */
9439 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9440 treeop1 = fold_convert_loc (loc, type, treeop1);
9441 /* FALLTHRU */
9443 case PLUS_EXPR:
9444 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9445 something else, make sure we add the register to the constant and
9446 then to the other thing. This case can occur during strength
9447 reduction and doing it this way will produce better code if the
9448 frame pointer or argument pointer is eliminated.
9450 fold-const.cc will ensure that the constant is always in the inner
9451 PLUS_EXPR, so the only case we need to do anything about is if
9452 sp, ap, or fp is our second argument, in which case we must swap
9453 the innermost first argument and our second argument. */
9455 if (TREE_CODE (treeop0) == PLUS_EXPR
9456 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9457 && VAR_P (treeop1)
9458 && (DECL_RTL (treeop1) == frame_pointer_rtx
9459 || DECL_RTL (treeop1) == stack_pointer_rtx
9460 || DECL_RTL (treeop1) == arg_pointer_rtx))
9462 gcc_unreachable ();
9465 /* If the result is to be ptr_mode and we are adding an integer to
9466 something, we might be forming a constant. So try to use
9467 plus_constant. If it produces a sum and we can't accept it,
9468 use force_operand. This allows P = &ARR[const] to generate
9469 efficient code on machines where a SYMBOL_REF is not a valid
9470 address.
9472 If this is an EXPAND_SUM call, always return the sum. */
9473 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9474 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9476 if (modifier == EXPAND_STACK_PARM)
9477 target = 0;
9478 if (TREE_CODE (treeop0) == INTEGER_CST
9479 && HWI_COMPUTABLE_MODE_P (mode)
9480 && TREE_CONSTANT (treeop1))
9482 rtx constant_part;
9483 HOST_WIDE_INT wc;
9484 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9486 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9487 EXPAND_SUM);
9488 /* Use wi::shwi to ensure that the constant is
9489 truncated according to the mode of OP1, then sign extended
9490 to a HOST_WIDE_INT. Using the constant directly can result
9491 in non-canonical RTL in a 64x32 cross compile. */
9492 wc = TREE_INT_CST_LOW (treeop0);
9493 constant_part =
9494 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9495 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9496 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9497 op1 = force_operand (op1, target);
9498 return REDUCE_BIT_FIELD (op1);
9501 else if (TREE_CODE (treeop1) == INTEGER_CST
9502 && HWI_COMPUTABLE_MODE_P (mode)
9503 && TREE_CONSTANT (treeop0))
9505 rtx constant_part;
9506 HOST_WIDE_INT wc;
9507 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9509 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9510 (modifier == EXPAND_INITIALIZER
9511 ? EXPAND_INITIALIZER : EXPAND_SUM));
9512 if (! CONSTANT_P (op0))
9514 op1 = expand_expr (treeop1, NULL_RTX,
9515 VOIDmode, modifier);
9516 /* Return a PLUS if modifier says it's OK. */
9517 if (modifier == EXPAND_SUM
9518 || modifier == EXPAND_INITIALIZER)
9519 return simplify_gen_binary (PLUS, mode, op0, op1);
9520 goto binop2;
9522 /* Use wi::shwi to ensure that the constant is
9523 truncated according to the mode of OP1, then sign extended
9524 to a HOST_WIDE_INT. Using the constant directly can result
9525 in non-canonical RTL in a 64x32 cross compile. */
9526 wc = TREE_INT_CST_LOW (treeop1);
9527 constant_part
9528 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9529 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9530 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9531 op0 = force_operand (op0, target);
9532 return REDUCE_BIT_FIELD (op0);
9536 /* Use TER to expand pointer addition of a negated value
9537 as pointer subtraction. */
9538 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9539 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9540 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9541 && TREE_CODE (treeop1) == SSA_NAME
9542 && TYPE_MODE (TREE_TYPE (treeop0))
9543 == TYPE_MODE (TREE_TYPE (treeop1)))
9545 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9546 if (def)
9548 treeop1 = gimple_assign_rhs1 (def);
9549 code = MINUS_EXPR;
9550 goto do_minus;
9554 /* No sense saving up arithmetic to be done
9555 if it's all in the wrong mode to form part of an address.
9556 And force_operand won't know whether to sign-extend or
9557 zero-extend. */
9558 if (modifier != EXPAND_INITIALIZER
9559 && (modifier != EXPAND_SUM || mode != ptr_mode))
9561 expand_operands (treeop0, treeop1,
9562 subtarget, &op0, &op1, modifier);
9563 if (op0 == const0_rtx)
9564 return op1;
9565 if (op1 == const0_rtx)
9566 return op0;
9567 goto binop2;
9570 expand_operands (treeop0, treeop1,
9571 subtarget, &op0, &op1, modifier);
9572 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9574 case MINUS_EXPR:
9575 case POINTER_DIFF_EXPR:
9576 do_minus:
9577 /* For initializers, we are allowed to return a MINUS of two
9578 symbolic constants. Here we handle all cases when both operands
9579 are constant. */
9580 /* Handle difference of two symbolic constants,
9581 for the sake of an initializer. */
9582 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9583 && really_constant_p (treeop0)
9584 && really_constant_p (treeop1))
9586 expand_operands (treeop0, treeop1,
9587 NULL_RTX, &op0, &op1, modifier);
9588 return simplify_gen_binary (MINUS, mode, op0, op1);
9591 /* No sense saving up arithmetic to be done
9592 if it's all in the wrong mode to form part of an address.
9593 And force_operand won't know whether to sign-extend or
9594 zero-extend. */
9595 if (modifier != EXPAND_INITIALIZER
9596 && (modifier != EXPAND_SUM || mode != ptr_mode))
9597 goto binop;
9599 expand_operands (treeop0, treeop1,
9600 subtarget, &op0, &op1, modifier);
9602 /* Convert A - const to A + (-const). */
9603 if (CONST_INT_P (op1))
9605 op1 = negate_rtx (mode, op1);
9606 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9609 goto binop2;
9611 case WIDEN_MULT_PLUS_EXPR:
9612 case WIDEN_MULT_MINUS_EXPR:
9613 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9614 op2 = expand_normal (treeop2);
9615 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9616 target, unsignedp);
9617 return target;
9619 case WIDEN_MULT_EXPR:
9620 /* If first operand is constant, swap them.
9621 Thus the following special case checks need only
9622 check the second operand. */
9623 if (TREE_CODE (treeop0) == INTEGER_CST)
9624 std::swap (treeop0, treeop1);
9626 /* First, check if we have a multiplication of one signed and one
9627 unsigned operand. */
9628 if (TREE_CODE (treeop1) != INTEGER_CST
9629 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9630 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9632 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9633 this_optab = usmul_widen_optab;
9634 if (find_widening_optab_handler (this_optab, mode, innermode)
9635 != CODE_FOR_nothing)
9637 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9638 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9639 EXPAND_NORMAL);
9640 else
9641 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9642 EXPAND_NORMAL);
9643 /* op0 and op1 might still be constant, despite the above
9644 != INTEGER_CST check. Handle it. */
9645 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9647 op0 = convert_modes (mode, innermode, op0, true);
9648 op1 = convert_modes (mode, innermode, op1, false);
9649 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9650 target, unsignedp));
9652 goto binop3;
9655 /* Check for a multiplication with matching signedness. */
9656 else if ((TREE_CODE (treeop1) == INTEGER_CST
9657 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9658 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9659 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9661 tree op0type = TREE_TYPE (treeop0);
9662 machine_mode innermode = TYPE_MODE (op0type);
9663 bool zextend_p = TYPE_UNSIGNED (op0type);
9664 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9665 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9667 if (TREE_CODE (treeop0) != INTEGER_CST)
9669 if (find_widening_optab_handler (this_optab, mode, innermode)
9670 != CODE_FOR_nothing)
9672 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9673 EXPAND_NORMAL);
9674 /* op0 and op1 might still be constant, despite the above
9675 != INTEGER_CST check. Handle it. */
9676 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9678 widen_mult_const:
9679 op0 = convert_modes (mode, innermode, op0, zextend_p);
9681 = convert_modes (mode, innermode, op1,
9682 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9683 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9684 target,
9685 unsignedp));
9687 temp = expand_widening_mult (mode, op0, op1, target,
9688 unsignedp, this_optab);
9689 return REDUCE_BIT_FIELD (temp);
9691 if (find_widening_optab_handler (other_optab, mode, innermode)
9692 != CODE_FOR_nothing
9693 && innermode == word_mode)
9695 rtx htem, hipart;
9696 op0 = expand_normal (treeop0);
9697 op1 = expand_normal (treeop1);
9698 /* op0 and op1 might be constants, despite the above
9699 != INTEGER_CST check. Handle it. */
9700 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9701 goto widen_mult_const;
9702 temp = expand_binop (mode, other_optab, op0, op1, target,
9703 unsignedp, OPTAB_LIB_WIDEN);
9704 hipart = gen_highpart (word_mode, temp);
9705 htem = expand_mult_highpart_adjust (word_mode, hipart,
9706 op0, op1, hipart,
9707 zextend_p);
9708 if (htem != hipart)
9709 emit_move_insn (hipart, htem);
9710 return REDUCE_BIT_FIELD (temp);
9714 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9715 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9716 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9717 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9719 case MULT_EXPR:
9720 /* If this is a fixed-point operation, then we cannot use the code
9721 below because "expand_mult" doesn't support sat/no-sat fixed-point
9722 multiplications. */
9723 if (ALL_FIXED_POINT_MODE_P (mode))
9724 goto binop;
9726 /* If first operand is constant, swap them.
9727 Thus the following special case checks need only
9728 check the second operand. */
9729 if (TREE_CODE (treeop0) == INTEGER_CST)
9730 std::swap (treeop0, treeop1);
9732 /* Attempt to return something suitable for generating an
9733 indexed address, for machines that support that. */
9735 if (modifier == EXPAND_SUM && mode == ptr_mode
9736 && tree_fits_shwi_p (treeop1))
9738 tree exp1 = treeop1;
9740 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9741 EXPAND_SUM);
9743 if (!REG_P (op0))
9744 op0 = force_operand (op0, NULL_RTX);
9745 if (!REG_P (op0))
9746 op0 = copy_to_mode_reg (mode, op0);
9748 op1 = gen_int_mode (tree_to_shwi (exp1),
9749 TYPE_MODE (TREE_TYPE (exp1)));
9750 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9753 if (modifier == EXPAND_STACK_PARM)
9754 target = 0;
9756 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9758 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9759 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9760 if (def_stmt0
9761 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9762 def_stmt0 = NULL;
9763 if (def_stmt1
9764 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9765 def_stmt1 = NULL;
9767 if (def_stmt0 || def_stmt1)
9769 /* X / Y * Y can be expanded as X - X % Y too.
9770 Choose the cheaper sequence of those two. */
9771 if (def_stmt0)
9772 treeop0 = gimple_assign_rhs1 (def_stmt0);
9773 else
9775 treeop1 = treeop0;
9776 treeop0 = gimple_assign_rhs1 (def_stmt1);
9778 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9779 EXPAND_NORMAL);
9780 bool speed_p = optimize_insn_for_speed_p ();
9781 do_pending_stack_adjust ();
9782 start_sequence ();
9783 rtx divmul_ret
9784 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9785 op0, op1, NULL_RTX, unsignedp);
9786 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9787 unsignedp);
9788 rtx_insn *divmul_insns = get_insns ();
9789 end_sequence ();
9790 start_sequence ();
9791 rtx modsub_ret
9792 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9793 op0, op1, NULL_RTX, unsignedp);
9794 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9795 optab_default);
9796 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9797 target, unsignedp, OPTAB_LIB_WIDEN);
9798 rtx_insn *modsub_insns = get_insns ();
9799 end_sequence ();
9800 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9801 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9802 /* If costs are the same then use as tie breaker the other other
9803 factor. */
9804 if (divmul_cost == modsub_cost)
9806 divmul_cost = seq_cost (divmul_insns, !speed_p);
9807 modsub_cost = seq_cost (modsub_insns, !speed_p);
9810 if (divmul_cost <= modsub_cost)
9812 emit_insn (divmul_insns);
9813 return REDUCE_BIT_FIELD (divmul_ret);
9815 emit_insn (modsub_insns);
9816 return REDUCE_BIT_FIELD (modsub_ret);
9820 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9822 /* Expand X*Y as X&-Y when Y must be zero or one. */
9823 if (SCALAR_INT_MODE_P (mode))
9825 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9826 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9828 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9829 if (bit0_p && bit1_p)
9830 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9832 if (bit0_p || bit1_p)
9834 bool speed = optimize_insn_for_speed_p ();
9835 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9836 struct algorithm algorithm;
9837 enum mult_variant variant;
9838 if (CONST_INT_P (op1)
9839 ? !choose_mult_variant (mode, INTVAL (op1),
9840 &algorithm, &variant, cost)
9841 : cost < mul_cost (speed, mode))
9843 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9844 op1, target)
9845 : expand_and (mode, op0,
9846 negate_rtx (mode, op1),
9847 target);
9848 return REDUCE_BIT_FIELD (target);
9853 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9855 case TRUNC_MOD_EXPR:
9856 case FLOOR_MOD_EXPR:
9857 case CEIL_MOD_EXPR:
9858 case ROUND_MOD_EXPR:
9860 case TRUNC_DIV_EXPR:
9861 case FLOOR_DIV_EXPR:
9862 case CEIL_DIV_EXPR:
9863 case ROUND_DIV_EXPR:
9864 case EXACT_DIV_EXPR:
9865 /* If this is a fixed-point operation, then we cannot use the code
9866 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9867 divisions. */
9868 if (ALL_FIXED_POINT_MODE_P (mode))
9869 goto binop;
9871 if (modifier == EXPAND_STACK_PARM)
9872 target = 0;
9873 /* Possible optimization: compute the dividend with EXPAND_SUM
9874 then if the divisor is constant can optimize the case
9875 where some terms of the dividend have coeffs divisible by it. */
9876 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9877 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9878 target, unsignedp);
9880 case RDIV_EXPR:
9881 goto binop;
9883 case MULT_HIGHPART_EXPR:
9884 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9885 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9886 gcc_assert (temp);
9887 return temp;
9889 case FIXED_CONVERT_EXPR:
9890 op0 = expand_normal (treeop0);
9891 if (target == 0 || modifier == EXPAND_STACK_PARM)
9892 target = gen_reg_rtx (mode);
9894 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9895 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9896 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9897 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9898 else
9899 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9900 return target;
9902 case FIX_TRUNC_EXPR:
9903 op0 = expand_normal (treeop0);
9904 if (target == 0 || modifier == EXPAND_STACK_PARM)
9905 target = gen_reg_rtx (mode);
9906 expand_fix (target, op0, unsignedp);
9907 return target;
9909 case FLOAT_EXPR:
9910 op0 = expand_normal (treeop0);
9911 if (target == 0 || modifier == EXPAND_STACK_PARM)
9912 target = gen_reg_rtx (mode);
9913 /* expand_float can't figure out what to do if FROM has VOIDmode.
9914 So give it the correct mode. With -O, cse will optimize this. */
9915 if (GET_MODE (op0) == VOIDmode)
9916 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9917 op0);
9918 expand_float (target, op0,
9919 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9920 return target;
9922 case NEGATE_EXPR:
9923 op0 = expand_expr (treeop0, subtarget,
9924 VOIDmode, EXPAND_NORMAL);
9925 if (modifier == EXPAND_STACK_PARM)
9926 target = 0;
9927 temp = expand_unop (mode,
9928 optab_for_tree_code (NEGATE_EXPR, type,
9929 optab_default),
9930 op0, target, 0);
9931 gcc_assert (temp);
9932 return REDUCE_BIT_FIELD (temp);
9934 case ABS_EXPR:
9935 case ABSU_EXPR:
9936 op0 = expand_expr (treeop0, subtarget,
9937 VOIDmode, EXPAND_NORMAL);
9938 if (modifier == EXPAND_STACK_PARM)
9939 target = 0;
9941 /* ABS_EXPR is not valid for complex arguments. */
9942 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9943 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9945 /* Unsigned abs is simply the operand. Testing here means we don't
9946 risk generating incorrect code below. */
9947 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9948 return op0;
9950 return expand_abs (mode, op0, target, unsignedp,
9951 safe_from_p (target, treeop0, 1));
9953 case MAX_EXPR:
9954 case MIN_EXPR:
9955 target = original_target;
9956 if (target == 0
9957 || modifier == EXPAND_STACK_PARM
9958 || (MEM_P (target) && MEM_VOLATILE_P (target))
9959 || GET_MODE (target) != mode
9960 || (REG_P (target)
9961 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9962 target = gen_reg_rtx (mode);
9963 expand_operands (treeop0, treeop1,
9964 target, &op0, &op1, EXPAND_NORMAL);
9966 /* First try to do it with a special MIN or MAX instruction.
9967 If that does not win, use a conditional jump to select the proper
9968 value. */
9969 this_optab = optab_for_tree_code (code, type, optab_default);
9970 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9971 OPTAB_WIDEN);
9972 if (temp != 0)
9973 return temp;
9975 if (VECTOR_TYPE_P (type))
9976 gcc_unreachable ();
9978 /* At this point, a MEM target is no longer useful; we will get better
9979 code without it. */
9981 if (! REG_P (target))
9982 target = gen_reg_rtx (mode);
9984 /* If op1 was placed in target, swap op0 and op1. */
9985 if (target != op0 && target == op1)
9986 std::swap (op0, op1);
9988 /* We generate better code and avoid problems with op1 mentioning
9989 target by forcing op1 into a pseudo if it isn't a constant. */
9990 if (! CONSTANT_P (op1))
9991 op1 = force_reg (mode, op1);
9994 enum rtx_code comparison_code;
9995 rtx cmpop1 = op1;
9997 if (code == MAX_EXPR)
9998 comparison_code = unsignedp ? GEU : GE;
9999 else
10000 comparison_code = unsignedp ? LEU : LE;
10002 /* Canonicalize to comparisons against 0. */
10003 if (op1 == const1_rtx)
10005 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10006 or (a != 0 ? a : 1) for unsigned.
10007 For MIN we are safe converting (a <= 1 ? a : 1)
10008 into (a <= 0 ? a : 1) */
10009 cmpop1 = const0_rtx;
10010 if (code == MAX_EXPR)
10011 comparison_code = unsignedp ? NE : GT;
10013 if (op1 == constm1_rtx && !unsignedp)
10015 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10016 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10017 cmpop1 = const0_rtx;
10018 if (code == MIN_EXPR)
10019 comparison_code = LT;
10022 /* Use a conditional move if possible. */
10023 if (can_conditionally_move_p (mode))
10025 rtx insn;
10027 start_sequence ();
10029 /* Try to emit the conditional move. */
10030 insn = emit_conditional_move (target,
10031 { comparison_code,
10032 op0, cmpop1, mode },
10033 op0, op1, mode,
10034 unsignedp);
10036 /* If we could do the conditional move, emit the sequence,
10037 and return. */
10038 if (insn)
10040 rtx_insn *seq = get_insns ();
10041 end_sequence ();
10042 emit_insn (seq);
10043 return target;
10046 /* Otherwise discard the sequence and fall back to code with
10047 branches. */
10048 end_sequence ();
10051 if (target != op0)
10052 emit_move_insn (target, op0);
10054 lab = gen_label_rtx ();
10055 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10056 unsignedp, mode, NULL_RTX, NULL, lab,
10057 profile_probability::uninitialized ());
10059 emit_move_insn (target, op1);
10060 emit_label (lab);
10061 return target;
10063 case BIT_NOT_EXPR:
10064 op0 = expand_expr (treeop0, subtarget,
10065 VOIDmode, EXPAND_NORMAL);
10066 if (modifier == EXPAND_STACK_PARM)
10067 target = 0;
10068 /* In case we have to reduce the result to bitfield precision
10069 for unsigned bitfield expand this as XOR with a proper constant
10070 instead. */
10071 if (reduce_bit_field && TYPE_UNSIGNED (type))
10073 int_mode = SCALAR_INT_TYPE_MODE (type);
10074 wide_int mask = wi::mask (TYPE_PRECISION (type),
10075 false, GET_MODE_PRECISION (int_mode));
10077 temp = expand_binop (int_mode, xor_optab, op0,
10078 immed_wide_int_const (mask, int_mode),
10079 target, 1, OPTAB_LIB_WIDEN);
10081 else
10082 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10083 gcc_assert (temp);
10084 return temp;
10086 /* ??? Can optimize bitwise operations with one arg constant.
10087 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10088 and (a bitwise1 b) bitwise2 b (etc)
10089 but that is probably not worth while. */
10091 case BIT_AND_EXPR:
10092 case BIT_IOR_EXPR:
10093 case BIT_XOR_EXPR:
10094 goto binop;
10096 case LROTATE_EXPR:
10097 case RROTATE_EXPR:
10098 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10099 || type_has_mode_precision_p (type));
10100 /* fall through */
10102 case LSHIFT_EXPR:
10103 case RSHIFT_EXPR:
10105 /* If this is a fixed-point operation, then we cannot use the code
10106 below because "expand_shift" doesn't support sat/no-sat fixed-point
10107 shifts. */
10108 if (ALL_FIXED_POINT_MODE_P (mode))
10109 goto binop;
10111 if (! safe_from_p (subtarget, treeop1, 1))
10112 subtarget = 0;
10113 if (modifier == EXPAND_STACK_PARM)
10114 target = 0;
10115 op0 = expand_expr (treeop0, subtarget,
10116 VOIDmode, EXPAND_NORMAL);
10118 /* Left shift optimization when shifting across word_size boundary.
10120 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10121 there isn't native instruction to support this wide mode
10122 left shift. Given below scenario:
10124 Type A = (Type) B << C
10126 |< T >|
10127 | dest_high | dest_low |
10129 | word_size |
10131 If the shift amount C caused we shift B to across the word
10132 size boundary, i.e part of B shifted into high half of
10133 destination register, and part of B remains in the low
10134 half, then GCC will use the following left shift expand
10135 logic:
10137 1. Initialize dest_low to B.
10138 2. Initialize every bit of dest_high to the sign bit of B.
10139 3. Logic left shift dest_low by C bit to finalize dest_low.
10140 The value of dest_low before this shift is kept in a temp D.
10141 4. Logic left shift dest_high by C.
10142 5. Logic right shift D by (word_size - C).
10143 6. Or the result of 4 and 5 to finalize dest_high.
10145 While, by checking gimple statements, if operand B is
10146 coming from signed extension, then we can simplify above
10147 expand logic into:
10149 1. dest_high = src_low >> (word_size - C).
10150 2. dest_low = src_low << C.
10152 We can use one arithmetic right shift to finish all the
10153 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10154 needed from 6 into 2.
10156 The case is similar for zero extension, except that we
10157 initialize dest_high to zero rather than copies of the sign
10158 bit from B. Furthermore, we need to use a logical right shift
10159 in this case.
10161 The choice of sign-extension versus zero-extension is
10162 determined entirely by whether or not B is signed and is
10163 independent of the current setting of unsignedp. */
10165 temp = NULL_RTX;
10166 if (code == LSHIFT_EXPR
10167 && target
10168 && REG_P (target)
10169 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10170 && mode == int_mode
10171 && TREE_CONSTANT (treeop1)
10172 && TREE_CODE (treeop0) == SSA_NAME)
10174 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10175 if (is_gimple_assign (def)
10176 && gimple_assign_rhs_code (def) == NOP_EXPR)
10178 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10179 (TREE_TYPE (gimple_assign_rhs1 (def)));
10181 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10182 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10183 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10184 >= GET_MODE_BITSIZE (word_mode)))
10186 rtx_insn *seq, *seq_old;
10187 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10188 int_mode);
10189 bool extend_unsigned
10190 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10191 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10192 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10193 rtx dest_high = simplify_gen_subreg (word_mode, target,
10194 int_mode, high_off);
10195 HOST_WIDE_INT ramount = (BITS_PER_WORD
10196 - TREE_INT_CST_LOW (treeop1));
10197 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10199 start_sequence ();
10200 /* dest_high = src_low >> (word_size - C). */
10201 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10202 rshift, dest_high,
10203 extend_unsigned);
10204 if (temp != dest_high)
10205 emit_move_insn (dest_high, temp);
10207 /* dest_low = src_low << C. */
10208 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10209 treeop1, dest_low, unsignedp);
10210 if (temp != dest_low)
10211 emit_move_insn (dest_low, temp);
10213 seq = get_insns ();
10214 end_sequence ();
10215 temp = target ;
10217 if (have_insn_for (ASHIFT, int_mode))
10219 bool speed_p = optimize_insn_for_speed_p ();
10220 start_sequence ();
10221 rtx ret_old = expand_variable_shift (code, int_mode,
10222 op0, treeop1,
10223 target,
10224 unsignedp);
10226 seq_old = get_insns ();
10227 end_sequence ();
10228 if (seq_cost (seq, speed_p)
10229 >= seq_cost (seq_old, speed_p))
10231 seq = seq_old;
10232 temp = ret_old;
10235 emit_insn (seq);
10240 if (temp == NULL_RTX)
10241 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10242 unsignedp);
10243 if (code == LSHIFT_EXPR)
10244 temp = REDUCE_BIT_FIELD (temp);
10245 return temp;
10248 /* Could determine the answer when only additive constants differ. Also,
10249 the addition of one can be handled by changing the condition. */
10250 case LT_EXPR:
10251 case LE_EXPR:
10252 case GT_EXPR:
10253 case GE_EXPR:
10254 case EQ_EXPR:
10255 case NE_EXPR:
10256 case UNORDERED_EXPR:
10257 case ORDERED_EXPR:
10258 case UNLT_EXPR:
10259 case UNLE_EXPR:
10260 case UNGT_EXPR:
10261 case UNGE_EXPR:
10262 case UNEQ_EXPR:
10263 case LTGT_EXPR:
10265 temp = do_store_flag (ops,
10266 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10267 tmode != VOIDmode ? tmode : mode);
10268 if (temp)
10269 return temp;
10271 /* Use a compare and a jump for BLKmode comparisons, or for function
10272 type comparisons is have_canonicalize_funcptr_for_compare. */
10274 if ((target == 0
10275 || modifier == EXPAND_STACK_PARM
10276 || ! safe_from_p (target, treeop0, 1)
10277 || ! safe_from_p (target, treeop1, 1)
10278 /* Make sure we don't have a hard reg (such as function's return
10279 value) live across basic blocks, if not optimizing. */
10280 || (!optimize && REG_P (target)
10281 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10282 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10284 emit_move_insn (target, const0_rtx);
10286 rtx_code_label *lab1 = gen_label_rtx ();
10287 jumpifnot_1 (code, treeop0, treeop1, lab1,
10288 profile_probability::uninitialized ());
10290 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10291 emit_move_insn (target, constm1_rtx);
10292 else
10293 emit_move_insn (target, const1_rtx);
10295 emit_label (lab1);
10296 return target;
10298 case COMPLEX_EXPR:
10299 /* Get the rtx code of the operands. */
10300 op0 = expand_normal (treeop0);
10301 op1 = expand_normal (treeop1);
10303 if (!target)
10304 target = gen_reg_rtx (TYPE_MODE (type));
10305 else
10306 /* If target overlaps with op1, then either we need to force
10307 op1 into a pseudo (if target also overlaps with op0),
10308 or write the complex parts in reverse order. */
10309 switch (GET_CODE (target))
10311 case CONCAT:
10312 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10314 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10316 complex_expr_force_op1:
10317 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10318 emit_move_insn (temp, op1);
10319 op1 = temp;
10320 break;
10322 complex_expr_swap_order:
10323 /* Move the imaginary (op1) and real (op0) parts to their
10324 location. */
10325 write_complex_part (target, op1, true, true);
10326 write_complex_part (target, op0, false, false);
10328 return target;
10330 break;
10331 case MEM:
10332 temp = adjust_address_nv (target,
10333 GET_MODE_INNER (GET_MODE (target)), 0);
10334 if (reg_overlap_mentioned_p (temp, op1))
10336 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10337 temp = adjust_address_nv (target, imode,
10338 GET_MODE_SIZE (imode));
10339 if (reg_overlap_mentioned_p (temp, op0))
10340 goto complex_expr_force_op1;
10341 goto complex_expr_swap_order;
10343 break;
10344 default:
10345 if (reg_overlap_mentioned_p (target, op1))
10347 if (reg_overlap_mentioned_p (target, op0))
10348 goto complex_expr_force_op1;
10349 goto complex_expr_swap_order;
10351 break;
10354 /* Move the real (op0) and imaginary (op1) parts to their location. */
10355 write_complex_part (target, op0, false, true);
10356 write_complex_part (target, op1, true, false);
10358 return target;
10360 case WIDEN_SUM_EXPR:
10362 tree oprnd0 = treeop0;
10363 tree oprnd1 = treeop1;
10365 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10366 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10367 target, unsignedp);
10368 return target;
10371 case VEC_UNPACK_HI_EXPR:
10372 case VEC_UNPACK_LO_EXPR:
10373 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10374 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10376 op0 = expand_normal (treeop0);
10377 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10378 target, unsignedp);
10379 gcc_assert (temp);
10380 return temp;
10383 case VEC_UNPACK_FLOAT_HI_EXPR:
10384 case VEC_UNPACK_FLOAT_LO_EXPR:
10386 op0 = expand_normal (treeop0);
10387 /* The signedness is determined from input operand. */
10388 temp = expand_widen_pattern_expr
10389 (ops, op0, NULL_RTX, NULL_RTX,
10390 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10392 gcc_assert (temp);
10393 return temp;
10396 case VEC_WIDEN_MULT_HI_EXPR:
10397 case VEC_WIDEN_MULT_LO_EXPR:
10398 case VEC_WIDEN_MULT_EVEN_EXPR:
10399 case VEC_WIDEN_MULT_ODD_EXPR:
10400 case VEC_WIDEN_LSHIFT_HI_EXPR:
10401 case VEC_WIDEN_LSHIFT_LO_EXPR:
10402 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10403 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10404 target, unsignedp);
10405 gcc_assert (target);
10406 return target;
10408 case VEC_PACK_SAT_EXPR:
10409 case VEC_PACK_FIX_TRUNC_EXPR:
10410 mode = TYPE_MODE (TREE_TYPE (treeop0));
10411 subtarget = NULL_RTX;
10412 goto binop;
10414 case VEC_PACK_TRUNC_EXPR:
10415 if (VECTOR_BOOLEAN_TYPE_P (type)
10416 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10417 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10418 && SCALAR_INT_MODE_P (mode))
10420 class expand_operand eops[4];
10421 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10422 expand_operands (treeop0, treeop1,
10423 subtarget, &op0, &op1, EXPAND_NORMAL);
10424 this_optab = vec_pack_sbool_trunc_optab;
10425 enum insn_code icode = optab_handler (this_optab, imode);
10426 create_output_operand (&eops[0], target, mode);
10427 create_convert_operand_from (&eops[1], op0, imode, false);
10428 create_convert_operand_from (&eops[2], op1, imode, false);
10429 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10430 create_input_operand (&eops[3], temp, imode);
10431 expand_insn (icode, 4, eops);
10432 return eops[0].value;
10434 mode = TYPE_MODE (TREE_TYPE (treeop0));
10435 subtarget = NULL_RTX;
10436 goto binop;
10438 case VEC_PACK_FLOAT_EXPR:
10439 mode = TYPE_MODE (TREE_TYPE (treeop0));
10440 expand_operands (treeop0, treeop1,
10441 subtarget, &op0, &op1, EXPAND_NORMAL);
10442 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10443 optab_default);
10444 target = expand_binop (mode, this_optab, op0, op1, target,
10445 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10446 OPTAB_LIB_WIDEN);
10447 gcc_assert (target);
10448 return target;
10450 case VEC_PERM_EXPR:
10452 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10453 vec_perm_builder sel;
10454 if (TREE_CODE (treeop2) == VECTOR_CST
10455 && tree_to_vec_perm_builder (&sel, treeop2))
10457 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10458 temp = expand_vec_perm_const (mode, op0, op1, sel,
10459 sel_mode, target);
10461 else
10463 op2 = expand_normal (treeop2);
10464 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10466 gcc_assert (temp);
10467 return temp;
10470 case DOT_PROD_EXPR:
10472 tree oprnd0 = treeop0;
10473 tree oprnd1 = treeop1;
10474 tree oprnd2 = treeop2;
10476 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10477 op2 = expand_normal (oprnd2);
10478 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10479 target, unsignedp);
10480 return target;
10483 case SAD_EXPR:
10485 tree oprnd0 = treeop0;
10486 tree oprnd1 = treeop1;
10487 tree oprnd2 = treeop2;
10489 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10490 op2 = expand_normal (oprnd2);
10491 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10492 target, unsignedp);
10493 return target;
10496 case REALIGN_LOAD_EXPR:
10498 tree oprnd0 = treeop0;
10499 tree oprnd1 = treeop1;
10500 tree oprnd2 = treeop2;
10502 this_optab = optab_for_tree_code (code, type, optab_default);
10503 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10504 op2 = expand_normal (oprnd2);
10505 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10506 target, unsignedp);
10507 gcc_assert (temp);
10508 return temp;
10511 case COND_EXPR:
10513 /* A COND_EXPR with its type being VOID_TYPE represents a
10514 conditional jump and is handled in
10515 expand_gimple_cond_expr. */
10516 gcc_assert (!VOID_TYPE_P (type));
10518 /* Note that COND_EXPRs whose type is a structure or union
10519 are required to be constructed to contain assignments of
10520 a temporary variable, so that we can evaluate them here
10521 for side effect only. If type is void, we must do likewise. */
10523 gcc_assert (!TREE_ADDRESSABLE (type)
10524 && !ignore
10525 && TREE_TYPE (treeop1) != void_type_node
10526 && TREE_TYPE (treeop2) != void_type_node);
10528 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10529 if (temp)
10530 return temp;
10532 /* If we are not to produce a result, we have no target. Otherwise,
10533 if a target was specified use it; it will not be used as an
10534 intermediate target unless it is safe. If no target, use a
10535 temporary. */
10537 if (modifier != EXPAND_STACK_PARM
10538 && original_target
10539 && safe_from_p (original_target, treeop0, 1)
10540 && GET_MODE (original_target) == mode
10541 && !MEM_P (original_target))
10542 temp = original_target;
10543 else
10544 temp = assign_temp (type, 0, 1);
10546 do_pending_stack_adjust ();
10547 NO_DEFER_POP;
10548 rtx_code_label *lab0 = gen_label_rtx ();
10549 rtx_code_label *lab1 = gen_label_rtx ();
10550 jumpifnot (treeop0, lab0,
10551 profile_probability::uninitialized ());
10552 store_expr (treeop1, temp,
10553 modifier == EXPAND_STACK_PARM,
10554 false, false);
10556 emit_jump_insn (targetm.gen_jump (lab1));
10557 emit_barrier ();
10558 emit_label (lab0);
10559 store_expr (treeop2, temp,
10560 modifier == EXPAND_STACK_PARM,
10561 false, false);
10563 emit_label (lab1);
10564 OK_DEFER_POP;
10565 return temp;
10568 case VEC_DUPLICATE_EXPR:
10569 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10570 target = expand_vector_broadcast (mode, op0);
10571 gcc_assert (target);
10572 return target;
10574 case VEC_SERIES_EXPR:
10575 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10576 return expand_vec_series_expr (mode, op0, op1, target);
10578 case BIT_INSERT_EXPR:
10580 unsigned bitpos = tree_to_uhwi (treeop2);
10581 unsigned bitsize;
10582 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10583 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10584 else
10585 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10586 op0 = expand_normal (treeop0);
10587 op1 = expand_normal (treeop1);
10588 rtx dst = gen_reg_rtx (mode);
10589 emit_move_insn (dst, op0);
10590 store_bit_field (dst, bitsize, bitpos, 0, 0,
10591 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10592 return dst;
10595 default:
10596 gcc_unreachable ();
10599 /* Here to do an ordinary binary operator. */
10600 binop:
10601 expand_operands (treeop0, treeop1,
10602 subtarget, &op0, &op1, EXPAND_NORMAL);
10603 binop2:
10604 this_optab = optab_for_tree_code (code, type, optab_default);
10605 binop3:
10606 if (modifier == EXPAND_STACK_PARM)
10607 target = 0;
10608 temp = expand_binop (mode, this_optab, op0, op1, target,
10609 unsignedp, OPTAB_LIB_WIDEN);
10610 gcc_assert (temp);
10611 /* Bitwise operations do not need bitfield reduction as we expect their
10612 operands being properly truncated. */
10613 if (code == BIT_XOR_EXPR
10614 || code == BIT_AND_EXPR
10615 || code == BIT_IOR_EXPR)
10616 return temp;
10617 return REDUCE_BIT_FIELD (temp);
10619 #undef REDUCE_BIT_FIELD
10622 /* Return TRUE if expression STMT is suitable for replacement.
10623 Never consider memory loads as replaceable, because those don't ever lead
10624 into constant expressions. */
10626 static bool
10627 stmt_is_replaceable_p (gimple *stmt)
10629 if (ssa_is_replaceable_p (stmt))
10631 /* Don't move around loads. */
10632 if (!gimple_assign_single_p (stmt)
10633 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10634 return true;
10636 return false;
10640 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10641 enum expand_modifier modifier, rtx *alt_rtl,
10642 bool inner_reference_p)
10644 rtx op0, op1, temp, decl_rtl;
10645 tree type;
10646 int unsignedp;
10647 machine_mode mode, dmode;
10648 enum tree_code code = TREE_CODE (exp);
10649 rtx subtarget, original_target;
10650 int ignore;
10651 bool reduce_bit_field;
10652 location_t loc = EXPR_LOCATION (exp);
10653 struct separate_ops ops;
10654 tree treeop0, treeop1, treeop2;
10655 tree ssa_name = NULL_TREE;
10656 gimple *g;
10658 type = TREE_TYPE (exp);
10659 mode = TYPE_MODE (type);
10660 unsignedp = TYPE_UNSIGNED (type);
10662 treeop0 = treeop1 = treeop2 = NULL_TREE;
10663 if (!VL_EXP_CLASS_P (exp))
10664 switch (TREE_CODE_LENGTH (code))
10666 default:
10667 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10668 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10669 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10670 case 0: break;
10672 ops.code = code;
10673 ops.type = type;
10674 ops.op0 = treeop0;
10675 ops.op1 = treeop1;
10676 ops.op2 = treeop2;
10677 ops.location = loc;
10679 ignore = (target == const0_rtx
10680 || ((CONVERT_EXPR_CODE_P (code)
10681 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10682 && TREE_CODE (type) == VOID_TYPE));
10684 /* An operation in what may be a bit-field type needs the
10685 result to be reduced to the precision of the bit-field type,
10686 which is narrower than that of the type's mode. */
10687 reduce_bit_field = (!ignore
10688 && INTEGRAL_TYPE_P (type)
10689 && !type_has_mode_precision_p (type));
10691 /* If we are going to ignore this result, we need only do something
10692 if there is a side-effect somewhere in the expression. If there
10693 is, short-circuit the most common cases here. Note that we must
10694 not call expand_expr with anything but const0_rtx in case this
10695 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10697 if (ignore)
10699 if (! TREE_SIDE_EFFECTS (exp))
10700 return const0_rtx;
10702 /* Ensure we reference a volatile object even if value is ignored, but
10703 don't do this if all we are doing is taking its address. */
10704 if (TREE_THIS_VOLATILE (exp)
10705 && TREE_CODE (exp) != FUNCTION_DECL
10706 && mode != VOIDmode && mode != BLKmode
10707 && modifier != EXPAND_CONST_ADDRESS)
10709 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10710 if (MEM_P (temp))
10711 copy_to_reg (temp);
10712 return const0_rtx;
10715 if (TREE_CODE_CLASS (code) == tcc_unary
10716 || code == BIT_FIELD_REF
10717 || code == COMPONENT_REF
10718 || code == INDIRECT_REF)
10719 return expand_expr (treeop0, const0_rtx, VOIDmode,
10720 modifier);
10722 else if (TREE_CODE_CLASS (code) == tcc_binary
10723 || TREE_CODE_CLASS (code) == tcc_comparison
10724 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10726 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10727 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10728 return const0_rtx;
10731 target = 0;
10734 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10735 target = 0;
10737 /* Use subtarget as the target for operand 0 of a binary operation. */
10738 subtarget = get_subtarget (target);
10739 original_target = target;
10741 switch (code)
10743 case LABEL_DECL:
10745 tree function = decl_function_context (exp);
10747 temp = label_rtx (exp);
10748 temp = gen_rtx_LABEL_REF (Pmode, temp);
10750 if (function != current_function_decl
10751 && function != 0)
10752 LABEL_REF_NONLOCAL_P (temp) = 1;
10754 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10755 return temp;
10758 case SSA_NAME:
10759 /* ??? ivopts calls expander, without any preparation from
10760 out-of-ssa. So fake instructions as if this was an access to the
10761 base variable. This unnecessarily allocates a pseudo, see how we can
10762 reuse it, if partition base vars have it set already. */
10763 if (!currently_expanding_to_rtl)
10765 tree var = SSA_NAME_VAR (exp);
10766 if (var && DECL_RTL_SET_P (var))
10767 return DECL_RTL (var);
10768 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10769 LAST_VIRTUAL_REGISTER + 1);
10772 g = get_gimple_for_ssa_name (exp);
10773 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10774 if (g == NULL
10775 && modifier == EXPAND_INITIALIZER
10776 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10777 && (optimize || !SSA_NAME_VAR (exp)
10778 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10779 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10780 g = SSA_NAME_DEF_STMT (exp);
10781 if (g)
10783 rtx r;
10784 location_t saved_loc = curr_insn_location ();
10785 loc = gimple_location (g);
10786 if (loc != UNKNOWN_LOCATION)
10787 set_curr_insn_location (loc);
10788 ops.code = gimple_assign_rhs_code (g);
10789 switch (get_gimple_rhs_class (ops.code))
10791 case GIMPLE_TERNARY_RHS:
10792 ops.op2 = gimple_assign_rhs3 (g);
10793 /* Fallthru */
10794 case GIMPLE_BINARY_RHS:
10795 ops.op1 = gimple_assign_rhs2 (g);
10797 /* Try to expand conditonal compare. */
10798 if (targetm.gen_ccmp_first)
10800 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10801 r = expand_ccmp_expr (g, mode);
10802 if (r)
10803 break;
10805 /* Fallthru */
10806 case GIMPLE_UNARY_RHS:
10807 ops.op0 = gimple_assign_rhs1 (g);
10808 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10809 ops.location = loc;
10810 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10811 break;
10812 case GIMPLE_SINGLE_RHS:
10814 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10815 tmode, modifier, alt_rtl,
10816 inner_reference_p);
10817 break;
10819 default:
10820 gcc_unreachable ();
10822 set_curr_insn_location (saved_loc);
10823 if (REG_P (r) && !REG_EXPR (r))
10824 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10825 return r;
10828 ssa_name = exp;
10829 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10830 exp = SSA_NAME_VAR (ssa_name);
10831 goto expand_decl_rtl;
10833 case VAR_DECL:
10834 /* Allow accel compiler to handle variables that require special
10835 treatment, e.g. if they have been modified in some way earlier in
10836 compilation by the adjust_private_decl OpenACC hook. */
10837 if (flag_openacc && targetm.goacc.expand_var_decl)
10839 temp = targetm.goacc.expand_var_decl (exp);
10840 if (temp)
10841 return temp;
10843 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10844 have scalar integer modes to a reg via store_constructor. */
10845 if (TREE_READONLY (exp)
10846 && !TREE_SIDE_EFFECTS (exp)
10847 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10848 && immediate_const_ctor_p (DECL_INITIAL (exp))
10849 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10850 && crtl->emit.regno_pointer_align_length
10851 && !target)
10853 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10854 store_constructor (DECL_INITIAL (exp), target, 0,
10855 int_expr_size (DECL_INITIAL (exp)), false);
10856 return target;
10858 /* ... fall through ... */
10860 case PARM_DECL:
10861 /* If a static var's type was incomplete when the decl was written,
10862 but the type is complete now, lay out the decl now. */
10863 if (DECL_SIZE (exp) == 0
10864 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10865 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10866 layout_decl (exp, 0);
10868 /* fall through */
10870 case FUNCTION_DECL:
10871 case RESULT_DECL:
10872 decl_rtl = DECL_RTL (exp);
10873 expand_decl_rtl:
10874 gcc_assert (decl_rtl);
10876 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10877 settings for VECTOR_TYPE_P that might switch for the function. */
10878 if (currently_expanding_to_rtl
10879 && code == VAR_DECL && MEM_P (decl_rtl)
10880 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10881 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10882 else
10883 decl_rtl = copy_rtx (decl_rtl);
10885 /* Record writes to register variables. */
10886 if (modifier == EXPAND_WRITE
10887 && REG_P (decl_rtl)
10888 && HARD_REGISTER_P (decl_rtl))
10889 add_to_hard_reg_set (&crtl->asm_clobbers,
10890 GET_MODE (decl_rtl), REGNO (decl_rtl));
10892 /* Ensure variable marked as used even if it doesn't go through
10893 a parser. If it hasn't be used yet, write out an external
10894 definition. */
10895 if (exp)
10896 TREE_USED (exp) = 1;
10898 /* Show we haven't gotten RTL for this yet. */
10899 temp = 0;
10901 /* Variables inherited from containing functions should have
10902 been lowered by this point. */
10903 if (exp)
10905 tree context = decl_function_context (exp);
10906 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10907 || context == current_function_decl
10908 || TREE_STATIC (exp)
10909 || DECL_EXTERNAL (exp)
10910 /* ??? C++ creates functions that are not
10911 TREE_STATIC. */
10912 || TREE_CODE (exp) == FUNCTION_DECL);
10915 /* This is the case of an array whose size is to be determined
10916 from its initializer, while the initializer is still being parsed.
10917 ??? We aren't parsing while expanding anymore. */
10919 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10920 temp = validize_mem (decl_rtl);
10922 /* If DECL_RTL is memory, we are in the normal case and the
10923 address is not valid, get the address into a register. */
10925 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10927 if (alt_rtl)
10928 *alt_rtl = decl_rtl;
10929 decl_rtl = use_anchored_address (decl_rtl);
10930 if (modifier != EXPAND_CONST_ADDRESS
10931 && modifier != EXPAND_SUM
10932 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10933 : GET_MODE (decl_rtl),
10934 XEXP (decl_rtl, 0),
10935 MEM_ADDR_SPACE (decl_rtl)))
10936 temp = replace_equiv_address (decl_rtl,
10937 copy_rtx (XEXP (decl_rtl, 0)));
10940 /* If we got something, return it. But first, set the alignment
10941 if the address is a register. */
10942 if (temp != 0)
10944 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10945 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10947 else if (MEM_P (decl_rtl))
10948 temp = decl_rtl;
10950 if (temp != 0)
10952 if (MEM_P (temp)
10953 && modifier != EXPAND_WRITE
10954 && modifier != EXPAND_MEMORY
10955 && modifier != EXPAND_INITIALIZER
10956 && modifier != EXPAND_CONST_ADDRESS
10957 && modifier != EXPAND_SUM
10958 && !inner_reference_p
10959 && mode != BLKmode
10960 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10961 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10962 MEM_ALIGN (temp), NULL_RTX, NULL);
10964 return temp;
10967 if (exp)
10968 dmode = DECL_MODE (exp);
10969 else
10970 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10972 /* If the mode of DECL_RTL does not match that of the decl,
10973 there are two cases: we are dealing with a BLKmode value
10974 that is returned in a register, or we are dealing with
10975 a promoted value. In the latter case, return a SUBREG
10976 of the wanted mode, but mark it so that we know that it
10977 was already extended. */
10978 if (REG_P (decl_rtl)
10979 && dmode != BLKmode
10980 && GET_MODE (decl_rtl) != dmode)
10982 machine_mode pmode;
10984 /* Get the signedness to be used for this variable. Ensure we get
10985 the same mode we got when the variable was declared. */
10986 if (code != SSA_NAME)
10987 pmode = promote_decl_mode (exp, &unsignedp);
10988 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10989 && gimple_code (g) == GIMPLE_CALL
10990 && !gimple_call_internal_p (g))
10991 pmode = promote_function_mode (type, mode, &unsignedp,
10992 gimple_call_fntype (g),
10994 else
10995 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10996 gcc_assert (GET_MODE (decl_rtl) == pmode);
10998 /* Some ABIs require scalar floating point modes to be passed
10999 in a wider scalar integer mode. We need to explicitly
11000 truncate to an integer mode of the correct precision before
11001 using a SUBREG to reinterpret as a floating point value. */
11002 if (SCALAR_FLOAT_MODE_P (mode)
11003 && SCALAR_INT_MODE_P (pmode)
11004 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11005 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11007 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11008 SUBREG_PROMOTED_VAR_P (temp) = 1;
11009 SUBREG_PROMOTED_SET (temp, unsignedp);
11010 return temp;
11013 return decl_rtl;
11015 case INTEGER_CST:
11017 /* Given that TYPE_PRECISION (type) is not always equal to
11018 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11019 the former to the latter according to the signedness of the
11020 type. */
11021 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11022 temp = immed_wide_int_const
11023 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11024 return temp;
11027 case VECTOR_CST:
11029 tree tmp = NULL_TREE;
11030 if (VECTOR_MODE_P (mode))
11031 return const_vector_from_tree (exp);
11032 scalar_int_mode int_mode;
11033 if (is_int_mode (mode, &int_mode))
11035 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11036 if (type_for_mode)
11037 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11038 type_for_mode, exp);
11040 if (!tmp)
11042 vec<constructor_elt, va_gc> *v;
11043 /* Constructors need to be fixed-length. FIXME. */
11044 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11045 vec_alloc (v, nunits);
11046 for (unsigned int i = 0; i < nunits; ++i)
11047 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11048 tmp = build_constructor (type, v);
11050 return expand_expr (tmp, ignore ? const0_rtx : target,
11051 tmode, modifier);
11054 case CONST_DECL:
11055 if (modifier == EXPAND_WRITE)
11057 /* Writing into CONST_DECL is always invalid, but handle it
11058 gracefully. */
11059 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11060 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11061 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11062 EXPAND_NORMAL, as);
11063 op0 = memory_address_addr_space (mode, op0, as);
11064 temp = gen_rtx_MEM (mode, op0);
11065 set_mem_addr_space (temp, as);
11066 return temp;
11068 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11070 case REAL_CST:
11071 /* If optimized, generate immediate CONST_DOUBLE
11072 which will be turned into memory by reload if necessary.
11074 We used to force a register so that loop.c could see it. But
11075 this does not allow gen_* patterns to perform optimizations with
11076 the constants. It also produces two insns in cases like "x = 1.0;".
11077 On most machines, floating-point constants are not permitted in
11078 many insns, so we'd end up copying it to a register in any case.
11080 Now, we do the copying in expand_binop, if appropriate. */
11081 return const_double_from_real_value (TREE_REAL_CST (exp),
11082 TYPE_MODE (TREE_TYPE (exp)));
11084 case FIXED_CST:
11085 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11086 TYPE_MODE (TREE_TYPE (exp)));
11088 case COMPLEX_CST:
11089 /* Handle evaluating a complex constant in a CONCAT target. */
11090 if (original_target && GET_CODE (original_target) == CONCAT)
11092 rtx rtarg, itarg;
11094 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11095 rtarg = XEXP (original_target, 0);
11096 itarg = XEXP (original_target, 1);
11098 /* Move the real and imaginary parts separately. */
11099 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11100 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11102 if (op0 != rtarg)
11103 emit_move_insn (rtarg, op0);
11104 if (op1 != itarg)
11105 emit_move_insn (itarg, op1);
11107 return original_target;
11110 /* fall through */
11112 case STRING_CST:
11113 temp = expand_expr_constant (exp, 1, modifier);
11115 /* temp contains a constant address.
11116 On RISC machines where a constant address isn't valid,
11117 make some insns to get that address into a register. */
11118 if (modifier != EXPAND_CONST_ADDRESS
11119 && modifier != EXPAND_INITIALIZER
11120 && modifier != EXPAND_SUM
11121 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11122 MEM_ADDR_SPACE (temp)))
11123 return replace_equiv_address (temp,
11124 copy_rtx (XEXP (temp, 0)));
11125 return temp;
11127 case POLY_INT_CST:
11128 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11130 case SAVE_EXPR:
11132 tree val = treeop0;
11133 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11134 inner_reference_p);
11136 if (!SAVE_EXPR_RESOLVED_P (exp))
11138 /* We can indeed still hit this case, typically via builtin
11139 expanders calling save_expr immediately before expanding
11140 something. Assume this means that we only have to deal
11141 with non-BLKmode values. */
11142 gcc_assert (GET_MODE (ret) != BLKmode);
11144 val = build_decl (curr_insn_location (),
11145 VAR_DECL, NULL, TREE_TYPE (exp));
11146 DECL_ARTIFICIAL (val) = 1;
11147 DECL_IGNORED_P (val) = 1;
11148 treeop0 = val;
11149 TREE_OPERAND (exp, 0) = treeop0;
11150 SAVE_EXPR_RESOLVED_P (exp) = 1;
11152 if (!CONSTANT_P (ret))
11153 ret = copy_to_reg (ret);
11154 SET_DECL_RTL (val, ret);
11157 return ret;
11161 case CONSTRUCTOR:
11162 /* If we don't need the result, just ensure we evaluate any
11163 subexpressions. */
11164 if (ignore)
11166 unsigned HOST_WIDE_INT idx;
11167 tree value;
11169 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11170 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11172 return const0_rtx;
11175 return expand_constructor (exp, target, modifier, false);
11177 case TARGET_MEM_REF:
11179 addr_space_t as
11180 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11181 unsigned int align;
11183 op0 = addr_for_mem_ref (exp, as, true);
11184 op0 = memory_address_addr_space (mode, op0, as);
11185 temp = gen_rtx_MEM (mode, op0);
11186 set_mem_attributes (temp, exp, 0);
11187 set_mem_addr_space (temp, as);
11188 align = get_object_alignment (exp);
11189 if (modifier != EXPAND_WRITE
11190 && modifier != EXPAND_MEMORY
11191 && mode != BLKmode
11192 && align < GET_MODE_ALIGNMENT (mode))
11193 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11194 align, NULL_RTX, NULL);
11195 return temp;
11198 case MEM_REF:
11200 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11201 addr_space_t as
11202 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11203 machine_mode address_mode;
11204 tree base = TREE_OPERAND (exp, 0);
11205 gimple *def_stmt;
11206 unsigned align;
11207 /* Handle expansion of non-aliased memory with non-BLKmode. That
11208 might end up in a register. */
11209 if (mem_ref_refers_to_non_mem_p (exp))
11211 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11212 base = TREE_OPERAND (base, 0);
11213 poly_uint64 type_size;
11214 if (known_eq (offset, 0)
11215 && !reverse
11216 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11217 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11218 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11219 target, tmode, modifier);
11220 if (TYPE_MODE (type) == BLKmode)
11222 temp = assign_stack_temp (DECL_MODE (base),
11223 GET_MODE_SIZE (DECL_MODE (base)));
11224 store_expr (base, temp, 0, false, false);
11225 temp = adjust_address (temp, BLKmode, offset);
11226 set_mem_size (temp, int_size_in_bytes (type));
11227 return temp;
11229 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11230 bitsize_int (offset * BITS_PER_UNIT));
11231 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11232 return expand_expr (exp, target, tmode, modifier);
11234 address_mode = targetm.addr_space.address_mode (as);
11235 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11237 tree mask = gimple_assign_rhs2 (def_stmt);
11238 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11239 gimple_assign_rhs1 (def_stmt), mask);
11240 TREE_OPERAND (exp, 0) = base;
11242 align = get_object_alignment (exp);
11243 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11244 op0 = memory_address_addr_space (mode, op0, as);
11245 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11247 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11248 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11249 op0 = memory_address_addr_space (mode, op0, as);
11251 temp = gen_rtx_MEM (mode, op0);
11252 set_mem_attributes (temp, exp, 0);
11253 set_mem_addr_space (temp, as);
11254 if (TREE_THIS_VOLATILE (exp))
11255 MEM_VOLATILE_P (temp) = 1;
11256 if (modifier != EXPAND_WRITE
11257 && modifier != EXPAND_MEMORY
11258 && !inner_reference_p
11259 && mode != BLKmode
11260 && align < GET_MODE_ALIGNMENT (mode))
11261 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11262 modifier == EXPAND_STACK_PARM
11263 ? NULL_RTX : target, alt_rtl);
11264 if (reverse
11265 && modifier != EXPAND_MEMORY
11266 && modifier != EXPAND_WRITE)
11267 temp = flip_storage_order (mode, temp);
11268 return temp;
11271 case ARRAY_REF:
11274 tree array = treeop0;
11275 tree index = treeop1;
11276 tree init;
11278 /* Fold an expression like: "foo"[2].
11279 This is not done in fold so it won't happen inside &.
11280 Don't fold if this is for wide characters since it's too
11281 difficult to do correctly and this is a very rare case. */
11283 if (modifier != EXPAND_CONST_ADDRESS
11284 && modifier != EXPAND_INITIALIZER
11285 && modifier != EXPAND_MEMORY)
11287 tree t = fold_read_from_constant_string (exp);
11289 if (t)
11290 return expand_expr (t, target, tmode, modifier);
11293 /* If this is a constant index into a constant array,
11294 just get the value from the array. Handle both the cases when
11295 we have an explicit constructor and when our operand is a variable
11296 that was declared const. */
11298 if (modifier != EXPAND_CONST_ADDRESS
11299 && modifier != EXPAND_INITIALIZER
11300 && modifier != EXPAND_MEMORY
11301 && TREE_CODE (array) == CONSTRUCTOR
11302 && ! TREE_SIDE_EFFECTS (array)
11303 && TREE_CODE (index) == INTEGER_CST)
11305 unsigned HOST_WIDE_INT ix;
11306 tree field, value;
11308 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11309 field, value)
11310 if (tree_int_cst_equal (field, index))
11312 if (!TREE_SIDE_EFFECTS (value))
11313 return expand_expr (fold (value), target, tmode, modifier);
11314 break;
11318 else if (optimize >= 1
11319 && modifier != EXPAND_CONST_ADDRESS
11320 && modifier != EXPAND_INITIALIZER
11321 && modifier != EXPAND_MEMORY
11322 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11323 && TREE_CODE (index) == INTEGER_CST
11324 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11325 && (init = ctor_for_folding (array)) != error_mark_node)
11327 if (init == NULL_TREE)
11329 tree value = build_zero_cst (type);
11330 if (TREE_CODE (value) == CONSTRUCTOR)
11332 /* If VALUE is a CONSTRUCTOR, this optimization is only
11333 useful if this doesn't store the CONSTRUCTOR into
11334 memory. If it does, it is more efficient to just
11335 load the data from the array directly. */
11336 rtx ret = expand_constructor (value, target,
11337 modifier, true);
11338 if (ret == NULL_RTX)
11339 value = NULL_TREE;
11342 if (value)
11343 return expand_expr (value, target, tmode, modifier);
11345 else if (TREE_CODE (init) == CONSTRUCTOR)
11347 unsigned HOST_WIDE_INT ix;
11348 tree field, value;
11350 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11351 field, value)
11352 if (tree_int_cst_equal (field, index))
11354 if (TREE_SIDE_EFFECTS (value))
11355 break;
11357 if (TREE_CODE (value) == CONSTRUCTOR)
11359 /* If VALUE is a CONSTRUCTOR, this
11360 optimization is only useful if
11361 this doesn't store the CONSTRUCTOR
11362 into memory. If it does, it is more
11363 efficient to just load the data from
11364 the array directly. */
11365 rtx ret = expand_constructor (value, target,
11366 modifier, true);
11367 if (ret == NULL_RTX)
11368 break;
11371 return
11372 expand_expr (fold (value), target, tmode, modifier);
11375 else if (TREE_CODE (init) == STRING_CST)
11377 tree low_bound = array_ref_low_bound (exp);
11378 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11380 /* Optimize the special case of a zero lower bound.
11382 We convert the lower bound to sizetype to avoid problems
11383 with constant folding. E.g. suppose the lower bound is
11384 1 and its mode is QI. Without the conversion
11385 (ARRAY + (INDEX - (unsigned char)1))
11386 becomes
11387 (ARRAY + (-(unsigned char)1) + INDEX)
11388 which becomes
11389 (ARRAY + 255 + INDEX). Oops! */
11390 if (!integer_zerop (low_bound))
11391 index1 = size_diffop_loc (loc, index1,
11392 fold_convert_loc (loc, sizetype,
11393 low_bound));
11395 if (tree_fits_uhwi_p (index1)
11396 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11398 tree char_type = TREE_TYPE (TREE_TYPE (init));
11399 scalar_int_mode char_mode;
11401 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11402 && GET_MODE_SIZE (char_mode) == 1)
11403 return gen_int_mode (TREE_STRING_POINTER (init)
11404 [TREE_INT_CST_LOW (index1)],
11405 char_mode);
11410 goto normal_inner_ref;
11412 case COMPONENT_REF:
11413 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11414 /* Fall through. */
11415 case BIT_FIELD_REF:
11416 case ARRAY_RANGE_REF:
11417 normal_inner_ref:
11419 machine_mode mode1, mode2;
11420 poly_int64 bitsize, bitpos, bytepos;
11421 tree offset;
11422 int reversep, volatilep = 0;
11423 tree tem
11424 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11425 &unsignedp, &reversep, &volatilep);
11426 rtx orig_op0, memloc;
11427 bool clear_mem_expr = false;
11428 bool must_force_mem;
11430 /* If we got back the original object, something is wrong. Perhaps
11431 we are evaluating an expression too early. In any event, don't
11432 infinitely recurse. */
11433 gcc_assert (tem != exp);
11435 /* Make sure bitpos is not negative, this can wreak havoc later. */
11436 if (maybe_lt (bitpos, 0))
11438 gcc_checking_assert (offset == NULL_TREE);
11439 offset = size_int (bits_to_bytes_round_down (bitpos));
11440 bitpos = num_trailing_bits (bitpos);
11443 /* If we have either an offset, a BLKmode result, or a reference
11444 outside the underlying object, we must force it to memory.
11445 Such a case can occur in Ada if we have unchecked conversion
11446 of an expression from a scalar type to an aggregate type or
11447 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11448 passed a partially uninitialized object or a view-conversion
11449 to a larger size. */
11450 must_force_mem = offset != NULL_TREE
11451 || mode1 == BLKmode
11452 || (mode == BLKmode
11453 && !int_mode_for_size (bitsize, 1).exists ());
11455 const enum expand_modifier tem_modifier
11456 = must_force_mem
11457 ? EXPAND_MEMORY
11458 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11460 /* If TEM's type is a union of variable size, pass TARGET to the inner
11461 computation, since it will need a temporary and TARGET is known
11462 to have to do. This occurs in unchecked conversion in Ada. */
11463 const rtx tem_target
11464 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11465 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11466 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11467 && modifier != EXPAND_STACK_PARM
11468 ? target
11469 : NULL_RTX;
11471 orig_op0 = op0
11472 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11473 true);
11475 /* If the field has a mode, we want to access it in the
11476 field's mode, not the computed mode.
11477 If a MEM has VOIDmode (external with incomplete type),
11478 use BLKmode for it instead. */
11479 if (MEM_P (op0))
11481 if (mode1 != VOIDmode)
11482 op0 = adjust_address (op0, mode1, 0);
11483 else if (GET_MODE (op0) == VOIDmode)
11484 op0 = adjust_address (op0, BLKmode, 0);
11487 mode2
11488 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11490 /* See above for the rationale. */
11491 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11492 must_force_mem = true;
11494 /* Handle CONCAT first. */
11495 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11497 if (known_eq (bitpos, 0)
11498 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11499 && COMPLEX_MODE_P (mode1)
11500 && COMPLEX_MODE_P (GET_MODE (op0))
11501 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11502 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11504 if (reversep)
11505 op0 = flip_storage_order (GET_MODE (op0), op0);
11506 if (mode1 != GET_MODE (op0))
11508 rtx parts[2];
11509 for (int i = 0; i < 2; i++)
11511 rtx op = read_complex_part (op0, i != 0);
11512 if (GET_CODE (op) == SUBREG)
11513 op = force_reg (GET_MODE (op), op);
11514 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11515 if (temp)
11516 op = temp;
11517 else
11519 if (!REG_P (op) && !MEM_P (op))
11520 op = force_reg (GET_MODE (op), op);
11521 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11523 parts[i] = op;
11525 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11527 return op0;
11529 if (known_eq (bitpos, 0)
11530 && known_eq (bitsize,
11531 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11532 && maybe_ne (bitsize, 0))
11534 op0 = XEXP (op0, 0);
11535 mode2 = GET_MODE (op0);
11537 else if (known_eq (bitpos,
11538 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11539 && known_eq (bitsize,
11540 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11541 && maybe_ne (bitpos, 0)
11542 && maybe_ne (bitsize, 0))
11544 op0 = XEXP (op0, 1);
11545 bitpos = 0;
11546 mode2 = GET_MODE (op0);
11548 else
11549 /* Otherwise force into memory. */
11550 must_force_mem = true;
11553 /* If this is a constant, put it in a register if it is a legitimate
11554 constant and we don't need a memory reference. */
11555 if (CONSTANT_P (op0)
11556 && mode2 != BLKmode
11557 && targetm.legitimate_constant_p (mode2, op0)
11558 && !must_force_mem)
11559 op0 = force_reg (mode2, op0);
11561 /* Otherwise, if this is a constant, try to force it to the constant
11562 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11563 is a legitimate constant. */
11564 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11565 op0 = validize_mem (memloc);
11567 /* Otherwise, if this is a constant or the object is not in memory
11568 and need be, put it there. */
11569 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11571 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11572 emit_move_insn (memloc, op0);
11573 op0 = memloc;
11574 clear_mem_expr = true;
11577 if (offset)
11579 machine_mode address_mode;
11580 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11581 EXPAND_SUM);
11583 gcc_assert (MEM_P (op0));
11585 address_mode = get_address_mode (op0);
11586 if (GET_MODE (offset_rtx) != address_mode)
11588 /* We cannot be sure that the RTL in offset_rtx is valid outside
11589 of a memory address context, so force it into a register
11590 before attempting to convert it to the desired mode. */
11591 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11592 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11595 /* See the comment in expand_assignment for the rationale. */
11596 if (mode1 != VOIDmode
11597 && maybe_ne (bitpos, 0)
11598 && maybe_gt (bitsize, 0)
11599 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11600 && multiple_p (bitpos, bitsize)
11601 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11602 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11604 op0 = adjust_address (op0, mode1, bytepos);
11605 bitpos = 0;
11608 op0 = offset_address (op0, offset_rtx,
11609 highest_pow2_factor (offset));
11612 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11613 record its alignment as BIGGEST_ALIGNMENT. */
11614 if (MEM_P (op0)
11615 && known_eq (bitpos, 0)
11616 && offset != 0
11617 && is_aligning_offset (offset, tem))
11618 set_mem_align (op0, BIGGEST_ALIGNMENT);
11620 /* Don't forget about volatility even if this is a bitfield. */
11621 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11623 if (op0 == orig_op0)
11624 op0 = copy_rtx (op0);
11626 MEM_VOLATILE_P (op0) = 1;
11629 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11631 if (op0 == orig_op0)
11632 op0 = copy_rtx (op0);
11634 set_mem_align (op0, BITS_PER_UNIT);
11637 /* In cases where an aligned union has an unaligned object
11638 as a field, we might be extracting a BLKmode value from
11639 an integer-mode (e.g., SImode) object. Handle this case
11640 by doing the extract into an object as wide as the field
11641 (which we know to be the width of a basic mode), then
11642 storing into memory, and changing the mode to BLKmode. */
11643 if (mode1 == VOIDmode
11644 || REG_P (op0) || GET_CODE (op0) == SUBREG
11645 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11646 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11647 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11648 && modifier != EXPAND_CONST_ADDRESS
11649 && modifier != EXPAND_INITIALIZER
11650 && modifier != EXPAND_MEMORY)
11651 /* If the bitfield is volatile and the bitsize
11652 is narrower than the access size of the bitfield,
11653 we need to extract bitfields from the access. */
11654 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11655 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11656 && mode1 != BLKmode
11657 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11658 /* If the field isn't aligned enough to fetch as a memref,
11659 fetch it as a bit field. */
11660 || (mode1 != BLKmode
11661 && (((MEM_P (op0)
11662 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11663 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11664 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11665 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11666 && modifier != EXPAND_MEMORY
11667 && ((modifier == EXPAND_CONST_ADDRESS
11668 || modifier == EXPAND_INITIALIZER)
11669 ? STRICT_ALIGNMENT
11670 : targetm.slow_unaligned_access (mode1,
11671 MEM_ALIGN (op0))))
11672 || !multiple_p (bitpos, BITS_PER_UNIT)))
11673 /* If the type and the field are a constant size and the
11674 size of the type isn't the same size as the bitfield,
11675 we must use bitfield operations. */
11676 || (known_size_p (bitsize)
11677 && TYPE_SIZE (TREE_TYPE (exp))
11678 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11679 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11680 bitsize)))
11682 machine_mode ext_mode = mode;
11684 if (ext_mode == BLKmode
11685 && ! (target != 0 && MEM_P (op0)
11686 && MEM_P (target)
11687 && multiple_p (bitpos, BITS_PER_UNIT)))
11688 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11690 if (ext_mode == BLKmode)
11692 if (target == 0)
11693 target = assign_temp (type, 1, 1);
11695 /* ??? Unlike the similar test a few lines below, this one is
11696 very likely obsolete. */
11697 if (known_eq (bitsize, 0))
11698 return target;
11700 /* In this case, BITPOS must start at a byte boundary and
11701 TARGET, if specified, must be a MEM. */
11702 gcc_assert (MEM_P (op0)
11703 && (!target || MEM_P (target)));
11705 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11706 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11707 emit_block_move (target,
11708 adjust_address (op0, VOIDmode, bytepos),
11709 gen_int_mode (bytesize, Pmode),
11710 (modifier == EXPAND_STACK_PARM
11711 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11713 return target;
11716 /* If we have nothing to extract, the result will be 0 for targets
11717 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11718 return 0 for the sake of consistency, as reading a zero-sized
11719 bitfield is valid in Ada and the value is fully specified. */
11720 if (known_eq (bitsize, 0))
11721 return const0_rtx;
11723 op0 = validize_mem (op0);
11725 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11726 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11728 /* If the result has aggregate type and the extraction is done in
11729 an integral mode, then the field may be not aligned on a byte
11730 boundary; in this case, if it has reverse storage order, it
11731 needs to be extracted as a scalar field with reverse storage
11732 order and put back into memory order afterwards. */
11733 if (AGGREGATE_TYPE_P (type)
11734 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11735 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11737 gcc_checking_assert (known_ge (bitpos, 0));
11738 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11739 (modifier == EXPAND_STACK_PARM
11740 ? NULL_RTX : target),
11741 ext_mode, ext_mode, reversep, alt_rtl);
11743 /* If the result has aggregate type and the mode of OP0 is an
11744 integral mode then, if BITSIZE is narrower than this mode
11745 and this is for big-endian data, we must put the field
11746 into the high-order bits. And we must also put it back
11747 into memory order if it has been previously reversed. */
11748 scalar_int_mode op0_mode;
11749 if (AGGREGATE_TYPE_P (type)
11750 && is_int_mode (GET_MODE (op0), &op0_mode))
11752 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11754 gcc_checking_assert (known_le (bitsize, size));
11755 if (maybe_lt (bitsize, size)
11756 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11757 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11758 size - bitsize, op0, 1);
11760 if (reversep)
11761 op0 = flip_storage_order (op0_mode, op0);
11764 /* If the result type is BLKmode, store the data into a temporary
11765 of the appropriate type, but with the mode corresponding to the
11766 mode for the data we have (op0's mode). */
11767 if (mode == BLKmode)
11769 rtx new_rtx
11770 = assign_stack_temp_for_type (ext_mode,
11771 GET_MODE_BITSIZE (ext_mode),
11772 type);
11773 emit_move_insn (new_rtx, op0);
11774 op0 = copy_rtx (new_rtx);
11775 PUT_MODE (op0, BLKmode);
11778 return op0;
11781 /* If the result is BLKmode, use that to access the object
11782 now as well. */
11783 if (mode == BLKmode)
11784 mode1 = BLKmode;
11786 /* Get a reference to just this component. */
11787 bytepos = bits_to_bytes_round_down (bitpos);
11788 if (modifier == EXPAND_CONST_ADDRESS
11789 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11790 op0 = adjust_address_nv (op0, mode1, bytepos);
11791 else
11792 op0 = adjust_address (op0, mode1, bytepos);
11794 if (op0 == orig_op0)
11795 op0 = copy_rtx (op0);
11797 /* Don't set memory attributes if the base expression is
11798 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11799 we should just honor its original memory attributes. */
11800 if (!(TREE_CODE (tem) == SSA_NAME
11801 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11802 set_mem_attributes (op0, exp, 0);
11804 if (REG_P (XEXP (op0, 0)))
11805 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11807 /* If op0 is a temporary because the original expressions was forced
11808 to memory, clear MEM_EXPR so that the original expression cannot
11809 be marked as addressable through MEM_EXPR of the temporary. */
11810 if (clear_mem_expr)
11811 set_mem_expr (op0, NULL_TREE);
11813 MEM_VOLATILE_P (op0) |= volatilep;
11815 if (reversep
11816 && modifier != EXPAND_MEMORY
11817 && modifier != EXPAND_WRITE)
11818 op0 = flip_storage_order (mode1, op0);
11820 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11821 || modifier == EXPAND_CONST_ADDRESS
11822 || modifier == EXPAND_INITIALIZER)
11823 return op0;
11825 if (target == 0)
11826 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11828 convert_move (target, op0, unsignedp);
11829 return target;
11832 case OBJ_TYPE_REF:
11833 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11835 case CALL_EXPR:
11836 /* All valid uses of __builtin_va_arg_pack () are removed during
11837 inlining. */
11838 if (CALL_EXPR_VA_ARG_PACK (exp))
11839 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11841 tree fndecl = get_callee_fndecl (exp), attr;
11843 if (fndecl
11844 /* Don't diagnose the error attribute in thunks, those are
11845 artificially created. */
11846 && !CALL_FROM_THUNK_P (exp)
11847 && (attr = lookup_attribute ("error",
11848 DECL_ATTRIBUTES (fndecl))) != NULL)
11850 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11851 error ("call to %qs declared with attribute error: %s",
11852 identifier_to_locale (ident),
11853 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11855 if (fndecl
11856 /* Don't diagnose the warning attribute in thunks, those are
11857 artificially created. */
11858 && !CALL_FROM_THUNK_P (exp)
11859 && (attr = lookup_attribute ("warning",
11860 DECL_ATTRIBUTES (fndecl))) != NULL)
11862 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11863 warning_at (EXPR_LOCATION (exp),
11864 OPT_Wattribute_warning,
11865 "call to %qs declared with attribute warning: %s",
11866 identifier_to_locale (ident),
11867 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11870 /* Check for a built-in function. */
11871 if (fndecl && fndecl_built_in_p (fndecl))
11873 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11874 return expand_builtin (exp, target, subtarget, tmode, ignore);
11877 return expand_call (exp, target, ignore);
11879 case VIEW_CONVERT_EXPR:
11880 op0 = NULL_RTX;
11882 /* If we are converting to BLKmode, try to avoid an intermediate
11883 temporary by fetching an inner memory reference. */
11884 if (mode == BLKmode
11885 && poly_int_tree_p (TYPE_SIZE (type))
11886 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11887 && handled_component_p (treeop0))
11889 machine_mode mode1;
11890 poly_int64 bitsize, bitpos, bytepos;
11891 tree offset;
11892 int reversep, volatilep = 0;
11893 tree tem
11894 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11895 &unsignedp, &reversep, &volatilep);
11897 /* ??? We should work harder and deal with non-zero offsets. */
11898 if (!offset
11899 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11900 && !reversep
11901 && known_size_p (bitsize)
11902 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11904 /* See the normal_inner_ref case for the rationale. */
11905 rtx orig_op0
11906 = expand_expr_real (tem,
11907 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11908 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11909 != INTEGER_CST)
11910 && modifier != EXPAND_STACK_PARM
11911 ? target : NULL_RTX),
11912 VOIDmode,
11913 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11914 NULL, true);
11916 if (MEM_P (orig_op0))
11918 op0 = orig_op0;
11920 /* Get a reference to just this component. */
11921 if (modifier == EXPAND_CONST_ADDRESS
11922 || modifier == EXPAND_SUM
11923 || modifier == EXPAND_INITIALIZER)
11924 op0 = adjust_address_nv (op0, mode, bytepos);
11925 else
11926 op0 = adjust_address (op0, mode, bytepos);
11928 if (op0 == orig_op0)
11929 op0 = copy_rtx (op0);
11931 set_mem_attributes (op0, treeop0, 0);
11932 if (REG_P (XEXP (op0, 0)))
11933 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11935 MEM_VOLATILE_P (op0) |= volatilep;
11940 if (!op0)
11941 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11942 NULL, inner_reference_p);
11944 /* If the input and output modes are both the same, we are done. */
11945 if (mode == GET_MODE (op0))
11947 /* If neither mode is BLKmode, and both modes are the same size
11948 then we can use gen_lowpart. */
11949 else if (mode != BLKmode
11950 && GET_MODE (op0) != BLKmode
11951 && known_eq (GET_MODE_PRECISION (mode),
11952 GET_MODE_PRECISION (GET_MODE (op0)))
11953 && !COMPLEX_MODE_P (GET_MODE (op0)))
11955 if (GET_CODE (op0) == SUBREG)
11956 op0 = force_reg (GET_MODE (op0), op0);
11957 temp = gen_lowpart_common (mode, op0);
11958 if (temp)
11959 op0 = temp;
11960 else
11962 if (!REG_P (op0) && !MEM_P (op0))
11963 op0 = force_reg (GET_MODE (op0), op0);
11964 op0 = gen_lowpart (mode, op0);
11967 /* If both types are integral, convert from one mode to the other. */
11968 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11969 op0 = convert_modes (mode, GET_MODE (op0), op0,
11970 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11971 /* If the output type is a bit-field type, do an extraction. */
11972 else if (reduce_bit_field)
11973 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11974 TYPE_UNSIGNED (type), NULL_RTX,
11975 mode, mode, false, NULL);
11976 /* As a last resort, spill op0 to memory, and reload it in a
11977 different mode. */
11978 else if (!MEM_P (op0))
11980 /* If the operand is not a MEM, force it into memory. Since we
11981 are going to be changing the mode of the MEM, don't call
11982 force_const_mem for constants because we don't allow pool
11983 constants to change mode. */
11984 tree inner_type = TREE_TYPE (treeop0);
11986 gcc_assert (!TREE_ADDRESSABLE (exp));
11988 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11989 target
11990 = assign_stack_temp_for_type
11991 (TYPE_MODE (inner_type),
11992 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11994 emit_move_insn (target, op0);
11995 op0 = target;
11998 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11999 output type is such that the operand is known to be aligned, indicate
12000 that it is. Otherwise, we need only be concerned about alignment for
12001 non-BLKmode results. */
12002 if (MEM_P (op0))
12004 enum insn_code icode;
12006 if (modifier != EXPAND_WRITE
12007 && modifier != EXPAND_MEMORY
12008 && !inner_reference_p
12009 && mode != BLKmode
12010 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12012 /* If the target does have special handling for unaligned
12013 loads of mode then use them. */
12014 if ((icode = optab_handler (movmisalign_optab, mode))
12015 != CODE_FOR_nothing)
12017 rtx reg;
12019 op0 = adjust_address (op0, mode, 0);
12020 /* We've already validated the memory, and we're creating a
12021 new pseudo destination. The predicates really can't
12022 fail. */
12023 reg = gen_reg_rtx (mode);
12025 /* Nor can the insn generator. */
12026 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12027 emit_insn (insn);
12028 return reg;
12030 else if (STRICT_ALIGNMENT)
12032 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12033 poly_uint64 temp_size = mode_size;
12034 if (GET_MODE (op0) != BLKmode)
12035 temp_size = upper_bound (temp_size,
12036 GET_MODE_SIZE (GET_MODE (op0)));
12037 rtx new_rtx
12038 = assign_stack_temp_for_type (mode, temp_size, type);
12039 rtx new_with_op0_mode
12040 = adjust_address (new_rtx, GET_MODE (op0), 0);
12042 gcc_assert (!TREE_ADDRESSABLE (exp));
12044 if (GET_MODE (op0) == BLKmode)
12046 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12047 emit_block_move (new_with_op0_mode, op0, size_rtx,
12048 (modifier == EXPAND_STACK_PARM
12049 ? BLOCK_OP_CALL_PARM
12050 : BLOCK_OP_NORMAL));
12052 else
12053 emit_move_insn (new_with_op0_mode, op0);
12055 op0 = new_rtx;
12059 op0 = adjust_address (op0, mode, 0);
12062 return op0;
12064 case MODIFY_EXPR:
12066 tree lhs = treeop0;
12067 tree rhs = treeop1;
12068 gcc_assert (ignore);
12070 /* Check for |= or &= of a bitfield of size one into another bitfield
12071 of size 1. In this case, (unless we need the result of the
12072 assignment) we can do this more efficiently with a
12073 test followed by an assignment, if necessary.
12075 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12076 things change so we do, this code should be enhanced to
12077 support it. */
12078 if (TREE_CODE (lhs) == COMPONENT_REF
12079 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12080 || TREE_CODE (rhs) == BIT_AND_EXPR)
12081 && TREE_OPERAND (rhs, 0) == lhs
12082 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12083 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12084 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12086 rtx_code_label *label = gen_label_rtx ();
12087 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12088 profile_probability prob = profile_probability::uninitialized ();
12089 if (value)
12090 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12091 else
12092 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12093 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12094 false);
12095 do_pending_stack_adjust ();
12096 emit_label (label);
12097 return const0_rtx;
12100 expand_assignment (lhs, rhs, false);
12101 return const0_rtx;
12104 case ADDR_EXPR:
12105 return expand_expr_addr_expr (exp, target, tmode, modifier);
12107 case REALPART_EXPR:
12108 op0 = expand_normal (treeop0);
12109 return read_complex_part (op0, false);
12111 case IMAGPART_EXPR:
12112 op0 = expand_normal (treeop0);
12113 return read_complex_part (op0, true);
12115 case RETURN_EXPR:
12116 case LABEL_EXPR:
12117 case GOTO_EXPR:
12118 case SWITCH_EXPR:
12119 case ASM_EXPR:
12120 /* Expanded in cfgexpand.cc. */
12121 gcc_unreachable ();
12123 case TRY_CATCH_EXPR:
12124 case CATCH_EXPR:
12125 case EH_FILTER_EXPR:
12126 case TRY_FINALLY_EXPR:
12127 case EH_ELSE_EXPR:
12128 /* Lowered by tree-eh.cc. */
12129 gcc_unreachable ();
12131 case WITH_CLEANUP_EXPR:
12132 case CLEANUP_POINT_EXPR:
12133 case TARGET_EXPR:
12134 case CASE_LABEL_EXPR:
12135 case VA_ARG_EXPR:
12136 case BIND_EXPR:
12137 case INIT_EXPR:
12138 case CONJ_EXPR:
12139 case COMPOUND_EXPR:
12140 case PREINCREMENT_EXPR:
12141 case PREDECREMENT_EXPR:
12142 case POSTINCREMENT_EXPR:
12143 case POSTDECREMENT_EXPR:
12144 case LOOP_EXPR:
12145 case EXIT_EXPR:
12146 case COMPOUND_LITERAL_EXPR:
12147 /* Lowered by gimplify.cc. */
12148 gcc_unreachable ();
12150 case FDESC_EXPR:
12151 /* Function descriptors are not valid except for as
12152 initialization constants, and should not be expanded. */
12153 gcc_unreachable ();
12155 case WITH_SIZE_EXPR:
12156 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12157 have pulled out the size to use in whatever context it needed. */
12158 return expand_expr_real (treeop0, original_target, tmode,
12159 modifier, alt_rtl, inner_reference_p);
12161 default:
12162 return expand_expr_real_2 (&ops, target, tmode, modifier);
12166 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12167 signedness of TYPE), possibly returning the result in TARGET.
12168 TYPE is known to be a partial integer type. */
12169 static rtx
12170 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12172 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12173 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12174 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12175 && (!target || GET_MODE (target) == mode));
12177 /* For constant values, reduce using wide_int_to_tree. */
12178 if (poly_int_rtx_p (exp))
12180 auto value = wi::to_poly_wide (exp, mode);
12181 tree t = wide_int_to_tree (type, value);
12182 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12184 else if (TYPE_UNSIGNED (type))
12186 rtx mask = immed_wide_int_const
12187 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12188 return expand_and (mode, exp, mask, target);
12190 else
12192 int count = GET_MODE_PRECISION (mode) - prec;
12193 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12194 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12198 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12199 when applied to the address of EXP produces an address known to be
12200 aligned more than BIGGEST_ALIGNMENT. */
12202 static bool
12203 is_aligning_offset (const_tree offset, const_tree exp)
12205 /* Strip off any conversions. */
12206 while (CONVERT_EXPR_P (offset))
12207 offset = TREE_OPERAND (offset, 0);
12209 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12210 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12211 if (TREE_CODE (offset) != BIT_AND_EXPR
12212 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12213 || compare_tree_int (TREE_OPERAND (offset, 1),
12214 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12215 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12216 return false;
12218 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12219 It must be NEGATE_EXPR. Then strip any more conversions. */
12220 offset = TREE_OPERAND (offset, 0);
12221 while (CONVERT_EXPR_P (offset))
12222 offset = TREE_OPERAND (offset, 0);
12224 if (TREE_CODE (offset) != NEGATE_EXPR)
12225 return false;
12227 offset = TREE_OPERAND (offset, 0);
12228 while (CONVERT_EXPR_P (offset))
12229 offset = TREE_OPERAND (offset, 0);
12231 /* This must now be the address of EXP. */
12232 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12235 /* Return a STRING_CST corresponding to ARG's constant initializer either
12236 if it's a string constant, or, when VALREP is set, any other constant,
12237 or null otherwise.
12238 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12239 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12240 to the size of the byte string. If nonnull, set *DECL to the constant
12241 declaration ARG refers to. */
12243 static tree
12244 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12245 bool valrep = false)
12247 tree dummy = NULL_TREE;
12248 if (!mem_size)
12249 mem_size = &dummy;
12251 /* Store the type of the original expression before conversions
12252 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12253 removed. */
12254 tree argtype = TREE_TYPE (arg);
12256 tree array;
12257 STRIP_NOPS (arg);
12259 /* Non-constant index into the character array in an ARRAY_REF
12260 expression or null. */
12261 tree varidx = NULL_TREE;
12263 poly_int64 base_off = 0;
12265 if (TREE_CODE (arg) == ADDR_EXPR)
12267 arg = TREE_OPERAND (arg, 0);
12268 tree ref = arg;
12269 if (TREE_CODE (arg) == ARRAY_REF)
12271 tree idx = TREE_OPERAND (arg, 1);
12272 if (TREE_CODE (idx) != INTEGER_CST)
12274 /* From a pointer (but not array) argument extract the variable
12275 index to prevent get_addr_base_and_unit_offset() from failing
12276 due to it. Use it later to compute the non-constant offset
12277 into the string and return it to the caller. */
12278 varidx = idx;
12279 ref = TREE_OPERAND (arg, 0);
12281 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12282 return NULL_TREE;
12284 if (!integer_zerop (array_ref_low_bound (arg)))
12285 return NULL_TREE;
12287 if (!integer_onep (array_ref_element_size (arg)))
12288 return NULL_TREE;
12291 array = get_addr_base_and_unit_offset (ref, &base_off);
12292 if (!array
12293 || (TREE_CODE (array) != VAR_DECL
12294 && TREE_CODE (array) != CONST_DECL
12295 && TREE_CODE (array) != STRING_CST))
12296 return NULL_TREE;
12298 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12300 tree arg0 = TREE_OPERAND (arg, 0);
12301 tree arg1 = TREE_OPERAND (arg, 1);
12303 tree offset;
12304 tree str = string_constant (arg0, &offset, mem_size, decl);
12305 if (!str)
12307 str = string_constant (arg1, &offset, mem_size, decl);
12308 arg1 = arg0;
12311 if (str)
12313 /* Avoid pointers to arrays (see bug 86622). */
12314 if (POINTER_TYPE_P (TREE_TYPE (arg))
12315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12316 && !(decl && !*decl)
12317 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12318 && tree_fits_uhwi_p (*mem_size)
12319 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12320 return NULL_TREE;
12322 tree type = TREE_TYPE (offset);
12323 arg1 = fold_convert (type, arg1);
12324 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12325 return str;
12327 return NULL_TREE;
12329 else if (TREE_CODE (arg) == SSA_NAME)
12331 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12332 if (!is_gimple_assign (stmt))
12333 return NULL_TREE;
12335 tree rhs1 = gimple_assign_rhs1 (stmt);
12336 tree_code code = gimple_assign_rhs_code (stmt);
12337 if (code == ADDR_EXPR)
12338 return string_constant (rhs1, ptr_offset, mem_size, decl);
12339 else if (code != POINTER_PLUS_EXPR)
12340 return NULL_TREE;
12342 tree offset;
12343 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12345 /* Avoid pointers to arrays (see bug 86622). */
12346 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12347 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12348 && !(decl && !*decl)
12349 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12350 && tree_fits_uhwi_p (*mem_size)
12351 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12352 return NULL_TREE;
12354 tree rhs2 = gimple_assign_rhs2 (stmt);
12355 tree type = TREE_TYPE (offset);
12356 rhs2 = fold_convert (type, rhs2);
12357 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12358 return str;
12360 return NULL_TREE;
12362 else if (DECL_P (arg))
12363 array = arg;
12364 else
12365 return NULL_TREE;
12367 tree offset = wide_int_to_tree (sizetype, base_off);
12368 if (varidx)
12370 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12371 return NULL_TREE;
12373 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12374 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12375 if (TREE_CODE (chartype) != INTEGER_TYPE)
12376 return NULL;
12378 offset = fold_convert (sizetype, varidx);
12381 if (TREE_CODE (array) == STRING_CST)
12383 *ptr_offset = fold_convert (sizetype, offset);
12384 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12385 if (decl)
12386 *decl = NULL_TREE;
12387 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12388 >= TREE_STRING_LENGTH (array));
12389 return array;
12392 tree init = ctor_for_folding (array);
12393 if (!init || init == error_mark_node)
12394 return NULL_TREE;
12396 if (valrep)
12398 HOST_WIDE_INT cstoff;
12399 if (!base_off.is_constant (&cstoff))
12400 return NULL_TREE;
12402 /* Check that the host and target are sane. */
12403 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12404 return NULL_TREE;
12406 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12407 if (typesz <= 0 || (int) typesz != typesz)
12408 return NULL_TREE;
12410 HOST_WIDE_INT size = typesz;
12411 if (VAR_P (array)
12412 && DECL_SIZE_UNIT (array)
12413 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12415 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12416 gcc_checking_assert (size >= typesz);
12419 /* If value representation was requested convert the initializer
12420 for the whole array or object into a string of bytes forming
12421 its value representation and return it. */
12422 unsigned char *bytes = XNEWVEC (unsigned char, size);
12423 int r = native_encode_initializer (init, bytes, size);
12424 if (r < typesz)
12426 XDELETEVEC (bytes);
12427 return NULL_TREE;
12430 if (r < size)
12431 memset (bytes + r, '\0', size - r);
12433 const char *p = reinterpret_cast<const char *>(bytes);
12434 init = build_string_literal (size, p, char_type_node);
12435 init = TREE_OPERAND (init, 0);
12436 init = TREE_OPERAND (init, 0);
12437 XDELETE (bytes);
12439 *mem_size = size_int (TREE_STRING_LENGTH (init));
12440 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12442 if (decl)
12443 *decl = array;
12445 return init;
12448 if (TREE_CODE (init) == CONSTRUCTOR)
12450 /* Convert the 64-bit constant offset to a wider type to avoid
12451 overflow and use it to obtain the initializer for the subobject
12452 it points into. */
12453 offset_int wioff;
12454 if (!base_off.is_constant (&wioff))
12455 return NULL_TREE;
12457 wioff *= BITS_PER_UNIT;
12458 if (!wi::fits_uhwi_p (wioff))
12459 return NULL_TREE;
12461 base_off = wioff.to_uhwi ();
12462 unsigned HOST_WIDE_INT fieldoff = 0;
12463 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12464 &fieldoff);
12465 if (!init || init == error_mark_node)
12466 return NULL_TREE;
12468 HOST_WIDE_INT cstoff;
12469 if (!base_off.is_constant (&cstoff))
12470 return NULL_TREE;
12472 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12473 tree off = build_int_cst (sizetype, cstoff);
12474 if (varidx)
12475 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12476 else
12477 offset = off;
12480 *ptr_offset = offset;
12482 tree inittype = TREE_TYPE (init);
12484 if (TREE_CODE (init) == INTEGER_CST
12485 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12486 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12488 /* Check that the host and target are sane. */
12489 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12490 return NULL_TREE;
12492 /* For a reference to (address of) a single constant character,
12493 store the native representation of the character in CHARBUF.
12494 If the reference is to an element of an array or a member
12495 of a struct, only consider narrow characters until ctors
12496 for wide character arrays are transformed to STRING_CSTs
12497 like those for narrow arrays. */
12498 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12499 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12500 if (len > 0)
12502 /* Construct a string literal with elements of INITTYPE and
12503 the representation above. Then strip
12504 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12505 init = build_string_literal (len, (char *)charbuf, inittype);
12506 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12510 tree initsize = TYPE_SIZE_UNIT (inittype);
12512 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12514 /* Fold an empty/zero constructor for an implicitly initialized
12515 object or subobject into the empty string. */
12517 /* Determine the character type from that of the original
12518 expression. */
12519 tree chartype = argtype;
12520 if (POINTER_TYPE_P (chartype))
12521 chartype = TREE_TYPE (chartype);
12522 while (TREE_CODE (chartype) == ARRAY_TYPE)
12523 chartype = TREE_TYPE (chartype);
12525 if (INTEGRAL_TYPE_P (chartype)
12526 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12528 /* Convert a char array to an empty STRING_CST having an array
12529 of the expected type and size. */
12530 if (!initsize)
12531 initsize = integer_zero_node;
12533 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12534 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12535 return NULL_TREE;
12537 init = build_string_literal (size, NULL, chartype, size);
12538 init = TREE_OPERAND (init, 0);
12539 init = TREE_OPERAND (init, 0);
12541 *ptr_offset = integer_zero_node;
12545 if (decl)
12546 *decl = array;
12548 if (TREE_CODE (init) != STRING_CST)
12549 return NULL_TREE;
12551 *mem_size = initsize;
12553 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12555 return init;
12558 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12559 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12560 non-constant) offset in bytes within the string that ARG is accessing.
12561 If MEM_SIZE is non-zero the storage size of the memory is returned.
12562 If DECL is non-zero the constant declaration is returned if available. */
12564 tree
12565 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12567 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12570 /* Similar to string_constant, return a STRING_CST corresponding
12571 to the value representation of the first argument if it's
12572 a constant. */
12574 tree
12575 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12577 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12580 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12581 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12582 for C2 > 0 to x & C3 == C2
12583 for C2 < 0 to x & C3 == (C2 & C3). */
12584 enum tree_code
12585 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12587 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12588 tree treeop0 = gimple_assign_rhs1 (stmt);
12589 tree treeop1 = gimple_assign_rhs2 (stmt);
12590 tree type = TREE_TYPE (*arg0);
12591 scalar_int_mode mode;
12592 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12593 return code;
12594 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12595 || TYPE_PRECISION (type) <= 1
12596 || TYPE_UNSIGNED (type)
12597 /* Signed x % c == 0 should have been optimized into unsigned modulo
12598 earlier. */
12599 || integer_zerop (*arg1)
12600 /* If c is known to be non-negative, modulo will be expanded as unsigned
12601 modulo. */
12602 || get_range_pos_neg (treeop0) == 1)
12603 return code;
12605 /* x % c == d where d < 0 && d <= -c should be always false. */
12606 if (tree_int_cst_sgn (*arg1) == -1
12607 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12608 return code;
12610 int prec = TYPE_PRECISION (type);
12611 wide_int w = wi::to_wide (treeop1) - 1;
12612 w |= wi::shifted_mask (0, prec - 1, true, prec);
12613 tree c3 = wide_int_to_tree (type, w);
12614 tree c4 = *arg1;
12615 if (tree_int_cst_sgn (*arg1) == -1)
12616 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12618 rtx op0 = expand_normal (treeop0);
12619 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12621 bool speed_p = optimize_insn_for_speed_p ();
12623 do_pending_stack_adjust ();
12625 location_t loc = gimple_location (stmt);
12626 struct separate_ops ops;
12627 ops.code = TRUNC_MOD_EXPR;
12628 ops.location = loc;
12629 ops.type = TREE_TYPE (treeop0);
12630 ops.op0 = treeop0;
12631 ops.op1 = treeop1;
12632 ops.op2 = NULL_TREE;
12633 start_sequence ();
12634 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12635 EXPAND_NORMAL);
12636 rtx_insn *moinsns = get_insns ();
12637 end_sequence ();
12639 unsigned mocost = seq_cost (moinsns, speed_p);
12640 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12641 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12643 ops.code = BIT_AND_EXPR;
12644 ops.location = loc;
12645 ops.type = TREE_TYPE (treeop0);
12646 ops.op0 = treeop0;
12647 ops.op1 = c3;
12648 ops.op2 = NULL_TREE;
12649 start_sequence ();
12650 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12651 EXPAND_NORMAL);
12652 rtx_insn *muinsns = get_insns ();
12653 end_sequence ();
12655 unsigned mucost = seq_cost (muinsns, speed_p);
12656 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12657 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12659 if (mocost <= mucost)
12661 emit_insn (moinsns);
12662 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12663 return code;
12666 emit_insn (muinsns);
12667 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12668 *arg1 = c4;
12669 return code;
12672 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12673 If C1 is odd to:
12674 (X - C2) * C3 <= C4 (or >), where
12675 C3 is modular multiplicative inverse of C1 and 1<<prec and
12676 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12677 if C2 > ((1<<prec) - 1) % C1).
12678 If C1 is even, S = ctz (C1) and C2 is 0, use
12679 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12680 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12682 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12683 unsigned):
12684 (X * C3) + C4 <= 2 * C4, where
12685 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12686 C4 is ((1<<(prec - 1) - 1) / C1).
12687 If C1 is even, S = ctz(C1), use
12688 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12689 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12690 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12692 See the Hacker's Delight book, section 10-17. */
12693 enum tree_code
12694 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12696 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12697 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12699 if (optimize < 2)
12700 return code;
12702 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12703 if (stmt == NULL)
12704 return code;
12706 tree treeop0 = gimple_assign_rhs1 (stmt);
12707 tree treeop1 = gimple_assign_rhs2 (stmt);
12708 if (TREE_CODE (treeop0) != SSA_NAME
12709 || TREE_CODE (treeop1) != INTEGER_CST
12710 /* Don't optimize the undefined behavior case x % 0;
12711 x % 1 should have been optimized into zero, punt if
12712 it makes it here for whatever reason;
12713 x % -c should have been optimized into x % c. */
12714 || compare_tree_int (treeop1, 2) <= 0
12715 /* Likewise x % c == d where d >= c should be always false. */
12716 || tree_int_cst_le (treeop1, *arg1))
12717 return code;
12719 /* Unsigned x % pow2 is handled right already, for signed
12720 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12721 if (integer_pow2p (treeop1))
12722 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12724 tree type = TREE_TYPE (*arg0);
12725 scalar_int_mode mode;
12726 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12727 return code;
12728 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12729 || TYPE_PRECISION (type) <= 1)
12730 return code;
12732 signop sgn = UNSIGNED;
12733 /* If both operands are known to have the sign bit clear, handle
12734 even the signed modulo case as unsigned. treeop1 is always
12735 positive >= 2, checked above. */
12736 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12737 sgn = SIGNED;
12739 if (!TYPE_UNSIGNED (type))
12741 if (tree_int_cst_sgn (*arg1) == -1)
12742 return code;
12743 type = unsigned_type_for (type);
12744 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12745 return code;
12748 int prec = TYPE_PRECISION (type);
12749 wide_int w = wi::to_wide (treeop1);
12750 int shift = wi::ctz (w);
12751 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12752 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12753 If C1 is odd, we can handle all cases by subtracting
12754 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12755 e.g. by testing for overflow on the subtraction, punt on that for now
12756 though. */
12757 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12759 if (sgn == SIGNED)
12760 return code;
12761 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12762 if (wi::gtu_p (wi::to_wide (*arg1), x))
12763 return code;
12766 imm_use_iterator imm_iter;
12767 use_operand_p use_p;
12768 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12770 gimple *use_stmt = USE_STMT (use_p);
12771 /* Punt if treeop0 is used in the same bb in a division
12772 or another modulo with the same divisor. We should expect
12773 the division and modulo combined together. */
12774 if (use_stmt == stmt
12775 || gimple_bb (use_stmt) != gimple_bb (stmt))
12776 continue;
12777 if (!is_gimple_assign (use_stmt)
12778 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12779 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12780 continue;
12781 if (gimple_assign_rhs1 (use_stmt) != treeop0
12782 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12783 continue;
12784 return code;
12787 w = wi::lrshift (w, shift);
12788 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12789 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12790 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12791 tree c3 = wide_int_to_tree (type, m);
12792 tree c5 = NULL_TREE;
12793 wide_int d, e;
12794 if (sgn == UNSIGNED)
12796 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12797 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12798 otherwise use < or subtract one from C4. E.g. for
12799 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12800 x % 3U == 1 already needs to be
12801 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12802 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12803 d -= 1;
12804 if (shift)
12805 d = wi::lrshift (d, shift);
12807 else
12809 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12810 if (!shift)
12811 d = wi::lshift (e, 1);
12812 else
12814 e = wi::bit_and (e, wi::mask (shift, true, prec));
12815 d = wi::lrshift (e, shift - 1);
12817 c5 = wide_int_to_tree (type, e);
12819 tree c4 = wide_int_to_tree (type, d);
12821 rtx op0 = expand_normal (treeop0);
12822 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12824 bool speed_p = optimize_insn_for_speed_p ();
12826 do_pending_stack_adjust ();
12828 location_t loc = gimple_location (stmt);
12829 struct separate_ops ops;
12830 ops.code = TRUNC_MOD_EXPR;
12831 ops.location = loc;
12832 ops.type = TREE_TYPE (treeop0);
12833 ops.op0 = treeop0;
12834 ops.op1 = treeop1;
12835 ops.op2 = NULL_TREE;
12836 start_sequence ();
12837 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12838 EXPAND_NORMAL);
12839 rtx_insn *moinsns = get_insns ();
12840 end_sequence ();
12842 unsigned mocost = seq_cost (moinsns, speed_p);
12843 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12844 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12846 tree t = fold_convert_loc (loc, type, treeop0);
12847 if (!integer_zerop (*arg1))
12848 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12849 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12850 if (sgn == SIGNED)
12851 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12852 if (shift)
12854 tree s = build_int_cst (NULL_TREE, shift);
12855 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12858 start_sequence ();
12859 rtx mur = expand_normal (t);
12860 rtx_insn *muinsns = get_insns ();
12861 end_sequence ();
12863 unsigned mucost = seq_cost (muinsns, speed_p);
12864 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12865 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12867 if (mocost <= mucost)
12869 emit_insn (moinsns);
12870 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12871 return code;
12874 emit_insn (muinsns);
12875 *arg0 = make_tree (type, mur);
12876 *arg1 = c4;
12877 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12880 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12882 void
12883 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12885 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12886 || code == LT_EXPR || code == LE_EXPR);
12887 gcc_checking_assert (integer_zerop (*arg1));
12889 if (!optimize)
12890 return;
12892 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12893 if (stmt == NULL)
12894 return;
12896 tree treeop0 = gimple_assign_rhs1 (stmt);
12897 tree treeop1 = gimple_assign_rhs2 (stmt);
12898 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12899 return;
12901 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12902 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12903 "assuming signed overflow does not occur when "
12904 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12905 op_symbol_code (code), op_symbol_code (code));
12907 *arg0 = treeop0;
12908 *arg1 = treeop1;
12912 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
12913 a single bit equality/inequality test, returns where the result is located. */
12915 static rtx
12916 expand_single_bit_test (location_t loc, enum tree_code code,
12917 tree inner, int bitnum,
12918 tree result_type, rtx target,
12919 machine_mode mode)
12921 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
12923 tree type = TREE_TYPE (inner);
12924 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
12925 int ops_unsigned;
12926 tree signed_type, unsigned_type, intermediate_type;
12927 gimple *inner_def;
12929 /* First, see if we can fold the single bit test into a sign-bit
12930 test. */
12931 if (bitnum == TYPE_PRECISION (type) - 1
12932 && type_has_mode_precision_p (type))
12934 tree stype = signed_type_for (type);
12935 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12936 result_type,
12937 fold_convert_loc (loc, stype, inner),
12938 build_int_cst (stype, 0));
12939 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
12942 /* Otherwise we have (A & C) != 0 where C is a single bit,
12943 convert that into ((A >> C2) & 1). Where C2 = log2(C).
12944 Similarly for (A & C) == 0. */
12946 /* If INNER is a right shift of a constant and it plus BITNUM does
12947 not overflow, adjust BITNUM and INNER. */
12948 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
12949 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
12950 && bitnum < TYPE_PRECISION (type)
12951 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
12952 TYPE_PRECISION (type) - bitnum))
12954 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
12955 inner = gimple_assign_rhs1 (inner_def);
12958 /* If we are going to be able to omit the AND below, we must do our
12959 operations as unsigned. If we must use the AND, we have a choice.
12960 Normally unsigned is faster, but for some machines signed is. */
12961 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
12962 && !flag_syntax_only) ? 0 : 1;
12964 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
12965 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
12966 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
12967 inner = fold_convert_loc (loc, intermediate_type, inner);
12969 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
12971 if (CONST_SCALAR_INT_P (inner0))
12973 wide_int t = rtx_mode_t (inner0, operand_mode);
12974 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
12975 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
12977 int bitpos = bitnum;
12979 if (BYTES_BIG_ENDIAN)
12980 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
12982 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
12983 operand_mode, mode, 0, NULL);
12985 if (code == EQ_EXPR)
12986 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
12987 NULL_RTX, 1, OPTAB_LIB_WIDEN);
12988 if (GET_MODE (inner0) != mode)
12990 rtx t = gen_reg_rtx (mode);
12991 convert_move (t, inner0, 0);
12992 return t;
12994 return inner0;
12997 /* Generate code to calculate OPS, and exploded expression
12998 using a store-flag instruction and return an rtx for the result.
12999 OPS reflects a comparison.
13001 If TARGET is nonzero, store the result there if convenient.
13003 Return zero if there is no suitable set-flag instruction
13004 available on this machine.
13006 Once expand_expr has been called on the arguments of the comparison,
13007 we are committed to doing the store flag, since it is not safe to
13008 re-evaluate the expression. We emit the store-flag insn by calling
13009 emit_store_flag, but only expand the arguments if we have a reason
13010 to believe that emit_store_flag will be successful. If we think that
13011 it will, but it isn't, we have to simulate the store-flag with a
13012 set/jump/set sequence. */
13014 static rtx
13015 do_store_flag (sepops ops, rtx target, machine_mode mode)
13017 enum rtx_code code;
13018 tree arg0, arg1, type;
13019 machine_mode operand_mode;
13020 int unsignedp;
13021 rtx op0, op1;
13022 rtx subtarget = target;
13023 location_t loc = ops->location;
13025 arg0 = ops->op0;
13026 arg1 = ops->op1;
13028 /* Don't crash if the comparison was erroneous. */
13029 if (arg0 == error_mark_node || arg1 == error_mark_node)
13030 return const0_rtx;
13032 type = TREE_TYPE (arg0);
13033 operand_mode = TYPE_MODE (type);
13034 unsignedp = TYPE_UNSIGNED (type);
13036 /* We won't bother with BLKmode store-flag operations because it would mean
13037 passing a lot of information to emit_store_flag. */
13038 if (operand_mode == BLKmode)
13039 return 0;
13041 /* We won't bother with store-flag operations involving function pointers
13042 when function pointers must be canonicalized before comparisons. */
13043 if (targetm.have_canonicalize_funcptr_for_compare ()
13044 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13045 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13046 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13047 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13048 return 0;
13050 STRIP_NOPS (arg0);
13051 STRIP_NOPS (arg1);
13053 /* For vector typed comparisons emit code to generate the desired
13054 all-ones or all-zeros mask. */
13055 if (VECTOR_TYPE_P (ops->type))
13057 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13058 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13059 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13060 return expand_vec_cmp_expr (ops->type, ifexp, target);
13061 else
13062 gcc_unreachable ();
13065 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13066 into (x - C2) * C3 < C4. */
13067 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13068 && TREE_CODE (arg0) == SSA_NAME
13069 && TREE_CODE (arg1) == INTEGER_CST)
13071 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13072 &arg0, &arg1);
13073 if (new_code != ops->code)
13075 struct separate_ops nops = *ops;
13076 nops.code = ops->code = new_code;
13077 nops.op0 = arg0;
13078 nops.op1 = arg1;
13079 nops.type = TREE_TYPE (arg0);
13080 return do_store_flag (&nops, target, mode);
13084 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13085 if (!unsignedp
13086 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13087 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13088 && integer_zerop (arg1)
13089 && TREE_CODE (arg0) == SSA_NAME)
13090 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13092 /* Get the rtx comparison code to use. We know that EXP is a comparison
13093 operation of some type. Some comparisons against 1 and -1 can be
13094 converted to comparisons with zero. Do so here so that the tests
13095 below will be aware that we have a comparison with zero. These
13096 tests will not catch constants in the first operand, but constants
13097 are rarely passed as the first operand. */
13099 switch (ops->code)
13101 case EQ_EXPR:
13102 code = EQ;
13103 break;
13104 case NE_EXPR:
13105 code = NE;
13106 break;
13107 case LT_EXPR:
13108 if (integer_onep (arg1))
13109 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13110 else
13111 code = unsignedp ? LTU : LT;
13112 break;
13113 case LE_EXPR:
13114 if (! unsignedp && integer_all_onesp (arg1))
13115 arg1 = integer_zero_node, code = LT;
13116 else
13117 code = unsignedp ? LEU : LE;
13118 break;
13119 case GT_EXPR:
13120 if (! unsignedp && integer_all_onesp (arg1))
13121 arg1 = integer_zero_node, code = GE;
13122 else
13123 code = unsignedp ? GTU : GT;
13124 break;
13125 case GE_EXPR:
13126 if (integer_onep (arg1))
13127 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13128 else
13129 code = unsignedp ? GEU : GE;
13130 break;
13132 case UNORDERED_EXPR:
13133 code = UNORDERED;
13134 break;
13135 case ORDERED_EXPR:
13136 code = ORDERED;
13137 break;
13138 case UNLT_EXPR:
13139 code = UNLT;
13140 break;
13141 case UNLE_EXPR:
13142 code = UNLE;
13143 break;
13144 case UNGT_EXPR:
13145 code = UNGT;
13146 break;
13147 case UNGE_EXPR:
13148 code = UNGE;
13149 break;
13150 case UNEQ_EXPR:
13151 code = UNEQ;
13152 break;
13153 case LTGT_EXPR:
13154 code = LTGT;
13155 break;
13157 default:
13158 gcc_unreachable ();
13161 /* Put a constant second. */
13162 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13163 || TREE_CODE (arg0) == FIXED_CST)
13165 std::swap (arg0, arg1);
13166 code = swap_condition (code);
13169 /* If this is an equality or inequality test of a single bit, we can
13170 do this by shifting the bit being tested to the low-order bit and
13171 masking the result with the constant 1. If the condition was EQ,
13172 we xor it with 1. This does not require an scc insn and is faster
13173 than an scc insn even if we have it. */
13175 if ((code == NE || code == EQ)
13176 && (integer_zerop (arg1)
13177 || integer_pow2p (arg1))
13178 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13180 wide_int nz = tree_nonzero_bits (arg0);
13181 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
13182 /* If the defining statement was (x & POW2), then use that instead of
13183 the non-zero bits. */
13184 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13186 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13187 arg0 = gimple_assign_rhs1 (srcstmt);
13190 if (wi::popcount (nz) == 1
13191 && (integer_zerop (arg1)
13192 || wi::to_wide (arg1) == nz))
13194 int bitnum = wi::exact_log2 (nz);
13195 enum tree_code tcode = EQ_EXPR;
13196 if ((code == NE) ^ !integer_zerop (arg1))
13197 tcode = NE_EXPR;
13199 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13200 return expand_single_bit_test (loc, tcode,
13201 arg0,
13202 bitnum, type, target, mode);
13207 if (! get_subtarget (target)
13208 || GET_MODE (subtarget) != operand_mode)
13209 subtarget = 0;
13211 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13213 if (target == 0)
13214 target = gen_reg_rtx (mode);
13216 /* Try a cstore if possible. */
13217 return emit_store_flag_force (target, code, op0, op1,
13218 operand_mode, unsignedp,
13219 (TYPE_PRECISION (ops->type) == 1
13220 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13223 /* Attempt to generate a casesi instruction. Returns true if successful,
13224 false otherwise (i.e. if there is no casesi instruction).
13226 DEFAULT_PROBABILITY is the probability of jumping to the default
13227 label. */
13228 bool
13229 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13230 rtx table_label, rtx default_label, rtx fallback_label,
13231 profile_probability default_probability)
13233 class expand_operand ops[5];
13234 scalar_int_mode index_mode = SImode;
13235 rtx op1, op2, index;
13237 if (! targetm.have_casesi ())
13238 return false;
13240 /* The index must be some form of integer. Convert it to SImode. */
13241 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13242 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13244 rtx rangertx = expand_normal (range);
13246 /* We must handle the endpoints in the original mode. */
13247 index_expr = build2 (MINUS_EXPR, index_type,
13248 index_expr, minval);
13249 minval = integer_zero_node;
13250 index = expand_normal (index_expr);
13251 if (default_label)
13252 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13253 omode, 1, default_label,
13254 default_probability);
13255 /* Now we can safely truncate. */
13256 index = convert_to_mode (index_mode, index, 0);
13258 else
13260 if (omode != index_mode)
13262 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13263 index_expr = fold_convert (index_type, index_expr);
13266 index = expand_normal (index_expr);
13269 do_pending_stack_adjust ();
13271 op1 = expand_normal (minval);
13272 op2 = expand_normal (range);
13274 create_input_operand (&ops[0], index, index_mode);
13275 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13276 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13277 create_fixed_operand (&ops[3], table_label);
13278 create_fixed_operand (&ops[4], (default_label
13279 ? default_label
13280 : fallback_label));
13281 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13282 return true;
13285 /* Attempt to generate a tablejump instruction; same concept. */
13286 /* Subroutine of the next function.
13288 INDEX is the value being switched on, with the lowest value
13289 in the table already subtracted.
13290 MODE is its expected mode (needed if INDEX is constant).
13291 RANGE is the length of the jump table.
13292 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13294 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13295 index value is out of range.
13296 DEFAULT_PROBABILITY is the probability of jumping to
13297 the default label. */
13299 static void
13300 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13301 rtx default_label, profile_probability default_probability)
13303 rtx temp, vector;
13305 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13306 cfun->cfg->max_jumptable_ents = INTVAL (range);
13308 /* Do an unsigned comparison (in the proper mode) between the index
13309 expression and the value which represents the length of the range.
13310 Since we just finished subtracting the lower bound of the range
13311 from the index expression, this comparison allows us to simultaneously
13312 check that the original index expression value is both greater than
13313 or equal to the minimum value of the range and less than or equal to
13314 the maximum value of the range. */
13316 if (default_label)
13317 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13318 default_label, default_probability);
13320 /* If index is in range, it must fit in Pmode.
13321 Convert to Pmode so we can index with it. */
13322 if (mode != Pmode)
13324 unsigned int width;
13326 /* We know the value of INDEX is between 0 and RANGE. If we have a
13327 sign-extended subreg, and RANGE does not have the sign bit set, then
13328 we have a value that is valid for both sign and zero extension. In
13329 this case, we get better code if we sign extend. */
13330 if (GET_CODE (index) == SUBREG
13331 && SUBREG_PROMOTED_VAR_P (index)
13332 && SUBREG_PROMOTED_SIGNED_P (index)
13333 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13334 <= HOST_BITS_PER_WIDE_INT)
13335 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13336 index = convert_to_mode (Pmode, index, 0);
13337 else
13338 index = convert_to_mode (Pmode, index, 1);
13341 /* Don't let a MEM slip through, because then INDEX that comes
13342 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13343 and break_out_memory_refs will go to work on it and mess it up. */
13344 #ifdef PIC_CASE_VECTOR_ADDRESS
13345 if (flag_pic && !REG_P (index))
13346 index = copy_to_mode_reg (Pmode, index);
13347 #endif
13349 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13350 GET_MODE_SIZE, because this indicates how large insns are. The other
13351 uses should all be Pmode, because they are addresses. This code
13352 could fail if addresses and insns are not the same size. */
13353 index = simplify_gen_binary (MULT, Pmode, index,
13354 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13355 Pmode));
13356 index = simplify_gen_binary (PLUS, Pmode, index,
13357 gen_rtx_LABEL_REF (Pmode, table_label));
13359 #ifdef PIC_CASE_VECTOR_ADDRESS
13360 if (flag_pic)
13361 index = PIC_CASE_VECTOR_ADDRESS (index);
13362 else
13363 #endif
13364 index = memory_address (CASE_VECTOR_MODE, index);
13365 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13366 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13367 convert_move (temp, vector, 0);
13369 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13371 /* If we are generating PIC code or if the table is PC-relative, the
13372 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13373 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13374 emit_barrier ();
13377 bool
13378 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13379 rtx table_label, rtx default_label,
13380 profile_probability default_probability)
13382 rtx index;
13384 if (! targetm.have_tablejump ())
13385 return false;
13387 index_expr = fold_build2 (MINUS_EXPR, index_type,
13388 fold_convert (index_type, index_expr),
13389 fold_convert (index_type, minval));
13390 index = expand_normal (index_expr);
13391 do_pending_stack_adjust ();
13393 do_tablejump (index, TYPE_MODE (index_type),
13394 convert_modes (TYPE_MODE (index_type),
13395 TYPE_MODE (TREE_TYPE (range)),
13396 expand_normal (range),
13397 TYPE_UNSIGNED (TREE_TYPE (range))),
13398 table_label, default_label, default_probability);
13399 return true;
13402 /* Return a CONST_VECTOR rtx representing vector mask for
13403 a VECTOR_CST of booleans. */
13404 static rtx
13405 const_vector_mask_from_tree (tree exp)
13407 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13408 machine_mode inner = GET_MODE_INNER (mode);
13410 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13411 VECTOR_CST_NELTS_PER_PATTERN (exp));
13412 unsigned int count = builder.encoded_nelts ();
13413 for (unsigned int i = 0; i < count; ++i)
13415 tree elt = VECTOR_CST_ELT (exp, i);
13416 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13417 if (integer_zerop (elt))
13418 builder.quick_push (CONST0_RTX (inner));
13419 else if (integer_onep (elt)
13420 || integer_minus_onep (elt))
13421 builder.quick_push (CONSTM1_RTX (inner));
13422 else
13423 gcc_unreachable ();
13425 return builder.build ();
13428 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13429 static rtx
13430 const_vector_from_tree (tree exp)
13432 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13434 if (initializer_zerop (exp))
13435 return CONST0_RTX (mode);
13437 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13438 return const_vector_mask_from_tree (exp);
13440 machine_mode inner = GET_MODE_INNER (mode);
13442 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13443 VECTOR_CST_NELTS_PER_PATTERN (exp));
13444 unsigned int count = builder.encoded_nelts ();
13445 for (unsigned int i = 0; i < count; ++i)
13447 tree elt = VECTOR_CST_ELT (exp, i);
13448 if (TREE_CODE (elt) == REAL_CST)
13449 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13450 inner));
13451 else if (TREE_CODE (elt) == FIXED_CST)
13452 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13453 inner));
13454 else
13455 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13456 inner));
13458 return builder.build ();
13461 /* Build a decl for a personality function given a language prefix. */
13463 tree
13464 build_personality_function (const char *lang)
13466 const char *unwind_and_version;
13467 tree decl, type;
13468 char *name;
13470 switch (targetm_common.except_unwind_info (&global_options))
13472 case UI_NONE:
13473 return NULL;
13474 case UI_SJLJ:
13475 unwind_and_version = "_sj0";
13476 break;
13477 case UI_DWARF2:
13478 case UI_TARGET:
13479 unwind_and_version = "_v0";
13480 break;
13481 case UI_SEH:
13482 unwind_and_version = "_seh0";
13483 break;
13484 default:
13485 gcc_unreachable ();
13488 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13490 type = build_function_type_list (unsigned_type_node,
13491 integer_type_node, integer_type_node,
13492 long_long_unsigned_type_node,
13493 ptr_type_node, ptr_type_node, NULL_TREE);
13494 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13495 get_identifier (name), type);
13496 DECL_ARTIFICIAL (decl) = 1;
13497 DECL_EXTERNAL (decl) = 1;
13498 TREE_PUBLIC (decl) = 1;
13500 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13501 are the flags assigned by targetm.encode_section_info. */
13502 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13504 return decl;
13507 /* Extracts the personality function of DECL and returns the corresponding
13508 libfunc. */
13511 get_personality_function (tree decl)
13513 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13514 enum eh_personality_kind pk;
13516 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13517 if (pk == eh_personality_none)
13518 return NULL;
13520 if (!personality
13521 && pk == eh_personality_any)
13522 personality = lang_hooks.eh_personality ();
13524 if (pk == eh_personality_lang)
13525 gcc_assert (personality != NULL_TREE);
13527 return XEXP (DECL_RTL (personality), 0);
13530 /* Returns a tree for the size of EXP in bytes. */
13532 static tree
13533 tree_expr_size (const_tree exp)
13535 if (DECL_P (exp)
13536 && DECL_SIZE_UNIT (exp) != 0)
13537 return DECL_SIZE_UNIT (exp);
13538 else
13539 return size_in_bytes (TREE_TYPE (exp));
13542 /* Return an rtx for the size in bytes of the value of EXP. */
13545 expr_size (tree exp)
13547 tree size;
13549 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13550 size = TREE_OPERAND (exp, 1);
13551 else
13553 size = tree_expr_size (exp);
13554 gcc_assert (size);
13555 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13558 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13561 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13562 if the size can vary or is larger than an integer. */
13564 HOST_WIDE_INT
13565 int_expr_size (const_tree exp)
13567 tree size;
13569 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13570 size = TREE_OPERAND (exp, 1);
13571 else
13573 size = tree_expr_size (exp);
13574 gcc_assert (size);
13577 if (size == 0 || !tree_fits_shwi_p (size))
13578 return -1;
13580 return tree_to_shwi (size);