hppa: Fix pr104869.C on hpux
[official-gcc.git] / gcc / expr.cc
blobc432170403bda3d9ca46e507e3657ab4a632c85a
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 ())
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 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1099 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1100 if (up > l)
1101 l = up;
1102 align = GET_MODE_ALIGNMENT (mode);
1105 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1107 while (max_size > 1 && l > 0)
1109 mode = widest_fixed_size_mode_for_size (max_size, op);
1110 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1112 unsigned int modesize = GET_MODE_SIZE (mode);
1114 if (align >= GET_MODE_ALIGNMENT (mode))
1116 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1117 l %= modesize;
1118 switch (op)
1120 default:
1121 n_insns += n_pieces;
1122 break;
1124 case COMPARE_BY_PIECES:
1125 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1126 int batch_ops = 4 * batch - 1;
1127 unsigned HOST_WIDE_INT full = n_pieces / batch;
1128 n_insns += full * batch_ops;
1129 if (n_pieces % batch != 0)
1130 n_insns++;
1131 break;
1135 max_size = modesize;
1138 gcc_assert (!l);
1139 return n_insns;
1142 /* Used when performing piecewise block operations, holds information
1143 about one of the memory objects involved. The member functions
1144 can be used to generate code for loading from the object and
1145 updating the address when iterating. */
1147 class pieces_addr
1149 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1150 stack pushes. */
1151 rtx m_obj;
1152 /* The address of the object. Can differ from that seen in the
1153 MEM rtx if we copied the address to a register. */
1154 rtx m_addr;
1155 /* Nonzero if the address on the object has an autoincrement already,
1156 signifies whether that was an increment or decrement. */
1157 signed char m_addr_inc;
1158 /* Nonzero if we intend to use autoinc without the address already
1159 having autoinc form. We will insert add insns around each memory
1160 reference, expecting later passes to form autoinc addressing modes.
1161 The only supported options are predecrement and postincrement. */
1162 signed char m_explicit_inc;
1163 /* True if we have either of the two possible cases of using
1164 autoincrement. */
1165 bool m_auto;
1166 /* True if this is an address to be used for load operations rather
1167 than stores. */
1168 bool m_is_load;
1170 /* Optionally, a function to obtain constants for any given offset into
1171 the objects, and data associated with it. */
1172 by_pieces_constfn m_constfn;
1173 void *m_cfndata;
1174 public:
1175 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1176 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1177 void increment_address (HOST_WIDE_INT);
1178 void maybe_predec (HOST_WIDE_INT);
1179 void maybe_postinc (HOST_WIDE_INT);
1180 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1181 int get_addr_inc ()
1183 return m_addr_inc;
1187 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1188 true if the operation to be performed on this object is a load
1189 rather than a store. For stores, OBJ can be NULL, in which case we
1190 assume the operation is a stack push. For loads, the optional
1191 CONSTFN and its associated CFNDATA can be used in place of the
1192 memory load. */
1194 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1195 void *cfndata)
1196 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1198 m_addr_inc = 0;
1199 m_auto = false;
1200 if (obj)
1202 rtx addr = XEXP (obj, 0);
1203 rtx_code code = GET_CODE (addr);
1204 m_addr = addr;
1205 bool dec = code == PRE_DEC || code == POST_DEC;
1206 bool inc = code == PRE_INC || code == POST_INC;
1207 m_auto = inc || dec;
1208 if (m_auto)
1209 m_addr_inc = dec ? -1 : 1;
1211 /* While we have always looked for these codes here, the code
1212 implementing the memory operation has never handled them.
1213 Support could be added later if necessary or beneficial. */
1214 gcc_assert (code != PRE_INC && code != POST_DEC);
1216 else
1218 m_addr = NULL_RTX;
1219 if (!is_load)
1221 m_auto = true;
1222 if (STACK_GROWS_DOWNWARD)
1223 m_addr_inc = -1;
1224 else
1225 m_addr_inc = 1;
1227 else
1228 gcc_assert (constfn != NULL);
1230 m_explicit_inc = 0;
1231 if (constfn)
1232 gcc_assert (is_load);
1235 /* Decide whether to use autoinc for an address involved in a memory op.
1236 MODE is the mode of the accesses, REVERSE is true if we've decided to
1237 perform the operation starting from the end, and LEN is the length of
1238 the operation. Don't override an earlier decision to set m_auto. */
1240 void
1241 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1242 HOST_WIDE_INT len)
1244 if (m_auto || m_obj == NULL_RTX)
1245 return;
1247 bool use_predec = (m_is_load
1248 ? USE_LOAD_PRE_DECREMENT (mode)
1249 : USE_STORE_PRE_DECREMENT (mode));
1250 bool use_postinc = (m_is_load
1251 ? USE_LOAD_POST_INCREMENT (mode)
1252 : USE_STORE_POST_INCREMENT (mode));
1253 machine_mode addr_mode = get_address_mode (m_obj);
1255 if (use_predec && reverse)
1257 m_addr = copy_to_mode_reg (addr_mode,
1258 plus_constant (addr_mode,
1259 m_addr, len));
1260 m_auto = true;
1261 m_explicit_inc = -1;
1263 else if (use_postinc && !reverse)
1265 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1266 m_auto = true;
1267 m_explicit_inc = 1;
1269 else if (CONSTANT_P (m_addr))
1270 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1273 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1274 are using autoincrement for this address, we don't add the offset,
1275 but we still modify the MEM's properties. */
1278 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1279 by_pieces_prev *prev)
1281 if (m_constfn)
1282 /* Pass the previous data to m_constfn. */
1283 return m_constfn (m_cfndata, prev, offset, mode);
1284 if (m_obj == NULL_RTX)
1285 return NULL_RTX;
1286 if (m_auto)
1287 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1288 else
1289 return adjust_address (m_obj, mode, offset);
1292 /* Emit an add instruction to increment the address by SIZE. */
1294 void
1295 pieces_addr::increment_address (HOST_WIDE_INT size)
1297 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1298 emit_insn (gen_add2_insn (m_addr, amount));
1301 /* If we are supposed to decrement the address after each access, emit code
1302 to do so now. Increment by SIZE (which has should have the correct sign
1303 already). */
1305 void
1306 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1308 if (m_explicit_inc >= 0)
1309 return;
1310 gcc_assert (HAVE_PRE_DECREMENT);
1311 increment_address (size);
1314 /* If we are supposed to decrement the address after each access, emit code
1315 to do so now. Increment by SIZE. */
1317 void
1318 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1320 if (m_explicit_inc <= 0)
1321 return;
1322 gcc_assert (HAVE_POST_INCREMENT);
1323 increment_address (size);
1326 /* This structure is used by do_op_by_pieces to describe the operation
1327 to be performed. */
1329 class op_by_pieces_d
1331 private:
1332 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1333 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1335 protected:
1336 pieces_addr m_to, m_from;
1337 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1338 use it to check the valid mode size. */
1339 const unsigned HOST_WIDE_INT m_len;
1340 HOST_WIDE_INT m_offset;
1341 unsigned int m_align;
1342 unsigned int m_max_size;
1343 bool m_reverse;
1344 /* True if this is a stack push. */
1345 bool m_push;
1346 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1347 bool m_overlap_op_by_pieces;
1348 /* The type of operation that we're performing. */
1349 by_pieces_operation m_op;
1351 /* Virtual functions, overriden by derived classes for the specific
1352 operation. */
1353 virtual void generate (rtx, rtx, machine_mode) = 0;
1354 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1355 virtual void finish_mode (machine_mode)
1359 public:
1360 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1361 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1362 by_pieces_operation);
1363 void run ();
1366 /* The constructor for an op_by_pieces_d structure. We require two
1367 objects named TO and FROM, which are identified as loads or stores
1368 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1369 and its associated FROM_CFN_DATA can be used to replace loads with
1370 constant values. MAX_PIECES describes the maximum number of bytes
1371 at a time which can be moved efficiently. LEN describes the length
1372 of the operation. */
1374 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1375 bool to_load, rtx from, bool from_load,
1376 by_pieces_constfn from_cfn,
1377 void *from_cfn_data,
1378 unsigned HOST_WIDE_INT len,
1379 unsigned int align, bool push,
1380 by_pieces_operation op)
1381 : m_to (to, to_load, NULL, NULL),
1382 m_from (from, from_load, from_cfn, from_cfn_data),
1383 m_len (len), m_max_size (max_pieces + 1),
1384 m_push (push), m_op (op)
1386 int toi = m_to.get_addr_inc ();
1387 int fromi = m_from.get_addr_inc ();
1388 if (toi >= 0 && fromi >= 0)
1389 m_reverse = false;
1390 else if (toi <= 0 && fromi <= 0)
1391 m_reverse = true;
1392 else
1393 gcc_unreachable ();
1395 m_offset = m_reverse ? len : 0;
1396 align = MIN (to ? MEM_ALIGN (to) : align,
1397 from ? MEM_ALIGN (from) : align);
1399 /* If copying requires more than two move insns,
1400 copy addresses to registers (to make displacements shorter)
1401 and use post-increment if available. */
1402 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1404 /* Find the mode of the largest comparison. */
1405 fixed_size_mode mode
1406 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1408 m_from.decide_autoinc (mode, m_reverse, len);
1409 m_to.decide_autoinc (mode, m_reverse, len);
1412 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1413 m_align = align;
1415 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1418 /* This function returns the largest usable integer mode for LEN bytes
1419 whose size is no bigger than size of MODE. */
1421 fixed_size_mode
1422 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1424 unsigned int size;
1427 size = GET_MODE_SIZE (mode);
1428 if (len >= size && prepare_mode (mode, m_align))
1429 break;
1430 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1431 mode = widest_fixed_size_mode_for_size (size, m_op);
1433 while (1);
1434 return mode;
1437 /* Return the smallest integer or QI vector mode that is not narrower
1438 than SIZE bytes. */
1440 fixed_size_mode
1441 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1443 /* Use QI vector only for > size of WORD. */
1444 if (can_use_qi_vectors (m_op) && size > UNITS_PER_WORD)
1446 machine_mode mode;
1447 fixed_size_mode candidate;
1448 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1449 if (is_a<fixed_size_mode> (mode, &candidate)
1450 && GET_MODE_INNER (candidate) == QImode)
1452 /* Don't return a mode wider than M_LEN. */
1453 if (GET_MODE_SIZE (candidate) > m_len)
1454 break;
1456 if (GET_MODE_SIZE (candidate) >= size
1457 && by_pieces_mode_supported_p (candidate, m_op))
1458 return candidate;
1462 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1465 /* This function contains the main loop used for expanding a block
1466 operation. First move what we can in the largest integer mode,
1467 then go to successively smaller modes. For every access, call
1468 GENFUN with the two operands and the EXTRA_DATA. */
1470 void
1471 op_by_pieces_d::run ()
1473 if (m_len == 0)
1474 return;
1476 unsigned HOST_WIDE_INT length = m_len;
1478 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1479 fixed_size_mode mode
1480 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1481 mode = get_usable_mode (mode, length);
1483 by_pieces_prev to_prev = { nullptr, mode };
1484 by_pieces_prev from_prev = { nullptr, mode };
1488 unsigned int size = GET_MODE_SIZE (mode);
1489 rtx to1 = NULL_RTX, from1;
1491 while (length >= size)
1493 if (m_reverse)
1494 m_offset -= size;
1496 to1 = m_to.adjust (mode, m_offset, &to_prev);
1497 to_prev.data = to1;
1498 to_prev.mode = mode;
1499 from1 = m_from.adjust (mode, m_offset, &from_prev);
1500 from_prev.data = from1;
1501 from_prev.mode = mode;
1503 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1504 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1506 generate (to1, from1, mode);
1508 m_to.maybe_postinc (size);
1509 m_from.maybe_postinc (size);
1511 if (!m_reverse)
1512 m_offset += size;
1514 length -= size;
1517 finish_mode (mode);
1519 if (length == 0)
1520 return;
1522 if (!m_push && m_overlap_op_by_pieces)
1524 /* NB: Generate overlapping operations if it is not a stack
1525 push since stack push must not overlap. Get the smallest
1526 fixed size mode for M_LEN bytes. */
1527 mode = smallest_fixed_size_mode_for_size (length);
1528 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1529 int gap = GET_MODE_SIZE (mode) - length;
1530 if (gap > 0)
1532 /* If size of MODE > M_LEN, generate the last operation
1533 in MODE for the remaining bytes with ovelapping memory
1534 from the previois operation. */
1535 if (m_reverse)
1536 m_offset += gap;
1537 else
1538 m_offset -= gap;
1539 length += gap;
1542 else
1544 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1545 mode = widest_fixed_size_mode_for_size (size, m_op);
1546 mode = get_usable_mode (mode, length);
1549 while (1);
1552 /* Derived class from op_by_pieces_d, providing support for block move
1553 operations. */
1555 #ifdef PUSH_ROUNDING
1556 #define PUSHG_P(to) ((to) == nullptr)
1557 #else
1558 #define PUSHG_P(to) false
1559 #endif
1561 class move_by_pieces_d : public op_by_pieces_d
1563 insn_gen_fn m_gen_fun;
1564 void generate (rtx, rtx, machine_mode) final override;
1565 bool prepare_mode (machine_mode, unsigned int) final override;
1567 public:
1568 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1569 unsigned int align)
1570 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1571 NULL, len, align, PUSHG_P (to), MOVE_BY_PIECES)
1574 rtx finish_retmode (memop_ret);
1577 /* Return true if MODE can be used for a set of copies, given an
1578 alignment ALIGN. Prepare whatever data is necessary for later
1579 calls to generate. */
1581 bool
1582 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1584 insn_code icode = optab_handler (mov_optab, mode);
1585 m_gen_fun = GEN_FCN (icode);
1586 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1589 /* A callback used when iterating for a compare_by_pieces_operation.
1590 OP0 and OP1 are the values that have been loaded and should be
1591 compared in MODE. If OP0 is NULL, this means we should generate a
1592 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1593 gen function that should be used to generate the mode. */
1595 void
1596 move_by_pieces_d::generate (rtx op0, rtx op1,
1597 machine_mode mode ATTRIBUTE_UNUSED)
1599 #ifdef PUSH_ROUNDING
1600 if (op0 == NULL_RTX)
1602 emit_single_push_insn (mode, op1, NULL);
1603 return;
1605 #endif
1606 emit_insn (m_gen_fun (op0, op1));
1609 /* Perform the final adjustment at the end of a string to obtain the
1610 correct return value for the block operation.
1611 Return value is based on RETMODE argument. */
1614 move_by_pieces_d::finish_retmode (memop_ret retmode)
1616 gcc_assert (!m_reverse);
1617 if (retmode == RETURN_END_MINUS_ONE)
1619 m_to.maybe_postinc (-1);
1620 --m_offset;
1622 return m_to.adjust (QImode, m_offset);
1625 /* Generate several move instructions to copy LEN bytes from block FROM to
1626 block TO. (These are MEM rtx's with BLKmode).
1628 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1629 used to push FROM to the stack.
1631 ALIGN is maximum stack alignment we can assume.
1633 Return value is based on RETMODE argument. */
1636 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1637 unsigned int align, memop_ret retmode)
1639 #ifndef PUSH_ROUNDING
1640 if (to == NULL)
1641 gcc_unreachable ();
1642 #endif
1644 move_by_pieces_d data (to, from, len, align);
1646 data.run ();
1648 if (retmode != RETURN_BEGIN)
1649 return data.finish_retmode (retmode);
1650 else
1651 return to;
1654 /* Derived class from op_by_pieces_d, providing support for block move
1655 operations. */
1657 class store_by_pieces_d : public op_by_pieces_d
1659 insn_gen_fn m_gen_fun;
1661 void generate (rtx, rtx, machine_mode) final override;
1662 bool prepare_mode (machine_mode, unsigned int) final override;
1664 public:
1665 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1666 unsigned HOST_WIDE_INT len, unsigned int align,
1667 by_pieces_operation op)
1668 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1669 cfn_data, len, align, false, op)
1672 rtx finish_retmode (memop_ret);
1675 /* Return true if MODE can be used for a set of stores, given an
1676 alignment ALIGN. Prepare whatever data is necessary for later
1677 calls to generate. */
1679 bool
1680 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1682 insn_code icode = optab_handler (mov_optab, mode);
1683 m_gen_fun = GEN_FCN (icode);
1684 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1687 /* A callback used when iterating for a store_by_pieces_operation.
1688 OP0 and OP1 are the values that have been loaded and should be
1689 compared in MODE. If OP0 is NULL, this means we should generate a
1690 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1691 gen function that should be used to generate the mode. */
1693 void
1694 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1696 emit_insn (m_gen_fun (op0, op1));
1699 /* Perform the final adjustment at the end of a string to obtain the
1700 correct return value for the block operation.
1701 Return value is based on RETMODE argument. */
1704 store_by_pieces_d::finish_retmode (memop_ret retmode)
1706 gcc_assert (!m_reverse);
1707 if (retmode == RETURN_END_MINUS_ONE)
1709 m_to.maybe_postinc (-1);
1710 --m_offset;
1712 return m_to.adjust (QImode, m_offset);
1715 /* Determine whether the LEN bytes generated by CONSTFUN can be
1716 stored to memory using several move instructions. CONSTFUNDATA is
1717 a pointer which will be passed as argument in every CONSTFUN call.
1718 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1719 a memset operation and false if it's a copy of a constant string.
1720 Return true if a call to store_by_pieces should succeed. */
1722 bool
1723 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1724 by_pieces_constfn constfun,
1725 void *constfundata, unsigned int align, bool memsetp)
1727 unsigned HOST_WIDE_INT l;
1728 unsigned int max_size;
1729 HOST_WIDE_INT offset = 0;
1730 enum insn_code icode;
1731 int reverse;
1732 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1733 rtx cst ATTRIBUTE_UNUSED;
1735 if (len == 0)
1736 return true;
1738 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1739 memsetp
1740 ? SET_BY_PIECES
1741 : STORE_BY_PIECES,
1742 optimize_insn_for_speed_p ()))
1743 return false;
1745 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1747 /* We would first store what we can in the largest integer mode, then go to
1748 successively smaller modes. */
1750 for (reverse = 0;
1751 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1752 reverse++)
1754 l = len;
1755 max_size = STORE_MAX_PIECES + 1;
1756 while (max_size > 1 && l > 0)
1758 auto op = memsetp ? SET_BY_PIECES : STORE_BY_PIECES;
1759 auto mode = widest_fixed_size_mode_for_size (max_size, op);
1761 icode = optab_handler (mov_optab, mode);
1762 if (icode != CODE_FOR_nothing
1763 && align >= GET_MODE_ALIGNMENT (mode))
1765 unsigned int size = GET_MODE_SIZE (mode);
1767 while (l >= size)
1769 if (reverse)
1770 offset -= size;
1772 cst = (*constfun) (constfundata, nullptr, offset, mode);
1773 /* All CONST_VECTORs can be loaded for memset since
1774 vec_duplicate_optab is a precondition to pick a
1775 vector mode for the memset expander. */
1776 if (!((memsetp && VECTOR_MODE_P (mode))
1777 || targetm.legitimate_constant_p (mode, cst)))
1778 return false;
1780 if (!reverse)
1781 offset += size;
1783 l -= size;
1787 max_size = GET_MODE_SIZE (mode);
1790 /* The code above should have handled everything. */
1791 gcc_assert (!l);
1794 return true;
1797 /* Generate several move instructions to store LEN bytes generated by
1798 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1799 pointer which will be passed as argument in every CONSTFUN call.
1800 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1801 a memset operation and false if it's a copy of a constant string.
1802 Return value is based on RETMODE argument. */
1805 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1806 by_pieces_constfn constfun,
1807 void *constfundata, unsigned int align, bool memsetp,
1808 memop_ret retmode)
1810 if (len == 0)
1812 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1813 return to;
1816 gcc_assert (targetm.use_by_pieces_infrastructure_p
1817 (len, align,
1818 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1819 optimize_insn_for_speed_p ()));
1821 store_by_pieces_d data (to, constfun, constfundata, len, align,
1822 memsetp ? SET_BY_PIECES : STORE_BY_PIECES);
1823 data.run ();
1825 if (retmode != RETURN_BEGIN)
1826 return data.finish_retmode (retmode);
1827 else
1828 return to;
1831 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1832 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1834 static void
1835 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1837 if (len == 0)
1838 return;
1840 /* Use builtin_memset_read_str to support vector mode broadcast. */
1841 char c = 0;
1842 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1843 CLEAR_BY_PIECES);
1844 data.run ();
1847 /* Context used by compare_by_pieces_genfn. It stores the fail label
1848 to jump to in case of miscomparison, and for branch ratios greater than 1,
1849 it stores an accumulator and the current and maximum counts before
1850 emitting another branch. */
1852 class compare_by_pieces_d : public op_by_pieces_d
1854 rtx_code_label *m_fail_label;
1855 rtx m_accumulator;
1856 int m_count, m_batch;
1858 void generate (rtx, rtx, machine_mode) final override;
1859 bool prepare_mode (machine_mode, unsigned int) final override;
1860 void finish_mode (machine_mode) final override;
1862 public:
1863 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1864 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1865 rtx_code_label *fail_label)
1866 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1867 op1_cfn_data, len, align, false, COMPARE_BY_PIECES)
1869 m_fail_label = fail_label;
1873 /* A callback used when iterating for a compare_by_pieces_operation.
1874 OP0 and OP1 are the values that have been loaded and should be
1875 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1876 context structure. */
1878 void
1879 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1881 if (m_batch > 1)
1883 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1884 true, OPTAB_LIB_WIDEN);
1885 if (m_count != 0)
1886 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1887 true, OPTAB_LIB_WIDEN);
1888 m_accumulator = temp;
1890 if (++m_count < m_batch)
1891 return;
1893 m_count = 0;
1894 op0 = m_accumulator;
1895 op1 = const0_rtx;
1896 m_accumulator = NULL_RTX;
1898 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1899 m_fail_label, profile_probability::uninitialized ());
1902 /* Return true if MODE can be used for a set of moves and comparisons,
1903 given an alignment ALIGN. Prepare whatever data is necessary for
1904 later calls to generate. */
1906 bool
1907 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1909 insn_code icode = optab_handler (mov_optab, mode);
1910 if (icode == CODE_FOR_nothing
1911 || align < GET_MODE_ALIGNMENT (mode)
1912 || !can_compare_p (EQ, mode, ccp_jump))
1913 return false;
1914 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1915 if (m_batch < 0)
1916 return false;
1917 m_accumulator = NULL_RTX;
1918 m_count = 0;
1919 return true;
1922 /* Called after expanding a series of comparisons in MODE. If we have
1923 accumulated results for which we haven't emitted a branch yet, do
1924 so now. */
1926 void
1927 compare_by_pieces_d::finish_mode (machine_mode mode)
1929 if (m_accumulator != NULL_RTX)
1930 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1931 NULL_RTX, NULL, m_fail_label,
1932 profile_probability::uninitialized ());
1935 /* Generate several move instructions to compare LEN bytes from blocks
1936 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1938 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1939 used to push FROM to the stack.
1941 ALIGN is maximum stack alignment we can assume.
1943 Optionally, the caller can pass a constfn and associated data in A1_CFN
1944 and A1_CFN_DATA. describing that the second operand being compared is a
1945 known constant and how to obtain its data. */
1947 static rtx
1948 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1949 rtx target, unsigned int align,
1950 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1952 rtx_code_label *fail_label = gen_label_rtx ();
1953 rtx_code_label *end_label = gen_label_rtx ();
1955 if (target == NULL_RTX
1956 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1957 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1959 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1960 fail_label);
1962 data.run ();
1964 emit_move_insn (target, const0_rtx);
1965 emit_jump (end_label);
1966 emit_barrier ();
1967 emit_label (fail_label);
1968 emit_move_insn (target, const1_rtx);
1969 emit_label (end_label);
1971 return target;
1974 /* Emit code to move a block Y to a block X. This may be done with
1975 string-move instructions, with multiple scalar move instructions,
1976 or with a library call.
1978 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1979 SIZE is an rtx that says how long they are.
1980 ALIGN is the maximum alignment we can assume they have.
1981 METHOD describes what kind of copy this is, and what mechanisms may be used.
1982 MIN_SIZE is the minimal size of block to move
1983 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1984 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1986 Return the address of the new block, if memcpy is called and returns it,
1987 0 otherwise. */
1990 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1991 unsigned int expected_align, HOST_WIDE_INT expected_size,
1992 unsigned HOST_WIDE_INT min_size,
1993 unsigned HOST_WIDE_INT max_size,
1994 unsigned HOST_WIDE_INT probable_max_size,
1995 bool bail_out_libcall, bool *is_move_done,
1996 bool might_overlap)
1998 int may_use_call;
1999 rtx retval = 0;
2000 unsigned int align;
2002 if (is_move_done)
2003 *is_move_done = true;
2005 gcc_assert (size);
2006 if (CONST_INT_P (size) && INTVAL (size) == 0)
2007 return 0;
2009 switch (method)
2011 case BLOCK_OP_NORMAL:
2012 case BLOCK_OP_TAILCALL:
2013 may_use_call = 1;
2014 break;
2016 case BLOCK_OP_CALL_PARM:
2017 may_use_call = block_move_libcall_safe_for_call_parm ();
2019 /* Make inhibit_defer_pop nonzero around the library call
2020 to force it to pop the arguments right away. */
2021 NO_DEFER_POP;
2022 break;
2024 case BLOCK_OP_NO_LIBCALL:
2025 may_use_call = 0;
2026 break;
2028 case BLOCK_OP_NO_LIBCALL_RET:
2029 may_use_call = -1;
2030 break;
2032 default:
2033 gcc_unreachable ();
2036 gcc_assert (MEM_P (x) && MEM_P (y));
2037 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2038 gcc_assert (align >= BITS_PER_UNIT);
2040 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2041 block copy is more efficient for other large modes, e.g. DCmode. */
2042 x = adjust_address (x, BLKmode, 0);
2043 y = adjust_address (y, BLKmode, 0);
2045 /* If source and destination are the same, no need to copy anything. */
2046 if (rtx_equal_p (x, y)
2047 && !MEM_VOLATILE_P (x)
2048 && !MEM_VOLATILE_P (y))
2049 return 0;
2051 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2052 can be incorrect is coming from __builtin_memcpy. */
2053 poly_int64 const_size;
2054 if (poly_int_rtx_p (size, &const_size))
2056 x = shallow_copy_rtx (x);
2057 y = shallow_copy_rtx (y);
2058 set_mem_size (x, const_size);
2059 set_mem_size (y, const_size);
2062 bool pieces_ok = CONST_INT_P (size)
2063 && can_move_by_pieces (INTVAL (size), align);
2064 bool pattern_ok = false;
2066 if (!pieces_ok || might_overlap)
2068 pattern_ok
2069 = emit_block_move_via_pattern (x, y, size, align,
2070 expected_align, expected_size,
2071 min_size, max_size, probable_max_size,
2072 might_overlap);
2073 if (!pattern_ok && might_overlap)
2075 /* Do not try any of the other methods below as they are not safe
2076 for overlapping moves. */
2077 *is_move_done = false;
2078 return retval;
2082 if (pattern_ok)
2084 else if (pieces_ok)
2085 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2086 else if (may_use_call && !might_overlap
2087 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2088 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2090 if (bail_out_libcall)
2092 if (is_move_done)
2093 *is_move_done = false;
2094 return retval;
2097 if (may_use_call < 0)
2098 return pc_rtx;
2100 retval = emit_block_copy_via_libcall (x, y, size,
2101 method == BLOCK_OP_TAILCALL);
2103 else if (might_overlap)
2104 *is_move_done = false;
2105 else
2106 emit_block_move_via_loop (x, y, size, align);
2108 if (method == BLOCK_OP_CALL_PARM)
2109 OK_DEFER_POP;
2111 return retval;
2115 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
2117 unsigned HOST_WIDE_INT max, min = 0;
2118 if (GET_CODE (size) == CONST_INT)
2119 min = max = UINTVAL (size);
2120 else
2121 max = GET_MODE_MASK (GET_MODE (size));
2122 return emit_block_move_hints (x, y, size, method, 0, -1,
2123 min, max, max);
2126 /* A subroutine of emit_block_move. Returns true if calling the
2127 block move libcall will not clobber any parameters which may have
2128 already been placed on the stack. */
2130 static bool
2131 block_move_libcall_safe_for_call_parm (void)
2133 tree fn;
2135 /* If arguments are pushed on the stack, then they're safe. */
2136 if (targetm.calls.push_argument (0))
2137 return true;
2139 /* If registers go on the stack anyway, any argument is sure to clobber
2140 an outgoing argument. */
2141 #if defined (REG_PARM_STACK_SPACE)
2142 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2143 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2144 depend on its argument. */
2145 (void) fn;
2146 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2147 && REG_PARM_STACK_SPACE (fn) != 0)
2148 return false;
2149 #endif
2151 /* If any argument goes in memory, then it might clobber an outgoing
2152 argument. */
2154 CUMULATIVE_ARGS args_so_far_v;
2155 cumulative_args_t args_so_far;
2156 tree arg;
2158 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2159 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2160 args_so_far = pack_cumulative_args (&args_so_far_v);
2162 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2163 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2165 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2166 function_arg_info arg_info (mode, /*named=*/true);
2167 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2168 if (!tmp || !REG_P (tmp))
2169 return false;
2170 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2171 return false;
2172 targetm.calls.function_arg_advance (args_so_far, arg_info);
2175 return true;
2178 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2179 return true if successful.
2181 X is the destination of the copy or move.
2182 Y is the source of the copy or move.
2183 SIZE is the size of the block to be moved.
2185 MIGHT_OVERLAP indicates this originated with expansion of a
2186 builtin_memmove() and the source and destination blocks may
2187 overlap.
2190 static bool
2191 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2192 unsigned int expected_align,
2193 HOST_WIDE_INT expected_size,
2194 unsigned HOST_WIDE_INT min_size,
2195 unsigned HOST_WIDE_INT max_size,
2196 unsigned HOST_WIDE_INT probable_max_size,
2197 bool might_overlap)
2199 if (expected_align < align)
2200 expected_align = align;
2201 if (expected_size != -1)
2203 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2204 expected_size = probable_max_size;
2205 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2206 expected_size = min_size;
2209 /* Since this is a move insn, we don't care about volatility. */
2210 temporary_volatile_ok v (true);
2212 /* Try the most limited insn first, because there's no point
2213 including more than one in the machine description unless
2214 the more limited one has some advantage. */
2216 opt_scalar_int_mode mode_iter;
2217 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2219 scalar_int_mode mode = mode_iter.require ();
2220 enum insn_code code;
2221 if (might_overlap)
2222 code = direct_optab_handler (movmem_optab, mode);
2223 else
2224 code = direct_optab_handler (cpymem_optab, mode);
2226 if (code != CODE_FOR_nothing
2227 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2228 here because if SIZE is less than the mode mask, as it is
2229 returned by the macro, it will definitely be less than the
2230 actual mode mask. Since SIZE is within the Pmode address
2231 space, we limit MODE to Pmode. */
2232 && ((CONST_INT_P (size)
2233 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2234 <= (GET_MODE_MASK (mode) >> 1)))
2235 || max_size <= (GET_MODE_MASK (mode) >> 1)
2236 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2238 class expand_operand ops[9];
2239 unsigned int nops;
2241 /* ??? When called via emit_block_move_for_call, it'd be
2242 nice if there were some way to inform the backend, so
2243 that it doesn't fail the expansion because it thinks
2244 emitting the libcall would be more efficient. */
2245 nops = insn_data[(int) code].n_generator_args;
2246 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2248 create_fixed_operand (&ops[0], x);
2249 create_fixed_operand (&ops[1], y);
2250 /* The check above guarantees that this size conversion is valid. */
2251 create_convert_operand_to (&ops[2], size, mode, true);
2252 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2253 if (nops >= 6)
2255 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2256 create_integer_operand (&ops[5], expected_size);
2258 if (nops >= 8)
2260 create_integer_operand (&ops[6], min_size);
2261 /* If we cannot represent the maximal size,
2262 make parameter NULL. */
2263 if ((HOST_WIDE_INT) max_size != -1)
2264 create_integer_operand (&ops[7], max_size);
2265 else
2266 create_fixed_operand (&ops[7], NULL);
2268 if (nops == 9)
2270 /* If we cannot represent the maximal size,
2271 make parameter NULL. */
2272 if ((HOST_WIDE_INT) probable_max_size != -1)
2273 create_integer_operand (&ops[8], probable_max_size);
2274 else
2275 create_fixed_operand (&ops[8], NULL);
2277 if (maybe_expand_insn (code, nops, ops))
2278 return true;
2282 return false;
2285 /* A subroutine of emit_block_move. Copy the data via an explicit
2286 loop. This is used only when libcalls are forbidden. */
2287 /* ??? It'd be nice to copy in hunks larger than QImode. */
2289 static void
2290 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2291 unsigned int align ATTRIBUTE_UNUSED)
2293 rtx_code_label *cmp_label, *top_label;
2294 rtx iter, x_addr, y_addr, tmp;
2295 machine_mode x_addr_mode = get_address_mode (x);
2296 machine_mode y_addr_mode = get_address_mode (y);
2297 machine_mode iter_mode;
2299 iter_mode = GET_MODE (size);
2300 if (iter_mode == VOIDmode)
2301 iter_mode = word_mode;
2303 top_label = gen_label_rtx ();
2304 cmp_label = gen_label_rtx ();
2305 iter = gen_reg_rtx (iter_mode);
2307 emit_move_insn (iter, const0_rtx);
2309 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2310 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2311 do_pending_stack_adjust ();
2313 emit_jump (cmp_label);
2314 emit_label (top_label);
2316 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2317 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2319 if (x_addr_mode != y_addr_mode)
2320 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2321 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2323 x = change_address (x, QImode, x_addr);
2324 y = change_address (y, QImode, y_addr);
2326 emit_move_insn (x, y);
2328 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2329 true, OPTAB_LIB_WIDEN);
2330 if (tmp != iter)
2331 emit_move_insn (iter, tmp);
2333 emit_label (cmp_label);
2335 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2336 true, top_label,
2337 profile_probability::guessed_always ()
2338 .apply_scale (9, 10));
2341 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2342 TAILCALL is true if this is a tail call. */
2345 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2346 rtx size, bool tailcall)
2348 rtx dst_addr, src_addr;
2349 tree call_expr, dst_tree, src_tree, size_tree;
2350 machine_mode size_mode;
2352 /* Since dst and src are passed to a libcall, mark the corresponding
2353 tree EXPR as addressable. */
2354 tree dst_expr = MEM_EXPR (dst);
2355 tree src_expr = MEM_EXPR (src);
2356 if (dst_expr)
2357 mark_addressable (dst_expr);
2358 if (src_expr)
2359 mark_addressable (src_expr);
2361 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2362 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2363 dst_tree = make_tree (ptr_type_node, dst_addr);
2365 src_addr = copy_addr_to_reg (XEXP (src, 0));
2366 src_addr = convert_memory_address (ptr_mode, src_addr);
2367 src_tree = make_tree (ptr_type_node, src_addr);
2369 size_mode = TYPE_MODE (sizetype);
2370 size = convert_to_mode (size_mode, size, 1);
2371 size = copy_to_mode_reg (size_mode, size);
2372 size_tree = make_tree (sizetype, size);
2374 /* It is incorrect to use the libcall calling conventions for calls to
2375 memcpy/memmove/memcmp because they can be provided by the user. */
2376 tree fn = builtin_decl_implicit (fncode);
2377 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2378 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2380 return expand_call (call_expr, NULL_RTX, false);
2383 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2384 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2385 otherwise return null. */
2388 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2389 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2390 HOST_WIDE_INT align)
2392 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2394 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2395 target = NULL_RTX;
2397 class expand_operand ops[5];
2398 create_output_operand (&ops[0], target, insn_mode);
2399 create_fixed_operand (&ops[1], arg1_rtx);
2400 create_fixed_operand (&ops[2], arg2_rtx);
2401 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2402 TYPE_UNSIGNED (arg3_type));
2403 create_integer_operand (&ops[4], align);
2404 if (maybe_expand_insn (icode, 5, ops))
2405 return ops[0].value;
2406 return NULL_RTX;
2409 /* Expand a block compare between X and Y with length LEN using the
2410 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2411 of the expression that was used to calculate the length. ALIGN
2412 gives the known minimum common alignment. */
2414 static rtx
2415 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2416 unsigned align)
2418 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2419 implementing memcmp because it will stop if it encounters two
2420 zero bytes. */
2421 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2423 if (icode == CODE_FOR_nothing)
2424 return NULL_RTX;
2426 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2429 /* Emit code to compare a block Y to a block X. This may be done with
2430 string-compare instructions, with multiple scalar instructions,
2431 or with a library call.
2433 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2434 they are. LEN_TYPE is the type of the expression that was used to
2435 calculate it.
2437 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2438 value of a normal memcmp call, instead we can just compare for equality.
2439 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2440 returning NULL_RTX.
2442 Optionally, the caller can pass a constfn and associated data in Y_CFN
2443 and Y_CFN_DATA. describing that the second operand being compared is a
2444 known constant and how to obtain its data.
2445 Return the result of the comparison, or NULL_RTX if we failed to
2446 perform the operation. */
2449 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2450 bool equality_only, by_pieces_constfn y_cfn,
2451 void *y_cfndata)
2453 rtx result = 0;
2455 if (CONST_INT_P (len) && INTVAL (len) == 0)
2456 return const0_rtx;
2458 gcc_assert (MEM_P (x) && MEM_P (y));
2459 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2460 gcc_assert (align >= BITS_PER_UNIT);
2462 x = adjust_address (x, BLKmode, 0);
2463 y = adjust_address (y, BLKmode, 0);
2465 if (equality_only
2466 && CONST_INT_P (len)
2467 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2468 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2469 y_cfn, y_cfndata);
2470 else
2471 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2473 return result;
2476 /* Copy all or part of a value X into registers starting at REGNO.
2477 The number of registers to be filled is NREGS. */
2479 void
2480 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2482 if (nregs == 0)
2483 return;
2485 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2486 x = validize_mem (force_const_mem (mode, x));
2488 /* See if the machine can do this with a load multiple insn. */
2489 if (targetm.have_load_multiple ())
2491 rtx_insn *last = get_last_insn ();
2492 rtx first = gen_rtx_REG (word_mode, regno);
2493 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2494 GEN_INT (nregs)))
2496 emit_insn (pat);
2497 return;
2499 else
2500 delete_insns_since (last);
2503 for (int i = 0; i < nregs; i++)
2504 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2505 operand_subword_force (x, i, mode));
2508 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2509 The number of registers to be filled is NREGS. */
2511 void
2512 move_block_from_reg (int regno, rtx x, int nregs)
2514 if (nregs == 0)
2515 return;
2517 /* See if the machine can do this with a store multiple insn. */
2518 if (targetm.have_store_multiple ())
2520 rtx_insn *last = get_last_insn ();
2521 rtx first = gen_rtx_REG (word_mode, regno);
2522 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2523 GEN_INT (nregs)))
2525 emit_insn (pat);
2526 return;
2528 else
2529 delete_insns_since (last);
2532 for (int i = 0; i < nregs; i++)
2534 rtx tem = operand_subword (x, i, 1, BLKmode);
2536 gcc_assert (tem);
2538 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2542 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2543 ORIG, where ORIG is a non-consecutive group of registers represented by
2544 a PARALLEL. The clone is identical to the original except in that the
2545 original set of registers is replaced by a new set of pseudo registers.
2546 The new set has the same modes as the original set. */
2549 gen_group_rtx (rtx orig)
2551 int i, length;
2552 rtx *tmps;
2554 gcc_assert (GET_CODE (orig) == PARALLEL);
2556 length = XVECLEN (orig, 0);
2557 tmps = XALLOCAVEC (rtx, length);
2559 /* Skip a NULL entry in first slot. */
2560 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2562 if (i)
2563 tmps[0] = 0;
2565 for (; i < length; i++)
2567 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2568 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2570 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2573 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2576 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2577 except that values are placed in TMPS[i], and must later be moved
2578 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2580 static void
2581 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2582 poly_int64 ssize)
2584 rtx src;
2585 int start, i;
2586 machine_mode m = GET_MODE (orig_src);
2588 gcc_assert (GET_CODE (dst) == PARALLEL);
2590 if (m != VOIDmode
2591 && !SCALAR_INT_MODE_P (m)
2592 && !MEM_P (orig_src)
2593 && GET_CODE (orig_src) != CONCAT)
2595 scalar_int_mode imode;
2596 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2598 src = gen_reg_rtx (imode);
2599 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2601 else
2603 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2604 emit_move_insn (src, orig_src);
2606 emit_group_load_1 (tmps, dst, src, type, ssize);
2607 return;
2610 /* Check for a NULL entry, used to indicate that the parameter goes
2611 both on the stack and in registers. */
2612 if (XEXP (XVECEXP (dst, 0, 0), 0))
2613 start = 0;
2614 else
2615 start = 1;
2617 /* Process the pieces. */
2618 for (i = start; i < XVECLEN (dst, 0); i++)
2620 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2621 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2622 poly_int64 bytelen = GET_MODE_SIZE (mode);
2623 poly_int64 shift = 0;
2625 /* Handle trailing fragments that run over the size of the struct.
2626 It's the target's responsibility to make sure that the fragment
2627 cannot be strictly smaller in some cases and strictly larger
2628 in others. */
2629 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2630 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2632 /* Arrange to shift the fragment to where it belongs.
2633 extract_bit_field loads to the lsb of the reg. */
2634 if (
2635 #ifdef BLOCK_REG_PADDING
2636 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2637 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2638 #else
2639 BYTES_BIG_ENDIAN
2640 #endif
2642 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2643 bytelen = ssize - bytepos;
2644 gcc_assert (maybe_gt (bytelen, 0));
2647 /* If we won't be loading directly from memory, protect the real source
2648 from strange tricks we might play; but make sure that the source can
2649 be loaded directly into the destination. */
2650 src = orig_src;
2651 if (!MEM_P (orig_src)
2652 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src))
2653 && !CONSTANT_P (orig_src))
2655 gcc_assert (GET_MODE (orig_src) != VOIDmode);
2656 src = force_reg (GET_MODE (orig_src), orig_src);
2659 /* Optimize the access just a bit. */
2660 if (MEM_P (src)
2661 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2662 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2663 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2664 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2666 tmps[i] = gen_reg_rtx (mode);
2667 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2669 else if (COMPLEX_MODE_P (mode)
2670 && GET_MODE (src) == mode
2671 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2672 /* Let emit_move_complex do the bulk of the work. */
2673 tmps[i] = src;
2674 else if (GET_CODE (src) == CONCAT)
2676 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2677 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2678 unsigned int elt;
2679 poly_int64 subpos;
2681 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2682 && known_le (subpos + bytelen, slen0))
2684 /* The following assumes that the concatenated objects all
2685 have the same size. In this case, a simple calculation
2686 can be used to determine the object and the bit field
2687 to be extracted. */
2688 tmps[i] = XEXP (src, elt);
2689 if (maybe_ne (subpos, 0)
2690 || maybe_ne (subpos + bytelen, slen0)
2691 || (!CONSTANT_P (tmps[i])
2692 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2693 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2694 subpos * BITS_PER_UNIT,
2695 1, NULL_RTX, mode, mode, false,
2696 NULL);
2698 else
2700 rtx mem;
2702 gcc_assert (known_eq (bytepos, 0));
2703 mem = assign_stack_temp (GET_MODE (src), slen);
2704 emit_move_insn (mem, src);
2705 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2706 0, 1, NULL_RTX, mode, mode, false,
2707 NULL);
2710 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2711 && XVECLEN (dst, 0) > 1)
2712 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2713 else if (CONSTANT_P (src))
2715 if (known_eq (bytelen, ssize))
2716 tmps[i] = src;
2717 else
2719 rtx first, second;
2721 /* TODO: const_wide_int can have sizes other than this... */
2722 gcc_assert (known_eq (2 * bytelen, ssize));
2723 split_double (src, &first, &second);
2724 if (i)
2725 tmps[i] = second;
2726 else
2727 tmps[i] = first;
2730 else if (REG_P (src) && GET_MODE (src) == mode)
2731 tmps[i] = src;
2732 else
2733 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2734 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2735 mode, mode, false, NULL);
2737 if (maybe_ne (shift, 0))
2738 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2739 shift, tmps[i], 0);
2743 /* Emit code to move a block SRC of type TYPE to a block DST,
2744 where DST is non-consecutive registers represented by a PARALLEL.
2745 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2746 if not known. */
2748 void
2749 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2751 rtx *tmps;
2752 int i;
2754 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2755 emit_group_load_1 (tmps, dst, src, type, ssize);
2757 /* Copy the extracted pieces into the proper (probable) hard regs. */
2758 for (i = 0; i < XVECLEN (dst, 0); i++)
2760 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2761 if (d == NULL)
2762 continue;
2763 emit_move_insn (d, tmps[i]);
2767 /* Similar, but load SRC into new pseudos in a format that looks like
2768 PARALLEL. This can later be fed to emit_group_move to get things
2769 in the right place. */
2772 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2774 rtvec vec;
2775 int i;
2777 vec = rtvec_alloc (XVECLEN (parallel, 0));
2778 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2780 /* Convert the vector to look just like the original PARALLEL, except
2781 with the computed values. */
2782 for (i = 0; i < XVECLEN (parallel, 0); i++)
2784 rtx e = XVECEXP (parallel, 0, i);
2785 rtx d = XEXP (e, 0);
2787 if (d)
2789 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2790 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2792 RTVEC_ELT (vec, i) = e;
2795 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2798 /* Emit code to move a block SRC to block DST, where SRC and DST are
2799 non-consecutive groups of registers, each represented by a PARALLEL. */
2801 void
2802 emit_group_move (rtx dst, rtx src)
2804 int i;
2806 gcc_assert (GET_CODE (src) == PARALLEL
2807 && GET_CODE (dst) == PARALLEL
2808 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2810 /* Skip first entry if NULL. */
2811 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2812 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2813 XEXP (XVECEXP (src, 0, i), 0));
2816 /* Move a group of registers represented by a PARALLEL into pseudos. */
2819 emit_group_move_into_temps (rtx src)
2821 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2822 int i;
2824 for (i = 0; i < XVECLEN (src, 0); i++)
2826 rtx e = XVECEXP (src, 0, i);
2827 rtx d = XEXP (e, 0);
2829 if (d)
2830 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2831 RTVEC_ELT (vec, i) = e;
2834 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2837 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2838 where SRC is non-consecutive registers represented by a PARALLEL.
2839 SSIZE represents the total size of block ORIG_DST, or -1 if not
2840 known. */
2842 void
2843 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2844 poly_int64 ssize)
2846 rtx *tmps, dst;
2847 int start, finish, i;
2848 machine_mode m = GET_MODE (orig_dst);
2850 gcc_assert (GET_CODE (src) == PARALLEL);
2852 if (!SCALAR_INT_MODE_P (m)
2853 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2855 scalar_int_mode imode;
2856 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2858 dst = gen_reg_rtx (imode);
2859 emit_group_store (dst, src, type, ssize);
2860 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2862 else
2864 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2865 emit_group_store (dst, src, type, ssize);
2867 emit_move_insn (orig_dst, dst);
2868 return;
2871 /* Check for a NULL entry, used to indicate that the parameter goes
2872 both on the stack and in registers. */
2873 if (XEXP (XVECEXP (src, 0, 0), 0))
2874 start = 0;
2875 else
2876 start = 1;
2877 finish = XVECLEN (src, 0);
2879 tmps = XALLOCAVEC (rtx, finish);
2881 /* Copy the (probable) hard regs into pseudos. */
2882 for (i = start; i < finish; i++)
2884 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2885 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2887 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2888 emit_move_insn (tmps[i], reg);
2890 else
2891 tmps[i] = reg;
2894 /* If we won't be storing directly into memory, protect the real destination
2895 from strange tricks we might play. */
2896 dst = orig_dst;
2897 if (GET_CODE (dst) == PARALLEL)
2899 rtx temp;
2901 /* We can get a PARALLEL dst if there is a conditional expression in
2902 a return statement. In that case, the dst and src are the same,
2903 so no action is necessary. */
2904 if (rtx_equal_p (dst, src))
2905 return;
2907 /* It is unclear if we can ever reach here, but we may as well handle
2908 it. Allocate a temporary, and split this into a store/load to/from
2909 the temporary. */
2910 temp = assign_stack_temp (GET_MODE (dst), ssize);
2911 emit_group_store (temp, src, type, ssize);
2912 emit_group_load (dst, temp, type, ssize);
2913 return;
2915 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2917 machine_mode outer = GET_MODE (dst);
2918 machine_mode inner;
2919 poly_int64 bytepos;
2920 bool done = false;
2921 rtx temp;
2923 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2924 dst = gen_reg_rtx (outer);
2926 /* Make life a bit easier for combine: if the first element of the
2927 vector is the low part of the destination mode, use a paradoxical
2928 subreg to initialize the destination. */
2929 if (start < finish)
2931 inner = GET_MODE (tmps[start]);
2932 bytepos = subreg_lowpart_offset (inner, outer);
2933 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2934 bytepos))
2936 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
2937 if (temp)
2939 emit_move_insn (dst, temp);
2940 done = true;
2941 start++;
2946 /* If the first element wasn't the low part, try the last. */
2947 if (!done
2948 && start < finish - 1)
2950 inner = GET_MODE (tmps[finish - 1]);
2951 bytepos = subreg_lowpart_offset (inner, outer);
2952 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2953 finish - 1), 1)),
2954 bytepos))
2956 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
2957 if (temp)
2959 emit_move_insn (dst, temp);
2960 done = true;
2961 finish--;
2966 /* Otherwise, simply initialize the result to zero. */
2967 if (!done)
2968 emit_move_insn (dst, CONST0_RTX (outer));
2971 /* Process the pieces. */
2972 for (i = start; i < finish; i++)
2974 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2975 machine_mode mode = GET_MODE (tmps[i]);
2976 poly_int64 bytelen = GET_MODE_SIZE (mode);
2977 poly_uint64 adj_bytelen;
2978 rtx dest = dst;
2980 /* Handle trailing fragments that run over the size of the struct.
2981 It's the target's responsibility to make sure that the fragment
2982 cannot be strictly smaller in some cases and strictly larger
2983 in others. */
2984 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2985 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2986 adj_bytelen = ssize - bytepos;
2987 else
2988 adj_bytelen = bytelen;
2990 /* Deal with destination CONCATs by either storing into one of the parts
2991 or doing a copy after storing into a register or stack temporary. */
2992 if (GET_CODE (dst) == CONCAT)
2994 if (known_le (bytepos + adj_bytelen,
2995 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2996 dest = XEXP (dst, 0);
2998 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3000 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
3001 dest = XEXP (dst, 1);
3004 else
3006 machine_mode dest_mode = GET_MODE (dest);
3007 machine_mode tmp_mode = GET_MODE (tmps[i]);
3008 scalar_int_mode dest_imode;
3010 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
3012 /* If the source is a single scalar integer register, and the
3013 destination has a complex mode for which a same-sized integer
3014 mode exists, then we can take the left-justified part of the
3015 source in the complex mode. */
3016 if (finish == start + 1
3017 && REG_P (tmps[i])
3018 && SCALAR_INT_MODE_P (tmp_mode)
3019 && COMPLEX_MODE_P (dest_mode)
3020 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3022 const scalar_int_mode tmp_imode
3023 = as_a <scalar_int_mode> (tmp_mode);
3025 if (GET_MODE_BITSIZE (dest_imode)
3026 < GET_MODE_BITSIZE (tmp_imode))
3028 dest = gen_reg_rtx (dest_imode);
3029 if (BYTES_BIG_ENDIAN)
3030 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3031 GET_MODE_BITSIZE (tmp_imode)
3032 - GET_MODE_BITSIZE (dest_imode),
3033 NULL_RTX, 1);
3034 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3035 dst = gen_lowpart (dest_mode, dest);
3037 else
3038 dst = gen_lowpart (dest_mode, tmps[i]);
3041 /* Otherwise spill the source onto the stack using the more
3042 aligned of the two modes. */
3043 else if (GET_MODE_ALIGNMENT (dest_mode)
3044 >= GET_MODE_ALIGNMENT (tmp_mode))
3046 dest = assign_stack_temp (dest_mode,
3047 GET_MODE_SIZE (dest_mode));
3048 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3049 tmps[i]);
3050 dst = dest;
3053 else
3055 dest = assign_stack_temp (tmp_mode,
3056 GET_MODE_SIZE (tmp_mode));
3057 emit_move_insn (dest, tmps[i]);
3058 dst = adjust_address (dest, dest_mode, bytepos);
3061 break;
3065 /* Handle trailing fragments that run over the size of the struct. */
3066 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3068 /* store_bit_field always takes its value from the lsb.
3069 Move the fragment to the lsb if it's not already there. */
3070 if (
3071 #ifdef BLOCK_REG_PADDING
3072 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3073 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3074 #else
3075 BYTES_BIG_ENDIAN
3076 #endif
3079 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3080 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3081 shift, tmps[i], 0);
3084 /* Make sure not to write past the end of the struct. */
3085 store_bit_field (dest,
3086 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3087 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3088 VOIDmode, tmps[i], false, false);
3091 /* Optimize the access just a bit. */
3092 else if (MEM_P (dest)
3093 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3094 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3095 && multiple_p (bytepos * BITS_PER_UNIT,
3096 GET_MODE_ALIGNMENT (mode))
3097 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3098 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3100 else
3101 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3102 0, 0, mode, tmps[i], false, false);
3105 /* Copy from the pseudo into the (probable) hard reg. */
3106 if (orig_dst != dst)
3107 emit_move_insn (orig_dst, dst);
3110 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3111 of the value stored in X. */
3114 maybe_emit_group_store (rtx x, tree type)
3116 machine_mode mode = TYPE_MODE (type);
3117 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3118 if (GET_CODE (x) == PARALLEL)
3120 rtx result = gen_reg_rtx (mode);
3121 emit_group_store (result, x, type, int_size_in_bytes (type));
3122 return result;
3124 return x;
3127 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3129 This is used on targets that return BLKmode values in registers. */
3131 static void
3132 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3134 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3135 rtx src = NULL, dst = NULL;
3136 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3137 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3138 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3139 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3140 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3141 fixed_size_mode copy_mode;
3143 /* BLKmode registers created in the back-end shouldn't have survived. */
3144 gcc_assert (mode != BLKmode);
3146 /* If the structure doesn't take up a whole number of words, see whether
3147 SRCREG is padded on the left or on the right. If it's on the left,
3148 set PADDING_CORRECTION to the number of bits to skip.
3150 In most ABIs, the structure will be returned at the least end of
3151 the register, which translates to right padding on little-endian
3152 targets and left padding on big-endian targets. The opposite
3153 holds if the structure is returned at the most significant
3154 end of the register. */
3155 if (bytes % UNITS_PER_WORD != 0
3156 && (targetm.calls.return_in_msb (type)
3157 ? !BYTES_BIG_ENDIAN
3158 : BYTES_BIG_ENDIAN))
3159 padding_correction
3160 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3162 /* We can use a single move if we have an exact mode for the size. */
3163 else if (MEM_P (target)
3164 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3165 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3166 && bytes == GET_MODE_SIZE (mode))
3168 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3169 return;
3172 /* And if we additionally have the same mode for a register. */
3173 else if (REG_P (target)
3174 && GET_MODE (target) == mode
3175 && bytes == GET_MODE_SIZE (mode))
3177 emit_move_insn (target, srcreg);
3178 return;
3181 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3182 into a new pseudo which is a full word. */
3183 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3185 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3186 mode = word_mode;
3189 /* Copy the structure BITSIZE bits at a time. If the target lives in
3190 memory, take care of not reading/writing past its end by selecting
3191 a copy mode suited to BITSIZE. This should always be possible given
3192 how it is computed.
3194 If the target lives in register, make sure not to select a copy mode
3195 larger than the mode of the register.
3197 We could probably emit more efficient code for machines which do not use
3198 strict alignment, but it doesn't seem worth the effort at the current
3199 time. */
3201 copy_mode = word_mode;
3202 if (MEM_P (target))
3204 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3205 if (mem_mode.exists ())
3206 copy_mode = mem_mode.require ();
3208 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3209 copy_mode = tmode;
3211 for (bitpos = 0, xbitpos = padding_correction;
3212 bitpos < bytes * BITS_PER_UNIT;
3213 bitpos += bitsize, xbitpos += bitsize)
3215 /* We need a new source operand each time xbitpos is on a
3216 word boundary and when xbitpos == padding_correction
3217 (the first time through). */
3218 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3219 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3221 /* We need a new destination operand each time bitpos is on
3222 a word boundary. */
3223 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3224 dst = target;
3225 else if (bitpos % BITS_PER_WORD == 0)
3226 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3228 /* Use xbitpos for the source extraction (right justified) and
3229 bitpos for the destination store (left justified). */
3230 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3231 extract_bit_field (src, bitsize,
3232 xbitpos % BITS_PER_WORD, 1,
3233 NULL_RTX, copy_mode, copy_mode,
3234 false, NULL),
3235 false, false);
3239 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3240 register if it contains any data, otherwise return null.
3242 This is used on targets that return BLKmode values in registers. */
3245 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3247 int i, n_regs;
3248 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3249 unsigned int bitsize;
3250 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3251 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3252 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3253 fixed_size_mode dst_mode;
3254 scalar_int_mode min_mode;
3256 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3258 x = expand_normal (src);
3260 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3261 if (bytes == 0)
3262 return NULL_RTX;
3264 /* If the structure doesn't take up a whole number of words, see
3265 whether the register value should be padded on the left or on
3266 the right. Set PADDING_CORRECTION to the number of padding
3267 bits needed on the left side.
3269 In most ABIs, the structure will be returned at the least end of
3270 the register, which translates to right padding on little-endian
3271 targets and left padding on big-endian targets. The opposite
3272 holds if the structure is returned at the most significant
3273 end of the register. */
3274 if (bytes % UNITS_PER_WORD != 0
3275 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3276 ? !BYTES_BIG_ENDIAN
3277 : BYTES_BIG_ENDIAN))
3278 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3279 * BITS_PER_UNIT));
3281 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3282 dst_words = XALLOCAVEC (rtx, n_regs);
3283 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3284 min_mode = smallest_int_mode_for_size (bitsize);
3286 /* Copy the structure BITSIZE bits at a time. */
3287 for (bitpos = 0, xbitpos = padding_correction;
3288 bitpos < bytes * BITS_PER_UNIT;
3289 bitpos += bitsize, xbitpos += bitsize)
3291 /* We need a new destination pseudo each time xbitpos is
3292 on a word boundary and when xbitpos == padding_correction
3293 (the first time through). */
3294 if (xbitpos % BITS_PER_WORD == 0
3295 || xbitpos == padding_correction)
3297 /* Generate an appropriate register. */
3298 dst_word = gen_reg_rtx (word_mode);
3299 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3301 /* Clear the destination before we move anything into it. */
3302 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3305 /* Find the largest integer mode that can be used to copy all or as
3306 many bits as possible of the structure if the target supports larger
3307 copies. There are too many corner cases here w.r.t to alignments on
3308 the read/writes. So if there is any padding just use single byte
3309 operations. */
3310 opt_scalar_int_mode mode_iter;
3311 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3313 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3315 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3316 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3317 && msize <= BITS_PER_WORD)
3318 bitsize = msize;
3319 else
3320 break;
3324 /* We need a new source operand each time bitpos is on a word
3325 boundary. */
3326 if (bitpos % BITS_PER_WORD == 0)
3327 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3329 /* Use bitpos for the source extraction (left justified) and
3330 xbitpos for the destination store (right justified). */
3331 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3332 0, 0, word_mode,
3333 extract_bit_field (src_word, bitsize,
3334 bitpos % BITS_PER_WORD, 1,
3335 NULL_RTX, word_mode, word_mode,
3336 false, NULL),
3337 false, false);
3340 if (mode == BLKmode)
3342 /* Find the smallest integer mode large enough to hold the
3343 entire structure. */
3344 opt_scalar_int_mode mode_iter;
3345 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3346 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3347 break;
3349 /* A suitable mode should have been found. */
3350 mode = mode_iter.require ();
3353 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3354 dst_mode = word_mode;
3355 else
3356 dst_mode = mode;
3357 dst = gen_reg_rtx (dst_mode);
3359 for (i = 0; i < n_regs; i++)
3360 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3362 if (mode != dst_mode)
3363 dst = gen_lowpart (mode, dst);
3365 return dst;
3368 /* Add a USE expression for REG to the (possibly empty) list pointed
3369 to by CALL_FUSAGE. REG must denote a hard register. */
3371 void
3372 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3374 gcc_assert (REG_P (reg));
3376 if (!HARD_REGISTER_P (reg))
3377 return;
3379 *call_fusage
3380 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3383 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3384 to by CALL_FUSAGE. REG must denote a hard register. */
3386 void
3387 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3389 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3391 *call_fusage
3392 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3395 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3396 starting at REGNO. All of these registers must be hard registers. */
3398 void
3399 use_regs (rtx *call_fusage, int regno, int nregs)
3401 int i;
3403 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3405 for (i = 0; i < nregs; i++)
3406 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3409 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3410 PARALLEL REGS. This is for calls that pass values in multiple
3411 non-contiguous locations. The Irix 6 ABI has examples of this. */
3413 void
3414 use_group_regs (rtx *call_fusage, rtx regs)
3416 int i;
3418 for (i = 0; i < XVECLEN (regs, 0); i++)
3420 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3422 /* A NULL entry means the parameter goes both on the stack and in
3423 registers. This can also be a MEM for targets that pass values
3424 partially on the stack and partially in registers. */
3425 if (reg != 0 && REG_P (reg))
3426 use_reg (call_fusage, reg);
3430 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3431 assigment and the code of the expresion on the RHS is CODE. Return
3432 NULL otherwise. */
3434 static gimple *
3435 get_def_for_expr (tree name, enum tree_code code)
3437 gimple *def_stmt;
3439 if (TREE_CODE (name) != SSA_NAME)
3440 return NULL;
3442 def_stmt = get_gimple_for_ssa_name (name);
3443 if (!def_stmt
3444 || gimple_assign_rhs_code (def_stmt) != code)
3445 return NULL;
3447 return def_stmt;
3450 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3451 assigment and the class of the expresion on the RHS is CLASS. Return
3452 NULL otherwise. */
3454 static gimple *
3455 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3457 gimple *def_stmt;
3459 if (TREE_CODE (name) != SSA_NAME)
3460 return NULL;
3462 def_stmt = get_gimple_for_ssa_name (name);
3463 if (!def_stmt
3464 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3465 return NULL;
3467 return def_stmt;
3470 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3471 its length in bytes. */
3474 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3475 unsigned int expected_align, HOST_WIDE_INT expected_size,
3476 unsigned HOST_WIDE_INT min_size,
3477 unsigned HOST_WIDE_INT max_size,
3478 unsigned HOST_WIDE_INT probable_max_size,
3479 unsigned ctz_size)
3481 machine_mode mode = GET_MODE (object);
3482 unsigned int align;
3484 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3486 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3487 just move a zero. Otherwise, do this a piece at a time. */
3488 poly_int64 size_val;
3489 if (mode != BLKmode
3490 && poly_int_rtx_p (size, &size_val)
3491 && known_eq (size_val, GET_MODE_SIZE (mode)))
3493 rtx zero = CONST0_RTX (mode);
3494 if (zero != NULL)
3496 emit_move_insn (object, zero);
3497 return NULL;
3500 if (COMPLEX_MODE_P (mode))
3502 zero = CONST0_RTX (GET_MODE_INNER (mode));
3503 if (zero != NULL)
3505 write_complex_part (object, zero, 0, true);
3506 write_complex_part (object, zero, 1, false);
3507 return NULL;
3512 if (size == const0_rtx)
3513 return NULL;
3515 align = MEM_ALIGN (object);
3517 if (CONST_INT_P (size)
3518 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3519 CLEAR_BY_PIECES,
3520 optimize_insn_for_speed_p ()))
3521 clear_by_pieces (object, INTVAL (size), align);
3522 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3523 expected_align, expected_size,
3524 min_size, max_size, probable_max_size))
3526 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3527 min_size, max_size,
3528 NULL_RTX, 0, align))
3530 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3531 return set_storage_via_libcall (object, size, const0_rtx,
3532 method == BLOCK_OP_TAILCALL);
3533 else
3534 gcc_unreachable ();
3536 return NULL;
3540 clear_storage (rtx object, rtx size, enum block_op_methods method)
3542 unsigned HOST_WIDE_INT max, min = 0;
3543 if (GET_CODE (size) == CONST_INT)
3544 min = max = UINTVAL (size);
3545 else
3546 max = GET_MODE_MASK (GET_MODE (size));
3547 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3551 /* A subroutine of clear_storage. Expand a call to memset.
3552 Return the return value of memset, 0 otherwise. */
3555 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3557 tree call_expr, fn, object_tree, size_tree, val_tree;
3558 machine_mode size_mode;
3560 object = copy_addr_to_reg (XEXP (object, 0));
3561 object_tree = make_tree (ptr_type_node, object);
3563 if (!CONST_INT_P (val))
3564 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3565 val_tree = make_tree (integer_type_node, val);
3567 size_mode = TYPE_MODE (sizetype);
3568 size = convert_to_mode (size_mode, size, 1);
3569 size = copy_to_mode_reg (size_mode, size);
3570 size_tree = make_tree (sizetype, size);
3572 /* It is incorrect to use the libcall calling conventions for calls to
3573 memset because it can be provided by the user. */
3574 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3575 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3576 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3578 return expand_call (call_expr, NULL_RTX, false);
3581 /* Expand a setmem pattern; return true if successful. */
3583 bool
3584 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3585 unsigned int expected_align, HOST_WIDE_INT expected_size,
3586 unsigned HOST_WIDE_INT min_size,
3587 unsigned HOST_WIDE_INT max_size,
3588 unsigned HOST_WIDE_INT probable_max_size)
3590 /* Try the most limited insn first, because there's no point
3591 including more than one in the machine description unless
3592 the more limited one has some advantage. */
3594 if (expected_align < align)
3595 expected_align = align;
3596 if (expected_size != -1)
3598 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3599 expected_size = max_size;
3600 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3601 expected_size = min_size;
3604 opt_scalar_int_mode mode_iter;
3605 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3607 scalar_int_mode mode = mode_iter.require ();
3608 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3610 if (code != CODE_FOR_nothing
3611 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3612 here because if SIZE is less than the mode mask, as it is
3613 returned by the macro, it will definitely be less than the
3614 actual mode mask. Since SIZE is within the Pmode address
3615 space, we limit MODE to Pmode. */
3616 && ((CONST_INT_P (size)
3617 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3618 <= (GET_MODE_MASK (mode) >> 1)))
3619 || max_size <= (GET_MODE_MASK (mode) >> 1)
3620 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3622 class expand_operand ops[9];
3623 unsigned int nops;
3625 nops = insn_data[(int) code].n_generator_args;
3626 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3628 create_fixed_operand (&ops[0], object);
3629 /* The check above guarantees that this size conversion is valid. */
3630 create_convert_operand_to (&ops[1], size, mode, true);
3631 create_convert_operand_from (&ops[2], val, byte_mode, true);
3632 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3633 if (nops >= 6)
3635 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3636 create_integer_operand (&ops[5], expected_size);
3638 if (nops >= 8)
3640 create_integer_operand (&ops[6], min_size);
3641 /* If we cannot represent the maximal size,
3642 make parameter NULL. */
3643 if ((HOST_WIDE_INT) max_size != -1)
3644 create_integer_operand (&ops[7], max_size);
3645 else
3646 create_fixed_operand (&ops[7], NULL);
3648 if (nops == 9)
3650 /* If we cannot represent the maximal size,
3651 make parameter NULL. */
3652 if ((HOST_WIDE_INT) probable_max_size != -1)
3653 create_integer_operand (&ops[8], probable_max_size);
3654 else
3655 create_fixed_operand (&ops[8], NULL);
3657 if (maybe_expand_insn (code, nops, ops))
3658 return true;
3662 return false;
3666 /* Write to one of the components of the complex value CPLX. Write VAL to
3667 the real part if IMAG_P is false, and the imaginary part if its true.
3668 If UNDEFINED_P then the value in CPLX is currently undefined. */
3670 void
3671 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
3673 machine_mode cmode;
3674 scalar_mode imode;
3675 unsigned ibitsize;
3677 if (GET_CODE (cplx) == CONCAT)
3679 emit_move_insn (XEXP (cplx, imag_p), val);
3680 return;
3683 cmode = GET_MODE (cplx);
3684 imode = GET_MODE_INNER (cmode);
3685 ibitsize = GET_MODE_BITSIZE (imode);
3687 /* For MEMs simplify_gen_subreg may generate an invalid new address
3688 because, e.g., the original address is considered mode-dependent
3689 by the target, which restricts simplify_subreg from invoking
3690 adjust_address_nv. Instead of preparing fallback support for an
3691 invalid address, we call adjust_address_nv directly. */
3692 if (MEM_P (cplx))
3694 emit_move_insn (adjust_address_nv (cplx, imode,
3695 imag_p ? GET_MODE_SIZE (imode) : 0),
3696 val);
3697 return;
3700 /* If the sub-object is at least word sized, then we know that subregging
3701 will work. This special case is important, since store_bit_field
3702 wants to operate on integer modes, and there's rarely an OImode to
3703 correspond to TCmode. */
3704 if (ibitsize >= BITS_PER_WORD
3705 /* For hard regs we have exact predicates. Assume we can split
3706 the original object if it spans an even number of hard regs.
3707 This special case is important for SCmode on 64-bit platforms
3708 where the natural size of floating-point regs is 32-bit. */
3709 || (REG_P (cplx)
3710 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3711 && REG_NREGS (cplx) % 2 == 0))
3713 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3714 imag_p ? GET_MODE_SIZE (imode) : 0);
3715 if (part)
3717 emit_move_insn (part, val);
3718 return;
3720 else
3721 /* simplify_gen_subreg may fail for sub-word MEMs. */
3722 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3725 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3726 false, undefined_p);
3729 /* Extract one of the components of the complex value CPLX. Extract the
3730 real part if IMAG_P is false, and the imaginary part if it's true. */
3733 read_complex_part (rtx cplx, bool imag_p)
3735 machine_mode cmode;
3736 scalar_mode imode;
3737 unsigned ibitsize;
3739 if (GET_CODE (cplx) == CONCAT)
3740 return XEXP (cplx, imag_p);
3742 cmode = GET_MODE (cplx);
3743 imode = GET_MODE_INNER (cmode);
3744 ibitsize = GET_MODE_BITSIZE (imode);
3746 /* Special case reads from complex constants that got spilled to memory. */
3747 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3749 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3750 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3752 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3753 if (CONSTANT_CLASS_P (part))
3754 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3758 /* For MEMs simplify_gen_subreg may generate an invalid new address
3759 because, e.g., the original address is considered mode-dependent
3760 by the target, which restricts simplify_subreg from invoking
3761 adjust_address_nv. Instead of preparing fallback support for an
3762 invalid address, we call adjust_address_nv directly. */
3763 if (MEM_P (cplx))
3764 return adjust_address_nv (cplx, imode,
3765 imag_p ? GET_MODE_SIZE (imode) : 0);
3767 /* If the sub-object is at least word sized, then we know that subregging
3768 will work. This special case is important, since extract_bit_field
3769 wants to operate on integer modes, and there's rarely an OImode to
3770 correspond to TCmode. */
3771 if (ibitsize >= BITS_PER_WORD
3772 /* For hard regs we have exact predicates. Assume we can split
3773 the original object if it spans an even number of hard regs.
3774 This special case is important for SCmode on 64-bit platforms
3775 where the natural size of floating-point regs is 32-bit. */
3776 || (REG_P (cplx)
3777 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3778 && REG_NREGS (cplx) % 2 == 0))
3780 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3781 imag_p ? GET_MODE_SIZE (imode) : 0);
3782 if (ret)
3783 return ret;
3784 else
3785 /* simplify_gen_subreg may fail for sub-word MEMs. */
3786 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3789 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3790 true, NULL_RTX, imode, imode, false, NULL);
3793 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3794 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3795 represented in NEW_MODE. If FORCE is true, this will never happen, as
3796 we'll force-create a SUBREG if needed. */
3798 static rtx
3799 emit_move_change_mode (machine_mode new_mode,
3800 machine_mode old_mode, rtx x, bool force)
3802 rtx ret;
3804 if (push_operand (x, GET_MODE (x)))
3806 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3807 MEM_COPY_ATTRIBUTES (ret, x);
3809 else if (MEM_P (x))
3811 /* We don't have to worry about changing the address since the
3812 size in bytes is supposed to be the same. */
3813 if (reload_in_progress)
3815 /* Copy the MEM to change the mode and move any
3816 substitutions from the old MEM to the new one. */
3817 ret = adjust_address_nv (x, new_mode, 0);
3818 copy_replacements (x, ret);
3820 else
3821 ret = adjust_address (x, new_mode, 0);
3823 else
3825 /* Note that we do want simplify_subreg's behavior of validating
3826 that the new mode is ok for a hard register. If we were to use
3827 simplify_gen_subreg, we would create the subreg, but would
3828 probably run into the target not being able to implement it. */
3829 /* Except, of course, when FORCE is true, when this is exactly what
3830 we want. Which is needed for CCmodes on some targets. */
3831 if (force)
3832 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3833 else
3834 ret = simplify_subreg (new_mode, x, old_mode, 0);
3837 return ret;
3840 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3841 an integer mode of the same size as MODE. Returns the instruction
3842 emitted, or NULL if such a move could not be generated. */
3844 static rtx_insn *
3845 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3847 scalar_int_mode imode;
3848 enum insn_code code;
3850 /* There must exist a mode of the exact size we require. */
3851 if (!int_mode_for_mode (mode).exists (&imode))
3852 return NULL;
3854 /* The target must support moves in this mode. */
3855 code = optab_handler (mov_optab, imode);
3856 if (code == CODE_FOR_nothing)
3857 return NULL;
3859 x = emit_move_change_mode (imode, mode, x, force);
3860 if (x == NULL_RTX)
3861 return NULL;
3862 y = emit_move_change_mode (imode, mode, y, force);
3863 if (y == NULL_RTX)
3864 return NULL;
3865 return emit_insn (GEN_FCN (code) (x, y));
3868 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3869 Return an equivalent MEM that does not use an auto-increment. */
3872 emit_move_resolve_push (machine_mode mode, rtx x)
3874 enum rtx_code code = GET_CODE (XEXP (x, 0));
3875 rtx temp;
3877 poly_int64 adjust = GET_MODE_SIZE (mode);
3878 #ifdef PUSH_ROUNDING
3879 adjust = PUSH_ROUNDING (adjust);
3880 #endif
3881 if (code == PRE_DEC || code == POST_DEC)
3882 adjust = -adjust;
3883 else if (code == PRE_MODIFY || code == POST_MODIFY)
3885 rtx expr = XEXP (XEXP (x, 0), 1);
3887 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3888 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3889 if (GET_CODE (expr) == MINUS)
3890 val = -val;
3891 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3892 adjust = val;
3895 /* Do not use anti_adjust_stack, since we don't want to update
3896 stack_pointer_delta. */
3897 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3898 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3899 0, OPTAB_LIB_WIDEN);
3900 if (temp != stack_pointer_rtx)
3901 emit_move_insn (stack_pointer_rtx, temp);
3903 switch (code)
3905 case PRE_INC:
3906 case PRE_DEC:
3907 case PRE_MODIFY:
3908 temp = stack_pointer_rtx;
3909 break;
3910 case POST_INC:
3911 case POST_DEC:
3912 case POST_MODIFY:
3913 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3914 break;
3915 default:
3916 gcc_unreachable ();
3919 return replace_equiv_address (x, temp);
3922 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3923 X is known to satisfy push_operand, and MODE is known to be complex.
3924 Returns the last instruction emitted. */
3926 rtx_insn *
3927 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3929 scalar_mode submode = GET_MODE_INNER (mode);
3930 bool imag_first;
3932 #ifdef PUSH_ROUNDING
3933 poly_int64 submodesize = GET_MODE_SIZE (submode);
3935 /* In case we output to the stack, but the size is smaller than the
3936 machine can push exactly, we need to use move instructions. */
3937 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3939 x = emit_move_resolve_push (mode, x);
3940 return emit_move_insn (x, y);
3942 #endif
3944 /* Note that the real part always precedes the imag part in memory
3945 regardless of machine's endianness. */
3946 switch (GET_CODE (XEXP (x, 0)))
3948 case PRE_DEC:
3949 case POST_DEC:
3950 imag_first = true;
3951 break;
3952 case PRE_INC:
3953 case POST_INC:
3954 imag_first = false;
3955 break;
3956 default:
3957 gcc_unreachable ();
3960 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3961 read_complex_part (y, imag_first));
3962 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3963 read_complex_part (y, !imag_first));
3966 /* A subroutine of emit_move_complex. Perform the move from Y to X
3967 via two moves of the parts. Returns the last instruction emitted. */
3969 rtx_insn *
3970 emit_move_complex_parts (rtx x, rtx y)
3972 /* Show the output dies here. This is necessary for SUBREGs
3973 of pseudos since we cannot track their lifetimes correctly;
3974 hard regs shouldn't appear here except as return values. */
3975 if (!reload_completed && !reload_in_progress
3976 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3977 emit_clobber (x);
3979 write_complex_part (x, read_complex_part (y, false), false, true);
3980 write_complex_part (x, read_complex_part (y, true), true, false);
3982 return get_last_insn ();
3985 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3986 MODE is known to be complex. Returns the last instruction emitted. */
3988 static rtx_insn *
3989 emit_move_complex (machine_mode mode, rtx x, rtx y)
3991 bool try_int;
3993 /* Need to take special care for pushes, to maintain proper ordering
3994 of the data, and possibly extra padding. */
3995 if (push_operand (x, mode))
3996 return emit_move_complex_push (mode, x, y);
3998 /* See if we can coerce the target into moving both values at once, except
3999 for floating point where we favor moving as parts if this is easy. */
4000 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4001 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
4002 && !(REG_P (x)
4003 && HARD_REGISTER_P (x)
4004 && REG_NREGS (x) == 1)
4005 && !(REG_P (y)
4006 && HARD_REGISTER_P (y)
4007 && REG_NREGS (y) == 1))
4008 try_int = false;
4009 /* Not possible if the values are inherently not adjacent. */
4010 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
4011 try_int = false;
4012 /* Is possible if both are registers (or subregs of registers). */
4013 else if (register_operand (x, mode) && register_operand (y, mode))
4014 try_int = true;
4015 /* If one of the operands is a memory, and alignment constraints
4016 are friendly enough, we may be able to do combined memory operations.
4017 We do not attempt this if Y is a constant because that combination is
4018 usually better with the by-parts thing below. */
4019 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4020 && (!STRICT_ALIGNMENT
4021 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4022 try_int = true;
4023 else
4024 try_int = false;
4026 if (try_int)
4028 rtx_insn *ret;
4030 /* For memory to memory moves, optimal behavior can be had with the
4031 existing block move logic. But use normal expansion if optimizing
4032 for size. */
4033 if (MEM_P (x) && MEM_P (y))
4035 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4036 (optimize_insn_for_speed_p()
4037 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4038 return get_last_insn ();
4041 ret = emit_move_via_integer (mode, x, y, true);
4042 if (ret)
4043 return ret;
4046 return emit_move_complex_parts (x, y);
4049 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4050 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4052 static rtx_insn *
4053 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4055 rtx_insn *ret;
4057 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4058 if (mode != CCmode)
4060 enum insn_code code = optab_handler (mov_optab, CCmode);
4061 if (code != CODE_FOR_nothing)
4063 x = emit_move_change_mode (CCmode, mode, x, true);
4064 y = emit_move_change_mode (CCmode, mode, y, true);
4065 return emit_insn (GEN_FCN (code) (x, y));
4069 /* Otherwise, find the MODE_INT mode of the same width. */
4070 ret = emit_move_via_integer (mode, x, y, false);
4071 gcc_assert (ret != NULL);
4072 return ret;
4075 /* Return true if word I of OP lies entirely in the
4076 undefined bits of a paradoxical subreg. */
4078 static bool
4079 undefined_operand_subword_p (const_rtx op, int i)
4081 if (GET_CODE (op) != SUBREG)
4082 return false;
4083 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4084 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4085 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4086 || known_le (offset, -UNITS_PER_WORD));
4089 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4090 MODE is any multi-word or full-word mode that lacks a move_insn
4091 pattern. Note that you will get better code if you define such
4092 patterns, even if they must turn into multiple assembler instructions. */
4094 static rtx_insn *
4095 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4097 rtx_insn *last_insn = 0;
4098 rtx_insn *seq;
4099 rtx inner;
4100 bool need_clobber;
4101 int i, mode_size;
4103 /* This function can only handle cases where the number of words is
4104 known at compile time. */
4105 mode_size = GET_MODE_SIZE (mode).to_constant ();
4106 gcc_assert (mode_size >= UNITS_PER_WORD);
4108 /* If X is a push on the stack, do the push now and replace
4109 X with a reference to the stack pointer. */
4110 if (push_operand (x, mode))
4111 x = emit_move_resolve_push (mode, x);
4113 /* If we are in reload, see if either operand is a MEM whose address
4114 is scheduled for replacement. */
4115 if (reload_in_progress && MEM_P (x)
4116 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4117 x = replace_equiv_address_nv (x, inner);
4118 if (reload_in_progress && MEM_P (y)
4119 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4120 y = replace_equiv_address_nv (y, inner);
4122 start_sequence ();
4124 need_clobber = false;
4125 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4127 /* Do not generate code for a move if it would go entirely
4128 to the non-existing bits of a paradoxical subreg. */
4129 if (undefined_operand_subword_p (x, i))
4130 continue;
4132 rtx xpart = operand_subword (x, i, 1, mode);
4133 rtx ypart;
4135 /* Do not generate code for a move if it would come entirely
4136 from the undefined bits of a paradoxical subreg. */
4137 if (undefined_operand_subword_p (y, i))
4138 continue;
4140 ypart = operand_subword (y, i, 1, mode);
4142 /* If we can't get a part of Y, put Y into memory if it is a
4143 constant. Otherwise, force it into a register. Then we must
4144 be able to get a part of Y. */
4145 if (ypart == 0 && CONSTANT_P (y))
4147 y = use_anchored_address (force_const_mem (mode, y));
4148 ypart = operand_subword (y, i, 1, mode);
4150 else if (ypart == 0)
4151 ypart = operand_subword_force (y, i, mode);
4153 gcc_assert (xpart && ypart);
4155 need_clobber |= (GET_CODE (xpart) == SUBREG);
4157 last_insn = emit_move_insn (xpart, ypart);
4160 seq = get_insns ();
4161 end_sequence ();
4163 /* Show the output dies here. This is necessary for SUBREGs
4164 of pseudos since we cannot track their lifetimes correctly;
4165 hard regs shouldn't appear here except as return values.
4166 We never want to emit such a clobber after reload. */
4167 if (x != y
4168 && ! (reload_in_progress || reload_completed)
4169 && need_clobber != 0)
4170 emit_clobber (x);
4172 emit_insn (seq);
4174 return last_insn;
4177 /* Low level part of emit_move_insn.
4178 Called just like emit_move_insn, but assumes X and Y
4179 are basically valid. */
4181 rtx_insn *
4182 emit_move_insn_1 (rtx x, rtx y)
4184 machine_mode mode = GET_MODE (x);
4185 enum insn_code code;
4187 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4189 code = optab_handler (mov_optab, mode);
4190 if (code != CODE_FOR_nothing)
4191 return emit_insn (GEN_FCN (code) (x, y));
4193 /* Expand complex moves by moving real part and imag part. */
4194 if (COMPLEX_MODE_P (mode))
4195 return emit_move_complex (mode, x, y);
4197 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4198 || ALL_FIXED_POINT_MODE_P (mode))
4200 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4202 /* If we can't find an integer mode, use multi words. */
4203 if (result)
4204 return result;
4205 else
4206 return emit_move_multi_word (mode, x, y);
4209 if (GET_MODE_CLASS (mode) == MODE_CC)
4210 return emit_move_ccmode (mode, x, y);
4212 /* Try using a move pattern for the corresponding integer mode. This is
4213 only safe when simplify_subreg can convert MODE constants into integer
4214 constants. At present, it can only do this reliably if the value
4215 fits within a HOST_WIDE_INT. */
4216 if (!CONSTANT_P (y)
4217 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4219 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4221 if (ret)
4223 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4224 return ret;
4228 return emit_move_multi_word (mode, x, y);
4231 /* Generate code to copy Y into X.
4232 Both Y and X must have the same mode, except that
4233 Y can be a constant with VOIDmode.
4234 This mode cannot be BLKmode; use emit_block_move for that.
4236 Return the last instruction emitted. */
4238 rtx_insn *
4239 emit_move_insn (rtx x, rtx y)
4241 machine_mode mode = GET_MODE (x);
4242 rtx y_cst = NULL_RTX;
4243 rtx_insn *last_insn;
4244 rtx set;
4246 gcc_assert (mode != BLKmode
4247 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4249 /* If we have a copy that looks like one of the following patterns:
4250 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4251 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4252 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4253 (set (subreg:M1 (reg:M2 ...)) (constant C))
4254 where mode M1 is equal in size to M2, try to detect whether the
4255 mode change involves an implicit round trip through memory.
4256 If so, see if we can avoid that by removing the subregs and
4257 doing the move in mode M2 instead. */
4259 rtx x_inner = NULL_RTX;
4260 rtx y_inner = NULL_RTX;
4262 auto candidate_subreg_p = [&](rtx subreg) {
4263 return (REG_P (SUBREG_REG (subreg))
4264 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4265 GET_MODE_SIZE (GET_MODE (subreg)))
4266 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4267 != CODE_FOR_nothing);
4270 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4271 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4272 && !push_operand (mem, GET_MODE (mem))
4273 /* Not a candiate if innermode requires too much alignment. */
4274 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4275 || targetm.slow_unaligned_access (GET_MODE (mem),
4276 MEM_ALIGN (mem))
4277 || !targetm.slow_unaligned_access (innermode,
4278 MEM_ALIGN (mem))));
4281 if (SUBREG_P (x) && candidate_subreg_p (x))
4282 x_inner = SUBREG_REG (x);
4284 if (SUBREG_P (y) && candidate_subreg_p (y))
4285 y_inner = SUBREG_REG (y);
4287 if (x_inner != NULL_RTX
4288 && y_inner != NULL_RTX
4289 && GET_MODE (x_inner) == GET_MODE (y_inner)
4290 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4292 x = x_inner;
4293 y = y_inner;
4294 mode = GET_MODE (x_inner);
4296 else if (x_inner != NULL_RTX
4297 && MEM_P (y)
4298 && candidate_mem_p (GET_MODE (x_inner), y))
4300 x = x_inner;
4301 y = adjust_address (y, GET_MODE (x_inner), 0);
4302 mode = GET_MODE (x_inner);
4304 else if (y_inner != NULL_RTX
4305 && MEM_P (x)
4306 && candidate_mem_p (GET_MODE (y_inner), x))
4308 x = adjust_address (x, GET_MODE (y_inner), 0);
4309 y = y_inner;
4310 mode = GET_MODE (y_inner);
4312 else if (x_inner != NULL_RTX
4313 && CONSTANT_P (y)
4314 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4315 mode, ALL_REGS)
4316 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4318 x = x_inner;
4319 y = y_inner;
4320 mode = GET_MODE (x_inner);
4323 if (CONSTANT_P (y))
4325 if (optimize
4326 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4327 && (last_insn = compress_float_constant (x, y)))
4328 return last_insn;
4330 y_cst = y;
4332 if (!targetm.legitimate_constant_p (mode, y))
4334 y = force_const_mem (mode, y);
4336 /* If the target's cannot_force_const_mem prevented the spill,
4337 assume that the target's move expanders will also take care
4338 of the non-legitimate constant. */
4339 if (!y)
4340 y = y_cst;
4341 else
4342 y = use_anchored_address (y);
4346 /* If X or Y are memory references, verify that their addresses are valid
4347 for the machine. */
4348 if (MEM_P (x)
4349 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4350 MEM_ADDR_SPACE (x))
4351 && ! push_operand (x, GET_MODE (x))))
4352 x = validize_mem (x);
4354 if (MEM_P (y)
4355 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4356 MEM_ADDR_SPACE (y)))
4357 y = validize_mem (y);
4359 gcc_assert (mode != BLKmode);
4361 last_insn = emit_move_insn_1 (x, y);
4363 if (y_cst && REG_P (x)
4364 && (set = single_set (last_insn)) != NULL_RTX
4365 && SET_DEST (set) == x
4366 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4367 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4369 return last_insn;
4372 /* Generate the body of an instruction to copy Y into X.
4373 It may be a list of insns, if one insn isn't enough. */
4375 rtx_insn *
4376 gen_move_insn (rtx x, rtx y)
4378 rtx_insn *seq;
4380 start_sequence ();
4381 emit_move_insn_1 (x, y);
4382 seq = get_insns ();
4383 end_sequence ();
4384 return seq;
4387 /* If Y is representable exactly in a narrower mode, and the target can
4388 perform the extension directly from constant or memory, then emit the
4389 move as an extension. */
4391 static rtx_insn *
4392 compress_float_constant (rtx x, rtx y)
4394 machine_mode dstmode = GET_MODE (x);
4395 machine_mode orig_srcmode = GET_MODE (y);
4396 machine_mode srcmode;
4397 const REAL_VALUE_TYPE *r;
4398 int oldcost, newcost;
4399 bool speed = optimize_insn_for_speed_p ();
4401 r = CONST_DOUBLE_REAL_VALUE (y);
4403 if (targetm.legitimate_constant_p (dstmode, y))
4404 oldcost = set_src_cost (y, orig_srcmode, speed);
4405 else
4406 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4408 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4410 enum insn_code ic;
4411 rtx trunc_y;
4412 rtx_insn *last_insn;
4414 /* Skip if the target can't extend this way. */
4415 ic = can_extend_p (dstmode, srcmode, 0);
4416 if (ic == CODE_FOR_nothing)
4417 continue;
4419 /* Skip if the narrowed value isn't exact. */
4420 if (! exact_real_truncate (srcmode, r))
4421 continue;
4423 trunc_y = const_double_from_real_value (*r, srcmode);
4425 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4427 /* Skip if the target needs extra instructions to perform
4428 the extension. */
4429 if (!insn_operand_matches (ic, 1, trunc_y))
4430 continue;
4431 /* This is valid, but may not be cheaper than the original. */
4432 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4433 dstmode, speed);
4434 if (oldcost < newcost)
4435 continue;
4437 else if (float_extend_from_mem[dstmode][srcmode])
4439 trunc_y = force_const_mem (srcmode, trunc_y);
4440 /* This is valid, but may not be cheaper than the original. */
4441 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4442 dstmode, speed);
4443 if (oldcost < newcost)
4444 continue;
4445 trunc_y = validize_mem (trunc_y);
4447 else
4448 continue;
4450 /* For CSE's benefit, force the compressed constant pool entry
4451 into a new pseudo. This constant may be used in different modes,
4452 and if not, combine will put things back together for us. */
4453 trunc_y = force_reg (srcmode, trunc_y);
4455 /* If x is a hard register, perform the extension into a pseudo,
4456 so that e.g. stack realignment code is aware of it. */
4457 rtx target = x;
4458 if (REG_P (x) && HARD_REGISTER_P (x))
4459 target = gen_reg_rtx (dstmode);
4461 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4462 last_insn = get_last_insn ();
4464 if (REG_P (target))
4465 set_unique_reg_note (last_insn, REG_EQUAL, y);
4467 if (target != x)
4468 return emit_move_insn (x, target);
4469 return last_insn;
4472 return NULL;
4475 /* Pushing data onto the stack. */
4477 /* Push a block of length SIZE (perhaps variable)
4478 and return an rtx to address the beginning of the block.
4479 The value may be virtual_outgoing_args_rtx.
4481 EXTRA is the number of bytes of padding to push in addition to SIZE.
4482 BELOW nonzero means this padding comes at low addresses;
4483 otherwise, the padding comes at high addresses. */
4486 push_block (rtx size, poly_int64 extra, int below)
4488 rtx temp;
4490 size = convert_modes (Pmode, ptr_mode, size, 1);
4491 if (CONSTANT_P (size))
4492 anti_adjust_stack (plus_constant (Pmode, size, extra));
4493 else if (REG_P (size) && known_eq (extra, 0))
4494 anti_adjust_stack (size);
4495 else
4497 temp = copy_to_mode_reg (Pmode, size);
4498 if (maybe_ne (extra, 0))
4499 temp = expand_binop (Pmode, add_optab, temp,
4500 gen_int_mode (extra, Pmode),
4501 temp, 0, OPTAB_LIB_WIDEN);
4502 anti_adjust_stack (temp);
4505 if (STACK_GROWS_DOWNWARD)
4507 temp = virtual_outgoing_args_rtx;
4508 if (maybe_ne (extra, 0) && below)
4509 temp = plus_constant (Pmode, temp, extra);
4511 else
4513 poly_int64 csize;
4514 if (poly_int_rtx_p (size, &csize))
4515 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4516 -csize - (below ? 0 : extra));
4517 else if (maybe_ne (extra, 0) && !below)
4518 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4519 negate_rtx (Pmode, plus_constant (Pmode, size,
4520 extra)));
4521 else
4522 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4523 negate_rtx (Pmode, size));
4526 return memory_address (NARROWEST_INT_MODE, temp);
4529 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4531 static rtx
4532 mem_autoinc_base (rtx mem)
4534 if (MEM_P (mem))
4536 rtx addr = XEXP (mem, 0);
4537 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4538 return XEXP (addr, 0);
4540 return NULL;
4543 /* A utility routine used here, in reload, and in try_split. The insns
4544 after PREV up to and including LAST are known to adjust the stack,
4545 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4546 placing notes as appropriate. PREV may be NULL, indicating the
4547 entire insn sequence prior to LAST should be scanned.
4549 The set of allowed stack pointer modifications is small:
4550 (1) One or more auto-inc style memory references (aka pushes),
4551 (2) One or more addition/subtraction with the SP as destination,
4552 (3) A single move insn with the SP as destination,
4553 (4) A call_pop insn,
4554 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4556 Insns in the sequence that do not modify the SP are ignored,
4557 except for noreturn calls.
4559 The return value is the amount of adjustment that can be trivially
4560 verified, via immediate operand or auto-inc. If the adjustment
4561 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4563 poly_int64
4564 find_args_size_adjust (rtx_insn *insn)
4566 rtx dest, set, pat;
4567 int i;
4569 pat = PATTERN (insn);
4570 set = NULL;
4572 /* Look for a call_pop pattern. */
4573 if (CALL_P (insn))
4575 /* We have to allow non-call_pop patterns for the case
4576 of emit_single_push_insn of a TLS address. */
4577 if (GET_CODE (pat) != PARALLEL)
4578 return 0;
4580 /* All call_pop have a stack pointer adjust in the parallel.
4581 The call itself is always first, and the stack adjust is
4582 usually last, so search from the end. */
4583 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4585 set = XVECEXP (pat, 0, i);
4586 if (GET_CODE (set) != SET)
4587 continue;
4588 dest = SET_DEST (set);
4589 if (dest == stack_pointer_rtx)
4590 break;
4592 /* We'd better have found the stack pointer adjust. */
4593 if (i == 0)
4594 return 0;
4595 /* Fall through to process the extracted SET and DEST
4596 as if it was a standalone insn. */
4598 else if (GET_CODE (pat) == SET)
4599 set = pat;
4600 else if ((set = single_set (insn)) != NULL)
4602 else if (GET_CODE (pat) == PARALLEL)
4604 /* ??? Some older ports use a parallel with a stack adjust
4605 and a store for a PUSH_ROUNDING pattern, rather than a
4606 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4607 /* ??? See h8300 and m68k, pushqi1. */
4608 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4610 set = XVECEXP (pat, 0, i);
4611 if (GET_CODE (set) != SET)
4612 continue;
4613 dest = SET_DEST (set);
4614 if (dest == stack_pointer_rtx)
4615 break;
4617 /* We do not expect an auto-inc of the sp in the parallel. */
4618 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4619 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4620 != stack_pointer_rtx);
4622 if (i < 0)
4623 return 0;
4625 else
4626 return 0;
4628 dest = SET_DEST (set);
4630 /* Look for direct modifications of the stack pointer. */
4631 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4633 /* Look for a trivial adjustment, otherwise assume nothing. */
4634 /* Note that the SPU restore_stack_block pattern refers to
4635 the stack pointer in V4SImode. Consider that non-trivial. */
4636 poly_int64 offset;
4637 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4638 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4639 return offset;
4640 /* ??? Reload can generate no-op moves, which will be cleaned
4641 up later. Recognize it and continue searching. */
4642 else if (rtx_equal_p (dest, SET_SRC (set)))
4643 return 0;
4644 else
4645 return HOST_WIDE_INT_MIN;
4647 else
4649 rtx mem, addr;
4651 /* Otherwise only think about autoinc patterns. */
4652 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4654 mem = dest;
4655 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4656 != stack_pointer_rtx);
4658 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4659 mem = SET_SRC (set);
4660 else
4661 return 0;
4663 addr = XEXP (mem, 0);
4664 switch (GET_CODE (addr))
4666 case PRE_INC:
4667 case POST_INC:
4668 return GET_MODE_SIZE (GET_MODE (mem));
4669 case PRE_DEC:
4670 case POST_DEC:
4671 return -GET_MODE_SIZE (GET_MODE (mem));
4672 case PRE_MODIFY:
4673 case POST_MODIFY:
4674 addr = XEXP (addr, 1);
4675 gcc_assert (GET_CODE (addr) == PLUS);
4676 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4677 return rtx_to_poly_int64 (XEXP (addr, 1));
4678 default:
4679 gcc_unreachable ();
4684 poly_int64
4685 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4686 poly_int64 end_args_size)
4688 poly_int64 args_size = end_args_size;
4689 bool saw_unknown = false;
4690 rtx_insn *insn;
4692 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4694 if (!NONDEBUG_INSN_P (insn))
4695 continue;
4697 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4698 a call argument containing a TLS address that itself requires
4699 a call to __tls_get_addr. The handling of stack_pointer_delta
4700 in emit_single_push_insn is supposed to ensure that any such
4701 notes are already correct. */
4702 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4703 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4705 poly_int64 this_delta = find_args_size_adjust (insn);
4706 if (known_eq (this_delta, 0))
4708 if (!CALL_P (insn)
4709 || ACCUMULATE_OUTGOING_ARGS
4710 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4711 continue;
4714 gcc_assert (!saw_unknown);
4715 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4716 saw_unknown = true;
4718 if (!note)
4719 add_args_size_note (insn, args_size);
4720 if (STACK_GROWS_DOWNWARD)
4721 this_delta = -poly_uint64 (this_delta);
4723 if (saw_unknown)
4724 args_size = HOST_WIDE_INT_MIN;
4725 else
4726 args_size -= this_delta;
4729 return args_size;
4732 #ifdef PUSH_ROUNDING
4733 /* Emit single push insn. */
4735 static void
4736 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4738 rtx dest_addr;
4739 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4740 rtx dest;
4741 enum insn_code icode;
4743 /* If there is push pattern, use it. Otherwise try old way of throwing
4744 MEM representing push operation to move expander. */
4745 icode = optab_handler (push_optab, mode);
4746 if (icode != CODE_FOR_nothing)
4748 class expand_operand ops[1];
4750 create_input_operand (&ops[0], x, mode);
4751 if (maybe_expand_insn (icode, 1, ops))
4752 return;
4754 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4755 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4756 /* If we are to pad downward, adjust the stack pointer first and
4757 then store X into the stack location using an offset. This is
4758 because emit_move_insn does not know how to pad; it does not have
4759 access to type. */
4760 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4762 emit_move_insn (stack_pointer_rtx,
4763 expand_binop (Pmode,
4764 STACK_GROWS_DOWNWARD ? sub_optab
4765 : add_optab,
4766 stack_pointer_rtx,
4767 gen_int_mode (rounded_size, Pmode),
4768 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4770 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4771 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4772 /* We have already decremented the stack pointer, so get the
4773 previous value. */
4774 offset += rounded_size;
4776 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4777 /* We have already incremented the stack pointer, so get the
4778 previous value. */
4779 offset -= rounded_size;
4781 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4783 else
4785 if (STACK_GROWS_DOWNWARD)
4786 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4787 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4788 else
4789 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4790 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4792 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4795 dest = gen_rtx_MEM (mode, dest_addr);
4797 if (type != 0)
4799 set_mem_attributes (dest, type, 1);
4801 if (cfun->tail_call_marked)
4802 /* Function incoming arguments may overlap with sibling call
4803 outgoing arguments and we cannot allow reordering of reads
4804 from function arguments with stores to outgoing arguments
4805 of sibling calls. */
4806 set_mem_alias_set (dest, 0);
4808 emit_move_insn (dest, x);
4811 /* Emit and annotate a single push insn. */
4813 static void
4814 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4816 poly_int64 delta, old_delta = stack_pointer_delta;
4817 rtx_insn *prev = get_last_insn ();
4818 rtx_insn *last;
4820 emit_single_push_insn_1 (mode, x, type);
4822 /* Adjust stack_pointer_delta to describe the situation after the push
4823 we just performed. Note that we must do this after the push rather
4824 than before the push in case calculating X needs pushes and pops of
4825 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4826 for such pushes and pops must not include the effect of the future
4827 push of X. */
4828 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4830 last = get_last_insn ();
4832 /* Notice the common case where we emitted exactly one insn. */
4833 if (PREV_INSN (last) == prev)
4835 add_args_size_note (last, stack_pointer_delta);
4836 return;
4839 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4840 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4841 || known_eq (delta, old_delta));
4843 #endif
4845 /* If reading SIZE bytes from X will end up reading from
4846 Y return the number of bytes that overlap. Return -1
4847 if there is no overlap or -2 if we can't determine
4848 (for example when X and Y have different base registers). */
4850 static int
4851 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4853 rtx tmp = plus_constant (Pmode, x, size);
4854 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4856 if (!CONST_INT_P (sub))
4857 return -2;
4859 HOST_WIDE_INT val = INTVAL (sub);
4861 return IN_RANGE (val, 1, size) ? val : -1;
4864 /* Generate code to push X onto the stack, assuming it has mode MODE and
4865 type TYPE.
4866 MODE is redundant except when X is a CONST_INT (since they don't
4867 carry mode info).
4868 SIZE is an rtx for the size of data to be copied (in bytes),
4869 needed only if X is BLKmode.
4870 Return true if successful. May return false if asked to push a
4871 partial argument during a sibcall optimization (as specified by
4872 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4873 to not overlap.
4875 ALIGN (in bits) is maximum alignment we can assume.
4877 If PARTIAL and REG are both nonzero, then copy that many of the first
4878 bytes of X into registers starting with REG, and push the rest of X.
4879 The amount of space pushed is decreased by PARTIAL bytes.
4880 REG must be a hard register in this case.
4881 If REG is zero but PARTIAL is not, take any all others actions for an
4882 argument partially in registers, but do not actually load any
4883 registers.
4885 EXTRA is the amount in bytes of extra space to leave next to this arg.
4886 This is ignored if an argument block has already been allocated.
4888 On a machine that lacks real push insns, ARGS_ADDR is the address of
4889 the bottom of the argument block for this call. We use indexing off there
4890 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4891 argument block has not been preallocated.
4893 ARGS_SO_FAR is the size of args previously pushed for this call.
4895 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4896 for arguments passed in registers. If nonzero, it will be the number
4897 of bytes required. */
4899 bool
4900 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4901 unsigned int align, int partial, rtx reg, poly_int64 extra,
4902 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4903 rtx alignment_pad, bool sibcall_p)
4905 rtx xinner;
4906 pad_direction stack_direction
4907 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4909 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4910 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4911 Default is below for small data on big-endian machines; else above. */
4912 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4914 /* Invert direction if stack is post-decrement.
4915 FIXME: why? */
4916 if (STACK_PUSH_CODE == POST_DEC)
4917 if (where_pad != PAD_NONE)
4918 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4920 xinner = x;
4922 int nregs = partial / UNITS_PER_WORD;
4923 rtx *tmp_regs = NULL;
4924 int overlapping = 0;
4926 if (mode == BLKmode
4927 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4929 /* Copy a block into the stack, entirely or partially. */
4931 rtx temp;
4932 int used;
4933 int offset;
4934 int skip;
4936 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4937 used = partial - offset;
4939 if (mode != BLKmode)
4941 /* A value is to be stored in an insufficiently aligned
4942 stack slot; copy via a suitably aligned slot if
4943 necessary. */
4944 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4945 if (!MEM_P (xinner))
4947 temp = assign_temp (type, 1, 1);
4948 emit_move_insn (temp, xinner);
4949 xinner = temp;
4953 gcc_assert (size);
4955 /* USED is now the # of bytes we need not copy to the stack
4956 because registers will take care of them. */
4958 if (partial != 0)
4959 xinner = adjust_address (xinner, BLKmode, used);
4961 /* If the partial register-part of the arg counts in its stack size,
4962 skip the part of stack space corresponding to the registers.
4963 Otherwise, start copying to the beginning of the stack space,
4964 by setting SKIP to 0. */
4965 skip = (reg_parm_stack_space == 0) ? 0 : used;
4967 #ifdef PUSH_ROUNDING
4968 /* NB: Let the backend known the number of bytes to push and
4969 decide if push insns should be generated. */
4970 unsigned int push_size;
4971 if (CONST_INT_P (size))
4972 push_size = INTVAL (size);
4973 else
4974 push_size = 0;
4976 /* Do it with several push insns if that doesn't take lots of insns
4977 and if there is no difficulty with push insns that skip bytes
4978 on the stack for alignment purposes. */
4979 if (args_addr == 0
4980 && targetm.calls.push_argument (push_size)
4981 && CONST_INT_P (size)
4982 && skip == 0
4983 && MEM_ALIGN (xinner) >= align
4984 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4985 /* Here we avoid the case of a structure whose weak alignment
4986 forces many pushes of a small amount of data,
4987 and such small pushes do rounding that causes trouble. */
4988 && ((!targetm.slow_unaligned_access (word_mode, align))
4989 || align >= BIGGEST_ALIGNMENT
4990 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4991 align / BITS_PER_UNIT))
4992 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4994 /* Push padding now if padding above and stack grows down,
4995 or if padding below and stack grows up.
4996 But if space already allocated, this has already been done. */
4997 if (maybe_ne (extra, 0)
4998 && args_addr == 0
4999 && where_pad != PAD_NONE
5000 && where_pad != stack_direction)
5001 anti_adjust_stack (gen_int_mode (extra, Pmode));
5003 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
5004 RETURN_BEGIN);
5006 else
5007 #endif /* PUSH_ROUNDING */
5009 rtx target;
5011 /* Otherwise make space on the stack and copy the data
5012 to the address of that space. */
5014 /* Deduct words put into registers from the size we must copy. */
5015 if (partial != 0)
5017 if (CONST_INT_P (size))
5018 size = GEN_INT (INTVAL (size) - used);
5019 else
5020 size = expand_binop (GET_MODE (size), sub_optab, size,
5021 gen_int_mode (used, GET_MODE (size)),
5022 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5025 /* Get the address of the stack space.
5026 In this case, we do not deal with EXTRA separately.
5027 A single stack adjust will do. */
5028 poly_int64 const_args_so_far;
5029 if (! args_addr)
5031 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5032 extra = 0;
5034 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5035 temp = memory_address (BLKmode,
5036 plus_constant (Pmode, args_addr,
5037 skip + const_args_so_far));
5038 else
5039 temp = memory_address (BLKmode,
5040 plus_constant (Pmode,
5041 gen_rtx_PLUS (Pmode,
5042 args_addr,
5043 args_so_far),
5044 skip));
5046 if (!ACCUMULATE_OUTGOING_ARGS)
5048 /* If the source is referenced relative to the stack pointer,
5049 copy it to another register to stabilize it. We do not need
5050 to do this if we know that we won't be changing sp. */
5052 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5053 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5054 temp = copy_to_reg (temp);
5057 target = gen_rtx_MEM (BLKmode, temp);
5059 /* We do *not* set_mem_attributes here, because incoming arguments
5060 may overlap with sibling call outgoing arguments and we cannot
5061 allow reordering of reads from function arguments with stores
5062 to outgoing arguments of sibling calls. We do, however, want
5063 to record the alignment of the stack slot. */
5064 /* ALIGN may well be better aligned than TYPE, e.g. due to
5065 PARM_BOUNDARY. Assume the caller isn't lying. */
5066 set_mem_align (target, align);
5068 /* If part should go in registers and pushing to that part would
5069 overwrite some of the values that need to go into regs, load the
5070 overlapping values into temporary pseudos to be moved into the hard
5071 regs at the end after the stack pushing has completed.
5072 We cannot load them directly into the hard regs here because
5073 they can be clobbered by the block move expansions.
5074 See PR 65358. */
5076 if (partial > 0 && reg != 0 && mode == BLKmode
5077 && GET_CODE (reg) != PARALLEL)
5079 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5080 if (overlapping > 0)
5082 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5083 overlapping /= UNITS_PER_WORD;
5085 tmp_regs = XALLOCAVEC (rtx, overlapping);
5087 for (int i = 0; i < overlapping; i++)
5088 tmp_regs[i] = gen_reg_rtx (word_mode);
5090 for (int i = 0; i < overlapping; i++)
5091 emit_move_insn (tmp_regs[i],
5092 operand_subword_force (target, i, mode));
5094 else if (overlapping == -1)
5095 overlapping = 0;
5096 /* Could not determine whether there is overlap.
5097 Fail the sibcall. */
5098 else
5100 overlapping = 0;
5101 if (sibcall_p)
5102 return false;
5106 /* If source is a constant VAR_DECL with a simple constructor,
5107 store the constructor to the stack instead of moving it. */
5108 const_tree decl;
5109 if (partial == 0
5110 && MEM_P (xinner)
5111 && SYMBOL_REF_P (XEXP (xinner, 0))
5112 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5113 && VAR_P (decl)
5114 && TREE_READONLY (decl)
5115 && !TREE_SIDE_EFFECTS (decl)
5116 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5117 store_constructor (DECL_INITIAL (decl), target, 0,
5118 int_expr_size (DECL_INITIAL (decl)), false);
5119 else
5120 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5123 else if (partial > 0)
5125 /* Scalar partly in registers. This case is only supported
5126 for fixed-wdth modes. */
5127 int num_words = GET_MODE_SIZE (mode).to_constant ();
5128 num_words /= UNITS_PER_WORD;
5129 int i;
5130 int not_stack;
5131 /* # bytes of start of argument
5132 that we must make space for but need not store. */
5133 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5134 int args_offset = INTVAL (args_so_far);
5135 int skip;
5137 /* Push padding now if padding above and stack grows down,
5138 or if padding below and stack grows up.
5139 But if space already allocated, this has already been done. */
5140 if (maybe_ne (extra, 0)
5141 && args_addr == 0
5142 && where_pad != PAD_NONE
5143 && where_pad != stack_direction)
5144 anti_adjust_stack (gen_int_mode (extra, Pmode));
5146 /* If we make space by pushing it, we might as well push
5147 the real data. Otherwise, we can leave OFFSET nonzero
5148 and leave the space uninitialized. */
5149 if (args_addr == 0)
5150 offset = 0;
5152 /* Now NOT_STACK gets the number of words that we don't need to
5153 allocate on the stack. Convert OFFSET to words too. */
5154 not_stack = (partial - offset) / UNITS_PER_WORD;
5155 offset /= UNITS_PER_WORD;
5157 /* If the partial register-part of the arg counts in its stack size,
5158 skip the part of stack space corresponding to the registers.
5159 Otherwise, start copying to the beginning of the stack space,
5160 by setting SKIP to 0. */
5161 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5163 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5164 x = validize_mem (force_const_mem (mode, x));
5166 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5167 SUBREGs of such registers are not allowed. */
5168 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5169 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5170 x = copy_to_reg (x);
5172 /* Loop over all the words allocated on the stack for this arg. */
5173 /* We can do it by words, because any scalar bigger than a word
5174 has a size a multiple of a word. */
5175 for (i = num_words - 1; i >= not_stack; i--)
5176 if (i >= not_stack + offset)
5177 if (!emit_push_insn (operand_subword_force (x, i, mode),
5178 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
5179 0, args_addr,
5180 GEN_INT (args_offset + ((i - not_stack + skip)
5181 * UNITS_PER_WORD)),
5182 reg_parm_stack_space, alignment_pad, sibcall_p))
5183 return false;
5185 else
5187 rtx addr;
5188 rtx dest;
5190 /* Push padding now if padding above and stack grows down,
5191 or if padding below and stack grows up.
5192 But if space already allocated, this has already been done. */
5193 if (maybe_ne (extra, 0)
5194 && args_addr == 0
5195 && where_pad != PAD_NONE
5196 && where_pad != stack_direction)
5197 anti_adjust_stack (gen_int_mode (extra, Pmode));
5199 #ifdef PUSH_ROUNDING
5200 if (args_addr == 0 && targetm.calls.push_argument (0))
5201 emit_single_push_insn (mode, x, type);
5202 else
5203 #endif
5205 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5206 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5208 /* We do *not* set_mem_attributes here, because incoming arguments
5209 may overlap with sibling call outgoing arguments and we cannot
5210 allow reordering of reads from function arguments with stores
5211 to outgoing arguments of sibling calls. We do, however, want
5212 to record the alignment of the stack slot. */
5213 /* ALIGN may well be better aligned than TYPE, e.g. due to
5214 PARM_BOUNDARY. Assume the caller isn't lying. */
5215 set_mem_align (dest, align);
5217 emit_move_insn (dest, x);
5221 /* Move the partial arguments into the registers and any overlapping
5222 values that we moved into the pseudos in tmp_regs. */
5223 if (partial > 0 && reg != 0)
5225 /* Handle calls that pass values in multiple non-contiguous locations.
5226 The Irix 6 ABI has examples of this. */
5227 if (GET_CODE (reg) == PARALLEL)
5228 emit_group_load (reg, x, type, -1);
5229 else
5231 gcc_assert (partial % UNITS_PER_WORD == 0);
5232 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5234 for (int i = 0; i < overlapping; i++)
5235 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5236 + nregs - overlapping + i),
5237 tmp_regs[i]);
5242 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5243 anti_adjust_stack (gen_int_mode (extra, Pmode));
5245 if (alignment_pad && args_addr == 0)
5246 anti_adjust_stack (alignment_pad);
5248 return true;
5251 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5252 operations. */
5254 static rtx
5255 get_subtarget (rtx x)
5257 return (optimize
5258 || x == 0
5259 /* Only registers can be subtargets. */
5260 || !REG_P (x)
5261 /* Don't use hard regs to avoid extending their life. */
5262 || REGNO (x) < FIRST_PSEUDO_REGISTER
5263 ? 0 : x);
5266 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5267 FIELD is a bitfield. Returns true if the optimization was successful,
5268 and there's nothing else to do. */
5270 static bool
5271 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5272 poly_uint64 pbitpos,
5273 poly_uint64 pbitregion_start,
5274 poly_uint64 pbitregion_end,
5275 machine_mode mode1, rtx str_rtx,
5276 tree to, tree src, bool reverse)
5278 /* str_mode is not guaranteed to be a scalar type. */
5279 machine_mode str_mode = GET_MODE (str_rtx);
5280 unsigned int str_bitsize;
5281 tree op0, op1;
5282 rtx value, result;
5283 optab binop;
5284 gimple *srcstmt;
5285 enum tree_code code;
5287 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5288 if (mode1 != VOIDmode
5289 || !pbitsize.is_constant (&bitsize)
5290 || !pbitpos.is_constant (&bitpos)
5291 || !pbitregion_start.is_constant (&bitregion_start)
5292 || !pbitregion_end.is_constant (&bitregion_end)
5293 || bitsize >= BITS_PER_WORD
5294 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5295 || str_bitsize > BITS_PER_WORD
5296 || TREE_SIDE_EFFECTS (to)
5297 || TREE_THIS_VOLATILE (to))
5298 return false;
5300 STRIP_NOPS (src);
5301 if (TREE_CODE (src) != SSA_NAME)
5302 return false;
5303 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5304 return false;
5306 srcstmt = get_gimple_for_ssa_name (src);
5307 if (!srcstmt
5308 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5309 return false;
5311 code = gimple_assign_rhs_code (srcstmt);
5313 op0 = gimple_assign_rhs1 (srcstmt);
5315 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5316 to find its initialization. Hopefully the initialization will
5317 be from a bitfield load. */
5318 if (TREE_CODE (op0) == SSA_NAME)
5320 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5322 /* We want to eventually have OP0 be the same as TO, which
5323 should be a bitfield. */
5324 if (!op0stmt
5325 || !is_gimple_assign (op0stmt)
5326 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5327 return false;
5328 op0 = gimple_assign_rhs1 (op0stmt);
5331 op1 = gimple_assign_rhs2 (srcstmt);
5333 if (!operand_equal_p (to, op0, 0))
5334 return false;
5336 if (MEM_P (str_rtx))
5338 unsigned HOST_WIDE_INT offset1;
5340 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5341 str_bitsize = BITS_PER_WORD;
5343 scalar_int_mode best_mode;
5344 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5345 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5346 return false;
5347 str_mode = best_mode;
5348 str_bitsize = GET_MODE_BITSIZE (best_mode);
5350 offset1 = bitpos;
5351 bitpos %= str_bitsize;
5352 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5353 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5355 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5356 return false;
5358 /* If the bit field covers the whole REG/MEM, store_field
5359 will likely generate better code. */
5360 if (bitsize >= str_bitsize)
5361 return false;
5363 /* We can't handle fields split across multiple entities. */
5364 if (bitpos + bitsize > str_bitsize)
5365 return false;
5367 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5368 bitpos = str_bitsize - bitpos - bitsize;
5370 switch (code)
5372 case PLUS_EXPR:
5373 case MINUS_EXPR:
5374 /* For now, just optimize the case of the topmost bitfield
5375 where we don't need to do any masking and also
5376 1 bit bitfields where xor can be used.
5377 We might win by one instruction for the other bitfields
5378 too if insv/extv instructions aren't used, so that
5379 can be added later. */
5380 if ((reverse || bitpos + bitsize != str_bitsize)
5381 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5382 break;
5384 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5385 value = convert_modes (str_mode,
5386 TYPE_MODE (TREE_TYPE (op1)), value,
5387 TYPE_UNSIGNED (TREE_TYPE (op1)));
5389 /* We may be accessing data outside the field, which means
5390 we can alias adjacent data. */
5391 if (MEM_P (str_rtx))
5393 str_rtx = shallow_copy_rtx (str_rtx);
5394 set_mem_alias_set (str_rtx, 0);
5395 set_mem_expr (str_rtx, 0);
5398 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5400 value = expand_and (str_mode, value, const1_rtx, NULL);
5401 binop = xor_optab;
5403 else
5404 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5406 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5407 if (reverse)
5408 value = flip_storage_order (str_mode, value);
5409 result = expand_binop (str_mode, binop, str_rtx,
5410 value, str_rtx, 1, OPTAB_WIDEN);
5411 if (result != str_rtx)
5412 emit_move_insn (str_rtx, result);
5413 return true;
5415 case BIT_IOR_EXPR:
5416 case BIT_XOR_EXPR:
5417 if (TREE_CODE (op1) != INTEGER_CST)
5418 break;
5419 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5420 value = convert_modes (str_mode,
5421 TYPE_MODE (TREE_TYPE (op1)), value,
5422 TYPE_UNSIGNED (TREE_TYPE (op1)));
5424 /* We may be accessing data outside the field, which means
5425 we can alias adjacent data. */
5426 if (MEM_P (str_rtx))
5428 str_rtx = shallow_copy_rtx (str_rtx);
5429 set_mem_alias_set (str_rtx, 0);
5430 set_mem_expr (str_rtx, 0);
5433 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5434 if (bitpos + bitsize != str_bitsize)
5436 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5437 str_mode);
5438 value = expand_and (str_mode, value, mask, NULL_RTX);
5440 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5441 if (reverse)
5442 value = flip_storage_order (str_mode, value);
5443 result = expand_binop (str_mode, binop, str_rtx,
5444 value, str_rtx, 1, OPTAB_WIDEN);
5445 if (result != str_rtx)
5446 emit_move_insn (str_rtx, result);
5447 return true;
5449 default:
5450 break;
5453 return false;
5456 /* In the C++ memory model, consecutive bit fields in a structure are
5457 considered one memory location.
5459 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5460 returns the bit range of consecutive bits in which this COMPONENT_REF
5461 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5462 and *OFFSET may be adjusted in the process.
5464 If the access does not need to be restricted, 0 is returned in both
5465 *BITSTART and *BITEND. */
5467 void
5468 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp,
5469 poly_int64 *bitpos, tree *offset)
5471 poly_int64 bitoffset;
5472 tree field, repr;
5474 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5476 field = TREE_OPERAND (exp, 1);
5477 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5478 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5479 need to limit the range we can access. */
5480 if (!repr)
5482 *bitstart = *bitend = 0;
5483 return;
5486 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5487 part of a larger bit field, then the representative does not serve any
5488 useful purpose. This can occur in Ada. */
5489 if (handled_component_p (TREE_OPERAND (exp, 0)))
5491 machine_mode rmode;
5492 poly_int64 rbitsize, rbitpos;
5493 tree roffset;
5494 int unsignedp, reversep, volatilep = 0;
5495 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5496 &roffset, &rmode, &unsignedp, &reversep,
5497 &volatilep);
5498 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5500 *bitstart = *bitend = 0;
5501 return;
5505 /* Compute the adjustment to bitpos from the offset of the field
5506 relative to the representative. DECL_FIELD_OFFSET of field and
5507 repr are the same by construction if they are not constants,
5508 see finish_bitfield_layout. */
5509 poly_uint64 field_offset, repr_offset;
5510 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5511 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5512 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5513 else
5514 bitoffset = 0;
5515 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5516 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5518 /* If the adjustment is larger than bitpos, we would have a negative bit
5519 position for the lower bound and this may wreak havoc later. Adjust
5520 offset and bitpos to make the lower bound non-negative in that case. */
5521 if (maybe_gt (bitoffset, *bitpos))
5523 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5524 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5526 *bitpos += adjust_bits;
5527 if (*offset == NULL_TREE)
5528 *offset = size_int (-adjust_bytes);
5529 else
5530 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5531 *bitstart = 0;
5533 else
5534 *bitstart = *bitpos - bitoffset;
5536 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5539 /* Returns true if BASE is a DECL that does not reside in memory and
5540 has non-BLKmode. DECL_RTL must not be a MEM; if
5541 DECL_RTL was not set yet, return false. */
5543 bool
5544 non_mem_decl_p (tree base)
5546 if (!DECL_P (base)
5547 || TREE_ADDRESSABLE (base)
5548 || DECL_MODE (base) == BLKmode)
5549 return false;
5551 if (!DECL_RTL_SET_P (base))
5552 return false;
5554 return (!MEM_P (DECL_RTL (base)));
5557 /* Returns true if REF refers to an object that does not
5558 reside in memory and has non-BLKmode. */
5560 bool
5561 mem_ref_refers_to_non_mem_p (tree ref)
5563 tree base;
5565 if (TREE_CODE (ref) == MEM_REF
5566 || TREE_CODE (ref) == TARGET_MEM_REF)
5568 tree addr = TREE_OPERAND (ref, 0);
5570 if (TREE_CODE (addr) != ADDR_EXPR)
5571 return false;
5573 base = TREE_OPERAND (addr, 0);
5575 else
5576 base = ref;
5578 return non_mem_decl_p (base);
5581 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5582 is true, try generating a nontemporal store. */
5584 void
5585 expand_assignment (tree to, tree from, bool nontemporal)
5587 rtx to_rtx = 0;
5588 rtx result;
5589 machine_mode mode;
5590 unsigned int align;
5591 enum insn_code icode;
5593 /* Don't crash if the lhs of the assignment was erroneous. */
5594 if (TREE_CODE (to) == ERROR_MARK)
5596 expand_normal (from);
5597 return;
5600 /* Optimize away no-op moves without side-effects. */
5601 if (operand_equal_p (to, from, 0))
5602 return;
5604 /* Handle misaligned stores. */
5605 mode = TYPE_MODE (TREE_TYPE (to));
5606 if ((TREE_CODE (to) == MEM_REF
5607 || TREE_CODE (to) == TARGET_MEM_REF
5608 || DECL_P (to))
5609 && mode != BLKmode
5610 && !mem_ref_refers_to_non_mem_p (to)
5611 && ((align = get_object_alignment (to))
5612 < GET_MODE_ALIGNMENT (mode))
5613 && (((icode = optab_handler (movmisalign_optab, mode))
5614 != CODE_FOR_nothing)
5615 || targetm.slow_unaligned_access (mode, align)))
5617 rtx reg, mem;
5619 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5620 /* Handle PARALLEL. */
5621 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5622 reg = force_not_mem (reg);
5623 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5624 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5625 reg = flip_storage_order (mode, reg);
5627 if (icode != CODE_FOR_nothing)
5629 class expand_operand ops[2];
5631 create_fixed_operand (&ops[0], mem);
5632 create_input_operand (&ops[1], reg, mode);
5633 /* The movmisalign<mode> pattern cannot fail, else the assignment
5634 would silently be omitted. */
5635 expand_insn (icode, 2, ops);
5637 else
5638 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5639 false, false);
5640 return;
5643 /* Assignment of a structure component needs special treatment
5644 if the structure component's rtx is not simply a MEM.
5645 Assignment of an array element at a constant index, and assignment of
5646 an array element in an unaligned packed structure field, has the same
5647 problem. Same for (partially) storing into a non-memory object. */
5648 if (handled_component_p (to)
5649 || (TREE_CODE (to) == MEM_REF
5650 && (REF_REVERSE_STORAGE_ORDER (to)
5651 || mem_ref_refers_to_non_mem_p (to)))
5652 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5654 machine_mode mode1;
5655 poly_int64 bitsize, bitpos;
5656 poly_uint64 bitregion_start = 0;
5657 poly_uint64 bitregion_end = 0;
5658 tree offset;
5659 int unsignedp, reversep, volatilep = 0;
5660 tree tem;
5662 push_temp_slots ();
5663 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5664 &unsignedp, &reversep, &volatilep);
5666 /* Make sure bitpos is not negative, it can wreak havoc later. */
5667 if (maybe_lt (bitpos, 0))
5669 gcc_assert (offset == NULL_TREE);
5670 offset = size_int (bits_to_bytes_round_down (bitpos));
5671 bitpos = num_trailing_bits (bitpos);
5674 if (TREE_CODE (to) == COMPONENT_REF
5675 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5676 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5677 /* The C++ memory model naturally applies to byte-aligned fields.
5678 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5679 BITSIZE are not byte-aligned, there is no need to limit the range
5680 we can access. This can occur with packed structures in Ada. */
5681 else if (maybe_gt (bitsize, 0)
5682 && multiple_p (bitsize, BITS_PER_UNIT)
5683 && multiple_p (bitpos, BITS_PER_UNIT))
5685 bitregion_start = bitpos;
5686 bitregion_end = bitpos + bitsize - 1;
5689 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5691 /* If the field has a mode, we want to access it in the
5692 field's mode, not the computed mode.
5693 If a MEM has VOIDmode (external with incomplete type),
5694 use BLKmode for it instead. */
5695 if (MEM_P (to_rtx))
5697 if (mode1 != VOIDmode)
5698 to_rtx = adjust_address (to_rtx, mode1, 0);
5699 else if (GET_MODE (to_rtx) == VOIDmode)
5700 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5703 if (offset != 0)
5705 machine_mode address_mode;
5706 rtx offset_rtx;
5708 if (!MEM_P (to_rtx))
5710 /* We can get constant negative offsets into arrays with broken
5711 user code. Translate this to a trap instead of ICEing. */
5712 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5713 expand_builtin_trap ();
5714 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5717 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5718 address_mode = get_address_mode (to_rtx);
5719 if (GET_MODE (offset_rtx) != address_mode)
5721 /* We cannot be sure that the RTL in offset_rtx is valid outside
5722 of a memory address context, so force it into a register
5723 before attempting to convert it to the desired mode. */
5724 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5725 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5728 /* If we have an expression in OFFSET_RTX and a non-zero
5729 byte offset in BITPOS, adding the byte offset before the
5730 OFFSET_RTX results in better intermediate code, which makes
5731 later rtl optimization passes perform better.
5733 We prefer intermediate code like this:
5735 r124:DI=r123:DI+0x18
5736 [r124:DI]=r121:DI
5738 ... instead of ...
5740 r124:DI=r123:DI+0x10
5741 [r124:DI+0x8]=r121:DI
5743 This is only done for aligned data values, as these can
5744 be expected to result in single move instructions. */
5745 poly_int64 bytepos;
5746 if (mode1 != VOIDmode
5747 && maybe_ne (bitpos, 0)
5748 && maybe_gt (bitsize, 0)
5749 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5750 && multiple_p (bitpos, bitsize)
5751 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5752 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5754 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5755 bitregion_start = 0;
5756 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5757 bitregion_end -= bitpos;
5758 bitpos = 0;
5761 to_rtx = offset_address (to_rtx, offset_rtx,
5762 highest_pow2_factor_for_target (to,
5763 offset));
5766 /* No action is needed if the target is not a memory and the field
5767 lies completely outside that target. This can occur if the source
5768 code contains an out-of-bounds access to a small array. */
5769 if (!MEM_P (to_rtx)
5770 && GET_MODE (to_rtx) != BLKmode
5771 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5773 expand_normal (from);
5774 result = NULL;
5776 /* Handle expand_expr of a complex value returning a CONCAT. */
5777 else if (GET_CODE (to_rtx) == CONCAT)
5779 machine_mode to_mode = GET_MODE (to_rtx);
5780 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5781 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5782 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5783 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5784 && known_eq (bitpos, 0)
5785 && known_eq (bitsize, mode_bitsize))
5786 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5787 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5788 && known_eq (bitsize, inner_bitsize)
5789 && (known_eq (bitpos, 0)
5790 || known_eq (bitpos, inner_bitsize)))
5791 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5792 false, nontemporal, reversep);
5793 else if (known_le (bitpos + bitsize, inner_bitsize))
5794 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5795 bitregion_start, bitregion_end,
5796 mode1, from, get_alias_set (to),
5797 nontemporal, reversep);
5798 else if (known_ge (bitpos, inner_bitsize))
5799 result = store_field (XEXP (to_rtx, 1), bitsize,
5800 bitpos - inner_bitsize,
5801 bitregion_start, bitregion_end,
5802 mode1, from, get_alias_set (to),
5803 nontemporal, reversep);
5804 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5806 result = expand_normal (from);
5807 if (GET_CODE (result) == CONCAT)
5809 to_mode = GET_MODE_INNER (to_mode);
5810 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5811 rtx from_real
5812 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5813 from_mode, 0);
5814 rtx from_imag
5815 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5816 from_mode, 0);
5817 if (!from_real || !from_imag)
5818 goto concat_store_slow;
5819 emit_move_insn (XEXP (to_rtx, 0), from_real);
5820 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5822 else
5824 machine_mode from_mode
5825 = GET_MODE (result) == VOIDmode
5826 ? TYPE_MODE (TREE_TYPE (from))
5827 : GET_MODE (result);
5828 rtx from_rtx;
5829 if (MEM_P (result))
5830 from_rtx = change_address (result, to_mode, NULL_RTX);
5831 else
5832 from_rtx
5833 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5834 if (from_rtx)
5836 emit_move_insn (XEXP (to_rtx, 0),
5837 read_complex_part (from_rtx, false));
5838 emit_move_insn (XEXP (to_rtx, 1),
5839 read_complex_part (from_rtx, true));
5841 else
5843 to_mode = GET_MODE_INNER (to_mode);
5844 rtx from_real
5845 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5846 rtx from_imag
5847 = simplify_gen_subreg (to_mode, result, from_mode,
5848 GET_MODE_SIZE (to_mode));
5849 if (!from_real || !from_imag)
5850 goto concat_store_slow;
5851 emit_move_insn (XEXP (to_rtx, 0), from_real);
5852 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5856 else
5858 concat_store_slow:;
5859 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5860 GET_MODE_SIZE (GET_MODE (to_rtx)));
5861 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
5862 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
5863 result = store_field (temp, bitsize, bitpos,
5864 bitregion_start, bitregion_end,
5865 mode1, from, get_alias_set (to),
5866 nontemporal, reversep);
5867 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5868 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5871 /* For calls to functions returning variable length structures, if TO_RTX
5872 is not a MEM, go through a MEM because we must not create temporaries
5873 of the VLA type. */
5874 else if (!MEM_P (to_rtx)
5875 && TREE_CODE (from) == CALL_EXPR
5876 && COMPLETE_TYPE_P (TREE_TYPE (from))
5877 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5879 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5880 GET_MODE_SIZE (GET_MODE (to_rtx)));
5881 result = store_field (temp, bitsize, bitpos, bitregion_start,
5882 bitregion_end, mode1, from, get_alias_set (to),
5883 nontemporal, reversep);
5884 emit_move_insn (to_rtx, temp);
5886 else
5888 if (MEM_P (to_rtx))
5890 /* If the field is at offset zero, we could have been given the
5891 DECL_RTX of the parent struct. Don't munge it. */
5892 to_rtx = shallow_copy_rtx (to_rtx);
5893 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5894 if (volatilep)
5895 MEM_VOLATILE_P (to_rtx) = 1;
5898 gcc_checking_assert (known_ge (bitpos, 0));
5899 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5900 bitregion_start, bitregion_end,
5901 mode1, to_rtx, to, from,
5902 reversep))
5903 result = NULL;
5904 else if (SUBREG_P (to_rtx)
5905 && SUBREG_PROMOTED_VAR_P (to_rtx))
5907 /* If to_rtx is a promoted subreg, we need to zero or sign
5908 extend the value afterwards. */
5909 if (TREE_CODE (to) == MEM_REF
5910 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5911 && !REF_REVERSE_STORAGE_ORDER (to)
5912 && known_eq (bitpos, 0)
5913 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5914 result = store_expr (from, to_rtx, 0, nontemporal, false);
5915 else
5917 rtx to_rtx1
5918 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5919 SUBREG_REG (to_rtx),
5920 subreg_promoted_mode (to_rtx));
5921 result = store_field (to_rtx1, bitsize, bitpos,
5922 bitregion_start, bitregion_end,
5923 mode1, from, get_alias_set (to),
5924 nontemporal, reversep);
5925 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5926 SUBREG_PROMOTED_SIGN (to_rtx));
5929 else
5930 result = store_field (to_rtx, bitsize, bitpos,
5931 bitregion_start, bitregion_end,
5932 mode1, from, get_alias_set (to),
5933 nontemporal, reversep);
5936 if (result)
5937 preserve_temp_slots (result);
5938 pop_temp_slots ();
5939 return;
5942 /* If the rhs is a function call and its value is not an aggregate,
5943 call the function before we start to compute the lhs.
5944 This is needed for correct code for cases such as
5945 val = setjmp (buf) on machines where reference to val
5946 requires loading up part of an address in a separate insn.
5948 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5949 since it might be a promoted variable where the zero- or sign- extension
5950 needs to be done. Handling this in the normal way is safe because no
5951 computation is done before the call. The same is true for SSA names. */
5952 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5953 && COMPLETE_TYPE_P (TREE_TYPE (from))
5954 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5955 && ! (((VAR_P (to)
5956 || TREE_CODE (to) == PARM_DECL
5957 || TREE_CODE (to) == RESULT_DECL)
5958 && REG_P (DECL_RTL (to)))
5959 || TREE_CODE (to) == SSA_NAME))
5961 rtx value;
5963 push_temp_slots ();
5964 value = expand_normal (from);
5966 if (to_rtx == 0)
5967 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5969 /* Handle calls that return values in multiple non-contiguous locations.
5970 The Irix 6 ABI has examples of this. */
5971 if (GET_CODE (to_rtx) == PARALLEL)
5973 if (GET_CODE (value) == PARALLEL)
5974 emit_group_move (to_rtx, value);
5975 else
5976 emit_group_load (to_rtx, value, TREE_TYPE (from),
5977 int_size_in_bytes (TREE_TYPE (from)));
5979 else if (GET_CODE (value) == PARALLEL)
5980 emit_group_store (to_rtx, value, TREE_TYPE (from),
5981 int_size_in_bytes (TREE_TYPE (from)));
5982 else if (GET_MODE (to_rtx) == BLKmode)
5984 /* Handle calls that return BLKmode values in registers. */
5985 if (REG_P (value))
5986 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5987 else
5988 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5990 else
5992 if (POINTER_TYPE_P (TREE_TYPE (to)))
5993 value = convert_memory_address_addr_space
5994 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5995 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5997 emit_move_insn (to_rtx, value);
6000 preserve_temp_slots (to_rtx);
6001 pop_temp_slots ();
6002 return;
6005 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
6006 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6008 /* Don't move directly into a return register. */
6009 if (TREE_CODE (to) == RESULT_DECL
6010 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
6012 rtx temp;
6014 push_temp_slots ();
6016 /* If the source is itself a return value, it still is in a pseudo at
6017 this point so we can move it back to the return register directly. */
6018 if (REG_P (to_rtx)
6019 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6020 && TREE_CODE (from) != CALL_EXPR)
6021 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6022 else
6023 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6025 /* Handle calls that return values in multiple non-contiguous locations.
6026 The Irix 6 ABI has examples of this. */
6027 if (GET_CODE (to_rtx) == PARALLEL)
6029 if (GET_CODE (temp) == PARALLEL)
6030 emit_group_move (to_rtx, temp);
6031 else
6032 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6033 int_size_in_bytes (TREE_TYPE (from)));
6035 else if (temp)
6036 emit_move_insn (to_rtx, temp);
6038 preserve_temp_slots (to_rtx);
6039 pop_temp_slots ();
6040 return;
6043 /* In case we are returning the contents of an object which overlaps
6044 the place the value is being stored, use a safe function when copying
6045 a value through a pointer into a structure value return block. */
6046 if (TREE_CODE (to) == RESULT_DECL
6047 && TREE_CODE (from) == INDIRECT_REF
6048 && ADDR_SPACE_GENERIC_P
6049 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6050 && refs_may_alias_p (to, from)
6051 && cfun->returns_struct
6052 && !cfun->returns_pcc_struct)
6054 rtx from_rtx, size;
6056 push_temp_slots ();
6057 size = expr_size (from);
6058 from_rtx = expand_normal (from);
6060 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6062 preserve_temp_slots (to_rtx);
6063 pop_temp_slots ();
6064 return;
6067 /* Compute FROM and store the value in the rtx we got. */
6069 push_temp_slots ();
6070 result = store_expr (from, to_rtx, 0, nontemporal, false);
6071 preserve_temp_slots (result);
6072 pop_temp_slots ();
6073 return;
6076 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6077 succeeded, false otherwise. */
6079 bool
6080 emit_storent_insn (rtx to, rtx from)
6082 class expand_operand ops[2];
6083 machine_mode mode = GET_MODE (to);
6084 enum insn_code code = optab_handler (storent_optab, mode);
6086 if (code == CODE_FOR_nothing)
6087 return false;
6089 create_fixed_operand (&ops[0], to);
6090 create_input_operand (&ops[1], from, mode);
6091 return maybe_expand_insn (code, 2, ops);
6094 /* Helper function for store_expr storing of STRING_CST. */
6096 static rtx
6097 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6098 fixed_size_mode mode)
6100 tree str = (tree) data;
6102 gcc_assert (offset >= 0);
6103 if (offset >= TREE_STRING_LENGTH (str))
6104 return const0_rtx;
6106 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6107 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6109 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6110 size_t l = TREE_STRING_LENGTH (str) - offset;
6111 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6112 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6113 return c_readstr (p, mode, false);
6116 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
6119 /* Generate code for computing expression EXP,
6120 and storing the value into TARGET.
6122 If the mode is BLKmode then we may return TARGET itself.
6123 It turns out that in BLKmode it doesn't cause a problem.
6124 because C has no operators that could combine two different
6125 assignments into the same BLKmode object with different values
6126 with no sequence point. Will other languages need this to
6127 be more thorough?
6129 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6130 stack, and block moves may need to be treated specially.
6132 If NONTEMPORAL is true, try using a nontemporal store instruction.
6134 If REVERSE is true, the store is to be done in reverse order. */
6137 store_expr (tree exp, rtx target, int call_param_p,
6138 bool nontemporal, bool reverse)
6140 rtx temp;
6141 rtx alt_rtl = NULL_RTX;
6142 location_t loc = curr_insn_location ();
6143 bool shortened_string_cst = false;
6145 if (VOID_TYPE_P (TREE_TYPE (exp)))
6147 /* C++ can generate ?: expressions with a throw expression in one
6148 branch and an rvalue in the other. Here, we resolve attempts to
6149 store the throw expression's nonexistent result. */
6150 gcc_assert (!call_param_p);
6151 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6152 return NULL_RTX;
6154 if (TREE_CODE (exp) == COMPOUND_EXPR)
6156 /* Perform first part of compound expression, then assign from second
6157 part. */
6158 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6159 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6160 return store_expr (TREE_OPERAND (exp, 1), target,
6161 call_param_p, nontemporal, reverse);
6163 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6165 /* For conditional expression, get safe form of the target. Then
6166 test the condition, doing the appropriate assignment on either
6167 side. This avoids the creation of unnecessary temporaries.
6168 For non-BLKmode, it is more efficient not to do this. */
6170 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6172 do_pending_stack_adjust ();
6173 NO_DEFER_POP;
6174 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6175 profile_probability::uninitialized ());
6176 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6177 nontemporal, reverse);
6178 emit_jump_insn (targetm.gen_jump (lab2));
6179 emit_barrier ();
6180 emit_label (lab1);
6181 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6182 nontemporal, reverse);
6183 emit_label (lab2);
6184 OK_DEFER_POP;
6186 return NULL_RTX;
6188 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6189 /* If this is a scalar in a register that is stored in a wider mode
6190 than the declared mode, compute the result into its declared mode
6191 and then convert to the wider mode. Our value is the computed
6192 expression. */
6194 rtx inner_target = 0;
6195 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6196 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6198 /* We can do the conversion inside EXP, which will often result
6199 in some optimizations. Do the conversion in two steps: first
6200 change the signedness, if needed, then the extend. But don't
6201 do this if the type of EXP is a subtype of something else
6202 since then the conversion might involve more than just
6203 converting modes. */
6204 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6205 && TREE_TYPE (TREE_TYPE (exp)) == 0
6206 && GET_MODE_PRECISION (outer_mode)
6207 == TYPE_PRECISION (TREE_TYPE (exp)))
6209 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6210 TYPE_UNSIGNED (TREE_TYPE (exp))))
6212 /* Some types, e.g. Fortran's logical*4, won't have a signed
6213 version, so use the mode instead. */
6214 tree ntype
6215 = (signed_or_unsigned_type_for
6216 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6217 if (ntype == NULL)
6218 ntype = lang_hooks.types.type_for_mode
6219 (TYPE_MODE (TREE_TYPE (exp)),
6220 SUBREG_PROMOTED_SIGN (target));
6222 exp = fold_convert_loc (loc, ntype, exp);
6225 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6226 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6227 exp);
6229 inner_target = SUBREG_REG (target);
6232 temp = expand_expr (exp, inner_target, VOIDmode,
6233 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6236 /* If TEMP is a VOIDmode constant, use convert_modes to make
6237 sure that we properly convert it. */
6238 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6240 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6241 temp, SUBREG_PROMOTED_SIGN (target));
6242 temp = convert_modes (inner_mode, outer_mode, temp,
6243 SUBREG_PROMOTED_SIGN (target));
6245 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6246 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6247 temp, SUBREG_PROMOTED_SIGN (target));
6249 convert_move (SUBREG_REG (target), temp,
6250 SUBREG_PROMOTED_SIGN (target));
6252 return NULL_RTX;
6254 else if ((TREE_CODE (exp) == STRING_CST
6255 || (TREE_CODE (exp) == MEM_REF
6256 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6257 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6258 == STRING_CST
6259 && integer_zerop (TREE_OPERAND (exp, 1))))
6260 && !nontemporal && !call_param_p
6261 && MEM_P (target))
6263 /* Optimize initialization of an array with a STRING_CST. */
6264 HOST_WIDE_INT exp_len, str_copy_len;
6265 rtx dest_mem;
6266 tree str = TREE_CODE (exp) == STRING_CST
6267 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6269 exp_len = int_expr_size (exp);
6270 if (exp_len <= 0)
6271 goto normal_expr;
6273 if (TREE_STRING_LENGTH (str) <= 0)
6274 goto normal_expr;
6276 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6277 MEM_ALIGN (target), false))
6279 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6280 MEM_ALIGN (target), false, RETURN_BEGIN);
6281 return NULL_RTX;
6284 str_copy_len = TREE_STRING_LENGTH (str);
6286 /* Trailing NUL bytes in EXP will be handled by the call to
6287 clear_storage, which is more efficient than copying them from
6288 the STRING_CST, so trim those from STR_COPY_LEN. */
6289 while (str_copy_len)
6291 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6292 break;
6293 str_copy_len--;
6296 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6298 str_copy_len += STORE_MAX_PIECES - 1;
6299 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6301 if (str_copy_len >= exp_len)
6302 goto normal_expr;
6304 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6305 (void *) str, MEM_ALIGN (target), false))
6306 goto normal_expr;
6308 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6309 (void *) str, MEM_ALIGN (target), false,
6310 RETURN_END);
6311 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6312 exp_len - str_copy_len),
6313 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6314 return NULL_RTX;
6316 else
6318 rtx tmp_target;
6320 normal_expr:
6321 /* If we want to use a nontemporal or a reverse order store, force the
6322 value into a register first. */
6323 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6324 tree rexp = exp;
6325 if (TREE_CODE (exp) == STRING_CST
6326 && tmp_target == target
6327 && GET_MODE (target) == BLKmode
6328 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6330 rtx size = expr_size (exp);
6331 if (CONST_INT_P (size)
6332 && size != const0_rtx
6333 && (UINTVAL (size)
6334 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6336 /* If the STRING_CST has much larger array type than
6337 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6338 it into the rodata section as the code later on will use
6339 memset zero for the remainder anyway. See PR95052. */
6340 tmp_target = NULL_RTX;
6341 rexp = copy_node (exp);
6342 tree index
6343 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6344 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6345 index);
6346 shortened_string_cst = true;
6349 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6350 (call_param_p
6351 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6352 &alt_rtl, false);
6353 if (shortened_string_cst)
6355 gcc_assert (MEM_P (temp));
6356 temp = change_address (temp, BLKmode, NULL_RTX);
6360 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6361 the same as that of TARGET, adjust the constant. This is needed, for
6362 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6363 only a word-sized value. */
6364 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6365 && TREE_CODE (exp) != ERROR_MARK
6366 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6368 gcc_assert (!shortened_string_cst);
6369 if (GET_MODE_CLASS (GET_MODE (target))
6370 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6371 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6372 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6374 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6375 TYPE_MODE (TREE_TYPE (exp)), 0);
6376 if (t)
6377 temp = t;
6379 if (GET_MODE (temp) == VOIDmode)
6380 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6381 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6384 /* If value was not generated in the target, store it there.
6385 Convert the value to TARGET's type first if necessary and emit the
6386 pending incrementations that have been queued when expanding EXP.
6387 Note that we cannot emit the whole queue blindly because this will
6388 effectively disable the POST_INC optimization later.
6390 If TEMP and TARGET compare equal according to rtx_equal_p, but
6391 one or both of them are volatile memory refs, we have to distinguish
6392 two cases:
6393 - expand_expr has used TARGET. In this case, we must not generate
6394 another copy. This can be detected by TARGET being equal according
6395 to == .
6396 - expand_expr has not used TARGET - that means that the source just
6397 happens to have the same RTX form. Since temp will have been created
6398 by expand_expr, it will compare unequal according to == .
6399 We must generate a copy in this case, to reach the correct number
6400 of volatile memory references. */
6402 if ((! rtx_equal_p (temp, target)
6403 || (temp != target && (side_effects_p (temp)
6404 || side_effects_p (target)
6405 || (MEM_P (temp)
6406 && !mems_same_for_tbaa_p (temp, target)))))
6407 && TREE_CODE (exp) != ERROR_MARK
6408 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6409 but TARGET is not valid memory reference, TEMP will differ
6410 from TARGET although it is really the same location. */
6411 && !(alt_rtl
6412 && rtx_equal_p (alt_rtl, target)
6413 && !side_effects_p (alt_rtl)
6414 && !side_effects_p (target))
6415 /* If there's nothing to copy, don't bother. Don't call
6416 expr_size unless necessary, because some front-ends (C++)
6417 expr_size-hook must not be given objects that are not
6418 supposed to be bit-copied or bit-initialized. */
6419 && expr_size (exp) != const0_rtx)
6421 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6423 gcc_assert (!shortened_string_cst);
6424 if (GET_MODE (target) == BLKmode)
6426 /* Handle calls that return BLKmode values in registers. */
6427 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6428 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6429 else
6430 store_bit_field (target,
6431 rtx_to_poly_int64 (expr_size (exp))
6432 * BITS_PER_UNIT,
6433 0, 0, 0, GET_MODE (temp), temp, reverse,
6434 false);
6436 else
6437 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6440 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6442 /* Handle copying a string constant into an array. The string
6443 constant may be shorter than the array. So copy just the string's
6444 actual length, and clear the rest. First get the size of the data
6445 type of the string, which is actually the size of the target. */
6446 rtx size = expr_size (exp);
6448 if (CONST_INT_P (size)
6449 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6450 emit_block_move (target, temp, size,
6451 (call_param_p
6452 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6453 else
6455 machine_mode pointer_mode
6456 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6457 machine_mode address_mode = get_address_mode (target);
6459 /* Compute the size of the data to copy from the string. */
6460 tree copy_size
6461 = size_binop_loc (loc, MIN_EXPR,
6462 make_tree (sizetype, size),
6463 size_int (TREE_STRING_LENGTH (exp)));
6464 rtx copy_size_rtx
6465 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6466 (call_param_p
6467 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6468 rtx_code_label *label = 0;
6470 /* Copy that much. */
6471 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6472 TYPE_UNSIGNED (sizetype));
6473 emit_block_move (target, temp, copy_size_rtx,
6474 (call_param_p
6475 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6477 /* Figure out how much is left in TARGET that we have to clear.
6478 Do all calculations in pointer_mode. */
6479 poly_int64 const_copy_size;
6480 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6482 size = plus_constant (address_mode, size, -const_copy_size);
6483 target = adjust_address (target, BLKmode, const_copy_size);
6485 else
6487 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6488 copy_size_rtx, NULL_RTX, 0,
6489 OPTAB_LIB_WIDEN);
6491 if (GET_MODE (copy_size_rtx) != address_mode)
6492 copy_size_rtx = convert_to_mode (address_mode,
6493 copy_size_rtx,
6494 TYPE_UNSIGNED (sizetype));
6496 target = offset_address (target, copy_size_rtx,
6497 highest_pow2_factor (copy_size));
6498 label = gen_label_rtx ();
6499 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6500 GET_MODE (size), 0, label);
6503 if (size != const0_rtx)
6504 clear_storage (target, size, BLOCK_OP_NORMAL);
6506 if (label)
6507 emit_label (label);
6510 else if (shortened_string_cst)
6511 gcc_unreachable ();
6512 /* Handle calls that return values in multiple non-contiguous locations.
6513 The Irix 6 ABI has examples of this. */
6514 else if (GET_CODE (target) == PARALLEL)
6516 if (GET_CODE (temp) == PARALLEL)
6517 emit_group_move (target, temp);
6518 else
6519 emit_group_load (target, temp, TREE_TYPE (exp),
6520 int_size_in_bytes (TREE_TYPE (exp)));
6522 else if (GET_CODE (temp) == PARALLEL)
6523 emit_group_store (target, temp, TREE_TYPE (exp),
6524 int_size_in_bytes (TREE_TYPE (exp)));
6525 else if (GET_MODE (temp) == BLKmode)
6526 emit_block_move (target, temp, expr_size (exp),
6527 (call_param_p
6528 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6529 /* If we emit a nontemporal store, there is nothing else to do. */
6530 else if (nontemporal && emit_storent_insn (target, temp))
6532 else
6534 if (reverse)
6535 temp = flip_storage_order (GET_MODE (target), temp);
6536 temp = force_operand (temp, target);
6537 if (temp != target)
6538 emit_move_insn (target, temp);
6541 else
6542 gcc_assert (!shortened_string_cst);
6544 return NULL_RTX;
6547 /* Return true if field F of structure TYPE is a flexible array. */
6549 static bool
6550 flexible_array_member_p (const_tree f, const_tree type)
6552 const_tree tf;
6554 tf = TREE_TYPE (f);
6555 return (DECL_CHAIN (f) == NULL
6556 && TREE_CODE (tf) == ARRAY_TYPE
6557 && TYPE_DOMAIN (tf)
6558 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6559 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6560 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6561 && int_size_in_bytes (type) >= 0);
6564 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6565 must have in order for it to completely initialize a value of type TYPE.
6566 Return -1 if the number isn't known.
6568 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6570 static HOST_WIDE_INT
6571 count_type_elements (const_tree type, bool for_ctor_p)
6573 switch (TREE_CODE (type))
6575 case ARRAY_TYPE:
6577 tree nelts;
6579 nelts = array_type_nelts (type);
6580 if (nelts && tree_fits_uhwi_p (nelts))
6582 unsigned HOST_WIDE_INT n;
6584 n = tree_to_uhwi (nelts) + 1;
6585 if (n == 0 || for_ctor_p)
6586 return n;
6587 else
6588 return n * count_type_elements (TREE_TYPE (type), false);
6590 return for_ctor_p ? -1 : 1;
6593 case RECORD_TYPE:
6595 unsigned HOST_WIDE_INT n;
6596 tree f;
6598 n = 0;
6599 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6600 if (TREE_CODE (f) == FIELD_DECL)
6602 if (!for_ctor_p)
6603 n += count_type_elements (TREE_TYPE (f), false);
6604 else if (!flexible_array_member_p (f, type))
6605 /* Don't count flexible arrays, which are not supposed
6606 to be initialized. */
6607 n += 1;
6610 return n;
6613 case UNION_TYPE:
6614 case QUAL_UNION_TYPE:
6616 tree f;
6617 HOST_WIDE_INT n, m;
6619 gcc_assert (!for_ctor_p);
6620 /* Estimate the number of scalars in each field and pick the
6621 maximum. Other estimates would do instead; the idea is simply
6622 to make sure that the estimate is not sensitive to the ordering
6623 of the fields. */
6624 n = 1;
6625 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6626 if (TREE_CODE (f) == FIELD_DECL)
6628 m = count_type_elements (TREE_TYPE (f), false);
6629 /* If the field doesn't span the whole union, add an extra
6630 scalar for the rest. */
6631 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6632 TYPE_SIZE (type)) != 1)
6633 m++;
6634 if (n < m)
6635 n = m;
6637 return n;
6640 case COMPLEX_TYPE:
6641 return 2;
6643 case VECTOR_TYPE:
6645 unsigned HOST_WIDE_INT nelts;
6646 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6647 return nelts;
6648 else
6649 return -1;
6652 case INTEGER_TYPE:
6653 case REAL_TYPE:
6654 case FIXED_POINT_TYPE:
6655 case ENUMERAL_TYPE:
6656 case BOOLEAN_TYPE:
6657 case POINTER_TYPE:
6658 case OFFSET_TYPE:
6659 case REFERENCE_TYPE:
6660 case NULLPTR_TYPE:
6661 case OPAQUE_TYPE:
6662 return 1;
6664 case ERROR_MARK:
6665 return 0;
6667 case VOID_TYPE:
6668 case METHOD_TYPE:
6669 case FUNCTION_TYPE:
6670 case LANG_TYPE:
6671 default:
6672 gcc_unreachable ();
6676 /* Helper for categorize_ctor_elements. Identical interface. */
6678 static bool
6679 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6680 HOST_WIDE_INT *p_unique_nz_elts,
6681 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6683 unsigned HOST_WIDE_INT idx;
6684 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6685 tree value, purpose, elt_type;
6687 /* Whether CTOR is a valid constant initializer, in accordance with what
6688 initializer_constant_valid_p does. If inferred from the constructor
6689 elements, true until proven otherwise. */
6690 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6691 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6693 nz_elts = 0;
6694 unique_nz_elts = 0;
6695 init_elts = 0;
6696 num_fields = 0;
6697 elt_type = NULL_TREE;
6699 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6701 HOST_WIDE_INT mult = 1;
6703 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6705 tree lo_index = TREE_OPERAND (purpose, 0);
6706 tree hi_index = TREE_OPERAND (purpose, 1);
6708 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6709 mult = (tree_to_uhwi (hi_index)
6710 - tree_to_uhwi (lo_index) + 1);
6712 num_fields += mult;
6713 elt_type = TREE_TYPE (value);
6715 switch (TREE_CODE (value))
6717 case CONSTRUCTOR:
6719 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6721 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6722 &ic, p_complete);
6724 nz_elts += mult * nz;
6725 unique_nz_elts += unz;
6726 init_elts += mult * ic;
6728 if (const_from_elts_p && const_p)
6729 const_p = const_elt_p;
6731 break;
6733 case INTEGER_CST:
6734 case REAL_CST:
6735 case FIXED_CST:
6736 if (!initializer_zerop (value))
6738 nz_elts += mult;
6739 unique_nz_elts++;
6741 init_elts += mult;
6742 break;
6744 case STRING_CST:
6745 nz_elts += mult * TREE_STRING_LENGTH (value);
6746 unique_nz_elts += TREE_STRING_LENGTH (value);
6747 init_elts += mult * TREE_STRING_LENGTH (value);
6748 break;
6750 case COMPLEX_CST:
6751 if (!initializer_zerop (TREE_REALPART (value)))
6753 nz_elts += mult;
6754 unique_nz_elts++;
6756 if (!initializer_zerop (TREE_IMAGPART (value)))
6758 nz_elts += mult;
6759 unique_nz_elts++;
6761 init_elts += 2 * mult;
6762 break;
6764 case VECTOR_CST:
6766 /* We can only construct constant-length vectors using
6767 CONSTRUCTOR. */
6768 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6769 for (unsigned int i = 0; i < nunits; ++i)
6771 tree v = VECTOR_CST_ELT (value, i);
6772 if (!initializer_zerop (v))
6774 nz_elts += mult;
6775 unique_nz_elts++;
6777 init_elts += mult;
6780 break;
6782 default:
6784 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6785 nz_elts += mult * tc;
6786 unique_nz_elts += tc;
6787 init_elts += mult * tc;
6789 if (const_from_elts_p && const_p)
6790 const_p
6791 = initializer_constant_valid_p (value,
6792 elt_type,
6793 TYPE_REVERSE_STORAGE_ORDER
6794 (TREE_TYPE (ctor)))
6795 != NULL_TREE;
6797 break;
6801 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6802 num_fields, elt_type))
6803 *p_complete = false;
6805 *p_nz_elts += nz_elts;
6806 *p_unique_nz_elts += unique_nz_elts;
6807 *p_init_elts += init_elts;
6809 return const_p;
6812 /* Examine CTOR to discover:
6813 * how many scalar fields are set to nonzero values,
6814 and place it in *P_NZ_ELTS;
6815 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6816 high - low + 1 (this can be useful for callers to determine ctors
6817 that could be cheaply initialized with - perhaps nested - loops
6818 compared to copied from huge read-only data),
6819 and place it in *P_UNIQUE_NZ_ELTS;
6820 * how many scalar fields in total are in CTOR,
6821 and place it in *P_ELT_COUNT.
6822 * whether the constructor is complete -- in the sense that every
6823 meaningful byte is explicitly given a value --
6824 and place it in *P_COMPLETE.
6826 Return whether or not CTOR is a valid static constant initializer, the same
6827 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6829 bool
6830 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6831 HOST_WIDE_INT *p_unique_nz_elts,
6832 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6834 *p_nz_elts = 0;
6835 *p_unique_nz_elts = 0;
6836 *p_init_elts = 0;
6837 *p_complete = true;
6839 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6840 p_init_elts, p_complete);
6843 /* Return true if constructor CTOR is simple enough to be materialized
6844 in an integer mode register. Limit the size to WORDS words, which
6845 is 1 by default. */
6847 bool
6848 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6850 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6851 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6852 return false;
6854 return TREE_CONSTANT (ctor)
6855 && !TREE_ADDRESSABLE (ctor)
6856 && CONSTRUCTOR_NELTS (ctor)
6857 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6858 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6859 && initializer_constant_valid_for_bitfield_p (ctor);
6862 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6863 of which had type LAST_TYPE. Each element was itself a complete
6864 initializer, in the sense that every meaningful byte was explicitly
6865 given a value. Return true if the same is true for the constructor
6866 as a whole. */
6868 bool
6869 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6870 const_tree last_type)
6872 if (TREE_CODE (type) == UNION_TYPE
6873 || TREE_CODE (type) == QUAL_UNION_TYPE)
6875 if (num_elts == 0)
6876 return false;
6878 gcc_assert (num_elts == 1 && last_type);
6880 /* ??? We could look at each element of the union, and find the
6881 largest element. Which would avoid comparing the size of the
6882 initialized element against any tail padding in the union.
6883 Doesn't seem worth the effort... */
6884 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6887 return count_type_elements (type, true) == num_elts;
6890 /* Return true if EXP contains mostly (3/4) zeros. */
6892 static bool
6893 mostly_zeros_p (const_tree exp)
6895 if (TREE_CODE (exp) == CONSTRUCTOR)
6897 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6898 bool complete_p;
6900 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6901 &complete_p);
6902 return !complete_p || nz_elts < init_elts / 4;
6905 return initializer_zerop (exp);
6908 /* Return true if EXP contains all zeros. */
6910 static bool
6911 all_zeros_p (const_tree exp)
6913 if (TREE_CODE (exp) == CONSTRUCTOR)
6915 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6916 bool complete_p;
6918 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6919 &complete_p);
6920 return nz_elts == 0;
6923 return initializer_zerop (exp);
6926 /* Helper function for store_constructor.
6927 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6928 CLEARED is as for store_constructor.
6929 ALIAS_SET is the alias set to use for any stores.
6930 If REVERSE is true, the store is to be done in reverse order.
6932 This provides a recursive shortcut back to store_constructor when it isn't
6933 necessary to go through store_field. This is so that we can pass through
6934 the cleared field to let store_constructor know that we may not have to
6935 clear a substructure if the outer structure has already been cleared. */
6937 static void
6938 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6939 poly_uint64 bitregion_start,
6940 poly_uint64 bitregion_end,
6941 machine_mode mode,
6942 tree exp, int cleared,
6943 alias_set_type alias_set, bool reverse)
6945 poly_int64 bytepos;
6946 poly_uint64 bytesize;
6947 if (TREE_CODE (exp) == CONSTRUCTOR
6948 /* We can only call store_constructor recursively if the size and
6949 bit position are on a byte boundary. */
6950 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6951 && maybe_ne (bitsize, 0U)
6952 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6953 /* If we have a nonzero bitpos for a register target, then we just
6954 let store_field do the bitfield handling. This is unlikely to
6955 generate unnecessary clear instructions anyways. */
6956 && (known_eq (bitpos, 0) || MEM_P (target)))
6958 if (MEM_P (target))
6960 machine_mode target_mode = GET_MODE (target);
6961 if (target_mode != BLKmode
6962 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6963 target_mode = BLKmode;
6964 target = adjust_address (target, target_mode, bytepos);
6968 /* Update the alias set, if required. */
6969 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6970 && MEM_ALIAS_SET (target) != 0)
6972 target = copy_rtx (target);
6973 set_mem_alias_set (target, alias_set);
6976 store_constructor (exp, target, cleared, bytesize, reverse);
6978 else
6979 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6980 exp, alias_set, false, reverse);
6984 /* Returns the number of FIELD_DECLs in TYPE. */
6986 static int
6987 fields_length (const_tree type)
6989 tree t = TYPE_FIELDS (type);
6990 int count = 0;
6992 for (; t; t = DECL_CHAIN (t))
6993 if (TREE_CODE (t) == FIELD_DECL)
6994 ++count;
6996 return count;
7000 /* Store the value of constructor EXP into the rtx TARGET.
7001 TARGET is either a REG or a MEM; we know it cannot conflict, since
7002 safe_from_p has been called.
7003 CLEARED is true if TARGET is known to have been zero'd.
7004 SIZE is the number of bytes of TARGET we are allowed to modify: this
7005 may not be the same as the size of EXP if we are assigning to a field
7006 which has been packed to exclude padding bits.
7007 If REVERSE is true, the store is to be done in reverse order. */
7009 void
7010 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
7011 bool reverse)
7013 tree type = TREE_TYPE (exp);
7014 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
7015 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
7017 switch (TREE_CODE (type))
7019 case RECORD_TYPE:
7020 case UNION_TYPE:
7021 case QUAL_UNION_TYPE:
7023 unsigned HOST_WIDE_INT idx;
7024 tree field, value;
7026 /* The storage order is specified for every aggregate type. */
7027 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7029 /* If size is zero or the target is already cleared, do nothing. */
7030 if (known_eq (size, 0) || cleared)
7031 cleared = 1;
7032 /* We either clear the aggregate or indicate the value is dead. */
7033 else if ((TREE_CODE (type) == UNION_TYPE
7034 || TREE_CODE (type) == QUAL_UNION_TYPE)
7035 && ! CONSTRUCTOR_ELTS (exp))
7036 /* If the constructor is empty, clear the union. */
7038 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7039 cleared = 1;
7042 /* If we are building a static constructor into a register,
7043 set the initial value as zero so we can fold the value into
7044 a constant. But if more than one register is involved,
7045 this probably loses. */
7046 else if (REG_P (target) && TREE_STATIC (exp)
7047 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7048 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7050 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7051 cleared = 1;
7054 /* If the constructor has fewer fields than the structure or
7055 if we are initializing the structure to mostly zeros, clear
7056 the whole structure first. Don't do this if TARGET is a
7057 register whose mode size isn't equal to SIZE since
7058 clear_storage can't handle this case. */
7059 else if (known_size_p (size)
7060 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7061 || mostly_zeros_p (exp))
7062 && (!REG_P (target)
7063 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7065 clear_storage (target, gen_int_mode (size, Pmode),
7066 BLOCK_OP_NORMAL);
7067 cleared = 1;
7070 if (REG_P (target) && !cleared)
7071 emit_clobber (target);
7073 /* Store each element of the constructor into the
7074 corresponding field of TARGET. */
7075 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7077 machine_mode mode;
7078 HOST_WIDE_INT bitsize;
7079 HOST_WIDE_INT bitpos = 0;
7080 tree offset;
7081 rtx to_rtx = target;
7083 /* Just ignore missing fields. We cleared the whole
7084 structure, above, if any fields are missing. */
7085 if (field == 0)
7086 continue;
7088 if (cleared && initializer_zerop (value))
7089 continue;
7091 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7092 bitsize = tree_to_uhwi (DECL_SIZE (field));
7093 else
7094 gcc_unreachable ();
7096 mode = DECL_MODE (field);
7097 if (DECL_BIT_FIELD (field))
7098 mode = VOIDmode;
7100 offset = DECL_FIELD_OFFSET (field);
7101 if (tree_fits_shwi_p (offset)
7102 && tree_fits_shwi_p (bit_position (field)))
7104 bitpos = int_bit_position (field);
7105 offset = NULL_TREE;
7107 else
7108 gcc_unreachable ();
7110 /* If this initializes a field that is smaller than a
7111 word, at the start of a word, try to widen it to a full
7112 word. This special case allows us to output C++ member
7113 function initializations in a form that the optimizers
7114 can understand. */
7115 if (WORD_REGISTER_OPERATIONS
7116 && REG_P (target)
7117 && bitsize < BITS_PER_WORD
7118 && bitpos % BITS_PER_WORD == 0
7119 && GET_MODE_CLASS (mode) == MODE_INT
7120 && TREE_CODE (value) == INTEGER_CST
7121 && exp_size >= 0
7122 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7124 type = TREE_TYPE (value);
7126 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7128 type = lang_hooks.types.type_for_mode
7129 (word_mode, TYPE_UNSIGNED (type));
7130 value = fold_convert (type, value);
7131 /* Make sure the bits beyond the original bitsize are zero
7132 so that we can correctly avoid extra zeroing stores in
7133 later constructor elements. */
7134 tree bitsize_mask
7135 = wide_int_to_tree (type, wi::mask (bitsize, false,
7136 BITS_PER_WORD));
7137 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7140 if (BYTES_BIG_ENDIAN)
7141 value
7142 = fold_build2 (LSHIFT_EXPR, type, value,
7143 build_int_cst (type,
7144 BITS_PER_WORD - bitsize));
7145 bitsize = BITS_PER_WORD;
7146 mode = word_mode;
7149 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7150 && DECL_NONADDRESSABLE_P (field))
7152 to_rtx = copy_rtx (to_rtx);
7153 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7156 store_constructor_field (to_rtx, bitsize, bitpos,
7157 0, bitregion_end, mode,
7158 value, cleared,
7159 get_alias_set (TREE_TYPE (field)),
7160 reverse);
7162 break;
7164 case ARRAY_TYPE:
7166 tree value, index;
7167 unsigned HOST_WIDE_INT i;
7168 bool need_to_clear;
7169 tree domain;
7170 tree elttype = TREE_TYPE (type);
7171 bool const_bounds_p;
7172 HOST_WIDE_INT minelt = 0;
7173 HOST_WIDE_INT maxelt = 0;
7175 /* The storage order is specified for every aggregate type. */
7176 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7178 domain = TYPE_DOMAIN (type);
7179 const_bounds_p = (TYPE_MIN_VALUE (domain)
7180 && TYPE_MAX_VALUE (domain)
7181 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7182 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7184 /* If we have constant bounds for the range of the type, get them. */
7185 if (const_bounds_p)
7187 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7188 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7191 /* If the constructor has fewer elements than the array, clear
7192 the whole array first. Similarly if this is static
7193 constructor of a non-BLKmode object. */
7194 if (cleared)
7195 need_to_clear = false;
7196 else if (REG_P (target) && TREE_STATIC (exp))
7197 need_to_clear = true;
7198 else
7200 unsigned HOST_WIDE_INT idx;
7201 HOST_WIDE_INT count = 0, zero_count = 0;
7202 need_to_clear = ! const_bounds_p;
7204 /* This loop is a more accurate version of the loop in
7205 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7206 is also needed to check for missing elements. */
7207 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7209 HOST_WIDE_INT this_node_count;
7211 if (need_to_clear)
7212 break;
7214 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7216 tree lo_index = TREE_OPERAND (index, 0);
7217 tree hi_index = TREE_OPERAND (index, 1);
7219 if (! tree_fits_uhwi_p (lo_index)
7220 || ! tree_fits_uhwi_p (hi_index))
7222 need_to_clear = true;
7223 break;
7226 this_node_count = (tree_to_uhwi (hi_index)
7227 - tree_to_uhwi (lo_index) + 1);
7229 else
7230 this_node_count = 1;
7232 count += this_node_count;
7233 if (mostly_zeros_p (value))
7234 zero_count += this_node_count;
7237 /* Clear the entire array first if there are any missing
7238 elements, or if the incidence of zero elements is >=
7239 75%. */
7240 if (! need_to_clear
7241 && (count < maxelt - minelt + 1
7242 || 4 * zero_count >= 3 * count))
7243 need_to_clear = true;
7246 if (need_to_clear && maybe_gt (size, 0))
7248 if (REG_P (target))
7249 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7250 else
7251 clear_storage (target, gen_int_mode (size, Pmode),
7252 BLOCK_OP_NORMAL);
7253 cleared = 1;
7256 if (!cleared && REG_P (target))
7257 /* Inform later passes that the old value is dead. */
7258 emit_clobber (target);
7260 /* Store each element of the constructor into the
7261 corresponding element of TARGET, determined by counting the
7262 elements. */
7263 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7265 machine_mode mode;
7266 poly_int64 bitsize;
7267 HOST_WIDE_INT bitpos;
7268 rtx xtarget = target;
7270 if (cleared && initializer_zerop (value))
7271 continue;
7273 mode = TYPE_MODE (elttype);
7274 if (mode != BLKmode)
7275 bitsize = GET_MODE_BITSIZE (mode);
7276 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7277 bitsize = -1;
7279 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7281 tree lo_index = TREE_OPERAND (index, 0);
7282 tree hi_index = TREE_OPERAND (index, 1);
7283 rtx index_r, pos_rtx;
7284 HOST_WIDE_INT lo, hi, count;
7285 tree position;
7287 /* If the range is constant and "small", unroll the loop. */
7288 if (const_bounds_p
7289 && tree_fits_shwi_p (lo_index)
7290 && tree_fits_shwi_p (hi_index)
7291 && (lo = tree_to_shwi (lo_index),
7292 hi = tree_to_shwi (hi_index),
7293 count = hi - lo + 1,
7294 (!MEM_P (target)
7295 || count <= 2
7296 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7297 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7298 <= 40 * 8)))))
7300 lo -= minelt; hi -= minelt;
7301 for (; lo <= hi; lo++)
7303 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7305 if (MEM_P (target)
7306 && !MEM_KEEP_ALIAS_SET_P (target)
7307 && TREE_CODE (type) == ARRAY_TYPE
7308 && TYPE_NONALIASED_COMPONENT (type))
7310 target = copy_rtx (target);
7311 MEM_KEEP_ALIAS_SET_P (target) = 1;
7314 store_constructor_field
7315 (target, bitsize, bitpos, 0, bitregion_end,
7316 mode, value, cleared,
7317 get_alias_set (elttype), reverse);
7320 else
7322 rtx_code_label *loop_start = gen_label_rtx ();
7323 rtx_code_label *loop_end = gen_label_rtx ();
7324 tree exit_cond;
7326 expand_normal (hi_index);
7328 index = build_decl (EXPR_LOCATION (exp),
7329 VAR_DECL, NULL_TREE, domain);
7330 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7331 SET_DECL_RTL (index, index_r);
7332 store_expr (lo_index, index_r, 0, false, reverse);
7334 /* Build the head of the loop. */
7335 do_pending_stack_adjust ();
7336 emit_label (loop_start);
7338 /* Assign value to element index. */
7339 position =
7340 fold_convert (ssizetype,
7341 fold_build2 (MINUS_EXPR,
7342 TREE_TYPE (index),
7343 index,
7344 TYPE_MIN_VALUE (domain)));
7346 position =
7347 size_binop (MULT_EXPR, position,
7348 fold_convert (ssizetype,
7349 TYPE_SIZE_UNIT (elttype)));
7351 pos_rtx = expand_normal (position);
7352 xtarget = offset_address (target, pos_rtx,
7353 highest_pow2_factor (position));
7354 xtarget = adjust_address (xtarget, mode, 0);
7355 if (TREE_CODE (value) == CONSTRUCTOR)
7356 store_constructor (value, xtarget, cleared,
7357 exact_div (bitsize, BITS_PER_UNIT),
7358 reverse);
7359 else
7360 store_expr (value, xtarget, 0, false, reverse);
7362 /* Generate a conditional jump to exit the loop. */
7363 exit_cond = build2 (LT_EXPR, integer_type_node,
7364 index, hi_index);
7365 jumpif (exit_cond, loop_end,
7366 profile_probability::uninitialized ());
7368 /* Update the loop counter, and jump to the head of
7369 the loop. */
7370 expand_assignment (index,
7371 build2 (PLUS_EXPR, TREE_TYPE (index),
7372 index, integer_one_node),
7373 false);
7375 emit_jump (loop_start);
7377 /* Build the end of the loop. */
7378 emit_label (loop_end);
7381 else if ((index != 0 && ! tree_fits_shwi_p (index))
7382 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7384 tree position;
7386 if (index == 0)
7387 index = ssize_int (1);
7389 if (minelt)
7390 index = fold_convert (ssizetype,
7391 fold_build2 (MINUS_EXPR,
7392 TREE_TYPE (index),
7393 index,
7394 TYPE_MIN_VALUE (domain)));
7396 position =
7397 size_binop (MULT_EXPR, index,
7398 fold_convert (ssizetype,
7399 TYPE_SIZE_UNIT (elttype)));
7400 xtarget = offset_address (target,
7401 expand_normal (position),
7402 highest_pow2_factor (position));
7403 xtarget = adjust_address (xtarget, mode, 0);
7404 store_expr (value, xtarget, 0, false, reverse);
7406 else
7408 if (index != 0)
7409 bitpos = ((tree_to_shwi (index) - minelt)
7410 * tree_to_uhwi (TYPE_SIZE (elttype)));
7411 else
7412 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7414 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7415 && TREE_CODE (type) == ARRAY_TYPE
7416 && TYPE_NONALIASED_COMPONENT (type))
7418 target = copy_rtx (target);
7419 MEM_KEEP_ALIAS_SET_P (target) = 1;
7421 store_constructor_field (target, bitsize, bitpos, 0,
7422 bitregion_end, mode, value,
7423 cleared, get_alias_set (elttype),
7424 reverse);
7427 break;
7430 case VECTOR_TYPE:
7432 unsigned HOST_WIDE_INT idx;
7433 constructor_elt *ce;
7434 int i;
7435 bool need_to_clear;
7436 insn_code icode = CODE_FOR_nothing;
7437 tree elt;
7438 tree elttype = TREE_TYPE (type);
7439 int elt_size = vector_element_bits (type);
7440 machine_mode eltmode = TYPE_MODE (elttype);
7441 HOST_WIDE_INT bitsize;
7442 HOST_WIDE_INT bitpos;
7443 rtvec vector = NULL;
7444 poly_uint64 n_elts;
7445 unsigned HOST_WIDE_INT const_n_elts;
7446 alias_set_type alias;
7447 bool vec_vec_init_p = false;
7448 machine_mode mode = GET_MODE (target);
7450 gcc_assert (eltmode != BLKmode);
7452 /* Try using vec_duplicate_optab for uniform vectors. */
7453 if (!TREE_SIDE_EFFECTS (exp)
7454 && VECTOR_MODE_P (mode)
7455 && eltmode == GET_MODE_INNER (mode)
7456 && ((icode = optab_handler (vec_duplicate_optab, mode))
7457 != CODE_FOR_nothing)
7458 && (elt = uniform_vector_p (exp))
7459 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7461 class expand_operand ops[2];
7462 create_output_operand (&ops[0], target, mode);
7463 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7464 expand_insn (icode, 2, ops);
7465 if (!rtx_equal_p (target, ops[0].value))
7466 emit_move_insn (target, ops[0].value);
7467 break;
7469 /* Use sign-extension for uniform boolean vectors with
7470 integer modes. Effectively "vec_duplicate" for bitmasks. */
7471 if (!TREE_SIDE_EFFECTS (exp)
7472 && VECTOR_BOOLEAN_TYPE_P (type)
7473 && SCALAR_INT_MODE_P (mode)
7474 && (elt = uniform_vector_p (exp))
7475 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7477 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
7478 expand_normal (elt));
7479 rtx tmp = gen_reg_rtx (mode);
7480 convert_move (tmp, op0, 0);
7482 /* Ensure no excess bits are set.
7483 GCN needs this for nunits < 64.
7484 x86 needs this for nunits < 8. */
7485 auto nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
7486 if (maybe_ne (GET_MODE_PRECISION (mode), nunits))
7487 tmp = expand_binop (mode, and_optab, tmp,
7488 GEN_INT ((1 << nunits) - 1), target,
7489 true, OPTAB_WIDEN);
7490 if (tmp != target)
7491 emit_move_insn (target, tmp);
7492 break;
7495 n_elts = TYPE_VECTOR_SUBPARTS (type);
7496 if (REG_P (target)
7497 && VECTOR_MODE_P (mode)
7498 && n_elts.is_constant (&const_n_elts))
7500 machine_mode emode = eltmode;
7501 bool vector_typed_elts_p = false;
7503 if (CONSTRUCTOR_NELTS (exp)
7504 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7505 == VECTOR_TYPE))
7507 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7508 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7509 * TYPE_VECTOR_SUBPARTS (etype),
7510 n_elts));
7511 emode = TYPE_MODE (etype);
7512 vector_typed_elts_p = true;
7514 icode = convert_optab_handler (vec_init_optab, mode, emode);
7515 if (icode != CODE_FOR_nothing)
7517 unsigned int n = const_n_elts;
7519 if (vector_typed_elts_p)
7521 n = CONSTRUCTOR_NELTS (exp);
7522 vec_vec_init_p = true;
7524 vector = rtvec_alloc (n);
7525 for (unsigned int k = 0; k < n; k++)
7526 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7530 /* Compute the size of the elements in the CTOR. It differs
7531 from the size of the vector type elements only when the
7532 CTOR elements are vectors themselves. */
7533 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7534 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7535 : elttype);
7536 if (VECTOR_TYPE_P (val_type))
7537 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7538 else
7539 bitsize = elt_size;
7541 /* If the constructor has fewer elements than the vector,
7542 clear the whole array first. Similarly if this is static
7543 constructor of a non-BLKmode object. */
7544 if (cleared)
7545 need_to_clear = false;
7546 else if (REG_P (target) && TREE_STATIC (exp))
7547 need_to_clear = true;
7548 else
7550 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7551 tree value;
7553 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7555 int n_elts_here = bitsize / elt_size;
7556 count += n_elts_here;
7557 if (mostly_zeros_p (value))
7558 zero_count += n_elts_here;
7561 /* Clear the entire vector first if there are any missing elements,
7562 or if the incidence of zero elements is >= 75%. */
7563 need_to_clear = (maybe_lt (count, n_elts)
7564 || 4 * zero_count >= 3 * count);
7567 if (need_to_clear && maybe_gt (size, 0) && !vector)
7569 if (REG_P (target))
7570 emit_move_insn (target, CONST0_RTX (mode));
7571 else
7572 clear_storage (target, gen_int_mode (size, Pmode),
7573 BLOCK_OP_NORMAL);
7574 cleared = 1;
7577 /* Inform later passes that the old value is dead. */
7578 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
7580 emit_move_insn (target, CONST0_RTX (mode));
7581 cleared = 1;
7584 if (MEM_P (target))
7585 alias = MEM_ALIAS_SET (target);
7586 else
7587 alias = get_alias_set (elttype);
7589 /* Store each element of the constructor into the corresponding
7590 element of TARGET, determined by counting the elements. */
7591 for (idx = 0, i = 0;
7592 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7593 idx++, i += bitsize / elt_size)
7595 HOST_WIDE_INT eltpos;
7596 tree value = ce->value;
7598 if (cleared && initializer_zerop (value))
7599 continue;
7601 if (ce->index)
7602 eltpos = tree_to_uhwi (ce->index);
7603 else
7604 eltpos = i;
7606 if (vector)
7608 if (vec_vec_init_p)
7610 gcc_assert (ce->index == NULL_TREE);
7611 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7612 eltpos = idx;
7614 else
7615 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7616 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7618 else
7620 machine_mode value_mode
7621 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7622 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7623 bitpos = eltpos * elt_size;
7624 store_constructor_field (target, bitsize, bitpos, 0,
7625 bitregion_end, value_mode,
7626 value, cleared, alias, reverse);
7630 if (vector)
7631 emit_insn (GEN_FCN (icode) (target,
7632 gen_rtx_PARALLEL (mode, vector)));
7633 break;
7636 default:
7637 gcc_unreachable ();
7641 /* Store the value of EXP (an expression tree)
7642 into a subfield of TARGET which has mode MODE and occupies
7643 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7644 If MODE is VOIDmode, it means that we are storing into a bit-field.
7646 BITREGION_START is bitpos of the first bitfield in this region.
7647 BITREGION_END is the bitpos of the ending bitfield in this region.
7648 These two fields are 0, if the C++ memory model does not apply,
7649 or we are not interested in keeping track of bitfield regions.
7651 Always return const0_rtx unless we have something particular to
7652 return.
7654 ALIAS_SET is the alias set for the destination. This value will
7655 (in general) be different from that for TARGET, since TARGET is a
7656 reference to the containing structure.
7658 If NONTEMPORAL is true, try generating a nontemporal store.
7660 If REVERSE is true, the store is to be done in reverse order. */
7662 static rtx
7663 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7664 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7665 machine_mode mode, tree exp,
7666 alias_set_type alias_set, bool nontemporal, bool reverse)
7668 if (TREE_CODE (exp) == ERROR_MARK)
7669 return const0_rtx;
7671 /* If we have nothing to store, do nothing unless the expression has
7672 side-effects. Don't do that for zero sized addressable lhs of
7673 calls. */
7674 if (known_eq (bitsize, 0)
7675 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7676 || TREE_CODE (exp) != CALL_EXPR))
7677 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7679 if (GET_CODE (target) == CONCAT)
7681 /* We're storing into a struct containing a single __complex. */
7683 gcc_assert (known_eq (bitpos, 0));
7684 return store_expr (exp, target, 0, nontemporal, reverse);
7687 /* If the structure is in a register or if the component
7688 is a bit field, we cannot use addressing to access it.
7689 Use bit-field techniques or SUBREG to store in it. */
7691 poly_int64 decl_bitsize;
7692 if (mode == VOIDmode
7693 || (mode != BLKmode && ! direct_store[(int) mode]
7694 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7695 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7696 || REG_P (target)
7697 || GET_CODE (target) == SUBREG
7698 /* If the field isn't aligned enough to store as an ordinary memref,
7699 store it as a bit field. */
7700 || (mode != BLKmode
7701 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7702 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7703 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7704 || !multiple_p (bitpos, BITS_PER_UNIT)))
7705 || (known_size_p (bitsize)
7706 && mode != BLKmode
7707 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7708 /* If the RHS and field are a constant size and the size of the
7709 RHS isn't the same size as the bitfield, we must use bitfield
7710 operations. */
7711 || (known_size_p (bitsize)
7712 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7713 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7714 bitsize)
7715 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7716 we will handle specially below. */
7717 && !(TREE_CODE (exp) == CONSTRUCTOR
7718 && multiple_p (bitsize, BITS_PER_UNIT))
7719 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7720 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7721 includes some extra padding. store_expr / expand_expr will in
7722 that case call get_inner_reference that will have the bitsize
7723 we check here and thus the block move will not clobber the
7724 padding that shouldn't be clobbered. In the future we could
7725 replace the TREE_ADDRESSABLE check with a check that
7726 get_base_address needs to live in memory. */
7727 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7728 || TREE_CODE (exp) != COMPONENT_REF
7729 || !multiple_p (bitsize, BITS_PER_UNIT)
7730 || !multiple_p (bitpos, BITS_PER_UNIT)
7731 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7732 &decl_bitsize)
7733 || maybe_ne (decl_bitsize, bitsize))
7734 /* A call with an addressable return type and return-slot
7735 optimization must not need bitfield operations but we must
7736 pass down the original target. */
7737 && (TREE_CODE (exp) != CALL_EXPR
7738 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7739 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7740 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7741 decl we must use bitfield operations. */
7742 || (known_size_p (bitsize)
7743 && TREE_CODE (exp) == MEM_REF
7744 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7745 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7746 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7747 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7749 rtx temp;
7750 gimple *nop_def;
7752 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7753 implies a mask operation. If the precision is the same size as
7754 the field we're storing into, that mask is redundant. This is
7755 particularly common with bit field assignments generated by the
7756 C front end. */
7757 nop_def = get_def_for_expr (exp, NOP_EXPR);
7758 if (nop_def)
7760 tree type = TREE_TYPE (exp);
7761 if (INTEGRAL_TYPE_P (type)
7762 && maybe_ne (TYPE_PRECISION (type),
7763 GET_MODE_BITSIZE (TYPE_MODE (type)))
7764 && known_eq (bitsize, TYPE_PRECISION (type)))
7766 tree op = gimple_assign_rhs1 (nop_def);
7767 type = TREE_TYPE (op);
7768 if (INTEGRAL_TYPE_P (type)
7769 && known_ge (TYPE_PRECISION (type), bitsize))
7770 exp = op;
7774 temp = expand_normal (exp);
7776 /* We don't support variable-sized BLKmode bitfields, since our
7777 handling of BLKmode is bound up with the ability to break
7778 things into words. */
7779 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7781 /* Handle calls that return values in multiple non-contiguous locations.
7782 The Irix 6 ABI has examples of this. */
7783 if (GET_CODE (temp) == PARALLEL)
7785 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7786 machine_mode temp_mode = GET_MODE (temp);
7787 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7788 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7789 rtx temp_target = gen_reg_rtx (temp_mode);
7790 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7791 temp = temp_target;
7794 /* Handle calls that return BLKmode values in registers. */
7795 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7797 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7798 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7799 temp = temp_target;
7802 /* If the value has aggregate type and an integral mode then, if BITSIZE
7803 is narrower than this mode and this is for big-endian data, we first
7804 need to put the value into the low-order bits for store_bit_field,
7805 except when MODE is BLKmode and BITSIZE larger than the word size
7806 (see the handling of fields larger than a word in store_bit_field).
7807 Moreover, the field may be not aligned on a byte boundary; in this
7808 case, if it has reverse storage order, it needs to be accessed as a
7809 scalar field with reverse storage order and we must first put the
7810 value into target order. */
7811 scalar_int_mode temp_mode;
7812 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7813 && is_int_mode (GET_MODE (temp), &temp_mode))
7815 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7817 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7819 if (reverse)
7820 temp = flip_storage_order (temp_mode, temp);
7822 gcc_checking_assert (known_le (bitsize, size));
7823 if (maybe_lt (bitsize, size)
7824 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7825 /* Use of to_constant for BLKmode was checked above. */
7826 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7827 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7828 size - bitsize, NULL_RTX, 1);
7831 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7832 if (mode != VOIDmode && mode != BLKmode
7833 && mode != TYPE_MODE (TREE_TYPE (exp)))
7834 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7836 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7837 and BITPOS must be aligned on a byte boundary. If so, we simply do
7838 a block copy. Likewise for a BLKmode-like TARGET. */
7839 if (GET_MODE (temp) == BLKmode
7840 && (GET_MODE (target) == BLKmode
7841 || (MEM_P (target)
7842 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7843 && multiple_p (bitpos, BITS_PER_UNIT)
7844 && multiple_p (bitsize, BITS_PER_UNIT))))
7846 gcc_assert (MEM_P (target) && MEM_P (temp));
7847 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7848 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7850 target = adjust_address (target, VOIDmode, bytepos);
7851 emit_block_move (target, temp,
7852 gen_int_mode (bytesize, Pmode),
7853 BLOCK_OP_NORMAL);
7855 return const0_rtx;
7858 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7859 word size, we need to load the value (see again store_bit_field). */
7860 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7862 temp_mode = smallest_int_mode_for_size (bitsize);
7863 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7864 temp_mode, false, NULL);
7867 /* Store the value in the bitfield. */
7868 gcc_checking_assert (known_ge (bitpos, 0));
7869 store_bit_field (target, bitsize, bitpos,
7870 bitregion_start, bitregion_end,
7871 mode, temp, reverse, false);
7873 return const0_rtx;
7875 else
7877 /* Now build a reference to just the desired component. */
7878 rtx to_rtx = adjust_address (target, mode,
7879 exact_div (bitpos, BITS_PER_UNIT));
7881 if (to_rtx == target)
7882 to_rtx = copy_rtx (to_rtx);
7884 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7885 set_mem_alias_set (to_rtx, alias_set);
7887 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7888 into a target smaller than its type; handle that case now. */
7889 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7891 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7892 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7893 return to_rtx;
7896 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7900 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7901 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7902 codes and find the ultimate containing object, which we return.
7904 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7905 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7906 storage order of the field.
7907 If the position of the field is variable, we store a tree
7908 giving the variable offset (in units) in *POFFSET.
7909 This offset is in addition to the bit position.
7910 If the position is not variable, we store 0 in *POFFSET.
7912 If any of the extraction expressions is volatile,
7913 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7915 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7916 Otherwise, it is a mode that can be used to access the field.
7918 If the field describes a variable-sized object, *PMODE is set to
7919 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7920 this case, but the address of the object can be found. */
7922 tree
7923 get_inner_reference (tree exp, poly_int64 *pbitsize,
7924 poly_int64 *pbitpos, tree *poffset,
7925 machine_mode *pmode, int *punsignedp,
7926 int *preversep, int *pvolatilep)
7928 tree size_tree = 0;
7929 machine_mode mode = VOIDmode;
7930 bool blkmode_bitfield = false;
7931 tree offset = size_zero_node;
7932 poly_offset_int bit_offset = 0;
7934 /* First get the mode, signedness, storage order and size. We do this from
7935 just the outermost expression. */
7936 *pbitsize = -1;
7937 if (TREE_CODE (exp) == COMPONENT_REF)
7939 tree field = TREE_OPERAND (exp, 1);
7940 size_tree = DECL_SIZE (field);
7941 if (flag_strict_volatile_bitfields > 0
7942 && TREE_THIS_VOLATILE (exp)
7943 && DECL_BIT_FIELD_TYPE (field)
7944 && DECL_MODE (field) != BLKmode)
7945 /* Volatile bitfields should be accessed in the mode of the
7946 field's type, not the mode computed based on the bit
7947 size. */
7948 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7949 else if (!DECL_BIT_FIELD (field))
7951 mode = DECL_MODE (field);
7952 /* For vector fields re-check the target flags, as DECL_MODE
7953 could have been set with different target flags than
7954 the current function has. */
7955 if (VECTOR_TYPE_P (TREE_TYPE (field))
7956 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7957 mode = TYPE_MODE (TREE_TYPE (field));
7959 else if (DECL_MODE (field) == BLKmode)
7960 blkmode_bitfield = true;
7962 *punsignedp = DECL_UNSIGNED (field);
7964 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7966 size_tree = TREE_OPERAND (exp, 1);
7967 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7968 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7970 /* For vector element types with the correct size of access or for
7971 vector typed accesses use the mode of the access type. */
7972 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7973 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7974 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7975 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7976 mode = TYPE_MODE (TREE_TYPE (exp));
7978 else
7980 mode = TYPE_MODE (TREE_TYPE (exp));
7981 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7983 if (mode == BLKmode)
7984 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7985 else
7986 *pbitsize = GET_MODE_BITSIZE (mode);
7989 if (size_tree != 0)
7991 if (! tree_fits_uhwi_p (size_tree))
7992 mode = BLKmode, *pbitsize = -1;
7993 else
7994 *pbitsize = tree_to_uhwi (size_tree);
7997 *preversep = reverse_storage_order_for_component_p (exp);
7999 /* Compute cumulative bit-offset for nested component-refs and array-refs,
8000 and find the ultimate containing object. */
8001 while (1)
8003 switch (TREE_CODE (exp))
8005 case BIT_FIELD_REF:
8006 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
8007 break;
8009 case COMPONENT_REF:
8011 tree field = TREE_OPERAND (exp, 1);
8012 tree this_offset = component_ref_field_offset (exp);
8014 /* If this field hasn't been filled in yet, don't go past it.
8015 This should only happen when folding expressions made during
8016 type construction. */
8017 if (this_offset == 0)
8018 break;
8020 offset = size_binop (PLUS_EXPR, offset, this_offset);
8021 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
8023 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
8025 break;
8027 case ARRAY_REF:
8028 case ARRAY_RANGE_REF:
8030 tree index = TREE_OPERAND (exp, 1);
8031 tree low_bound = array_ref_low_bound (exp);
8032 tree unit_size = array_ref_element_size (exp);
8034 /* We assume all arrays have sizes that are a multiple of a byte.
8035 First subtract the lower bound, if any, in the type of the
8036 index, then convert to sizetype and multiply by the size of
8037 the array element. */
8038 if (! integer_zerop (low_bound))
8039 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8040 index, low_bound);
8042 offset = size_binop (PLUS_EXPR, offset,
8043 size_binop (MULT_EXPR,
8044 fold_convert (sizetype, index),
8045 unit_size));
8047 break;
8049 case REALPART_EXPR:
8050 break;
8052 case IMAGPART_EXPR:
8053 bit_offset += *pbitsize;
8054 break;
8056 case VIEW_CONVERT_EXPR:
8057 break;
8059 case MEM_REF:
8060 /* Hand back the decl for MEM[&decl, off]. */
8061 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8063 tree off = TREE_OPERAND (exp, 1);
8064 if (!integer_zerop (off))
8066 poly_offset_int boff = mem_ref_offset (exp);
8067 boff <<= LOG2_BITS_PER_UNIT;
8068 bit_offset += boff;
8070 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8072 goto done;
8074 default:
8075 goto done;
8078 /* If any reference in the chain is volatile, the effect is volatile. */
8079 if (TREE_THIS_VOLATILE (exp))
8080 *pvolatilep = 1;
8082 exp = TREE_OPERAND (exp, 0);
8084 done:
8086 /* If OFFSET is constant, see if we can return the whole thing as a
8087 constant bit position. Make sure to handle overflow during
8088 this conversion. */
8089 if (poly_int_tree_p (offset))
8091 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8092 TYPE_PRECISION (sizetype));
8093 tem <<= LOG2_BITS_PER_UNIT;
8094 tem += bit_offset;
8095 if (tem.to_shwi (pbitpos))
8096 *poffset = offset = NULL_TREE;
8099 /* Otherwise, split it up. */
8100 if (offset)
8102 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8103 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8105 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8106 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8107 offset = size_binop (PLUS_EXPR, offset,
8108 build_int_cst (sizetype, bytes.force_shwi ()));
8111 *poffset = offset;
8114 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8115 if (mode == VOIDmode
8116 && blkmode_bitfield
8117 && multiple_p (*pbitpos, BITS_PER_UNIT)
8118 && multiple_p (*pbitsize, BITS_PER_UNIT))
8119 *pmode = BLKmode;
8120 else
8121 *pmode = mode;
8123 return exp;
8126 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8128 static unsigned HOST_WIDE_INT
8129 target_align (const_tree target)
8131 /* We might have a chain of nested references with intermediate misaligning
8132 bitfields components, so need to recurse to find out. */
8134 unsigned HOST_WIDE_INT this_align, outer_align;
8136 switch (TREE_CODE (target))
8138 case BIT_FIELD_REF:
8139 return 1;
8141 case COMPONENT_REF:
8142 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8143 outer_align = target_align (TREE_OPERAND (target, 0));
8144 return MIN (this_align, outer_align);
8146 case ARRAY_REF:
8147 case ARRAY_RANGE_REF:
8148 this_align = TYPE_ALIGN (TREE_TYPE (target));
8149 outer_align = target_align (TREE_OPERAND (target, 0));
8150 return MIN (this_align, outer_align);
8152 CASE_CONVERT:
8153 case NON_LVALUE_EXPR:
8154 case VIEW_CONVERT_EXPR:
8155 this_align = TYPE_ALIGN (TREE_TYPE (target));
8156 outer_align = target_align (TREE_OPERAND (target, 0));
8157 return MAX (this_align, outer_align);
8159 default:
8160 return TYPE_ALIGN (TREE_TYPE (target));
8165 /* Given an rtx VALUE that may contain additions and multiplications, return
8166 an equivalent value that just refers to a register, memory, or constant.
8167 This is done by generating instructions to perform the arithmetic and
8168 returning a pseudo-register containing the value.
8170 The returned value may be a REG, SUBREG, MEM or constant. */
8173 force_operand (rtx value, rtx target)
8175 rtx op1, op2;
8176 /* Use subtarget as the target for operand 0 of a binary operation. */
8177 rtx subtarget = get_subtarget (target);
8178 enum rtx_code code = GET_CODE (value);
8180 /* Check for subreg applied to an expression produced by loop optimizer. */
8181 if (code == SUBREG
8182 && !REG_P (SUBREG_REG (value))
8183 && !MEM_P (SUBREG_REG (value)))
8185 value
8186 = simplify_gen_subreg (GET_MODE (value),
8187 force_reg (GET_MODE (SUBREG_REG (value)),
8188 force_operand (SUBREG_REG (value),
8189 NULL_RTX)),
8190 GET_MODE (SUBREG_REG (value)),
8191 SUBREG_BYTE (value));
8192 code = GET_CODE (value);
8195 /* Check for a PIC address load. */
8196 if ((code == PLUS || code == MINUS)
8197 && XEXP (value, 0) == pic_offset_table_rtx
8198 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8199 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8200 || GET_CODE (XEXP (value, 1)) == CONST))
8202 if (!subtarget)
8203 subtarget = gen_reg_rtx (GET_MODE (value));
8204 emit_move_insn (subtarget, value);
8205 return subtarget;
8208 if (ARITHMETIC_P (value))
8210 op2 = XEXP (value, 1);
8211 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8212 subtarget = 0;
8213 if (code == MINUS && CONST_INT_P (op2))
8215 code = PLUS;
8216 op2 = negate_rtx (GET_MODE (value), op2);
8219 /* Check for an addition with OP2 a constant integer and our first
8220 operand a PLUS of a virtual register and something else. In that
8221 case, we want to emit the sum of the virtual register and the
8222 constant first and then add the other value. This allows virtual
8223 register instantiation to simply modify the constant rather than
8224 creating another one around this addition. */
8225 if (code == PLUS && CONST_INT_P (op2)
8226 && GET_CODE (XEXP (value, 0)) == PLUS
8227 && REG_P (XEXP (XEXP (value, 0), 0))
8228 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8230 rtx temp = expand_simple_binop (GET_MODE (value), code,
8231 XEXP (XEXP (value, 0), 0), op2,
8232 subtarget, 0, OPTAB_LIB_WIDEN);
8233 return expand_simple_binop (GET_MODE (value), code, temp,
8234 force_operand (XEXP (XEXP (value,
8235 0), 1), 0),
8236 target, 0, OPTAB_LIB_WIDEN);
8239 op1 = force_operand (XEXP (value, 0), subtarget);
8240 op2 = force_operand (op2, NULL_RTX);
8241 switch (code)
8243 case MULT:
8244 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8245 case DIV:
8246 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8247 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8248 target, 1, OPTAB_LIB_WIDEN);
8249 else
8250 return expand_divmod (0,
8251 FLOAT_MODE_P (GET_MODE (value))
8252 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8253 GET_MODE (value), op1, op2, target, 0);
8254 case MOD:
8255 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8256 target, 0);
8257 case UDIV:
8258 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8259 target, 1);
8260 case UMOD:
8261 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8262 target, 1);
8263 case ASHIFTRT:
8264 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8265 target, 0, OPTAB_LIB_WIDEN);
8266 default:
8267 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8268 target, 1, OPTAB_LIB_WIDEN);
8271 if (UNARY_P (value))
8273 if (!target)
8274 target = gen_reg_rtx (GET_MODE (value));
8275 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8276 switch (code)
8278 case ZERO_EXTEND:
8279 case SIGN_EXTEND:
8280 case TRUNCATE:
8281 case FLOAT_EXTEND:
8282 case FLOAT_TRUNCATE:
8283 convert_move (target, op1, code == ZERO_EXTEND);
8284 return target;
8286 case FIX:
8287 case UNSIGNED_FIX:
8288 expand_fix (target, op1, code == UNSIGNED_FIX);
8289 return target;
8291 case FLOAT:
8292 case UNSIGNED_FLOAT:
8293 expand_float (target, op1, code == UNSIGNED_FLOAT);
8294 return target;
8296 default:
8297 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8301 #ifdef INSN_SCHEDULING
8302 /* On machines that have insn scheduling, we want all memory reference to be
8303 explicit, so we need to deal with such paradoxical SUBREGs. */
8304 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8305 value
8306 = simplify_gen_subreg (GET_MODE (value),
8307 force_reg (GET_MODE (SUBREG_REG (value)),
8308 force_operand (SUBREG_REG (value),
8309 NULL_RTX)),
8310 GET_MODE (SUBREG_REG (value)),
8311 SUBREG_BYTE (value));
8312 #endif
8314 return value;
8317 /* Subroutine of expand_expr: return true iff there is no way that
8318 EXP can reference X, which is being modified. TOP_P is nonzero if this
8319 call is going to be used to determine whether we need a temporary
8320 for EXP, as opposed to a recursive call to this function.
8322 It is always safe for this routine to return false since it merely
8323 searches for optimization opportunities. */
8325 bool
8326 safe_from_p (const_rtx x, tree exp, int top_p)
8328 rtx exp_rtl = 0;
8329 int i, nops;
8331 if (x == 0
8332 /* If EXP has varying size, we MUST use a target since we currently
8333 have no way of allocating temporaries of variable size
8334 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8335 So we assume here that something at a higher level has prevented a
8336 clash. This is somewhat bogus, but the best we can do. Only
8337 do this when X is BLKmode and when we are at the top level. */
8338 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8339 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8340 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8341 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8342 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8343 != INTEGER_CST)
8344 && GET_MODE (x) == BLKmode)
8345 /* If X is in the outgoing argument area, it is always safe. */
8346 || (MEM_P (x)
8347 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8348 || (GET_CODE (XEXP (x, 0)) == PLUS
8349 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8350 return true;
8352 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8353 find the underlying pseudo. */
8354 if (GET_CODE (x) == SUBREG)
8356 x = SUBREG_REG (x);
8357 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8358 return false;
8361 /* Now look at our tree code and possibly recurse. */
8362 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8364 case tcc_declaration:
8365 exp_rtl = DECL_RTL_IF_SET (exp);
8366 break;
8368 case tcc_constant:
8369 return true;
8371 case tcc_exceptional:
8372 if (TREE_CODE (exp) == TREE_LIST)
8374 while (1)
8376 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8377 return false;
8378 exp = TREE_CHAIN (exp);
8379 if (!exp)
8380 return true;
8381 if (TREE_CODE (exp) != TREE_LIST)
8382 return safe_from_p (x, exp, 0);
8385 else if (TREE_CODE (exp) == CONSTRUCTOR)
8387 constructor_elt *ce;
8388 unsigned HOST_WIDE_INT idx;
8390 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8391 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8392 || !safe_from_p (x, ce->value, 0))
8393 return false;
8394 return true;
8396 else if (TREE_CODE (exp) == ERROR_MARK)
8397 return true; /* An already-visited SAVE_EXPR? */
8398 else
8399 return false;
8401 case tcc_statement:
8402 /* The only case we look at here is the DECL_INITIAL inside a
8403 DECL_EXPR. */
8404 return (TREE_CODE (exp) != DECL_EXPR
8405 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8406 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8407 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8409 case tcc_binary:
8410 case tcc_comparison:
8411 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8412 return false;
8413 /* Fall through. */
8415 case tcc_unary:
8416 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8418 case tcc_expression:
8419 case tcc_reference:
8420 case tcc_vl_exp:
8421 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8422 the expression. If it is set, we conflict iff we are that rtx or
8423 both are in memory. Otherwise, we check all operands of the
8424 expression recursively. */
8426 switch (TREE_CODE (exp))
8428 case ADDR_EXPR:
8429 /* If the operand is static or we are static, we can't conflict.
8430 Likewise if we don't conflict with the operand at all. */
8431 if (staticp (TREE_OPERAND (exp, 0))
8432 || TREE_STATIC (exp)
8433 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8434 return true;
8436 /* Otherwise, the only way this can conflict is if we are taking
8437 the address of a DECL a that address if part of X, which is
8438 very rare. */
8439 exp = TREE_OPERAND (exp, 0);
8440 if (DECL_P (exp))
8442 if (!DECL_RTL_SET_P (exp)
8443 || !MEM_P (DECL_RTL (exp)))
8444 return false;
8445 else
8446 exp_rtl = XEXP (DECL_RTL (exp), 0);
8448 break;
8450 case MEM_REF:
8451 if (MEM_P (x)
8452 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8453 get_alias_set (exp)))
8454 return false;
8455 break;
8457 case CALL_EXPR:
8458 /* Assume that the call will clobber all hard registers and
8459 all of memory. */
8460 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8461 || MEM_P (x))
8462 return false;
8463 break;
8465 case WITH_CLEANUP_EXPR:
8466 case CLEANUP_POINT_EXPR:
8467 /* Lowered by gimplify.cc. */
8468 gcc_unreachable ();
8470 case SAVE_EXPR:
8471 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8473 default:
8474 break;
8477 /* If we have an rtx, we do not need to scan our operands. */
8478 if (exp_rtl)
8479 break;
8481 nops = TREE_OPERAND_LENGTH (exp);
8482 for (i = 0; i < nops; i++)
8483 if (TREE_OPERAND (exp, i) != 0
8484 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8485 return false;
8487 break;
8489 case tcc_type:
8490 /* Should never get a type here. */
8491 gcc_unreachable ();
8494 /* If we have an rtl, find any enclosed object. Then see if we conflict
8495 with it. */
8496 if (exp_rtl)
8498 if (GET_CODE (exp_rtl) == SUBREG)
8500 exp_rtl = SUBREG_REG (exp_rtl);
8501 if (REG_P (exp_rtl)
8502 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8503 return false;
8506 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8507 are memory and they conflict. */
8508 return ! (rtx_equal_p (x, exp_rtl)
8509 || (MEM_P (x) && MEM_P (exp_rtl)
8510 && true_dependence (exp_rtl, VOIDmode, x)));
8513 /* If we reach here, it is safe. */
8514 return true;
8518 /* Return the highest power of two that EXP is known to be a multiple of.
8519 This is used in updating alignment of MEMs in array references. */
8521 unsigned HOST_WIDE_INT
8522 highest_pow2_factor (const_tree exp)
8524 unsigned HOST_WIDE_INT ret;
8525 int trailing_zeros = tree_ctz (exp);
8526 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8527 return BIGGEST_ALIGNMENT;
8528 ret = HOST_WIDE_INT_1U << trailing_zeros;
8529 if (ret > BIGGEST_ALIGNMENT)
8530 return BIGGEST_ALIGNMENT;
8531 return ret;
8534 /* Similar, except that the alignment requirements of TARGET are
8535 taken into account. Assume it is at least as aligned as its
8536 type, unless it is a COMPONENT_REF in which case the layout of
8537 the structure gives the alignment. */
8539 static unsigned HOST_WIDE_INT
8540 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8542 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8543 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8545 return MAX (factor, talign);
8548 /* Convert the tree comparison code TCODE to the rtl one where the
8549 signedness is UNSIGNEDP. */
8551 static enum rtx_code
8552 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8554 enum rtx_code code;
8555 switch (tcode)
8557 case EQ_EXPR:
8558 code = EQ;
8559 break;
8560 case NE_EXPR:
8561 code = NE;
8562 break;
8563 case LT_EXPR:
8564 code = unsignedp ? LTU : LT;
8565 break;
8566 case LE_EXPR:
8567 code = unsignedp ? LEU : LE;
8568 break;
8569 case GT_EXPR:
8570 code = unsignedp ? GTU : GT;
8571 break;
8572 case GE_EXPR:
8573 code = unsignedp ? GEU : GE;
8574 break;
8575 case UNORDERED_EXPR:
8576 code = UNORDERED;
8577 break;
8578 case ORDERED_EXPR:
8579 code = ORDERED;
8580 break;
8581 case UNLT_EXPR:
8582 code = UNLT;
8583 break;
8584 case UNLE_EXPR:
8585 code = UNLE;
8586 break;
8587 case UNGT_EXPR:
8588 code = UNGT;
8589 break;
8590 case UNGE_EXPR:
8591 code = UNGE;
8592 break;
8593 case UNEQ_EXPR:
8594 code = UNEQ;
8595 break;
8596 case LTGT_EXPR:
8597 code = LTGT;
8598 break;
8600 default:
8601 gcc_unreachable ();
8603 return code;
8606 /* Subroutine of expand_expr. Expand the two operands of a binary
8607 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8608 The value may be stored in TARGET if TARGET is nonzero. The
8609 MODIFIER argument is as documented by expand_expr. */
8611 void
8612 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8613 enum expand_modifier modifier)
8615 if (! safe_from_p (target, exp1, 1))
8616 target = 0;
8617 if (operand_equal_p (exp0, exp1, 0))
8619 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8620 *op1 = copy_rtx (*op0);
8622 else
8624 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8625 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8630 /* Return a MEM that contains constant EXP. DEFER is as for
8631 output_constant_def and MODIFIER is as for expand_expr. */
8633 static rtx
8634 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8636 rtx mem;
8638 mem = output_constant_def (exp, defer);
8639 if (modifier != EXPAND_INITIALIZER)
8640 mem = use_anchored_address (mem);
8641 return mem;
8644 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8645 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8647 static rtx
8648 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8649 enum expand_modifier modifier, addr_space_t as)
8651 rtx result, subtarget;
8652 tree inner, offset;
8653 poly_int64 bitsize, bitpos;
8654 int unsignedp, reversep, volatilep = 0;
8655 machine_mode mode1;
8657 /* If we are taking the address of a constant and are at the top level,
8658 we have to use output_constant_def since we can't call force_const_mem
8659 at top level. */
8660 /* ??? This should be considered a front-end bug. We should not be
8661 generating ADDR_EXPR of something that isn't an LVALUE. The only
8662 exception here is STRING_CST. */
8663 if (CONSTANT_CLASS_P (exp))
8665 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8666 if (modifier < EXPAND_SUM)
8667 result = force_operand (result, target);
8668 return result;
8671 /* Everything must be something allowed by is_gimple_addressable. */
8672 switch (TREE_CODE (exp))
8674 case INDIRECT_REF:
8675 /* This case will happen via recursion for &a->b. */
8676 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8678 case MEM_REF:
8680 tree tem = TREE_OPERAND (exp, 0);
8681 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8682 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8683 return expand_expr (tem, target, tmode, modifier);
8686 case TARGET_MEM_REF:
8687 return addr_for_mem_ref (exp, as, true);
8689 case CONST_DECL:
8690 /* Expand the initializer like constants above. */
8691 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8692 0, modifier), 0);
8693 if (modifier < EXPAND_SUM)
8694 result = force_operand (result, target);
8695 return result;
8697 case REALPART_EXPR:
8698 /* The real part of the complex number is always first, therefore
8699 the address is the same as the address of the parent object. */
8700 offset = 0;
8701 bitpos = 0;
8702 inner = TREE_OPERAND (exp, 0);
8703 break;
8705 case IMAGPART_EXPR:
8706 /* The imaginary part of the complex number is always second.
8707 The expression is therefore always offset by the size of the
8708 scalar type. */
8709 offset = 0;
8710 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8711 inner = TREE_OPERAND (exp, 0);
8712 break;
8714 case COMPOUND_LITERAL_EXPR:
8715 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8716 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8717 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8718 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8719 the initializers aren't gimplified. */
8720 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8721 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8722 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8723 target, tmode, modifier, as);
8724 /* FALLTHRU */
8725 default:
8726 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8727 expand_expr, as that can have various side effects; LABEL_DECLs for
8728 example, may not have their DECL_RTL set yet. Expand the rtl of
8729 CONSTRUCTORs too, which should yield a memory reference for the
8730 constructor's contents. Assume language specific tree nodes can
8731 be expanded in some interesting way. */
8732 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8733 if (DECL_P (exp)
8734 || TREE_CODE (exp) == CONSTRUCTOR
8735 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8737 result = expand_expr (exp, target, tmode,
8738 modifier == EXPAND_INITIALIZER
8739 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8741 /* If the DECL isn't in memory, then the DECL wasn't properly
8742 marked TREE_ADDRESSABLE, which will be either a front-end
8743 or a tree optimizer bug. */
8745 gcc_assert (MEM_P (result));
8746 result = XEXP (result, 0);
8748 /* ??? Is this needed anymore? */
8749 if (DECL_P (exp))
8750 TREE_USED (exp) = 1;
8752 if (modifier != EXPAND_INITIALIZER
8753 && modifier != EXPAND_CONST_ADDRESS
8754 && modifier != EXPAND_SUM)
8755 result = force_operand (result, target);
8756 return result;
8759 /* Pass FALSE as the last argument to get_inner_reference although
8760 we are expanding to RTL. The rationale is that we know how to
8761 handle "aligning nodes" here: we can just bypass them because
8762 they won't change the final object whose address will be returned
8763 (they actually exist only for that purpose). */
8764 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8765 &unsignedp, &reversep, &volatilep);
8766 break;
8769 /* We must have made progress. */
8770 gcc_assert (inner != exp);
8772 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8773 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8774 inner alignment, force the inner to be sufficiently aligned. */
8775 if (CONSTANT_CLASS_P (inner)
8776 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8778 inner = copy_node (inner);
8779 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8780 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8781 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8783 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8785 if (offset)
8787 rtx tmp;
8789 if (modifier != EXPAND_NORMAL)
8790 result = force_operand (result, NULL);
8791 tmp = expand_expr (offset, NULL_RTX, tmode,
8792 modifier == EXPAND_INITIALIZER
8793 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8795 /* expand_expr is allowed to return an object in a mode other
8796 than TMODE. If it did, we need to convert. */
8797 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8798 tmp = convert_modes (tmode, GET_MODE (tmp),
8799 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8800 result = convert_memory_address_addr_space (tmode, result, as);
8801 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8803 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8804 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8805 else
8807 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8808 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8809 1, OPTAB_LIB_WIDEN);
8813 if (maybe_ne (bitpos, 0))
8815 /* Someone beforehand should have rejected taking the address
8816 of an object that isn't byte-aligned. */
8817 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8818 result = convert_memory_address_addr_space (tmode, result, as);
8819 result = plus_constant (tmode, result, bytepos);
8820 if (modifier < EXPAND_SUM)
8821 result = force_operand (result, target);
8824 return result;
8827 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8828 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8830 static rtx
8831 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8832 enum expand_modifier modifier)
8834 addr_space_t as = ADDR_SPACE_GENERIC;
8835 scalar_int_mode address_mode = Pmode;
8836 scalar_int_mode pointer_mode = ptr_mode;
8837 machine_mode rmode;
8838 rtx result;
8840 /* Target mode of VOIDmode says "whatever's natural". */
8841 if (tmode == VOIDmode)
8842 tmode = TYPE_MODE (TREE_TYPE (exp));
8844 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8846 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8847 address_mode = targetm.addr_space.address_mode (as);
8848 pointer_mode = targetm.addr_space.pointer_mode (as);
8851 /* We can get called with some Weird Things if the user does silliness
8852 like "(short) &a". In that case, convert_memory_address won't do
8853 the right thing, so ignore the given target mode. */
8854 scalar_int_mode new_tmode = (tmode == pointer_mode
8855 ? pointer_mode
8856 : address_mode);
8858 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8859 new_tmode, modifier, as);
8861 /* Despite expand_expr claims concerning ignoring TMODE when not
8862 strictly convenient, stuff breaks if we don't honor it. Note
8863 that combined with the above, we only do this for pointer modes. */
8864 rmode = GET_MODE (result);
8865 if (rmode == VOIDmode)
8866 rmode = new_tmode;
8867 if (rmode != new_tmode)
8868 result = convert_memory_address_addr_space (new_tmode, result, as);
8870 return result;
8873 /* Generate code for computing CONSTRUCTOR EXP.
8874 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8875 is TRUE, instead of creating a temporary variable in memory
8876 NULL is returned and the caller needs to handle it differently. */
8878 static rtx
8879 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8880 bool avoid_temp_mem)
8882 tree type = TREE_TYPE (exp);
8883 machine_mode mode = TYPE_MODE (type);
8885 /* Try to avoid creating a temporary at all. This is possible
8886 if all of the initializer is zero.
8887 FIXME: try to handle all [0..255] initializers we can handle
8888 with memset. */
8889 if (TREE_STATIC (exp)
8890 && !TREE_ADDRESSABLE (exp)
8891 && target != 0 && mode == BLKmode
8892 && all_zeros_p (exp))
8894 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8895 return target;
8898 /* All elts simple constants => refer to a constant in memory. But
8899 if this is a non-BLKmode mode, let it store a field at a time
8900 since that should make a CONST_INT, CONST_WIDE_INT or
8901 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8902 use, it is best to store directly into the target unless the type
8903 is large enough that memcpy will be used. If we are making an
8904 initializer and all operands are constant, put it in memory as
8905 well.
8907 FIXME: Avoid trying to fill vector constructors piece-meal.
8908 Output them with output_constant_def below unless we're sure
8909 they're zeros. This should go away when vector initializers
8910 are treated like VECTOR_CST instead of arrays. */
8911 if ((TREE_STATIC (exp)
8912 && ((mode == BLKmode
8913 && ! (target != 0 && safe_from_p (target, exp, 1)))
8914 || TREE_ADDRESSABLE (exp)
8915 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8916 && (! can_move_by_pieces
8917 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8918 TYPE_ALIGN (type)))
8919 && ! mostly_zeros_p (exp))))
8920 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8921 && TREE_CONSTANT (exp)))
8923 rtx constructor;
8925 if (avoid_temp_mem)
8926 return NULL_RTX;
8928 constructor = expand_expr_constant (exp, 1, modifier);
8930 if (modifier != EXPAND_CONST_ADDRESS
8931 && modifier != EXPAND_INITIALIZER
8932 && modifier != EXPAND_SUM)
8933 constructor = validize_mem (constructor);
8935 return constructor;
8938 /* If the CTOR is available in static storage and not mostly
8939 zeros and we can move it by pieces prefer to do so since
8940 that's usually more efficient than performing a series of
8941 stores from immediates. */
8942 if (avoid_temp_mem
8943 && TREE_STATIC (exp)
8944 && TREE_CONSTANT (exp)
8945 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8946 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8947 TYPE_ALIGN (type))
8948 && ! mostly_zeros_p (exp))
8949 return NULL_RTX;
8951 /* Handle calls that pass values in multiple non-contiguous
8952 locations. The Irix 6 ABI has examples of this. */
8953 if (target == 0 || ! safe_from_p (target, exp, 1)
8954 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8955 /* Also make a temporary if the store is to volatile memory, to
8956 avoid individual accesses to aggregate members. */
8957 || (GET_CODE (target) == MEM
8958 && MEM_VOLATILE_P (target)
8959 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8961 if (avoid_temp_mem)
8962 return NULL_RTX;
8964 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8967 store_constructor (exp, target, 0, int_expr_size (exp), false);
8968 return target;
8972 /* expand_expr: generate code for computing expression EXP.
8973 An rtx for the computed value is returned. The value is never null.
8974 In the case of a void EXP, const0_rtx is returned.
8976 The value may be stored in TARGET if TARGET is nonzero.
8977 TARGET is just a suggestion; callers must assume that
8978 the rtx returned may not be the same as TARGET.
8980 If TARGET is CONST0_RTX, it means that the value will be ignored.
8982 If TMODE is not VOIDmode, it suggests generating the
8983 result in mode TMODE. But this is done only when convenient.
8984 Otherwise, TMODE is ignored and the value generated in its natural mode.
8985 TMODE is just a suggestion; callers must assume that
8986 the rtx returned may not have mode TMODE.
8988 Note that TARGET may have neither TMODE nor MODE. In that case, it
8989 probably will not be used.
8991 If MODIFIER is EXPAND_SUM then when EXP is an addition
8992 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8993 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8994 products as above, or REG or MEM, or constant.
8995 Ordinarily in such cases we would output mul or add instructions
8996 and then return a pseudo reg containing the sum.
8998 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8999 it also marks a label as absolutely required (it can't be dead).
9000 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
9001 This is used for outputting expressions used in initializers.
9003 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
9004 with a constant address even if that address is not normally legitimate.
9005 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
9007 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
9008 a call parameter. Such targets require special care as we haven't yet
9009 marked TARGET so that it's safe from being trashed by libcalls. We
9010 don't want to use TARGET for anything but the final result;
9011 Intermediate values must go elsewhere. Additionally, calls to
9012 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
9014 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
9015 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
9016 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
9017 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
9018 recursively.
9019 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9020 then *ALT_RTL is set to TARGET (before legitimziation).
9022 If INNER_REFERENCE_P is true, we are expanding an inner reference.
9023 In this case, we don't adjust a returned MEM rtx that wouldn't be
9024 sufficiently aligned for its mode; instead, it's up to the caller
9025 to deal with it afterwards. This is used to make sure that unaligned
9026 base objects for which out-of-bounds accesses are supported, for
9027 example record types with trailing arrays, aren't realigned behind
9028 the back of the caller.
9029 The normal operating mode is to pass FALSE for this parameter. */
9032 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9033 enum expand_modifier modifier, rtx *alt_rtl,
9034 bool inner_reference_p)
9036 rtx ret;
9038 /* Handle ERROR_MARK before anybody tries to access its type. */
9039 if (TREE_CODE (exp) == ERROR_MARK
9040 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9042 ret = CONST0_RTX (tmode);
9043 return ret ? ret : const0_rtx;
9046 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9047 inner_reference_p);
9048 return ret;
9051 /* Try to expand the conditional expression which is represented by
9052 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9053 return the rtl reg which represents the result. Otherwise return
9054 NULL_RTX. */
9056 static rtx
9057 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9058 tree treeop1 ATTRIBUTE_UNUSED,
9059 tree treeop2 ATTRIBUTE_UNUSED)
9061 rtx insn;
9062 rtx op00, op01, op1, op2;
9063 enum rtx_code comparison_code;
9064 machine_mode comparison_mode;
9065 gimple *srcstmt;
9066 rtx temp;
9067 tree type = TREE_TYPE (treeop1);
9068 int unsignedp = TYPE_UNSIGNED (type);
9069 machine_mode mode = TYPE_MODE (type);
9070 machine_mode orig_mode = mode;
9071 static bool expanding_cond_expr_using_cmove = false;
9073 /* Conditional move expansion can end up TERing two operands which,
9074 when recursively hitting conditional expressions can result in
9075 exponential behavior if the cmove expansion ultimatively fails.
9076 It's hardly profitable to TER a cmove into a cmove so avoid doing
9077 that by failing early if we end up recursing. */
9078 if (expanding_cond_expr_using_cmove)
9079 return NULL_RTX;
9081 /* If we cannot do a conditional move on the mode, try doing it
9082 with the promoted mode. */
9083 if (!can_conditionally_move_p (mode))
9085 mode = promote_mode (type, mode, &unsignedp);
9086 if (!can_conditionally_move_p (mode))
9087 return NULL_RTX;
9088 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9090 else
9091 temp = assign_temp (type, 0, 1);
9093 expanding_cond_expr_using_cmove = true;
9094 start_sequence ();
9095 expand_operands (treeop1, treeop2,
9096 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9097 EXPAND_NORMAL);
9099 if (TREE_CODE (treeop0) == SSA_NAME
9100 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9102 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9103 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9104 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9105 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9106 comparison_mode = TYPE_MODE (type);
9107 unsignedp = TYPE_UNSIGNED (type);
9108 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9110 else if (COMPARISON_CLASS_P (treeop0))
9112 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9113 enum tree_code cmpcode = TREE_CODE (treeop0);
9114 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9115 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9116 unsignedp = TYPE_UNSIGNED (type);
9117 comparison_mode = TYPE_MODE (type);
9118 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9120 else
9122 op00 = expand_normal (treeop0);
9123 op01 = const0_rtx;
9124 comparison_code = NE;
9125 comparison_mode = GET_MODE (op00);
9126 if (comparison_mode == VOIDmode)
9127 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9129 expanding_cond_expr_using_cmove = false;
9131 if (GET_MODE (op1) != mode)
9132 op1 = gen_lowpart (mode, op1);
9134 if (GET_MODE (op2) != mode)
9135 op2 = gen_lowpart (mode, op2);
9137 /* Try to emit the conditional move. */
9138 insn = emit_conditional_move (temp,
9139 { comparison_code, op00, op01,
9140 comparison_mode },
9141 op1, op2, mode,
9142 unsignedp);
9144 /* If we could do the conditional move, emit the sequence,
9145 and return. */
9146 if (insn)
9148 rtx_insn *seq = get_insns ();
9149 end_sequence ();
9150 emit_insn (seq);
9151 return convert_modes (orig_mode, mode, temp, 0);
9154 /* Otherwise discard the sequence and fall back to code with
9155 branches. */
9156 end_sequence ();
9157 return NULL_RTX;
9160 /* A helper function for expand_expr_real_2 to be used with a
9161 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9162 is nonzero, with alignment ALIGN in bits.
9163 Store the value at TARGET if possible (if TARGET is nonzero).
9164 Regardless of TARGET, we return the rtx for where the value is placed.
9165 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9166 then *ALT_RTL is set to TARGET (before legitimziation). */
9168 static rtx
9169 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9170 unsigned int align, rtx target, rtx *alt_rtl)
9172 enum insn_code icode;
9174 if ((icode = optab_handler (movmisalign_optab, mode))
9175 != CODE_FOR_nothing)
9177 class expand_operand ops[2];
9179 /* We've already validated the memory, and we're creating a
9180 new pseudo destination. The predicates really can't fail,
9181 nor can the generator. */
9182 create_output_operand (&ops[0], NULL_RTX, mode);
9183 create_fixed_operand (&ops[1], temp);
9184 expand_insn (icode, 2, ops);
9185 temp = ops[0].value;
9187 else if (targetm.slow_unaligned_access (mode, align))
9188 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9189 0, unsignedp, target,
9190 mode, mode, false, alt_rtl);
9191 return temp;
9194 /* Helper function of expand_expr_2, expand a division or modulo.
9195 op0 and op1 should be already expanded treeop0 and treeop1, using
9196 expand_operands. */
9198 static rtx
9199 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9200 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9202 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9203 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9204 if (SCALAR_INT_MODE_P (mode)
9205 && optimize >= 2
9206 && get_range_pos_neg (treeop0) == 1
9207 && get_range_pos_neg (treeop1) == 1)
9209 /* If both arguments are known to be positive when interpreted
9210 as signed, we can expand it as both signed and unsigned
9211 division or modulo. Choose the cheaper sequence in that case. */
9212 bool speed_p = optimize_insn_for_speed_p ();
9213 do_pending_stack_adjust ();
9214 start_sequence ();
9215 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9216 rtx_insn *uns_insns = get_insns ();
9217 end_sequence ();
9218 start_sequence ();
9219 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9220 rtx_insn *sgn_insns = get_insns ();
9221 end_sequence ();
9222 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9223 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9225 /* If costs are the same then use as tie breaker the other other
9226 factor. */
9227 if (uns_cost == sgn_cost)
9229 uns_cost = seq_cost (uns_insns, !speed_p);
9230 sgn_cost = seq_cost (sgn_insns, !speed_p);
9233 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9235 emit_insn (uns_insns);
9236 return uns_ret;
9238 emit_insn (sgn_insns);
9239 return sgn_ret;
9241 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9245 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9246 enum expand_modifier modifier)
9248 rtx op0, op1, op2, temp;
9249 rtx_code_label *lab;
9250 tree type;
9251 int unsignedp;
9252 machine_mode mode;
9253 scalar_int_mode int_mode;
9254 enum tree_code code = ops->code;
9255 optab this_optab;
9256 rtx subtarget, original_target;
9257 int ignore;
9258 bool reduce_bit_field;
9259 location_t loc = ops->location;
9260 tree treeop0, treeop1, treeop2;
9261 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9262 ? reduce_to_bit_field_precision ((expr), \
9263 target, \
9264 type) \
9265 : (expr))
9267 type = ops->type;
9268 mode = TYPE_MODE (type);
9269 unsignedp = TYPE_UNSIGNED (type);
9271 treeop0 = ops->op0;
9272 treeop1 = ops->op1;
9273 treeop2 = ops->op2;
9275 /* We should be called only on simple (binary or unary) expressions,
9276 exactly those that are valid in gimple expressions that aren't
9277 GIMPLE_SINGLE_RHS (or invalid). */
9278 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9279 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9280 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9282 ignore = (target == const0_rtx
9283 || ((CONVERT_EXPR_CODE_P (code)
9284 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9285 && TREE_CODE (type) == VOID_TYPE));
9287 /* We should be called only if we need the result. */
9288 gcc_assert (!ignore);
9290 /* An operation in what may be a bit-field type needs the
9291 result to be reduced to the precision of the bit-field type,
9292 which is narrower than that of the type's mode. */
9293 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9294 && !type_has_mode_precision_p (type));
9296 if (reduce_bit_field
9297 && (modifier == EXPAND_STACK_PARM
9298 || (target && GET_MODE (target) != mode)))
9299 target = 0;
9301 /* Use subtarget as the target for operand 0 of a binary operation. */
9302 subtarget = get_subtarget (target);
9303 original_target = target;
9305 switch (code)
9307 case NON_LVALUE_EXPR:
9308 case PAREN_EXPR:
9309 CASE_CONVERT:
9310 if (treeop0 == error_mark_node)
9311 return const0_rtx;
9313 if (TREE_CODE (type) == UNION_TYPE)
9315 tree valtype = TREE_TYPE (treeop0);
9317 /* If both input and output are BLKmode, this conversion isn't doing
9318 anything except possibly changing memory attribute. */
9319 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9321 rtx result = expand_expr (treeop0, target, tmode,
9322 modifier);
9324 result = copy_rtx (result);
9325 set_mem_attributes (result, type, 0);
9326 return result;
9329 if (target == 0)
9331 if (TYPE_MODE (type) != BLKmode)
9332 target = gen_reg_rtx (TYPE_MODE (type));
9333 else
9334 target = assign_temp (type, 1, 1);
9337 if (MEM_P (target))
9338 /* Store data into beginning of memory target. */
9339 store_expr (treeop0,
9340 adjust_address (target, TYPE_MODE (valtype), 0),
9341 modifier == EXPAND_STACK_PARM,
9342 false, TYPE_REVERSE_STORAGE_ORDER (type));
9344 else
9346 gcc_assert (REG_P (target)
9347 && !TYPE_REVERSE_STORAGE_ORDER (type));
9349 /* Store this field into a union of the proper type. */
9350 poly_uint64 op0_size
9351 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9352 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9353 store_field (target,
9354 /* The conversion must be constructed so that
9355 we know at compile time how many bits
9356 to preserve. */
9357 ordered_min (op0_size, union_size),
9358 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9359 false, false);
9362 /* Return the entire union. */
9363 return target;
9366 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9368 op0 = expand_expr (treeop0, target, VOIDmode,
9369 modifier);
9371 return REDUCE_BIT_FIELD (op0);
9374 op0 = expand_expr (treeop0, NULL_RTX, mode,
9375 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9376 if (GET_MODE (op0) == mode)
9379 /* If OP0 is a constant, just convert it into the proper mode. */
9380 else if (CONSTANT_P (op0))
9382 tree inner_type = TREE_TYPE (treeop0);
9383 machine_mode inner_mode = GET_MODE (op0);
9385 if (inner_mode == VOIDmode)
9386 inner_mode = TYPE_MODE (inner_type);
9388 if (modifier == EXPAND_INITIALIZER)
9389 op0 = lowpart_subreg (mode, op0, inner_mode);
9390 else
9391 op0= convert_modes (mode, inner_mode, op0,
9392 TYPE_UNSIGNED (inner_type));
9395 else if (modifier == EXPAND_INITIALIZER)
9396 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9397 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9399 else if (target == 0)
9400 op0 = convert_to_mode (mode, op0,
9401 TYPE_UNSIGNED (TREE_TYPE
9402 (treeop0)));
9403 else
9405 convert_move (target, op0,
9406 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9407 op0 = target;
9410 return REDUCE_BIT_FIELD (op0);
9412 case ADDR_SPACE_CONVERT_EXPR:
9414 tree treeop0_type = TREE_TYPE (treeop0);
9416 gcc_assert (POINTER_TYPE_P (type));
9417 gcc_assert (POINTER_TYPE_P (treeop0_type));
9419 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9420 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9422 /* Conversions between pointers to the same address space should
9423 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9424 gcc_assert (as_to != as_from);
9426 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9428 /* Ask target code to handle conversion between pointers
9429 to overlapping address spaces. */
9430 if (targetm.addr_space.subset_p (as_to, as_from)
9431 || targetm.addr_space.subset_p (as_from, as_to))
9433 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9435 else
9437 /* For disjoint address spaces, converting anything but a null
9438 pointer invokes undefined behavior. We truncate or extend the
9439 value as if we'd converted via integers, which handles 0 as
9440 required, and all others as the programmer likely expects. */
9441 #ifndef POINTERS_EXTEND_UNSIGNED
9442 const int POINTERS_EXTEND_UNSIGNED = 1;
9443 #endif
9444 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9445 op0, POINTERS_EXTEND_UNSIGNED);
9447 gcc_assert (op0);
9448 return op0;
9451 case POINTER_PLUS_EXPR:
9452 /* Even though the sizetype mode and the pointer's mode can be different
9453 expand is able to handle this correctly and get the correct result out
9454 of the PLUS_EXPR code. */
9455 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9456 if sizetype precision is smaller than pointer precision. */
9457 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9458 treeop1 = fold_convert_loc (loc, type,
9459 fold_convert_loc (loc, ssizetype,
9460 treeop1));
9461 /* If sizetype precision is larger than pointer precision, truncate the
9462 offset to have matching modes. */
9463 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9464 treeop1 = fold_convert_loc (loc, type, treeop1);
9465 /* FALLTHRU */
9467 case PLUS_EXPR:
9468 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9469 something else, make sure we add the register to the constant and
9470 then to the other thing. This case can occur during strength
9471 reduction and doing it this way will produce better code if the
9472 frame pointer or argument pointer is eliminated.
9474 fold-const.cc will ensure that the constant is always in the inner
9475 PLUS_EXPR, so the only case we need to do anything about is if
9476 sp, ap, or fp is our second argument, in which case we must swap
9477 the innermost first argument and our second argument. */
9479 if (TREE_CODE (treeop0) == PLUS_EXPR
9480 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9481 && VAR_P (treeop1)
9482 && (DECL_RTL (treeop1) == frame_pointer_rtx
9483 || DECL_RTL (treeop1) == stack_pointer_rtx
9484 || DECL_RTL (treeop1) == arg_pointer_rtx))
9486 gcc_unreachable ();
9489 /* If the result is to be ptr_mode and we are adding an integer to
9490 something, we might be forming a constant. So try to use
9491 plus_constant. If it produces a sum and we can't accept it,
9492 use force_operand. This allows P = &ARR[const] to generate
9493 efficient code on machines where a SYMBOL_REF is not a valid
9494 address.
9496 If this is an EXPAND_SUM call, always return the sum. */
9497 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9498 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9500 if (modifier == EXPAND_STACK_PARM)
9501 target = 0;
9502 if (TREE_CODE (treeop0) == INTEGER_CST
9503 && HWI_COMPUTABLE_MODE_P (mode)
9504 && TREE_CONSTANT (treeop1))
9506 rtx constant_part;
9507 HOST_WIDE_INT wc;
9508 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9510 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9511 EXPAND_SUM);
9512 /* Use wi::shwi to ensure that the constant is
9513 truncated according to the mode of OP1, then sign extended
9514 to a HOST_WIDE_INT. Using the constant directly can result
9515 in non-canonical RTL in a 64x32 cross compile. */
9516 wc = TREE_INT_CST_LOW (treeop0);
9517 constant_part =
9518 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9519 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9520 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9521 op1 = force_operand (op1, target);
9522 return REDUCE_BIT_FIELD (op1);
9525 else if (TREE_CODE (treeop1) == INTEGER_CST
9526 && HWI_COMPUTABLE_MODE_P (mode)
9527 && TREE_CONSTANT (treeop0))
9529 rtx constant_part;
9530 HOST_WIDE_INT wc;
9531 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9533 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9534 (modifier == EXPAND_INITIALIZER
9535 ? EXPAND_INITIALIZER : EXPAND_SUM));
9536 if (! CONSTANT_P (op0))
9538 op1 = expand_expr (treeop1, NULL_RTX,
9539 VOIDmode, modifier);
9540 /* Return a PLUS if modifier says it's OK. */
9541 if (modifier == EXPAND_SUM
9542 || modifier == EXPAND_INITIALIZER)
9543 return simplify_gen_binary (PLUS, mode, op0, op1);
9544 goto binop2;
9546 /* Use wi::shwi to ensure that the constant is
9547 truncated according to the mode of OP1, then sign extended
9548 to a HOST_WIDE_INT. Using the constant directly can result
9549 in non-canonical RTL in a 64x32 cross compile. */
9550 wc = TREE_INT_CST_LOW (treeop1);
9551 constant_part
9552 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9553 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9554 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9555 op0 = force_operand (op0, target);
9556 return REDUCE_BIT_FIELD (op0);
9560 /* Use TER to expand pointer addition of a negated value
9561 as pointer subtraction. */
9562 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9563 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9564 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9565 && TREE_CODE (treeop1) == SSA_NAME
9566 && TYPE_MODE (TREE_TYPE (treeop0))
9567 == TYPE_MODE (TREE_TYPE (treeop1)))
9569 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9570 if (def)
9572 treeop1 = gimple_assign_rhs1 (def);
9573 code = MINUS_EXPR;
9574 goto do_minus;
9578 /* No sense saving up arithmetic to be done
9579 if it's all in the wrong mode to form part of an address.
9580 And force_operand won't know whether to sign-extend or
9581 zero-extend. */
9582 if (modifier != EXPAND_INITIALIZER
9583 && (modifier != EXPAND_SUM || mode != ptr_mode))
9585 expand_operands (treeop0, treeop1,
9586 subtarget, &op0, &op1, modifier);
9587 if (op0 == const0_rtx)
9588 return op1;
9589 if (op1 == const0_rtx)
9590 return op0;
9591 goto binop2;
9594 expand_operands (treeop0, treeop1,
9595 subtarget, &op0, &op1, modifier);
9596 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9598 case MINUS_EXPR:
9599 case POINTER_DIFF_EXPR:
9600 do_minus:
9601 /* For initializers, we are allowed to return a MINUS of two
9602 symbolic constants. Here we handle all cases when both operands
9603 are constant. */
9604 /* Handle difference of two symbolic constants,
9605 for the sake of an initializer. */
9606 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9607 && really_constant_p (treeop0)
9608 && really_constant_p (treeop1))
9610 expand_operands (treeop0, treeop1,
9611 NULL_RTX, &op0, &op1, modifier);
9612 return simplify_gen_binary (MINUS, mode, op0, op1);
9615 /* No sense saving up arithmetic to be done
9616 if it's all in the wrong mode to form part of an address.
9617 And force_operand won't know whether to sign-extend or
9618 zero-extend. */
9619 if (modifier != EXPAND_INITIALIZER
9620 && (modifier != EXPAND_SUM || mode != ptr_mode))
9621 goto binop;
9623 expand_operands (treeop0, treeop1,
9624 subtarget, &op0, &op1, modifier);
9626 /* Convert A - const to A + (-const). */
9627 if (CONST_INT_P (op1))
9629 op1 = negate_rtx (mode, op1);
9630 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9633 goto binop2;
9635 case WIDEN_MULT_PLUS_EXPR:
9636 case WIDEN_MULT_MINUS_EXPR:
9637 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9638 op2 = expand_normal (treeop2);
9639 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9640 target, unsignedp);
9641 return target;
9643 case WIDEN_MULT_EXPR:
9644 /* If first operand is constant, swap them.
9645 Thus the following special case checks need only
9646 check the second operand. */
9647 if (TREE_CODE (treeop0) == INTEGER_CST)
9648 std::swap (treeop0, treeop1);
9650 /* First, check if we have a multiplication of one signed and one
9651 unsigned operand. */
9652 if (TREE_CODE (treeop1) != INTEGER_CST
9653 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9654 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9656 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9657 this_optab = usmul_widen_optab;
9658 if (find_widening_optab_handler (this_optab, mode, innermode)
9659 != CODE_FOR_nothing)
9661 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9662 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9663 EXPAND_NORMAL);
9664 else
9665 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9666 EXPAND_NORMAL);
9667 /* op0 and op1 might still be constant, despite the above
9668 != INTEGER_CST check. Handle it. */
9669 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9671 op0 = convert_modes (mode, innermode, op0, true);
9672 op1 = convert_modes (mode, innermode, op1, false);
9673 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9674 target, unsignedp));
9676 goto binop3;
9679 /* Check for a multiplication with matching signedness. */
9680 else if ((TREE_CODE (treeop1) == INTEGER_CST
9681 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9682 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9683 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9685 tree op0type = TREE_TYPE (treeop0);
9686 machine_mode innermode = TYPE_MODE (op0type);
9687 bool zextend_p = TYPE_UNSIGNED (op0type);
9688 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9689 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9691 if (TREE_CODE (treeop0) != INTEGER_CST)
9693 if (find_widening_optab_handler (this_optab, mode, innermode)
9694 != CODE_FOR_nothing)
9696 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9697 EXPAND_NORMAL);
9698 /* op0 and op1 might still be constant, despite the above
9699 != INTEGER_CST check. Handle it. */
9700 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9702 widen_mult_const:
9703 op0 = convert_modes (mode, innermode, op0, zextend_p);
9705 = convert_modes (mode, innermode, op1,
9706 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9707 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9708 target,
9709 unsignedp));
9711 temp = expand_widening_mult (mode, op0, op1, target,
9712 unsignedp, this_optab);
9713 return REDUCE_BIT_FIELD (temp);
9715 if (find_widening_optab_handler (other_optab, mode, innermode)
9716 != CODE_FOR_nothing
9717 && innermode == word_mode)
9719 rtx htem, hipart;
9720 op0 = expand_normal (treeop0);
9721 op1 = expand_normal (treeop1);
9722 /* op0 and op1 might be constants, despite the above
9723 != INTEGER_CST check. Handle it. */
9724 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9725 goto widen_mult_const;
9726 temp = expand_binop (mode, other_optab, op0, op1, target,
9727 unsignedp, OPTAB_LIB_WIDEN);
9728 hipart = gen_highpart (word_mode, temp);
9729 htem = expand_mult_highpart_adjust (word_mode, hipart,
9730 op0, op1, hipart,
9731 zextend_p);
9732 if (htem != hipart)
9733 emit_move_insn (hipart, htem);
9734 return REDUCE_BIT_FIELD (temp);
9738 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9739 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9740 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9741 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9743 case MULT_EXPR:
9744 /* If this is a fixed-point operation, then we cannot use the code
9745 below because "expand_mult" doesn't support sat/no-sat fixed-point
9746 multiplications. */
9747 if (ALL_FIXED_POINT_MODE_P (mode))
9748 goto binop;
9750 /* If first operand is constant, swap them.
9751 Thus the following special case checks need only
9752 check the second operand. */
9753 if (TREE_CODE (treeop0) == INTEGER_CST)
9754 std::swap (treeop0, treeop1);
9756 /* Attempt to return something suitable for generating an
9757 indexed address, for machines that support that. */
9759 if (modifier == EXPAND_SUM && mode == ptr_mode
9760 && tree_fits_shwi_p (treeop1))
9762 tree exp1 = treeop1;
9764 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9765 EXPAND_SUM);
9767 if (!REG_P (op0))
9768 op0 = force_operand (op0, NULL_RTX);
9769 if (!REG_P (op0))
9770 op0 = copy_to_mode_reg (mode, op0);
9772 op1 = gen_int_mode (tree_to_shwi (exp1),
9773 TYPE_MODE (TREE_TYPE (exp1)));
9774 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9777 if (modifier == EXPAND_STACK_PARM)
9778 target = 0;
9780 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9782 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9783 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9784 if (def_stmt0
9785 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9786 def_stmt0 = NULL;
9787 if (def_stmt1
9788 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9789 def_stmt1 = NULL;
9791 if (def_stmt0 || def_stmt1)
9793 /* X / Y * Y can be expanded as X - X % Y too.
9794 Choose the cheaper sequence of those two. */
9795 if (def_stmt0)
9796 treeop0 = gimple_assign_rhs1 (def_stmt0);
9797 else
9799 treeop1 = treeop0;
9800 treeop0 = gimple_assign_rhs1 (def_stmt1);
9802 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9803 EXPAND_NORMAL);
9804 bool speed_p = optimize_insn_for_speed_p ();
9805 do_pending_stack_adjust ();
9806 start_sequence ();
9807 rtx divmul_ret
9808 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9809 op0, op1, NULL_RTX, unsignedp);
9810 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9811 unsignedp);
9812 rtx_insn *divmul_insns = get_insns ();
9813 end_sequence ();
9814 start_sequence ();
9815 rtx modsub_ret
9816 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9817 op0, op1, NULL_RTX, unsignedp);
9818 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9819 optab_default);
9820 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9821 target, unsignedp, OPTAB_LIB_WIDEN);
9822 rtx_insn *modsub_insns = get_insns ();
9823 end_sequence ();
9824 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9825 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9826 /* If costs are the same then use as tie breaker the other other
9827 factor. */
9828 if (divmul_cost == modsub_cost)
9830 divmul_cost = seq_cost (divmul_insns, !speed_p);
9831 modsub_cost = seq_cost (modsub_insns, !speed_p);
9834 if (divmul_cost <= modsub_cost)
9836 emit_insn (divmul_insns);
9837 return REDUCE_BIT_FIELD (divmul_ret);
9839 emit_insn (modsub_insns);
9840 return REDUCE_BIT_FIELD (modsub_ret);
9844 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9846 /* Expand X*Y as X&-Y when Y must be zero or one. */
9847 if (SCALAR_INT_MODE_P (mode))
9849 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9850 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9852 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9853 if (bit0_p && bit1_p)
9854 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9856 if (bit0_p || bit1_p)
9858 bool speed = optimize_insn_for_speed_p ();
9859 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9860 struct algorithm algorithm;
9861 enum mult_variant variant;
9862 if (CONST_INT_P (op1)
9863 ? !choose_mult_variant (mode, INTVAL (op1),
9864 &algorithm, &variant, cost)
9865 : cost < mul_cost (speed, mode))
9867 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9868 op1, target)
9869 : expand_and (mode, op0,
9870 negate_rtx (mode, op1),
9871 target);
9872 return REDUCE_BIT_FIELD (target);
9877 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9879 case TRUNC_MOD_EXPR:
9880 case FLOOR_MOD_EXPR:
9881 case CEIL_MOD_EXPR:
9882 case ROUND_MOD_EXPR:
9884 case TRUNC_DIV_EXPR:
9885 case FLOOR_DIV_EXPR:
9886 case CEIL_DIV_EXPR:
9887 case ROUND_DIV_EXPR:
9888 case EXACT_DIV_EXPR:
9889 /* If this is a fixed-point operation, then we cannot use the code
9890 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9891 divisions. */
9892 if (ALL_FIXED_POINT_MODE_P (mode))
9893 goto binop;
9895 if (modifier == EXPAND_STACK_PARM)
9896 target = 0;
9897 /* Possible optimization: compute the dividend with EXPAND_SUM
9898 then if the divisor is constant can optimize the case
9899 where some terms of the dividend have coeffs divisible by it. */
9900 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9901 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9902 target, unsignedp);
9904 case RDIV_EXPR:
9905 goto binop;
9907 case MULT_HIGHPART_EXPR:
9908 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9909 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9910 gcc_assert (temp);
9911 return temp;
9913 case FIXED_CONVERT_EXPR:
9914 op0 = expand_normal (treeop0);
9915 if (target == 0 || modifier == EXPAND_STACK_PARM)
9916 target = gen_reg_rtx (mode);
9918 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9919 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9920 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9921 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9922 else
9923 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9924 return target;
9926 case FIX_TRUNC_EXPR:
9927 op0 = expand_normal (treeop0);
9928 if (target == 0 || modifier == EXPAND_STACK_PARM)
9929 target = gen_reg_rtx (mode);
9930 expand_fix (target, op0, unsignedp);
9931 return target;
9933 case FLOAT_EXPR:
9934 op0 = expand_normal (treeop0);
9935 if (target == 0 || modifier == EXPAND_STACK_PARM)
9936 target = gen_reg_rtx (mode);
9937 /* expand_float can't figure out what to do if FROM has VOIDmode.
9938 So give it the correct mode. With -O, cse will optimize this. */
9939 if (GET_MODE (op0) == VOIDmode)
9940 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9941 op0);
9942 expand_float (target, op0,
9943 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9944 return target;
9946 case NEGATE_EXPR:
9947 op0 = expand_expr (treeop0, subtarget,
9948 VOIDmode, EXPAND_NORMAL);
9949 if (modifier == EXPAND_STACK_PARM)
9950 target = 0;
9951 temp = expand_unop (mode,
9952 optab_for_tree_code (NEGATE_EXPR, type,
9953 optab_default),
9954 op0, target, 0);
9955 gcc_assert (temp);
9956 return REDUCE_BIT_FIELD (temp);
9958 case ABS_EXPR:
9959 case ABSU_EXPR:
9960 op0 = expand_expr (treeop0, subtarget,
9961 VOIDmode, EXPAND_NORMAL);
9962 if (modifier == EXPAND_STACK_PARM)
9963 target = 0;
9965 /* ABS_EXPR is not valid for complex arguments. */
9966 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9967 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9969 /* Unsigned abs is simply the operand. Testing here means we don't
9970 risk generating incorrect code below. */
9971 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9972 return op0;
9974 return expand_abs (mode, op0, target, unsignedp,
9975 safe_from_p (target, treeop0, 1));
9977 case MAX_EXPR:
9978 case MIN_EXPR:
9979 target = original_target;
9980 if (target == 0
9981 || modifier == EXPAND_STACK_PARM
9982 || (MEM_P (target) && MEM_VOLATILE_P (target))
9983 || GET_MODE (target) != mode
9984 || (REG_P (target)
9985 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9986 target = gen_reg_rtx (mode);
9987 expand_operands (treeop0, treeop1,
9988 target, &op0, &op1, EXPAND_NORMAL);
9990 /* First try to do it with a special MIN or MAX instruction.
9991 If that does not win, use a conditional jump to select the proper
9992 value. */
9993 this_optab = optab_for_tree_code (code, type, optab_default);
9994 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9995 OPTAB_WIDEN);
9996 if (temp != 0)
9997 return temp;
9999 if (VECTOR_TYPE_P (type))
10000 gcc_unreachable ();
10002 /* At this point, a MEM target is no longer useful; we will get better
10003 code without it. */
10005 if (! REG_P (target))
10006 target = gen_reg_rtx (mode);
10008 /* If op1 was placed in target, swap op0 and op1. */
10009 if (target != op0 && target == op1)
10010 std::swap (op0, op1);
10012 /* We generate better code and avoid problems with op1 mentioning
10013 target by forcing op1 into a pseudo if it isn't a constant. */
10014 if (! CONSTANT_P (op1))
10015 op1 = force_reg (mode, op1);
10018 enum rtx_code comparison_code;
10019 rtx cmpop1 = op1;
10021 if (code == MAX_EXPR)
10022 comparison_code = unsignedp ? GEU : GE;
10023 else
10024 comparison_code = unsignedp ? LEU : LE;
10026 /* Canonicalize to comparisons against 0. */
10027 if (op1 == const1_rtx)
10029 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10030 or (a != 0 ? a : 1) for unsigned.
10031 For MIN we are safe converting (a <= 1 ? a : 1)
10032 into (a <= 0 ? a : 1) */
10033 cmpop1 = const0_rtx;
10034 if (code == MAX_EXPR)
10035 comparison_code = unsignedp ? NE : GT;
10037 if (op1 == constm1_rtx && !unsignedp)
10039 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10040 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10041 cmpop1 = const0_rtx;
10042 if (code == MIN_EXPR)
10043 comparison_code = LT;
10046 /* Use a conditional move if possible. */
10047 if (can_conditionally_move_p (mode))
10049 rtx insn;
10051 start_sequence ();
10053 /* Try to emit the conditional move. */
10054 insn = emit_conditional_move (target,
10055 { comparison_code,
10056 op0, cmpop1, mode },
10057 op0, op1, mode,
10058 unsignedp);
10060 /* If we could do the conditional move, emit the sequence,
10061 and return. */
10062 if (insn)
10064 rtx_insn *seq = get_insns ();
10065 end_sequence ();
10066 emit_insn (seq);
10067 return target;
10070 /* Otherwise discard the sequence and fall back to code with
10071 branches. */
10072 end_sequence ();
10075 if (target != op0)
10076 emit_move_insn (target, op0);
10078 lab = gen_label_rtx ();
10079 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10080 unsignedp, mode, NULL_RTX, NULL, lab,
10081 profile_probability::uninitialized ());
10083 emit_move_insn (target, op1);
10084 emit_label (lab);
10085 return target;
10087 case BIT_NOT_EXPR:
10088 op0 = expand_expr (treeop0, subtarget,
10089 VOIDmode, EXPAND_NORMAL);
10090 if (modifier == EXPAND_STACK_PARM)
10091 target = 0;
10092 /* In case we have to reduce the result to bitfield precision
10093 for unsigned bitfield expand this as XOR with a proper constant
10094 instead. */
10095 if (reduce_bit_field && TYPE_UNSIGNED (type))
10097 int_mode = SCALAR_INT_TYPE_MODE (type);
10098 wide_int mask = wi::mask (TYPE_PRECISION (type),
10099 false, GET_MODE_PRECISION (int_mode));
10101 temp = expand_binop (int_mode, xor_optab, op0,
10102 immed_wide_int_const (mask, int_mode),
10103 target, 1, OPTAB_LIB_WIDEN);
10105 else
10106 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10107 gcc_assert (temp);
10108 return temp;
10110 /* ??? Can optimize bitwise operations with one arg constant.
10111 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10112 and (a bitwise1 b) bitwise2 b (etc)
10113 but that is probably not worth while. */
10115 case BIT_AND_EXPR:
10116 case BIT_IOR_EXPR:
10117 case BIT_XOR_EXPR:
10118 goto binop;
10120 case LROTATE_EXPR:
10121 case RROTATE_EXPR:
10122 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10123 || type_has_mode_precision_p (type));
10124 /* fall through */
10126 case LSHIFT_EXPR:
10127 case RSHIFT_EXPR:
10129 /* If this is a fixed-point operation, then we cannot use the code
10130 below because "expand_shift" doesn't support sat/no-sat fixed-point
10131 shifts. */
10132 if (ALL_FIXED_POINT_MODE_P (mode))
10133 goto binop;
10135 if (! safe_from_p (subtarget, treeop1, 1))
10136 subtarget = 0;
10137 if (modifier == EXPAND_STACK_PARM)
10138 target = 0;
10139 op0 = expand_expr (treeop0, subtarget,
10140 VOIDmode, EXPAND_NORMAL);
10142 /* Left shift optimization when shifting across word_size boundary.
10144 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10145 there isn't native instruction to support this wide mode
10146 left shift. Given below scenario:
10148 Type A = (Type) B << C
10150 |< T >|
10151 | dest_high | dest_low |
10153 | word_size |
10155 If the shift amount C caused we shift B to across the word
10156 size boundary, i.e part of B shifted into high half of
10157 destination register, and part of B remains in the low
10158 half, then GCC will use the following left shift expand
10159 logic:
10161 1. Initialize dest_low to B.
10162 2. Initialize every bit of dest_high to the sign bit of B.
10163 3. Logic left shift dest_low by C bit to finalize dest_low.
10164 The value of dest_low before this shift is kept in a temp D.
10165 4. Logic left shift dest_high by C.
10166 5. Logic right shift D by (word_size - C).
10167 6. Or the result of 4 and 5 to finalize dest_high.
10169 While, by checking gimple statements, if operand B is
10170 coming from signed extension, then we can simplify above
10171 expand logic into:
10173 1. dest_high = src_low >> (word_size - C).
10174 2. dest_low = src_low << C.
10176 We can use one arithmetic right shift to finish all the
10177 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10178 needed from 6 into 2.
10180 The case is similar for zero extension, except that we
10181 initialize dest_high to zero rather than copies of the sign
10182 bit from B. Furthermore, we need to use a logical right shift
10183 in this case.
10185 The choice of sign-extension versus zero-extension is
10186 determined entirely by whether or not B is signed and is
10187 independent of the current setting of unsignedp. */
10189 temp = NULL_RTX;
10190 if (code == LSHIFT_EXPR
10191 && target
10192 && REG_P (target)
10193 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10194 && mode == int_mode
10195 && TREE_CONSTANT (treeop1)
10196 && TREE_CODE (treeop0) == SSA_NAME)
10198 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10199 if (is_gimple_assign (def)
10200 && gimple_assign_rhs_code (def) == NOP_EXPR)
10202 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10203 (TREE_TYPE (gimple_assign_rhs1 (def)));
10205 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10206 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10207 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10208 >= GET_MODE_BITSIZE (word_mode)))
10210 rtx_insn *seq, *seq_old;
10211 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10212 int_mode);
10213 bool extend_unsigned
10214 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10215 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10216 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10217 rtx dest_high = simplify_gen_subreg (word_mode, target,
10218 int_mode, high_off);
10219 HOST_WIDE_INT ramount = (BITS_PER_WORD
10220 - TREE_INT_CST_LOW (treeop1));
10221 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10223 start_sequence ();
10224 /* dest_high = src_low >> (word_size - C). */
10225 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10226 rshift, dest_high,
10227 extend_unsigned);
10228 if (temp != dest_high)
10229 emit_move_insn (dest_high, temp);
10231 /* dest_low = src_low << C. */
10232 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10233 treeop1, dest_low, unsignedp);
10234 if (temp != dest_low)
10235 emit_move_insn (dest_low, temp);
10237 seq = get_insns ();
10238 end_sequence ();
10239 temp = target ;
10241 if (have_insn_for (ASHIFT, int_mode))
10243 bool speed_p = optimize_insn_for_speed_p ();
10244 start_sequence ();
10245 rtx ret_old = expand_variable_shift (code, int_mode,
10246 op0, treeop1,
10247 target,
10248 unsignedp);
10250 seq_old = get_insns ();
10251 end_sequence ();
10252 if (seq_cost (seq, speed_p)
10253 >= seq_cost (seq_old, speed_p))
10255 seq = seq_old;
10256 temp = ret_old;
10259 emit_insn (seq);
10264 if (temp == NULL_RTX)
10265 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10266 unsignedp);
10267 if (code == LSHIFT_EXPR)
10268 temp = REDUCE_BIT_FIELD (temp);
10269 return temp;
10272 /* Could determine the answer when only additive constants differ. Also,
10273 the addition of one can be handled by changing the condition. */
10274 case LT_EXPR:
10275 case LE_EXPR:
10276 case GT_EXPR:
10277 case GE_EXPR:
10278 case EQ_EXPR:
10279 case NE_EXPR:
10280 case UNORDERED_EXPR:
10281 case ORDERED_EXPR:
10282 case UNLT_EXPR:
10283 case UNLE_EXPR:
10284 case UNGT_EXPR:
10285 case UNGE_EXPR:
10286 case UNEQ_EXPR:
10287 case LTGT_EXPR:
10289 temp = do_store_flag (ops,
10290 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10291 tmode != VOIDmode ? tmode : mode);
10292 if (temp)
10293 return temp;
10295 /* Use a compare and a jump for BLKmode comparisons, or for function
10296 type comparisons is have_canonicalize_funcptr_for_compare. */
10298 if ((target == 0
10299 || modifier == EXPAND_STACK_PARM
10300 || ! safe_from_p (target, treeop0, 1)
10301 || ! safe_from_p (target, treeop1, 1)
10302 /* Make sure we don't have a hard reg (such as function's return
10303 value) live across basic blocks, if not optimizing. */
10304 || (!optimize && REG_P (target)
10305 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10306 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10308 emit_move_insn (target, const0_rtx);
10310 rtx_code_label *lab1 = gen_label_rtx ();
10311 jumpifnot_1 (code, treeop0, treeop1, lab1,
10312 profile_probability::uninitialized ());
10314 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10315 emit_move_insn (target, constm1_rtx);
10316 else
10317 emit_move_insn (target, const1_rtx);
10319 emit_label (lab1);
10320 return target;
10322 case COMPLEX_EXPR:
10323 /* Get the rtx code of the operands. */
10324 op0 = expand_normal (treeop0);
10325 op1 = expand_normal (treeop1);
10327 if (!target)
10328 target = gen_reg_rtx (TYPE_MODE (type));
10329 else
10330 /* If target overlaps with op1, then either we need to force
10331 op1 into a pseudo (if target also overlaps with op0),
10332 or write the complex parts in reverse order. */
10333 switch (GET_CODE (target))
10335 case CONCAT:
10336 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10338 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10340 complex_expr_force_op1:
10341 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10342 emit_move_insn (temp, op1);
10343 op1 = temp;
10344 break;
10346 complex_expr_swap_order:
10347 /* Move the imaginary (op1) and real (op0) parts to their
10348 location. */
10349 write_complex_part (target, op1, true, true);
10350 write_complex_part (target, op0, false, false);
10352 return target;
10354 break;
10355 case MEM:
10356 temp = adjust_address_nv (target,
10357 GET_MODE_INNER (GET_MODE (target)), 0);
10358 if (reg_overlap_mentioned_p (temp, op1))
10360 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10361 temp = adjust_address_nv (target, imode,
10362 GET_MODE_SIZE (imode));
10363 if (reg_overlap_mentioned_p (temp, op0))
10364 goto complex_expr_force_op1;
10365 goto complex_expr_swap_order;
10367 break;
10368 default:
10369 if (reg_overlap_mentioned_p (target, op1))
10371 if (reg_overlap_mentioned_p (target, op0))
10372 goto complex_expr_force_op1;
10373 goto complex_expr_swap_order;
10375 break;
10378 /* Move the real (op0) and imaginary (op1) parts to their location. */
10379 write_complex_part (target, op0, false, true);
10380 write_complex_part (target, op1, true, false);
10382 return target;
10384 case WIDEN_SUM_EXPR:
10386 tree oprnd0 = treeop0;
10387 tree oprnd1 = treeop1;
10389 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10390 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10391 target, unsignedp);
10392 return target;
10395 case VEC_UNPACK_HI_EXPR:
10396 case VEC_UNPACK_LO_EXPR:
10397 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10398 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10400 op0 = expand_normal (treeop0);
10401 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10402 target, unsignedp);
10403 gcc_assert (temp);
10404 return temp;
10407 case VEC_UNPACK_FLOAT_HI_EXPR:
10408 case VEC_UNPACK_FLOAT_LO_EXPR:
10410 op0 = expand_normal (treeop0);
10411 /* The signedness is determined from input operand. */
10412 temp = expand_widen_pattern_expr
10413 (ops, op0, NULL_RTX, NULL_RTX,
10414 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10416 gcc_assert (temp);
10417 return temp;
10420 case VEC_WIDEN_MULT_HI_EXPR:
10421 case VEC_WIDEN_MULT_LO_EXPR:
10422 case VEC_WIDEN_MULT_EVEN_EXPR:
10423 case VEC_WIDEN_MULT_ODD_EXPR:
10424 case VEC_WIDEN_LSHIFT_HI_EXPR:
10425 case VEC_WIDEN_LSHIFT_LO_EXPR:
10426 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10427 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10428 target, unsignedp);
10429 gcc_assert (target);
10430 return target;
10432 case VEC_PACK_SAT_EXPR:
10433 case VEC_PACK_FIX_TRUNC_EXPR:
10434 mode = TYPE_MODE (TREE_TYPE (treeop0));
10435 subtarget = NULL_RTX;
10436 goto binop;
10438 case VEC_PACK_TRUNC_EXPR:
10439 if (VECTOR_BOOLEAN_TYPE_P (type)
10440 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10441 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10442 && SCALAR_INT_MODE_P (mode))
10444 class expand_operand eops[4];
10445 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10446 expand_operands (treeop0, treeop1,
10447 subtarget, &op0, &op1, EXPAND_NORMAL);
10448 this_optab = vec_pack_sbool_trunc_optab;
10449 enum insn_code icode = optab_handler (this_optab, imode);
10450 create_output_operand (&eops[0], target, mode);
10451 create_convert_operand_from (&eops[1], op0, imode, false);
10452 create_convert_operand_from (&eops[2], op1, imode, false);
10453 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10454 create_input_operand (&eops[3], temp, imode);
10455 expand_insn (icode, 4, eops);
10456 return eops[0].value;
10458 mode = TYPE_MODE (TREE_TYPE (treeop0));
10459 subtarget = NULL_RTX;
10460 goto binop;
10462 case VEC_PACK_FLOAT_EXPR:
10463 mode = TYPE_MODE (TREE_TYPE (treeop0));
10464 expand_operands (treeop0, treeop1,
10465 subtarget, &op0, &op1, EXPAND_NORMAL);
10466 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10467 optab_default);
10468 target = expand_binop (mode, this_optab, op0, op1, target,
10469 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10470 OPTAB_LIB_WIDEN);
10471 gcc_assert (target);
10472 return target;
10474 case VEC_PERM_EXPR:
10476 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10477 vec_perm_builder sel;
10478 if (TREE_CODE (treeop2) == VECTOR_CST
10479 && tree_to_vec_perm_builder (&sel, treeop2))
10481 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10482 temp = expand_vec_perm_const (mode, op0, op1, sel,
10483 sel_mode, target);
10485 else
10487 op2 = expand_normal (treeop2);
10488 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10490 gcc_assert (temp);
10491 return temp;
10494 case DOT_PROD_EXPR:
10496 tree oprnd0 = treeop0;
10497 tree oprnd1 = treeop1;
10498 tree oprnd2 = treeop2;
10500 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10501 op2 = expand_normal (oprnd2);
10502 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10503 target, unsignedp);
10504 return target;
10507 case SAD_EXPR:
10509 tree oprnd0 = treeop0;
10510 tree oprnd1 = treeop1;
10511 tree oprnd2 = treeop2;
10513 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10514 op2 = expand_normal (oprnd2);
10515 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10516 target, unsignedp);
10517 return target;
10520 case REALIGN_LOAD_EXPR:
10522 tree oprnd0 = treeop0;
10523 tree oprnd1 = treeop1;
10524 tree oprnd2 = treeop2;
10526 this_optab = optab_for_tree_code (code, type, optab_default);
10527 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10528 op2 = expand_normal (oprnd2);
10529 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10530 target, unsignedp);
10531 gcc_assert (temp);
10532 return temp;
10535 case COND_EXPR:
10537 /* A COND_EXPR with its type being VOID_TYPE represents a
10538 conditional jump and is handled in
10539 expand_gimple_cond_expr. */
10540 gcc_assert (!VOID_TYPE_P (type));
10542 /* Note that COND_EXPRs whose type is a structure or union
10543 are required to be constructed to contain assignments of
10544 a temporary variable, so that we can evaluate them here
10545 for side effect only. If type is void, we must do likewise. */
10547 gcc_assert (!TREE_ADDRESSABLE (type)
10548 && !ignore
10549 && TREE_TYPE (treeop1) != void_type_node
10550 && TREE_TYPE (treeop2) != void_type_node);
10552 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10553 if (temp)
10554 return temp;
10556 /* If we are not to produce a result, we have no target. Otherwise,
10557 if a target was specified use it; it will not be used as an
10558 intermediate target unless it is safe. If no target, use a
10559 temporary. */
10561 if (modifier != EXPAND_STACK_PARM
10562 && original_target
10563 && safe_from_p (original_target, treeop0, 1)
10564 && GET_MODE (original_target) == mode
10565 && !MEM_P (original_target))
10566 temp = original_target;
10567 else
10568 temp = assign_temp (type, 0, 1);
10570 do_pending_stack_adjust ();
10571 NO_DEFER_POP;
10572 rtx_code_label *lab0 = gen_label_rtx ();
10573 rtx_code_label *lab1 = gen_label_rtx ();
10574 jumpifnot (treeop0, lab0,
10575 profile_probability::uninitialized ());
10576 store_expr (treeop1, temp,
10577 modifier == EXPAND_STACK_PARM,
10578 false, false);
10580 emit_jump_insn (targetm.gen_jump (lab1));
10581 emit_barrier ();
10582 emit_label (lab0);
10583 store_expr (treeop2, temp,
10584 modifier == EXPAND_STACK_PARM,
10585 false, false);
10587 emit_label (lab1);
10588 OK_DEFER_POP;
10589 return temp;
10592 case VEC_DUPLICATE_EXPR:
10593 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10594 target = expand_vector_broadcast (mode, op0);
10595 gcc_assert (target);
10596 return target;
10598 case VEC_SERIES_EXPR:
10599 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10600 return expand_vec_series_expr (mode, op0, op1, target);
10602 case BIT_INSERT_EXPR:
10604 unsigned bitpos = tree_to_uhwi (treeop2);
10605 unsigned bitsize;
10606 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10607 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10608 else
10609 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10610 op0 = expand_normal (treeop0);
10611 op1 = expand_normal (treeop1);
10612 rtx dst = gen_reg_rtx (mode);
10613 emit_move_insn (dst, op0);
10614 store_bit_field (dst, bitsize, bitpos, 0, 0,
10615 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10616 return dst;
10619 default:
10620 gcc_unreachable ();
10623 /* Here to do an ordinary binary operator. */
10624 binop:
10625 expand_operands (treeop0, treeop1,
10626 subtarget, &op0, &op1, EXPAND_NORMAL);
10627 binop2:
10628 this_optab = optab_for_tree_code (code, type, optab_default);
10629 binop3:
10630 if (modifier == EXPAND_STACK_PARM)
10631 target = 0;
10632 temp = expand_binop (mode, this_optab, op0, op1, target,
10633 unsignedp, OPTAB_LIB_WIDEN);
10634 gcc_assert (temp);
10635 /* Bitwise operations do not need bitfield reduction as we expect their
10636 operands being properly truncated. */
10637 if (code == BIT_XOR_EXPR
10638 || code == BIT_AND_EXPR
10639 || code == BIT_IOR_EXPR)
10640 return temp;
10641 return REDUCE_BIT_FIELD (temp);
10643 #undef REDUCE_BIT_FIELD
10646 /* Return TRUE if expression STMT is suitable for replacement.
10647 Never consider memory loads as replaceable, because those don't ever lead
10648 into constant expressions. */
10650 static bool
10651 stmt_is_replaceable_p (gimple *stmt)
10653 if (ssa_is_replaceable_p (stmt))
10655 /* Don't move around loads. */
10656 if (!gimple_assign_single_p (stmt)
10657 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10658 return true;
10660 return false;
10664 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10665 enum expand_modifier modifier, rtx *alt_rtl,
10666 bool inner_reference_p)
10668 rtx op0, op1, temp, decl_rtl;
10669 tree type;
10670 int unsignedp;
10671 machine_mode mode, dmode;
10672 enum tree_code code = TREE_CODE (exp);
10673 rtx subtarget, original_target;
10674 int ignore;
10675 bool reduce_bit_field;
10676 location_t loc = EXPR_LOCATION (exp);
10677 struct separate_ops ops;
10678 tree treeop0, treeop1, treeop2;
10679 tree ssa_name = NULL_TREE;
10680 gimple *g;
10682 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL
10683 expansion sign/zero extends integral types with less than mode precision
10684 when reading from bit-fields and after arithmetic operations (see
10685 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies
10686 on those extensions to have been already performed, but because of the
10687 above for _BitInt they need to be sign/zero extended when reading from
10688 locations that could be exposed to ABI boundaries (when loading from
10689 objects in memory, or function arguments, return value). Because we
10690 internally extend after arithmetic operations, we can avoid doing that
10691 when reading from SSA_NAMEs of vars. */
10692 #define EXTEND_BITINT(expr) \
10693 ((TREE_CODE (type) == BITINT_TYPE \
10694 && reduce_bit_field \
10695 && mode != BLKmode \
10696 && modifier != EXPAND_MEMORY \
10697 && modifier != EXPAND_WRITE \
10698 && modifier != EXPAND_INITIALIZER \
10699 && modifier != EXPAND_CONST_ADDRESS) \
10700 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr))
10702 type = TREE_TYPE (exp);
10703 mode = TYPE_MODE (type);
10704 unsignedp = TYPE_UNSIGNED (type);
10706 treeop0 = treeop1 = treeop2 = NULL_TREE;
10707 if (!VL_EXP_CLASS_P (exp))
10708 switch (TREE_CODE_LENGTH (code))
10710 default:
10711 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10712 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10713 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10714 case 0: break;
10716 ops.code = code;
10717 ops.type = type;
10718 ops.op0 = treeop0;
10719 ops.op1 = treeop1;
10720 ops.op2 = treeop2;
10721 ops.location = loc;
10723 ignore = (target == const0_rtx
10724 || ((CONVERT_EXPR_CODE_P (code)
10725 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10726 && TREE_CODE (type) == VOID_TYPE));
10728 /* An operation in what may be a bit-field type needs the
10729 result to be reduced to the precision of the bit-field type,
10730 which is narrower than that of the type's mode. */
10731 reduce_bit_field = (!ignore
10732 && INTEGRAL_TYPE_P (type)
10733 && !type_has_mode_precision_p (type));
10735 /* If we are going to ignore this result, we need only do something
10736 if there is a side-effect somewhere in the expression. If there
10737 is, short-circuit the most common cases here. Note that we must
10738 not call expand_expr with anything but const0_rtx in case this
10739 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10741 if (ignore)
10743 if (! TREE_SIDE_EFFECTS (exp))
10744 return const0_rtx;
10746 /* Ensure we reference a volatile object even if value is ignored, but
10747 don't do this if all we are doing is taking its address. */
10748 if (TREE_THIS_VOLATILE (exp)
10749 && TREE_CODE (exp) != FUNCTION_DECL
10750 && mode != VOIDmode && mode != BLKmode
10751 && modifier != EXPAND_CONST_ADDRESS)
10753 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10754 if (MEM_P (temp))
10755 copy_to_reg (temp);
10756 return const0_rtx;
10759 if (TREE_CODE_CLASS (code) == tcc_unary
10760 || code == BIT_FIELD_REF
10761 || code == COMPONENT_REF
10762 || code == INDIRECT_REF)
10763 return expand_expr (treeop0, const0_rtx, VOIDmode,
10764 modifier);
10766 else if (TREE_CODE_CLASS (code) == tcc_binary
10767 || TREE_CODE_CLASS (code) == tcc_comparison
10768 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10770 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10771 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10772 return const0_rtx;
10775 target = 0;
10778 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10779 target = 0;
10781 /* Use subtarget as the target for operand 0 of a binary operation. */
10782 subtarget = get_subtarget (target);
10783 original_target = target;
10785 switch (code)
10787 case LABEL_DECL:
10789 tree function = decl_function_context (exp);
10791 temp = label_rtx (exp);
10792 temp = gen_rtx_LABEL_REF (Pmode, temp);
10794 if (function != current_function_decl
10795 && function != 0)
10796 LABEL_REF_NONLOCAL_P (temp) = 1;
10798 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10799 return temp;
10802 case SSA_NAME:
10803 /* ??? ivopts calls expander, without any preparation from
10804 out-of-ssa. So fake instructions as if this was an access to the
10805 base variable. This unnecessarily allocates a pseudo, see how we can
10806 reuse it, if partition base vars have it set already. */
10807 if (!currently_expanding_to_rtl)
10809 tree var = SSA_NAME_VAR (exp);
10810 if (var && DECL_RTL_SET_P (var))
10811 return DECL_RTL (var);
10812 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10813 LAST_VIRTUAL_REGISTER + 1);
10816 g = get_gimple_for_ssa_name (exp);
10817 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10818 if (g == NULL
10819 && modifier == EXPAND_INITIALIZER
10820 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10821 && (optimize || !SSA_NAME_VAR (exp)
10822 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10823 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10824 g = SSA_NAME_DEF_STMT (exp);
10825 if (g)
10827 rtx r;
10828 location_t saved_loc = curr_insn_location ();
10829 loc = gimple_location (g);
10830 if (loc != UNKNOWN_LOCATION)
10831 set_curr_insn_location (loc);
10832 ops.code = gimple_assign_rhs_code (g);
10833 switch (get_gimple_rhs_class (ops.code))
10835 case GIMPLE_TERNARY_RHS:
10836 ops.op2 = gimple_assign_rhs3 (g);
10837 /* Fallthru */
10838 case GIMPLE_BINARY_RHS:
10839 ops.op1 = gimple_assign_rhs2 (g);
10841 /* Try to expand conditonal compare. */
10842 if (targetm.gen_ccmp_first)
10844 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10845 r = expand_ccmp_expr (g, mode);
10846 if (r)
10847 break;
10849 /* Fallthru */
10850 case GIMPLE_UNARY_RHS:
10851 ops.op0 = gimple_assign_rhs1 (g);
10852 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10853 ops.location = loc;
10854 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10855 break;
10856 case GIMPLE_SINGLE_RHS:
10858 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10859 tmode, modifier, alt_rtl,
10860 inner_reference_p);
10861 break;
10863 default:
10864 gcc_unreachable ();
10866 set_curr_insn_location (saved_loc);
10867 if (REG_P (r) && !REG_EXPR (r))
10868 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10869 return r;
10872 ssa_name = exp;
10873 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10874 exp = SSA_NAME_VAR (ssa_name);
10875 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an
10876 SSA_NAME computed within the current function. In such case the
10877 value have been already extended before. While if it is a function
10878 parameter, result or some memory location, we need to be prepared
10879 for some other compiler leaving the bits uninitialized. */
10880 if (!exp || VAR_P (exp))
10881 reduce_bit_field = false;
10882 goto expand_decl_rtl;
10884 case VAR_DECL:
10885 /* Allow accel compiler to handle variables that require special
10886 treatment, e.g. if they have been modified in some way earlier in
10887 compilation by the adjust_private_decl OpenACC hook. */
10888 if (flag_openacc && targetm.goacc.expand_var_decl)
10890 temp = targetm.goacc.expand_var_decl (exp);
10891 if (temp)
10892 return temp;
10894 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10895 have scalar integer modes to a reg via store_constructor. */
10896 if (TREE_READONLY (exp)
10897 && !TREE_SIDE_EFFECTS (exp)
10898 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10899 && immediate_const_ctor_p (DECL_INITIAL (exp))
10900 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10901 && crtl->emit.regno_pointer_align_length
10902 && !target)
10904 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10905 store_constructor (DECL_INITIAL (exp), target, 0,
10906 int_expr_size (DECL_INITIAL (exp)), false);
10907 return target;
10909 /* ... fall through ... */
10911 case PARM_DECL:
10912 /* If a static var's type was incomplete when the decl was written,
10913 but the type is complete now, lay out the decl now. */
10914 if (DECL_SIZE (exp) == 0
10915 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10916 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10917 layout_decl (exp, 0);
10919 /* fall through */
10921 case FUNCTION_DECL:
10922 case RESULT_DECL:
10923 decl_rtl = DECL_RTL (exp);
10924 expand_decl_rtl:
10925 gcc_assert (decl_rtl);
10927 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10928 settings for VECTOR_TYPE_P that might switch for the function. */
10929 if (currently_expanding_to_rtl
10930 && code == VAR_DECL && MEM_P (decl_rtl)
10931 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10932 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10933 else
10934 decl_rtl = copy_rtx (decl_rtl);
10936 /* Record writes to register variables. */
10937 if (modifier == EXPAND_WRITE
10938 && REG_P (decl_rtl)
10939 && HARD_REGISTER_P (decl_rtl))
10940 add_to_hard_reg_set (&crtl->asm_clobbers,
10941 GET_MODE (decl_rtl), REGNO (decl_rtl));
10943 /* Ensure variable marked as used even if it doesn't go through
10944 a parser. If it hasn't be used yet, write out an external
10945 definition. */
10946 if (exp)
10947 TREE_USED (exp) = 1;
10949 /* Show we haven't gotten RTL for this yet. */
10950 temp = 0;
10952 /* Variables inherited from containing functions should have
10953 been lowered by this point. */
10954 if (exp)
10956 tree context = decl_function_context (exp);
10957 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10958 || context == current_function_decl
10959 || TREE_STATIC (exp)
10960 || DECL_EXTERNAL (exp)
10961 /* ??? C++ creates functions that are not
10962 TREE_STATIC. */
10963 || TREE_CODE (exp) == FUNCTION_DECL);
10966 /* This is the case of an array whose size is to be determined
10967 from its initializer, while the initializer is still being parsed.
10968 ??? We aren't parsing while expanding anymore. */
10970 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10971 temp = validize_mem (decl_rtl);
10973 /* If DECL_RTL is memory, we are in the normal case and the
10974 address is not valid, get the address into a register. */
10976 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10978 if (alt_rtl)
10979 *alt_rtl = decl_rtl;
10980 decl_rtl = use_anchored_address (decl_rtl);
10981 if (modifier != EXPAND_CONST_ADDRESS
10982 && modifier != EXPAND_SUM
10983 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10984 : GET_MODE (decl_rtl),
10985 XEXP (decl_rtl, 0),
10986 MEM_ADDR_SPACE (decl_rtl)))
10987 temp = replace_equiv_address (decl_rtl,
10988 copy_rtx (XEXP (decl_rtl, 0)));
10991 /* If we got something, return it. But first, set the alignment
10992 if the address is a register. */
10993 if (temp != 0)
10995 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10996 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10998 else if (MEM_P (decl_rtl))
10999 temp = decl_rtl;
11001 if (temp != 0)
11003 if (MEM_P (temp)
11004 && modifier != EXPAND_WRITE
11005 && modifier != EXPAND_MEMORY
11006 && modifier != EXPAND_INITIALIZER
11007 && modifier != EXPAND_CONST_ADDRESS
11008 && modifier != EXPAND_SUM
11009 && !inner_reference_p
11010 && mode != BLKmode
11011 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
11012 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11013 MEM_ALIGN (temp), NULL_RTX, NULL);
11015 return EXTEND_BITINT (temp);
11018 if (exp)
11019 dmode = DECL_MODE (exp);
11020 else
11021 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
11023 /* If the mode of DECL_RTL does not match that of the decl,
11024 there are two cases: we are dealing with a BLKmode value
11025 that is returned in a register, or we are dealing with
11026 a promoted value. In the latter case, return a SUBREG
11027 of the wanted mode, but mark it so that we know that it
11028 was already extended. */
11029 if (REG_P (decl_rtl)
11030 && dmode != BLKmode
11031 && GET_MODE (decl_rtl) != dmode)
11033 machine_mode pmode;
11035 /* Get the signedness to be used for this variable. Ensure we get
11036 the same mode we got when the variable was declared. */
11037 if (code != SSA_NAME)
11038 pmode = promote_decl_mode (exp, &unsignedp);
11039 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
11040 && gimple_code (g) == GIMPLE_CALL
11041 && !gimple_call_internal_p (g))
11042 pmode = promote_function_mode (type, mode, &unsignedp,
11043 gimple_call_fntype (g),
11045 else
11046 pmode = promote_ssa_mode (ssa_name, &unsignedp);
11047 gcc_assert (GET_MODE (decl_rtl) == pmode);
11049 /* Some ABIs require scalar floating point modes to be passed
11050 in a wider scalar integer mode. We need to explicitly
11051 truncate to an integer mode of the correct precision before
11052 using a SUBREG to reinterpret as a floating point value. */
11053 if (SCALAR_FLOAT_MODE_P (mode)
11054 && SCALAR_INT_MODE_P (pmode)
11055 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11056 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11058 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11059 SUBREG_PROMOTED_VAR_P (temp) = 1;
11060 SUBREG_PROMOTED_SET (temp, unsignedp);
11061 return EXTEND_BITINT (temp);
11064 return EXTEND_BITINT (decl_rtl);
11066 case INTEGER_CST:
11068 if (TREE_CODE (type) == BITINT_TYPE)
11070 unsigned int prec = TYPE_PRECISION (type);
11071 struct bitint_info info;
11072 bool ok = targetm.c.bitint_type_info (prec, &info);
11073 gcc_assert (ok);
11074 scalar_int_mode limb_mode
11075 = as_a <scalar_int_mode> (info.limb_mode);
11076 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode);
11077 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE)
11079 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */
11080 exp = tree_output_constant_def (exp);
11081 return expand_expr (exp, target, VOIDmode, modifier);
11085 /* Given that TYPE_PRECISION (type) is not always equal to
11086 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11087 the former to the latter according to the signedness of the
11088 type. */
11089 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11090 temp = immed_wide_int_const
11091 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11092 return temp;
11095 case VECTOR_CST:
11097 tree tmp = NULL_TREE;
11098 if (VECTOR_MODE_P (mode))
11099 return const_vector_from_tree (exp);
11100 scalar_int_mode int_mode;
11101 if (is_int_mode (mode, &int_mode))
11103 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11104 if (type_for_mode)
11105 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11106 type_for_mode, exp);
11108 if (!tmp)
11110 vec<constructor_elt, va_gc> *v;
11111 /* Constructors need to be fixed-length. FIXME. */
11112 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11113 vec_alloc (v, nunits);
11114 for (unsigned int i = 0; i < nunits; ++i)
11115 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11116 tmp = build_constructor (type, v);
11118 return expand_expr (tmp, ignore ? const0_rtx : target,
11119 tmode, modifier);
11122 case CONST_DECL:
11123 if (modifier == EXPAND_WRITE)
11125 /* Writing into CONST_DECL is always invalid, but handle it
11126 gracefully. */
11127 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11128 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11129 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11130 EXPAND_NORMAL, as);
11131 op0 = memory_address_addr_space (mode, op0, as);
11132 temp = gen_rtx_MEM (mode, op0);
11133 set_mem_addr_space (temp, as);
11134 return temp;
11136 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11138 case REAL_CST:
11139 /* If optimized, generate immediate CONST_DOUBLE
11140 which will be turned into memory by reload if necessary.
11142 We used to force a register so that loop.c could see it. But
11143 this does not allow gen_* patterns to perform optimizations with
11144 the constants. It also produces two insns in cases like "x = 1.0;".
11145 On most machines, floating-point constants are not permitted in
11146 many insns, so we'd end up copying it to a register in any case.
11148 Now, we do the copying in expand_binop, if appropriate. */
11149 return const_double_from_real_value (TREE_REAL_CST (exp),
11150 TYPE_MODE (TREE_TYPE (exp)));
11152 case FIXED_CST:
11153 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11154 TYPE_MODE (TREE_TYPE (exp)));
11156 case COMPLEX_CST:
11157 /* Handle evaluating a complex constant in a CONCAT target. */
11158 if (original_target && GET_CODE (original_target) == CONCAT)
11160 rtx rtarg, itarg;
11162 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11163 rtarg = XEXP (original_target, 0);
11164 itarg = XEXP (original_target, 1);
11166 /* Move the real and imaginary parts separately. */
11167 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11168 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11170 if (op0 != rtarg)
11171 emit_move_insn (rtarg, op0);
11172 if (op1 != itarg)
11173 emit_move_insn (itarg, op1);
11175 return original_target;
11178 /* fall through */
11180 case STRING_CST:
11181 temp = expand_expr_constant (exp, 1, modifier);
11183 /* temp contains a constant address.
11184 On RISC machines where a constant address isn't valid,
11185 make some insns to get that address into a register. */
11186 if (modifier != EXPAND_CONST_ADDRESS
11187 && modifier != EXPAND_INITIALIZER
11188 && modifier != EXPAND_SUM
11189 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11190 MEM_ADDR_SPACE (temp)))
11191 return replace_equiv_address (temp,
11192 copy_rtx (XEXP (temp, 0)));
11193 return temp;
11195 case POLY_INT_CST:
11196 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11198 case SAVE_EXPR:
11200 tree val = treeop0;
11201 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11202 inner_reference_p);
11204 if (!SAVE_EXPR_RESOLVED_P (exp))
11206 /* We can indeed still hit this case, typically via builtin
11207 expanders calling save_expr immediately before expanding
11208 something. Assume this means that we only have to deal
11209 with non-BLKmode values. */
11210 gcc_assert (GET_MODE (ret) != BLKmode);
11212 val = build_decl (curr_insn_location (),
11213 VAR_DECL, NULL, TREE_TYPE (exp));
11214 DECL_ARTIFICIAL (val) = 1;
11215 DECL_IGNORED_P (val) = 1;
11216 treeop0 = val;
11217 TREE_OPERAND (exp, 0) = treeop0;
11218 SAVE_EXPR_RESOLVED_P (exp) = 1;
11220 if (!CONSTANT_P (ret))
11221 ret = copy_to_reg (ret);
11222 SET_DECL_RTL (val, ret);
11225 return ret;
11229 case CONSTRUCTOR:
11230 /* If we don't need the result, just ensure we evaluate any
11231 subexpressions. */
11232 if (ignore)
11234 unsigned HOST_WIDE_INT idx;
11235 tree value;
11237 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11238 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11240 return const0_rtx;
11243 return expand_constructor (exp, target, modifier, false);
11245 case TARGET_MEM_REF:
11247 addr_space_t as
11248 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11249 unsigned int align;
11251 op0 = addr_for_mem_ref (exp, as, true);
11252 op0 = memory_address_addr_space (mode, op0, as);
11253 temp = gen_rtx_MEM (mode, op0);
11254 set_mem_attributes (temp, exp, 0);
11255 set_mem_addr_space (temp, as);
11256 align = get_object_alignment (exp);
11257 if (modifier != EXPAND_WRITE
11258 && modifier != EXPAND_MEMORY
11259 && mode != BLKmode
11260 && align < GET_MODE_ALIGNMENT (mode))
11261 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11262 align, NULL_RTX, NULL);
11263 return EXTEND_BITINT (temp);
11266 case MEM_REF:
11268 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11269 addr_space_t as
11270 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11271 machine_mode address_mode;
11272 tree base = TREE_OPERAND (exp, 0);
11273 gimple *def_stmt;
11274 unsigned align;
11275 /* Handle expansion of non-aliased memory with non-BLKmode. That
11276 might end up in a register. */
11277 if (mem_ref_refers_to_non_mem_p (exp))
11279 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11280 base = TREE_OPERAND (base, 0);
11281 poly_uint64 type_size;
11282 if (known_eq (offset, 0)
11283 && !reverse
11284 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11285 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11286 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11287 target, tmode, modifier);
11288 if (TYPE_MODE (type) == BLKmode)
11290 temp = assign_stack_temp (DECL_MODE (base),
11291 GET_MODE_SIZE (DECL_MODE (base)));
11292 store_expr (base, temp, 0, false, false);
11293 temp = adjust_address (temp, BLKmode, offset);
11294 set_mem_size (temp, int_size_in_bytes (type));
11295 return temp;
11297 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11298 bitsize_int (offset * BITS_PER_UNIT));
11299 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11300 return expand_expr (exp, target, tmode, modifier);
11302 address_mode = targetm.addr_space.address_mode (as);
11303 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11305 tree mask = gimple_assign_rhs2 (def_stmt);
11306 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11307 gimple_assign_rhs1 (def_stmt), mask);
11308 TREE_OPERAND (exp, 0) = base;
11310 align = get_object_alignment (exp);
11311 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11312 op0 = memory_address_addr_space (mode, op0, as);
11313 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11315 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11316 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11317 op0 = memory_address_addr_space (mode, op0, as);
11319 temp = gen_rtx_MEM (mode, op0);
11320 set_mem_attributes (temp, exp, 0);
11321 set_mem_addr_space (temp, as);
11322 if (TREE_THIS_VOLATILE (exp))
11323 MEM_VOLATILE_P (temp) = 1;
11324 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY)
11325 return temp;
11326 if (!inner_reference_p
11327 && mode != BLKmode
11328 && align < GET_MODE_ALIGNMENT (mode))
11329 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11330 modifier == EXPAND_STACK_PARM
11331 ? NULL_RTX : target, alt_rtl);
11332 if (reverse)
11333 temp = flip_storage_order (mode, temp);
11334 return EXTEND_BITINT (temp);
11337 case ARRAY_REF:
11340 tree array = treeop0;
11341 tree index = treeop1;
11342 tree init;
11344 /* Fold an expression like: "foo"[2].
11345 This is not done in fold so it won't happen inside &.
11346 Don't fold if this is for wide characters since it's too
11347 difficult to do correctly and this is a very rare case. */
11349 if (modifier != EXPAND_CONST_ADDRESS
11350 && modifier != EXPAND_INITIALIZER
11351 && modifier != EXPAND_MEMORY)
11353 tree t = fold_read_from_constant_string (exp);
11355 if (t)
11356 return expand_expr (t, target, tmode, modifier);
11359 /* If this is a constant index into a constant array,
11360 just get the value from the array. Handle both the cases when
11361 we have an explicit constructor and when our operand is a variable
11362 that was declared const. */
11364 if (modifier != EXPAND_CONST_ADDRESS
11365 && modifier != EXPAND_INITIALIZER
11366 && modifier != EXPAND_MEMORY
11367 && TREE_CODE (array) == CONSTRUCTOR
11368 && ! TREE_SIDE_EFFECTS (array)
11369 && TREE_CODE (index) == INTEGER_CST)
11371 unsigned HOST_WIDE_INT ix;
11372 tree field, value;
11374 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11375 field, value)
11376 if (tree_int_cst_equal (field, index))
11378 if (!TREE_SIDE_EFFECTS (value))
11379 return expand_expr (fold (value), target, tmode, modifier);
11380 break;
11384 else if (optimize >= 1
11385 && modifier != EXPAND_CONST_ADDRESS
11386 && modifier != EXPAND_INITIALIZER
11387 && modifier != EXPAND_MEMORY
11388 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11389 && TREE_CODE (index) == INTEGER_CST
11390 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11391 && (init = ctor_for_folding (array)) != error_mark_node)
11393 if (init == NULL_TREE)
11395 tree value = build_zero_cst (type);
11396 if (TREE_CODE (value) == CONSTRUCTOR)
11398 /* If VALUE is a CONSTRUCTOR, this optimization is only
11399 useful if this doesn't store the CONSTRUCTOR into
11400 memory. If it does, it is more efficient to just
11401 load the data from the array directly. */
11402 rtx ret = expand_constructor (value, target,
11403 modifier, true);
11404 if (ret == NULL_RTX)
11405 value = NULL_TREE;
11408 if (value)
11409 return expand_expr (value, target, tmode, modifier);
11411 else if (TREE_CODE (init) == CONSTRUCTOR)
11413 unsigned HOST_WIDE_INT ix;
11414 tree field, value;
11416 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11417 field, value)
11418 if (tree_int_cst_equal (field, index))
11420 if (TREE_SIDE_EFFECTS (value))
11421 break;
11423 if (TREE_CODE (value) == CONSTRUCTOR)
11425 /* If VALUE is a CONSTRUCTOR, this
11426 optimization is only useful if
11427 this doesn't store the CONSTRUCTOR
11428 into memory. If it does, it is more
11429 efficient to just load the data from
11430 the array directly. */
11431 rtx ret = expand_constructor (value, target,
11432 modifier, true);
11433 if (ret == NULL_RTX)
11434 break;
11437 return
11438 expand_expr (fold (value), target, tmode, modifier);
11441 else if (TREE_CODE (init) == STRING_CST)
11443 tree low_bound = array_ref_low_bound (exp);
11444 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11446 /* Optimize the special case of a zero lower bound.
11448 We convert the lower bound to sizetype to avoid problems
11449 with constant folding. E.g. suppose the lower bound is
11450 1 and its mode is QI. Without the conversion
11451 (ARRAY + (INDEX - (unsigned char)1))
11452 becomes
11453 (ARRAY + (-(unsigned char)1) + INDEX)
11454 which becomes
11455 (ARRAY + 255 + INDEX). Oops! */
11456 if (!integer_zerop (low_bound))
11457 index1 = size_diffop_loc (loc, index1,
11458 fold_convert_loc (loc, sizetype,
11459 low_bound));
11461 if (tree_fits_uhwi_p (index1)
11462 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11464 tree char_type = TREE_TYPE (TREE_TYPE (init));
11465 scalar_int_mode char_mode;
11467 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11468 && GET_MODE_SIZE (char_mode) == 1)
11469 return gen_int_mode (TREE_STRING_POINTER (init)
11470 [TREE_INT_CST_LOW (index1)],
11471 char_mode);
11476 goto normal_inner_ref;
11478 case COMPONENT_REF:
11479 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11480 /* Fall through. */
11481 case BIT_FIELD_REF:
11482 case ARRAY_RANGE_REF:
11483 normal_inner_ref:
11485 machine_mode mode1, mode2;
11486 poly_int64 bitsize, bitpos, bytepos;
11487 tree offset;
11488 int reversep, volatilep = 0;
11489 tree tem
11490 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11491 &unsignedp, &reversep, &volatilep);
11492 rtx orig_op0, memloc;
11493 bool clear_mem_expr = false;
11494 bool must_force_mem;
11496 /* If we got back the original object, something is wrong. Perhaps
11497 we are evaluating an expression too early. In any event, don't
11498 infinitely recurse. */
11499 gcc_assert (tem != exp);
11501 /* Make sure bitpos is not negative, this can wreak havoc later. */
11502 if (maybe_lt (bitpos, 0))
11504 gcc_checking_assert (offset == NULL_TREE);
11505 offset = size_int (bits_to_bytes_round_down (bitpos));
11506 bitpos = num_trailing_bits (bitpos);
11509 /* If we have either an offset, a BLKmode result, or a reference
11510 outside the underlying object, we must force it to memory.
11511 Such a case can occur in Ada if we have unchecked conversion
11512 of an expression from a scalar type to an aggregate type or
11513 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11514 passed a partially uninitialized object or a view-conversion
11515 to a larger size. */
11516 must_force_mem = offset != NULL_TREE
11517 || mode1 == BLKmode
11518 || (mode == BLKmode
11519 && !int_mode_for_size (bitsize, 1).exists ());
11521 const enum expand_modifier tem_modifier
11522 = must_force_mem
11523 ? EXPAND_MEMORY
11524 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11526 /* If TEM's type is a union of variable size, pass TARGET to the inner
11527 computation, since it will need a temporary and TARGET is known
11528 to have to do. This occurs in unchecked conversion in Ada. */
11529 const rtx tem_target
11530 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11531 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11532 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11533 && modifier != EXPAND_STACK_PARM
11534 ? target
11535 : NULL_RTX;
11537 orig_op0 = op0
11538 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11539 true);
11541 /* If the field has a mode, we want to access it in the
11542 field's mode, not the computed mode.
11543 If a MEM has VOIDmode (external with incomplete type),
11544 use BLKmode for it instead. */
11545 if (MEM_P (op0))
11547 if (mode1 != VOIDmode)
11548 op0 = adjust_address (op0, mode1, 0);
11549 else if (GET_MODE (op0) == VOIDmode)
11550 op0 = adjust_address (op0, BLKmode, 0);
11553 mode2
11554 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11556 /* See above for the rationale. */
11557 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11558 must_force_mem = true;
11560 /* Handle CONCAT first. */
11561 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11563 if (known_eq (bitpos, 0)
11564 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11565 && COMPLEX_MODE_P (mode1)
11566 && COMPLEX_MODE_P (GET_MODE (op0))
11567 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11568 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11570 if (reversep)
11571 op0 = flip_storage_order (GET_MODE (op0), op0);
11572 if (mode1 != GET_MODE (op0))
11574 rtx parts[2];
11575 for (int i = 0; i < 2; i++)
11577 rtx op = read_complex_part (op0, i != 0);
11578 if (GET_CODE (op) == SUBREG)
11579 op = force_reg (GET_MODE (op), op);
11580 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11581 if (temp)
11582 op = temp;
11583 else
11585 if (!REG_P (op) && !MEM_P (op))
11586 op = force_reg (GET_MODE (op), op);
11587 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11589 parts[i] = op;
11591 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11593 return op0;
11595 if (known_eq (bitpos, 0)
11596 && known_eq (bitsize,
11597 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11598 && maybe_ne (bitsize, 0))
11600 op0 = XEXP (op0, 0);
11601 mode2 = GET_MODE (op0);
11603 else if (known_eq (bitpos,
11604 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11605 && known_eq (bitsize,
11606 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11607 && maybe_ne (bitpos, 0)
11608 && maybe_ne (bitsize, 0))
11610 op0 = XEXP (op0, 1);
11611 bitpos = 0;
11612 mode2 = GET_MODE (op0);
11614 else
11615 /* Otherwise force into memory. */
11616 must_force_mem = true;
11619 /* If this is a constant, put it in a register if it is a legitimate
11620 constant and we don't need a memory reference. */
11621 if (CONSTANT_P (op0)
11622 && mode2 != BLKmode
11623 && targetm.legitimate_constant_p (mode2, op0)
11624 && !must_force_mem)
11625 op0 = force_reg (mode2, op0);
11627 /* Otherwise, if this is a constant, try to force it to the constant
11628 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11629 is a legitimate constant. */
11630 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11631 op0 = validize_mem (memloc);
11633 /* Otherwise, if this is a constant or the object is not in memory
11634 and need be, put it there. */
11635 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11637 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11638 emit_move_insn (memloc, op0);
11639 op0 = memloc;
11640 clear_mem_expr = true;
11643 if (offset)
11645 machine_mode address_mode;
11646 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11647 EXPAND_SUM);
11649 gcc_assert (MEM_P (op0));
11651 address_mode = get_address_mode (op0);
11652 if (GET_MODE (offset_rtx) != address_mode)
11654 /* We cannot be sure that the RTL in offset_rtx is valid outside
11655 of a memory address context, so force it into a register
11656 before attempting to convert it to the desired mode. */
11657 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11658 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11661 /* See the comment in expand_assignment for the rationale. */
11662 if (mode1 != VOIDmode
11663 && maybe_ne (bitpos, 0)
11664 && maybe_gt (bitsize, 0)
11665 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11666 && multiple_p (bitpos, bitsize)
11667 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11668 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11670 op0 = adjust_address (op0, mode1, bytepos);
11671 bitpos = 0;
11674 op0 = offset_address (op0, offset_rtx,
11675 highest_pow2_factor (offset));
11678 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11679 record its alignment as BIGGEST_ALIGNMENT. */
11680 if (MEM_P (op0)
11681 && known_eq (bitpos, 0)
11682 && offset != 0
11683 && is_aligning_offset (offset, tem))
11684 set_mem_align (op0, BIGGEST_ALIGNMENT);
11686 /* Don't forget about volatility even if this is a bitfield. */
11687 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11689 if (op0 == orig_op0)
11690 op0 = copy_rtx (op0);
11692 MEM_VOLATILE_P (op0) = 1;
11695 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11697 if (op0 == orig_op0)
11698 op0 = copy_rtx (op0);
11700 set_mem_align (op0, BITS_PER_UNIT);
11703 /* In cases where an aligned union has an unaligned object
11704 as a field, we might be extracting a BLKmode value from
11705 an integer-mode (e.g., SImode) object. Handle this case
11706 by doing the extract into an object as wide as the field
11707 (which we know to be the width of a basic mode), then
11708 storing into memory, and changing the mode to BLKmode. */
11709 if (mode1 == VOIDmode
11710 || REG_P (op0) || GET_CODE (op0) == SUBREG
11711 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11712 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11713 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11714 && modifier != EXPAND_CONST_ADDRESS
11715 && modifier != EXPAND_INITIALIZER
11716 && modifier != EXPAND_MEMORY)
11717 /* If the bitfield is volatile and the bitsize
11718 is narrower than the access size of the bitfield,
11719 we need to extract bitfields from the access. */
11720 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11721 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11722 && mode1 != BLKmode
11723 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11724 /* If the field isn't aligned enough to fetch as a memref,
11725 fetch it as a bit field. */
11726 || (mode1 != BLKmode
11727 && (((MEM_P (op0)
11728 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11729 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11730 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11731 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11732 && modifier != EXPAND_MEMORY
11733 && ((modifier == EXPAND_CONST_ADDRESS
11734 || modifier == EXPAND_INITIALIZER)
11735 ? STRICT_ALIGNMENT
11736 : targetm.slow_unaligned_access (mode1,
11737 MEM_ALIGN (op0))))
11738 || !multiple_p (bitpos, BITS_PER_UNIT)))
11739 /* If the type and the field are a constant size and the
11740 size of the type isn't the same size as the bitfield,
11741 we must use bitfield operations. */
11742 || (known_size_p (bitsize)
11743 && TYPE_SIZE (TREE_TYPE (exp))
11744 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11745 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11746 bitsize)))
11748 machine_mode ext_mode = mode;
11750 if (ext_mode == BLKmode
11751 && ! (target != 0 && MEM_P (op0)
11752 && MEM_P (target)
11753 && multiple_p (bitpos, BITS_PER_UNIT)))
11754 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11756 if (ext_mode == BLKmode)
11758 if (target == 0)
11759 target = assign_temp (type, 1, 1);
11761 /* ??? Unlike the similar test a few lines below, this one is
11762 very likely obsolete. */
11763 if (known_eq (bitsize, 0))
11764 return target;
11766 /* In this case, BITPOS must start at a byte boundary and
11767 TARGET, if specified, must be a MEM. */
11768 gcc_assert (MEM_P (op0)
11769 && (!target || MEM_P (target)));
11771 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11772 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11773 emit_block_move (target,
11774 adjust_address (op0, VOIDmode, bytepos),
11775 gen_int_mode (bytesize, Pmode),
11776 (modifier == EXPAND_STACK_PARM
11777 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11779 return target;
11782 /* If we have nothing to extract, the result will be 0 for targets
11783 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11784 return 0 for the sake of consistency, as reading a zero-sized
11785 bitfield is valid in Ada and the value is fully specified. */
11786 if (known_eq (bitsize, 0))
11787 return const0_rtx;
11789 op0 = validize_mem (op0);
11791 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11792 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11794 /* If the result has aggregate type and the extraction is done in
11795 an integral mode, then the field may be not aligned on a byte
11796 boundary; in this case, if it has reverse storage order, it
11797 needs to be extracted as a scalar field with reverse storage
11798 order and put back into memory order afterwards. */
11799 if (AGGREGATE_TYPE_P (type)
11800 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11801 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11803 gcc_checking_assert (known_ge (bitpos, 0));
11804 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11805 (modifier == EXPAND_STACK_PARM
11806 ? NULL_RTX : target),
11807 ext_mode, ext_mode, reversep, alt_rtl);
11809 /* If the result has aggregate type and the mode of OP0 is an
11810 integral mode then, if BITSIZE is narrower than this mode
11811 and this is for big-endian data, we must put the field
11812 into the high-order bits. And we must also put it back
11813 into memory order if it has been previously reversed. */
11814 scalar_int_mode op0_mode;
11815 if (AGGREGATE_TYPE_P (type)
11816 && is_int_mode (GET_MODE (op0), &op0_mode))
11818 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11820 gcc_checking_assert (known_le (bitsize, size));
11821 if (maybe_lt (bitsize, size)
11822 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11823 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11824 size - bitsize, op0, 1);
11826 if (reversep)
11827 op0 = flip_storage_order (op0_mode, op0);
11830 /* If the result type is BLKmode, store the data into a temporary
11831 of the appropriate type, but with the mode corresponding to the
11832 mode for the data we have (op0's mode). */
11833 if (mode == BLKmode)
11835 rtx new_rtx
11836 = assign_stack_temp_for_type (ext_mode,
11837 GET_MODE_BITSIZE (ext_mode),
11838 type);
11839 emit_move_insn (new_rtx, op0);
11840 op0 = copy_rtx (new_rtx);
11841 PUT_MODE (op0, BLKmode);
11844 return op0;
11847 /* If the result is BLKmode, use that to access the object
11848 now as well. */
11849 if (mode == BLKmode)
11850 mode1 = BLKmode;
11852 /* Get a reference to just this component. */
11853 bytepos = bits_to_bytes_round_down (bitpos);
11854 if (modifier == EXPAND_CONST_ADDRESS
11855 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11856 op0 = adjust_address_nv (op0, mode1, bytepos);
11857 else
11858 op0 = adjust_address (op0, mode1, bytepos);
11860 if (op0 == orig_op0)
11861 op0 = copy_rtx (op0);
11863 /* Don't set memory attributes if the base expression is
11864 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11865 we should just honor its original memory attributes. */
11866 if (!(TREE_CODE (tem) == SSA_NAME
11867 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11868 set_mem_attributes (op0, exp, 0);
11870 if (REG_P (XEXP (op0, 0)))
11871 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11873 /* If op0 is a temporary because the original expressions was forced
11874 to memory, clear MEM_EXPR so that the original expression cannot
11875 be marked as addressable through MEM_EXPR of the temporary. */
11876 if (clear_mem_expr)
11877 set_mem_expr (op0, NULL_TREE);
11879 MEM_VOLATILE_P (op0) |= volatilep;
11881 if (reversep
11882 && modifier != EXPAND_MEMORY
11883 && modifier != EXPAND_WRITE)
11884 op0 = flip_storage_order (mode1, op0);
11886 op0 = EXTEND_BITINT (op0);
11888 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11889 || modifier == EXPAND_CONST_ADDRESS
11890 || modifier == EXPAND_INITIALIZER)
11891 return op0;
11893 if (target == 0)
11894 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11896 convert_move (target, op0, unsignedp);
11897 return target;
11900 case OBJ_TYPE_REF:
11901 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11903 case CALL_EXPR:
11904 /* All valid uses of __builtin_va_arg_pack () are removed during
11905 inlining. */
11906 if (CALL_EXPR_VA_ARG_PACK (exp))
11907 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11909 tree fndecl = get_callee_fndecl (exp), attr;
11911 if (fndecl
11912 /* Don't diagnose the error attribute in thunks, those are
11913 artificially created. */
11914 && !CALL_FROM_THUNK_P (exp)
11915 && (attr = lookup_attribute ("error",
11916 DECL_ATTRIBUTES (fndecl))) != NULL)
11918 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11919 error ("call to %qs declared with attribute error: %s",
11920 identifier_to_locale (ident),
11921 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11923 if (fndecl
11924 /* Don't diagnose the warning attribute in thunks, those are
11925 artificially created. */
11926 && !CALL_FROM_THUNK_P (exp)
11927 && (attr = lookup_attribute ("warning",
11928 DECL_ATTRIBUTES (fndecl))) != NULL)
11930 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11931 warning_at (EXPR_LOCATION (exp),
11932 OPT_Wattribute_warning,
11933 "call to %qs declared with attribute warning: %s",
11934 identifier_to_locale (ident),
11935 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11938 /* Check for a built-in function. */
11939 if (fndecl && fndecl_built_in_p (fndecl))
11941 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11942 return expand_builtin (exp, target, subtarget, tmode, ignore);
11945 return expand_call (exp, target, ignore);
11947 case VIEW_CONVERT_EXPR:
11948 op0 = NULL_RTX;
11950 /* If we are converting to BLKmode, try to avoid an intermediate
11951 temporary by fetching an inner memory reference. */
11952 if (mode == BLKmode
11953 && poly_int_tree_p (TYPE_SIZE (type))
11954 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11955 && handled_component_p (treeop0))
11957 machine_mode mode1;
11958 poly_int64 bitsize, bitpos, bytepos;
11959 tree offset;
11960 int reversep, volatilep = 0;
11961 tree tem
11962 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11963 &unsignedp, &reversep, &volatilep);
11965 /* ??? We should work harder and deal with non-zero offsets. */
11966 if (!offset
11967 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11968 && !reversep
11969 && known_size_p (bitsize)
11970 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11972 /* See the normal_inner_ref case for the rationale. */
11973 rtx orig_op0
11974 = expand_expr_real (tem,
11975 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11976 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11977 != INTEGER_CST)
11978 && modifier != EXPAND_STACK_PARM
11979 ? target : NULL_RTX),
11980 VOIDmode,
11981 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11982 NULL, true);
11984 if (MEM_P (orig_op0))
11986 op0 = orig_op0;
11988 /* Get a reference to just this component. */
11989 if (modifier == EXPAND_CONST_ADDRESS
11990 || modifier == EXPAND_SUM
11991 || modifier == EXPAND_INITIALIZER)
11992 op0 = adjust_address_nv (op0, mode, bytepos);
11993 else
11994 op0 = adjust_address (op0, mode, bytepos);
11996 if (op0 == orig_op0)
11997 op0 = copy_rtx (op0);
11999 set_mem_attributes (op0, treeop0, 0);
12000 if (REG_P (XEXP (op0, 0)))
12001 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12003 MEM_VOLATILE_P (op0) |= volatilep;
12008 if (!op0)
12009 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
12010 NULL, inner_reference_p);
12012 /* If the input and output modes are both the same, we are done. */
12013 if (mode == GET_MODE (op0))
12015 /* If neither mode is BLKmode, and both modes are the same size
12016 then we can use gen_lowpart. */
12017 else if (mode != BLKmode
12018 && GET_MODE (op0) != BLKmode
12019 && known_eq (GET_MODE_PRECISION (mode),
12020 GET_MODE_PRECISION (GET_MODE (op0)))
12021 && !COMPLEX_MODE_P (GET_MODE (op0)))
12023 if (GET_CODE (op0) == SUBREG)
12024 op0 = force_reg (GET_MODE (op0), op0);
12025 temp = gen_lowpart_common (mode, op0);
12026 if (temp)
12027 op0 = temp;
12028 else
12030 if (!REG_P (op0) && !MEM_P (op0))
12031 op0 = force_reg (GET_MODE (op0), op0);
12032 op0 = gen_lowpart (mode, op0);
12035 /* If both types are integral, convert from one mode to the other. */
12036 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
12037 op0 = convert_modes (mode, GET_MODE (op0), op0,
12038 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
12039 /* If the output type is a bit-field type, do an extraction. */
12040 else if (reduce_bit_field)
12041 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12042 TYPE_UNSIGNED (type), NULL_RTX,
12043 mode, mode, false, NULL);
12044 /* As a last resort, spill op0 to memory, and reload it in a
12045 different mode. */
12046 else if (!MEM_P (op0))
12048 /* If the operand is not a MEM, force it into memory. Since we
12049 are going to be changing the mode of the MEM, don't call
12050 force_const_mem for constants because we don't allow pool
12051 constants to change mode. */
12052 tree inner_type = TREE_TYPE (treeop0);
12054 gcc_assert (!TREE_ADDRESSABLE (exp));
12056 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
12057 target
12058 = assign_stack_temp_for_type
12059 (TYPE_MODE (inner_type),
12060 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
12062 emit_move_insn (target, op0);
12063 op0 = target;
12066 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
12067 output type is such that the operand is known to be aligned, indicate
12068 that it is. Otherwise, we need only be concerned about alignment for
12069 non-BLKmode results. */
12070 if (MEM_P (op0))
12072 enum insn_code icode;
12074 if (modifier != EXPAND_WRITE
12075 && modifier != EXPAND_MEMORY
12076 && !inner_reference_p
12077 && mode != BLKmode
12078 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12080 /* If the target does have special handling for unaligned
12081 loads of mode then use them. */
12082 if ((icode = optab_handler (movmisalign_optab, mode))
12083 != CODE_FOR_nothing)
12085 rtx reg;
12087 op0 = adjust_address (op0, mode, 0);
12088 /* We've already validated the memory, and we're creating a
12089 new pseudo destination. The predicates really can't
12090 fail. */
12091 reg = gen_reg_rtx (mode);
12093 /* Nor can the insn generator. */
12094 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12095 emit_insn (insn);
12096 return reg;
12098 else if (STRICT_ALIGNMENT)
12100 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12101 poly_uint64 temp_size = mode_size;
12102 if (GET_MODE (op0) != BLKmode)
12103 temp_size = upper_bound (temp_size,
12104 GET_MODE_SIZE (GET_MODE (op0)));
12105 rtx new_rtx
12106 = assign_stack_temp_for_type (mode, temp_size, type);
12107 rtx new_with_op0_mode
12108 = adjust_address (new_rtx, GET_MODE (op0), 0);
12110 gcc_assert (!TREE_ADDRESSABLE (exp));
12112 if (GET_MODE (op0) == BLKmode)
12114 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12115 emit_block_move (new_with_op0_mode, op0, size_rtx,
12116 (modifier == EXPAND_STACK_PARM
12117 ? BLOCK_OP_CALL_PARM
12118 : BLOCK_OP_NORMAL));
12120 else
12121 emit_move_insn (new_with_op0_mode, op0);
12123 op0 = new_rtx;
12127 op0 = adjust_address (op0, mode, 0);
12130 return op0;
12132 case MODIFY_EXPR:
12134 tree lhs = treeop0;
12135 tree rhs = treeop1;
12136 gcc_assert (ignore);
12138 /* Check for |= or &= of a bitfield of size one into another bitfield
12139 of size 1. In this case, (unless we need the result of the
12140 assignment) we can do this more efficiently with a
12141 test followed by an assignment, if necessary.
12143 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12144 things change so we do, this code should be enhanced to
12145 support it. */
12146 if (TREE_CODE (lhs) == COMPONENT_REF
12147 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12148 || TREE_CODE (rhs) == BIT_AND_EXPR)
12149 && TREE_OPERAND (rhs, 0) == lhs
12150 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12151 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12152 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12154 rtx_code_label *label = gen_label_rtx ();
12155 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12156 profile_probability prob = profile_probability::uninitialized ();
12157 if (value)
12158 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12159 else
12160 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12161 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12162 false);
12163 do_pending_stack_adjust ();
12164 emit_label (label);
12165 return const0_rtx;
12168 expand_assignment (lhs, rhs, false);
12169 return const0_rtx;
12172 case ADDR_EXPR:
12173 return expand_expr_addr_expr (exp, target, tmode, modifier);
12175 case REALPART_EXPR:
12176 op0 = expand_normal (treeop0);
12177 return read_complex_part (op0, false);
12179 case IMAGPART_EXPR:
12180 op0 = expand_normal (treeop0);
12181 return read_complex_part (op0, true);
12183 case RETURN_EXPR:
12184 case LABEL_EXPR:
12185 case GOTO_EXPR:
12186 case SWITCH_EXPR:
12187 case ASM_EXPR:
12188 /* Expanded in cfgexpand.cc. */
12189 gcc_unreachable ();
12191 case TRY_CATCH_EXPR:
12192 case CATCH_EXPR:
12193 case EH_FILTER_EXPR:
12194 case TRY_FINALLY_EXPR:
12195 case EH_ELSE_EXPR:
12196 /* Lowered by tree-eh.cc. */
12197 gcc_unreachable ();
12199 case WITH_CLEANUP_EXPR:
12200 case CLEANUP_POINT_EXPR:
12201 case TARGET_EXPR:
12202 case CASE_LABEL_EXPR:
12203 case VA_ARG_EXPR:
12204 case BIND_EXPR:
12205 case INIT_EXPR:
12206 case CONJ_EXPR:
12207 case COMPOUND_EXPR:
12208 case PREINCREMENT_EXPR:
12209 case PREDECREMENT_EXPR:
12210 case POSTINCREMENT_EXPR:
12211 case POSTDECREMENT_EXPR:
12212 case LOOP_EXPR:
12213 case EXIT_EXPR:
12214 case COMPOUND_LITERAL_EXPR:
12215 /* Lowered by gimplify.cc. */
12216 gcc_unreachable ();
12218 case FDESC_EXPR:
12219 /* Function descriptors are not valid except for as
12220 initialization constants, and should not be expanded. */
12221 gcc_unreachable ();
12223 case WITH_SIZE_EXPR:
12224 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12225 have pulled out the size to use in whatever context it needed. */
12226 return expand_expr_real (treeop0, original_target, tmode,
12227 modifier, alt_rtl, inner_reference_p);
12229 default:
12230 return expand_expr_real_2 (&ops, target, tmode, modifier);
12233 #undef EXTEND_BITINT
12235 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12236 signedness of TYPE), possibly returning the result in TARGET.
12237 TYPE is known to be a partial integer type. */
12238 static rtx
12239 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12241 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12242 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12243 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12244 && (!target || GET_MODE (target) == mode));
12246 /* For constant values, reduce using wide_int_to_tree. */
12247 if (poly_int_rtx_p (exp))
12249 auto value = wi::to_poly_wide (exp, mode);
12250 tree t = wide_int_to_tree (type, value);
12251 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12253 else if (TYPE_UNSIGNED (type))
12255 rtx mask = immed_wide_int_const
12256 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12257 return expand_and (mode, exp, mask, target);
12259 else
12261 int count = GET_MODE_PRECISION (mode) - prec;
12262 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12263 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12267 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12268 when applied to the address of EXP produces an address known to be
12269 aligned more than BIGGEST_ALIGNMENT. */
12271 static bool
12272 is_aligning_offset (const_tree offset, const_tree exp)
12274 /* Strip off any conversions. */
12275 while (CONVERT_EXPR_P (offset))
12276 offset = TREE_OPERAND (offset, 0);
12278 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12279 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12280 if (TREE_CODE (offset) != BIT_AND_EXPR
12281 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12282 || compare_tree_int (TREE_OPERAND (offset, 1),
12283 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12284 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12285 return false;
12287 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12288 It must be NEGATE_EXPR. Then strip any more conversions. */
12289 offset = TREE_OPERAND (offset, 0);
12290 while (CONVERT_EXPR_P (offset))
12291 offset = TREE_OPERAND (offset, 0);
12293 if (TREE_CODE (offset) != NEGATE_EXPR)
12294 return false;
12296 offset = TREE_OPERAND (offset, 0);
12297 while (CONVERT_EXPR_P (offset))
12298 offset = TREE_OPERAND (offset, 0);
12300 /* This must now be the address of EXP. */
12301 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12304 /* Return a STRING_CST corresponding to ARG's constant initializer either
12305 if it's a string constant, or, when VALREP is set, any other constant,
12306 or null otherwise.
12307 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12308 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12309 to the size of the byte string. If nonnull, set *DECL to the constant
12310 declaration ARG refers to. */
12312 static tree
12313 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12314 bool valrep = false)
12316 tree dummy = NULL_TREE;
12317 if (!mem_size)
12318 mem_size = &dummy;
12320 /* Store the type of the original expression before conversions
12321 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12322 removed. */
12323 tree argtype = TREE_TYPE (arg);
12325 tree array;
12326 STRIP_NOPS (arg);
12328 /* Non-constant index into the character array in an ARRAY_REF
12329 expression or null. */
12330 tree varidx = NULL_TREE;
12332 poly_int64 base_off = 0;
12334 if (TREE_CODE (arg) == ADDR_EXPR)
12336 arg = TREE_OPERAND (arg, 0);
12337 tree ref = arg;
12338 if (TREE_CODE (arg) == ARRAY_REF)
12340 tree idx = TREE_OPERAND (arg, 1);
12341 if (TREE_CODE (idx) != INTEGER_CST)
12343 /* From a pointer (but not array) argument extract the variable
12344 index to prevent get_addr_base_and_unit_offset() from failing
12345 due to it. Use it later to compute the non-constant offset
12346 into the string and return it to the caller. */
12347 varidx = idx;
12348 ref = TREE_OPERAND (arg, 0);
12350 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12351 return NULL_TREE;
12353 if (!integer_zerop (array_ref_low_bound (arg)))
12354 return NULL_TREE;
12356 if (!integer_onep (array_ref_element_size (arg)))
12357 return NULL_TREE;
12360 array = get_addr_base_and_unit_offset (ref, &base_off);
12361 if (!array
12362 || (TREE_CODE (array) != VAR_DECL
12363 && TREE_CODE (array) != CONST_DECL
12364 && TREE_CODE (array) != STRING_CST))
12365 return NULL_TREE;
12367 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12369 tree arg0 = TREE_OPERAND (arg, 0);
12370 tree arg1 = TREE_OPERAND (arg, 1);
12372 tree offset;
12373 tree str = string_constant (arg0, &offset, mem_size, decl);
12374 if (!str)
12376 str = string_constant (arg1, &offset, mem_size, decl);
12377 arg1 = arg0;
12380 if (str)
12382 /* Avoid pointers to arrays (see bug 86622). */
12383 if (POINTER_TYPE_P (TREE_TYPE (arg))
12384 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12385 && !(decl && !*decl)
12386 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12387 && tree_fits_uhwi_p (*mem_size)
12388 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12389 return NULL_TREE;
12391 tree type = TREE_TYPE (offset);
12392 arg1 = fold_convert (type, arg1);
12393 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12394 return str;
12396 return NULL_TREE;
12398 else if (TREE_CODE (arg) == SSA_NAME)
12400 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12401 if (!is_gimple_assign (stmt))
12402 return NULL_TREE;
12404 tree rhs1 = gimple_assign_rhs1 (stmt);
12405 tree_code code = gimple_assign_rhs_code (stmt);
12406 if (code == ADDR_EXPR)
12407 return string_constant (rhs1, ptr_offset, mem_size, decl);
12408 else if (code != POINTER_PLUS_EXPR)
12409 return NULL_TREE;
12411 tree offset;
12412 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12414 /* Avoid pointers to arrays (see bug 86622). */
12415 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12416 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12417 && !(decl && !*decl)
12418 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12419 && tree_fits_uhwi_p (*mem_size)
12420 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12421 return NULL_TREE;
12423 tree rhs2 = gimple_assign_rhs2 (stmt);
12424 tree type = TREE_TYPE (offset);
12425 rhs2 = fold_convert (type, rhs2);
12426 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12427 return str;
12429 return NULL_TREE;
12431 else if (DECL_P (arg))
12432 array = arg;
12433 else
12434 return NULL_TREE;
12436 tree offset = wide_int_to_tree (sizetype, base_off);
12437 if (varidx)
12439 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12440 return NULL_TREE;
12442 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12443 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12444 if (TREE_CODE (chartype) != INTEGER_TYPE)
12445 return NULL;
12447 offset = fold_convert (sizetype, varidx);
12450 if (TREE_CODE (array) == STRING_CST)
12452 *ptr_offset = fold_convert (sizetype, offset);
12453 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12454 if (decl)
12455 *decl = NULL_TREE;
12456 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12457 >= TREE_STRING_LENGTH (array));
12458 return array;
12461 tree init = ctor_for_folding (array);
12462 if (!init || init == error_mark_node)
12463 return NULL_TREE;
12465 if (valrep)
12467 HOST_WIDE_INT cstoff;
12468 if (!base_off.is_constant (&cstoff))
12469 return NULL_TREE;
12471 /* Check that the host and target are sane. */
12472 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12473 return NULL_TREE;
12475 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12476 if (typesz <= 0 || (int) typesz != typesz)
12477 return NULL_TREE;
12479 HOST_WIDE_INT size = typesz;
12480 if (VAR_P (array)
12481 && DECL_SIZE_UNIT (array)
12482 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12484 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12485 gcc_checking_assert (size >= typesz);
12488 /* If value representation was requested convert the initializer
12489 for the whole array or object into a string of bytes forming
12490 its value representation and return it. */
12491 unsigned char *bytes = XNEWVEC (unsigned char, size);
12492 int r = native_encode_initializer (init, bytes, size);
12493 if (r < typesz)
12495 XDELETEVEC (bytes);
12496 return NULL_TREE;
12499 if (r < size)
12500 memset (bytes + r, '\0', size - r);
12502 const char *p = reinterpret_cast<const char *>(bytes);
12503 init = build_string_literal (size, p, char_type_node);
12504 init = TREE_OPERAND (init, 0);
12505 init = TREE_OPERAND (init, 0);
12506 XDELETE (bytes);
12508 *mem_size = size_int (TREE_STRING_LENGTH (init));
12509 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12511 if (decl)
12512 *decl = array;
12514 return init;
12517 if (TREE_CODE (init) == CONSTRUCTOR)
12519 /* Convert the 64-bit constant offset to a wider type to avoid
12520 overflow and use it to obtain the initializer for the subobject
12521 it points into. */
12522 offset_int wioff;
12523 if (!base_off.is_constant (&wioff))
12524 return NULL_TREE;
12526 wioff *= BITS_PER_UNIT;
12527 if (!wi::fits_uhwi_p (wioff))
12528 return NULL_TREE;
12530 base_off = wioff.to_uhwi ();
12531 unsigned HOST_WIDE_INT fieldoff = 0;
12532 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12533 &fieldoff);
12534 if (!init || init == error_mark_node)
12535 return NULL_TREE;
12537 HOST_WIDE_INT cstoff;
12538 if (!base_off.is_constant (&cstoff))
12539 return NULL_TREE;
12541 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12542 tree off = build_int_cst (sizetype, cstoff);
12543 if (varidx)
12544 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12545 else
12546 offset = off;
12549 *ptr_offset = offset;
12551 tree inittype = TREE_TYPE (init);
12553 if (TREE_CODE (init) == INTEGER_CST
12554 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12555 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12557 /* Check that the host and target are sane. */
12558 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12559 return NULL_TREE;
12561 /* For a reference to (address of) a single constant character,
12562 store the native representation of the character in CHARBUF.
12563 If the reference is to an element of an array or a member
12564 of a struct, only consider narrow characters until ctors
12565 for wide character arrays are transformed to STRING_CSTs
12566 like those for narrow arrays. */
12567 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12568 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12569 if (len > 0)
12571 /* Construct a string literal with elements of INITTYPE and
12572 the representation above. Then strip
12573 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12574 init = build_string_literal (len, (char *)charbuf, inittype);
12575 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12579 tree initsize = TYPE_SIZE_UNIT (inittype);
12581 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12583 /* Fold an empty/zero constructor for an implicitly initialized
12584 object or subobject into the empty string. */
12586 /* Determine the character type from that of the original
12587 expression. */
12588 tree chartype = argtype;
12589 if (POINTER_TYPE_P (chartype))
12590 chartype = TREE_TYPE (chartype);
12591 while (TREE_CODE (chartype) == ARRAY_TYPE)
12592 chartype = TREE_TYPE (chartype);
12594 if (INTEGRAL_TYPE_P (chartype)
12595 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12597 /* Convert a char array to an empty STRING_CST having an array
12598 of the expected type and size. */
12599 if (!initsize)
12600 initsize = integer_zero_node;
12602 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12603 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12604 return NULL_TREE;
12606 init = build_string_literal (size, NULL, chartype, size);
12607 init = TREE_OPERAND (init, 0);
12608 init = TREE_OPERAND (init, 0);
12610 *ptr_offset = integer_zero_node;
12614 if (decl)
12615 *decl = array;
12617 if (TREE_CODE (init) != STRING_CST)
12618 return NULL_TREE;
12620 *mem_size = initsize;
12622 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12624 return init;
12627 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12628 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12629 non-constant) offset in bytes within the string that ARG is accessing.
12630 If MEM_SIZE is non-zero the storage size of the memory is returned.
12631 If DECL is non-zero the constant declaration is returned if available. */
12633 tree
12634 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12636 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12639 /* Similar to string_constant, return a STRING_CST corresponding
12640 to the value representation of the first argument if it's
12641 a constant. */
12643 tree
12644 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12646 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12649 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12650 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12651 for C2 > 0 to x & C3 == C2
12652 for C2 < 0 to x & C3 == (C2 & C3). */
12653 enum tree_code
12654 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12656 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12657 tree treeop0 = gimple_assign_rhs1 (stmt);
12658 tree treeop1 = gimple_assign_rhs2 (stmt);
12659 tree type = TREE_TYPE (*arg0);
12660 scalar_int_mode mode;
12661 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12662 return code;
12663 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12664 || TYPE_PRECISION (type) <= 1
12665 || TYPE_UNSIGNED (type)
12666 /* Signed x % c == 0 should have been optimized into unsigned modulo
12667 earlier. */
12668 || integer_zerop (*arg1)
12669 /* If c is known to be non-negative, modulo will be expanded as unsigned
12670 modulo. */
12671 || get_range_pos_neg (treeop0) == 1)
12672 return code;
12674 /* x % c == d where d < 0 && d <= -c should be always false. */
12675 if (tree_int_cst_sgn (*arg1) == -1
12676 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12677 return code;
12679 int prec = TYPE_PRECISION (type);
12680 wide_int w = wi::to_wide (treeop1) - 1;
12681 w |= wi::shifted_mask (0, prec - 1, true, prec);
12682 tree c3 = wide_int_to_tree (type, w);
12683 tree c4 = *arg1;
12684 if (tree_int_cst_sgn (*arg1) == -1)
12685 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12687 rtx op0 = expand_normal (treeop0);
12688 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12690 bool speed_p = optimize_insn_for_speed_p ();
12692 do_pending_stack_adjust ();
12694 location_t loc = gimple_location (stmt);
12695 struct separate_ops ops;
12696 ops.code = TRUNC_MOD_EXPR;
12697 ops.location = loc;
12698 ops.type = TREE_TYPE (treeop0);
12699 ops.op0 = treeop0;
12700 ops.op1 = treeop1;
12701 ops.op2 = NULL_TREE;
12702 start_sequence ();
12703 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12704 EXPAND_NORMAL);
12705 rtx_insn *moinsns = get_insns ();
12706 end_sequence ();
12708 unsigned mocost = seq_cost (moinsns, speed_p);
12709 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12710 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12712 ops.code = BIT_AND_EXPR;
12713 ops.location = loc;
12714 ops.type = TREE_TYPE (treeop0);
12715 ops.op0 = treeop0;
12716 ops.op1 = c3;
12717 ops.op2 = NULL_TREE;
12718 start_sequence ();
12719 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12720 EXPAND_NORMAL);
12721 rtx_insn *muinsns = get_insns ();
12722 end_sequence ();
12724 unsigned mucost = seq_cost (muinsns, speed_p);
12725 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12726 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12728 if (mocost <= mucost)
12730 emit_insn (moinsns);
12731 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12732 return code;
12735 emit_insn (muinsns);
12736 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12737 *arg1 = c4;
12738 return code;
12741 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12742 If C1 is odd to:
12743 (X - C2) * C3 <= C4 (or >), where
12744 C3 is modular multiplicative inverse of C1 and 1<<prec and
12745 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12746 if C2 > ((1<<prec) - 1) % C1).
12747 If C1 is even, S = ctz (C1) and C2 is 0, use
12748 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12749 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12751 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12752 unsigned):
12753 (X * C3) + C4 <= 2 * C4, where
12754 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12755 C4 is ((1<<(prec - 1) - 1) / C1).
12756 If C1 is even, S = ctz(C1), use
12757 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12758 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12759 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12761 See the Hacker's Delight book, section 10-17. */
12762 enum tree_code
12763 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12765 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12766 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12768 if (optimize < 2)
12769 return code;
12771 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12772 if (stmt == NULL)
12773 return code;
12775 tree treeop0 = gimple_assign_rhs1 (stmt);
12776 tree treeop1 = gimple_assign_rhs2 (stmt);
12777 if (TREE_CODE (treeop0) != SSA_NAME
12778 || TREE_CODE (treeop1) != INTEGER_CST
12779 /* Don't optimize the undefined behavior case x % 0;
12780 x % 1 should have been optimized into zero, punt if
12781 it makes it here for whatever reason;
12782 x % -c should have been optimized into x % c. */
12783 || compare_tree_int (treeop1, 2) <= 0
12784 /* Likewise x % c == d where d >= c should be always false. */
12785 || tree_int_cst_le (treeop1, *arg1))
12786 return code;
12788 /* Unsigned x % pow2 is handled right already, for signed
12789 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12790 if (integer_pow2p (treeop1))
12791 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12793 tree type = TREE_TYPE (*arg0);
12794 scalar_int_mode mode;
12795 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12796 return code;
12797 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12798 || TYPE_PRECISION (type) <= 1)
12799 return code;
12801 signop sgn = UNSIGNED;
12802 /* If both operands are known to have the sign bit clear, handle
12803 even the signed modulo case as unsigned. treeop1 is always
12804 positive >= 2, checked above. */
12805 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12806 sgn = SIGNED;
12808 if (!TYPE_UNSIGNED (type))
12810 if (tree_int_cst_sgn (*arg1) == -1)
12811 return code;
12812 type = unsigned_type_for (type);
12813 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12814 return code;
12817 int prec = TYPE_PRECISION (type);
12818 wide_int w = wi::to_wide (treeop1);
12819 int shift = wi::ctz (w);
12820 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12821 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12822 If C1 is odd, we can handle all cases by subtracting
12823 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12824 e.g. by testing for overflow on the subtraction, punt on that for now
12825 though. */
12826 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12828 if (sgn == SIGNED)
12829 return code;
12830 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12831 if (wi::gtu_p (wi::to_wide (*arg1), x))
12832 return code;
12835 imm_use_iterator imm_iter;
12836 use_operand_p use_p;
12837 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12839 gimple *use_stmt = USE_STMT (use_p);
12840 /* Punt if treeop0 is used in the same bb in a division
12841 or another modulo with the same divisor. We should expect
12842 the division and modulo combined together. */
12843 if (use_stmt == stmt
12844 || gimple_bb (use_stmt) != gimple_bb (stmt))
12845 continue;
12846 if (!is_gimple_assign (use_stmt)
12847 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12848 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12849 continue;
12850 if (gimple_assign_rhs1 (use_stmt) != treeop0
12851 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12852 continue;
12853 return code;
12856 w = wi::lrshift (w, shift);
12857 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12858 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12859 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12860 tree c3 = wide_int_to_tree (type, m);
12861 tree c5 = NULL_TREE;
12862 wide_int d, e;
12863 if (sgn == UNSIGNED)
12865 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12866 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12867 otherwise use < or subtract one from C4. E.g. for
12868 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12869 x % 3U == 1 already needs to be
12870 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12871 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12872 d -= 1;
12873 if (shift)
12874 d = wi::lrshift (d, shift);
12876 else
12878 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12879 if (!shift)
12880 d = wi::lshift (e, 1);
12881 else
12883 e = wi::bit_and (e, wi::mask (shift, true, prec));
12884 d = wi::lrshift (e, shift - 1);
12886 c5 = wide_int_to_tree (type, e);
12888 tree c4 = wide_int_to_tree (type, d);
12890 rtx op0 = expand_normal (treeop0);
12891 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12893 bool speed_p = optimize_insn_for_speed_p ();
12895 do_pending_stack_adjust ();
12897 location_t loc = gimple_location (stmt);
12898 struct separate_ops ops;
12899 ops.code = TRUNC_MOD_EXPR;
12900 ops.location = loc;
12901 ops.type = TREE_TYPE (treeop0);
12902 ops.op0 = treeop0;
12903 ops.op1 = treeop1;
12904 ops.op2 = NULL_TREE;
12905 start_sequence ();
12906 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12907 EXPAND_NORMAL);
12908 rtx_insn *moinsns = get_insns ();
12909 end_sequence ();
12911 unsigned mocost = seq_cost (moinsns, speed_p);
12912 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12913 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12915 tree t = fold_convert_loc (loc, type, treeop0);
12916 if (!integer_zerop (*arg1))
12917 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12918 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12919 if (sgn == SIGNED)
12920 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12921 if (shift)
12923 tree s = build_int_cst (NULL_TREE, shift);
12924 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12927 start_sequence ();
12928 rtx mur = expand_normal (t);
12929 rtx_insn *muinsns = get_insns ();
12930 end_sequence ();
12932 unsigned mucost = seq_cost (muinsns, speed_p);
12933 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12934 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12936 if (mocost <= mucost)
12938 emit_insn (moinsns);
12939 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12940 return code;
12943 emit_insn (muinsns);
12944 *arg0 = make_tree (type, mur);
12945 *arg1 = c4;
12946 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12949 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12951 void
12952 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12954 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12955 || code == LT_EXPR || code == LE_EXPR);
12956 gcc_checking_assert (integer_zerop (*arg1));
12958 if (!optimize)
12959 return;
12961 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12962 if (stmt == NULL)
12963 return;
12965 tree treeop0 = gimple_assign_rhs1 (stmt);
12966 tree treeop1 = gimple_assign_rhs2 (stmt);
12967 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12968 return;
12970 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12971 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12972 "assuming signed overflow does not occur when "
12973 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12974 op_symbol_code (code), op_symbol_code (code));
12976 *arg0 = treeop0;
12977 *arg1 = treeop1;
12981 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
12982 a single bit equality/inequality test, returns where the result is located. */
12984 static rtx
12985 expand_single_bit_test (location_t loc, enum tree_code code,
12986 tree inner, int bitnum,
12987 tree result_type, rtx target,
12988 machine_mode mode)
12990 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
12992 tree type = TREE_TYPE (inner);
12993 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
12994 int ops_unsigned;
12995 tree signed_type, unsigned_type, intermediate_type;
12996 gimple *inner_def;
12998 /* First, see if we can fold the single bit test into a sign-bit
12999 test. */
13000 if (bitnum == TYPE_PRECISION (type) - 1
13001 && type_has_mode_precision_p (type))
13003 tree stype = signed_type_for (type);
13004 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13005 result_type,
13006 fold_convert_loc (loc, stype, inner),
13007 build_int_cst (stype, 0));
13008 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
13011 /* Otherwise we have (A & C) != 0 where C is a single bit,
13012 convert that into ((A >> C2) & 1). Where C2 = log2(C).
13013 Similarly for (A & C) == 0. */
13015 /* If INNER is a right shift of a constant and it plus BITNUM does
13016 not overflow, adjust BITNUM and INNER. */
13017 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
13018 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
13019 && bitnum < TYPE_PRECISION (type)
13020 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
13021 TYPE_PRECISION (type) - bitnum))
13023 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
13024 inner = gimple_assign_rhs1 (inner_def);
13027 /* If we are going to be able to omit the AND below, we must do our
13028 operations as unsigned. If we must use the AND, we have a choice.
13029 Normally unsigned is faster, but for some machines signed is. */
13030 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
13031 && !flag_syntax_only) ? 0 : 1;
13033 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
13034 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
13035 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
13036 inner = fold_convert_loc (loc, intermediate_type, inner);
13038 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
13040 if (CONST_SCALAR_INT_P (inner0))
13042 wide_int t = rtx_mode_t (inner0, operand_mode);
13043 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
13044 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
13046 int bitpos = bitnum;
13048 if (BYTES_BIG_ENDIAN)
13049 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
13051 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
13052 operand_mode, mode, 0, NULL);
13054 if (code == EQ_EXPR)
13055 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
13056 NULL_RTX, 1, OPTAB_LIB_WIDEN);
13057 if (GET_MODE (inner0) != mode)
13059 rtx t = gen_reg_rtx (mode);
13060 convert_move (t, inner0, 0);
13061 return t;
13063 return inner0;
13066 /* Generate code to calculate OPS, and exploded expression
13067 using a store-flag instruction and return an rtx for the result.
13068 OPS reflects a comparison.
13070 If TARGET is nonzero, store the result there if convenient.
13072 Return zero if there is no suitable set-flag instruction
13073 available on this machine.
13075 Once expand_expr has been called on the arguments of the comparison,
13076 we are committed to doing the store flag, since it is not safe to
13077 re-evaluate the expression. We emit the store-flag insn by calling
13078 emit_store_flag, but only expand the arguments if we have a reason
13079 to believe that emit_store_flag will be successful. If we think that
13080 it will, but it isn't, we have to simulate the store-flag with a
13081 set/jump/set sequence. */
13083 static rtx
13084 do_store_flag (sepops ops, rtx target, machine_mode mode)
13086 enum rtx_code code;
13087 tree arg0, arg1, type;
13088 machine_mode operand_mode;
13089 int unsignedp;
13090 rtx op0, op1;
13091 rtx subtarget = target;
13092 location_t loc = ops->location;
13094 arg0 = ops->op0;
13095 arg1 = ops->op1;
13097 /* Don't crash if the comparison was erroneous. */
13098 if (arg0 == error_mark_node || arg1 == error_mark_node)
13099 return const0_rtx;
13101 type = TREE_TYPE (arg0);
13102 operand_mode = TYPE_MODE (type);
13103 unsignedp = TYPE_UNSIGNED (type);
13105 /* We won't bother with BLKmode store-flag operations because it would mean
13106 passing a lot of information to emit_store_flag. */
13107 if (operand_mode == BLKmode)
13108 return 0;
13110 /* We won't bother with store-flag operations involving function pointers
13111 when function pointers must be canonicalized before comparisons. */
13112 if (targetm.have_canonicalize_funcptr_for_compare ()
13113 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13114 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13115 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13116 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13117 return 0;
13119 STRIP_NOPS (arg0);
13120 STRIP_NOPS (arg1);
13122 /* For vector typed comparisons emit code to generate the desired
13123 all-ones or all-zeros mask. */
13124 if (VECTOR_TYPE_P (ops->type))
13126 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13127 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13128 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13129 return expand_vec_cmp_expr (ops->type, ifexp, target);
13130 else
13131 gcc_unreachable ();
13134 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13135 into (x - C2) * C3 < C4. */
13136 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13137 && TREE_CODE (arg0) == SSA_NAME
13138 && TREE_CODE (arg1) == INTEGER_CST)
13140 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13141 &arg0, &arg1);
13142 if (new_code != ops->code)
13144 struct separate_ops nops = *ops;
13145 nops.code = ops->code = new_code;
13146 nops.op0 = arg0;
13147 nops.op1 = arg1;
13148 nops.type = TREE_TYPE (arg0);
13149 return do_store_flag (&nops, target, mode);
13153 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13154 if (!unsignedp
13155 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13156 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13157 && integer_zerop (arg1)
13158 && TREE_CODE (arg0) == SSA_NAME)
13159 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13161 /* Get the rtx comparison code to use. We know that EXP is a comparison
13162 operation of some type. Some comparisons against 1 and -1 can be
13163 converted to comparisons with zero. Do so here so that the tests
13164 below will be aware that we have a comparison with zero. These
13165 tests will not catch constants in the first operand, but constants
13166 are rarely passed as the first operand. */
13168 switch (ops->code)
13170 case EQ_EXPR:
13171 code = EQ;
13172 break;
13173 case NE_EXPR:
13174 code = NE;
13175 break;
13176 case LT_EXPR:
13177 if (integer_onep (arg1))
13178 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13179 else
13180 code = unsignedp ? LTU : LT;
13181 break;
13182 case LE_EXPR:
13183 if (! unsignedp && integer_all_onesp (arg1))
13184 arg1 = integer_zero_node, code = LT;
13185 else
13186 code = unsignedp ? LEU : LE;
13187 break;
13188 case GT_EXPR:
13189 if (! unsignedp && integer_all_onesp (arg1))
13190 arg1 = integer_zero_node, code = GE;
13191 else
13192 code = unsignedp ? GTU : GT;
13193 break;
13194 case GE_EXPR:
13195 if (integer_onep (arg1))
13196 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13197 else
13198 code = unsignedp ? GEU : GE;
13199 break;
13201 case UNORDERED_EXPR:
13202 code = UNORDERED;
13203 break;
13204 case ORDERED_EXPR:
13205 code = ORDERED;
13206 break;
13207 case UNLT_EXPR:
13208 code = UNLT;
13209 break;
13210 case UNLE_EXPR:
13211 code = UNLE;
13212 break;
13213 case UNGT_EXPR:
13214 code = UNGT;
13215 break;
13216 case UNGE_EXPR:
13217 code = UNGE;
13218 break;
13219 case UNEQ_EXPR:
13220 code = UNEQ;
13221 break;
13222 case LTGT_EXPR:
13223 code = LTGT;
13224 break;
13226 default:
13227 gcc_unreachable ();
13230 /* Put a constant second. */
13231 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13232 || TREE_CODE (arg0) == FIXED_CST)
13234 std::swap (arg0, arg1);
13235 code = swap_condition (code);
13238 /* If this is an equality or inequality test of a single bit, we can
13239 do this by shifting the bit being tested to the low-order bit and
13240 masking the result with the constant 1. If the condition was EQ,
13241 we xor it with 1. This does not require an scc insn and is faster
13242 than an scc insn even if we have it. */
13244 if ((code == NE || code == EQ)
13245 && (integer_zerop (arg1)
13246 || integer_pow2p (arg1))
13247 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13249 tree narg0 = arg0;
13250 wide_int nz = tree_nonzero_bits (narg0);
13251 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR);
13252 /* If the defining statement was (x & POW2), then use that instead of
13253 the non-zero bits. */
13254 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13256 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13257 narg0 = gimple_assign_rhs1 (srcstmt);
13260 if (wi::popcount (nz) == 1
13261 && (integer_zerop (arg1)
13262 || wi::to_wide (arg1) == nz))
13264 int bitnum = wi::exact_log2 (nz);
13265 enum tree_code tcode = EQ_EXPR;
13266 if ((code == NE) ^ !integer_zerop (arg1))
13267 tcode = NE_EXPR;
13269 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13270 return expand_single_bit_test (loc, tcode,
13271 narg0,
13272 bitnum, type, target, mode);
13277 if (! get_subtarget (target)
13278 || GET_MODE (subtarget) != operand_mode)
13279 subtarget = 0;
13281 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13283 if (target == 0)
13284 target = gen_reg_rtx (mode);
13286 /* Try a cstore if possible. */
13287 return emit_store_flag_force (target, code, op0, op1,
13288 operand_mode, unsignedp,
13289 (TYPE_PRECISION (ops->type) == 1
13290 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13293 /* Attempt to generate a casesi instruction. Returns true if successful,
13294 false otherwise (i.e. if there is no casesi instruction).
13296 DEFAULT_PROBABILITY is the probability of jumping to the default
13297 label. */
13298 bool
13299 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13300 rtx table_label, rtx default_label, rtx fallback_label,
13301 profile_probability default_probability)
13303 class expand_operand ops[5];
13304 scalar_int_mode index_mode = SImode;
13305 rtx op1, op2, index;
13307 if (! targetm.have_casesi ())
13308 return false;
13310 /* The index must be some form of integer. Convert it to SImode. */
13311 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13312 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13314 rtx rangertx = expand_normal (range);
13316 /* We must handle the endpoints in the original mode. */
13317 index_expr = build2 (MINUS_EXPR, index_type,
13318 index_expr, minval);
13319 minval = integer_zero_node;
13320 index = expand_normal (index_expr);
13321 if (default_label)
13322 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13323 omode, 1, default_label,
13324 default_probability);
13325 /* Now we can safely truncate. */
13326 index = convert_to_mode (index_mode, index, 0);
13328 else
13330 if (omode != index_mode)
13332 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13333 index_expr = fold_convert (index_type, index_expr);
13336 index = expand_normal (index_expr);
13339 do_pending_stack_adjust ();
13341 op1 = expand_normal (minval);
13342 op2 = expand_normal (range);
13344 create_input_operand (&ops[0], index, index_mode);
13345 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13346 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13347 create_fixed_operand (&ops[3], table_label);
13348 create_fixed_operand (&ops[4], (default_label
13349 ? default_label
13350 : fallback_label));
13351 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13352 return true;
13355 /* Attempt to generate a tablejump instruction; same concept. */
13356 /* Subroutine of the next function.
13358 INDEX is the value being switched on, with the lowest value
13359 in the table already subtracted.
13360 MODE is its expected mode (needed if INDEX is constant).
13361 RANGE is the length of the jump table.
13362 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13364 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13365 index value is out of range.
13366 DEFAULT_PROBABILITY is the probability of jumping to
13367 the default label. */
13369 static void
13370 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13371 rtx default_label, profile_probability default_probability)
13373 rtx temp, vector;
13375 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13376 cfun->cfg->max_jumptable_ents = INTVAL (range);
13378 /* Do an unsigned comparison (in the proper mode) between the index
13379 expression and the value which represents the length of the range.
13380 Since we just finished subtracting the lower bound of the range
13381 from the index expression, this comparison allows us to simultaneously
13382 check that the original index expression value is both greater than
13383 or equal to the minimum value of the range and less than or equal to
13384 the maximum value of the range. */
13386 if (default_label)
13387 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13388 default_label, default_probability);
13390 /* If index is in range, it must fit in Pmode.
13391 Convert to Pmode so we can index with it. */
13392 if (mode != Pmode)
13394 unsigned int width;
13396 /* We know the value of INDEX is between 0 and RANGE. If we have a
13397 sign-extended subreg, and RANGE does not have the sign bit set, then
13398 we have a value that is valid for both sign and zero extension. In
13399 this case, we get better code if we sign extend. */
13400 if (GET_CODE (index) == SUBREG
13401 && SUBREG_PROMOTED_VAR_P (index)
13402 && SUBREG_PROMOTED_SIGNED_P (index)
13403 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13404 <= HOST_BITS_PER_WIDE_INT)
13405 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13406 index = convert_to_mode (Pmode, index, 0);
13407 else
13408 index = convert_to_mode (Pmode, index, 1);
13411 /* Don't let a MEM slip through, because then INDEX that comes
13412 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13413 and break_out_memory_refs will go to work on it and mess it up. */
13414 #ifdef PIC_CASE_VECTOR_ADDRESS
13415 if (flag_pic && !REG_P (index))
13416 index = copy_to_mode_reg (Pmode, index);
13417 #endif
13419 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13420 GET_MODE_SIZE, because this indicates how large insns are. The other
13421 uses should all be Pmode, because they are addresses. This code
13422 could fail if addresses and insns are not the same size. */
13423 index = simplify_gen_binary (MULT, Pmode, index,
13424 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13425 Pmode));
13426 index = simplify_gen_binary (PLUS, Pmode, index,
13427 gen_rtx_LABEL_REF (Pmode, table_label));
13429 #ifdef PIC_CASE_VECTOR_ADDRESS
13430 if (flag_pic)
13431 index = PIC_CASE_VECTOR_ADDRESS (index);
13432 else
13433 #endif
13434 index = memory_address (CASE_VECTOR_MODE, index);
13435 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13436 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13437 convert_move (temp, vector, 0);
13439 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13441 /* If we are generating PIC code or if the table is PC-relative, the
13442 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13443 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13444 emit_barrier ();
13447 bool
13448 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13449 rtx table_label, rtx default_label,
13450 profile_probability default_probability)
13452 rtx index;
13454 if (! targetm.have_tablejump ())
13455 return false;
13457 index_expr = fold_build2 (MINUS_EXPR, index_type,
13458 fold_convert (index_type, index_expr),
13459 fold_convert (index_type, minval));
13460 index = expand_normal (index_expr);
13461 do_pending_stack_adjust ();
13463 do_tablejump (index, TYPE_MODE (index_type),
13464 convert_modes (TYPE_MODE (index_type),
13465 TYPE_MODE (TREE_TYPE (range)),
13466 expand_normal (range),
13467 TYPE_UNSIGNED (TREE_TYPE (range))),
13468 table_label, default_label, default_probability);
13469 return true;
13472 /* Return a CONST_VECTOR rtx representing vector mask for
13473 a VECTOR_CST of booleans. */
13474 static rtx
13475 const_vector_mask_from_tree (tree exp)
13477 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13478 machine_mode inner = GET_MODE_INNER (mode);
13480 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13481 VECTOR_CST_NELTS_PER_PATTERN (exp));
13482 unsigned int count = builder.encoded_nelts ();
13483 for (unsigned int i = 0; i < count; ++i)
13485 tree elt = VECTOR_CST_ELT (exp, i);
13486 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13487 if (integer_zerop (elt))
13488 builder.quick_push (CONST0_RTX (inner));
13489 else if (integer_onep (elt)
13490 || integer_minus_onep (elt))
13491 builder.quick_push (CONSTM1_RTX (inner));
13492 else
13493 gcc_unreachable ();
13495 return builder.build ();
13498 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13499 static rtx
13500 const_vector_from_tree (tree exp)
13502 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13504 if (initializer_zerop (exp))
13505 return CONST0_RTX (mode);
13507 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13508 return const_vector_mask_from_tree (exp);
13510 machine_mode inner = GET_MODE_INNER (mode);
13512 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13513 VECTOR_CST_NELTS_PER_PATTERN (exp));
13514 unsigned int count = builder.encoded_nelts ();
13515 for (unsigned int i = 0; i < count; ++i)
13517 tree elt = VECTOR_CST_ELT (exp, i);
13518 if (TREE_CODE (elt) == REAL_CST)
13519 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13520 inner));
13521 else if (TREE_CODE (elt) == FIXED_CST)
13522 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13523 inner));
13524 else
13525 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13526 inner));
13528 return builder.build ();
13531 /* Build a decl for a personality function given a language prefix. */
13533 tree
13534 build_personality_function (const char *lang)
13536 const char *unwind_and_version;
13537 tree decl, type;
13538 char *name;
13540 switch (targetm_common.except_unwind_info (&global_options))
13542 case UI_NONE:
13543 return NULL;
13544 case UI_SJLJ:
13545 unwind_and_version = "_sj0";
13546 break;
13547 case UI_DWARF2:
13548 case UI_TARGET:
13549 unwind_and_version = "_v0";
13550 break;
13551 case UI_SEH:
13552 unwind_and_version = "_seh0";
13553 break;
13554 default:
13555 gcc_unreachable ();
13558 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13560 type = build_function_type_list (unsigned_type_node,
13561 integer_type_node, integer_type_node,
13562 long_long_unsigned_type_node,
13563 ptr_type_node, ptr_type_node, NULL_TREE);
13564 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13565 get_identifier (name), type);
13566 DECL_ARTIFICIAL (decl) = 1;
13567 DECL_EXTERNAL (decl) = 1;
13568 TREE_PUBLIC (decl) = 1;
13570 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13571 are the flags assigned by targetm.encode_section_info. */
13572 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13574 return decl;
13577 /* Extracts the personality function of DECL and returns the corresponding
13578 libfunc. */
13581 get_personality_function (tree decl)
13583 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13584 enum eh_personality_kind pk;
13586 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13587 if (pk == eh_personality_none)
13588 return NULL;
13590 if (!personality
13591 && pk == eh_personality_any)
13592 personality = lang_hooks.eh_personality ();
13594 if (pk == eh_personality_lang)
13595 gcc_assert (personality != NULL_TREE);
13597 return XEXP (DECL_RTL (personality), 0);
13600 /* Returns a tree for the size of EXP in bytes. */
13602 static tree
13603 tree_expr_size (const_tree exp)
13605 if (DECL_P (exp)
13606 && DECL_SIZE_UNIT (exp) != 0)
13607 return DECL_SIZE_UNIT (exp);
13608 else
13609 return size_in_bytes (TREE_TYPE (exp));
13612 /* Return an rtx for the size in bytes of the value of EXP. */
13615 expr_size (tree exp)
13617 tree size;
13619 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13620 size = TREE_OPERAND (exp, 1);
13621 else
13623 size = tree_expr_size (exp);
13624 gcc_assert (size);
13625 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13628 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13631 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13632 if the size can vary or is larger than an integer. */
13634 HOST_WIDE_INT
13635 int_expr_size (const_tree exp)
13637 tree size;
13639 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13640 size = TREE_OPERAND (exp, 1);
13641 else
13643 size = tree_expr_size (exp);
13644 gcc_assert (size);
13647 if (size == 0 || !tree_fits_shwi_p (size))
13648 return -1;
13650 return tree_to_shwi (size);