compiler: use correct init order for multi-value initialization
[official-gcc.git] / gcc / expr.cc
blob53af027a4d0ca1a55e66f9c752d5a9e4f6929b1c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2022 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 int 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)));
349 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
350 /* Conversion between decimal float and binary float, same size. */
351 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
352 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
353 tab = sext_optab;
354 else
355 tab = trunc_optab;
357 /* Try converting directly if the insn is supported. */
359 code = convert_optab_handler (tab, to_mode, from_mode);
360 if (code != CODE_FOR_nothing)
362 emit_unop_insn (code, to, from,
363 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
364 return;
367 /* Otherwise use a libcall. */
368 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
370 /* Is this conversion implemented yet? */
371 gcc_assert (libcall);
373 start_sequence ();
374 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
375 from, from_mode);
376 insns = get_insns ();
377 end_sequence ();
378 emit_libcall_block (insns, to, value,
379 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
380 from)
381 : gen_rtx_FLOAT_EXTEND (to_mode, from));
382 return;
385 /* Handle pointer conversion. */ /* SPEE 900220. */
386 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
388 convert_optab ctab;
390 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
391 ctab = trunc_optab;
392 else if (unsignedp)
393 ctab = zext_optab;
394 else
395 ctab = sext_optab;
397 if (convert_optab_handler (ctab, to_mode, from_mode)
398 != CODE_FOR_nothing)
400 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
401 to, from, UNKNOWN);
402 return;
406 /* Targets are expected to provide conversion insns between PxImode and
407 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
408 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
410 scalar_int_mode full_mode
411 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
413 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
414 != CODE_FOR_nothing);
416 if (full_mode != from_mode)
417 from = convert_to_mode (full_mode, from, unsignedp);
418 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
419 to, from, UNKNOWN);
420 return;
422 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
424 rtx new_from;
425 scalar_int_mode full_mode
426 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
427 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
428 enum insn_code icode;
430 icode = convert_optab_handler (ctab, full_mode, from_mode);
431 gcc_assert (icode != CODE_FOR_nothing);
433 if (to_mode == full_mode)
435 emit_unop_insn (icode, to, from, UNKNOWN);
436 return;
439 new_from = gen_reg_rtx (full_mode);
440 emit_unop_insn (icode, new_from, from, UNKNOWN);
442 /* else proceed to integer conversions below. */
443 from_mode = full_mode;
444 from = new_from;
447 /* Make sure both are fixed-point modes or both are not. */
448 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
449 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
450 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
452 /* If we widen from_mode to to_mode and they are in the same class,
453 we won't saturate the result.
454 Otherwise, always saturate the result to play safe. */
455 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
456 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
457 expand_fixed_convert (to, from, 0, 0);
458 else
459 expand_fixed_convert (to, from, 0, 1);
460 return;
463 /* Now both modes are integers. */
465 /* Handle expanding beyond a word. */
466 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
467 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
469 rtx_insn *insns;
470 rtx lowpart;
471 rtx fill_value;
472 rtx lowfrom;
473 int i;
474 scalar_mode lowpart_mode;
475 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
477 /* Try converting directly if the insn is supported. */
478 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
479 != CODE_FOR_nothing)
481 /* If FROM is a SUBREG, put it into a register. Do this
482 so that we always generate the same set of insns for
483 better cse'ing; if an intermediate assignment occurred,
484 we won't be doing the operation directly on the SUBREG. */
485 if (optimize > 0 && GET_CODE (from) == SUBREG)
486 from = force_reg (from_mode, from);
487 emit_unop_insn (code, to, from, equiv_code);
488 return;
490 /* Next, try converting via full word. */
491 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
492 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
493 != CODE_FOR_nothing))
495 rtx word_to = gen_reg_rtx (word_mode);
496 if (REG_P (to))
498 if (reg_overlap_mentioned_p (to, from))
499 from = force_reg (from_mode, from);
500 emit_clobber (to);
502 convert_move (word_to, from, unsignedp);
503 emit_unop_insn (code, to, word_to, equiv_code);
504 return;
507 /* No special multiword conversion insn; do it by hand. */
508 start_sequence ();
510 /* Since we will turn this into a no conflict block, we must ensure
511 the source does not overlap the target so force it into an isolated
512 register when maybe so. Likewise for any MEM input, since the
513 conversion sequence might require several references to it and we
514 must ensure we're getting the same value every time. */
516 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
517 from = force_reg (from_mode, from);
519 /* Get a copy of FROM widened to a word, if necessary. */
520 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
521 lowpart_mode = word_mode;
522 else
523 lowpart_mode = from_mode;
525 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
527 lowpart = gen_lowpart (lowpart_mode, to);
528 emit_move_insn (lowpart, lowfrom);
530 /* Compute the value to put in each remaining word. */
531 if (unsignedp)
532 fill_value = const0_rtx;
533 else
534 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
535 LT, lowfrom, const0_rtx,
536 lowpart_mode, 0, -1);
538 /* Fill the remaining words. */
539 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
541 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
542 rtx subword = operand_subword (to, index, 1, to_mode);
544 gcc_assert (subword);
546 if (fill_value != subword)
547 emit_move_insn (subword, fill_value);
550 insns = get_insns ();
551 end_sequence ();
553 emit_insn (insns);
554 return;
557 /* Truncating multi-word to a word or less. */
558 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
559 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
561 if (!((MEM_P (from)
562 && ! MEM_VOLATILE_P (from)
563 && direct_load[(int) to_mode]
564 && ! mode_dependent_address_p (XEXP (from, 0),
565 MEM_ADDR_SPACE (from)))
566 || REG_P (from)
567 || GET_CODE (from) == SUBREG))
568 from = force_reg (from_mode, from);
569 convert_move (to, gen_lowpart (word_mode, from), 0);
570 return;
573 /* Now follow all the conversions between integers
574 no more than a word long. */
576 /* For truncation, usually we can just refer to FROM in a narrower mode. */
577 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
578 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
580 if (!((MEM_P (from)
581 && ! MEM_VOLATILE_P (from)
582 && direct_load[(int) to_mode]
583 && ! mode_dependent_address_p (XEXP (from, 0),
584 MEM_ADDR_SPACE (from)))
585 || REG_P (from)
586 || GET_CODE (from) == SUBREG))
587 from = force_reg (from_mode, from);
588 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
589 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
590 from = copy_to_reg (from);
591 emit_move_insn (to, gen_lowpart (to_mode, from));
592 return;
595 /* Handle extension. */
596 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
598 /* Convert directly if that works. */
599 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
600 != CODE_FOR_nothing)
602 emit_unop_insn (code, to, from, equiv_code);
603 return;
605 else
607 rtx tmp;
608 int shift_amount;
610 /* Search for a mode to convert via. */
611 opt_scalar_mode intermediate_iter;
612 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
614 scalar_mode intermediate = intermediate_iter.require ();
615 if (((can_extend_p (to_mode, intermediate, unsignedp)
616 != CODE_FOR_nothing)
617 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
618 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
619 intermediate)))
620 && (can_extend_p (intermediate, from_mode, unsignedp)
621 != CODE_FOR_nothing))
623 convert_move (to, convert_to_mode (intermediate, from,
624 unsignedp), unsignedp);
625 return;
629 /* No suitable intermediate mode.
630 Generate what we need with shifts. */
631 shift_amount = (GET_MODE_PRECISION (to_mode)
632 - GET_MODE_PRECISION (from_mode));
633 from = gen_lowpart (to_mode, force_reg (from_mode, from));
634 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
635 to, unsignedp);
636 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
637 to, unsignedp);
638 if (tmp != to)
639 emit_move_insn (to, tmp);
640 return;
644 /* Support special truncate insns for certain modes. */
645 if (convert_optab_handler (trunc_optab, to_mode,
646 from_mode) != CODE_FOR_nothing)
648 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
649 to, from, UNKNOWN);
650 return;
653 /* Handle truncation of volatile memrefs, and so on;
654 the things that couldn't be truncated directly,
655 and for which there was no special instruction.
657 ??? Code above formerly short-circuited this, for most integer
658 mode pairs, with a force_reg in from_mode followed by a recursive
659 call to this routine. Appears always to have been wrong. */
660 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
662 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
663 emit_move_insn (to, temp);
664 return;
667 /* Mode combination is not recognized. */
668 gcc_unreachable ();
671 /* Return an rtx for a value that would result
672 from converting X to mode MODE.
673 Both X and MODE may be floating, or both integer.
674 UNSIGNEDP is nonzero if X is an unsigned value.
675 This can be done by referring to a part of X in place
676 or by copying to a new temporary with conversion. */
679 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
681 return convert_modes (mode, VOIDmode, x, unsignedp);
684 /* Return an rtx for a value that would result
685 from converting X from mode OLDMODE to mode MODE.
686 Both modes may be floating, or both integer.
687 UNSIGNEDP is nonzero if X is an unsigned value.
689 This can be done by referring to a part of X in place
690 or by copying to a new temporary with conversion.
692 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
695 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
697 rtx temp;
698 scalar_int_mode int_mode;
700 /* If FROM is a SUBREG that indicates that we have already done at least
701 the required extension, strip it. */
703 if (GET_CODE (x) == SUBREG
704 && SUBREG_PROMOTED_VAR_P (x)
705 && is_a <scalar_int_mode> (mode, &int_mode)
706 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
707 >= GET_MODE_PRECISION (int_mode))
708 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
710 scalar_int_mode int_orig_mode;
711 scalar_int_mode int_inner_mode;
712 machine_mode orig_mode = GET_MODE (x);
713 x = gen_lowpart (int_mode, SUBREG_REG (x));
715 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
716 the original mode, but narrower than the inner mode. */
717 if (GET_CODE (x) == SUBREG
718 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
719 && GET_MODE_PRECISION (int_mode)
720 > GET_MODE_PRECISION (int_orig_mode)
721 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
722 &int_inner_mode)
723 && GET_MODE_PRECISION (int_inner_mode)
724 > GET_MODE_PRECISION (int_mode))
726 SUBREG_PROMOTED_VAR_P (x) = 1;
727 SUBREG_PROMOTED_SET (x, unsignedp);
731 if (GET_MODE (x) != VOIDmode)
732 oldmode = GET_MODE (x);
734 if (mode == oldmode)
735 return x;
737 if (CONST_SCALAR_INT_P (x)
738 && is_a <scalar_int_mode> (mode, &int_mode))
740 /* If the caller did not tell us the old mode, then there is not
741 much to do with respect to canonicalization. We have to
742 assume that all the bits are significant. */
743 if (!is_a <scalar_int_mode> (oldmode))
744 oldmode = MAX_MODE_INT;
745 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
746 GET_MODE_PRECISION (int_mode),
747 unsignedp ? UNSIGNED : SIGNED);
748 return immed_wide_int_const (w, int_mode);
751 /* We can do this with a gen_lowpart if both desired and current modes
752 are integer, and this is either a constant integer, a register, or a
753 non-volatile MEM. */
754 scalar_int_mode int_oldmode;
755 if (is_int_mode (mode, &int_mode)
756 && is_int_mode (oldmode, &int_oldmode)
757 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
758 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
759 || CONST_POLY_INT_P (x)
760 || (REG_P (x)
761 && (!HARD_REGISTER_P (x)
762 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
763 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
764 return gen_lowpart (int_mode, x);
766 /* Converting from integer constant into mode is always equivalent to an
767 subreg operation. */
768 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
770 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
771 GET_MODE_BITSIZE (oldmode)));
772 return simplify_gen_subreg (mode, x, oldmode, 0);
775 temp = gen_reg_rtx (mode);
776 convert_move (temp, x, unsignedp);
777 return temp;
780 /* Return the largest alignment we can use for doing a move (or store)
781 of MAX_PIECES. ALIGN is the largest alignment we could use. */
783 static unsigned int
784 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
786 scalar_int_mode tmode
787 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
789 if (align >= GET_MODE_ALIGNMENT (tmode))
790 align = GET_MODE_ALIGNMENT (tmode);
791 else
793 scalar_int_mode xmode = NARROWEST_INT_MODE;
794 opt_scalar_int_mode mode_iter;
795 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
797 tmode = mode_iter.require ();
798 if (GET_MODE_SIZE (tmode) > max_pieces
799 || targetm.slow_unaligned_access (tmode, align))
800 break;
801 xmode = tmode;
804 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
807 return align;
810 /* Return the widest QI vector, if QI_MODE is true, or integer mode
811 that is narrower than SIZE bytes. */
813 static fixed_size_mode
814 widest_fixed_size_mode_for_size (unsigned int size, bool qi_vector)
816 fixed_size_mode result = NARROWEST_INT_MODE;
818 gcc_checking_assert (size > 1);
820 /* Use QI vector only if size is wider than a WORD. */
821 if (qi_vector && size > UNITS_PER_WORD)
823 machine_mode mode;
824 fixed_size_mode candidate;
825 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
826 if (is_a<fixed_size_mode> (mode, &candidate)
827 && GET_MODE_INNER (candidate) == QImode)
829 if (GET_MODE_SIZE (candidate) >= size)
830 break;
831 if (optab_handler (vec_duplicate_optab, candidate)
832 != CODE_FOR_nothing)
833 result = candidate;
836 if (result != NARROWEST_INT_MODE)
837 return result;
840 opt_scalar_int_mode tmode;
841 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
842 if (GET_MODE_SIZE (tmode.require ()) < size)
843 result = tmode.require ();
845 return result;
848 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
849 and should be performed piecewise. */
851 static bool
852 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
853 enum by_pieces_operation op)
855 return targetm.use_by_pieces_infrastructure_p (len, align, op,
856 optimize_insn_for_speed_p ());
859 /* Determine whether the LEN bytes can be moved by using several move
860 instructions. Return nonzero if a call to move_by_pieces should
861 succeed. */
863 bool
864 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
866 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
869 /* Return number of insns required to perform operation OP by pieces
870 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
872 unsigned HOST_WIDE_INT
873 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
874 unsigned int max_size, by_pieces_operation op)
876 unsigned HOST_WIDE_INT n_insns = 0;
877 fixed_size_mode mode;
879 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
881 /* NB: Round up L and ALIGN to the widest integer mode for
882 MAX_SIZE. */
883 mode = widest_fixed_size_mode_for_size (max_size,
884 op == SET_BY_PIECES);
885 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
887 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
888 if (up > l)
889 l = up;
890 align = GET_MODE_ALIGNMENT (mode);
894 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
896 while (max_size > 1 && l > 0)
898 mode = widest_fixed_size_mode_for_size (max_size,
899 op == SET_BY_PIECES);
900 enum insn_code icode;
902 unsigned int modesize = GET_MODE_SIZE (mode);
904 icode = optab_handler (mov_optab, mode);
905 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
907 unsigned HOST_WIDE_INT n_pieces = l / modesize;
908 l %= modesize;
909 switch (op)
911 default:
912 n_insns += n_pieces;
913 break;
915 case COMPARE_BY_PIECES:
916 int batch = targetm.compare_by_pieces_branch_ratio (mode);
917 int batch_ops = 4 * batch - 1;
918 unsigned HOST_WIDE_INT full = n_pieces / batch;
919 n_insns += full * batch_ops;
920 if (n_pieces % batch != 0)
921 n_insns++;
922 break;
926 max_size = modesize;
929 gcc_assert (!l);
930 return n_insns;
933 /* Used when performing piecewise block operations, holds information
934 about one of the memory objects involved. The member functions
935 can be used to generate code for loading from the object and
936 updating the address when iterating. */
938 class pieces_addr
940 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
941 stack pushes. */
942 rtx m_obj;
943 /* The address of the object. Can differ from that seen in the
944 MEM rtx if we copied the address to a register. */
945 rtx m_addr;
946 /* Nonzero if the address on the object has an autoincrement already,
947 signifies whether that was an increment or decrement. */
948 signed char m_addr_inc;
949 /* Nonzero if we intend to use autoinc without the address already
950 having autoinc form. We will insert add insns around each memory
951 reference, expecting later passes to form autoinc addressing modes.
952 The only supported options are predecrement and postincrement. */
953 signed char m_explicit_inc;
954 /* True if we have either of the two possible cases of using
955 autoincrement. */
956 bool m_auto;
957 /* True if this is an address to be used for load operations rather
958 than stores. */
959 bool m_is_load;
961 /* Optionally, a function to obtain constants for any given offset into
962 the objects, and data associated with it. */
963 by_pieces_constfn m_constfn;
964 void *m_cfndata;
965 public:
966 pieces_addr (rtx, bool, by_pieces_constfn, void *);
967 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
968 void increment_address (HOST_WIDE_INT);
969 void maybe_predec (HOST_WIDE_INT);
970 void maybe_postinc (HOST_WIDE_INT);
971 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
972 int get_addr_inc ()
974 return m_addr_inc;
978 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
979 true if the operation to be performed on this object is a load
980 rather than a store. For stores, OBJ can be NULL, in which case we
981 assume the operation is a stack push. For loads, the optional
982 CONSTFN and its associated CFNDATA can be used in place of the
983 memory load. */
985 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
986 void *cfndata)
987 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
989 m_addr_inc = 0;
990 m_auto = false;
991 if (obj)
993 rtx addr = XEXP (obj, 0);
994 rtx_code code = GET_CODE (addr);
995 m_addr = addr;
996 bool dec = code == PRE_DEC || code == POST_DEC;
997 bool inc = code == PRE_INC || code == POST_INC;
998 m_auto = inc || dec;
999 if (m_auto)
1000 m_addr_inc = dec ? -1 : 1;
1002 /* While we have always looked for these codes here, the code
1003 implementing the memory operation has never handled them.
1004 Support could be added later if necessary or beneficial. */
1005 gcc_assert (code != PRE_INC && code != POST_DEC);
1007 else
1009 m_addr = NULL_RTX;
1010 if (!is_load)
1012 m_auto = true;
1013 if (STACK_GROWS_DOWNWARD)
1014 m_addr_inc = -1;
1015 else
1016 m_addr_inc = 1;
1018 else
1019 gcc_assert (constfn != NULL);
1021 m_explicit_inc = 0;
1022 if (constfn)
1023 gcc_assert (is_load);
1026 /* Decide whether to use autoinc for an address involved in a memory op.
1027 MODE is the mode of the accesses, REVERSE is true if we've decided to
1028 perform the operation starting from the end, and LEN is the length of
1029 the operation. Don't override an earlier decision to set m_auto. */
1031 void
1032 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1033 HOST_WIDE_INT len)
1035 if (m_auto || m_obj == NULL_RTX)
1036 return;
1038 bool use_predec = (m_is_load
1039 ? USE_LOAD_PRE_DECREMENT (mode)
1040 : USE_STORE_PRE_DECREMENT (mode));
1041 bool use_postinc = (m_is_load
1042 ? USE_LOAD_POST_INCREMENT (mode)
1043 : USE_STORE_POST_INCREMENT (mode));
1044 machine_mode addr_mode = get_address_mode (m_obj);
1046 if (use_predec && reverse)
1048 m_addr = copy_to_mode_reg (addr_mode,
1049 plus_constant (addr_mode,
1050 m_addr, len));
1051 m_auto = true;
1052 m_explicit_inc = -1;
1054 else if (use_postinc && !reverse)
1056 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1057 m_auto = true;
1058 m_explicit_inc = 1;
1060 else if (CONSTANT_P (m_addr))
1061 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1064 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1065 are using autoincrement for this address, we don't add the offset,
1066 but we still modify the MEM's properties. */
1069 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1070 by_pieces_prev *prev)
1072 if (m_constfn)
1073 /* Pass the previous data to m_constfn. */
1074 return m_constfn (m_cfndata, prev, offset, mode);
1075 if (m_obj == NULL_RTX)
1076 return NULL_RTX;
1077 if (m_auto)
1078 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1079 else
1080 return adjust_address (m_obj, mode, offset);
1083 /* Emit an add instruction to increment the address by SIZE. */
1085 void
1086 pieces_addr::increment_address (HOST_WIDE_INT size)
1088 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1089 emit_insn (gen_add2_insn (m_addr, amount));
1092 /* If we are supposed to decrement the address after each access, emit code
1093 to do so now. Increment by SIZE (which has should have the correct sign
1094 already). */
1096 void
1097 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1099 if (m_explicit_inc >= 0)
1100 return;
1101 gcc_assert (HAVE_PRE_DECREMENT);
1102 increment_address (size);
1105 /* If we are supposed to decrement the address after each access, emit code
1106 to do so now. Increment by SIZE. */
1108 void
1109 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1111 if (m_explicit_inc <= 0)
1112 return;
1113 gcc_assert (HAVE_POST_INCREMENT);
1114 increment_address (size);
1117 /* This structure is used by do_op_by_pieces to describe the operation
1118 to be performed. */
1120 class op_by_pieces_d
1122 private:
1123 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1124 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1126 protected:
1127 pieces_addr m_to, m_from;
1128 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1129 use it to check the valid mode size. */
1130 const unsigned HOST_WIDE_INT m_len;
1131 HOST_WIDE_INT m_offset;
1132 unsigned int m_align;
1133 unsigned int m_max_size;
1134 bool m_reverse;
1135 /* True if this is a stack push. */
1136 bool m_push;
1137 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1138 bool m_overlap_op_by_pieces;
1139 /* True if QI vector mode can be used. */
1140 bool m_qi_vector_mode;
1142 /* Virtual functions, overriden by derived classes for the specific
1143 operation. */
1144 virtual void generate (rtx, rtx, machine_mode) = 0;
1145 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1146 virtual void finish_mode (machine_mode)
1150 public:
1151 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1152 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1153 bool = false);
1154 void run ();
1157 /* The constructor for an op_by_pieces_d structure. We require two
1158 objects named TO and FROM, which are identified as loads or stores
1159 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1160 and its associated FROM_CFN_DATA can be used to replace loads with
1161 constant values. MAX_PIECES describes the maximum number of bytes
1162 at a time which can be moved efficiently. LEN describes the length
1163 of the operation. */
1165 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1166 bool to_load, rtx from, bool from_load,
1167 by_pieces_constfn from_cfn,
1168 void *from_cfn_data,
1169 unsigned HOST_WIDE_INT len,
1170 unsigned int align, bool push,
1171 bool qi_vector_mode)
1172 : m_to (to, to_load, NULL, NULL),
1173 m_from (from, from_load, from_cfn, from_cfn_data),
1174 m_len (len), m_max_size (max_pieces + 1),
1175 m_push (push), m_qi_vector_mode (qi_vector_mode)
1177 int toi = m_to.get_addr_inc ();
1178 int fromi = m_from.get_addr_inc ();
1179 if (toi >= 0 && fromi >= 0)
1180 m_reverse = false;
1181 else if (toi <= 0 && fromi <= 0)
1182 m_reverse = true;
1183 else
1184 gcc_unreachable ();
1186 m_offset = m_reverse ? len : 0;
1187 align = MIN (to ? MEM_ALIGN (to) : align,
1188 from ? MEM_ALIGN (from) : align);
1190 /* If copying requires more than two move insns,
1191 copy addresses to registers (to make displacements shorter)
1192 and use post-increment if available. */
1193 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1195 /* Find the mode of the largest comparison. */
1196 fixed_size_mode mode
1197 = widest_fixed_size_mode_for_size (m_max_size,
1198 m_qi_vector_mode);
1200 m_from.decide_autoinc (mode, m_reverse, len);
1201 m_to.decide_autoinc (mode, m_reverse, len);
1204 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1205 m_align = align;
1207 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1210 /* This function returns the largest usable integer mode for LEN bytes
1211 whose size is no bigger than size of MODE. */
1213 fixed_size_mode
1214 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1216 unsigned int size;
1219 size = GET_MODE_SIZE (mode);
1220 if (len >= size && prepare_mode (mode, m_align))
1221 break;
1222 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1223 mode = widest_fixed_size_mode_for_size (size, m_qi_vector_mode);
1225 while (1);
1226 return mode;
1229 /* Return the smallest integer or QI vector mode that is not narrower
1230 than SIZE bytes. */
1232 fixed_size_mode
1233 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1235 /* Use QI vector only for > size of WORD. */
1236 if (m_qi_vector_mode && size > UNITS_PER_WORD)
1238 machine_mode mode;
1239 fixed_size_mode candidate;
1240 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1241 if (is_a<fixed_size_mode> (mode, &candidate)
1242 && GET_MODE_INNER (candidate) == QImode)
1244 /* Don't return a mode wider than M_LEN. */
1245 if (GET_MODE_SIZE (candidate) > m_len)
1246 break;
1248 if (GET_MODE_SIZE (candidate) >= size
1249 && (optab_handler (vec_duplicate_optab, candidate)
1250 != CODE_FOR_nothing))
1251 return candidate;
1255 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1258 /* This function contains the main loop used for expanding a block
1259 operation. First move what we can in the largest integer mode,
1260 then go to successively smaller modes. For every access, call
1261 GENFUN with the two operands and the EXTRA_DATA. */
1263 void
1264 op_by_pieces_d::run ()
1266 if (m_len == 0)
1267 return;
1269 unsigned HOST_WIDE_INT length = m_len;
1271 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1272 fixed_size_mode mode
1273 = widest_fixed_size_mode_for_size (m_max_size, m_qi_vector_mode);
1274 mode = get_usable_mode (mode, length);
1276 by_pieces_prev to_prev = { nullptr, mode };
1277 by_pieces_prev from_prev = { nullptr, mode };
1281 unsigned int size = GET_MODE_SIZE (mode);
1282 rtx to1 = NULL_RTX, from1;
1284 while (length >= size)
1286 if (m_reverse)
1287 m_offset -= size;
1289 to1 = m_to.adjust (mode, m_offset, &to_prev);
1290 to_prev.data = to1;
1291 to_prev.mode = mode;
1292 from1 = m_from.adjust (mode, m_offset, &from_prev);
1293 from_prev.data = from1;
1294 from_prev.mode = mode;
1296 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1297 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1299 generate (to1, from1, mode);
1301 m_to.maybe_postinc (size);
1302 m_from.maybe_postinc (size);
1304 if (!m_reverse)
1305 m_offset += size;
1307 length -= size;
1310 finish_mode (mode);
1312 if (length == 0)
1313 return;
1315 if (!m_push && m_overlap_op_by_pieces)
1317 /* NB: Generate overlapping operations if it is not a stack
1318 push since stack push must not overlap. Get the smallest
1319 fixed size mode for M_LEN bytes. */
1320 mode = smallest_fixed_size_mode_for_size (length);
1321 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1322 int gap = GET_MODE_SIZE (mode) - length;
1323 if (gap > 0)
1325 /* If size of MODE > M_LEN, generate the last operation
1326 in MODE for the remaining bytes with ovelapping memory
1327 from the previois operation. */
1328 if (m_reverse)
1329 m_offset += gap;
1330 else
1331 m_offset -= gap;
1332 length += gap;
1335 else
1337 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1338 mode = widest_fixed_size_mode_for_size (size,
1339 m_qi_vector_mode);
1340 mode = get_usable_mode (mode, length);
1343 while (1);
1346 /* Derived class from op_by_pieces_d, providing support for block move
1347 operations. */
1349 #ifdef PUSH_ROUNDING
1350 #define PUSHG_P(to) ((to) == nullptr)
1351 #else
1352 #define PUSHG_P(to) false
1353 #endif
1355 class move_by_pieces_d : public op_by_pieces_d
1357 insn_gen_fn m_gen_fun;
1358 void generate (rtx, rtx, machine_mode) final override;
1359 bool prepare_mode (machine_mode, unsigned int) final override;
1361 public:
1362 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1363 unsigned int align)
1364 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1365 NULL, len, align, PUSHG_P (to))
1368 rtx finish_retmode (memop_ret);
1371 /* Return true if MODE can be used for a set of copies, given an
1372 alignment ALIGN. Prepare whatever data is necessary for later
1373 calls to generate. */
1375 bool
1376 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1378 insn_code icode = optab_handler (mov_optab, mode);
1379 m_gen_fun = GEN_FCN (icode);
1380 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1383 /* A callback used when iterating for a compare_by_pieces_operation.
1384 OP0 and OP1 are the values that have been loaded and should be
1385 compared in MODE. If OP0 is NULL, this means we should generate a
1386 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1387 gen function that should be used to generate the mode. */
1389 void
1390 move_by_pieces_d::generate (rtx op0, rtx op1,
1391 machine_mode mode ATTRIBUTE_UNUSED)
1393 #ifdef PUSH_ROUNDING
1394 if (op0 == NULL_RTX)
1396 emit_single_push_insn (mode, op1, NULL);
1397 return;
1399 #endif
1400 emit_insn (m_gen_fun (op0, op1));
1403 /* Perform the final adjustment at the end of a string to obtain the
1404 correct return value for the block operation.
1405 Return value is based on RETMODE argument. */
1408 move_by_pieces_d::finish_retmode (memop_ret retmode)
1410 gcc_assert (!m_reverse);
1411 if (retmode == RETURN_END_MINUS_ONE)
1413 m_to.maybe_postinc (-1);
1414 --m_offset;
1416 return m_to.adjust (QImode, m_offset);
1419 /* Generate several move instructions to copy LEN bytes from block FROM to
1420 block TO. (These are MEM rtx's with BLKmode).
1422 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1423 used to push FROM to the stack.
1425 ALIGN is maximum stack alignment we can assume.
1427 Return value is based on RETMODE argument. */
1430 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1431 unsigned int align, memop_ret retmode)
1433 #ifndef PUSH_ROUNDING
1434 if (to == NULL)
1435 gcc_unreachable ();
1436 #endif
1438 move_by_pieces_d data (to, from, len, align);
1440 data.run ();
1442 if (retmode != RETURN_BEGIN)
1443 return data.finish_retmode (retmode);
1444 else
1445 return to;
1448 /* Derived class from op_by_pieces_d, providing support for block move
1449 operations. */
1451 class store_by_pieces_d : public op_by_pieces_d
1453 insn_gen_fn m_gen_fun;
1454 void generate (rtx, rtx, machine_mode) final override;
1455 bool prepare_mode (machine_mode, unsigned int) final override;
1457 public:
1458 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1459 unsigned HOST_WIDE_INT len, unsigned int align,
1460 bool qi_vector_mode)
1461 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1462 cfn_data, len, align, false, qi_vector_mode)
1465 rtx finish_retmode (memop_ret);
1468 /* Return true if MODE can be used for a set of stores, given an
1469 alignment ALIGN. Prepare whatever data is necessary for later
1470 calls to generate. */
1472 bool
1473 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1475 insn_code icode = optab_handler (mov_optab, mode);
1476 m_gen_fun = GEN_FCN (icode);
1477 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1480 /* A callback used when iterating for a store_by_pieces_operation.
1481 OP0 and OP1 are the values that have been loaded and should be
1482 compared in MODE. If OP0 is NULL, this means we should generate a
1483 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1484 gen function that should be used to generate the mode. */
1486 void
1487 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1489 emit_insn (m_gen_fun (op0, op1));
1492 /* Perform the final adjustment at the end of a string to obtain the
1493 correct return value for the block operation.
1494 Return value is based on RETMODE argument. */
1497 store_by_pieces_d::finish_retmode (memop_ret retmode)
1499 gcc_assert (!m_reverse);
1500 if (retmode == RETURN_END_MINUS_ONE)
1502 m_to.maybe_postinc (-1);
1503 --m_offset;
1505 return m_to.adjust (QImode, m_offset);
1508 /* Determine whether the LEN bytes generated by CONSTFUN can be
1509 stored to memory using several move instructions. CONSTFUNDATA is
1510 a pointer which will be passed as argument in every CONSTFUN call.
1511 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1512 a memset operation and false if it's a copy of a constant string.
1513 Return nonzero if a call to store_by_pieces should succeed. */
1516 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1517 by_pieces_constfn constfun,
1518 void *constfundata, unsigned int align, bool memsetp)
1520 unsigned HOST_WIDE_INT l;
1521 unsigned int max_size;
1522 HOST_WIDE_INT offset = 0;
1523 enum insn_code icode;
1524 int reverse;
1525 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1526 rtx cst ATTRIBUTE_UNUSED;
1528 if (len == 0)
1529 return 1;
1531 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1532 memsetp
1533 ? SET_BY_PIECES
1534 : STORE_BY_PIECES,
1535 optimize_insn_for_speed_p ()))
1536 return 0;
1538 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1540 /* We would first store what we can in the largest integer mode, then go to
1541 successively smaller modes. */
1543 for (reverse = 0;
1544 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1545 reverse++)
1547 l = len;
1548 max_size = STORE_MAX_PIECES + 1;
1549 while (max_size > 1 && l > 0)
1551 fixed_size_mode mode
1552 = widest_fixed_size_mode_for_size (max_size, memsetp);
1554 icode = optab_handler (mov_optab, mode);
1555 if (icode != CODE_FOR_nothing
1556 && align >= GET_MODE_ALIGNMENT (mode))
1558 unsigned int size = GET_MODE_SIZE (mode);
1560 while (l >= size)
1562 if (reverse)
1563 offset -= size;
1565 cst = (*constfun) (constfundata, nullptr, offset, mode);
1566 /* All CONST_VECTORs can be loaded for memset since
1567 vec_duplicate_optab is a precondition to pick a
1568 vector mode for the memset expander. */
1569 if (!((memsetp && VECTOR_MODE_P (mode))
1570 || targetm.legitimate_constant_p (mode, cst)))
1571 return 0;
1573 if (!reverse)
1574 offset += size;
1576 l -= size;
1580 max_size = GET_MODE_SIZE (mode);
1583 /* The code above should have handled everything. */
1584 gcc_assert (!l);
1587 return 1;
1590 /* Generate several move instructions to store LEN bytes generated by
1591 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1592 pointer which will be passed as argument in every CONSTFUN call.
1593 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1594 a memset operation and false if it's a copy of a constant string.
1595 Return value is based on RETMODE argument. */
1598 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1599 by_pieces_constfn constfun,
1600 void *constfundata, unsigned int align, bool memsetp,
1601 memop_ret retmode)
1603 if (len == 0)
1605 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1606 return to;
1609 gcc_assert (targetm.use_by_pieces_infrastructure_p
1610 (len, align,
1611 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1612 optimize_insn_for_speed_p ()));
1614 store_by_pieces_d data (to, constfun, constfundata, len, align,
1615 memsetp);
1616 data.run ();
1618 if (retmode != RETURN_BEGIN)
1619 return data.finish_retmode (retmode);
1620 else
1621 return to;
1624 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1625 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1627 static void
1628 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1630 if (len == 0)
1631 return;
1633 /* Use builtin_memset_read_str to support vector mode broadcast. */
1634 char c = 0;
1635 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1636 true);
1637 data.run ();
1640 /* Context used by compare_by_pieces_genfn. It stores the fail label
1641 to jump to in case of miscomparison, and for branch ratios greater than 1,
1642 it stores an accumulator and the current and maximum counts before
1643 emitting another branch. */
1645 class compare_by_pieces_d : public op_by_pieces_d
1647 rtx_code_label *m_fail_label;
1648 rtx m_accumulator;
1649 int m_count, m_batch;
1651 void generate (rtx, rtx, machine_mode) final override;
1652 bool prepare_mode (machine_mode, unsigned int) final override;
1653 void finish_mode (machine_mode) final override;
1654 public:
1655 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1656 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1657 rtx_code_label *fail_label)
1658 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1659 op1_cfn_data, len, align, false)
1661 m_fail_label = fail_label;
1665 /* A callback used when iterating for a compare_by_pieces_operation.
1666 OP0 and OP1 are the values that have been loaded and should be
1667 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1668 context structure. */
1670 void
1671 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1673 if (m_batch > 1)
1675 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1676 true, OPTAB_LIB_WIDEN);
1677 if (m_count != 0)
1678 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1679 true, OPTAB_LIB_WIDEN);
1680 m_accumulator = temp;
1682 if (++m_count < m_batch)
1683 return;
1685 m_count = 0;
1686 op0 = m_accumulator;
1687 op1 = const0_rtx;
1688 m_accumulator = NULL_RTX;
1690 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1691 m_fail_label, profile_probability::uninitialized ());
1694 /* Return true if MODE can be used for a set of moves and comparisons,
1695 given an alignment ALIGN. Prepare whatever data is necessary for
1696 later calls to generate. */
1698 bool
1699 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1701 insn_code icode = optab_handler (mov_optab, mode);
1702 if (icode == CODE_FOR_nothing
1703 || align < GET_MODE_ALIGNMENT (mode)
1704 || !can_compare_p (EQ, mode, ccp_jump))
1705 return false;
1706 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1707 if (m_batch < 0)
1708 return false;
1709 m_accumulator = NULL_RTX;
1710 m_count = 0;
1711 return true;
1714 /* Called after expanding a series of comparisons in MODE. If we have
1715 accumulated results for which we haven't emitted a branch yet, do
1716 so now. */
1718 void
1719 compare_by_pieces_d::finish_mode (machine_mode mode)
1721 if (m_accumulator != NULL_RTX)
1722 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1723 NULL_RTX, NULL, m_fail_label,
1724 profile_probability::uninitialized ());
1727 /* Generate several move instructions to compare LEN bytes from blocks
1728 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1730 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1731 used to push FROM to the stack.
1733 ALIGN is maximum stack alignment we can assume.
1735 Optionally, the caller can pass a constfn and associated data in A1_CFN
1736 and A1_CFN_DATA. describing that the second operand being compared is a
1737 known constant and how to obtain its data. */
1739 static rtx
1740 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1741 rtx target, unsigned int align,
1742 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1744 rtx_code_label *fail_label = gen_label_rtx ();
1745 rtx_code_label *end_label = gen_label_rtx ();
1747 if (target == NULL_RTX
1748 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1749 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1751 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1752 fail_label);
1754 data.run ();
1756 emit_move_insn (target, const0_rtx);
1757 emit_jump (end_label);
1758 emit_barrier ();
1759 emit_label (fail_label);
1760 emit_move_insn (target, const1_rtx);
1761 emit_label (end_label);
1763 return target;
1766 /* Emit code to move a block Y to a block X. This may be done with
1767 string-move instructions, with multiple scalar move instructions,
1768 or with a library call.
1770 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1771 SIZE is an rtx that says how long they are.
1772 ALIGN is the maximum alignment we can assume they have.
1773 METHOD describes what kind of copy this is, and what mechanisms may be used.
1774 MIN_SIZE is the minimal size of block to move
1775 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1776 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1778 Return the address of the new block, if memcpy is called and returns it,
1779 0 otherwise. */
1782 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1783 unsigned int expected_align, HOST_WIDE_INT expected_size,
1784 unsigned HOST_WIDE_INT min_size,
1785 unsigned HOST_WIDE_INT max_size,
1786 unsigned HOST_WIDE_INT probable_max_size,
1787 bool bail_out_libcall, bool *is_move_done,
1788 bool might_overlap)
1790 int may_use_call;
1791 rtx retval = 0;
1792 unsigned int align;
1794 if (is_move_done)
1795 *is_move_done = true;
1797 gcc_assert (size);
1798 if (CONST_INT_P (size) && INTVAL (size) == 0)
1799 return 0;
1801 switch (method)
1803 case BLOCK_OP_NORMAL:
1804 case BLOCK_OP_TAILCALL:
1805 may_use_call = 1;
1806 break;
1808 case BLOCK_OP_CALL_PARM:
1809 may_use_call = block_move_libcall_safe_for_call_parm ();
1811 /* Make inhibit_defer_pop nonzero around the library call
1812 to force it to pop the arguments right away. */
1813 NO_DEFER_POP;
1814 break;
1816 case BLOCK_OP_NO_LIBCALL:
1817 may_use_call = 0;
1818 break;
1820 case BLOCK_OP_NO_LIBCALL_RET:
1821 may_use_call = -1;
1822 break;
1824 default:
1825 gcc_unreachable ();
1828 gcc_assert (MEM_P (x) && MEM_P (y));
1829 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1830 gcc_assert (align >= BITS_PER_UNIT);
1832 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1833 block copy is more efficient for other large modes, e.g. DCmode. */
1834 x = adjust_address (x, BLKmode, 0);
1835 y = adjust_address (y, BLKmode, 0);
1837 /* If source and destination are the same, no need to copy anything. */
1838 if (rtx_equal_p (x, y)
1839 && !MEM_VOLATILE_P (x)
1840 && !MEM_VOLATILE_P (y))
1841 return 0;
1843 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1844 can be incorrect is coming from __builtin_memcpy. */
1845 poly_int64 const_size;
1846 if (poly_int_rtx_p (size, &const_size))
1848 x = shallow_copy_rtx (x);
1849 y = shallow_copy_rtx (y);
1850 set_mem_size (x, const_size);
1851 set_mem_size (y, const_size);
1854 bool pieces_ok = CONST_INT_P (size)
1855 && can_move_by_pieces (INTVAL (size), align);
1856 bool pattern_ok = false;
1858 if (!pieces_ok || might_overlap)
1860 pattern_ok
1861 = emit_block_move_via_pattern (x, y, size, align,
1862 expected_align, expected_size,
1863 min_size, max_size, probable_max_size,
1864 might_overlap);
1865 if (!pattern_ok && might_overlap)
1867 /* Do not try any of the other methods below as they are not safe
1868 for overlapping moves. */
1869 *is_move_done = false;
1870 return retval;
1874 if (pattern_ok)
1876 else if (pieces_ok)
1877 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1878 else if (may_use_call && !might_overlap
1879 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1880 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1882 if (bail_out_libcall)
1884 if (is_move_done)
1885 *is_move_done = false;
1886 return retval;
1889 if (may_use_call < 0)
1890 return pc_rtx;
1892 retval = emit_block_copy_via_libcall (x, y, size,
1893 method == BLOCK_OP_TAILCALL);
1895 else if (might_overlap)
1896 *is_move_done = false;
1897 else
1898 emit_block_move_via_loop (x, y, size, align);
1900 if (method == BLOCK_OP_CALL_PARM)
1901 OK_DEFER_POP;
1903 return retval;
1907 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1909 unsigned HOST_WIDE_INT max, min = 0;
1910 if (GET_CODE (size) == CONST_INT)
1911 min = max = UINTVAL (size);
1912 else
1913 max = GET_MODE_MASK (GET_MODE (size));
1914 return emit_block_move_hints (x, y, size, method, 0, -1,
1915 min, max, max);
1918 /* A subroutine of emit_block_move. Returns true if calling the
1919 block move libcall will not clobber any parameters which may have
1920 already been placed on the stack. */
1922 static bool
1923 block_move_libcall_safe_for_call_parm (void)
1925 tree fn;
1927 /* If arguments are pushed on the stack, then they're safe. */
1928 if (targetm.calls.push_argument (0))
1929 return true;
1931 /* If registers go on the stack anyway, any argument is sure to clobber
1932 an outgoing argument. */
1933 #if defined (REG_PARM_STACK_SPACE)
1934 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1935 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1936 depend on its argument. */
1937 (void) fn;
1938 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1939 && REG_PARM_STACK_SPACE (fn) != 0)
1940 return false;
1941 #endif
1943 /* If any argument goes in memory, then it might clobber an outgoing
1944 argument. */
1946 CUMULATIVE_ARGS args_so_far_v;
1947 cumulative_args_t args_so_far;
1948 tree arg;
1950 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1951 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1952 args_so_far = pack_cumulative_args (&args_so_far_v);
1954 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1955 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1957 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1958 function_arg_info arg_info (mode, /*named=*/true);
1959 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1960 if (!tmp || !REG_P (tmp))
1961 return false;
1962 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1963 return false;
1964 targetm.calls.function_arg_advance (args_so_far, arg_info);
1967 return true;
1970 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1971 return true if successful.
1973 X is the destination of the copy or move.
1974 Y is the source of the copy or move.
1975 SIZE is the size of the block to be moved.
1977 MIGHT_OVERLAP indicates this originated with expansion of a
1978 builtin_memmove() and the source and destination blocks may
1979 overlap.
1982 static bool
1983 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
1984 unsigned int expected_align,
1985 HOST_WIDE_INT expected_size,
1986 unsigned HOST_WIDE_INT min_size,
1987 unsigned HOST_WIDE_INT max_size,
1988 unsigned HOST_WIDE_INT probable_max_size,
1989 bool might_overlap)
1991 if (expected_align < align)
1992 expected_align = align;
1993 if (expected_size != -1)
1995 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1996 expected_size = probable_max_size;
1997 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1998 expected_size = min_size;
2001 /* Since this is a move insn, we don't care about volatility. */
2002 temporary_volatile_ok v (true);
2004 /* Try the most limited insn first, because there's no point
2005 including more than one in the machine description unless
2006 the more limited one has some advantage. */
2008 opt_scalar_int_mode mode_iter;
2009 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2011 scalar_int_mode mode = mode_iter.require ();
2012 enum insn_code code;
2013 if (might_overlap)
2014 code = direct_optab_handler (movmem_optab, mode);
2015 else
2016 code = direct_optab_handler (cpymem_optab, mode);
2018 if (code != CODE_FOR_nothing
2019 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2020 here because if SIZE is less than the mode mask, as it is
2021 returned by the macro, it will definitely be less than the
2022 actual mode mask. Since SIZE is within the Pmode address
2023 space, we limit MODE to Pmode. */
2024 && ((CONST_INT_P (size)
2025 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2026 <= (GET_MODE_MASK (mode) >> 1)))
2027 || max_size <= (GET_MODE_MASK (mode) >> 1)
2028 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2030 class expand_operand ops[9];
2031 unsigned int nops;
2033 /* ??? When called via emit_block_move_for_call, it'd be
2034 nice if there were some way to inform the backend, so
2035 that it doesn't fail the expansion because it thinks
2036 emitting the libcall would be more efficient. */
2037 nops = insn_data[(int) code].n_generator_args;
2038 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2040 create_fixed_operand (&ops[0], x);
2041 create_fixed_operand (&ops[1], y);
2042 /* The check above guarantees that this size conversion is valid. */
2043 create_convert_operand_to (&ops[2], size, mode, true);
2044 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2045 if (nops >= 6)
2047 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2048 create_integer_operand (&ops[5], expected_size);
2050 if (nops >= 8)
2052 create_integer_operand (&ops[6], min_size);
2053 /* If we cannot represent the maximal size,
2054 make parameter NULL. */
2055 if ((HOST_WIDE_INT) max_size != -1)
2056 create_integer_operand (&ops[7], max_size);
2057 else
2058 create_fixed_operand (&ops[7], NULL);
2060 if (nops == 9)
2062 /* If we cannot represent the maximal size,
2063 make parameter NULL. */
2064 if ((HOST_WIDE_INT) probable_max_size != -1)
2065 create_integer_operand (&ops[8], probable_max_size);
2066 else
2067 create_fixed_operand (&ops[8], NULL);
2069 if (maybe_expand_insn (code, nops, ops))
2070 return true;
2074 return false;
2077 /* A subroutine of emit_block_move. Copy the data via an explicit
2078 loop. This is used only when libcalls are forbidden. */
2079 /* ??? It'd be nice to copy in hunks larger than QImode. */
2081 static void
2082 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2083 unsigned int align ATTRIBUTE_UNUSED)
2085 rtx_code_label *cmp_label, *top_label;
2086 rtx iter, x_addr, y_addr, tmp;
2087 machine_mode x_addr_mode = get_address_mode (x);
2088 machine_mode y_addr_mode = get_address_mode (y);
2089 machine_mode iter_mode;
2091 iter_mode = GET_MODE (size);
2092 if (iter_mode == VOIDmode)
2093 iter_mode = word_mode;
2095 top_label = gen_label_rtx ();
2096 cmp_label = gen_label_rtx ();
2097 iter = gen_reg_rtx (iter_mode);
2099 emit_move_insn (iter, const0_rtx);
2101 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2102 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2103 do_pending_stack_adjust ();
2105 emit_jump (cmp_label);
2106 emit_label (top_label);
2108 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2109 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2111 if (x_addr_mode != y_addr_mode)
2112 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2113 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2115 x = change_address (x, QImode, x_addr);
2116 y = change_address (y, QImode, y_addr);
2118 emit_move_insn (x, y);
2120 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2121 true, OPTAB_LIB_WIDEN);
2122 if (tmp != iter)
2123 emit_move_insn (iter, tmp);
2125 emit_label (cmp_label);
2127 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2128 true, top_label,
2129 profile_probability::guessed_always ()
2130 .apply_scale (9, 10));
2133 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2134 TAILCALL is true if this is a tail call. */
2137 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2138 rtx size, bool tailcall)
2140 rtx dst_addr, src_addr;
2141 tree call_expr, dst_tree, src_tree, size_tree;
2142 machine_mode size_mode;
2144 /* Since dst and src are passed to a libcall, mark the corresponding
2145 tree EXPR as addressable. */
2146 tree dst_expr = MEM_EXPR (dst);
2147 tree src_expr = MEM_EXPR (src);
2148 if (dst_expr)
2149 mark_addressable (dst_expr);
2150 if (src_expr)
2151 mark_addressable (src_expr);
2153 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2154 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2155 dst_tree = make_tree (ptr_type_node, dst_addr);
2157 src_addr = copy_addr_to_reg (XEXP (src, 0));
2158 src_addr = convert_memory_address (ptr_mode, src_addr);
2159 src_tree = make_tree (ptr_type_node, src_addr);
2161 size_mode = TYPE_MODE (sizetype);
2162 size = convert_to_mode (size_mode, size, 1);
2163 size = copy_to_mode_reg (size_mode, size);
2164 size_tree = make_tree (sizetype, size);
2166 /* It is incorrect to use the libcall calling conventions for calls to
2167 memcpy/memmove/memcmp because they can be provided by the user. */
2168 tree fn = builtin_decl_implicit (fncode);
2169 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2170 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2172 return expand_call (call_expr, NULL_RTX, false);
2175 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2176 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2177 otherwise return null. */
2180 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2181 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2182 HOST_WIDE_INT align)
2184 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2186 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2187 target = NULL_RTX;
2189 class expand_operand ops[5];
2190 create_output_operand (&ops[0], target, insn_mode);
2191 create_fixed_operand (&ops[1], arg1_rtx);
2192 create_fixed_operand (&ops[2], arg2_rtx);
2193 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2194 TYPE_UNSIGNED (arg3_type));
2195 create_integer_operand (&ops[4], align);
2196 if (maybe_expand_insn (icode, 5, ops))
2197 return ops[0].value;
2198 return NULL_RTX;
2201 /* Expand a block compare between X and Y with length LEN using the
2202 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2203 of the expression that was used to calculate the length. ALIGN
2204 gives the known minimum common alignment. */
2206 static rtx
2207 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2208 unsigned align)
2210 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2211 implementing memcmp because it will stop if it encounters two
2212 zero bytes. */
2213 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2215 if (icode == CODE_FOR_nothing)
2216 return NULL_RTX;
2218 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2221 /* Emit code to compare a block Y to a block X. This may be done with
2222 string-compare instructions, with multiple scalar instructions,
2223 or with a library call.
2225 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2226 they are. LEN_TYPE is the type of the expression that was used to
2227 calculate it.
2229 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2230 value of a normal memcmp call, instead we can just compare for equality.
2231 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2232 returning NULL_RTX.
2234 Optionally, the caller can pass a constfn and associated data in Y_CFN
2235 and Y_CFN_DATA. describing that the second operand being compared is a
2236 known constant and how to obtain its data.
2237 Return the result of the comparison, or NULL_RTX if we failed to
2238 perform the operation. */
2241 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2242 bool equality_only, by_pieces_constfn y_cfn,
2243 void *y_cfndata)
2245 rtx result = 0;
2247 if (CONST_INT_P (len) && INTVAL (len) == 0)
2248 return const0_rtx;
2250 gcc_assert (MEM_P (x) && MEM_P (y));
2251 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2252 gcc_assert (align >= BITS_PER_UNIT);
2254 x = adjust_address (x, BLKmode, 0);
2255 y = adjust_address (y, BLKmode, 0);
2257 if (equality_only
2258 && CONST_INT_P (len)
2259 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2260 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2261 y_cfn, y_cfndata);
2262 else
2263 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2265 return result;
2268 /* Copy all or part of a value X into registers starting at REGNO.
2269 The number of registers to be filled is NREGS. */
2271 void
2272 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2274 if (nregs == 0)
2275 return;
2277 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2278 x = validize_mem (force_const_mem (mode, x));
2280 /* See if the machine can do this with a load multiple insn. */
2281 if (targetm.have_load_multiple ())
2283 rtx_insn *last = get_last_insn ();
2284 rtx first = gen_rtx_REG (word_mode, regno);
2285 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2286 GEN_INT (nregs)))
2288 emit_insn (pat);
2289 return;
2291 else
2292 delete_insns_since (last);
2295 for (int i = 0; i < nregs; i++)
2296 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2297 operand_subword_force (x, i, mode));
2300 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2301 The number of registers to be filled is NREGS. */
2303 void
2304 move_block_from_reg (int regno, rtx x, int nregs)
2306 if (nregs == 0)
2307 return;
2309 /* See if the machine can do this with a store multiple insn. */
2310 if (targetm.have_store_multiple ())
2312 rtx_insn *last = get_last_insn ();
2313 rtx first = gen_rtx_REG (word_mode, regno);
2314 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2315 GEN_INT (nregs)))
2317 emit_insn (pat);
2318 return;
2320 else
2321 delete_insns_since (last);
2324 for (int i = 0; i < nregs; i++)
2326 rtx tem = operand_subword (x, i, 1, BLKmode);
2328 gcc_assert (tem);
2330 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2334 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2335 ORIG, where ORIG is a non-consecutive group of registers represented by
2336 a PARALLEL. The clone is identical to the original except in that the
2337 original set of registers is replaced by a new set of pseudo registers.
2338 The new set has the same modes as the original set. */
2341 gen_group_rtx (rtx orig)
2343 int i, length;
2344 rtx *tmps;
2346 gcc_assert (GET_CODE (orig) == PARALLEL);
2348 length = XVECLEN (orig, 0);
2349 tmps = XALLOCAVEC (rtx, length);
2351 /* Skip a NULL entry in first slot. */
2352 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2354 if (i)
2355 tmps[0] = 0;
2357 for (; i < length; i++)
2359 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2360 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2362 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2365 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2368 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2369 except that values are placed in TMPS[i], and must later be moved
2370 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2372 static void
2373 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2374 poly_int64 ssize)
2376 rtx src;
2377 int start, i;
2378 machine_mode m = GET_MODE (orig_src);
2380 gcc_assert (GET_CODE (dst) == PARALLEL);
2382 if (m != VOIDmode
2383 && !SCALAR_INT_MODE_P (m)
2384 && !MEM_P (orig_src)
2385 && GET_CODE (orig_src) != CONCAT)
2387 scalar_int_mode imode;
2388 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2390 src = gen_reg_rtx (imode);
2391 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2393 else
2395 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2396 emit_move_insn (src, orig_src);
2398 emit_group_load_1 (tmps, dst, src, type, ssize);
2399 return;
2402 /* Check for a NULL entry, used to indicate that the parameter goes
2403 both on the stack and in registers. */
2404 if (XEXP (XVECEXP (dst, 0, 0), 0))
2405 start = 0;
2406 else
2407 start = 1;
2409 /* Process the pieces. */
2410 for (i = start; i < XVECLEN (dst, 0); i++)
2412 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2413 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2414 poly_int64 bytelen = GET_MODE_SIZE (mode);
2415 poly_int64 shift = 0;
2417 /* Handle trailing fragments that run over the size of the struct.
2418 It's the target's responsibility to make sure that the fragment
2419 cannot be strictly smaller in some cases and strictly larger
2420 in others. */
2421 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2422 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2424 /* Arrange to shift the fragment to where it belongs.
2425 extract_bit_field loads to the lsb of the reg. */
2426 if (
2427 #ifdef BLOCK_REG_PADDING
2428 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2429 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2430 #else
2431 BYTES_BIG_ENDIAN
2432 #endif
2434 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2435 bytelen = ssize - bytepos;
2436 gcc_assert (maybe_gt (bytelen, 0));
2439 /* If we won't be loading directly from memory, protect the real source
2440 from strange tricks we might play; but make sure that the source can
2441 be loaded directly into the destination. */
2442 src = orig_src;
2443 if (!MEM_P (orig_src)
2444 && (!CONSTANT_P (orig_src)
2445 || (GET_MODE (orig_src) != mode
2446 && GET_MODE (orig_src) != VOIDmode)))
2448 if (GET_MODE (orig_src) == VOIDmode)
2449 src = gen_reg_rtx (mode);
2450 else
2451 src = gen_reg_rtx (GET_MODE (orig_src));
2453 emit_move_insn (src, orig_src);
2456 /* Optimize the access just a bit. */
2457 if (MEM_P (src)
2458 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2459 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2460 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2461 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2463 tmps[i] = gen_reg_rtx (mode);
2464 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2466 else if (COMPLEX_MODE_P (mode)
2467 && GET_MODE (src) == mode
2468 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2469 /* Let emit_move_complex do the bulk of the work. */
2470 tmps[i] = src;
2471 else if (GET_CODE (src) == CONCAT)
2473 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2474 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2475 unsigned int elt;
2476 poly_int64 subpos;
2478 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2479 && known_le (subpos + bytelen, slen0))
2481 /* The following assumes that the concatenated objects all
2482 have the same size. In this case, a simple calculation
2483 can be used to determine the object and the bit field
2484 to be extracted. */
2485 tmps[i] = XEXP (src, elt);
2486 if (maybe_ne (subpos, 0)
2487 || maybe_ne (subpos + bytelen, slen0)
2488 || (!CONSTANT_P (tmps[i])
2489 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2490 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2491 subpos * BITS_PER_UNIT,
2492 1, NULL_RTX, mode, mode, false,
2493 NULL);
2495 else
2497 rtx mem;
2499 gcc_assert (known_eq (bytepos, 0));
2500 mem = assign_stack_temp (GET_MODE (src), slen);
2501 emit_move_insn (mem, src);
2502 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2503 0, 1, NULL_RTX, mode, mode, false,
2504 NULL);
2507 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2508 && XVECLEN (dst, 0) > 1)
2509 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2510 else if (CONSTANT_P (src))
2512 if (known_eq (bytelen, ssize))
2513 tmps[i] = src;
2514 else
2516 rtx first, second;
2518 /* TODO: const_wide_int can have sizes other than this... */
2519 gcc_assert (known_eq (2 * bytelen, ssize));
2520 split_double (src, &first, &second);
2521 if (i)
2522 tmps[i] = second;
2523 else
2524 tmps[i] = first;
2527 else if (REG_P (src) && GET_MODE (src) == mode)
2528 tmps[i] = src;
2529 else
2530 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2531 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2532 mode, mode, false, NULL);
2534 if (maybe_ne (shift, 0))
2535 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2536 shift, tmps[i], 0);
2540 /* Emit code to move a block SRC of type TYPE to a block DST,
2541 where DST is non-consecutive registers represented by a PARALLEL.
2542 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2543 if not known. */
2545 void
2546 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2548 rtx *tmps;
2549 int i;
2551 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2552 emit_group_load_1 (tmps, dst, src, type, ssize);
2554 /* Copy the extracted pieces into the proper (probable) hard regs. */
2555 for (i = 0; i < XVECLEN (dst, 0); i++)
2557 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2558 if (d == NULL)
2559 continue;
2560 emit_move_insn (d, tmps[i]);
2564 /* Similar, but load SRC into new pseudos in a format that looks like
2565 PARALLEL. This can later be fed to emit_group_move to get things
2566 in the right place. */
2569 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2571 rtvec vec;
2572 int i;
2574 vec = rtvec_alloc (XVECLEN (parallel, 0));
2575 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2577 /* Convert the vector to look just like the original PARALLEL, except
2578 with the computed values. */
2579 for (i = 0; i < XVECLEN (parallel, 0); i++)
2581 rtx e = XVECEXP (parallel, 0, i);
2582 rtx d = XEXP (e, 0);
2584 if (d)
2586 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2587 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2589 RTVEC_ELT (vec, i) = e;
2592 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2595 /* Emit code to move a block SRC to block DST, where SRC and DST are
2596 non-consecutive groups of registers, each represented by a PARALLEL. */
2598 void
2599 emit_group_move (rtx dst, rtx src)
2601 int i;
2603 gcc_assert (GET_CODE (src) == PARALLEL
2604 && GET_CODE (dst) == PARALLEL
2605 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2607 /* Skip first entry if NULL. */
2608 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2609 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2610 XEXP (XVECEXP (src, 0, i), 0));
2613 /* Move a group of registers represented by a PARALLEL into pseudos. */
2616 emit_group_move_into_temps (rtx src)
2618 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2619 int i;
2621 for (i = 0; i < XVECLEN (src, 0); i++)
2623 rtx e = XVECEXP (src, 0, i);
2624 rtx d = XEXP (e, 0);
2626 if (d)
2627 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2628 RTVEC_ELT (vec, i) = e;
2631 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2634 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2635 where SRC is non-consecutive registers represented by a PARALLEL.
2636 SSIZE represents the total size of block ORIG_DST, or -1 if not
2637 known. */
2639 void
2640 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2641 poly_int64 ssize)
2643 rtx *tmps, dst;
2644 int start, finish, i;
2645 machine_mode m = GET_MODE (orig_dst);
2647 gcc_assert (GET_CODE (src) == PARALLEL);
2649 if (!SCALAR_INT_MODE_P (m)
2650 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2652 scalar_int_mode imode;
2653 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2655 dst = gen_reg_rtx (imode);
2656 emit_group_store (dst, src, type, ssize);
2657 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2659 else
2661 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2662 emit_group_store (dst, src, type, ssize);
2664 emit_move_insn (orig_dst, dst);
2665 return;
2668 /* Check for a NULL entry, used to indicate that the parameter goes
2669 both on the stack and in registers. */
2670 if (XEXP (XVECEXP (src, 0, 0), 0))
2671 start = 0;
2672 else
2673 start = 1;
2674 finish = XVECLEN (src, 0);
2676 tmps = XALLOCAVEC (rtx, finish);
2678 /* Copy the (probable) hard regs into pseudos. */
2679 for (i = start; i < finish; i++)
2681 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2682 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2684 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2685 emit_move_insn (tmps[i], reg);
2687 else
2688 tmps[i] = reg;
2691 /* If we won't be storing directly into memory, protect the real destination
2692 from strange tricks we might play. */
2693 dst = orig_dst;
2694 if (GET_CODE (dst) == PARALLEL)
2696 rtx temp;
2698 /* We can get a PARALLEL dst if there is a conditional expression in
2699 a return statement. In that case, the dst and src are the same,
2700 so no action is necessary. */
2701 if (rtx_equal_p (dst, src))
2702 return;
2704 /* It is unclear if we can ever reach here, but we may as well handle
2705 it. Allocate a temporary, and split this into a store/load to/from
2706 the temporary. */
2707 temp = assign_stack_temp (GET_MODE (dst), ssize);
2708 emit_group_store (temp, src, type, ssize);
2709 emit_group_load (dst, temp, type, ssize);
2710 return;
2712 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2714 machine_mode outer = GET_MODE (dst);
2715 machine_mode inner;
2716 poly_int64 bytepos;
2717 bool done = false;
2718 rtx temp;
2720 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2721 dst = gen_reg_rtx (outer);
2723 /* Make life a bit easier for combine. */
2724 /* If the first element of the vector is the low part
2725 of the destination mode, use a paradoxical subreg to
2726 initialize the destination. */
2727 if (start < finish)
2729 inner = GET_MODE (tmps[start]);
2730 bytepos = subreg_lowpart_offset (inner, outer);
2731 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2732 bytepos))
2734 temp = simplify_gen_subreg (outer, tmps[start],
2735 inner, 0);
2736 if (temp)
2738 emit_move_insn (dst, temp);
2739 done = true;
2740 start++;
2745 /* If the first element wasn't the low part, try the last. */
2746 if (!done
2747 && start < finish - 1)
2749 inner = GET_MODE (tmps[finish - 1]);
2750 bytepos = subreg_lowpart_offset (inner, outer);
2751 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2752 finish - 1), 1)),
2753 bytepos))
2755 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2756 inner, 0);
2757 if (temp)
2759 emit_move_insn (dst, temp);
2760 done = true;
2761 finish--;
2766 /* Otherwise, simply initialize the result to zero. */
2767 if (!done)
2768 emit_move_insn (dst, CONST0_RTX (outer));
2771 /* Process the pieces. */
2772 for (i = start; i < finish; i++)
2774 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2775 machine_mode mode = GET_MODE (tmps[i]);
2776 poly_int64 bytelen = GET_MODE_SIZE (mode);
2777 poly_uint64 adj_bytelen;
2778 rtx dest = dst;
2780 /* Handle trailing fragments that run over the size of the struct.
2781 It's the target's responsibility to make sure that the fragment
2782 cannot be strictly smaller in some cases and strictly larger
2783 in others. */
2784 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2785 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2786 adj_bytelen = ssize - bytepos;
2787 else
2788 adj_bytelen = bytelen;
2790 if (GET_CODE (dst) == CONCAT)
2792 if (known_le (bytepos + adj_bytelen,
2793 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2794 dest = XEXP (dst, 0);
2795 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2797 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2798 dest = XEXP (dst, 1);
2800 else
2802 machine_mode dest_mode = GET_MODE (dest);
2803 machine_mode tmp_mode = GET_MODE (tmps[i]);
2804 scalar_int_mode imode;
2806 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2808 if (finish == 1
2809 && REG_P (tmps[i])
2810 && COMPLEX_MODE_P (dest_mode)
2811 && SCALAR_INT_MODE_P (tmp_mode)
2812 && int_mode_for_mode (dest_mode).exists (&imode))
2814 if (tmp_mode != imode)
2816 rtx tmp = gen_reg_rtx (imode);
2817 emit_move_insn (tmp, gen_lowpart (imode, tmps[i]));
2818 dst = gen_lowpart (dest_mode, tmp);
2820 else
2821 dst = gen_lowpart (dest_mode, tmps[i]);
2823 else if (GET_MODE_ALIGNMENT (dest_mode)
2824 >= GET_MODE_ALIGNMENT (tmp_mode))
2826 dest = assign_stack_temp (dest_mode,
2827 GET_MODE_SIZE (dest_mode));
2828 emit_move_insn (adjust_address (dest,
2829 tmp_mode,
2830 bytepos),
2831 tmps[i]);
2832 dst = dest;
2834 else
2836 dest = assign_stack_temp (tmp_mode,
2837 GET_MODE_SIZE (tmp_mode));
2838 emit_move_insn (dest, tmps[i]);
2839 dst = adjust_address (dest, dest_mode, bytepos);
2841 break;
2845 /* Handle trailing fragments that run over the size of the struct. */
2846 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2848 /* store_bit_field always takes its value from the lsb.
2849 Move the fragment to the lsb if it's not already there. */
2850 if (
2851 #ifdef BLOCK_REG_PADDING
2852 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2853 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2854 #else
2855 BYTES_BIG_ENDIAN
2856 #endif
2859 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2860 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2861 shift, tmps[i], 0);
2864 /* Make sure not to write past the end of the struct. */
2865 store_bit_field (dest,
2866 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2867 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2868 VOIDmode, tmps[i], false);
2871 /* Optimize the access just a bit. */
2872 else if (MEM_P (dest)
2873 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2874 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2875 && multiple_p (bytepos * BITS_PER_UNIT,
2876 GET_MODE_ALIGNMENT (mode))
2877 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2878 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2880 else
2881 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2882 0, 0, mode, tmps[i], false);
2885 /* Copy from the pseudo into the (probable) hard reg. */
2886 if (orig_dst != dst)
2887 emit_move_insn (orig_dst, dst);
2890 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2891 of the value stored in X. */
2894 maybe_emit_group_store (rtx x, tree type)
2896 machine_mode mode = TYPE_MODE (type);
2897 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2898 if (GET_CODE (x) == PARALLEL)
2900 rtx result = gen_reg_rtx (mode);
2901 emit_group_store (result, x, type, int_size_in_bytes (type));
2902 return result;
2904 return x;
2907 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2909 This is used on targets that return BLKmode values in registers. */
2911 static void
2912 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2914 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2915 rtx src = NULL, dst = NULL;
2916 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2917 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2918 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2919 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2920 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2921 fixed_size_mode copy_mode;
2923 /* BLKmode registers created in the back-end shouldn't have survived. */
2924 gcc_assert (mode != BLKmode);
2926 /* If the structure doesn't take up a whole number of words, see whether
2927 SRCREG is padded on the left or on the right. If it's on the left,
2928 set PADDING_CORRECTION to the number of bits to skip.
2930 In most ABIs, the structure will be returned at the least end of
2931 the register, which translates to right padding on little-endian
2932 targets and left padding on big-endian targets. The opposite
2933 holds if the structure is returned at the most significant
2934 end of the register. */
2935 if (bytes % UNITS_PER_WORD != 0
2936 && (targetm.calls.return_in_msb (type)
2937 ? !BYTES_BIG_ENDIAN
2938 : BYTES_BIG_ENDIAN))
2939 padding_correction
2940 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2942 /* We can use a single move if we have an exact mode for the size. */
2943 else if (MEM_P (target)
2944 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2945 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2946 && bytes == GET_MODE_SIZE (mode))
2948 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2949 return;
2952 /* And if we additionally have the same mode for a register. */
2953 else if (REG_P (target)
2954 && GET_MODE (target) == mode
2955 && bytes == GET_MODE_SIZE (mode))
2957 emit_move_insn (target, srcreg);
2958 return;
2961 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2962 into a new pseudo which is a full word. */
2963 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2965 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2966 mode = word_mode;
2969 /* Copy the structure BITSIZE bits at a time. If the target lives in
2970 memory, take care of not reading/writing past its end by selecting
2971 a copy mode suited to BITSIZE. This should always be possible given
2972 how it is computed.
2974 If the target lives in register, make sure not to select a copy mode
2975 larger than the mode of the register.
2977 We could probably emit more efficient code for machines which do not use
2978 strict alignment, but it doesn't seem worth the effort at the current
2979 time. */
2981 copy_mode = word_mode;
2982 if (MEM_P (target))
2984 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2985 if (mem_mode.exists ())
2986 copy_mode = mem_mode.require ();
2988 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2989 copy_mode = tmode;
2991 for (bitpos = 0, xbitpos = padding_correction;
2992 bitpos < bytes * BITS_PER_UNIT;
2993 bitpos += bitsize, xbitpos += bitsize)
2995 /* We need a new source operand each time xbitpos is on a
2996 word boundary and when xbitpos == padding_correction
2997 (the first time through). */
2998 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2999 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3001 /* We need a new destination operand each time bitpos is on
3002 a word boundary. */
3003 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3004 dst = target;
3005 else if (bitpos % BITS_PER_WORD == 0)
3006 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3008 /* Use xbitpos for the source extraction (right justified) and
3009 bitpos for the destination store (left justified). */
3010 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3011 extract_bit_field (src, bitsize,
3012 xbitpos % BITS_PER_WORD, 1,
3013 NULL_RTX, copy_mode, copy_mode,
3014 false, NULL),
3015 false);
3019 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3020 register if it contains any data, otherwise return null.
3022 This is used on targets that return BLKmode values in registers. */
3025 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3027 int i, n_regs;
3028 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3029 unsigned int bitsize;
3030 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3031 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3032 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3033 fixed_size_mode dst_mode;
3034 scalar_int_mode min_mode;
3036 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3038 x = expand_normal (src);
3040 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3041 if (bytes == 0)
3042 return NULL_RTX;
3044 /* If the structure doesn't take up a whole number of words, see
3045 whether the register value should be padded on the left or on
3046 the right. Set PADDING_CORRECTION to the number of padding
3047 bits needed on the left side.
3049 In most ABIs, the structure will be returned at the least end of
3050 the register, which translates to right padding on little-endian
3051 targets and left padding on big-endian targets. The opposite
3052 holds if the structure is returned at the most significant
3053 end of the register. */
3054 if (bytes % UNITS_PER_WORD != 0
3055 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3056 ? !BYTES_BIG_ENDIAN
3057 : BYTES_BIG_ENDIAN))
3058 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3059 * BITS_PER_UNIT));
3061 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3062 dst_words = XALLOCAVEC (rtx, n_regs);
3063 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3064 min_mode = smallest_int_mode_for_size (bitsize);
3066 /* Copy the structure BITSIZE bits at a time. */
3067 for (bitpos = 0, xbitpos = padding_correction;
3068 bitpos < bytes * BITS_PER_UNIT;
3069 bitpos += bitsize, xbitpos += bitsize)
3071 /* We need a new destination pseudo each time xbitpos is
3072 on a word boundary and when xbitpos == padding_correction
3073 (the first time through). */
3074 if (xbitpos % BITS_PER_WORD == 0
3075 || xbitpos == padding_correction)
3077 /* Generate an appropriate register. */
3078 dst_word = gen_reg_rtx (word_mode);
3079 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3081 /* Clear the destination before we move anything into it. */
3082 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3085 /* Find the largest integer mode that can be used to copy all or as
3086 many bits as possible of the structure if the target supports larger
3087 copies. There are too many corner cases here w.r.t to alignments on
3088 the read/writes. So if there is any padding just use single byte
3089 operations. */
3090 opt_scalar_int_mode mode_iter;
3091 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3093 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3095 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3096 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3097 && msize <= BITS_PER_WORD)
3098 bitsize = msize;
3099 else
3100 break;
3104 /* We need a new source operand each time bitpos is on a word
3105 boundary. */
3106 if (bitpos % BITS_PER_WORD == 0)
3107 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3109 /* Use bitpos for the source extraction (left justified) and
3110 xbitpos for the destination store (right justified). */
3111 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3112 0, 0, word_mode,
3113 extract_bit_field (src_word, bitsize,
3114 bitpos % BITS_PER_WORD, 1,
3115 NULL_RTX, word_mode, word_mode,
3116 false, NULL),
3117 false);
3120 if (mode == BLKmode)
3122 /* Find the smallest integer mode large enough to hold the
3123 entire structure. */
3124 opt_scalar_int_mode mode_iter;
3125 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3126 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3127 break;
3129 /* A suitable mode should have been found. */
3130 mode = mode_iter.require ();
3133 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3134 dst_mode = word_mode;
3135 else
3136 dst_mode = mode;
3137 dst = gen_reg_rtx (dst_mode);
3139 for (i = 0; i < n_regs; i++)
3140 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3142 if (mode != dst_mode)
3143 dst = gen_lowpart (mode, dst);
3145 return dst;
3148 /* Add a USE expression for REG to the (possibly empty) list pointed
3149 to by CALL_FUSAGE. REG must denote a hard register. */
3151 void
3152 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3154 gcc_assert (REG_P (reg));
3156 if (!HARD_REGISTER_P (reg))
3157 return;
3159 *call_fusage
3160 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3163 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3164 to by CALL_FUSAGE. REG must denote a hard register. */
3166 void
3167 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3169 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3171 *call_fusage
3172 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3175 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3176 starting at REGNO. All of these registers must be hard registers. */
3178 void
3179 use_regs (rtx *call_fusage, int regno, int nregs)
3181 int i;
3183 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3185 for (i = 0; i < nregs; i++)
3186 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3189 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3190 PARALLEL REGS. This is for calls that pass values in multiple
3191 non-contiguous locations. The Irix 6 ABI has examples of this. */
3193 void
3194 use_group_regs (rtx *call_fusage, rtx regs)
3196 int i;
3198 for (i = 0; i < XVECLEN (regs, 0); i++)
3200 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3202 /* A NULL entry means the parameter goes both on the stack and in
3203 registers. This can also be a MEM for targets that pass values
3204 partially on the stack and partially in registers. */
3205 if (reg != 0 && REG_P (reg))
3206 use_reg (call_fusage, reg);
3210 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3211 assigment and the code of the expresion on the RHS is CODE. Return
3212 NULL otherwise. */
3214 static gimple *
3215 get_def_for_expr (tree name, enum tree_code code)
3217 gimple *def_stmt;
3219 if (TREE_CODE (name) != SSA_NAME)
3220 return NULL;
3222 def_stmt = get_gimple_for_ssa_name (name);
3223 if (!def_stmt
3224 || gimple_assign_rhs_code (def_stmt) != code)
3225 return NULL;
3227 return def_stmt;
3230 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3231 assigment and the class of the expresion on the RHS is CLASS. Return
3232 NULL otherwise. */
3234 static gimple *
3235 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3237 gimple *def_stmt;
3239 if (TREE_CODE (name) != SSA_NAME)
3240 return NULL;
3242 def_stmt = get_gimple_for_ssa_name (name);
3243 if (!def_stmt
3244 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3245 return NULL;
3247 return def_stmt;
3250 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3251 its length in bytes. */
3254 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3255 unsigned int expected_align, HOST_WIDE_INT expected_size,
3256 unsigned HOST_WIDE_INT min_size,
3257 unsigned HOST_WIDE_INT max_size,
3258 unsigned HOST_WIDE_INT probable_max_size,
3259 unsigned ctz_size)
3261 machine_mode mode = GET_MODE (object);
3262 unsigned int align;
3264 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3266 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3267 just move a zero. Otherwise, do this a piece at a time. */
3268 poly_int64 size_val;
3269 if (mode != BLKmode
3270 && poly_int_rtx_p (size, &size_val)
3271 && known_eq (size_val, GET_MODE_SIZE (mode)))
3273 rtx zero = CONST0_RTX (mode);
3274 if (zero != NULL)
3276 emit_move_insn (object, zero);
3277 return NULL;
3280 if (COMPLEX_MODE_P (mode))
3282 zero = CONST0_RTX (GET_MODE_INNER (mode));
3283 if (zero != NULL)
3285 write_complex_part (object, zero, 0);
3286 write_complex_part (object, zero, 1);
3287 return NULL;
3292 if (size == const0_rtx)
3293 return NULL;
3295 align = MEM_ALIGN (object);
3297 if (CONST_INT_P (size)
3298 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3299 CLEAR_BY_PIECES,
3300 optimize_insn_for_speed_p ()))
3301 clear_by_pieces (object, INTVAL (size), align);
3302 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3303 expected_align, expected_size,
3304 min_size, max_size, probable_max_size))
3306 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3307 min_size, max_size,
3308 NULL_RTX, 0, align))
3310 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3311 return set_storage_via_libcall (object, size, const0_rtx,
3312 method == BLOCK_OP_TAILCALL);
3313 else
3314 gcc_unreachable ();
3316 return NULL;
3320 clear_storage (rtx object, rtx size, enum block_op_methods method)
3322 unsigned HOST_WIDE_INT max, min = 0;
3323 if (GET_CODE (size) == CONST_INT)
3324 min = max = UINTVAL (size);
3325 else
3326 max = GET_MODE_MASK (GET_MODE (size));
3327 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3331 /* A subroutine of clear_storage. Expand a call to memset.
3332 Return the return value of memset, 0 otherwise. */
3335 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3337 tree call_expr, fn, object_tree, size_tree, val_tree;
3338 machine_mode size_mode;
3340 object = copy_addr_to_reg (XEXP (object, 0));
3341 object_tree = make_tree (ptr_type_node, object);
3343 if (!CONST_INT_P (val))
3344 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3345 val_tree = make_tree (integer_type_node, val);
3347 size_mode = TYPE_MODE (sizetype);
3348 size = convert_to_mode (size_mode, size, 1);
3349 size = copy_to_mode_reg (size_mode, size);
3350 size_tree = make_tree (sizetype, size);
3352 /* It is incorrect to use the libcall calling conventions for calls to
3353 memset because it can be provided by the user. */
3354 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3355 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3356 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3358 return expand_call (call_expr, NULL_RTX, false);
3361 /* Expand a setmem pattern; return true if successful. */
3363 bool
3364 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3365 unsigned int expected_align, HOST_WIDE_INT expected_size,
3366 unsigned HOST_WIDE_INT min_size,
3367 unsigned HOST_WIDE_INT max_size,
3368 unsigned HOST_WIDE_INT probable_max_size)
3370 /* Try the most limited insn first, because there's no point
3371 including more than one in the machine description unless
3372 the more limited one has some advantage. */
3374 if (expected_align < align)
3375 expected_align = align;
3376 if (expected_size != -1)
3378 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3379 expected_size = max_size;
3380 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3381 expected_size = min_size;
3384 opt_scalar_int_mode mode_iter;
3385 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3387 scalar_int_mode mode = mode_iter.require ();
3388 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3390 if (code != CODE_FOR_nothing
3391 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3392 here because if SIZE is less than the mode mask, as it is
3393 returned by the macro, it will definitely be less than the
3394 actual mode mask. Since SIZE is within the Pmode address
3395 space, we limit MODE to Pmode. */
3396 && ((CONST_INT_P (size)
3397 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3398 <= (GET_MODE_MASK (mode) >> 1)))
3399 || max_size <= (GET_MODE_MASK (mode) >> 1)
3400 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3402 class expand_operand ops[9];
3403 unsigned int nops;
3405 nops = insn_data[(int) code].n_generator_args;
3406 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3408 create_fixed_operand (&ops[0], object);
3409 /* The check above guarantees that this size conversion is valid. */
3410 create_convert_operand_to (&ops[1], size, mode, true);
3411 create_convert_operand_from (&ops[2], val, byte_mode, true);
3412 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3413 if (nops >= 6)
3415 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3416 create_integer_operand (&ops[5], expected_size);
3418 if (nops >= 8)
3420 create_integer_operand (&ops[6], min_size);
3421 /* If we cannot represent the maximal size,
3422 make parameter NULL. */
3423 if ((HOST_WIDE_INT) max_size != -1)
3424 create_integer_operand (&ops[7], max_size);
3425 else
3426 create_fixed_operand (&ops[7], NULL);
3428 if (nops == 9)
3430 /* If we cannot represent the maximal size,
3431 make parameter NULL. */
3432 if ((HOST_WIDE_INT) probable_max_size != -1)
3433 create_integer_operand (&ops[8], probable_max_size);
3434 else
3435 create_fixed_operand (&ops[8], NULL);
3437 if (maybe_expand_insn (code, nops, ops))
3438 return true;
3442 return false;
3446 /* Write to one of the components of the complex value CPLX. Write VAL to
3447 the real part if IMAG_P is false, and the imaginary part if its true. */
3449 void
3450 write_complex_part (rtx cplx, rtx val, bool imag_p)
3452 machine_mode cmode;
3453 scalar_mode imode;
3454 unsigned ibitsize;
3456 if (GET_CODE (cplx) == CONCAT)
3458 emit_move_insn (XEXP (cplx, imag_p), val);
3459 return;
3462 cmode = GET_MODE (cplx);
3463 imode = GET_MODE_INNER (cmode);
3464 ibitsize = GET_MODE_BITSIZE (imode);
3466 /* For MEMs simplify_gen_subreg may generate an invalid new address
3467 because, e.g., the original address is considered mode-dependent
3468 by the target, which restricts simplify_subreg from invoking
3469 adjust_address_nv. Instead of preparing fallback support for an
3470 invalid address, we call adjust_address_nv directly. */
3471 if (MEM_P (cplx))
3473 emit_move_insn (adjust_address_nv (cplx, imode,
3474 imag_p ? GET_MODE_SIZE (imode) : 0),
3475 val);
3476 return;
3479 /* If the sub-object is at least word sized, then we know that subregging
3480 will work. This special case is important, since store_bit_field
3481 wants to operate on integer modes, and there's rarely an OImode to
3482 correspond to TCmode. */
3483 if (ibitsize >= BITS_PER_WORD
3484 /* For hard regs we have exact predicates. Assume we can split
3485 the original object if it spans an even number of hard regs.
3486 This special case is important for SCmode on 64-bit platforms
3487 where the natural size of floating-point regs is 32-bit. */
3488 || (REG_P (cplx)
3489 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3490 && REG_NREGS (cplx) % 2 == 0))
3492 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3493 imag_p ? GET_MODE_SIZE (imode) : 0);
3494 if (part)
3496 emit_move_insn (part, val);
3497 return;
3499 else
3500 /* simplify_gen_subreg may fail for sub-word MEMs. */
3501 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3504 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3505 false);
3508 /* Extract one of the components of the complex value CPLX. Extract the
3509 real part if IMAG_P is false, and the imaginary part if it's true. */
3512 read_complex_part (rtx cplx, bool imag_p)
3514 machine_mode cmode;
3515 scalar_mode imode;
3516 unsigned ibitsize;
3518 if (GET_CODE (cplx) == CONCAT)
3519 return XEXP (cplx, imag_p);
3521 cmode = GET_MODE (cplx);
3522 imode = GET_MODE_INNER (cmode);
3523 ibitsize = GET_MODE_BITSIZE (imode);
3525 /* Special case reads from complex constants that got spilled to memory. */
3526 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3528 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3529 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3531 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3532 if (CONSTANT_CLASS_P (part))
3533 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3537 /* For MEMs simplify_gen_subreg may generate an invalid new address
3538 because, e.g., the original address is considered mode-dependent
3539 by the target, which restricts simplify_subreg from invoking
3540 adjust_address_nv. Instead of preparing fallback support for an
3541 invalid address, we call adjust_address_nv directly. */
3542 if (MEM_P (cplx))
3543 return adjust_address_nv (cplx, imode,
3544 imag_p ? GET_MODE_SIZE (imode) : 0);
3546 /* If the sub-object is at least word sized, then we know that subregging
3547 will work. This special case is important, since extract_bit_field
3548 wants to operate on integer modes, and there's rarely an OImode to
3549 correspond to TCmode. */
3550 if (ibitsize >= BITS_PER_WORD
3551 /* For hard regs we have exact predicates. Assume we can split
3552 the original object if it spans an even number of hard regs.
3553 This special case is important for SCmode on 64-bit platforms
3554 where the natural size of floating-point regs is 32-bit. */
3555 || (REG_P (cplx)
3556 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3557 && REG_NREGS (cplx) % 2 == 0))
3559 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3560 imag_p ? GET_MODE_SIZE (imode) : 0);
3561 if (ret)
3562 return ret;
3563 else
3564 /* simplify_gen_subreg may fail for sub-word MEMs. */
3565 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3568 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3569 true, NULL_RTX, imode, imode, false, NULL);
3572 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3573 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3574 represented in NEW_MODE. If FORCE is true, this will never happen, as
3575 we'll force-create a SUBREG if needed. */
3577 static rtx
3578 emit_move_change_mode (machine_mode new_mode,
3579 machine_mode old_mode, rtx x, bool force)
3581 rtx ret;
3583 if (push_operand (x, GET_MODE (x)))
3585 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3586 MEM_COPY_ATTRIBUTES (ret, x);
3588 else if (MEM_P (x))
3590 /* We don't have to worry about changing the address since the
3591 size in bytes is supposed to be the same. */
3592 if (reload_in_progress)
3594 /* Copy the MEM to change the mode and move any
3595 substitutions from the old MEM to the new one. */
3596 ret = adjust_address_nv (x, new_mode, 0);
3597 copy_replacements (x, ret);
3599 else
3600 ret = adjust_address (x, new_mode, 0);
3602 else
3604 /* Note that we do want simplify_subreg's behavior of validating
3605 that the new mode is ok for a hard register. If we were to use
3606 simplify_gen_subreg, we would create the subreg, but would
3607 probably run into the target not being able to implement it. */
3608 /* Except, of course, when FORCE is true, when this is exactly what
3609 we want. Which is needed for CCmodes on some targets. */
3610 if (force)
3611 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3612 else
3613 ret = simplify_subreg (new_mode, x, old_mode, 0);
3616 return ret;
3619 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3620 an integer mode of the same size as MODE. Returns the instruction
3621 emitted, or NULL if such a move could not be generated. */
3623 static rtx_insn *
3624 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3626 scalar_int_mode imode;
3627 enum insn_code code;
3629 /* There must exist a mode of the exact size we require. */
3630 if (!int_mode_for_mode (mode).exists (&imode))
3631 return NULL;
3633 /* The target must support moves in this mode. */
3634 code = optab_handler (mov_optab, imode);
3635 if (code == CODE_FOR_nothing)
3636 return NULL;
3638 x = emit_move_change_mode (imode, mode, x, force);
3639 if (x == NULL_RTX)
3640 return NULL;
3641 y = emit_move_change_mode (imode, mode, y, force);
3642 if (y == NULL_RTX)
3643 return NULL;
3644 return emit_insn (GEN_FCN (code) (x, y));
3647 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3648 Return an equivalent MEM that does not use an auto-increment. */
3651 emit_move_resolve_push (machine_mode mode, rtx x)
3653 enum rtx_code code = GET_CODE (XEXP (x, 0));
3654 rtx temp;
3656 poly_int64 adjust = GET_MODE_SIZE (mode);
3657 #ifdef PUSH_ROUNDING
3658 adjust = PUSH_ROUNDING (adjust);
3659 #endif
3660 if (code == PRE_DEC || code == POST_DEC)
3661 adjust = -adjust;
3662 else if (code == PRE_MODIFY || code == POST_MODIFY)
3664 rtx expr = XEXP (XEXP (x, 0), 1);
3666 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3667 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3668 if (GET_CODE (expr) == MINUS)
3669 val = -val;
3670 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3671 adjust = val;
3674 /* Do not use anti_adjust_stack, since we don't want to update
3675 stack_pointer_delta. */
3676 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3677 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3678 0, OPTAB_LIB_WIDEN);
3679 if (temp != stack_pointer_rtx)
3680 emit_move_insn (stack_pointer_rtx, temp);
3682 switch (code)
3684 case PRE_INC:
3685 case PRE_DEC:
3686 case PRE_MODIFY:
3687 temp = stack_pointer_rtx;
3688 break;
3689 case POST_INC:
3690 case POST_DEC:
3691 case POST_MODIFY:
3692 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3693 break;
3694 default:
3695 gcc_unreachable ();
3698 return replace_equiv_address (x, temp);
3701 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3702 X is known to satisfy push_operand, and MODE is known to be complex.
3703 Returns the last instruction emitted. */
3705 rtx_insn *
3706 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3708 scalar_mode submode = GET_MODE_INNER (mode);
3709 bool imag_first;
3711 #ifdef PUSH_ROUNDING
3712 poly_int64 submodesize = GET_MODE_SIZE (submode);
3714 /* In case we output to the stack, but the size is smaller than the
3715 machine can push exactly, we need to use move instructions. */
3716 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3718 x = emit_move_resolve_push (mode, x);
3719 return emit_move_insn (x, y);
3721 #endif
3723 /* Note that the real part always precedes the imag part in memory
3724 regardless of machine's endianness. */
3725 switch (GET_CODE (XEXP (x, 0)))
3727 case PRE_DEC:
3728 case POST_DEC:
3729 imag_first = true;
3730 break;
3731 case PRE_INC:
3732 case POST_INC:
3733 imag_first = false;
3734 break;
3735 default:
3736 gcc_unreachable ();
3739 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3740 read_complex_part (y, imag_first));
3741 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3742 read_complex_part (y, !imag_first));
3745 /* A subroutine of emit_move_complex. Perform the move from Y to X
3746 via two moves of the parts. Returns the last instruction emitted. */
3748 rtx_insn *
3749 emit_move_complex_parts (rtx x, rtx y)
3751 /* Show the output dies here. This is necessary for SUBREGs
3752 of pseudos since we cannot track their lifetimes correctly;
3753 hard regs shouldn't appear here except as return values. */
3754 if (!reload_completed && !reload_in_progress
3755 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3756 emit_clobber (x);
3758 write_complex_part (x, read_complex_part (y, false), false);
3759 write_complex_part (x, read_complex_part (y, true), true);
3761 return get_last_insn ();
3764 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3765 MODE is known to be complex. Returns the last instruction emitted. */
3767 static rtx_insn *
3768 emit_move_complex (machine_mode mode, rtx x, rtx y)
3770 bool try_int;
3772 /* Need to take special care for pushes, to maintain proper ordering
3773 of the data, and possibly extra padding. */
3774 if (push_operand (x, mode))
3775 return emit_move_complex_push (mode, x, y);
3777 /* See if we can coerce the target into moving both values at once, except
3778 for floating point where we favor moving as parts if this is easy. */
3779 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3780 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3781 && !(REG_P (x)
3782 && HARD_REGISTER_P (x)
3783 && REG_NREGS (x) == 1)
3784 && !(REG_P (y)
3785 && HARD_REGISTER_P (y)
3786 && REG_NREGS (y) == 1))
3787 try_int = false;
3788 /* Not possible if the values are inherently not adjacent. */
3789 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3790 try_int = false;
3791 /* Is possible if both are registers (or subregs of registers). */
3792 else if (register_operand (x, mode) && register_operand (y, mode))
3793 try_int = true;
3794 /* If one of the operands is a memory, and alignment constraints
3795 are friendly enough, we may be able to do combined memory operations.
3796 We do not attempt this if Y is a constant because that combination is
3797 usually better with the by-parts thing below. */
3798 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3799 && (!STRICT_ALIGNMENT
3800 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3801 try_int = true;
3802 else
3803 try_int = false;
3805 if (try_int)
3807 rtx_insn *ret;
3809 /* For memory to memory moves, optimal behavior can be had with the
3810 existing block move logic. But use normal expansion if optimizing
3811 for size. */
3812 if (MEM_P (x) && MEM_P (y))
3814 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3815 (optimize_insn_for_speed_p()
3816 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3817 return get_last_insn ();
3820 ret = emit_move_via_integer (mode, x, y, true);
3821 if (ret)
3822 return ret;
3825 return emit_move_complex_parts (x, y);
3828 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3829 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3831 static rtx_insn *
3832 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3834 rtx_insn *ret;
3836 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3837 if (mode != CCmode)
3839 enum insn_code code = optab_handler (mov_optab, CCmode);
3840 if (code != CODE_FOR_nothing)
3842 x = emit_move_change_mode (CCmode, mode, x, true);
3843 y = emit_move_change_mode (CCmode, mode, y, true);
3844 return emit_insn (GEN_FCN (code) (x, y));
3848 /* Otherwise, find the MODE_INT mode of the same width. */
3849 ret = emit_move_via_integer (mode, x, y, false);
3850 gcc_assert (ret != NULL);
3851 return ret;
3854 /* Return true if word I of OP lies entirely in the
3855 undefined bits of a paradoxical subreg. */
3857 static bool
3858 undefined_operand_subword_p (const_rtx op, int i)
3860 if (GET_CODE (op) != SUBREG)
3861 return false;
3862 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3863 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3864 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3865 || known_le (offset, -UNITS_PER_WORD));
3868 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3869 MODE is any multi-word or full-word mode that lacks a move_insn
3870 pattern. Note that you will get better code if you define such
3871 patterns, even if they must turn into multiple assembler instructions. */
3873 static rtx_insn *
3874 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3876 rtx_insn *last_insn = 0;
3877 rtx_insn *seq;
3878 rtx inner;
3879 bool need_clobber;
3880 int i, mode_size;
3882 /* This function can only handle cases where the number of words is
3883 known at compile time. */
3884 mode_size = GET_MODE_SIZE (mode).to_constant ();
3885 gcc_assert (mode_size >= UNITS_PER_WORD);
3887 /* If X is a push on the stack, do the push now and replace
3888 X with a reference to the stack pointer. */
3889 if (push_operand (x, mode))
3890 x = emit_move_resolve_push (mode, x);
3892 /* If we are in reload, see if either operand is a MEM whose address
3893 is scheduled for replacement. */
3894 if (reload_in_progress && MEM_P (x)
3895 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3896 x = replace_equiv_address_nv (x, inner);
3897 if (reload_in_progress && MEM_P (y)
3898 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3899 y = replace_equiv_address_nv (y, inner);
3901 start_sequence ();
3903 need_clobber = false;
3904 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3906 /* Do not generate code for a move if it would go entirely
3907 to the non-existing bits of a paradoxical subreg. */
3908 if (undefined_operand_subword_p (x, i))
3909 continue;
3911 rtx xpart = operand_subword (x, i, 1, mode);
3912 rtx ypart;
3914 /* Do not generate code for a move if it would come entirely
3915 from the undefined bits of a paradoxical subreg. */
3916 if (undefined_operand_subword_p (y, i))
3917 continue;
3919 ypart = operand_subword (y, i, 1, mode);
3921 /* If we can't get a part of Y, put Y into memory if it is a
3922 constant. Otherwise, force it into a register. Then we must
3923 be able to get a part of Y. */
3924 if (ypart == 0 && CONSTANT_P (y))
3926 y = use_anchored_address (force_const_mem (mode, y));
3927 ypart = operand_subword (y, i, 1, mode);
3929 else if (ypart == 0)
3930 ypart = operand_subword_force (y, i, mode);
3932 gcc_assert (xpart && ypart);
3934 need_clobber |= (GET_CODE (xpart) == SUBREG);
3936 last_insn = emit_move_insn (xpart, ypart);
3939 seq = get_insns ();
3940 end_sequence ();
3942 /* Show the output dies here. This is necessary for SUBREGs
3943 of pseudos since we cannot track their lifetimes correctly;
3944 hard regs shouldn't appear here except as return values.
3945 We never want to emit such a clobber after reload. */
3946 if (x != y
3947 && ! (reload_in_progress || reload_completed)
3948 && need_clobber != 0)
3949 emit_clobber (x);
3951 emit_insn (seq);
3953 return last_insn;
3956 /* Low level part of emit_move_insn.
3957 Called just like emit_move_insn, but assumes X and Y
3958 are basically valid. */
3960 rtx_insn *
3961 emit_move_insn_1 (rtx x, rtx y)
3963 machine_mode mode = GET_MODE (x);
3964 enum insn_code code;
3966 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3968 code = optab_handler (mov_optab, mode);
3969 if (code != CODE_FOR_nothing)
3970 return emit_insn (GEN_FCN (code) (x, y));
3972 /* Expand complex moves by moving real part and imag part. */
3973 if (COMPLEX_MODE_P (mode))
3974 return emit_move_complex (mode, x, y);
3976 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3977 || ALL_FIXED_POINT_MODE_P (mode))
3979 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3981 /* If we can't find an integer mode, use multi words. */
3982 if (result)
3983 return result;
3984 else
3985 return emit_move_multi_word (mode, x, y);
3988 if (GET_MODE_CLASS (mode) == MODE_CC)
3989 return emit_move_ccmode (mode, x, y);
3991 /* Try using a move pattern for the corresponding integer mode. This is
3992 only safe when simplify_subreg can convert MODE constants into integer
3993 constants. At present, it can only do this reliably if the value
3994 fits within a HOST_WIDE_INT. */
3995 if (!CONSTANT_P (y)
3996 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3998 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4000 if (ret)
4002 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4003 return ret;
4007 return emit_move_multi_word (mode, x, y);
4010 /* Generate code to copy Y into X.
4011 Both Y and X must have the same mode, except that
4012 Y can be a constant with VOIDmode.
4013 This mode cannot be BLKmode; use emit_block_move for that.
4015 Return the last instruction emitted. */
4017 rtx_insn *
4018 emit_move_insn (rtx x, rtx y)
4020 machine_mode mode = GET_MODE (x);
4021 rtx y_cst = NULL_RTX;
4022 rtx_insn *last_insn;
4023 rtx set;
4025 gcc_assert (mode != BLKmode
4026 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4028 /* If we have a copy that looks like one of the following patterns:
4029 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4030 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4031 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4032 (set (subreg:M1 (reg:M2 ...)) (constant C))
4033 where mode M1 is equal in size to M2, try to detect whether the
4034 mode change involves an implicit round trip through memory.
4035 If so, see if we can avoid that by removing the subregs and
4036 doing the move in mode M2 instead. */
4038 rtx x_inner = NULL_RTX;
4039 rtx y_inner = NULL_RTX;
4041 auto candidate_subreg_p = [&](rtx subreg) {
4042 return (REG_P (SUBREG_REG (subreg))
4043 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4044 GET_MODE_SIZE (GET_MODE (subreg)))
4045 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4046 != CODE_FOR_nothing);
4049 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4050 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4051 && !push_operand (mem, GET_MODE (mem))
4052 /* Not a candiate if innermode requires too much alignment. */
4053 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4054 || targetm.slow_unaligned_access (GET_MODE (mem),
4055 MEM_ALIGN (mem))
4056 || !targetm.slow_unaligned_access (innermode,
4057 MEM_ALIGN (mem))));
4060 if (SUBREG_P (x) && candidate_subreg_p (x))
4061 x_inner = SUBREG_REG (x);
4063 if (SUBREG_P (y) && candidate_subreg_p (y))
4064 y_inner = SUBREG_REG (y);
4066 if (x_inner != NULL_RTX
4067 && y_inner != NULL_RTX
4068 && GET_MODE (x_inner) == GET_MODE (y_inner)
4069 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4071 x = x_inner;
4072 y = y_inner;
4073 mode = GET_MODE (x_inner);
4075 else if (x_inner != NULL_RTX
4076 && MEM_P (y)
4077 && candidate_mem_p (GET_MODE (x_inner), y))
4079 x = x_inner;
4080 y = adjust_address (y, GET_MODE (x_inner), 0);
4081 mode = GET_MODE (x_inner);
4083 else if (y_inner != NULL_RTX
4084 && MEM_P (x)
4085 && candidate_mem_p (GET_MODE (y_inner), x))
4087 x = adjust_address (x, GET_MODE (y_inner), 0);
4088 y = y_inner;
4089 mode = GET_MODE (y_inner);
4091 else if (x_inner != NULL_RTX
4092 && CONSTANT_P (y)
4093 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4094 mode, ALL_REGS)
4095 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4097 x = x_inner;
4098 y = y_inner;
4099 mode = GET_MODE (x_inner);
4102 if (CONSTANT_P (y))
4104 if (optimize
4105 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4106 && (last_insn = compress_float_constant (x, y)))
4107 return last_insn;
4109 y_cst = y;
4111 if (!targetm.legitimate_constant_p (mode, y))
4113 y = force_const_mem (mode, y);
4115 /* If the target's cannot_force_const_mem prevented the spill,
4116 assume that the target's move expanders will also take care
4117 of the non-legitimate constant. */
4118 if (!y)
4119 y = y_cst;
4120 else
4121 y = use_anchored_address (y);
4125 /* If X or Y are memory references, verify that their addresses are valid
4126 for the machine. */
4127 if (MEM_P (x)
4128 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4129 MEM_ADDR_SPACE (x))
4130 && ! push_operand (x, GET_MODE (x))))
4131 x = validize_mem (x);
4133 if (MEM_P (y)
4134 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4135 MEM_ADDR_SPACE (y)))
4136 y = validize_mem (y);
4138 gcc_assert (mode != BLKmode);
4140 last_insn = emit_move_insn_1 (x, y);
4142 if (y_cst && REG_P (x)
4143 && (set = single_set (last_insn)) != NULL_RTX
4144 && SET_DEST (set) == x
4145 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4146 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4148 return last_insn;
4151 /* Generate the body of an instruction to copy Y into X.
4152 It may be a list of insns, if one insn isn't enough. */
4154 rtx_insn *
4155 gen_move_insn (rtx x, rtx y)
4157 rtx_insn *seq;
4159 start_sequence ();
4160 emit_move_insn_1 (x, y);
4161 seq = get_insns ();
4162 end_sequence ();
4163 return seq;
4166 /* If Y is representable exactly in a narrower mode, and the target can
4167 perform the extension directly from constant or memory, then emit the
4168 move as an extension. */
4170 static rtx_insn *
4171 compress_float_constant (rtx x, rtx y)
4173 machine_mode dstmode = GET_MODE (x);
4174 machine_mode orig_srcmode = GET_MODE (y);
4175 machine_mode srcmode;
4176 const REAL_VALUE_TYPE *r;
4177 int oldcost, newcost;
4178 bool speed = optimize_insn_for_speed_p ();
4180 r = CONST_DOUBLE_REAL_VALUE (y);
4182 if (targetm.legitimate_constant_p (dstmode, y))
4183 oldcost = set_src_cost (y, orig_srcmode, speed);
4184 else
4185 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4187 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4189 enum insn_code ic;
4190 rtx trunc_y;
4191 rtx_insn *last_insn;
4193 /* Skip if the target can't extend this way. */
4194 ic = can_extend_p (dstmode, srcmode, 0);
4195 if (ic == CODE_FOR_nothing)
4196 continue;
4198 /* Skip if the narrowed value isn't exact. */
4199 if (! exact_real_truncate (srcmode, r))
4200 continue;
4202 trunc_y = const_double_from_real_value (*r, srcmode);
4204 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4206 /* Skip if the target needs extra instructions to perform
4207 the extension. */
4208 if (!insn_operand_matches (ic, 1, trunc_y))
4209 continue;
4210 /* This is valid, but may not be cheaper than the original. */
4211 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4212 dstmode, speed);
4213 if (oldcost < newcost)
4214 continue;
4216 else if (float_extend_from_mem[dstmode][srcmode])
4218 trunc_y = force_const_mem (srcmode, trunc_y);
4219 /* This is valid, but may not be cheaper than the original. */
4220 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4221 dstmode, speed);
4222 if (oldcost < newcost)
4223 continue;
4224 trunc_y = validize_mem (trunc_y);
4226 else
4227 continue;
4229 /* For CSE's benefit, force the compressed constant pool entry
4230 into a new pseudo. This constant may be used in different modes,
4231 and if not, combine will put things back together for us. */
4232 trunc_y = force_reg (srcmode, trunc_y);
4234 /* If x is a hard register, perform the extension into a pseudo,
4235 so that e.g. stack realignment code is aware of it. */
4236 rtx target = x;
4237 if (REG_P (x) && HARD_REGISTER_P (x))
4238 target = gen_reg_rtx (dstmode);
4240 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4241 last_insn = get_last_insn ();
4243 if (REG_P (target))
4244 set_unique_reg_note (last_insn, REG_EQUAL, y);
4246 if (target != x)
4247 return emit_move_insn (x, target);
4248 return last_insn;
4251 return NULL;
4254 /* Pushing data onto the stack. */
4256 /* Push a block of length SIZE (perhaps variable)
4257 and return an rtx to address the beginning of the block.
4258 The value may be virtual_outgoing_args_rtx.
4260 EXTRA is the number of bytes of padding to push in addition to SIZE.
4261 BELOW nonzero means this padding comes at low addresses;
4262 otherwise, the padding comes at high addresses. */
4265 push_block (rtx size, poly_int64 extra, int below)
4267 rtx temp;
4269 size = convert_modes (Pmode, ptr_mode, size, 1);
4270 if (CONSTANT_P (size))
4271 anti_adjust_stack (plus_constant (Pmode, size, extra));
4272 else if (REG_P (size) && known_eq (extra, 0))
4273 anti_adjust_stack (size);
4274 else
4276 temp = copy_to_mode_reg (Pmode, size);
4277 if (maybe_ne (extra, 0))
4278 temp = expand_binop (Pmode, add_optab, temp,
4279 gen_int_mode (extra, Pmode),
4280 temp, 0, OPTAB_LIB_WIDEN);
4281 anti_adjust_stack (temp);
4284 if (STACK_GROWS_DOWNWARD)
4286 temp = virtual_outgoing_args_rtx;
4287 if (maybe_ne (extra, 0) && below)
4288 temp = plus_constant (Pmode, temp, extra);
4290 else
4292 poly_int64 csize;
4293 if (poly_int_rtx_p (size, &csize))
4294 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4295 -csize - (below ? 0 : extra));
4296 else if (maybe_ne (extra, 0) && !below)
4297 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4298 negate_rtx (Pmode, plus_constant (Pmode, size,
4299 extra)));
4300 else
4301 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4302 negate_rtx (Pmode, size));
4305 return memory_address (NARROWEST_INT_MODE, temp);
4308 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4310 static rtx
4311 mem_autoinc_base (rtx mem)
4313 if (MEM_P (mem))
4315 rtx addr = XEXP (mem, 0);
4316 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4317 return XEXP (addr, 0);
4319 return NULL;
4322 /* A utility routine used here, in reload, and in try_split. The insns
4323 after PREV up to and including LAST are known to adjust the stack,
4324 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4325 placing notes as appropriate. PREV may be NULL, indicating the
4326 entire insn sequence prior to LAST should be scanned.
4328 The set of allowed stack pointer modifications is small:
4329 (1) One or more auto-inc style memory references (aka pushes),
4330 (2) One or more addition/subtraction with the SP as destination,
4331 (3) A single move insn with the SP as destination,
4332 (4) A call_pop insn,
4333 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4335 Insns in the sequence that do not modify the SP are ignored,
4336 except for noreturn calls.
4338 The return value is the amount of adjustment that can be trivially
4339 verified, via immediate operand or auto-inc. If the adjustment
4340 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4342 poly_int64
4343 find_args_size_adjust (rtx_insn *insn)
4345 rtx dest, set, pat;
4346 int i;
4348 pat = PATTERN (insn);
4349 set = NULL;
4351 /* Look for a call_pop pattern. */
4352 if (CALL_P (insn))
4354 /* We have to allow non-call_pop patterns for the case
4355 of emit_single_push_insn of a TLS address. */
4356 if (GET_CODE (pat) != PARALLEL)
4357 return 0;
4359 /* All call_pop have a stack pointer adjust in the parallel.
4360 The call itself is always first, and the stack adjust is
4361 usually last, so search from the end. */
4362 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4364 set = XVECEXP (pat, 0, i);
4365 if (GET_CODE (set) != SET)
4366 continue;
4367 dest = SET_DEST (set);
4368 if (dest == stack_pointer_rtx)
4369 break;
4371 /* We'd better have found the stack pointer adjust. */
4372 if (i == 0)
4373 return 0;
4374 /* Fall through to process the extracted SET and DEST
4375 as if it was a standalone insn. */
4377 else if (GET_CODE (pat) == SET)
4378 set = pat;
4379 else if ((set = single_set (insn)) != NULL)
4381 else if (GET_CODE (pat) == PARALLEL)
4383 /* ??? Some older ports use a parallel with a stack adjust
4384 and a store for a PUSH_ROUNDING pattern, rather than a
4385 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4386 /* ??? See h8300 and m68k, pushqi1. */
4387 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4389 set = XVECEXP (pat, 0, i);
4390 if (GET_CODE (set) != SET)
4391 continue;
4392 dest = SET_DEST (set);
4393 if (dest == stack_pointer_rtx)
4394 break;
4396 /* We do not expect an auto-inc of the sp in the parallel. */
4397 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4398 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4399 != stack_pointer_rtx);
4401 if (i < 0)
4402 return 0;
4404 else
4405 return 0;
4407 dest = SET_DEST (set);
4409 /* Look for direct modifications of the stack pointer. */
4410 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4412 /* Look for a trivial adjustment, otherwise assume nothing. */
4413 /* Note that the SPU restore_stack_block pattern refers to
4414 the stack pointer in V4SImode. Consider that non-trivial. */
4415 poly_int64 offset;
4416 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4417 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4418 return offset;
4419 /* ??? Reload can generate no-op moves, which will be cleaned
4420 up later. Recognize it and continue searching. */
4421 else if (rtx_equal_p (dest, SET_SRC (set)))
4422 return 0;
4423 else
4424 return HOST_WIDE_INT_MIN;
4426 else
4428 rtx mem, addr;
4430 /* Otherwise only think about autoinc patterns. */
4431 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4433 mem = dest;
4434 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4435 != stack_pointer_rtx);
4437 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4438 mem = SET_SRC (set);
4439 else
4440 return 0;
4442 addr = XEXP (mem, 0);
4443 switch (GET_CODE (addr))
4445 case PRE_INC:
4446 case POST_INC:
4447 return GET_MODE_SIZE (GET_MODE (mem));
4448 case PRE_DEC:
4449 case POST_DEC:
4450 return -GET_MODE_SIZE (GET_MODE (mem));
4451 case PRE_MODIFY:
4452 case POST_MODIFY:
4453 addr = XEXP (addr, 1);
4454 gcc_assert (GET_CODE (addr) == PLUS);
4455 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4456 return rtx_to_poly_int64 (XEXP (addr, 1));
4457 default:
4458 gcc_unreachable ();
4463 poly_int64
4464 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4465 poly_int64 end_args_size)
4467 poly_int64 args_size = end_args_size;
4468 bool saw_unknown = false;
4469 rtx_insn *insn;
4471 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4473 if (!NONDEBUG_INSN_P (insn))
4474 continue;
4476 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4477 a call argument containing a TLS address that itself requires
4478 a call to __tls_get_addr. The handling of stack_pointer_delta
4479 in emit_single_push_insn is supposed to ensure that any such
4480 notes are already correct. */
4481 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4482 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4484 poly_int64 this_delta = find_args_size_adjust (insn);
4485 if (known_eq (this_delta, 0))
4487 if (!CALL_P (insn)
4488 || ACCUMULATE_OUTGOING_ARGS
4489 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4490 continue;
4493 gcc_assert (!saw_unknown);
4494 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4495 saw_unknown = true;
4497 if (!note)
4498 add_args_size_note (insn, args_size);
4499 if (STACK_GROWS_DOWNWARD)
4500 this_delta = -poly_uint64 (this_delta);
4502 if (saw_unknown)
4503 args_size = HOST_WIDE_INT_MIN;
4504 else
4505 args_size -= this_delta;
4508 return args_size;
4511 #ifdef PUSH_ROUNDING
4512 /* Emit single push insn. */
4514 static void
4515 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4517 rtx dest_addr;
4518 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4519 rtx dest;
4520 enum insn_code icode;
4522 /* If there is push pattern, use it. Otherwise try old way of throwing
4523 MEM representing push operation to move expander. */
4524 icode = optab_handler (push_optab, mode);
4525 if (icode != CODE_FOR_nothing)
4527 class expand_operand ops[1];
4529 create_input_operand (&ops[0], x, mode);
4530 if (maybe_expand_insn (icode, 1, ops))
4531 return;
4533 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4534 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4535 /* If we are to pad downward, adjust the stack pointer first and
4536 then store X into the stack location using an offset. This is
4537 because emit_move_insn does not know how to pad; it does not have
4538 access to type. */
4539 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4541 emit_move_insn (stack_pointer_rtx,
4542 expand_binop (Pmode,
4543 STACK_GROWS_DOWNWARD ? sub_optab
4544 : add_optab,
4545 stack_pointer_rtx,
4546 gen_int_mode (rounded_size, Pmode),
4547 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4549 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4550 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4551 /* We have already decremented the stack pointer, so get the
4552 previous value. */
4553 offset += rounded_size;
4555 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4556 /* We have already incremented the stack pointer, so get the
4557 previous value. */
4558 offset -= rounded_size;
4560 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4562 else
4564 if (STACK_GROWS_DOWNWARD)
4565 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4566 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4567 else
4568 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4569 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4571 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4574 dest = gen_rtx_MEM (mode, dest_addr);
4576 if (type != 0)
4578 set_mem_attributes (dest, type, 1);
4580 if (cfun->tail_call_marked)
4581 /* Function incoming arguments may overlap with sibling call
4582 outgoing arguments and we cannot allow reordering of reads
4583 from function arguments with stores to outgoing arguments
4584 of sibling calls. */
4585 set_mem_alias_set (dest, 0);
4587 emit_move_insn (dest, x);
4590 /* Emit and annotate a single push insn. */
4592 static void
4593 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4595 poly_int64 delta, old_delta = stack_pointer_delta;
4596 rtx_insn *prev = get_last_insn ();
4597 rtx_insn *last;
4599 emit_single_push_insn_1 (mode, x, type);
4601 /* Adjust stack_pointer_delta to describe the situation after the push
4602 we just performed. Note that we must do this after the push rather
4603 than before the push in case calculating X needs pushes and pops of
4604 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4605 for such pushes and pops must not include the effect of the future
4606 push of X. */
4607 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4609 last = get_last_insn ();
4611 /* Notice the common case where we emitted exactly one insn. */
4612 if (PREV_INSN (last) == prev)
4614 add_args_size_note (last, stack_pointer_delta);
4615 return;
4618 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4619 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4620 || known_eq (delta, old_delta));
4622 #endif
4624 /* If reading SIZE bytes from X will end up reading from
4625 Y return the number of bytes that overlap. Return -1
4626 if there is no overlap or -2 if we can't determine
4627 (for example when X and Y have different base registers). */
4629 static int
4630 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4632 rtx tmp = plus_constant (Pmode, x, size);
4633 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4635 if (!CONST_INT_P (sub))
4636 return -2;
4638 HOST_WIDE_INT val = INTVAL (sub);
4640 return IN_RANGE (val, 1, size) ? val : -1;
4643 /* Generate code to push X onto the stack, assuming it has mode MODE and
4644 type TYPE.
4645 MODE is redundant except when X is a CONST_INT (since they don't
4646 carry mode info).
4647 SIZE is an rtx for the size of data to be copied (in bytes),
4648 needed only if X is BLKmode.
4649 Return true if successful. May return false if asked to push a
4650 partial argument during a sibcall optimization (as specified by
4651 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4652 to not overlap.
4654 ALIGN (in bits) is maximum alignment we can assume.
4656 If PARTIAL and REG are both nonzero, then copy that many of the first
4657 bytes of X into registers starting with REG, and push the rest of X.
4658 The amount of space pushed is decreased by PARTIAL bytes.
4659 REG must be a hard register in this case.
4660 If REG is zero but PARTIAL is not, take any all others actions for an
4661 argument partially in registers, but do not actually load any
4662 registers.
4664 EXTRA is the amount in bytes of extra space to leave next to this arg.
4665 This is ignored if an argument block has already been allocated.
4667 On a machine that lacks real push insns, ARGS_ADDR is the address of
4668 the bottom of the argument block for this call. We use indexing off there
4669 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4670 argument block has not been preallocated.
4672 ARGS_SO_FAR is the size of args previously pushed for this call.
4674 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4675 for arguments passed in registers. If nonzero, it will be the number
4676 of bytes required. */
4678 bool
4679 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4680 unsigned int align, int partial, rtx reg, poly_int64 extra,
4681 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4682 rtx alignment_pad, bool sibcall_p)
4684 rtx xinner;
4685 pad_direction stack_direction
4686 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4688 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4689 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4690 Default is below for small data on big-endian machines; else above. */
4691 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4693 /* Invert direction if stack is post-decrement.
4694 FIXME: why? */
4695 if (STACK_PUSH_CODE == POST_DEC)
4696 if (where_pad != PAD_NONE)
4697 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4699 xinner = x;
4701 int nregs = partial / UNITS_PER_WORD;
4702 rtx *tmp_regs = NULL;
4703 int overlapping = 0;
4705 if (mode == BLKmode
4706 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4708 /* Copy a block into the stack, entirely or partially. */
4710 rtx temp;
4711 int used;
4712 int offset;
4713 int skip;
4715 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4716 used = partial - offset;
4718 if (mode != BLKmode)
4720 /* A value is to be stored in an insufficiently aligned
4721 stack slot; copy via a suitably aligned slot if
4722 necessary. */
4723 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4724 if (!MEM_P (xinner))
4726 temp = assign_temp (type, 1, 1);
4727 emit_move_insn (temp, xinner);
4728 xinner = temp;
4732 gcc_assert (size);
4734 /* USED is now the # of bytes we need not copy to the stack
4735 because registers will take care of them. */
4737 if (partial != 0)
4738 xinner = adjust_address (xinner, BLKmode, used);
4740 /* If the partial register-part of the arg counts in its stack size,
4741 skip the part of stack space corresponding to the registers.
4742 Otherwise, start copying to the beginning of the stack space,
4743 by setting SKIP to 0. */
4744 skip = (reg_parm_stack_space == 0) ? 0 : used;
4746 #ifdef PUSH_ROUNDING
4747 /* NB: Let the backend known the number of bytes to push and
4748 decide if push insns should be generated. */
4749 unsigned int push_size;
4750 if (CONST_INT_P (size))
4751 push_size = INTVAL (size);
4752 else
4753 push_size = 0;
4755 /* Do it with several push insns if that doesn't take lots of insns
4756 and if there is no difficulty with push insns that skip bytes
4757 on the stack for alignment purposes. */
4758 if (args_addr == 0
4759 && targetm.calls.push_argument (push_size)
4760 && CONST_INT_P (size)
4761 && skip == 0
4762 && MEM_ALIGN (xinner) >= align
4763 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4764 /* Here we avoid the case of a structure whose weak alignment
4765 forces many pushes of a small amount of data,
4766 and such small pushes do rounding that causes trouble. */
4767 && ((!targetm.slow_unaligned_access (word_mode, align))
4768 || align >= BIGGEST_ALIGNMENT
4769 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4770 align / BITS_PER_UNIT))
4771 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4773 /* Push padding now if padding above and stack grows down,
4774 or if padding below and stack grows up.
4775 But if space already allocated, this has already been done. */
4776 if (maybe_ne (extra, 0)
4777 && args_addr == 0
4778 && where_pad != PAD_NONE
4779 && where_pad != stack_direction)
4780 anti_adjust_stack (gen_int_mode (extra, Pmode));
4782 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4783 RETURN_BEGIN);
4785 else
4786 #endif /* PUSH_ROUNDING */
4788 rtx target;
4790 /* Otherwise make space on the stack and copy the data
4791 to the address of that space. */
4793 /* Deduct words put into registers from the size we must copy. */
4794 if (partial != 0)
4796 if (CONST_INT_P (size))
4797 size = GEN_INT (INTVAL (size) - used);
4798 else
4799 size = expand_binop (GET_MODE (size), sub_optab, size,
4800 gen_int_mode (used, GET_MODE (size)),
4801 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4804 /* Get the address of the stack space.
4805 In this case, we do not deal with EXTRA separately.
4806 A single stack adjust will do. */
4807 poly_int64 const_args_so_far;
4808 if (! args_addr)
4810 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4811 extra = 0;
4813 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4814 temp = memory_address (BLKmode,
4815 plus_constant (Pmode, args_addr,
4816 skip + const_args_so_far));
4817 else
4818 temp = memory_address (BLKmode,
4819 plus_constant (Pmode,
4820 gen_rtx_PLUS (Pmode,
4821 args_addr,
4822 args_so_far),
4823 skip));
4825 if (!ACCUMULATE_OUTGOING_ARGS)
4827 /* If the source is referenced relative to the stack pointer,
4828 copy it to another register to stabilize it. We do not need
4829 to do this if we know that we won't be changing sp. */
4831 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4832 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4833 temp = copy_to_reg (temp);
4836 target = gen_rtx_MEM (BLKmode, temp);
4838 /* We do *not* set_mem_attributes here, because incoming arguments
4839 may overlap with sibling call outgoing arguments and we cannot
4840 allow reordering of reads from function arguments with stores
4841 to outgoing arguments of sibling calls. We do, however, want
4842 to record the alignment of the stack slot. */
4843 /* ALIGN may well be better aligned than TYPE, e.g. due to
4844 PARM_BOUNDARY. Assume the caller isn't lying. */
4845 set_mem_align (target, align);
4847 /* If part should go in registers and pushing to that part would
4848 overwrite some of the values that need to go into regs, load the
4849 overlapping values into temporary pseudos to be moved into the hard
4850 regs at the end after the stack pushing has completed.
4851 We cannot load them directly into the hard regs here because
4852 they can be clobbered by the block move expansions.
4853 See PR 65358. */
4855 if (partial > 0 && reg != 0 && mode == BLKmode
4856 && GET_CODE (reg) != PARALLEL)
4858 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4859 if (overlapping > 0)
4861 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4862 overlapping /= UNITS_PER_WORD;
4864 tmp_regs = XALLOCAVEC (rtx, overlapping);
4866 for (int i = 0; i < overlapping; i++)
4867 tmp_regs[i] = gen_reg_rtx (word_mode);
4869 for (int i = 0; i < overlapping; i++)
4870 emit_move_insn (tmp_regs[i],
4871 operand_subword_force (target, i, mode));
4873 else if (overlapping == -1)
4874 overlapping = 0;
4875 /* Could not determine whether there is overlap.
4876 Fail the sibcall. */
4877 else
4879 overlapping = 0;
4880 if (sibcall_p)
4881 return false;
4885 /* If source is a constant VAR_DECL with a simple constructor,
4886 store the constructor to the stack instead of moving it. */
4887 const_tree decl;
4888 if (partial == 0
4889 && MEM_P (xinner)
4890 && SYMBOL_REF_P (XEXP (xinner, 0))
4891 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
4892 && VAR_P (decl)
4893 && TREE_READONLY (decl)
4894 && !TREE_SIDE_EFFECTS (decl)
4895 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
4896 store_constructor (DECL_INITIAL (decl), target, 0,
4897 int_expr_size (DECL_INITIAL (decl)), false);
4898 else
4899 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4902 else if (partial > 0)
4904 /* Scalar partly in registers. This case is only supported
4905 for fixed-wdth modes. */
4906 int num_words = GET_MODE_SIZE (mode).to_constant ();
4907 num_words /= UNITS_PER_WORD;
4908 int i;
4909 int not_stack;
4910 /* # bytes of start of argument
4911 that we must make space for but need not store. */
4912 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4913 int args_offset = INTVAL (args_so_far);
4914 int skip;
4916 /* Push padding now if padding above and stack grows down,
4917 or if padding below and stack grows up.
4918 But if space already allocated, this has already been done. */
4919 if (maybe_ne (extra, 0)
4920 && args_addr == 0
4921 && where_pad != PAD_NONE
4922 && where_pad != stack_direction)
4923 anti_adjust_stack (gen_int_mode (extra, Pmode));
4925 /* If we make space by pushing it, we might as well push
4926 the real data. Otherwise, we can leave OFFSET nonzero
4927 and leave the space uninitialized. */
4928 if (args_addr == 0)
4929 offset = 0;
4931 /* Now NOT_STACK gets the number of words that we don't need to
4932 allocate on the stack. Convert OFFSET to words too. */
4933 not_stack = (partial - offset) / UNITS_PER_WORD;
4934 offset /= UNITS_PER_WORD;
4936 /* If the partial register-part of the arg counts in its stack size,
4937 skip the part of stack space corresponding to the registers.
4938 Otherwise, start copying to the beginning of the stack space,
4939 by setting SKIP to 0. */
4940 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4942 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4943 x = validize_mem (force_const_mem (mode, x));
4945 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4946 SUBREGs of such registers are not allowed. */
4947 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4948 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4949 x = copy_to_reg (x);
4951 /* Loop over all the words allocated on the stack for this arg. */
4952 /* We can do it by words, because any scalar bigger than a word
4953 has a size a multiple of a word. */
4954 for (i = num_words - 1; i >= not_stack; i--)
4955 if (i >= not_stack + offset)
4956 if (!emit_push_insn (operand_subword_force (x, i, mode),
4957 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4958 0, args_addr,
4959 GEN_INT (args_offset + ((i - not_stack + skip)
4960 * UNITS_PER_WORD)),
4961 reg_parm_stack_space, alignment_pad, sibcall_p))
4962 return false;
4964 else
4966 rtx addr;
4967 rtx dest;
4969 /* Push padding now if padding above and stack grows down,
4970 or if padding below and stack grows up.
4971 But if space already allocated, this has already been done. */
4972 if (maybe_ne (extra, 0)
4973 && args_addr == 0
4974 && where_pad != PAD_NONE
4975 && where_pad != stack_direction)
4976 anti_adjust_stack (gen_int_mode (extra, Pmode));
4978 #ifdef PUSH_ROUNDING
4979 if (args_addr == 0 && targetm.calls.push_argument (0))
4980 emit_single_push_insn (mode, x, type);
4981 else
4982 #endif
4984 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4985 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4987 /* We do *not* set_mem_attributes here, because incoming arguments
4988 may overlap with sibling call outgoing arguments and we cannot
4989 allow reordering of reads from function arguments with stores
4990 to outgoing arguments of sibling calls. We do, however, want
4991 to record the alignment of the stack slot. */
4992 /* ALIGN may well be better aligned than TYPE, e.g. due to
4993 PARM_BOUNDARY. Assume the caller isn't lying. */
4994 set_mem_align (dest, align);
4996 emit_move_insn (dest, x);
5000 /* Move the partial arguments into the registers and any overlapping
5001 values that we moved into the pseudos in tmp_regs. */
5002 if (partial > 0 && reg != 0)
5004 /* Handle calls that pass values in multiple non-contiguous locations.
5005 The Irix 6 ABI has examples of this. */
5006 if (GET_CODE (reg) == PARALLEL)
5007 emit_group_load (reg, x, type, -1);
5008 else
5010 gcc_assert (partial % UNITS_PER_WORD == 0);
5011 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5013 for (int i = 0; i < overlapping; i++)
5014 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5015 + nregs - overlapping + i),
5016 tmp_regs[i]);
5021 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5022 anti_adjust_stack (gen_int_mode (extra, Pmode));
5024 if (alignment_pad && args_addr == 0)
5025 anti_adjust_stack (alignment_pad);
5027 return true;
5030 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5031 operations. */
5033 static rtx
5034 get_subtarget (rtx x)
5036 return (optimize
5037 || x == 0
5038 /* Only registers can be subtargets. */
5039 || !REG_P (x)
5040 /* Don't use hard regs to avoid extending their life. */
5041 || REGNO (x) < FIRST_PSEUDO_REGISTER
5042 ? 0 : x);
5045 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5046 FIELD is a bitfield. Returns true if the optimization was successful,
5047 and there's nothing else to do. */
5049 static bool
5050 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5051 poly_uint64 pbitpos,
5052 poly_uint64 pbitregion_start,
5053 poly_uint64 pbitregion_end,
5054 machine_mode mode1, rtx str_rtx,
5055 tree to, tree src, bool reverse)
5057 /* str_mode is not guaranteed to be a scalar type. */
5058 machine_mode str_mode = GET_MODE (str_rtx);
5059 unsigned int str_bitsize;
5060 tree op0, op1;
5061 rtx value, result;
5062 optab binop;
5063 gimple *srcstmt;
5064 enum tree_code code;
5066 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5067 if (mode1 != VOIDmode
5068 || !pbitsize.is_constant (&bitsize)
5069 || !pbitpos.is_constant (&bitpos)
5070 || !pbitregion_start.is_constant (&bitregion_start)
5071 || !pbitregion_end.is_constant (&bitregion_end)
5072 || bitsize >= BITS_PER_WORD
5073 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5074 || str_bitsize > BITS_PER_WORD
5075 || TREE_SIDE_EFFECTS (to)
5076 || TREE_THIS_VOLATILE (to))
5077 return false;
5079 STRIP_NOPS (src);
5080 if (TREE_CODE (src) != SSA_NAME)
5081 return false;
5082 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5083 return false;
5085 srcstmt = get_gimple_for_ssa_name (src);
5086 if (!srcstmt
5087 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5088 return false;
5090 code = gimple_assign_rhs_code (srcstmt);
5092 op0 = gimple_assign_rhs1 (srcstmt);
5094 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5095 to find its initialization. Hopefully the initialization will
5096 be from a bitfield load. */
5097 if (TREE_CODE (op0) == SSA_NAME)
5099 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5101 /* We want to eventually have OP0 be the same as TO, which
5102 should be a bitfield. */
5103 if (!op0stmt
5104 || !is_gimple_assign (op0stmt)
5105 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5106 return false;
5107 op0 = gimple_assign_rhs1 (op0stmt);
5110 op1 = gimple_assign_rhs2 (srcstmt);
5112 if (!operand_equal_p (to, op0, 0))
5113 return false;
5115 if (MEM_P (str_rtx))
5117 unsigned HOST_WIDE_INT offset1;
5119 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5120 str_bitsize = BITS_PER_WORD;
5122 scalar_int_mode best_mode;
5123 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5124 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5125 return false;
5126 str_mode = best_mode;
5127 str_bitsize = GET_MODE_BITSIZE (best_mode);
5129 offset1 = bitpos;
5130 bitpos %= str_bitsize;
5131 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5132 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5134 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5135 return false;
5137 /* If the bit field covers the whole REG/MEM, store_field
5138 will likely generate better code. */
5139 if (bitsize >= str_bitsize)
5140 return false;
5142 /* We can't handle fields split across multiple entities. */
5143 if (bitpos + bitsize > str_bitsize)
5144 return false;
5146 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5147 bitpos = str_bitsize - bitpos - bitsize;
5149 switch (code)
5151 case PLUS_EXPR:
5152 case MINUS_EXPR:
5153 /* For now, just optimize the case of the topmost bitfield
5154 where we don't need to do any masking and also
5155 1 bit bitfields where xor can be used.
5156 We might win by one instruction for the other bitfields
5157 too if insv/extv instructions aren't used, so that
5158 can be added later. */
5159 if ((reverse || bitpos + bitsize != str_bitsize)
5160 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5161 break;
5163 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5164 value = convert_modes (str_mode,
5165 TYPE_MODE (TREE_TYPE (op1)), value,
5166 TYPE_UNSIGNED (TREE_TYPE (op1)));
5168 /* We may be accessing data outside the field, which means
5169 we can alias adjacent data. */
5170 if (MEM_P (str_rtx))
5172 str_rtx = shallow_copy_rtx (str_rtx);
5173 set_mem_alias_set (str_rtx, 0);
5174 set_mem_expr (str_rtx, 0);
5177 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5179 value = expand_and (str_mode, value, const1_rtx, NULL);
5180 binop = xor_optab;
5182 else
5183 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5185 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5186 if (reverse)
5187 value = flip_storage_order (str_mode, value);
5188 result = expand_binop (str_mode, binop, str_rtx,
5189 value, str_rtx, 1, OPTAB_WIDEN);
5190 if (result != str_rtx)
5191 emit_move_insn (str_rtx, result);
5192 return true;
5194 case BIT_IOR_EXPR:
5195 case BIT_XOR_EXPR:
5196 if (TREE_CODE (op1) != INTEGER_CST)
5197 break;
5198 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5199 value = convert_modes (str_mode,
5200 TYPE_MODE (TREE_TYPE (op1)), value,
5201 TYPE_UNSIGNED (TREE_TYPE (op1)));
5203 /* We may be accessing data outside the field, which means
5204 we can alias adjacent data. */
5205 if (MEM_P (str_rtx))
5207 str_rtx = shallow_copy_rtx (str_rtx);
5208 set_mem_alias_set (str_rtx, 0);
5209 set_mem_expr (str_rtx, 0);
5212 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5213 if (bitpos + bitsize != str_bitsize)
5215 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5216 str_mode);
5217 value = expand_and (str_mode, value, mask, NULL_RTX);
5219 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5220 if (reverse)
5221 value = flip_storage_order (str_mode, value);
5222 result = expand_binop (str_mode, binop, str_rtx,
5223 value, str_rtx, 1, OPTAB_WIDEN);
5224 if (result != str_rtx)
5225 emit_move_insn (str_rtx, result);
5226 return true;
5228 default:
5229 break;
5232 return false;
5235 /* In the C++ memory model, consecutive bit fields in a structure are
5236 considered one memory location.
5238 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5239 returns the bit range of consecutive bits in which this COMPONENT_REF
5240 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5241 and *OFFSET may be adjusted in the process.
5243 If the access does not need to be restricted, 0 is returned in both
5244 *BITSTART and *BITEND. */
5246 void
5247 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5248 poly_int64_pod *bitpos, tree *offset)
5250 poly_int64 bitoffset;
5251 tree field, repr;
5253 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5255 field = TREE_OPERAND (exp, 1);
5256 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5257 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5258 need to limit the range we can access. */
5259 if (!repr)
5261 *bitstart = *bitend = 0;
5262 return;
5265 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5266 part of a larger bit field, then the representative does not serve any
5267 useful purpose. This can occur in Ada. */
5268 if (handled_component_p (TREE_OPERAND (exp, 0)))
5270 machine_mode rmode;
5271 poly_int64 rbitsize, rbitpos;
5272 tree roffset;
5273 int unsignedp, reversep, volatilep = 0;
5274 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5275 &roffset, &rmode, &unsignedp, &reversep,
5276 &volatilep);
5277 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5279 *bitstart = *bitend = 0;
5280 return;
5284 /* Compute the adjustment to bitpos from the offset of the field
5285 relative to the representative. DECL_FIELD_OFFSET of field and
5286 repr are the same by construction if they are not constants,
5287 see finish_bitfield_layout. */
5288 poly_uint64 field_offset, repr_offset;
5289 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5290 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5291 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5292 else
5293 bitoffset = 0;
5294 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5295 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5297 /* If the adjustment is larger than bitpos, we would have a negative bit
5298 position for the lower bound and this may wreak havoc later. Adjust
5299 offset and bitpos to make the lower bound non-negative in that case. */
5300 if (maybe_gt (bitoffset, *bitpos))
5302 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5303 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5305 *bitpos += adjust_bits;
5306 if (*offset == NULL_TREE)
5307 *offset = size_int (-adjust_bytes);
5308 else
5309 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5310 *bitstart = 0;
5312 else
5313 *bitstart = *bitpos - bitoffset;
5315 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5318 /* Returns true if BASE is a DECL that does not reside in memory and
5319 has non-BLKmode. DECL_RTL must not be a MEM; if
5320 DECL_RTL was not set yet, return false. */
5322 bool
5323 non_mem_decl_p (tree base)
5325 if (!DECL_P (base)
5326 || TREE_ADDRESSABLE (base)
5327 || DECL_MODE (base) == BLKmode)
5328 return false;
5330 if (!DECL_RTL_SET_P (base))
5331 return false;
5333 return (!MEM_P (DECL_RTL (base)));
5336 /* Returns true if REF refers to an object that does not
5337 reside in memory and has non-BLKmode. */
5339 bool
5340 mem_ref_refers_to_non_mem_p (tree ref)
5342 tree base;
5344 if (TREE_CODE (ref) == MEM_REF
5345 || TREE_CODE (ref) == TARGET_MEM_REF)
5347 tree addr = TREE_OPERAND (ref, 0);
5349 if (TREE_CODE (addr) != ADDR_EXPR)
5350 return false;
5352 base = TREE_OPERAND (addr, 0);
5354 else
5355 base = ref;
5357 return non_mem_decl_p (base);
5360 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5361 is true, try generating a nontemporal store. */
5363 void
5364 expand_assignment (tree to, tree from, bool nontemporal)
5366 rtx to_rtx = 0;
5367 rtx result;
5368 machine_mode mode;
5369 unsigned int align;
5370 enum insn_code icode;
5372 /* Don't crash if the lhs of the assignment was erroneous. */
5373 if (TREE_CODE (to) == ERROR_MARK)
5375 expand_normal (from);
5376 return;
5379 /* Optimize away no-op moves without side-effects. */
5380 if (operand_equal_p (to, from, 0))
5381 return;
5383 /* Handle misaligned stores. */
5384 mode = TYPE_MODE (TREE_TYPE (to));
5385 if ((TREE_CODE (to) == MEM_REF
5386 || TREE_CODE (to) == TARGET_MEM_REF
5387 || DECL_P (to))
5388 && mode != BLKmode
5389 && !mem_ref_refers_to_non_mem_p (to)
5390 && ((align = get_object_alignment (to))
5391 < GET_MODE_ALIGNMENT (mode))
5392 && (((icode = optab_handler (movmisalign_optab, mode))
5393 != CODE_FOR_nothing)
5394 || targetm.slow_unaligned_access (mode, align)))
5396 rtx reg, mem;
5398 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5399 /* Handle PARALLEL. */
5400 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5401 reg = force_not_mem (reg);
5402 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5403 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5404 reg = flip_storage_order (mode, reg);
5406 if (icode != CODE_FOR_nothing)
5408 class expand_operand ops[2];
5410 create_fixed_operand (&ops[0], mem);
5411 create_input_operand (&ops[1], reg, mode);
5412 /* The movmisalign<mode> pattern cannot fail, else the assignment
5413 would silently be omitted. */
5414 expand_insn (icode, 2, ops);
5416 else
5417 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5418 false);
5419 return;
5422 /* Assignment of a structure component needs special treatment
5423 if the structure component's rtx is not simply a MEM.
5424 Assignment of an array element at a constant index, and assignment of
5425 an array element in an unaligned packed structure field, has the same
5426 problem. Same for (partially) storing into a non-memory object. */
5427 if (handled_component_p (to)
5428 || (TREE_CODE (to) == MEM_REF
5429 && (REF_REVERSE_STORAGE_ORDER (to)
5430 || mem_ref_refers_to_non_mem_p (to)))
5431 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5433 machine_mode mode1;
5434 poly_int64 bitsize, bitpos;
5435 poly_uint64 bitregion_start = 0;
5436 poly_uint64 bitregion_end = 0;
5437 tree offset;
5438 int unsignedp, reversep, volatilep = 0;
5439 tree tem;
5441 push_temp_slots ();
5442 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5443 &unsignedp, &reversep, &volatilep);
5445 /* Make sure bitpos is not negative, it can wreak havoc later. */
5446 if (maybe_lt (bitpos, 0))
5448 gcc_assert (offset == NULL_TREE);
5449 offset = size_int (bits_to_bytes_round_down (bitpos));
5450 bitpos = num_trailing_bits (bitpos);
5453 if (TREE_CODE (to) == COMPONENT_REF
5454 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5455 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5456 /* The C++ memory model naturally applies to byte-aligned fields.
5457 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5458 BITSIZE are not byte-aligned, there is no need to limit the range
5459 we can access. This can occur with packed structures in Ada. */
5460 else if (maybe_gt (bitsize, 0)
5461 && multiple_p (bitsize, BITS_PER_UNIT)
5462 && multiple_p (bitpos, BITS_PER_UNIT))
5464 bitregion_start = bitpos;
5465 bitregion_end = bitpos + bitsize - 1;
5468 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5470 /* If the field has a mode, we want to access it in the
5471 field's mode, not the computed mode.
5472 If a MEM has VOIDmode (external with incomplete type),
5473 use BLKmode for it instead. */
5474 if (MEM_P (to_rtx))
5476 if (mode1 != VOIDmode)
5477 to_rtx = adjust_address (to_rtx, mode1, 0);
5478 else if (GET_MODE (to_rtx) == VOIDmode)
5479 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5482 if (offset != 0)
5484 machine_mode address_mode;
5485 rtx offset_rtx;
5487 if (!MEM_P (to_rtx))
5489 /* We can get constant negative offsets into arrays with broken
5490 user code. Translate this to a trap instead of ICEing. */
5491 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5492 expand_builtin_trap ();
5493 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5496 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5497 address_mode = get_address_mode (to_rtx);
5498 if (GET_MODE (offset_rtx) != address_mode)
5500 /* We cannot be sure that the RTL in offset_rtx is valid outside
5501 of a memory address context, so force it into a register
5502 before attempting to convert it to the desired mode. */
5503 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5504 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5507 /* If we have an expression in OFFSET_RTX and a non-zero
5508 byte offset in BITPOS, adding the byte offset before the
5509 OFFSET_RTX results in better intermediate code, which makes
5510 later rtl optimization passes perform better.
5512 We prefer intermediate code like this:
5514 r124:DI=r123:DI+0x18
5515 [r124:DI]=r121:DI
5517 ... instead of ...
5519 r124:DI=r123:DI+0x10
5520 [r124:DI+0x8]=r121:DI
5522 This is only done for aligned data values, as these can
5523 be expected to result in single move instructions. */
5524 poly_int64 bytepos;
5525 if (mode1 != VOIDmode
5526 && maybe_ne (bitpos, 0)
5527 && maybe_gt (bitsize, 0)
5528 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5529 && multiple_p (bitpos, bitsize)
5530 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5531 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5533 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5534 bitregion_start = 0;
5535 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5536 bitregion_end -= bitpos;
5537 bitpos = 0;
5540 to_rtx = offset_address (to_rtx, offset_rtx,
5541 highest_pow2_factor_for_target (to,
5542 offset));
5545 /* No action is needed if the target is not a memory and the field
5546 lies completely outside that target. This can occur if the source
5547 code contains an out-of-bounds access to a small array. */
5548 if (!MEM_P (to_rtx)
5549 && GET_MODE (to_rtx) != BLKmode
5550 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5552 expand_normal (from);
5553 result = NULL;
5555 /* Handle expand_expr of a complex value returning a CONCAT. */
5556 else if (GET_CODE (to_rtx) == CONCAT)
5558 machine_mode to_mode = GET_MODE (to_rtx);
5559 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5560 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5561 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5562 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5563 && known_eq (bitpos, 0)
5564 && known_eq (bitsize, mode_bitsize))
5565 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5566 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5567 && known_eq (bitsize, inner_bitsize)
5568 && (known_eq (bitpos, 0)
5569 || known_eq (bitpos, inner_bitsize)))
5570 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5571 false, nontemporal, reversep);
5572 else if (known_le (bitpos + bitsize, inner_bitsize))
5573 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5574 bitregion_start, bitregion_end,
5575 mode1, from, get_alias_set (to),
5576 nontemporal, reversep);
5577 else if (known_ge (bitpos, inner_bitsize))
5578 result = store_field (XEXP (to_rtx, 1), bitsize,
5579 bitpos - inner_bitsize,
5580 bitregion_start, bitregion_end,
5581 mode1, from, get_alias_set (to),
5582 nontemporal, reversep);
5583 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5585 result = expand_normal (from);
5586 if (GET_CODE (result) == CONCAT)
5588 to_mode = GET_MODE_INNER (to_mode);
5589 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5590 rtx from_real
5591 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5592 from_mode, 0);
5593 rtx from_imag
5594 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5595 from_mode, 0);
5596 if (!from_real || !from_imag)
5597 goto concat_store_slow;
5598 emit_move_insn (XEXP (to_rtx, 0), from_real);
5599 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5601 else
5603 machine_mode from_mode
5604 = GET_MODE (result) == VOIDmode
5605 ? TYPE_MODE (TREE_TYPE (from))
5606 : GET_MODE (result);
5607 rtx from_rtx;
5608 if (MEM_P (result))
5609 from_rtx = change_address (result, to_mode, NULL_RTX);
5610 else
5611 from_rtx
5612 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5613 if (from_rtx)
5615 emit_move_insn (XEXP (to_rtx, 0),
5616 read_complex_part (from_rtx, false));
5617 emit_move_insn (XEXP (to_rtx, 1),
5618 read_complex_part (from_rtx, true));
5620 else
5622 to_mode = GET_MODE_INNER (to_mode);
5623 rtx from_real
5624 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5625 rtx from_imag
5626 = simplify_gen_subreg (to_mode, result, from_mode,
5627 GET_MODE_SIZE (to_mode));
5628 if (!from_real || !from_imag)
5629 goto concat_store_slow;
5630 emit_move_insn (XEXP (to_rtx, 0), from_real);
5631 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5635 else
5637 concat_store_slow:;
5638 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5639 GET_MODE_SIZE (GET_MODE (to_rtx)));
5640 write_complex_part (temp, XEXP (to_rtx, 0), false);
5641 write_complex_part (temp, XEXP (to_rtx, 1), true);
5642 result = store_field (temp, bitsize, bitpos,
5643 bitregion_start, bitregion_end,
5644 mode1, from, get_alias_set (to),
5645 nontemporal, reversep);
5646 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5647 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5650 /* For calls to functions returning variable length structures, if TO_RTX
5651 is not a MEM, go through a MEM because we must not create temporaries
5652 of the VLA type. */
5653 else if (!MEM_P (to_rtx)
5654 && TREE_CODE (from) == CALL_EXPR
5655 && COMPLETE_TYPE_P (TREE_TYPE (from))
5656 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5658 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5659 GET_MODE_SIZE (GET_MODE (to_rtx)));
5660 result = store_field (temp, bitsize, bitpos, bitregion_start,
5661 bitregion_end, mode1, from, get_alias_set (to),
5662 nontemporal, reversep);
5663 emit_move_insn (to_rtx, temp);
5665 else
5667 if (MEM_P (to_rtx))
5669 /* If the field is at offset zero, we could have been given the
5670 DECL_RTX of the parent struct. Don't munge it. */
5671 to_rtx = shallow_copy_rtx (to_rtx);
5672 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5673 if (volatilep)
5674 MEM_VOLATILE_P (to_rtx) = 1;
5677 gcc_checking_assert (known_ge (bitpos, 0));
5678 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5679 bitregion_start, bitregion_end,
5680 mode1, to_rtx, to, from,
5681 reversep))
5682 result = NULL;
5683 else if (SUBREG_P (to_rtx)
5684 && SUBREG_PROMOTED_VAR_P (to_rtx))
5686 /* If to_rtx is a promoted subreg, we need to zero or sign
5687 extend the value afterwards. */
5688 if (TREE_CODE (to) == MEM_REF
5689 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5690 && !REF_REVERSE_STORAGE_ORDER (to)
5691 && known_eq (bitpos, 0)
5692 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5693 result = store_expr (from, to_rtx, 0, nontemporal, false);
5694 else
5696 rtx to_rtx1
5697 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5698 SUBREG_REG (to_rtx),
5699 subreg_promoted_mode (to_rtx));
5700 result = store_field (to_rtx1, bitsize, bitpos,
5701 bitregion_start, bitregion_end,
5702 mode1, from, get_alias_set (to),
5703 nontemporal, reversep);
5704 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5705 SUBREG_PROMOTED_SIGN (to_rtx));
5708 else
5709 result = store_field (to_rtx, bitsize, bitpos,
5710 bitregion_start, bitregion_end,
5711 mode1, from, get_alias_set (to),
5712 nontemporal, reversep);
5715 if (result)
5716 preserve_temp_slots (result);
5717 pop_temp_slots ();
5718 return;
5721 /* If the rhs is a function call and its value is not an aggregate,
5722 call the function before we start to compute the lhs.
5723 This is needed for correct code for cases such as
5724 val = setjmp (buf) on machines where reference to val
5725 requires loading up part of an address in a separate insn.
5727 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5728 since it might be a promoted variable where the zero- or sign- extension
5729 needs to be done. Handling this in the normal way is safe because no
5730 computation is done before the call. The same is true for SSA names. */
5731 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5732 && COMPLETE_TYPE_P (TREE_TYPE (from))
5733 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5734 && ! (((VAR_P (to)
5735 || TREE_CODE (to) == PARM_DECL
5736 || TREE_CODE (to) == RESULT_DECL)
5737 && REG_P (DECL_RTL (to)))
5738 || TREE_CODE (to) == SSA_NAME))
5740 rtx value;
5742 push_temp_slots ();
5743 value = expand_normal (from);
5745 if (to_rtx == 0)
5746 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5748 /* Handle calls that return values in multiple non-contiguous locations.
5749 The Irix 6 ABI has examples of this. */
5750 if (GET_CODE (to_rtx) == PARALLEL)
5752 if (GET_CODE (value) == PARALLEL)
5753 emit_group_move (to_rtx, value);
5754 else
5755 emit_group_load (to_rtx, value, TREE_TYPE (from),
5756 int_size_in_bytes (TREE_TYPE (from)));
5758 else if (GET_CODE (value) == PARALLEL)
5759 emit_group_store (to_rtx, value, TREE_TYPE (from),
5760 int_size_in_bytes (TREE_TYPE (from)));
5761 else if (GET_MODE (to_rtx) == BLKmode)
5763 /* Handle calls that return BLKmode values in registers. */
5764 if (REG_P (value))
5765 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5766 else
5767 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5769 else
5771 if (POINTER_TYPE_P (TREE_TYPE (to)))
5772 value = convert_memory_address_addr_space
5773 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5774 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5776 emit_move_insn (to_rtx, value);
5779 preserve_temp_slots (to_rtx);
5780 pop_temp_slots ();
5781 return;
5784 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5785 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5787 /* Don't move directly into a return register. */
5788 if (TREE_CODE (to) == RESULT_DECL
5789 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5791 rtx temp;
5793 push_temp_slots ();
5795 /* If the source is itself a return value, it still is in a pseudo at
5796 this point so we can move it back to the return register directly. */
5797 if (REG_P (to_rtx)
5798 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5799 && TREE_CODE (from) != CALL_EXPR)
5800 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5801 else
5802 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5804 /* Handle calls that return values in multiple non-contiguous locations.
5805 The Irix 6 ABI has examples of this. */
5806 if (GET_CODE (to_rtx) == PARALLEL)
5808 if (GET_CODE (temp) == PARALLEL)
5809 emit_group_move (to_rtx, temp);
5810 else
5811 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5812 int_size_in_bytes (TREE_TYPE (from)));
5814 else if (temp)
5815 emit_move_insn (to_rtx, temp);
5817 preserve_temp_slots (to_rtx);
5818 pop_temp_slots ();
5819 return;
5822 /* In case we are returning the contents of an object which overlaps
5823 the place the value is being stored, use a safe function when copying
5824 a value through a pointer into a structure value return block. */
5825 if (TREE_CODE (to) == RESULT_DECL
5826 && TREE_CODE (from) == INDIRECT_REF
5827 && ADDR_SPACE_GENERIC_P
5828 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5829 && refs_may_alias_p (to, from)
5830 && cfun->returns_struct
5831 && !cfun->returns_pcc_struct)
5833 rtx from_rtx, size;
5835 push_temp_slots ();
5836 size = expr_size (from);
5837 from_rtx = expand_normal (from);
5839 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5841 preserve_temp_slots (to_rtx);
5842 pop_temp_slots ();
5843 return;
5846 /* Compute FROM and store the value in the rtx we got. */
5848 push_temp_slots ();
5849 result = store_expr (from, to_rtx, 0, nontemporal, false);
5850 preserve_temp_slots (result);
5851 pop_temp_slots ();
5852 return;
5855 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5856 succeeded, false otherwise. */
5858 bool
5859 emit_storent_insn (rtx to, rtx from)
5861 class expand_operand ops[2];
5862 machine_mode mode = GET_MODE (to);
5863 enum insn_code code = optab_handler (storent_optab, mode);
5865 if (code == CODE_FOR_nothing)
5866 return false;
5868 create_fixed_operand (&ops[0], to);
5869 create_input_operand (&ops[1], from, mode);
5870 return maybe_expand_insn (code, 2, ops);
5873 /* Helper function for store_expr storing of STRING_CST. */
5875 static rtx
5876 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5877 fixed_size_mode mode)
5879 tree str = (tree) data;
5881 gcc_assert (offset >= 0);
5882 if (offset >= TREE_STRING_LENGTH (str))
5883 return const0_rtx;
5885 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5886 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5888 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5889 size_t l = TREE_STRING_LENGTH (str) - offset;
5890 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5891 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5892 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
5895 /* The by-pieces infrastructure does not try to pick a vector mode
5896 for storing STRING_CST. */
5897 return c_readstr (TREE_STRING_POINTER (str) + offset,
5898 as_a <scalar_int_mode> (mode), false);
5901 /* Generate code for computing expression EXP,
5902 and storing the value into TARGET.
5904 If the mode is BLKmode then we may return TARGET itself.
5905 It turns out that in BLKmode it doesn't cause a problem.
5906 because C has no operators that could combine two different
5907 assignments into the same BLKmode object with different values
5908 with no sequence point. Will other languages need this to
5909 be more thorough?
5911 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5912 stack, and block moves may need to be treated specially.
5914 If NONTEMPORAL is true, try using a nontemporal store instruction.
5916 If REVERSE is true, the store is to be done in reverse order. */
5919 store_expr (tree exp, rtx target, int call_param_p,
5920 bool nontemporal, bool reverse)
5922 rtx temp;
5923 rtx alt_rtl = NULL_RTX;
5924 location_t loc = curr_insn_location ();
5925 bool shortened_string_cst = false;
5927 if (VOID_TYPE_P (TREE_TYPE (exp)))
5929 /* C++ can generate ?: expressions with a throw expression in one
5930 branch and an rvalue in the other. Here, we resolve attempts to
5931 store the throw expression's nonexistent result. */
5932 gcc_assert (!call_param_p);
5933 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5934 return NULL_RTX;
5936 if (TREE_CODE (exp) == COMPOUND_EXPR)
5938 /* Perform first part of compound expression, then assign from second
5939 part. */
5940 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5941 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5942 return store_expr (TREE_OPERAND (exp, 1), target,
5943 call_param_p, nontemporal, reverse);
5945 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5947 /* For conditional expression, get safe form of the target. Then
5948 test the condition, doing the appropriate assignment on either
5949 side. This avoids the creation of unnecessary temporaries.
5950 For non-BLKmode, it is more efficient not to do this. */
5952 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5954 do_pending_stack_adjust ();
5955 NO_DEFER_POP;
5956 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5957 profile_probability::uninitialized ());
5958 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5959 nontemporal, reverse);
5960 emit_jump_insn (targetm.gen_jump (lab2));
5961 emit_barrier ();
5962 emit_label (lab1);
5963 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5964 nontemporal, reverse);
5965 emit_label (lab2);
5966 OK_DEFER_POP;
5968 return NULL_RTX;
5970 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5971 /* If this is a scalar in a register that is stored in a wider mode
5972 than the declared mode, compute the result into its declared mode
5973 and then convert to the wider mode. Our value is the computed
5974 expression. */
5976 rtx inner_target = 0;
5977 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5978 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5980 /* We can do the conversion inside EXP, which will often result
5981 in some optimizations. Do the conversion in two steps: first
5982 change the signedness, if needed, then the extend. But don't
5983 do this if the type of EXP is a subtype of something else
5984 since then the conversion might involve more than just
5985 converting modes. */
5986 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5987 && TREE_TYPE (TREE_TYPE (exp)) == 0
5988 && GET_MODE_PRECISION (outer_mode)
5989 == TYPE_PRECISION (TREE_TYPE (exp)))
5991 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5992 TYPE_UNSIGNED (TREE_TYPE (exp))))
5994 /* Some types, e.g. Fortran's logical*4, won't have a signed
5995 version, so use the mode instead. */
5996 tree ntype
5997 = (signed_or_unsigned_type_for
5998 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5999 if (ntype == NULL)
6000 ntype = lang_hooks.types.type_for_mode
6001 (TYPE_MODE (TREE_TYPE (exp)),
6002 SUBREG_PROMOTED_SIGN (target));
6004 exp = fold_convert_loc (loc, ntype, exp);
6007 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6008 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6009 exp);
6011 inner_target = SUBREG_REG (target);
6014 temp = expand_expr (exp, inner_target, VOIDmode,
6015 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6018 /* If TEMP is a VOIDmode constant, use convert_modes to make
6019 sure that we properly convert it. */
6020 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6022 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6023 temp, SUBREG_PROMOTED_SIGN (target));
6024 temp = convert_modes (inner_mode, outer_mode, temp,
6025 SUBREG_PROMOTED_SIGN (target));
6028 convert_move (SUBREG_REG (target), temp,
6029 SUBREG_PROMOTED_SIGN (target));
6031 return NULL_RTX;
6033 else if ((TREE_CODE (exp) == STRING_CST
6034 || (TREE_CODE (exp) == MEM_REF
6035 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6036 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6037 == STRING_CST
6038 && integer_zerop (TREE_OPERAND (exp, 1))))
6039 && !nontemporal && !call_param_p
6040 && MEM_P (target))
6042 /* Optimize initialization of an array with a STRING_CST. */
6043 HOST_WIDE_INT exp_len, str_copy_len;
6044 rtx dest_mem;
6045 tree str = TREE_CODE (exp) == STRING_CST
6046 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6048 exp_len = int_expr_size (exp);
6049 if (exp_len <= 0)
6050 goto normal_expr;
6052 if (TREE_STRING_LENGTH (str) <= 0)
6053 goto normal_expr;
6055 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6056 MEM_ALIGN (target), false))
6058 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6059 MEM_ALIGN (target), false, RETURN_BEGIN);
6060 return NULL_RTX;
6063 str_copy_len = TREE_STRING_LENGTH (str);
6064 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6066 str_copy_len += STORE_MAX_PIECES - 1;
6067 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6069 if (str_copy_len >= exp_len)
6070 goto normal_expr;
6072 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6073 (void *) str, MEM_ALIGN (target), false))
6074 goto normal_expr;
6076 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6077 (void *) str, MEM_ALIGN (target), false,
6078 RETURN_END);
6079 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6080 exp_len - str_copy_len),
6081 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6082 return NULL_RTX;
6084 else
6086 rtx tmp_target;
6088 normal_expr:
6089 /* If we want to use a nontemporal or a reverse order store, force the
6090 value into a register first. */
6091 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6092 tree rexp = exp;
6093 if (TREE_CODE (exp) == STRING_CST
6094 && tmp_target == target
6095 && GET_MODE (target) == BLKmode
6096 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6098 rtx size = expr_size (exp);
6099 if (CONST_INT_P (size)
6100 && size != const0_rtx
6101 && (UINTVAL (size)
6102 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6104 /* If the STRING_CST has much larger array type than
6105 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6106 it into the rodata section as the code later on will use
6107 memset zero for the remainder anyway. See PR95052. */
6108 tmp_target = NULL_RTX;
6109 rexp = copy_node (exp);
6110 tree index
6111 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6112 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6113 index);
6114 shortened_string_cst = true;
6117 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6118 (call_param_p
6119 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6120 &alt_rtl, false);
6121 if (shortened_string_cst)
6123 gcc_assert (MEM_P (temp));
6124 temp = change_address (temp, BLKmode, NULL_RTX);
6128 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6129 the same as that of TARGET, adjust the constant. This is needed, for
6130 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6131 only a word-sized value. */
6132 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6133 && TREE_CODE (exp) != ERROR_MARK
6134 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6136 gcc_assert (!shortened_string_cst);
6137 if (GET_MODE_CLASS (GET_MODE (target))
6138 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6139 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6140 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6142 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6143 TYPE_MODE (TREE_TYPE (exp)), 0);
6144 if (t)
6145 temp = t;
6147 if (GET_MODE (temp) == VOIDmode)
6148 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6149 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6152 /* If value was not generated in the target, store it there.
6153 Convert the value to TARGET's type first if necessary and emit the
6154 pending incrementations that have been queued when expanding EXP.
6155 Note that we cannot emit the whole queue blindly because this will
6156 effectively disable the POST_INC optimization later.
6158 If TEMP and TARGET compare equal according to rtx_equal_p, but
6159 one or both of them are volatile memory refs, we have to distinguish
6160 two cases:
6161 - expand_expr has used TARGET. In this case, we must not generate
6162 another copy. This can be detected by TARGET being equal according
6163 to == .
6164 - expand_expr has not used TARGET - that means that the source just
6165 happens to have the same RTX form. Since temp will have been created
6166 by expand_expr, it will compare unequal according to == .
6167 We must generate a copy in this case, to reach the correct number
6168 of volatile memory references. */
6170 if ((! rtx_equal_p (temp, target)
6171 || (temp != target && (side_effects_p (temp)
6172 || side_effects_p (target))))
6173 && TREE_CODE (exp) != ERROR_MARK
6174 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6175 but TARGET is not valid memory reference, TEMP will differ
6176 from TARGET although it is really the same location. */
6177 && !(alt_rtl
6178 && rtx_equal_p (alt_rtl, target)
6179 && !side_effects_p (alt_rtl)
6180 && !side_effects_p (target))
6181 /* If there's nothing to copy, don't bother. Don't call
6182 expr_size unless necessary, because some front-ends (C++)
6183 expr_size-hook must not be given objects that are not
6184 supposed to be bit-copied or bit-initialized. */
6185 && expr_size (exp) != const0_rtx)
6187 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6189 gcc_assert (!shortened_string_cst);
6190 if (GET_MODE (target) == BLKmode)
6192 /* Handle calls that return BLKmode values in registers. */
6193 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6194 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6195 else
6196 store_bit_field (target,
6197 rtx_to_poly_int64 (expr_size (exp))
6198 * BITS_PER_UNIT,
6199 0, 0, 0, GET_MODE (temp), temp, reverse);
6201 else
6202 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6205 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6207 /* Handle copying a string constant into an array. The string
6208 constant may be shorter than the array. So copy just the string's
6209 actual length, and clear the rest. First get the size of the data
6210 type of the string, which is actually the size of the target. */
6211 rtx size = expr_size (exp);
6213 if (CONST_INT_P (size)
6214 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6215 emit_block_move (target, temp, size,
6216 (call_param_p
6217 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6218 else
6220 machine_mode pointer_mode
6221 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6222 machine_mode address_mode = get_address_mode (target);
6224 /* Compute the size of the data to copy from the string. */
6225 tree copy_size
6226 = size_binop_loc (loc, MIN_EXPR,
6227 make_tree (sizetype, size),
6228 size_int (TREE_STRING_LENGTH (exp)));
6229 rtx copy_size_rtx
6230 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6231 (call_param_p
6232 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6233 rtx_code_label *label = 0;
6235 /* Copy that much. */
6236 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6237 TYPE_UNSIGNED (sizetype));
6238 emit_block_move (target, temp, copy_size_rtx,
6239 (call_param_p
6240 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6242 /* Figure out how much is left in TARGET that we have to clear.
6243 Do all calculations in pointer_mode. */
6244 poly_int64 const_copy_size;
6245 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6247 size = plus_constant (address_mode, size, -const_copy_size);
6248 target = adjust_address (target, BLKmode, const_copy_size);
6250 else
6252 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6253 copy_size_rtx, NULL_RTX, 0,
6254 OPTAB_LIB_WIDEN);
6256 if (GET_MODE (copy_size_rtx) != address_mode)
6257 copy_size_rtx = convert_to_mode (address_mode,
6258 copy_size_rtx,
6259 TYPE_UNSIGNED (sizetype));
6261 target = offset_address (target, copy_size_rtx,
6262 highest_pow2_factor (copy_size));
6263 label = gen_label_rtx ();
6264 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6265 GET_MODE (size), 0, label);
6268 if (size != const0_rtx)
6269 clear_storage (target, size, BLOCK_OP_NORMAL);
6271 if (label)
6272 emit_label (label);
6275 else if (shortened_string_cst)
6276 gcc_unreachable ();
6277 /* Handle calls that return values in multiple non-contiguous locations.
6278 The Irix 6 ABI has examples of this. */
6279 else if (GET_CODE (target) == PARALLEL)
6281 if (GET_CODE (temp) == PARALLEL)
6282 emit_group_move (target, temp);
6283 else
6284 emit_group_load (target, temp, TREE_TYPE (exp),
6285 int_size_in_bytes (TREE_TYPE (exp)));
6287 else if (GET_CODE (temp) == PARALLEL)
6288 emit_group_store (target, temp, TREE_TYPE (exp),
6289 int_size_in_bytes (TREE_TYPE (exp)));
6290 else if (GET_MODE (temp) == BLKmode)
6291 emit_block_move (target, temp, expr_size (exp),
6292 (call_param_p
6293 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6294 /* If we emit a nontemporal store, there is nothing else to do. */
6295 else if (nontemporal && emit_storent_insn (target, temp))
6297 else
6299 if (reverse)
6300 temp = flip_storage_order (GET_MODE (target), temp);
6301 temp = force_operand (temp, target);
6302 if (temp != target)
6303 emit_move_insn (target, temp);
6306 else
6307 gcc_assert (!shortened_string_cst);
6309 return NULL_RTX;
6312 /* Return true if field F of structure TYPE is a flexible array. */
6314 static bool
6315 flexible_array_member_p (const_tree f, const_tree type)
6317 const_tree tf;
6319 tf = TREE_TYPE (f);
6320 return (DECL_CHAIN (f) == NULL
6321 && TREE_CODE (tf) == ARRAY_TYPE
6322 && TYPE_DOMAIN (tf)
6323 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6324 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6325 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6326 && int_size_in_bytes (type) >= 0);
6329 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6330 must have in order for it to completely initialize a value of type TYPE.
6331 Return -1 if the number isn't known.
6333 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6335 static HOST_WIDE_INT
6336 count_type_elements (const_tree type, bool for_ctor_p)
6338 switch (TREE_CODE (type))
6340 case ARRAY_TYPE:
6342 tree nelts;
6344 nelts = array_type_nelts (type);
6345 if (nelts && tree_fits_uhwi_p (nelts))
6347 unsigned HOST_WIDE_INT n;
6349 n = tree_to_uhwi (nelts) + 1;
6350 if (n == 0 || for_ctor_p)
6351 return n;
6352 else
6353 return n * count_type_elements (TREE_TYPE (type), false);
6355 return for_ctor_p ? -1 : 1;
6358 case RECORD_TYPE:
6360 unsigned HOST_WIDE_INT n;
6361 tree f;
6363 n = 0;
6364 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6365 if (TREE_CODE (f) == FIELD_DECL)
6367 if (!for_ctor_p)
6368 n += count_type_elements (TREE_TYPE (f), false);
6369 else if (!flexible_array_member_p (f, type))
6370 /* Don't count flexible arrays, which are not supposed
6371 to be initialized. */
6372 n += 1;
6375 return n;
6378 case UNION_TYPE:
6379 case QUAL_UNION_TYPE:
6381 tree f;
6382 HOST_WIDE_INT n, m;
6384 gcc_assert (!for_ctor_p);
6385 /* Estimate the number of scalars in each field and pick the
6386 maximum. Other estimates would do instead; the idea is simply
6387 to make sure that the estimate is not sensitive to the ordering
6388 of the fields. */
6389 n = 1;
6390 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6391 if (TREE_CODE (f) == FIELD_DECL)
6393 m = count_type_elements (TREE_TYPE (f), false);
6394 /* If the field doesn't span the whole union, add an extra
6395 scalar for the rest. */
6396 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6397 TYPE_SIZE (type)) != 1)
6398 m++;
6399 if (n < m)
6400 n = m;
6402 return n;
6405 case COMPLEX_TYPE:
6406 return 2;
6408 case VECTOR_TYPE:
6410 unsigned HOST_WIDE_INT nelts;
6411 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6412 return nelts;
6413 else
6414 return -1;
6417 case INTEGER_TYPE:
6418 case REAL_TYPE:
6419 case FIXED_POINT_TYPE:
6420 case ENUMERAL_TYPE:
6421 case BOOLEAN_TYPE:
6422 case POINTER_TYPE:
6423 case OFFSET_TYPE:
6424 case REFERENCE_TYPE:
6425 case NULLPTR_TYPE:
6426 return 1;
6428 case ERROR_MARK:
6429 return 0;
6431 case VOID_TYPE:
6432 case OPAQUE_TYPE:
6433 case METHOD_TYPE:
6434 case FUNCTION_TYPE:
6435 case LANG_TYPE:
6436 default:
6437 gcc_unreachable ();
6441 /* Helper for categorize_ctor_elements. Identical interface. */
6443 static bool
6444 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6445 HOST_WIDE_INT *p_unique_nz_elts,
6446 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6448 unsigned HOST_WIDE_INT idx;
6449 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6450 tree value, purpose, elt_type;
6452 /* Whether CTOR is a valid constant initializer, in accordance with what
6453 initializer_constant_valid_p does. If inferred from the constructor
6454 elements, true until proven otherwise. */
6455 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6456 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6458 nz_elts = 0;
6459 unique_nz_elts = 0;
6460 init_elts = 0;
6461 num_fields = 0;
6462 elt_type = NULL_TREE;
6464 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6466 HOST_WIDE_INT mult = 1;
6468 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6470 tree lo_index = TREE_OPERAND (purpose, 0);
6471 tree hi_index = TREE_OPERAND (purpose, 1);
6473 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6474 mult = (tree_to_uhwi (hi_index)
6475 - tree_to_uhwi (lo_index) + 1);
6477 num_fields += mult;
6478 elt_type = TREE_TYPE (value);
6480 switch (TREE_CODE (value))
6482 case CONSTRUCTOR:
6484 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6486 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6487 &ic, p_complete);
6489 nz_elts += mult * nz;
6490 unique_nz_elts += unz;
6491 init_elts += mult * ic;
6493 if (const_from_elts_p && const_p)
6494 const_p = const_elt_p;
6496 break;
6498 case INTEGER_CST:
6499 case REAL_CST:
6500 case FIXED_CST:
6501 if (!initializer_zerop (value))
6503 nz_elts += mult;
6504 unique_nz_elts++;
6506 init_elts += mult;
6507 break;
6509 case STRING_CST:
6510 nz_elts += mult * TREE_STRING_LENGTH (value);
6511 unique_nz_elts += TREE_STRING_LENGTH (value);
6512 init_elts += mult * TREE_STRING_LENGTH (value);
6513 break;
6515 case COMPLEX_CST:
6516 if (!initializer_zerop (TREE_REALPART (value)))
6518 nz_elts += mult;
6519 unique_nz_elts++;
6521 if (!initializer_zerop (TREE_IMAGPART (value)))
6523 nz_elts += mult;
6524 unique_nz_elts++;
6526 init_elts += 2 * mult;
6527 break;
6529 case VECTOR_CST:
6531 /* We can only construct constant-length vectors using
6532 CONSTRUCTOR. */
6533 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6534 for (unsigned int i = 0; i < nunits; ++i)
6536 tree v = VECTOR_CST_ELT (value, i);
6537 if (!initializer_zerop (v))
6539 nz_elts += mult;
6540 unique_nz_elts++;
6542 init_elts += mult;
6545 break;
6547 default:
6549 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6550 nz_elts += mult * tc;
6551 unique_nz_elts += tc;
6552 init_elts += mult * tc;
6554 if (const_from_elts_p && const_p)
6555 const_p
6556 = initializer_constant_valid_p (value,
6557 elt_type,
6558 TYPE_REVERSE_STORAGE_ORDER
6559 (TREE_TYPE (ctor)))
6560 != NULL_TREE;
6562 break;
6566 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6567 num_fields, elt_type))
6568 *p_complete = false;
6570 *p_nz_elts += nz_elts;
6571 *p_unique_nz_elts += unique_nz_elts;
6572 *p_init_elts += init_elts;
6574 return const_p;
6577 /* Examine CTOR to discover:
6578 * how many scalar fields are set to nonzero values,
6579 and place it in *P_NZ_ELTS;
6580 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6581 high - low + 1 (this can be useful for callers to determine ctors
6582 that could be cheaply initialized with - perhaps nested - loops
6583 compared to copied from huge read-only data),
6584 and place it in *P_UNIQUE_NZ_ELTS;
6585 * how many scalar fields in total are in CTOR,
6586 and place it in *P_ELT_COUNT.
6587 * whether the constructor is complete -- in the sense that every
6588 meaningful byte is explicitly given a value --
6589 and place it in *P_COMPLETE.
6591 Return whether or not CTOR is a valid static constant initializer, the same
6592 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6594 bool
6595 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6596 HOST_WIDE_INT *p_unique_nz_elts,
6597 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6599 *p_nz_elts = 0;
6600 *p_unique_nz_elts = 0;
6601 *p_init_elts = 0;
6602 *p_complete = true;
6604 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6605 p_init_elts, p_complete);
6608 /* Return true if constructor CTOR is simple enough to be materialized
6609 in an integer mode register. Limit the size to WORDS words, which
6610 is 1 by default. */
6612 bool
6613 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6615 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6616 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6617 return false;
6619 return TREE_CONSTANT (ctor)
6620 && !TREE_ADDRESSABLE (ctor)
6621 && CONSTRUCTOR_NELTS (ctor)
6622 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6623 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6624 && initializer_constant_valid_for_bitfield_p (ctor);
6627 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6628 of which had type LAST_TYPE. Each element was itself a complete
6629 initializer, in the sense that every meaningful byte was explicitly
6630 given a value. Return true if the same is true for the constructor
6631 as a whole. */
6633 bool
6634 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6635 const_tree last_type)
6637 if (TREE_CODE (type) == UNION_TYPE
6638 || TREE_CODE (type) == QUAL_UNION_TYPE)
6640 if (num_elts == 0)
6641 return false;
6643 gcc_assert (num_elts == 1 && last_type);
6645 /* ??? We could look at each element of the union, and find the
6646 largest element. Which would avoid comparing the size of the
6647 initialized element against any tail padding in the union.
6648 Doesn't seem worth the effort... */
6649 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6652 return count_type_elements (type, true) == num_elts;
6655 /* Return 1 if EXP contains mostly (3/4) zeros. */
6657 static int
6658 mostly_zeros_p (const_tree exp)
6660 if (TREE_CODE (exp) == CONSTRUCTOR)
6662 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6663 bool complete_p;
6665 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6666 &complete_p);
6667 return !complete_p || nz_elts < init_elts / 4;
6670 return initializer_zerop (exp);
6673 /* Return 1 if EXP contains all zeros. */
6675 static int
6676 all_zeros_p (const_tree exp)
6678 if (TREE_CODE (exp) == CONSTRUCTOR)
6680 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6681 bool complete_p;
6683 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6684 &complete_p);
6685 return nz_elts == 0;
6688 return initializer_zerop (exp);
6691 /* Helper function for store_constructor.
6692 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6693 CLEARED is as for store_constructor.
6694 ALIAS_SET is the alias set to use for any stores.
6695 If REVERSE is true, the store is to be done in reverse order.
6697 This provides a recursive shortcut back to store_constructor when it isn't
6698 necessary to go through store_field. This is so that we can pass through
6699 the cleared field to let store_constructor know that we may not have to
6700 clear a substructure if the outer structure has already been cleared. */
6702 static void
6703 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6704 poly_uint64 bitregion_start,
6705 poly_uint64 bitregion_end,
6706 machine_mode mode,
6707 tree exp, int cleared,
6708 alias_set_type alias_set, bool reverse)
6710 poly_int64 bytepos;
6711 poly_uint64 bytesize;
6712 if (TREE_CODE (exp) == CONSTRUCTOR
6713 /* We can only call store_constructor recursively if the size and
6714 bit position are on a byte boundary. */
6715 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6716 && maybe_ne (bitsize, 0U)
6717 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6718 /* If we have a nonzero bitpos for a register target, then we just
6719 let store_field do the bitfield handling. This is unlikely to
6720 generate unnecessary clear instructions anyways. */
6721 && (known_eq (bitpos, 0) || MEM_P (target)))
6723 if (MEM_P (target))
6725 machine_mode target_mode = GET_MODE (target);
6726 if (target_mode != BLKmode
6727 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6728 target_mode = BLKmode;
6729 target = adjust_address (target, target_mode, bytepos);
6733 /* Update the alias set, if required. */
6734 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6735 && MEM_ALIAS_SET (target) != 0)
6737 target = copy_rtx (target);
6738 set_mem_alias_set (target, alias_set);
6741 store_constructor (exp, target, cleared, bytesize, reverse);
6743 else
6744 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6745 exp, alias_set, false, reverse);
6749 /* Returns the number of FIELD_DECLs in TYPE. */
6751 static int
6752 fields_length (const_tree type)
6754 tree t = TYPE_FIELDS (type);
6755 int count = 0;
6757 for (; t; t = DECL_CHAIN (t))
6758 if (TREE_CODE (t) == FIELD_DECL)
6759 ++count;
6761 return count;
6765 /* Store the value of constructor EXP into the rtx TARGET.
6766 TARGET is either a REG or a MEM; we know it cannot conflict, since
6767 safe_from_p has been called.
6768 CLEARED is true if TARGET is known to have been zero'd.
6769 SIZE is the number of bytes of TARGET we are allowed to modify: this
6770 may not be the same as the size of EXP if we are assigning to a field
6771 which has been packed to exclude padding bits.
6772 If REVERSE is true, the store is to be done in reverse order. */
6774 void
6775 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6776 bool reverse)
6778 tree type = TREE_TYPE (exp);
6779 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6780 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6782 switch (TREE_CODE (type))
6784 case RECORD_TYPE:
6785 case UNION_TYPE:
6786 case QUAL_UNION_TYPE:
6788 unsigned HOST_WIDE_INT idx;
6789 tree field, value;
6791 /* The storage order is specified for every aggregate type. */
6792 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6794 /* If size is zero or the target is already cleared, do nothing. */
6795 if (known_eq (size, 0) || cleared)
6796 cleared = 1;
6797 /* We either clear the aggregate or indicate the value is dead. */
6798 else if ((TREE_CODE (type) == UNION_TYPE
6799 || TREE_CODE (type) == QUAL_UNION_TYPE)
6800 && ! CONSTRUCTOR_ELTS (exp))
6801 /* If the constructor is empty, clear the union. */
6803 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6804 cleared = 1;
6807 /* If we are building a static constructor into a register,
6808 set the initial value as zero so we can fold the value into
6809 a constant. But if more than one register is involved,
6810 this probably loses. */
6811 else if (REG_P (target) && TREE_STATIC (exp)
6812 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6813 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6815 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6816 cleared = 1;
6819 /* If the constructor has fewer fields than the structure or
6820 if we are initializing the structure to mostly zeros, clear
6821 the whole structure first. Don't do this if TARGET is a
6822 register whose mode size isn't equal to SIZE since
6823 clear_storage can't handle this case. */
6824 else if (known_size_p (size)
6825 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6826 || mostly_zeros_p (exp))
6827 && (!REG_P (target)
6828 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6830 clear_storage (target, gen_int_mode (size, Pmode),
6831 BLOCK_OP_NORMAL);
6832 cleared = 1;
6835 if (REG_P (target) && !cleared)
6836 emit_clobber (target);
6838 /* Store each element of the constructor into the
6839 corresponding field of TARGET. */
6840 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6842 machine_mode mode;
6843 HOST_WIDE_INT bitsize;
6844 HOST_WIDE_INT bitpos = 0;
6845 tree offset;
6846 rtx to_rtx = target;
6848 /* Just ignore missing fields. We cleared the whole
6849 structure, above, if any fields are missing. */
6850 if (field == 0)
6851 continue;
6853 if (cleared && initializer_zerop (value))
6854 continue;
6856 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6857 bitsize = tree_to_uhwi (DECL_SIZE (field));
6858 else
6859 gcc_unreachable ();
6861 mode = DECL_MODE (field);
6862 if (DECL_BIT_FIELD (field))
6863 mode = VOIDmode;
6865 offset = DECL_FIELD_OFFSET (field);
6866 if (tree_fits_shwi_p (offset)
6867 && tree_fits_shwi_p (bit_position (field)))
6869 bitpos = int_bit_position (field);
6870 offset = NULL_TREE;
6872 else
6873 gcc_unreachable ();
6875 /* If this initializes a field that is smaller than a
6876 word, at the start of a word, try to widen it to a full
6877 word. This special case allows us to output C++ member
6878 function initializations in a form that the optimizers
6879 can understand. */
6880 if (WORD_REGISTER_OPERATIONS
6881 && REG_P (target)
6882 && bitsize < BITS_PER_WORD
6883 && bitpos % BITS_PER_WORD == 0
6884 && GET_MODE_CLASS (mode) == MODE_INT
6885 && TREE_CODE (value) == INTEGER_CST
6886 && exp_size >= 0
6887 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6889 type = TREE_TYPE (value);
6891 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6893 type = lang_hooks.types.type_for_mode
6894 (word_mode, TYPE_UNSIGNED (type));
6895 value = fold_convert (type, value);
6896 /* Make sure the bits beyond the original bitsize are zero
6897 so that we can correctly avoid extra zeroing stores in
6898 later constructor elements. */
6899 tree bitsize_mask
6900 = wide_int_to_tree (type, wi::mask (bitsize, false,
6901 BITS_PER_WORD));
6902 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6905 if (BYTES_BIG_ENDIAN)
6906 value
6907 = fold_build2 (LSHIFT_EXPR, type, value,
6908 build_int_cst (type,
6909 BITS_PER_WORD - bitsize));
6910 bitsize = BITS_PER_WORD;
6911 mode = word_mode;
6914 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6915 && DECL_NONADDRESSABLE_P (field))
6917 to_rtx = copy_rtx (to_rtx);
6918 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6921 store_constructor_field (to_rtx, bitsize, bitpos,
6922 0, bitregion_end, mode,
6923 value, cleared,
6924 get_alias_set (TREE_TYPE (field)),
6925 reverse);
6927 break;
6929 case ARRAY_TYPE:
6931 tree value, index;
6932 unsigned HOST_WIDE_INT i;
6933 int need_to_clear;
6934 tree domain;
6935 tree elttype = TREE_TYPE (type);
6936 int const_bounds_p;
6937 HOST_WIDE_INT minelt = 0;
6938 HOST_WIDE_INT maxelt = 0;
6940 /* The storage order is specified for every aggregate type. */
6941 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6943 domain = TYPE_DOMAIN (type);
6944 const_bounds_p = (TYPE_MIN_VALUE (domain)
6945 && TYPE_MAX_VALUE (domain)
6946 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6947 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6949 /* If we have constant bounds for the range of the type, get them. */
6950 if (const_bounds_p)
6952 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6953 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6956 /* If the constructor has fewer elements than the array, clear
6957 the whole array first. Similarly if this is static
6958 constructor of a non-BLKmode object. */
6959 if (cleared)
6960 need_to_clear = 0;
6961 else if (REG_P (target) && TREE_STATIC (exp))
6962 need_to_clear = 1;
6963 else
6965 unsigned HOST_WIDE_INT idx;
6966 HOST_WIDE_INT count = 0, zero_count = 0;
6967 need_to_clear = ! const_bounds_p;
6969 /* This loop is a more accurate version of the loop in
6970 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6971 is also needed to check for missing elements. */
6972 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6974 HOST_WIDE_INT this_node_count;
6976 if (need_to_clear)
6977 break;
6979 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6981 tree lo_index = TREE_OPERAND (index, 0);
6982 tree hi_index = TREE_OPERAND (index, 1);
6984 if (! tree_fits_uhwi_p (lo_index)
6985 || ! tree_fits_uhwi_p (hi_index))
6987 need_to_clear = 1;
6988 break;
6991 this_node_count = (tree_to_uhwi (hi_index)
6992 - tree_to_uhwi (lo_index) + 1);
6994 else
6995 this_node_count = 1;
6997 count += this_node_count;
6998 if (mostly_zeros_p (value))
6999 zero_count += this_node_count;
7002 /* Clear the entire array first if there are any missing
7003 elements, or if the incidence of zero elements is >=
7004 75%. */
7005 if (! need_to_clear
7006 && (count < maxelt - minelt + 1
7007 || 4 * zero_count >= 3 * count))
7008 need_to_clear = 1;
7011 if (need_to_clear && maybe_gt (size, 0))
7013 if (REG_P (target))
7014 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7015 else
7016 clear_storage (target, gen_int_mode (size, Pmode),
7017 BLOCK_OP_NORMAL);
7018 cleared = 1;
7021 if (!cleared && REG_P (target))
7022 /* Inform later passes that the old value is dead. */
7023 emit_clobber (target);
7025 /* Store each element of the constructor into the
7026 corresponding element of TARGET, determined by counting the
7027 elements. */
7028 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7030 machine_mode mode;
7031 poly_int64 bitsize;
7032 HOST_WIDE_INT bitpos;
7033 rtx xtarget = target;
7035 if (cleared && initializer_zerop (value))
7036 continue;
7038 mode = TYPE_MODE (elttype);
7039 if (mode != BLKmode)
7040 bitsize = GET_MODE_BITSIZE (mode);
7041 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7042 bitsize = -1;
7044 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7046 tree lo_index = TREE_OPERAND (index, 0);
7047 tree hi_index = TREE_OPERAND (index, 1);
7048 rtx index_r, pos_rtx;
7049 HOST_WIDE_INT lo, hi, count;
7050 tree position;
7052 /* If the range is constant and "small", unroll the loop. */
7053 if (const_bounds_p
7054 && tree_fits_shwi_p (lo_index)
7055 && tree_fits_shwi_p (hi_index)
7056 && (lo = tree_to_shwi (lo_index),
7057 hi = tree_to_shwi (hi_index),
7058 count = hi - lo + 1,
7059 (!MEM_P (target)
7060 || count <= 2
7061 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7062 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7063 <= 40 * 8)))))
7065 lo -= minelt; hi -= minelt;
7066 for (; lo <= hi; lo++)
7068 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7070 if (MEM_P (target)
7071 && !MEM_KEEP_ALIAS_SET_P (target)
7072 && TREE_CODE (type) == ARRAY_TYPE
7073 && TYPE_NONALIASED_COMPONENT (type))
7075 target = copy_rtx (target);
7076 MEM_KEEP_ALIAS_SET_P (target) = 1;
7079 store_constructor_field
7080 (target, bitsize, bitpos, 0, bitregion_end,
7081 mode, value, cleared,
7082 get_alias_set (elttype), reverse);
7085 else
7087 rtx_code_label *loop_start = gen_label_rtx ();
7088 rtx_code_label *loop_end = gen_label_rtx ();
7089 tree exit_cond;
7091 expand_normal (hi_index);
7093 index = build_decl (EXPR_LOCATION (exp),
7094 VAR_DECL, NULL_TREE, domain);
7095 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7096 SET_DECL_RTL (index, index_r);
7097 store_expr (lo_index, index_r, 0, false, reverse);
7099 /* Build the head of the loop. */
7100 do_pending_stack_adjust ();
7101 emit_label (loop_start);
7103 /* Assign value to element index. */
7104 position =
7105 fold_convert (ssizetype,
7106 fold_build2 (MINUS_EXPR,
7107 TREE_TYPE (index),
7108 index,
7109 TYPE_MIN_VALUE (domain)));
7111 position =
7112 size_binop (MULT_EXPR, position,
7113 fold_convert (ssizetype,
7114 TYPE_SIZE_UNIT (elttype)));
7116 pos_rtx = expand_normal (position);
7117 xtarget = offset_address (target, pos_rtx,
7118 highest_pow2_factor (position));
7119 xtarget = adjust_address (xtarget, mode, 0);
7120 if (TREE_CODE (value) == CONSTRUCTOR)
7121 store_constructor (value, xtarget, cleared,
7122 exact_div (bitsize, BITS_PER_UNIT),
7123 reverse);
7124 else
7125 store_expr (value, xtarget, 0, false, reverse);
7127 /* Generate a conditional jump to exit the loop. */
7128 exit_cond = build2 (LT_EXPR, integer_type_node,
7129 index, hi_index);
7130 jumpif (exit_cond, loop_end,
7131 profile_probability::uninitialized ());
7133 /* Update the loop counter, and jump to the head of
7134 the loop. */
7135 expand_assignment (index,
7136 build2 (PLUS_EXPR, TREE_TYPE (index),
7137 index, integer_one_node),
7138 false);
7140 emit_jump (loop_start);
7142 /* Build the end of the loop. */
7143 emit_label (loop_end);
7146 else if ((index != 0 && ! tree_fits_shwi_p (index))
7147 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7149 tree position;
7151 if (index == 0)
7152 index = ssize_int (1);
7154 if (minelt)
7155 index = fold_convert (ssizetype,
7156 fold_build2 (MINUS_EXPR,
7157 TREE_TYPE (index),
7158 index,
7159 TYPE_MIN_VALUE (domain)));
7161 position =
7162 size_binop (MULT_EXPR, index,
7163 fold_convert (ssizetype,
7164 TYPE_SIZE_UNIT (elttype)));
7165 xtarget = offset_address (target,
7166 expand_normal (position),
7167 highest_pow2_factor (position));
7168 xtarget = adjust_address (xtarget, mode, 0);
7169 store_expr (value, xtarget, 0, false, reverse);
7171 else
7173 if (index != 0)
7174 bitpos = ((tree_to_shwi (index) - minelt)
7175 * tree_to_uhwi (TYPE_SIZE (elttype)));
7176 else
7177 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7179 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7180 && TREE_CODE (type) == ARRAY_TYPE
7181 && TYPE_NONALIASED_COMPONENT (type))
7183 target = copy_rtx (target);
7184 MEM_KEEP_ALIAS_SET_P (target) = 1;
7186 store_constructor_field (target, bitsize, bitpos, 0,
7187 bitregion_end, mode, value,
7188 cleared, get_alias_set (elttype),
7189 reverse);
7192 break;
7195 case VECTOR_TYPE:
7197 unsigned HOST_WIDE_INT idx;
7198 constructor_elt *ce;
7199 int i;
7200 int need_to_clear;
7201 insn_code icode = CODE_FOR_nothing;
7202 tree elt;
7203 tree elttype = TREE_TYPE (type);
7204 int elt_size = vector_element_bits (type);
7205 machine_mode eltmode = TYPE_MODE (elttype);
7206 HOST_WIDE_INT bitsize;
7207 HOST_WIDE_INT bitpos;
7208 rtvec vector = NULL;
7209 poly_uint64 n_elts;
7210 unsigned HOST_WIDE_INT const_n_elts;
7211 alias_set_type alias;
7212 bool vec_vec_init_p = false;
7213 machine_mode mode = GET_MODE (target);
7215 gcc_assert (eltmode != BLKmode);
7217 /* Try using vec_duplicate_optab for uniform vectors. */
7218 if (!TREE_SIDE_EFFECTS (exp)
7219 && VECTOR_MODE_P (mode)
7220 && eltmode == GET_MODE_INNER (mode)
7221 && ((icode = optab_handler (vec_duplicate_optab, mode))
7222 != CODE_FOR_nothing)
7223 && (elt = uniform_vector_p (exp))
7224 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7226 class expand_operand ops[2];
7227 create_output_operand (&ops[0], target, mode);
7228 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7229 expand_insn (icode, 2, ops);
7230 if (!rtx_equal_p (target, ops[0].value))
7231 emit_move_insn (target, ops[0].value);
7232 break;
7235 n_elts = TYPE_VECTOR_SUBPARTS (type);
7236 if (REG_P (target)
7237 && VECTOR_MODE_P (mode)
7238 && n_elts.is_constant (&const_n_elts))
7240 machine_mode emode = eltmode;
7241 bool vector_typed_elts_p = false;
7243 if (CONSTRUCTOR_NELTS (exp)
7244 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7245 == VECTOR_TYPE))
7247 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7248 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7249 * TYPE_VECTOR_SUBPARTS (etype),
7250 n_elts));
7251 emode = TYPE_MODE (etype);
7252 vector_typed_elts_p = true;
7254 icode = convert_optab_handler (vec_init_optab, mode, emode);
7255 if (icode != CODE_FOR_nothing)
7257 unsigned int n = const_n_elts;
7259 if (vector_typed_elts_p)
7261 n = CONSTRUCTOR_NELTS (exp);
7262 vec_vec_init_p = true;
7264 vector = rtvec_alloc (n);
7265 for (unsigned int k = 0; k < n; k++)
7266 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7270 /* Compute the size of the elements in the CTOR. It differs
7271 from the size of the vector type elements only when the
7272 CTOR elements are vectors themselves. */
7273 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7274 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7275 : elttype);
7276 if (VECTOR_TYPE_P (val_type))
7277 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7278 else
7279 bitsize = elt_size;
7281 /* If the constructor has fewer elements than the vector,
7282 clear the whole array first. Similarly if this is static
7283 constructor of a non-BLKmode object. */
7284 if (cleared)
7285 need_to_clear = 0;
7286 else if (REG_P (target) && TREE_STATIC (exp))
7287 need_to_clear = 1;
7288 else
7290 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7291 tree value;
7293 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7295 int n_elts_here = bitsize / elt_size;
7296 count += n_elts_here;
7297 if (mostly_zeros_p (value))
7298 zero_count += n_elts_here;
7301 /* Clear the entire vector first if there are any missing elements,
7302 or if the incidence of zero elements is >= 75%. */
7303 need_to_clear = (maybe_lt (count, n_elts)
7304 || 4 * zero_count >= 3 * count);
7307 if (need_to_clear && maybe_gt (size, 0) && !vector)
7309 if (REG_P (target))
7310 emit_move_insn (target, CONST0_RTX (mode));
7311 else
7312 clear_storage (target, gen_int_mode (size, Pmode),
7313 BLOCK_OP_NORMAL);
7314 cleared = 1;
7317 /* Inform later passes that the old value is dead. */
7318 if (!cleared && !vector && REG_P (target))
7319 emit_move_insn (target, CONST0_RTX (mode));
7321 if (MEM_P (target))
7322 alias = MEM_ALIAS_SET (target);
7323 else
7324 alias = get_alias_set (elttype);
7326 /* Store each element of the constructor into the corresponding
7327 element of TARGET, determined by counting the elements. */
7328 for (idx = 0, i = 0;
7329 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7330 idx++, i += bitsize / elt_size)
7332 HOST_WIDE_INT eltpos;
7333 tree value = ce->value;
7335 if (cleared && initializer_zerop (value))
7336 continue;
7338 if (ce->index)
7339 eltpos = tree_to_uhwi (ce->index);
7340 else
7341 eltpos = i;
7343 if (vector)
7345 if (vec_vec_init_p)
7347 gcc_assert (ce->index == NULL_TREE);
7348 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7349 eltpos = idx;
7351 else
7352 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7353 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7355 else
7357 machine_mode value_mode
7358 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7359 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7360 bitpos = eltpos * elt_size;
7361 store_constructor_field (target, bitsize, bitpos, 0,
7362 bitregion_end, value_mode,
7363 value, cleared, alias, reverse);
7367 if (vector)
7368 emit_insn (GEN_FCN (icode) (target,
7369 gen_rtx_PARALLEL (mode, vector)));
7370 break;
7373 default:
7374 gcc_unreachable ();
7378 /* Store the value of EXP (an expression tree)
7379 into a subfield of TARGET which has mode MODE and occupies
7380 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7381 If MODE is VOIDmode, it means that we are storing into a bit-field.
7383 BITREGION_START is bitpos of the first bitfield in this region.
7384 BITREGION_END is the bitpos of the ending bitfield in this region.
7385 These two fields are 0, if the C++ memory model does not apply,
7386 or we are not interested in keeping track of bitfield regions.
7388 Always return const0_rtx unless we have something particular to
7389 return.
7391 ALIAS_SET is the alias set for the destination. This value will
7392 (in general) be different from that for TARGET, since TARGET is a
7393 reference to the containing structure.
7395 If NONTEMPORAL is true, try generating a nontemporal store.
7397 If REVERSE is true, the store is to be done in reverse order. */
7399 static rtx
7400 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7401 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7402 machine_mode mode, tree exp,
7403 alias_set_type alias_set, bool nontemporal, bool reverse)
7405 if (TREE_CODE (exp) == ERROR_MARK)
7406 return const0_rtx;
7408 /* If we have nothing to store, do nothing unless the expression has
7409 side-effects. Don't do that for zero sized addressable lhs of
7410 calls. */
7411 if (known_eq (bitsize, 0)
7412 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7413 || TREE_CODE (exp) != CALL_EXPR))
7414 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7416 if (GET_CODE (target) == CONCAT)
7418 /* We're storing into a struct containing a single __complex. */
7420 gcc_assert (known_eq (bitpos, 0));
7421 return store_expr (exp, target, 0, nontemporal, reverse);
7424 /* If the structure is in a register or if the component
7425 is a bit field, we cannot use addressing to access it.
7426 Use bit-field techniques or SUBREG to store in it. */
7428 poly_int64 decl_bitsize;
7429 if (mode == VOIDmode
7430 || (mode != BLKmode && ! direct_store[(int) mode]
7431 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7432 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7433 || REG_P (target)
7434 || GET_CODE (target) == SUBREG
7435 /* If the field isn't aligned enough to store as an ordinary memref,
7436 store it as a bit field. */
7437 || (mode != BLKmode
7438 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7439 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7440 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7441 || !multiple_p (bitpos, BITS_PER_UNIT)))
7442 || (known_size_p (bitsize)
7443 && mode != BLKmode
7444 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7445 /* If the RHS and field are a constant size and the size of the
7446 RHS isn't the same size as the bitfield, we must use bitfield
7447 operations. */
7448 || (known_size_p (bitsize)
7449 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7450 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7451 bitsize)
7452 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7453 we will handle specially below. */
7454 && !(TREE_CODE (exp) == CONSTRUCTOR
7455 && multiple_p (bitsize, BITS_PER_UNIT))
7456 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7457 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7458 includes some extra padding. store_expr / expand_expr will in
7459 that case call get_inner_reference that will have the bitsize
7460 we check here and thus the block move will not clobber the
7461 padding that shouldn't be clobbered. In the future we could
7462 replace the TREE_ADDRESSABLE check with a check that
7463 get_base_address needs to live in memory. */
7464 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7465 || TREE_CODE (exp) != COMPONENT_REF
7466 || !multiple_p (bitsize, BITS_PER_UNIT)
7467 || !multiple_p (bitpos, BITS_PER_UNIT)
7468 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7469 &decl_bitsize)
7470 || maybe_ne (decl_bitsize, bitsize))
7471 /* A call with an addressable return type and return-slot
7472 optimization must not need bitfield operations but we must
7473 pass down the original target. */
7474 && (TREE_CODE (exp) != CALL_EXPR
7475 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7476 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7477 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7478 decl we must use bitfield operations. */
7479 || (known_size_p (bitsize)
7480 && TREE_CODE (exp) == MEM_REF
7481 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7482 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7483 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7484 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7486 rtx temp;
7487 gimple *nop_def;
7489 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7490 implies a mask operation. If the precision is the same size as
7491 the field we're storing into, that mask is redundant. This is
7492 particularly common with bit field assignments generated by the
7493 C front end. */
7494 nop_def = get_def_for_expr (exp, NOP_EXPR);
7495 if (nop_def)
7497 tree type = TREE_TYPE (exp);
7498 if (INTEGRAL_TYPE_P (type)
7499 && maybe_ne (TYPE_PRECISION (type),
7500 GET_MODE_BITSIZE (TYPE_MODE (type)))
7501 && known_eq (bitsize, TYPE_PRECISION (type)))
7503 tree op = gimple_assign_rhs1 (nop_def);
7504 type = TREE_TYPE (op);
7505 if (INTEGRAL_TYPE_P (type)
7506 && known_ge (TYPE_PRECISION (type), bitsize))
7507 exp = op;
7511 temp = expand_normal (exp);
7513 /* We don't support variable-sized BLKmode bitfields, since our
7514 handling of BLKmode is bound up with the ability to break
7515 things into words. */
7516 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7518 /* Handle calls that return values in multiple non-contiguous locations.
7519 The Irix 6 ABI has examples of this. */
7520 if (GET_CODE (temp) == PARALLEL)
7522 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7523 machine_mode temp_mode = GET_MODE (temp);
7524 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7525 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7526 rtx temp_target = gen_reg_rtx (temp_mode);
7527 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7528 temp = temp_target;
7531 /* Handle calls that return BLKmode values in registers. */
7532 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7534 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7535 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7536 temp = temp_target;
7539 /* If the value has aggregate type and an integral mode then, if BITSIZE
7540 is narrower than this mode and this is for big-endian data, we first
7541 need to put the value into the low-order bits for store_bit_field,
7542 except when MODE is BLKmode and BITSIZE larger than the word size
7543 (see the handling of fields larger than a word in store_bit_field).
7544 Moreover, the field may be not aligned on a byte boundary; in this
7545 case, if it has reverse storage order, it needs to be accessed as a
7546 scalar field with reverse storage order and we must first put the
7547 value into target order. */
7548 scalar_int_mode temp_mode;
7549 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7550 && is_int_mode (GET_MODE (temp), &temp_mode))
7552 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7554 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7556 if (reverse)
7557 temp = flip_storage_order (temp_mode, temp);
7559 gcc_checking_assert (known_le (bitsize, size));
7560 if (maybe_lt (bitsize, size)
7561 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7562 /* Use of to_constant for BLKmode was checked above. */
7563 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7564 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7565 size - bitsize, NULL_RTX, 1);
7568 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7569 if (mode != VOIDmode && mode != BLKmode
7570 && mode != TYPE_MODE (TREE_TYPE (exp)))
7571 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7573 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7574 and BITPOS must be aligned on a byte boundary. If so, we simply do
7575 a block copy. Likewise for a BLKmode-like TARGET. */
7576 if (GET_MODE (temp) == BLKmode
7577 && (GET_MODE (target) == BLKmode
7578 || (MEM_P (target)
7579 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7580 && multiple_p (bitpos, BITS_PER_UNIT)
7581 && multiple_p (bitsize, BITS_PER_UNIT))))
7583 gcc_assert (MEM_P (target) && MEM_P (temp));
7584 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7585 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7587 target = adjust_address (target, VOIDmode, bytepos);
7588 emit_block_move (target, temp,
7589 gen_int_mode (bytesize, Pmode),
7590 BLOCK_OP_NORMAL);
7592 return const0_rtx;
7595 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7596 word size, we need to load the value (see again store_bit_field). */
7597 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7599 temp_mode = smallest_int_mode_for_size (bitsize);
7600 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7601 temp_mode, false, NULL);
7604 /* Store the value in the bitfield. */
7605 gcc_checking_assert (known_ge (bitpos, 0));
7606 store_bit_field (target, bitsize, bitpos,
7607 bitregion_start, bitregion_end,
7608 mode, temp, reverse);
7610 return const0_rtx;
7612 else
7614 /* Now build a reference to just the desired component. */
7615 rtx to_rtx = adjust_address (target, mode,
7616 exact_div (bitpos, BITS_PER_UNIT));
7618 if (to_rtx == target)
7619 to_rtx = copy_rtx (to_rtx);
7621 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7622 set_mem_alias_set (to_rtx, alias_set);
7624 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7625 into a target smaller than its type; handle that case now. */
7626 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7628 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7629 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7630 return to_rtx;
7633 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7637 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7638 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7639 codes and find the ultimate containing object, which we return.
7641 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7642 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7643 storage order of the field.
7644 If the position of the field is variable, we store a tree
7645 giving the variable offset (in units) in *POFFSET.
7646 This offset is in addition to the bit position.
7647 If the position is not variable, we store 0 in *POFFSET.
7649 If any of the extraction expressions is volatile,
7650 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7652 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7653 Otherwise, it is a mode that can be used to access the field.
7655 If the field describes a variable-sized object, *PMODE is set to
7656 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7657 this case, but the address of the object can be found. */
7659 tree
7660 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7661 poly_int64_pod *pbitpos, tree *poffset,
7662 machine_mode *pmode, int *punsignedp,
7663 int *preversep, int *pvolatilep)
7665 tree size_tree = 0;
7666 machine_mode mode = VOIDmode;
7667 bool blkmode_bitfield = false;
7668 tree offset = size_zero_node;
7669 poly_offset_int bit_offset = 0;
7671 /* First get the mode, signedness, storage order and size. We do this from
7672 just the outermost expression. */
7673 *pbitsize = -1;
7674 if (TREE_CODE (exp) == COMPONENT_REF)
7676 tree field = TREE_OPERAND (exp, 1);
7677 size_tree = DECL_SIZE (field);
7678 if (flag_strict_volatile_bitfields > 0
7679 && TREE_THIS_VOLATILE (exp)
7680 && DECL_BIT_FIELD_TYPE (field)
7681 && DECL_MODE (field) != BLKmode)
7682 /* Volatile bitfields should be accessed in the mode of the
7683 field's type, not the mode computed based on the bit
7684 size. */
7685 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7686 else if (!DECL_BIT_FIELD (field))
7688 mode = DECL_MODE (field);
7689 /* For vector fields re-check the target flags, as DECL_MODE
7690 could have been set with different target flags than
7691 the current function has. */
7692 if (mode == BLKmode
7693 && VECTOR_TYPE_P (TREE_TYPE (field))
7694 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7695 mode = TYPE_MODE (TREE_TYPE (field));
7697 else if (DECL_MODE (field) == BLKmode)
7698 blkmode_bitfield = true;
7700 *punsignedp = DECL_UNSIGNED (field);
7702 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7704 size_tree = TREE_OPERAND (exp, 1);
7705 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7706 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7708 /* For vector element types with the correct size of access or for
7709 vector typed accesses use the mode of the access type. */
7710 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7711 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7712 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7713 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7714 mode = TYPE_MODE (TREE_TYPE (exp));
7716 else
7718 mode = TYPE_MODE (TREE_TYPE (exp));
7719 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7721 if (mode == BLKmode)
7722 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7723 else
7724 *pbitsize = GET_MODE_BITSIZE (mode);
7727 if (size_tree != 0)
7729 if (! tree_fits_uhwi_p (size_tree))
7730 mode = BLKmode, *pbitsize = -1;
7731 else
7732 *pbitsize = tree_to_uhwi (size_tree);
7735 *preversep = reverse_storage_order_for_component_p (exp);
7737 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7738 and find the ultimate containing object. */
7739 while (1)
7741 switch (TREE_CODE (exp))
7743 case BIT_FIELD_REF:
7744 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7745 break;
7747 case COMPONENT_REF:
7749 tree field = TREE_OPERAND (exp, 1);
7750 tree this_offset = component_ref_field_offset (exp);
7752 /* If this field hasn't been filled in yet, don't go past it.
7753 This should only happen when folding expressions made during
7754 type construction. */
7755 if (this_offset == 0)
7756 break;
7758 offset = size_binop (PLUS_EXPR, offset, this_offset);
7759 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7761 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7763 break;
7765 case ARRAY_REF:
7766 case ARRAY_RANGE_REF:
7768 tree index = TREE_OPERAND (exp, 1);
7769 tree low_bound = array_ref_low_bound (exp);
7770 tree unit_size = array_ref_element_size (exp);
7772 /* We assume all arrays have sizes that are a multiple of a byte.
7773 First subtract the lower bound, if any, in the type of the
7774 index, then convert to sizetype and multiply by the size of
7775 the array element. */
7776 if (! integer_zerop (low_bound))
7777 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7778 index, low_bound);
7780 offset = size_binop (PLUS_EXPR, offset,
7781 size_binop (MULT_EXPR,
7782 fold_convert (sizetype, index),
7783 unit_size));
7785 break;
7787 case REALPART_EXPR:
7788 break;
7790 case IMAGPART_EXPR:
7791 bit_offset += *pbitsize;
7792 break;
7794 case VIEW_CONVERT_EXPR:
7795 break;
7797 case MEM_REF:
7798 /* Hand back the decl for MEM[&decl, off]. */
7799 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7801 tree off = TREE_OPERAND (exp, 1);
7802 if (!integer_zerop (off))
7804 poly_offset_int boff = mem_ref_offset (exp);
7805 boff <<= LOG2_BITS_PER_UNIT;
7806 bit_offset += boff;
7808 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7810 goto done;
7812 default:
7813 goto done;
7816 /* If any reference in the chain is volatile, the effect is volatile. */
7817 if (TREE_THIS_VOLATILE (exp))
7818 *pvolatilep = 1;
7820 exp = TREE_OPERAND (exp, 0);
7822 done:
7824 /* If OFFSET is constant, see if we can return the whole thing as a
7825 constant bit position. Make sure to handle overflow during
7826 this conversion. */
7827 if (poly_int_tree_p (offset))
7829 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7830 TYPE_PRECISION (sizetype));
7831 tem <<= LOG2_BITS_PER_UNIT;
7832 tem += bit_offset;
7833 if (tem.to_shwi (pbitpos))
7834 *poffset = offset = NULL_TREE;
7837 /* Otherwise, split it up. */
7838 if (offset)
7840 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7841 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7843 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7844 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7845 offset = size_binop (PLUS_EXPR, offset,
7846 build_int_cst (sizetype, bytes.force_shwi ()));
7849 *poffset = offset;
7852 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7853 if (mode == VOIDmode
7854 && blkmode_bitfield
7855 && multiple_p (*pbitpos, BITS_PER_UNIT)
7856 && multiple_p (*pbitsize, BITS_PER_UNIT))
7857 *pmode = BLKmode;
7858 else
7859 *pmode = mode;
7861 return exp;
7864 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7866 static unsigned HOST_WIDE_INT
7867 target_align (const_tree target)
7869 /* We might have a chain of nested references with intermediate misaligning
7870 bitfields components, so need to recurse to find out. */
7872 unsigned HOST_WIDE_INT this_align, outer_align;
7874 switch (TREE_CODE (target))
7876 case BIT_FIELD_REF:
7877 return 1;
7879 case COMPONENT_REF:
7880 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7881 outer_align = target_align (TREE_OPERAND (target, 0));
7882 return MIN (this_align, outer_align);
7884 case ARRAY_REF:
7885 case ARRAY_RANGE_REF:
7886 this_align = TYPE_ALIGN (TREE_TYPE (target));
7887 outer_align = target_align (TREE_OPERAND (target, 0));
7888 return MIN (this_align, outer_align);
7890 CASE_CONVERT:
7891 case NON_LVALUE_EXPR:
7892 case VIEW_CONVERT_EXPR:
7893 this_align = TYPE_ALIGN (TREE_TYPE (target));
7894 outer_align = target_align (TREE_OPERAND (target, 0));
7895 return MAX (this_align, outer_align);
7897 default:
7898 return TYPE_ALIGN (TREE_TYPE (target));
7903 /* Given an rtx VALUE that may contain additions and multiplications, return
7904 an equivalent value that just refers to a register, memory, or constant.
7905 This is done by generating instructions to perform the arithmetic and
7906 returning a pseudo-register containing the value.
7908 The returned value may be a REG, SUBREG, MEM or constant. */
7911 force_operand (rtx value, rtx target)
7913 rtx op1, op2;
7914 /* Use subtarget as the target for operand 0 of a binary operation. */
7915 rtx subtarget = get_subtarget (target);
7916 enum rtx_code code = GET_CODE (value);
7918 /* Check for subreg applied to an expression produced by loop optimizer. */
7919 if (code == SUBREG
7920 && !REG_P (SUBREG_REG (value))
7921 && !MEM_P (SUBREG_REG (value)))
7923 value
7924 = simplify_gen_subreg (GET_MODE (value),
7925 force_reg (GET_MODE (SUBREG_REG (value)),
7926 force_operand (SUBREG_REG (value),
7927 NULL_RTX)),
7928 GET_MODE (SUBREG_REG (value)),
7929 SUBREG_BYTE (value));
7930 code = GET_CODE (value);
7933 /* Check for a PIC address load. */
7934 if ((code == PLUS || code == MINUS)
7935 && XEXP (value, 0) == pic_offset_table_rtx
7936 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7937 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7938 || GET_CODE (XEXP (value, 1)) == CONST))
7940 if (!subtarget)
7941 subtarget = gen_reg_rtx (GET_MODE (value));
7942 emit_move_insn (subtarget, value);
7943 return subtarget;
7946 if (ARITHMETIC_P (value))
7948 op2 = XEXP (value, 1);
7949 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7950 subtarget = 0;
7951 if (code == MINUS && CONST_INT_P (op2))
7953 code = PLUS;
7954 op2 = negate_rtx (GET_MODE (value), op2);
7957 /* Check for an addition with OP2 a constant integer and our first
7958 operand a PLUS of a virtual register and something else. In that
7959 case, we want to emit the sum of the virtual register and the
7960 constant first and then add the other value. This allows virtual
7961 register instantiation to simply modify the constant rather than
7962 creating another one around this addition. */
7963 if (code == PLUS && CONST_INT_P (op2)
7964 && GET_CODE (XEXP (value, 0)) == PLUS
7965 && REG_P (XEXP (XEXP (value, 0), 0))
7966 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7967 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7969 rtx temp = expand_simple_binop (GET_MODE (value), code,
7970 XEXP (XEXP (value, 0), 0), op2,
7971 subtarget, 0, OPTAB_LIB_WIDEN);
7972 return expand_simple_binop (GET_MODE (value), code, temp,
7973 force_operand (XEXP (XEXP (value,
7974 0), 1), 0),
7975 target, 0, OPTAB_LIB_WIDEN);
7978 op1 = force_operand (XEXP (value, 0), subtarget);
7979 op2 = force_operand (op2, NULL_RTX);
7980 switch (code)
7982 case MULT:
7983 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7984 case DIV:
7985 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7986 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7987 target, 1, OPTAB_LIB_WIDEN);
7988 else
7989 return expand_divmod (0,
7990 FLOAT_MODE_P (GET_MODE (value))
7991 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7992 GET_MODE (value), op1, op2, target, 0);
7993 case MOD:
7994 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7995 target, 0);
7996 case UDIV:
7997 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7998 target, 1);
7999 case UMOD:
8000 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8001 target, 1);
8002 case ASHIFTRT:
8003 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8004 target, 0, OPTAB_LIB_WIDEN);
8005 default:
8006 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8007 target, 1, OPTAB_LIB_WIDEN);
8010 if (UNARY_P (value))
8012 if (!target)
8013 target = gen_reg_rtx (GET_MODE (value));
8014 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8015 switch (code)
8017 case ZERO_EXTEND:
8018 case SIGN_EXTEND:
8019 case TRUNCATE:
8020 case FLOAT_EXTEND:
8021 case FLOAT_TRUNCATE:
8022 convert_move (target, op1, code == ZERO_EXTEND);
8023 return target;
8025 case FIX:
8026 case UNSIGNED_FIX:
8027 expand_fix (target, op1, code == UNSIGNED_FIX);
8028 return target;
8030 case FLOAT:
8031 case UNSIGNED_FLOAT:
8032 expand_float (target, op1, code == UNSIGNED_FLOAT);
8033 return target;
8035 default:
8036 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8040 #ifdef INSN_SCHEDULING
8041 /* On machines that have insn scheduling, we want all memory reference to be
8042 explicit, so we need to deal with such paradoxical SUBREGs. */
8043 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8044 value
8045 = simplify_gen_subreg (GET_MODE (value),
8046 force_reg (GET_MODE (SUBREG_REG (value)),
8047 force_operand (SUBREG_REG (value),
8048 NULL_RTX)),
8049 GET_MODE (SUBREG_REG (value)),
8050 SUBREG_BYTE (value));
8051 #endif
8053 return value;
8056 /* Subroutine of expand_expr: return nonzero iff there is no way that
8057 EXP can reference X, which is being modified. TOP_P is nonzero if this
8058 call is going to be used to determine whether we need a temporary
8059 for EXP, as opposed to a recursive call to this function.
8061 It is always safe for this routine to return zero since it merely
8062 searches for optimization opportunities. */
8065 safe_from_p (const_rtx x, tree exp, int top_p)
8067 rtx exp_rtl = 0;
8068 int i, nops;
8070 if (x == 0
8071 /* If EXP has varying size, we MUST use a target since we currently
8072 have no way of allocating temporaries of variable size
8073 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8074 So we assume here that something at a higher level has prevented a
8075 clash. This is somewhat bogus, but the best we can do. Only
8076 do this when X is BLKmode and when we are at the top level. */
8077 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8078 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8079 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8080 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8081 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8082 != INTEGER_CST)
8083 && GET_MODE (x) == BLKmode)
8084 /* If X is in the outgoing argument area, it is always safe. */
8085 || (MEM_P (x)
8086 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8087 || (GET_CODE (XEXP (x, 0)) == PLUS
8088 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8089 return 1;
8091 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8092 find the underlying pseudo. */
8093 if (GET_CODE (x) == SUBREG)
8095 x = SUBREG_REG (x);
8096 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8097 return 0;
8100 /* Now look at our tree code and possibly recurse. */
8101 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8103 case tcc_declaration:
8104 exp_rtl = DECL_RTL_IF_SET (exp);
8105 break;
8107 case tcc_constant:
8108 return 1;
8110 case tcc_exceptional:
8111 if (TREE_CODE (exp) == TREE_LIST)
8113 while (1)
8115 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8116 return 0;
8117 exp = TREE_CHAIN (exp);
8118 if (!exp)
8119 return 1;
8120 if (TREE_CODE (exp) != TREE_LIST)
8121 return safe_from_p (x, exp, 0);
8124 else if (TREE_CODE (exp) == CONSTRUCTOR)
8126 constructor_elt *ce;
8127 unsigned HOST_WIDE_INT idx;
8129 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8130 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8131 || !safe_from_p (x, ce->value, 0))
8132 return 0;
8133 return 1;
8135 else if (TREE_CODE (exp) == ERROR_MARK)
8136 return 1; /* An already-visited SAVE_EXPR? */
8137 else
8138 return 0;
8140 case tcc_statement:
8141 /* The only case we look at here is the DECL_INITIAL inside a
8142 DECL_EXPR. */
8143 return (TREE_CODE (exp) != DECL_EXPR
8144 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8145 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8146 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8148 case tcc_binary:
8149 case tcc_comparison:
8150 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8151 return 0;
8152 /* Fall through. */
8154 case tcc_unary:
8155 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8157 case tcc_expression:
8158 case tcc_reference:
8159 case tcc_vl_exp:
8160 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8161 the expression. If it is set, we conflict iff we are that rtx or
8162 both are in memory. Otherwise, we check all operands of the
8163 expression recursively. */
8165 switch (TREE_CODE (exp))
8167 case ADDR_EXPR:
8168 /* If the operand is static or we are static, we can't conflict.
8169 Likewise if we don't conflict with the operand at all. */
8170 if (staticp (TREE_OPERAND (exp, 0))
8171 || TREE_STATIC (exp)
8172 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8173 return 1;
8175 /* Otherwise, the only way this can conflict is if we are taking
8176 the address of a DECL a that address if part of X, which is
8177 very rare. */
8178 exp = TREE_OPERAND (exp, 0);
8179 if (DECL_P (exp))
8181 if (!DECL_RTL_SET_P (exp)
8182 || !MEM_P (DECL_RTL (exp)))
8183 return 0;
8184 else
8185 exp_rtl = XEXP (DECL_RTL (exp), 0);
8187 break;
8189 case MEM_REF:
8190 if (MEM_P (x)
8191 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8192 get_alias_set (exp)))
8193 return 0;
8194 break;
8196 case CALL_EXPR:
8197 /* Assume that the call will clobber all hard registers and
8198 all of memory. */
8199 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8200 || MEM_P (x))
8201 return 0;
8202 break;
8204 case WITH_CLEANUP_EXPR:
8205 case CLEANUP_POINT_EXPR:
8206 /* Lowered by gimplify.cc. */
8207 gcc_unreachable ();
8209 case SAVE_EXPR:
8210 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8212 default:
8213 break;
8216 /* If we have an rtx, we do not need to scan our operands. */
8217 if (exp_rtl)
8218 break;
8220 nops = TREE_OPERAND_LENGTH (exp);
8221 for (i = 0; i < nops; i++)
8222 if (TREE_OPERAND (exp, i) != 0
8223 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8224 return 0;
8226 break;
8228 case tcc_type:
8229 /* Should never get a type here. */
8230 gcc_unreachable ();
8233 /* If we have an rtl, find any enclosed object. Then see if we conflict
8234 with it. */
8235 if (exp_rtl)
8237 if (GET_CODE (exp_rtl) == SUBREG)
8239 exp_rtl = SUBREG_REG (exp_rtl);
8240 if (REG_P (exp_rtl)
8241 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8242 return 0;
8245 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8246 are memory and they conflict. */
8247 return ! (rtx_equal_p (x, exp_rtl)
8248 || (MEM_P (x) && MEM_P (exp_rtl)
8249 && true_dependence (exp_rtl, VOIDmode, x)));
8252 /* If we reach here, it is safe. */
8253 return 1;
8257 /* Return the highest power of two that EXP is known to be a multiple of.
8258 This is used in updating alignment of MEMs in array references. */
8260 unsigned HOST_WIDE_INT
8261 highest_pow2_factor (const_tree exp)
8263 unsigned HOST_WIDE_INT ret;
8264 int trailing_zeros = tree_ctz (exp);
8265 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8266 return BIGGEST_ALIGNMENT;
8267 ret = HOST_WIDE_INT_1U << trailing_zeros;
8268 if (ret > BIGGEST_ALIGNMENT)
8269 return BIGGEST_ALIGNMENT;
8270 return ret;
8273 /* Similar, except that the alignment requirements of TARGET are
8274 taken into account. Assume it is at least as aligned as its
8275 type, unless it is a COMPONENT_REF in which case the layout of
8276 the structure gives the alignment. */
8278 static unsigned HOST_WIDE_INT
8279 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8281 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8282 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8284 return MAX (factor, talign);
8287 /* Convert the tree comparison code TCODE to the rtl one where the
8288 signedness is UNSIGNEDP. */
8290 static enum rtx_code
8291 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8293 enum rtx_code code;
8294 switch (tcode)
8296 case EQ_EXPR:
8297 code = EQ;
8298 break;
8299 case NE_EXPR:
8300 code = NE;
8301 break;
8302 case LT_EXPR:
8303 code = unsignedp ? LTU : LT;
8304 break;
8305 case LE_EXPR:
8306 code = unsignedp ? LEU : LE;
8307 break;
8308 case GT_EXPR:
8309 code = unsignedp ? GTU : GT;
8310 break;
8311 case GE_EXPR:
8312 code = unsignedp ? GEU : GE;
8313 break;
8314 case UNORDERED_EXPR:
8315 code = UNORDERED;
8316 break;
8317 case ORDERED_EXPR:
8318 code = ORDERED;
8319 break;
8320 case UNLT_EXPR:
8321 code = UNLT;
8322 break;
8323 case UNLE_EXPR:
8324 code = UNLE;
8325 break;
8326 case UNGT_EXPR:
8327 code = UNGT;
8328 break;
8329 case UNGE_EXPR:
8330 code = UNGE;
8331 break;
8332 case UNEQ_EXPR:
8333 code = UNEQ;
8334 break;
8335 case LTGT_EXPR:
8336 code = LTGT;
8337 break;
8339 default:
8340 gcc_unreachable ();
8342 return code;
8345 /* Subroutine of expand_expr. Expand the two operands of a binary
8346 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8347 The value may be stored in TARGET if TARGET is nonzero. The
8348 MODIFIER argument is as documented by expand_expr. */
8350 void
8351 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8352 enum expand_modifier modifier)
8354 if (! safe_from_p (target, exp1, 1))
8355 target = 0;
8356 if (operand_equal_p (exp0, exp1, 0))
8358 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8359 *op1 = copy_rtx (*op0);
8361 else
8363 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8364 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8369 /* Return a MEM that contains constant EXP. DEFER is as for
8370 output_constant_def and MODIFIER is as for expand_expr. */
8372 static rtx
8373 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8375 rtx mem;
8377 mem = output_constant_def (exp, defer);
8378 if (modifier != EXPAND_INITIALIZER)
8379 mem = use_anchored_address (mem);
8380 return mem;
8383 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8384 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8386 static rtx
8387 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8388 enum expand_modifier modifier, addr_space_t as)
8390 rtx result, subtarget;
8391 tree inner, offset;
8392 poly_int64 bitsize, bitpos;
8393 int unsignedp, reversep, volatilep = 0;
8394 machine_mode mode1;
8396 /* If we are taking the address of a constant and are at the top level,
8397 we have to use output_constant_def since we can't call force_const_mem
8398 at top level. */
8399 /* ??? This should be considered a front-end bug. We should not be
8400 generating ADDR_EXPR of something that isn't an LVALUE. The only
8401 exception here is STRING_CST. */
8402 if (CONSTANT_CLASS_P (exp))
8404 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8405 if (modifier < EXPAND_SUM)
8406 result = force_operand (result, target);
8407 return result;
8410 /* Everything must be something allowed by is_gimple_addressable. */
8411 switch (TREE_CODE (exp))
8413 case INDIRECT_REF:
8414 /* This case will happen via recursion for &a->b. */
8415 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8417 case MEM_REF:
8419 tree tem = TREE_OPERAND (exp, 0);
8420 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8421 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8422 return expand_expr (tem, target, tmode, modifier);
8425 case TARGET_MEM_REF:
8426 return addr_for_mem_ref (exp, as, true);
8428 case CONST_DECL:
8429 /* Expand the initializer like constants above. */
8430 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8431 0, modifier), 0);
8432 if (modifier < EXPAND_SUM)
8433 result = force_operand (result, target);
8434 return result;
8436 case REALPART_EXPR:
8437 /* The real part of the complex number is always first, therefore
8438 the address is the same as the address of the parent object. */
8439 offset = 0;
8440 bitpos = 0;
8441 inner = TREE_OPERAND (exp, 0);
8442 break;
8444 case IMAGPART_EXPR:
8445 /* The imaginary part of the complex number is always second.
8446 The expression is therefore always offset by the size of the
8447 scalar type. */
8448 offset = 0;
8449 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8450 inner = TREE_OPERAND (exp, 0);
8451 break;
8453 case COMPOUND_LITERAL_EXPR:
8454 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8455 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8456 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8457 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8458 the initializers aren't gimplified. */
8459 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8460 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8461 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8462 target, tmode, modifier, as);
8463 /* FALLTHRU */
8464 default:
8465 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8466 expand_expr, as that can have various side effects; LABEL_DECLs for
8467 example, may not have their DECL_RTL set yet. Expand the rtl of
8468 CONSTRUCTORs too, which should yield a memory reference for the
8469 constructor's contents. Assume language specific tree nodes can
8470 be expanded in some interesting way. */
8471 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8472 if (DECL_P (exp)
8473 || TREE_CODE (exp) == CONSTRUCTOR
8474 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8476 result = expand_expr (exp, target, tmode,
8477 modifier == EXPAND_INITIALIZER
8478 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8480 /* If the DECL isn't in memory, then the DECL wasn't properly
8481 marked TREE_ADDRESSABLE, which will be either a front-end
8482 or a tree optimizer bug. */
8484 gcc_assert (MEM_P (result));
8485 result = XEXP (result, 0);
8487 /* ??? Is this needed anymore? */
8488 if (DECL_P (exp))
8489 TREE_USED (exp) = 1;
8491 if (modifier != EXPAND_INITIALIZER
8492 && modifier != EXPAND_CONST_ADDRESS
8493 && modifier != EXPAND_SUM)
8494 result = force_operand (result, target);
8495 return result;
8498 /* Pass FALSE as the last argument to get_inner_reference although
8499 we are expanding to RTL. The rationale is that we know how to
8500 handle "aligning nodes" here: we can just bypass them because
8501 they won't change the final object whose address will be returned
8502 (they actually exist only for that purpose). */
8503 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8504 &unsignedp, &reversep, &volatilep);
8505 break;
8508 /* We must have made progress. */
8509 gcc_assert (inner != exp);
8511 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8512 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8513 inner alignment, force the inner to be sufficiently aligned. */
8514 if (CONSTANT_CLASS_P (inner)
8515 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8517 inner = copy_node (inner);
8518 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8519 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8520 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8522 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8524 if (offset)
8526 rtx tmp;
8528 if (modifier != EXPAND_NORMAL)
8529 result = force_operand (result, NULL);
8530 tmp = expand_expr (offset, NULL_RTX, tmode,
8531 modifier == EXPAND_INITIALIZER
8532 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8534 /* expand_expr is allowed to return an object in a mode other
8535 than TMODE. If it did, we need to convert. */
8536 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8537 tmp = convert_modes (tmode, GET_MODE (tmp),
8538 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8539 result = convert_memory_address_addr_space (tmode, result, as);
8540 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8542 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8543 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8544 else
8546 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8547 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8548 1, OPTAB_LIB_WIDEN);
8552 if (maybe_ne (bitpos, 0))
8554 /* Someone beforehand should have rejected taking the address
8555 of an object that isn't byte-aligned. */
8556 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8557 result = convert_memory_address_addr_space (tmode, result, as);
8558 result = plus_constant (tmode, result, bytepos);
8559 if (modifier < EXPAND_SUM)
8560 result = force_operand (result, target);
8563 return result;
8566 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8567 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8569 static rtx
8570 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8571 enum expand_modifier modifier)
8573 addr_space_t as = ADDR_SPACE_GENERIC;
8574 scalar_int_mode address_mode = Pmode;
8575 scalar_int_mode pointer_mode = ptr_mode;
8576 machine_mode rmode;
8577 rtx result;
8579 /* Target mode of VOIDmode says "whatever's natural". */
8580 if (tmode == VOIDmode)
8581 tmode = TYPE_MODE (TREE_TYPE (exp));
8583 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8585 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8586 address_mode = targetm.addr_space.address_mode (as);
8587 pointer_mode = targetm.addr_space.pointer_mode (as);
8590 /* We can get called with some Weird Things if the user does silliness
8591 like "(short) &a". In that case, convert_memory_address won't do
8592 the right thing, so ignore the given target mode. */
8593 scalar_int_mode new_tmode = (tmode == pointer_mode
8594 ? pointer_mode
8595 : address_mode);
8597 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8598 new_tmode, modifier, as);
8600 /* Despite expand_expr claims concerning ignoring TMODE when not
8601 strictly convenient, stuff breaks if we don't honor it. Note
8602 that combined with the above, we only do this for pointer modes. */
8603 rmode = GET_MODE (result);
8604 if (rmode == VOIDmode)
8605 rmode = new_tmode;
8606 if (rmode != new_tmode)
8607 result = convert_memory_address_addr_space (new_tmode, result, as);
8609 return result;
8612 /* Generate code for computing CONSTRUCTOR EXP.
8613 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8614 is TRUE, instead of creating a temporary variable in memory
8615 NULL is returned and the caller needs to handle it differently. */
8617 static rtx
8618 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8619 bool avoid_temp_mem)
8621 tree type = TREE_TYPE (exp);
8622 machine_mode mode = TYPE_MODE (type);
8624 /* Try to avoid creating a temporary at all. This is possible
8625 if all of the initializer is zero.
8626 FIXME: try to handle all [0..255] initializers we can handle
8627 with memset. */
8628 if (TREE_STATIC (exp)
8629 && !TREE_ADDRESSABLE (exp)
8630 && target != 0 && mode == BLKmode
8631 && all_zeros_p (exp))
8633 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8634 return target;
8637 /* All elts simple constants => refer to a constant in memory. But
8638 if this is a non-BLKmode mode, let it store a field at a time
8639 since that should make a CONST_INT, CONST_WIDE_INT or
8640 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8641 use, it is best to store directly into the target unless the type
8642 is large enough that memcpy will be used. If we are making an
8643 initializer and all operands are constant, put it in memory as
8644 well.
8646 FIXME: Avoid trying to fill vector constructors piece-meal.
8647 Output them with output_constant_def below unless we're sure
8648 they're zeros. This should go away when vector initializers
8649 are treated like VECTOR_CST instead of arrays. */
8650 if ((TREE_STATIC (exp)
8651 && ((mode == BLKmode
8652 && ! (target != 0 && safe_from_p (target, exp, 1)))
8653 || TREE_ADDRESSABLE (exp)
8654 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8655 && (! can_move_by_pieces
8656 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8657 TYPE_ALIGN (type)))
8658 && ! mostly_zeros_p (exp))))
8659 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8660 && TREE_CONSTANT (exp)))
8662 rtx constructor;
8664 if (avoid_temp_mem)
8665 return NULL_RTX;
8667 constructor = expand_expr_constant (exp, 1, modifier);
8669 if (modifier != EXPAND_CONST_ADDRESS
8670 && modifier != EXPAND_INITIALIZER
8671 && modifier != EXPAND_SUM)
8672 constructor = validize_mem (constructor);
8674 return constructor;
8677 /* If the CTOR is available in static storage and not mostly
8678 zeros and we can move it by pieces prefer to do so since
8679 that's usually more efficient than performing a series of
8680 stores from immediates. */
8681 if (avoid_temp_mem
8682 && TREE_STATIC (exp)
8683 && TREE_CONSTANT (exp)
8684 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8685 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8686 TYPE_ALIGN (type))
8687 && ! mostly_zeros_p (exp))
8688 return NULL_RTX;
8690 /* Handle calls that pass values in multiple non-contiguous
8691 locations. The Irix 6 ABI has examples of this. */
8692 if (target == 0 || ! safe_from_p (target, exp, 1)
8693 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8694 /* Also make a temporary if the store is to volatile memory, to
8695 avoid individual accesses to aggregate members. */
8696 || (GET_CODE (target) == MEM
8697 && MEM_VOLATILE_P (target)
8698 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8700 if (avoid_temp_mem)
8701 return NULL_RTX;
8703 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8706 store_constructor (exp, target, 0, int_expr_size (exp), false);
8707 return target;
8711 /* expand_expr: generate code for computing expression EXP.
8712 An rtx for the computed value is returned. The value is never null.
8713 In the case of a void EXP, const0_rtx is returned.
8715 The value may be stored in TARGET if TARGET is nonzero.
8716 TARGET is just a suggestion; callers must assume that
8717 the rtx returned may not be the same as TARGET.
8719 If TARGET is CONST0_RTX, it means that the value will be ignored.
8721 If TMODE is not VOIDmode, it suggests generating the
8722 result in mode TMODE. But this is done only when convenient.
8723 Otherwise, TMODE is ignored and the value generated in its natural mode.
8724 TMODE is just a suggestion; callers must assume that
8725 the rtx returned may not have mode TMODE.
8727 Note that TARGET may have neither TMODE nor MODE. In that case, it
8728 probably will not be used.
8730 If MODIFIER is EXPAND_SUM then when EXP is an addition
8731 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8732 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8733 products as above, or REG or MEM, or constant.
8734 Ordinarily in such cases we would output mul or add instructions
8735 and then return a pseudo reg containing the sum.
8737 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8738 it also marks a label as absolutely required (it can't be dead).
8739 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8740 This is used for outputting expressions used in initializers.
8742 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8743 with a constant address even if that address is not normally legitimate.
8744 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8746 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8747 a call parameter. Such targets require special care as we haven't yet
8748 marked TARGET so that it's safe from being trashed by libcalls. We
8749 don't want to use TARGET for anything but the final result;
8750 Intermediate values must go elsewhere. Additionally, calls to
8751 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8753 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8754 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8755 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8756 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8757 recursively.
8758 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8759 then *ALT_RTL is set to TARGET (before legitimziation).
8761 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8762 In this case, we don't adjust a returned MEM rtx that wouldn't be
8763 sufficiently aligned for its mode; instead, it's up to the caller
8764 to deal with it afterwards. This is used to make sure that unaligned
8765 base objects for which out-of-bounds accesses are supported, for
8766 example record types with trailing arrays, aren't realigned behind
8767 the back of the caller.
8768 The normal operating mode is to pass FALSE for this parameter. */
8771 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8772 enum expand_modifier modifier, rtx *alt_rtl,
8773 bool inner_reference_p)
8775 rtx ret;
8777 /* Handle ERROR_MARK before anybody tries to access its type. */
8778 if (TREE_CODE (exp) == ERROR_MARK
8779 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8781 ret = CONST0_RTX (tmode);
8782 return ret ? ret : const0_rtx;
8785 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8786 inner_reference_p);
8787 return ret;
8790 /* Try to expand the conditional expression which is represented by
8791 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8792 return the rtl reg which represents the result. Otherwise return
8793 NULL_RTX. */
8795 static rtx
8796 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8797 tree treeop1 ATTRIBUTE_UNUSED,
8798 tree treeop2 ATTRIBUTE_UNUSED)
8800 rtx insn;
8801 rtx op00, op01, op1, op2;
8802 enum rtx_code comparison_code;
8803 machine_mode comparison_mode;
8804 gimple *srcstmt;
8805 rtx temp;
8806 tree type = TREE_TYPE (treeop1);
8807 int unsignedp = TYPE_UNSIGNED (type);
8808 machine_mode mode = TYPE_MODE (type);
8809 machine_mode orig_mode = mode;
8810 static bool expanding_cond_expr_using_cmove = false;
8812 /* Conditional move expansion can end up TERing two operands which,
8813 when recursively hitting conditional expressions can result in
8814 exponential behavior if the cmove expansion ultimatively fails.
8815 It's hardly profitable to TER a cmove into a cmove so avoid doing
8816 that by failing early if we end up recursing. */
8817 if (expanding_cond_expr_using_cmove)
8818 return NULL_RTX;
8820 /* If we cannot do a conditional move on the mode, try doing it
8821 with the promoted mode. */
8822 if (!can_conditionally_move_p (mode))
8824 mode = promote_mode (type, mode, &unsignedp);
8825 if (!can_conditionally_move_p (mode))
8826 return NULL_RTX;
8827 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8829 else
8830 temp = assign_temp (type, 0, 1);
8832 expanding_cond_expr_using_cmove = true;
8833 start_sequence ();
8834 expand_operands (treeop1, treeop2,
8835 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
8836 EXPAND_NORMAL);
8838 if (TREE_CODE (treeop0) == SSA_NAME
8839 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8841 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8842 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8843 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8844 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8845 comparison_mode = TYPE_MODE (type);
8846 unsignedp = TYPE_UNSIGNED (type);
8847 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8849 else if (COMPARISON_CLASS_P (treeop0))
8851 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8852 enum tree_code cmpcode = TREE_CODE (treeop0);
8853 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8854 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8855 unsignedp = TYPE_UNSIGNED (type);
8856 comparison_mode = TYPE_MODE (type);
8857 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8859 else
8861 op00 = expand_normal (treeop0);
8862 op01 = const0_rtx;
8863 comparison_code = NE;
8864 comparison_mode = GET_MODE (op00);
8865 if (comparison_mode == VOIDmode)
8866 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8868 expanding_cond_expr_using_cmove = false;
8870 if (GET_MODE (op1) != mode)
8871 op1 = gen_lowpart (mode, op1);
8873 if (GET_MODE (op2) != mode)
8874 op2 = gen_lowpart (mode, op2);
8876 /* Try to emit the conditional move. */
8877 insn = emit_conditional_move (temp,
8878 { comparison_code, op00, op01,
8879 comparison_mode },
8880 op1, op2, mode,
8881 unsignedp);
8883 /* If we could do the conditional move, emit the sequence,
8884 and return. */
8885 if (insn)
8887 rtx_insn *seq = get_insns ();
8888 end_sequence ();
8889 emit_insn (seq);
8890 return convert_modes (orig_mode, mode, temp, 0);
8893 /* Otherwise discard the sequence and fall back to code with
8894 branches. */
8895 end_sequence ();
8896 return NULL_RTX;
8899 /* A helper function for expand_expr_real_2 to be used with a
8900 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8901 is nonzero, with alignment ALIGN in bits.
8902 Store the value at TARGET if possible (if TARGET is nonzero).
8903 Regardless of TARGET, we return the rtx for where the value is placed.
8904 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8905 then *ALT_RTL is set to TARGET (before legitimziation). */
8907 static rtx
8908 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8909 unsigned int align, rtx target, rtx *alt_rtl)
8911 enum insn_code icode;
8913 if ((icode = optab_handler (movmisalign_optab, mode))
8914 != CODE_FOR_nothing)
8916 class expand_operand ops[2];
8918 /* We've already validated the memory, and we're creating a
8919 new pseudo destination. The predicates really can't fail,
8920 nor can the generator. */
8921 create_output_operand (&ops[0], NULL_RTX, mode);
8922 create_fixed_operand (&ops[1], temp);
8923 expand_insn (icode, 2, ops);
8924 temp = ops[0].value;
8926 else if (targetm.slow_unaligned_access (mode, align))
8927 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8928 0, unsignedp, target,
8929 mode, mode, false, alt_rtl);
8930 return temp;
8933 /* Helper function of expand_expr_2, expand a division or modulo.
8934 op0 and op1 should be already expanded treeop0 and treeop1, using
8935 expand_operands. */
8937 static rtx
8938 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8939 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8941 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8942 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8943 if (SCALAR_INT_MODE_P (mode)
8944 && optimize >= 2
8945 && get_range_pos_neg (treeop0) == 1
8946 && get_range_pos_neg (treeop1) == 1)
8948 /* If both arguments are known to be positive when interpreted
8949 as signed, we can expand it as both signed and unsigned
8950 division or modulo. Choose the cheaper sequence in that case. */
8951 bool speed_p = optimize_insn_for_speed_p ();
8952 do_pending_stack_adjust ();
8953 start_sequence ();
8954 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8955 rtx_insn *uns_insns = get_insns ();
8956 end_sequence ();
8957 start_sequence ();
8958 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8959 rtx_insn *sgn_insns = get_insns ();
8960 end_sequence ();
8961 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8962 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8964 /* If costs are the same then use as tie breaker the other other
8965 factor. */
8966 if (uns_cost == sgn_cost)
8968 uns_cost = seq_cost (uns_insns, !speed_p);
8969 sgn_cost = seq_cost (sgn_insns, !speed_p);
8972 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8974 emit_insn (uns_insns);
8975 return uns_ret;
8977 emit_insn (sgn_insns);
8978 return sgn_ret;
8980 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8984 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8985 enum expand_modifier modifier)
8987 rtx op0, op1, op2, temp;
8988 rtx_code_label *lab;
8989 tree type;
8990 int unsignedp;
8991 machine_mode mode;
8992 scalar_int_mode int_mode;
8993 enum tree_code code = ops->code;
8994 optab this_optab;
8995 rtx subtarget, original_target;
8996 int ignore;
8997 bool reduce_bit_field;
8998 location_t loc = ops->location;
8999 tree treeop0, treeop1, treeop2;
9000 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9001 ? reduce_to_bit_field_precision ((expr), \
9002 target, \
9003 type) \
9004 : (expr))
9006 type = ops->type;
9007 mode = TYPE_MODE (type);
9008 unsignedp = TYPE_UNSIGNED (type);
9010 treeop0 = ops->op0;
9011 treeop1 = ops->op1;
9012 treeop2 = ops->op2;
9014 /* We should be called only on simple (binary or unary) expressions,
9015 exactly those that are valid in gimple expressions that aren't
9016 GIMPLE_SINGLE_RHS (or invalid). */
9017 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9018 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9019 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9021 ignore = (target == const0_rtx
9022 || ((CONVERT_EXPR_CODE_P (code)
9023 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9024 && TREE_CODE (type) == VOID_TYPE));
9026 /* We should be called only if we need the result. */
9027 gcc_assert (!ignore);
9029 /* An operation in what may be a bit-field type needs the
9030 result to be reduced to the precision of the bit-field type,
9031 which is narrower than that of the type's mode. */
9032 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9033 && !type_has_mode_precision_p (type));
9035 if (reduce_bit_field
9036 && (modifier == EXPAND_STACK_PARM
9037 || (target && GET_MODE (target) != mode)))
9038 target = 0;
9040 /* Use subtarget as the target for operand 0 of a binary operation. */
9041 subtarget = get_subtarget (target);
9042 original_target = target;
9044 switch (code)
9046 case NON_LVALUE_EXPR:
9047 case PAREN_EXPR:
9048 CASE_CONVERT:
9049 if (treeop0 == error_mark_node)
9050 return const0_rtx;
9052 if (TREE_CODE (type) == UNION_TYPE)
9054 tree valtype = TREE_TYPE (treeop0);
9056 /* If both input and output are BLKmode, this conversion isn't doing
9057 anything except possibly changing memory attribute. */
9058 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9060 rtx result = expand_expr (treeop0, target, tmode,
9061 modifier);
9063 result = copy_rtx (result);
9064 set_mem_attributes (result, type, 0);
9065 return result;
9068 if (target == 0)
9070 if (TYPE_MODE (type) != BLKmode)
9071 target = gen_reg_rtx (TYPE_MODE (type));
9072 else
9073 target = assign_temp (type, 1, 1);
9076 if (MEM_P (target))
9077 /* Store data into beginning of memory target. */
9078 store_expr (treeop0,
9079 adjust_address (target, TYPE_MODE (valtype), 0),
9080 modifier == EXPAND_STACK_PARM,
9081 false, TYPE_REVERSE_STORAGE_ORDER (type));
9083 else
9085 gcc_assert (REG_P (target)
9086 && !TYPE_REVERSE_STORAGE_ORDER (type));
9088 /* Store this field into a union of the proper type. */
9089 poly_uint64 op0_size
9090 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9091 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9092 store_field (target,
9093 /* The conversion must be constructed so that
9094 we know at compile time how many bits
9095 to preserve. */
9096 ordered_min (op0_size, union_size),
9097 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9098 false, false);
9101 /* Return the entire union. */
9102 return target;
9105 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9107 op0 = expand_expr (treeop0, target, VOIDmode,
9108 modifier);
9110 /* If the signedness of the conversion differs and OP0 is
9111 a promoted SUBREG, clear that indication since we now
9112 have to do the proper extension. */
9113 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9114 && GET_CODE (op0) == SUBREG)
9115 SUBREG_PROMOTED_VAR_P (op0) = 0;
9117 return REDUCE_BIT_FIELD (op0);
9120 op0 = expand_expr (treeop0, NULL_RTX, mode,
9121 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9122 if (GET_MODE (op0) == mode)
9125 /* If OP0 is a constant, just convert it into the proper mode. */
9126 else if (CONSTANT_P (op0))
9128 tree inner_type = TREE_TYPE (treeop0);
9129 machine_mode inner_mode = GET_MODE (op0);
9131 if (inner_mode == VOIDmode)
9132 inner_mode = TYPE_MODE (inner_type);
9134 if (modifier == EXPAND_INITIALIZER)
9135 op0 = lowpart_subreg (mode, op0, inner_mode);
9136 else
9137 op0= convert_modes (mode, inner_mode, op0,
9138 TYPE_UNSIGNED (inner_type));
9141 else if (modifier == EXPAND_INITIALIZER)
9142 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9143 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9145 else if (target == 0)
9146 op0 = convert_to_mode (mode, op0,
9147 TYPE_UNSIGNED (TREE_TYPE
9148 (treeop0)));
9149 else
9151 convert_move (target, op0,
9152 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9153 op0 = target;
9156 return REDUCE_BIT_FIELD (op0);
9158 case ADDR_SPACE_CONVERT_EXPR:
9160 tree treeop0_type = TREE_TYPE (treeop0);
9162 gcc_assert (POINTER_TYPE_P (type));
9163 gcc_assert (POINTER_TYPE_P (treeop0_type));
9165 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9166 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9168 /* Conversions between pointers to the same address space should
9169 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9170 gcc_assert (as_to != as_from);
9172 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9174 /* Ask target code to handle conversion between pointers
9175 to overlapping address spaces. */
9176 if (targetm.addr_space.subset_p (as_to, as_from)
9177 || targetm.addr_space.subset_p (as_from, as_to))
9179 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9181 else
9183 /* For disjoint address spaces, converting anything but a null
9184 pointer invokes undefined behavior. We truncate or extend the
9185 value as if we'd converted via integers, which handles 0 as
9186 required, and all others as the programmer likely expects. */
9187 #ifndef POINTERS_EXTEND_UNSIGNED
9188 const int POINTERS_EXTEND_UNSIGNED = 1;
9189 #endif
9190 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9191 op0, POINTERS_EXTEND_UNSIGNED);
9193 gcc_assert (op0);
9194 return op0;
9197 case POINTER_PLUS_EXPR:
9198 /* Even though the sizetype mode and the pointer's mode can be different
9199 expand is able to handle this correctly and get the correct result out
9200 of the PLUS_EXPR code. */
9201 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9202 if sizetype precision is smaller than pointer precision. */
9203 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9204 treeop1 = fold_convert_loc (loc, type,
9205 fold_convert_loc (loc, ssizetype,
9206 treeop1));
9207 /* If sizetype precision is larger than pointer precision, truncate the
9208 offset to have matching modes. */
9209 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9210 treeop1 = fold_convert_loc (loc, type, treeop1);
9211 /* FALLTHRU */
9213 case PLUS_EXPR:
9214 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9215 something else, make sure we add the register to the constant and
9216 then to the other thing. This case can occur during strength
9217 reduction and doing it this way will produce better code if the
9218 frame pointer or argument pointer is eliminated.
9220 fold-const.cc will ensure that the constant is always in the inner
9221 PLUS_EXPR, so the only case we need to do anything about is if
9222 sp, ap, or fp is our second argument, in which case we must swap
9223 the innermost first argument and our second argument. */
9225 if (TREE_CODE (treeop0) == PLUS_EXPR
9226 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9227 && VAR_P (treeop1)
9228 && (DECL_RTL (treeop1) == frame_pointer_rtx
9229 || DECL_RTL (treeop1) == stack_pointer_rtx
9230 || DECL_RTL (treeop1) == arg_pointer_rtx))
9232 gcc_unreachable ();
9235 /* If the result is to be ptr_mode and we are adding an integer to
9236 something, we might be forming a constant. So try to use
9237 plus_constant. If it produces a sum and we can't accept it,
9238 use force_operand. This allows P = &ARR[const] to generate
9239 efficient code on machines where a SYMBOL_REF is not a valid
9240 address.
9242 If this is an EXPAND_SUM call, always return the sum. */
9243 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9244 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9246 if (modifier == EXPAND_STACK_PARM)
9247 target = 0;
9248 if (TREE_CODE (treeop0) == INTEGER_CST
9249 && HWI_COMPUTABLE_MODE_P (mode)
9250 && TREE_CONSTANT (treeop1))
9252 rtx constant_part;
9253 HOST_WIDE_INT wc;
9254 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9256 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9257 EXPAND_SUM);
9258 /* Use wi::shwi to ensure that the constant is
9259 truncated according to the mode of OP1, then sign extended
9260 to a HOST_WIDE_INT. Using the constant directly can result
9261 in non-canonical RTL in a 64x32 cross compile. */
9262 wc = TREE_INT_CST_LOW (treeop0);
9263 constant_part =
9264 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9265 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9266 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9267 op1 = force_operand (op1, target);
9268 return REDUCE_BIT_FIELD (op1);
9271 else if (TREE_CODE (treeop1) == INTEGER_CST
9272 && HWI_COMPUTABLE_MODE_P (mode)
9273 && TREE_CONSTANT (treeop0))
9275 rtx constant_part;
9276 HOST_WIDE_INT wc;
9277 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9279 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9280 (modifier == EXPAND_INITIALIZER
9281 ? EXPAND_INITIALIZER : EXPAND_SUM));
9282 if (! CONSTANT_P (op0))
9284 op1 = expand_expr (treeop1, NULL_RTX,
9285 VOIDmode, modifier);
9286 /* Return a PLUS if modifier says it's OK. */
9287 if (modifier == EXPAND_SUM
9288 || modifier == EXPAND_INITIALIZER)
9289 return simplify_gen_binary (PLUS, mode, op0, op1);
9290 goto binop2;
9292 /* Use wi::shwi to ensure that the constant is
9293 truncated according to the mode of OP1, then sign extended
9294 to a HOST_WIDE_INT. Using the constant directly can result
9295 in non-canonical RTL in a 64x32 cross compile. */
9296 wc = TREE_INT_CST_LOW (treeop1);
9297 constant_part
9298 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9299 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9300 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9301 op0 = force_operand (op0, target);
9302 return REDUCE_BIT_FIELD (op0);
9306 /* Use TER to expand pointer addition of a negated value
9307 as pointer subtraction. */
9308 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9309 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9310 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9311 && TREE_CODE (treeop1) == SSA_NAME
9312 && TYPE_MODE (TREE_TYPE (treeop0))
9313 == TYPE_MODE (TREE_TYPE (treeop1)))
9315 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9316 if (def)
9318 treeop1 = gimple_assign_rhs1 (def);
9319 code = MINUS_EXPR;
9320 goto do_minus;
9324 /* No sense saving up arithmetic to be done
9325 if it's all in the wrong mode to form part of an address.
9326 And force_operand won't know whether to sign-extend or
9327 zero-extend. */
9328 if (modifier != EXPAND_INITIALIZER
9329 && (modifier != EXPAND_SUM || mode != ptr_mode))
9331 expand_operands (treeop0, treeop1,
9332 subtarget, &op0, &op1, modifier);
9333 if (op0 == const0_rtx)
9334 return op1;
9335 if (op1 == const0_rtx)
9336 return op0;
9337 goto binop2;
9340 expand_operands (treeop0, treeop1,
9341 subtarget, &op0, &op1, modifier);
9342 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9344 case MINUS_EXPR:
9345 case POINTER_DIFF_EXPR:
9346 do_minus:
9347 /* For initializers, we are allowed to return a MINUS of two
9348 symbolic constants. Here we handle all cases when both operands
9349 are constant. */
9350 /* Handle difference of two symbolic constants,
9351 for the sake of an initializer. */
9352 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9353 && really_constant_p (treeop0)
9354 && really_constant_p (treeop1))
9356 expand_operands (treeop0, treeop1,
9357 NULL_RTX, &op0, &op1, modifier);
9358 return simplify_gen_binary (MINUS, mode, op0, op1);
9361 /* No sense saving up arithmetic to be done
9362 if it's all in the wrong mode to form part of an address.
9363 And force_operand won't know whether to sign-extend or
9364 zero-extend. */
9365 if (modifier != EXPAND_INITIALIZER
9366 && (modifier != EXPAND_SUM || mode != ptr_mode))
9367 goto binop;
9369 expand_operands (treeop0, treeop1,
9370 subtarget, &op0, &op1, modifier);
9372 /* Convert A - const to A + (-const). */
9373 if (CONST_INT_P (op1))
9375 op1 = negate_rtx (mode, op1);
9376 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9379 goto binop2;
9381 case WIDEN_MULT_PLUS_EXPR:
9382 case WIDEN_MULT_MINUS_EXPR:
9383 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9384 op2 = expand_normal (treeop2);
9385 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9386 target, unsignedp);
9387 return target;
9389 case WIDEN_PLUS_EXPR:
9390 case WIDEN_MINUS_EXPR:
9391 case WIDEN_MULT_EXPR:
9392 /* If first operand is constant, swap them.
9393 Thus the following special case checks need only
9394 check the second operand. */
9395 if (TREE_CODE (treeop0) == INTEGER_CST)
9396 std::swap (treeop0, treeop1);
9398 /* First, check if we have a multiplication of one signed and one
9399 unsigned operand. */
9400 if (TREE_CODE (treeop1) != INTEGER_CST
9401 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9402 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9404 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9405 this_optab = usmul_widen_optab;
9406 if (find_widening_optab_handler (this_optab, mode, innermode)
9407 != CODE_FOR_nothing)
9409 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9410 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9411 EXPAND_NORMAL);
9412 else
9413 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9414 EXPAND_NORMAL);
9415 /* op0 and op1 might still be constant, despite the above
9416 != INTEGER_CST check. Handle it. */
9417 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9419 op0 = convert_modes (mode, innermode, op0, true);
9420 op1 = convert_modes (mode, innermode, op1, false);
9421 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9422 target, unsignedp));
9424 goto binop3;
9427 /* Check for a multiplication with matching signedness. */
9428 else if ((TREE_CODE (treeop1) == INTEGER_CST
9429 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9430 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9431 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9433 tree op0type = TREE_TYPE (treeop0);
9434 machine_mode innermode = TYPE_MODE (op0type);
9435 bool zextend_p = TYPE_UNSIGNED (op0type);
9436 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9437 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9439 if (TREE_CODE (treeop0) != INTEGER_CST)
9441 if (find_widening_optab_handler (this_optab, mode, innermode)
9442 != CODE_FOR_nothing)
9444 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9445 EXPAND_NORMAL);
9446 /* op0 and op1 might still be constant, despite the above
9447 != INTEGER_CST check. Handle it. */
9448 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9450 widen_mult_const:
9451 op0 = convert_modes (mode, innermode, op0, zextend_p);
9453 = convert_modes (mode, innermode, op1,
9454 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9455 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9456 target,
9457 unsignedp));
9459 temp = expand_widening_mult (mode, op0, op1, target,
9460 unsignedp, this_optab);
9461 return REDUCE_BIT_FIELD (temp);
9463 if (find_widening_optab_handler (other_optab, mode, innermode)
9464 != CODE_FOR_nothing
9465 && innermode == word_mode)
9467 rtx htem, hipart;
9468 op0 = expand_normal (treeop0);
9469 op1 = expand_normal (treeop1);
9470 /* op0 and op1 might be constants, despite the above
9471 != INTEGER_CST check. Handle it. */
9472 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9473 goto widen_mult_const;
9474 temp = expand_binop (mode, other_optab, op0, op1, target,
9475 unsignedp, OPTAB_LIB_WIDEN);
9476 hipart = gen_highpart (word_mode, temp);
9477 htem = expand_mult_highpart_adjust (word_mode, hipart,
9478 op0, op1, hipart,
9479 zextend_p);
9480 if (htem != hipart)
9481 emit_move_insn (hipart, htem);
9482 return REDUCE_BIT_FIELD (temp);
9486 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9487 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9488 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9489 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9491 case MULT_EXPR:
9492 /* If this is a fixed-point operation, then we cannot use the code
9493 below because "expand_mult" doesn't support sat/no-sat fixed-point
9494 multiplications. */
9495 if (ALL_FIXED_POINT_MODE_P (mode))
9496 goto binop;
9498 /* If first operand is constant, swap them.
9499 Thus the following special case checks need only
9500 check the second operand. */
9501 if (TREE_CODE (treeop0) == INTEGER_CST)
9502 std::swap (treeop0, treeop1);
9504 /* Attempt to return something suitable for generating an
9505 indexed address, for machines that support that. */
9507 if (modifier == EXPAND_SUM && mode == ptr_mode
9508 && tree_fits_shwi_p (treeop1))
9510 tree exp1 = treeop1;
9512 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9513 EXPAND_SUM);
9515 if (!REG_P (op0))
9516 op0 = force_operand (op0, NULL_RTX);
9517 if (!REG_P (op0))
9518 op0 = copy_to_mode_reg (mode, op0);
9520 op1 = gen_int_mode (tree_to_shwi (exp1),
9521 TYPE_MODE (TREE_TYPE (exp1)));
9522 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9525 if (modifier == EXPAND_STACK_PARM)
9526 target = 0;
9528 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9530 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9531 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9532 if (def_stmt0
9533 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9534 def_stmt0 = NULL;
9535 if (def_stmt1
9536 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9537 def_stmt1 = NULL;
9539 if (def_stmt0 || def_stmt1)
9541 /* X / Y * Y can be expanded as X - X % Y too.
9542 Choose the cheaper sequence of those two. */
9543 if (def_stmt0)
9544 treeop0 = gimple_assign_rhs1 (def_stmt0);
9545 else
9547 treeop1 = treeop0;
9548 treeop0 = gimple_assign_rhs1 (def_stmt1);
9550 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9551 EXPAND_NORMAL);
9552 bool speed_p = optimize_insn_for_speed_p ();
9553 do_pending_stack_adjust ();
9554 start_sequence ();
9555 rtx divmul_ret
9556 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9557 op0, op1, NULL_RTX, unsignedp);
9558 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9559 unsignedp);
9560 rtx_insn *divmul_insns = get_insns ();
9561 end_sequence ();
9562 start_sequence ();
9563 rtx modsub_ret
9564 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9565 op0, op1, NULL_RTX, unsignedp);
9566 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9567 optab_default);
9568 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9569 target, unsignedp, OPTAB_LIB_WIDEN);
9570 rtx_insn *modsub_insns = get_insns ();
9571 end_sequence ();
9572 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9573 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9574 /* If costs are the same then use as tie breaker the other other
9575 factor. */
9576 if (divmul_cost == modsub_cost)
9578 divmul_cost = seq_cost (divmul_insns, !speed_p);
9579 modsub_cost = seq_cost (modsub_insns, !speed_p);
9582 if (divmul_cost <= modsub_cost)
9584 emit_insn (divmul_insns);
9585 return REDUCE_BIT_FIELD (divmul_ret);
9587 emit_insn (modsub_insns);
9588 return REDUCE_BIT_FIELD (modsub_ret);
9592 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9594 /* Expand X*Y as X&-Y when Y must be zero or one. */
9595 if (SCALAR_INT_MODE_P (mode))
9597 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9598 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9600 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9601 if (bit0_p && bit1_p)
9602 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9604 if (bit0_p || bit1_p)
9606 bool speed = optimize_insn_for_speed_p ();
9607 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9608 struct algorithm algorithm;
9609 enum mult_variant variant;
9610 if (CONST_INT_P (op1)
9611 ? !choose_mult_variant (mode, INTVAL (op1),
9612 &algorithm, &variant, cost)
9613 : cost < mul_cost (speed, mode))
9615 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9616 op1, target)
9617 : expand_and (mode, op0,
9618 negate_rtx (mode, op1),
9619 target);
9620 return REDUCE_BIT_FIELD (target);
9625 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9627 case TRUNC_MOD_EXPR:
9628 case FLOOR_MOD_EXPR:
9629 case CEIL_MOD_EXPR:
9630 case ROUND_MOD_EXPR:
9632 case TRUNC_DIV_EXPR:
9633 case FLOOR_DIV_EXPR:
9634 case CEIL_DIV_EXPR:
9635 case ROUND_DIV_EXPR:
9636 case EXACT_DIV_EXPR:
9637 /* If this is a fixed-point operation, then we cannot use the code
9638 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9639 divisions. */
9640 if (ALL_FIXED_POINT_MODE_P (mode))
9641 goto binop;
9643 if (modifier == EXPAND_STACK_PARM)
9644 target = 0;
9645 /* Possible optimization: compute the dividend with EXPAND_SUM
9646 then if the divisor is constant can optimize the case
9647 where some terms of the dividend have coeffs divisible by it. */
9648 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9649 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9650 target, unsignedp);
9652 case RDIV_EXPR:
9653 goto binop;
9655 case MULT_HIGHPART_EXPR:
9656 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9657 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9658 gcc_assert (temp);
9659 return temp;
9661 case FIXED_CONVERT_EXPR:
9662 op0 = expand_normal (treeop0);
9663 if (target == 0 || modifier == EXPAND_STACK_PARM)
9664 target = gen_reg_rtx (mode);
9666 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9667 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9668 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9669 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9670 else
9671 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9672 return target;
9674 case FIX_TRUNC_EXPR:
9675 op0 = expand_normal (treeop0);
9676 if (target == 0 || modifier == EXPAND_STACK_PARM)
9677 target = gen_reg_rtx (mode);
9678 expand_fix (target, op0, unsignedp);
9679 return target;
9681 case FLOAT_EXPR:
9682 op0 = expand_normal (treeop0);
9683 if (target == 0 || modifier == EXPAND_STACK_PARM)
9684 target = gen_reg_rtx (mode);
9685 /* expand_float can't figure out what to do if FROM has VOIDmode.
9686 So give it the correct mode. With -O, cse will optimize this. */
9687 if (GET_MODE (op0) == VOIDmode)
9688 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9689 op0);
9690 expand_float (target, op0,
9691 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9692 return target;
9694 case NEGATE_EXPR:
9695 op0 = expand_expr (treeop0, subtarget,
9696 VOIDmode, EXPAND_NORMAL);
9697 if (modifier == EXPAND_STACK_PARM)
9698 target = 0;
9699 temp = expand_unop (mode,
9700 optab_for_tree_code (NEGATE_EXPR, type,
9701 optab_default),
9702 op0, target, 0);
9703 gcc_assert (temp);
9704 return REDUCE_BIT_FIELD (temp);
9706 case ABS_EXPR:
9707 case ABSU_EXPR:
9708 op0 = expand_expr (treeop0, subtarget,
9709 VOIDmode, EXPAND_NORMAL);
9710 if (modifier == EXPAND_STACK_PARM)
9711 target = 0;
9713 /* ABS_EXPR is not valid for complex arguments. */
9714 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9715 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9717 /* Unsigned abs is simply the operand. Testing here means we don't
9718 risk generating incorrect code below. */
9719 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9720 return op0;
9722 return expand_abs (mode, op0, target, unsignedp,
9723 safe_from_p (target, treeop0, 1));
9725 case MAX_EXPR:
9726 case MIN_EXPR:
9727 target = original_target;
9728 if (target == 0
9729 || modifier == EXPAND_STACK_PARM
9730 || (MEM_P (target) && MEM_VOLATILE_P (target))
9731 || GET_MODE (target) != mode
9732 || (REG_P (target)
9733 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9734 target = gen_reg_rtx (mode);
9735 expand_operands (treeop0, treeop1,
9736 target, &op0, &op1, EXPAND_NORMAL);
9738 /* First try to do it with a special MIN or MAX instruction.
9739 If that does not win, use a conditional jump to select the proper
9740 value. */
9741 this_optab = optab_for_tree_code (code, type, optab_default);
9742 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9743 OPTAB_WIDEN);
9744 if (temp != 0)
9745 return temp;
9747 if (VECTOR_TYPE_P (type))
9748 gcc_unreachable ();
9750 /* At this point, a MEM target is no longer useful; we will get better
9751 code without it. */
9753 if (! REG_P (target))
9754 target = gen_reg_rtx (mode);
9756 /* If op1 was placed in target, swap op0 and op1. */
9757 if (target != op0 && target == op1)
9758 std::swap (op0, op1);
9760 /* We generate better code and avoid problems with op1 mentioning
9761 target by forcing op1 into a pseudo if it isn't a constant. */
9762 if (! CONSTANT_P (op1))
9763 op1 = force_reg (mode, op1);
9766 enum rtx_code comparison_code;
9767 rtx cmpop1 = op1;
9769 if (code == MAX_EXPR)
9770 comparison_code = unsignedp ? GEU : GE;
9771 else
9772 comparison_code = unsignedp ? LEU : LE;
9774 /* Canonicalize to comparisons against 0. */
9775 if (op1 == const1_rtx)
9777 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9778 or (a != 0 ? a : 1) for unsigned.
9779 For MIN we are safe converting (a <= 1 ? a : 1)
9780 into (a <= 0 ? a : 1) */
9781 cmpop1 = const0_rtx;
9782 if (code == MAX_EXPR)
9783 comparison_code = unsignedp ? NE : GT;
9785 if (op1 == constm1_rtx && !unsignedp)
9787 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9788 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9789 cmpop1 = const0_rtx;
9790 if (code == MIN_EXPR)
9791 comparison_code = LT;
9794 /* Use a conditional move if possible. */
9795 if (can_conditionally_move_p (mode))
9797 rtx insn;
9799 start_sequence ();
9801 /* Try to emit the conditional move. */
9802 insn = emit_conditional_move (target,
9803 { comparison_code,
9804 op0, cmpop1, mode },
9805 op0, op1, mode,
9806 unsignedp);
9808 /* If we could do the conditional move, emit the sequence,
9809 and return. */
9810 if (insn)
9812 rtx_insn *seq = get_insns ();
9813 end_sequence ();
9814 emit_insn (seq);
9815 return target;
9818 /* Otherwise discard the sequence and fall back to code with
9819 branches. */
9820 end_sequence ();
9823 if (target != op0)
9824 emit_move_insn (target, op0);
9826 lab = gen_label_rtx ();
9827 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9828 unsignedp, mode, NULL_RTX, NULL, lab,
9829 profile_probability::uninitialized ());
9831 emit_move_insn (target, op1);
9832 emit_label (lab);
9833 return target;
9835 case BIT_NOT_EXPR:
9836 op0 = expand_expr (treeop0, subtarget,
9837 VOIDmode, EXPAND_NORMAL);
9838 if (modifier == EXPAND_STACK_PARM)
9839 target = 0;
9840 /* In case we have to reduce the result to bitfield precision
9841 for unsigned bitfield expand this as XOR with a proper constant
9842 instead. */
9843 if (reduce_bit_field && TYPE_UNSIGNED (type))
9845 int_mode = SCALAR_INT_TYPE_MODE (type);
9846 wide_int mask = wi::mask (TYPE_PRECISION (type),
9847 false, GET_MODE_PRECISION (int_mode));
9849 temp = expand_binop (int_mode, xor_optab, op0,
9850 immed_wide_int_const (mask, int_mode),
9851 target, 1, OPTAB_LIB_WIDEN);
9853 else
9854 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9855 gcc_assert (temp);
9856 return temp;
9858 /* ??? Can optimize bitwise operations with one arg constant.
9859 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9860 and (a bitwise1 b) bitwise2 b (etc)
9861 but that is probably not worth while. */
9863 case BIT_AND_EXPR:
9864 case BIT_IOR_EXPR:
9865 case BIT_XOR_EXPR:
9866 goto binop;
9868 case LROTATE_EXPR:
9869 case RROTATE_EXPR:
9870 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9871 || type_has_mode_precision_p (type));
9872 /* fall through */
9874 case LSHIFT_EXPR:
9875 case RSHIFT_EXPR:
9877 /* If this is a fixed-point operation, then we cannot use the code
9878 below because "expand_shift" doesn't support sat/no-sat fixed-point
9879 shifts. */
9880 if (ALL_FIXED_POINT_MODE_P (mode))
9881 goto binop;
9883 if (! safe_from_p (subtarget, treeop1, 1))
9884 subtarget = 0;
9885 if (modifier == EXPAND_STACK_PARM)
9886 target = 0;
9887 op0 = expand_expr (treeop0, subtarget,
9888 VOIDmode, EXPAND_NORMAL);
9890 /* Left shift optimization when shifting across word_size boundary.
9892 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9893 there isn't native instruction to support this wide mode
9894 left shift. Given below scenario:
9896 Type A = (Type) B << C
9898 |< T >|
9899 | dest_high | dest_low |
9901 | word_size |
9903 If the shift amount C caused we shift B to across the word
9904 size boundary, i.e part of B shifted into high half of
9905 destination register, and part of B remains in the low
9906 half, then GCC will use the following left shift expand
9907 logic:
9909 1. Initialize dest_low to B.
9910 2. Initialize every bit of dest_high to the sign bit of B.
9911 3. Logic left shift dest_low by C bit to finalize dest_low.
9912 The value of dest_low before this shift is kept in a temp D.
9913 4. Logic left shift dest_high by C.
9914 5. Logic right shift D by (word_size - C).
9915 6. Or the result of 4 and 5 to finalize dest_high.
9917 While, by checking gimple statements, if operand B is
9918 coming from signed extension, then we can simplify above
9919 expand logic into:
9921 1. dest_high = src_low >> (word_size - C).
9922 2. dest_low = src_low << C.
9924 We can use one arithmetic right shift to finish all the
9925 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9926 needed from 6 into 2.
9928 The case is similar for zero extension, except that we
9929 initialize dest_high to zero rather than copies of the sign
9930 bit from B. Furthermore, we need to use a logical right shift
9931 in this case.
9933 The choice of sign-extension versus zero-extension is
9934 determined entirely by whether or not B is signed and is
9935 independent of the current setting of unsignedp. */
9937 temp = NULL_RTX;
9938 if (code == LSHIFT_EXPR
9939 && target
9940 && REG_P (target)
9941 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9942 && mode == int_mode
9943 && TREE_CONSTANT (treeop1)
9944 && TREE_CODE (treeop0) == SSA_NAME)
9946 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9947 if (is_gimple_assign (def)
9948 && gimple_assign_rhs_code (def) == NOP_EXPR)
9950 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9951 (TREE_TYPE (gimple_assign_rhs1 (def)));
9953 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9954 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9955 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9956 >= GET_MODE_BITSIZE (word_mode)))
9958 rtx_insn *seq, *seq_old;
9959 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9960 int_mode);
9961 bool extend_unsigned
9962 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9963 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9964 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9965 rtx dest_high = simplify_gen_subreg (word_mode, target,
9966 int_mode, high_off);
9967 HOST_WIDE_INT ramount = (BITS_PER_WORD
9968 - TREE_INT_CST_LOW (treeop1));
9969 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9971 start_sequence ();
9972 /* dest_high = src_low >> (word_size - C). */
9973 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9974 rshift, dest_high,
9975 extend_unsigned);
9976 if (temp != dest_high)
9977 emit_move_insn (dest_high, temp);
9979 /* dest_low = src_low << C. */
9980 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9981 treeop1, dest_low, unsignedp);
9982 if (temp != dest_low)
9983 emit_move_insn (dest_low, temp);
9985 seq = get_insns ();
9986 end_sequence ();
9987 temp = target ;
9989 if (have_insn_for (ASHIFT, int_mode))
9991 bool speed_p = optimize_insn_for_speed_p ();
9992 start_sequence ();
9993 rtx ret_old = expand_variable_shift (code, int_mode,
9994 op0, treeop1,
9995 target,
9996 unsignedp);
9998 seq_old = get_insns ();
9999 end_sequence ();
10000 if (seq_cost (seq, speed_p)
10001 >= seq_cost (seq_old, speed_p))
10003 seq = seq_old;
10004 temp = ret_old;
10007 emit_insn (seq);
10012 if (temp == NULL_RTX)
10013 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10014 unsignedp);
10015 if (code == LSHIFT_EXPR)
10016 temp = REDUCE_BIT_FIELD (temp);
10017 return temp;
10020 /* Could determine the answer when only additive constants differ. Also,
10021 the addition of one can be handled by changing the condition. */
10022 case LT_EXPR:
10023 case LE_EXPR:
10024 case GT_EXPR:
10025 case GE_EXPR:
10026 case EQ_EXPR:
10027 case NE_EXPR:
10028 case UNORDERED_EXPR:
10029 case ORDERED_EXPR:
10030 case UNLT_EXPR:
10031 case UNLE_EXPR:
10032 case UNGT_EXPR:
10033 case UNGE_EXPR:
10034 case UNEQ_EXPR:
10035 case LTGT_EXPR:
10037 temp = do_store_flag (ops,
10038 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10039 tmode != VOIDmode ? tmode : mode);
10040 if (temp)
10041 return temp;
10043 /* Use a compare and a jump for BLKmode comparisons, or for function
10044 type comparisons is have_canonicalize_funcptr_for_compare. */
10046 if ((target == 0
10047 || modifier == EXPAND_STACK_PARM
10048 || ! safe_from_p (target, treeop0, 1)
10049 || ! safe_from_p (target, treeop1, 1)
10050 /* Make sure we don't have a hard reg (such as function's return
10051 value) live across basic blocks, if not optimizing. */
10052 || (!optimize && REG_P (target)
10053 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10054 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10056 emit_move_insn (target, const0_rtx);
10058 rtx_code_label *lab1 = gen_label_rtx ();
10059 jumpifnot_1 (code, treeop0, treeop1, lab1,
10060 profile_probability::uninitialized ());
10062 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10063 emit_move_insn (target, constm1_rtx);
10064 else
10065 emit_move_insn (target, const1_rtx);
10067 emit_label (lab1);
10068 return target;
10070 case COMPLEX_EXPR:
10071 /* Get the rtx code of the operands. */
10072 op0 = expand_normal (treeop0);
10073 op1 = expand_normal (treeop1);
10075 if (!target)
10076 target = gen_reg_rtx (TYPE_MODE (type));
10077 else
10078 /* If target overlaps with op1, then either we need to force
10079 op1 into a pseudo (if target also overlaps with op0),
10080 or write the complex parts in reverse order. */
10081 switch (GET_CODE (target))
10083 case CONCAT:
10084 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10086 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10088 complex_expr_force_op1:
10089 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10090 emit_move_insn (temp, op1);
10091 op1 = temp;
10092 break;
10094 complex_expr_swap_order:
10095 /* Move the imaginary (op1) and real (op0) parts to their
10096 location. */
10097 write_complex_part (target, op1, true);
10098 write_complex_part (target, op0, false);
10100 return target;
10102 break;
10103 case MEM:
10104 temp = adjust_address_nv (target,
10105 GET_MODE_INNER (GET_MODE (target)), 0);
10106 if (reg_overlap_mentioned_p (temp, op1))
10108 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10109 temp = adjust_address_nv (target, imode,
10110 GET_MODE_SIZE (imode));
10111 if (reg_overlap_mentioned_p (temp, op0))
10112 goto complex_expr_force_op1;
10113 goto complex_expr_swap_order;
10115 break;
10116 default:
10117 if (reg_overlap_mentioned_p (target, op1))
10119 if (reg_overlap_mentioned_p (target, op0))
10120 goto complex_expr_force_op1;
10121 goto complex_expr_swap_order;
10123 break;
10126 /* Move the real (op0) and imaginary (op1) parts to their location. */
10127 write_complex_part (target, op0, false);
10128 write_complex_part (target, op1, true);
10130 return target;
10132 case WIDEN_SUM_EXPR:
10134 tree oprnd0 = treeop0;
10135 tree oprnd1 = treeop1;
10137 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10138 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10139 target, unsignedp);
10140 return target;
10143 case VEC_UNPACK_HI_EXPR:
10144 case VEC_UNPACK_LO_EXPR:
10145 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10146 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10148 op0 = expand_normal (treeop0);
10149 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10150 target, unsignedp);
10151 gcc_assert (temp);
10152 return temp;
10155 case VEC_UNPACK_FLOAT_HI_EXPR:
10156 case VEC_UNPACK_FLOAT_LO_EXPR:
10158 op0 = expand_normal (treeop0);
10159 /* The signedness is determined from input operand. */
10160 temp = expand_widen_pattern_expr
10161 (ops, op0, NULL_RTX, NULL_RTX,
10162 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10164 gcc_assert (temp);
10165 return temp;
10168 case VEC_WIDEN_PLUS_HI_EXPR:
10169 case VEC_WIDEN_PLUS_LO_EXPR:
10170 case VEC_WIDEN_MINUS_HI_EXPR:
10171 case VEC_WIDEN_MINUS_LO_EXPR:
10172 case VEC_WIDEN_MULT_HI_EXPR:
10173 case VEC_WIDEN_MULT_LO_EXPR:
10174 case VEC_WIDEN_MULT_EVEN_EXPR:
10175 case VEC_WIDEN_MULT_ODD_EXPR:
10176 case VEC_WIDEN_LSHIFT_HI_EXPR:
10177 case VEC_WIDEN_LSHIFT_LO_EXPR:
10178 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10179 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10180 target, unsignedp);
10181 gcc_assert (target);
10182 return target;
10184 case VEC_PACK_SAT_EXPR:
10185 case VEC_PACK_FIX_TRUNC_EXPR:
10186 mode = TYPE_MODE (TREE_TYPE (treeop0));
10187 subtarget = NULL_RTX;
10188 goto binop;
10190 case VEC_PACK_TRUNC_EXPR:
10191 if (VECTOR_BOOLEAN_TYPE_P (type)
10192 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10193 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10194 && SCALAR_INT_MODE_P (mode))
10196 class expand_operand eops[4];
10197 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10198 expand_operands (treeop0, treeop1,
10199 subtarget, &op0, &op1, EXPAND_NORMAL);
10200 this_optab = vec_pack_sbool_trunc_optab;
10201 enum insn_code icode = optab_handler (this_optab, imode);
10202 create_output_operand (&eops[0], target, mode);
10203 create_convert_operand_from (&eops[1], op0, imode, false);
10204 create_convert_operand_from (&eops[2], op1, imode, false);
10205 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10206 create_input_operand (&eops[3], temp, imode);
10207 expand_insn (icode, 4, eops);
10208 return eops[0].value;
10210 mode = TYPE_MODE (TREE_TYPE (treeop0));
10211 subtarget = NULL_RTX;
10212 goto binop;
10214 case VEC_PACK_FLOAT_EXPR:
10215 mode = TYPE_MODE (TREE_TYPE (treeop0));
10216 expand_operands (treeop0, treeop1,
10217 subtarget, &op0, &op1, EXPAND_NORMAL);
10218 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10219 optab_default);
10220 target = expand_binop (mode, this_optab, op0, op1, target,
10221 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10222 OPTAB_LIB_WIDEN);
10223 gcc_assert (target);
10224 return target;
10226 case VEC_PERM_EXPR:
10228 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10229 vec_perm_builder sel;
10230 if (TREE_CODE (treeop2) == VECTOR_CST
10231 && tree_to_vec_perm_builder (&sel, treeop2))
10233 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10234 temp = expand_vec_perm_const (mode, op0, op1, sel,
10235 sel_mode, target);
10237 else
10239 op2 = expand_normal (treeop2);
10240 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10242 gcc_assert (temp);
10243 return temp;
10246 case DOT_PROD_EXPR:
10248 tree oprnd0 = treeop0;
10249 tree oprnd1 = treeop1;
10250 tree oprnd2 = treeop2;
10252 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10253 op2 = expand_normal (oprnd2);
10254 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10255 target, unsignedp);
10256 return target;
10259 case SAD_EXPR:
10261 tree oprnd0 = treeop0;
10262 tree oprnd1 = treeop1;
10263 tree oprnd2 = treeop2;
10265 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10266 op2 = expand_normal (oprnd2);
10267 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10268 target, unsignedp);
10269 return target;
10272 case REALIGN_LOAD_EXPR:
10274 tree oprnd0 = treeop0;
10275 tree oprnd1 = treeop1;
10276 tree oprnd2 = treeop2;
10278 this_optab = optab_for_tree_code (code, type, optab_default);
10279 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10280 op2 = expand_normal (oprnd2);
10281 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10282 target, unsignedp);
10283 gcc_assert (temp);
10284 return temp;
10287 case COND_EXPR:
10289 /* A COND_EXPR with its type being VOID_TYPE represents a
10290 conditional jump and is handled in
10291 expand_gimple_cond_expr. */
10292 gcc_assert (!VOID_TYPE_P (type));
10294 /* Note that COND_EXPRs whose type is a structure or union
10295 are required to be constructed to contain assignments of
10296 a temporary variable, so that we can evaluate them here
10297 for side effect only. If type is void, we must do likewise. */
10299 gcc_assert (!TREE_ADDRESSABLE (type)
10300 && !ignore
10301 && TREE_TYPE (treeop1) != void_type_node
10302 && TREE_TYPE (treeop2) != void_type_node);
10304 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10305 if (temp)
10306 return temp;
10308 /* If we are not to produce a result, we have no target. Otherwise,
10309 if a target was specified use it; it will not be used as an
10310 intermediate target unless it is safe. If no target, use a
10311 temporary. */
10313 if (modifier != EXPAND_STACK_PARM
10314 && original_target
10315 && safe_from_p (original_target, treeop0, 1)
10316 && GET_MODE (original_target) == mode
10317 && !MEM_P (original_target))
10318 temp = original_target;
10319 else
10320 temp = assign_temp (type, 0, 1);
10322 do_pending_stack_adjust ();
10323 NO_DEFER_POP;
10324 rtx_code_label *lab0 = gen_label_rtx ();
10325 rtx_code_label *lab1 = gen_label_rtx ();
10326 jumpifnot (treeop0, lab0,
10327 profile_probability::uninitialized ());
10328 store_expr (treeop1, temp,
10329 modifier == EXPAND_STACK_PARM,
10330 false, false);
10332 emit_jump_insn (targetm.gen_jump (lab1));
10333 emit_barrier ();
10334 emit_label (lab0);
10335 store_expr (treeop2, temp,
10336 modifier == EXPAND_STACK_PARM,
10337 false, false);
10339 emit_label (lab1);
10340 OK_DEFER_POP;
10341 return temp;
10344 case VEC_DUPLICATE_EXPR:
10345 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10346 target = expand_vector_broadcast (mode, op0);
10347 gcc_assert (target);
10348 return target;
10350 case VEC_SERIES_EXPR:
10351 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10352 return expand_vec_series_expr (mode, op0, op1, target);
10354 case BIT_INSERT_EXPR:
10356 unsigned bitpos = tree_to_uhwi (treeop2);
10357 unsigned bitsize;
10358 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10359 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10360 else
10361 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10362 op0 = expand_normal (treeop0);
10363 op1 = expand_normal (treeop1);
10364 rtx dst = gen_reg_rtx (mode);
10365 emit_move_insn (dst, op0);
10366 store_bit_field (dst, bitsize, bitpos, 0, 0,
10367 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
10368 return dst;
10371 default:
10372 gcc_unreachable ();
10375 /* Here to do an ordinary binary operator. */
10376 binop:
10377 expand_operands (treeop0, treeop1,
10378 subtarget, &op0, &op1, EXPAND_NORMAL);
10379 binop2:
10380 this_optab = optab_for_tree_code (code, type, optab_default);
10381 binop3:
10382 if (modifier == EXPAND_STACK_PARM)
10383 target = 0;
10384 temp = expand_binop (mode, this_optab, op0, op1, target,
10385 unsignedp, OPTAB_LIB_WIDEN);
10386 gcc_assert (temp);
10387 /* Bitwise operations do not need bitfield reduction as we expect their
10388 operands being properly truncated. */
10389 if (code == BIT_XOR_EXPR
10390 || code == BIT_AND_EXPR
10391 || code == BIT_IOR_EXPR)
10392 return temp;
10393 return REDUCE_BIT_FIELD (temp);
10395 #undef REDUCE_BIT_FIELD
10398 /* Return TRUE if expression STMT is suitable for replacement.
10399 Never consider memory loads as replaceable, because those don't ever lead
10400 into constant expressions. */
10402 static bool
10403 stmt_is_replaceable_p (gimple *stmt)
10405 if (ssa_is_replaceable_p (stmt))
10407 /* Don't move around loads. */
10408 if (!gimple_assign_single_p (stmt)
10409 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10410 return true;
10412 return false;
10416 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10417 enum expand_modifier modifier, rtx *alt_rtl,
10418 bool inner_reference_p)
10420 rtx op0, op1, temp, decl_rtl;
10421 tree type;
10422 int unsignedp;
10423 machine_mode mode, dmode;
10424 enum tree_code code = TREE_CODE (exp);
10425 rtx subtarget, original_target;
10426 int ignore;
10427 bool reduce_bit_field;
10428 location_t loc = EXPR_LOCATION (exp);
10429 struct separate_ops ops;
10430 tree treeop0, treeop1, treeop2;
10431 tree ssa_name = NULL_TREE;
10432 gimple *g;
10434 type = TREE_TYPE (exp);
10435 mode = TYPE_MODE (type);
10436 unsignedp = TYPE_UNSIGNED (type);
10438 treeop0 = treeop1 = treeop2 = NULL_TREE;
10439 if (!VL_EXP_CLASS_P (exp))
10440 switch (TREE_CODE_LENGTH (code))
10442 default:
10443 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10444 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10445 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10446 case 0: break;
10448 ops.code = code;
10449 ops.type = type;
10450 ops.op0 = treeop0;
10451 ops.op1 = treeop1;
10452 ops.op2 = treeop2;
10453 ops.location = loc;
10455 ignore = (target == const0_rtx
10456 || ((CONVERT_EXPR_CODE_P (code)
10457 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10458 && TREE_CODE (type) == VOID_TYPE));
10460 /* An operation in what may be a bit-field type needs the
10461 result to be reduced to the precision of the bit-field type,
10462 which is narrower than that of the type's mode. */
10463 reduce_bit_field = (!ignore
10464 && INTEGRAL_TYPE_P (type)
10465 && !type_has_mode_precision_p (type));
10467 /* If we are going to ignore this result, we need only do something
10468 if there is a side-effect somewhere in the expression. If there
10469 is, short-circuit the most common cases here. Note that we must
10470 not call expand_expr with anything but const0_rtx in case this
10471 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10473 if (ignore)
10475 if (! TREE_SIDE_EFFECTS (exp))
10476 return const0_rtx;
10478 /* Ensure we reference a volatile object even if value is ignored, but
10479 don't do this if all we are doing is taking its address. */
10480 if (TREE_THIS_VOLATILE (exp)
10481 && TREE_CODE (exp) != FUNCTION_DECL
10482 && mode != VOIDmode && mode != BLKmode
10483 && modifier != EXPAND_CONST_ADDRESS)
10485 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10486 if (MEM_P (temp))
10487 copy_to_reg (temp);
10488 return const0_rtx;
10491 if (TREE_CODE_CLASS (code) == tcc_unary
10492 || code == BIT_FIELD_REF
10493 || code == COMPONENT_REF
10494 || code == INDIRECT_REF)
10495 return expand_expr (treeop0, const0_rtx, VOIDmode,
10496 modifier);
10498 else if (TREE_CODE_CLASS (code) == tcc_binary
10499 || TREE_CODE_CLASS (code) == tcc_comparison
10500 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10502 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10503 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10504 return const0_rtx;
10507 target = 0;
10510 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10511 target = 0;
10513 /* Use subtarget as the target for operand 0 of a binary operation. */
10514 subtarget = get_subtarget (target);
10515 original_target = target;
10517 switch (code)
10519 case LABEL_DECL:
10521 tree function = decl_function_context (exp);
10523 temp = label_rtx (exp);
10524 temp = gen_rtx_LABEL_REF (Pmode, temp);
10526 if (function != current_function_decl
10527 && function != 0)
10528 LABEL_REF_NONLOCAL_P (temp) = 1;
10530 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10531 return temp;
10534 case SSA_NAME:
10535 /* ??? ivopts calls expander, without any preparation from
10536 out-of-ssa. So fake instructions as if this was an access to the
10537 base variable. This unnecessarily allocates a pseudo, see how we can
10538 reuse it, if partition base vars have it set already. */
10539 if (!currently_expanding_to_rtl)
10541 tree var = SSA_NAME_VAR (exp);
10542 if (var && DECL_RTL_SET_P (var))
10543 return DECL_RTL (var);
10544 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10545 LAST_VIRTUAL_REGISTER + 1);
10548 g = get_gimple_for_ssa_name (exp);
10549 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10550 if (g == NULL
10551 && modifier == EXPAND_INITIALIZER
10552 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10553 && (optimize || !SSA_NAME_VAR (exp)
10554 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10555 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10556 g = SSA_NAME_DEF_STMT (exp);
10557 if (g)
10559 rtx r;
10560 location_t saved_loc = curr_insn_location ();
10561 loc = gimple_location (g);
10562 if (loc != UNKNOWN_LOCATION)
10563 set_curr_insn_location (loc);
10564 ops.code = gimple_assign_rhs_code (g);
10565 switch (get_gimple_rhs_class (ops.code))
10567 case GIMPLE_TERNARY_RHS:
10568 ops.op2 = gimple_assign_rhs3 (g);
10569 /* Fallthru */
10570 case GIMPLE_BINARY_RHS:
10571 ops.op1 = gimple_assign_rhs2 (g);
10573 /* Try to expand conditonal compare. */
10574 if (targetm.gen_ccmp_first)
10576 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10577 r = expand_ccmp_expr (g, mode);
10578 if (r)
10579 break;
10581 /* Fallthru */
10582 case GIMPLE_UNARY_RHS:
10583 ops.op0 = gimple_assign_rhs1 (g);
10584 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10585 ops.location = loc;
10586 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10587 break;
10588 case GIMPLE_SINGLE_RHS:
10590 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10591 tmode, modifier, alt_rtl,
10592 inner_reference_p);
10593 break;
10595 default:
10596 gcc_unreachable ();
10598 set_curr_insn_location (saved_loc);
10599 if (REG_P (r) && !REG_EXPR (r))
10600 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10601 return r;
10604 ssa_name = exp;
10605 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10606 exp = SSA_NAME_VAR (ssa_name);
10607 goto expand_decl_rtl;
10609 case VAR_DECL:
10610 /* Allow accel compiler to handle variables that require special
10611 treatment, e.g. if they have been modified in some way earlier in
10612 compilation by the adjust_private_decl OpenACC hook. */
10613 if (flag_openacc && targetm.goacc.expand_var_decl)
10615 temp = targetm.goacc.expand_var_decl (exp);
10616 if (temp)
10617 return temp;
10619 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10620 have scalar integer modes to a reg via store_constructor. */
10621 if (TREE_READONLY (exp)
10622 && !TREE_SIDE_EFFECTS (exp)
10623 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10624 && immediate_const_ctor_p (DECL_INITIAL (exp))
10625 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10626 && crtl->emit.regno_pointer_align_length
10627 && !target)
10629 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10630 store_constructor (DECL_INITIAL (exp), target, 0,
10631 int_expr_size (DECL_INITIAL (exp)), false);
10632 return target;
10634 /* ... fall through ... */
10636 case PARM_DECL:
10637 /* If a static var's type was incomplete when the decl was written,
10638 but the type is complete now, lay out the decl now. */
10639 if (DECL_SIZE (exp) == 0
10640 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10641 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10642 layout_decl (exp, 0);
10644 /* fall through */
10646 case FUNCTION_DECL:
10647 case RESULT_DECL:
10648 decl_rtl = DECL_RTL (exp);
10649 expand_decl_rtl:
10650 gcc_assert (decl_rtl);
10652 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10653 settings for VECTOR_TYPE_P that might switch for the function. */
10654 if (currently_expanding_to_rtl
10655 && code == VAR_DECL && MEM_P (decl_rtl)
10656 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10657 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10658 else
10659 decl_rtl = copy_rtx (decl_rtl);
10661 /* Record writes to register variables. */
10662 if (modifier == EXPAND_WRITE
10663 && REG_P (decl_rtl)
10664 && HARD_REGISTER_P (decl_rtl))
10665 add_to_hard_reg_set (&crtl->asm_clobbers,
10666 GET_MODE (decl_rtl), REGNO (decl_rtl));
10668 /* Ensure variable marked as used even if it doesn't go through
10669 a parser. If it hasn't be used yet, write out an external
10670 definition. */
10671 if (exp)
10672 TREE_USED (exp) = 1;
10674 /* Show we haven't gotten RTL for this yet. */
10675 temp = 0;
10677 /* Variables inherited from containing functions should have
10678 been lowered by this point. */
10679 if (exp)
10681 tree context = decl_function_context (exp);
10682 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10683 || context == current_function_decl
10684 || TREE_STATIC (exp)
10685 || DECL_EXTERNAL (exp)
10686 /* ??? C++ creates functions that are not
10687 TREE_STATIC. */
10688 || TREE_CODE (exp) == FUNCTION_DECL);
10691 /* This is the case of an array whose size is to be determined
10692 from its initializer, while the initializer is still being parsed.
10693 ??? We aren't parsing while expanding anymore. */
10695 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10696 temp = validize_mem (decl_rtl);
10698 /* If DECL_RTL is memory, we are in the normal case and the
10699 address is not valid, get the address into a register. */
10701 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10703 if (alt_rtl)
10704 *alt_rtl = decl_rtl;
10705 decl_rtl = use_anchored_address (decl_rtl);
10706 if (modifier != EXPAND_CONST_ADDRESS
10707 && modifier != EXPAND_SUM
10708 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10709 : GET_MODE (decl_rtl),
10710 XEXP (decl_rtl, 0),
10711 MEM_ADDR_SPACE (decl_rtl)))
10712 temp = replace_equiv_address (decl_rtl,
10713 copy_rtx (XEXP (decl_rtl, 0)));
10716 /* If we got something, return it. But first, set the alignment
10717 if the address is a register. */
10718 if (temp != 0)
10720 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10721 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10723 else if (MEM_P (decl_rtl))
10724 temp = decl_rtl;
10726 if (temp != 0)
10728 if (MEM_P (temp)
10729 && modifier != EXPAND_WRITE
10730 && modifier != EXPAND_MEMORY
10731 && modifier != EXPAND_INITIALIZER
10732 && modifier != EXPAND_CONST_ADDRESS
10733 && modifier != EXPAND_SUM
10734 && !inner_reference_p
10735 && mode != BLKmode
10736 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10737 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10738 MEM_ALIGN (temp), NULL_RTX, NULL);
10740 return temp;
10743 if (exp)
10744 dmode = DECL_MODE (exp);
10745 else
10746 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10748 /* If the mode of DECL_RTL does not match that of the decl,
10749 there are two cases: we are dealing with a BLKmode value
10750 that is returned in a register, or we are dealing with
10751 a promoted value. In the latter case, return a SUBREG
10752 of the wanted mode, but mark it so that we know that it
10753 was already extended. */
10754 if (REG_P (decl_rtl)
10755 && dmode != BLKmode
10756 && GET_MODE (decl_rtl) != dmode)
10758 machine_mode pmode;
10760 /* Get the signedness to be used for this variable. Ensure we get
10761 the same mode we got when the variable was declared. */
10762 if (code != SSA_NAME)
10763 pmode = promote_decl_mode (exp, &unsignedp);
10764 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10765 && gimple_code (g) == GIMPLE_CALL
10766 && !gimple_call_internal_p (g))
10767 pmode = promote_function_mode (type, mode, &unsignedp,
10768 gimple_call_fntype (g),
10770 else
10771 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10772 gcc_assert (GET_MODE (decl_rtl) == pmode);
10774 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10775 SUBREG_PROMOTED_VAR_P (temp) = 1;
10776 SUBREG_PROMOTED_SET (temp, unsignedp);
10777 return temp;
10780 return decl_rtl;
10782 case INTEGER_CST:
10784 /* Given that TYPE_PRECISION (type) is not always equal to
10785 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10786 the former to the latter according to the signedness of the
10787 type. */
10788 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10789 temp = immed_wide_int_const
10790 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10791 return temp;
10794 case VECTOR_CST:
10796 tree tmp = NULL_TREE;
10797 if (VECTOR_MODE_P (mode))
10798 return const_vector_from_tree (exp);
10799 scalar_int_mode int_mode;
10800 if (is_int_mode (mode, &int_mode))
10802 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10803 if (type_for_mode)
10804 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10805 type_for_mode, exp);
10807 if (!tmp)
10809 vec<constructor_elt, va_gc> *v;
10810 /* Constructors need to be fixed-length. FIXME. */
10811 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10812 vec_alloc (v, nunits);
10813 for (unsigned int i = 0; i < nunits; ++i)
10814 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10815 tmp = build_constructor (type, v);
10817 return expand_expr (tmp, ignore ? const0_rtx : target,
10818 tmode, modifier);
10821 case CONST_DECL:
10822 if (modifier == EXPAND_WRITE)
10824 /* Writing into CONST_DECL is always invalid, but handle it
10825 gracefully. */
10826 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10827 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10828 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10829 EXPAND_NORMAL, as);
10830 op0 = memory_address_addr_space (mode, op0, as);
10831 temp = gen_rtx_MEM (mode, op0);
10832 set_mem_addr_space (temp, as);
10833 return temp;
10835 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10837 case REAL_CST:
10838 /* If optimized, generate immediate CONST_DOUBLE
10839 which will be turned into memory by reload if necessary.
10841 We used to force a register so that loop.c could see it. But
10842 this does not allow gen_* patterns to perform optimizations with
10843 the constants. It also produces two insns in cases like "x = 1.0;".
10844 On most machines, floating-point constants are not permitted in
10845 many insns, so we'd end up copying it to a register in any case.
10847 Now, we do the copying in expand_binop, if appropriate. */
10848 return const_double_from_real_value (TREE_REAL_CST (exp),
10849 TYPE_MODE (TREE_TYPE (exp)));
10851 case FIXED_CST:
10852 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10853 TYPE_MODE (TREE_TYPE (exp)));
10855 case COMPLEX_CST:
10856 /* Handle evaluating a complex constant in a CONCAT target. */
10857 if (original_target && GET_CODE (original_target) == CONCAT)
10859 rtx rtarg, itarg;
10861 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10862 rtarg = XEXP (original_target, 0);
10863 itarg = XEXP (original_target, 1);
10865 /* Move the real and imaginary parts separately. */
10866 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10867 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10869 if (op0 != rtarg)
10870 emit_move_insn (rtarg, op0);
10871 if (op1 != itarg)
10872 emit_move_insn (itarg, op1);
10874 return original_target;
10877 /* fall through */
10879 case STRING_CST:
10880 temp = expand_expr_constant (exp, 1, modifier);
10882 /* temp contains a constant address.
10883 On RISC machines where a constant address isn't valid,
10884 make some insns to get that address into a register. */
10885 if (modifier != EXPAND_CONST_ADDRESS
10886 && modifier != EXPAND_INITIALIZER
10887 && modifier != EXPAND_SUM
10888 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10889 MEM_ADDR_SPACE (temp)))
10890 return replace_equiv_address (temp,
10891 copy_rtx (XEXP (temp, 0)));
10892 return temp;
10894 case POLY_INT_CST:
10895 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10897 case SAVE_EXPR:
10899 tree val = treeop0;
10900 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10901 inner_reference_p);
10903 if (!SAVE_EXPR_RESOLVED_P (exp))
10905 /* We can indeed still hit this case, typically via builtin
10906 expanders calling save_expr immediately before expanding
10907 something. Assume this means that we only have to deal
10908 with non-BLKmode values. */
10909 gcc_assert (GET_MODE (ret) != BLKmode);
10911 val = build_decl (curr_insn_location (),
10912 VAR_DECL, NULL, TREE_TYPE (exp));
10913 DECL_ARTIFICIAL (val) = 1;
10914 DECL_IGNORED_P (val) = 1;
10915 treeop0 = val;
10916 TREE_OPERAND (exp, 0) = treeop0;
10917 SAVE_EXPR_RESOLVED_P (exp) = 1;
10919 if (!CONSTANT_P (ret))
10920 ret = copy_to_reg (ret);
10921 SET_DECL_RTL (val, ret);
10924 return ret;
10928 case CONSTRUCTOR:
10929 /* If we don't need the result, just ensure we evaluate any
10930 subexpressions. */
10931 if (ignore)
10933 unsigned HOST_WIDE_INT idx;
10934 tree value;
10936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10937 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10939 return const0_rtx;
10942 return expand_constructor (exp, target, modifier, false);
10944 case TARGET_MEM_REF:
10946 addr_space_t as
10947 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10948 unsigned int align;
10950 op0 = addr_for_mem_ref (exp, as, true);
10951 op0 = memory_address_addr_space (mode, op0, as);
10952 temp = gen_rtx_MEM (mode, op0);
10953 set_mem_attributes (temp, exp, 0);
10954 set_mem_addr_space (temp, as);
10955 align = get_object_alignment (exp);
10956 if (modifier != EXPAND_WRITE
10957 && modifier != EXPAND_MEMORY
10958 && mode != BLKmode
10959 && align < GET_MODE_ALIGNMENT (mode))
10960 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10961 align, NULL_RTX, NULL);
10962 return temp;
10965 case MEM_REF:
10967 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10968 addr_space_t as
10969 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10970 machine_mode address_mode;
10971 tree base = TREE_OPERAND (exp, 0);
10972 gimple *def_stmt;
10973 unsigned align;
10974 /* Handle expansion of non-aliased memory with non-BLKmode. That
10975 might end up in a register. */
10976 if (mem_ref_refers_to_non_mem_p (exp))
10978 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10979 base = TREE_OPERAND (base, 0);
10980 poly_uint64 type_size;
10981 if (known_eq (offset, 0)
10982 && !reverse
10983 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10984 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10985 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10986 target, tmode, modifier);
10987 if (TYPE_MODE (type) == BLKmode)
10989 temp = assign_stack_temp (DECL_MODE (base),
10990 GET_MODE_SIZE (DECL_MODE (base)));
10991 store_expr (base, temp, 0, false, false);
10992 temp = adjust_address (temp, BLKmode, offset);
10993 set_mem_size (temp, int_size_in_bytes (type));
10994 return temp;
10996 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10997 bitsize_int (offset * BITS_PER_UNIT));
10998 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10999 return expand_expr (exp, target, tmode, modifier);
11001 address_mode = targetm.addr_space.address_mode (as);
11002 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11004 tree mask = gimple_assign_rhs2 (def_stmt);
11005 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11006 gimple_assign_rhs1 (def_stmt), mask);
11007 TREE_OPERAND (exp, 0) = base;
11009 align = get_object_alignment (exp);
11010 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11011 op0 = memory_address_addr_space (mode, op0, as);
11012 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11014 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11015 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11016 op0 = memory_address_addr_space (mode, op0, as);
11018 temp = gen_rtx_MEM (mode, op0);
11019 set_mem_attributes (temp, exp, 0);
11020 set_mem_addr_space (temp, as);
11021 if (TREE_THIS_VOLATILE (exp))
11022 MEM_VOLATILE_P (temp) = 1;
11023 if (modifier != EXPAND_WRITE
11024 && modifier != EXPAND_MEMORY
11025 && !inner_reference_p
11026 && mode != BLKmode
11027 && align < GET_MODE_ALIGNMENT (mode))
11028 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11029 modifier == EXPAND_STACK_PARM
11030 ? NULL_RTX : target, alt_rtl);
11031 if (reverse
11032 && modifier != EXPAND_MEMORY
11033 && modifier != EXPAND_WRITE)
11034 temp = flip_storage_order (mode, temp);
11035 return temp;
11038 case ARRAY_REF:
11041 tree array = treeop0;
11042 tree index = treeop1;
11043 tree init;
11045 /* Fold an expression like: "foo"[2].
11046 This is not done in fold so it won't happen inside &.
11047 Don't fold if this is for wide characters since it's too
11048 difficult to do correctly and this is a very rare case. */
11050 if (modifier != EXPAND_CONST_ADDRESS
11051 && modifier != EXPAND_INITIALIZER
11052 && modifier != EXPAND_MEMORY)
11054 tree t = fold_read_from_constant_string (exp);
11056 if (t)
11057 return expand_expr (t, target, tmode, modifier);
11060 /* If this is a constant index into a constant array,
11061 just get the value from the array. Handle both the cases when
11062 we have an explicit constructor and when our operand is a variable
11063 that was declared const. */
11065 if (modifier != EXPAND_CONST_ADDRESS
11066 && modifier != EXPAND_INITIALIZER
11067 && modifier != EXPAND_MEMORY
11068 && TREE_CODE (array) == CONSTRUCTOR
11069 && ! TREE_SIDE_EFFECTS (array)
11070 && TREE_CODE (index) == INTEGER_CST)
11072 unsigned HOST_WIDE_INT ix;
11073 tree field, value;
11075 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11076 field, value)
11077 if (tree_int_cst_equal (field, index))
11079 if (!TREE_SIDE_EFFECTS (value))
11080 return expand_expr (fold (value), target, tmode, modifier);
11081 break;
11085 else if (optimize >= 1
11086 && modifier != EXPAND_CONST_ADDRESS
11087 && modifier != EXPAND_INITIALIZER
11088 && modifier != EXPAND_MEMORY
11089 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11090 && TREE_CODE (index) == INTEGER_CST
11091 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11092 && (init = ctor_for_folding (array)) != error_mark_node)
11094 if (init == NULL_TREE)
11096 tree value = build_zero_cst (type);
11097 if (TREE_CODE (value) == CONSTRUCTOR)
11099 /* If VALUE is a CONSTRUCTOR, this optimization is only
11100 useful if this doesn't store the CONSTRUCTOR into
11101 memory. If it does, it is more efficient to just
11102 load the data from the array directly. */
11103 rtx ret = expand_constructor (value, target,
11104 modifier, true);
11105 if (ret == NULL_RTX)
11106 value = NULL_TREE;
11109 if (value)
11110 return expand_expr (value, target, tmode, modifier);
11112 else if (TREE_CODE (init) == CONSTRUCTOR)
11114 unsigned HOST_WIDE_INT ix;
11115 tree field, value;
11117 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11118 field, value)
11119 if (tree_int_cst_equal (field, index))
11121 if (TREE_SIDE_EFFECTS (value))
11122 break;
11124 if (TREE_CODE (value) == CONSTRUCTOR)
11126 /* If VALUE is a CONSTRUCTOR, this
11127 optimization is only useful if
11128 this doesn't store the CONSTRUCTOR
11129 into memory. If it does, it is more
11130 efficient to just load the data from
11131 the array directly. */
11132 rtx ret = expand_constructor (value, target,
11133 modifier, true);
11134 if (ret == NULL_RTX)
11135 break;
11138 return
11139 expand_expr (fold (value), target, tmode, modifier);
11142 else if (TREE_CODE (init) == STRING_CST)
11144 tree low_bound = array_ref_low_bound (exp);
11145 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11147 /* Optimize the special case of a zero lower bound.
11149 We convert the lower bound to sizetype to avoid problems
11150 with constant folding. E.g. suppose the lower bound is
11151 1 and its mode is QI. Without the conversion
11152 (ARRAY + (INDEX - (unsigned char)1))
11153 becomes
11154 (ARRAY + (-(unsigned char)1) + INDEX)
11155 which becomes
11156 (ARRAY + 255 + INDEX). Oops! */
11157 if (!integer_zerop (low_bound))
11158 index1 = size_diffop_loc (loc, index1,
11159 fold_convert_loc (loc, sizetype,
11160 low_bound));
11162 if (tree_fits_uhwi_p (index1)
11163 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11165 tree char_type = TREE_TYPE (TREE_TYPE (init));
11166 scalar_int_mode char_mode;
11168 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11169 && GET_MODE_SIZE (char_mode) == 1)
11170 return gen_int_mode (TREE_STRING_POINTER (init)
11171 [TREE_INT_CST_LOW (index1)],
11172 char_mode);
11177 goto normal_inner_ref;
11179 case COMPONENT_REF:
11180 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11181 /* Fall through. */
11182 case BIT_FIELD_REF:
11183 case ARRAY_RANGE_REF:
11184 normal_inner_ref:
11186 machine_mode mode1, mode2;
11187 poly_int64 bitsize, bitpos, bytepos;
11188 tree offset;
11189 int reversep, volatilep = 0;
11190 tree tem
11191 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11192 &unsignedp, &reversep, &volatilep);
11193 rtx orig_op0, memloc;
11194 bool clear_mem_expr = false;
11195 bool must_force_mem;
11197 /* If we got back the original object, something is wrong. Perhaps
11198 we are evaluating an expression too early. In any event, don't
11199 infinitely recurse. */
11200 gcc_assert (tem != exp);
11202 /* Make sure bitpos is not negative, this can wreak havoc later. */
11203 if (maybe_lt (bitpos, 0))
11205 gcc_checking_assert (offset == NULL_TREE);
11206 offset = size_int (bits_to_bytes_round_down (bitpos));
11207 bitpos = num_trailing_bits (bitpos);
11210 /* If we have either an offset, a BLKmode result, or a reference
11211 outside the underlying object, we must force it to memory.
11212 Such a case can occur in Ada if we have unchecked conversion
11213 of an expression from a scalar type to an aggregate type or
11214 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11215 passed a partially uninitialized object or a view-conversion
11216 to a larger size. */
11217 must_force_mem = offset != NULL_TREE
11218 || mode1 == BLKmode
11219 || (mode == BLKmode
11220 && !int_mode_for_size (bitsize, 1).exists ());
11222 const enum expand_modifier tem_modifier
11223 = must_force_mem
11224 ? EXPAND_MEMORY
11225 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11227 /* If TEM's type is a union of variable size, pass TARGET to the inner
11228 computation, since it will need a temporary and TARGET is known
11229 to have to do. This occurs in unchecked conversion in Ada. */
11230 const rtx tem_target
11231 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11232 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11233 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11234 && modifier != EXPAND_STACK_PARM
11235 ? target
11236 : NULL_RTX;
11238 orig_op0 = op0
11239 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11240 true);
11242 /* If the field has a mode, we want to access it in the
11243 field's mode, not the computed mode.
11244 If a MEM has VOIDmode (external with incomplete type),
11245 use BLKmode for it instead. */
11246 if (MEM_P (op0))
11248 if (mode1 != VOIDmode)
11249 op0 = adjust_address (op0, mode1, 0);
11250 else if (GET_MODE (op0) == VOIDmode)
11251 op0 = adjust_address (op0, BLKmode, 0);
11254 mode2
11255 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11257 /* See above for the rationale. */
11258 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11259 must_force_mem = true;
11261 /* Handle CONCAT first. */
11262 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11264 if (known_eq (bitpos, 0)
11265 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11266 && COMPLEX_MODE_P (mode1)
11267 && COMPLEX_MODE_P (GET_MODE (op0))
11268 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11269 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11271 if (reversep)
11272 op0 = flip_storage_order (GET_MODE (op0), op0);
11273 if (mode1 != GET_MODE (op0))
11275 rtx parts[2];
11276 for (int i = 0; i < 2; i++)
11278 rtx op = read_complex_part (op0, i != 0);
11279 if (GET_CODE (op) == SUBREG)
11280 op = force_reg (GET_MODE (op), op);
11281 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11282 if (temp)
11283 op = temp;
11284 else
11286 if (!REG_P (op) && !MEM_P (op))
11287 op = force_reg (GET_MODE (op), op);
11288 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11290 parts[i] = op;
11292 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11294 return op0;
11296 if (known_eq (bitpos, 0)
11297 && known_eq (bitsize,
11298 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11299 && maybe_ne (bitsize, 0))
11301 op0 = XEXP (op0, 0);
11302 mode2 = GET_MODE (op0);
11304 else if (known_eq (bitpos,
11305 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11306 && known_eq (bitsize,
11307 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11308 && maybe_ne (bitpos, 0)
11309 && maybe_ne (bitsize, 0))
11311 op0 = XEXP (op0, 1);
11312 bitpos = 0;
11313 mode2 = GET_MODE (op0);
11315 else
11316 /* Otherwise force into memory. */
11317 must_force_mem = true;
11320 /* If this is a constant, put it in a register if it is a legitimate
11321 constant and we don't need a memory reference. */
11322 if (CONSTANT_P (op0)
11323 && mode2 != BLKmode
11324 && targetm.legitimate_constant_p (mode2, op0)
11325 && !must_force_mem)
11326 op0 = force_reg (mode2, op0);
11328 /* Otherwise, if this is a constant, try to force it to the constant
11329 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11330 is a legitimate constant. */
11331 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11332 op0 = validize_mem (memloc);
11334 /* Otherwise, if this is a constant or the object is not in memory
11335 and need be, put it there. */
11336 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11338 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11339 emit_move_insn (memloc, op0);
11340 op0 = memloc;
11341 clear_mem_expr = true;
11344 if (offset)
11346 machine_mode address_mode;
11347 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11348 EXPAND_SUM);
11350 gcc_assert (MEM_P (op0));
11352 address_mode = get_address_mode (op0);
11353 if (GET_MODE (offset_rtx) != address_mode)
11355 /* We cannot be sure that the RTL in offset_rtx is valid outside
11356 of a memory address context, so force it into a register
11357 before attempting to convert it to the desired mode. */
11358 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11359 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11362 /* See the comment in expand_assignment for the rationale. */
11363 if (mode1 != VOIDmode
11364 && maybe_ne (bitpos, 0)
11365 && maybe_gt (bitsize, 0)
11366 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11367 && multiple_p (bitpos, bitsize)
11368 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11369 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11371 op0 = adjust_address (op0, mode1, bytepos);
11372 bitpos = 0;
11375 op0 = offset_address (op0, offset_rtx,
11376 highest_pow2_factor (offset));
11379 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11380 record its alignment as BIGGEST_ALIGNMENT. */
11381 if (MEM_P (op0)
11382 && known_eq (bitpos, 0)
11383 && offset != 0
11384 && is_aligning_offset (offset, tem))
11385 set_mem_align (op0, BIGGEST_ALIGNMENT);
11387 /* Don't forget about volatility even if this is a bitfield. */
11388 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11390 if (op0 == orig_op0)
11391 op0 = copy_rtx (op0);
11393 MEM_VOLATILE_P (op0) = 1;
11396 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11398 if (op0 == orig_op0)
11399 op0 = copy_rtx (op0);
11401 set_mem_align (op0, BITS_PER_UNIT);
11404 /* In cases where an aligned union has an unaligned object
11405 as a field, we might be extracting a BLKmode value from
11406 an integer-mode (e.g., SImode) object. Handle this case
11407 by doing the extract into an object as wide as the field
11408 (which we know to be the width of a basic mode), then
11409 storing into memory, and changing the mode to BLKmode. */
11410 if (mode1 == VOIDmode
11411 || REG_P (op0) || GET_CODE (op0) == SUBREG
11412 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11413 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11414 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11415 && modifier != EXPAND_CONST_ADDRESS
11416 && modifier != EXPAND_INITIALIZER
11417 && modifier != EXPAND_MEMORY)
11418 /* If the bitfield is volatile and the bitsize
11419 is narrower than the access size of the bitfield,
11420 we need to extract bitfields from the access. */
11421 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11422 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11423 && mode1 != BLKmode
11424 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11425 /* If the field isn't aligned enough to fetch as a memref,
11426 fetch it as a bit field. */
11427 || (mode1 != BLKmode
11428 && (((MEM_P (op0)
11429 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11430 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11431 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11432 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11433 && modifier != EXPAND_MEMORY
11434 && ((modifier == EXPAND_CONST_ADDRESS
11435 || modifier == EXPAND_INITIALIZER)
11436 ? STRICT_ALIGNMENT
11437 : targetm.slow_unaligned_access (mode1,
11438 MEM_ALIGN (op0))))
11439 || !multiple_p (bitpos, BITS_PER_UNIT)))
11440 /* If the type and the field are a constant size and the
11441 size of the type isn't the same size as the bitfield,
11442 we must use bitfield operations. */
11443 || (known_size_p (bitsize)
11444 && TYPE_SIZE (TREE_TYPE (exp))
11445 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11446 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11447 bitsize)))
11449 machine_mode ext_mode = mode;
11451 if (ext_mode == BLKmode
11452 && ! (target != 0 && MEM_P (op0)
11453 && MEM_P (target)
11454 && multiple_p (bitpos, BITS_PER_UNIT)))
11455 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11457 if (ext_mode == BLKmode)
11459 if (target == 0)
11460 target = assign_temp (type, 1, 1);
11462 /* ??? Unlike the similar test a few lines below, this one is
11463 very likely obsolete. */
11464 if (known_eq (bitsize, 0))
11465 return target;
11467 /* In this case, BITPOS must start at a byte boundary and
11468 TARGET, if specified, must be a MEM. */
11469 gcc_assert (MEM_P (op0)
11470 && (!target || MEM_P (target)));
11472 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11473 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11474 emit_block_move (target,
11475 adjust_address (op0, VOIDmode, bytepos),
11476 gen_int_mode (bytesize, Pmode),
11477 (modifier == EXPAND_STACK_PARM
11478 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11480 return target;
11483 /* If we have nothing to extract, the result will be 0 for targets
11484 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11485 return 0 for the sake of consistency, as reading a zero-sized
11486 bitfield is valid in Ada and the value is fully specified. */
11487 if (known_eq (bitsize, 0))
11488 return const0_rtx;
11490 op0 = validize_mem (op0);
11492 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11493 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11495 /* If the result has aggregate type and the extraction is done in
11496 an integral mode, then the field may be not aligned on a byte
11497 boundary; in this case, if it has reverse storage order, it
11498 needs to be extracted as a scalar field with reverse storage
11499 order and put back into memory order afterwards. */
11500 if (AGGREGATE_TYPE_P (type)
11501 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11502 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11504 gcc_checking_assert (known_ge (bitpos, 0));
11505 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11506 (modifier == EXPAND_STACK_PARM
11507 ? NULL_RTX : target),
11508 ext_mode, ext_mode, reversep, alt_rtl);
11510 /* If the result has aggregate type and the mode of OP0 is an
11511 integral mode then, if BITSIZE is narrower than this mode
11512 and this is for big-endian data, we must put the field
11513 into the high-order bits. And we must also put it back
11514 into memory order if it has been previously reversed. */
11515 scalar_int_mode op0_mode;
11516 if (AGGREGATE_TYPE_P (type)
11517 && is_int_mode (GET_MODE (op0), &op0_mode))
11519 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11521 gcc_checking_assert (known_le (bitsize, size));
11522 if (maybe_lt (bitsize, size)
11523 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11524 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11525 size - bitsize, op0, 1);
11527 if (reversep)
11528 op0 = flip_storage_order (op0_mode, op0);
11531 /* If the result type is BLKmode, store the data into a temporary
11532 of the appropriate type, but with the mode corresponding to the
11533 mode for the data we have (op0's mode). */
11534 if (mode == BLKmode)
11536 rtx new_rtx
11537 = assign_stack_temp_for_type (ext_mode,
11538 GET_MODE_BITSIZE (ext_mode),
11539 type);
11540 emit_move_insn (new_rtx, op0);
11541 op0 = copy_rtx (new_rtx);
11542 PUT_MODE (op0, BLKmode);
11545 return op0;
11548 /* If the result is BLKmode, use that to access the object
11549 now as well. */
11550 if (mode == BLKmode)
11551 mode1 = BLKmode;
11553 /* Get a reference to just this component. */
11554 bytepos = bits_to_bytes_round_down (bitpos);
11555 if (modifier == EXPAND_CONST_ADDRESS
11556 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11557 op0 = adjust_address_nv (op0, mode1, bytepos);
11558 else
11559 op0 = adjust_address (op0, mode1, bytepos);
11561 if (op0 == orig_op0)
11562 op0 = copy_rtx (op0);
11564 /* Don't set memory attributes if the base expression is
11565 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11566 we should just honor its original memory attributes. */
11567 if (!(TREE_CODE (tem) == SSA_NAME
11568 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11569 set_mem_attributes (op0, exp, 0);
11571 if (REG_P (XEXP (op0, 0)))
11572 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11574 /* If op0 is a temporary because the original expressions was forced
11575 to memory, clear MEM_EXPR so that the original expression cannot
11576 be marked as addressable through MEM_EXPR of the temporary. */
11577 if (clear_mem_expr)
11578 set_mem_expr (op0, NULL_TREE);
11580 MEM_VOLATILE_P (op0) |= volatilep;
11582 if (reversep
11583 && modifier != EXPAND_MEMORY
11584 && modifier != EXPAND_WRITE)
11585 op0 = flip_storage_order (mode1, op0);
11587 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11588 || modifier == EXPAND_CONST_ADDRESS
11589 || modifier == EXPAND_INITIALIZER)
11590 return op0;
11592 if (target == 0)
11593 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11595 convert_move (target, op0, unsignedp);
11596 return target;
11599 case OBJ_TYPE_REF:
11600 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11602 case CALL_EXPR:
11603 /* All valid uses of __builtin_va_arg_pack () are removed during
11604 inlining. */
11605 if (CALL_EXPR_VA_ARG_PACK (exp))
11606 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11608 tree fndecl = get_callee_fndecl (exp), attr;
11610 if (fndecl
11611 /* Don't diagnose the error attribute in thunks, those are
11612 artificially created. */
11613 && !CALL_FROM_THUNK_P (exp)
11614 && (attr = lookup_attribute ("error",
11615 DECL_ATTRIBUTES (fndecl))) != NULL)
11617 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11618 error ("call to %qs declared with attribute error: %s",
11619 identifier_to_locale (ident),
11620 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11622 if (fndecl
11623 /* Don't diagnose the warning attribute in thunks, those are
11624 artificially created. */
11625 && !CALL_FROM_THUNK_P (exp)
11626 && (attr = lookup_attribute ("warning",
11627 DECL_ATTRIBUTES (fndecl))) != NULL)
11629 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11630 warning_at (EXPR_LOCATION (exp),
11631 OPT_Wattribute_warning,
11632 "call to %qs declared with attribute warning: %s",
11633 identifier_to_locale (ident),
11634 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11637 /* Check for a built-in function. */
11638 if (fndecl && fndecl_built_in_p (fndecl))
11640 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11641 return expand_builtin (exp, target, subtarget, tmode, ignore);
11644 return expand_call (exp, target, ignore);
11646 case VIEW_CONVERT_EXPR:
11647 op0 = NULL_RTX;
11649 /* If we are converting to BLKmode, try to avoid an intermediate
11650 temporary by fetching an inner memory reference. */
11651 if (mode == BLKmode
11652 && poly_int_tree_p (TYPE_SIZE (type))
11653 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11654 && handled_component_p (treeop0))
11656 machine_mode mode1;
11657 poly_int64 bitsize, bitpos, bytepos;
11658 tree offset;
11659 int reversep, volatilep = 0;
11660 tree tem
11661 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11662 &unsignedp, &reversep, &volatilep);
11664 /* ??? We should work harder and deal with non-zero offsets. */
11665 if (!offset
11666 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11667 && !reversep
11668 && known_size_p (bitsize)
11669 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11671 /* See the normal_inner_ref case for the rationale. */
11672 rtx orig_op0
11673 = expand_expr_real (tem,
11674 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11675 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11676 != INTEGER_CST)
11677 && modifier != EXPAND_STACK_PARM
11678 ? target : NULL_RTX),
11679 VOIDmode,
11680 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11681 NULL, true);
11683 if (MEM_P (orig_op0))
11685 op0 = orig_op0;
11687 /* Get a reference to just this component. */
11688 if (modifier == EXPAND_CONST_ADDRESS
11689 || modifier == EXPAND_SUM
11690 || modifier == EXPAND_INITIALIZER)
11691 op0 = adjust_address_nv (op0, mode, bytepos);
11692 else
11693 op0 = adjust_address (op0, mode, bytepos);
11695 if (op0 == orig_op0)
11696 op0 = copy_rtx (op0);
11698 set_mem_attributes (op0, treeop0, 0);
11699 if (REG_P (XEXP (op0, 0)))
11700 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11702 MEM_VOLATILE_P (op0) |= volatilep;
11707 if (!op0)
11708 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11709 NULL, inner_reference_p);
11711 /* If the input and output modes are both the same, we are done. */
11712 if (mode == GET_MODE (op0))
11714 /* If neither mode is BLKmode, and both modes are the same size
11715 then we can use gen_lowpart. */
11716 else if (mode != BLKmode
11717 && GET_MODE (op0) != BLKmode
11718 && known_eq (GET_MODE_PRECISION (mode),
11719 GET_MODE_PRECISION (GET_MODE (op0)))
11720 && !COMPLEX_MODE_P (GET_MODE (op0)))
11722 if (GET_CODE (op0) == SUBREG)
11723 op0 = force_reg (GET_MODE (op0), op0);
11724 temp = gen_lowpart_common (mode, op0);
11725 if (temp)
11726 op0 = temp;
11727 else
11729 if (!REG_P (op0) && !MEM_P (op0))
11730 op0 = force_reg (GET_MODE (op0), op0);
11731 op0 = gen_lowpart (mode, op0);
11734 /* If both types are integral, convert from one mode to the other. */
11735 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11736 op0 = convert_modes (mode, GET_MODE (op0), op0,
11737 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11738 /* If the output type is a bit-field type, do an extraction. */
11739 else if (reduce_bit_field)
11740 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11741 TYPE_UNSIGNED (type), NULL_RTX,
11742 mode, mode, false, NULL);
11743 /* As a last resort, spill op0 to memory, and reload it in a
11744 different mode. */
11745 else if (!MEM_P (op0))
11747 /* If the operand is not a MEM, force it into memory. Since we
11748 are going to be changing the mode of the MEM, don't call
11749 force_const_mem for constants because we don't allow pool
11750 constants to change mode. */
11751 tree inner_type = TREE_TYPE (treeop0);
11753 gcc_assert (!TREE_ADDRESSABLE (exp));
11755 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11756 target
11757 = assign_stack_temp_for_type
11758 (TYPE_MODE (inner_type),
11759 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11761 emit_move_insn (target, op0);
11762 op0 = target;
11765 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11766 output type is such that the operand is known to be aligned, indicate
11767 that it is. Otherwise, we need only be concerned about alignment for
11768 non-BLKmode results. */
11769 if (MEM_P (op0))
11771 enum insn_code icode;
11773 if (modifier != EXPAND_WRITE
11774 && modifier != EXPAND_MEMORY
11775 && !inner_reference_p
11776 && mode != BLKmode
11777 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11779 /* If the target does have special handling for unaligned
11780 loads of mode then use them. */
11781 if ((icode = optab_handler (movmisalign_optab, mode))
11782 != CODE_FOR_nothing)
11784 rtx reg;
11786 op0 = adjust_address (op0, mode, 0);
11787 /* We've already validated the memory, and we're creating a
11788 new pseudo destination. The predicates really can't
11789 fail. */
11790 reg = gen_reg_rtx (mode);
11792 /* Nor can the insn generator. */
11793 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11794 emit_insn (insn);
11795 return reg;
11797 else if (STRICT_ALIGNMENT)
11799 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11800 poly_uint64 temp_size = mode_size;
11801 if (GET_MODE (op0) != BLKmode)
11802 temp_size = upper_bound (temp_size,
11803 GET_MODE_SIZE (GET_MODE (op0)));
11804 rtx new_rtx
11805 = assign_stack_temp_for_type (mode, temp_size, type);
11806 rtx new_with_op0_mode
11807 = adjust_address (new_rtx, GET_MODE (op0), 0);
11809 gcc_assert (!TREE_ADDRESSABLE (exp));
11811 if (GET_MODE (op0) == BLKmode)
11813 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11814 emit_block_move (new_with_op0_mode, op0, size_rtx,
11815 (modifier == EXPAND_STACK_PARM
11816 ? BLOCK_OP_CALL_PARM
11817 : BLOCK_OP_NORMAL));
11819 else
11820 emit_move_insn (new_with_op0_mode, op0);
11822 op0 = new_rtx;
11826 op0 = adjust_address (op0, mode, 0);
11829 return op0;
11831 case MODIFY_EXPR:
11833 tree lhs = treeop0;
11834 tree rhs = treeop1;
11835 gcc_assert (ignore);
11837 /* Check for |= or &= of a bitfield of size one into another bitfield
11838 of size 1. In this case, (unless we need the result of the
11839 assignment) we can do this more efficiently with a
11840 test followed by an assignment, if necessary.
11842 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11843 things change so we do, this code should be enhanced to
11844 support it. */
11845 if (TREE_CODE (lhs) == COMPONENT_REF
11846 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11847 || TREE_CODE (rhs) == BIT_AND_EXPR)
11848 && TREE_OPERAND (rhs, 0) == lhs
11849 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11850 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11851 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11853 rtx_code_label *label = gen_label_rtx ();
11854 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11855 profile_probability prob = profile_probability::uninitialized ();
11856 if (value)
11857 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11858 else
11859 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11860 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11861 false);
11862 do_pending_stack_adjust ();
11863 emit_label (label);
11864 return const0_rtx;
11867 expand_assignment (lhs, rhs, false);
11868 return const0_rtx;
11871 case ADDR_EXPR:
11872 return expand_expr_addr_expr (exp, target, tmode, modifier);
11874 case REALPART_EXPR:
11875 op0 = expand_normal (treeop0);
11876 return read_complex_part (op0, false);
11878 case IMAGPART_EXPR:
11879 op0 = expand_normal (treeop0);
11880 return read_complex_part (op0, true);
11882 case RETURN_EXPR:
11883 case LABEL_EXPR:
11884 case GOTO_EXPR:
11885 case SWITCH_EXPR:
11886 case ASM_EXPR:
11887 /* Expanded in cfgexpand.cc. */
11888 gcc_unreachable ();
11890 case TRY_CATCH_EXPR:
11891 case CATCH_EXPR:
11892 case EH_FILTER_EXPR:
11893 case TRY_FINALLY_EXPR:
11894 case EH_ELSE_EXPR:
11895 /* Lowered by tree-eh.cc. */
11896 gcc_unreachable ();
11898 case WITH_CLEANUP_EXPR:
11899 case CLEANUP_POINT_EXPR:
11900 case TARGET_EXPR:
11901 case CASE_LABEL_EXPR:
11902 case VA_ARG_EXPR:
11903 case BIND_EXPR:
11904 case INIT_EXPR:
11905 case CONJ_EXPR:
11906 case COMPOUND_EXPR:
11907 case PREINCREMENT_EXPR:
11908 case PREDECREMENT_EXPR:
11909 case POSTINCREMENT_EXPR:
11910 case POSTDECREMENT_EXPR:
11911 case LOOP_EXPR:
11912 case EXIT_EXPR:
11913 case COMPOUND_LITERAL_EXPR:
11914 /* Lowered by gimplify.cc. */
11915 gcc_unreachable ();
11917 case FDESC_EXPR:
11918 /* Function descriptors are not valid except for as
11919 initialization constants, and should not be expanded. */
11920 gcc_unreachable ();
11922 case WITH_SIZE_EXPR:
11923 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11924 have pulled out the size to use in whatever context it needed. */
11925 return expand_expr_real (treeop0, original_target, tmode,
11926 modifier, alt_rtl, inner_reference_p);
11928 default:
11929 return expand_expr_real_2 (&ops, target, tmode, modifier);
11933 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11934 signedness of TYPE), possibly returning the result in TARGET.
11935 TYPE is known to be a partial integer type. */
11936 static rtx
11937 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11939 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11940 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11941 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11942 && (!target || GET_MODE (target) == mode));
11944 /* For constant values, reduce using wide_int_to_tree. */
11945 if (poly_int_rtx_p (exp))
11947 auto value = wi::to_poly_wide (exp, mode);
11948 tree t = wide_int_to_tree (type, value);
11949 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11951 else if (TYPE_UNSIGNED (type))
11953 rtx mask = immed_wide_int_const
11954 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11955 return expand_and (mode, exp, mask, target);
11957 else
11959 int count = GET_MODE_PRECISION (mode) - prec;
11960 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11961 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11965 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11966 when applied to the address of EXP produces an address known to be
11967 aligned more than BIGGEST_ALIGNMENT. */
11969 static int
11970 is_aligning_offset (const_tree offset, const_tree exp)
11972 /* Strip off any conversions. */
11973 while (CONVERT_EXPR_P (offset))
11974 offset = TREE_OPERAND (offset, 0);
11976 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11977 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11978 if (TREE_CODE (offset) != BIT_AND_EXPR
11979 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11980 || compare_tree_int (TREE_OPERAND (offset, 1),
11981 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11982 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11983 return 0;
11985 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11986 It must be NEGATE_EXPR. Then strip any more conversions. */
11987 offset = TREE_OPERAND (offset, 0);
11988 while (CONVERT_EXPR_P (offset))
11989 offset = TREE_OPERAND (offset, 0);
11991 if (TREE_CODE (offset) != NEGATE_EXPR)
11992 return 0;
11994 offset = TREE_OPERAND (offset, 0);
11995 while (CONVERT_EXPR_P (offset))
11996 offset = TREE_OPERAND (offset, 0);
11998 /* This must now be the address of EXP. */
11999 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12002 /* Return a STRING_CST corresponding to ARG's constant initializer either
12003 if it's a string constant, or, when VALREP is set, any other constant,
12004 or null otherwise.
12005 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12006 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12007 to the size of the byte string. If nonnull, set *DECL to the constant
12008 declaration ARG refers to. */
12010 static tree
12011 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12012 bool valrep = false)
12014 tree dummy = NULL_TREE;
12015 if (!mem_size)
12016 mem_size = &dummy;
12018 /* Store the type of the original expression before conversions
12019 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12020 removed. */
12021 tree argtype = TREE_TYPE (arg);
12023 tree array;
12024 STRIP_NOPS (arg);
12026 /* Non-constant index into the character array in an ARRAY_REF
12027 expression or null. */
12028 tree varidx = NULL_TREE;
12030 poly_int64 base_off = 0;
12032 if (TREE_CODE (arg) == ADDR_EXPR)
12034 arg = TREE_OPERAND (arg, 0);
12035 tree ref = arg;
12036 if (TREE_CODE (arg) == ARRAY_REF)
12038 tree idx = TREE_OPERAND (arg, 1);
12039 if (TREE_CODE (idx) != INTEGER_CST)
12041 /* From a pointer (but not array) argument extract the variable
12042 index to prevent get_addr_base_and_unit_offset() from failing
12043 due to it. Use it later to compute the non-constant offset
12044 into the string and return it to the caller. */
12045 varidx = idx;
12046 ref = TREE_OPERAND (arg, 0);
12048 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12049 return NULL_TREE;
12051 if (!integer_zerop (array_ref_low_bound (arg)))
12052 return NULL_TREE;
12054 if (!integer_onep (array_ref_element_size (arg)))
12055 return NULL_TREE;
12058 array = get_addr_base_and_unit_offset (ref, &base_off);
12059 if (!array
12060 || (TREE_CODE (array) != VAR_DECL
12061 && TREE_CODE (array) != CONST_DECL
12062 && TREE_CODE (array) != STRING_CST))
12063 return NULL_TREE;
12065 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12067 tree arg0 = TREE_OPERAND (arg, 0);
12068 tree arg1 = TREE_OPERAND (arg, 1);
12070 tree offset;
12071 tree str = string_constant (arg0, &offset, mem_size, decl);
12072 if (!str)
12074 str = string_constant (arg1, &offset, mem_size, decl);
12075 arg1 = arg0;
12078 if (str)
12080 /* Avoid pointers to arrays (see bug 86622). */
12081 if (POINTER_TYPE_P (TREE_TYPE (arg))
12082 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12083 && !(decl && !*decl)
12084 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12085 && tree_fits_uhwi_p (*mem_size)
12086 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12087 return NULL_TREE;
12089 tree type = TREE_TYPE (offset);
12090 arg1 = fold_convert (type, arg1);
12091 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12092 return str;
12094 return NULL_TREE;
12096 else if (TREE_CODE (arg) == SSA_NAME)
12098 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12099 if (!is_gimple_assign (stmt))
12100 return NULL_TREE;
12102 tree rhs1 = gimple_assign_rhs1 (stmt);
12103 tree_code code = gimple_assign_rhs_code (stmt);
12104 if (code == ADDR_EXPR)
12105 return string_constant (rhs1, ptr_offset, mem_size, decl);
12106 else if (code != POINTER_PLUS_EXPR)
12107 return NULL_TREE;
12109 tree offset;
12110 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12112 /* Avoid pointers to arrays (see bug 86622). */
12113 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12114 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12115 && !(decl && !*decl)
12116 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12117 && tree_fits_uhwi_p (*mem_size)
12118 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12119 return NULL_TREE;
12121 tree rhs2 = gimple_assign_rhs2 (stmt);
12122 tree type = TREE_TYPE (offset);
12123 rhs2 = fold_convert (type, rhs2);
12124 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12125 return str;
12127 return NULL_TREE;
12129 else if (DECL_P (arg))
12130 array = arg;
12131 else
12132 return NULL_TREE;
12134 tree offset = wide_int_to_tree (sizetype, base_off);
12135 if (varidx)
12137 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12138 return NULL_TREE;
12140 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12141 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12142 if (TREE_CODE (chartype) != INTEGER_TYPE)
12143 return NULL;
12145 offset = fold_convert (sizetype, varidx);
12148 if (TREE_CODE (array) == STRING_CST)
12150 *ptr_offset = fold_convert (sizetype, offset);
12151 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12152 if (decl)
12153 *decl = NULL_TREE;
12154 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12155 >= TREE_STRING_LENGTH (array));
12156 return array;
12159 tree init = ctor_for_folding (array);
12160 if (!init || init == error_mark_node)
12161 return NULL_TREE;
12163 if (valrep)
12165 HOST_WIDE_INT cstoff;
12166 if (!base_off.is_constant (&cstoff))
12167 return NULL_TREE;
12169 /* Check that the host and target are sane. */
12170 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12171 return NULL_TREE;
12173 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12174 if (typesz <= 0 || (int) typesz != typesz)
12175 return NULL_TREE;
12177 HOST_WIDE_INT size = typesz;
12178 if (VAR_P (array)
12179 && DECL_SIZE_UNIT (array)
12180 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12182 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12183 gcc_checking_assert (size >= typesz);
12186 /* If value representation was requested convert the initializer
12187 for the whole array or object into a string of bytes forming
12188 its value representation and return it. */
12189 unsigned char *bytes = XNEWVEC (unsigned char, size);
12190 int r = native_encode_initializer (init, bytes, size);
12191 if (r < typesz)
12193 XDELETEVEC (bytes);
12194 return NULL_TREE;
12197 if (r < size)
12198 memset (bytes + r, '\0', size - r);
12200 const char *p = reinterpret_cast<const char *>(bytes);
12201 init = build_string_literal (size, p, char_type_node);
12202 init = TREE_OPERAND (init, 0);
12203 init = TREE_OPERAND (init, 0);
12204 XDELETE (bytes);
12206 *mem_size = size_int (TREE_STRING_LENGTH (init));
12207 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12209 if (decl)
12210 *decl = array;
12212 return init;
12215 if (TREE_CODE (init) == CONSTRUCTOR)
12217 /* Convert the 64-bit constant offset to a wider type to avoid
12218 overflow and use it to obtain the initializer for the subobject
12219 it points into. */
12220 offset_int wioff;
12221 if (!base_off.is_constant (&wioff))
12222 return NULL_TREE;
12224 wioff *= BITS_PER_UNIT;
12225 if (!wi::fits_uhwi_p (wioff))
12226 return NULL_TREE;
12228 base_off = wioff.to_uhwi ();
12229 unsigned HOST_WIDE_INT fieldoff = 0;
12230 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12231 &fieldoff);
12232 if (!init || init == error_mark_node)
12233 return NULL_TREE;
12235 HOST_WIDE_INT cstoff;
12236 if (!base_off.is_constant (&cstoff))
12237 return NULL_TREE;
12239 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12240 tree off = build_int_cst (sizetype, cstoff);
12241 if (varidx)
12242 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12243 else
12244 offset = off;
12247 *ptr_offset = offset;
12249 tree inittype = TREE_TYPE (init);
12251 if (TREE_CODE (init) == INTEGER_CST
12252 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12253 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12255 /* Check that the host and target are sane. */
12256 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12257 return NULL_TREE;
12259 /* For a reference to (address of) a single constant character,
12260 store the native representation of the character in CHARBUF.
12261 If the reference is to an element of an array or a member
12262 of a struct, only consider narrow characters until ctors
12263 for wide character arrays are transformed to STRING_CSTs
12264 like those for narrow arrays. */
12265 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12266 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12267 if (len > 0)
12269 /* Construct a string literal with elements of INITTYPE and
12270 the representation above. Then strip
12271 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12272 init = build_string_literal (len, (char *)charbuf, inittype);
12273 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12277 tree initsize = TYPE_SIZE_UNIT (inittype);
12279 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12281 /* Fold an empty/zero constructor for an implicitly initialized
12282 object or subobject into the empty string. */
12284 /* Determine the character type from that of the original
12285 expression. */
12286 tree chartype = argtype;
12287 if (POINTER_TYPE_P (chartype))
12288 chartype = TREE_TYPE (chartype);
12289 while (TREE_CODE (chartype) == ARRAY_TYPE)
12290 chartype = TREE_TYPE (chartype);
12292 if (INTEGRAL_TYPE_P (chartype)
12293 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12295 /* Convert a char array to an empty STRING_CST having an array
12296 of the expected type and size. */
12297 if (!initsize)
12298 initsize = integer_zero_node;
12300 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12301 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12302 return NULL_TREE;
12304 init = build_string_literal (size, NULL, chartype, size);
12305 init = TREE_OPERAND (init, 0);
12306 init = TREE_OPERAND (init, 0);
12308 *ptr_offset = integer_zero_node;
12312 if (decl)
12313 *decl = array;
12315 if (TREE_CODE (init) != STRING_CST)
12316 return NULL_TREE;
12318 *mem_size = initsize;
12320 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12322 return init;
12325 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12326 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12327 non-constant) offset in bytes within the string that ARG is accessing.
12328 If MEM_SIZE is non-zero the storage size of the memory is returned.
12329 If DECL is non-zero the constant declaration is returned if available. */
12331 tree
12332 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12334 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12337 /* Similar to string_constant, return a STRING_CST corresponding
12338 to the value representation of the first argument if it's
12339 a constant. */
12341 tree
12342 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12344 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12347 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12348 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12349 for C2 > 0 to x & C3 == C2
12350 for C2 < 0 to x & C3 == (C2 & C3). */
12351 enum tree_code
12352 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12354 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12355 tree treeop0 = gimple_assign_rhs1 (stmt);
12356 tree treeop1 = gimple_assign_rhs2 (stmt);
12357 tree type = TREE_TYPE (*arg0);
12358 scalar_int_mode mode;
12359 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12360 return code;
12361 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12362 || TYPE_PRECISION (type) <= 1
12363 || TYPE_UNSIGNED (type)
12364 /* Signed x % c == 0 should have been optimized into unsigned modulo
12365 earlier. */
12366 || integer_zerop (*arg1)
12367 /* If c is known to be non-negative, modulo will be expanded as unsigned
12368 modulo. */
12369 || get_range_pos_neg (treeop0) == 1)
12370 return code;
12372 /* x % c == d where d < 0 && d <= -c should be always false. */
12373 if (tree_int_cst_sgn (*arg1) == -1
12374 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12375 return code;
12377 int prec = TYPE_PRECISION (type);
12378 wide_int w = wi::to_wide (treeop1) - 1;
12379 w |= wi::shifted_mask (0, prec - 1, true, prec);
12380 tree c3 = wide_int_to_tree (type, w);
12381 tree c4 = *arg1;
12382 if (tree_int_cst_sgn (*arg1) == -1)
12383 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12385 rtx op0 = expand_normal (treeop0);
12386 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12388 bool speed_p = optimize_insn_for_speed_p ();
12390 do_pending_stack_adjust ();
12392 location_t loc = gimple_location (stmt);
12393 struct separate_ops ops;
12394 ops.code = TRUNC_MOD_EXPR;
12395 ops.location = loc;
12396 ops.type = TREE_TYPE (treeop0);
12397 ops.op0 = treeop0;
12398 ops.op1 = treeop1;
12399 ops.op2 = NULL_TREE;
12400 start_sequence ();
12401 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12402 EXPAND_NORMAL);
12403 rtx_insn *moinsns = get_insns ();
12404 end_sequence ();
12406 unsigned mocost = seq_cost (moinsns, speed_p);
12407 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12408 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12410 ops.code = BIT_AND_EXPR;
12411 ops.location = loc;
12412 ops.type = TREE_TYPE (treeop0);
12413 ops.op0 = treeop0;
12414 ops.op1 = c3;
12415 ops.op2 = NULL_TREE;
12416 start_sequence ();
12417 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12418 EXPAND_NORMAL);
12419 rtx_insn *muinsns = get_insns ();
12420 end_sequence ();
12422 unsigned mucost = seq_cost (muinsns, speed_p);
12423 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12424 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12426 if (mocost <= mucost)
12428 emit_insn (moinsns);
12429 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12430 return code;
12433 emit_insn (muinsns);
12434 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12435 *arg1 = c4;
12436 return code;
12439 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12440 If C1 is odd to:
12441 (X - C2) * C3 <= C4 (or >), where
12442 C3 is modular multiplicative inverse of C1 and 1<<prec and
12443 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12444 if C2 > ((1<<prec) - 1) % C1).
12445 If C1 is even, S = ctz (C1) and C2 is 0, use
12446 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12447 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12449 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12450 unsigned):
12451 (X * C3) + C4 <= 2 * C4, where
12452 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12453 C4 is ((1<<(prec - 1) - 1) / C1).
12454 If C1 is even, S = ctz(C1), use
12455 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12456 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12457 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12459 See the Hacker's Delight book, section 10-17. */
12460 enum tree_code
12461 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12463 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12464 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12466 if (optimize < 2)
12467 return code;
12469 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12470 if (stmt == NULL)
12471 return code;
12473 tree treeop0 = gimple_assign_rhs1 (stmt);
12474 tree treeop1 = gimple_assign_rhs2 (stmt);
12475 if (TREE_CODE (treeop0) != SSA_NAME
12476 || TREE_CODE (treeop1) != INTEGER_CST
12477 /* Don't optimize the undefined behavior case x % 0;
12478 x % 1 should have been optimized into zero, punt if
12479 it makes it here for whatever reason;
12480 x % -c should have been optimized into x % c. */
12481 || compare_tree_int (treeop1, 2) <= 0
12482 /* Likewise x % c == d where d >= c should be always false. */
12483 || tree_int_cst_le (treeop1, *arg1))
12484 return code;
12486 /* Unsigned x % pow2 is handled right already, for signed
12487 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12488 if (integer_pow2p (treeop1))
12489 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12491 tree type = TREE_TYPE (*arg0);
12492 scalar_int_mode mode;
12493 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12494 return code;
12495 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12496 || TYPE_PRECISION (type) <= 1)
12497 return code;
12499 signop sgn = UNSIGNED;
12500 /* If both operands are known to have the sign bit clear, handle
12501 even the signed modulo case as unsigned. treeop1 is always
12502 positive >= 2, checked above. */
12503 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12504 sgn = SIGNED;
12506 if (!TYPE_UNSIGNED (type))
12508 if (tree_int_cst_sgn (*arg1) == -1)
12509 return code;
12510 type = unsigned_type_for (type);
12511 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12512 return code;
12515 int prec = TYPE_PRECISION (type);
12516 wide_int w = wi::to_wide (treeop1);
12517 int shift = wi::ctz (w);
12518 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12519 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12520 If C1 is odd, we can handle all cases by subtracting
12521 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12522 e.g. by testing for overflow on the subtraction, punt on that for now
12523 though. */
12524 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12526 if (sgn == SIGNED)
12527 return code;
12528 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12529 if (wi::gtu_p (wi::to_wide (*arg1), x))
12530 return code;
12533 imm_use_iterator imm_iter;
12534 use_operand_p use_p;
12535 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12537 gimple *use_stmt = USE_STMT (use_p);
12538 /* Punt if treeop0 is used in the same bb in a division
12539 or another modulo with the same divisor. We should expect
12540 the division and modulo combined together. */
12541 if (use_stmt == stmt
12542 || gimple_bb (use_stmt) != gimple_bb (stmt))
12543 continue;
12544 if (!is_gimple_assign (use_stmt)
12545 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12546 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12547 continue;
12548 if (gimple_assign_rhs1 (use_stmt) != treeop0
12549 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12550 continue;
12551 return code;
12554 w = wi::lrshift (w, shift);
12555 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12556 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12557 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12558 tree c3 = wide_int_to_tree (type, m);
12559 tree c5 = NULL_TREE;
12560 wide_int d, e;
12561 if (sgn == UNSIGNED)
12563 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12564 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12565 otherwise use < or subtract one from C4. E.g. for
12566 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12567 x % 3U == 1 already needs to be
12568 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12569 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12570 d -= 1;
12571 if (shift)
12572 d = wi::lrshift (d, shift);
12574 else
12576 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12577 if (!shift)
12578 d = wi::lshift (e, 1);
12579 else
12581 e = wi::bit_and (e, wi::mask (shift, true, prec));
12582 d = wi::lrshift (e, shift - 1);
12584 c5 = wide_int_to_tree (type, e);
12586 tree c4 = wide_int_to_tree (type, d);
12588 rtx op0 = expand_normal (treeop0);
12589 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12591 bool speed_p = optimize_insn_for_speed_p ();
12593 do_pending_stack_adjust ();
12595 location_t loc = gimple_location (stmt);
12596 struct separate_ops ops;
12597 ops.code = TRUNC_MOD_EXPR;
12598 ops.location = loc;
12599 ops.type = TREE_TYPE (treeop0);
12600 ops.op0 = treeop0;
12601 ops.op1 = treeop1;
12602 ops.op2 = NULL_TREE;
12603 start_sequence ();
12604 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12605 EXPAND_NORMAL);
12606 rtx_insn *moinsns = get_insns ();
12607 end_sequence ();
12609 unsigned mocost = seq_cost (moinsns, speed_p);
12610 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12611 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12613 tree t = fold_convert_loc (loc, type, treeop0);
12614 if (!integer_zerop (*arg1))
12615 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12616 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12617 if (sgn == SIGNED)
12618 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12619 if (shift)
12621 tree s = build_int_cst (NULL_TREE, shift);
12622 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12625 start_sequence ();
12626 rtx mur = expand_normal (t);
12627 rtx_insn *muinsns = get_insns ();
12628 end_sequence ();
12630 unsigned mucost = seq_cost (muinsns, speed_p);
12631 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12632 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12634 if (mocost <= mucost)
12636 emit_insn (moinsns);
12637 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12638 return code;
12641 emit_insn (muinsns);
12642 *arg0 = make_tree (type, mur);
12643 *arg1 = c4;
12644 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12647 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12649 void
12650 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12652 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12653 || code == LT_EXPR || code == LE_EXPR);
12654 gcc_checking_assert (integer_zerop (*arg1));
12656 if (!optimize)
12657 return;
12659 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12660 if (stmt == NULL)
12661 return;
12663 tree treeop0 = gimple_assign_rhs1 (stmt);
12664 tree treeop1 = gimple_assign_rhs2 (stmt);
12665 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12666 return;
12668 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12669 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12670 "assuming signed overflow does not occur when "
12671 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12672 op_symbol_code (code), op_symbol_code (code));
12674 *arg0 = treeop0;
12675 *arg1 = treeop1;
12678 /* Generate code to calculate OPS, and exploded expression
12679 using a store-flag instruction and return an rtx for the result.
12680 OPS reflects a comparison.
12682 If TARGET is nonzero, store the result there if convenient.
12684 Return zero if there is no suitable set-flag instruction
12685 available on this machine.
12687 Once expand_expr has been called on the arguments of the comparison,
12688 we are committed to doing the store flag, since it is not safe to
12689 re-evaluate the expression. We emit the store-flag insn by calling
12690 emit_store_flag, but only expand the arguments if we have a reason
12691 to believe that emit_store_flag will be successful. If we think that
12692 it will, but it isn't, we have to simulate the store-flag with a
12693 set/jump/set sequence. */
12695 static rtx
12696 do_store_flag (sepops ops, rtx target, machine_mode mode)
12698 enum rtx_code code;
12699 tree arg0, arg1, type;
12700 machine_mode operand_mode;
12701 int unsignedp;
12702 rtx op0, op1;
12703 rtx subtarget = target;
12704 location_t loc = ops->location;
12706 arg0 = ops->op0;
12707 arg1 = ops->op1;
12709 /* Don't crash if the comparison was erroneous. */
12710 if (arg0 == error_mark_node || arg1 == error_mark_node)
12711 return const0_rtx;
12713 type = TREE_TYPE (arg0);
12714 operand_mode = TYPE_MODE (type);
12715 unsignedp = TYPE_UNSIGNED (type);
12717 /* We won't bother with BLKmode store-flag operations because it would mean
12718 passing a lot of information to emit_store_flag. */
12719 if (operand_mode == BLKmode)
12720 return 0;
12722 /* We won't bother with store-flag operations involving function pointers
12723 when function pointers must be canonicalized before comparisons. */
12724 if (targetm.have_canonicalize_funcptr_for_compare ()
12725 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12726 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12727 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12728 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12729 return 0;
12731 STRIP_NOPS (arg0);
12732 STRIP_NOPS (arg1);
12734 /* For vector typed comparisons emit code to generate the desired
12735 all-ones or all-zeros mask. */
12736 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12738 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12739 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12740 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12741 return expand_vec_cmp_expr (ops->type, ifexp, target);
12742 else
12743 gcc_unreachable ();
12746 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12747 into (x - C2) * C3 < C4. */
12748 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12749 && TREE_CODE (arg0) == SSA_NAME
12750 && TREE_CODE (arg1) == INTEGER_CST)
12752 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12753 &arg0, &arg1);
12754 if (new_code != ops->code)
12756 struct separate_ops nops = *ops;
12757 nops.code = ops->code = new_code;
12758 nops.op0 = arg0;
12759 nops.op1 = arg1;
12760 nops.type = TREE_TYPE (arg0);
12761 return do_store_flag (&nops, target, mode);
12765 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12766 if (!unsignedp
12767 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12768 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12769 && integer_zerop (arg1)
12770 && TREE_CODE (arg0) == SSA_NAME)
12771 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12773 /* Get the rtx comparison code to use. We know that EXP is a comparison
12774 operation of some type. Some comparisons against 1 and -1 can be
12775 converted to comparisons with zero. Do so here so that the tests
12776 below will be aware that we have a comparison with zero. These
12777 tests will not catch constants in the first operand, but constants
12778 are rarely passed as the first operand. */
12780 switch (ops->code)
12782 case EQ_EXPR:
12783 code = EQ;
12784 break;
12785 case NE_EXPR:
12786 code = NE;
12787 break;
12788 case LT_EXPR:
12789 if (integer_onep (arg1))
12790 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12791 else
12792 code = unsignedp ? LTU : LT;
12793 break;
12794 case LE_EXPR:
12795 if (! unsignedp && integer_all_onesp (arg1))
12796 arg1 = integer_zero_node, code = LT;
12797 else
12798 code = unsignedp ? LEU : LE;
12799 break;
12800 case GT_EXPR:
12801 if (! unsignedp && integer_all_onesp (arg1))
12802 arg1 = integer_zero_node, code = GE;
12803 else
12804 code = unsignedp ? GTU : GT;
12805 break;
12806 case GE_EXPR:
12807 if (integer_onep (arg1))
12808 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12809 else
12810 code = unsignedp ? GEU : GE;
12811 break;
12813 case UNORDERED_EXPR:
12814 code = UNORDERED;
12815 break;
12816 case ORDERED_EXPR:
12817 code = ORDERED;
12818 break;
12819 case UNLT_EXPR:
12820 code = UNLT;
12821 break;
12822 case UNLE_EXPR:
12823 code = UNLE;
12824 break;
12825 case UNGT_EXPR:
12826 code = UNGT;
12827 break;
12828 case UNGE_EXPR:
12829 code = UNGE;
12830 break;
12831 case UNEQ_EXPR:
12832 code = UNEQ;
12833 break;
12834 case LTGT_EXPR:
12835 code = LTGT;
12836 break;
12838 default:
12839 gcc_unreachable ();
12842 /* Put a constant second. */
12843 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12844 || TREE_CODE (arg0) == FIXED_CST)
12846 std::swap (arg0, arg1);
12847 code = swap_condition (code);
12850 /* If this is an equality or inequality test of a single bit, we can
12851 do this by shifting the bit being tested to the low-order bit and
12852 masking the result with the constant 1. If the condition was EQ,
12853 we xor it with 1. This does not require an scc insn and is faster
12854 than an scc insn even if we have it.
12856 The code to make this transformation was moved into fold_single_bit_test,
12857 so we just call into the folder and expand its result. */
12859 if ((code == NE || code == EQ)
12860 && integer_zerop (arg1)
12861 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12863 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12864 if (srcstmt
12865 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12867 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12868 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12869 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12870 gimple_assign_rhs1 (srcstmt),
12871 gimple_assign_rhs2 (srcstmt));
12872 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12873 if (temp)
12874 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12878 if (! get_subtarget (target)
12879 || GET_MODE (subtarget) != operand_mode)
12880 subtarget = 0;
12882 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12884 if (target == 0)
12885 target = gen_reg_rtx (mode);
12887 /* Try a cstore if possible. */
12888 return emit_store_flag_force (target, code, op0, op1,
12889 operand_mode, unsignedp,
12890 (TYPE_PRECISION (ops->type) == 1
12891 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12894 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12895 0 otherwise (i.e. if there is no casesi instruction).
12897 DEFAULT_PROBABILITY is the probability of jumping to the default
12898 label. */
12900 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12901 rtx table_label, rtx default_label, rtx fallback_label,
12902 profile_probability default_probability)
12904 class expand_operand ops[5];
12905 scalar_int_mode index_mode = SImode;
12906 rtx op1, op2, index;
12908 if (! targetm.have_casesi ())
12909 return 0;
12911 /* The index must be some form of integer. Convert it to SImode. */
12912 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12913 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12915 rtx rangertx = expand_normal (range);
12917 /* We must handle the endpoints in the original mode. */
12918 index_expr = build2 (MINUS_EXPR, index_type,
12919 index_expr, minval);
12920 minval = integer_zero_node;
12921 index = expand_normal (index_expr);
12922 if (default_label)
12923 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12924 omode, 1, default_label,
12925 default_probability);
12926 /* Now we can safely truncate. */
12927 index = convert_to_mode (index_mode, index, 0);
12929 else
12931 if (omode != index_mode)
12933 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12934 index_expr = fold_convert (index_type, index_expr);
12937 index = expand_normal (index_expr);
12940 do_pending_stack_adjust ();
12942 op1 = expand_normal (minval);
12943 op2 = expand_normal (range);
12945 create_input_operand (&ops[0], index, index_mode);
12946 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12947 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12948 create_fixed_operand (&ops[3], table_label);
12949 create_fixed_operand (&ops[4], (default_label
12950 ? default_label
12951 : fallback_label));
12952 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12953 return 1;
12956 /* Attempt to generate a tablejump instruction; same concept. */
12957 /* Subroutine of the next function.
12959 INDEX is the value being switched on, with the lowest value
12960 in the table already subtracted.
12961 MODE is its expected mode (needed if INDEX is constant).
12962 RANGE is the length of the jump table.
12963 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12965 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12966 index value is out of range.
12967 DEFAULT_PROBABILITY is the probability of jumping to
12968 the default label. */
12970 static void
12971 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12972 rtx default_label, profile_probability default_probability)
12974 rtx temp, vector;
12976 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12977 cfun->cfg->max_jumptable_ents = INTVAL (range);
12979 /* Do an unsigned comparison (in the proper mode) between the index
12980 expression and the value which represents the length of the range.
12981 Since we just finished subtracting the lower bound of the range
12982 from the index expression, this comparison allows us to simultaneously
12983 check that the original index expression value is both greater than
12984 or equal to the minimum value of the range and less than or equal to
12985 the maximum value of the range. */
12987 if (default_label)
12988 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12989 default_label, default_probability);
12991 /* If index is in range, it must fit in Pmode.
12992 Convert to Pmode so we can index with it. */
12993 if (mode != Pmode)
12995 unsigned int width;
12997 /* We know the value of INDEX is between 0 and RANGE. If we have a
12998 sign-extended subreg, and RANGE does not have the sign bit set, then
12999 we have a value that is valid for both sign and zero extension. In
13000 this case, we get better code if we sign extend. */
13001 if (GET_CODE (index) == SUBREG
13002 && SUBREG_PROMOTED_VAR_P (index)
13003 && SUBREG_PROMOTED_SIGNED_P (index)
13004 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13005 <= HOST_BITS_PER_WIDE_INT)
13006 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13007 index = convert_to_mode (Pmode, index, 0);
13008 else
13009 index = convert_to_mode (Pmode, index, 1);
13012 /* Don't let a MEM slip through, because then INDEX that comes
13013 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13014 and break_out_memory_refs will go to work on it and mess it up. */
13015 #ifdef PIC_CASE_VECTOR_ADDRESS
13016 if (flag_pic && !REG_P (index))
13017 index = copy_to_mode_reg (Pmode, index);
13018 #endif
13020 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13021 GET_MODE_SIZE, because this indicates how large insns are. The other
13022 uses should all be Pmode, because they are addresses. This code
13023 could fail if addresses and insns are not the same size. */
13024 index = simplify_gen_binary (MULT, Pmode, index,
13025 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13026 Pmode));
13027 index = simplify_gen_binary (PLUS, Pmode, index,
13028 gen_rtx_LABEL_REF (Pmode, table_label));
13030 #ifdef PIC_CASE_VECTOR_ADDRESS
13031 if (flag_pic)
13032 index = PIC_CASE_VECTOR_ADDRESS (index);
13033 else
13034 #endif
13035 index = memory_address (CASE_VECTOR_MODE, index);
13036 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13037 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13038 convert_move (temp, vector, 0);
13040 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13042 /* If we are generating PIC code or if the table is PC-relative, the
13043 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13044 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13045 emit_barrier ();
13049 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13050 rtx table_label, rtx default_label,
13051 profile_probability default_probability)
13053 rtx index;
13055 if (! targetm.have_tablejump ())
13056 return 0;
13058 index_expr = fold_build2 (MINUS_EXPR, index_type,
13059 fold_convert (index_type, index_expr),
13060 fold_convert (index_type, minval));
13061 index = expand_normal (index_expr);
13062 do_pending_stack_adjust ();
13064 do_tablejump (index, TYPE_MODE (index_type),
13065 convert_modes (TYPE_MODE (index_type),
13066 TYPE_MODE (TREE_TYPE (range)),
13067 expand_normal (range),
13068 TYPE_UNSIGNED (TREE_TYPE (range))),
13069 table_label, default_label, default_probability);
13070 return 1;
13073 /* Return a CONST_VECTOR rtx representing vector mask for
13074 a VECTOR_CST of booleans. */
13075 static rtx
13076 const_vector_mask_from_tree (tree exp)
13078 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13079 machine_mode inner = GET_MODE_INNER (mode);
13081 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13082 VECTOR_CST_NELTS_PER_PATTERN (exp));
13083 unsigned int count = builder.encoded_nelts ();
13084 for (unsigned int i = 0; i < count; ++i)
13086 tree elt = VECTOR_CST_ELT (exp, i);
13087 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13088 if (integer_zerop (elt))
13089 builder.quick_push (CONST0_RTX (inner));
13090 else if (integer_onep (elt)
13091 || integer_minus_onep (elt))
13092 builder.quick_push (CONSTM1_RTX (inner));
13093 else
13094 gcc_unreachable ();
13096 return builder.build ();
13099 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13100 static rtx
13101 const_vector_from_tree (tree exp)
13103 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13105 if (initializer_zerop (exp))
13106 return CONST0_RTX (mode);
13108 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13109 return const_vector_mask_from_tree (exp);
13111 machine_mode inner = GET_MODE_INNER (mode);
13113 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13114 VECTOR_CST_NELTS_PER_PATTERN (exp));
13115 unsigned int count = builder.encoded_nelts ();
13116 for (unsigned int i = 0; i < count; ++i)
13118 tree elt = VECTOR_CST_ELT (exp, i);
13119 if (TREE_CODE (elt) == REAL_CST)
13120 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13121 inner));
13122 else if (TREE_CODE (elt) == FIXED_CST)
13123 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13124 inner));
13125 else
13126 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13127 inner));
13129 return builder.build ();
13132 /* Build a decl for a personality function given a language prefix. */
13134 tree
13135 build_personality_function (const char *lang)
13137 const char *unwind_and_version;
13138 tree decl, type;
13139 char *name;
13141 switch (targetm_common.except_unwind_info (&global_options))
13143 case UI_NONE:
13144 return NULL;
13145 case UI_SJLJ:
13146 unwind_and_version = "_sj0";
13147 break;
13148 case UI_DWARF2:
13149 case UI_TARGET:
13150 unwind_and_version = "_v0";
13151 break;
13152 case UI_SEH:
13153 unwind_and_version = "_seh0";
13154 break;
13155 default:
13156 gcc_unreachable ();
13159 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13161 type = build_function_type_list (unsigned_type_node,
13162 integer_type_node, integer_type_node,
13163 long_long_unsigned_type_node,
13164 ptr_type_node, ptr_type_node, NULL_TREE);
13165 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13166 get_identifier (name), type);
13167 DECL_ARTIFICIAL (decl) = 1;
13168 DECL_EXTERNAL (decl) = 1;
13169 TREE_PUBLIC (decl) = 1;
13171 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13172 are the flags assigned by targetm.encode_section_info. */
13173 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13175 return decl;
13178 /* Extracts the personality function of DECL and returns the corresponding
13179 libfunc. */
13182 get_personality_function (tree decl)
13184 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13185 enum eh_personality_kind pk;
13187 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13188 if (pk == eh_personality_none)
13189 return NULL;
13191 if (!personality
13192 && pk == eh_personality_any)
13193 personality = lang_hooks.eh_personality ();
13195 if (pk == eh_personality_lang)
13196 gcc_assert (personality != NULL_TREE);
13198 return XEXP (DECL_RTL (personality), 0);
13201 /* Returns a tree for the size of EXP in bytes. */
13203 static tree
13204 tree_expr_size (const_tree exp)
13206 if (DECL_P (exp)
13207 && DECL_SIZE_UNIT (exp) != 0)
13208 return DECL_SIZE_UNIT (exp);
13209 else
13210 return size_in_bytes (TREE_TYPE (exp));
13213 /* Return an rtx for the size in bytes of the value of EXP. */
13216 expr_size (tree exp)
13218 tree size;
13220 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13221 size = TREE_OPERAND (exp, 1);
13222 else
13224 size = tree_expr_size (exp);
13225 gcc_assert (size);
13226 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13229 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13232 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13233 if the size can vary or is larger than an integer. */
13235 HOST_WIDE_INT
13236 int_expr_size (const_tree exp)
13238 tree size;
13240 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13241 size = TREE_OPERAND (exp, 1);
13242 else
13244 size = tree_expr_size (exp);
13245 gcc_assert (size);
13248 if (size == 0 || !tree_fits_shwi_p (size))
13249 return -1;
13251 return tree_to_shwi (size);