hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / expr.cc
blob556bcf7ef59b13cf4bdb05f6b87569474d2d84ec
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 true if we know how to implement OP using vectors of bytes. */
992 static bool
993 can_use_qi_vectors (by_pieces_operation op)
995 return (op == COMPARE_BY_PIECES
996 || op == SET_BY_PIECES
997 || op == CLEAR_BY_PIECES);
1000 /* Return true if optabs exists for the mode and certain by pieces
1001 operations. */
1002 static bool
1003 by_pieces_mode_supported_p (fixed_size_mode mode, by_pieces_operation op)
1005 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
1006 return false;
1008 if ((op == SET_BY_PIECES || op == CLEAR_BY_PIECES)
1009 && VECTOR_MODE_P (mode)
1010 && optab_handler (vec_duplicate_optab, mode) == CODE_FOR_nothing)
1011 return false;
1013 if (op == COMPARE_BY_PIECES
1014 && !can_compare_p (EQ, mode, ccp_jump))
1015 return false;
1017 return true;
1020 /* Return the widest mode that can be used to perform part of an
1021 operation OP on SIZE bytes. Try to use QI vector modes where
1022 possible. */
1023 static fixed_size_mode
1024 widest_fixed_size_mode_for_size (unsigned int size, by_pieces_operation op)
1026 fixed_size_mode result = NARROWEST_INT_MODE;
1028 gcc_checking_assert (size > 1);
1030 /* Use QI vector only if size is wider than a WORD. */
1031 if (can_use_qi_vectors (op) && size > UNITS_PER_WORD)
1033 machine_mode mode;
1034 fixed_size_mode candidate;
1035 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1036 if (is_a<fixed_size_mode> (mode, &candidate)
1037 && GET_MODE_INNER (candidate) == QImode)
1039 if (GET_MODE_SIZE (candidate) >= size)
1040 break;
1041 if (by_pieces_mode_supported_p (candidate, op))
1042 result = candidate;
1045 if (result != NARROWEST_INT_MODE)
1046 return result;
1049 opt_scalar_int_mode tmode;
1050 scalar_int_mode mode;
1051 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
1053 mode = tmode.require ();
1054 if (GET_MODE_SIZE (mode) < size
1055 && by_pieces_mode_supported_p (mode, op))
1056 result = mode;
1059 return result;
1062 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
1063 and should be performed piecewise. */
1065 static bool
1066 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
1067 enum by_pieces_operation op)
1069 return targetm.use_by_pieces_infrastructure_p (len, align, op,
1070 optimize_insn_for_speed_p ());
1073 /* Determine whether the LEN bytes can be moved by using several move
1074 instructions. Return nonzero if a call to move_by_pieces should
1075 succeed. */
1077 bool
1078 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
1080 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
1083 /* Return number of insns required to perform operation OP by pieces
1084 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
1086 unsigned HOST_WIDE_INT
1087 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1088 unsigned int max_size, by_pieces_operation op)
1090 unsigned HOST_WIDE_INT n_insns = 0;
1091 fixed_size_mode mode;
1093 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
1095 /* NB: Round up L and ALIGN to the widest integer mode for
1096 MAX_SIZE. */
1097 mode = widest_fixed_size_mode_for_size (max_size, op);
1098 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
1100 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1101 if (up > l)
1102 l = up;
1103 align = GET_MODE_ALIGNMENT (mode);
1107 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1109 while (max_size > 1 && l > 0)
1111 mode = widest_fixed_size_mode_for_size (max_size, op);
1112 enum insn_code icode;
1114 unsigned int modesize = GET_MODE_SIZE (mode);
1116 icode = optab_handler (mov_optab, mode);
1117 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1119 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1120 l %= modesize;
1121 switch (op)
1123 default:
1124 n_insns += n_pieces;
1125 break;
1127 case COMPARE_BY_PIECES:
1128 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1129 int batch_ops = 4 * batch - 1;
1130 unsigned HOST_WIDE_INT full = n_pieces / batch;
1131 n_insns += full * batch_ops;
1132 if (n_pieces % batch != 0)
1133 n_insns++;
1134 break;
1138 max_size = modesize;
1141 gcc_assert (!l);
1142 return n_insns;
1145 /* Used when performing piecewise block operations, holds information
1146 about one of the memory objects involved. The member functions
1147 can be used to generate code for loading from the object and
1148 updating the address when iterating. */
1150 class pieces_addr
1152 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1153 stack pushes. */
1154 rtx m_obj;
1155 /* The address of the object. Can differ from that seen in the
1156 MEM rtx if we copied the address to a register. */
1157 rtx m_addr;
1158 /* Nonzero if the address on the object has an autoincrement already,
1159 signifies whether that was an increment or decrement. */
1160 signed char m_addr_inc;
1161 /* Nonzero if we intend to use autoinc without the address already
1162 having autoinc form. We will insert add insns around each memory
1163 reference, expecting later passes to form autoinc addressing modes.
1164 The only supported options are predecrement and postincrement. */
1165 signed char m_explicit_inc;
1166 /* True if we have either of the two possible cases of using
1167 autoincrement. */
1168 bool m_auto;
1169 /* True if this is an address to be used for load operations rather
1170 than stores. */
1171 bool m_is_load;
1173 /* Optionally, a function to obtain constants for any given offset into
1174 the objects, and data associated with it. */
1175 by_pieces_constfn m_constfn;
1176 void *m_cfndata;
1177 public:
1178 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1179 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1180 void increment_address (HOST_WIDE_INT);
1181 void maybe_predec (HOST_WIDE_INT);
1182 void maybe_postinc (HOST_WIDE_INT);
1183 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1184 int get_addr_inc ()
1186 return m_addr_inc;
1190 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1191 true if the operation to be performed on this object is a load
1192 rather than a store. For stores, OBJ can be NULL, in which case we
1193 assume the operation is a stack push. For loads, the optional
1194 CONSTFN and its associated CFNDATA can be used in place of the
1195 memory load. */
1197 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1198 void *cfndata)
1199 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1201 m_addr_inc = 0;
1202 m_auto = false;
1203 if (obj)
1205 rtx addr = XEXP (obj, 0);
1206 rtx_code code = GET_CODE (addr);
1207 m_addr = addr;
1208 bool dec = code == PRE_DEC || code == POST_DEC;
1209 bool inc = code == PRE_INC || code == POST_INC;
1210 m_auto = inc || dec;
1211 if (m_auto)
1212 m_addr_inc = dec ? -1 : 1;
1214 /* While we have always looked for these codes here, the code
1215 implementing the memory operation has never handled them.
1216 Support could be added later if necessary or beneficial. */
1217 gcc_assert (code != PRE_INC && code != POST_DEC);
1219 else
1221 m_addr = NULL_RTX;
1222 if (!is_load)
1224 m_auto = true;
1225 if (STACK_GROWS_DOWNWARD)
1226 m_addr_inc = -1;
1227 else
1228 m_addr_inc = 1;
1230 else
1231 gcc_assert (constfn != NULL);
1233 m_explicit_inc = 0;
1234 if (constfn)
1235 gcc_assert (is_load);
1238 /* Decide whether to use autoinc for an address involved in a memory op.
1239 MODE is the mode of the accesses, REVERSE is true if we've decided to
1240 perform the operation starting from the end, and LEN is the length of
1241 the operation. Don't override an earlier decision to set m_auto. */
1243 void
1244 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1245 HOST_WIDE_INT len)
1247 if (m_auto || m_obj == NULL_RTX)
1248 return;
1250 bool use_predec = (m_is_load
1251 ? USE_LOAD_PRE_DECREMENT (mode)
1252 : USE_STORE_PRE_DECREMENT (mode));
1253 bool use_postinc = (m_is_load
1254 ? USE_LOAD_POST_INCREMENT (mode)
1255 : USE_STORE_POST_INCREMENT (mode));
1256 machine_mode addr_mode = get_address_mode (m_obj);
1258 if (use_predec && reverse)
1260 m_addr = copy_to_mode_reg (addr_mode,
1261 plus_constant (addr_mode,
1262 m_addr, len));
1263 m_auto = true;
1264 m_explicit_inc = -1;
1266 else if (use_postinc && !reverse)
1268 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1269 m_auto = true;
1270 m_explicit_inc = 1;
1272 else if (CONSTANT_P (m_addr))
1273 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1276 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1277 are using autoincrement for this address, we don't add the offset,
1278 but we still modify the MEM's properties. */
1281 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1282 by_pieces_prev *prev)
1284 if (m_constfn)
1285 /* Pass the previous data to m_constfn. */
1286 return m_constfn (m_cfndata, prev, offset, mode);
1287 if (m_obj == NULL_RTX)
1288 return NULL_RTX;
1289 if (m_auto)
1290 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1291 else
1292 return adjust_address (m_obj, mode, offset);
1295 /* Emit an add instruction to increment the address by SIZE. */
1297 void
1298 pieces_addr::increment_address (HOST_WIDE_INT size)
1300 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1301 emit_insn (gen_add2_insn (m_addr, amount));
1304 /* If we are supposed to decrement the address after each access, emit code
1305 to do so now. Increment by SIZE (which has should have the correct sign
1306 already). */
1308 void
1309 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1311 if (m_explicit_inc >= 0)
1312 return;
1313 gcc_assert (HAVE_PRE_DECREMENT);
1314 increment_address (size);
1317 /* If we are supposed to decrement the address after each access, emit code
1318 to do so now. Increment by SIZE. */
1320 void
1321 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1323 if (m_explicit_inc <= 0)
1324 return;
1325 gcc_assert (HAVE_POST_INCREMENT);
1326 increment_address (size);
1329 /* This structure is used by do_op_by_pieces to describe the operation
1330 to be performed. */
1332 class op_by_pieces_d
1334 private:
1335 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1336 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1338 protected:
1339 pieces_addr m_to, m_from;
1340 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1341 use it to check the valid mode size. */
1342 const unsigned HOST_WIDE_INT m_len;
1343 HOST_WIDE_INT m_offset;
1344 unsigned int m_align;
1345 unsigned int m_max_size;
1346 bool m_reverse;
1347 /* True if this is a stack push. */
1348 bool m_push;
1349 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1350 bool m_overlap_op_by_pieces;
1351 /* The type of operation that we're performing. */
1352 by_pieces_operation m_op;
1354 /* Virtual functions, overriden by derived classes for the specific
1355 operation. */
1356 virtual void generate (rtx, rtx, machine_mode) = 0;
1357 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1358 virtual void finish_mode (machine_mode)
1362 public:
1363 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1364 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1365 by_pieces_operation);
1366 void run ();
1369 /* The constructor for an op_by_pieces_d structure. We require two
1370 objects named TO and FROM, which are identified as loads or stores
1371 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1372 and its associated FROM_CFN_DATA can be used to replace loads with
1373 constant values. MAX_PIECES describes the maximum number of bytes
1374 at a time which can be moved efficiently. LEN describes the length
1375 of the operation. */
1377 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1378 bool to_load, rtx from, bool from_load,
1379 by_pieces_constfn from_cfn,
1380 void *from_cfn_data,
1381 unsigned HOST_WIDE_INT len,
1382 unsigned int align, bool push,
1383 by_pieces_operation op)
1384 : m_to (to, to_load, NULL, NULL),
1385 m_from (from, from_load, from_cfn, from_cfn_data),
1386 m_len (len), m_max_size (max_pieces + 1),
1387 m_push (push), m_op (op)
1389 int toi = m_to.get_addr_inc ();
1390 int fromi = m_from.get_addr_inc ();
1391 if (toi >= 0 && fromi >= 0)
1392 m_reverse = false;
1393 else if (toi <= 0 && fromi <= 0)
1394 m_reverse = true;
1395 else
1396 gcc_unreachable ();
1398 m_offset = m_reverse ? len : 0;
1399 align = MIN (to ? MEM_ALIGN (to) : align,
1400 from ? MEM_ALIGN (from) : align);
1402 /* If copying requires more than two move insns,
1403 copy addresses to registers (to make displacements shorter)
1404 and use post-increment if available. */
1405 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1407 /* Find the mode of the largest comparison. */
1408 fixed_size_mode mode
1409 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1411 m_from.decide_autoinc (mode, m_reverse, len);
1412 m_to.decide_autoinc (mode, m_reverse, len);
1415 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1416 m_align = align;
1418 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1421 /* This function returns the largest usable integer mode for LEN bytes
1422 whose size is no bigger than size of MODE. */
1424 fixed_size_mode
1425 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1427 unsigned int size;
1430 size = GET_MODE_SIZE (mode);
1431 if (len >= size && prepare_mode (mode, m_align))
1432 break;
1433 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1434 mode = widest_fixed_size_mode_for_size (size, m_op);
1436 while (1);
1437 return mode;
1440 /* Return the smallest integer or QI vector mode that is not narrower
1441 than SIZE bytes. */
1443 fixed_size_mode
1444 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1446 /* Use QI vector only for > size of WORD. */
1447 if (can_use_qi_vectors (m_op) && size > UNITS_PER_WORD)
1449 machine_mode mode;
1450 fixed_size_mode candidate;
1451 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1452 if (is_a<fixed_size_mode> (mode, &candidate)
1453 && GET_MODE_INNER (candidate) == QImode)
1455 /* Don't return a mode wider than M_LEN. */
1456 if (GET_MODE_SIZE (candidate) > m_len)
1457 break;
1459 if (GET_MODE_SIZE (candidate) >= size
1460 && by_pieces_mode_supported_p (candidate, m_op))
1461 return candidate;
1465 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1468 /* This function contains the main loop used for expanding a block
1469 operation. First move what we can in the largest integer mode,
1470 then go to successively smaller modes. For every access, call
1471 GENFUN with the two operands and the EXTRA_DATA. */
1473 void
1474 op_by_pieces_d::run ()
1476 if (m_len == 0)
1477 return;
1479 unsigned HOST_WIDE_INT length = m_len;
1481 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1482 fixed_size_mode mode
1483 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1484 mode = get_usable_mode (mode, length);
1486 by_pieces_prev to_prev = { nullptr, mode };
1487 by_pieces_prev from_prev = { nullptr, mode };
1491 unsigned int size = GET_MODE_SIZE (mode);
1492 rtx to1 = NULL_RTX, from1;
1494 while (length >= size)
1496 if (m_reverse)
1497 m_offset -= size;
1499 to1 = m_to.adjust (mode, m_offset, &to_prev);
1500 to_prev.data = to1;
1501 to_prev.mode = mode;
1502 from1 = m_from.adjust (mode, m_offset, &from_prev);
1503 from_prev.data = from1;
1504 from_prev.mode = mode;
1506 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1507 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1509 generate (to1, from1, mode);
1511 m_to.maybe_postinc (size);
1512 m_from.maybe_postinc (size);
1514 if (!m_reverse)
1515 m_offset += size;
1517 length -= size;
1520 finish_mode (mode);
1522 if (length == 0)
1523 return;
1525 if (!m_push && m_overlap_op_by_pieces)
1527 /* NB: Generate overlapping operations if it is not a stack
1528 push since stack push must not overlap. Get the smallest
1529 fixed size mode for M_LEN bytes. */
1530 mode = smallest_fixed_size_mode_for_size (length);
1531 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1532 int gap = GET_MODE_SIZE (mode) - length;
1533 if (gap > 0)
1535 /* If size of MODE > M_LEN, generate the last operation
1536 in MODE for the remaining bytes with ovelapping memory
1537 from the previois operation. */
1538 if (m_reverse)
1539 m_offset += gap;
1540 else
1541 m_offset -= gap;
1542 length += gap;
1545 else
1547 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1548 mode = widest_fixed_size_mode_for_size (size, m_op);
1549 mode = get_usable_mode (mode, length);
1552 while (1);
1555 /* Derived class from op_by_pieces_d, providing support for block move
1556 operations. */
1558 #ifdef PUSH_ROUNDING
1559 #define PUSHG_P(to) ((to) == nullptr)
1560 #else
1561 #define PUSHG_P(to) false
1562 #endif
1564 class move_by_pieces_d : public op_by_pieces_d
1566 insn_gen_fn m_gen_fun;
1567 void generate (rtx, rtx, machine_mode) final override;
1568 bool prepare_mode (machine_mode, unsigned int) final override;
1570 public:
1571 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1572 unsigned int align)
1573 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1574 NULL, len, align, PUSHG_P (to), MOVE_BY_PIECES)
1577 rtx finish_retmode (memop_ret);
1580 /* Return true if MODE can be used for a set of copies, given an
1581 alignment ALIGN. Prepare whatever data is necessary for later
1582 calls to generate. */
1584 bool
1585 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1587 insn_code icode = optab_handler (mov_optab, mode);
1588 m_gen_fun = GEN_FCN (icode);
1589 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1592 /* A callback used when iterating for a compare_by_pieces_operation.
1593 OP0 and OP1 are the values that have been loaded and should be
1594 compared in MODE. If OP0 is NULL, this means we should generate a
1595 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1596 gen function that should be used to generate the mode. */
1598 void
1599 move_by_pieces_d::generate (rtx op0, rtx op1,
1600 machine_mode mode ATTRIBUTE_UNUSED)
1602 #ifdef PUSH_ROUNDING
1603 if (op0 == NULL_RTX)
1605 emit_single_push_insn (mode, op1, NULL);
1606 return;
1608 #endif
1609 emit_insn (m_gen_fun (op0, op1));
1612 /* Perform the final adjustment at the end of a string to obtain the
1613 correct return value for the block operation.
1614 Return value is based on RETMODE argument. */
1617 move_by_pieces_d::finish_retmode (memop_ret retmode)
1619 gcc_assert (!m_reverse);
1620 if (retmode == RETURN_END_MINUS_ONE)
1622 m_to.maybe_postinc (-1);
1623 --m_offset;
1625 return m_to.adjust (QImode, m_offset);
1628 /* Generate several move instructions to copy LEN bytes from block FROM to
1629 block TO. (These are MEM rtx's with BLKmode).
1631 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1632 used to push FROM to the stack.
1634 ALIGN is maximum stack alignment we can assume.
1636 Return value is based on RETMODE argument. */
1639 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1640 unsigned int align, memop_ret retmode)
1642 #ifndef PUSH_ROUNDING
1643 if (to == NULL)
1644 gcc_unreachable ();
1645 #endif
1647 move_by_pieces_d data (to, from, len, align);
1649 data.run ();
1651 if (retmode != RETURN_BEGIN)
1652 return data.finish_retmode (retmode);
1653 else
1654 return to;
1657 /* Derived class from op_by_pieces_d, providing support for block move
1658 operations. */
1660 class store_by_pieces_d : public op_by_pieces_d
1662 insn_gen_fn m_gen_fun;
1664 void generate (rtx, rtx, machine_mode) final override;
1665 bool prepare_mode (machine_mode, unsigned int) final override;
1667 public:
1668 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1669 unsigned HOST_WIDE_INT len, unsigned int align,
1670 by_pieces_operation op)
1671 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1672 cfn_data, len, align, false, op)
1675 rtx finish_retmode (memop_ret);
1678 /* Return true if MODE can be used for a set of stores, given an
1679 alignment ALIGN. Prepare whatever data is necessary for later
1680 calls to generate. */
1682 bool
1683 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1685 insn_code icode = optab_handler (mov_optab, mode);
1686 m_gen_fun = GEN_FCN (icode);
1687 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1690 /* A callback used when iterating for a store_by_pieces_operation.
1691 OP0 and OP1 are the values that have been loaded and should be
1692 compared in MODE. If OP0 is NULL, this means we should generate a
1693 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1694 gen function that should be used to generate the mode. */
1696 void
1697 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1699 emit_insn (m_gen_fun (op0, op1));
1702 /* Perform the final adjustment at the end of a string to obtain the
1703 correct return value for the block operation.
1704 Return value is based on RETMODE argument. */
1707 store_by_pieces_d::finish_retmode (memop_ret retmode)
1709 gcc_assert (!m_reverse);
1710 if (retmode == RETURN_END_MINUS_ONE)
1712 m_to.maybe_postinc (-1);
1713 --m_offset;
1715 return m_to.adjust (QImode, m_offset);
1718 /* Determine whether the LEN bytes generated by CONSTFUN can be
1719 stored to memory using several move instructions. CONSTFUNDATA is
1720 a pointer which will be passed as argument in every CONSTFUN call.
1721 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1722 a memset operation and false if it's a copy of a constant string.
1723 Return true if a call to store_by_pieces should succeed. */
1725 bool
1726 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1727 by_pieces_constfn constfun,
1728 void *constfundata, unsigned int align, bool memsetp)
1730 unsigned HOST_WIDE_INT l;
1731 unsigned int max_size;
1732 HOST_WIDE_INT offset = 0;
1733 enum insn_code icode;
1734 int reverse;
1735 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1736 rtx cst ATTRIBUTE_UNUSED;
1738 if (len == 0)
1739 return true;
1741 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1742 memsetp
1743 ? SET_BY_PIECES
1744 : STORE_BY_PIECES,
1745 optimize_insn_for_speed_p ()))
1746 return false;
1748 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1750 /* We would first store what we can in the largest integer mode, then go to
1751 successively smaller modes. */
1753 for (reverse = 0;
1754 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1755 reverse++)
1757 l = len;
1758 max_size = STORE_MAX_PIECES + 1;
1759 while (max_size > 1 && l > 0)
1761 auto op = memsetp ? SET_BY_PIECES : STORE_BY_PIECES;
1762 auto mode = widest_fixed_size_mode_for_size (max_size, op);
1764 icode = optab_handler (mov_optab, mode);
1765 if (icode != CODE_FOR_nothing
1766 && align >= GET_MODE_ALIGNMENT (mode))
1768 unsigned int size = GET_MODE_SIZE (mode);
1770 while (l >= size)
1772 if (reverse)
1773 offset -= size;
1775 cst = (*constfun) (constfundata, nullptr, offset, mode);
1776 /* All CONST_VECTORs can be loaded for memset since
1777 vec_duplicate_optab is a precondition to pick a
1778 vector mode for the memset expander. */
1779 if (!((memsetp && VECTOR_MODE_P (mode))
1780 || targetm.legitimate_constant_p (mode, cst)))
1781 return false;
1783 if (!reverse)
1784 offset += size;
1786 l -= size;
1790 max_size = GET_MODE_SIZE (mode);
1793 /* The code above should have handled everything. */
1794 gcc_assert (!l);
1797 return true;
1800 /* Generate several move instructions to store LEN bytes generated by
1801 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1802 pointer which will be passed as argument in every CONSTFUN call.
1803 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1804 a memset operation and false if it's a copy of a constant string.
1805 Return value is based on RETMODE argument. */
1808 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1809 by_pieces_constfn constfun,
1810 void *constfundata, unsigned int align, bool memsetp,
1811 memop_ret retmode)
1813 if (len == 0)
1815 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1816 return to;
1819 gcc_assert (targetm.use_by_pieces_infrastructure_p
1820 (len, align,
1821 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1822 optimize_insn_for_speed_p ()));
1824 store_by_pieces_d data (to, constfun, constfundata, len, align,
1825 memsetp ? SET_BY_PIECES : STORE_BY_PIECES);
1826 data.run ();
1828 if (retmode != RETURN_BEGIN)
1829 return data.finish_retmode (retmode);
1830 else
1831 return to;
1834 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1835 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1837 static void
1838 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1840 if (len == 0)
1841 return;
1843 /* Use builtin_memset_read_str to support vector mode broadcast. */
1844 char c = 0;
1845 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1846 CLEAR_BY_PIECES);
1847 data.run ();
1850 /* Context used by compare_by_pieces_genfn. It stores the fail label
1851 to jump to in case of miscomparison, and for branch ratios greater than 1,
1852 it stores an accumulator and the current and maximum counts before
1853 emitting another branch. */
1855 class compare_by_pieces_d : public op_by_pieces_d
1857 rtx_code_label *m_fail_label;
1858 rtx m_accumulator;
1859 int m_count, m_batch;
1861 void generate (rtx, rtx, machine_mode) final override;
1862 bool prepare_mode (machine_mode, unsigned int) final override;
1863 void finish_mode (machine_mode) final override;
1865 public:
1866 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1867 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1868 rtx_code_label *fail_label)
1869 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1870 op1_cfn_data, len, align, false, COMPARE_BY_PIECES)
1872 m_fail_label = fail_label;
1876 /* A callback used when iterating for a compare_by_pieces_operation.
1877 OP0 and OP1 are the values that have been loaded and should be
1878 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1879 context structure. */
1881 void
1882 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1884 if (m_batch > 1)
1886 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1887 true, OPTAB_LIB_WIDEN);
1888 if (m_count != 0)
1889 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1890 true, OPTAB_LIB_WIDEN);
1891 m_accumulator = temp;
1893 if (++m_count < m_batch)
1894 return;
1896 m_count = 0;
1897 op0 = m_accumulator;
1898 op1 = const0_rtx;
1899 m_accumulator = NULL_RTX;
1901 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1902 m_fail_label, profile_probability::uninitialized ());
1905 /* Return true if MODE can be used for a set of moves and comparisons,
1906 given an alignment ALIGN. Prepare whatever data is necessary for
1907 later calls to generate. */
1909 bool
1910 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1912 insn_code icode = optab_handler (mov_optab, mode);
1913 if (icode == CODE_FOR_nothing
1914 || align < GET_MODE_ALIGNMENT (mode)
1915 || !can_compare_p (EQ, mode, ccp_jump))
1916 return false;
1917 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1918 if (m_batch < 0)
1919 return false;
1920 m_accumulator = NULL_RTX;
1921 m_count = 0;
1922 return true;
1925 /* Called after expanding a series of comparisons in MODE. If we have
1926 accumulated results for which we haven't emitted a branch yet, do
1927 so now. */
1929 void
1930 compare_by_pieces_d::finish_mode (machine_mode mode)
1932 if (m_accumulator != NULL_RTX)
1933 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1934 NULL_RTX, NULL, m_fail_label,
1935 profile_probability::uninitialized ());
1938 /* Generate several move instructions to compare LEN bytes from blocks
1939 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1941 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1942 used to push FROM to the stack.
1944 ALIGN is maximum stack alignment we can assume.
1946 Optionally, the caller can pass a constfn and associated data in A1_CFN
1947 and A1_CFN_DATA. describing that the second operand being compared is a
1948 known constant and how to obtain its data. */
1950 static rtx
1951 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1952 rtx target, unsigned int align,
1953 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1955 rtx_code_label *fail_label = gen_label_rtx ();
1956 rtx_code_label *end_label = gen_label_rtx ();
1958 if (target == NULL_RTX
1959 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1960 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1962 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1963 fail_label);
1965 data.run ();
1967 emit_move_insn (target, const0_rtx);
1968 emit_jump (end_label);
1969 emit_barrier ();
1970 emit_label (fail_label);
1971 emit_move_insn (target, const1_rtx);
1972 emit_label (end_label);
1974 return target;
1977 /* Emit code to move a block Y to a block X. This may be done with
1978 string-move instructions, with multiple scalar move instructions,
1979 or with a library call.
1981 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1982 SIZE is an rtx that says how long they are.
1983 ALIGN is the maximum alignment we can assume they have.
1984 METHOD describes what kind of copy this is, and what mechanisms may be used.
1985 MIN_SIZE is the minimal size of block to move
1986 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1987 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1989 Return the address of the new block, if memcpy is called and returns it,
1990 0 otherwise. */
1993 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1994 unsigned int expected_align, HOST_WIDE_INT expected_size,
1995 unsigned HOST_WIDE_INT min_size,
1996 unsigned HOST_WIDE_INT max_size,
1997 unsigned HOST_WIDE_INT probable_max_size,
1998 bool bail_out_libcall, bool *is_move_done,
1999 bool might_overlap)
2001 int may_use_call;
2002 rtx retval = 0;
2003 unsigned int align;
2005 if (is_move_done)
2006 *is_move_done = true;
2008 gcc_assert (size);
2009 if (CONST_INT_P (size) && INTVAL (size) == 0)
2010 return 0;
2012 switch (method)
2014 case BLOCK_OP_NORMAL:
2015 case BLOCK_OP_TAILCALL:
2016 may_use_call = 1;
2017 break;
2019 case BLOCK_OP_CALL_PARM:
2020 may_use_call = block_move_libcall_safe_for_call_parm ();
2022 /* Make inhibit_defer_pop nonzero around the library call
2023 to force it to pop the arguments right away. */
2024 NO_DEFER_POP;
2025 break;
2027 case BLOCK_OP_NO_LIBCALL:
2028 may_use_call = 0;
2029 break;
2031 case BLOCK_OP_NO_LIBCALL_RET:
2032 may_use_call = -1;
2033 break;
2035 default:
2036 gcc_unreachable ();
2039 gcc_assert (MEM_P (x) && MEM_P (y));
2040 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2041 gcc_assert (align >= BITS_PER_UNIT);
2043 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2044 block copy is more efficient for other large modes, e.g. DCmode. */
2045 x = adjust_address (x, BLKmode, 0);
2046 y = adjust_address (y, BLKmode, 0);
2048 /* If source and destination are the same, no need to copy anything. */
2049 if (rtx_equal_p (x, y)
2050 && !MEM_VOLATILE_P (x)
2051 && !MEM_VOLATILE_P (y))
2052 return 0;
2054 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2055 can be incorrect is coming from __builtin_memcpy. */
2056 poly_int64 const_size;
2057 if (poly_int_rtx_p (size, &const_size))
2059 x = shallow_copy_rtx (x);
2060 y = shallow_copy_rtx (y);
2061 set_mem_size (x, const_size);
2062 set_mem_size (y, const_size);
2065 bool pieces_ok = CONST_INT_P (size)
2066 && can_move_by_pieces (INTVAL (size), align);
2067 bool pattern_ok = false;
2069 if (!pieces_ok || might_overlap)
2071 pattern_ok
2072 = emit_block_move_via_pattern (x, y, size, align,
2073 expected_align, expected_size,
2074 min_size, max_size, probable_max_size,
2075 might_overlap);
2076 if (!pattern_ok && might_overlap)
2078 /* Do not try any of the other methods below as they are not safe
2079 for overlapping moves. */
2080 *is_move_done = false;
2081 return retval;
2085 if (pattern_ok)
2087 else if (pieces_ok)
2088 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2089 else if (may_use_call && !might_overlap
2090 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2091 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2093 if (bail_out_libcall)
2095 if (is_move_done)
2096 *is_move_done = false;
2097 return retval;
2100 if (may_use_call < 0)
2101 return pc_rtx;
2103 retval = emit_block_copy_via_libcall (x, y, size,
2104 method == BLOCK_OP_TAILCALL);
2106 else if (might_overlap)
2107 *is_move_done = false;
2108 else
2109 emit_block_move_via_loop (x, y, size, align);
2111 if (method == BLOCK_OP_CALL_PARM)
2112 OK_DEFER_POP;
2114 return retval;
2118 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
2120 unsigned HOST_WIDE_INT max, min = 0;
2121 if (GET_CODE (size) == CONST_INT)
2122 min = max = UINTVAL (size);
2123 else
2124 max = GET_MODE_MASK (GET_MODE (size));
2125 return emit_block_move_hints (x, y, size, method, 0, -1,
2126 min, max, max);
2129 /* A subroutine of emit_block_move. Returns true if calling the
2130 block move libcall will not clobber any parameters which may have
2131 already been placed on the stack. */
2133 static bool
2134 block_move_libcall_safe_for_call_parm (void)
2136 tree fn;
2138 /* If arguments are pushed on the stack, then they're safe. */
2139 if (targetm.calls.push_argument (0))
2140 return true;
2142 /* If registers go on the stack anyway, any argument is sure to clobber
2143 an outgoing argument. */
2144 #if defined (REG_PARM_STACK_SPACE)
2145 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2146 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2147 depend on its argument. */
2148 (void) fn;
2149 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2150 && REG_PARM_STACK_SPACE (fn) != 0)
2151 return false;
2152 #endif
2154 /* If any argument goes in memory, then it might clobber an outgoing
2155 argument. */
2157 CUMULATIVE_ARGS args_so_far_v;
2158 cumulative_args_t args_so_far;
2159 tree arg;
2161 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2162 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2163 args_so_far = pack_cumulative_args (&args_so_far_v);
2165 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2166 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2168 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2169 function_arg_info arg_info (mode, /*named=*/true);
2170 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2171 if (!tmp || !REG_P (tmp))
2172 return false;
2173 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2174 return false;
2175 targetm.calls.function_arg_advance (args_so_far, arg_info);
2178 return true;
2181 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2182 return true if successful.
2184 X is the destination of the copy or move.
2185 Y is the source of the copy or move.
2186 SIZE is the size of the block to be moved.
2188 MIGHT_OVERLAP indicates this originated with expansion of a
2189 builtin_memmove() and the source and destination blocks may
2190 overlap.
2193 static bool
2194 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2195 unsigned int expected_align,
2196 HOST_WIDE_INT expected_size,
2197 unsigned HOST_WIDE_INT min_size,
2198 unsigned HOST_WIDE_INT max_size,
2199 unsigned HOST_WIDE_INT probable_max_size,
2200 bool might_overlap)
2202 if (expected_align < align)
2203 expected_align = align;
2204 if (expected_size != -1)
2206 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2207 expected_size = probable_max_size;
2208 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2209 expected_size = min_size;
2212 /* Since this is a move insn, we don't care about volatility. */
2213 temporary_volatile_ok v (true);
2215 /* Try the most limited insn first, because there's no point
2216 including more than one in the machine description unless
2217 the more limited one has some advantage. */
2219 opt_scalar_int_mode mode_iter;
2220 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2222 scalar_int_mode mode = mode_iter.require ();
2223 enum insn_code code;
2224 if (might_overlap)
2225 code = direct_optab_handler (movmem_optab, mode);
2226 else
2227 code = direct_optab_handler (cpymem_optab, mode);
2229 if (code != CODE_FOR_nothing
2230 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2231 here because if SIZE is less than the mode mask, as it is
2232 returned by the macro, it will definitely be less than the
2233 actual mode mask. Since SIZE is within the Pmode address
2234 space, we limit MODE to Pmode. */
2235 && ((CONST_INT_P (size)
2236 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2237 <= (GET_MODE_MASK (mode) >> 1)))
2238 || max_size <= (GET_MODE_MASK (mode) >> 1)
2239 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2241 class expand_operand ops[9];
2242 unsigned int nops;
2244 /* ??? When called via emit_block_move_for_call, it'd be
2245 nice if there were some way to inform the backend, so
2246 that it doesn't fail the expansion because it thinks
2247 emitting the libcall would be more efficient. */
2248 nops = insn_data[(int) code].n_generator_args;
2249 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2251 create_fixed_operand (&ops[0], x);
2252 create_fixed_operand (&ops[1], y);
2253 /* The check above guarantees that this size conversion is valid. */
2254 create_convert_operand_to (&ops[2], size, mode, true);
2255 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2256 if (nops >= 6)
2258 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2259 create_integer_operand (&ops[5], expected_size);
2261 if (nops >= 8)
2263 create_integer_operand (&ops[6], min_size);
2264 /* If we cannot represent the maximal size,
2265 make parameter NULL. */
2266 if ((HOST_WIDE_INT) max_size != -1)
2267 create_integer_operand (&ops[7], max_size);
2268 else
2269 create_fixed_operand (&ops[7], NULL);
2271 if (nops == 9)
2273 /* If we cannot represent the maximal size,
2274 make parameter NULL. */
2275 if ((HOST_WIDE_INT) probable_max_size != -1)
2276 create_integer_operand (&ops[8], probable_max_size);
2277 else
2278 create_fixed_operand (&ops[8], NULL);
2280 if (maybe_expand_insn (code, nops, ops))
2281 return true;
2285 return false;
2288 /* A subroutine of emit_block_move. Copy the data via an explicit
2289 loop. This is used only when libcalls are forbidden. */
2290 /* ??? It'd be nice to copy in hunks larger than QImode. */
2292 static void
2293 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2294 unsigned int align ATTRIBUTE_UNUSED)
2296 rtx_code_label *cmp_label, *top_label;
2297 rtx iter, x_addr, y_addr, tmp;
2298 machine_mode x_addr_mode = get_address_mode (x);
2299 machine_mode y_addr_mode = get_address_mode (y);
2300 machine_mode iter_mode;
2302 iter_mode = GET_MODE (size);
2303 if (iter_mode == VOIDmode)
2304 iter_mode = word_mode;
2306 top_label = gen_label_rtx ();
2307 cmp_label = gen_label_rtx ();
2308 iter = gen_reg_rtx (iter_mode);
2310 emit_move_insn (iter, const0_rtx);
2312 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2313 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2314 do_pending_stack_adjust ();
2316 emit_jump (cmp_label);
2317 emit_label (top_label);
2319 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2320 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2322 if (x_addr_mode != y_addr_mode)
2323 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2324 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2326 x = change_address (x, QImode, x_addr);
2327 y = change_address (y, QImode, y_addr);
2329 emit_move_insn (x, y);
2331 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2332 true, OPTAB_LIB_WIDEN);
2333 if (tmp != iter)
2334 emit_move_insn (iter, tmp);
2336 emit_label (cmp_label);
2338 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2339 true, top_label,
2340 profile_probability::guessed_always ()
2341 .apply_scale (9, 10));
2344 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2345 TAILCALL is true if this is a tail call. */
2348 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2349 rtx size, bool tailcall)
2351 rtx dst_addr, src_addr;
2352 tree call_expr, dst_tree, src_tree, size_tree;
2353 machine_mode size_mode;
2355 /* Since dst and src are passed to a libcall, mark the corresponding
2356 tree EXPR as addressable. */
2357 tree dst_expr = MEM_EXPR (dst);
2358 tree src_expr = MEM_EXPR (src);
2359 if (dst_expr)
2360 mark_addressable (dst_expr);
2361 if (src_expr)
2362 mark_addressable (src_expr);
2364 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2365 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2366 dst_tree = make_tree (ptr_type_node, dst_addr);
2368 src_addr = copy_addr_to_reg (XEXP (src, 0));
2369 src_addr = convert_memory_address (ptr_mode, src_addr);
2370 src_tree = make_tree (ptr_type_node, src_addr);
2372 size_mode = TYPE_MODE (sizetype);
2373 size = convert_to_mode (size_mode, size, 1);
2374 size = copy_to_mode_reg (size_mode, size);
2375 size_tree = make_tree (sizetype, size);
2377 /* It is incorrect to use the libcall calling conventions for calls to
2378 memcpy/memmove/memcmp because they can be provided by the user. */
2379 tree fn = builtin_decl_implicit (fncode);
2380 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2381 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2383 return expand_call (call_expr, NULL_RTX, false);
2386 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2387 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2388 otherwise return null. */
2391 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2392 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2393 HOST_WIDE_INT align)
2395 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2397 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2398 target = NULL_RTX;
2400 class expand_operand ops[5];
2401 create_output_operand (&ops[0], target, insn_mode);
2402 create_fixed_operand (&ops[1], arg1_rtx);
2403 create_fixed_operand (&ops[2], arg2_rtx);
2404 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2405 TYPE_UNSIGNED (arg3_type));
2406 create_integer_operand (&ops[4], align);
2407 if (maybe_expand_insn (icode, 5, ops))
2408 return ops[0].value;
2409 return NULL_RTX;
2412 /* Expand a block compare between X and Y with length LEN using the
2413 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2414 of the expression that was used to calculate the length. ALIGN
2415 gives the known minimum common alignment. */
2417 static rtx
2418 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2419 unsigned align)
2421 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2422 implementing memcmp because it will stop if it encounters two
2423 zero bytes. */
2424 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2426 if (icode == CODE_FOR_nothing)
2427 return NULL_RTX;
2429 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2432 /* Emit code to compare a block Y to a block X. This may be done with
2433 string-compare instructions, with multiple scalar instructions,
2434 or with a library call.
2436 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2437 they are. LEN_TYPE is the type of the expression that was used to
2438 calculate it.
2440 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2441 value of a normal memcmp call, instead we can just compare for equality.
2442 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2443 returning NULL_RTX.
2445 Optionally, the caller can pass a constfn and associated data in Y_CFN
2446 and Y_CFN_DATA. describing that the second operand being compared is a
2447 known constant and how to obtain its data.
2448 Return the result of the comparison, or NULL_RTX if we failed to
2449 perform the operation. */
2452 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2453 bool equality_only, by_pieces_constfn y_cfn,
2454 void *y_cfndata)
2456 rtx result = 0;
2458 if (CONST_INT_P (len) && INTVAL (len) == 0)
2459 return const0_rtx;
2461 gcc_assert (MEM_P (x) && MEM_P (y));
2462 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2463 gcc_assert (align >= BITS_PER_UNIT);
2465 x = adjust_address (x, BLKmode, 0);
2466 y = adjust_address (y, BLKmode, 0);
2468 if (equality_only
2469 && CONST_INT_P (len)
2470 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2471 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2472 y_cfn, y_cfndata);
2473 else
2474 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2476 return result;
2479 /* Copy all or part of a value X into registers starting at REGNO.
2480 The number of registers to be filled is NREGS. */
2482 void
2483 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2485 if (nregs == 0)
2486 return;
2488 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2489 x = validize_mem (force_const_mem (mode, x));
2491 /* See if the machine can do this with a load multiple insn. */
2492 if (targetm.have_load_multiple ())
2494 rtx_insn *last = get_last_insn ();
2495 rtx first = gen_rtx_REG (word_mode, regno);
2496 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2497 GEN_INT (nregs)))
2499 emit_insn (pat);
2500 return;
2502 else
2503 delete_insns_since (last);
2506 for (int i = 0; i < nregs; i++)
2507 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2508 operand_subword_force (x, i, mode));
2511 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2512 The number of registers to be filled is NREGS. */
2514 void
2515 move_block_from_reg (int regno, rtx x, int nregs)
2517 if (nregs == 0)
2518 return;
2520 /* See if the machine can do this with a store multiple insn. */
2521 if (targetm.have_store_multiple ())
2523 rtx_insn *last = get_last_insn ();
2524 rtx first = gen_rtx_REG (word_mode, regno);
2525 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2526 GEN_INT (nregs)))
2528 emit_insn (pat);
2529 return;
2531 else
2532 delete_insns_since (last);
2535 for (int i = 0; i < nregs; i++)
2537 rtx tem = operand_subword (x, i, 1, BLKmode);
2539 gcc_assert (tem);
2541 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2545 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2546 ORIG, where ORIG is a non-consecutive group of registers represented by
2547 a PARALLEL. The clone is identical to the original except in that the
2548 original set of registers is replaced by a new set of pseudo registers.
2549 The new set has the same modes as the original set. */
2552 gen_group_rtx (rtx orig)
2554 int i, length;
2555 rtx *tmps;
2557 gcc_assert (GET_CODE (orig) == PARALLEL);
2559 length = XVECLEN (orig, 0);
2560 tmps = XALLOCAVEC (rtx, length);
2562 /* Skip a NULL entry in first slot. */
2563 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2565 if (i)
2566 tmps[0] = 0;
2568 for (; i < length; i++)
2570 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2571 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2573 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2576 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2579 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2580 except that values are placed in TMPS[i], and must later be moved
2581 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2583 static void
2584 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2585 poly_int64 ssize)
2587 rtx src;
2588 int start, i;
2589 machine_mode m = GET_MODE (orig_src);
2591 gcc_assert (GET_CODE (dst) == PARALLEL);
2593 if (m != VOIDmode
2594 && !SCALAR_INT_MODE_P (m)
2595 && !MEM_P (orig_src)
2596 && GET_CODE (orig_src) != CONCAT)
2598 scalar_int_mode imode;
2599 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2601 src = gen_reg_rtx (imode);
2602 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2604 else
2606 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2607 emit_move_insn (src, orig_src);
2609 emit_group_load_1 (tmps, dst, src, type, ssize);
2610 return;
2613 /* Check for a NULL entry, used to indicate that the parameter goes
2614 both on the stack and in registers. */
2615 if (XEXP (XVECEXP (dst, 0, 0), 0))
2616 start = 0;
2617 else
2618 start = 1;
2620 /* Process the pieces. */
2621 for (i = start; i < XVECLEN (dst, 0); i++)
2623 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2624 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2625 poly_int64 bytelen = GET_MODE_SIZE (mode);
2626 poly_int64 shift = 0;
2628 /* Handle trailing fragments that run over the size of the struct.
2629 It's the target's responsibility to make sure that the fragment
2630 cannot be strictly smaller in some cases and strictly larger
2631 in others. */
2632 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2633 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2635 /* Arrange to shift the fragment to where it belongs.
2636 extract_bit_field loads to the lsb of the reg. */
2637 if (
2638 #ifdef BLOCK_REG_PADDING
2639 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2640 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2641 #else
2642 BYTES_BIG_ENDIAN
2643 #endif
2645 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2646 bytelen = ssize - bytepos;
2647 gcc_assert (maybe_gt (bytelen, 0));
2650 /* If we won't be loading directly from memory, protect the real source
2651 from strange tricks we might play; but make sure that the source can
2652 be loaded directly into the destination. */
2653 src = orig_src;
2654 if (!MEM_P (orig_src)
2655 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src))
2656 && !CONSTANT_P (orig_src))
2658 gcc_assert (GET_MODE (orig_src) != VOIDmode);
2659 src = force_reg (GET_MODE (orig_src), orig_src);
2662 /* Optimize the access just a bit. */
2663 if (MEM_P (src)
2664 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2665 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2666 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2667 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2669 tmps[i] = gen_reg_rtx (mode);
2670 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2672 else if (COMPLEX_MODE_P (mode)
2673 && GET_MODE (src) == mode
2674 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2675 /* Let emit_move_complex do the bulk of the work. */
2676 tmps[i] = src;
2677 else if (GET_CODE (src) == CONCAT)
2679 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2680 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2681 unsigned int elt;
2682 poly_int64 subpos;
2684 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2685 && known_le (subpos + bytelen, slen0))
2687 /* The following assumes that the concatenated objects all
2688 have the same size. In this case, a simple calculation
2689 can be used to determine the object and the bit field
2690 to be extracted. */
2691 tmps[i] = XEXP (src, elt);
2692 if (maybe_ne (subpos, 0)
2693 || maybe_ne (subpos + bytelen, slen0)
2694 || (!CONSTANT_P (tmps[i])
2695 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2696 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2697 subpos * BITS_PER_UNIT,
2698 1, NULL_RTX, mode, mode, false,
2699 NULL);
2701 else
2703 rtx mem;
2705 gcc_assert (known_eq (bytepos, 0));
2706 mem = assign_stack_temp (GET_MODE (src), slen);
2707 emit_move_insn (mem, src);
2708 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2709 0, 1, NULL_RTX, mode, mode, false,
2710 NULL);
2713 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2714 && XVECLEN (dst, 0) > 1)
2715 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2716 else if (CONSTANT_P (src))
2718 if (known_eq (bytelen, ssize))
2719 tmps[i] = src;
2720 else
2722 rtx first, second;
2724 /* TODO: const_wide_int can have sizes other than this... */
2725 gcc_assert (known_eq (2 * bytelen, ssize));
2726 split_double (src, &first, &second);
2727 if (i)
2728 tmps[i] = second;
2729 else
2730 tmps[i] = first;
2733 else if (REG_P (src) && GET_MODE (src) == mode)
2734 tmps[i] = src;
2735 else
2736 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2737 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2738 mode, mode, false, NULL);
2740 if (maybe_ne (shift, 0))
2741 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2742 shift, tmps[i], 0);
2746 /* Emit code to move a block SRC of type TYPE to a block DST,
2747 where DST is non-consecutive registers represented by a PARALLEL.
2748 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2749 if not known. */
2751 void
2752 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2754 rtx *tmps;
2755 int i;
2757 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2758 emit_group_load_1 (tmps, dst, src, type, ssize);
2760 /* Copy the extracted pieces into the proper (probable) hard regs. */
2761 for (i = 0; i < XVECLEN (dst, 0); i++)
2763 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2764 if (d == NULL)
2765 continue;
2766 emit_move_insn (d, tmps[i]);
2770 /* Similar, but load SRC into new pseudos in a format that looks like
2771 PARALLEL. This can later be fed to emit_group_move to get things
2772 in the right place. */
2775 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2777 rtvec vec;
2778 int i;
2780 vec = rtvec_alloc (XVECLEN (parallel, 0));
2781 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2783 /* Convert the vector to look just like the original PARALLEL, except
2784 with the computed values. */
2785 for (i = 0; i < XVECLEN (parallel, 0); i++)
2787 rtx e = XVECEXP (parallel, 0, i);
2788 rtx d = XEXP (e, 0);
2790 if (d)
2792 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2793 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2795 RTVEC_ELT (vec, i) = e;
2798 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2801 /* Emit code to move a block SRC to block DST, where SRC and DST are
2802 non-consecutive groups of registers, each represented by a PARALLEL. */
2804 void
2805 emit_group_move (rtx dst, rtx src)
2807 int i;
2809 gcc_assert (GET_CODE (src) == PARALLEL
2810 && GET_CODE (dst) == PARALLEL
2811 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2813 /* Skip first entry if NULL. */
2814 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2815 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2816 XEXP (XVECEXP (src, 0, i), 0));
2819 /* Move a group of registers represented by a PARALLEL into pseudos. */
2822 emit_group_move_into_temps (rtx src)
2824 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2825 int i;
2827 for (i = 0; i < XVECLEN (src, 0); i++)
2829 rtx e = XVECEXP (src, 0, i);
2830 rtx d = XEXP (e, 0);
2832 if (d)
2833 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2834 RTVEC_ELT (vec, i) = e;
2837 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2840 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2841 where SRC is non-consecutive registers represented by a PARALLEL.
2842 SSIZE represents the total size of block ORIG_DST, or -1 if not
2843 known. */
2845 void
2846 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2847 poly_int64 ssize)
2849 rtx *tmps, dst;
2850 int start, finish, i;
2851 machine_mode m = GET_MODE (orig_dst);
2853 gcc_assert (GET_CODE (src) == PARALLEL);
2855 if (!SCALAR_INT_MODE_P (m)
2856 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2858 scalar_int_mode imode;
2859 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2861 dst = gen_reg_rtx (imode);
2862 emit_group_store (dst, src, type, ssize);
2863 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2865 else
2867 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2868 emit_group_store (dst, src, type, ssize);
2870 emit_move_insn (orig_dst, dst);
2871 return;
2874 /* Check for a NULL entry, used to indicate that the parameter goes
2875 both on the stack and in registers. */
2876 if (XEXP (XVECEXP (src, 0, 0), 0))
2877 start = 0;
2878 else
2879 start = 1;
2880 finish = XVECLEN (src, 0);
2882 tmps = XALLOCAVEC (rtx, finish);
2884 /* Copy the (probable) hard regs into pseudos. */
2885 for (i = start; i < finish; i++)
2887 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2888 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2890 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2891 emit_move_insn (tmps[i], reg);
2893 else
2894 tmps[i] = reg;
2897 /* If we won't be storing directly into memory, protect the real destination
2898 from strange tricks we might play. */
2899 dst = orig_dst;
2900 if (GET_CODE (dst) == PARALLEL)
2902 rtx temp;
2904 /* We can get a PARALLEL dst if there is a conditional expression in
2905 a return statement. In that case, the dst and src are the same,
2906 so no action is necessary. */
2907 if (rtx_equal_p (dst, src))
2908 return;
2910 /* It is unclear if we can ever reach here, but we may as well handle
2911 it. Allocate a temporary, and split this into a store/load to/from
2912 the temporary. */
2913 temp = assign_stack_temp (GET_MODE (dst), ssize);
2914 emit_group_store (temp, src, type, ssize);
2915 emit_group_load (dst, temp, type, ssize);
2916 return;
2918 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2920 machine_mode outer = GET_MODE (dst);
2921 machine_mode inner;
2922 poly_int64 bytepos;
2923 bool done = false;
2924 rtx temp;
2926 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2927 dst = gen_reg_rtx (outer);
2929 /* Make life a bit easier for combine: if the first element of the
2930 vector is the low part of the destination mode, use a paradoxical
2931 subreg to initialize the destination. */
2932 if (start < finish)
2934 inner = GET_MODE (tmps[start]);
2935 bytepos = subreg_lowpart_offset (inner, outer);
2936 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2937 bytepos))
2939 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
2940 if (temp)
2942 emit_move_insn (dst, temp);
2943 done = true;
2944 start++;
2949 /* If the first element wasn't the low part, try the last. */
2950 if (!done
2951 && start < finish - 1)
2953 inner = GET_MODE (tmps[finish - 1]);
2954 bytepos = subreg_lowpart_offset (inner, outer);
2955 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2956 finish - 1), 1)),
2957 bytepos))
2959 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
2960 if (temp)
2962 emit_move_insn (dst, temp);
2963 done = true;
2964 finish--;
2969 /* Otherwise, simply initialize the result to zero. */
2970 if (!done)
2971 emit_move_insn (dst, CONST0_RTX (outer));
2974 /* Process the pieces. */
2975 for (i = start; i < finish; i++)
2977 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2978 machine_mode mode = GET_MODE (tmps[i]);
2979 poly_int64 bytelen = GET_MODE_SIZE (mode);
2980 poly_uint64 adj_bytelen;
2981 rtx dest = dst;
2983 /* Handle trailing fragments that run over the size of the struct.
2984 It's the target's responsibility to make sure that the fragment
2985 cannot be strictly smaller in some cases and strictly larger
2986 in others. */
2987 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2988 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2989 adj_bytelen = ssize - bytepos;
2990 else
2991 adj_bytelen = bytelen;
2993 /* Deal with destination CONCATs by either storing into one of the parts
2994 or doing a copy after storing into a register or stack temporary. */
2995 if (GET_CODE (dst) == CONCAT)
2997 if (known_le (bytepos + adj_bytelen,
2998 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2999 dest = XEXP (dst, 0);
3001 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3003 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
3004 dest = XEXP (dst, 1);
3007 else
3009 machine_mode dest_mode = GET_MODE (dest);
3010 machine_mode tmp_mode = GET_MODE (tmps[i]);
3011 scalar_int_mode dest_imode;
3013 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
3015 /* If the source is a single scalar integer register, and the
3016 destination has a complex mode for which a same-sized integer
3017 mode exists, then we can take the left-justified part of the
3018 source in the complex mode. */
3019 if (finish == start + 1
3020 && REG_P (tmps[i])
3021 && SCALAR_INT_MODE_P (tmp_mode)
3022 && COMPLEX_MODE_P (dest_mode)
3023 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3025 const scalar_int_mode tmp_imode
3026 = as_a <scalar_int_mode> (tmp_mode);
3028 if (GET_MODE_BITSIZE (dest_imode)
3029 < GET_MODE_BITSIZE (tmp_imode))
3031 dest = gen_reg_rtx (dest_imode);
3032 if (BYTES_BIG_ENDIAN)
3033 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3034 GET_MODE_BITSIZE (tmp_imode)
3035 - GET_MODE_BITSIZE (dest_imode),
3036 NULL_RTX, 1);
3037 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3038 dst = gen_lowpart (dest_mode, dest);
3040 else
3041 dst = gen_lowpart (dest_mode, tmps[i]);
3044 /* Otherwise spill the source onto the stack using the more
3045 aligned of the two modes. */
3046 else if (GET_MODE_ALIGNMENT (dest_mode)
3047 >= GET_MODE_ALIGNMENT (tmp_mode))
3049 dest = assign_stack_temp (dest_mode,
3050 GET_MODE_SIZE (dest_mode));
3051 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3052 tmps[i]);
3053 dst = dest;
3056 else
3058 dest = assign_stack_temp (tmp_mode,
3059 GET_MODE_SIZE (tmp_mode));
3060 emit_move_insn (dest, tmps[i]);
3061 dst = adjust_address (dest, dest_mode, bytepos);
3064 break;
3068 /* Handle trailing fragments that run over the size of the struct. */
3069 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3071 /* store_bit_field always takes its value from the lsb.
3072 Move the fragment to the lsb if it's not already there. */
3073 if (
3074 #ifdef BLOCK_REG_PADDING
3075 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3076 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3077 #else
3078 BYTES_BIG_ENDIAN
3079 #endif
3082 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3083 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3084 shift, tmps[i], 0);
3087 /* Make sure not to write past the end of the struct. */
3088 store_bit_field (dest,
3089 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3090 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3091 VOIDmode, tmps[i], false, false);
3094 /* Optimize the access just a bit. */
3095 else if (MEM_P (dest)
3096 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3097 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3098 && multiple_p (bytepos * BITS_PER_UNIT,
3099 GET_MODE_ALIGNMENT (mode))
3100 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3101 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3103 else
3104 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3105 0, 0, mode, tmps[i], false, false);
3108 /* Copy from the pseudo into the (probable) hard reg. */
3109 if (orig_dst != dst)
3110 emit_move_insn (orig_dst, dst);
3113 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3114 of the value stored in X. */
3117 maybe_emit_group_store (rtx x, tree type)
3119 machine_mode mode = TYPE_MODE (type);
3120 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3121 if (GET_CODE (x) == PARALLEL)
3123 rtx result = gen_reg_rtx (mode);
3124 emit_group_store (result, x, type, int_size_in_bytes (type));
3125 return result;
3127 return x;
3130 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3132 This is used on targets that return BLKmode values in registers. */
3134 static void
3135 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3137 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3138 rtx src = NULL, dst = NULL;
3139 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3140 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3141 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3142 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3143 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3144 fixed_size_mode copy_mode;
3146 /* BLKmode registers created in the back-end shouldn't have survived. */
3147 gcc_assert (mode != BLKmode);
3149 /* If the structure doesn't take up a whole number of words, see whether
3150 SRCREG is padded on the left or on the right. If it's on the left,
3151 set PADDING_CORRECTION to the number of bits to skip.
3153 In most ABIs, the structure will be returned at the least end of
3154 the register, which translates to right padding on little-endian
3155 targets and left padding on big-endian targets. The opposite
3156 holds if the structure is returned at the most significant
3157 end of the register. */
3158 if (bytes % UNITS_PER_WORD != 0
3159 && (targetm.calls.return_in_msb (type)
3160 ? !BYTES_BIG_ENDIAN
3161 : BYTES_BIG_ENDIAN))
3162 padding_correction
3163 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3165 /* We can use a single move if we have an exact mode for the size. */
3166 else if (MEM_P (target)
3167 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3168 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3169 && bytes == GET_MODE_SIZE (mode))
3171 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3172 return;
3175 /* And if we additionally have the same mode for a register. */
3176 else if (REG_P (target)
3177 && GET_MODE (target) == mode
3178 && bytes == GET_MODE_SIZE (mode))
3180 emit_move_insn (target, srcreg);
3181 return;
3184 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3185 into a new pseudo which is a full word. */
3186 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3188 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3189 mode = word_mode;
3192 /* Copy the structure BITSIZE bits at a time. If the target lives in
3193 memory, take care of not reading/writing past its end by selecting
3194 a copy mode suited to BITSIZE. This should always be possible given
3195 how it is computed.
3197 If the target lives in register, make sure not to select a copy mode
3198 larger than the mode of the register.
3200 We could probably emit more efficient code for machines which do not use
3201 strict alignment, but it doesn't seem worth the effort at the current
3202 time. */
3204 copy_mode = word_mode;
3205 if (MEM_P (target))
3207 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3208 if (mem_mode.exists ())
3209 copy_mode = mem_mode.require ();
3211 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3212 copy_mode = tmode;
3214 for (bitpos = 0, xbitpos = padding_correction;
3215 bitpos < bytes * BITS_PER_UNIT;
3216 bitpos += bitsize, xbitpos += bitsize)
3218 /* We need a new source operand each time xbitpos is on a
3219 word boundary and when xbitpos == padding_correction
3220 (the first time through). */
3221 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3222 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3224 /* We need a new destination operand each time bitpos is on
3225 a word boundary. */
3226 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3227 dst = target;
3228 else if (bitpos % BITS_PER_WORD == 0)
3229 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3231 /* Use xbitpos for the source extraction (right justified) and
3232 bitpos for the destination store (left justified). */
3233 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3234 extract_bit_field (src, bitsize,
3235 xbitpos % BITS_PER_WORD, 1,
3236 NULL_RTX, copy_mode, copy_mode,
3237 false, NULL),
3238 false, false);
3242 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3243 register if it contains any data, otherwise return null.
3245 This is used on targets that return BLKmode values in registers. */
3248 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3250 int i, n_regs;
3251 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3252 unsigned int bitsize;
3253 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3254 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3255 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3256 fixed_size_mode dst_mode;
3257 scalar_int_mode min_mode;
3259 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3261 x = expand_normal (src);
3263 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3264 if (bytes == 0)
3265 return NULL_RTX;
3267 /* If the structure doesn't take up a whole number of words, see
3268 whether the register value should be padded on the left or on
3269 the right. Set PADDING_CORRECTION to the number of padding
3270 bits needed on the left side.
3272 In most ABIs, the structure will be returned at the least end of
3273 the register, which translates to right padding on little-endian
3274 targets and left padding on big-endian targets. The opposite
3275 holds if the structure is returned at the most significant
3276 end of the register. */
3277 if (bytes % UNITS_PER_WORD != 0
3278 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3279 ? !BYTES_BIG_ENDIAN
3280 : BYTES_BIG_ENDIAN))
3281 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3282 * BITS_PER_UNIT));
3284 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3285 dst_words = XALLOCAVEC (rtx, n_regs);
3286 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3287 min_mode = smallest_int_mode_for_size (bitsize);
3289 /* Copy the structure BITSIZE bits at a time. */
3290 for (bitpos = 0, xbitpos = padding_correction;
3291 bitpos < bytes * BITS_PER_UNIT;
3292 bitpos += bitsize, xbitpos += bitsize)
3294 /* We need a new destination pseudo each time xbitpos is
3295 on a word boundary and when xbitpos == padding_correction
3296 (the first time through). */
3297 if (xbitpos % BITS_PER_WORD == 0
3298 || xbitpos == padding_correction)
3300 /* Generate an appropriate register. */
3301 dst_word = gen_reg_rtx (word_mode);
3302 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3304 /* Clear the destination before we move anything into it. */
3305 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3308 /* Find the largest integer mode that can be used to copy all or as
3309 many bits as possible of the structure if the target supports larger
3310 copies. There are too many corner cases here w.r.t to alignments on
3311 the read/writes. So if there is any padding just use single byte
3312 operations. */
3313 opt_scalar_int_mode mode_iter;
3314 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3316 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3318 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3319 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3320 && msize <= BITS_PER_WORD)
3321 bitsize = msize;
3322 else
3323 break;
3327 /* We need a new source operand each time bitpos is on a word
3328 boundary. */
3329 if (bitpos % BITS_PER_WORD == 0)
3330 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3332 /* Use bitpos for the source extraction (left justified) and
3333 xbitpos for the destination store (right justified). */
3334 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3335 0, 0, word_mode,
3336 extract_bit_field (src_word, bitsize,
3337 bitpos % BITS_PER_WORD, 1,
3338 NULL_RTX, word_mode, word_mode,
3339 false, NULL),
3340 false, false);
3343 if (mode == BLKmode)
3345 /* Find the smallest integer mode large enough to hold the
3346 entire structure. */
3347 opt_scalar_int_mode mode_iter;
3348 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3349 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3350 break;
3352 /* A suitable mode should have been found. */
3353 mode = mode_iter.require ();
3356 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3357 dst_mode = word_mode;
3358 else
3359 dst_mode = mode;
3360 dst = gen_reg_rtx (dst_mode);
3362 for (i = 0; i < n_regs; i++)
3363 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3365 if (mode != dst_mode)
3366 dst = gen_lowpart (mode, dst);
3368 return dst;
3371 /* Add a USE expression for REG to the (possibly empty) list pointed
3372 to by CALL_FUSAGE. REG must denote a hard register. */
3374 void
3375 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3377 gcc_assert (REG_P (reg));
3379 if (!HARD_REGISTER_P (reg))
3380 return;
3382 *call_fusage
3383 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3386 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3387 to by CALL_FUSAGE. REG must denote a hard register. */
3389 void
3390 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3392 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3394 *call_fusage
3395 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3398 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3399 starting at REGNO. All of these registers must be hard registers. */
3401 void
3402 use_regs (rtx *call_fusage, int regno, int nregs)
3404 int i;
3406 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3408 for (i = 0; i < nregs; i++)
3409 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3412 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3413 PARALLEL REGS. This is for calls that pass values in multiple
3414 non-contiguous locations. The Irix 6 ABI has examples of this. */
3416 void
3417 use_group_regs (rtx *call_fusage, rtx regs)
3419 int i;
3421 for (i = 0; i < XVECLEN (regs, 0); i++)
3423 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3425 /* A NULL entry means the parameter goes both on the stack and in
3426 registers. This can also be a MEM for targets that pass values
3427 partially on the stack and partially in registers. */
3428 if (reg != 0 && REG_P (reg))
3429 use_reg (call_fusage, reg);
3433 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3434 assigment and the code of the expresion on the RHS is CODE. Return
3435 NULL otherwise. */
3437 static gimple *
3438 get_def_for_expr (tree name, enum tree_code code)
3440 gimple *def_stmt;
3442 if (TREE_CODE (name) != SSA_NAME)
3443 return NULL;
3445 def_stmt = get_gimple_for_ssa_name (name);
3446 if (!def_stmt
3447 || gimple_assign_rhs_code (def_stmt) != code)
3448 return NULL;
3450 return def_stmt;
3453 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3454 assigment and the class of the expresion on the RHS is CLASS. Return
3455 NULL otherwise. */
3457 static gimple *
3458 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3460 gimple *def_stmt;
3462 if (TREE_CODE (name) != SSA_NAME)
3463 return NULL;
3465 def_stmt = get_gimple_for_ssa_name (name);
3466 if (!def_stmt
3467 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3468 return NULL;
3470 return def_stmt;
3473 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3474 its length in bytes. */
3477 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3478 unsigned int expected_align, HOST_WIDE_INT expected_size,
3479 unsigned HOST_WIDE_INT min_size,
3480 unsigned HOST_WIDE_INT max_size,
3481 unsigned HOST_WIDE_INT probable_max_size,
3482 unsigned ctz_size)
3484 machine_mode mode = GET_MODE (object);
3485 unsigned int align;
3487 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3489 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3490 just move a zero. Otherwise, do this a piece at a time. */
3491 poly_int64 size_val;
3492 if (mode != BLKmode
3493 && poly_int_rtx_p (size, &size_val)
3494 && known_eq (size_val, GET_MODE_SIZE (mode)))
3496 rtx zero = CONST0_RTX (mode);
3497 if (zero != NULL)
3499 emit_move_insn (object, zero);
3500 return NULL;
3503 if (COMPLEX_MODE_P (mode))
3505 zero = CONST0_RTX (GET_MODE_INNER (mode));
3506 if (zero != NULL)
3508 write_complex_part (object, zero, 0, true);
3509 write_complex_part (object, zero, 1, false);
3510 return NULL;
3515 if (size == const0_rtx)
3516 return NULL;
3518 align = MEM_ALIGN (object);
3520 if (CONST_INT_P (size)
3521 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3522 CLEAR_BY_PIECES,
3523 optimize_insn_for_speed_p ()))
3524 clear_by_pieces (object, INTVAL (size), align);
3525 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3526 expected_align, expected_size,
3527 min_size, max_size, probable_max_size))
3529 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3530 min_size, max_size,
3531 NULL_RTX, 0, align))
3533 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3534 return set_storage_via_libcall (object, size, const0_rtx,
3535 method == BLOCK_OP_TAILCALL);
3536 else
3537 gcc_unreachable ();
3539 return NULL;
3543 clear_storage (rtx object, rtx size, enum block_op_methods method)
3545 unsigned HOST_WIDE_INT max, min = 0;
3546 if (GET_CODE (size) == CONST_INT)
3547 min = max = UINTVAL (size);
3548 else
3549 max = GET_MODE_MASK (GET_MODE (size));
3550 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3554 /* A subroutine of clear_storage. Expand a call to memset.
3555 Return the return value of memset, 0 otherwise. */
3558 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3560 tree call_expr, fn, object_tree, size_tree, val_tree;
3561 machine_mode size_mode;
3563 object = copy_addr_to_reg (XEXP (object, 0));
3564 object_tree = make_tree (ptr_type_node, object);
3566 if (!CONST_INT_P (val))
3567 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3568 val_tree = make_tree (integer_type_node, val);
3570 size_mode = TYPE_MODE (sizetype);
3571 size = convert_to_mode (size_mode, size, 1);
3572 size = copy_to_mode_reg (size_mode, size);
3573 size_tree = make_tree (sizetype, size);
3575 /* It is incorrect to use the libcall calling conventions for calls to
3576 memset because it can be provided by the user. */
3577 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3578 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3579 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3581 return expand_call (call_expr, NULL_RTX, false);
3584 /* Expand a setmem pattern; return true if successful. */
3586 bool
3587 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3588 unsigned int expected_align, HOST_WIDE_INT expected_size,
3589 unsigned HOST_WIDE_INT min_size,
3590 unsigned HOST_WIDE_INT max_size,
3591 unsigned HOST_WIDE_INT probable_max_size)
3593 /* Try the most limited insn first, because there's no point
3594 including more than one in the machine description unless
3595 the more limited one has some advantage. */
3597 if (expected_align < align)
3598 expected_align = align;
3599 if (expected_size != -1)
3601 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3602 expected_size = max_size;
3603 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3604 expected_size = min_size;
3607 opt_scalar_int_mode mode_iter;
3608 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3610 scalar_int_mode mode = mode_iter.require ();
3611 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3613 if (code != CODE_FOR_nothing
3614 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3615 here because if SIZE is less than the mode mask, as it is
3616 returned by the macro, it will definitely be less than the
3617 actual mode mask. Since SIZE is within the Pmode address
3618 space, we limit MODE to Pmode. */
3619 && ((CONST_INT_P (size)
3620 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3621 <= (GET_MODE_MASK (mode) >> 1)))
3622 || max_size <= (GET_MODE_MASK (mode) >> 1)
3623 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3625 class expand_operand ops[9];
3626 unsigned int nops;
3628 nops = insn_data[(int) code].n_generator_args;
3629 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3631 create_fixed_operand (&ops[0], object);
3632 /* The check above guarantees that this size conversion is valid. */
3633 create_convert_operand_to (&ops[1], size, mode, true);
3634 create_convert_operand_from (&ops[2], val, byte_mode, true);
3635 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3636 if (nops >= 6)
3638 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3639 create_integer_operand (&ops[5], expected_size);
3641 if (nops >= 8)
3643 create_integer_operand (&ops[6], min_size);
3644 /* If we cannot represent the maximal size,
3645 make parameter NULL. */
3646 if ((HOST_WIDE_INT) max_size != -1)
3647 create_integer_operand (&ops[7], max_size);
3648 else
3649 create_fixed_operand (&ops[7], NULL);
3651 if (nops == 9)
3653 /* If we cannot represent the maximal size,
3654 make parameter NULL. */
3655 if ((HOST_WIDE_INT) probable_max_size != -1)
3656 create_integer_operand (&ops[8], probable_max_size);
3657 else
3658 create_fixed_operand (&ops[8], NULL);
3660 if (maybe_expand_insn (code, nops, ops))
3661 return true;
3665 return false;
3669 /* Write to one of the components of the complex value CPLX. Write VAL to
3670 the real part if IMAG_P is false, and the imaginary part if its true.
3671 If UNDEFINED_P then the value in CPLX is currently undefined. */
3673 void
3674 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
3676 machine_mode cmode;
3677 scalar_mode imode;
3678 unsigned ibitsize;
3680 if (GET_CODE (cplx) == CONCAT)
3682 emit_move_insn (XEXP (cplx, imag_p), val);
3683 return;
3686 cmode = GET_MODE (cplx);
3687 imode = GET_MODE_INNER (cmode);
3688 ibitsize = GET_MODE_BITSIZE (imode);
3690 /* For MEMs simplify_gen_subreg may generate an invalid new address
3691 because, e.g., the original address is considered mode-dependent
3692 by the target, which restricts simplify_subreg from invoking
3693 adjust_address_nv. Instead of preparing fallback support for an
3694 invalid address, we call adjust_address_nv directly. */
3695 if (MEM_P (cplx))
3697 emit_move_insn (adjust_address_nv (cplx, imode,
3698 imag_p ? GET_MODE_SIZE (imode) : 0),
3699 val);
3700 return;
3703 /* If the sub-object is at least word sized, then we know that subregging
3704 will work. This special case is important, since store_bit_field
3705 wants to operate on integer modes, and there's rarely an OImode to
3706 correspond to TCmode. */
3707 if (ibitsize >= BITS_PER_WORD
3708 /* For hard regs we have exact predicates. Assume we can split
3709 the original object if it spans an even number of hard regs.
3710 This special case is important for SCmode on 64-bit platforms
3711 where the natural size of floating-point regs is 32-bit. */
3712 || (REG_P (cplx)
3713 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3714 && REG_NREGS (cplx) % 2 == 0))
3716 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3717 imag_p ? GET_MODE_SIZE (imode) : 0);
3718 if (part)
3720 emit_move_insn (part, val);
3721 return;
3723 else
3724 /* simplify_gen_subreg may fail for sub-word MEMs. */
3725 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3728 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3729 false, undefined_p);
3732 /* Extract one of the components of the complex value CPLX. Extract the
3733 real part if IMAG_P is false, and the imaginary part if it's true. */
3736 read_complex_part (rtx cplx, bool imag_p)
3738 machine_mode cmode;
3739 scalar_mode imode;
3740 unsigned ibitsize;
3742 if (GET_CODE (cplx) == CONCAT)
3743 return XEXP (cplx, imag_p);
3745 cmode = GET_MODE (cplx);
3746 imode = GET_MODE_INNER (cmode);
3747 ibitsize = GET_MODE_BITSIZE (imode);
3749 /* Special case reads from complex constants that got spilled to memory. */
3750 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3752 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3753 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3755 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3756 if (CONSTANT_CLASS_P (part))
3757 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3761 /* For MEMs simplify_gen_subreg may generate an invalid new address
3762 because, e.g., the original address is considered mode-dependent
3763 by the target, which restricts simplify_subreg from invoking
3764 adjust_address_nv. Instead of preparing fallback support for an
3765 invalid address, we call adjust_address_nv directly. */
3766 if (MEM_P (cplx))
3767 return adjust_address_nv (cplx, imode,
3768 imag_p ? GET_MODE_SIZE (imode) : 0);
3770 /* If the sub-object is at least word sized, then we know that subregging
3771 will work. This special case is important, since extract_bit_field
3772 wants to operate on integer modes, and there's rarely an OImode to
3773 correspond to TCmode. */
3774 if (ibitsize >= BITS_PER_WORD
3775 /* For hard regs we have exact predicates. Assume we can split
3776 the original object if it spans an even number of hard regs.
3777 This special case is important for SCmode on 64-bit platforms
3778 where the natural size of floating-point regs is 32-bit. */
3779 || (REG_P (cplx)
3780 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3781 && REG_NREGS (cplx) % 2 == 0))
3783 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3784 imag_p ? GET_MODE_SIZE (imode) : 0);
3785 if (ret)
3786 return ret;
3787 else
3788 /* simplify_gen_subreg may fail for sub-word MEMs. */
3789 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3792 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3793 true, NULL_RTX, imode, imode, false, NULL);
3796 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3797 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3798 represented in NEW_MODE. If FORCE is true, this will never happen, as
3799 we'll force-create a SUBREG if needed. */
3801 static rtx
3802 emit_move_change_mode (machine_mode new_mode,
3803 machine_mode old_mode, rtx x, bool force)
3805 rtx ret;
3807 if (push_operand (x, GET_MODE (x)))
3809 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3810 MEM_COPY_ATTRIBUTES (ret, x);
3812 else if (MEM_P (x))
3814 /* We don't have to worry about changing the address since the
3815 size in bytes is supposed to be the same. */
3816 if (reload_in_progress)
3818 /* Copy the MEM to change the mode and move any
3819 substitutions from the old MEM to the new one. */
3820 ret = adjust_address_nv (x, new_mode, 0);
3821 copy_replacements (x, ret);
3823 else
3824 ret = adjust_address (x, new_mode, 0);
3826 else
3828 /* Note that we do want simplify_subreg's behavior of validating
3829 that the new mode is ok for a hard register. If we were to use
3830 simplify_gen_subreg, we would create the subreg, but would
3831 probably run into the target not being able to implement it. */
3832 /* Except, of course, when FORCE is true, when this is exactly what
3833 we want. Which is needed for CCmodes on some targets. */
3834 if (force)
3835 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3836 else
3837 ret = simplify_subreg (new_mode, x, old_mode, 0);
3840 return ret;
3843 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3844 an integer mode of the same size as MODE. Returns the instruction
3845 emitted, or NULL if such a move could not be generated. */
3847 static rtx_insn *
3848 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3850 scalar_int_mode imode;
3851 enum insn_code code;
3853 /* There must exist a mode of the exact size we require. */
3854 if (!int_mode_for_mode (mode).exists (&imode))
3855 return NULL;
3857 /* The target must support moves in this mode. */
3858 code = optab_handler (mov_optab, imode);
3859 if (code == CODE_FOR_nothing)
3860 return NULL;
3862 x = emit_move_change_mode (imode, mode, x, force);
3863 if (x == NULL_RTX)
3864 return NULL;
3865 y = emit_move_change_mode (imode, mode, y, force);
3866 if (y == NULL_RTX)
3867 return NULL;
3868 return emit_insn (GEN_FCN (code) (x, y));
3871 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3872 Return an equivalent MEM that does not use an auto-increment. */
3875 emit_move_resolve_push (machine_mode mode, rtx x)
3877 enum rtx_code code = GET_CODE (XEXP (x, 0));
3878 rtx temp;
3880 poly_int64 adjust = GET_MODE_SIZE (mode);
3881 #ifdef PUSH_ROUNDING
3882 adjust = PUSH_ROUNDING (adjust);
3883 #endif
3884 if (code == PRE_DEC || code == POST_DEC)
3885 adjust = -adjust;
3886 else if (code == PRE_MODIFY || code == POST_MODIFY)
3888 rtx expr = XEXP (XEXP (x, 0), 1);
3890 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3891 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3892 if (GET_CODE (expr) == MINUS)
3893 val = -val;
3894 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3895 adjust = val;
3898 /* Do not use anti_adjust_stack, since we don't want to update
3899 stack_pointer_delta. */
3900 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3901 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3902 0, OPTAB_LIB_WIDEN);
3903 if (temp != stack_pointer_rtx)
3904 emit_move_insn (stack_pointer_rtx, temp);
3906 switch (code)
3908 case PRE_INC:
3909 case PRE_DEC:
3910 case PRE_MODIFY:
3911 temp = stack_pointer_rtx;
3912 break;
3913 case POST_INC:
3914 case POST_DEC:
3915 case POST_MODIFY:
3916 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3917 break;
3918 default:
3919 gcc_unreachable ();
3922 return replace_equiv_address (x, temp);
3925 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3926 X is known to satisfy push_operand, and MODE is known to be complex.
3927 Returns the last instruction emitted. */
3929 rtx_insn *
3930 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3932 scalar_mode submode = GET_MODE_INNER (mode);
3933 bool imag_first;
3935 #ifdef PUSH_ROUNDING
3936 poly_int64 submodesize = GET_MODE_SIZE (submode);
3938 /* In case we output to the stack, but the size is smaller than the
3939 machine can push exactly, we need to use move instructions. */
3940 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3942 x = emit_move_resolve_push (mode, x);
3943 return emit_move_insn (x, y);
3945 #endif
3947 /* Note that the real part always precedes the imag part in memory
3948 regardless of machine's endianness. */
3949 switch (GET_CODE (XEXP (x, 0)))
3951 case PRE_DEC:
3952 case POST_DEC:
3953 imag_first = true;
3954 break;
3955 case PRE_INC:
3956 case POST_INC:
3957 imag_first = false;
3958 break;
3959 default:
3960 gcc_unreachable ();
3963 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3964 read_complex_part (y, imag_first));
3965 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3966 read_complex_part (y, !imag_first));
3969 /* A subroutine of emit_move_complex. Perform the move from Y to X
3970 via two moves of the parts. Returns the last instruction emitted. */
3972 rtx_insn *
3973 emit_move_complex_parts (rtx x, rtx y)
3975 /* Show the output dies here. This is necessary for SUBREGs
3976 of pseudos since we cannot track their lifetimes correctly;
3977 hard regs shouldn't appear here except as return values. */
3978 if (!reload_completed && !reload_in_progress
3979 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3980 emit_clobber (x);
3982 write_complex_part (x, read_complex_part (y, false), false, true);
3983 write_complex_part (x, read_complex_part (y, true), true, false);
3985 return get_last_insn ();
3988 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3989 MODE is known to be complex. Returns the last instruction emitted. */
3991 static rtx_insn *
3992 emit_move_complex (machine_mode mode, rtx x, rtx y)
3994 bool try_int;
3996 /* Need to take special care for pushes, to maintain proper ordering
3997 of the data, and possibly extra padding. */
3998 if (push_operand (x, mode))
3999 return emit_move_complex_push (mode, x, y);
4001 /* See if we can coerce the target into moving both values at once, except
4002 for floating point where we favor moving as parts if this is easy. */
4003 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4004 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
4005 && !(REG_P (x)
4006 && HARD_REGISTER_P (x)
4007 && REG_NREGS (x) == 1)
4008 && !(REG_P (y)
4009 && HARD_REGISTER_P (y)
4010 && REG_NREGS (y) == 1))
4011 try_int = false;
4012 /* Not possible if the values are inherently not adjacent. */
4013 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
4014 try_int = false;
4015 /* Is possible if both are registers (or subregs of registers). */
4016 else if (register_operand (x, mode) && register_operand (y, mode))
4017 try_int = true;
4018 /* If one of the operands is a memory, and alignment constraints
4019 are friendly enough, we may be able to do combined memory operations.
4020 We do not attempt this if Y is a constant because that combination is
4021 usually better with the by-parts thing below. */
4022 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4023 && (!STRICT_ALIGNMENT
4024 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4025 try_int = true;
4026 else
4027 try_int = false;
4029 if (try_int)
4031 rtx_insn *ret;
4033 /* For memory to memory moves, optimal behavior can be had with the
4034 existing block move logic. But use normal expansion if optimizing
4035 for size. */
4036 if (MEM_P (x) && MEM_P (y))
4038 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4039 (optimize_insn_for_speed_p()
4040 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4041 return get_last_insn ();
4044 ret = emit_move_via_integer (mode, x, y, true);
4045 if (ret)
4046 return ret;
4049 return emit_move_complex_parts (x, y);
4052 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4053 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4055 static rtx_insn *
4056 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4058 rtx_insn *ret;
4060 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4061 if (mode != CCmode)
4063 enum insn_code code = optab_handler (mov_optab, CCmode);
4064 if (code != CODE_FOR_nothing)
4066 x = emit_move_change_mode (CCmode, mode, x, true);
4067 y = emit_move_change_mode (CCmode, mode, y, true);
4068 return emit_insn (GEN_FCN (code) (x, y));
4072 /* Otherwise, find the MODE_INT mode of the same width. */
4073 ret = emit_move_via_integer (mode, x, y, false);
4074 gcc_assert (ret != NULL);
4075 return ret;
4078 /* Return true if word I of OP lies entirely in the
4079 undefined bits of a paradoxical subreg. */
4081 static bool
4082 undefined_operand_subword_p (const_rtx op, int i)
4084 if (GET_CODE (op) != SUBREG)
4085 return false;
4086 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4087 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4088 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4089 || known_le (offset, -UNITS_PER_WORD));
4092 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4093 MODE is any multi-word or full-word mode that lacks a move_insn
4094 pattern. Note that you will get better code if you define such
4095 patterns, even if they must turn into multiple assembler instructions. */
4097 static rtx_insn *
4098 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4100 rtx_insn *last_insn = 0;
4101 rtx_insn *seq;
4102 rtx inner;
4103 bool need_clobber;
4104 int i, mode_size;
4106 /* This function can only handle cases where the number of words is
4107 known at compile time. */
4108 mode_size = GET_MODE_SIZE (mode).to_constant ();
4109 gcc_assert (mode_size >= UNITS_PER_WORD);
4111 /* If X is a push on the stack, do the push now and replace
4112 X with a reference to the stack pointer. */
4113 if (push_operand (x, mode))
4114 x = emit_move_resolve_push (mode, x);
4116 /* If we are in reload, see if either operand is a MEM whose address
4117 is scheduled for replacement. */
4118 if (reload_in_progress && MEM_P (x)
4119 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4120 x = replace_equiv_address_nv (x, inner);
4121 if (reload_in_progress && MEM_P (y)
4122 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4123 y = replace_equiv_address_nv (y, inner);
4125 start_sequence ();
4127 need_clobber = false;
4128 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4130 /* Do not generate code for a move if it would go entirely
4131 to the non-existing bits of a paradoxical subreg. */
4132 if (undefined_operand_subword_p (x, i))
4133 continue;
4135 rtx xpart = operand_subword (x, i, 1, mode);
4136 rtx ypart;
4138 /* Do not generate code for a move if it would come entirely
4139 from the undefined bits of a paradoxical subreg. */
4140 if (undefined_operand_subword_p (y, i))
4141 continue;
4143 ypart = operand_subword (y, i, 1, mode);
4145 /* If we can't get a part of Y, put Y into memory if it is a
4146 constant. Otherwise, force it into a register. Then we must
4147 be able to get a part of Y. */
4148 if (ypart == 0 && CONSTANT_P (y))
4150 y = use_anchored_address (force_const_mem (mode, y));
4151 ypart = operand_subword (y, i, 1, mode);
4153 else if (ypart == 0)
4154 ypart = operand_subword_force (y, i, mode);
4156 gcc_assert (xpart && ypart);
4158 need_clobber |= (GET_CODE (xpart) == SUBREG);
4160 last_insn = emit_move_insn (xpart, ypart);
4163 seq = get_insns ();
4164 end_sequence ();
4166 /* Show the output dies here. This is necessary for SUBREGs
4167 of pseudos since we cannot track their lifetimes correctly;
4168 hard regs shouldn't appear here except as return values.
4169 We never want to emit such a clobber after reload. */
4170 if (x != y
4171 && ! (reload_in_progress || reload_completed)
4172 && need_clobber != 0)
4173 emit_clobber (x);
4175 emit_insn (seq);
4177 return last_insn;
4180 /* Low level part of emit_move_insn.
4181 Called just like emit_move_insn, but assumes X and Y
4182 are basically valid. */
4184 rtx_insn *
4185 emit_move_insn_1 (rtx x, rtx y)
4187 machine_mode mode = GET_MODE (x);
4188 enum insn_code code;
4190 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4192 code = optab_handler (mov_optab, mode);
4193 if (code != CODE_FOR_nothing)
4194 return emit_insn (GEN_FCN (code) (x, y));
4196 /* Expand complex moves by moving real part and imag part. */
4197 if (COMPLEX_MODE_P (mode))
4198 return emit_move_complex (mode, x, y);
4200 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4201 || ALL_FIXED_POINT_MODE_P (mode))
4203 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4205 /* If we can't find an integer mode, use multi words. */
4206 if (result)
4207 return result;
4208 else
4209 return emit_move_multi_word (mode, x, y);
4212 if (GET_MODE_CLASS (mode) == MODE_CC)
4213 return emit_move_ccmode (mode, x, y);
4215 /* Try using a move pattern for the corresponding integer mode. This is
4216 only safe when simplify_subreg can convert MODE constants into integer
4217 constants. At present, it can only do this reliably if the value
4218 fits within a HOST_WIDE_INT. */
4219 if (!CONSTANT_P (y)
4220 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4222 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4224 if (ret)
4226 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4227 return ret;
4231 return emit_move_multi_word (mode, x, y);
4234 /* Generate code to copy Y into X.
4235 Both Y and X must have the same mode, except that
4236 Y can be a constant with VOIDmode.
4237 This mode cannot be BLKmode; use emit_block_move for that.
4239 Return the last instruction emitted. */
4241 rtx_insn *
4242 emit_move_insn (rtx x, rtx y)
4244 machine_mode mode = GET_MODE (x);
4245 rtx y_cst = NULL_RTX;
4246 rtx_insn *last_insn;
4247 rtx set;
4249 gcc_assert (mode != BLKmode
4250 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4252 /* If we have a copy that looks like one of the following patterns:
4253 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4254 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4255 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4256 (set (subreg:M1 (reg:M2 ...)) (constant C))
4257 where mode M1 is equal in size to M2, try to detect whether the
4258 mode change involves an implicit round trip through memory.
4259 If so, see if we can avoid that by removing the subregs and
4260 doing the move in mode M2 instead. */
4262 rtx x_inner = NULL_RTX;
4263 rtx y_inner = NULL_RTX;
4265 auto candidate_subreg_p = [&](rtx subreg) {
4266 return (REG_P (SUBREG_REG (subreg))
4267 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4268 GET_MODE_SIZE (GET_MODE (subreg)))
4269 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4270 != CODE_FOR_nothing);
4273 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4274 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4275 && !push_operand (mem, GET_MODE (mem))
4276 /* Not a candiate if innermode requires too much alignment. */
4277 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4278 || targetm.slow_unaligned_access (GET_MODE (mem),
4279 MEM_ALIGN (mem))
4280 || !targetm.slow_unaligned_access (innermode,
4281 MEM_ALIGN (mem))));
4284 if (SUBREG_P (x) && candidate_subreg_p (x))
4285 x_inner = SUBREG_REG (x);
4287 if (SUBREG_P (y) && candidate_subreg_p (y))
4288 y_inner = SUBREG_REG (y);
4290 if (x_inner != NULL_RTX
4291 && y_inner != NULL_RTX
4292 && GET_MODE (x_inner) == GET_MODE (y_inner)
4293 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4295 x = x_inner;
4296 y = y_inner;
4297 mode = GET_MODE (x_inner);
4299 else if (x_inner != NULL_RTX
4300 && MEM_P (y)
4301 && candidate_mem_p (GET_MODE (x_inner), y))
4303 x = x_inner;
4304 y = adjust_address (y, GET_MODE (x_inner), 0);
4305 mode = GET_MODE (x_inner);
4307 else if (y_inner != NULL_RTX
4308 && MEM_P (x)
4309 && candidate_mem_p (GET_MODE (y_inner), x))
4311 x = adjust_address (x, GET_MODE (y_inner), 0);
4312 y = y_inner;
4313 mode = GET_MODE (y_inner);
4315 else if (x_inner != NULL_RTX
4316 && CONSTANT_P (y)
4317 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4318 mode, ALL_REGS)
4319 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4321 x = x_inner;
4322 y = y_inner;
4323 mode = GET_MODE (x_inner);
4326 if (CONSTANT_P (y))
4328 if (optimize
4329 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4330 && (last_insn = compress_float_constant (x, y)))
4331 return last_insn;
4333 y_cst = y;
4335 if (!targetm.legitimate_constant_p (mode, y))
4337 y = force_const_mem (mode, y);
4339 /* If the target's cannot_force_const_mem prevented the spill,
4340 assume that the target's move expanders will also take care
4341 of the non-legitimate constant. */
4342 if (!y)
4343 y = y_cst;
4344 else
4345 y = use_anchored_address (y);
4349 /* If X or Y are memory references, verify that their addresses are valid
4350 for the machine. */
4351 if (MEM_P (x)
4352 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4353 MEM_ADDR_SPACE (x))
4354 && ! push_operand (x, GET_MODE (x))))
4355 x = validize_mem (x);
4357 if (MEM_P (y)
4358 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4359 MEM_ADDR_SPACE (y)))
4360 y = validize_mem (y);
4362 gcc_assert (mode != BLKmode);
4364 last_insn = emit_move_insn_1 (x, y);
4366 if (y_cst && REG_P (x)
4367 && (set = single_set (last_insn)) != NULL_RTX
4368 && SET_DEST (set) == x
4369 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4370 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4372 return last_insn;
4375 /* Generate the body of an instruction to copy Y into X.
4376 It may be a list of insns, if one insn isn't enough. */
4378 rtx_insn *
4379 gen_move_insn (rtx x, rtx y)
4381 rtx_insn *seq;
4383 start_sequence ();
4384 emit_move_insn_1 (x, y);
4385 seq = get_insns ();
4386 end_sequence ();
4387 return seq;
4390 /* If Y is representable exactly in a narrower mode, and the target can
4391 perform the extension directly from constant or memory, then emit the
4392 move as an extension. */
4394 static rtx_insn *
4395 compress_float_constant (rtx x, rtx y)
4397 machine_mode dstmode = GET_MODE (x);
4398 machine_mode orig_srcmode = GET_MODE (y);
4399 machine_mode srcmode;
4400 const REAL_VALUE_TYPE *r;
4401 int oldcost, newcost;
4402 bool speed = optimize_insn_for_speed_p ();
4404 r = CONST_DOUBLE_REAL_VALUE (y);
4406 if (targetm.legitimate_constant_p (dstmode, y))
4407 oldcost = set_src_cost (y, orig_srcmode, speed);
4408 else
4409 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4411 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4413 enum insn_code ic;
4414 rtx trunc_y;
4415 rtx_insn *last_insn;
4417 /* Skip if the target can't extend this way. */
4418 ic = can_extend_p (dstmode, srcmode, 0);
4419 if (ic == CODE_FOR_nothing)
4420 continue;
4422 /* Skip if the narrowed value isn't exact. */
4423 if (! exact_real_truncate (srcmode, r))
4424 continue;
4426 trunc_y = const_double_from_real_value (*r, srcmode);
4428 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4430 /* Skip if the target needs extra instructions to perform
4431 the extension. */
4432 if (!insn_operand_matches (ic, 1, trunc_y))
4433 continue;
4434 /* This is valid, but may not be cheaper than the original. */
4435 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4436 dstmode, speed);
4437 if (oldcost < newcost)
4438 continue;
4440 else if (float_extend_from_mem[dstmode][srcmode])
4442 trunc_y = force_const_mem (srcmode, trunc_y);
4443 /* This is valid, but may not be cheaper than the original. */
4444 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4445 dstmode, speed);
4446 if (oldcost < newcost)
4447 continue;
4448 trunc_y = validize_mem (trunc_y);
4450 else
4451 continue;
4453 /* For CSE's benefit, force the compressed constant pool entry
4454 into a new pseudo. This constant may be used in different modes,
4455 and if not, combine will put things back together for us. */
4456 trunc_y = force_reg (srcmode, trunc_y);
4458 /* If x is a hard register, perform the extension into a pseudo,
4459 so that e.g. stack realignment code is aware of it. */
4460 rtx target = x;
4461 if (REG_P (x) && HARD_REGISTER_P (x))
4462 target = gen_reg_rtx (dstmode);
4464 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4465 last_insn = get_last_insn ();
4467 if (REG_P (target))
4468 set_unique_reg_note (last_insn, REG_EQUAL, y);
4470 if (target != x)
4471 return emit_move_insn (x, target);
4472 return last_insn;
4475 return NULL;
4478 /* Pushing data onto the stack. */
4480 /* Push a block of length SIZE (perhaps variable)
4481 and return an rtx to address the beginning of the block.
4482 The value may be virtual_outgoing_args_rtx.
4484 EXTRA is the number of bytes of padding to push in addition to SIZE.
4485 BELOW nonzero means this padding comes at low addresses;
4486 otherwise, the padding comes at high addresses. */
4489 push_block (rtx size, poly_int64 extra, int below)
4491 rtx temp;
4493 size = convert_modes (Pmode, ptr_mode, size, 1);
4494 if (CONSTANT_P (size))
4495 anti_adjust_stack (plus_constant (Pmode, size, extra));
4496 else if (REG_P (size) && known_eq (extra, 0))
4497 anti_adjust_stack (size);
4498 else
4500 temp = copy_to_mode_reg (Pmode, size);
4501 if (maybe_ne (extra, 0))
4502 temp = expand_binop (Pmode, add_optab, temp,
4503 gen_int_mode (extra, Pmode),
4504 temp, 0, OPTAB_LIB_WIDEN);
4505 anti_adjust_stack (temp);
4508 if (STACK_GROWS_DOWNWARD)
4510 temp = virtual_outgoing_args_rtx;
4511 if (maybe_ne (extra, 0) && below)
4512 temp = plus_constant (Pmode, temp, extra);
4514 else
4516 poly_int64 csize;
4517 if (poly_int_rtx_p (size, &csize))
4518 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4519 -csize - (below ? 0 : extra));
4520 else if (maybe_ne (extra, 0) && !below)
4521 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4522 negate_rtx (Pmode, plus_constant (Pmode, size,
4523 extra)));
4524 else
4525 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4526 negate_rtx (Pmode, size));
4529 return memory_address (NARROWEST_INT_MODE, temp);
4532 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4534 static rtx
4535 mem_autoinc_base (rtx mem)
4537 if (MEM_P (mem))
4539 rtx addr = XEXP (mem, 0);
4540 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4541 return XEXP (addr, 0);
4543 return NULL;
4546 /* A utility routine used here, in reload, and in try_split. The insns
4547 after PREV up to and including LAST are known to adjust the stack,
4548 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4549 placing notes as appropriate. PREV may be NULL, indicating the
4550 entire insn sequence prior to LAST should be scanned.
4552 The set of allowed stack pointer modifications is small:
4553 (1) One or more auto-inc style memory references (aka pushes),
4554 (2) One or more addition/subtraction with the SP as destination,
4555 (3) A single move insn with the SP as destination,
4556 (4) A call_pop insn,
4557 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4559 Insns in the sequence that do not modify the SP are ignored,
4560 except for noreturn calls.
4562 The return value is the amount of adjustment that can be trivially
4563 verified, via immediate operand or auto-inc. If the adjustment
4564 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4566 poly_int64
4567 find_args_size_adjust (rtx_insn *insn)
4569 rtx dest, set, pat;
4570 int i;
4572 pat = PATTERN (insn);
4573 set = NULL;
4575 /* Look for a call_pop pattern. */
4576 if (CALL_P (insn))
4578 /* We have to allow non-call_pop patterns for the case
4579 of emit_single_push_insn of a TLS address. */
4580 if (GET_CODE (pat) != PARALLEL)
4581 return 0;
4583 /* All call_pop have a stack pointer adjust in the parallel.
4584 The call itself is always first, and the stack adjust is
4585 usually last, so search from the end. */
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'd better have found the stack pointer adjust. */
4596 if (i == 0)
4597 return 0;
4598 /* Fall through to process the extracted SET and DEST
4599 as if it was a standalone insn. */
4601 else if (GET_CODE (pat) == SET)
4602 set = pat;
4603 else if ((set = single_set (insn)) != NULL)
4605 else if (GET_CODE (pat) == PARALLEL)
4607 /* ??? Some older ports use a parallel with a stack adjust
4608 and a store for a PUSH_ROUNDING pattern, rather than a
4609 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4610 /* ??? See h8300 and m68k, pushqi1. */
4611 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4613 set = XVECEXP (pat, 0, i);
4614 if (GET_CODE (set) != SET)
4615 continue;
4616 dest = SET_DEST (set);
4617 if (dest == stack_pointer_rtx)
4618 break;
4620 /* We do not expect an auto-inc of the sp in the parallel. */
4621 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4622 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4623 != stack_pointer_rtx);
4625 if (i < 0)
4626 return 0;
4628 else
4629 return 0;
4631 dest = SET_DEST (set);
4633 /* Look for direct modifications of the stack pointer. */
4634 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4636 /* Look for a trivial adjustment, otherwise assume nothing. */
4637 /* Note that the SPU restore_stack_block pattern refers to
4638 the stack pointer in V4SImode. Consider that non-trivial. */
4639 poly_int64 offset;
4640 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4641 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4642 return offset;
4643 /* ??? Reload can generate no-op moves, which will be cleaned
4644 up later. Recognize it and continue searching. */
4645 else if (rtx_equal_p (dest, SET_SRC (set)))
4646 return 0;
4647 else
4648 return HOST_WIDE_INT_MIN;
4650 else
4652 rtx mem, addr;
4654 /* Otherwise only think about autoinc patterns. */
4655 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4657 mem = dest;
4658 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4659 != stack_pointer_rtx);
4661 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4662 mem = SET_SRC (set);
4663 else
4664 return 0;
4666 addr = XEXP (mem, 0);
4667 switch (GET_CODE (addr))
4669 case PRE_INC:
4670 case POST_INC:
4671 return GET_MODE_SIZE (GET_MODE (mem));
4672 case PRE_DEC:
4673 case POST_DEC:
4674 return -GET_MODE_SIZE (GET_MODE (mem));
4675 case PRE_MODIFY:
4676 case POST_MODIFY:
4677 addr = XEXP (addr, 1);
4678 gcc_assert (GET_CODE (addr) == PLUS);
4679 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4680 return rtx_to_poly_int64 (XEXP (addr, 1));
4681 default:
4682 gcc_unreachable ();
4687 poly_int64
4688 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4689 poly_int64 end_args_size)
4691 poly_int64 args_size = end_args_size;
4692 bool saw_unknown = false;
4693 rtx_insn *insn;
4695 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4697 if (!NONDEBUG_INSN_P (insn))
4698 continue;
4700 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4701 a call argument containing a TLS address that itself requires
4702 a call to __tls_get_addr. The handling of stack_pointer_delta
4703 in emit_single_push_insn is supposed to ensure that any such
4704 notes are already correct. */
4705 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4706 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4708 poly_int64 this_delta = find_args_size_adjust (insn);
4709 if (known_eq (this_delta, 0))
4711 if (!CALL_P (insn)
4712 || ACCUMULATE_OUTGOING_ARGS
4713 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4714 continue;
4717 gcc_assert (!saw_unknown);
4718 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4719 saw_unknown = true;
4721 if (!note)
4722 add_args_size_note (insn, args_size);
4723 if (STACK_GROWS_DOWNWARD)
4724 this_delta = -poly_uint64 (this_delta);
4726 if (saw_unknown)
4727 args_size = HOST_WIDE_INT_MIN;
4728 else
4729 args_size -= this_delta;
4732 return args_size;
4735 #ifdef PUSH_ROUNDING
4736 /* Emit single push insn. */
4738 static void
4739 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4741 rtx dest_addr;
4742 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4743 rtx dest;
4744 enum insn_code icode;
4746 /* If there is push pattern, use it. Otherwise try old way of throwing
4747 MEM representing push operation to move expander. */
4748 icode = optab_handler (push_optab, mode);
4749 if (icode != CODE_FOR_nothing)
4751 class expand_operand ops[1];
4753 create_input_operand (&ops[0], x, mode);
4754 if (maybe_expand_insn (icode, 1, ops))
4755 return;
4757 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4758 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4759 /* If we are to pad downward, adjust the stack pointer first and
4760 then store X into the stack location using an offset. This is
4761 because emit_move_insn does not know how to pad; it does not have
4762 access to type. */
4763 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4765 emit_move_insn (stack_pointer_rtx,
4766 expand_binop (Pmode,
4767 STACK_GROWS_DOWNWARD ? sub_optab
4768 : add_optab,
4769 stack_pointer_rtx,
4770 gen_int_mode (rounded_size, Pmode),
4771 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4773 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4774 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4775 /* We have already decremented the stack pointer, so get the
4776 previous value. */
4777 offset += rounded_size;
4779 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4780 /* We have already incremented the stack pointer, so get the
4781 previous value. */
4782 offset -= rounded_size;
4784 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4786 else
4788 if (STACK_GROWS_DOWNWARD)
4789 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4790 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4791 else
4792 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4793 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4795 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4798 dest = gen_rtx_MEM (mode, dest_addr);
4800 if (type != 0)
4802 set_mem_attributes (dest, type, 1);
4804 if (cfun->tail_call_marked)
4805 /* Function incoming arguments may overlap with sibling call
4806 outgoing arguments and we cannot allow reordering of reads
4807 from function arguments with stores to outgoing arguments
4808 of sibling calls. */
4809 set_mem_alias_set (dest, 0);
4811 emit_move_insn (dest, x);
4814 /* Emit and annotate a single push insn. */
4816 static void
4817 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4819 poly_int64 delta, old_delta = stack_pointer_delta;
4820 rtx_insn *prev = get_last_insn ();
4821 rtx_insn *last;
4823 emit_single_push_insn_1 (mode, x, type);
4825 /* Adjust stack_pointer_delta to describe the situation after the push
4826 we just performed. Note that we must do this after the push rather
4827 than before the push in case calculating X needs pushes and pops of
4828 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4829 for such pushes and pops must not include the effect of the future
4830 push of X. */
4831 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4833 last = get_last_insn ();
4835 /* Notice the common case where we emitted exactly one insn. */
4836 if (PREV_INSN (last) == prev)
4838 add_args_size_note (last, stack_pointer_delta);
4839 return;
4842 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4843 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4844 || known_eq (delta, old_delta));
4846 #endif
4848 /* If reading SIZE bytes from X will end up reading from
4849 Y return the number of bytes that overlap. Return -1
4850 if there is no overlap or -2 if we can't determine
4851 (for example when X and Y have different base registers). */
4853 static int
4854 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4856 rtx tmp = plus_constant (Pmode, x, size);
4857 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4859 if (!CONST_INT_P (sub))
4860 return -2;
4862 HOST_WIDE_INT val = INTVAL (sub);
4864 return IN_RANGE (val, 1, size) ? val : -1;
4867 /* Generate code to push X onto the stack, assuming it has mode MODE and
4868 type TYPE.
4869 MODE is redundant except when X is a CONST_INT (since they don't
4870 carry mode info).
4871 SIZE is an rtx for the size of data to be copied (in bytes),
4872 needed only if X is BLKmode.
4873 Return true if successful. May return false if asked to push a
4874 partial argument during a sibcall optimization (as specified by
4875 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4876 to not overlap.
4878 ALIGN (in bits) is maximum alignment we can assume.
4880 If PARTIAL and REG are both nonzero, then copy that many of the first
4881 bytes of X into registers starting with REG, and push the rest of X.
4882 The amount of space pushed is decreased by PARTIAL bytes.
4883 REG must be a hard register in this case.
4884 If REG is zero but PARTIAL is not, take any all others actions for an
4885 argument partially in registers, but do not actually load any
4886 registers.
4888 EXTRA is the amount in bytes of extra space to leave next to this arg.
4889 This is ignored if an argument block has already been allocated.
4891 On a machine that lacks real push insns, ARGS_ADDR is the address of
4892 the bottom of the argument block for this call. We use indexing off there
4893 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4894 argument block has not been preallocated.
4896 ARGS_SO_FAR is the size of args previously pushed for this call.
4898 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4899 for arguments passed in registers. If nonzero, it will be the number
4900 of bytes required. */
4902 bool
4903 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4904 unsigned int align, int partial, rtx reg, poly_int64 extra,
4905 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4906 rtx alignment_pad, bool sibcall_p)
4908 rtx xinner;
4909 pad_direction stack_direction
4910 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4912 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4913 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4914 Default is below for small data on big-endian machines; else above. */
4915 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4917 /* Invert direction if stack is post-decrement.
4918 FIXME: why? */
4919 if (STACK_PUSH_CODE == POST_DEC)
4920 if (where_pad != PAD_NONE)
4921 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4923 xinner = x;
4925 int nregs = partial / UNITS_PER_WORD;
4926 rtx *tmp_regs = NULL;
4927 int overlapping = 0;
4929 if (mode == BLKmode
4930 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4932 /* Copy a block into the stack, entirely or partially. */
4934 rtx temp;
4935 int used;
4936 int offset;
4937 int skip;
4939 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4940 used = partial - offset;
4942 if (mode != BLKmode)
4944 /* A value is to be stored in an insufficiently aligned
4945 stack slot; copy via a suitably aligned slot if
4946 necessary. */
4947 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4948 if (!MEM_P (xinner))
4950 temp = assign_temp (type, 1, 1);
4951 emit_move_insn (temp, xinner);
4952 xinner = temp;
4956 gcc_assert (size);
4958 /* USED is now the # of bytes we need not copy to the stack
4959 because registers will take care of them. */
4961 if (partial != 0)
4962 xinner = adjust_address (xinner, BLKmode, used);
4964 /* If the partial register-part of the arg counts in its stack size,
4965 skip the part of stack space corresponding to the registers.
4966 Otherwise, start copying to the beginning of the stack space,
4967 by setting SKIP to 0. */
4968 skip = (reg_parm_stack_space == 0) ? 0 : used;
4970 #ifdef PUSH_ROUNDING
4971 /* NB: Let the backend known the number of bytes to push and
4972 decide if push insns should be generated. */
4973 unsigned int push_size;
4974 if (CONST_INT_P (size))
4975 push_size = INTVAL (size);
4976 else
4977 push_size = 0;
4979 /* Do it with several push insns if that doesn't take lots of insns
4980 and if there is no difficulty with push insns that skip bytes
4981 on the stack for alignment purposes. */
4982 if (args_addr == 0
4983 && targetm.calls.push_argument (push_size)
4984 && CONST_INT_P (size)
4985 && skip == 0
4986 && MEM_ALIGN (xinner) >= align
4987 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4988 /* Here we avoid the case of a structure whose weak alignment
4989 forces many pushes of a small amount of data,
4990 and such small pushes do rounding that causes trouble. */
4991 && ((!targetm.slow_unaligned_access (word_mode, align))
4992 || align >= BIGGEST_ALIGNMENT
4993 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4994 align / BITS_PER_UNIT))
4995 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4997 /* Push padding now if padding above and stack grows down,
4998 or if padding below and stack grows up.
4999 But if space already allocated, this has already been done. */
5000 if (maybe_ne (extra, 0)
5001 && args_addr == 0
5002 && where_pad != PAD_NONE
5003 && where_pad != stack_direction)
5004 anti_adjust_stack (gen_int_mode (extra, Pmode));
5006 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
5007 RETURN_BEGIN);
5009 else
5010 #endif /* PUSH_ROUNDING */
5012 rtx target;
5014 /* Otherwise make space on the stack and copy the data
5015 to the address of that space. */
5017 /* Deduct words put into registers from the size we must copy. */
5018 if (partial != 0)
5020 if (CONST_INT_P (size))
5021 size = GEN_INT (INTVAL (size) - used);
5022 else
5023 size = expand_binop (GET_MODE (size), sub_optab, size,
5024 gen_int_mode (used, GET_MODE (size)),
5025 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5028 /* Get the address of the stack space.
5029 In this case, we do not deal with EXTRA separately.
5030 A single stack adjust will do. */
5031 poly_int64 const_args_so_far;
5032 if (! args_addr)
5034 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5035 extra = 0;
5037 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5038 temp = memory_address (BLKmode,
5039 plus_constant (Pmode, args_addr,
5040 skip + const_args_so_far));
5041 else
5042 temp = memory_address (BLKmode,
5043 plus_constant (Pmode,
5044 gen_rtx_PLUS (Pmode,
5045 args_addr,
5046 args_so_far),
5047 skip));
5049 if (!ACCUMULATE_OUTGOING_ARGS)
5051 /* If the source is referenced relative to the stack pointer,
5052 copy it to another register to stabilize it. We do not need
5053 to do this if we know that we won't be changing sp. */
5055 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5056 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5057 temp = copy_to_reg (temp);
5060 target = gen_rtx_MEM (BLKmode, temp);
5062 /* We do *not* set_mem_attributes here, because incoming arguments
5063 may overlap with sibling call outgoing arguments and we cannot
5064 allow reordering of reads from function arguments with stores
5065 to outgoing arguments of sibling calls. We do, however, want
5066 to record the alignment of the stack slot. */
5067 /* ALIGN may well be better aligned than TYPE, e.g. due to
5068 PARM_BOUNDARY. Assume the caller isn't lying. */
5069 set_mem_align (target, align);
5071 /* If part should go in registers and pushing to that part would
5072 overwrite some of the values that need to go into regs, load the
5073 overlapping values into temporary pseudos to be moved into the hard
5074 regs at the end after the stack pushing has completed.
5075 We cannot load them directly into the hard regs here because
5076 they can be clobbered by the block move expansions.
5077 See PR 65358. */
5079 if (partial > 0 && reg != 0 && mode == BLKmode
5080 && GET_CODE (reg) != PARALLEL)
5082 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5083 if (overlapping > 0)
5085 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5086 overlapping /= UNITS_PER_WORD;
5088 tmp_regs = XALLOCAVEC (rtx, overlapping);
5090 for (int i = 0; i < overlapping; i++)
5091 tmp_regs[i] = gen_reg_rtx (word_mode);
5093 for (int i = 0; i < overlapping; i++)
5094 emit_move_insn (tmp_regs[i],
5095 operand_subword_force (target, i, mode));
5097 else if (overlapping == -1)
5098 overlapping = 0;
5099 /* Could not determine whether there is overlap.
5100 Fail the sibcall. */
5101 else
5103 overlapping = 0;
5104 if (sibcall_p)
5105 return false;
5109 /* If source is a constant VAR_DECL with a simple constructor,
5110 store the constructor to the stack instead of moving it. */
5111 const_tree decl;
5112 if (partial == 0
5113 && MEM_P (xinner)
5114 && SYMBOL_REF_P (XEXP (xinner, 0))
5115 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5116 && VAR_P (decl)
5117 && TREE_READONLY (decl)
5118 && !TREE_SIDE_EFFECTS (decl)
5119 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5120 store_constructor (DECL_INITIAL (decl), target, 0,
5121 int_expr_size (DECL_INITIAL (decl)), false);
5122 else
5123 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5126 else if (partial > 0)
5128 /* Scalar partly in registers. This case is only supported
5129 for fixed-wdth modes. */
5130 int num_words = GET_MODE_SIZE (mode).to_constant ();
5131 num_words /= UNITS_PER_WORD;
5132 int i;
5133 int not_stack;
5134 /* # bytes of start of argument
5135 that we must make space for but need not store. */
5136 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5137 int args_offset = INTVAL (args_so_far);
5138 int skip;
5140 /* Push padding now if padding above and stack grows down,
5141 or if padding below and stack grows up.
5142 But if space already allocated, this has already been done. */
5143 if (maybe_ne (extra, 0)
5144 && args_addr == 0
5145 && where_pad != PAD_NONE
5146 && where_pad != stack_direction)
5147 anti_adjust_stack (gen_int_mode (extra, Pmode));
5149 /* If we make space by pushing it, we might as well push
5150 the real data. Otherwise, we can leave OFFSET nonzero
5151 and leave the space uninitialized. */
5152 if (args_addr == 0)
5153 offset = 0;
5155 /* Now NOT_STACK gets the number of words that we don't need to
5156 allocate on the stack. Convert OFFSET to words too. */
5157 not_stack = (partial - offset) / UNITS_PER_WORD;
5158 offset /= UNITS_PER_WORD;
5160 /* If the partial register-part of the arg counts in its stack size,
5161 skip the part of stack space corresponding to the registers.
5162 Otherwise, start copying to the beginning of the stack space,
5163 by setting SKIP to 0. */
5164 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5166 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5167 x = validize_mem (force_const_mem (mode, x));
5169 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5170 SUBREGs of such registers are not allowed. */
5171 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5172 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5173 x = copy_to_reg (x);
5175 /* Loop over all the words allocated on the stack for this arg. */
5176 /* We can do it by words, because any scalar bigger than a word
5177 has a size a multiple of a word. */
5178 for (i = num_words - 1; i >= not_stack; i--)
5179 if (i >= not_stack + offset)
5180 if (!emit_push_insn (operand_subword_force (x, i, mode),
5181 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
5182 0, args_addr,
5183 GEN_INT (args_offset + ((i - not_stack + skip)
5184 * UNITS_PER_WORD)),
5185 reg_parm_stack_space, alignment_pad, sibcall_p))
5186 return false;
5188 else
5190 rtx addr;
5191 rtx dest;
5193 /* Push padding now if padding above and stack grows down,
5194 or if padding below and stack grows up.
5195 But if space already allocated, this has already been done. */
5196 if (maybe_ne (extra, 0)
5197 && args_addr == 0
5198 && where_pad != PAD_NONE
5199 && where_pad != stack_direction)
5200 anti_adjust_stack (gen_int_mode (extra, Pmode));
5202 #ifdef PUSH_ROUNDING
5203 if (args_addr == 0 && targetm.calls.push_argument (0))
5204 emit_single_push_insn (mode, x, type);
5205 else
5206 #endif
5208 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5209 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5211 /* We do *not* set_mem_attributes here, because incoming arguments
5212 may overlap with sibling call outgoing arguments and we cannot
5213 allow reordering of reads from function arguments with stores
5214 to outgoing arguments of sibling calls. We do, however, want
5215 to record the alignment of the stack slot. */
5216 /* ALIGN may well be better aligned than TYPE, e.g. due to
5217 PARM_BOUNDARY. Assume the caller isn't lying. */
5218 set_mem_align (dest, align);
5220 emit_move_insn (dest, x);
5224 /* Move the partial arguments into the registers and any overlapping
5225 values that we moved into the pseudos in tmp_regs. */
5226 if (partial > 0 && reg != 0)
5228 /* Handle calls that pass values in multiple non-contiguous locations.
5229 The Irix 6 ABI has examples of this. */
5230 if (GET_CODE (reg) == PARALLEL)
5231 emit_group_load (reg, x, type, -1);
5232 else
5234 gcc_assert (partial % UNITS_PER_WORD == 0);
5235 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5237 for (int i = 0; i < overlapping; i++)
5238 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5239 + nregs - overlapping + i),
5240 tmp_regs[i]);
5245 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5246 anti_adjust_stack (gen_int_mode (extra, Pmode));
5248 if (alignment_pad && args_addr == 0)
5249 anti_adjust_stack (alignment_pad);
5251 return true;
5254 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5255 operations. */
5257 static rtx
5258 get_subtarget (rtx x)
5260 return (optimize
5261 || x == 0
5262 /* Only registers can be subtargets. */
5263 || !REG_P (x)
5264 /* Don't use hard regs to avoid extending their life. */
5265 || REGNO (x) < FIRST_PSEUDO_REGISTER
5266 ? 0 : x);
5269 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5270 FIELD is a bitfield. Returns true if the optimization was successful,
5271 and there's nothing else to do. */
5273 static bool
5274 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5275 poly_uint64 pbitpos,
5276 poly_uint64 pbitregion_start,
5277 poly_uint64 pbitregion_end,
5278 machine_mode mode1, rtx str_rtx,
5279 tree to, tree src, bool reverse)
5281 /* str_mode is not guaranteed to be a scalar type. */
5282 machine_mode str_mode = GET_MODE (str_rtx);
5283 unsigned int str_bitsize;
5284 tree op0, op1;
5285 rtx value, result;
5286 optab binop;
5287 gimple *srcstmt;
5288 enum tree_code code;
5290 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5291 if (mode1 != VOIDmode
5292 || !pbitsize.is_constant (&bitsize)
5293 || !pbitpos.is_constant (&bitpos)
5294 || !pbitregion_start.is_constant (&bitregion_start)
5295 || !pbitregion_end.is_constant (&bitregion_end)
5296 || bitsize >= BITS_PER_WORD
5297 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5298 || str_bitsize > BITS_PER_WORD
5299 || TREE_SIDE_EFFECTS (to)
5300 || TREE_THIS_VOLATILE (to))
5301 return false;
5303 STRIP_NOPS (src);
5304 if (TREE_CODE (src) != SSA_NAME)
5305 return false;
5306 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5307 return false;
5309 srcstmt = get_gimple_for_ssa_name (src);
5310 if (!srcstmt
5311 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5312 return false;
5314 code = gimple_assign_rhs_code (srcstmt);
5316 op0 = gimple_assign_rhs1 (srcstmt);
5318 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5319 to find its initialization. Hopefully the initialization will
5320 be from a bitfield load. */
5321 if (TREE_CODE (op0) == SSA_NAME)
5323 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5325 /* We want to eventually have OP0 be the same as TO, which
5326 should be a bitfield. */
5327 if (!op0stmt
5328 || !is_gimple_assign (op0stmt)
5329 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5330 return false;
5331 op0 = gimple_assign_rhs1 (op0stmt);
5334 op1 = gimple_assign_rhs2 (srcstmt);
5336 if (!operand_equal_p (to, op0, 0))
5337 return false;
5339 if (MEM_P (str_rtx))
5341 unsigned HOST_WIDE_INT offset1;
5343 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5344 str_bitsize = BITS_PER_WORD;
5346 scalar_int_mode best_mode;
5347 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5348 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5349 return false;
5350 str_mode = best_mode;
5351 str_bitsize = GET_MODE_BITSIZE (best_mode);
5353 offset1 = bitpos;
5354 bitpos %= str_bitsize;
5355 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5356 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5358 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5359 return false;
5361 /* If the bit field covers the whole REG/MEM, store_field
5362 will likely generate better code. */
5363 if (bitsize >= str_bitsize)
5364 return false;
5366 /* We can't handle fields split across multiple entities. */
5367 if (bitpos + bitsize > str_bitsize)
5368 return false;
5370 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5371 bitpos = str_bitsize - bitpos - bitsize;
5373 switch (code)
5375 case PLUS_EXPR:
5376 case MINUS_EXPR:
5377 /* For now, just optimize the case of the topmost bitfield
5378 where we don't need to do any masking and also
5379 1 bit bitfields where xor can be used.
5380 We might win by one instruction for the other bitfields
5381 too if insv/extv instructions aren't used, so that
5382 can be added later. */
5383 if ((reverse || bitpos + bitsize != str_bitsize)
5384 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5385 break;
5387 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5388 value = convert_modes (str_mode,
5389 TYPE_MODE (TREE_TYPE (op1)), value,
5390 TYPE_UNSIGNED (TREE_TYPE (op1)));
5392 /* We may be accessing data outside the field, which means
5393 we can alias adjacent data. */
5394 if (MEM_P (str_rtx))
5396 str_rtx = shallow_copy_rtx (str_rtx);
5397 set_mem_alias_set (str_rtx, 0);
5398 set_mem_expr (str_rtx, 0);
5401 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5403 value = expand_and (str_mode, value, const1_rtx, NULL);
5404 binop = xor_optab;
5406 else
5407 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5409 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5410 if (reverse)
5411 value = flip_storage_order (str_mode, value);
5412 result = expand_binop (str_mode, binop, str_rtx,
5413 value, str_rtx, 1, OPTAB_WIDEN);
5414 if (result != str_rtx)
5415 emit_move_insn (str_rtx, result);
5416 return true;
5418 case BIT_IOR_EXPR:
5419 case BIT_XOR_EXPR:
5420 if (TREE_CODE (op1) != INTEGER_CST)
5421 break;
5422 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5423 value = convert_modes (str_mode,
5424 TYPE_MODE (TREE_TYPE (op1)), value,
5425 TYPE_UNSIGNED (TREE_TYPE (op1)));
5427 /* We may be accessing data outside the field, which means
5428 we can alias adjacent data. */
5429 if (MEM_P (str_rtx))
5431 str_rtx = shallow_copy_rtx (str_rtx);
5432 set_mem_alias_set (str_rtx, 0);
5433 set_mem_expr (str_rtx, 0);
5436 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5437 if (bitpos + bitsize != str_bitsize)
5439 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5440 str_mode);
5441 value = expand_and (str_mode, value, mask, NULL_RTX);
5443 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5444 if (reverse)
5445 value = flip_storage_order (str_mode, value);
5446 result = expand_binop (str_mode, binop, str_rtx,
5447 value, str_rtx, 1, OPTAB_WIDEN);
5448 if (result != str_rtx)
5449 emit_move_insn (str_rtx, result);
5450 return true;
5452 default:
5453 break;
5456 return false;
5459 /* In the C++ memory model, consecutive bit fields in a structure are
5460 considered one memory location.
5462 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5463 returns the bit range of consecutive bits in which this COMPONENT_REF
5464 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5465 and *OFFSET may be adjusted in the process.
5467 If the access does not need to be restricted, 0 is returned in both
5468 *BITSTART and *BITEND. */
5470 void
5471 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp,
5472 poly_int64 *bitpos, tree *offset)
5474 poly_int64 bitoffset;
5475 tree field, repr;
5477 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5479 field = TREE_OPERAND (exp, 1);
5480 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5481 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5482 need to limit the range we can access. */
5483 if (!repr)
5485 *bitstart = *bitend = 0;
5486 return;
5489 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5490 part of a larger bit field, then the representative does not serve any
5491 useful purpose. This can occur in Ada. */
5492 if (handled_component_p (TREE_OPERAND (exp, 0)))
5494 machine_mode rmode;
5495 poly_int64 rbitsize, rbitpos;
5496 tree roffset;
5497 int unsignedp, reversep, volatilep = 0;
5498 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5499 &roffset, &rmode, &unsignedp, &reversep,
5500 &volatilep);
5501 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5503 *bitstart = *bitend = 0;
5504 return;
5508 /* Compute the adjustment to bitpos from the offset of the field
5509 relative to the representative. DECL_FIELD_OFFSET of field and
5510 repr are the same by construction if they are not constants,
5511 see finish_bitfield_layout. */
5512 poly_uint64 field_offset, repr_offset;
5513 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5514 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5515 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5516 else
5517 bitoffset = 0;
5518 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5519 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5521 /* If the adjustment is larger than bitpos, we would have a negative bit
5522 position for the lower bound and this may wreak havoc later. Adjust
5523 offset and bitpos to make the lower bound non-negative in that case. */
5524 if (maybe_gt (bitoffset, *bitpos))
5526 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5527 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5529 *bitpos += adjust_bits;
5530 if (*offset == NULL_TREE)
5531 *offset = size_int (-adjust_bytes);
5532 else
5533 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5534 *bitstart = 0;
5536 else
5537 *bitstart = *bitpos - bitoffset;
5539 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5542 /* Returns true if BASE is a DECL that does not reside in memory and
5543 has non-BLKmode. DECL_RTL must not be a MEM; if
5544 DECL_RTL was not set yet, return false. */
5546 bool
5547 non_mem_decl_p (tree base)
5549 if (!DECL_P (base)
5550 || TREE_ADDRESSABLE (base)
5551 || DECL_MODE (base) == BLKmode)
5552 return false;
5554 if (!DECL_RTL_SET_P (base))
5555 return false;
5557 return (!MEM_P (DECL_RTL (base)));
5560 /* Returns true if REF refers to an object that does not
5561 reside in memory and has non-BLKmode. */
5563 bool
5564 mem_ref_refers_to_non_mem_p (tree ref)
5566 tree base;
5568 if (TREE_CODE (ref) == MEM_REF
5569 || TREE_CODE (ref) == TARGET_MEM_REF)
5571 tree addr = TREE_OPERAND (ref, 0);
5573 if (TREE_CODE (addr) != ADDR_EXPR)
5574 return false;
5576 base = TREE_OPERAND (addr, 0);
5578 else
5579 base = ref;
5581 return non_mem_decl_p (base);
5584 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5585 is true, try generating a nontemporal store. */
5587 void
5588 expand_assignment (tree to, tree from, bool nontemporal)
5590 rtx to_rtx = 0;
5591 rtx result;
5592 machine_mode mode;
5593 unsigned int align;
5594 enum insn_code icode;
5596 /* Don't crash if the lhs of the assignment was erroneous. */
5597 if (TREE_CODE (to) == ERROR_MARK)
5599 expand_normal (from);
5600 return;
5603 /* Optimize away no-op moves without side-effects. */
5604 if (operand_equal_p (to, from, 0))
5605 return;
5607 /* Handle misaligned stores. */
5608 mode = TYPE_MODE (TREE_TYPE (to));
5609 if ((TREE_CODE (to) == MEM_REF
5610 || TREE_CODE (to) == TARGET_MEM_REF
5611 || DECL_P (to))
5612 && mode != BLKmode
5613 && !mem_ref_refers_to_non_mem_p (to)
5614 && ((align = get_object_alignment (to))
5615 < GET_MODE_ALIGNMENT (mode))
5616 && (((icode = optab_handler (movmisalign_optab, mode))
5617 != CODE_FOR_nothing)
5618 || targetm.slow_unaligned_access (mode, align)))
5620 rtx reg, mem;
5622 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5623 /* Handle PARALLEL. */
5624 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5625 reg = force_not_mem (reg);
5626 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5627 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5628 reg = flip_storage_order (mode, reg);
5630 if (icode != CODE_FOR_nothing)
5632 class expand_operand ops[2];
5634 create_fixed_operand (&ops[0], mem);
5635 create_input_operand (&ops[1], reg, mode);
5636 /* The movmisalign<mode> pattern cannot fail, else the assignment
5637 would silently be omitted. */
5638 expand_insn (icode, 2, ops);
5640 else
5641 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5642 false, false);
5643 return;
5646 /* Assignment of a structure component needs special treatment
5647 if the structure component's rtx is not simply a MEM.
5648 Assignment of an array element at a constant index, and assignment of
5649 an array element in an unaligned packed structure field, has the same
5650 problem. Same for (partially) storing into a non-memory object. */
5651 if (handled_component_p (to)
5652 || (TREE_CODE (to) == MEM_REF
5653 && (REF_REVERSE_STORAGE_ORDER (to)
5654 || mem_ref_refers_to_non_mem_p (to)))
5655 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5657 machine_mode mode1;
5658 poly_int64 bitsize, bitpos;
5659 poly_uint64 bitregion_start = 0;
5660 poly_uint64 bitregion_end = 0;
5661 tree offset;
5662 int unsignedp, reversep, volatilep = 0;
5663 tree tem;
5665 push_temp_slots ();
5666 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5667 &unsignedp, &reversep, &volatilep);
5669 /* Make sure bitpos is not negative, it can wreak havoc later. */
5670 if (maybe_lt (bitpos, 0))
5672 gcc_assert (offset == NULL_TREE);
5673 offset = size_int (bits_to_bytes_round_down (bitpos));
5674 bitpos = num_trailing_bits (bitpos);
5677 if (TREE_CODE (to) == COMPONENT_REF
5678 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5679 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5680 /* The C++ memory model naturally applies to byte-aligned fields.
5681 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5682 BITSIZE are not byte-aligned, there is no need to limit the range
5683 we can access. This can occur with packed structures in Ada. */
5684 else if (maybe_gt (bitsize, 0)
5685 && multiple_p (bitsize, BITS_PER_UNIT)
5686 && multiple_p (bitpos, BITS_PER_UNIT))
5688 bitregion_start = bitpos;
5689 bitregion_end = bitpos + bitsize - 1;
5692 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5694 /* If the field has a mode, we want to access it in the
5695 field's mode, not the computed mode.
5696 If a MEM has VOIDmode (external with incomplete type),
5697 use BLKmode for it instead. */
5698 if (MEM_P (to_rtx))
5700 if (mode1 != VOIDmode)
5701 to_rtx = adjust_address (to_rtx, mode1, 0);
5702 else if (GET_MODE (to_rtx) == VOIDmode)
5703 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5706 if (offset != 0)
5708 machine_mode address_mode;
5709 rtx offset_rtx;
5711 if (!MEM_P (to_rtx))
5713 /* We can get constant negative offsets into arrays with broken
5714 user code. Translate this to a trap instead of ICEing. */
5715 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5716 expand_builtin_trap ();
5717 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5720 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5721 address_mode = get_address_mode (to_rtx);
5722 if (GET_MODE (offset_rtx) != address_mode)
5724 /* We cannot be sure that the RTL in offset_rtx is valid outside
5725 of a memory address context, so force it into a register
5726 before attempting to convert it to the desired mode. */
5727 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5728 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5731 /* If we have an expression in OFFSET_RTX and a non-zero
5732 byte offset in BITPOS, adding the byte offset before the
5733 OFFSET_RTX results in better intermediate code, which makes
5734 later rtl optimization passes perform better.
5736 We prefer intermediate code like this:
5738 r124:DI=r123:DI+0x18
5739 [r124:DI]=r121:DI
5741 ... instead of ...
5743 r124:DI=r123:DI+0x10
5744 [r124:DI+0x8]=r121:DI
5746 This is only done for aligned data values, as these can
5747 be expected to result in single move instructions. */
5748 poly_int64 bytepos;
5749 if (mode1 != VOIDmode
5750 && maybe_ne (bitpos, 0)
5751 && maybe_gt (bitsize, 0)
5752 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5753 && multiple_p (bitpos, bitsize)
5754 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5755 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5757 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5758 bitregion_start = 0;
5759 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5760 bitregion_end -= bitpos;
5761 bitpos = 0;
5764 to_rtx = offset_address (to_rtx, offset_rtx,
5765 highest_pow2_factor_for_target (to,
5766 offset));
5769 /* No action is needed if the target is not a memory and the field
5770 lies completely outside that target. This can occur if the source
5771 code contains an out-of-bounds access to a small array. */
5772 if (!MEM_P (to_rtx)
5773 && GET_MODE (to_rtx) != BLKmode
5774 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5776 expand_normal (from);
5777 result = NULL;
5779 /* Handle expand_expr of a complex value returning a CONCAT. */
5780 else if (GET_CODE (to_rtx) == CONCAT)
5782 machine_mode to_mode = GET_MODE (to_rtx);
5783 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5784 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5785 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5786 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5787 && known_eq (bitpos, 0)
5788 && known_eq (bitsize, mode_bitsize))
5789 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5790 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5791 && known_eq (bitsize, inner_bitsize)
5792 && (known_eq (bitpos, 0)
5793 || known_eq (bitpos, inner_bitsize)))
5794 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5795 false, nontemporal, reversep);
5796 else if (known_le (bitpos + bitsize, inner_bitsize))
5797 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5798 bitregion_start, bitregion_end,
5799 mode1, from, get_alias_set (to),
5800 nontemporal, reversep);
5801 else if (known_ge (bitpos, inner_bitsize))
5802 result = store_field (XEXP (to_rtx, 1), bitsize,
5803 bitpos - inner_bitsize,
5804 bitregion_start, bitregion_end,
5805 mode1, from, get_alias_set (to),
5806 nontemporal, reversep);
5807 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5809 result = expand_normal (from);
5810 if (GET_CODE (result) == CONCAT)
5812 to_mode = GET_MODE_INNER (to_mode);
5813 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5814 rtx from_real
5815 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5816 from_mode, 0);
5817 rtx from_imag
5818 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5819 from_mode, 0);
5820 if (!from_real || !from_imag)
5821 goto concat_store_slow;
5822 emit_move_insn (XEXP (to_rtx, 0), from_real);
5823 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5825 else
5827 machine_mode from_mode
5828 = GET_MODE (result) == VOIDmode
5829 ? TYPE_MODE (TREE_TYPE (from))
5830 : GET_MODE (result);
5831 rtx from_rtx;
5832 if (MEM_P (result))
5833 from_rtx = change_address (result, to_mode, NULL_RTX);
5834 else
5835 from_rtx
5836 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5837 if (from_rtx)
5839 emit_move_insn (XEXP (to_rtx, 0),
5840 read_complex_part (from_rtx, false));
5841 emit_move_insn (XEXP (to_rtx, 1),
5842 read_complex_part (from_rtx, true));
5844 else
5846 to_mode = GET_MODE_INNER (to_mode);
5847 rtx from_real
5848 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5849 rtx from_imag
5850 = simplify_gen_subreg (to_mode, result, from_mode,
5851 GET_MODE_SIZE (to_mode));
5852 if (!from_real || !from_imag)
5853 goto concat_store_slow;
5854 emit_move_insn (XEXP (to_rtx, 0), from_real);
5855 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5859 else
5861 concat_store_slow:;
5862 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5863 GET_MODE_SIZE (GET_MODE (to_rtx)));
5864 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
5865 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
5866 result = store_field (temp, bitsize, bitpos,
5867 bitregion_start, bitregion_end,
5868 mode1, from, get_alias_set (to),
5869 nontemporal, reversep);
5870 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5871 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5874 /* For calls to functions returning variable length structures, if TO_RTX
5875 is not a MEM, go through a MEM because we must not create temporaries
5876 of the VLA type. */
5877 else if (!MEM_P (to_rtx)
5878 && TREE_CODE (from) == CALL_EXPR
5879 && COMPLETE_TYPE_P (TREE_TYPE (from))
5880 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5882 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5883 GET_MODE_SIZE (GET_MODE (to_rtx)));
5884 result = store_field (temp, bitsize, bitpos, bitregion_start,
5885 bitregion_end, mode1, from, get_alias_set (to),
5886 nontemporal, reversep);
5887 emit_move_insn (to_rtx, temp);
5889 else
5891 if (MEM_P (to_rtx))
5893 /* If the field is at offset zero, we could have been given the
5894 DECL_RTX of the parent struct. Don't munge it. */
5895 to_rtx = shallow_copy_rtx (to_rtx);
5896 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5897 if (volatilep)
5898 MEM_VOLATILE_P (to_rtx) = 1;
5901 gcc_checking_assert (known_ge (bitpos, 0));
5902 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5903 bitregion_start, bitregion_end,
5904 mode1, to_rtx, to, from,
5905 reversep))
5906 result = NULL;
5907 else if (SUBREG_P (to_rtx)
5908 && SUBREG_PROMOTED_VAR_P (to_rtx))
5910 /* If to_rtx is a promoted subreg, we need to zero or sign
5911 extend the value afterwards. */
5912 if (TREE_CODE (to) == MEM_REF
5913 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5914 && !REF_REVERSE_STORAGE_ORDER (to)
5915 && known_eq (bitpos, 0)
5916 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5917 result = store_expr (from, to_rtx, 0, nontemporal, false);
5918 else
5920 rtx to_rtx1
5921 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5922 SUBREG_REG (to_rtx),
5923 subreg_promoted_mode (to_rtx));
5924 result = store_field (to_rtx1, bitsize, bitpos,
5925 bitregion_start, bitregion_end,
5926 mode1, from, get_alias_set (to),
5927 nontemporal, reversep);
5928 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5929 SUBREG_PROMOTED_SIGN (to_rtx));
5932 else
5933 result = store_field (to_rtx, bitsize, bitpos,
5934 bitregion_start, bitregion_end,
5935 mode1, from, get_alias_set (to),
5936 nontemporal, reversep);
5939 if (result)
5940 preserve_temp_slots (result);
5941 pop_temp_slots ();
5942 return;
5945 /* If the rhs is a function call and its value is not an aggregate,
5946 call the function before we start to compute the lhs.
5947 This is needed for correct code for cases such as
5948 val = setjmp (buf) on machines where reference to val
5949 requires loading up part of an address in a separate insn.
5951 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5952 since it might be a promoted variable where the zero- or sign- extension
5953 needs to be done. Handling this in the normal way is safe because no
5954 computation is done before the call. The same is true for SSA names. */
5955 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5956 && COMPLETE_TYPE_P (TREE_TYPE (from))
5957 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5958 && ! (((VAR_P (to)
5959 || TREE_CODE (to) == PARM_DECL
5960 || TREE_CODE (to) == RESULT_DECL)
5961 && REG_P (DECL_RTL (to)))
5962 || TREE_CODE (to) == SSA_NAME))
5964 rtx value;
5966 push_temp_slots ();
5967 value = expand_normal (from);
5969 if (to_rtx == 0)
5970 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5972 /* Handle calls that return values in multiple non-contiguous locations.
5973 The Irix 6 ABI has examples of this. */
5974 if (GET_CODE (to_rtx) == PARALLEL)
5976 if (GET_CODE (value) == PARALLEL)
5977 emit_group_move (to_rtx, value);
5978 else
5979 emit_group_load (to_rtx, value, TREE_TYPE (from),
5980 int_size_in_bytes (TREE_TYPE (from)));
5982 else if (GET_CODE (value) == PARALLEL)
5983 emit_group_store (to_rtx, value, TREE_TYPE (from),
5984 int_size_in_bytes (TREE_TYPE (from)));
5985 else if (GET_MODE (to_rtx) == BLKmode)
5987 /* Handle calls that return BLKmode values in registers. */
5988 if (REG_P (value))
5989 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5990 else
5991 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5993 else
5995 if (POINTER_TYPE_P (TREE_TYPE (to)))
5996 value = convert_memory_address_addr_space
5997 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5998 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
6000 emit_move_insn (to_rtx, value);
6003 preserve_temp_slots (to_rtx);
6004 pop_temp_slots ();
6005 return;
6008 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
6009 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6011 /* Don't move directly into a return register. */
6012 if (TREE_CODE (to) == RESULT_DECL
6013 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
6015 rtx temp;
6017 push_temp_slots ();
6019 /* If the source is itself a return value, it still is in a pseudo at
6020 this point so we can move it back to the return register directly. */
6021 if (REG_P (to_rtx)
6022 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6023 && TREE_CODE (from) != CALL_EXPR)
6024 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6025 else
6026 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6028 /* Handle calls that return values in multiple non-contiguous locations.
6029 The Irix 6 ABI has examples of this. */
6030 if (GET_CODE (to_rtx) == PARALLEL)
6032 if (GET_CODE (temp) == PARALLEL)
6033 emit_group_move (to_rtx, temp);
6034 else
6035 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6036 int_size_in_bytes (TREE_TYPE (from)));
6038 else if (temp)
6039 emit_move_insn (to_rtx, temp);
6041 preserve_temp_slots (to_rtx);
6042 pop_temp_slots ();
6043 return;
6046 /* In case we are returning the contents of an object which overlaps
6047 the place the value is being stored, use a safe function when copying
6048 a value through a pointer into a structure value return block. */
6049 if (TREE_CODE (to) == RESULT_DECL
6050 && TREE_CODE (from) == INDIRECT_REF
6051 && ADDR_SPACE_GENERIC_P
6052 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6053 && refs_may_alias_p (to, from)
6054 && cfun->returns_struct
6055 && !cfun->returns_pcc_struct)
6057 rtx from_rtx, size;
6059 push_temp_slots ();
6060 size = expr_size (from);
6061 from_rtx = expand_normal (from);
6063 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6065 preserve_temp_slots (to_rtx);
6066 pop_temp_slots ();
6067 return;
6070 /* Compute FROM and store the value in the rtx we got. */
6072 push_temp_slots ();
6073 result = store_expr (from, to_rtx, 0, nontemporal, false);
6074 preserve_temp_slots (result);
6075 pop_temp_slots ();
6076 return;
6079 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6080 succeeded, false otherwise. */
6082 bool
6083 emit_storent_insn (rtx to, rtx from)
6085 class expand_operand ops[2];
6086 machine_mode mode = GET_MODE (to);
6087 enum insn_code code = optab_handler (storent_optab, mode);
6089 if (code == CODE_FOR_nothing)
6090 return false;
6092 create_fixed_operand (&ops[0], to);
6093 create_input_operand (&ops[1], from, mode);
6094 return maybe_expand_insn (code, 2, ops);
6097 /* Helper function for store_expr storing of STRING_CST. */
6099 static rtx
6100 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6101 fixed_size_mode mode)
6103 tree str = (tree) data;
6105 gcc_assert (offset >= 0);
6106 if (offset >= TREE_STRING_LENGTH (str))
6107 return const0_rtx;
6109 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6110 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6112 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6113 size_t l = TREE_STRING_LENGTH (str) - offset;
6114 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6115 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6116 return c_readstr (p, mode, false);
6119 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
6122 /* Generate code for computing expression EXP,
6123 and storing the value into TARGET.
6125 If the mode is BLKmode then we may return TARGET itself.
6126 It turns out that in BLKmode it doesn't cause a problem.
6127 because C has no operators that could combine two different
6128 assignments into the same BLKmode object with different values
6129 with no sequence point. Will other languages need this to
6130 be more thorough?
6132 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6133 stack, and block moves may need to be treated specially.
6135 If NONTEMPORAL is true, try using a nontemporal store instruction.
6137 If REVERSE is true, the store is to be done in reverse order. */
6140 store_expr (tree exp, rtx target, int call_param_p,
6141 bool nontemporal, bool reverse)
6143 rtx temp;
6144 rtx alt_rtl = NULL_RTX;
6145 location_t loc = curr_insn_location ();
6146 bool shortened_string_cst = false;
6148 if (VOID_TYPE_P (TREE_TYPE (exp)))
6150 /* C++ can generate ?: expressions with a throw expression in one
6151 branch and an rvalue in the other. Here, we resolve attempts to
6152 store the throw expression's nonexistent result. */
6153 gcc_assert (!call_param_p);
6154 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6155 return NULL_RTX;
6157 if (TREE_CODE (exp) == COMPOUND_EXPR)
6159 /* Perform first part of compound expression, then assign from second
6160 part. */
6161 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6162 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6163 return store_expr (TREE_OPERAND (exp, 1), target,
6164 call_param_p, nontemporal, reverse);
6166 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6168 /* For conditional expression, get safe form of the target. Then
6169 test the condition, doing the appropriate assignment on either
6170 side. This avoids the creation of unnecessary temporaries.
6171 For non-BLKmode, it is more efficient not to do this. */
6173 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6175 do_pending_stack_adjust ();
6176 NO_DEFER_POP;
6177 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6178 profile_probability::uninitialized ());
6179 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6180 nontemporal, reverse);
6181 emit_jump_insn (targetm.gen_jump (lab2));
6182 emit_barrier ();
6183 emit_label (lab1);
6184 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6185 nontemporal, reverse);
6186 emit_label (lab2);
6187 OK_DEFER_POP;
6189 return NULL_RTX;
6191 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6192 /* If this is a scalar in a register that is stored in a wider mode
6193 than the declared mode, compute the result into its declared mode
6194 and then convert to the wider mode. Our value is the computed
6195 expression. */
6197 rtx inner_target = 0;
6198 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6199 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6201 /* We can do the conversion inside EXP, which will often result
6202 in some optimizations. Do the conversion in two steps: first
6203 change the signedness, if needed, then the extend. But don't
6204 do this if the type of EXP is a subtype of something else
6205 since then the conversion might involve more than just
6206 converting modes. */
6207 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6208 && TREE_TYPE (TREE_TYPE (exp)) == 0
6209 && GET_MODE_PRECISION (outer_mode)
6210 == TYPE_PRECISION (TREE_TYPE (exp)))
6212 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6213 TYPE_UNSIGNED (TREE_TYPE (exp))))
6215 /* Some types, e.g. Fortran's logical*4, won't have a signed
6216 version, so use the mode instead. */
6217 tree ntype
6218 = (signed_or_unsigned_type_for
6219 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6220 if (ntype == NULL)
6221 ntype = lang_hooks.types.type_for_mode
6222 (TYPE_MODE (TREE_TYPE (exp)),
6223 SUBREG_PROMOTED_SIGN (target));
6225 exp = fold_convert_loc (loc, ntype, exp);
6228 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6229 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6230 exp);
6232 inner_target = SUBREG_REG (target);
6235 temp = expand_expr (exp, inner_target, VOIDmode,
6236 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6239 /* If TEMP is a VOIDmode constant, use convert_modes to make
6240 sure that we properly convert it. */
6241 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6243 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6244 temp, SUBREG_PROMOTED_SIGN (target));
6245 temp = convert_modes (inner_mode, outer_mode, temp,
6246 SUBREG_PROMOTED_SIGN (target));
6248 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6249 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6250 temp, SUBREG_PROMOTED_SIGN (target));
6252 convert_move (SUBREG_REG (target), temp,
6253 SUBREG_PROMOTED_SIGN (target));
6255 return NULL_RTX;
6257 else if ((TREE_CODE (exp) == STRING_CST
6258 || (TREE_CODE (exp) == MEM_REF
6259 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6260 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6261 == STRING_CST
6262 && integer_zerop (TREE_OPERAND (exp, 1))))
6263 && !nontemporal && !call_param_p
6264 && MEM_P (target))
6266 /* Optimize initialization of an array with a STRING_CST. */
6267 HOST_WIDE_INT exp_len, str_copy_len;
6268 rtx dest_mem;
6269 tree str = TREE_CODE (exp) == STRING_CST
6270 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6272 exp_len = int_expr_size (exp);
6273 if (exp_len <= 0)
6274 goto normal_expr;
6276 if (TREE_STRING_LENGTH (str) <= 0)
6277 goto normal_expr;
6279 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6280 MEM_ALIGN (target), false))
6282 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6283 MEM_ALIGN (target), false, RETURN_BEGIN);
6284 return NULL_RTX;
6287 str_copy_len = TREE_STRING_LENGTH (str);
6289 /* Trailing NUL bytes in EXP will be handled by the call to
6290 clear_storage, which is more efficient than copying them from
6291 the STRING_CST, so trim those from STR_COPY_LEN. */
6292 while (str_copy_len)
6294 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6295 break;
6296 str_copy_len--;
6299 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6301 str_copy_len += STORE_MAX_PIECES - 1;
6302 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6304 if (str_copy_len >= exp_len)
6305 goto normal_expr;
6307 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6308 (void *) str, MEM_ALIGN (target), false))
6309 goto normal_expr;
6311 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6312 (void *) str, MEM_ALIGN (target), false,
6313 RETURN_END);
6314 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6315 exp_len - str_copy_len),
6316 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6317 return NULL_RTX;
6319 else
6321 rtx tmp_target;
6323 normal_expr:
6324 /* If we want to use a nontemporal or a reverse order store, force the
6325 value into a register first. */
6326 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6327 tree rexp = exp;
6328 if (TREE_CODE (exp) == STRING_CST
6329 && tmp_target == target
6330 && GET_MODE (target) == BLKmode
6331 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6333 rtx size = expr_size (exp);
6334 if (CONST_INT_P (size)
6335 && size != const0_rtx
6336 && (UINTVAL (size)
6337 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6339 /* If the STRING_CST has much larger array type than
6340 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6341 it into the rodata section as the code later on will use
6342 memset zero for the remainder anyway. See PR95052. */
6343 tmp_target = NULL_RTX;
6344 rexp = copy_node (exp);
6345 tree index
6346 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6347 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6348 index);
6349 shortened_string_cst = true;
6352 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6353 (call_param_p
6354 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6355 &alt_rtl, false);
6356 if (shortened_string_cst)
6358 gcc_assert (MEM_P (temp));
6359 temp = change_address (temp, BLKmode, NULL_RTX);
6363 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6364 the same as that of TARGET, adjust the constant. This is needed, for
6365 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6366 only a word-sized value. */
6367 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6368 && TREE_CODE (exp) != ERROR_MARK
6369 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6371 gcc_assert (!shortened_string_cst);
6372 if (GET_MODE_CLASS (GET_MODE (target))
6373 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6374 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6375 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6377 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6378 TYPE_MODE (TREE_TYPE (exp)), 0);
6379 if (t)
6380 temp = t;
6382 if (GET_MODE (temp) == VOIDmode)
6383 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6384 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6387 /* If value was not generated in the target, store it there.
6388 Convert the value to TARGET's type first if necessary and emit the
6389 pending incrementations that have been queued when expanding EXP.
6390 Note that we cannot emit the whole queue blindly because this will
6391 effectively disable the POST_INC optimization later.
6393 If TEMP and TARGET compare equal according to rtx_equal_p, but
6394 one or both of them are volatile memory refs, we have to distinguish
6395 two cases:
6396 - expand_expr has used TARGET. In this case, we must not generate
6397 another copy. This can be detected by TARGET being equal according
6398 to == .
6399 - expand_expr has not used TARGET - that means that the source just
6400 happens to have the same RTX form. Since temp will have been created
6401 by expand_expr, it will compare unequal according to == .
6402 We must generate a copy in this case, to reach the correct number
6403 of volatile memory references. */
6405 if ((! rtx_equal_p (temp, target)
6406 || (temp != target && (side_effects_p (temp)
6407 || side_effects_p (target)
6408 || (MEM_P (temp)
6409 && !mems_same_for_tbaa_p (temp, target)))))
6410 && TREE_CODE (exp) != ERROR_MARK
6411 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6412 but TARGET is not valid memory reference, TEMP will differ
6413 from TARGET although it is really the same location. */
6414 && !(alt_rtl
6415 && rtx_equal_p (alt_rtl, target)
6416 && !side_effects_p (alt_rtl)
6417 && !side_effects_p (target))
6418 /* If there's nothing to copy, don't bother. Don't call
6419 expr_size unless necessary, because some front-ends (C++)
6420 expr_size-hook must not be given objects that are not
6421 supposed to be bit-copied or bit-initialized. */
6422 && expr_size (exp) != const0_rtx)
6424 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6426 gcc_assert (!shortened_string_cst);
6427 if (GET_MODE (target) == BLKmode)
6429 /* Handle calls that return BLKmode values in registers. */
6430 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6431 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6432 else
6433 store_bit_field (target,
6434 rtx_to_poly_int64 (expr_size (exp))
6435 * BITS_PER_UNIT,
6436 0, 0, 0, GET_MODE (temp), temp, reverse,
6437 false);
6439 else
6440 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6443 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6445 /* Handle copying a string constant into an array. The string
6446 constant may be shorter than the array. So copy just the string's
6447 actual length, and clear the rest. First get the size of the data
6448 type of the string, which is actually the size of the target. */
6449 rtx size = expr_size (exp);
6451 if (CONST_INT_P (size)
6452 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6453 emit_block_move (target, temp, size,
6454 (call_param_p
6455 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6456 else
6458 machine_mode pointer_mode
6459 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6460 machine_mode address_mode = get_address_mode (target);
6462 /* Compute the size of the data to copy from the string. */
6463 tree copy_size
6464 = size_binop_loc (loc, MIN_EXPR,
6465 make_tree (sizetype, size),
6466 size_int (TREE_STRING_LENGTH (exp)));
6467 rtx copy_size_rtx
6468 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6469 (call_param_p
6470 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6471 rtx_code_label *label = 0;
6473 /* Copy that much. */
6474 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6475 TYPE_UNSIGNED (sizetype));
6476 emit_block_move (target, temp, copy_size_rtx,
6477 (call_param_p
6478 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6480 /* Figure out how much is left in TARGET that we have to clear.
6481 Do all calculations in pointer_mode. */
6482 poly_int64 const_copy_size;
6483 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6485 size = plus_constant (address_mode, size, -const_copy_size);
6486 target = adjust_address (target, BLKmode, const_copy_size);
6488 else
6490 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6491 copy_size_rtx, NULL_RTX, 0,
6492 OPTAB_LIB_WIDEN);
6494 if (GET_MODE (copy_size_rtx) != address_mode)
6495 copy_size_rtx = convert_to_mode (address_mode,
6496 copy_size_rtx,
6497 TYPE_UNSIGNED (sizetype));
6499 target = offset_address (target, copy_size_rtx,
6500 highest_pow2_factor (copy_size));
6501 label = gen_label_rtx ();
6502 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6503 GET_MODE (size), 0, label);
6506 if (size != const0_rtx)
6507 clear_storage (target, size, BLOCK_OP_NORMAL);
6509 if (label)
6510 emit_label (label);
6513 else if (shortened_string_cst)
6514 gcc_unreachable ();
6515 /* Handle calls that return values in multiple non-contiguous locations.
6516 The Irix 6 ABI has examples of this. */
6517 else if (GET_CODE (target) == PARALLEL)
6519 if (GET_CODE (temp) == PARALLEL)
6520 emit_group_move (target, temp);
6521 else
6522 emit_group_load (target, temp, TREE_TYPE (exp),
6523 int_size_in_bytes (TREE_TYPE (exp)));
6525 else if (GET_CODE (temp) == PARALLEL)
6526 emit_group_store (target, temp, TREE_TYPE (exp),
6527 int_size_in_bytes (TREE_TYPE (exp)));
6528 else if (GET_MODE (temp) == BLKmode)
6529 emit_block_move (target, temp, expr_size (exp),
6530 (call_param_p
6531 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6532 /* If we emit a nontemporal store, there is nothing else to do. */
6533 else if (nontemporal && emit_storent_insn (target, temp))
6535 else
6537 if (reverse)
6538 temp = flip_storage_order (GET_MODE (target), temp);
6539 temp = force_operand (temp, target);
6540 if (temp != target)
6541 emit_move_insn (target, temp);
6544 else
6545 gcc_assert (!shortened_string_cst);
6547 return NULL_RTX;
6550 /* Return true if field F of structure TYPE is a flexible array. */
6552 static bool
6553 flexible_array_member_p (const_tree f, const_tree type)
6555 const_tree tf;
6557 tf = TREE_TYPE (f);
6558 return (DECL_CHAIN (f) == NULL
6559 && TREE_CODE (tf) == ARRAY_TYPE
6560 && TYPE_DOMAIN (tf)
6561 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6562 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6563 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6564 && int_size_in_bytes (type) >= 0);
6567 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6568 must have in order for it to completely initialize a value of type TYPE.
6569 Return -1 if the number isn't known.
6571 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6573 static HOST_WIDE_INT
6574 count_type_elements (const_tree type, bool for_ctor_p)
6576 switch (TREE_CODE (type))
6578 case ARRAY_TYPE:
6580 tree nelts;
6582 nelts = array_type_nelts (type);
6583 if (nelts && tree_fits_uhwi_p (nelts))
6585 unsigned HOST_WIDE_INT n;
6587 n = tree_to_uhwi (nelts) + 1;
6588 if (n == 0 || for_ctor_p)
6589 return n;
6590 else
6591 return n * count_type_elements (TREE_TYPE (type), false);
6593 return for_ctor_p ? -1 : 1;
6596 case RECORD_TYPE:
6598 unsigned HOST_WIDE_INT n;
6599 tree f;
6601 n = 0;
6602 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6603 if (TREE_CODE (f) == FIELD_DECL)
6605 if (!for_ctor_p)
6606 n += count_type_elements (TREE_TYPE (f), false);
6607 else if (!flexible_array_member_p (f, type))
6608 /* Don't count flexible arrays, which are not supposed
6609 to be initialized. */
6610 n += 1;
6613 return n;
6616 case UNION_TYPE:
6617 case QUAL_UNION_TYPE:
6619 tree f;
6620 HOST_WIDE_INT n, m;
6622 gcc_assert (!for_ctor_p);
6623 /* Estimate the number of scalars in each field and pick the
6624 maximum. Other estimates would do instead; the idea is simply
6625 to make sure that the estimate is not sensitive to the ordering
6626 of the fields. */
6627 n = 1;
6628 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6629 if (TREE_CODE (f) == FIELD_DECL)
6631 m = count_type_elements (TREE_TYPE (f), false);
6632 /* If the field doesn't span the whole union, add an extra
6633 scalar for the rest. */
6634 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6635 TYPE_SIZE (type)) != 1)
6636 m++;
6637 if (n < m)
6638 n = m;
6640 return n;
6643 case COMPLEX_TYPE:
6644 return 2;
6646 case VECTOR_TYPE:
6648 unsigned HOST_WIDE_INT nelts;
6649 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6650 return nelts;
6651 else
6652 return -1;
6655 case INTEGER_TYPE:
6656 case REAL_TYPE:
6657 case FIXED_POINT_TYPE:
6658 case ENUMERAL_TYPE:
6659 case BOOLEAN_TYPE:
6660 case POINTER_TYPE:
6661 case OFFSET_TYPE:
6662 case REFERENCE_TYPE:
6663 case NULLPTR_TYPE:
6664 case OPAQUE_TYPE:
6665 return 1;
6667 case ERROR_MARK:
6668 return 0;
6670 case VOID_TYPE:
6671 case METHOD_TYPE:
6672 case FUNCTION_TYPE:
6673 case LANG_TYPE:
6674 default:
6675 gcc_unreachable ();
6679 /* Helper for categorize_ctor_elements. Identical interface. */
6681 static bool
6682 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6683 HOST_WIDE_INT *p_unique_nz_elts,
6684 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6686 unsigned HOST_WIDE_INT idx;
6687 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6688 tree value, purpose, elt_type;
6690 /* Whether CTOR is a valid constant initializer, in accordance with what
6691 initializer_constant_valid_p does. If inferred from the constructor
6692 elements, true until proven otherwise. */
6693 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6694 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6696 nz_elts = 0;
6697 unique_nz_elts = 0;
6698 init_elts = 0;
6699 num_fields = 0;
6700 elt_type = NULL_TREE;
6702 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6704 HOST_WIDE_INT mult = 1;
6706 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6708 tree lo_index = TREE_OPERAND (purpose, 0);
6709 tree hi_index = TREE_OPERAND (purpose, 1);
6711 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6712 mult = (tree_to_uhwi (hi_index)
6713 - tree_to_uhwi (lo_index) + 1);
6715 num_fields += mult;
6716 elt_type = TREE_TYPE (value);
6718 switch (TREE_CODE (value))
6720 case CONSTRUCTOR:
6722 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6724 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6725 &ic, p_complete);
6727 nz_elts += mult * nz;
6728 unique_nz_elts += unz;
6729 init_elts += mult * ic;
6731 if (const_from_elts_p && const_p)
6732 const_p = const_elt_p;
6734 break;
6736 case INTEGER_CST:
6737 case REAL_CST:
6738 case FIXED_CST:
6739 if (!initializer_zerop (value))
6741 nz_elts += mult;
6742 unique_nz_elts++;
6744 init_elts += mult;
6745 break;
6747 case STRING_CST:
6748 nz_elts += mult * TREE_STRING_LENGTH (value);
6749 unique_nz_elts += TREE_STRING_LENGTH (value);
6750 init_elts += mult * TREE_STRING_LENGTH (value);
6751 break;
6753 case COMPLEX_CST:
6754 if (!initializer_zerop (TREE_REALPART (value)))
6756 nz_elts += mult;
6757 unique_nz_elts++;
6759 if (!initializer_zerop (TREE_IMAGPART (value)))
6761 nz_elts += mult;
6762 unique_nz_elts++;
6764 init_elts += 2 * mult;
6765 break;
6767 case VECTOR_CST:
6769 /* We can only construct constant-length vectors using
6770 CONSTRUCTOR. */
6771 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6772 for (unsigned int i = 0; i < nunits; ++i)
6774 tree v = VECTOR_CST_ELT (value, i);
6775 if (!initializer_zerop (v))
6777 nz_elts += mult;
6778 unique_nz_elts++;
6780 init_elts += mult;
6783 break;
6785 default:
6787 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6788 nz_elts += mult * tc;
6789 unique_nz_elts += tc;
6790 init_elts += mult * tc;
6792 if (const_from_elts_p && const_p)
6793 const_p
6794 = initializer_constant_valid_p (value,
6795 elt_type,
6796 TYPE_REVERSE_STORAGE_ORDER
6797 (TREE_TYPE (ctor)))
6798 != NULL_TREE;
6800 break;
6804 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6805 num_fields, elt_type))
6806 *p_complete = false;
6808 *p_nz_elts += nz_elts;
6809 *p_unique_nz_elts += unique_nz_elts;
6810 *p_init_elts += init_elts;
6812 return const_p;
6815 /* Examine CTOR to discover:
6816 * how many scalar fields are set to nonzero values,
6817 and place it in *P_NZ_ELTS;
6818 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6819 high - low + 1 (this can be useful for callers to determine ctors
6820 that could be cheaply initialized with - perhaps nested - loops
6821 compared to copied from huge read-only data),
6822 and place it in *P_UNIQUE_NZ_ELTS;
6823 * how many scalar fields in total are in CTOR,
6824 and place it in *P_ELT_COUNT.
6825 * whether the constructor is complete -- in the sense that every
6826 meaningful byte is explicitly given a value --
6827 and place it in *P_COMPLETE.
6829 Return whether or not CTOR is a valid static constant initializer, the same
6830 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6832 bool
6833 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6834 HOST_WIDE_INT *p_unique_nz_elts,
6835 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6837 *p_nz_elts = 0;
6838 *p_unique_nz_elts = 0;
6839 *p_init_elts = 0;
6840 *p_complete = true;
6842 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6843 p_init_elts, p_complete);
6846 /* Return true if constructor CTOR is simple enough to be materialized
6847 in an integer mode register. Limit the size to WORDS words, which
6848 is 1 by default. */
6850 bool
6851 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6853 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6854 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6855 return false;
6857 return TREE_CONSTANT (ctor)
6858 && !TREE_ADDRESSABLE (ctor)
6859 && CONSTRUCTOR_NELTS (ctor)
6860 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6861 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6862 && initializer_constant_valid_for_bitfield_p (ctor);
6865 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6866 of which had type LAST_TYPE. Each element was itself a complete
6867 initializer, in the sense that every meaningful byte was explicitly
6868 given a value. Return true if the same is true for the constructor
6869 as a whole. */
6871 bool
6872 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6873 const_tree last_type)
6875 if (TREE_CODE (type) == UNION_TYPE
6876 || TREE_CODE (type) == QUAL_UNION_TYPE)
6878 if (num_elts == 0)
6879 return false;
6881 gcc_assert (num_elts == 1 && last_type);
6883 /* ??? We could look at each element of the union, and find the
6884 largest element. Which would avoid comparing the size of the
6885 initialized element against any tail padding in the union.
6886 Doesn't seem worth the effort... */
6887 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6890 return count_type_elements (type, true) == num_elts;
6893 /* Return true if EXP contains mostly (3/4) zeros. */
6895 static bool
6896 mostly_zeros_p (const_tree exp)
6898 if (TREE_CODE (exp) == CONSTRUCTOR)
6900 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6901 bool complete_p;
6903 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6904 &complete_p);
6905 return !complete_p || nz_elts < init_elts / 4;
6908 return initializer_zerop (exp);
6911 /* Return true if EXP contains all zeros. */
6913 static bool
6914 all_zeros_p (const_tree exp)
6916 if (TREE_CODE (exp) == CONSTRUCTOR)
6918 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6919 bool complete_p;
6921 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6922 &complete_p);
6923 return nz_elts == 0;
6926 return initializer_zerop (exp);
6929 /* Helper function for store_constructor.
6930 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6931 CLEARED is as for store_constructor.
6932 ALIAS_SET is the alias set to use for any stores.
6933 If REVERSE is true, the store is to be done in reverse order.
6935 This provides a recursive shortcut back to store_constructor when it isn't
6936 necessary to go through store_field. This is so that we can pass through
6937 the cleared field to let store_constructor know that we may not have to
6938 clear a substructure if the outer structure has already been cleared. */
6940 static void
6941 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6942 poly_uint64 bitregion_start,
6943 poly_uint64 bitregion_end,
6944 machine_mode mode,
6945 tree exp, int cleared,
6946 alias_set_type alias_set, bool reverse)
6948 poly_int64 bytepos;
6949 poly_uint64 bytesize;
6950 if (TREE_CODE (exp) == CONSTRUCTOR
6951 /* We can only call store_constructor recursively if the size and
6952 bit position are on a byte boundary. */
6953 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6954 && maybe_ne (bitsize, 0U)
6955 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6956 /* If we have a nonzero bitpos for a register target, then we just
6957 let store_field do the bitfield handling. This is unlikely to
6958 generate unnecessary clear instructions anyways. */
6959 && (known_eq (bitpos, 0) || MEM_P (target)))
6961 if (MEM_P (target))
6963 machine_mode target_mode = GET_MODE (target);
6964 if (target_mode != BLKmode
6965 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6966 target_mode = BLKmode;
6967 target = adjust_address (target, target_mode, bytepos);
6971 /* Update the alias set, if required. */
6972 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6973 && MEM_ALIAS_SET (target) != 0)
6975 target = copy_rtx (target);
6976 set_mem_alias_set (target, alias_set);
6979 store_constructor (exp, target, cleared, bytesize, reverse);
6981 else
6982 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6983 exp, alias_set, false, reverse);
6987 /* Returns the number of FIELD_DECLs in TYPE. */
6989 static int
6990 fields_length (const_tree type)
6992 tree t = TYPE_FIELDS (type);
6993 int count = 0;
6995 for (; t; t = DECL_CHAIN (t))
6996 if (TREE_CODE (t) == FIELD_DECL)
6997 ++count;
6999 return count;
7003 /* Store the value of constructor EXP into the rtx TARGET.
7004 TARGET is either a REG or a MEM; we know it cannot conflict, since
7005 safe_from_p has been called.
7006 CLEARED is true if TARGET is known to have been zero'd.
7007 SIZE is the number of bytes of TARGET we are allowed to modify: this
7008 may not be the same as the size of EXP if we are assigning to a field
7009 which has been packed to exclude padding bits.
7010 If REVERSE is true, the store is to be done in reverse order. */
7012 void
7013 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
7014 bool reverse)
7016 tree type = TREE_TYPE (exp);
7017 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
7018 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
7020 switch (TREE_CODE (type))
7022 case RECORD_TYPE:
7023 case UNION_TYPE:
7024 case QUAL_UNION_TYPE:
7026 unsigned HOST_WIDE_INT idx;
7027 tree field, value;
7029 /* The storage order is specified for every aggregate type. */
7030 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7032 /* If size is zero or the target is already cleared, do nothing. */
7033 if (known_eq (size, 0) || cleared)
7034 cleared = 1;
7035 /* We either clear the aggregate or indicate the value is dead. */
7036 else if ((TREE_CODE (type) == UNION_TYPE
7037 || TREE_CODE (type) == QUAL_UNION_TYPE)
7038 && ! CONSTRUCTOR_ELTS (exp))
7039 /* If the constructor is empty, clear the union. */
7041 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7042 cleared = 1;
7045 /* If we are building a static constructor into a register,
7046 set the initial value as zero so we can fold the value into
7047 a constant. But if more than one register is involved,
7048 this probably loses. */
7049 else if (REG_P (target) && TREE_STATIC (exp)
7050 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7051 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7053 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7054 cleared = 1;
7057 /* If the constructor has fewer fields than the structure or
7058 if we are initializing the structure to mostly zeros, clear
7059 the whole structure first. Don't do this if TARGET is a
7060 register whose mode size isn't equal to SIZE since
7061 clear_storage can't handle this case. */
7062 else if (known_size_p (size)
7063 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7064 || mostly_zeros_p (exp))
7065 && (!REG_P (target)
7066 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7068 clear_storage (target, gen_int_mode (size, Pmode),
7069 BLOCK_OP_NORMAL);
7070 cleared = 1;
7073 if (REG_P (target) && !cleared)
7074 emit_clobber (target);
7076 /* Store each element of the constructor into the
7077 corresponding field of TARGET. */
7078 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7080 machine_mode mode;
7081 HOST_WIDE_INT bitsize;
7082 HOST_WIDE_INT bitpos = 0;
7083 tree offset;
7084 rtx to_rtx = target;
7086 /* Just ignore missing fields. We cleared the whole
7087 structure, above, if any fields are missing. */
7088 if (field == 0)
7089 continue;
7091 if (cleared && initializer_zerop (value))
7092 continue;
7094 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7095 bitsize = tree_to_uhwi (DECL_SIZE (field));
7096 else
7097 gcc_unreachable ();
7099 mode = DECL_MODE (field);
7100 if (DECL_BIT_FIELD (field))
7101 mode = VOIDmode;
7103 offset = DECL_FIELD_OFFSET (field);
7104 if (tree_fits_shwi_p (offset)
7105 && tree_fits_shwi_p (bit_position (field)))
7107 bitpos = int_bit_position (field);
7108 offset = NULL_TREE;
7110 else
7111 gcc_unreachable ();
7113 /* If this initializes a field that is smaller than a
7114 word, at the start of a word, try to widen it to a full
7115 word. This special case allows us to output C++ member
7116 function initializations in a form that the optimizers
7117 can understand. */
7118 if (WORD_REGISTER_OPERATIONS
7119 && REG_P (target)
7120 && bitsize < BITS_PER_WORD
7121 && bitpos % BITS_PER_WORD == 0
7122 && GET_MODE_CLASS (mode) == MODE_INT
7123 && TREE_CODE (value) == INTEGER_CST
7124 && exp_size >= 0
7125 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7127 type = TREE_TYPE (value);
7129 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7131 type = lang_hooks.types.type_for_mode
7132 (word_mode, TYPE_UNSIGNED (type));
7133 value = fold_convert (type, value);
7134 /* Make sure the bits beyond the original bitsize are zero
7135 so that we can correctly avoid extra zeroing stores in
7136 later constructor elements. */
7137 tree bitsize_mask
7138 = wide_int_to_tree (type, wi::mask (bitsize, false,
7139 BITS_PER_WORD));
7140 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7143 if (BYTES_BIG_ENDIAN)
7144 value
7145 = fold_build2 (LSHIFT_EXPR, type, value,
7146 build_int_cst (type,
7147 BITS_PER_WORD - bitsize));
7148 bitsize = BITS_PER_WORD;
7149 mode = word_mode;
7152 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7153 && DECL_NONADDRESSABLE_P (field))
7155 to_rtx = copy_rtx (to_rtx);
7156 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7159 store_constructor_field (to_rtx, bitsize, bitpos,
7160 0, bitregion_end, mode,
7161 value, cleared,
7162 get_alias_set (TREE_TYPE (field)),
7163 reverse);
7165 break;
7167 case ARRAY_TYPE:
7169 tree value, index;
7170 unsigned HOST_WIDE_INT i;
7171 bool need_to_clear;
7172 tree domain;
7173 tree elttype = TREE_TYPE (type);
7174 bool const_bounds_p;
7175 HOST_WIDE_INT minelt = 0;
7176 HOST_WIDE_INT maxelt = 0;
7178 /* The storage order is specified for every aggregate type. */
7179 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7181 domain = TYPE_DOMAIN (type);
7182 const_bounds_p = (TYPE_MIN_VALUE (domain)
7183 && TYPE_MAX_VALUE (domain)
7184 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7185 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7187 /* If we have constant bounds for the range of the type, get them. */
7188 if (const_bounds_p)
7190 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7191 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7194 /* If the constructor has fewer elements than the array, clear
7195 the whole array first. Similarly if this is static
7196 constructor of a non-BLKmode object. */
7197 if (cleared)
7198 need_to_clear = false;
7199 else if (REG_P (target) && TREE_STATIC (exp))
7200 need_to_clear = true;
7201 else
7203 unsigned HOST_WIDE_INT idx;
7204 HOST_WIDE_INT count = 0, zero_count = 0;
7205 need_to_clear = ! const_bounds_p;
7207 /* This loop is a more accurate version of the loop in
7208 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7209 is also needed to check for missing elements. */
7210 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7212 HOST_WIDE_INT this_node_count;
7214 if (need_to_clear)
7215 break;
7217 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7219 tree lo_index = TREE_OPERAND (index, 0);
7220 tree hi_index = TREE_OPERAND (index, 1);
7222 if (! tree_fits_uhwi_p (lo_index)
7223 || ! tree_fits_uhwi_p (hi_index))
7225 need_to_clear = true;
7226 break;
7229 this_node_count = (tree_to_uhwi (hi_index)
7230 - tree_to_uhwi (lo_index) + 1);
7232 else
7233 this_node_count = 1;
7235 count += this_node_count;
7236 if (mostly_zeros_p (value))
7237 zero_count += this_node_count;
7240 /* Clear the entire array first if there are any missing
7241 elements, or if the incidence of zero elements is >=
7242 75%. */
7243 if (! need_to_clear
7244 && (count < maxelt - minelt + 1
7245 || 4 * zero_count >= 3 * count))
7246 need_to_clear = true;
7249 if (need_to_clear && maybe_gt (size, 0))
7251 if (REG_P (target))
7252 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7253 else
7254 clear_storage (target, gen_int_mode (size, Pmode),
7255 BLOCK_OP_NORMAL);
7256 cleared = 1;
7259 if (!cleared && REG_P (target))
7260 /* Inform later passes that the old value is dead. */
7261 emit_clobber (target);
7263 /* Store each element of the constructor into the
7264 corresponding element of TARGET, determined by counting the
7265 elements. */
7266 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7268 machine_mode mode;
7269 poly_int64 bitsize;
7270 HOST_WIDE_INT bitpos;
7271 rtx xtarget = target;
7273 if (cleared && initializer_zerop (value))
7274 continue;
7276 mode = TYPE_MODE (elttype);
7277 if (mode != BLKmode)
7278 bitsize = GET_MODE_BITSIZE (mode);
7279 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7280 bitsize = -1;
7282 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7284 tree lo_index = TREE_OPERAND (index, 0);
7285 tree hi_index = TREE_OPERAND (index, 1);
7286 rtx index_r, pos_rtx;
7287 HOST_WIDE_INT lo, hi, count;
7288 tree position;
7290 /* If the range is constant and "small", unroll the loop. */
7291 if (const_bounds_p
7292 && tree_fits_shwi_p (lo_index)
7293 && tree_fits_shwi_p (hi_index)
7294 && (lo = tree_to_shwi (lo_index),
7295 hi = tree_to_shwi (hi_index),
7296 count = hi - lo + 1,
7297 (!MEM_P (target)
7298 || count <= 2
7299 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7300 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7301 <= 40 * 8)))))
7303 lo -= minelt; hi -= minelt;
7304 for (; lo <= hi; lo++)
7306 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7308 if (MEM_P (target)
7309 && !MEM_KEEP_ALIAS_SET_P (target)
7310 && TREE_CODE (type) == ARRAY_TYPE
7311 && TYPE_NONALIASED_COMPONENT (type))
7313 target = copy_rtx (target);
7314 MEM_KEEP_ALIAS_SET_P (target) = 1;
7317 store_constructor_field
7318 (target, bitsize, bitpos, 0, bitregion_end,
7319 mode, value, cleared,
7320 get_alias_set (elttype), reverse);
7323 else
7325 rtx_code_label *loop_start = gen_label_rtx ();
7326 rtx_code_label *loop_end = gen_label_rtx ();
7327 tree exit_cond;
7329 expand_normal (hi_index);
7331 index = build_decl (EXPR_LOCATION (exp),
7332 VAR_DECL, NULL_TREE, domain);
7333 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7334 SET_DECL_RTL (index, index_r);
7335 store_expr (lo_index, index_r, 0, false, reverse);
7337 /* Build the head of the loop. */
7338 do_pending_stack_adjust ();
7339 emit_label (loop_start);
7341 /* Assign value to element index. */
7342 position =
7343 fold_convert (ssizetype,
7344 fold_build2 (MINUS_EXPR,
7345 TREE_TYPE (index),
7346 index,
7347 TYPE_MIN_VALUE (domain)));
7349 position =
7350 size_binop (MULT_EXPR, position,
7351 fold_convert (ssizetype,
7352 TYPE_SIZE_UNIT (elttype)));
7354 pos_rtx = expand_normal (position);
7355 xtarget = offset_address (target, pos_rtx,
7356 highest_pow2_factor (position));
7357 xtarget = adjust_address (xtarget, mode, 0);
7358 if (TREE_CODE (value) == CONSTRUCTOR)
7359 store_constructor (value, xtarget, cleared,
7360 exact_div (bitsize, BITS_PER_UNIT),
7361 reverse);
7362 else
7363 store_expr (value, xtarget, 0, false, reverse);
7365 /* Generate a conditional jump to exit the loop. */
7366 exit_cond = build2 (LT_EXPR, integer_type_node,
7367 index, hi_index);
7368 jumpif (exit_cond, loop_end,
7369 profile_probability::uninitialized ());
7371 /* Update the loop counter, and jump to the head of
7372 the loop. */
7373 expand_assignment (index,
7374 build2 (PLUS_EXPR, TREE_TYPE (index),
7375 index, integer_one_node),
7376 false);
7378 emit_jump (loop_start);
7380 /* Build the end of the loop. */
7381 emit_label (loop_end);
7384 else if ((index != 0 && ! tree_fits_shwi_p (index))
7385 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7387 tree position;
7389 if (index == 0)
7390 index = ssize_int (1);
7392 if (minelt)
7393 index = fold_convert (ssizetype,
7394 fold_build2 (MINUS_EXPR,
7395 TREE_TYPE (index),
7396 index,
7397 TYPE_MIN_VALUE (domain)));
7399 position =
7400 size_binop (MULT_EXPR, index,
7401 fold_convert (ssizetype,
7402 TYPE_SIZE_UNIT (elttype)));
7403 xtarget = offset_address (target,
7404 expand_normal (position),
7405 highest_pow2_factor (position));
7406 xtarget = adjust_address (xtarget, mode, 0);
7407 store_expr (value, xtarget, 0, false, reverse);
7409 else
7411 if (index != 0)
7412 bitpos = ((tree_to_shwi (index) - minelt)
7413 * tree_to_uhwi (TYPE_SIZE (elttype)));
7414 else
7415 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7417 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7418 && TREE_CODE (type) == ARRAY_TYPE
7419 && TYPE_NONALIASED_COMPONENT (type))
7421 target = copy_rtx (target);
7422 MEM_KEEP_ALIAS_SET_P (target) = 1;
7424 store_constructor_field (target, bitsize, bitpos, 0,
7425 bitregion_end, mode, value,
7426 cleared, get_alias_set (elttype),
7427 reverse);
7430 break;
7433 case VECTOR_TYPE:
7435 unsigned HOST_WIDE_INT idx;
7436 constructor_elt *ce;
7437 int i;
7438 bool need_to_clear;
7439 insn_code icode = CODE_FOR_nothing;
7440 tree elt;
7441 tree elttype = TREE_TYPE (type);
7442 int elt_size = vector_element_bits (type);
7443 machine_mode eltmode = TYPE_MODE (elttype);
7444 HOST_WIDE_INT bitsize;
7445 HOST_WIDE_INT bitpos;
7446 rtvec vector = NULL;
7447 poly_uint64 n_elts;
7448 unsigned HOST_WIDE_INT const_n_elts;
7449 alias_set_type alias;
7450 bool vec_vec_init_p = false;
7451 machine_mode mode = GET_MODE (target);
7453 gcc_assert (eltmode != BLKmode);
7455 /* Try using vec_duplicate_optab for uniform vectors. */
7456 if (!TREE_SIDE_EFFECTS (exp)
7457 && VECTOR_MODE_P (mode)
7458 && eltmode == GET_MODE_INNER (mode)
7459 && ((icode = optab_handler (vec_duplicate_optab, mode))
7460 != CODE_FOR_nothing)
7461 && (elt = uniform_vector_p (exp))
7462 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7464 class expand_operand ops[2];
7465 create_output_operand (&ops[0], target, mode);
7466 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7467 expand_insn (icode, 2, ops);
7468 if (!rtx_equal_p (target, ops[0].value))
7469 emit_move_insn (target, ops[0].value);
7470 break;
7472 /* Use sign-extension for uniform boolean vectors with
7473 integer modes. Effectively "vec_duplicate" for bitmasks. */
7474 if (!TREE_SIDE_EFFECTS (exp)
7475 && VECTOR_BOOLEAN_TYPE_P (type)
7476 && SCALAR_INT_MODE_P (mode)
7477 && (elt = uniform_vector_p (exp))
7478 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7480 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
7481 expand_normal (elt));
7482 rtx tmp = gen_reg_rtx (mode);
7483 convert_move (tmp, op0, 0);
7485 /* Ensure no excess bits are set.
7486 GCN needs this for nunits < 64.
7487 x86 needs this for nunits < 8. */
7488 auto nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
7489 if (maybe_ne (GET_MODE_PRECISION (mode), nunits))
7490 tmp = expand_binop (mode, and_optab, tmp,
7491 GEN_INT ((1 << nunits) - 1), target,
7492 true, OPTAB_WIDEN);
7493 if (tmp != target)
7494 emit_move_insn (target, tmp);
7495 break;
7498 n_elts = TYPE_VECTOR_SUBPARTS (type);
7499 if (REG_P (target)
7500 && VECTOR_MODE_P (mode)
7501 && n_elts.is_constant (&const_n_elts))
7503 machine_mode emode = eltmode;
7504 bool vector_typed_elts_p = false;
7506 if (CONSTRUCTOR_NELTS (exp)
7507 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7508 == VECTOR_TYPE))
7510 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7511 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7512 * TYPE_VECTOR_SUBPARTS (etype),
7513 n_elts));
7514 emode = TYPE_MODE (etype);
7515 vector_typed_elts_p = true;
7517 icode = convert_optab_handler (vec_init_optab, mode, emode);
7518 if (icode != CODE_FOR_nothing)
7520 unsigned int n = const_n_elts;
7522 if (vector_typed_elts_p)
7524 n = CONSTRUCTOR_NELTS (exp);
7525 vec_vec_init_p = true;
7527 vector = rtvec_alloc (n);
7528 for (unsigned int k = 0; k < n; k++)
7529 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7533 /* Compute the size of the elements in the CTOR. It differs
7534 from the size of the vector type elements only when the
7535 CTOR elements are vectors themselves. */
7536 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7537 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7538 : elttype);
7539 if (VECTOR_TYPE_P (val_type))
7540 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7541 else
7542 bitsize = elt_size;
7544 /* If the constructor has fewer elements than the vector,
7545 clear the whole array first. Similarly if this is static
7546 constructor of a non-BLKmode object. */
7547 if (cleared)
7548 need_to_clear = false;
7549 else if (REG_P (target) && TREE_STATIC (exp))
7550 need_to_clear = true;
7551 else
7553 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7554 tree value;
7556 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7558 int n_elts_here = bitsize / elt_size;
7559 count += n_elts_here;
7560 if (mostly_zeros_p (value))
7561 zero_count += n_elts_here;
7564 /* Clear the entire vector first if there are any missing elements,
7565 or if the incidence of zero elements is >= 75%. */
7566 need_to_clear = (maybe_lt (count, n_elts)
7567 || 4 * zero_count >= 3 * count);
7570 if (need_to_clear && maybe_gt (size, 0) && !vector)
7572 if (REG_P (target))
7573 emit_move_insn (target, CONST0_RTX (mode));
7574 else
7575 clear_storage (target, gen_int_mode (size, Pmode),
7576 BLOCK_OP_NORMAL);
7577 cleared = 1;
7580 /* Inform later passes that the old value is dead. */
7581 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
7583 emit_move_insn (target, CONST0_RTX (mode));
7584 cleared = 1;
7587 if (MEM_P (target))
7588 alias = MEM_ALIAS_SET (target);
7589 else
7590 alias = get_alias_set (elttype);
7592 /* Store each element of the constructor into the corresponding
7593 element of TARGET, determined by counting the elements. */
7594 for (idx = 0, i = 0;
7595 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7596 idx++, i += bitsize / elt_size)
7598 HOST_WIDE_INT eltpos;
7599 tree value = ce->value;
7601 if (cleared && initializer_zerop (value))
7602 continue;
7604 if (ce->index)
7605 eltpos = tree_to_uhwi (ce->index);
7606 else
7607 eltpos = i;
7609 if (vector)
7611 if (vec_vec_init_p)
7613 gcc_assert (ce->index == NULL_TREE);
7614 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7615 eltpos = idx;
7617 else
7618 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7619 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7621 else
7623 machine_mode value_mode
7624 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7625 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7626 bitpos = eltpos * elt_size;
7627 store_constructor_field (target, bitsize, bitpos, 0,
7628 bitregion_end, value_mode,
7629 value, cleared, alias, reverse);
7633 if (vector)
7634 emit_insn (GEN_FCN (icode) (target,
7635 gen_rtx_PARALLEL (mode, vector)));
7636 break;
7639 default:
7640 gcc_unreachable ();
7644 /* Store the value of EXP (an expression tree)
7645 into a subfield of TARGET which has mode MODE and occupies
7646 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7647 If MODE is VOIDmode, it means that we are storing into a bit-field.
7649 BITREGION_START is bitpos of the first bitfield in this region.
7650 BITREGION_END is the bitpos of the ending bitfield in this region.
7651 These two fields are 0, if the C++ memory model does not apply,
7652 or we are not interested in keeping track of bitfield regions.
7654 Always return const0_rtx unless we have something particular to
7655 return.
7657 ALIAS_SET is the alias set for the destination. This value will
7658 (in general) be different from that for TARGET, since TARGET is a
7659 reference to the containing structure.
7661 If NONTEMPORAL is true, try generating a nontemporal store.
7663 If REVERSE is true, the store is to be done in reverse order. */
7665 static rtx
7666 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7667 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7668 machine_mode mode, tree exp,
7669 alias_set_type alias_set, bool nontemporal, bool reverse)
7671 if (TREE_CODE (exp) == ERROR_MARK)
7672 return const0_rtx;
7674 /* If we have nothing to store, do nothing unless the expression has
7675 side-effects. Don't do that for zero sized addressable lhs of
7676 calls. */
7677 if (known_eq (bitsize, 0)
7678 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7679 || TREE_CODE (exp) != CALL_EXPR))
7680 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7682 if (GET_CODE (target) == CONCAT)
7684 /* We're storing into a struct containing a single __complex. */
7686 gcc_assert (known_eq (bitpos, 0));
7687 return store_expr (exp, target, 0, nontemporal, reverse);
7690 /* If the structure is in a register or if the component
7691 is a bit field, we cannot use addressing to access it.
7692 Use bit-field techniques or SUBREG to store in it. */
7694 poly_int64 decl_bitsize;
7695 if (mode == VOIDmode
7696 || (mode != BLKmode && ! direct_store[(int) mode]
7697 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7698 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7699 || REG_P (target)
7700 || GET_CODE (target) == SUBREG
7701 /* If the field isn't aligned enough to store as an ordinary memref,
7702 store it as a bit field. */
7703 || (mode != BLKmode
7704 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7705 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7706 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7707 || !multiple_p (bitpos, BITS_PER_UNIT)))
7708 || (known_size_p (bitsize)
7709 && mode != BLKmode
7710 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7711 /* If the RHS and field are a constant size and the size of the
7712 RHS isn't the same size as the bitfield, we must use bitfield
7713 operations. */
7714 || (known_size_p (bitsize)
7715 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7716 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7717 bitsize)
7718 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7719 we will handle specially below. */
7720 && !(TREE_CODE (exp) == CONSTRUCTOR
7721 && multiple_p (bitsize, BITS_PER_UNIT))
7722 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7723 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7724 includes some extra padding. store_expr / expand_expr will in
7725 that case call get_inner_reference that will have the bitsize
7726 we check here and thus the block move will not clobber the
7727 padding that shouldn't be clobbered. In the future we could
7728 replace the TREE_ADDRESSABLE check with a check that
7729 get_base_address needs to live in memory. */
7730 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7731 || TREE_CODE (exp) != COMPONENT_REF
7732 || !multiple_p (bitsize, BITS_PER_UNIT)
7733 || !multiple_p (bitpos, BITS_PER_UNIT)
7734 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7735 &decl_bitsize)
7736 || maybe_ne (decl_bitsize, bitsize))
7737 /* A call with an addressable return type and return-slot
7738 optimization must not need bitfield operations but we must
7739 pass down the original target. */
7740 && (TREE_CODE (exp) != CALL_EXPR
7741 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7742 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7743 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7744 decl we must use bitfield operations. */
7745 || (known_size_p (bitsize)
7746 && TREE_CODE (exp) == MEM_REF
7747 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7748 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7749 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7750 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7752 rtx temp;
7753 gimple *nop_def;
7755 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7756 implies a mask operation. If the precision is the same size as
7757 the field we're storing into, that mask is redundant. This is
7758 particularly common with bit field assignments generated by the
7759 C front end. */
7760 nop_def = get_def_for_expr (exp, NOP_EXPR);
7761 if (nop_def)
7763 tree type = TREE_TYPE (exp);
7764 if (INTEGRAL_TYPE_P (type)
7765 && maybe_ne (TYPE_PRECISION (type),
7766 GET_MODE_BITSIZE (TYPE_MODE (type)))
7767 && known_eq (bitsize, TYPE_PRECISION (type)))
7769 tree op = gimple_assign_rhs1 (nop_def);
7770 type = TREE_TYPE (op);
7771 if (INTEGRAL_TYPE_P (type)
7772 && known_ge (TYPE_PRECISION (type), bitsize))
7773 exp = op;
7777 temp = expand_normal (exp);
7779 /* We don't support variable-sized BLKmode bitfields, since our
7780 handling of BLKmode is bound up with the ability to break
7781 things into words. */
7782 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7784 /* Handle calls that return values in multiple non-contiguous locations.
7785 The Irix 6 ABI has examples of this. */
7786 if (GET_CODE (temp) == PARALLEL)
7788 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7789 machine_mode temp_mode = GET_MODE (temp);
7790 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7791 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7792 rtx temp_target = gen_reg_rtx (temp_mode);
7793 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7794 temp = temp_target;
7797 /* Handle calls that return BLKmode values in registers. */
7798 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7800 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7801 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7802 temp = temp_target;
7805 /* If the value has aggregate type and an integral mode then, if BITSIZE
7806 is narrower than this mode and this is for big-endian data, we first
7807 need to put the value into the low-order bits for store_bit_field,
7808 except when MODE is BLKmode and BITSIZE larger than the word size
7809 (see the handling of fields larger than a word in store_bit_field).
7810 Moreover, the field may be not aligned on a byte boundary; in this
7811 case, if it has reverse storage order, it needs to be accessed as a
7812 scalar field with reverse storage order and we must first put the
7813 value into target order. */
7814 scalar_int_mode temp_mode;
7815 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7816 && is_int_mode (GET_MODE (temp), &temp_mode))
7818 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7820 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7822 if (reverse)
7823 temp = flip_storage_order (temp_mode, temp);
7825 gcc_checking_assert (known_le (bitsize, size));
7826 if (maybe_lt (bitsize, size)
7827 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7828 /* Use of to_constant for BLKmode was checked above. */
7829 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7830 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7831 size - bitsize, NULL_RTX, 1);
7834 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7835 if (mode != VOIDmode && mode != BLKmode
7836 && mode != TYPE_MODE (TREE_TYPE (exp)))
7837 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7839 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7840 and BITPOS must be aligned on a byte boundary. If so, we simply do
7841 a block copy. Likewise for a BLKmode-like TARGET. */
7842 if (GET_MODE (temp) == BLKmode
7843 && (GET_MODE (target) == BLKmode
7844 || (MEM_P (target)
7845 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7846 && multiple_p (bitpos, BITS_PER_UNIT)
7847 && multiple_p (bitsize, BITS_PER_UNIT))))
7849 gcc_assert (MEM_P (target) && MEM_P (temp));
7850 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7851 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7853 target = adjust_address (target, VOIDmode, bytepos);
7854 emit_block_move (target, temp,
7855 gen_int_mode (bytesize, Pmode),
7856 BLOCK_OP_NORMAL);
7858 return const0_rtx;
7861 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7862 word size, we need to load the value (see again store_bit_field). */
7863 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7865 temp_mode = smallest_int_mode_for_size (bitsize);
7866 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7867 temp_mode, false, NULL);
7870 /* Store the value in the bitfield. */
7871 gcc_checking_assert (known_ge (bitpos, 0));
7872 store_bit_field (target, bitsize, bitpos,
7873 bitregion_start, bitregion_end,
7874 mode, temp, reverse, false);
7876 return const0_rtx;
7878 else
7880 /* Now build a reference to just the desired component. */
7881 rtx to_rtx = adjust_address (target, mode,
7882 exact_div (bitpos, BITS_PER_UNIT));
7884 if (to_rtx == target)
7885 to_rtx = copy_rtx (to_rtx);
7887 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7888 set_mem_alias_set (to_rtx, alias_set);
7890 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7891 into a target smaller than its type; handle that case now. */
7892 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7894 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7895 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7896 return to_rtx;
7899 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7903 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7904 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7905 codes and find the ultimate containing object, which we return.
7907 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7908 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7909 storage order of the field.
7910 If the position of the field is variable, we store a tree
7911 giving the variable offset (in units) in *POFFSET.
7912 This offset is in addition to the bit position.
7913 If the position is not variable, we store 0 in *POFFSET.
7915 If any of the extraction expressions is volatile,
7916 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7918 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7919 Otherwise, it is a mode that can be used to access the field.
7921 If the field describes a variable-sized object, *PMODE is set to
7922 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7923 this case, but the address of the object can be found. */
7925 tree
7926 get_inner_reference (tree exp, poly_int64 *pbitsize,
7927 poly_int64 *pbitpos, tree *poffset,
7928 machine_mode *pmode, int *punsignedp,
7929 int *preversep, int *pvolatilep)
7931 tree size_tree = 0;
7932 machine_mode mode = VOIDmode;
7933 bool blkmode_bitfield = false;
7934 tree offset = size_zero_node;
7935 poly_offset_int bit_offset = 0;
7937 /* First get the mode, signedness, storage order and size. We do this from
7938 just the outermost expression. */
7939 *pbitsize = -1;
7940 if (TREE_CODE (exp) == COMPONENT_REF)
7942 tree field = TREE_OPERAND (exp, 1);
7943 size_tree = DECL_SIZE (field);
7944 if (flag_strict_volatile_bitfields > 0
7945 && TREE_THIS_VOLATILE (exp)
7946 && DECL_BIT_FIELD_TYPE (field)
7947 && DECL_MODE (field) != BLKmode)
7948 /* Volatile bitfields should be accessed in the mode of the
7949 field's type, not the mode computed based on the bit
7950 size. */
7951 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7952 else if (!DECL_BIT_FIELD (field))
7954 mode = DECL_MODE (field);
7955 /* For vector fields re-check the target flags, as DECL_MODE
7956 could have been set with different target flags than
7957 the current function has. */
7958 if (VECTOR_TYPE_P (TREE_TYPE (field))
7959 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7960 mode = TYPE_MODE (TREE_TYPE (field));
7962 else if (DECL_MODE (field) == BLKmode)
7963 blkmode_bitfield = true;
7965 *punsignedp = DECL_UNSIGNED (field);
7967 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7969 size_tree = TREE_OPERAND (exp, 1);
7970 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7971 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7973 /* For vector element types with the correct size of access or for
7974 vector typed accesses use the mode of the access type. */
7975 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7976 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7977 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7978 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7979 mode = TYPE_MODE (TREE_TYPE (exp));
7981 else
7983 mode = TYPE_MODE (TREE_TYPE (exp));
7984 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7986 if (mode == BLKmode)
7987 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7988 else
7989 *pbitsize = GET_MODE_BITSIZE (mode);
7992 if (size_tree != 0)
7994 if (! tree_fits_uhwi_p (size_tree))
7995 mode = BLKmode, *pbitsize = -1;
7996 else
7997 *pbitsize = tree_to_uhwi (size_tree);
8000 *preversep = reverse_storage_order_for_component_p (exp);
8002 /* Compute cumulative bit-offset for nested component-refs and array-refs,
8003 and find the ultimate containing object. */
8004 while (1)
8006 switch (TREE_CODE (exp))
8008 case BIT_FIELD_REF:
8009 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
8010 break;
8012 case COMPONENT_REF:
8014 tree field = TREE_OPERAND (exp, 1);
8015 tree this_offset = component_ref_field_offset (exp);
8017 /* If this field hasn't been filled in yet, don't go past it.
8018 This should only happen when folding expressions made during
8019 type construction. */
8020 if (this_offset == 0)
8021 break;
8023 offset = size_binop (PLUS_EXPR, offset, this_offset);
8024 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
8026 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
8028 break;
8030 case ARRAY_REF:
8031 case ARRAY_RANGE_REF:
8033 tree index = TREE_OPERAND (exp, 1);
8034 tree low_bound = array_ref_low_bound (exp);
8035 tree unit_size = array_ref_element_size (exp);
8037 /* We assume all arrays have sizes that are a multiple of a byte.
8038 First subtract the lower bound, if any, in the type of the
8039 index, then convert to sizetype and multiply by the size of
8040 the array element. */
8041 if (! integer_zerop (low_bound))
8042 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8043 index, low_bound);
8045 offset = size_binop (PLUS_EXPR, offset,
8046 size_binop (MULT_EXPR,
8047 fold_convert (sizetype, index),
8048 unit_size));
8050 break;
8052 case REALPART_EXPR:
8053 break;
8055 case IMAGPART_EXPR:
8056 bit_offset += *pbitsize;
8057 break;
8059 case VIEW_CONVERT_EXPR:
8060 break;
8062 case MEM_REF:
8063 /* Hand back the decl for MEM[&decl, off]. */
8064 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8066 tree off = TREE_OPERAND (exp, 1);
8067 if (!integer_zerop (off))
8069 poly_offset_int boff = mem_ref_offset (exp);
8070 boff <<= LOG2_BITS_PER_UNIT;
8071 bit_offset += boff;
8073 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8075 goto done;
8077 default:
8078 goto done;
8081 /* If any reference in the chain is volatile, the effect is volatile. */
8082 if (TREE_THIS_VOLATILE (exp))
8083 *pvolatilep = 1;
8085 exp = TREE_OPERAND (exp, 0);
8087 done:
8089 /* If OFFSET is constant, see if we can return the whole thing as a
8090 constant bit position. Make sure to handle overflow during
8091 this conversion. */
8092 if (poly_int_tree_p (offset))
8094 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8095 TYPE_PRECISION (sizetype));
8096 tem <<= LOG2_BITS_PER_UNIT;
8097 tem += bit_offset;
8098 if (tem.to_shwi (pbitpos))
8099 *poffset = offset = NULL_TREE;
8102 /* Otherwise, split it up. */
8103 if (offset)
8105 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8106 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8108 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8109 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8110 offset = size_binop (PLUS_EXPR, offset,
8111 build_int_cst (sizetype, bytes.force_shwi ()));
8114 *poffset = offset;
8117 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8118 if (mode == VOIDmode
8119 && blkmode_bitfield
8120 && multiple_p (*pbitpos, BITS_PER_UNIT)
8121 && multiple_p (*pbitsize, BITS_PER_UNIT))
8122 *pmode = BLKmode;
8123 else
8124 *pmode = mode;
8126 return exp;
8129 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8131 static unsigned HOST_WIDE_INT
8132 target_align (const_tree target)
8134 /* We might have a chain of nested references with intermediate misaligning
8135 bitfields components, so need to recurse to find out. */
8137 unsigned HOST_WIDE_INT this_align, outer_align;
8139 switch (TREE_CODE (target))
8141 case BIT_FIELD_REF:
8142 return 1;
8144 case COMPONENT_REF:
8145 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8146 outer_align = target_align (TREE_OPERAND (target, 0));
8147 return MIN (this_align, outer_align);
8149 case ARRAY_REF:
8150 case ARRAY_RANGE_REF:
8151 this_align = TYPE_ALIGN (TREE_TYPE (target));
8152 outer_align = target_align (TREE_OPERAND (target, 0));
8153 return MIN (this_align, outer_align);
8155 CASE_CONVERT:
8156 case NON_LVALUE_EXPR:
8157 case VIEW_CONVERT_EXPR:
8158 this_align = TYPE_ALIGN (TREE_TYPE (target));
8159 outer_align = target_align (TREE_OPERAND (target, 0));
8160 return MAX (this_align, outer_align);
8162 default:
8163 return TYPE_ALIGN (TREE_TYPE (target));
8168 /* Given an rtx VALUE that may contain additions and multiplications, return
8169 an equivalent value that just refers to a register, memory, or constant.
8170 This is done by generating instructions to perform the arithmetic and
8171 returning a pseudo-register containing the value.
8173 The returned value may be a REG, SUBREG, MEM or constant. */
8176 force_operand (rtx value, rtx target)
8178 rtx op1, op2;
8179 /* Use subtarget as the target for operand 0 of a binary operation. */
8180 rtx subtarget = get_subtarget (target);
8181 enum rtx_code code = GET_CODE (value);
8183 /* Check for subreg applied to an expression produced by loop optimizer. */
8184 if (code == SUBREG
8185 && !REG_P (SUBREG_REG (value))
8186 && !MEM_P (SUBREG_REG (value)))
8188 value
8189 = simplify_gen_subreg (GET_MODE (value),
8190 force_reg (GET_MODE (SUBREG_REG (value)),
8191 force_operand (SUBREG_REG (value),
8192 NULL_RTX)),
8193 GET_MODE (SUBREG_REG (value)),
8194 SUBREG_BYTE (value));
8195 code = GET_CODE (value);
8198 /* Check for a PIC address load. */
8199 if ((code == PLUS || code == MINUS)
8200 && XEXP (value, 0) == pic_offset_table_rtx
8201 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8202 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8203 || GET_CODE (XEXP (value, 1)) == CONST))
8205 if (!subtarget)
8206 subtarget = gen_reg_rtx (GET_MODE (value));
8207 emit_move_insn (subtarget, value);
8208 return subtarget;
8211 if (ARITHMETIC_P (value))
8213 op2 = XEXP (value, 1);
8214 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8215 subtarget = 0;
8216 if (code == MINUS && CONST_INT_P (op2))
8218 code = PLUS;
8219 op2 = negate_rtx (GET_MODE (value), op2);
8222 /* Check for an addition with OP2 a constant integer and our first
8223 operand a PLUS of a virtual register and something else. In that
8224 case, we want to emit the sum of the virtual register and the
8225 constant first and then add the other value. This allows virtual
8226 register instantiation to simply modify the constant rather than
8227 creating another one around this addition. */
8228 if (code == PLUS && CONST_INT_P (op2)
8229 && GET_CODE (XEXP (value, 0)) == PLUS
8230 && REG_P (XEXP (XEXP (value, 0), 0))
8231 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8233 rtx temp = expand_simple_binop (GET_MODE (value), code,
8234 XEXP (XEXP (value, 0), 0), op2,
8235 subtarget, 0, OPTAB_LIB_WIDEN);
8236 return expand_simple_binop (GET_MODE (value), code, temp,
8237 force_operand (XEXP (XEXP (value,
8238 0), 1), 0),
8239 target, 0, OPTAB_LIB_WIDEN);
8242 op1 = force_operand (XEXP (value, 0), subtarget);
8243 op2 = force_operand (op2, NULL_RTX);
8244 switch (code)
8246 case MULT:
8247 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8248 case DIV:
8249 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8250 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8251 target, 1, OPTAB_LIB_WIDEN);
8252 else
8253 return expand_divmod (0,
8254 FLOAT_MODE_P (GET_MODE (value))
8255 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8256 GET_MODE (value), op1, op2, target, 0);
8257 case MOD:
8258 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8259 target, 0);
8260 case UDIV:
8261 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8262 target, 1);
8263 case UMOD:
8264 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8265 target, 1);
8266 case ASHIFTRT:
8267 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8268 target, 0, OPTAB_LIB_WIDEN);
8269 default:
8270 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8271 target, 1, OPTAB_LIB_WIDEN);
8274 if (UNARY_P (value))
8276 if (!target)
8277 target = gen_reg_rtx (GET_MODE (value));
8278 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8279 switch (code)
8281 case ZERO_EXTEND:
8282 case SIGN_EXTEND:
8283 case TRUNCATE:
8284 case FLOAT_EXTEND:
8285 case FLOAT_TRUNCATE:
8286 convert_move (target, op1, code == ZERO_EXTEND);
8287 return target;
8289 case FIX:
8290 case UNSIGNED_FIX:
8291 expand_fix (target, op1, code == UNSIGNED_FIX);
8292 return target;
8294 case FLOAT:
8295 case UNSIGNED_FLOAT:
8296 expand_float (target, op1, code == UNSIGNED_FLOAT);
8297 return target;
8299 default:
8300 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8304 #ifdef INSN_SCHEDULING
8305 /* On machines that have insn scheduling, we want all memory reference to be
8306 explicit, so we need to deal with such paradoxical SUBREGs. */
8307 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8308 value
8309 = simplify_gen_subreg (GET_MODE (value),
8310 force_reg (GET_MODE (SUBREG_REG (value)),
8311 force_operand (SUBREG_REG (value),
8312 NULL_RTX)),
8313 GET_MODE (SUBREG_REG (value)),
8314 SUBREG_BYTE (value));
8315 #endif
8317 return value;
8320 /* Subroutine of expand_expr: return true iff there is no way that
8321 EXP can reference X, which is being modified. TOP_P is nonzero if this
8322 call is going to be used to determine whether we need a temporary
8323 for EXP, as opposed to a recursive call to this function.
8325 It is always safe for this routine to return false since it merely
8326 searches for optimization opportunities. */
8328 bool
8329 safe_from_p (const_rtx x, tree exp, int top_p)
8331 rtx exp_rtl = 0;
8332 int i, nops;
8334 if (x == 0
8335 /* If EXP has varying size, we MUST use a target since we currently
8336 have no way of allocating temporaries of variable size
8337 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8338 So we assume here that something at a higher level has prevented a
8339 clash. This is somewhat bogus, but the best we can do. Only
8340 do this when X is BLKmode and when we are at the top level. */
8341 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8342 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8343 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8344 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8345 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8346 != INTEGER_CST)
8347 && GET_MODE (x) == BLKmode)
8348 /* If X is in the outgoing argument area, it is always safe. */
8349 || (MEM_P (x)
8350 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8351 || (GET_CODE (XEXP (x, 0)) == PLUS
8352 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8353 return true;
8355 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8356 find the underlying pseudo. */
8357 if (GET_CODE (x) == SUBREG)
8359 x = SUBREG_REG (x);
8360 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8361 return false;
8364 /* Now look at our tree code and possibly recurse. */
8365 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8367 case tcc_declaration:
8368 exp_rtl = DECL_RTL_IF_SET (exp);
8369 break;
8371 case tcc_constant:
8372 return true;
8374 case tcc_exceptional:
8375 if (TREE_CODE (exp) == TREE_LIST)
8377 while (1)
8379 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8380 return false;
8381 exp = TREE_CHAIN (exp);
8382 if (!exp)
8383 return true;
8384 if (TREE_CODE (exp) != TREE_LIST)
8385 return safe_from_p (x, exp, 0);
8388 else if (TREE_CODE (exp) == CONSTRUCTOR)
8390 constructor_elt *ce;
8391 unsigned HOST_WIDE_INT idx;
8393 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8394 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8395 || !safe_from_p (x, ce->value, 0))
8396 return false;
8397 return true;
8399 else if (TREE_CODE (exp) == ERROR_MARK)
8400 return true; /* An already-visited SAVE_EXPR? */
8401 else
8402 return false;
8404 case tcc_statement:
8405 /* The only case we look at here is the DECL_INITIAL inside a
8406 DECL_EXPR. */
8407 return (TREE_CODE (exp) != DECL_EXPR
8408 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8409 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8410 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8412 case tcc_binary:
8413 case tcc_comparison:
8414 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8415 return false;
8416 /* Fall through. */
8418 case tcc_unary:
8419 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8421 case tcc_expression:
8422 case tcc_reference:
8423 case tcc_vl_exp:
8424 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8425 the expression. If it is set, we conflict iff we are that rtx or
8426 both are in memory. Otherwise, we check all operands of the
8427 expression recursively. */
8429 switch (TREE_CODE (exp))
8431 case ADDR_EXPR:
8432 /* If the operand is static or we are static, we can't conflict.
8433 Likewise if we don't conflict with the operand at all. */
8434 if (staticp (TREE_OPERAND (exp, 0))
8435 || TREE_STATIC (exp)
8436 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8437 return true;
8439 /* Otherwise, the only way this can conflict is if we are taking
8440 the address of a DECL a that address if part of X, which is
8441 very rare. */
8442 exp = TREE_OPERAND (exp, 0);
8443 if (DECL_P (exp))
8445 if (!DECL_RTL_SET_P (exp)
8446 || !MEM_P (DECL_RTL (exp)))
8447 return false;
8448 else
8449 exp_rtl = XEXP (DECL_RTL (exp), 0);
8451 break;
8453 case MEM_REF:
8454 if (MEM_P (x)
8455 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8456 get_alias_set (exp)))
8457 return false;
8458 break;
8460 case CALL_EXPR:
8461 /* Assume that the call will clobber all hard registers and
8462 all of memory. */
8463 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8464 || MEM_P (x))
8465 return false;
8466 break;
8468 case WITH_CLEANUP_EXPR:
8469 case CLEANUP_POINT_EXPR:
8470 /* Lowered by gimplify.cc. */
8471 gcc_unreachable ();
8473 case SAVE_EXPR:
8474 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8476 default:
8477 break;
8480 /* If we have an rtx, we do not need to scan our operands. */
8481 if (exp_rtl)
8482 break;
8484 nops = TREE_OPERAND_LENGTH (exp);
8485 for (i = 0; i < nops; i++)
8486 if (TREE_OPERAND (exp, i) != 0
8487 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8488 return false;
8490 break;
8492 case tcc_type:
8493 /* Should never get a type here. */
8494 gcc_unreachable ();
8497 /* If we have an rtl, find any enclosed object. Then see if we conflict
8498 with it. */
8499 if (exp_rtl)
8501 if (GET_CODE (exp_rtl) == SUBREG)
8503 exp_rtl = SUBREG_REG (exp_rtl);
8504 if (REG_P (exp_rtl)
8505 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8506 return false;
8509 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8510 are memory and they conflict. */
8511 return ! (rtx_equal_p (x, exp_rtl)
8512 || (MEM_P (x) && MEM_P (exp_rtl)
8513 && true_dependence (exp_rtl, VOIDmode, x)));
8516 /* If we reach here, it is safe. */
8517 return true;
8521 /* Return the highest power of two that EXP is known to be a multiple of.
8522 This is used in updating alignment of MEMs in array references. */
8524 unsigned HOST_WIDE_INT
8525 highest_pow2_factor (const_tree exp)
8527 unsigned HOST_WIDE_INT ret;
8528 int trailing_zeros = tree_ctz (exp);
8529 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8530 return BIGGEST_ALIGNMENT;
8531 ret = HOST_WIDE_INT_1U << trailing_zeros;
8532 if (ret > BIGGEST_ALIGNMENT)
8533 return BIGGEST_ALIGNMENT;
8534 return ret;
8537 /* Similar, except that the alignment requirements of TARGET are
8538 taken into account. Assume it is at least as aligned as its
8539 type, unless it is a COMPONENT_REF in which case the layout of
8540 the structure gives the alignment. */
8542 static unsigned HOST_WIDE_INT
8543 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8545 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8546 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8548 return MAX (factor, talign);
8551 /* Convert the tree comparison code TCODE to the rtl one where the
8552 signedness is UNSIGNEDP. */
8554 static enum rtx_code
8555 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8557 enum rtx_code code;
8558 switch (tcode)
8560 case EQ_EXPR:
8561 code = EQ;
8562 break;
8563 case NE_EXPR:
8564 code = NE;
8565 break;
8566 case LT_EXPR:
8567 code = unsignedp ? LTU : LT;
8568 break;
8569 case LE_EXPR:
8570 code = unsignedp ? LEU : LE;
8571 break;
8572 case GT_EXPR:
8573 code = unsignedp ? GTU : GT;
8574 break;
8575 case GE_EXPR:
8576 code = unsignedp ? GEU : GE;
8577 break;
8578 case UNORDERED_EXPR:
8579 code = UNORDERED;
8580 break;
8581 case ORDERED_EXPR:
8582 code = ORDERED;
8583 break;
8584 case UNLT_EXPR:
8585 code = UNLT;
8586 break;
8587 case UNLE_EXPR:
8588 code = UNLE;
8589 break;
8590 case UNGT_EXPR:
8591 code = UNGT;
8592 break;
8593 case UNGE_EXPR:
8594 code = UNGE;
8595 break;
8596 case UNEQ_EXPR:
8597 code = UNEQ;
8598 break;
8599 case LTGT_EXPR:
8600 code = LTGT;
8601 break;
8603 default:
8604 gcc_unreachable ();
8606 return code;
8609 /* Subroutine of expand_expr. Expand the two operands of a binary
8610 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8611 The value may be stored in TARGET if TARGET is nonzero. The
8612 MODIFIER argument is as documented by expand_expr. */
8614 void
8615 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8616 enum expand_modifier modifier)
8618 if (! safe_from_p (target, exp1, 1))
8619 target = 0;
8620 if (operand_equal_p (exp0, exp1, 0))
8622 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8623 *op1 = copy_rtx (*op0);
8625 else
8627 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8628 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8633 /* Return a MEM that contains constant EXP. DEFER is as for
8634 output_constant_def and MODIFIER is as for expand_expr. */
8636 static rtx
8637 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8639 rtx mem;
8641 mem = output_constant_def (exp, defer);
8642 if (modifier != EXPAND_INITIALIZER)
8643 mem = use_anchored_address (mem);
8644 return mem;
8647 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8648 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8650 static rtx
8651 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8652 enum expand_modifier modifier, addr_space_t as)
8654 rtx result, subtarget;
8655 tree inner, offset;
8656 poly_int64 bitsize, bitpos;
8657 int unsignedp, reversep, volatilep = 0;
8658 machine_mode mode1;
8660 /* If we are taking the address of a constant and are at the top level,
8661 we have to use output_constant_def since we can't call force_const_mem
8662 at top level. */
8663 /* ??? This should be considered a front-end bug. We should not be
8664 generating ADDR_EXPR of something that isn't an LVALUE. The only
8665 exception here is STRING_CST. */
8666 if (CONSTANT_CLASS_P (exp))
8668 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8669 if (modifier < EXPAND_SUM)
8670 result = force_operand (result, target);
8671 return result;
8674 /* Everything must be something allowed by is_gimple_addressable. */
8675 switch (TREE_CODE (exp))
8677 case INDIRECT_REF:
8678 /* This case will happen via recursion for &a->b. */
8679 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8681 case MEM_REF:
8683 tree tem = TREE_OPERAND (exp, 0);
8684 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8685 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8686 return expand_expr (tem, target, tmode, modifier);
8689 case TARGET_MEM_REF:
8690 return addr_for_mem_ref (exp, as, true);
8692 case CONST_DECL:
8693 /* Expand the initializer like constants above. */
8694 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8695 0, modifier), 0);
8696 if (modifier < EXPAND_SUM)
8697 result = force_operand (result, target);
8698 return result;
8700 case REALPART_EXPR:
8701 /* The real part of the complex number is always first, therefore
8702 the address is the same as the address of the parent object. */
8703 offset = 0;
8704 bitpos = 0;
8705 inner = TREE_OPERAND (exp, 0);
8706 break;
8708 case IMAGPART_EXPR:
8709 /* The imaginary part of the complex number is always second.
8710 The expression is therefore always offset by the size of the
8711 scalar type. */
8712 offset = 0;
8713 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8714 inner = TREE_OPERAND (exp, 0);
8715 break;
8717 case COMPOUND_LITERAL_EXPR:
8718 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8719 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8720 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8721 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8722 the initializers aren't gimplified. */
8723 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8724 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8725 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8726 target, tmode, modifier, as);
8727 /* FALLTHRU */
8728 default:
8729 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8730 expand_expr, as that can have various side effects; LABEL_DECLs for
8731 example, may not have their DECL_RTL set yet. Expand the rtl of
8732 CONSTRUCTORs too, which should yield a memory reference for the
8733 constructor's contents. Assume language specific tree nodes can
8734 be expanded in some interesting way. */
8735 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8736 if (DECL_P (exp)
8737 || TREE_CODE (exp) == CONSTRUCTOR
8738 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8740 result = expand_expr (exp, target, tmode,
8741 modifier == EXPAND_INITIALIZER
8742 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8744 /* If the DECL isn't in memory, then the DECL wasn't properly
8745 marked TREE_ADDRESSABLE, which will be either a front-end
8746 or a tree optimizer bug. */
8748 gcc_assert (MEM_P (result));
8749 result = XEXP (result, 0);
8751 /* ??? Is this needed anymore? */
8752 if (DECL_P (exp))
8753 TREE_USED (exp) = 1;
8755 if (modifier != EXPAND_INITIALIZER
8756 && modifier != EXPAND_CONST_ADDRESS
8757 && modifier != EXPAND_SUM)
8758 result = force_operand (result, target);
8759 return result;
8762 /* Pass FALSE as the last argument to get_inner_reference although
8763 we are expanding to RTL. The rationale is that we know how to
8764 handle "aligning nodes" here: we can just bypass them because
8765 they won't change the final object whose address will be returned
8766 (they actually exist only for that purpose). */
8767 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8768 &unsignedp, &reversep, &volatilep);
8769 break;
8772 /* We must have made progress. */
8773 gcc_assert (inner != exp);
8775 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8776 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8777 inner alignment, force the inner to be sufficiently aligned. */
8778 if (CONSTANT_CLASS_P (inner)
8779 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8781 inner = copy_node (inner);
8782 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8783 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8784 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8786 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8788 if (offset)
8790 rtx tmp;
8792 if (modifier != EXPAND_NORMAL)
8793 result = force_operand (result, NULL);
8794 tmp = expand_expr (offset, NULL_RTX, tmode,
8795 modifier == EXPAND_INITIALIZER
8796 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8798 /* expand_expr is allowed to return an object in a mode other
8799 than TMODE. If it did, we need to convert. */
8800 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8801 tmp = convert_modes (tmode, GET_MODE (tmp),
8802 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8803 result = convert_memory_address_addr_space (tmode, result, as);
8804 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8806 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8807 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8808 else
8810 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8811 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8812 1, OPTAB_LIB_WIDEN);
8816 if (maybe_ne (bitpos, 0))
8818 /* Someone beforehand should have rejected taking the address
8819 of an object that isn't byte-aligned. */
8820 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8821 result = convert_memory_address_addr_space (tmode, result, as);
8822 result = plus_constant (tmode, result, bytepos);
8823 if (modifier < EXPAND_SUM)
8824 result = force_operand (result, target);
8827 return result;
8830 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8831 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8833 static rtx
8834 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8835 enum expand_modifier modifier)
8837 addr_space_t as = ADDR_SPACE_GENERIC;
8838 scalar_int_mode address_mode = Pmode;
8839 scalar_int_mode pointer_mode = ptr_mode;
8840 machine_mode rmode;
8841 rtx result;
8843 /* Target mode of VOIDmode says "whatever's natural". */
8844 if (tmode == VOIDmode)
8845 tmode = TYPE_MODE (TREE_TYPE (exp));
8847 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8849 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8850 address_mode = targetm.addr_space.address_mode (as);
8851 pointer_mode = targetm.addr_space.pointer_mode (as);
8854 /* We can get called with some Weird Things if the user does silliness
8855 like "(short) &a". In that case, convert_memory_address won't do
8856 the right thing, so ignore the given target mode. */
8857 scalar_int_mode new_tmode = (tmode == pointer_mode
8858 ? pointer_mode
8859 : address_mode);
8861 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8862 new_tmode, modifier, as);
8864 /* Despite expand_expr claims concerning ignoring TMODE when not
8865 strictly convenient, stuff breaks if we don't honor it. Note
8866 that combined with the above, we only do this for pointer modes. */
8867 rmode = GET_MODE (result);
8868 if (rmode == VOIDmode)
8869 rmode = new_tmode;
8870 if (rmode != new_tmode)
8871 result = convert_memory_address_addr_space (new_tmode, result, as);
8873 return result;
8876 /* Generate code for computing CONSTRUCTOR EXP.
8877 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8878 is TRUE, instead of creating a temporary variable in memory
8879 NULL is returned and the caller needs to handle it differently. */
8881 static rtx
8882 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8883 bool avoid_temp_mem)
8885 tree type = TREE_TYPE (exp);
8886 machine_mode mode = TYPE_MODE (type);
8888 /* Try to avoid creating a temporary at all. This is possible
8889 if all of the initializer is zero.
8890 FIXME: try to handle all [0..255] initializers we can handle
8891 with memset. */
8892 if (TREE_STATIC (exp)
8893 && !TREE_ADDRESSABLE (exp)
8894 && target != 0 && mode == BLKmode
8895 && all_zeros_p (exp))
8897 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8898 return target;
8901 /* All elts simple constants => refer to a constant in memory. But
8902 if this is a non-BLKmode mode, let it store a field at a time
8903 since that should make a CONST_INT, CONST_WIDE_INT or
8904 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8905 use, it is best to store directly into the target unless the type
8906 is large enough that memcpy will be used. If we are making an
8907 initializer and all operands are constant, put it in memory as
8908 well.
8910 FIXME: Avoid trying to fill vector constructors piece-meal.
8911 Output them with output_constant_def below unless we're sure
8912 they're zeros. This should go away when vector initializers
8913 are treated like VECTOR_CST instead of arrays. */
8914 if ((TREE_STATIC (exp)
8915 && ((mode == BLKmode
8916 && ! (target != 0 && safe_from_p (target, exp, 1)))
8917 || TREE_ADDRESSABLE (exp)
8918 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8919 && (! can_move_by_pieces
8920 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8921 TYPE_ALIGN (type)))
8922 && ! mostly_zeros_p (exp))))
8923 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8924 && TREE_CONSTANT (exp)))
8926 rtx constructor;
8928 if (avoid_temp_mem)
8929 return NULL_RTX;
8931 constructor = expand_expr_constant (exp, 1, modifier);
8933 if (modifier != EXPAND_CONST_ADDRESS
8934 && modifier != EXPAND_INITIALIZER
8935 && modifier != EXPAND_SUM)
8936 constructor = validize_mem (constructor);
8938 return constructor;
8941 /* If the CTOR is available in static storage and not mostly
8942 zeros and we can move it by pieces prefer to do so since
8943 that's usually more efficient than performing a series of
8944 stores from immediates. */
8945 if (avoid_temp_mem
8946 && TREE_STATIC (exp)
8947 && TREE_CONSTANT (exp)
8948 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8949 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8950 TYPE_ALIGN (type))
8951 && ! mostly_zeros_p (exp))
8952 return NULL_RTX;
8954 /* Handle calls that pass values in multiple non-contiguous
8955 locations. The Irix 6 ABI has examples of this. */
8956 if (target == 0 || ! safe_from_p (target, exp, 1)
8957 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8958 /* Also make a temporary if the store is to volatile memory, to
8959 avoid individual accesses to aggregate members. */
8960 || (GET_CODE (target) == MEM
8961 && MEM_VOLATILE_P (target)
8962 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8964 if (avoid_temp_mem)
8965 return NULL_RTX;
8967 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8970 store_constructor (exp, target, 0, int_expr_size (exp), false);
8971 return target;
8975 /* expand_expr: generate code for computing expression EXP.
8976 An rtx for the computed value is returned. The value is never null.
8977 In the case of a void EXP, const0_rtx is returned.
8979 The value may be stored in TARGET if TARGET is nonzero.
8980 TARGET is just a suggestion; callers must assume that
8981 the rtx returned may not be the same as TARGET.
8983 If TARGET is CONST0_RTX, it means that the value will be ignored.
8985 If TMODE is not VOIDmode, it suggests generating the
8986 result in mode TMODE. But this is done only when convenient.
8987 Otherwise, TMODE is ignored and the value generated in its natural mode.
8988 TMODE is just a suggestion; callers must assume that
8989 the rtx returned may not have mode TMODE.
8991 Note that TARGET may have neither TMODE nor MODE. In that case, it
8992 probably will not be used.
8994 If MODIFIER is EXPAND_SUM then when EXP is an addition
8995 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8996 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8997 products as above, or REG or MEM, or constant.
8998 Ordinarily in such cases we would output mul or add instructions
8999 and then return a pseudo reg containing the sum.
9001 EXPAND_INITIALIZER is much like EXPAND_SUM except that
9002 it also marks a label as absolutely required (it can't be dead).
9003 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
9004 This is used for outputting expressions used in initializers.
9006 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
9007 with a constant address even if that address is not normally legitimate.
9008 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
9010 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
9011 a call parameter. Such targets require special care as we haven't yet
9012 marked TARGET so that it's safe from being trashed by libcalls. We
9013 don't want to use TARGET for anything but the final result;
9014 Intermediate values must go elsewhere. Additionally, calls to
9015 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
9017 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
9018 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
9019 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
9020 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
9021 recursively.
9022 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9023 then *ALT_RTL is set to TARGET (before legitimziation).
9025 If INNER_REFERENCE_P is true, we are expanding an inner reference.
9026 In this case, we don't adjust a returned MEM rtx that wouldn't be
9027 sufficiently aligned for its mode; instead, it's up to the caller
9028 to deal with it afterwards. This is used to make sure that unaligned
9029 base objects for which out-of-bounds accesses are supported, for
9030 example record types with trailing arrays, aren't realigned behind
9031 the back of the caller.
9032 The normal operating mode is to pass FALSE for this parameter. */
9035 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9036 enum expand_modifier modifier, rtx *alt_rtl,
9037 bool inner_reference_p)
9039 rtx ret;
9041 /* Handle ERROR_MARK before anybody tries to access its type. */
9042 if (TREE_CODE (exp) == ERROR_MARK
9043 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9045 ret = CONST0_RTX (tmode);
9046 return ret ? ret : const0_rtx;
9049 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9050 inner_reference_p);
9051 return ret;
9054 /* Try to expand the conditional expression which is represented by
9055 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9056 return the rtl reg which represents the result. Otherwise return
9057 NULL_RTX. */
9059 static rtx
9060 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9061 tree treeop1 ATTRIBUTE_UNUSED,
9062 tree treeop2 ATTRIBUTE_UNUSED)
9064 rtx insn;
9065 rtx op00, op01, op1, op2;
9066 enum rtx_code comparison_code;
9067 machine_mode comparison_mode;
9068 gimple *srcstmt;
9069 rtx temp;
9070 tree type = TREE_TYPE (treeop1);
9071 int unsignedp = TYPE_UNSIGNED (type);
9072 machine_mode mode = TYPE_MODE (type);
9073 machine_mode orig_mode = mode;
9074 static bool expanding_cond_expr_using_cmove = false;
9076 /* Conditional move expansion can end up TERing two operands which,
9077 when recursively hitting conditional expressions can result in
9078 exponential behavior if the cmove expansion ultimatively fails.
9079 It's hardly profitable to TER a cmove into a cmove so avoid doing
9080 that by failing early if we end up recursing. */
9081 if (expanding_cond_expr_using_cmove)
9082 return NULL_RTX;
9084 /* If we cannot do a conditional move on the mode, try doing it
9085 with the promoted mode. */
9086 if (!can_conditionally_move_p (mode))
9088 mode = promote_mode (type, mode, &unsignedp);
9089 if (!can_conditionally_move_p (mode))
9090 return NULL_RTX;
9091 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9093 else
9094 temp = assign_temp (type, 0, 1);
9096 expanding_cond_expr_using_cmove = true;
9097 start_sequence ();
9098 expand_operands (treeop1, treeop2,
9099 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9100 EXPAND_NORMAL);
9102 if (TREE_CODE (treeop0) == SSA_NAME
9103 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9105 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9106 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9107 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9108 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9109 comparison_mode = TYPE_MODE (type);
9110 unsignedp = TYPE_UNSIGNED (type);
9111 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9113 else if (COMPARISON_CLASS_P (treeop0))
9115 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9116 enum tree_code cmpcode = TREE_CODE (treeop0);
9117 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9118 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9119 unsignedp = TYPE_UNSIGNED (type);
9120 comparison_mode = TYPE_MODE (type);
9121 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9123 else
9125 op00 = expand_normal (treeop0);
9126 op01 = const0_rtx;
9127 comparison_code = NE;
9128 comparison_mode = GET_MODE (op00);
9129 if (comparison_mode == VOIDmode)
9130 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9132 expanding_cond_expr_using_cmove = false;
9134 if (GET_MODE (op1) != mode)
9135 op1 = gen_lowpart (mode, op1);
9137 if (GET_MODE (op2) != mode)
9138 op2 = gen_lowpart (mode, op2);
9140 /* Try to emit the conditional move. */
9141 insn = emit_conditional_move (temp,
9142 { comparison_code, op00, op01,
9143 comparison_mode },
9144 op1, op2, mode,
9145 unsignedp);
9147 /* If we could do the conditional move, emit the sequence,
9148 and return. */
9149 if (insn)
9151 rtx_insn *seq = get_insns ();
9152 end_sequence ();
9153 emit_insn (seq);
9154 return convert_modes (orig_mode, mode, temp, 0);
9157 /* Otherwise discard the sequence and fall back to code with
9158 branches. */
9159 end_sequence ();
9160 return NULL_RTX;
9163 /* A helper function for expand_expr_real_2 to be used with a
9164 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9165 is nonzero, with alignment ALIGN in bits.
9166 Store the value at TARGET if possible (if TARGET is nonzero).
9167 Regardless of TARGET, we return the rtx for where the value is placed.
9168 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9169 then *ALT_RTL is set to TARGET (before legitimziation). */
9171 static rtx
9172 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9173 unsigned int align, rtx target, rtx *alt_rtl)
9175 enum insn_code icode;
9177 if ((icode = optab_handler (movmisalign_optab, mode))
9178 != CODE_FOR_nothing)
9180 class expand_operand ops[2];
9182 /* We've already validated the memory, and we're creating a
9183 new pseudo destination. The predicates really can't fail,
9184 nor can the generator. */
9185 create_output_operand (&ops[0], NULL_RTX, mode);
9186 create_fixed_operand (&ops[1], temp);
9187 expand_insn (icode, 2, ops);
9188 temp = ops[0].value;
9190 else if (targetm.slow_unaligned_access (mode, align))
9191 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9192 0, unsignedp, target,
9193 mode, mode, false, alt_rtl);
9194 return temp;
9197 /* Helper function of expand_expr_2, expand a division or modulo.
9198 op0 and op1 should be already expanded treeop0 and treeop1, using
9199 expand_operands. */
9201 static rtx
9202 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9203 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9205 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9206 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9207 if (SCALAR_INT_MODE_P (mode)
9208 && optimize >= 2
9209 && get_range_pos_neg (treeop0) == 1
9210 && get_range_pos_neg (treeop1) == 1)
9212 /* If both arguments are known to be positive when interpreted
9213 as signed, we can expand it as both signed and unsigned
9214 division or modulo. Choose the cheaper sequence in that case. */
9215 bool speed_p = optimize_insn_for_speed_p ();
9216 do_pending_stack_adjust ();
9217 start_sequence ();
9218 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9219 rtx_insn *uns_insns = get_insns ();
9220 end_sequence ();
9221 start_sequence ();
9222 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9223 rtx_insn *sgn_insns = get_insns ();
9224 end_sequence ();
9225 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9226 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9228 /* If costs are the same then use as tie breaker the other other
9229 factor. */
9230 if (uns_cost == sgn_cost)
9232 uns_cost = seq_cost (uns_insns, !speed_p);
9233 sgn_cost = seq_cost (sgn_insns, !speed_p);
9236 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9238 emit_insn (uns_insns);
9239 return uns_ret;
9241 emit_insn (sgn_insns);
9242 return sgn_ret;
9244 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9248 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9249 enum expand_modifier modifier)
9251 rtx op0, op1, op2, temp;
9252 rtx_code_label *lab;
9253 tree type;
9254 int unsignedp;
9255 machine_mode mode;
9256 scalar_int_mode int_mode;
9257 enum tree_code code = ops->code;
9258 optab this_optab;
9259 rtx subtarget, original_target;
9260 int ignore;
9261 bool reduce_bit_field;
9262 location_t loc = ops->location;
9263 tree treeop0, treeop1, treeop2;
9264 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9265 ? reduce_to_bit_field_precision ((expr), \
9266 target, \
9267 type) \
9268 : (expr))
9270 type = ops->type;
9271 mode = TYPE_MODE (type);
9272 unsignedp = TYPE_UNSIGNED (type);
9274 treeop0 = ops->op0;
9275 treeop1 = ops->op1;
9276 treeop2 = ops->op2;
9278 /* We should be called only on simple (binary or unary) expressions,
9279 exactly those that are valid in gimple expressions that aren't
9280 GIMPLE_SINGLE_RHS (or invalid). */
9281 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9282 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9283 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9285 ignore = (target == const0_rtx
9286 || ((CONVERT_EXPR_CODE_P (code)
9287 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9288 && TREE_CODE (type) == VOID_TYPE));
9290 /* We should be called only if we need the result. */
9291 gcc_assert (!ignore);
9293 /* An operation in what may be a bit-field type needs the
9294 result to be reduced to the precision of the bit-field type,
9295 which is narrower than that of the type's mode. */
9296 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9297 && !type_has_mode_precision_p (type));
9299 if (reduce_bit_field
9300 && (modifier == EXPAND_STACK_PARM
9301 || (target && GET_MODE (target) != mode)))
9302 target = 0;
9304 /* Use subtarget as the target for operand 0 of a binary operation. */
9305 subtarget = get_subtarget (target);
9306 original_target = target;
9308 switch (code)
9310 case NON_LVALUE_EXPR:
9311 case PAREN_EXPR:
9312 CASE_CONVERT:
9313 if (treeop0 == error_mark_node)
9314 return const0_rtx;
9316 if (TREE_CODE (type) == UNION_TYPE)
9318 tree valtype = TREE_TYPE (treeop0);
9320 /* If both input and output are BLKmode, this conversion isn't doing
9321 anything except possibly changing memory attribute. */
9322 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9324 rtx result = expand_expr (treeop0, target, tmode,
9325 modifier);
9327 result = copy_rtx (result);
9328 set_mem_attributes (result, type, 0);
9329 return result;
9332 if (target == 0)
9334 if (TYPE_MODE (type) != BLKmode)
9335 target = gen_reg_rtx (TYPE_MODE (type));
9336 else
9337 target = assign_temp (type, 1, 1);
9340 if (MEM_P (target))
9341 /* Store data into beginning of memory target. */
9342 store_expr (treeop0,
9343 adjust_address (target, TYPE_MODE (valtype), 0),
9344 modifier == EXPAND_STACK_PARM,
9345 false, TYPE_REVERSE_STORAGE_ORDER (type));
9347 else
9349 gcc_assert (REG_P (target)
9350 && !TYPE_REVERSE_STORAGE_ORDER (type));
9352 /* Store this field into a union of the proper type. */
9353 poly_uint64 op0_size
9354 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9355 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9356 store_field (target,
9357 /* The conversion must be constructed so that
9358 we know at compile time how many bits
9359 to preserve. */
9360 ordered_min (op0_size, union_size),
9361 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9362 false, false);
9365 /* Return the entire union. */
9366 return target;
9369 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9371 op0 = expand_expr (treeop0, target, VOIDmode,
9372 modifier);
9374 return REDUCE_BIT_FIELD (op0);
9377 op0 = expand_expr (treeop0, NULL_RTX, mode,
9378 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9379 if (GET_MODE (op0) == mode)
9382 /* If OP0 is a constant, just convert it into the proper mode. */
9383 else if (CONSTANT_P (op0))
9385 tree inner_type = TREE_TYPE (treeop0);
9386 machine_mode inner_mode = GET_MODE (op0);
9388 if (inner_mode == VOIDmode)
9389 inner_mode = TYPE_MODE (inner_type);
9391 if (modifier == EXPAND_INITIALIZER)
9392 op0 = lowpart_subreg (mode, op0, inner_mode);
9393 else
9394 op0= convert_modes (mode, inner_mode, op0,
9395 TYPE_UNSIGNED (inner_type));
9398 else if (modifier == EXPAND_INITIALIZER)
9399 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9400 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9402 else if (target == 0)
9403 op0 = convert_to_mode (mode, op0,
9404 TYPE_UNSIGNED (TREE_TYPE
9405 (treeop0)));
9406 else
9408 convert_move (target, op0,
9409 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9410 op0 = target;
9413 return REDUCE_BIT_FIELD (op0);
9415 case ADDR_SPACE_CONVERT_EXPR:
9417 tree treeop0_type = TREE_TYPE (treeop0);
9419 gcc_assert (POINTER_TYPE_P (type));
9420 gcc_assert (POINTER_TYPE_P (treeop0_type));
9422 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9423 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9425 /* Conversions between pointers to the same address space should
9426 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9427 gcc_assert (as_to != as_from);
9429 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9431 /* Ask target code to handle conversion between pointers
9432 to overlapping address spaces. */
9433 if (targetm.addr_space.subset_p (as_to, as_from)
9434 || targetm.addr_space.subset_p (as_from, as_to))
9436 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9438 else
9440 /* For disjoint address spaces, converting anything but a null
9441 pointer invokes undefined behavior. We truncate or extend the
9442 value as if we'd converted via integers, which handles 0 as
9443 required, and all others as the programmer likely expects. */
9444 #ifndef POINTERS_EXTEND_UNSIGNED
9445 const int POINTERS_EXTEND_UNSIGNED = 1;
9446 #endif
9447 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9448 op0, POINTERS_EXTEND_UNSIGNED);
9450 gcc_assert (op0);
9451 return op0;
9454 case POINTER_PLUS_EXPR:
9455 /* Even though the sizetype mode and the pointer's mode can be different
9456 expand is able to handle this correctly and get the correct result out
9457 of the PLUS_EXPR code. */
9458 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9459 if sizetype precision is smaller than pointer precision. */
9460 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9461 treeop1 = fold_convert_loc (loc, type,
9462 fold_convert_loc (loc, ssizetype,
9463 treeop1));
9464 /* If sizetype precision is larger than pointer precision, truncate the
9465 offset to have matching modes. */
9466 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9467 treeop1 = fold_convert_loc (loc, type, treeop1);
9468 /* FALLTHRU */
9470 case PLUS_EXPR:
9471 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9472 something else, make sure we add the register to the constant and
9473 then to the other thing. This case can occur during strength
9474 reduction and doing it this way will produce better code if the
9475 frame pointer or argument pointer is eliminated.
9477 fold-const.cc will ensure that the constant is always in the inner
9478 PLUS_EXPR, so the only case we need to do anything about is if
9479 sp, ap, or fp is our second argument, in which case we must swap
9480 the innermost first argument and our second argument. */
9482 if (TREE_CODE (treeop0) == PLUS_EXPR
9483 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9484 && VAR_P (treeop1)
9485 && (DECL_RTL (treeop1) == frame_pointer_rtx
9486 || DECL_RTL (treeop1) == stack_pointer_rtx
9487 || DECL_RTL (treeop1) == arg_pointer_rtx))
9489 gcc_unreachable ();
9492 /* If the result is to be ptr_mode and we are adding an integer to
9493 something, we might be forming a constant. So try to use
9494 plus_constant. If it produces a sum and we can't accept it,
9495 use force_operand. This allows P = &ARR[const] to generate
9496 efficient code on machines where a SYMBOL_REF is not a valid
9497 address.
9499 If this is an EXPAND_SUM call, always return the sum. */
9500 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9501 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9503 if (modifier == EXPAND_STACK_PARM)
9504 target = 0;
9505 if (TREE_CODE (treeop0) == INTEGER_CST
9506 && HWI_COMPUTABLE_MODE_P (mode)
9507 && TREE_CONSTANT (treeop1))
9509 rtx constant_part;
9510 HOST_WIDE_INT wc;
9511 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9513 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9514 EXPAND_SUM);
9515 /* Use wi::shwi to ensure that the constant is
9516 truncated according to the mode of OP1, then sign extended
9517 to a HOST_WIDE_INT. Using the constant directly can result
9518 in non-canonical RTL in a 64x32 cross compile. */
9519 wc = TREE_INT_CST_LOW (treeop0);
9520 constant_part =
9521 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9522 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9523 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9524 op1 = force_operand (op1, target);
9525 return REDUCE_BIT_FIELD (op1);
9528 else if (TREE_CODE (treeop1) == INTEGER_CST
9529 && HWI_COMPUTABLE_MODE_P (mode)
9530 && TREE_CONSTANT (treeop0))
9532 rtx constant_part;
9533 HOST_WIDE_INT wc;
9534 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9536 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9537 (modifier == EXPAND_INITIALIZER
9538 ? EXPAND_INITIALIZER : EXPAND_SUM));
9539 if (! CONSTANT_P (op0))
9541 op1 = expand_expr (treeop1, NULL_RTX,
9542 VOIDmode, modifier);
9543 /* Return a PLUS if modifier says it's OK. */
9544 if (modifier == EXPAND_SUM
9545 || modifier == EXPAND_INITIALIZER)
9546 return simplify_gen_binary (PLUS, mode, op0, op1);
9547 goto binop2;
9549 /* Use wi::shwi to ensure that the constant is
9550 truncated according to the mode of OP1, then sign extended
9551 to a HOST_WIDE_INT. Using the constant directly can result
9552 in non-canonical RTL in a 64x32 cross compile. */
9553 wc = TREE_INT_CST_LOW (treeop1);
9554 constant_part
9555 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9556 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9557 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9558 op0 = force_operand (op0, target);
9559 return REDUCE_BIT_FIELD (op0);
9563 /* Use TER to expand pointer addition of a negated value
9564 as pointer subtraction. */
9565 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9566 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9567 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9568 && TREE_CODE (treeop1) == SSA_NAME
9569 && TYPE_MODE (TREE_TYPE (treeop0))
9570 == TYPE_MODE (TREE_TYPE (treeop1)))
9572 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9573 if (def)
9575 treeop1 = gimple_assign_rhs1 (def);
9576 code = MINUS_EXPR;
9577 goto do_minus;
9581 /* No sense saving up arithmetic to be done
9582 if it's all in the wrong mode to form part of an address.
9583 And force_operand won't know whether to sign-extend or
9584 zero-extend. */
9585 if (modifier != EXPAND_INITIALIZER
9586 && (modifier != EXPAND_SUM || mode != ptr_mode))
9588 expand_operands (treeop0, treeop1,
9589 subtarget, &op0, &op1, modifier);
9590 if (op0 == const0_rtx)
9591 return op1;
9592 if (op1 == const0_rtx)
9593 return op0;
9594 goto binop2;
9597 expand_operands (treeop0, treeop1,
9598 subtarget, &op0, &op1, modifier);
9599 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9601 case MINUS_EXPR:
9602 case POINTER_DIFF_EXPR:
9603 do_minus:
9604 /* For initializers, we are allowed to return a MINUS of two
9605 symbolic constants. Here we handle all cases when both operands
9606 are constant. */
9607 /* Handle difference of two symbolic constants,
9608 for the sake of an initializer. */
9609 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9610 && really_constant_p (treeop0)
9611 && really_constant_p (treeop1))
9613 expand_operands (treeop0, treeop1,
9614 NULL_RTX, &op0, &op1, modifier);
9615 return simplify_gen_binary (MINUS, mode, op0, op1);
9618 /* No sense saving up arithmetic to be done
9619 if it's all in the wrong mode to form part of an address.
9620 And force_operand won't know whether to sign-extend or
9621 zero-extend. */
9622 if (modifier != EXPAND_INITIALIZER
9623 && (modifier != EXPAND_SUM || mode != ptr_mode))
9624 goto binop;
9626 expand_operands (treeop0, treeop1,
9627 subtarget, &op0, &op1, modifier);
9629 /* Convert A - const to A + (-const). */
9630 if (CONST_INT_P (op1))
9632 op1 = negate_rtx (mode, op1);
9633 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9636 goto binop2;
9638 case WIDEN_MULT_PLUS_EXPR:
9639 case WIDEN_MULT_MINUS_EXPR:
9640 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9641 op2 = expand_normal (treeop2);
9642 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9643 target, unsignedp);
9644 return target;
9646 case WIDEN_MULT_EXPR:
9647 /* If first operand is constant, swap them.
9648 Thus the following special case checks need only
9649 check the second operand. */
9650 if (TREE_CODE (treeop0) == INTEGER_CST)
9651 std::swap (treeop0, treeop1);
9653 /* First, check if we have a multiplication of one signed and one
9654 unsigned operand. */
9655 if (TREE_CODE (treeop1) != INTEGER_CST
9656 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9657 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9659 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9660 this_optab = usmul_widen_optab;
9661 if (find_widening_optab_handler (this_optab, mode, innermode)
9662 != CODE_FOR_nothing)
9664 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9665 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9666 EXPAND_NORMAL);
9667 else
9668 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9669 EXPAND_NORMAL);
9670 /* op0 and op1 might still be constant, despite the above
9671 != INTEGER_CST check. Handle it. */
9672 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9674 op0 = convert_modes (mode, innermode, op0, true);
9675 op1 = convert_modes (mode, innermode, op1, false);
9676 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9677 target, unsignedp));
9679 goto binop3;
9682 /* Check for a multiplication with matching signedness. */
9683 else if ((TREE_CODE (treeop1) == INTEGER_CST
9684 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9685 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9686 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9688 tree op0type = TREE_TYPE (treeop0);
9689 machine_mode innermode = TYPE_MODE (op0type);
9690 bool zextend_p = TYPE_UNSIGNED (op0type);
9691 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9692 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9694 if (TREE_CODE (treeop0) != INTEGER_CST)
9696 if (find_widening_optab_handler (this_optab, mode, innermode)
9697 != CODE_FOR_nothing)
9699 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9700 EXPAND_NORMAL);
9701 /* op0 and op1 might still be constant, despite the above
9702 != INTEGER_CST check. Handle it. */
9703 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9705 widen_mult_const:
9706 op0 = convert_modes (mode, innermode, op0, zextend_p);
9708 = convert_modes (mode, innermode, op1,
9709 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9710 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9711 target,
9712 unsignedp));
9714 temp = expand_widening_mult (mode, op0, op1, target,
9715 unsignedp, this_optab);
9716 return REDUCE_BIT_FIELD (temp);
9718 if (find_widening_optab_handler (other_optab, mode, innermode)
9719 != CODE_FOR_nothing
9720 && innermode == word_mode)
9722 rtx htem, hipart;
9723 op0 = expand_normal (treeop0);
9724 op1 = expand_normal (treeop1);
9725 /* op0 and op1 might be constants, despite the above
9726 != INTEGER_CST check. Handle it. */
9727 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9728 goto widen_mult_const;
9729 temp = expand_binop (mode, other_optab, op0, op1, target,
9730 unsignedp, OPTAB_LIB_WIDEN);
9731 hipart = gen_highpart (word_mode, temp);
9732 htem = expand_mult_highpart_adjust (word_mode, hipart,
9733 op0, op1, hipart,
9734 zextend_p);
9735 if (htem != hipart)
9736 emit_move_insn (hipart, htem);
9737 return REDUCE_BIT_FIELD (temp);
9741 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9742 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9743 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9744 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9746 case MULT_EXPR:
9747 /* If this is a fixed-point operation, then we cannot use the code
9748 below because "expand_mult" doesn't support sat/no-sat fixed-point
9749 multiplications. */
9750 if (ALL_FIXED_POINT_MODE_P (mode))
9751 goto binop;
9753 /* If first operand is constant, swap them.
9754 Thus the following special case checks need only
9755 check the second operand. */
9756 if (TREE_CODE (treeop0) == INTEGER_CST)
9757 std::swap (treeop0, treeop1);
9759 /* Attempt to return something suitable for generating an
9760 indexed address, for machines that support that. */
9762 if (modifier == EXPAND_SUM && mode == ptr_mode
9763 && tree_fits_shwi_p (treeop1))
9765 tree exp1 = treeop1;
9767 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9768 EXPAND_SUM);
9770 if (!REG_P (op0))
9771 op0 = force_operand (op0, NULL_RTX);
9772 if (!REG_P (op0))
9773 op0 = copy_to_mode_reg (mode, op0);
9775 op1 = gen_int_mode (tree_to_shwi (exp1),
9776 TYPE_MODE (TREE_TYPE (exp1)));
9777 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9780 if (modifier == EXPAND_STACK_PARM)
9781 target = 0;
9783 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9785 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9786 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9787 if (def_stmt0
9788 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9789 def_stmt0 = NULL;
9790 if (def_stmt1
9791 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9792 def_stmt1 = NULL;
9794 if (def_stmt0 || def_stmt1)
9796 /* X / Y * Y can be expanded as X - X % Y too.
9797 Choose the cheaper sequence of those two. */
9798 if (def_stmt0)
9799 treeop0 = gimple_assign_rhs1 (def_stmt0);
9800 else
9802 treeop1 = treeop0;
9803 treeop0 = gimple_assign_rhs1 (def_stmt1);
9805 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9806 EXPAND_NORMAL);
9807 bool speed_p = optimize_insn_for_speed_p ();
9808 do_pending_stack_adjust ();
9809 start_sequence ();
9810 rtx divmul_ret
9811 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9812 op0, op1, NULL_RTX, unsignedp);
9813 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9814 unsignedp);
9815 rtx_insn *divmul_insns = get_insns ();
9816 end_sequence ();
9817 start_sequence ();
9818 rtx modsub_ret
9819 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9820 op0, op1, NULL_RTX, unsignedp);
9821 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9822 optab_default);
9823 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9824 target, unsignedp, OPTAB_LIB_WIDEN);
9825 rtx_insn *modsub_insns = get_insns ();
9826 end_sequence ();
9827 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9828 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9829 /* If costs are the same then use as tie breaker the other other
9830 factor. */
9831 if (divmul_cost == modsub_cost)
9833 divmul_cost = seq_cost (divmul_insns, !speed_p);
9834 modsub_cost = seq_cost (modsub_insns, !speed_p);
9837 if (divmul_cost <= modsub_cost)
9839 emit_insn (divmul_insns);
9840 return REDUCE_BIT_FIELD (divmul_ret);
9842 emit_insn (modsub_insns);
9843 return REDUCE_BIT_FIELD (modsub_ret);
9847 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9849 /* Expand X*Y as X&-Y when Y must be zero or one. */
9850 if (SCALAR_INT_MODE_P (mode))
9852 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9853 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9855 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9856 if (bit0_p && bit1_p)
9857 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9859 if (bit0_p || bit1_p)
9861 bool speed = optimize_insn_for_speed_p ();
9862 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9863 struct algorithm algorithm;
9864 enum mult_variant variant;
9865 if (CONST_INT_P (op1)
9866 ? !choose_mult_variant (mode, INTVAL (op1),
9867 &algorithm, &variant, cost)
9868 : cost < mul_cost (speed, mode))
9870 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9871 op1, target)
9872 : expand_and (mode, op0,
9873 negate_rtx (mode, op1),
9874 target);
9875 return REDUCE_BIT_FIELD (target);
9880 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9882 case TRUNC_MOD_EXPR:
9883 case FLOOR_MOD_EXPR:
9884 case CEIL_MOD_EXPR:
9885 case ROUND_MOD_EXPR:
9887 case TRUNC_DIV_EXPR:
9888 case FLOOR_DIV_EXPR:
9889 case CEIL_DIV_EXPR:
9890 case ROUND_DIV_EXPR:
9891 case EXACT_DIV_EXPR:
9892 /* If this is a fixed-point operation, then we cannot use the code
9893 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9894 divisions. */
9895 if (ALL_FIXED_POINT_MODE_P (mode))
9896 goto binop;
9898 if (modifier == EXPAND_STACK_PARM)
9899 target = 0;
9900 /* Possible optimization: compute the dividend with EXPAND_SUM
9901 then if the divisor is constant can optimize the case
9902 where some terms of the dividend have coeffs divisible by it. */
9903 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9904 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9905 target, unsignedp);
9907 case RDIV_EXPR:
9908 goto binop;
9910 case MULT_HIGHPART_EXPR:
9911 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9912 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9913 gcc_assert (temp);
9914 return temp;
9916 case FIXED_CONVERT_EXPR:
9917 op0 = expand_normal (treeop0);
9918 if (target == 0 || modifier == EXPAND_STACK_PARM)
9919 target = gen_reg_rtx (mode);
9921 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9922 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9923 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9924 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9925 else
9926 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9927 return target;
9929 case FIX_TRUNC_EXPR:
9930 op0 = expand_normal (treeop0);
9931 if (target == 0 || modifier == EXPAND_STACK_PARM)
9932 target = gen_reg_rtx (mode);
9933 expand_fix (target, op0, unsignedp);
9934 return target;
9936 case FLOAT_EXPR:
9937 op0 = expand_normal (treeop0);
9938 if (target == 0 || modifier == EXPAND_STACK_PARM)
9939 target = gen_reg_rtx (mode);
9940 /* expand_float can't figure out what to do if FROM has VOIDmode.
9941 So give it the correct mode. With -O, cse will optimize this. */
9942 if (GET_MODE (op0) == VOIDmode)
9943 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9944 op0);
9945 expand_float (target, op0,
9946 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9947 return target;
9949 case NEGATE_EXPR:
9950 op0 = expand_expr (treeop0, subtarget,
9951 VOIDmode, EXPAND_NORMAL);
9952 if (modifier == EXPAND_STACK_PARM)
9953 target = 0;
9954 temp = expand_unop (mode,
9955 optab_for_tree_code (NEGATE_EXPR, type,
9956 optab_default),
9957 op0, target, 0);
9958 gcc_assert (temp);
9959 return REDUCE_BIT_FIELD (temp);
9961 case ABS_EXPR:
9962 case ABSU_EXPR:
9963 op0 = expand_expr (treeop0, subtarget,
9964 VOIDmode, EXPAND_NORMAL);
9965 if (modifier == EXPAND_STACK_PARM)
9966 target = 0;
9968 /* ABS_EXPR is not valid for complex arguments. */
9969 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9970 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9972 /* Unsigned abs is simply the operand. Testing here means we don't
9973 risk generating incorrect code below. */
9974 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9975 return op0;
9977 return expand_abs (mode, op0, target, unsignedp,
9978 safe_from_p (target, treeop0, 1));
9980 case MAX_EXPR:
9981 case MIN_EXPR:
9982 target = original_target;
9983 if (target == 0
9984 || modifier == EXPAND_STACK_PARM
9985 || (MEM_P (target) && MEM_VOLATILE_P (target))
9986 || GET_MODE (target) != mode
9987 || (REG_P (target)
9988 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9989 target = gen_reg_rtx (mode);
9990 expand_operands (treeop0, treeop1,
9991 target, &op0, &op1, EXPAND_NORMAL);
9993 /* First try to do it with a special MIN or MAX instruction.
9994 If that does not win, use a conditional jump to select the proper
9995 value. */
9996 this_optab = optab_for_tree_code (code, type, optab_default);
9997 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9998 OPTAB_WIDEN);
9999 if (temp != 0)
10000 return temp;
10002 if (VECTOR_TYPE_P (type))
10003 gcc_unreachable ();
10005 /* At this point, a MEM target is no longer useful; we will get better
10006 code without it. */
10008 if (! REG_P (target))
10009 target = gen_reg_rtx (mode);
10011 /* If op1 was placed in target, swap op0 and op1. */
10012 if (target != op0 && target == op1)
10013 std::swap (op0, op1);
10015 /* We generate better code and avoid problems with op1 mentioning
10016 target by forcing op1 into a pseudo if it isn't a constant. */
10017 if (! CONSTANT_P (op1))
10018 op1 = force_reg (mode, op1);
10021 enum rtx_code comparison_code;
10022 rtx cmpop1 = op1;
10024 if (code == MAX_EXPR)
10025 comparison_code = unsignedp ? GEU : GE;
10026 else
10027 comparison_code = unsignedp ? LEU : LE;
10029 /* Canonicalize to comparisons against 0. */
10030 if (op1 == const1_rtx)
10032 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10033 or (a != 0 ? a : 1) for unsigned.
10034 For MIN we are safe converting (a <= 1 ? a : 1)
10035 into (a <= 0 ? a : 1) */
10036 cmpop1 = const0_rtx;
10037 if (code == MAX_EXPR)
10038 comparison_code = unsignedp ? NE : GT;
10040 if (op1 == constm1_rtx && !unsignedp)
10042 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10043 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10044 cmpop1 = const0_rtx;
10045 if (code == MIN_EXPR)
10046 comparison_code = LT;
10049 /* Use a conditional move if possible. */
10050 if (can_conditionally_move_p (mode))
10052 rtx insn;
10054 start_sequence ();
10056 /* Try to emit the conditional move. */
10057 insn = emit_conditional_move (target,
10058 { comparison_code,
10059 op0, cmpop1, mode },
10060 op0, op1, mode,
10061 unsignedp);
10063 /* If we could do the conditional move, emit the sequence,
10064 and return. */
10065 if (insn)
10067 rtx_insn *seq = get_insns ();
10068 end_sequence ();
10069 emit_insn (seq);
10070 return target;
10073 /* Otherwise discard the sequence and fall back to code with
10074 branches. */
10075 end_sequence ();
10078 if (target != op0)
10079 emit_move_insn (target, op0);
10081 lab = gen_label_rtx ();
10082 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10083 unsignedp, mode, NULL_RTX, NULL, lab,
10084 profile_probability::uninitialized ());
10086 emit_move_insn (target, op1);
10087 emit_label (lab);
10088 return target;
10090 case BIT_NOT_EXPR:
10091 op0 = expand_expr (treeop0, subtarget,
10092 VOIDmode, EXPAND_NORMAL);
10093 if (modifier == EXPAND_STACK_PARM)
10094 target = 0;
10095 /* In case we have to reduce the result to bitfield precision
10096 for unsigned bitfield expand this as XOR with a proper constant
10097 instead. */
10098 if (reduce_bit_field && TYPE_UNSIGNED (type))
10100 int_mode = SCALAR_INT_TYPE_MODE (type);
10101 wide_int mask = wi::mask (TYPE_PRECISION (type),
10102 false, GET_MODE_PRECISION (int_mode));
10104 temp = expand_binop (int_mode, xor_optab, op0,
10105 immed_wide_int_const (mask, int_mode),
10106 target, 1, OPTAB_LIB_WIDEN);
10108 else
10109 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10110 gcc_assert (temp);
10111 return temp;
10113 /* ??? Can optimize bitwise operations with one arg constant.
10114 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10115 and (a bitwise1 b) bitwise2 b (etc)
10116 but that is probably not worth while. */
10118 case BIT_AND_EXPR:
10119 case BIT_IOR_EXPR:
10120 case BIT_XOR_EXPR:
10121 goto binop;
10123 case LROTATE_EXPR:
10124 case RROTATE_EXPR:
10125 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10126 || type_has_mode_precision_p (type));
10127 /* fall through */
10129 case LSHIFT_EXPR:
10130 case RSHIFT_EXPR:
10132 /* If this is a fixed-point operation, then we cannot use the code
10133 below because "expand_shift" doesn't support sat/no-sat fixed-point
10134 shifts. */
10135 if (ALL_FIXED_POINT_MODE_P (mode))
10136 goto binop;
10138 if (! safe_from_p (subtarget, treeop1, 1))
10139 subtarget = 0;
10140 if (modifier == EXPAND_STACK_PARM)
10141 target = 0;
10142 op0 = expand_expr (treeop0, subtarget,
10143 VOIDmode, EXPAND_NORMAL);
10145 /* Left shift optimization when shifting across word_size boundary.
10147 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10148 there isn't native instruction to support this wide mode
10149 left shift. Given below scenario:
10151 Type A = (Type) B << C
10153 |< T >|
10154 | dest_high | dest_low |
10156 | word_size |
10158 If the shift amount C caused we shift B to across the word
10159 size boundary, i.e part of B shifted into high half of
10160 destination register, and part of B remains in the low
10161 half, then GCC will use the following left shift expand
10162 logic:
10164 1. Initialize dest_low to B.
10165 2. Initialize every bit of dest_high to the sign bit of B.
10166 3. Logic left shift dest_low by C bit to finalize dest_low.
10167 The value of dest_low before this shift is kept in a temp D.
10168 4. Logic left shift dest_high by C.
10169 5. Logic right shift D by (word_size - C).
10170 6. Or the result of 4 and 5 to finalize dest_high.
10172 While, by checking gimple statements, if operand B is
10173 coming from signed extension, then we can simplify above
10174 expand logic into:
10176 1. dest_high = src_low >> (word_size - C).
10177 2. dest_low = src_low << C.
10179 We can use one arithmetic right shift to finish all the
10180 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10181 needed from 6 into 2.
10183 The case is similar for zero extension, except that we
10184 initialize dest_high to zero rather than copies of the sign
10185 bit from B. Furthermore, we need to use a logical right shift
10186 in this case.
10188 The choice of sign-extension versus zero-extension is
10189 determined entirely by whether or not B is signed and is
10190 independent of the current setting of unsignedp. */
10192 temp = NULL_RTX;
10193 if (code == LSHIFT_EXPR
10194 && target
10195 && REG_P (target)
10196 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10197 && mode == int_mode
10198 && TREE_CONSTANT (treeop1)
10199 && TREE_CODE (treeop0) == SSA_NAME)
10201 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10202 if (is_gimple_assign (def)
10203 && gimple_assign_rhs_code (def) == NOP_EXPR)
10205 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10206 (TREE_TYPE (gimple_assign_rhs1 (def)));
10208 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10209 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10210 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10211 >= GET_MODE_BITSIZE (word_mode)))
10213 rtx_insn *seq, *seq_old;
10214 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10215 int_mode);
10216 bool extend_unsigned
10217 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10218 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10219 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10220 rtx dest_high = simplify_gen_subreg (word_mode, target,
10221 int_mode, high_off);
10222 HOST_WIDE_INT ramount = (BITS_PER_WORD
10223 - TREE_INT_CST_LOW (treeop1));
10224 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10226 start_sequence ();
10227 /* dest_high = src_low >> (word_size - C). */
10228 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10229 rshift, dest_high,
10230 extend_unsigned);
10231 if (temp != dest_high)
10232 emit_move_insn (dest_high, temp);
10234 /* dest_low = src_low << C. */
10235 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10236 treeop1, dest_low, unsignedp);
10237 if (temp != dest_low)
10238 emit_move_insn (dest_low, temp);
10240 seq = get_insns ();
10241 end_sequence ();
10242 temp = target ;
10244 if (have_insn_for (ASHIFT, int_mode))
10246 bool speed_p = optimize_insn_for_speed_p ();
10247 start_sequence ();
10248 rtx ret_old = expand_variable_shift (code, int_mode,
10249 op0, treeop1,
10250 target,
10251 unsignedp);
10253 seq_old = get_insns ();
10254 end_sequence ();
10255 if (seq_cost (seq, speed_p)
10256 >= seq_cost (seq_old, speed_p))
10258 seq = seq_old;
10259 temp = ret_old;
10262 emit_insn (seq);
10267 if (temp == NULL_RTX)
10268 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10269 unsignedp);
10270 if (code == LSHIFT_EXPR)
10271 temp = REDUCE_BIT_FIELD (temp);
10272 return temp;
10275 /* Could determine the answer when only additive constants differ. Also,
10276 the addition of one can be handled by changing the condition. */
10277 case LT_EXPR:
10278 case LE_EXPR:
10279 case GT_EXPR:
10280 case GE_EXPR:
10281 case EQ_EXPR:
10282 case NE_EXPR:
10283 case UNORDERED_EXPR:
10284 case ORDERED_EXPR:
10285 case UNLT_EXPR:
10286 case UNLE_EXPR:
10287 case UNGT_EXPR:
10288 case UNGE_EXPR:
10289 case UNEQ_EXPR:
10290 case LTGT_EXPR:
10292 temp = do_store_flag (ops,
10293 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10294 tmode != VOIDmode ? tmode : mode);
10295 if (temp)
10296 return temp;
10298 /* Use a compare and a jump for BLKmode comparisons, or for function
10299 type comparisons is have_canonicalize_funcptr_for_compare. */
10301 if ((target == 0
10302 || modifier == EXPAND_STACK_PARM
10303 || ! safe_from_p (target, treeop0, 1)
10304 || ! safe_from_p (target, treeop1, 1)
10305 /* Make sure we don't have a hard reg (such as function's return
10306 value) live across basic blocks, if not optimizing. */
10307 || (!optimize && REG_P (target)
10308 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10309 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10311 emit_move_insn (target, const0_rtx);
10313 rtx_code_label *lab1 = gen_label_rtx ();
10314 jumpifnot_1 (code, treeop0, treeop1, lab1,
10315 profile_probability::uninitialized ());
10317 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10318 emit_move_insn (target, constm1_rtx);
10319 else
10320 emit_move_insn (target, const1_rtx);
10322 emit_label (lab1);
10323 return target;
10325 case COMPLEX_EXPR:
10326 /* Get the rtx code of the operands. */
10327 op0 = expand_normal (treeop0);
10328 op1 = expand_normal (treeop1);
10330 if (!target)
10331 target = gen_reg_rtx (TYPE_MODE (type));
10332 else
10333 /* If target overlaps with op1, then either we need to force
10334 op1 into a pseudo (if target also overlaps with op0),
10335 or write the complex parts in reverse order. */
10336 switch (GET_CODE (target))
10338 case CONCAT:
10339 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10341 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10343 complex_expr_force_op1:
10344 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10345 emit_move_insn (temp, op1);
10346 op1 = temp;
10347 break;
10349 complex_expr_swap_order:
10350 /* Move the imaginary (op1) and real (op0) parts to their
10351 location. */
10352 write_complex_part (target, op1, true, true);
10353 write_complex_part (target, op0, false, false);
10355 return target;
10357 break;
10358 case MEM:
10359 temp = adjust_address_nv (target,
10360 GET_MODE_INNER (GET_MODE (target)), 0);
10361 if (reg_overlap_mentioned_p (temp, op1))
10363 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10364 temp = adjust_address_nv (target, imode,
10365 GET_MODE_SIZE (imode));
10366 if (reg_overlap_mentioned_p (temp, op0))
10367 goto complex_expr_force_op1;
10368 goto complex_expr_swap_order;
10370 break;
10371 default:
10372 if (reg_overlap_mentioned_p (target, op1))
10374 if (reg_overlap_mentioned_p (target, op0))
10375 goto complex_expr_force_op1;
10376 goto complex_expr_swap_order;
10378 break;
10381 /* Move the real (op0) and imaginary (op1) parts to their location. */
10382 write_complex_part (target, op0, false, true);
10383 write_complex_part (target, op1, true, false);
10385 return target;
10387 case WIDEN_SUM_EXPR:
10389 tree oprnd0 = treeop0;
10390 tree oprnd1 = treeop1;
10392 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10393 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10394 target, unsignedp);
10395 return target;
10398 case VEC_UNPACK_HI_EXPR:
10399 case VEC_UNPACK_LO_EXPR:
10400 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10401 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10403 op0 = expand_normal (treeop0);
10404 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10405 target, unsignedp);
10406 gcc_assert (temp);
10407 return temp;
10410 case VEC_UNPACK_FLOAT_HI_EXPR:
10411 case VEC_UNPACK_FLOAT_LO_EXPR:
10413 op0 = expand_normal (treeop0);
10414 /* The signedness is determined from input operand. */
10415 temp = expand_widen_pattern_expr
10416 (ops, op0, NULL_RTX, NULL_RTX,
10417 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10419 gcc_assert (temp);
10420 return temp;
10423 case VEC_WIDEN_MULT_HI_EXPR:
10424 case VEC_WIDEN_MULT_LO_EXPR:
10425 case VEC_WIDEN_MULT_EVEN_EXPR:
10426 case VEC_WIDEN_MULT_ODD_EXPR:
10427 case VEC_WIDEN_LSHIFT_HI_EXPR:
10428 case VEC_WIDEN_LSHIFT_LO_EXPR:
10429 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10430 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10431 target, unsignedp);
10432 gcc_assert (target);
10433 return target;
10435 case VEC_PACK_SAT_EXPR:
10436 case VEC_PACK_FIX_TRUNC_EXPR:
10437 mode = TYPE_MODE (TREE_TYPE (treeop0));
10438 subtarget = NULL_RTX;
10439 goto binop;
10441 case VEC_PACK_TRUNC_EXPR:
10442 if (VECTOR_BOOLEAN_TYPE_P (type)
10443 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10444 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10445 && SCALAR_INT_MODE_P (mode))
10447 class expand_operand eops[4];
10448 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10449 expand_operands (treeop0, treeop1,
10450 subtarget, &op0, &op1, EXPAND_NORMAL);
10451 this_optab = vec_pack_sbool_trunc_optab;
10452 enum insn_code icode = optab_handler (this_optab, imode);
10453 create_output_operand (&eops[0], target, mode);
10454 create_convert_operand_from (&eops[1], op0, imode, false);
10455 create_convert_operand_from (&eops[2], op1, imode, false);
10456 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10457 create_input_operand (&eops[3], temp, imode);
10458 expand_insn (icode, 4, eops);
10459 return eops[0].value;
10461 mode = TYPE_MODE (TREE_TYPE (treeop0));
10462 subtarget = NULL_RTX;
10463 goto binop;
10465 case VEC_PACK_FLOAT_EXPR:
10466 mode = TYPE_MODE (TREE_TYPE (treeop0));
10467 expand_operands (treeop0, treeop1,
10468 subtarget, &op0, &op1, EXPAND_NORMAL);
10469 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10470 optab_default);
10471 target = expand_binop (mode, this_optab, op0, op1, target,
10472 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10473 OPTAB_LIB_WIDEN);
10474 gcc_assert (target);
10475 return target;
10477 case VEC_PERM_EXPR:
10479 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10480 vec_perm_builder sel;
10481 if (TREE_CODE (treeop2) == VECTOR_CST
10482 && tree_to_vec_perm_builder (&sel, treeop2))
10484 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10485 temp = expand_vec_perm_const (mode, op0, op1, sel,
10486 sel_mode, target);
10488 else
10490 op2 = expand_normal (treeop2);
10491 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10493 gcc_assert (temp);
10494 return temp;
10497 case DOT_PROD_EXPR:
10499 tree oprnd0 = treeop0;
10500 tree oprnd1 = treeop1;
10501 tree oprnd2 = treeop2;
10503 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10504 op2 = expand_normal (oprnd2);
10505 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10506 target, unsignedp);
10507 return target;
10510 case SAD_EXPR:
10512 tree oprnd0 = treeop0;
10513 tree oprnd1 = treeop1;
10514 tree oprnd2 = treeop2;
10516 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10517 op2 = expand_normal (oprnd2);
10518 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10519 target, unsignedp);
10520 return target;
10523 case REALIGN_LOAD_EXPR:
10525 tree oprnd0 = treeop0;
10526 tree oprnd1 = treeop1;
10527 tree oprnd2 = treeop2;
10529 this_optab = optab_for_tree_code (code, type, optab_default);
10530 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10531 op2 = expand_normal (oprnd2);
10532 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10533 target, unsignedp);
10534 gcc_assert (temp);
10535 return temp;
10538 case COND_EXPR:
10540 /* A COND_EXPR with its type being VOID_TYPE represents a
10541 conditional jump and is handled in
10542 expand_gimple_cond_expr. */
10543 gcc_assert (!VOID_TYPE_P (type));
10545 /* Note that COND_EXPRs whose type is a structure or union
10546 are required to be constructed to contain assignments of
10547 a temporary variable, so that we can evaluate them here
10548 for side effect only. If type is void, we must do likewise. */
10550 gcc_assert (!TREE_ADDRESSABLE (type)
10551 && !ignore
10552 && TREE_TYPE (treeop1) != void_type_node
10553 && TREE_TYPE (treeop2) != void_type_node);
10555 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10556 if (temp)
10557 return temp;
10559 /* If we are not to produce a result, we have no target. Otherwise,
10560 if a target was specified use it; it will not be used as an
10561 intermediate target unless it is safe. If no target, use a
10562 temporary. */
10564 if (modifier != EXPAND_STACK_PARM
10565 && original_target
10566 && safe_from_p (original_target, treeop0, 1)
10567 && GET_MODE (original_target) == mode
10568 && !MEM_P (original_target))
10569 temp = original_target;
10570 else
10571 temp = assign_temp (type, 0, 1);
10573 do_pending_stack_adjust ();
10574 NO_DEFER_POP;
10575 rtx_code_label *lab0 = gen_label_rtx ();
10576 rtx_code_label *lab1 = gen_label_rtx ();
10577 jumpifnot (treeop0, lab0,
10578 profile_probability::uninitialized ());
10579 store_expr (treeop1, temp,
10580 modifier == EXPAND_STACK_PARM,
10581 false, false);
10583 emit_jump_insn (targetm.gen_jump (lab1));
10584 emit_barrier ();
10585 emit_label (lab0);
10586 store_expr (treeop2, temp,
10587 modifier == EXPAND_STACK_PARM,
10588 false, false);
10590 emit_label (lab1);
10591 OK_DEFER_POP;
10592 return temp;
10595 case VEC_DUPLICATE_EXPR:
10596 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10597 target = expand_vector_broadcast (mode, op0);
10598 gcc_assert (target);
10599 return target;
10601 case VEC_SERIES_EXPR:
10602 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10603 return expand_vec_series_expr (mode, op0, op1, target);
10605 case BIT_INSERT_EXPR:
10607 unsigned bitpos = tree_to_uhwi (treeop2);
10608 unsigned bitsize;
10609 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10610 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10611 else
10612 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10613 op0 = expand_normal (treeop0);
10614 op1 = expand_normal (treeop1);
10615 rtx dst = gen_reg_rtx (mode);
10616 emit_move_insn (dst, op0);
10617 store_bit_field (dst, bitsize, bitpos, 0, 0,
10618 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10619 return dst;
10622 default:
10623 gcc_unreachable ();
10626 /* Here to do an ordinary binary operator. */
10627 binop:
10628 expand_operands (treeop0, treeop1,
10629 subtarget, &op0, &op1, EXPAND_NORMAL);
10630 binop2:
10631 this_optab = optab_for_tree_code (code, type, optab_default);
10632 binop3:
10633 if (modifier == EXPAND_STACK_PARM)
10634 target = 0;
10635 temp = expand_binop (mode, this_optab, op0, op1, target,
10636 unsignedp, OPTAB_LIB_WIDEN);
10637 gcc_assert (temp);
10638 /* Bitwise operations do not need bitfield reduction as we expect their
10639 operands being properly truncated. */
10640 if (code == BIT_XOR_EXPR
10641 || code == BIT_AND_EXPR
10642 || code == BIT_IOR_EXPR)
10643 return temp;
10644 return REDUCE_BIT_FIELD (temp);
10646 #undef REDUCE_BIT_FIELD
10649 /* Return TRUE if expression STMT is suitable for replacement.
10650 Never consider memory loads as replaceable, because those don't ever lead
10651 into constant expressions. */
10653 static bool
10654 stmt_is_replaceable_p (gimple *stmt)
10656 if (ssa_is_replaceable_p (stmt))
10658 /* Don't move around loads. */
10659 if (!gimple_assign_single_p (stmt)
10660 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10661 return true;
10663 return false;
10667 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10668 enum expand_modifier modifier, rtx *alt_rtl,
10669 bool inner_reference_p)
10671 rtx op0, op1, temp, decl_rtl;
10672 tree type;
10673 int unsignedp;
10674 machine_mode mode, dmode;
10675 enum tree_code code = TREE_CODE (exp);
10676 rtx subtarget, original_target;
10677 int ignore;
10678 bool reduce_bit_field;
10679 location_t loc = EXPR_LOCATION (exp);
10680 struct separate_ops ops;
10681 tree treeop0, treeop1, treeop2;
10682 tree ssa_name = NULL_TREE;
10683 gimple *g;
10685 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL
10686 expansion sign/zero extends integral types with less than mode precision
10687 when reading from bit-fields and after arithmetic operations (see
10688 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies
10689 on those extensions to have been already performed, but because of the
10690 above for _BitInt they need to be sign/zero extended when reading from
10691 locations that could be exposed to ABI boundaries (when loading from
10692 objects in memory, or function arguments, return value). Because we
10693 internally extend after arithmetic operations, we can avoid doing that
10694 when reading from SSA_NAMEs of vars. */
10695 #define EXTEND_BITINT(expr) \
10696 ((TREE_CODE (type) == BITINT_TYPE \
10697 && reduce_bit_field \
10698 && mode != BLKmode \
10699 && modifier != EXPAND_MEMORY \
10700 && modifier != EXPAND_WRITE \
10701 && modifier != EXPAND_CONST_ADDRESS) \
10702 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr))
10704 type = TREE_TYPE (exp);
10705 mode = TYPE_MODE (type);
10706 unsignedp = TYPE_UNSIGNED (type);
10708 treeop0 = treeop1 = treeop2 = NULL_TREE;
10709 if (!VL_EXP_CLASS_P (exp))
10710 switch (TREE_CODE_LENGTH (code))
10712 default:
10713 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10714 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10715 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10716 case 0: break;
10718 ops.code = code;
10719 ops.type = type;
10720 ops.op0 = treeop0;
10721 ops.op1 = treeop1;
10722 ops.op2 = treeop2;
10723 ops.location = loc;
10725 ignore = (target == const0_rtx
10726 || ((CONVERT_EXPR_CODE_P (code)
10727 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10728 && TREE_CODE (type) == VOID_TYPE));
10730 /* An operation in what may be a bit-field type needs the
10731 result to be reduced to the precision of the bit-field type,
10732 which is narrower than that of the type's mode. */
10733 reduce_bit_field = (!ignore
10734 && INTEGRAL_TYPE_P (type)
10735 && !type_has_mode_precision_p (type));
10737 /* If we are going to ignore this result, we need only do something
10738 if there is a side-effect somewhere in the expression. If there
10739 is, short-circuit the most common cases here. Note that we must
10740 not call expand_expr with anything but const0_rtx in case this
10741 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10743 if (ignore)
10745 if (! TREE_SIDE_EFFECTS (exp))
10746 return const0_rtx;
10748 /* Ensure we reference a volatile object even if value is ignored, but
10749 don't do this if all we are doing is taking its address. */
10750 if (TREE_THIS_VOLATILE (exp)
10751 && TREE_CODE (exp) != FUNCTION_DECL
10752 && mode != VOIDmode && mode != BLKmode
10753 && modifier != EXPAND_CONST_ADDRESS)
10755 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10756 if (MEM_P (temp))
10757 copy_to_reg (temp);
10758 return const0_rtx;
10761 if (TREE_CODE_CLASS (code) == tcc_unary
10762 || code == BIT_FIELD_REF
10763 || code == COMPONENT_REF
10764 || code == INDIRECT_REF)
10765 return expand_expr (treeop0, const0_rtx, VOIDmode,
10766 modifier);
10768 else if (TREE_CODE_CLASS (code) == tcc_binary
10769 || TREE_CODE_CLASS (code) == tcc_comparison
10770 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10772 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10773 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10774 return const0_rtx;
10777 target = 0;
10780 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10781 target = 0;
10783 /* Use subtarget as the target for operand 0 of a binary operation. */
10784 subtarget = get_subtarget (target);
10785 original_target = target;
10787 switch (code)
10789 case LABEL_DECL:
10791 tree function = decl_function_context (exp);
10793 temp = label_rtx (exp);
10794 temp = gen_rtx_LABEL_REF (Pmode, temp);
10796 if (function != current_function_decl
10797 && function != 0)
10798 LABEL_REF_NONLOCAL_P (temp) = 1;
10800 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10801 return temp;
10804 case SSA_NAME:
10805 /* ??? ivopts calls expander, without any preparation from
10806 out-of-ssa. So fake instructions as if this was an access to the
10807 base variable. This unnecessarily allocates a pseudo, see how we can
10808 reuse it, if partition base vars have it set already. */
10809 if (!currently_expanding_to_rtl)
10811 tree var = SSA_NAME_VAR (exp);
10812 if (var && DECL_RTL_SET_P (var))
10813 return DECL_RTL (var);
10814 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10815 LAST_VIRTUAL_REGISTER + 1);
10818 g = get_gimple_for_ssa_name (exp);
10819 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10820 if (g == NULL
10821 && modifier == EXPAND_INITIALIZER
10822 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10823 && (optimize || !SSA_NAME_VAR (exp)
10824 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10825 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10826 g = SSA_NAME_DEF_STMT (exp);
10827 if (g)
10829 rtx r;
10830 location_t saved_loc = curr_insn_location ();
10831 loc = gimple_location (g);
10832 if (loc != UNKNOWN_LOCATION)
10833 set_curr_insn_location (loc);
10834 ops.code = gimple_assign_rhs_code (g);
10835 switch (get_gimple_rhs_class (ops.code))
10837 case GIMPLE_TERNARY_RHS:
10838 ops.op2 = gimple_assign_rhs3 (g);
10839 /* Fallthru */
10840 case GIMPLE_BINARY_RHS:
10841 ops.op1 = gimple_assign_rhs2 (g);
10843 /* Try to expand conditonal compare. */
10844 if (targetm.gen_ccmp_first)
10846 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10847 r = expand_ccmp_expr (g, mode);
10848 if (r)
10849 break;
10851 /* Fallthru */
10852 case GIMPLE_UNARY_RHS:
10853 ops.op0 = gimple_assign_rhs1 (g);
10854 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10855 ops.location = loc;
10856 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10857 break;
10858 case GIMPLE_SINGLE_RHS:
10860 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10861 tmode, modifier, alt_rtl,
10862 inner_reference_p);
10863 break;
10865 default:
10866 gcc_unreachable ();
10868 set_curr_insn_location (saved_loc);
10869 if (REG_P (r) && !REG_EXPR (r))
10870 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10871 return r;
10874 ssa_name = exp;
10875 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10876 exp = SSA_NAME_VAR (ssa_name);
10877 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an
10878 SSA_NAME computed within the current function. In such case the
10879 value have been already extended before. While if it is a function
10880 parameter, result or some memory location, we need to be prepared
10881 for some other compiler leaving the bits uninitialized. */
10882 if (!exp || VAR_P (exp))
10883 reduce_bit_field = false;
10884 goto expand_decl_rtl;
10886 case VAR_DECL:
10887 /* Allow accel compiler to handle variables that require special
10888 treatment, e.g. if they have been modified in some way earlier in
10889 compilation by the adjust_private_decl OpenACC hook. */
10890 if (flag_openacc && targetm.goacc.expand_var_decl)
10892 temp = targetm.goacc.expand_var_decl (exp);
10893 if (temp)
10894 return temp;
10896 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10897 have scalar integer modes to a reg via store_constructor. */
10898 if (TREE_READONLY (exp)
10899 && !TREE_SIDE_EFFECTS (exp)
10900 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10901 && immediate_const_ctor_p (DECL_INITIAL (exp))
10902 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10903 && crtl->emit.regno_pointer_align_length
10904 && !target)
10906 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10907 store_constructor (DECL_INITIAL (exp), target, 0,
10908 int_expr_size (DECL_INITIAL (exp)), false);
10909 return target;
10911 /* ... fall through ... */
10913 case PARM_DECL:
10914 /* If a static var's type was incomplete when the decl was written,
10915 but the type is complete now, lay out the decl now. */
10916 if (DECL_SIZE (exp) == 0
10917 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10918 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10919 layout_decl (exp, 0);
10921 /* fall through */
10923 case FUNCTION_DECL:
10924 case RESULT_DECL:
10925 decl_rtl = DECL_RTL (exp);
10926 expand_decl_rtl:
10927 gcc_assert (decl_rtl);
10929 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10930 settings for VECTOR_TYPE_P that might switch for the function. */
10931 if (currently_expanding_to_rtl
10932 && code == VAR_DECL && MEM_P (decl_rtl)
10933 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10934 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10935 else
10936 decl_rtl = copy_rtx (decl_rtl);
10938 /* Record writes to register variables. */
10939 if (modifier == EXPAND_WRITE
10940 && REG_P (decl_rtl)
10941 && HARD_REGISTER_P (decl_rtl))
10942 add_to_hard_reg_set (&crtl->asm_clobbers,
10943 GET_MODE (decl_rtl), REGNO (decl_rtl));
10945 /* Ensure variable marked as used even if it doesn't go through
10946 a parser. If it hasn't be used yet, write out an external
10947 definition. */
10948 if (exp)
10949 TREE_USED (exp) = 1;
10951 /* Show we haven't gotten RTL for this yet. */
10952 temp = 0;
10954 /* Variables inherited from containing functions should have
10955 been lowered by this point. */
10956 if (exp)
10958 tree context = decl_function_context (exp);
10959 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10960 || context == current_function_decl
10961 || TREE_STATIC (exp)
10962 || DECL_EXTERNAL (exp)
10963 /* ??? C++ creates functions that are not
10964 TREE_STATIC. */
10965 || TREE_CODE (exp) == FUNCTION_DECL);
10968 /* This is the case of an array whose size is to be determined
10969 from its initializer, while the initializer is still being parsed.
10970 ??? We aren't parsing while expanding anymore. */
10972 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10973 temp = validize_mem (decl_rtl);
10975 /* If DECL_RTL is memory, we are in the normal case and the
10976 address is not valid, get the address into a register. */
10978 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10980 if (alt_rtl)
10981 *alt_rtl = decl_rtl;
10982 decl_rtl = use_anchored_address (decl_rtl);
10983 if (modifier != EXPAND_CONST_ADDRESS
10984 && modifier != EXPAND_SUM
10985 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10986 : GET_MODE (decl_rtl),
10987 XEXP (decl_rtl, 0),
10988 MEM_ADDR_SPACE (decl_rtl)))
10989 temp = replace_equiv_address (decl_rtl,
10990 copy_rtx (XEXP (decl_rtl, 0)));
10993 /* If we got something, return it. But first, set the alignment
10994 if the address is a register. */
10995 if (temp != 0)
10997 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10998 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
11000 else if (MEM_P (decl_rtl))
11001 temp = decl_rtl;
11003 if (temp != 0)
11005 if (MEM_P (temp)
11006 && modifier != EXPAND_WRITE
11007 && modifier != EXPAND_MEMORY
11008 && modifier != EXPAND_INITIALIZER
11009 && modifier != EXPAND_CONST_ADDRESS
11010 && modifier != EXPAND_SUM
11011 && !inner_reference_p
11012 && mode != BLKmode
11013 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
11014 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11015 MEM_ALIGN (temp), NULL_RTX, NULL);
11017 return EXTEND_BITINT (temp);
11020 if (exp)
11021 dmode = DECL_MODE (exp);
11022 else
11023 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
11025 /* If the mode of DECL_RTL does not match that of the decl,
11026 there are two cases: we are dealing with a BLKmode value
11027 that is returned in a register, or we are dealing with
11028 a promoted value. In the latter case, return a SUBREG
11029 of the wanted mode, but mark it so that we know that it
11030 was already extended. */
11031 if (REG_P (decl_rtl)
11032 && dmode != BLKmode
11033 && GET_MODE (decl_rtl) != dmode)
11035 machine_mode pmode;
11037 /* Get the signedness to be used for this variable. Ensure we get
11038 the same mode we got when the variable was declared. */
11039 if (code != SSA_NAME)
11040 pmode = promote_decl_mode (exp, &unsignedp);
11041 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
11042 && gimple_code (g) == GIMPLE_CALL
11043 && !gimple_call_internal_p (g))
11044 pmode = promote_function_mode (type, mode, &unsignedp,
11045 gimple_call_fntype (g),
11047 else
11048 pmode = promote_ssa_mode (ssa_name, &unsignedp);
11049 gcc_assert (GET_MODE (decl_rtl) == pmode);
11051 /* Some ABIs require scalar floating point modes to be passed
11052 in a wider scalar integer mode. We need to explicitly
11053 truncate to an integer mode of the correct precision before
11054 using a SUBREG to reinterpret as a floating point value. */
11055 if (SCALAR_FLOAT_MODE_P (mode)
11056 && SCALAR_INT_MODE_P (pmode)
11057 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11058 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11060 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11061 SUBREG_PROMOTED_VAR_P (temp) = 1;
11062 SUBREG_PROMOTED_SET (temp, unsignedp);
11063 return EXTEND_BITINT (temp);
11066 return EXTEND_BITINT (decl_rtl);
11068 case INTEGER_CST:
11070 if (TREE_CODE (type) == BITINT_TYPE)
11072 unsigned int prec = TYPE_PRECISION (type);
11073 struct bitint_info info;
11074 bool ok = targetm.c.bitint_type_info (prec, &info);
11075 gcc_assert (ok);
11076 scalar_int_mode limb_mode
11077 = as_a <scalar_int_mode> (info.limb_mode);
11078 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode);
11079 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE)
11081 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */
11082 exp = tree_output_constant_def (exp);
11083 return expand_expr (exp, target, VOIDmode, modifier);
11087 /* Given that TYPE_PRECISION (type) is not always equal to
11088 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11089 the former to the latter according to the signedness of the
11090 type. */
11091 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11092 temp = immed_wide_int_const
11093 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11094 return temp;
11097 case VECTOR_CST:
11099 tree tmp = NULL_TREE;
11100 if (VECTOR_MODE_P (mode))
11101 return const_vector_from_tree (exp);
11102 scalar_int_mode int_mode;
11103 if (is_int_mode (mode, &int_mode))
11105 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11106 if (type_for_mode)
11107 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11108 type_for_mode, exp);
11110 if (!tmp)
11112 vec<constructor_elt, va_gc> *v;
11113 /* Constructors need to be fixed-length. FIXME. */
11114 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11115 vec_alloc (v, nunits);
11116 for (unsigned int i = 0; i < nunits; ++i)
11117 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11118 tmp = build_constructor (type, v);
11120 return expand_expr (tmp, ignore ? const0_rtx : target,
11121 tmode, modifier);
11124 case CONST_DECL:
11125 if (modifier == EXPAND_WRITE)
11127 /* Writing into CONST_DECL is always invalid, but handle it
11128 gracefully. */
11129 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11130 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11131 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11132 EXPAND_NORMAL, as);
11133 op0 = memory_address_addr_space (mode, op0, as);
11134 temp = gen_rtx_MEM (mode, op0);
11135 set_mem_addr_space (temp, as);
11136 return temp;
11138 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11140 case REAL_CST:
11141 /* If optimized, generate immediate CONST_DOUBLE
11142 which will be turned into memory by reload if necessary.
11144 We used to force a register so that loop.c could see it. But
11145 this does not allow gen_* patterns to perform optimizations with
11146 the constants. It also produces two insns in cases like "x = 1.0;".
11147 On most machines, floating-point constants are not permitted in
11148 many insns, so we'd end up copying it to a register in any case.
11150 Now, we do the copying in expand_binop, if appropriate. */
11151 return const_double_from_real_value (TREE_REAL_CST (exp),
11152 TYPE_MODE (TREE_TYPE (exp)));
11154 case FIXED_CST:
11155 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11156 TYPE_MODE (TREE_TYPE (exp)));
11158 case COMPLEX_CST:
11159 /* Handle evaluating a complex constant in a CONCAT target. */
11160 if (original_target && GET_CODE (original_target) == CONCAT)
11162 rtx rtarg, itarg;
11164 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11165 rtarg = XEXP (original_target, 0);
11166 itarg = XEXP (original_target, 1);
11168 /* Move the real and imaginary parts separately. */
11169 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11170 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11172 if (op0 != rtarg)
11173 emit_move_insn (rtarg, op0);
11174 if (op1 != itarg)
11175 emit_move_insn (itarg, op1);
11177 return original_target;
11180 /* fall through */
11182 case STRING_CST:
11183 temp = expand_expr_constant (exp, 1, modifier);
11185 /* temp contains a constant address.
11186 On RISC machines where a constant address isn't valid,
11187 make some insns to get that address into a register. */
11188 if (modifier != EXPAND_CONST_ADDRESS
11189 && modifier != EXPAND_INITIALIZER
11190 && modifier != EXPAND_SUM
11191 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11192 MEM_ADDR_SPACE (temp)))
11193 return replace_equiv_address (temp,
11194 copy_rtx (XEXP (temp, 0)));
11195 return temp;
11197 case POLY_INT_CST:
11198 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11200 case SAVE_EXPR:
11202 tree val = treeop0;
11203 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11204 inner_reference_p);
11206 if (!SAVE_EXPR_RESOLVED_P (exp))
11208 /* We can indeed still hit this case, typically via builtin
11209 expanders calling save_expr immediately before expanding
11210 something. Assume this means that we only have to deal
11211 with non-BLKmode values. */
11212 gcc_assert (GET_MODE (ret) != BLKmode);
11214 val = build_decl (curr_insn_location (),
11215 VAR_DECL, NULL, TREE_TYPE (exp));
11216 DECL_ARTIFICIAL (val) = 1;
11217 DECL_IGNORED_P (val) = 1;
11218 treeop0 = val;
11219 TREE_OPERAND (exp, 0) = treeop0;
11220 SAVE_EXPR_RESOLVED_P (exp) = 1;
11222 if (!CONSTANT_P (ret))
11223 ret = copy_to_reg (ret);
11224 SET_DECL_RTL (val, ret);
11227 return ret;
11231 case CONSTRUCTOR:
11232 /* If we don't need the result, just ensure we evaluate any
11233 subexpressions. */
11234 if (ignore)
11236 unsigned HOST_WIDE_INT idx;
11237 tree value;
11239 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11240 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11242 return const0_rtx;
11245 return expand_constructor (exp, target, modifier, false);
11247 case TARGET_MEM_REF:
11249 addr_space_t as
11250 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11251 unsigned int align;
11253 op0 = addr_for_mem_ref (exp, as, true);
11254 op0 = memory_address_addr_space (mode, op0, as);
11255 temp = gen_rtx_MEM (mode, op0);
11256 set_mem_attributes (temp, exp, 0);
11257 set_mem_addr_space (temp, as);
11258 align = get_object_alignment (exp);
11259 if (modifier != EXPAND_WRITE
11260 && modifier != EXPAND_MEMORY
11261 && mode != BLKmode
11262 && align < GET_MODE_ALIGNMENT (mode))
11263 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11264 align, NULL_RTX, NULL);
11265 return EXTEND_BITINT (temp);
11268 case MEM_REF:
11270 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11271 addr_space_t as
11272 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11273 machine_mode address_mode;
11274 tree base = TREE_OPERAND (exp, 0);
11275 gimple *def_stmt;
11276 unsigned align;
11277 /* Handle expansion of non-aliased memory with non-BLKmode. That
11278 might end up in a register. */
11279 if (mem_ref_refers_to_non_mem_p (exp))
11281 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11282 base = TREE_OPERAND (base, 0);
11283 poly_uint64 type_size;
11284 if (known_eq (offset, 0)
11285 && !reverse
11286 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11287 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11288 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11289 target, tmode, modifier);
11290 if (TYPE_MODE (type) == BLKmode)
11292 temp = assign_stack_temp (DECL_MODE (base),
11293 GET_MODE_SIZE (DECL_MODE (base)));
11294 store_expr (base, temp, 0, false, false);
11295 temp = adjust_address (temp, BLKmode, offset);
11296 set_mem_size (temp, int_size_in_bytes (type));
11297 return temp;
11299 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11300 bitsize_int (offset * BITS_PER_UNIT));
11301 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11302 return expand_expr (exp, target, tmode, modifier);
11304 address_mode = targetm.addr_space.address_mode (as);
11305 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11307 tree mask = gimple_assign_rhs2 (def_stmt);
11308 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11309 gimple_assign_rhs1 (def_stmt), mask);
11310 TREE_OPERAND (exp, 0) = base;
11312 align = get_object_alignment (exp);
11313 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11314 op0 = memory_address_addr_space (mode, op0, as);
11315 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11317 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11318 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11319 op0 = memory_address_addr_space (mode, op0, as);
11321 temp = gen_rtx_MEM (mode, op0);
11322 set_mem_attributes (temp, exp, 0);
11323 set_mem_addr_space (temp, as);
11324 if (TREE_THIS_VOLATILE (exp))
11325 MEM_VOLATILE_P (temp) = 1;
11326 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY)
11327 return temp;
11328 if (!inner_reference_p
11329 && mode != BLKmode
11330 && align < GET_MODE_ALIGNMENT (mode))
11331 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11332 modifier == EXPAND_STACK_PARM
11333 ? NULL_RTX : target, alt_rtl);
11334 if (reverse)
11335 temp = flip_storage_order (mode, temp);
11336 return EXTEND_BITINT (temp);
11339 case ARRAY_REF:
11342 tree array = treeop0;
11343 tree index = treeop1;
11344 tree init;
11346 /* Fold an expression like: "foo"[2].
11347 This is not done in fold so it won't happen inside &.
11348 Don't fold if this is for wide characters since it's too
11349 difficult to do correctly and this is a very rare case. */
11351 if (modifier != EXPAND_CONST_ADDRESS
11352 && modifier != EXPAND_INITIALIZER
11353 && modifier != EXPAND_MEMORY)
11355 tree t = fold_read_from_constant_string (exp);
11357 if (t)
11358 return expand_expr (t, target, tmode, modifier);
11361 /* If this is a constant index into a constant array,
11362 just get the value from the array. Handle both the cases when
11363 we have an explicit constructor and when our operand is a variable
11364 that was declared const. */
11366 if (modifier != EXPAND_CONST_ADDRESS
11367 && modifier != EXPAND_INITIALIZER
11368 && modifier != EXPAND_MEMORY
11369 && TREE_CODE (array) == CONSTRUCTOR
11370 && ! TREE_SIDE_EFFECTS (array)
11371 && TREE_CODE (index) == INTEGER_CST)
11373 unsigned HOST_WIDE_INT ix;
11374 tree field, value;
11376 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11377 field, value)
11378 if (tree_int_cst_equal (field, index))
11380 if (!TREE_SIDE_EFFECTS (value))
11381 return expand_expr (fold (value), target, tmode, modifier);
11382 break;
11386 else if (optimize >= 1
11387 && modifier != EXPAND_CONST_ADDRESS
11388 && modifier != EXPAND_INITIALIZER
11389 && modifier != EXPAND_MEMORY
11390 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11391 && TREE_CODE (index) == INTEGER_CST
11392 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11393 && (init = ctor_for_folding (array)) != error_mark_node)
11395 if (init == NULL_TREE)
11397 tree value = build_zero_cst (type);
11398 if (TREE_CODE (value) == CONSTRUCTOR)
11400 /* If VALUE is a CONSTRUCTOR, this optimization is only
11401 useful if this doesn't store the CONSTRUCTOR into
11402 memory. If it does, it is more efficient to just
11403 load the data from the array directly. */
11404 rtx ret = expand_constructor (value, target,
11405 modifier, true);
11406 if (ret == NULL_RTX)
11407 value = NULL_TREE;
11410 if (value)
11411 return expand_expr (value, target, tmode, modifier);
11413 else if (TREE_CODE (init) == CONSTRUCTOR)
11415 unsigned HOST_WIDE_INT ix;
11416 tree field, value;
11418 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11419 field, value)
11420 if (tree_int_cst_equal (field, index))
11422 if (TREE_SIDE_EFFECTS (value))
11423 break;
11425 if (TREE_CODE (value) == CONSTRUCTOR)
11427 /* If VALUE is a CONSTRUCTOR, this
11428 optimization is only useful if
11429 this doesn't store the CONSTRUCTOR
11430 into memory. If it does, it is more
11431 efficient to just load the data from
11432 the array directly. */
11433 rtx ret = expand_constructor (value, target,
11434 modifier, true);
11435 if (ret == NULL_RTX)
11436 break;
11439 return
11440 expand_expr (fold (value), target, tmode, modifier);
11443 else if (TREE_CODE (init) == STRING_CST)
11445 tree low_bound = array_ref_low_bound (exp);
11446 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11448 /* Optimize the special case of a zero lower bound.
11450 We convert the lower bound to sizetype to avoid problems
11451 with constant folding. E.g. suppose the lower bound is
11452 1 and its mode is QI. Without the conversion
11453 (ARRAY + (INDEX - (unsigned char)1))
11454 becomes
11455 (ARRAY + (-(unsigned char)1) + INDEX)
11456 which becomes
11457 (ARRAY + 255 + INDEX). Oops! */
11458 if (!integer_zerop (low_bound))
11459 index1 = size_diffop_loc (loc, index1,
11460 fold_convert_loc (loc, sizetype,
11461 low_bound));
11463 if (tree_fits_uhwi_p (index1)
11464 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11466 tree char_type = TREE_TYPE (TREE_TYPE (init));
11467 scalar_int_mode char_mode;
11469 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11470 && GET_MODE_SIZE (char_mode) == 1)
11471 return gen_int_mode (TREE_STRING_POINTER (init)
11472 [TREE_INT_CST_LOW (index1)],
11473 char_mode);
11478 goto normal_inner_ref;
11480 case COMPONENT_REF:
11481 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11482 /* Fall through. */
11483 case BIT_FIELD_REF:
11484 case ARRAY_RANGE_REF:
11485 normal_inner_ref:
11487 machine_mode mode1, mode2;
11488 poly_int64 bitsize, bitpos, bytepos;
11489 tree offset;
11490 int reversep, volatilep = 0;
11491 tree tem
11492 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11493 &unsignedp, &reversep, &volatilep);
11494 rtx orig_op0, memloc;
11495 bool clear_mem_expr = false;
11496 bool must_force_mem;
11498 /* If we got back the original object, something is wrong. Perhaps
11499 we are evaluating an expression too early. In any event, don't
11500 infinitely recurse. */
11501 gcc_assert (tem != exp);
11503 /* Make sure bitpos is not negative, this can wreak havoc later. */
11504 if (maybe_lt (bitpos, 0))
11506 gcc_checking_assert (offset == NULL_TREE);
11507 offset = size_int (bits_to_bytes_round_down (bitpos));
11508 bitpos = num_trailing_bits (bitpos);
11511 /* If we have either an offset, a BLKmode result, or a reference
11512 outside the underlying object, we must force it to memory.
11513 Such a case can occur in Ada if we have unchecked conversion
11514 of an expression from a scalar type to an aggregate type or
11515 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11516 passed a partially uninitialized object or a view-conversion
11517 to a larger size. */
11518 must_force_mem = offset != NULL_TREE
11519 || mode1 == BLKmode
11520 || (mode == BLKmode
11521 && !int_mode_for_size (bitsize, 1).exists ());
11523 const enum expand_modifier tem_modifier
11524 = must_force_mem
11525 ? EXPAND_MEMORY
11526 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11528 /* If TEM's type is a union of variable size, pass TARGET to the inner
11529 computation, since it will need a temporary and TARGET is known
11530 to have to do. This occurs in unchecked conversion in Ada. */
11531 const rtx tem_target
11532 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11533 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11534 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11535 && modifier != EXPAND_STACK_PARM
11536 ? target
11537 : NULL_RTX;
11539 orig_op0 = op0
11540 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11541 true);
11543 /* If the field has a mode, we want to access it in the
11544 field's mode, not the computed mode.
11545 If a MEM has VOIDmode (external with incomplete type),
11546 use BLKmode for it instead. */
11547 if (MEM_P (op0))
11549 if (mode1 != VOIDmode)
11550 op0 = adjust_address (op0, mode1, 0);
11551 else if (GET_MODE (op0) == VOIDmode)
11552 op0 = adjust_address (op0, BLKmode, 0);
11555 mode2
11556 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11558 /* See above for the rationale. */
11559 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11560 must_force_mem = true;
11562 /* Handle CONCAT first. */
11563 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11565 if (known_eq (bitpos, 0)
11566 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11567 && COMPLEX_MODE_P (mode1)
11568 && COMPLEX_MODE_P (GET_MODE (op0))
11569 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11570 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11572 if (reversep)
11573 op0 = flip_storage_order (GET_MODE (op0), op0);
11574 if (mode1 != GET_MODE (op0))
11576 rtx parts[2];
11577 for (int i = 0; i < 2; i++)
11579 rtx op = read_complex_part (op0, i != 0);
11580 if (GET_CODE (op) == SUBREG)
11581 op = force_reg (GET_MODE (op), op);
11582 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11583 if (temp)
11584 op = temp;
11585 else
11587 if (!REG_P (op) && !MEM_P (op))
11588 op = force_reg (GET_MODE (op), op);
11589 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11591 parts[i] = op;
11593 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11595 return op0;
11597 if (known_eq (bitpos, 0)
11598 && known_eq (bitsize,
11599 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11600 && maybe_ne (bitsize, 0))
11602 op0 = XEXP (op0, 0);
11603 mode2 = GET_MODE (op0);
11605 else if (known_eq (bitpos,
11606 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11607 && known_eq (bitsize,
11608 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11609 && maybe_ne (bitpos, 0)
11610 && maybe_ne (bitsize, 0))
11612 op0 = XEXP (op0, 1);
11613 bitpos = 0;
11614 mode2 = GET_MODE (op0);
11616 else
11617 /* Otherwise force into memory. */
11618 must_force_mem = true;
11621 /* If this is a constant, put it in a register if it is a legitimate
11622 constant and we don't need a memory reference. */
11623 if (CONSTANT_P (op0)
11624 && mode2 != BLKmode
11625 && targetm.legitimate_constant_p (mode2, op0)
11626 && !must_force_mem)
11627 op0 = force_reg (mode2, op0);
11629 /* Otherwise, if this is a constant, try to force it to the constant
11630 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11631 is a legitimate constant. */
11632 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11633 op0 = validize_mem (memloc);
11635 /* Otherwise, if this is a constant or the object is not in memory
11636 and need be, put it there. */
11637 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11639 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11640 emit_move_insn (memloc, op0);
11641 op0 = memloc;
11642 clear_mem_expr = true;
11645 if (offset)
11647 machine_mode address_mode;
11648 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11649 EXPAND_SUM);
11651 gcc_assert (MEM_P (op0));
11653 address_mode = get_address_mode (op0);
11654 if (GET_MODE (offset_rtx) != address_mode)
11656 /* We cannot be sure that the RTL in offset_rtx is valid outside
11657 of a memory address context, so force it into a register
11658 before attempting to convert it to the desired mode. */
11659 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11660 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11663 /* See the comment in expand_assignment for the rationale. */
11664 if (mode1 != VOIDmode
11665 && maybe_ne (bitpos, 0)
11666 && maybe_gt (bitsize, 0)
11667 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11668 && multiple_p (bitpos, bitsize)
11669 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11670 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11672 op0 = adjust_address (op0, mode1, bytepos);
11673 bitpos = 0;
11676 op0 = offset_address (op0, offset_rtx,
11677 highest_pow2_factor (offset));
11680 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11681 record its alignment as BIGGEST_ALIGNMENT. */
11682 if (MEM_P (op0)
11683 && known_eq (bitpos, 0)
11684 && offset != 0
11685 && is_aligning_offset (offset, tem))
11686 set_mem_align (op0, BIGGEST_ALIGNMENT);
11688 /* Don't forget about volatility even if this is a bitfield. */
11689 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11691 if (op0 == orig_op0)
11692 op0 = copy_rtx (op0);
11694 MEM_VOLATILE_P (op0) = 1;
11697 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11699 if (op0 == orig_op0)
11700 op0 = copy_rtx (op0);
11702 set_mem_align (op0, BITS_PER_UNIT);
11705 /* In cases where an aligned union has an unaligned object
11706 as a field, we might be extracting a BLKmode value from
11707 an integer-mode (e.g., SImode) object. Handle this case
11708 by doing the extract into an object as wide as the field
11709 (which we know to be the width of a basic mode), then
11710 storing into memory, and changing the mode to BLKmode. */
11711 if (mode1 == VOIDmode
11712 || REG_P (op0) || GET_CODE (op0) == SUBREG
11713 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11714 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11715 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11716 && modifier != EXPAND_CONST_ADDRESS
11717 && modifier != EXPAND_INITIALIZER
11718 && modifier != EXPAND_MEMORY)
11719 /* If the bitfield is volatile and the bitsize
11720 is narrower than the access size of the bitfield,
11721 we need to extract bitfields from the access. */
11722 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11723 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11724 && mode1 != BLKmode
11725 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11726 /* If the field isn't aligned enough to fetch as a memref,
11727 fetch it as a bit field. */
11728 || (mode1 != BLKmode
11729 && (((MEM_P (op0)
11730 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11731 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11732 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11733 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11734 && modifier != EXPAND_MEMORY
11735 && ((modifier == EXPAND_CONST_ADDRESS
11736 || modifier == EXPAND_INITIALIZER)
11737 ? STRICT_ALIGNMENT
11738 : targetm.slow_unaligned_access (mode1,
11739 MEM_ALIGN (op0))))
11740 || !multiple_p (bitpos, BITS_PER_UNIT)))
11741 /* If the type and the field are a constant size and the
11742 size of the type isn't the same size as the bitfield,
11743 we must use bitfield operations. */
11744 || (known_size_p (bitsize)
11745 && TYPE_SIZE (TREE_TYPE (exp))
11746 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11747 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11748 bitsize)))
11750 machine_mode ext_mode = mode;
11752 if (ext_mode == BLKmode
11753 && ! (target != 0 && MEM_P (op0)
11754 && MEM_P (target)
11755 && multiple_p (bitpos, BITS_PER_UNIT)))
11756 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11758 if (ext_mode == BLKmode)
11760 if (target == 0)
11761 target = assign_temp (type, 1, 1);
11763 /* ??? Unlike the similar test a few lines below, this one is
11764 very likely obsolete. */
11765 if (known_eq (bitsize, 0))
11766 return target;
11768 /* In this case, BITPOS must start at a byte boundary and
11769 TARGET, if specified, must be a MEM. */
11770 gcc_assert (MEM_P (op0)
11771 && (!target || MEM_P (target)));
11773 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11774 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11775 emit_block_move (target,
11776 adjust_address (op0, VOIDmode, bytepos),
11777 gen_int_mode (bytesize, Pmode),
11778 (modifier == EXPAND_STACK_PARM
11779 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11781 return target;
11784 /* If we have nothing to extract, the result will be 0 for targets
11785 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11786 return 0 for the sake of consistency, as reading a zero-sized
11787 bitfield is valid in Ada and the value is fully specified. */
11788 if (known_eq (bitsize, 0))
11789 return const0_rtx;
11791 op0 = validize_mem (op0);
11793 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11794 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11796 /* If the result has aggregate type and the extraction is done in
11797 an integral mode, then the field may be not aligned on a byte
11798 boundary; in this case, if it has reverse storage order, it
11799 needs to be extracted as a scalar field with reverse storage
11800 order and put back into memory order afterwards. */
11801 if (AGGREGATE_TYPE_P (type)
11802 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11803 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11805 gcc_checking_assert (known_ge (bitpos, 0));
11806 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11807 (modifier == EXPAND_STACK_PARM
11808 ? NULL_RTX : target),
11809 ext_mode, ext_mode, reversep, alt_rtl);
11811 /* If the result has aggregate type and the mode of OP0 is an
11812 integral mode then, if BITSIZE is narrower than this mode
11813 and this is for big-endian data, we must put the field
11814 into the high-order bits. And we must also put it back
11815 into memory order if it has been previously reversed. */
11816 scalar_int_mode op0_mode;
11817 if (AGGREGATE_TYPE_P (type)
11818 && is_int_mode (GET_MODE (op0), &op0_mode))
11820 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11822 gcc_checking_assert (known_le (bitsize, size));
11823 if (maybe_lt (bitsize, size)
11824 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11825 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11826 size - bitsize, op0, 1);
11828 if (reversep)
11829 op0 = flip_storage_order (op0_mode, op0);
11832 /* If the result type is BLKmode, store the data into a temporary
11833 of the appropriate type, but with the mode corresponding to the
11834 mode for the data we have (op0's mode). */
11835 if (mode == BLKmode)
11837 rtx new_rtx
11838 = assign_stack_temp_for_type (ext_mode,
11839 GET_MODE_BITSIZE (ext_mode),
11840 type);
11841 emit_move_insn (new_rtx, op0);
11842 op0 = copy_rtx (new_rtx);
11843 PUT_MODE (op0, BLKmode);
11846 return op0;
11849 /* If the result is BLKmode, use that to access the object
11850 now as well. */
11851 if (mode == BLKmode)
11852 mode1 = BLKmode;
11854 /* Get a reference to just this component. */
11855 bytepos = bits_to_bytes_round_down (bitpos);
11856 if (modifier == EXPAND_CONST_ADDRESS
11857 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11858 op0 = adjust_address_nv (op0, mode1, bytepos);
11859 else
11860 op0 = adjust_address (op0, mode1, bytepos);
11862 if (op0 == orig_op0)
11863 op0 = copy_rtx (op0);
11865 /* Don't set memory attributes if the base expression is
11866 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11867 we should just honor its original memory attributes. */
11868 if (!(TREE_CODE (tem) == SSA_NAME
11869 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11870 set_mem_attributes (op0, exp, 0);
11872 if (REG_P (XEXP (op0, 0)))
11873 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11875 /* If op0 is a temporary because the original expressions was forced
11876 to memory, clear MEM_EXPR so that the original expression cannot
11877 be marked as addressable through MEM_EXPR of the temporary. */
11878 if (clear_mem_expr)
11879 set_mem_expr (op0, NULL_TREE);
11881 MEM_VOLATILE_P (op0) |= volatilep;
11883 if (reversep
11884 && modifier != EXPAND_MEMORY
11885 && modifier != EXPAND_WRITE)
11886 op0 = flip_storage_order (mode1, op0);
11888 op0 = EXTEND_BITINT (op0);
11890 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11891 || modifier == EXPAND_CONST_ADDRESS
11892 || modifier == EXPAND_INITIALIZER)
11893 return op0;
11895 if (target == 0)
11896 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11898 convert_move (target, op0, unsignedp);
11899 return target;
11902 case OBJ_TYPE_REF:
11903 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11905 case CALL_EXPR:
11906 /* All valid uses of __builtin_va_arg_pack () are removed during
11907 inlining. */
11908 if (CALL_EXPR_VA_ARG_PACK (exp))
11909 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11911 tree fndecl = get_callee_fndecl (exp), attr;
11913 if (fndecl
11914 /* Don't diagnose the error attribute in thunks, those are
11915 artificially created. */
11916 && !CALL_FROM_THUNK_P (exp)
11917 && (attr = lookup_attribute ("error",
11918 DECL_ATTRIBUTES (fndecl))) != NULL)
11920 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11921 error ("call to %qs declared with attribute error: %s",
11922 identifier_to_locale (ident),
11923 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11925 if (fndecl
11926 /* Don't diagnose the warning attribute in thunks, those are
11927 artificially created. */
11928 && !CALL_FROM_THUNK_P (exp)
11929 && (attr = lookup_attribute ("warning",
11930 DECL_ATTRIBUTES (fndecl))) != NULL)
11932 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11933 warning_at (EXPR_LOCATION (exp),
11934 OPT_Wattribute_warning,
11935 "call to %qs declared with attribute warning: %s",
11936 identifier_to_locale (ident),
11937 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11940 /* Check for a built-in function. */
11941 if (fndecl && fndecl_built_in_p (fndecl))
11943 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11944 return expand_builtin (exp, target, subtarget, tmode, ignore);
11947 return expand_call (exp, target, ignore);
11949 case VIEW_CONVERT_EXPR:
11950 op0 = NULL_RTX;
11952 /* If we are converting to BLKmode, try to avoid an intermediate
11953 temporary by fetching an inner memory reference. */
11954 if (mode == BLKmode
11955 && poly_int_tree_p (TYPE_SIZE (type))
11956 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11957 && handled_component_p (treeop0))
11959 machine_mode mode1;
11960 poly_int64 bitsize, bitpos, bytepos;
11961 tree offset;
11962 int reversep, volatilep = 0;
11963 tree tem
11964 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11965 &unsignedp, &reversep, &volatilep);
11967 /* ??? We should work harder and deal with non-zero offsets. */
11968 if (!offset
11969 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11970 && !reversep
11971 && known_size_p (bitsize)
11972 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11974 /* See the normal_inner_ref case for the rationale. */
11975 rtx orig_op0
11976 = expand_expr_real (tem,
11977 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11978 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11979 != INTEGER_CST)
11980 && modifier != EXPAND_STACK_PARM
11981 ? target : NULL_RTX),
11982 VOIDmode,
11983 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11984 NULL, true);
11986 if (MEM_P (orig_op0))
11988 op0 = orig_op0;
11990 /* Get a reference to just this component. */
11991 if (modifier == EXPAND_CONST_ADDRESS
11992 || modifier == EXPAND_SUM
11993 || modifier == EXPAND_INITIALIZER)
11994 op0 = adjust_address_nv (op0, mode, bytepos);
11995 else
11996 op0 = adjust_address (op0, mode, bytepos);
11998 if (op0 == orig_op0)
11999 op0 = copy_rtx (op0);
12001 set_mem_attributes (op0, treeop0, 0);
12002 if (REG_P (XEXP (op0, 0)))
12003 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12005 MEM_VOLATILE_P (op0) |= volatilep;
12010 if (!op0)
12011 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
12012 NULL, inner_reference_p);
12014 /* If the input and output modes are both the same, we are done. */
12015 if (mode == GET_MODE (op0))
12017 /* If neither mode is BLKmode, and both modes are the same size
12018 then we can use gen_lowpart. */
12019 else if (mode != BLKmode
12020 && GET_MODE (op0) != BLKmode
12021 && known_eq (GET_MODE_PRECISION (mode),
12022 GET_MODE_PRECISION (GET_MODE (op0)))
12023 && !COMPLEX_MODE_P (GET_MODE (op0)))
12025 if (GET_CODE (op0) == SUBREG)
12026 op0 = force_reg (GET_MODE (op0), op0);
12027 temp = gen_lowpart_common (mode, op0);
12028 if (temp)
12029 op0 = temp;
12030 else
12032 if (!REG_P (op0) && !MEM_P (op0))
12033 op0 = force_reg (GET_MODE (op0), op0);
12034 op0 = gen_lowpart (mode, op0);
12037 /* If both types are integral, convert from one mode to the other. */
12038 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
12039 op0 = convert_modes (mode, GET_MODE (op0), op0,
12040 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
12041 /* If the output type is a bit-field type, do an extraction. */
12042 else if (reduce_bit_field)
12043 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12044 TYPE_UNSIGNED (type), NULL_RTX,
12045 mode, mode, false, NULL);
12046 /* As a last resort, spill op0 to memory, and reload it in a
12047 different mode. */
12048 else if (!MEM_P (op0))
12050 /* If the operand is not a MEM, force it into memory. Since we
12051 are going to be changing the mode of the MEM, don't call
12052 force_const_mem for constants because we don't allow pool
12053 constants to change mode. */
12054 tree inner_type = TREE_TYPE (treeop0);
12056 gcc_assert (!TREE_ADDRESSABLE (exp));
12058 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
12059 target
12060 = assign_stack_temp_for_type
12061 (TYPE_MODE (inner_type),
12062 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
12064 emit_move_insn (target, op0);
12065 op0 = target;
12068 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
12069 output type is such that the operand is known to be aligned, indicate
12070 that it is. Otherwise, we need only be concerned about alignment for
12071 non-BLKmode results. */
12072 if (MEM_P (op0))
12074 enum insn_code icode;
12076 if (modifier != EXPAND_WRITE
12077 && modifier != EXPAND_MEMORY
12078 && !inner_reference_p
12079 && mode != BLKmode
12080 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12082 /* If the target does have special handling for unaligned
12083 loads of mode then use them. */
12084 if ((icode = optab_handler (movmisalign_optab, mode))
12085 != CODE_FOR_nothing)
12087 rtx reg;
12089 op0 = adjust_address (op0, mode, 0);
12090 /* We've already validated the memory, and we're creating a
12091 new pseudo destination. The predicates really can't
12092 fail. */
12093 reg = gen_reg_rtx (mode);
12095 /* Nor can the insn generator. */
12096 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12097 emit_insn (insn);
12098 return reg;
12100 else if (STRICT_ALIGNMENT)
12102 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12103 poly_uint64 temp_size = mode_size;
12104 if (GET_MODE (op0) != BLKmode)
12105 temp_size = upper_bound (temp_size,
12106 GET_MODE_SIZE (GET_MODE (op0)));
12107 rtx new_rtx
12108 = assign_stack_temp_for_type (mode, temp_size, type);
12109 rtx new_with_op0_mode
12110 = adjust_address (new_rtx, GET_MODE (op0), 0);
12112 gcc_assert (!TREE_ADDRESSABLE (exp));
12114 if (GET_MODE (op0) == BLKmode)
12116 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12117 emit_block_move (new_with_op0_mode, op0, size_rtx,
12118 (modifier == EXPAND_STACK_PARM
12119 ? BLOCK_OP_CALL_PARM
12120 : BLOCK_OP_NORMAL));
12122 else
12123 emit_move_insn (new_with_op0_mode, op0);
12125 op0 = new_rtx;
12129 op0 = adjust_address (op0, mode, 0);
12132 return op0;
12134 case MODIFY_EXPR:
12136 tree lhs = treeop0;
12137 tree rhs = treeop1;
12138 gcc_assert (ignore);
12140 /* Check for |= or &= of a bitfield of size one into another bitfield
12141 of size 1. In this case, (unless we need the result of the
12142 assignment) we can do this more efficiently with a
12143 test followed by an assignment, if necessary.
12145 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12146 things change so we do, this code should be enhanced to
12147 support it. */
12148 if (TREE_CODE (lhs) == COMPONENT_REF
12149 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12150 || TREE_CODE (rhs) == BIT_AND_EXPR)
12151 && TREE_OPERAND (rhs, 0) == lhs
12152 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12153 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12154 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12156 rtx_code_label *label = gen_label_rtx ();
12157 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12158 profile_probability prob = profile_probability::uninitialized ();
12159 if (value)
12160 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12161 else
12162 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12163 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12164 false);
12165 do_pending_stack_adjust ();
12166 emit_label (label);
12167 return const0_rtx;
12170 expand_assignment (lhs, rhs, false);
12171 return const0_rtx;
12174 case ADDR_EXPR:
12175 return expand_expr_addr_expr (exp, target, tmode, modifier);
12177 case REALPART_EXPR:
12178 op0 = expand_normal (treeop0);
12179 return read_complex_part (op0, false);
12181 case IMAGPART_EXPR:
12182 op0 = expand_normal (treeop0);
12183 return read_complex_part (op0, true);
12185 case RETURN_EXPR:
12186 case LABEL_EXPR:
12187 case GOTO_EXPR:
12188 case SWITCH_EXPR:
12189 case ASM_EXPR:
12190 /* Expanded in cfgexpand.cc. */
12191 gcc_unreachable ();
12193 case TRY_CATCH_EXPR:
12194 case CATCH_EXPR:
12195 case EH_FILTER_EXPR:
12196 case TRY_FINALLY_EXPR:
12197 case EH_ELSE_EXPR:
12198 /* Lowered by tree-eh.cc. */
12199 gcc_unreachable ();
12201 case WITH_CLEANUP_EXPR:
12202 case CLEANUP_POINT_EXPR:
12203 case TARGET_EXPR:
12204 case CASE_LABEL_EXPR:
12205 case VA_ARG_EXPR:
12206 case BIND_EXPR:
12207 case INIT_EXPR:
12208 case CONJ_EXPR:
12209 case COMPOUND_EXPR:
12210 case PREINCREMENT_EXPR:
12211 case PREDECREMENT_EXPR:
12212 case POSTINCREMENT_EXPR:
12213 case POSTDECREMENT_EXPR:
12214 case LOOP_EXPR:
12215 case EXIT_EXPR:
12216 case COMPOUND_LITERAL_EXPR:
12217 /* Lowered by gimplify.cc. */
12218 gcc_unreachable ();
12220 case FDESC_EXPR:
12221 /* Function descriptors are not valid except for as
12222 initialization constants, and should not be expanded. */
12223 gcc_unreachable ();
12225 case WITH_SIZE_EXPR:
12226 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12227 have pulled out the size to use in whatever context it needed. */
12228 return expand_expr_real (treeop0, original_target, tmode,
12229 modifier, alt_rtl, inner_reference_p);
12231 default:
12232 return expand_expr_real_2 (&ops, target, tmode, modifier);
12235 #undef EXTEND_BITINT
12237 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12238 signedness of TYPE), possibly returning the result in TARGET.
12239 TYPE is known to be a partial integer type. */
12240 static rtx
12241 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12243 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12244 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12245 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12246 && (!target || GET_MODE (target) == mode));
12248 /* For constant values, reduce using wide_int_to_tree. */
12249 if (poly_int_rtx_p (exp))
12251 auto value = wi::to_poly_wide (exp, mode);
12252 tree t = wide_int_to_tree (type, value);
12253 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12255 else if (TYPE_UNSIGNED (type))
12257 rtx mask = immed_wide_int_const
12258 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12259 return expand_and (mode, exp, mask, target);
12261 else
12263 int count = GET_MODE_PRECISION (mode) - prec;
12264 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12265 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12269 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12270 when applied to the address of EXP produces an address known to be
12271 aligned more than BIGGEST_ALIGNMENT. */
12273 static bool
12274 is_aligning_offset (const_tree offset, const_tree exp)
12276 /* Strip off any conversions. */
12277 while (CONVERT_EXPR_P (offset))
12278 offset = TREE_OPERAND (offset, 0);
12280 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12281 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12282 if (TREE_CODE (offset) != BIT_AND_EXPR
12283 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12284 || compare_tree_int (TREE_OPERAND (offset, 1),
12285 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12286 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12287 return false;
12289 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12290 It must be NEGATE_EXPR. Then strip any more conversions. */
12291 offset = TREE_OPERAND (offset, 0);
12292 while (CONVERT_EXPR_P (offset))
12293 offset = TREE_OPERAND (offset, 0);
12295 if (TREE_CODE (offset) != NEGATE_EXPR)
12296 return false;
12298 offset = TREE_OPERAND (offset, 0);
12299 while (CONVERT_EXPR_P (offset))
12300 offset = TREE_OPERAND (offset, 0);
12302 /* This must now be the address of EXP. */
12303 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12306 /* Return a STRING_CST corresponding to ARG's constant initializer either
12307 if it's a string constant, or, when VALREP is set, any other constant,
12308 or null otherwise.
12309 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12310 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12311 to the size of the byte string. If nonnull, set *DECL to the constant
12312 declaration ARG refers to. */
12314 static tree
12315 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12316 bool valrep = false)
12318 tree dummy = NULL_TREE;
12319 if (!mem_size)
12320 mem_size = &dummy;
12322 /* Store the type of the original expression before conversions
12323 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12324 removed. */
12325 tree argtype = TREE_TYPE (arg);
12327 tree array;
12328 STRIP_NOPS (arg);
12330 /* Non-constant index into the character array in an ARRAY_REF
12331 expression or null. */
12332 tree varidx = NULL_TREE;
12334 poly_int64 base_off = 0;
12336 if (TREE_CODE (arg) == ADDR_EXPR)
12338 arg = TREE_OPERAND (arg, 0);
12339 tree ref = arg;
12340 if (TREE_CODE (arg) == ARRAY_REF)
12342 tree idx = TREE_OPERAND (arg, 1);
12343 if (TREE_CODE (idx) != INTEGER_CST)
12345 /* From a pointer (but not array) argument extract the variable
12346 index to prevent get_addr_base_and_unit_offset() from failing
12347 due to it. Use it later to compute the non-constant offset
12348 into the string and return it to the caller. */
12349 varidx = idx;
12350 ref = TREE_OPERAND (arg, 0);
12352 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12353 return NULL_TREE;
12355 if (!integer_zerop (array_ref_low_bound (arg)))
12356 return NULL_TREE;
12358 if (!integer_onep (array_ref_element_size (arg)))
12359 return NULL_TREE;
12362 array = get_addr_base_and_unit_offset (ref, &base_off);
12363 if (!array
12364 || (TREE_CODE (array) != VAR_DECL
12365 && TREE_CODE (array) != CONST_DECL
12366 && TREE_CODE (array) != STRING_CST))
12367 return NULL_TREE;
12369 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12371 tree arg0 = TREE_OPERAND (arg, 0);
12372 tree arg1 = TREE_OPERAND (arg, 1);
12374 tree offset;
12375 tree str = string_constant (arg0, &offset, mem_size, decl);
12376 if (!str)
12378 str = string_constant (arg1, &offset, mem_size, decl);
12379 arg1 = arg0;
12382 if (str)
12384 /* Avoid pointers to arrays (see bug 86622). */
12385 if (POINTER_TYPE_P (TREE_TYPE (arg))
12386 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12387 && !(decl && !*decl)
12388 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12389 && tree_fits_uhwi_p (*mem_size)
12390 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12391 return NULL_TREE;
12393 tree type = TREE_TYPE (offset);
12394 arg1 = fold_convert (type, arg1);
12395 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12396 return str;
12398 return NULL_TREE;
12400 else if (TREE_CODE (arg) == SSA_NAME)
12402 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12403 if (!is_gimple_assign (stmt))
12404 return NULL_TREE;
12406 tree rhs1 = gimple_assign_rhs1 (stmt);
12407 tree_code code = gimple_assign_rhs_code (stmt);
12408 if (code == ADDR_EXPR)
12409 return string_constant (rhs1, ptr_offset, mem_size, decl);
12410 else if (code != POINTER_PLUS_EXPR)
12411 return NULL_TREE;
12413 tree offset;
12414 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12416 /* Avoid pointers to arrays (see bug 86622). */
12417 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12418 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12419 && !(decl && !*decl)
12420 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12421 && tree_fits_uhwi_p (*mem_size)
12422 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12423 return NULL_TREE;
12425 tree rhs2 = gimple_assign_rhs2 (stmt);
12426 tree type = TREE_TYPE (offset);
12427 rhs2 = fold_convert (type, rhs2);
12428 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12429 return str;
12431 return NULL_TREE;
12433 else if (DECL_P (arg))
12434 array = arg;
12435 else
12436 return NULL_TREE;
12438 tree offset = wide_int_to_tree (sizetype, base_off);
12439 if (varidx)
12441 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12442 return NULL_TREE;
12444 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12445 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12446 if (TREE_CODE (chartype) != INTEGER_TYPE)
12447 return NULL;
12449 offset = fold_convert (sizetype, varidx);
12452 if (TREE_CODE (array) == STRING_CST)
12454 *ptr_offset = fold_convert (sizetype, offset);
12455 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12456 if (decl)
12457 *decl = NULL_TREE;
12458 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12459 >= TREE_STRING_LENGTH (array));
12460 return array;
12463 tree init = ctor_for_folding (array);
12464 if (!init || init == error_mark_node)
12465 return NULL_TREE;
12467 if (valrep)
12469 HOST_WIDE_INT cstoff;
12470 if (!base_off.is_constant (&cstoff))
12471 return NULL_TREE;
12473 /* Check that the host and target are sane. */
12474 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12475 return NULL_TREE;
12477 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12478 if (typesz <= 0 || (int) typesz != typesz)
12479 return NULL_TREE;
12481 HOST_WIDE_INT size = typesz;
12482 if (VAR_P (array)
12483 && DECL_SIZE_UNIT (array)
12484 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12486 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12487 gcc_checking_assert (size >= typesz);
12490 /* If value representation was requested convert the initializer
12491 for the whole array or object into a string of bytes forming
12492 its value representation and return it. */
12493 unsigned char *bytes = XNEWVEC (unsigned char, size);
12494 int r = native_encode_initializer (init, bytes, size);
12495 if (r < typesz)
12497 XDELETEVEC (bytes);
12498 return NULL_TREE;
12501 if (r < size)
12502 memset (bytes + r, '\0', size - r);
12504 const char *p = reinterpret_cast<const char *>(bytes);
12505 init = build_string_literal (size, p, char_type_node);
12506 init = TREE_OPERAND (init, 0);
12507 init = TREE_OPERAND (init, 0);
12508 XDELETE (bytes);
12510 *mem_size = size_int (TREE_STRING_LENGTH (init));
12511 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12513 if (decl)
12514 *decl = array;
12516 return init;
12519 if (TREE_CODE (init) == CONSTRUCTOR)
12521 /* Convert the 64-bit constant offset to a wider type to avoid
12522 overflow and use it to obtain the initializer for the subobject
12523 it points into. */
12524 offset_int wioff;
12525 if (!base_off.is_constant (&wioff))
12526 return NULL_TREE;
12528 wioff *= BITS_PER_UNIT;
12529 if (!wi::fits_uhwi_p (wioff))
12530 return NULL_TREE;
12532 base_off = wioff.to_uhwi ();
12533 unsigned HOST_WIDE_INT fieldoff = 0;
12534 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12535 &fieldoff);
12536 if (!init || init == error_mark_node)
12537 return NULL_TREE;
12539 HOST_WIDE_INT cstoff;
12540 if (!base_off.is_constant (&cstoff))
12541 return NULL_TREE;
12543 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12544 tree off = build_int_cst (sizetype, cstoff);
12545 if (varidx)
12546 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12547 else
12548 offset = off;
12551 *ptr_offset = offset;
12553 tree inittype = TREE_TYPE (init);
12555 if (TREE_CODE (init) == INTEGER_CST
12556 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12557 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12559 /* Check that the host and target are sane. */
12560 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12561 return NULL_TREE;
12563 /* For a reference to (address of) a single constant character,
12564 store the native representation of the character in CHARBUF.
12565 If the reference is to an element of an array or a member
12566 of a struct, only consider narrow characters until ctors
12567 for wide character arrays are transformed to STRING_CSTs
12568 like those for narrow arrays. */
12569 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12570 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12571 if (len > 0)
12573 /* Construct a string literal with elements of INITTYPE and
12574 the representation above. Then strip
12575 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12576 init = build_string_literal (len, (char *)charbuf, inittype);
12577 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12581 tree initsize = TYPE_SIZE_UNIT (inittype);
12583 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12585 /* Fold an empty/zero constructor for an implicitly initialized
12586 object or subobject into the empty string. */
12588 /* Determine the character type from that of the original
12589 expression. */
12590 tree chartype = argtype;
12591 if (POINTER_TYPE_P (chartype))
12592 chartype = TREE_TYPE (chartype);
12593 while (TREE_CODE (chartype) == ARRAY_TYPE)
12594 chartype = TREE_TYPE (chartype);
12596 if (INTEGRAL_TYPE_P (chartype)
12597 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12599 /* Convert a char array to an empty STRING_CST having an array
12600 of the expected type and size. */
12601 if (!initsize)
12602 initsize = integer_zero_node;
12604 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12605 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12606 return NULL_TREE;
12608 init = build_string_literal (size, NULL, chartype, size);
12609 init = TREE_OPERAND (init, 0);
12610 init = TREE_OPERAND (init, 0);
12612 *ptr_offset = integer_zero_node;
12616 if (decl)
12617 *decl = array;
12619 if (TREE_CODE (init) != STRING_CST)
12620 return NULL_TREE;
12622 *mem_size = initsize;
12624 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12626 return init;
12629 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12630 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12631 non-constant) offset in bytes within the string that ARG is accessing.
12632 If MEM_SIZE is non-zero the storage size of the memory is returned.
12633 If DECL is non-zero the constant declaration is returned if available. */
12635 tree
12636 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12638 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12641 /* Similar to string_constant, return a STRING_CST corresponding
12642 to the value representation of the first argument if it's
12643 a constant. */
12645 tree
12646 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12648 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12651 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12652 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12653 for C2 > 0 to x & C3 == C2
12654 for C2 < 0 to x & C3 == (C2 & C3). */
12655 enum tree_code
12656 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12658 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12659 tree treeop0 = gimple_assign_rhs1 (stmt);
12660 tree treeop1 = gimple_assign_rhs2 (stmt);
12661 tree type = TREE_TYPE (*arg0);
12662 scalar_int_mode mode;
12663 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12664 return code;
12665 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12666 || TYPE_PRECISION (type) <= 1
12667 || TYPE_UNSIGNED (type)
12668 /* Signed x % c == 0 should have been optimized into unsigned modulo
12669 earlier. */
12670 || integer_zerop (*arg1)
12671 /* If c is known to be non-negative, modulo will be expanded as unsigned
12672 modulo. */
12673 || get_range_pos_neg (treeop0) == 1)
12674 return code;
12676 /* x % c == d where d < 0 && d <= -c should be always false. */
12677 if (tree_int_cst_sgn (*arg1) == -1
12678 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12679 return code;
12681 int prec = TYPE_PRECISION (type);
12682 wide_int w = wi::to_wide (treeop1) - 1;
12683 w |= wi::shifted_mask (0, prec - 1, true, prec);
12684 tree c3 = wide_int_to_tree (type, w);
12685 tree c4 = *arg1;
12686 if (tree_int_cst_sgn (*arg1) == -1)
12687 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12689 rtx op0 = expand_normal (treeop0);
12690 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12692 bool speed_p = optimize_insn_for_speed_p ();
12694 do_pending_stack_adjust ();
12696 location_t loc = gimple_location (stmt);
12697 struct separate_ops ops;
12698 ops.code = TRUNC_MOD_EXPR;
12699 ops.location = loc;
12700 ops.type = TREE_TYPE (treeop0);
12701 ops.op0 = treeop0;
12702 ops.op1 = treeop1;
12703 ops.op2 = NULL_TREE;
12704 start_sequence ();
12705 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12706 EXPAND_NORMAL);
12707 rtx_insn *moinsns = get_insns ();
12708 end_sequence ();
12710 unsigned mocost = seq_cost (moinsns, speed_p);
12711 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12712 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12714 ops.code = BIT_AND_EXPR;
12715 ops.location = loc;
12716 ops.type = TREE_TYPE (treeop0);
12717 ops.op0 = treeop0;
12718 ops.op1 = c3;
12719 ops.op2 = NULL_TREE;
12720 start_sequence ();
12721 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12722 EXPAND_NORMAL);
12723 rtx_insn *muinsns = get_insns ();
12724 end_sequence ();
12726 unsigned mucost = seq_cost (muinsns, speed_p);
12727 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12728 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12730 if (mocost <= mucost)
12732 emit_insn (moinsns);
12733 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12734 return code;
12737 emit_insn (muinsns);
12738 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12739 *arg1 = c4;
12740 return code;
12743 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12744 If C1 is odd to:
12745 (X - C2) * C3 <= C4 (or >), where
12746 C3 is modular multiplicative inverse of C1 and 1<<prec and
12747 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12748 if C2 > ((1<<prec) - 1) % C1).
12749 If C1 is even, S = ctz (C1) and C2 is 0, use
12750 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12751 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12753 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12754 unsigned):
12755 (X * C3) + C4 <= 2 * C4, where
12756 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12757 C4 is ((1<<(prec - 1) - 1) / C1).
12758 If C1 is even, S = ctz(C1), use
12759 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12760 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12761 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12763 See the Hacker's Delight book, section 10-17. */
12764 enum tree_code
12765 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12767 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12768 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12770 if (optimize < 2)
12771 return code;
12773 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12774 if (stmt == NULL)
12775 return code;
12777 tree treeop0 = gimple_assign_rhs1 (stmt);
12778 tree treeop1 = gimple_assign_rhs2 (stmt);
12779 if (TREE_CODE (treeop0) != SSA_NAME
12780 || TREE_CODE (treeop1) != INTEGER_CST
12781 /* Don't optimize the undefined behavior case x % 0;
12782 x % 1 should have been optimized into zero, punt if
12783 it makes it here for whatever reason;
12784 x % -c should have been optimized into x % c. */
12785 || compare_tree_int (treeop1, 2) <= 0
12786 /* Likewise x % c == d where d >= c should be always false. */
12787 || tree_int_cst_le (treeop1, *arg1))
12788 return code;
12790 /* Unsigned x % pow2 is handled right already, for signed
12791 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12792 if (integer_pow2p (treeop1))
12793 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12795 tree type = TREE_TYPE (*arg0);
12796 scalar_int_mode mode;
12797 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12798 return code;
12799 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12800 || TYPE_PRECISION (type) <= 1)
12801 return code;
12803 signop sgn = UNSIGNED;
12804 /* If both operands are known to have the sign bit clear, handle
12805 even the signed modulo case as unsigned. treeop1 is always
12806 positive >= 2, checked above. */
12807 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12808 sgn = SIGNED;
12810 if (!TYPE_UNSIGNED (type))
12812 if (tree_int_cst_sgn (*arg1) == -1)
12813 return code;
12814 type = unsigned_type_for (type);
12815 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12816 return code;
12819 int prec = TYPE_PRECISION (type);
12820 wide_int w = wi::to_wide (treeop1);
12821 int shift = wi::ctz (w);
12822 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12823 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12824 If C1 is odd, we can handle all cases by subtracting
12825 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12826 e.g. by testing for overflow on the subtraction, punt on that for now
12827 though. */
12828 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12830 if (sgn == SIGNED)
12831 return code;
12832 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12833 if (wi::gtu_p (wi::to_wide (*arg1), x))
12834 return code;
12837 imm_use_iterator imm_iter;
12838 use_operand_p use_p;
12839 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12841 gimple *use_stmt = USE_STMT (use_p);
12842 /* Punt if treeop0 is used in the same bb in a division
12843 or another modulo with the same divisor. We should expect
12844 the division and modulo combined together. */
12845 if (use_stmt == stmt
12846 || gimple_bb (use_stmt) != gimple_bb (stmt))
12847 continue;
12848 if (!is_gimple_assign (use_stmt)
12849 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12850 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12851 continue;
12852 if (gimple_assign_rhs1 (use_stmt) != treeop0
12853 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12854 continue;
12855 return code;
12858 w = wi::lrshift (w, shift);
12859 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12860 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12861 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12862 tree c3 = wide_int_to_tree (type, m);
12863 tree c5 = NULL_TREE;
12864 wide_int d, e;
12865 if (sgn == UNSIGNED)
12867 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12868 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12869 otherwise use < or subtract one from C4. E.g. for
12870 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12871 x % 3U == 1 already needs to be
12872 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12873 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12874 d -= 1;
12875 if (shift)
12876 d = wi::lrshift (d, shift);
12878 else
12880 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12881 if (!shift)
12882 d = wi::lshift (e, 1);
12883 else
12885 e = wi::bit_and (e, wi::mask (shift, true, prec));
12886 d = wi::lrshift (e, shift - 1);
12888 c5 = wide_int_to_tree (type, e);
12890 tree c4 = wide_int_to_tree (type, d);
12892 rtx op0 = expand_normal (treeop0);
12893 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12895 bool speed_p = optimize_insn_for_speed_p ();
12897 do_pending_stack_adjust ();
12899 location_t loc = gimple_location (stmt);
12900 struct separate_ops ops;
12901 ops.code = TRUNC_MOD_EXPR;
12902 ops.location = loc;
12903 ops.type = TREE_TYPE (treeop0);
12904 ops.op0 = treeop0;
12905 ops.op1 = treeop1;
12906 ops.op2 = NULL_TREE;
12907 start_sequence ();
12908 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12909 EXPAND_NORMAL);
12910 rtx_insn *moinsns = get_insns ();
12911 end_sequence ();
12913 unsigned mocost = seq_cost (moinsns, speed_p);
12914 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12915 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12917 tree t = fold_convert_loc (loc, type, treeop0);
12918 if (!integer_zerop (*arg1))
12919 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12920 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12921 if (sgn == SIGNED)
12922 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12923 if (shift)
12925 tree s = build_int_cst (NULL_TREE, shift);
12926 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12929 start_sequence ();
12930 rtx mur = expand_normal (t);
12931 rtx_insn *muinsns = get_insns ();
12932 end_sequence ();
12934 unsigned mucost = seq_cost (muinsns, speed_p);
12935 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12936 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12938 if (mocost <= mucost)
12940 emit_insn (moinsns);
12941 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12942 return code;
12945 emit_insn (muinsns);
12946 *arg0 = make_tree (type, mur);
12947 *arg1 = c4;
12948 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12951 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12953 void
12954 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12956 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12957 || code == LT_EXPR || code == LE_EXPR);
12958 gcc_checking_assert (integer_zerop (*arg1));
12960 if (!optimize)
12961 return;
12963 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12964 if (stmt == NULL)
12965 return;
12967 tree treeop0 = gimple_assign_rhs1 (stmt);
12968 tree treeop1 = gimple_assign_rhs2 (stmt);
12969 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12970 return;
12972 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12973 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12974 "assuming signed overflow does not occur when "
12975 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12976 op_symbol_code (code), op_symbol_code (code));
12978 *arg0 = treeop0;
12979 *arg1 = treeop1;
12983 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
12984 a single bit equality/inequality test, returns where the result is located. */
12986 static rtx
12987 expand_single_bit_test (location_t loc, enum tree_code code,
12988 tree inner, int bitnum,
12989 tree result_type, rtx target,
12990 machine_mode mode)
12992 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
12994 tree type = TREE_TYPE (inner);
12995 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
12996 int ops_unsigned;
12997 tree signed_type, unsigned_type, intermediate_type;
12998 gimple *inner_def;
13000 /* First, see if we can fold the single bit test into a sign-bit
13001 test. */
13002 if (bitnum == TYPE_PRECISION (type) - 1
13003 && type_has_mode_precision_p (type))
13005 tree stype = signed_type_for (type);
13006 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13007 result_type,
13008 fold_convert_loc (loc, stype, inner),
13009 build_int_cst (stype, 0));
13010 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
13013 /* Otherwise we have (A & C) != 0 where C is a single bit,
13014 convert that into ((A >> C2) & 1). Where C2 = log2(C).
13015 Similarly for (A & C) == 0. */
13017 /* If INNER is a right shift of a constant and it plus BITNUM does
13018 not overflow, adjust BITNUM and INNER. */
13019 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
13020 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
13021 && bitnum < TYPE_PRECISION (type)
13022 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
13023 TYPE_PRECISION (type) - bitnum))
13025 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
13026 inner = gimple_assign_rhs1 (inner_def);
13029 /* If we are going to be able to omit the AND below, we must do our
13030 operations as unsigned. If we must use the AND, we have a choice.
13031 Normally unsigned is faster, but for some machines signed is. */
13032 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
13033 && !flag_syntax_only) ? 0 : 1;
13035 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
13036 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
13037 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
13038 inner = fold_convert_loc (loc, intermediate_type, inner);
13040 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
13042 if (CONST_SCALAR_INT_P (inner0))
13044 wide_int t = rtx_mode_t (inner0, operand_mode);
13045 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
13046 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
13048 int bitpos = bitnum;
13050 if (BYTES_BIG_ENDIAN)
13051 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
13053 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
13054 operand_mode, mode, 0, NULL);
13056 if (code == EQ_EXPR)
13057 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
13058 NULL_RTX, 1, OPTAB_LIB_WIDEN);
13059 if (GET_MODE (inner0) != mode)
13061 rtx t = gen_reg_rtx (mode);
13062 convert_move (t, inner0, 0);
13063 return t;
13065 return inner0;
13068 /* Generate code to calculate OPS, and exploded expression
13069 using a store-flag instruction and return an rtx for the result.
13070 OPS reflects a comparison.
13072 If TARGET is nonzero, store the result there if convenient.
13074 Return zero if there is no suitable set-flag instruction
13075 available on this machine.
13077 Once expand_expr has been called on the arguments of the comparison,
13078 we are committed to doing the store flag, since it is not safe to
13079 re-evaluate the expression. We emit the store-flag insn by calling
13080 emit_store_flag, but only expand the arguments if we have a reason
13081 to believe that emit_store_flag will be successful. If we think that
13082 it will, but it isn't, we have to simulate the store-flag with a
13083 set/jump/set sequence. */
13085 static rtx
13086 do_store_flag (sepops ops, rtx target, machine_mode mode)
13088 enum rtx_code code;
13089 tree arg0, arg1, type;
13090 machine_mode operand_mode;
13091 int unsignedp;
13092 rtx op0, op1;
13093 rtx subtarget = target;
13094 location_t loc = ops->location;
13096 arg0 = ops->op0;
13097 arg1 = ops->op1;
13099 /* Don't crash if the comparison was erroneous. */
13100 if (arg0 == error_mark_node || arg1 == error_mark_node)
13101 return const0_rtx;
13103 type = TREE_TYPE (arg0);
13104 operand_mode = TYPE_MODE (type);
13105 unsignedp = TYPE_UNSIGNED (type);
13107 /* We won't bother with BLKmode store-flag operations because it would mean
13108 passing a lot of information to emit_store_flag. */
13109 if (operand_mode == BLKmode)
13110 return 0;
13112 /* We won't bother with store-flag operations involving function pointers
13113 when function pointers must be canonicalized before comparisons. */
13114 if (targetm.have_canonicalize_funcptr_for_compare ()
13115 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13116 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13117 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13118 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13119 return 0;
13121 STRIP_NOPS (arg0);
13122 STRIP_NOPS (arg1);
13124 /* For vector typed comparisons emit code to generate the desired
13125 all-ones or all-zeros mask. */
13126 if (VECTOR_TYPE_P (ops->type))
13128 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13129 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13130 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13131 return expand_vec_cmp_expr (ops->type, ifexp, target);
13132 else
13133 gcc_unreachable ();
13136 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13137 into (x - C2) * C3 < C4. */
13138 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13139 && TREE_CODE (arg0) == SSA_NAME
13140 && TREE_CODE (arg1) == INTEGER_CST)
13142 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13143 &arg0, &arg1);
13144 if (new_code != ops->code)
13146 struct separate_ops nops = *ops;
13147 nops.code = ops->code = new_code;
13148 nops.op0 = arg0;
13149 nops.op1 = arg1;
13150 nops.type = TREE_TYPE (arg0);
13151 return do_store_flag (&nops, target, mode);
13155 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13156 if (!unsignedp
13157 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13158 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13159 && integer_zerop (arg1)
13160 && TREE_CODE (arg0) == SSA_NAME)
13161 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13163 /* Get the rtx comparison code to use. We know that EXP is a comparison
13164 operation of some type. Some comparisons against 1 and -1 can be
13165 converted to comparisons with zero. Do so here so that the tests
13166 below will be aware that we have a comparison with zero. These
13167 tests will not catch constants in the first operand, but constants
13168 are rarely passed as the first operand. */
13170 switch (ops->code)
13172 case EQ_EXPR:
13173 code = EQ;
13174 break;
13175 case NE_EXPR:
13176 code = NE;
13177 break;
13178 case LT_EXPR:
13179 if (integer_onep (arg1))
13180 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13181 else
13182 code = unsignedp ? LTU : LT;
13183 break;
13184 case LE_EXPR:
13185 if (! unsignedp && integer_all_onesp (arg1))
13186 arg1 = integer_zero_node, code = LT;
13187 else
13188 code = unsignedp ? LEU : LE;
13189 break;
13190 case GT_EXPR:
13191 if (! unsignedp && integer_all_onesp (arg1))
13192 arg1 = integer_zero_node, code = GE;
13193 else
13194 code = unsignedp ? GTU : GT;
13195 break;
13196 case GE_EXPR:
13197 if (integer_onep (arg1))
13198 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13199 else
13200 code = unsignedp ? GEU : GE;
13201 break;
13203 case UNORDERED_EXPR:
13204 code = UNORDERED;
13205 break;
13206 case ORDERED_EXPR:
13207 code = ORDERED;
13208 break;
13209 case UNLT_EXPR:
13210 code = UNLT;
13211 break;
13212 case UNLE_EXPR:
13213 code = UNLE;
13214 break;
13215 case UNGT_EXPR:
13216 code = UNGT;
13217 break;
13218 case UNGE_EXPR:
13219 code = UNGE;
13220 break;
13221 case UNEQ_EXPR:
13222 code = UNEQ;
13223 break;
13224 case LTGT_EXPR:
13225 code = LTGT;
13226 break;
13228 default:
13229 gcc_unreachable ();
13232 /* Put a constant second. */
13233 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13234 || TREE_CODE (arg0) == FIXED_CST)
13236 std::swap (arg0, arg1);
13237 code = swap_condition (code);
13240 /* If this is an equality or inequality test of a single bit, we can
13241 do this by shifting the bit being tested to the low-order bit and
13242 masking the result with the constant 1. If the condition was EQ,
13243 we xor it with 1. This does not require an scc insn and is faster
13244 than an scc insn even if we have it. */
13246 if ((code == NE || code == EQ)
13247 && (integer_zerop (arg1)
13248 || integer_pow2p (arg1))
13249 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13251 tree narg0 = arg0;
13252 wide_int nz = tree_nonzero_bits (narg0);
13253 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR);
13254 /* If the defining statement was (x & POW2), then use that instead of
13255 the non-zero bits. */
13256 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13258 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13259 narg0 = gimple_assign_rhs1 (srcstmt);
13262 if (wi::popcount (nz) == 1
13263 && (integer_zerop (arg1)
13264 || wi::to_wide (arg1) == nz))
13266 int bitnum = wi::exact_log2 (nz);
13267 enum tree_code tcode = EQ_EXPR;
13268 if ((code == NE) ^ !integer_zerop (arg1))
13269 tcode = NE_EXPR;
13271 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13272 return expand_single_bit_test (loc, tcode,
13273 narg0,
13274 bitnum, type, target, mode);
13279 if (! get_subtarget (target)
13280 || GET_MODE (subtarget) != operand_mode)
13281 subtarget = 0;
13283 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13285 if (target == 0)
13286 target = gen_reg_rtx (mode);
13288 /* Try a cstore if possible. */
13289 return emit_store_flag_force (target, code, op0, op1,
13290 operand_mode, unsignedp,
13291 (TYPE_PRECISION (ops->type) == 1
13292 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13295 /* Attempt to generate a casesi instruction. Returns true if successful,
13296 false otherwise (i.e. if there is no casesi instruction).
13298 DEFAULT_PROBABILITY is the probability of jumping to the default
13299 label. */
13300 bool
13301 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13302 rtx table_label, rtx default_label, rtx fallback_label,
13303 profile_probability default_probability)
13305 class expand_operand ops[5];
13306 scalar_int_mode index_mode = SImode;
13307 rtx op1, op2, index;
13309 if (! targetm.have_casesi ())
13310 return false;
13312 /* The index must be some form of integer. Convert it to SImode. */
13313 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13314 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13316 rtx rangertx = expand_normal (range);
13318 /* We must handle the endpoints in the original mode. */
13319 index_expr = build2 (MINUS_EXPR, index_type,
13320 index_expr, minval);
13321 minval = integer_zero_node;
13322 index = expand_normal (index_expr);
13323 if (default_label)
13324 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13325 omode, 1, default_label,
13326 default_probability);
13327 /* Now we can safely truncate. */
13328 index = convert_to_mode (index_mode, index, 0);
13330 else
13332 if (omode != index_mode)
13334 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13335 index_expr = fold_convert (index_type, index_expr);
13338 index = expand_normal (index_expr);
13341 do_pending_stack_adjust ();
13343 op1 = expand_normal (minval);
13344 op2 = expand_normal (range);
13346 create_input_operand (&ops[0], index, index_mode);
13347 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13348 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13349 create_fixed_operand (&ops[3], table_label);
13350 create_fixed_operand (&ops[4], (default_label
13351 ? default_label
13352 : fallback_label));
13353 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13354 return true;
13357 /* Attempt to generate a tablejump instruction; same concept. */
13358 /* Subroutine of the next function.
13360 INDEX is the value being switched on, with the lowest value
13361 in the table already subtracted.
13362 MODE is its expected mode (needed if INDEX is constant).
13363 RANGE is the length of the jump table.
13364 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13366 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13367 index value is out of range.
13368 DEFAULT_PROBABILITY is the probability of jumping to
13369 the default label. */
13371 static void
13372 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13373 rtx default_label, profile_probability default_probability)
13375 rtx temp, vector;
13377 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13378 cfun->cfg->max_jumptable_ents = INTVAL (range);
13380 /* Do an unsigned comparison (in the proper mode) between the index
13381 expression and the value which represents the length of the range.
13382 Since we just finished subtracting the lower bound of the range
13383 from the index expression, this comparison allows us to simultaneously
13384 check that the original index expression value is both greater than
13385 or equal to the minimum value of the range and less than or equal to
13386 the maximum value of the range. */
13388 if (default_label)
13389 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13390 default_label, default_probability);
13392 /* If index is in range, it must fit in Pmode.
13393 Convert to Pmode so we can index with it. */
13394 if (mode != Pmode)
13396 unsigned int width;
13398 /* We know the value of INDEX is between 0 and RANGE. If we have a
13399 sign-extended subreg, and RANGE does not have the sign bit set, then
13400 we have a value that is valid for both sign and zero extension. In
13401 this case, we get better code if we sign extend. */
13402 if (GET_CODE (index) == SUBREG
13403 && SUBREG_PROMOTED_VAR_P (index)
13404 && SUBREG_PROMOTED_SIGNED_P (index)
13405 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13406 <= HOST_BITS_PER_WIDE_INT)
13407 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13408 index = convert_to_mode (Pmode, index, 0);
13409 else
13410 index = convert_to_mode (Pmode, index, 1);
13413 /* Don't let a MEM slip through, because then INDEX that comes
13414 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13415 and break_out_memory_refs will go to work on it and mess it up. */
13416 #ifdef PIC_CASE_VECTOR_ADDRESS
13417 if (flag_pic && !REG_P (index))
13418 index = copy_to_mode_reg (Pmode, index);
13419 #endif
13421 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13422 GET_MODE_SIZE, because this indicates how large insns are. The other
13423 uses should all be Pmode, because they are addresses. This code
13424 could fail if addresses and insns are not the same size. */
13425 index = simplify_gen_binary (MULT, Pmode, index,
13426 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13427 Pmode));
13428 index = simplify_gen_binary (PLUS, Pmode, index,
13429 gen_rtx_LABEL_REF (Pmode, table_label));
13431 #ifdef PIC_CASE_VECTOR_ADDRESS
13432 if (flag_pic)
13433 index = PIC_CASE_VECTOR_ADDRESS (index);
13434 else
13435 #endif
13436 index = memory_address (CASE_VECTOR_MODE, index);
13437 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13438 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13439 convert_move (temp, vector, 0);
13441 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13443 /* If we are generating PIC code or if the table is PC-relative, the
13444 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13445 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13446 emit_barrier ();
13449 bool
13450 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13451 rtx table_label, rtx default_label,
13452 profile_probability default_probability)
13454 rtx index;
13456 if (! targetm.have_tablejump ())
13457 return false;
13459 index_expr = fold_build2 (MINUS_EXPR, index_type,
13460 fold_convert (index_type, index_expr),
13461 fold_convert (index_type, minval));
13462 index = expand_normal (index_expr);
13463 do_pending_stack_adjust ();
13465 do_tablejump (index, TYPE_MODE (index_type),
13466 convert_modes (TYPE_MODE (index_type),
13467 TYPE_MODE (TREE_TYPE (range)),
13468 expand_normal (range),
13469 TYPE_UNSIGNED (TREE_TYPE (range))),
13470 table_label, default_label, default_probability);
13471 return true;
13474 /* Return a CONST_VECTOR rtx representing vector mask for
13475 a VECTOR_CST of booleans. */
13476 static rtx
13477 const_vector_mask_from_tree (tree exp)
13479 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13480 machine_mode inner = GET_MODE_INNER (mode);
13482 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13483 VECTOR_CST_NELTS_PER_PATTERN (exp));
13484 unsigned int count = builder.encoded_nelts ();
13485 for (unsigned int i = 0; i < count; ++i)
13487 tree elt = VECTOR_CST_ELT (exp, i);
13488 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13489 if (integer_zerop (elt))
13490 builder.quick_push (CONST0_RTX (inner));
13491 else if (integer_onep (elt)
13492 || integer_minus_onep (elt))
13493 builder.quick_push (CONSTM1_RTX (inner));
13494 else
13495 gcc_unreachable ();
13497 return builder.build ();
13500 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13501 static rtx
13502 const_vector_from_tree (tree exp)
13504 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13506 if (initializer_zerop (exp))
13507 return CONST0_RTX (mode);
13509 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13510 return const_vector_mask_from_tree (exp);
13512 machine_mode inner = GET_MODE_INNER (mode);
13514 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13515 VECTOR_CST_NELTS_PER_PATTERN (exp));
13516 unsigned int count = builder.encoded_nelts ();
13517 for (unsigned int i = 0; i < count; ++i)
13519 tree elt = VECTOR_CST_ELT (exp, i);
13520 if (TREE_CODE (elt) == REAL_CST)
13521 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13522 inner));
13523 else if (TREE_CODE (elt) == FIXED_CST)
13524 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13525 inner));
13526 else
13527 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13528 inner));
13530 return builder.build ();
13533 /* Build a decl for a personality function given a language prefix. */
13535 tree
13536 build_personality_function (const char *lang)
13538 const char *unwind_and_version;
13539 tree decl, type;
13540 char *name;
13542 switch (targetm_common.except_unwind_info (&global_options))
13544 case UI_NONE:
13545 return NULL;
13546 case UI_SJLJ:
13547 unwind_and_version = "_sj0";
13548 break;
13549 case UI_DWARF2:
13550 case UI_TARGET:
13551 unwind_and_version = "_v0";
13552 break;
13553 case UI_SEH:
13554 unwind_and_version = "_seh0";
13555 break;
13556 default:
13557 gcc_unreachable ();
13560 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13562 type = build_function_type_list (unsigned_type_node,
13563 integer_type_node, integer_type_node,
13564 long_long_unsigned_type_node,
13565 ptr_type_node, ptr_type_node, NULL_TREE);
13566 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13567 get_identifier (name), type);
13568 DECL_ARTIFICIAL (decl) = 1;
13569 DECL_EXTERNAL (decl) = 1;
13570 TREE_PUBLIC (decl) = 1;
13572 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13573 are the flags assigned by targetm.encode_section_info. */
13574 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13576 return decl;
13579 /* Extracts the personality function of DECL and returns the corresponding
13580 libfunc. */
13583 get_personality_function (tree decl)
13585 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13586 enum eh_personality_kind pk;
13588 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13589 if (pk == eh_personality_none)
13590 return NULL;
13592 if (!personality
13593 && pk == eh_personality_any)
13594 personality = lang_hooks.eh_personality ();
13596 if (pk == eh_personality_lang)
13597 gcc_assert (personality != NULL_TREE);
13599 return XEXP (DECL_RTL (personality), 0);
13602 /* Returns a tree for the size of EXP in bytes. */
13604 static tree
13605 tree_expr_size (const_tree exp)
13607 if (DECL_P (exp)
13608 && DECL_SIZE_UNIT (exp) != 0)
13609 return DECL_SIZE_UNIT (exp);
13610 else
13611 return size_in_bytes (TREE_TYPE (exp));
13614 /* Return an rtx for the size in bytes of the value of EXP. */
13617 expr_size (tree exp)
13619 tree size;
13621 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13622 size = TREE_OPERAND (exp, 1);
13623 else
13625 size = tree_expr_size (exp);
13626 gcc_assert (size);
13627 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13630 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13633 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13634 if the size can vary or is larger than an integer. */
13636 HOST_WIDE_INT
13637 int_expr_size (const_tree exp)
13639 tree size;
13641 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13642 size = TREE_OPERAND (exp, 1);
13643 else
13645 size = tree_expr_size (exp);
13646 gcc_assert (size);
13649 if (size == 0 || !tree_fits_shwi_p (size))
13650 return -1;
13652 return tree_to_shwi (size);